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

import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Looper;
import android.text.TextUtils;
import android.webkit.WebView;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import xyz.stream.download.config.InnerConstant;

/* JADX INFO: loaded from: classes2.dex */
public final class dz implements ez {

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

    @Nullable
    @VisibleForTesting
    public static ez zza;

    @Nullable
    @VisibleForTesting
    static ez zzb;

    @Nullable
    @VisibleForTesting
    static ez zzc;

    @Nullable
    @VisibleForTesting
    static Boolean zzd;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final q7.a f10574e;

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final HashSet f10578i;

    @Nullable
    private final PackageInfo zzk;

    @Nullable
    private final String zzm;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final WeakHashMap f10572c = new WeakHashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ExecutorService f10573d = Executors.unconfigurableExecutorService(Executors.newCachedThreadPool());

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final AtomicBoolean f10576g = new AtomicBoolean();

    /* JADX WARN: Removed duplicated region for block: B:11:0x004d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public dz(Context context, q7.a aVar) {
        PackageInfo packageInfoB;
        String country;
        context = context.getApplicationContext() != null ? context.getApplicationContext() : context;
        this.f10571b = context;
        this.f10574e = aVar;
        String string = null;
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.Q8)).booleanValue()) {
            vi1 vi1Var = q7.e.f32222b;
            if (context == null || context.getApplicationInfo() == null) {
                packageInfoB = null;
            } else {
                try {
                    packageInfoB = j8.e.a(context).b(0, context.getApplicationInfo().packageName);
                } catch (PackageManager.NameNotFoundException unused) {
                    packageInfoB = null;
                }
            }
        }
        this.zzk = packageInfoB;
        tn tnVar = ao.E8;
        n7.y yVar = n7.y.f31093e;
        if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            vi1 vi1Var2 = q7.e.f32222b;
            country = Locale.getDefault().getCountry();
        } else {
            country = "unknown";
        }
        this.f10575f = country;
        if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            Context context2 = this.f10571b;
            vi1 vi1Var3 = q7.e.f32222b;
            if (context2 != null) {
                try {
                    PackageInfo packageInfoB2 = j8.e.a(context2).b(128, "com.android.vending");
                    if (packageInfoB2 != null) {
                        string = Integer.toString(packageInfoB2.versionCode);
                    }
                } catch (PackageManager.NameNotFoundException unused2) {
                }
            }
        } else {
            string = "unknown";
        }
        this.zzm = string;
        if (((Integer) n7.y.f31093e.f31096c.b(ao.A8)).intValue() > 0) {
            this.f10578i = new HashSet();
        }
    }

    public static ez a(Context context) {
        synchronized (f10569j) {
            try {
                if (zza == null) {
                    if (h(context)) {
                        zza = new dz(context, q7.a.w());
                    } else {
                        zza = new va2(26);
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return zza;
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0039  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static ez b(Context context, q7.a aVar) {
        synchronized (f10569j) {
            try {
                if (zzc == null) {
                    if (((Boolean) hp.f12219c.j()).booleanValue()) {
                        boolean z10 = !((Boolean) n7.y.f31093e.f31096c.b(ao.f9169y8)).booleanValue() || ((Boolean) hp.f12217a.j()).booleanValue();
                        if (h(context)) {
                            dz dzVar = new dz(context, aVar);
                            dzVar.i();
                            Thread.setDefaultUncaughtExceptionHandler(new cz(dzVar, Thread.getDefaultUncaughtExceptionHandler(), 0));
                            zzc = dzVar;
                        } else if (!z10 || context == null) {
                            zzc = new va2(26);
                        } else {
                            dz dzVar2 = new dz(context, aVar);
                            dzVar2.f10577h = true;
                            dzVar2.i();
                            Thread.setDefaultUncaughtExceptionHandler(new cz(dzVar2, Thread.getDefaultUncaughtExceptionHandler(), 0));
                            zzc = dzVar2;
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return zzc;
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0039 A[Catch: all -> 0x0037, TryCatch #0 {all -> 0x0037, blocks: (B:4:0x0003, B:6:0x0007, B:8:0x0019, B:11:0x002b, B:14:0x0039, B:15:0x0042), top: B:20:0x0003 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static ez c(Context context) {
        synchronized (f10569j) {
            try {
                if (zzb == null) {
                    tn tnVar = ao.f9184z8;
                    n7.y yVar = n7.y.f31093e;
                    if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                        if (((Boolean) yVar.f31096c.b(ao.f9169y8)).booleanValue() || context == null) {
                            zzb = new va2(26);
                        } else {
                            zzb = new dz(context, q7.a.w());
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return zzb;
    }

    public static String f(Throwable th) {
        StringWriter stringWriter = new StringWriter();
        th.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    public static boolean h(Context context) {
        if (context == null) {
            return false;
        }
        synchronized (f10569j) {
            try {
                if (zzd == null) {
                    zzd = Boolean.valueOf(n7.x.f31085g.f31090e.nextInt(100) < ((Integer) n7.y.f31093e.f31096c.b(ao.Vd)).intValue());
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (zzd.booleanValue()) {
            if (!((Boolean) n7.y.f31093e.f31096c.b(ao.f9169y8)).booleanValue()) {
                return true;
            }
        }
        return false;
    }

    @Override // com.google.android.gms.internal.ads.ez
    public final void d(String str, Throwable th) {
        if (this.f10577h) {
            return;
        }
        e(th, str, 1.0f);
    }

    @Override // com.google.android.gms.internal.ads.ez
    public final void e(Throwable th, String str, float f10) {
        Throwable th2;
        String packageName;
        PackageInfo packageInfoB;
        ActivityManager.MemoryInfo memoryInfoZze;
        String strZzg;
        Context context = this.f10571b;
        if (this.f10577h) {
            return;
        }
        vi1 vi1Var = q7.e.f32222b;
        if (((Boolean) xp.f18506e.j()).booleanValue()) {
            th2 = th;
        } else {
            LinkedList linkedList = new LinkedList();
            for (Throwable cause = th; cause != null; cause = cause.getCause()) {
                linkedList.push(cause);
            }
            th2 = null;
            while (!linkedList.isEmpty()) {
                Throwable th3 = (Throwable) linkedList.pop();
                StackTraceElement[] stackTrace = th3.getStackTrace();
                boolean z10 = ((Boolean) n7.y.f31093e.f31096c.b(ao.N2)).booleanValue() && stackTrace != null && stackTrace.length == 0 && q7.e.f(th3.getClass().getName());
                ArrayList arrayList = new ArrayList();
                arrayList.add(new StackTraceElement(th3.getClass().getName(), "<filtered>", "<filtered>", 1));
                for (StackTraceElement stackTraceElement : stackTrace) {
                    if (q7.e.f(stackTraceElement.getClassName())) {
                        arrayList.add(stackTraceElement);
                        z10 = true;
                    } else {
                        String className = stackTraceElement.getClassName();
                        if (!TextUtils.isEmpty(className) && (className.startsWith("android.") || className.startsWith("java."))) {
                            arrayList.add(stackTraceElement);
                        } else {
                            arrayList.add(new StackTraceElement("<filtered>", "<filtered>", "<filtered>", 1));
                        }
                    }
                }
                if (z10) {
                    th2 = th2 == null ? new Throwable(th3.getMessage()) : new Throwable(th3.getMessage(), th2);
                    th2.setStackTrace((StackTraceElement[]) arrayList.toArray(new StackTraceElement[0]));
                }
            }
        }
        boolean zC = false;
        if (th2 != null) {
            String name = th.getClass().getName();
            String strF = f(th);
            String str2 = "";
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.U9)).booleanValue() && (strZzg = q7.e.zzg(f(th))) != null) {
                str2 = strZzg;
            }
            double d10 = f10;
            double dRandom = Math.random();
            int i10 = f10 > 0.0f ? (int) (1.0f / f10) : 1;
            if (dRandom < d10) {
                ArrayList arrayList2 = new ArrayList();
                try {
                    zC = j8.e.a(context).c();
                } catch (Throwable unused) {
                }
                try {
                    packageName = context.getPackageName();
                } catch (Throwable unused2) {
                    q7.j.d("Cannot obtain package name, proceeding.");
                    packageName = "unknown";
                }
                Uri.Builder builderAppendQueryParameter = new Uri.Builder().scheme("https").path("//pagead2.googlesyndication.com/pagead/gen_204").appendQueryParameter("is_aia", Boolean.toString(zC)).appendQueryParameter(InnerConstant.Db.f36069id, "gmob-apps-report-exception").appendQueryParameter("os", Build.VERSION.RELEASE);
                int i11 = Build.VERSION.SDK_INT;
                Uri.Builder builderAppendQueryParameter2 = builderAppendQueryParameter.appendQueryParameter("api", String.valueOf(i11));
                String str3 = Build.MANUFACTURER;
                String strP = Build.MODEL;
                if (!strP.startsWith(str3)) {
                    strP = defpackage.e.p(new StringBuilder(String.valueOf(str3).length() + 1 + strP.length()), str3, " ", strP);
                }
                Uri.Builder builderAppendQueryParameter3 = builderAppendQueryParameter2.appendQueryParameter("device", strP);
                q7.a aVar = this.f10574e;
                Uri.Builder builderAppendQueryParameter4 = builderAppendQueryParameter3.appendQueryParameter("js", aVar.f32212a).appendQueryParameter("appid", packageName).appendQueryParameter("exceptiontype", name).appendQueryParameter("stacktrace", strF);
                n7.y yVar = n7.y.f31093e;
                Uri.Builder builderAppendQueryParameter5 = builderAppendQueryParameter4.appendQueryParameter("eids", TextUtils.join(",", yVar.f31094a.A())).appendQueryParameter("exceptionkey", str).appendQueryParameter("cl", "839961582").appendQueryParameter("rc", "dev").appendQueryParameter("sampling_rate", Integer.toString(i10)).appendQueryParameter("pb_tm", String.valueOf(xp.f18504c.j()));
                e8.e.f25522b.getClass();
                Uri.Builder builderAppendQueryParameter6 = builderAppendQueryParameter5.appendQueryParameter("gmscv", String.valueOf(e8.e.a(context))).appendQueryParameter("lite", true != aVar.f32216e ? SessionDescription.SUPPORTED_SDP_VERSION : "1");
                if (!TextUtils.isEmpty(str2)) {
                    builderAppendQueryParameter6.appendQueryParameter("hash", str2);
                }
                tn tnVar = ao.F8;
                yn ynVar = yVar.f31096c;
                if (((Boolean) ynVar.b(tnVar)).booleanValue() && (memoryInfoZze = q7.e.zze(context)) != null) {
                    builderAppendQueryParameter6.appendQueryParameter("available_memory", Long.toString(memoryInfoZze.availMem));
                    builderAppendQueryParameter6.appendQueryParameter("total_memory", Long.toString(memoryInfoZze.totalMem));
                    builderAppendQueryParameter6.appendQueryParameter("is_low_memory", true != memoryInfoZze.lowMemory ? SessionDescription.SUPPORTED_SDP_VERSION : "1");
                }
                if (((Boolean) ynVar.b(ao.E8)).booleanValue()) {
                    String str4 = this.f10575f;
                    if (!TextUtils.isEmpty(str4)) {
                        builderAppendQueryParameter6.appendQueryParameter("countrycode", str4);
                    }
                    String str5 = this.zzm;
                    if (!TextUtils.isEmpty(str5)) {
                        builderAppendQueryParameter6.appendQueryParameter("psv", str5);
                    }
                    if (i11 >= 26) {
                        packageInfoB = WebView.getCurrentWebViewPackage();
                    } else if (context == null) {
                        packageInfoB = null;
                    } else {
                        try {
                            packageInfoB = j8.e.a(context).b(128, "com.android.webview");
                        } catch (PackageManager.NameNotFoundException unused3) {
                            packageInfoB = null;
                        }
                    }
                    if (packageInfoB != null) {
                        builderAppendQueryParameter6.appendQueryParameter("wvvc", Integer.toString(packageInfoB.versionCode));
                        builderAppendQueryParameter6.appendQueryParameter("wvvn", packageInfoB.versionName);
                        builderAppendQueryParameter6.appendQueryParameter("wvpn", packageInfoB.packageName);
                    }
                }
                PackageInfo packageInfo = this.zzk;
                if (packageInfo != null) {
                    builderAppendQueryParameter6.appendQueryParameter("appvc", String.valueOf(packageInfo.versionCode));
                    builderAppendQueryParameter6.appendQueryParameter("appvn", packageInfo.versionName);
                }
                arrayList2.add(builderAppendQueryParameter6.toString());
                Iterator it = arrayList2.iterator();
                while (it.hasNext()) {
                    this.f10573d.execute(new bv(new q7.m(context, null), (String) it.next(), 11));
                }
            }
        }
    }

    public final void g(Throwable th) {
        Context context;
        SharedPreferences sharedPreferences;
        if (th != null) {
            boolean zF = false;
            boolean zEquals = false;
            for (Throwable cause = th; cause != null; cause = cause.getCause()) {
                for (StackTraceElement stackTraceElement : cause.getStackTrace()) {
                    zF |= q7.e.f(stackTraceElement.getClassName());
                    zEquals |= dz.class.getName().equals(stackTraceElement.getClassName());
                }
            }
            int iIntValue = ((Integer) n7.y.f31093e.f31096c.b(ao.A8)).intValue();
            if (iIntValue > 0) {
                HashSet hashSet = this.f10578i;
                if (hashSet.size() >= iIntValue) {
                    return;
                }
                String strZzg = q7.e.zzg(f(th));
                if (strZzg == null) {
                    strZzg = "";
                }
                if (hashSet.contains(strZzg)) {
                    return;
                } else {
                    hashSet.add(strZzg);
                }
            }
            if (!zF || zEquals) {
                return;
            }
            if (!this.f10577h) {
                d("", th);
            }
            if (this.f10576g.getAndSet(true) || !((Boolean) hp.f12219c.j()).booleanValue() || (sharedPreferences = (context = this.f10571b).getSharedPreferences("admob", 0)) == null) {
                return;
            }
            sharedPreferences.edit().putInt("crash_without_write", ni.G(context, "crash_without_write") + 1).commit();
        }
    }

    public final void i() {
        Thread thread = Looper.getMainLooper().getThread();
        if (thread == null) {
            return;
        }
        synchronized (this.f10570a) {
            this.f10572c.put(thread, Boolean.TRUE);
        }
        thread.setUncaughtExceptionHandler(new cz(this, thread.getUncaughtExceptionHandler(), 1));
    }
}
