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 765d8411d4b4337a952d70929a2696d776c412ba..cace0239b1a4cbf1a08e588ffa837de8c505ce19 100644 |
--- a/base/android/java/src/org/chromium/base/ActivityStatus.java |
+++ b/base/android/java/src/org/chromium/base/ActivityStatus.java |
@@ -4,6 +4,7 @@ |
package org.chromium.base; |
+import android.app.Activity; |
import android.os.Looper; |
import java.util.ArrayList; |
@@ -12,6 +13,61 @@ import java.util.ArrayList; |
* Provides information about the parent activity's status. |
*/ |
public class ActivityStatus { |
+ |
+ // Constants matching activity states reported to |
+ // StateListener.onStateChange |
Philippe
2012/12/04 16:12:42
Nit: I think this can fit on the line above. Gener
digit1
2012/12/04 17:31:23
Done.
|
+ 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; |
+ |
+ // Current activity, or null if none. |
+ private static Activity sActivity; |
+ |
+ // Current activity's state. This can be set to PAUSED or RESUMED |
+ // even if there sActivity is null to facilite unit testing. |
+ private static int sActivityState; |
+ |
+ // Current activity instance, or null. |
+ private static ActivityStatus sInstance; |
+ |
+ // List of deprecated pause/resume listeners. |
digit1
2012/12/04 15:45:34
Ignore the "deprecated" here, I'll remove it.
digit1
2012/12/04 17:31:23
Done.
|
+ private ArrayList<Listener> mListeners = new ArrayList<Listener>(); |
Philippe
2012/12/04 16:12:42
Nit: you can make this field and the one below 'fi
digit1
2012/12/04 17:31:23
Done.
|
+ |
+ // List of state listeners. |
+ private ArrayList<StateListener> mStateListeners = new ArrayList<StateListener>(); |
+ |
+ /** |
+ * Must be called by the activity changes state. |
+ * @param activity Current activity. |
+ * @param newState New state value. |
+ */ |
+ public static void onStateChange(Activity activity, int newState) { |
+ if (newState == CREATED) |
+ sActivity = activity; |
Philippe
2012/12/04 16:12:42
Nit: 4 space indentation here and in some other pl
digit1
2012/12/04 17:31:23
Done.
|
+ |
+ sActivityState = newState; |
+ getInstance().changeState(newState); |
+ |
+ if (newState == DESTROYED) |
+ sActivity = null; |
+ } |
+ |
+ private void changeState(int newState) { |
+ for (StateListener listener : mStateListeners) { |
+ listener.onActivityStateChange(newState); |
+ } |
+ if (newState == PAUSED || newState == RESUMED) { |
+ boolean paused = (newState == PAUSED); |
+ for (Listener listener : mListeners) { |
+ listener.onActivityStatusChanged(paused); |
+ } |
+ } |
+ } |
+ |
+ // This interface can only be used to listen to PAUSED and RESUMED events. |
public interface Listener { |
Philippe
2012/12/04 16:12:42
Nit: Maybe add a comment here to say that this lis
digit1
2012/12/04 17:31:23
Done.
|
/** |
* Called when the activity's status changes. |
@@ -20,47 +76,67 @@ public class ActivityStatus { |
public void onActivityStatusChanged(boolean isPaused); |
digit1
2012/12/04 15:41:55
Note that I'm pretty sure that the Android convent
|
} |
- private boolean mIsPaused = false; |
- private ArrayList<Listener> mListeners = new ArrayList<Listener>(); |
- private static ActivityStatus sActivityStatus; |
- |
- private ActivityStatus() { |
+ // Use this interface to listen to all state changes. |
+ public interface StateListener { |
+ /** |
+ * Called when the activity's state changes. |
+ * @param activity Current activity. |
Philippe
2012/12/04 16:12:42
Nit: It seems that you got rid of |activity| and |
digit1
2012/12/04 17:31:23
Done.
|
+ * @param oldState Previous activity state. |
+ * @param newState New activity state. |
+ */ |
+ public void onActivityStateChange(int newState); |
} |
public static ActivityStatus getInstance() { |
// Can only be called on the UI thread. |
assert Looper.myLooper() == Looper.getMainLooper(); |
- if (sActivityStatus == null) { |
- sActivityStatus = new ActivityStatus(); |
+ if (sInstance == null) { |
+ sInstance = new ActivityStatus(); |
Philippe
2012/12/04 16:12:42
Nit: as a bonus you could add a protected construc
digit1
2012/12/04 17:31:23
Done.
|
} |
- return sActivityStatus; |
+ return sInstance; |
} |
/** |
* Indicates that the parent activity was paused. |
*/ |
public void onPause() { |
- mIsPaused = true; |
- informAllListeners(); |
+ changeState(PAUSED); |
} |
/** |
* Indicates that the parent activity was resumed. |
*/ |
public void onResume() { |
- mIsPaused = false; |
- informAllListeners(); |
+ changeState(RESUMED); |
} |
/** |
* Indicates that the parent activity is currently paused. |
*/ |
public boolean isPaused() { |
- return mIsPaused; |
+ return sActivityState == PAUSED; |
+ } |
+ |
+ /** |
+ * Returns the current main application activity. |
+ */ |
+ static public Activity getActivity() { |
Philippe
2012/12/04 16:12:42
Nit: I think 'public static' is more common.
digit1
2012/12/04 17:31:23
Done.
|
+ return sActivity; |
+ } |
+ |
+ /** |
+ * Returns the current main application activity's state. |
+ */ |
+ static public int getState() { |
+ if (sActivity == null) |
+ return DESTROYED; |
+ else |
+ return sActivityState; |
} |
/** |
- * Registers the given listener to receive activity status updates. |
+ * Registers the given (deprecated) listener to receive activity |
+ * status updates. Use registerStateListener() instead. |
* @param listener Listener to receive status updates. |
*/ |
public void registerListener(Listener listener) { |
@@ -68,16 +144,29 @@ public class ActivityStatus { |
} |
/** |
- * Unregisters the given listener from receiving activity status updates. |
+ * Unregisters the given deprecated listener from receiving activity |
+ * status updates. Use unregisterStateListener() instead. |
* @param listener Listener that doesn't want to receive status updates. |
*/ |
public void unregisterListener(Listener listener) { |
mListeners.remove(listener); |
} |
- private void informAllListeners() { |
- for (Listener listener : mListeners) { |
- listener.onActivityStatusChanged(mIsPaused); |
- } |
+ /** |
+ * Registers the given listener to receive activity state changes. |
+ * @param listener Listener to receive state changes. |
+ */ |
+ public static void registerStateListener(StateListener listener) { |
+ ActivityStatus status = getInstance(); |
+ status.mStateListeners.add(listener); |
+ } |
+ |
+ /** |
+ * Unregisters the given listener from receiving activity state changes. |
+ * @param listener Listener that doesn't want to receive state changes. |
+ */ |
+ public static void unregisterStateListener(StateListener listener) { |
+ ActivityStatus status = getInstance(); |
+ status.mStateListeners.remove(listener); |
} |
} |