Index: base/android/java/src/org/chromium/base/ActivityStatus.java |
diff --git a/base/android/java/src/org/chromium/base/ActivityStatus.java b/base/android/java/src/org/chromium/base/ActivityStatus.java |
index 7d0ee947590a9ff0c58555fbb1ccfae8d3da801e..1060ef00dbbfaf99d2369f73855878f7686b11d1 100644 |
--- a/base/android/java/src/org/chromium/base/ActivityStatus.java |
+++ b/base/android/java/src/org/chromium/base/ActivityStatus.java |
@@ -5,20 +5,24 @@ |
package org.chromium.base; |
import android.app.Activity; |
+import android.os.Handler; |
import android.os.Looper; |
/** |
* Provides information about the parent activity's status. |
*/ |
+@JNINamespace("base::android") |
public class ActivityStatus { |
// Constants matching activity states reported to StateListener.onStateChange |
- public static final int CREATED = 1; |
- public static final int STARTED = 2; |
- public static final int RESUMED = 3; |
- public static final int PAUSED = 4; |
- public static final int STOPPED = 5; |
- public static final int DESTROYED = 6; |
+ // As an implementation detail, these are now defined in the auto-generated |
+ // ActivityState interface, to be shared with C++. |
+ public static final int CREATED = ActivityState.CREATED; |
+ public static final int STARTED = ActivityState.STARTED; |
+ public static final int RESUMED = ActivityState.RESUMED; |
+ public static final int PAUSED = ActivityState.PAUSED; |
+ public static final int STOPPED = ActivityState.STOPPED; |
+ public static final int DESTROYED = ActivityState.DESTROYED; |
// Current main activity, or null if none. |
private static Activity sActivity; |
@@ -102,4 +106,39 @@ public class ActivityStatus { |
public static void unregisterStateListener(StateListener listener) { |
sStateListeners.removeObserver(listener); |
} |
+ |
+ /** |
+ * Registers the single thread-safe native activity status listener. |
+ * This handles the case where the caller is not on the main thread. |
+ * Note that this is used by a leaky singleton object from the native |
+ * side, hence lifecycle management is greatly simplified. |
+ */ |
+ @CalledByNative |
+ private static void registerThreadSafeNativeStateListener() { |
+ Looper mainLooper = Looper.getMainLooper(); |
+ // If not on the main thread, post a Handler to perform the registration on it |
+ // then return immediately. Note that registration will happen later. |
+ if (Looper.myLooper() != mainLooper) { |
+ new Handler(mainLooper).post(new Runnable() { |
joth
2013/04/26 20:29:10
simpler to just use
ThreadUtils.runOnUiThread(new
digit1
2013/04/29 12:22:10
Good idea, I've changed the code to use that inste
|
+ @Override |
+ public void run() { |
+ // Note that this native method has to be static |
+ registerThreadSafeNativeStateListener(); |
+ } |
+ }); |
+ return; |
+ } |
+ |
+ // Otherwise register a new listener that calls nativeOnActivityStateChange. |
+ sStateListeners.addObserver(new StateListener() { |
+ @Override |
+ public void onActivityStateChange(int newState) { |
+ nativeOnActivityStateChange(newState); |
+ } |
+ }); |
+ } |
+ |
+ // Called to notify the native side of state changes. |
+ // IMPORTANT: This is always called on the main thread! |
+ private static native void nativeOnActivityStateChange(int newState); |
} |