package androidx.savedstate; import android.os.Bundle; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleEventObserver; import androidx.lifecycle.LifecycleOwner; import androidx.savedstate.SavedStateRegistry; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; /* loaded from: classes.dex */ public final class Recreator implements LifecycleEventObserver { public static final String CLASSES_KEY = "classes_to_restore"; public static final String COMPONENT_KEY = "androidx.savedstate.Restarter"; public static final Companion Companion = new Companion(null); private final SavedStateRegistryOwner owner; public Recreator(SavedStateRegistryOwner owner) { Intrinsics.checkNotNullParameter(owner, "owner"); this.owner = owner; } @Override // androidx.lifecycle.LifecycleEventObserver public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { Intrinsics.checkNotNullParameter(source, "source"); Intrinsics.checkNotNullParameter(event, "event"); if (event != Lifecycle.Event.ON_CREATE) { throw new AssertionError("Next event must be ON_CREATE"); } source.getLifecycle().removeObserver(this); Bundle consumeRestoredStateForKey = this.owner.getSavedStateRegistry().consumeRestoredStateForKey(COMPONENT_KEY); if (consumeRestoredStateForKey == null) { return; } ArrayList stringArrayList = consumeRestoredStateForKey.getStringArrayList(CLASSES_KEY); if (stringArrayList == null) { throw new IllegalStateException("Bundle with restored state for the component \"androidx.savedstate.Restarter\" must contain list of strings by the key \"classes_to_restore\""); } Iterator it = stringArrayList.iterator(); while (it.hasNext()) { reflectiveNew(it.next()); } } private final void reflectiveNew(String str) { try { Class asSubclass = Class.forName(str, false, Recreator.class.getClassLoader()).asSubclass(SavedStateRegistry.AutoRecreated.class); Intrinsics.checkNotNullExpressionValue(asSubclass, "{\n Class.…class.java)\n }"); try { Constructor declaredConstructor = asSubclass.getDeclaredConstructor(new Class[0]); declaredConstructor.setAccessible(true); try { Object newInstance = declaredConstructor.newInstance(new Object[0]); Intrinsics.checkNotNullExpressionValue(newInstance, "{\n constr…wInstance()\n }"); ((SavedStateRegistry.AutoRecreated) newInstance).onRecreated(this.owner); } catch (Exception e) { throw new RuntimeException("Failed to instantiate " + str, e); } } catch (NoSuchMethodException e2) { throw new IllegalStateException("Class " + asSubclass.getSimpleName() + " must have default constructor in order to be automatically recreated", e2); } } catch (ClassNotFoundException e3) { throw new RuntimeException("Class " + str + " wasn't found", e3); } } public static final class SavedStateProvider implements SavedStateRegistry.SavedStateProvider { private final Set classes; public SavedStateProvider(SavedStateRegistry registry) { Intrinsics.checkNotNullParameter(registry, "registry"); this.classes = new LinkedHashSet(); registry.registerSavedStateProvider(Recreator.COMPONENT_KEY, this); } @Override // androidx.savedstate.SavedStateRegistry.SavedStateProvider public Bundle saveState() { Bundle bundle = new Bundle(); bundle.putStringArrayList(Recreator.CLASSES_KEY, new ArrayList<>(this.classes)); return bundle; } public final void add(String className) { Intrinsics.checkNotNullParameter(className, "className"); this.classes.add(className); } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } }