package c.p;

import android.content.Context;
import android.os.Bundle;
import android.os.Trace;
import com.ifpdos.debugmenu.R;
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 {
    public static volatile a a;

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

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

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

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

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

    public static a c(Context context) {
        if (a == null) {
            synchronized (f1919b) {
                if (a == null) {
                    a = new a(context);
                }
            }
        }
        return a;
    }

    public void a(Bundle bundle) {
        String string = this.f1922e.getString(R.string.androidx_startup);
        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 (b.class.isAssignableFrom(cls)) {
                            this.f1921d.add((Class<? extends b<?>>) cls);
                        }
                    }
                }
                Iterator<Class<? extends b<?>>> it = this.f1921d.iterator();
                while (it.hasNext()) {
                    b(it.next(), hashSet);
                }
            } catch (ClassNotFoundException e2) {
                throw new c(e2);
            }
        }
    }

    public final <T> T b(Class<? extends b<?>> cls, Set<Class<?>> set) {
        T t;
        if (c.n.a.K()) {
            try {
                Trace.beginSection(cls.getSimpleName());
            } finally {
                Trace.endSection();
            }
        }
        if (set.contains(cls)) {
            throw new IllegalStateException(String.format("Cannot initialize %s. Cycle detected.", cls.getName()));
        }
        if (this.f1920c.containsKey(cls)) {
            t = (T) this.f1920c.get(cls);
        } else {
            set.add(cls);
            try {
                b<?> bVarNewInstance = cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
                List<Class<? extends b<?>>> listA = bVarNewInstance.a();
                if (!listA.isEmpty()) {
                    for (Class<? extends b<?>> cls2 : listA) {
                        if (!this.f1920c.containsKey(cls2)) {
                            b(cls2, set);
                        }
                    }
                }
                t = (T) bVarNewInstance.b(this.f1922e);
                set.remove(cls);
                this.f1920c.put(cls, t);
            } catch (Throwable th) {
                throw new c(th);
            }
        }
        return t;
    }
}
