package androidx.startup;

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

/* loaded from: classes.dex */
public final class a {

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

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

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

    /* renamed from: b, reason: collision with root package name */
    final Set f5638b = new HashSet();

    /* renamed from: a, reason: collision with root package name */
    final Map f5637a = new HashMap();

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

    private Object d(Class cls, Set set) {
        Object obj;
        if (t5.a.d()) {
            try {
                t5.a.a(cls.getSimpleName());
            } finally {
                t5.a.b();
            }
        }
        if (set.contains(cls)) {
            throw new IllegalStateException(String.format("Cannot initialize %s. Cycle detected.", cls.getName()));
        }
        if (this.f5637a.containsKey(cls)) {
            obj = this.f5637a.get(cls);
        } else {
            set.add(cls);
            try {
                r5.a aVar = (r5.a) cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
                List<Class> a10 = aVar.a();
                if (!a10.isEmpty()) {
                    for (Class cls2 : a10) {
                        if (!this.f5637a.containsKey(cls2)) {
                            d(cls2, set);
                        }
                    }
                }
                obj = aVar.b(this.f5639c);
                set.remove(cls);
                this.f5637a.put(cls, obj);
            } catch (Throwable th2) {
                throw new c(th2);
            }
        }
        return obj;
    }

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

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a() {
        try {
            try {
                t5.a.a("Startup");
                b(this.f5639c.getPackageManager().getProviderInfo(new ComponentName(this.f5639c.getPackageName(), InitializationProvider.class.getName()), 128).metaData);
            } catch (PackageManager.NameNotFoundException e10) {
                throw new c(e10);
            }
        } finally {
            t5.a.b();
        }
    }

    void b(Bundle bundle) {
        String string = this.f5639c.getString(b.f34451a);
        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 (r5.a.class.isAssignableFrom(cls)) {
                            this.f5638b.add(cls);
                        }
                    }
                }
                Iterator it = this.f5638b.iterator();
                while (it.hasNext()) {
                    d((Class) it.next(), hashSet);
                }
            } catch (ClassNotFoundException e10) {
                throw new c(e10);
            }
        }
    }

    Object c(Class cls) {
        Object obj;
        synchronized (f5636e) {
            obj = this.f5637a.get(cls);
            if (obj == null) {
                obj = d(cls, new HashSet());
            }
        }
        return obj;
    }

    public Object f(Class cls) {
        return c(cls);
    }

    public boolean g(Class cls) {
        return this.f5638b.contains(cls);
    }
}
