package androidx.fragment.app; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelStore; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public final class FragmentManagerViewModel extends ViewModel { private static final ViewModelProvider.Factory FACTORY = new AnonymousClass1(); private static final String TAG = "FragmentManager"; private final HashMap mChildNonConfigs = new HashMap<>(); private boolean mHasBeenCleared = false; private boolean mHasSavedSnapshot = false; private boolean mIsStateSaved = false; private final HashMap mRetainedFragments = new HashMap<>(); private final boolean mStateAutomaticallySaved; private final HashMap mViewModelStores = new HashMap<>(); /* renamed from: androidx.fragment.app.FragmentManagerViewModel$1 reason: invalid class name */ public class AnonymousClass1 implements ViewModelProvider.Factory { @Override // androidx.lifecycle.ViewModelProvider.Factory @NonNull public T create(@NonNull Class cls) { return new FragmentManagerViewModel(true); } } public FragmentManagerViewModel(boolean z2) { this.mStateAutomaticallySaved = z2; } @NonNull public static FragmentManagerViewModel getInstance(ViewModelStore viewModelStore) { return (FragmentManagerViewModel) new ViewModelProvider(viewModelStore, FACTORY).get(FragmentManagerViewModel.class); } public void addRetainedFragment(@NonNull Fragment fragment) { if (this.mIsStateSaved) { if (FragmentManager.isLoggingEnabled(2)) { Log.v("FragmentManager", "Ignoring addRetainedFragment as the state is already saved"); } } else if (!this.mRetainedFragments.containsKey(fragment.mWho)) { this.mRetainedFragments.put(fragment.mWho, fragment); if (FragmentManager.isLoggingEnabled(2)) { Log.v("FragmentManager", "Updating retained Fragments: Added " + fragment); } } } public void clearNonConfigState(@NonNull Fragment fragment) { if (FragmentManager.isLoggingEnabled(3)) { Log.d("FragmentManager", "Clearing non-config state for " + fragment); } FragmentManagerViewModel fragmentManagerViewModel = this.mChildNonConfigs.get(fragment.mWho); if (fragmentManagerViewModel != null) { fragmentManagerViewModel.onCleared(); this.mChildNonConfigs.remove(fragment.mWho); } ViewModelStore viewModelStore = this.mViewModelStores.get(fragment.mWho); if (viewModelStore != null) { viewModelStore.clear(); this.mViewModelStores.remove(fragment.mWho); } } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || FragmentManagerViewModel.class != obj.getClass()) { return false; } FragmentManagerViewModel fragmentManagerViewModel = (FragmentManagerViewModel) obj; return this.mRetainedFragments.equals(fragmentManagerViewModel.mRetainedFragments) && this.mChildNonConfigs.equals(fragmentManagerViewModel.mChildNonConfigs) && this.mViewModelStores.equals(fragmentManagerViewModel.mViewModelStores); } @Nullable public Fragment findRetainedFragmentByWho(String str) { return this.mRetainedFragments.get(str); } @NonNull public FragmentManagerViewModel getChildNonConfig(@NonNull Fragment fragment) { FragmentManagerViewModel fragmentManagerViewModel = this.mChildNonConfigs.get(fragment.mWho); if (fragmentManagerViewModel != null) { return fragmentManagerViewModel; } FragmentManagerViewModel fragmentManagerViewModel2 = new FragmentManagerViewModel(this.mStateAutomaticallySaved); this.mChildNonConfigs.put(fragment.mWho, fragmentManagerViewModel2); return fragmentManagerViewModel2; } @NonNull public Collection getRetainedFragments() { return new ArrayList(this.mRetainedFragments.values()); } @Nullable @Deprecated public FragmentManagerNonConfig getSnapshot() { if (this.mRetainedFragments.isEmpty() && this.mChildNonConfigs.isEmpty() && this.mViewModelStores.isEmpty()) { return null; } HashMap hashMap = new HashMap(); for (Map.Entry entry : this.mChildNonConfigs.entrySet()) { FragmentManagerNonConfig snapshot = entry.getValue().getSnapshot(); if (snapshot != null) { hashMap.put(entry.getKey(), snapshot); } } this.mHasSavedSnapshot = true; if (!this.mRetainedFragments.isEmpty() || !hashMap.isEmpty() || !this.mViewModelStores.isEmpty()) { return new FragmentManagerNonConfig(new ArrayList(this.mRetainedFragments.values()), hashMap, new HashMap(this.mViewModelStores)); } return null; } @NonNull public ViewModelStore getViewModelStore(@NonNull Fragment fragment) { ViewModelStore viewModelStore = this.mViewModelStores.get(fragment.mWho); if (viewModelStore != null) { return viewModelStore; } ViewModelStore viewModelStore2 = new ViewModelStore(); this.mViewModelStores.put(fragment.mWho, viewModelStore2); return viewModelStore2; } public int hashCode() { int hashCode = this.mChildNonConfigs.hashCode(); return this.mViewModelStores.hashCode() + ((hashCode + (this.mRetainedFragments.hashCode() * 31)) * 31); } public boolean isCleared() { return this.mHasBeenCleared; } @Override // androidx.lifecycle.ViewModel public void onCleared() { if (FragmentManager.isLoggingEnabled(3)) { Log.d("FragmentManager", "onCleared called for " + this); } this.mHasBeenCleared = true; } public void removeRetainedFragment(@NonNull Fragment fragment) { if (!this.mIsStateSaved) { if ((this.mRetainedFragments.remove(fragment.mWho) != null) && FragmentManager.isLoggingEnabled(2)) { Log.v("FragmentManager", "Updating retained Fragments: Removed " + fragment); } } else if (FragmentManager.isLoggingEnabled(2)) { Log.v("FragmentManager", "Ignoring removeRetainedFragment as the state is already saved"); } } @Deprecated public void restoreFromSnapshot(@Nullable FragmentManagerNonConfig fragmentManagerNonConfig) { this.mRetainedFragments.clear(); this.mChildNonConfigs.clear(); this.mViewModelStores.clear(); if (fragmentManagerNonConfig != null) { Collection fragments = fragmentManagerNonConfig.getFragments(); if (fragments != null) { for (Fragment fragment : fragments) { if (fragment != null) { this.mRetainedFragments.put(fragment.mWho, fragment); } } } Map childNonConfigs = fragmentManagerNonConfig.getChildNonConfigs(); if (childNonConfigs != null) { for (Map.Entry entry : childNonConfigs.entrySet()) { FragmentManagerViewModel fragmentManagerViewModel = new FragmentManagerViewModel(this.mStateAutomaticallySaved); fragmentManagerViewModel.restoreFromSnapshot(entry.getValue()); this.mChildNonConfigs.put(entry.getKey(), fragmentManagerViewModel); } } Map viewModelStores = fragmentManagerNonConfig.getViewModelStores(); if (viewModelStores != null) { this.mViewModelStores.putAll(viewModelStores); } } this.mHasSavedSnapshot = false; } public void setIsStateSaved(boolean z2) { this.mIsStateSaved = z2; } public boolean shouldDestroy(@NonNull Fragment fragment) { if (!this.mRetainedFragments.containsKey(fragment.mWho)) { return true; } return this.mStateAutomaticallySaved ? this.mHasBeenCleared : !this.mHasSavedSnapshot; } @NonNull public String toString() { StringBuilder sb = new StringBuilder("FragmentManagerViewModel{"); sb.append(Integer.toHexString(System.identityHashCode(this))); sb.append("} Fragments ("); Iterator it = this.mRetainedFragments.values().iterator(); while (it.hasNext()) { sb.append(it.next()); if (it.hasNext()) { sb.append(", "); } } sb.append(") Child Non Config ("); Iterator it2 = this.mChildNonConfigs.keySet().iterator(); while (it2.hasNext()) { sb.append(it2.next()); if (it2.hasNext()) { sb.append(", "); } } sb.append(") ViewModelStores ("); Iterator it3 = this.mViewModelStores.keySet().iterator(); while (it3.hasNext()) { sb.append(it3.next()); if (it3.hasNext()) { sb.append(", "); } } sb.append(')'); return sb.toString(); } }