package o3;

import android.content.Context;
import android.os.Bundle;
import android.os.Trace;
import androidx.annotation.Nullable;
import androidx.startup.StartupException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

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

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

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

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

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final HashMap f31371a = new HashMap();

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

    public static a b(Context context) {
        if (f31369d == null) {
            synchronized (f31370e) {
                try {
                    if (f31369d == null) {
                        f31369d = new a(context);
                    }
                } finally {
                }
            }
        }
        return f31369d;
    }

    public final Object a(Class cls, HashSet hashSet) {
        Object objA;
        if (androidx.datastore.preferences.core.c.m()) {
            try {
                Trace.beginSection(androidx.datastore.preferences.core.c.t(cls.getSimpleName()));
            } catch (Throwable th) {
                Trace.endSection();
                throw th;
            }
        }
        if (hashSet.contains(cls)) {
            throw new IllegalStateException(String.format("Cannot initialize %s. Cycle detected.", cls.getName()));
        }
        HashMap map = this.f31371a;
        if (map.containsKey(cls)) {
            objA = map.get(cls);
        } else {
            hashSet.add(cls);
            try {
                b bVar = (b) cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
                List<Class> listDependencies = bVar.dependencies();
                if (!listDependencies.isEmpty()) {
                    for (Class cls2 : listDependencies) {
                        if (!map.containsKey(cls2)) {
                            a(cls2, hashSet);
                        }
                    }
                }
                objA = bVar.a(this.f31373c);
                hashSet.remove(cls);
                map.put(cls, objA);
            } catch (Throwable th2) {
                throw new StartupException(th2);
            }
        }
        Trace.endSection();
        return objA;
    }

    public void discoverAndInitialize(@Nullable Bundle bundle) {
        HashSet hashSet;
        String string = this.f31373c.getString(c.androidx_startup);
        if (bundle != null) {
            try {
                HashSet hashSet2 = new HashSet();
                Iterator<String> it = bundle.keySet().iterator();
                while (true) {
                    boolean zHasNext = it.hasNext();
                    hashSet = this.f31372b;
                    if (!zHasNext) {
                        break;
                    }
                    String next = it.next();
                    if (string.equals(bundle.getString(next, null))) {
                        Class<?> cls = Class.forName(next);
                        if (b.class.isAssignableFrom(cls)) {
                            hashSet.add(cls);
                        }
                    }
                }
                Iterator it2 = hashSet.iterator();
                while (it2.hasNext()) {
                    a((Class) it2.next(), hashSet2);
                }
            } catch (ClassNotFoundException e10) {
                throw new StartupException(e10);
            }
        }
    }
}
