package androidx.lifecycle; import android.os.Binder; import android.os.Bundle; import android.os.Parcelable; import android.util.Size; import android.util.SizeF; import android.util.SparseArray; import androidx.annotation.MainThread; import androidx.annotation.RestrictTo; import androidx.core.os.BundleKt; import androidx.savedstate.SavedStateRegistry; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import kotlin.TuplesKt; import kotlin.collections.MapsKt__MapsKt; import kotlin.collections.SetsKt___SetsKt; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.SourceDebugExtension; import kotlinx.coroutines.flow.FlowKt; import kotlinx.coroutines.flow.MutableStateFlow; import kotlinx.coroutines.flow.StateFlow; import kotlinx.coroutines.flow.StateFlowKt; @SourceDebugExtension({"SMAP\nSavedStateHandle.kt\nKotlin\n*S Kotlin\n*F\n+ 1 SavedStateHandle.kt\nandroidx/lifecycle/SavedStateHandle\n+ 2 Maps.kt\nkotlin/collections/MapsKt__MapsKt\n+ 3 fake.kt\nkotlin/jvm/internal/FakeKt\n*L\n1#1,450:1\n361#2,3:451\n364#2,4:455\n1#3:454\n*S KotlinDebug\n*F\n+ 1 SavedStateHandle.kt\nandroidx/lifecycle/SavedStateHandle\n*L\n198#1:451,3\n198#1:455,4\n*E\n"}) /* loaded from: classes.dex */ public final class SavedStateHandle { private static final String KEYS = "keys"; private static final String VALUES = "values"; private final Map flows; private final Map> liveDatas; private final Map regular; private final SavedStateRegistry.SavedStateProvider savedStateProvider; private final Map savedStateProviders; public static final Companion Companion = new Companion(null); private static final Class[] ACCEPTABLE_CLASSES = {Boolean.TYPE, boolean[].class, Double.TYPE, double[].class, Integer.TYPE, int[].class, Long.TYPE, long[].class, String.class, String[].class, Binder.class, Bundle.class, Byte.TYPE, byte[].class, Character.TYPE, char[].class, CharSequence.class, CharSequence[].class, ArrayList.class, Float.TYPE, float[].class, Parcelable.class, Parcelable[].class, Serializable.class, Short.TYPE, short[].class, SparseArray.class, Size.class, SizeF.class}; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) public static final SavedStateHandle createHandle(Bundle bundle, Bundle bundle2) { return Companion.createHandle(bundle, bundle2); } @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) public final SavedStateRegistry.SavedStateProvider savedStateProvider() { return this.savedStateProvider; } /* JADX INFO: Access modifiers changed from: private */ public static final Bundle savedStateProvider$lambda$0(SavedStateHandle this$0) { Map map; Intrinsics.checkNotNullParameter(this$0, "this$0"); map = MapsKt__MapsKt.toMap(this$0.savedStateProviders); for (Map.Entry entry : map.entrySet()) { this$0.set((String) entry.getKey(), ((SavedStateRegistry.SavedStateProvider) entry.getValue()).saveState()); } Set keySet = this$0.regular.keySet(); ArrayList arrayList = new ArrayList(keySet.size()); ArrayList arrayList2 = new ArrayList(arrayList.size()); for (String str : keySet) { arrayList.add(str); arrayList2.add(this$0.regular.get(str)); } return BundleKt.bundleOf(TuplesKt.to(KEYS, arrayList), TuplesKt.to(VALUES, arrayList2)); } public SavedStateHandle(Map initialState) { Intrinsics.checkNotNullParameter(initialState, "initialState"); LinkedHashMap linkedHashMap = new LinkedHashMap(); this.regular = linkedHashMap; this.savedStateProviders = new LinkedHashMap(); this.liveDatas = new LinkedHashMap(); this.flows = new LinkedHashMap(); this.savedStateProvider = new SavedStateRegistry.SavedStateProvider() { // from class: androidx.lifecycle.SavedStateHandle$$ExternalSyntheticLambda0 @Override // androidx.savedstate.SavedStateRegistry.SavedStateProvider public final Bundle saveState() { Bundle savedStateProvider$lambda$0; savedStateProvider$lambda$0 = SavedStateHandle.savedStateProvider$lambda$0(SavedStateHandle.this); return savedStateProvider$lambda$0; } }; linkedHashMap.putAll(initialState); } public SavedStateHandle() { this.regular = new LinkedHashMap(); this.savedStateProviders = new LinkedHashMap(); this.liveDatas = new LinkedHashMap(); this.flows = new LinkedHashMap(); this.savedStateProvider = new SavedStateRegistry.SavedStateProvider() { // from class: androidx.lifecycle.SavedStateHandle$$ExternalSyntheticLambda0 @Override // androidx.savedstate.SavedStateRegistry.SavedStateProvider public final Bundle saveState() { Bundle savedStateProvider$lambda$0; savedStateProvider$lambda$0 = SavedStateHandle.savedStateProvider$lambda$0(SavedStateHandle.this); return savedStateProvider$lambda$0; } }; } @MainThread public final boolean contains(String key) { Intrinsics.checkNotNullParameter(key, "key"); return this.regular.containsKey(key); } @MainThread public final MutableLiveData getLiveData(String key) { Intrinsics.checkNotNullParameter(key, "key"); MutableLiveData liveDataInternal = getLiveDataInternal(key, false, null); Intrinsics.checkNotNull(liveDataInternal, "null cannot be cast to non-null type androidx.lifecycle.MutableLiveData"); return liveDataInternal; } @MainThread public final MutableLiveData getLiveData(String key, T t) { Intrinsics.checkNotNullParameter(key, "key"); return getLiveDataInternal(key, true, t); } private final MutableLiveData getLiveDataInternal(String str, boolean z, T t) { SavingStateLiveData savingStateLiveData; SavingStateLiveData savingStateLiveData2 = this.liveDatas.get(str); SavingStateLiveData savingStateLiveData3 = savingStateLiveData2 instanceof MutableLiveData ? savingStateLiveData2 : null; if (savingStateLiveData3 != null) { return savingStateLiveData3; } if (this.regular.containsKey(str)) { savingStateLiveData = new SavingStateLiveData<>(this, str, this.regular.get(str)); } else if (z) { this.regular.put(str, t); savingStateLiveData = new SavingStateLiveData<>(this, str, t); } else { savingStateLiveData = new SavingStateLiveData<>(this, str); } this.liveDatas.put(str, savingStateLiveData); return savingStateLiveData; } @MainThread public final Set keys() { Set plus; Set plus2; plus = SetsKt___SetsKt.plus((Set) this.regular.keySet(), (Iterable) this.savedStateProviders.keySet()); plus2 = SetsKt___SetsKt.plus(plus, (Iterable) this.liveDatas.keySet()); return plus2; } @MainThread public final T get(String key) { Intrinsics.checkNotNullParameter(key, "key"); try { return (T) this.regular.get(key); } catch (ClassCastException unused) { remove(key); return null; } } @MainThread public final void set(String key, T t) { Intrinsics.checkNotNullParameter(key, "key"); if (!Companion.validateValue(t)) { StringBuilder sb = new StringBuilder(); sb.append("Can't put value with type "); Intrinsics.checkNotNull(t); sb.append(t.getClass()); sb.append(" into saved state"); throw new IllegalArgumentException(sb.toString()); } SavingStateLiveData savingStateLiveData = this.liveDatas.get(key); SavingStateLiveData savingStateLiveData2 = savingStateLiveData instanceof MutableLiveData ? savingStateLiveData : null; if (savingStateLiveData2 != null) { savingStateLiveData2.setValue(t); } else { this.regular.put(key, t); } MutableStateFlow mutableStateFlow = this.flows.get(key); if (mutableStateFlow == null) { return; } mutableStateFlow.setValue(t); } @MainThread public final T remove(String key) { Intrinsics.checkNotNullParameter(key, "key"); T t = (T) this.regular.remove(key); SavingStateLiveData remove = this.liveDatas.remove(key); if (remove != null) { remove.detach(); } this.flows.remove(key); return t; } @MainThread public final void setSavedStateProvider(String key, SavedStateRegistry.SavedStateProvider provider) { Intrinsics.checkNotNullParameter(key, "key"); Intrinsics.checkNotNullParameter(provider, "provider"); this.savedStateProviders.put(key, provider); } @MainThread public final void clearSavedStateProvider(String key) { Intrinsics.checkNotNullParameter(key, "key"); this.savedStateProviders.remove(key); } public static final class SavingStateLiveData extends MutableLiveData { private SavedStateHandle handle; private String key; public final void detach() { this.handle = null; } /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public SavingStateLiveData(SavedStateHandle savedStateHandle, String key, T t) { super(t); Intrinsics.checkNotNullParameter(key, "key"); this.key = key; this.handle = savedStateHandle; } public SavingStateLiveData(SavedStateHandle savedStateHandle, String key) { Intrinsics.checkNotNullParameter(key, "key"); this.key = key; this.handle = savedStateHandle; } @Override // androidx.lifecycle.MutableLiveData, androidx.lifecycle.LiveData public void setValue(T t) { SavedStateHandle savedStateHandle = this.handle; if (savedStateHandle != null) { savedStateHandle.regular.put(this.key, t); MutableStateFlow mutableStateFlow = (MutableStateFlow) savedStateHandle.flows.get(this.key); if (mutableStateFlow != null) { mutableStateFlow.setValue(t); } } super.setValue(t); } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) public final SavedStateHandle createHandle(Bundle bundle, Bundle bundle2) { if (bundle == null) { if (bundle2 == null) { return new SavedStateHandle(); } HashMap hashMap = new HashMap(); for (String key : bundle2.keySet()) { Intrinsics.checkNotNullExpressionValue(key, "key"); hashMap.put(key, bundle2.get(key)); } return new SavedStateHandle(hashMap); } ArrayList parcelableArrayList = bundle.getParcelableArrayList(SavedStateHandle.KEYS); ArrayList parcelableArrayList2 = bundle.getParcelableArrayList(SavedStateHandle.VALUES); if (parcelableArrayList == null || parcelableArrayList2 == null || parcelableArrayList.size() != parcelableArrayList2.size()) { throw new IllegalStateException("Invalid bundle passed as restored state".toString()); } LinkedHashMap linkedHashMap = new LinkedHashMap(); int size = parcelableArrayList.size(); for (int i = 0; i < size; i++) { Object obj = parcelableArrayList.get(i); Intrinsics.checkNotNull(obj, "null cannot be cast to non-null type kotlin.String"); linkedHashMap.put((String) obj, parcelableArrayList2.get(i)); } return new SavedStateHandle(linkedHashMap); } @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) public final boolean validateValue(Object obj) { if (obj == null) { return true; } for (Class cls : SavedStateHandle.ACCEPTABLE_CLASSES) { Intrinsics.checkNotNull(cls); if (cls.isInstance(obj)) { return true; } } return false; } } @MainThread public final StateFlow getStateFlow(String key, T t) { Intrinsics.checkNotNullParameter(key, "key"); Map map = this.flows; MutableStateFlow mutableStateFlow = map.get(key); if (mutableStateFlow == null) { if (!this.regular.containsKey(key)) { this.regular.put(key, t); } mutableStateFlow = StateFlowKt.MutableStateFlow(this.regular.get(key)); this.flows.put(key, mutableStateFlow); map.put(key, mutableStateFlow); } StateFlow asStateFlow = FlowKt.asStateFlow(mutableStateFlow); Intrinsics.checkNotNull(asStateFlow, "null cannot be cast to non-null type kotlinx.coroutines.flow.StateFlow"); return asStateFlow; } }