package p138.p176.p177.p242.p243.p244;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.GuardedBy;
import p000.p001.p002.p003.p004.C0567;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.C4196;
import p138.p176.p177.p242.p246.C4198;
import p138.p176.p177.p242.p246.ServiceConnectionC4190;
import p138.p176.p177.p242.p246.p252.C4306;
import p138.p176.p177.p242.p258.p259.AbstractBinderC4353;
import p138.p176.p177.p242.p258.p259.InterfaceC4352;

/* JADX INFO: renamed from: ދ.ހ.֏.ހ.֏.֏.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
@ParametersAreNonnullByDefault
public class C4184 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    @GuardedBy("this")
    public ServiceConnectionC4190 f13374;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    @GuardedBy("this")
    public InterfaceC4352 f13375;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    @GuardedBy("this")
    public boolean f13376;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final Object f13377 = new Object();

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    @GuardedBy("mAutoDisconnectTaskLock")
    public C4186 f13378;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    @GuardedBy("this")
    public final Context f13379;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final boolean f13380;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final long f13381;

    /* JADX INFO: renamed from: ދ.ހ.֏.ހ.֏.֏.֏$֏, reason: contains not printable characters */
    public static final class C4185 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final String f13382;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final boolean f13383;

        public C4185(String str, boolean z) {
            this.f13382 = str;
            this.f13383 = z;
        }

        public final String toString() {
            String str = this.f13382;
            boolean z = this.f13383;
            StringBuilder sb = new StringBuilder(C2803.m4895(str, 7));
            sb.append("{");
            sb.append(str);
            sb.append("}");
            sb.append(z);
            return sb.toString();
        }
    }

    /* JADX INFO: renamed from: ދ.ހ.֏.ހ.֏.֏.֏$ؠ, reason: contains not printable characters */
    public static class C4186 extends Thread {

        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public WeakReference<C4184> f13384;

        /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
        public long f13385;

        /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
        public CountDownLatch f13386 = new CountDownLatch(1);

        /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
        public boolean f13387 = false;

        public C4186(C4184 c4184, long j) {
            this.f13384 = new WeakReference<>(c4184);
            this.f13385 = j;
            start();
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public final void run() {
            C4184 c4184;
            try {
                if (this.f13386.await(this.f13385, TimeUnit.MILLISECONDS) || (c4184 = this.f13384.get()) == null) {
                    return;
                }
                c4184.m6834();
                this.f13387 = true;
            } catch (InterruptedException unused) {
                C4184 c41842 = this.f13384.get();
                if (c41842 != null) {
                    c41842.m6834();
                    this.f13387 = true;
                }
            }
        }
    }

    public C4184(Context context, long j, boolean z, boolean z2) {
        Context applicationContext;
        C0567.m1910(context);
        if (z && (applicationContext = context.getApplicationContext()) != null) {
            context = applicationContext;
        }
        this.f13379 = context;
        this.f13376 = false;
        this.f13381 = j;
        this.f13380 = z2;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static C4185 m6832(Context context) {
        boolean z;
        float f;
        String string;
        boolean z2;
        SharedPreferences sharedPreferences = null;
        try {
            Context contextM6854 = C4198.m6854(context);
            if (contextM6854 != null) {
                sharedPreferences = contextM6854.getSharedPreferences("google_ads_flags", 0);
            }
        } catch (Throwable th) {
            Log.w("GmscoreFlag", "Error while getting SharedPreferences ", th);
        }
        if (sharedPreferences == null) {
            z = false;
        } else {
            try {
                z = sharedPreferences.getBoolean("gads:ad_id_app_context:enabled", false);
            } catch (Throwable th2) {
                Log.w("GmscoreFlag", "Error while reading from SharedPreferences ", th2);
                z = false;
            }
        }
        if (sharedPreferences == null) {
            f = 0.0f;
        } else {
            try {
                f = sharedPreferences.getFloat("gads:ad_id_app_context:ping_ratio", 0.0f);
            } catch (Throwable th3) {
                Log.w("GmscoreFlag", "Error while reading from SharedPreferences ", th3);
                f = 0.0f;
            }
        }
        if (sharedPreferences == null) {
            string = "";
        } else {
            try {
                string = sharedPreferences.getString("gads:ad_id_use_shared_preference:experiment_id", "");
            } catch (Throwable th4) {
                Log.w("GmscoreFlag", "Error while reading from SharedPreferences ", th4);
                string = "";
            }
        }
        if (sharedPreferences == null) {
            z2 = false;
        } else {
            try {
                z2 = sharedPreferences.getBoolean("gads:ad_id_use_persistent_service:enabled", false);
            } catch (Throwable th5) {
                Log.w("GmscoreFlag", "Error while reading from SharedPreferences ", th5);
                z2 = false;
            }
        }
        C4184 c4184 = new C4184(context, -1L, z, z2);
        try {
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            c4184.m6835(false);
            C4185 c4185M6837 = c4184.m6837();
            c4184.m6836(c4185M6837, z, f, SystemClock.elapsedRealtime() - jElapsedRealtime, string, null);
            return c4185M6837;
        } finally {
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static ServiceConnectionC4190 m6833(Context context, boolean z) throws GooglePlayServicesNotAvailableException, IOException {
        try {
            context.getPackageManager().getPackageInfo("com.android.vending", 0);
            int iMo6844 = C4196.f13406.mo6844(context, 12451000);
            if (iMo6844 != 0 && iMo6844 != 2) {
                throw new IOException("Google Play services not available");
            }
            String str = z ? "com.google.android.gms.ads.identifier.service.PERSISTENT_START" : "com.google.android.gms.ads.identifier.service.START";
            ServiceConnectionC4190 serviceConnectionC4190 = new ServiceConnectionC4190();
            Intent intent = new Intent(str);
            intent.setPackage("com.google.android.gms");
            try {
                C4306 c4306M7206 = C4306.m7206();
                if (c4306M7206 == null) {
                    throw null;
                }
                context.getClass().getName();
                if (c4306M7206.m7208(context, intent, serviceConnectionC4190, 1)) {
                    return serviceConnectionC4190;
                }
                throw new IOException("Connection failure");
            } catch (Throwable th) {
                throw new IOException(th);
            }
        } catch (PackageManager.NameNotFoundException unused) {
            throw new GooglePlayServicesNotAvailableException(9);
        }
    }

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

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m6834() {
        C0567.m1938("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            if (this.f13379 == null || this.f13374 == null) {
                return;
            }
            try {
                if (this.f13376) {
                    C4306 c4306M7206 = C4306.m7206();
                    Context context = this.f13379;
                    ServiceConnectionC4190 serviceConnectionC4190 = this.f13374;
                    if (c4306M7206 == null) {
                        throw null;
                    }
                    context.unbindService(serviceConnectionC4190);
                }
            } catch (Throwable th) {
                Log.i("AdvertisingIdClient", "AdvertisingIdClient unbindService failed.", th);
            }
            this.f13376 = false;
            this.f13375 = null;
            this.f13374 = null;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m6835(boolean z) {
        C0567.m1938("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            if (this.f13376) {
                m6834();
            }
            ServiceConnectionC4190 serviceConnectionC4190M6833 = m6833(this.f13379, this.f13380);
            this.f13374 = serviceConnectionC4190M6833;
            try {
                this.f13375 = AbstractBinderC4353.m7248(serviceConnectionC4190M6833.m6839(10000L, TimeUnit.MILLISECONDS));
                this.f13376 = true;
                if (z) {
                    m6838();
                }
            } catch (InterruptedException unused) {
                throw new IOException("Interrupted exception");
            } catch (Throwable th) {
                throw new IOException(th);
            }
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final boolean m6836(C4185 c4185, boolean z, float f, long j, String str, Throwable th) {
        String str2;
        if (Math.random() > f) {
            return false;
        }
        HashMap map = new HashMap();
        map.put("app_context", z ? "1" : "0");
        if (c4185 != null) {
            map.put("limit_ad_tracking", c4185.f13383 ? "1" : "0");
        }
        if (c4185 != null && (str2 = c4185.f13382) != null) {
            map.put("ad_id_size", Integer.toString(str2.length()));
        }
        if (th != null) {
            map.put("error", th.getClass().getName());
        }
        if (str != null && !str.isEmpty()) {
            map.put("experiment_id", str);
        }
        map.put("tag", "AdvertisingIdClient");
        map.put("time_spent", Long.toString(j));
        new C4187(map).start();
        return true;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public C4185 m6837() {
        C4185 c4185;
        C0567.m1938("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            if (this.f13376) {
                C0567.m1910(this.f13374);
                C0567.m1910(this.f13375);
                c4185 = new C4185(this.f13375.mo7247(), this.f13375.mo7246(true));
            } else {
                synchronized (this.f13377) {
                    if (this.f13378 == null || !this.f13378.f13387) {
                        throw new IOException("AdvertisingIdClient is not connected.");
                    }
                }
                try {
                    m6835(false);
                    if (!this.f13376) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.");
                    }
                    C0567.m1910(this.f13374);
                    C0567.m1910(this.f13375);
                    try {
                        c4185 = new C4185(this.f13375.mo7247(), this.f13375.mo7246(true));
                    } catch (RemoteException e) {
                        Log.i("AdvertisingIdClient", "GMS remote exception ", e);
                        throw new IOException("Remote exception");
                    }
                } catch (Exception e2) {
                    throw new IOException("AdvertisingIdClient cannot reconnect.", e2);
                }
            }
        }
        m6838();
        return c4185;
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final void m6838() {
        synchronized (this.f13377) {
            if (this.f13378 != null) {
                this.f13378.f13386.countDown();
                try {
                    this.f13378.join();
                } catch (InterruptedException unused) {
                }
            }
            if (this.f13381 > 0) {
                this.f13378 = new C4186(this, this.f13381);
            }
        }
    }
}
