88 lines
3.8 KiB
Java
88 lines
3.8 KiB
Java
package androidx.savedstate;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.os.Bundle;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.lifecycle.GenericLifecycleObserver;
|
|
import androidx.lifecycle.Lifecycle;
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
import androidx.savedstate.SavedStateRegistry;
|
|
import c.d.b.a.a;
|
|
import java.lang.reflect.Constructor;
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.Set;
|
|
@SuppressLint({"RestrictedApi"})
|
|
public final class Recreator implements GenericLifecycleObserver {
|
|
public static final String CLASSES_KEY = "classes_to_restore";
|
|
public static final String COMPONENT_KEY = "androidx.savedstate.Restarter";
|
|
private final SavedStateRegistryOwner mOwner;
|
|
|
|
public static final class SavedStateProvider implements SavedStateRegistry.SavedStateProvider {
|
|
public final Set<String> mClasses = new HashSet();
|
|
|
|
public SavedStateProvider(SavedStateRegistry savedStateRegistry) {
|
|
savedStateRegistry.registerSavedStateProvider("androidx.savedstate.Restarter", this);
|
|
}
|
|
|
|
public void add(String str) {
|
|
this.mClasses.add(str);
|
|
}
|
|
|
|
@Override // androidx.savedstate.SavedStateRegistry.SavedStateProvider
|
|
@NonNull
|
|
public Bundle saveState() {
|
|
Bundle bundle = new Bundle();
|
|
bundle.putStringArrayList("classes_to_restore", new ArrayList<>(this.mClasses));
|
|
return bundle;
|
|
}
|
|
}
|
|
|
|
public Recreator(SavedStateRegistryOwner savedStateRegistryOwner) {
|
|
this.mOwner = savedStateRegistryOwner;
|
|
}
|
|
|
|
private void reflectiveNew(String str) {
|
|
Class cls;
|
|
try {
|
|
try {
|
|
Constructor declaredConstructor = Class.forName(str, false, Recreator.class.getClassLoader()).asSubclass(SavedStateRegistry.AutoRecreated.class).getDeclaredConstructor(new Class[0]);
|
|
declaredConstructor.setAccessible(true);
|
|
try {
|
|
((SavedStateRegistry.AutoRecreated) declaredConstructor.newInstance(new Object[0])).onRecreated(this.mOwner);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(a.u("Failed to instantiate ", str), e);
|
|
}
|
|
} catch (NoSuchMethodException e2) {
|
|
StringBuilder L = a.L("Class");
|
|
L.append(cls.getSimpleName());
|
|
L.append(" must have default constructor in order to be automatically recreated");
|
|
throw new IllegalStateException(L.toString(), e2);
|
|
}
|
|
} catch (ClassNotFoundException e3) {
|
|
throw new RuntimeException(a.v("Class ", str, " wasn't found"), e3);
|
|
}
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LifecycleEventObserver
|
|
public void onStateChanged(LifecycleOwner lifecycleOwner, Lifecycle.Event event) {
|
|
if (event == Lifecycle.Event.ON_CREATE) {
|
|
lifecycleOwner.getLifecycle().removeObserver(this);
|
|
Bundle consumeRestoredStateForKey = this.mOwner.getSavedStateRegistry().consumeRestoredStateForKey("androidx.savedstate.Restarter");
|
|
if (consumeRestoredStateForKey != null) {
|
|
ArrayList<String> stringArrayList = consumeRestoredStateForKey.getStringArrayList("classes_to_restore");
|
|
if (stringArrayList != null) {
|
|
Iterator<String> it = stringArrayList.iterator();
|
|
while (it.hasNext()) {
|
|
reflectiveNew(it.next());
|
|
}
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Bundle with restored state for the component \"androidx.savedstate.Restarter\" must contain list of strings by the key \"classes_to_restore\"");
|
|
}
|
|
return;
|
|
}
|
|
throw new AssertionError("Next event must be ON_CREATE");
|
|
}
|
|
}
|