package q7;

import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.ads.AdFormat;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.admanager.AdManagerAdView;
import com.google.android.gms.internal.ads.ao;
import com.google.android.gms.internal.ads.vi1;
import com.google.android.gms.internal.ads.xp;
import com.google.android.gms.internal.ads.zzfvb;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import n7.a4;
import n7.f4;
import n7.p3;
import n7.t2;
import n7.y;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import xyz.stream.download.config.InnerConstant;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final vi1 f32222b = new vi1(Looper.getMainLooper(), 0);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final String f32223c = AdView.class.getName();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final String f32224d = r7.a.class.getName();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final String f32225e = AdManagerAdView.class.getName();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final String f32226f = h7.c.class.getName();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final String f32227g = zzfvb.class.getName();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final String f32228h = g7.d.class.getName();

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

    public static boolean f(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return str.startsWith((String) xp.f18505d.j());
    }

    public static final int g(DisplayMetrics displayMetrics, int i10) {
        return (int) TypedValue.applyDimension(1, i10, displayMetrics);
    }

    public static final boolean h() {
        boolean zBooleanValue = ((Boolean) y.f31093e.f31096c.b(ao.Fc)).booleanValue();
        if (Build.VERSION.SDK_INT < 31) {
            return Build.DEVICE.startsWith("generic");
        }
        String str = Build.FINGERPRINT;
        if (str.contains("generic") || str.contains("emulator")) {
            return true;
        }
        return zBooleanValue && Build.HARDWARE.contains("ranchu");
    }

    public static final void zzB(Context context, @Nullable String str, String str2, Bundle bundle, boolean z10, d dVar) {
        Context applicationContext = context.getApplicationContext();
        if (applicationContext == null) {
            applicationContext = context;
        }
        bundle.putString("os", Build.VERSION.RELEASE);
        bundle.putString("api", String.valueOf(Build.VERSION.SDK_INT));
        bundle.putString("appid", applicationContext.getPackageName());
        if (str == null) {
            e8.e.f25522b.getClass();
            int iA = e8.e.a(context);
            str = u.a.b(new StringBuilder(String.valueOf(iA).length() + 10), iA, ".254715000");
        }
        bundle.putString("js", str);
        Uri.Builder builderAppendQueryParameter = new Uri.Builder().scheme("https").path("//pagead2.googlesyndication.com/pagead/gen_204").appendQueryParameter(InnerConstant.Db.f36069id, "gmob-apps");
        for (String str3 : bundle.keySet()) {
            builderAppendQueryParameter.appendQueryParameter(str3, bundle.getString(str3));
        }
        dVar.zza(builderAppendQueryParameter.toString());
    }

    @Nullable
    public static final String zzD(Context context) {
        ao.a(context);
        ContentResolver contentResolver = context.getContentResolver();
        String string = contentResolver == null ? null : Settings.Secure.getString(contentResolver, "android_id");
        if (string == null || h()) {
            string = "emulator";
        }
        return zzE(string, "MD5");
    }

    @Nullable
    private static String zzE(String str, String str2) {
        for (int i10 = 0; i10 < 2; i10++) {
            try {
                MessageDigest messageDigest = MessageDigest.getInstance(str2);
                messageDigest.update(str.getBytes());
                return String.format(Locale.US, "%032X", new BigInteger(1, messageDigest.digest()));
            } catch (ArithmeticException unused) {
                return null;
            } catch (NoSuchAlgorithmException unused2) {
            }
        }
        return null;
    }

    private final void zzG(JSONArray jSONArray, @Nullable Object obj) throws JSONException {
        if (obj instanceof Bundle) {
            jSONArray.put(d((Bundle) obj));
            return;
        }
        if (obj instanceof Map) {
            jSONArray.put(c((Map) obj));
            return;
        }
        if (obj instanceof Collection) {
            jSONArray.put(a((Collection) obj));
        } else if (obj instanceof Object[]) {
            jSONArray.put(e((Object[]) obj));
        } else {
            jSONArray.put(obj);
        }
    }

    private final void zzH(JSONObject jSONObject, String str, @Nullable Object obj) throws JSONException {
        Boolean[] boolArr;
        Long[] lArr;
        Double[] dArr;
        Integer[] numArr;
        if (((Boolean) y.f31093e.f31096c.b(ao.f9060s)).booleanValue()) {
            str = String.valueOf(str);
        }
        if (obj instanceof Bundle) {
            jSONObject.put(str, d((Bundle) obj));
            return;
        }
        if (obj instanceof Map) {
            jSONObject.put(str, c((Map) obj));
            return;
        }
        if (obj instanceof Collection) {
            jSONObject.put(String.valueOf(str), a((Collection) obj));
            return;
        }
        if (obj instanceof Object[]) {
            jSONObject.put(str, a(Arrays.asList((Object[]) obj)));
            return;
        }
        int i10 = 0;
        if (obj instanceof int[]) {
            int[] iArr = (int[]) obj;
            if (iArr == null) {
                numArr = new Integer[0];
            } else {
                int length = iArr.length;
                Integer[] numArr2 = new Integer[length];
                while (i10 < length) {
                    numArr2[i10] = Integer.valueOf(iArr[i10]);
                    i10++;
                }
                numArr = numArr2;
            }
            jSONObject.put(str, e(numArr));
            return;
        }
        if (obj instanceof double[]) {
            double[] dArr2 = (double[]) obj;
            if (dArr2 == null) {
                dArr = new Double[0];
            } else {
                int length2 = dArr2.length;
                Double[] dArr3 = new Double[length2];
                while (i10 < length2) {
                    dArr3[i10] = Double.valueOf(dArr2[i10]);
                    i10++;
                }
                dArr = dArr3;
            }
            jSONObject.put(str, e(dArr));
            return;
        }
        if (obj instanceof long[]) {
            long[] jArr = (long[]) obj;
            if (jArr == null) {
                lArr = new Long[0];
            } else {
                int length3 = jArr.length;
                Long[] lArr2 = new Long[length3];
                while (i10 < length3) {
                    lArr2[i10] = Long.valueOf(jArr[i10]);
                    i10++;
                }
                lArr = lArr2;
            }
            jSONObject.put(str, e(lArr));
            return;
        }
        if (!(obj instanceof boolean[])) {
            jSONObject.put(str, obj);
            return;
        }
        boolean[] zArr = (boolean[]) obj;
        if (zArr == null) {
            boolArr = new Boolean[0];
        } else {
            int length4 = zArr.length;
            Boolean[] boolArr2 = new Boolean[length4];
            while (i10 < length4) {
                boolArr2[i10] = Boolean.valueOf(zArr[i10]);
                i10++;
            }
            boolArr = boolArr2;
        }
        jSONObject.put(str, e(boolArr));
    }

    private static final void zzI(ViewGroup viewGroup, f4 f4Var, @Nullable String str, int i10, int i11) {
        if (viewGroup.getChildCount() != 0) {
            return;
        }
        Context context = viewGroup.getContext();
        TextView textView = new TextView(context);
        textView.setGravity(17);
        textView.setText(str);
        textView.setTextColor(i10);
        textView.setBackgroundColor(i11);
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.setBackgroundColor(i10);
        int iG = g(context.getResources().getDisplayMetrics(), 3);
        int i12 = f4Var.f30951f;
        int i13 = f4Var.f30948c;
        frameLayout.addView(textView, new FrameLayout.LayoutParams(i12 - iG, i13 - iG, 17));
        viewGroup.addView(frameLayout, i12, i13);
    }

    @Nullable
    public static ActivityManager.MemoryInfo zze(Context context) {
        ActivityManager activityManager;
        if (context == null || (activityManager = (ActivityManager) context.getSystemService("activity")) == null) {
            return null;
        }
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        try {
            activityManager.getMemoryInfo(memoryInfo);
        } catch (NullPointerException unused) {
            j.d("Error retrieving the memory information.");
        }
        return memoryInfo;
    }

    @Nullable
    public static String zzf(String str) {
        return zzE(str, "MD5");
    }

    @Nullable
    public static String zzg(String str) {
        return zzE(str, "SHA-256");
    }

    @Nullable
    public static v7.a zzq(p3 p3Var) {
        v7.a aVarZzr = zzr(p3Var);
        if (aVarZzr == null) {
            return null;
        }
        androidx.browser.trusted.k kVar = new androidx.browser.trusted.k(aVarZzr.f33737a);
        kVar.f914e = aVarZzr.f33738b;
        kVar.f911b = aVarZzr.f33739c;
        return new v7.a(kVar);
    }

    @Nullable
    public static v7.a zzr(@NonNull p3 p3Var) {
        String str = p3Var.f31009a;
        AdFormat adFormat = AdFormat.getAdFormat(p3Var.f31010b);
        if (adFormat == null) {
            return null;
        }
        g7.e eVar = new g7.e();
        a4 a4Var = p3Var.f31011c;
        List list = a4Var.f30897e;
        if (list != null) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                eVar.c((String) it.next());
            }
        }
        Object obj = eVar.f31277a;
        t2 t2Var = (t2) obj;
        t2Var.f31027b.putAll(a4Var.f30905m);
        Bundle bundle = a4Var.Q;
        for (String str2 : bundle.keySet()) {
            eVar.b(str2, bundle.getString(str2));
        }
        t2Var.f31038m = a4Var.zzx;
        String str3 = a4Var.f30904l;
        if (str3 != null) {
            eVar.p(str3);
        }
        eVar.q(a4Var.W);
        ((t2) obj).f31035j = a4Var.S;
        g7.f fVar = new g7.f(eVar);
        androidx.browser.trusted.k kVar = new androidx.browser.trusted.k(str, adFormat);
        kVar.f914e = fVar;
        kVar.f911b = p3Var.f31012d;
        return new v7.a(kVar);
    }

    @Nullable
    @VisibleForTesting
    public static final String zzv(StackTraceElement[] stackTraceElementArr, String str) {
        int i10;
        String className;
        int i11 = 0;
        while (true) {
            i10 = i11 + 1;
            if (i10 >= stackTraceElementArr.length) {
                className = null;
                break;
            }
            StackTraceElement stackTraceElement = stackTraceElementArr[i11];
            String className2 = stackTraceElement.getClassName();
            if ("loadAd".equalsIgnoreCase(stackTraceElement.getMethodName()) && (f32223c.equalsIgnoreCase(className2) || f32224d.equalsIgnoreCase(className2) || f32225e.equalsIgnoreCase(className2) || f32226f.equalsIgnoreCase(className2) || f32227g.equalsIgnoreCase(className2) || f32228h.equalsIgnoreCase(className2))) {
                break;
            }
            i11 = i10;
        }
        className = stackTraceElementArr[i10].getClassName();
        if (str != null) {
            StringTokenizer stringTokenizer = new StringTokenizer(str, ".");
            StringBuilder sb = new StringBuilder();
            if (stringTokenizer.hasMoreElements()) {
                sb.append(stringTokenizer.nextToken());
                for (int i12 = 2; i12 > 0 && stringTokenizer.hasMoreElements(); i12--) {
                    sb.append(".");
                    sb.append(stringTokenizer.nextToken());
                }
                str = sb.toString();
            }
            if (className != null && !className.contains(str)) {
                return className;
            }
        }
        return null;
    }

    public final JSONArray a(Collection collection) throws JSONException {
        JSONArray jSONArray = new JSONArray();
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            zzG(jSONArray, it.next());
        }
        return jSONArray;
    }

    public final int b(Context context, int i10) {
        if (this.f32229a < 0.0f) {
            synchronized (this) {
                try {
                    if (this.f32229a < 0.0f) {
                        WindowManager windowManager = (WindowManager) context.getSystemService("window");
                        if (windowManager == null) {
                            return 0;
                        }
                        Display defaultDisplay = windowManager.getDefaultDisplay();
                        DisplayMetrics displayMetrics = new DisplayMetrics();
                        defaultDisplay.getMetrics(displayMetrics);
                        this.f32229a = displayMetrics.density;
                    }
                } finally {
                }
            }
        }
        return Math.round(i10 / this.f32229a);
    }

    public final JSONObject c(Map map) throws JSONException {
        try {
            JSONObject jSONObject = new JSONObject();
            for (String str : map.keySet()) {
                zzH(jSONObject, str, map.get(str));
            }
            return jSONObject;
        } catch (ClassCastException e10) {
            throw new JSONException("Could not convert map to JSON: ".concat(String.valueOf(e10.getMessage())));
        }
    }

    public final JSONObject d(Bundle bundle) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        for (String str : bundle.keySet()) {
            zzH(jSONObject, str, bundle.get(str));
        }
        return jSONObject;
    }

    public final JSONArray e(Object[] objArr) throws JSONException {
        JSONArray jSONArray = new JSONArray();
        for (Object obj : objArr) {
            zzG(jSONArray, obj);
        }
        return jSONArray;
    }

    public final void zzb(ViewGroup viewGroup, f4 f4Var, @Nullable String str, @Nullable String str2) {
        if (str2 != null) {
            j.d(str2);
        }
        zzI(viewGroup, f4Var, str, -65536, -16777216);
    }

    public final void zzc(ViewGroup viewGroup, f4 f4Var, @Nullable String str) {
        zzI(viewGroup, f4Var, "Ads by Google", -16777216, -1);
    }

    public final void zzh(Context context, @Nullable String str, String str2, Bundle bundle, boolean z10) {
        zzB(context, str, "gmob-apps", bundle, true, new fe.h(this, context, 11));
    }

    public final JSONObject zzl(@Nullable Bundle bundle, JSONObject jSONObject) {
        if (bundle == null) {
            return null;
        }
        try {
            return d(bundle);
        } catch (JSONException unused) {
            return null;
        }
    }
}
