package o;

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 com.google.android.gms.common.api.internal.BackgroundDetector;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.common.util.Base64Utils;
import com.google.android.gms.common.util.PlatformVersion;
import com.google.android.gms.common.util.ProcessUtils;
import com.google.firebase.FirebaseCommonRegistrar;
import com.google.firebase.components.ComponentDiscoveryService;
import com.google.firebase.concurrent.ExecutorsRegistrar;
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.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.Cy, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C0076Cy {
    public static final Object k = new Object();
    public static final C1839m9 l = new C1839m9(0);
    public final Context a;
    public final String b;
    public final C0439Qy c;
    public final C0450Rj d;
    public final AK g;
    public final DZ h;
    public final AtomicBoolean e = new AtomicBoolean(false);
    public final AtomicBoolean f = new AtomicBoolean();
    public final CopyOnWriteArrayList i = new CopyOnWriteArrayList();
    public final CopyOnWriteArrayList j = 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: r0v16, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v9, types: [java.util.ArrayList] */
    public C0076Cy(Context context, String str, C0439Qy c0439Qy) throws PackageManager.NameNotFoundException {
        ?? arrayList;
        int i = 0;
        this.a = (Context) Preconditions.checkNotNull(context);
        this.b = Preconditions.checkNotEmpty(str);
        this.c = (C0439Qy) Preconditions.checkNotNull(c0439Qy);
        C1586jc c1586jc = FirebaseInitProvider.l;
        Trace.beginSection("Firebase");
        Trace.beginSection("ComponentDiscovery");
        ArrayList arrayList2 = new ArrayList();
        Bundle bundle = null;
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                ServiceInfo serviceInfo = packageManager.getServiceInfo(new ComponentName(context, (Class<?>) ComponentDiscoveryService.class), 128);
                if (serviceInfo == null) {
                    Objects.toString(ComponentDiscoveryService.class);
                } else {
                    bundle = serviceInfo.metaData;
                }
            }
        } catch (PackageManager.NameNotFoundException unused) {
        }
        if (bundle == null) {
            arrayList = Collections.EMPTY_LIST;
        } 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 C0295Lj(i, (String) it.next()));
        }
        Trace.endSection();
        Trace.beginSection("Runtime");
        Dh0 dh0 = Dh0.l;
        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();
        arrayList3.addAll(arrayList2);
        int i2 = 1;
        arrayList3.add(new C0295Lj(i2, new FirebaseCommonRegistrar()));
        arrayList3.add(new C0295Lj(i2, new ExecutorsRegistrar()));
        arrayList4.add(C2549tj.c(context, Context.class, new Class[0]));
        arrayList4.add(C2549tj.c(this, C0076Cy.class, new Class[0]));
        arrayList4.add(C2549tj.c(c0439Qy, C0439Qy.class, new Class[0]));
        C0688a6 c0688a6 = new C0688a6(9);
        if (As0.J(context) && FirebaseInitProvider.m.get()) {
            arrayList4.add(C2549tj.c(c1586jc, C1586jc.class, new Class[0]));
        }
        C0450Rj c0450Rj = new C0450Rj(arrayList3, arrayList4, c0688a6);
        this.d = c0450Rj;
        Trace.endSection();
        this.g = new AK(new C0424Qj(2, this, context));
        this.h = c0450Rj.f(C0945cq.class);
        C3147zy c3147zy = new C3147zy(this);
        a();
        if (this.e.get()) {
            BackgroundDetector.getInstance().isInBackground();
        }
        this.i.add(c3147zy);
        Trace.endSection();
    }

    public static C0076Cy c() {
        C0076Cy c0076Cy;
        synchronized (k) {
            try {
                c0076Cy = (C0076Cy) l.get("[DEFAULT]");
                if (c0076Cy == null) {
                    throw new IllegalStateException("Default FirebaseApp is not initialized in this process " + ProcessUtils.getMyProcessName() + ". Make sure to call FirebaseApp.initializeApp(Context) first.");
                }
                ((C0945cq) c0076Cy.h.get()).b();
            } catch (Throwable th) {
                throw th;
            }
        }
        return c0076Cy;
    }

    public static C0076Cy f(Context context, C0439Qy c0439Qy) {
        C0076Cy c0076Cy;
        AtomicReference atomicReference = C0024Ay.a;
        if (PlatformVersion.isAtLeastIceCreamSandwich() && (context.getApplicationContext() instanceof Application)) {
            Application application = (Application) context.getApplicationContext();
            AtomicReference atomicReference2 = C0024Ay.a;
            if (atomicReference2.get() == null) {
                C0024Ay c0024Ay = new C0024Ay();
                while (true) {
                    if (atomicReference2.compareAndSet(null, c0024Ay)) {
                        BackgroundDetector.initialize(application);
                        BackgroundDetector.getInstance().addListener(c0024Ay);
                        break;
                    }
                    if (atomicReference2.get() != null) {
                        break;
                    }
                }
            }
        }
        if (context.getApplicationContext() != null) {
            context = context.getApplicationContext();
        }
        synchronized (k) {
            C1839m9 c1839m9 = l;
            Preconditions.checkState(!c1839m9.containsKey("[DEFAULT]"), "FirebaseApp name [DEFAULT] already exists!");
            Preconditions.checkNotNull(context, "Application context cannot be null.");
            c0076Cy = new C0076Cy(context, "[DEFAULT]", c0439Qy);
            c1839m9.put("[DEFAULT]", c0076Cy);
        }
        c0076Cy.e();
        return c0076Cy;
    }

    public static void g(Context context) {
        synchronized (k) {
            try {
                if (l.containsKey("[DEFAULT]")) {
                    c();
                    return;
                }
                C0439Qy c0439QyA = C0439Qy.a(context);
                if (c0439QyA == null) {
                    return;
                }
                f(context, c0439QyA);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void a() {
        Preconditions.checkState(!this.f.get(), "FirebaseApp was deleted");
    }

    public final Object b(Class cls) {
        a();
        return this.d.a(cls);
    }

    public final String d() {
        StringBuilder sb = new StringBuilder();
        a();
        sb.append(Base64Utils.encodeUrlSafeNoPadding(this.b.getBytes(Charset.defaultCharset())));
        sb.append("+");
        a();
        sb.append(Base64Utils.encodeUrlSafeNoPadding(this.c.b.getBytes(Charset.defaultCharset())));
        return sb.toString();
    }

    public final void e() {
        HashMap map;
        if (!As0.J(this.a)) {
            a();
            Context context = this.a;
            AtomicReference atomicReference = C0050By.b;
            if (atomicReference.get() == null) {
                C0050By c0050By = new C0050By(context);
                while (!atomicReference.compareAndSet(null, c0050By)) {
                    if (atomicReference.get() != null) {
                        return;
                    }
                }
                context.registerReceiver(c0050By, new IntentFilter("android.intent.action.USER_UNLOCKED"));
                return;
            }
            return;
        }
        a();
        C0450Rj c0450Rj = this.d;
        a();
        boolean zEquals = "[DEFAULT]".equals(this.b);
        AtomicReference atomicReference2 = (AtomicReference) c0450Rj.q;
        Boolean boolValueOf = Boolean.valueOf(zEquals);
        while (true) {
            if (atomicReference2.compareAndSet(null, boolValueOf)) {
                synchronized (c0450Rj) {
                    map = new HashMap((HashMap) c0450Rj.l);
                }
                c0450Rj.h(map, zEquals);
                break;
            } else if (atomicReference2.get() != null) {
                break;
            }
        }
        ((C0945cq) this.h.get()).b();
    }

    public final boolean equals(Object obj) {
        if (!(obj instanceof C0076Cy)) {
            return false;
        }
        C0076Cy c0076Cy = (C0076Cy) obj;
        c0076Cy.a();
        return this.b.equals(c0076Cy.b);
    }

    public final boolean h() {
        boolean z;
        a();
        C0663Zo c0663Zo = (C0663Zo) this.g.get();
        synchronized (c0663Zo) {
            z = c0663Zo.a;
        }
        return z;
    }

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

    public final String toString() {
        return com.google.android.gms.common.internal.Objects.toStringHelper(this).add("name", this.b).add("options", this.c).toString();
    }
}
