package o;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
import android.os.SystemClock;
import com.google.android.gms.common.BlockingServiceConnection;
import com.google.android.gms.common.GoogleApiAvailabilityLight;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.common.stats.ConnectionTracker;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class X1 {
    public BlockingServiceConnection a;
    public Up0 b;
    public boolean c;
    public final Object d = new Object();
    public Go0 e;
    public final Context f;
    public final long g;

    public X1(Context context) {
        Preconditions.checkNotNull(context);
        Context applicationContext = context.getApplicationContext();
        this.f = applicationContext != null ? applicationContext : context;
        this.c = false;
        this.g = -1L;
    }

    public static W1 a(Context context) {
        X1 x1 = new X1(context);
        try {
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            x1.c();
            W1 w1E = x1.e();
            d(w1E, SystemClock.elapsedRealtime() - jElapsedRealtime, null);
            return w1E;
        } finally {
        }
    }

    public static void d(W1 w1, long j, Throwable th) {
        if (Math.random() <= 0.0d) {
            HashMap map = new HashMap();
            map.put("app_context", "1");
            if (w1 != null) {
                map.put("limit_ad_tracking", true != w1.b ? "0" : "1");
                String str = w1.c;
                if (str != null) {
                    map.put("ad_id_size", Integer.toString(str.length()));
                }
            }
            if (th != null) {
                map.put("error", th.getClass().getName());
            }
            map.put("tag", "AdvertisingIdClient");
            map.put("time_spent", Long.toString(j));
            new Jn0(map).start();
        }
    }

    public final void b() {
        Preconditions.checkNotMainThread("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f == null || this.a == null) {
                    return;
                }
                try {
                    if (this.c) {
                        ConnectionTracker.getInstance().unbindService(this.f, this.a);
                    }
                } catch (Throwable unused) {
                }
                this.c = false;
                this.b = null;
                this.a = null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void c() {
        Up0 c2562tp0;
        Preconditions.checkNotMainThread("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.c) {
                    b();
                }
                Context context = this.f;
                try {
                    context.getPackageManager().getPackageInfo("com.android.vending", 0);
                    int iIsGooglePlayServicesAvailable = GoogleApiAvailabilityLight.getInstance().isGooglePlayServicesAvailable(context, 12451000);
                    if (iIsGooglePlayServicesAvailable != 0 && iIsGooglePlayServicesAvailable != 2) {
                        throw new IOException("Google Play services not available");
                    }
                    BlockingServiceConnection blockingServiceConnection = new BlockingServiceConnection();
                    Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
                    intent.setPackage("com.google.android.gms");
                    try {
                        if (!ConnectionTracker.getInstance().bindService(context, intent, blockingServiceConnection, 1)) {
                            throw new IOException("Connection failure");
                        }
                        this.a = blockingServiceConnection;
                        try {
                            IBinder serviceWithTimeout = blockingServiceConnection.getServiceWithTimeout(10000L, TimeUnit.MILLISECONDS);
                            int i = Op0.d;
                            if (serviceWithTimeout == null) {
                                c2562tp0 = null;
                            } else {
                                IInterface iInterfaceQueryLocalInterface = serviceWithTimeout.queryLocalInterface("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                                c2562tp0 = iInterfaceQueryLocalInterface instanceof Up0 ? (Up0) iInterfaceQueryLocalInterface : new C2562tp0(serviceWithTimeout);
                            }
                            this.b = c2562tp0;
                            this.c = true;
                        } catch (InterruptedException unused) {
                            throw new IOException("Interrupted exception");
                        } catch (Throwable th) {
                            throw new IOException(th);
                        }
                    } finally {
                        IOException iOException = new IOException(th);
                    }
                } catch (PackageManager.NameNotFoundException unused2) {
                    throw new GooglePlayServicesNotAvailableException(9);
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final W1 e() {
        W1 w1;
        Preconditions.checkNotMainThread("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (!this.c) {
                    synchronized (this.d) {
                        Go0 go0 = this.e;
                        if (go0 == null || !go0.f137o) {
                            throw new IOException("AdvertisingIdClient is not connected.");
                        }
                    }
                    try {
                        c();
                        if (!this.c) {
                            throw new IOException("AdvertisingIdClient cannot reconnect.");
                        }
                    } catch (Exception e) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.", e);
                    }
                }
                Preconditions.checkNotNull(this.a);
                Preconditions.checkNotNull(this.b);
                try {
                    C2562tp0 c2562tp0 = (C2562tp0) this.b;
                    c2562tp0.getClass();
                    Parcel parcelObtain = Parcel.obtain();
                    parcelObtain.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    boolean z = true;
                    Parcel parcelB = c2562tp0.B(1, parcelObtain);
                    String string = parcelB.readString();
                    parcelB.recycle();
                    C2562tp0 c2562tp02 = (C2562tp0) this.b;
                    c2562tp02.getClass();
                    Parcel parcelObtain2 = Parcel.obtain();
                    parcelObtain2.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    int i = AbstractC0753ap0.a;
                    parcelObtain2.writeInt(1);
                    Parcel parcelB2 = c2562tp02.B(2, parcelObtain2);
                    if (parcelB2.readInt() == 0) {
                        z = false;
                    }
                    parcelB2.recycle();
                    w1 = new W1(string, z);
                } catch (RemoteException unused) {
                    throw new IOException("Remote exception");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        synchronized (this.d) {
            Go0 go02 = this.e;
            if (go02 != null) {
                go02.n.countDown();
                try {
                    this.e.join();
                } catch (InterruptedException unused2) {
                }
            }
            long j = this.g;
            if (j > 0) {
                this.e = new Go0(this, j);
            }
        }
        return w1;
    }

    public final void finalize() throws Throwable {
        b();
        super.finalize();
    }
}
