ADD week 5
This commit is contained in:
136
02-Easy5/E5/sources/androidx/startup/AppInitializer.java
Normal file
136
02-Easy5/E5/sources/androidx/startup/AppInitializer.java
Normal file
@ -0,0 +1,136 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package androidx.startup;
|
||||
|
||||
import android.content.ContentProvider;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class InitializationProvider extends ContentProvider {
|
||||
@Override // android.content.ContentProvider
|
||||
public final boolean onCreate() {
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
if (context.getApplicationContext() != null) {
|
||||
AppInitializer.getInstance(context).discoverAndInitialize();
|
||||
return true;
|
||||
}
|
||||
StartupLogger.w("Deferring initialization because `applicationContext` is null.");
|
||||
return true;
|
||||
}
|
||||
throw new StartupException("Context cannot be null");
|
||||
}
|
||||
|
||||
@Override // android.content.ContentProvider
|
||||
public final Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
|
||||
throw new IllegalStateException("Not allowed.");
|
||||
}
|
||||
|
||||
@Override // android.content.ContentProvider
|
||||
public final String getType(Uri uri) {
|
||||
throw new IllegalStateException("Not allowed.");
|
||||
}
|
||||
|
||||
@Override // android.content.ContentProvider
|
||||
public final Uri insert(Uri uri, ContentValues contentValues) {
|
||||
throw new IllegalStateException("Not allowed.");
|
||||
}
|
||||
|
||||
@Override // android.content.ContentProvider
|
||||
public final int delete(Uri uri, String str, String[] strArr) {
|
||||
throw new IllegalStateException("Not allowed.");
|
||||
}
|
||||
|
||||
@Override // android.content.ContentProvider
|
||||
public final int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
|
||||
throw new IllegalStateException("Not allowed.");
|
||||
}
|
||||
}
|
11
02-Easy5/E5/sources/androidx/startup/Initializer.java
Normal file
11
02-Easy5/E5/sources/androidx/startup/Initializer.java
Normal file
@ -0,0 +1,11 @@
|
||||
package androidx.startup;
|
||||
|
||||
import android.content.Context;
|
||||
import java.util.List;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public interface Initializer<T> {
|
||||
T create(Context context);
|
||||
|
||||
List<Class<? extends Initializer<?>>> dependencies();
|
||||
}
|
15
02-Easy5/E5/sources/androidx/startup/R.java
Normal file
15
02-Easy5/E5/sources/androidx/startup/R.java
Normal file
@ -0,0 +1,15 @@
|
||||
package androidx.startup;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public final class R {
|
||||
|
||||
public static final class string {
|
||||
public static final int androidx_startup = 0x7f0f001d;
|
||||
|
||||
private string() {
|
||||
}
|
||||
}
|
||||
|
||||
private R() {
|
||||
}
|
||||
}
|
16
02-Easy5/E5/sources/androidx/startup/StartupException.java
Normal file
16
02-Easy5/E5/sources/androidx/startup/StartupException.java
Normal file
@ -0,0 +1,16 @@
|
||||
package androidx.startup;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public final class StartupException extends RuntimeException {
|
||||
public StartupException(String str) {
|
||||
super(str);
|
||||
}
|
||||
|
||||
public StartupException(Throwable th) {
|
||||
super(th);
|
||||
}
|
||||
|
||||
public StartupException(String str, Throwable th) {
|
||||
super(str, th);
|
||||
}
|
||||
}
|
24
02-Easy5/E5/sources/androidx/startup/StartupLogger.java
Normal file
24
02-Easy5/E5/sources/androidx/startup/StartupLogger.java
Normal file
@ -0,0 +1,24 @@
|
||||
package androidx.startup;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public final class StartupLogger {
|
||||
static final boolean DEBUG = false;
|
||||
private static final String TAG = "StartupLogger";
|
||||
|
||||
private StartupLogger() {
|
||||
}
|
||||
|
||||
public static void i(String str) {
|
||||
Log.i(TAG, str);
|
||||
}
|
||||
|
||||
public static void w(String str) {
|
||||
Log.w(TAG, str);
|
||||
}
|
||||
|
||||
public static void e(String str, Throwable th) {
|
||||
Log.e(TAG, str, th);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user