package com.google.android.gms.ads.identifier;

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 androidx.annotation.Nullable;
import androidx.media3.exoplayer.dash.DashMediaSource;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.internal.w;
import com.google.android.gms.common.util.VisibleForTesting;
import com.google.android.gms.measurement.internal.u1;
import e8.e;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.GuardedBy;
import m8.d;
import s1.h;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f7873b;

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

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

    @Nullable
    @GuardedBy("this")
    e8.a zza;

    @Nullable
    @GuardedBy("this")
    d zzb;

    @Nullable
    @GuardedBy("mAutoDisconnectTaskLock")
    c zze;

    public b(Context context) {
        this(context, DashMediaSource.DEFAULT_FALLBACK_TARGET_LIVE_OFFSET_MS, false);
    }

    public static a a(Context context) {
        b bVar = new b(context, -1L, true);
        try {
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            bVar.d(false);
            a aVarE = bVar.e();
            bVar.zzc(aVarE, true, 0.0f, SystemClock.elapsedRealtime() - jElapsedRealtime, "", null);
            return aVarE;
        } finally {
        }
    }

    public static boolean b(Context context) {
        boolean z10;
        b bVar = new b(context, -1L, false);
        try {
            bVar.d(false);
            w.e("Calling this from your main thread can lead to deadlock");
            synchronized (bVar) {
                try {
                    if (!bVar.f7872a) {
                        synchronized (bVar.f7873b) {
                            c cVar = bVar.zze;
                            if (cVar == null || !cVar.f7879d) {
                                throw new IOException("AdvertisingIdClient is not connected.");
                            }
                        }
                        try {
                            bVar.d(false);
                            if (!bVar.f7872a) {
                                throw new IOException("AdvertisingIdClient cannot reconnect.");
                            }
                        } catch (Exception e10) {
                            throw new IOException("AdvertisingIdClient cannot reconnect.", e10);
                        }
                    }
                    w.checkNotNull(bVar.zza);
                    w.checkNotNull(bVar.zzb);
                    try {
                        m8.b bVar2 = (m8.b) bVar.zzb;
                        bVar2.getClass();
                        Parcel parcelObtain = Parcel.obtain();
                        parcelObtain.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                        Parcel parcelG = bVar2.G(6, parcelObtain);
                        int i10 = m8.a.f30488a;
                        z10 = parcelG.readInt() != 0;
                        parcelG.recycle();
                    } catch (RemoteException unused) {
                        throw new IOException("Remote exception");
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            bVar.f();
            return z10;
        } finally {
            bVar.c();
        }
    }

    public final void c() {
        w.e("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f7874c == null || this.zza == null) {
                    return;
                }
                try {
                    if (this.f7872a) {
                        h8.a.b().c(this.f7874c, this.zza);
                    }
                } catch (Throwable unused) {
                }
                this.f7872a = false;
                this.zzb = null;
                this.zza = null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void d(boolean z10) {
        w.e("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f7872a) {
                    c();
                }
                Context context = this.f7874c;
                try {
                    context.getPackageManager().getPackageInfo("com.android.vending", 0);
                    int iB = e.f25522b.b(context, 12451000);
                    if (iB != 0 && iB != 2) {
                        throw new IOException("Google Play services not available");
                    }
                    e8.a aVar = new e8.a();
                    Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
                    intent.setPackage("com.google.android.gms");
                    try {
                        if (!h8.a.b().a(context, intent, aVar, 1)) {
                            throw new IOException("Connection failure");
                        }
                        this.zza = aVar;
                        try {
                            IBinder iBinderA = aVar.a(TimeUnit.MILLISECONDS);
                            int i10 = m8.c.f30490a;
                            IInterface iInterfaceQueryLocalInterface = iBinderA.queryLocalInterface("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                            this.zzb = iInterfaceQueryLocalInterface instanceof d ? (d) iInterfaceQueryLocalInterface : new m8.b(iBinderA);
                            this.f7872a = true;
                            if (z10) {
                                f();
                            }
                        } 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();
                }
            } 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.f7872a) {
                    synchronized (this.f7873b) {
                        c cVar = this.zze;
                        if (cVar == null || !cVar.f7879d) {
                            throw new IOException("AdvertisingIdClient is not connected.");
                        }
                    }
                    try {
                        d(false);
                        if (!this.f7872a) {
                            throw new IOException("AdvertisingIdClient cannot reconnect.");
                        }
                    } catch (Exception e10) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.", e10);
                    }
                }
                w.checkNotNull(this.zza);
                w.checkNotNull(this.zzb);
                try {
                    m8.b bVar = (m8.b) this.zzb;
                    bVar.getClass();
                    Parcel parcelObtain = Parcel.obtain();
                    parcelObtain.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    Parcel parcelG = bVar.G(1, parcelObtain);
                    String string = parcelG.readString();
                    parcelG.recycle();
                    m8.b bVar2 = (m8.b) this.zzb;
                    bVar2.getClass();
                    Parcel parcelObtain2 = Parcel.obtain();
                    parcelObtain2.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    int i10 = m8.a.f30488a;
                    parcelObtain2.writeInt(1);
                    Parcel parcelG2 = bVar2.G(2, parcelObtain2);
                    boolean z10 = parcelG2.readInt() != 0;
                    parcelG2.recycle();
                    aVar = new a(string, z10);
                } catch (RemoteException unused) {
                    throw new IOException("Remote exception");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        f();
        return aVar;
    }

    public final void f() {
        synchronized (this.f7873b) {
            c cVar = this.zze;
            if (cVar != null) {
                cVar.f7878c.countDown();
                try {
                    this.zze.join();
                } catch (InterruptedException unused) {
                }
            }
            long j10 = this.f7875d;
            if (j10 > 0) {
                this.zze = new c(this, j10);
            }
        }
    }

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

    @VisibleForTesting
    public final boolean zzc(@Nullable a aVar, boolean z10, float f10, long j10, String str, @Nullable Throwable th) {
        if (Math.random() > 0.0d) {
            return false;
        }
        HashMap mapO = u1.o("app_context", "1");
        if (aVar != null) {
            mapO.put("limit_ad_tracking", true != aVar.f7871a ? SessionDescription.SUPPORTED_SDP_VERSION : "1");
            String id2 = aVar.getId();
            if (id2 != null) {
                mapO.put("ad_id_size", Integer.toString(id2.length()));
            }
        }
        if (th != null) {
            mapO.put(IjkMediaPlayer.OnNativeInvokeListener.ARG_ERROR, th.getClass().getName());
        }
        mapO.put("tag", "AdvertisingIdClient");
        mapO.put("time_spent", Long.toString(j10));
        new h(mapO).start();
        return true;
    }

    public b(Context context, long j10, boolean z10) {
        Context applicationContext;
        this.f7873b = new Object();
        w.checkNotNull(context);
        if (z10 && (applicationContext = context.getApplicationContext()) != null) {
            context = applicationContext;
        }
        this.f7874c = context;
        this.f7872a = false;
        this.f7875d = j10;
    }
}
