Rename methods in ActivityLifecycleDispatcher
Test: refactoring, all existent tests pass.
Bug: 32342385
Change-Id: Ie12930192c0a9b498e5d810a06b1f3460fa2b111
diff --git a/lifecycle/runtime/src/main/java/com/android/support/lifecycle/ActivityLifecycleDispatcher.java b/lifecycle/runtime/src/main/java/com/android/support/lifecycle/ActivityLifecycleDispatcher.java
index 8d14fbe..05041aa 100644
--- a/lifecycle/runtime/src/main/java/com/android/support/lifecycle/ActivityLifecycleDispatcher.java
+++ b/lifecycle/runtime/src/main/java/com/android/support/lifecycle/ActivityLifecycleDispatcher.java
@@ -25,6 +25,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RestrictTo;
+import android.util.Log;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
@@ -35,12 +36,13 @@
* Helper class to dispatch lifecycle events for an activity. Use it only if it is impossible
* to use {@link LifecycleActivity}.
*/
+@SuppressWarnings({"WeakerAccess", "unused"})
public class ActivityLifecycleDispatcher {
private static final String FRAGMENT_TAG = "com.android.support.lifecycle.ReportFragment";
-
+ private static final String LOG_TAG = "ActivityLfDispatcher";
private static final int LOADER_ID = 26130239;
- private static Class sActivityThreadClass;
+ private static Class<?> sActivityThreadClass;
private static Class sPauseListenerClass;
private static Method sCurrentActivityThreadMethod;
private static Method sRegisterPauseListenerMethod;
@@ -51,20 +53,22 @@
}
private static void loadClassesAndMethods() {
+ //noinspection TryWithIdenticalCatches
try {
sActivityThreadClass = Class.forName("android.app.ActivityThread");
+ sCurrentActivityThreadMethod = sActivityThreadClass.getMethod("currentActivityThread");
+ sCurrentActivityThreadMethod.setAccessible(true);
+
sPauseListenerClass = Class.forName("android.app.OnActivityPausedListener");
sRegisterPauseListenerMethod = sActivityThreadClass.getMethod(
"registerOnActivityPausedListener", Activity.class, sPauseListenerClass);
sUnregisterPauseListenerMethod = sActivityThreadClass.getMethod(
"unregisterOnActivityPausedListener", Activity.class, sPauseListenerClass);
sRegisterPauseListenerMethod.setAccessible(true);
- sCurrentActivityThreadMethod = sActivityThreadClass.getMethod("currentActivityThread");
- sCurrentActivityThreadMethod.setAccessible(true);
} catch (ClassNotFoundException e) {
- e.printStackTrace();
+ Log.w(LOG_TAG, "Failed to find a class", e);
} catch (NoSuchMethodException e) {
- e.printStackTrace();
+ Log.w(LOG_TAG, "Failed to find a method", e);
}
}
@@ -74,6 +78,7 @@
// deliverNewIntents, start in paused state and etc, however, for most straightforward
// case it is called synchronously after onPause() call.
private final Object mPauseListener = createPauseListener();
+ private Handler mPauseHandler;
private final EmptyCursor mReportStopCursor = new EmptyCursor() {
@Override
public void deactivate() {
@@ -93,8 +98,8 @@
/**
*
- * @param activity - activity, lifecycle of which should be dispatched
- * @param provider - {@link LifecycleProvider} for this activity
+ * @param activity activity, lifecycle of which should be dispatched
+ * @param provider {@link LifecycleProvider} for this activity
*/
public ActivityLifecycleDispatcher(Activity activity, LifecycleProvider provider) {
mActivity = activity;
@@ -104,7 +109,7 @@
/**
* Must be called right after super.onCreate call in {@link Activity#onCreate(Bundle)}.
*/
- public void onCreate() {
+ public void onActivityPostSuperOnCreate() {
// loader might have been retained - kill it, kill it!
mActivity.getLoaderManager().destroyLoader(LOADER_ID);
FragmentManager manager = mActivity.getFragmentManager();
@@ -121,6 +126,112 @@
}
/**
+ * Must be the first call in {@link Activity#onResume()} method,
+ * even before super.onResume call.
+ */
+ public void onActivityPreSuperOnResume() {
+ dispatchPauseIfNeeded();
+ // this listener is called once and then it is removed, so we should set it on every resume.
+ registerPauseListener();
+ }
+
+ private Object createPauseListener() {
+ InvocationHandler handler = new InvocationHandler() {
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ dispatchPauseIfNeeded();
+ return null;
+ }
+ };
+
+ return Proxy.newProxyInstance(LifecycleActivity.class.getClassLoader(),
+ new Class[]{sPauseListenerClass}, handler);
+ }
+
+ private void invokePauseListenerLifecycleMethod(Method method) {
+ //noinspection TryWithIdenticalCatches
+ try {
+ Object activityThread = sCurrentActivityThreadMethod.invoke(sActivityThreadClass);
+ method.invoke(activityThread, mActivity, mPauseListener);
+ } catch (IllegalAccessException e) {
+ Log.w(LOG_TAG, "Failed to register a pause listener", e);
+ } catch (InvocationTargetException e) {
+ Log.w(LOG_TAG, "Failed to register a pause listener", e);
+ }
+ }
+
+ private void registerPauseListener() {
+ invokePauseListenerLifecycleMethod(sRegisterPauseListenerMethod);
+ }
+
+ private void unregisterPauseListener() {
+ invokePauseListenerLifecycleMethod(sUnregisterPauseListenerMethod);
+ }
+
+ void dispatchPauseIfNeeded() {
+ if (mNeedToDispatchPause) {
+ mRegistry.handleLifecycleEvent(Lifecycle.ON_PAUSE);
+ // not sure how we ended up here, so make sure to clean up our listener.
+ unregisterPauseListener();
+ mNeedToDispatchPause = false;
+ }
+ }
+
+ /**
+ * Must be the first call in {@link Activity#onPause()} method, even before super.onPause call.
+ */
+ public void onActivityPreSuperOnPause() {
+ mNeedToDispatchPause = true;
+ if (mPauseHandler == null) {
+ mPauseHandler = new Handler();
+ }
+ mPauseHandler.postAtFrontOfQueue(mPauseRunnable);
+ }
+
+ /**
+ * Must be the first call in {@link Activity#onStop()} method, even before super.onStop call.
+ */
+ @SuppressWarnings("deprecation")
+ public void onActivityPreSuperOnStop() {
+ dispatchPauseIfNeeded();
+ // clean up internal state associated with that cursor
+ mActivity.stopManagingCursor(mReportStopCursor);
+ // add it back
+ mActivity.startManagingCursor(mReportStopCursor);
+ }
+
+ /**
+ * Must be the first call in {@link Activity#onDestroy()} method,
+ * even before super.OnDestroy call.
+ */
+ public void onActivityPreSuperOnDestroy() {
+ mActivity.getLoaderManager().destroyLoader(LOADER_ID);
+ // Create two loaders on the same ID, so first one will be come inactive.
+ // After onDestroy method, inactive loaders will be 100% destroyed.
+ mActivity.getLoaderManager().initLoader(LOADER_ID, null,
+ new EmptyLoaderCallbacks() {
+ @Override
+ public Loader onCreateLoader(int id, Bundle args) {
+ return new ReportOnDestroyLoader(mRegistry, mActivity);
+ }
+ });
+
+ mActivity.getLoaderManager().restartLoader(LOADER_ID, null, new EmptyLoaderCallbacks() {
+ @Override
+ public Loader onCreateLoader(int id, Bundle args) {
+ return new Loader(mActivity.getApplicationContext());
+ }
+ });
+ }
+
+ /**
+ * @return {@link Lifecycle} for the given activity
+ */
+ public Lifecycle getLifecycle() {
+ return mRegistry;
+ }
+
+ /**
* Internal class that dispatches initialization events.
*
* @hide
@@ -157,77 +268,6 @@
}
}
- /**
- * Must be a first call in {@link Activity#onResume()} method, even before super.onResume call.
- */
- public void onResume() {
- dispatchPauseIfNeeded();
- // this listener is called once and then it is removed, so we should set it on every resume.
- registerPauseListener();
- }
-
- private Object createPauseListener() {
- InvocationHandler handler = new InvocationHandler() {
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- dispatchPauseIfNeeded();
- return null;
- }
- };
-
- return Proxy.newProxyInstance(LifecycleActivity.class.getClassLoader(),
- new Class[]{sPauseListenerClass}, handler);
- }
-
- private void invokePauseListenerLifecycleMethod(Method method) {
- try {
- Object activityThread = sCurrentActivityThreadMethod.invoke(sActivityThreadClass);
- method.invoke(activityThread, mActivity, mPauseListener);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
-
- private void registerPauseListener() {
- invokePauseListenerLifecycleMethod(sRegisterPauseListenerMethod);
- }
-
- private void unregisterPauseListener() {
- invokePauseListenerLifecycleMethod(sUnregisterPauseListenerMethod);
- }
-
- void dispatchPauseIfNeeded() {
- if (mNeedToDispatchPause) {
- mRegistry.handleLifecycleEvent(Lifecycle.ON_PAUSE);
- // not sure how we ended up here, so make sure to clean up our listener.
- unregisterPauseListener();
- mNeedToDispatchPause = false;
- }
- }
-
-
- /**
- * Must be a first call in {@link Activity#onPause()} method, even before super.onPause call.
- */
- public void onPause() {
- mNeedToDispatchPause = true;
- Handler h = new Handler();
- h.postAtFrontOfQueue(mPauseRunnable);
- }
-
- /**
- * Must be a first call in {@link Activity#onStop()} method, even before super.onPause call.
- */
- public void onStop() {
- dispatchPauseIfNeeded();
- // clean up internal state associated with that cursor
- mActivity.stopManagingCursor(mReportStopCursor);
- // add it back
- mActivity.startManagingCursor(mReportStopCursor);
- }
-
abstract static class EmptyLoaderCallbacks implements LoaderManager.LoaderCallbacks {
@Override
public void onLoadFinished(Loader loader, Object data) {
@@ -256,35 +296,4 @@
}
}
}
-
- /**
- * Must be a first call in {@link Activity#onDestroy()} method,
- * even before super.OnDestroy call.
- */
- public void onDestroy() {
- mActivity.getLoaderManager().destroyLoader(LOADER_ID);
- // Create two loaders on the same ID, so first one will be come inactive.
- // After onDestroy method, inactive loaders will be 100% destroyed.
- mActivity.getLoaderManager().initLoader(LOADER_ID, null,
- new EmptyLoaderCallbacks() {
- @Override
- public Loader onCreateLoader(int id, Bundle args) {
- return new ReportOnDestroyLoader(mRegistry, mActivity);
- }
- });
-
- mActivity.getLoaderManager().restartLoader(LOADER_ID, null, new EmptyLoaderCallbacks() {
- @Override
- public Loader onCreateLoader(int id, Bundle args) {
- return new Loader(mActivity.getApplicationContext());
- }
- });
- }
-
- /**
- * @return {@link Lifecycle} for the given activity
- */
- public Lifecycle getLifecycle() {
- return mRegistry;
- }
}
diff --git a/lifecycle/runtime/src/main/java/com/android/support/lifecycle/LifecycleActivity.java b/lifecycle/runtime/src/main/java/com/android/support/lifecycle/LifecycleActivity.java
index e317e9a..e7b92be 100644
--- a/lifecycle/runtime/src/main/java/com/android/support/lifecycle/LifecycleActivity.java
+++ b/lifecycle/runtime/src/main/java/com/android/support/lifecycle/LifecycleActivity.java
@@ -30,31 +30,31 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mDispatcher.onCreate();
+ mDispatcher.onActivityPostSuperOnCreate();
}
@Override
protected void onResume() {
- mDispatcher.onResume();
+ mDispatcher.onActivityPreSuperOnResume();
super.onResume();
}
@Override
protected void onPause() {
- mDispatcher.onPause();
+ mDispatcher.onActivityPreSuperOnPause();
super.onPause();
}
@Override
protected void onStop() {
- mDispatcher.onStop();
+ mDispatcher.onActivityPreSuperOnStop();
super.onStop();
}
@Override
protected void onDestroy() {
- mDispatcher.onDestroy();
+ mDispatcher.onActivityPreSuperOnDestroy();
super.onDestroy();
}