137 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			137 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package androidx.startup;
 | |
| 
 | |
| import android.content.ComponentName;
 | |
| import android.content.Context;
 | |
| import android.content.pm.PackageManager;
 | |
| import android.os.Bundle;
 | |
| import androidx.tracing.Trace;
 | |
| import java.util.HashMap;
 | |
| import java.util.HashSet;
 | |
| import java.util.Iterator;
 | |
| import java.util.List;
 | |
| import java.util.Map;
 | |
| import java.util.Set;
 | |
| 
 | |
| /* loaded from: classes.dex */
 | |
| public final class AppInitializer {
 | |
|     private static final String SECTION_NAME = "Startup";
 | |
|     private static volatile AppInitializer sInstance;
 | |
|     private static final Object sLock = new Object();
 | |
|     final Context mContext;
 | |
|     final Set<Class<? extends Initializer<?>>> mDiscovered = new HashSet();
 | |
|     final Map<Class<?>, Object> mInitialized = new HashMap();
 | |
| 
 | |
|     AppInitializer(Context context) {
 | |
|         this.mContext = context.getApplicationContext();
 | |
|     }
 | |
| 
 | |
|     public static AppInitializer getInstance(Context context) {
 | |
|         if (sInstance == null) {
 | |
|             synchronized (sLock) {
 | |
|                 if (sInstance == null) {
 | |
|                     sInstance = new AppInitializer(context);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         return sInstance;
 | |
|     }
 | |
| 
 | |
|     static void setDelegate(AppInitializer appInitializer) {
 | |
|         synchronized (sLock) {
 | |
|             sInstance = appInitializer;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public <T> T initializeComponent(Class<? extends Initializer<T>> cls) {
 | |
|         return (T) doInitialize(cls);
 | |
|     }
 | |
| 
 | |
|     public boolean isEagerlyInitialized(Class<? extends Initializer<?>> cls) {
 | |
|         return this.mDiscovered.contains(cls);
 | |
|     }
 | |
| 
 | |
|     <T> T doInitialize(Class<? extends Initializer<?>> cls) {
 | |
|         T t;
 | |
|         synchronized (sLock) {
 | |
|             t = (T) this.mInitialized.get(cls);
 | |
|             if (t == null) {
 | |
|                 t = (T) doInitialize(cls, new HashSet());
 | |
|             }
 | |
|         }
 | |
|         return t;
 | |
|     }
 | |
| 
 | |
|     private <T> T doInitialize(Class<? extends Initializer<?>> cls, Set<Class<?>> set) {
 | |
|         T t;
 | |
|         if (Trace.isEnabled()) {
 | |
|             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.mInitialized.containsKey(cls)) {
 | |
|             set.add(cls);
 | |
|             try {
 | |
|                 Initializer<?> newInstance = cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
 | |
|                 List<Class<? extends Initializer<?>>> dependencies = newInstance.dependencies();
 | |
|                 if (!dependencies.isEmpty()) {
 | |
|                     for (Class<? extends Initializer<?>> cls2 : dependencies) {
 | |
|                         if (!this.mInitialized.containsKey(cls2)) {
 | |
|                             doInitialize(cls2, set);
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
|                 t = (T) newInstance.create(this.mContext);
 | |
|                 set.remove(cls);
 | |
|                 this.mInitialized.put(cls, t);
 | |
|             } catch (Throwable th) {
 | |
|                 throw new StartupException(th);
 | |
|             }
 | |
|         } else {
 | |
|             t = (T) this.mInitialized.get(cls);
 | |
|         }
 | |
|         return t;
 | |
|     }
 | |
| 
 | |
|     void discoverAndInitialize() {
 | |
|         try {
 | |
|             try {
 | |
|                 Trace.beginSection(SECTION_NAME);
 | |
|                 discoverAndInitialize(this.mContext.getPackageManager().getProviderInfo(new ComponentName(this.mContext.getPackageName(), InitializationProvider.class.getName()), 128).metaData);
 | |
|             } catch (PackageManager.NameNotFoundException e) {
 | |
|                 throw new StartupException(e);
 | |
|             }
 | |
|         } finally {
 | |
|             Trace.endSection();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /* JADX WARN: Multi-variable type inference failed */
 | |
|     void discoverAndInitialize(Bundle bundle) {
 | |
|         String string = this.mContext.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 (Initializer.class.isAssignableFrom(cls)) {
 | |
|                             this.mDiscovered.add(cls);
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
|                 Iterator<Class<? extends Initializer<?>>> it = this.mDiscovered.iterator();
 | |
|                 while (it.hasNext()) {
 | |
|                     doInitialize(it.next(), hashSet);
 | |
|                 }
 | |
|             } catch (ClassNotFoundException e) {
 | |
|                 throw new StartupException(e);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 |