106 lines
5.1 KiB
Java
106 lines
5.1 KiB
Java
package androidx.lifecycle;
|
|
|
|
import android.os.Bundle;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.lifecycle.Lifecycle;
|
|
import androidx.savedstate.SavedStateRegistry;
|
|
import androidx.savedstate.SavedStateRegistryOwner;
|
|
public final class SavedStateHandleController implements LifecycleEventObserver {
|
|
public static final String TAG_SAVED_STATE_HANDLE_CONTROLLER = "androidx.lifecycle.savedstate.vm.tag";
|
|
private final SavedStateHandle mHandle;
|
|
private boolean mIsAttached = false;
|
|
private final String mKey;
|
|
|
|
/* renamed from: androidx.lifecycle.SavedStateHandleController$1 reason: invalid class name */
|
|
public class AnonymousClass1 implements LifecycleEventObserver {
|
|
public final /* synthetic */ Lifecycle val$lifecycle;
|
|
public final /* synthetic */ SavedStateRegistry val$registry;
|
|
|
|
public AnonymousClass1(Lifecycle lifecycle, SavedStateRegistry savedStateRegistry) {
|
|
this.val$lifecycle = lifecycle;
|
|
this.val$registry = savedStateRegistry;
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LifecycleEventObserver
|
|
public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {
|
|
if (event == Lifecycle.Event.ON_START) {
|
|
this.val$lifecycle.removeObserver(this);
|
|
this.val$registry.runOnNextRecreation(OnRecreation.class);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static final class OnRecreation implements SavedStateRegistry.AutoRecreated {
|
|
@Override // androidx.savedstate.SavedStateRegistry.AutoRecreated
|
|
public void onRecreated(@NonNull SavedStateRegistryOwner savedStateRegistryOwner) {
|
|
if (savedStateRegistryOwner instanceof ViewModelStoreOwner) {
|
|
ViewModelStore viewModelStore = ((ViewModelStoreOwner) savedStateRegistryOwner).getViewModelStore();
|
|
SavedStateRegistry savedStateRegistry = savedStateRegistryOwner.getSavedStateRegistry();
|
|
for (String str : viewModelStore.keys()) {
|
|
SavedStateHandleController.attachHandleIfNeeded(viewModelStore.get(str), savedStateRegistry, savedStateRegistryOwner.getLifecycle());
|
|
}
|
|
if (!viewModelStore.keys().isEmpty()) {
|
|
savedStateRegistry.runOnNextRecreation(OnRecreation.class);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Internal error: OnRecreation should be registered only on componentsthat implement ViewModelStoreOwner");
|
|
}
|
|
}
|
|
|
|
public SavedStateHandleController(String str, SavedStateHandle savedStateHandle) {
|
|
this.mKey = str;
|
|
this.mHandle = savedStateHandle;
|
|
}
|
|
|
|
public static void attachHandleIfNeeded(ViewModel viewModel, SavedStateRegistry savedStateRegistry, Lifecycle lifecycle) {
|
|
SavedStateHandleController savedStateHandleController = (SavedStateHandleController) viewModel.getTag("androidx.lifecycle.savedstate.vm.tag");
|
|
if (savedStateHandleController != null && !savedStateHandleController.isAttached()) {
|
|
savedStateHandleController.attachToLifecycle(savedStateRegistry, lifecycle);
|
|
tryToAddRecreator(savedStateRegistry, lifecycle);
|
|
}
|
|
}
|
|
|
|
public static SavedStateHandleController create(SavedStateRegistry savedStateRegistry, Lifecycle lifecycle, String str, Bundle bundle) {
|
|
SavedStateHandleController savedStateHandleController = new SavedStateHandleController(str, SavedStateHandle.createHandle(savedStateRegistry.consumeRestoredStateForKey(str), bundle));
|
|
savedStateHandleController.attachToLifecycle(savedStateRegistry, lifecycle);
|
|
tryToAddRecreator(savedStateRegistry, lifecycle);
|
|
return savedStateHandleController;
|
|
}
|
|
|
|
private static void tryToAddRecreator(SavedStateRegistry savedStateRegistry, Lifecycle lifecycle) {
|
|
Lifecycle.State currentState = lifecycle.getCurrentState();
|
|
if (currentState == Lifecycle.State.INITIALIZED || currentState.isAtLeast(Lifecycle.State.STARTED)) {
|
|
savedStateRegistry.runOnNextRecreation(OnRecreation.class);
|
|
} else {
|
|
lifecycle.addObserver(new AnonymousClass1(lifecycle, savedStateRegistry));
|
|
}
|
|
}
|
|
|
|
public void attachToLifecycle(SavedStateRegistry savedStateRegistry, Lifecycle lifecycle) {
|
|
if (!this.mIsAttached) {
|
|
this.mIsAttached = true;
|
|
lifecycle.addObserver(this);
|
|
savedStateRegistry.registerSavedStateProvider(this.mKey, this.mHandle.savedStateProvider());
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Already attached to lifecycleOwner");
|
|
}
|
|
|
|
public SavedStateHandle getHandle() {
|
|
return this.mHandle;
|
|
}
|
|
|
|
public boolean isAttached() {
|
|
return this.mIsAttached;
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LifecycleEventObserver
|
|
public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {
|
|
if (event == Lifecycle.Event.ON_DESTROY) {
|
|
this.mIsAttached = false;
|
|
lifecycleOwner.getLifecycle().removeObserver(this);
|
|
}
|
|
}
|
|
}
|