package ua;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.Trace;
import android.os.UserManager;
import android.util.Base64;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.common.internal.v;
import com.google.android.gms.common.internal.w;
import com.google.android.gms.measurement.internal.c0;
import com.google.firebase.FirebaseCommonRegistrar;
import com.google.firebase.components.m;
import com.google.firebase.concurrent.ExecutorsRegistrar;
import com.google.firebase.concurrent.UiExecutor;
import com.google.firebase.provider.FirebaseInitProvider;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import xyz.stream.utils.j0;

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final androidx.collection.f f33400k = new androidx.collection.f(0);

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final com.google.firebase.components.h f33404d;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final m f33407g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final gb.a f33408h;

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final CopyOnWriteArrayList f33409i = new CopyOnWriteArrayList();

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v10, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v31, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v9, types: [java.util.ArrayList] */
    public g(Context context, i iVar, String str) {
        ?? arrayList;
        int i10 = 0;
        new CopyOnWriteArrayList();
        this.f33401a = (Context) w.checkNotNull(context);
        this.f33402b = w.checkNotEmpty(str);
        this.f33403c = (i) w.checkNotNull(iVar);
        j startupTime = FirebaseInitProvider.getStartupTime();
        Trace.beginSection("Firebase");
        Trace.beginSection("ComponentDiscovery");
        j0 j0Var = new j0();
        ArrayList arrayList2 = new ArrayList();
        Bundle bundle = null;
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                ServiceInfo serviceInfo = packageManager.getServiceInfo(new ComponentName(context, (Class<?>) j0Var.f36154b), 128);
                if (serviceInfo == null) {
                    Objects.toString((Class) j0Var.f36154b);
                } else {
                    bundle = serviceInfo.metaData;
                }
            }
        } catch (PackageManager.NameNotFoundException unused) {
        }
        if (bundle == null) {
            arrayList = Collections.emptyList();
        } else {
            arrayList = new ArrayList();
            for (String str2 : bundle.keySet()) {
                if ("com.google.firebase.components.ComponentRegistrar".equals(bundle.get(str2)) && str2.startsWith("com.google.firebase.components:")) {
                    arrayList.add(str2.substring(31));
                }
            }
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            arrayList2.add(new com.google.firebase.components.c((String) it.next(), i10));
        }
        Trace.endSection();
        Trace.beginSection("Runtime");
        UiExecutor uiExecutor = UiExecutor.f24051a;
        u.e eVar = new u.e();
        eVar.f33126b = new ArrayList();
        eVar.f33127c = new ArrayList();
        eVar.f33128d = com.google.firebase.components.f.D;
        eVar.f33125a = uiExecutor;
        ((List) eVar.f33126b).addAll(arrayList2);
        int i11 = 1;
        ((List) eVar.f33126b).add(new com.google.firebase.components.c(new FirebaseCommonRegistrar(), i11));
        ((List) eVar.f33126b).add(new com.google.firebase.components.c(new ExecutorsRegistrar(), i11));
        ((List) eVar.f33127c).add(com.google.firebase.components.a.c(context, Context.class, new Class[0]));
        ((List) eVar.f33127c).add(com.google.firebase.components.a.c(this, g.class, new Class[0]));
        ((List) eVar.f33127c).add(com.google.firebase.components.a.c(iVar, i.class, new Class[0]));
        eVar.f33128d = new c0(13);
        if (((UserManager) context.getSystemService(UserManager.class)).isUserUnlocked() && FirebaseInitProvider.f24291a.get()) {
            ((List) eVar.f33127c).add(com.google.firebase.components.a.c(startupTime, j.class, new Class[0]));
        }
        Executor executor = (Executor) eVar.f33125a;
        List list = (List) eVar.f33126b;
        com.google.firebase.components.h hVar = new com.google.firebase.components.h(executor, list, (List) eVar.f33127c, (com.google.firebase.components.f) eVar.f33128d);
        this.f33404d = hVar;
        Trace.endSection();
        this.f33407g = new m(new c(this, context, i10));
        this.f33408h = hVar.b(eb.d.class);
        d dVar = new d(this);
        a();
        if (this.f33405e.get()) {
            com.google.android.gms.common.api.internal.c.f8223e.f8224a.get();
        }
        this.f33409i.add(dVar);
        Trace.endSection();
    }

    public static g b() {
        g gVar;
        synchronized (f33399j) {
            try {
                gVar = (g) f33400k.get("[DEFAULT]");
                if (gVar == null) {
                    throw new IllegalStateException("Default FirebaseApp is not initialized in this process " + i8.g.getMyProcessName() + ". Make sure to call FirebaseApp.initializeApp(Context) first.");
                }
                ((eb.d) gVar.f33408h.get()).b();
            } catch (Throwable th) {
                throw th;
            }
        }
        return gVar;
    }

    public static g e(Context context, i iVar) {
        g gVar;
        AtomicReference atomicReference = e.f33396a;
        if (context.getApplicationContext() instanceof Application) {
            Application application = (Application) context.getApplicationContext();
            AtomicReference atomicReference2 = e.f33396a;
            if (atomicReference2.get() == null) {
                e eVar = new e();
                while (true) {
                    if (atomicReference2.compareAndSet(null, eVar)) {
                        com.google.android.gms.common.api.internal.c.a(application);
                        com.google.android.gms.common.api.internal.c cVar = com.google.android.gms.common.api.internal.c.f8223e;
                        cVar.getClass();
                        synchronized (cVar) {
                            cVar.f8226c.add(eVar);
                        }
                        break;
                    }
                    if (atomicReference2.get() != null) {
                        break;
                    }
                }
            }
        }
        if (context.getApplicationContext() != null) {
            context = context.getApplicationContext();
        }
        synchronized (f33399j) {
            androidx.collection.f fVar = f33400k;
            w.h(!fVar.containsKey("[DEFAULT]"), "FirebaseApp name [DEFAULT] already exists!");
            w.f(context, "Application context cannot be null.");
            gVar = new g(context, iVar, "[DEFAULT]");
            fVar.put("[DEFAULT]", gVar);
        }
        gVar.d();
        return gVar;
    }

    @Nullable
    public static g initializeApp(@NonNull Context context) {
        synchronized (f33399j) {
            try {
                if (f33400k.containsKey("[DEFAULT]")) {
                    return b();
                }
                i iVarFromResource = i.fromResource(context);
                if (iVarFromResource == null) {
                    return null;
                }
                return e(context, iVarFromResource);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void a() {
        w.h(!this.f33406f.get(), "FirebaseApp was deleted");
    }

    public final String c() {
        StringBuilder sb = new StringBuilder();
        a();
        byte[] bytes = this.f33402b.getBytes(Charset.defaultCharset());
        sb.append(bytes == null ? null : Base64.encodeToString(bytes, 11));
        sb.append("+");
        a();
        byte[] bytes2 = this.f33403c.f33416b.getBytes(Charset.defaultCharset());
        sb.append(bytes2 != null ? Base64.encodeToString(bytes2, 11) : null);
        return sb.toString();
    }

    public final void d() {
        HashMap map;
        if (!((UserManager) this.f33401a.getSystemService(UserManager.class)).isUserUnlocked()) {
            a();
            Context context = this.f33401a;
            AtomicReference atomicReference = f.f33397b;
            if (atomicReference.get() == null) {
                f fVar = new f(context);
                while (!atomicReference.compareAndSet(null, fVar)) {
                    if (atomicReference.get() != null) {
                        return;
                    }
                }
                context.registerReceiver(fVar, new IntentFilter("android.intent.action.USER_UNLOCKED"));
                return;
            }
            return;
        }
        a();
        com.google.firebase.components.h hVar = this.f33404d;
        a();
        boolean zEquals = "[DEFAULT]".equals(this.f33402b);
        AtomicReference atomicReference2 = hVar.f24018f;
        Boolean boolValueOf = Boolean.valueOf(zEquals);
        while (true) {
            if (atomicReference2.compareAndSet(null, boolValueOf)) {
                synchronized (hVar) {
                    map = new HashMap(hVar.f24013a);
                }
                hVar.f(zEquals, map);
                break;
            } else if (atomicReference2.get() != null) {
                break;
            }
        }
        ((eb.d) this.f33408h.get()).b();
    }

    public final boolean equals(Object obj) {
        if (!(obj instanceof g)) {
            return false;
        }
        g gVar = (g) obj;
        gVar.a();
        return this.f33402b.equals(gVar.f33402b);
    }

    public final int hashCode() {
        return this.f33402b.hashCode();
    }

    public final String toString() {
        return v.a(this).add("name", this.f33402b).add("options", this.f33403c).toString();
    }
}
