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

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.gms.dynamite.descriptors.com.google.android.gms.ads.dynamite.ModuleDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

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

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

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

    @Nullable
    private String zzd = null;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AtomicInteger f18630d = new AtomicInteger(-1);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final AtomicReference f18631e = new AtomicReference(null);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final AtomicReference f18632f = new AtomicReference(null);

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

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

    public static final boolean f(Context context) {
        tn tnVar = ao.K0;
        n7.y yVar = n7.y.f31093e;
        if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            return false;
        }
        int iA = l8.c.a(context, ModuleDescriptor.MODULE_ID);
        tn tnVar2 = ao.L0;
        yn ynVar = yVar.f31096c;
        if (iA < ((Integer) ynVar.b(tnVar2)).intValue()) {
            return false;
        }
        if (((Boolean) ynVar.b(ao.M0)).booleanValue()) {
            try {
                context.getClassLoader().loadClass("com.google.firebase.analytics.FirebaseAnalytics");
                return false;
            } catch (ClassNotFoundException unused) {
            }
        }
        return true;
    }

    public static final Bundle zzq(@Nullable Map map) {
        Bundle bundle = new Bundle();
        if (map != null) {
            for (String str : map.keySet()) {
                try {
                    if (Objects.equals(str, "value")) {
                        bundle.putDouble(str, Double.parseDouble((String) map.get(str)));
                    } else {
                        bundle.putString(str, (String) map.get(str));
                    }
                } catch (NullPointerException | NumberFormatException unused) {
                }
            }
        }
        return bundle;
    }

    private final void zzs(Context context, String str, String str2, @Nullable Bundle bundle) {
        if (a(context)) {
            Bundle bundle2 = new Bundle();
            try {
                bundle2.putLong("_aeid", Long.parseLong(str2));
            } catch (NullPointerException | NumberFormatException unused) {
                "Invalid event ID: ".concat(String.valueOf(str2));
            }
            if ("_ac".equals(str)) {
                bundle2.putInt("_r", 1);
            }
            if (bundle != null) {
                bundle2.putAll(bundle);
            }
            AtomicReference atomicReference = this.f18631e;
            if (i(context, "com.google.android.gms.measurement.AppMeasurement", atomicReference, true)) {
                ConcurrentHashMap concurrentHashMap = this.f18633g;
                Method declaredMethod = (Method) concurrentHashMap.get("logEventInternal");
                if (declaredMethod == null) {
                    try {
                        declaredMethod = context.getClassLoader().loadClass("com.google.android.gms.measurement.AppMeasurement").getDeclaredMethod("logEventInternal", String.class, String.class, Bundle.class);
                        concurrentHashMap.put("logEventInternal", declaredMethod);
                    } catch (Exception unused2) {
                        h("logEventInternal", true);
                        declaredMethod = null;
                    }
                }
                try {
                    declaredMethod.invoke(atomicReference.get(), "am", str, bundle2);
                } catch (Exception unused3) {
                    h("logEventInternal", true);
                }
            }
        }
    }

    @Nullable
    private final Method zzt(Context context, String str) {
        ConcurrentHashMap concurrentHashMap = this.f18633g;
        Method method = (Method) concurrentHashMap.get(str);
        if (method != null) {
            return method;
        }
        try {
            Method declaredMethod = context.getClassLoader().loadClass("com.google.android.gms.measurement.AppMeasurement").getDeclaredMethod(str, new Class[0]);
            concurrentHashMap.put(str, declaredMethod);
            return declaredMethod;
        } catch (Exception unused) {
            h(str, false);
            return null;
        }
    }

    @Nullable
    private final Object zzv(String str, Context context) {
        AtomicReference atomicReference = this.f18631e;
        if (!i(context, "com.google.android.gms.measurement.AppMeasurement", atomicReference, true)) {
            return null;
        }
        try {
            return zzt(context, str).invoke(atomicReference.get(), new Object[0]);
        } catch (Exception unused) {
            h(str, true);
            return null;
        }
    }

    public final boolean a(Context context) {
        int iB;
        tn tnVar = ao.C0;
        n7.y yVar = n7.y.f31093e;
        if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue() && !this.f18629c.get()) {
            if (((Boolean) yVar.f31096c.b(ao.N0)).booleanValue()) {
                return true;
            }
            AtomicInteger atomicInteger = this.f18630d;
            if (atomicInteger.get() == -1) {
                q7.e eVar = n7.x.f31085g.f31086a;
                e8.e eVar2 = e8.e.f25522b;
                if (eVar2.b(context, 12451000) != 0 && ((iB = eVar2.b(context, 12451000)) == 0 || iB == 2)) {
                    q7.j.d("Google Play Service is out of date, the Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires updated Google Play Service.");
                    atomicInteger.set(0);
                } else {
                    atomicInteger.set(1);
                }
            }
            if (atomicInteger.get() == 1) {
                return true;
            }
        }
        return false;
    }

    public final String b(Context context) {
        if (a(context)) {
            AtomicReference atomicReference = this.f18631e;
            if (i(context, "com.google.android.gms.measurement.AppMeasurement", atomicReference, true)) {
                try {
                    String str = (String) zzt(context, "getCurrentScreenName").invoke(atomicReference.get(), new Object[0]);
                    if (str == null) {
                        str = (String) zzt(context, "getCurrentScreenClass").invoke(atomicReference.get(), new Object[0]);
                    }
                    return str == null ? "" : str;
                } catch (Exception unused) {
                    h("getCurrentScreenName", false);
                }
            }
        }
        return "";
    }

    public final void c(Context context, String str) {
        zzs(context, "_aq", str, null);
    }

    public final void d(Context context, String str) {
        zzs(context, "_aa", str, null);
    }

    public final /* synthetic */ String e(Context context) {
        return (String) zzv("getAppInstanceId", context);
    }

    public final void g(Context context, String str, String str2) {
        AtomicReference atomicReference = this.f18631e;
        if (i(context, "com.google.android.gms.measurement.AppMeasurement", atomicReference, true)) {
            ConcurrentHashMap concurrentHashMap = this.f18633g;
            Method declaredMethod = (Method) concurrentHashMap.get(str2);
            if (declaredMethod == null) {
                try {
                    declaredMethod = context.getClassLoader().loadClass("com.google.android.gms.measurement.AppMeasurement").getDeclaredMethod(str2, String.class);
                    concurrentHashMap.put(str2, declaredMethod);
                } catch (Exception unused) {
                    h(str2, false);
                    declaredMethod = null;
                }
            }
            try {
                declaredMethod.invoke(atomicReference.get(), str);
                StringBuilder sb = new StringBuilder(str2.length() + 37 + String.valueOf(str).length());
                sb.append("Invoke Firebase method ");
                sb.append(str2);
                sb.append(", Ad Unit Id: ");
                sb.append(str);
                p7.l0.g(sb.toString());
            } catch (Exception unused2) {
                h(str2, false);
            }
        }
    }

    public final void h(String str, boolean z10) {
        AtomicBoolean atomicBoolean = this.f18629c;
        if (atomicBoolean.get()) {
            return;
        }
        StringBuilder sb = new StringBuilder(str.length() + 30);
        sb.append("Invoke Firebase method ");
        sb.append(str);
        sb.append(" error.");
        q7.j.d(sb.toString());
        if (z10) {
            q7.j.d("The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date");
            atomicBoolean.set(true);
        }
    }

    public final boolean i(Context context, String str, AtomicReference atomicReference, boolean z10) {
        if (atomicReference.get() == null) {
            try {
                Object objInvoke = context.getClassLoader().loadClass(str).getDeclaredMethod("getInstance", Context.class).invoke(null, context);
                while (!atomicReference.compareAndSet(null, objInvoke) && atomicReference.get() == null) {
                }
            } catch (Exception unused) {
                h("getInstance", z10);
                return false;
            }
        }
        return true;
    }

    @Nullable
    public final String zzh(Context context) {
        if (!a(context)) {
            return null;
        }
        synchronized (this.f18628b) {
            try {
                String str = this.zzd;
                if (str != null) {
                    return str;
                }
                String str2 = (String) zzv("getGmpAppId", context);
                this.zzd = str2;
                return str2;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    public final String zzi(Context context) {
        if (!a(context)) {
            return null;
        }
        tn tnVar = ao.I0;
        n7.y yVar = n7.y.f31093e;
        long jLongValue = ((Long) yVar.f31096c.b(tnVar)).longValue();
        if (jLongValue < 0) {
            return (String) zzv("getAppInstanceId", context);
        }
        AtomicReference atomicReference = this.f18627a;
        if (atomicReference.get() == null) {
            tn tnVar2 = ao.J0;
            yn ynVar = yVar.f31096c;
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(((Integer) ynVar.b(tnVar2)).intValue(), ((Integer) ynVar.b(tnVar2)).intValue(), 1L, TimeUnit.MINUTES, new LinkedBlockingQueue(), new x10(this));
            while (!atomicReference.compareAndSet(null, threadPoolExecutor) && atomicReference.get() == null) {
            }
        }
        try {
            return (String) ((ExecutorService) atomicReference.get()).submit(new hg(this, context, 3)).get(jLongValue, TimeUnit.MILLISECONDS);
        } catch (TimeoutException unused) {
            return "TIME_OUT";
        } catch (Exception unused2) {
            return null;
        }
    }

    @Nullable
    public final String zzj(Context context) {
        Object objZzv;
        if (a(context) && (objZzv = zzv("generateEventId", context)) != null) {
            return objZzv.toString();
        }
        return null;
    }

    public final void zzk(Context context, @Nullable String str, @Nullable Map map) {
        zzs(context, "_ac", str, zzq(map));
    }

    public final void zzl(Context context, @Nullable String str, @Nullable Map map) {
        zzs(context, "_ai", str, zzq(map));
    }

    public final void zzo(Context context, @Nullable String str, String str2, String str3, int i10) {
        if (a(context)) {
            Bundle bundle = new Bundle();
            bundle.putString("_ai", str2);
            bundle.putString("reward_type", str3);
            bundle.putInt("reward_value", i10);
            zzs(context, "_ar", str, bundle);
            StringBuilder sb = new StringBuilder(String.valueOf(str3).length() + 64 + String.valueOf(i10).length());
            sb.append("Log a Firebase reward video event, reward type: ");
            sb.append(str3);
            sb.append(", reward value: ");
            sb.append(i10);
            p7.l0.g(sb.toString());
        }
    }
}
