Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Unified Diff: base/android/java/src/org/chromium/base/ActivityStatus.java

Issue 11419287: android: Improve ActivityStatus and add ChromiumActivity. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fix "if" indentations Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/java/src/org/chromium/base/ChromiumActivity.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..b80c1499675c12540e4f301f0ffe0e1e89520765 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,55 +13,137 @@ import java.util.ArrayList;
* Provides information about the parent activity's status.
*/
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;
+
+ // Current main activity, or null if none.
+ private static Activity sActivity;
+
+ // Current main activity's state. This can be set even if sActivity
+ // is null, to simplify unit testing.
+ private static int sActivityState;
+
+ // Current activity instance, or null.
+ private static ActivityStatus sInstance;
+
+ // List of pause/resume listeners.
+ private final ArrayList<Listener> mListeners;
+
+ // List of state listeners.
+ private final ArrayList<StateListener> mStateListeners;
+
+ protected ActivityStatus() {
+ mListeners = new ArrayList<Listener>();
+ mStateListeners = new ArrayList<StateListener>();
+ }
+
+ /**
+ * Must be called by the main activity when it changes state.
+ * @param activity Current activity.
+ * @param newState New state value.
+ */
+ public static void onStateChange(Activity activity, int newState) {
+ if (newState == CREATED) {
+ sActivity = activity;
+ }
+ 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. Deprecated, new code should use StateListener instead.
+ // TODO(digit): Remove once all users have switched to StateListener.
+ @Deprecated
public interface Listener {
/**
- * Called when the activity's status changes.
+ * Called when the activity is paused or resumed only.
* @param isPaused true if the activity is paused, false if not.
*/
public void onActivityStatusChanged(boolean isPaused);
}
- 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 newState New activity state.
+ */
+ public void onActivityStateChange(int newState);
}
+ /**
+ * Returns the current ActivityStatus instance.
+ */
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();
}
- 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.
+ */
+ public static Activity getActivity() {
+ return sActivity;
+ }
+
+ /**
+ * Returns the current main application activity's state.
+ */
+ public static int getState() {
+ // To simplify unit testing, don't check sActivity for null.
+ return sActivityState;
}
/**
- * Registers the given listener to receive activity status updates.
+ * Registers the given pause/resume listener to receive activity
+ * status updates. Use registerStateListener() instead.
* @param listener Listener to receive status updates.
*/
public void registerListener(Listener listener) {
@@ -68,16 +151,29 @@ public class ActivityStatus {
}
/**
- * Unregisters the given listener from receiving activity status updates.
+ * Unregisters the given pause/resume 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);
}
}
« no previous file with comments | « no previous file | base/android/java/src/org/chromium/base/ChromiumActivity.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698