package L2;

import A0.j;
import D0.q;
import M2.f;
import P2.w;
import X2.d;
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 android.util.Log;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public final class b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public M2.a f3980a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public d f3981b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f3982c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Object f3983d = new Object();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public c f3984e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Context f3985f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final long f3986g;

    public b(Context context) {
        w.f(context);
        Context applicationContext = context.getApplicationContext();
        this.f3985f = applicationContext != null ? applicationContext : context;
        this.f3982c = false;
        this.f3986g = -1L;
    }

    public static a a(Context context) {
        b bVar = new b(context);
        try {
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            bVar.c();
            a aVarE = bVar.e();
            d(aVarE, SystemClock.elapsedRealtime() - jElapsedRealtime, null);
            return aVarE;
        } finally {
        }
    }

    public static void d(a aVar, long j8, Throwable th) {
        if (Math.random() <= 0.0d) {
            HashMap map = new HashMap();
            map.put("app_context", "1");
            if (aVar != null) {
                map.put("limit_ad_tracking", true != aVar.f3978b ? "0" : "1");
                String str = aVar.f3979c;
                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(j8));
            new j(map).start();
        }
    }

    public final void b() {
        w.e("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f3985f == null || this.f3980a == null) {
                    return;
                }
                try {
                    if (this.f3982c) {
                        S2.a.a().b(this.f3985f, this.f3980a);
                    }
                } catch (Throwable th) {
                    Log.i("AdvertisingIdClient", "AdvertisingIdClient unbindService failed.", th);
                }
                this.f3982c = false;
                this.f3981b = null;
                this.f3980a = null;
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final void c() {
        w.e("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f3982c) {
                    b();
                }
                Context context = this.f3985f;
                try {
                    context.getPackageManager().getPackageInfo("com.android.vending", 0);
                    int iB = f.f4167b.b(context, 12451000);
                    if (iB != 0 && iB != 2) {
                        throw new IOException("Google Play services not available");
                    }
                    M2.a aVar = new M2.a();
                    Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
                    intent.setPackage("com.google.android.gms");
                    try {
                        if (!S2.a.a().c(context, context.getClass().getName(), intent, aVar, 1, null)) {
                            throw new IOException("Connection failure");
                        }
                        this.f3980a = aVar;
                        try {
                            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                            IBinder iBinderA = aVar.a();
                            int i = X2.c.f7216e;
                            IInterface iInterfaceQueryLocalInterface = iBinderA.queryLocalInterface("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                            this.f3981b = iInterfaceQueryLocalInterface instanceof d ? (d) iInterfaceQueryLocalInterface : new X2.b(iBinderA);
                            this.f3982c = 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 q();
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final a e() {
        a aVar;
        w.e("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (!this.f3982c) {
                    synchronized (this.f3983d) {
                        c cVar = this.f3984e;
                        if (cVar == null || !cVar.f3990t) {
                            throw new IOException("AdvertisingIdClient is not connected.");
                        }
                    }
                    try {
                        c();
                        if (!this.f3982c) {
                            throw new IOException("AdvertisingIdClient cannot reconnect.");
                        }
                    } catch (Exception e8) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.", e8);
                    }
                }
                w.f(this.f3980a);
                w.f(this.f3981b);
                try {
                    X2.b bVar = (X2.b) this.f3981b;
                    bVar.getClass();
                    Parcel parcelObtain = Parcel.obtain();
                    parcelObtain.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    boolean z8 = true;
                    Parcel parcelC = bVar.c(parcelObtain, 1);
                    String string = parcelC.readString();
                    parcelC.recycle();
                    X2.b bVar2 = (X2.b) this.f3981b;
                    bVar2.getClass();
                    Parcel parcelObtain2 = Parcel.obtain();
                    parcelObtain2.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    int i = X2.a.f7214a;
                    parcelObtain2.writeInt(1);
                    Parcel parcelC2 = bVar2.c(parcelObtain2, 2);
                    if (parcelC2.readInt() == 0) {
                        z8 = false;
                    }
                    parcelC2.recycle();
                    aVar = new a(0, string, z8);
                } catch (RemoteException e9) {
                    Log.i("AdvertisingIdClient", "GMS remote exception ", e9);
                    throw new IOException("Remote exception");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        f();
        return aVar;
    }

    public final void f() {
        synchronized (this.f3983d) {
            c cVar = this.f3984e;
            if (cVar != null) {
                cVar.f3989s.countDown();
                try {
                    this.f3984e.join();
                } catch (InterruptedException unused) {
                }
            }
            long j8 = this.f3986g;
            if (j8 > 0) {
                this.f3984e = new c(this, j8);
            }
        }
    }

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