package defpackage;

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.GooglePlayServicesNotAvailableException;
import java.io.IOException;
import java.util.HashMap;

/* renamed from: ᵭˊᵫ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4106 {

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public C1053 f15339;

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final long f15340;

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public final Context f15341;

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public boolean f15342;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public ServiceConnectionC5161 f15343;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public InterfaceC2643 f15344;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final Object f15345 = new Object();

    public C4106(Context context) {
        ᵩʛʽ.ᵝᵵᵺ(context);
        Context applicationContext = context.getApplicationContext();
        this.f15341 = applicationContext != null ? applicationContext : context;
        this.f15342 = false;
        this.f15340 = -1L;
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public static C4413 m8987(Context context) {
        C4106 c4106 = new C4106(context);
        try {
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            c4106.m8991();
            C4413 c4413M8989 = c4106.m8989();
            m8988(c4413M8989, SystemClock.elapsedRealtime() - jElapsedRealtime, null);
            return c4413M8989;
        } finally {
        }
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static void m8988(C4413 c4413, long j, Throwable th) {
        if (Math.random() <= 0.0d) {
            HashMap map = new HashMap();
            map.put("app_context", "1");
            if (c4413 != null) {
                map.put("limit_ad_tracking", true != c4413.f16510 ? "0" : "1");
                String str = c4413.f16508;
                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 ˏᵸ(map).start();
        }
    }

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

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final C4413 m8989() {
        C4413 c4413;
        ᵩʛʽ.ᵹᵝᵶ("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (!this.f15342) {
                    synchronized (this.f15345) {
                        C1053 c1053 = this.f15339;
                        if (c1053 == null || !c1053.f4573) {
                            throw new IOException("AdvertisingIdClient is not connected.");
                        }
                    }
                    try {
                        m8991();
                        if (!this.f15342) {
                            throw new IOException("AdvertisingIdClient cannot reconnect.");
                        }
                    } catch (Exception e) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.", e);
                    }
                }
                ᵩʛʽ.ᵝᵵᵺ(this.f15343);
                ᵩʛʽ.ᵝᵵᵺ(this.f15344);
                try {
                    C0780 c0780 = (C0780) this.f15344;
                    c0780.getClass();
                    Parcel parcelObtain = Parcel.obtain();
                    parcelObtain.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    boolean z = true;
                    Parcel parcelM2867 = c0780.m2867(parcelObtain, 1);
                    String string = parcelM2867.readString();
                    parcelM2867.recycle();
                    C0780 c07802 = (C0780) this.f15344;
                    c07802.getClass();
                    Parcel parcelObtain2 = Parcel.obtain();
                    parcelObtain2.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    int i = AbstractC0076.f1323;
                    parcelObtain2.writeInt(1);
                    Parcel parcelM28672 = c07802.m2867(parcelObtain2, 2);
                    if (parcelM28672.readInt() == 0) {
                        z = false;
                    }
                    parcelM28672.recycle();
                    c4413 = new C4413(0, string, z);
                } catch (RemoteException unused) {
                    throw new IOException("Remote exception");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        m8990();
        return c4413;
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public final void m8990() {
        synchronized (this.f15345) {
            C1053 c1053 = this.f15339;
            if (c1053 != null) {
                c1053.f4574.countDown();
                try {
                    this.f15339.join();
                } catch (InterruptedException unused) {
                }
            }
            long j = this.f15340;
            if (j > 0) {
                this.f15339 = new C1053(this, j);
            }
        }
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final void m8991() {
        ᵩʛʽ.ᵹᵝᵶ("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f15342) {
                    m8992();
                }
                Context context = this.f15341;
                try {
                    context.getPackageManager().getPackageInfo("com.android.vending", 0);
                    int iM3394 = C1029.f4496.m3394(context, 12451000);
                    if (iM3394 != 0 && iM3394 != 2) {
                        throw new IOException("Google Play services not available");
                    }
                    ServiceConnectionC5161 serviceConnectionC5161 = new ServiceConnectionC5161();
                    Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
                    intent.setPackage("com.google.android.gms");
                    try {
                        if (!C3580.m8186().m8187(context, context.getClass().getName(), intent, serviceConnectionC5161, 1, null)) {
                            throw new IOException("Connection failure");
                        }
                        this.f15343 = serviceConnectionC5161;
                        try {
                            IBinder iBinderM10658 = serviceConnectionC5161.m10658();
                            int i = AbstractBinderC2368.f9293;
                            IInterface iInterfaceQueryLocalInterface = iBinderM10658.queryLocalInterface("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                            this.f15344 = iInterfaceQueryLocalInterface instanceof InterfaceC2643 ? (InterfaceC2643) iInterfaceQueryLocalInterface : new C0780(iBinderM10658);
                            this.f15342 = 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();
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void m8992() {
        ᵩʛʽ.ᵹᵝᵶ("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f15341 == null || this.f15343 == null) {
                    return;
                }
                try {
                    if (this.f15342) {
                        C3580.m8186().m8188(this.f15341, this.f15343);
                    }
                } catch (Throwable unused) {
                }
                this.f15342 = false;
                this.f15344 = null;
                this.f15343 = null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
