Use Lifecycle 2.1 and LifecycleEventObserver

Replace internal usage of GenericLifecycleObserver
with the now public LifecycleEventObserver.

Test: tests still pass
BUG: 117687689
Change-Id: Ib4a1634fc724f56c02534d03cc7e50d47a6b6418
diff --git a/activity/build.gradle b/activity/build.gradle
index 8aa171e..91a9895 100644
--- a/activity/build.gradle
+++ b/activity/build.gradle
@@ -18,8 +18,8 @@
     api("androidx.core:core:1.0.0") {
         exclude group: 'com.google.guava', module: 'listenablefuture'
     }
-    api(ARCH_LIFECYCLE_RUNTIME, libs.exclude_annotations_transitive)
-    api(ARCH_LIFECYCLE_VIEWMODEL, libs.exclude_annotations_transitive)
+    api(project(":lifecycle:lifecycle-runtime"), libs.exclude_annotations_transitive)
+    api(project(":lifecycle:lifecycle-viewmodel"), libs.exclude_annotations_transitive)
     api(project(":savedstate:savedstate-bundle"))
 
     androidTestImplementation(KOTLIN_STDLIB)
diff --git a/activity/ktx/build.gradle b/activity/ktx/build.gradle
index 74e9a33..a39ae87 100644
--- a/activity/ktx/build.gradle
+++ b/activity/ktx/build.gradle
@@ -32,9 +32,7 @@
 }
 
 dependencies {
-    api(project(":activity")) {
-        exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel'
-    }
+    api(project(":activity"))
     api(project(":lifecycle:lifecycle-viewmodel-ktx"))
     api(KOTLIN_STDLIB)
     androidTestImplementation(JUNIT)
diff --git a/activity/src/androidTest/java/androidx/activity/ComponentActivityLifecycleTest.kt b/activity/src/androidTest/java/androidx/activity/ComponentActivityLifecycleTest.kt
index 4ae8419..279875a 100644
--- a/activity/src/androidTest/java/androidx/activity/ComponentActivityLifecycleTest.kt
+++ b/activity/src/androidTest/java/androidx/activity/ComponentActivityLifecycleTest.kt
@@ -17,8 +17,8 @@
 package androidx.activity
 
 import android.os.Bundle
-import androidx.lifecycle.GenericLifecycleObserver
 import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleEventObserver
 import androidx.lifecycle.LifecycleOwner
 import androidx.test.filters.MediumTest
 import androidx.test.rule.ActivityTestRule
@@ -83,7 +83,7 @@
 
 class LifecycleComponentActivity : ComponentActivity() {
     val activityCallbackLifecycleOwner: LifecycleOwner = mock(LifecycleOwner::class.java)
-    val lifecycleObserver: GenericLifecycleObserver = mock(GenericLifecycleObserver::class.java)
+    val lifecycleObserver: LifecycleEventObserver = mock(LifecycleEventObserver::class.java)
     val destroyCountDownLatch = CountDownLatch(1)
 
     init {
diff --git a/activity/src/androidTest/java/androidx/activity/ComponentActivityOnBackPressedTest.kt b/activity/src/androidTest/java/androidx/activity/ComponentActivityOnBackPressedTest.kt
index 6b4bb38..c8aea2c 100644
--- a/activity/src/androidTest/java/androidx/activity/ComponentActivityOnBackPressedTest.kt
+++ b/activity/src/androidTest/java/androidx/activity/ComponentActivityOnBackPressedTest.kt
@@ -16,8 +16,8 @@
 
 package androidx.activity
 
-import androidx.lifecycle.GenericLifecycleObserver
 import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleEventObserver
 import androidx.lifecycle.LifecycleOwner
 import androidx.lifecycle.LifecycleRegistry
 import androidx.test.annotation.UiThreadTest
@@ -201,7 +201,7 @@
 
 class OnBackPressedComponentActivity : ComponentActivity() {
     val activityCallbackLifecycleOwner: LifecycleOwner = mock(LifecycleOwner::class.java)
-    val lifecycleObserver: GenericLifecycleObserver = mock(GenericLifecycleObserver::class.java)
+    val lifecycleObserver: LifecycleEventObserver = mock(LifecycleEventObserver::class.java)
     val destroyCountDownLatch = CountDownLatch(1)
 
     init {
diff --git a/activity/src/androidTest/java/androidx/activity/ComponentActivityViewModelTest.kt b/activity/src/androidTest/java/androidx/activity/ComponentActivityViewModelTest.kt
index 89028fb..d8b2083 100644
--- a/activity/src/androidTest/java/androidx/activity/ComponentActivityViewModelTest.kt
+++ b/activity/src/androidTest/java/androidx/activity/ComponentActivityViewModelTest.kt
@@ -17,8 +17,8 @@
 package androidx.activity
 
 import android.os.Bundle
-import androidx.lifecycle.GenericLifecycleObserver
 import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleEventObserver
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.ViewModelStore
@@ -83,7 +83,7 @@
     fun testActivityOnCleared() {
         val activity = activityRule.activity
         val latch = CountDownLatch(1)
-        val observer = GenericLifecycleObserver { _, event ->
+        val observer = LifecycleEventObserver { _, event ->
             if (event == Lifecycle.Event.ON_DESTROY) {
                 activity.window.decorView.post {
                     try {
diff --git a/activity/src/main/java/androidx/activity/ComponentActivity.java b/activity/src/main/java/androidx/activity/ComponentActivity.java
index 99907f9..c90c1a2 100644
--- a/activity/src/main/java/androidx/activity/ComponentActivity.java
+++ b/activity/src/main/java/androidx/activity/ComponentActivity.java
@@ -27,8 +27,8 @@
 import androidx.annotation.CallSuper;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
-import androidx.lifecycle.GenericLifecycleObserver;
 import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleEventObserver;
 import androidx.lifecycle.LifecycleOwner;
 import androidx.lifecycle.LifecycleRegistry;
 import androidx.lifecycle.ReportFragment;
@@ -78,9 +78,10 @@
                     + "initialization.");
         }
         if (Build.VERSION.SDK_INT >= 19) {
-            getLifecycle().addObserver(new GenericLifecycleObserver() {
+            getLifecycle().addObserver(new LifecycleEventObserver() {
                 @Override
-                public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
+                public void onStateChanged(@NonNull LifecycleOwner source,
+                        @NonNull Lifecycle.Event event) {
                     if (event == Lifecycle.Event.ON_STOP) {
                         Window window = getWindow();
                         final View decor = window != null ? window.peekDecorView() : null;
@@ -91,9 +92,10 @@
                 }
             });
         }
-        getLifecycle().addObserver(new GenericLifecycleObserver() {
+        getLifecycle().addObserver(new LifecycleEventObserver() {
             @Override
-            public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
+            public void onStateChanged(@NonNull LifecycleOwner source,
+                    @NonNull Lifecycle.Event event) {
                 if (event == Lifecycle.Event.ON_DESTROY) {
                     if (!isChangingConfigurations()) {
                         getViewModelStore().clear();
@@ -346,7 +348,7 @@
 
     private class LifecycleAwareOnBackPressedCallback implements
             OnBackPressedCallback,
-            GenericLifecycleObserver {
+            LifecycleEventObserver {
         private final Lifecycle mLifecycle;
         private final OnBackPressedCallback mOnBackPressedCallback;
 
@@ -374,7 +376,8 @@
         }
 
         @Override
-        public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
+        public void onStateChanged(@NonNull LifecycleOwner source,
+                @NonNull Lifecycle.Event event) {
             if (event == Lifecycle.Event.ON_DESTROY) {
                 synchronized (mOnBackPressedCallbacks) {
                     mLifecycle.removeObserver(this);
diff --git a/activity/src/main/java/androidx/activity/ImmLeaksCleaner.java b/activity/src/main/java/androidx/activity/ImmLeaksCleaner.java
index 3b6feb5..ec401e4 100644
--- a/activity/src/main/java/androidx/activity/ImmLeaksCleaner.java
+++ b/activity/src/main/java/androidx/activity/ImmLeaksCleaner.java
@@ -22,15 +22,16 @@
 import android.view.inputmethod.InputMethodManager;
 
 import androidx.annotation.MainThread;
+import androidx.annotation.NonNull;
 import androidx.annotation.RequiresApi;
-import androidx.lifecycle.GenericLifecycleObserver;
 import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleEventObserver;
 import androidx.lifecycle.LifecycleOwner;
 
 import java.lang.reflect.Field;
 
 @RequiresApi(19)
-final class ImmLeaksCleaner implements GenericLifecycleObserver {
+final class ImmLeaksCleaner implements LifecycleEventObserver {
     private static final int NOT_INITIALIAZED = 0;
     private static final int INIT_SUCCESS = 1;
     private static final int INIT_FAILED = 2;
@@ -46,7 +47,7 @@
     }
 
     @Override
-    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
+    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
         if (event != Lifecycle.Event.ON_DESTROY) {
             return;
         }
diff --git a/fragment/src/androidTest/java/androidx/fragment/app/FragmentViewLifecycleTest.java b/fragment/src/androidTest/java/androidx/fragment/app/FragmentViewLifecycleTest.java
index e668c4f..fd96d7f 100644
--- a/fragment/src/androidTest/java/androidx/fragment/app/FragmentViewLifecycleTest.java
+++ b/fragment/src/androidTest/java/androidx/fragment/app/FragmentViewLifecycleTest.java
@@ -33,8 +33,8 @@
 import androidx.annotation.NonNull;
 import androidx.fragment.app.test.FragmentTestActivity;
 import androidx.fragment.test.R;
-import androidx.lifecycle.GenericLifecycleObserver;
 import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleEventObserver;
 import androidx.lifecycle.LifecycleOwner;
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.Observer;
@@ -168,7 +168,7 @@
 
         final StrictViewFragment fragment = new StrictViewFragment();
         fragment.setLayoutId(R.layout.fragment_a);
-        final GenericLifecycleObserver lifecycleObserver = mock(GenericLifecycleObserver.class);
+        final LifecycleEventObserver lifecycleObserver = mock(LifecycleEventObserver.class);
         final LifecycleOwner[] viewLifecycleOwner = new LifecycleOwner[1];
         mActivityRule.runOnUiThread(new Runnable() {
             @Override
diff --git a/fragment/src/main/java/androidx/fragment/app/Fragment.java b/fragment/src/main/java/androidx/fragment/app/Fragment.java
index c122d8f..b70c75d 100644
--- a/fragment/src/main/java/androidx/fragment/app/Fragment.java
+++ b/fragment/src/main/java/androidx/fragment/app/Fragment.java
@@ -55,8 +55,8 @@
 import androidx.core.app.SharedElementCallback;
 import androidx.core.util.DebugUtils;
 import androidx.core.view.LayoutInflaterCompat;
-import androidx.lifecycle.GenericLifecycleObserver;
 import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleEventObserver;
 import androidx.lifecycle.LifecycleOwner;
 import androidx.lifecycle.LifecycleRegistry;
 import androidx.lifecycle.LiveData;
@@ -415,9 +415,10 @@
     private void initLifecycle() {
         mLifecycleRegistry = new LifecycleRegistry(this);
         if (Build.VERSION.SDK_INT >= 19) {
-            mLifecycleRegistry.addObserver(new GenericLifecycleObserver() {
+            mLifecycleRegistry.addObserver(new LifecycleEventObserver() {
                 @Override
-                public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
+                public void onStateChanged(@NonNull LifecycleOwner source,
+                        @NonNull Lifecycle.Event event) {
                     if (event == Lifecycle.Event.ON_STOP) {
                         if (mView != null) {
                             mView.cancelPendingInputEvents();