Revert "Migrate usages of @OnLifecycleEvent in service/extensions/livedata-core"
This reverts r.android.com/979646
Bug: 135942802
Test: ./gradlew bOS
Change-Id: I8318db688871e4b60a21aae16e4ab67eb11995a2
diff --git a/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/FragmentOperationsLifecycleTest.java b/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/FragmentOperationsLifecycleTest.java
index ca3a860..55ae129 100644
--- a/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/FragmentOperationsLifecycleTest.java
+++ b/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/FragmentOperationsLifecycleTest.java
@@ -28,7 +28,6 @@
import static java.util.Arrays.asList;
-import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.activity.EmptyActivity;
@@ -103,11 +102,11 @@
return observer;
}
- private static class CollectingObserver implements LifecycleEventObserver {
+ private static class CollectingObserver implements LifecycleObserver {
final List<Lifecycle.Event> mCollectedEvents = new ArrayList<>();
- @Override
- public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
+ @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
+ public void anyEvent(LifecycleOwner owner, Lifecycle.Event event) {
mCollectedEvents.add(event);
}
diff --git a/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/ViewModelTest.java b/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/ViewModelTest.java
index 4e38fe6..a263822 100644
--- a/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/ViewModelTest.java
+++ b/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/ViewModelTest.java
@@ -16,6 +16,8 @@
package androidx.lifecycle;
+import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
+
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -23,7 +25,6 @@
import android.app.Instrumentation;
-import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
@@ -61,40 +62,46 @@
final TestViewModel[] fragment2Model = new TestViewModel[1];
final ViewModelActivity[] viewModelActivity = new ViewModelActivity[1];
viewModelActivity[0] = mActivityRule.getActivity();
- mActivityRule.runOnUiThread(() -> {
- ViewModelFragment fragment1 = getFragment(viewModelActivity[0],
- ViewModelActivity.FRAGMENT_TAG_1);
- ViewModelFragment fragment2 = getFragment(viewModelActivity[0],
- ViewModelActivity.FRAGMENT_TAG_2);
- assertThat(fragment1, notNullValue());
- assertThat(fragment2, notNullValue());
- assertThat(fragment1.activityModel, is(fragment2.activityModel));
- assertThat(fragment1.fragmentModel, not(is(fragment2.activityModel)));
- assertThat(mActivityRule.getActivity().activityModel, is(fragment1.activityModel));
- activityModel[0] = mActivityRule.getActivity().activityModel;
- defaultActivityModel[0] = mActivityRule.getActivity().defaultActivityModel;
- assertThat(defaultActivityModel[0], not(is(activityModel[0])));
- fragment1Model[0] = fragment1.fragmentModel;
- fragment2Model[0] = fragment2.fragmentModel;
+ mActivityRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ViewModelFragment fragment1 = getFragment(viewModelActivity[0],
+ ViewModelActivity.FRAGMENT_TAG_1);
+ ViewModelFragment fragment2 = getFragment(viewModelActivity[0],
+ ViewModelActivity.FRAGMENT_TAG_2);
+ assertThat(fragment1, notNullValue());
+ assertThat(fragment2, notNullValue());
+ assertThat(fragment1.activityModel, is(fragment2.activityModel));
+ assertThat(fragment1.fragmentModel, not(is(fragment2.activityModel)));
+ assertThat(mActivityRule.getActivity().activityModel, is(fragment1.activityModel));
+ activityModel[0] = mActivityRule.getActivity().activityModel;
+ defaultActivityModel[0] = mActivityRule.getActivity().defaultActivityModel;
+ assertThat(defaultActivityModel[0], not(is(activityModel[0])));
+ fragment1Model[0] = fragment1.fragmentModel;
+ fragment2Model[0] = fragment2.fragmentModel;
+ }
});
viewModelActivity[0] = recreateActivity();
- mActivityRule.runOnUiThread(() -> {
- ViewModelFragment fragment1 = getFragment(viewModelActivity[0],
- ViewModelActivity.FRAGMENT_TAG_1);
- ViewModelFragment fragment2 = getFragment(viewModelActivity[0],
- ViewModelActivity.FRAGMENT_TAG_2);
- assertThat(fragment1, notNullValue());
- assertThat(fragment2, notNullValue());
+ mActivityRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ViewModelFragment fragment1 = getFragment(viewModelActivity[0],
+ ViewModelActivity.FRAGMENT_TAG_1);
+ ViewModelFragment fragment2 = getFragment(viewModelActivity[0],
+ ViewModelActivity.FRAGMENT_TAG_2);
+ assertThat(fragment1, notNullValue());
+ assertThat(fragment2, notNullValue());
- assertThat(fragment1.activityModel, is(activityModel[0]));
- assertThat(fragment2.activityModel, is(activityModel[0]));
- assertThat(fragment1.fragmentModel, is(fragment1Model[0]));
- assertThat(fragment2.fragmentModel, is(fragment2Model[0]));
- assertThat(fragment1.defaultActivityModel, is(defaultActivityModel[0]));
- assertThat(fragment2.defaultActivityModel, is(defaultActivityModel[0]));
- assertThat(mActivityRule.getActivity().activityModel, is(activityModel[0]));
- assertThat(mActivityRule.getActivity().defaultActivityModel,
- is(defaultActivityModel[0]));
+ assertThat(fragment1.activityModel, is(activityModel[0]));
+ assertThat(fragment2.activityModel, is(activityModel[0]));
+ assertThat(fragment1.fragmentModel, is(fragment1Model[0]));
+ assertThat(fragment2.fragmentModel, is(fragment2Model[0]));
+ assertThat(fragment1.defaultActivityModel, is(defaultActivityModel[0]));
+ assertThat(fragment2.defaultActivityModel, is(defaultActivityModel[0]));
+ assertThat(mActivityRule.getActivity().activityModel, is(activityModel[0]));
+ assertThat(mActivityRule.getActivity().defaultActivityModel,
+ is(defaultActivityModel[0]));
+ }
});
}
@@ -110,9 +117,10 @@
public void testOnClear() throws Throwable {
final ViewModelActivity activity = mActivityRule.getActivity();
final CountDownLatch latch = new CountDownLatch(1);
- final LifecycleObserver observer = new DefaultLifecycleObserver() {
- @Override
- public void onResume(@NonNull LifecycleOwner owner) {
+ final LifecycleObserver observer = new LifecycleObserver() {
+ @SuppressWarnings("unused")
+ @OnLifecycleEvent(ON_RESUME)
+ void onResume() {
try {
final FragmentManager manager = activity.getSupportFragmentManager();
Fragment fragment = new Fragment();
@@ -127,7 +135,12 @@
}
};
- mActivityRule.runOnUiThread(() -> activity.getLifecycle().addObserver(observer));
+ mActivityRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ activity.getLifecycle().addObserver(observer);
+ }
+ });
assertThat(latch.await(TIMEOUT, TimeUnit.SECONDS), is(true));
}
@@ -142,7 +155,12 @@
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
instrumentation.addMonitor(monitor);
final ViewModelActivity previous = mActivityRule.getActivity();
- mActivityRule.runOnUiThread(() -> previous.recreate());
+ mActivityRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ previous.recreate();
+ }
+ });
ViewModelActivity result;
// this guarantee that we will reinstall monitor between notifications about onDestroy
diff --git a/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/activity/FragmentLifecycleActivity.java b/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/activity/FragmentLifecycleActivity.java
index caf1a05..597111d 100644
--- a/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/activity/FragmentLifecycleActivity.java
+++ b/lifecycle/lifecycle-extensions/src/androidTest/java/androidx/lifecycle/activity/FragmentLifecycleActivity.java
@@ -24,8 +24,9 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
-import androidx.lifecycle.LifecycleEventObserver;
+import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
+import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.extensions.test.R;
import java.util.ArrayList;
@@ -40,7 +41,7 @@
private static final String EXTRA_NESTED = "nested";
private final List<Lifecycle.Event> mLoggedEvents = Collections
- .synchronizedList(new ArrayList<>());
+ .synchronizedList(new ArrayList<Lifecycle.Event>());
private LifecycleOwner mObservedOwner;
private final CountDownLatch mDestroyLatch = new CountDownLatch(1);
@@ -96,8 +97,13 @@
public void observe(LifecycleOwner provider) {
mObservedOwner = provider;
- provider.getLifecycle().addObserver(
- (LifecycleEventObserver) (source, event) -> mLoggedEvents.add(event));
+ provider.getLifecycle().addObserver(new LifecycleObserver() {
+ @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
+ public void anyEvent(@SuppressWarnings("unused") LifecycleOwner owner,
+ Lifecycle.Event event) {
+ mLoggedEvents.add(event);
+ }
+ });
}
public List<Lifecycle.Event> getLoggedEvents() {
diff --git a/lifecycle/lifecycle-livedata-core/src/test/java/androidx/lifecycle/LiveDataTest.java b/lifecycle/lifecycle-livedata-core/src/test/java/androidx/lifecycle/LiveDataTest.java
index 8098459..f57d864 100644
--- a/lifecycle/lifecycle-livedata-core/src/test/java/androidx/lifecycle/LiveDataTest.java
+++ b/lifecycle/lifecycle-livedata-core/src/test/java/androidx/lifecycle/LiveDataTest.java
@@ -37,7 +37,6 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
-import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
@@ -421,9 +420,9 @@
@Test
public void testDataChangeDuringStateChange() {
mRegistry.handleLifecycleEvent(ON_START);
- mRegistry.addObserver(new DefaultLifecycleObserver() {
- @Override
- public void onStop(@NonNull LifecycleOwner owner) {
+ mRegistry.addObserver(new LifecycleObserver() {
+ @OnLifecycleEvent(ON_STOP)
+ public void onStop() {
// change data in onStop, observer should not be called!
mLiveData.setValue("b");
}
diff --git a/lifecycle/lifecycle-service/src/androidTest/java/androidx/lifecycle/service/TestService.java b/lifecycle/lifecycle-service/src/androidTest/java/androidx/lifecycle/service/TestService.java
index b8d023f..afc9dc8 100644
--- a/lifecycle/lifecycle-service/src/androidTest/java/androidx/lifecycle/service/TestService.java
+++ b/lifecycle/lifecycle-service/src/androidTest/java/androidx/lifecycle/service/TestService.java
@@ -22,8 +22,11 @@
import android.os.IBinder;
import androidx.annotation.Nullable;
-import androidx.lifecycle.LifecycleEventObserver;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleService;
+import androidx.lifecycle.OnLifecycleEvent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
public class TestService extends LifecycleService {
@@ -34,11 +37,14 @@
private final IBinder mBinder = new Binder();
public TestService() {
- getLifecycle().addObserver((LifecycleEventObserver) (owner, event) -> {
- Context context = (TestService) owner;
- Intent intent = new Intent(ACTION_LOG_EVENT);
- intent.putExtra(EXTRA_KEY_EVENT, event);
- LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
+ getLifecycle().addObserver(new LifecycleObserver() {
+ @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
+ public void anyEvent(LifecycleOwner owner, Lifecycle.Event event) {
+ Context context = (TestService) owner;
+ Intent intent = new Intent(ACTION_LOG_EVENT);
+ intent.putExtra(EXTRA_KEY_EVENT, event);
+ LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
+ }
});
}