package androidx.lifecycle; import android.annotation.SuppressLint; import android.app.Application; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.lifecycle.ViewModelProvider; import androidx.savedstate.SavedStateRegistry; import androidx.savedstate.SavedStateRegistryOwner; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; public final class SavedStateViewModelFactory extends ViewModelProvider.KeyedFactory { private static final Class[] ANDROID_VIEWMODEL_SIGNATURE = {Application.class, SavedStateHandle.class}; private static final Class[] VIEWMODEL_SIGNATURE = {SavedStateHandle.class}; private final Application mApplication; private final Bundle mDefaultArgs; private final ViewModelProvider.Factory mFactory; private final Lifecycle mLifecycle; private final SavedStateRegistry mSavedStateRegistry; public SavedStateViewModelFactory(@Nullable Application application, @NonNull SavedStateRegistryOwner savedStateRegistryOwner) { this(application, savedStateRegistryOwner, null); } @SuppressLint({"LambdaLast"}) public SavedStateViewModelFactory(@Nullable Application application, @NonNull SavedStateRegistryOwner savedStateRegistryOwner, @Nullable Bundle bundle) { this.mSavedStateRegistry = savedStateRegistryOwner.getSavedStateRegistry(); this.mLifecycle = savedStateRegistryOwner.getLifecycle(); this.mDefaultArgs = bundle; this.mApplication = application; this.mFactory = application != null ? ViewModelProvider.AndroidViewModelFactory.getInstance(application) : ViewModelProvider.NewInstanceFactory.getInstance(); } private static Constructor findMatchingConstructor(Class cls, Class[] clsArr) { for (Constructor constructor : cls.getConstructors()) { Constructor constructor2 = (Constructor) constructor; if (Arrays.equals(clsArr, constructor2.getParameterTypes())) { return constructor2; } } return null; } @Override // androidx.lifecycle.ViewModelProvider.KeyedFactory, androidx.lifecycle.ViewModelProvider.Factory @NonNull public T create(@NonNull Class cls) { String canonicalName = cls.getCanonicalName(); if (canonicalName != null) { return (T) create(canonicalName, cls); } throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels"); } @Override // androidx.lifecycle.ViewModelProvider.KeyedFactory @NonNull public T create(@NonNull String str, @NonNull Class cls) { T t; boolean isAssignableFrom = AndroidViewModel.class.isAssignableFrom(cls); Constructor findMatchingConstructor = (!isAssignableFrom || this.mApplication == null) ? findMatchingConstructor(cls, VIEWMODEL_SIGNATURE) : findMatchingConstructor(cls, ANDROID_VIEWMODEL_SIGNATURE); if (findMatchingConstructor == null) { return (T) this.mFactory.create(cls); } SavedStateHandleController create = SavedStateHandleController.create(this.mSavedStateRegistry, this.mLifecycle, str, this.mDefaultArgs); if (isAssignableFrom) { try { Application application = this.mApplication; if (application != null) { t = (T) ((ViewModel) findMatchingConstructor.newInstance(application, create.getHandle())); t.setTagIfAbsent("androidx.lifecycle.savedstate.vm.tag", create); return t; } } catch (IllegalAccessException e) { throw new RuntimeException("Failed to access " + cls, e); } catch (InstantiationException e2) { throw new RuntimeException("A " + cls + " cannot be instantiated.", e2); } catch (InvocationTargetException e3) { throw new RuntimeException("An exception happened in constructor of " + cls, e3.getCause()); } } t = (T) ((ViewModel) findMatchingConstructor.newInstance(create.getHandle())); t.setTagIfAbsent("androidx.lifecycle.savedstate.vm.tag", create); return t; } @Override // androidx.lifecycle.ViewModelProvider.OnRequeryFactory public void onRequery(@NonNull ViewModel viewModel) { SavedStateHandleController.attachHandleIfNeeded(viewModel, this.mSavedStateRegistry, this.mLifecycle); } }