[go: nahoru, domu]

Revert "Remove fragment dependency from viewmodel-savedstate"

This reverts r.android.com/987584

Bug: 135942802
Test: ./gradlew bOS
Change-Id: Ie0970436a01c2e475ea51e08df63e5d569f43f31
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/api/1.0.0-alpha02.txt b/lifecycle/lifecycle-viewmodel-savedstate/api/1.0.0-alpha02.txt
index d6d3637..4f9f93e 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/api/1.0.0-alpha02.txt
+++ b/lifecycle/lifecycle-viewmodel-savedstate/api/1.0.0-alpha02.txt
@@ -20,7 +20,10 @@
   }
 
   public final class SavedStateVMFactory extends androidx.lifecycle.AbstractSavedStateVMFactory implements androidx.lifecycle.ViewModelProvider.Factory {
-    ctor public SavedStateVMFactory(android.app.Application, androidx.savedstate.SavedStateRegistryOwner);
+    ctor public SavedStateVMFactory(androidx.fragment.app.Fragment);
+    ctor public SavedStateVMFactory(androidx.fragment.app.Fragment, android.os.Bundle?);
+    ctor public SavedStateVMFactory(androidx.fragment.app.FragmentActivity);
+    ctor public SavedStateVMFactory(androidx.fragment.app.FragmentActivity, android.os.Bundle?);
     ctor public SavedStateVMFactory(android.app.Application, androidx.savedstate.SavedStateRegistryOwner, android.os.Bundle?);
     method protected <T extends androidx.lifecycle.ViewModel> T create(String, Class<T!>, androidx.lifecycle.SavedStateHandle);
   }
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/api/current.txt b/lifecycle/lifecycle-viewmodel-savedstate/api/current.txt
index d6d3637..4f9f93e 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/api/current.txt
+++ b/lifecycle/lifecycle-viewmodel-savedstate/api/current.txt
@@ -20,7 +20,10 @@
   }
 
   public final class SavedStateVMFactory extends androidx.lifecycle.AbstractSavedStateVMFactory implements androidx.lifecycle.ViewModelProvider.Factory {
-    ctor public SavedStateVMFactory(android.app.Application, androidx.savedstate.SavedStateRegistryOwner);
+    ctor public SavedStateVMFactory(androidx.fragment.app.Fragment);
+    ctor public SavedStateVMFactory(androidx.fragment.app.Fragment, android.os.Bundle?);
+    ctor public SavedStateVMFactory(androidx.fragment.app.FragmentActivity);
+    ctor public SavedStateVMFactory(androidx.fragment.app.FragmentActivity, android.os.Bundle?);
     ctor public SavedStateVMFactory(android.app.Application, androidx.savedstate.SavedStateRegistryOwner, android.os.Bundle?);
     method protected <T extends androidx.lifecycle.ViewModel> T create(String, Class<T!>, androidx.lifecycle.SavedStateHandle);
   }
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/build.gradle b/lifecycle/lifecycle-viewmodel-savedstate/build.gradle
index a19cb38..51f2e88 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/build.gradle
+++ b/lifecycle/lifecycle-viewmodel-savedstate/build.gradle
@@ -38,7 +38,10 @@
     api(project(":lifecycle:lifecycle-livedata-core"))
     api(project(":lifecycle:lifecycle-viewmodel"))
 
-    androidTestImplementation(project(":fragment:fragment"))
+    api project(":fragment:fragment"), {
+        exclude group: 'androidx.lifecycle', module: 'lifecycle-livedata-core'
+    }
+
     androidTestImplementation(TRUTH)
     androidTestImplementation(KOTLIN_STDLIB)
     androidTestImplementation(ESPRESSO_CORE)
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/SavedStateFactoryTest.kt b/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/SavedStateFactoryTest.kt
index 92103fb..a87d766 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/SavedStateFactoryTest.kt
+++ b/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/SavedStateFactoryTest.kt
@@ -41,9 +41,7 @@
 
     @Test
     fun testCreateAndroidVM() {
-        val savedStateVMFactory = SavedStateVMFactory(
-            activityRule.activity.application,
-            activityRule.activity)
+        val savedStateVMFactory = SavedStateVMFactory(activityRule.activity)
         val vm = ViewModelProvider(ViewModelStore(), savedStateVMFactory)
         assertThat(vm.get(MyAndroidViewModel::class.java).handle).isNotNull()
         assertThat(vm.get(MyViewModel::class.java).handle).isNotNull()
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/ViewModelsWithStateTests.java b/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/ViewModelsWithStateTests.java
index 1fe67d8..25073a5 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/ViewModelsWithStateTests.java
+++ b/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/ViewModelsWithStateTests.java
@@ -143,19 +143,14 @@
     private ViewModelProvider vmProvider(FakingSavedStateActivity activity) {
         if (FRAGMENT_MODE.equals(mode)) {
             Fragment fragment = activity.getFragment();
-            return new ViewModelProvider(fragment, new SavedStateVMFactory(
-                    fragment.requireActivity().getApplication(),
-                    fragment));
+            return new ViewModelProvider(fragment, new SavedStateVMFactory(fragment));
         }
-        return new ViewModelProvider(activity, new SavedStateVMFactory(
-                activity.getApplication(),
-                activity));
+        return new ViewModelProvider(activity, new SavedStateVMFactory(activity));
     }
 
     // copy copy copy paste
     @SuppressWarnings("unchecked")
-    private static <T extends Activity> T recreateActivity(final T activity,
-            ActivityTestRule<?> rule)
+    private static <T extends Activity> T recreateActivity(final T activity, ActivityTestRule rule)
             throws Throwable {
         Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
                 activity.getClass().getCanonicalName(), null, false);
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/activity/FakingSavedStateActivity.java b/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/activity/FakingSavedStateActivity.java
index 995d71b..b5060e6 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/activity/FakingSavedStateActivity.java
+++ b/lifecycle/lifecycle-viewmodel-savedstate/src/androidTest/java/androidx/lifecycle/viewmodel/savedstate/activity/FakingSavedStateActivity.java
@@ -19,7 +19,6 @@
 import android.content.Intent;
 import android.os.Bundle;
 
-import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;
@@ -68,7 +67,7 @@
     }
 
     @Override
-    public void onSaveInstanceState(@NonNull Bundle outState) {
+    public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         if (mCountDownLatch != null) {
             mLastSavedState = outState;
diff --git a/lifecycle/lifecycle-viewmodel-savedstate/src/main/java/androidx/lifecycle/SavedStateVMFactory.java b/lifecycle/lifecycle-viewmodel-savedstate/src/main/java/androidx/lifecycle/SavedStateVMFactory.java
index f957f00..d2457ef1 100644
--- a/lifecycle/lifecycle-viewmodel-savedstate/src/main/java/androidx/lifecycle/SavedStateVMFactory.java
+++ b/lifecycle/lifecycle-viewmodel-savedstate/src/main/java/androidx/lifecycle/SavedStateVMFactory.java
@@ -16,12 +16,14 @@
 
 package androidx.lifecycle;
 
-import android.annotation.SuppressLint;
+import android.app.Activity;
 import android.app.Application;
 import android.os.Bundle;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.fragment.app.Fragment;
+import androidx.fragment.app.FragmentActivity;
 import androidx.savedstate.SavedStateRegistryOwner;
 
 import java.lang.reflect.Constructor;
@@ -31,7 +33,8 @@
 /**
  * {@link androidx.lifecycle.ViewModelProvider.Factory} that can create ViewModels accessing and contributing
  * to a saved state via {@link SavedStateHandle} received in a constructor. If {@code defaultArgs}
- * bundle was passed into the constructor, it will provide default values in
+ * bundle was passed in {@link #SavedStateVMFactory(Fragment, Bundle)}
+ * or {@link #SavedStateVMFactory(FragmentActivity, Bundle)}, it will provide default values in
  * {@code SavedStateHandle}.
  * <p>
  * If ViewModel is instance of {@link androidx.lifecycle.AndroidViewModel}, it looks for a constructor that
@@ -46,15 +49,54 @@
      * Creates {@link SavedStateVMFactory}.
      * <p>
      * {@link androidx.lifecycle.ViewModel} created with this factory can access to saved state scoped to
+     * the given {@code fragment}.
+     *
+     * @param fragment scope of this fragment will be used for state saving
+     */
+    public SavedStateVMFactory(@NonNull Fragment fragment) {
+        this(fragment, null);
+    }
+
+    /**
+     * Creates {@link SavedStateVMFactory}.
+     * <p>
+     * {@link androidx.lifecycle.ViewModel} created with this factory can access to saved state scoped to
+     * the given {@code fragment}.
+     *
+     * @param fragment scope of this fragment will be used for state saving
+     * @param defaultArgs values from this {@code Bundle} will be used as defaults by
+     * {@link SavedStateHandle} if there is no previously saved state or previously saved state
+     * miss a value by such key.
+     */
+    public SavedStateVMFactory(@NonNull Fragment fragment, @Nullable Bundle defaultArgs) {
+        this(checkApplication(checkActivity(fragment)), fragment, defaultArgs);
+    }
+
+    /**
+     * Creates {@link SavedStateVMFactory}.
+     * <p>
+     * {@link androidx.lifecycle.ViewModel} created with this factory can access to saved state scoped to
      * the given {@code activity}.
      *
-     * @param application an application
-     * @param owner {@link SavedStateRegistryOwner} that will provide restored state for created
-     * {@link androidx.lifecycle.ViewModel ViewModels}
+     * @param activity scope of this activity will be used for state saving
      */
-    public SavedStateVMFactory(@NonNull Application application,
-            @NonNull SavedStateRegistryOwner owner) {
-        this(application, owner, null);
+    public SavedStateVMFactory(@NonNull FragmentActivity activity) {
+        this(activity, null);
+    }
+
+    /**
+     * Creates {@link SavedStateVMFactory}.
+     * <p>
+     * {@link androidx.lifecycle.ViewModel} created with this factory can access to saved state scoped to
+     * the given {@code activity}.
+     *
+     * @param activity scope of this activity will be used for state saving
+     * @param defaultArgs values from this {@code Bundle} will be used as defaults by
+     * {@link SavedStateHandle} if there is no previously saved state or previously saved state
+     * misses a value by such key.
+     */
+    public SavedStateVMFactory(@NonNull FragmentActivity activity, @Nullable Bundle defaultArgs) {
+        this(checkApplication(activity), activity, defaultArgs);
     }
 
     /**
@@ -70,7 +112,6 @@
      * {@link SavedStateHandle} if there is no previously saved state or previously saved state
      * misses a value by such key.
      */
-    @SuppressLint("LambdaLast")
     public SavedStateVMFactory(@NonNull Application application,
             @NonNull SavedStateRegistryOwner owner,
             @Nullable Bundle defaultArgs) {
@@ -124,4 +165,23 @@
         }
         return null;
     }
+
+    private static Application checkApplication(Activity activity) {
+        Application application = activity.getApplication();
+        if (application == null) {
+            throw new IllegalStateException("Your activity/fragment is not yet attached to "
+                    + "Application. You can't request ViewModelsWithStateFactory "
+                    + "before onCreate call.");
+        }
+        return application;
+    }
+
+    private static Activity checkActivity(Fragment fragment) {
+        Activity activity = fragment.getActivity();
+        if (activity == null) {
+            throw new IllegalStateException("Can't create ViewModelsWithStateFactory"
+                    + " for detached fragment");
+        }
+        return activity;
+    }
 }