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;

/* JADX INFO: renamed from: ʿʕˌ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C0352 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public InterfaceC2040 f2134;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public boolean f2135;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public C1160 f2136;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public ServiceConnectionC1046 f2137;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final long f2138;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final Object f2139 = new Object();

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final Context f2140;

    public C0352(Context context) {
        ᵘᵞʿⁱ.ᵶᵥᵝ(context);
        Context applicationContext = context.getApplicationContext();
        this.f2140 = applicationContext != null ? applicationContext : context;
        this.f2135 = false;
        this.f2138 = -1L;
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public static C3789 m1667(Context context) {
        C0352 c0352 = new C0352(context);
        try {
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            c0352.m1670();
            C3789 c3789M1671 = c0352.m1671();
            m1668(c3789M1671, SystemClock.elapsedRealtime() - jElapsedRealtime, null);
            return c3789M1671;
        } finally {
        }
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public static void m1668(C3789 c3789, long j, Throwable th) {
        if (Math.random() <= 0.0d) {
            HashMap map = new HashMap();
            map.put("app_context", "1");
            if (c3789 != null) {
                map.put("limit_ad_tracking", true != c3789.f14391 ? "0" : "1");
                String str = c3789.f14392;
                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 {
        m1669();
        super.finalize();
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final void m1669() {
        ᵘᵞʿⁱ.ᵱᵽʡᵚ("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f2140 == null || this.f2137 == null) {
                    return;
                }
                try {
                    if (this.f2135) {
                        C0183.m1338().m1339(this.f2140, this.f2137);
                    }
                } catch (Throwable unused) {
                }
                this.f2135 = false;
                this.f2134 = null;
                this.f2137 = null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final void m1670() {
        ᵘᵞʿⁱ.ᵱᵽʡᵚ("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (this.f2135) {
                    m1669();
                }
                Context context = this.f2140;
                try {
                    context.getPackageManager().getPackageInfo("com.android.vending", 0);
                    int iM10399 = C5073.f19129.m10399(context, 12451000);
                    if (iM10399 != 0 && iM10399 != 2) {
                        throw new IOException("Google Play services not available");
                    }
                    ServiceConnectionC1046 serviceConnectionC1046 = new ServiceConnectionC1046();
                    Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
                    intent.setPackage("com.google.android.gms");
                    try {
                        if (!C0183.m1338().m1340(context, context.getClass().getName(), intent, serviceConnectionC1046, 1, null)) {
                            throw new IOException("Connection failure");
                        }
                        this.f2137 = serviceConnectionC1046;
                        try {
                            IBinder iBinderM3446 = serviceConnectionC1046.m3446();
                            int i = AbstractBinderC1450.f5897;
                            IInterface iInterfaceQueryLocalInterface = iBinderM3446.queryLocalInterface("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                            this.f2134 = iInterfaceQueryLocalInterface instanceof InterfaceC2040 ? (InterfaceC2040) iInterfaceQueryLocalInterface : new C5049(iBinderM3446);
                            this.f2135 = 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;
            }
        }
    }

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final C3789 m1671() {
        C3789 c3789;
        ᵘᵞʿⁱ.ᵱᵽʡᵚ("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            try {
                if (!this.f2135) {
                    synchronized (this.f2139) {
                        C1160 c1160 = this.f2136;
                        if (c1160 == null || !c1160.f4955) {
                            throw new IOException("AdvertisingIdClient is not connected.");
                        }
                    }
                    try {
                        m1670();
                        if (!this.f2135) {
                            throw new IOException("AdvertisingIdClient cannot reconnect.");
                        }
                    } catch (Exception e) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.", e);
                    }
                }
                ᵘᵞʿⁱ.ᵶᵥᵝ(this.f2137);
                ᵘᵞʿⁱ.ᵶᵥᵝ(this.f2134);
                try {
                    C5049 c5049 = (C5049) this.f2134;
                    c5049.getClass();
                    Parcel parcelObtain = Parcel.obtain();
                    parcelObtain.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    boolean z = true;
                    Parcel parcelM10387 = c5049.m10387(parcelObtain, 1);
                    String string = parcelM10387.readString();
                    parcelM10387.recycle();
                    C5049 c50492 = (C5049) this.f2134;
                    c50492.getClass();
                    Parcel parcelObtain2 = Parcel.obtain();
                    parcelObtain2.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                    int i = AbstractC0422.f2362;
                    parcelObtain2.writeInt(1);
                    Parcel parcelM103872 = c50492.m10387(parcelObtain2, 2);
                    if (parcelM103872.readInt() == 0) {
                        z = false;
                    }
                    parcelM103872.recycle();
                    c3789 = new C3789(0, string, z);
                } catch (RemoteException unused) {
                    throw new IOException("Remote exception");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        m1672();
        return c3789;
    }

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final void m1672() {
        synchronized (this.f2139) {
            C1160 c1160 = this.f2136;
            if (c1160 != null) {
                c1160.f4953.countDown();
                try {
                    this.f2136.join();
                } catch (InterruptedException unused) {
                }
            }
            long j = this.f2138;
            if (j > 0) {
                this.f2136 = new C1160(this, j);
            }
        }
    }
}
