package androidx.startup;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import h0.b;
import h0.c;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/* JADX INFO: compiled from: AppInitializer.java */
/* JADX INFO: loaded from: classes.dex */
public final class a {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static volatile a f3016d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static final Object f3017e = new Object();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    final Context f3020c;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    final Set<Class<? extends h0.a<?>>> f3019b = new HashSet();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    final Map<Class<?>, Object> f3018a = new HashMap();

    a(Context context) {
        this.f3020c = context.getApplicationContext();
    }

    private <T> T d(Class<? extends h0.a<?>> cls, Set<Class<?>> set) {
        T t2;
        if (i0.a.d()) {
            try {
                i0.a.a(cls.getSimpleName());
            } finally {
                i0.a.b();
            }
        }
        if (set.contains(cls)) {
            throw new IllegalStateException(String.format("Cannot initialize %s. Cycle detected.", cls.getName()));
        }
        if (this.f3018a.containsKey(cls)) {
            t2 = (T) this.f3018a.get(cls);
        } else {
            set.add(cls);
            try {
                h0.a<?> aVarNewInstance = cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
                List<Class<? extends h0.a<?>>> listA = aVarNewInstance.a();
                if (!listA.isEmpty()) {
                    for (Class<? extends h0.a<?>> cls2 : listA) {
                        if (!this.f3018a.containsKey(cls2)) {
                            d(cls2, set);
                        }
                    }
                }
                t2 = (T) aVarNewInstance.b(this.f3020c);
                set.remove(cls);
                this.f3018a.put(cls, t2);
            } catch (Throwable th) {
                throw new c(th);
            }
        }
        return t2;
    }

    public static a e(Context context) {
        if (f3016d == null) {
            synchronized (f3017e) {
                if (f3016d == null) {
                    f3016d = new a(context);
                }
            }
        }
        return f3016d;
    }

    void a() {
        try {
            try {
                i0.a.a("Startup");
                b(this.f3020c.getPackageManager().getProviderInfo(new ComponentName(this.f3020c.getPackageName(), InitializationProvider.class.getName()), 128).metaData);
            } catch (PackageManager.NameNotFoundException e3) {
                throw new c(e3);
            }
        } finally {
            i0.a.b();
        }
    }

    void b(Bundle bundle) {
        String string = this.f3020c.getString(b.f4786a);
        if (bundle != null) {
            try {
                HashSet hashSet = new HashSet();
                for (String str : bundle.keySet()) {
                    if (string.equals(bundle.getString(str, null))) {
                        Class<?> cls = Class.forName(str);
                        if (h0.a.class.isAssignableFrom(cls)) {
                            this.f3019b.add((Class<? extends h0.a<?>>) cls);
                        }
                    }
                }
                Iterator<Class<? extends h0.a<?>>> it = this.f3019b.iterator();
                while (it.hasNext()) {
                    d(it.next(), hashSet);
                }
            } catch (ClassNotFoundException e3) {
                throw new c(e3);
            }
        }
    }

    <T> T c(Class<? extends h0.a<?>> cls) {
        T t2;
        synchronized (f3017e) {
            t2 = (T) this.f3018a.get(cls);
            if (t2 == null) {
                t2 = (T) d(cls, new HashSet());
            }
        }
        return t2;
    }

    public <T> T f(Class<? extends h0.a<T>> cls) {
        return (T) c(cls);
    }

    public boolean g(Class<? extends h0.a<?>> cls) {
        return this.f3019b.contains(cls);
    }
}
