diff options
author | digit@chromium.org <digit@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-07 16:59:34 +0000 |
---|---|---|
committer | digit@chromium.org <digit@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-07 16:59:34 +0000 |
commit | f1bdf1c72a39f47c133f7a45ff441849ec8b496b (patch) | |
tree | db89f58152e350dd2afc52b127f7773e39c6b0f3 /base/android | |
parent | cbf03ba141434e753c517370696d93b43d8582d5 (diff) | |
download | chromium_src-f1bdf1c72a39f47c133f7a45ff441849ec8b496b.zip chromium_src-f1bdf1c72a39f47c133f7a45ff441849ec8b496b.tar.gz chromium_src-f1bdf1c72a39f47c133f7a45ff441849ec8b496b.tar.bz2 |
android: Improve ActivityStatus and add ChromiumActivity.
The ActivityStatus class used to track activity state changes is too basic
for some usage scenarios.
This patch does the following:
- Augment ActivityStatus with a new StateListener type that can be
registered to listen to all activity state changes.
Also add getActivity(), getState(), registerStateListener()
and unregisterStateListener() as static method.
- Add a new ChromiumActivity class that all Chromium main activities
should sub-class, to ensure that the ActivityStatus state is updated
appropriately.
- Modify all main activities in the Chromium code base to extend
from ChromiumActivity instead of Activity.
BUG=none
Review URL: https://chromiumcodereview.appspot.com/11419287
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171789 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/android')
-rw-r--r-- | base/android/java/src/org/chromium/base/ActivityStatus.java | 136 | ||||
-rw-r--r-- | base/android/java/src/org/chromium/base/ChromiumActivity.java | 50 |
2 files changed, 166 insertions, 20 deletions
diff --git a/base/android/java/src/org/chromium/base/ActivityStatus.java b/base/android/java/src/org/chromium/base/ActivityStatus.java index 765d841..b80c149 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); } } diff --git a/base/android/java/src/org/chromium/base/ChromiumActivity.java b/base/android/java/src/org/chromium/base/ChromiumActivity.java new file mode 100644 index 0000000..f857e12 --- /dev/null +++ b/base/android/java/src/org/chromium/base/ChromiumActivity.java @@ -0,0 +1,50 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.base; + +import android.app.Activity; +import android.os.Bundle; + +// All Chromium main activities should extend this class. +// This allows various sub-systems to properly react to activity +// state changes. +public class ChromiumActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstance) { + super.onCreate(savedInstance); + ActivityStatus.onStateChange(this, ActivityStatus.CREATED); + } + + @Override + protected void onStart() { + super.onStart(); + ActivityStatus.onStateChange(this, ActivityStatus.STARTED); + } + + @Override + protected void onResume() { + super.onResume(); + ActivityStatus.onStateChange(this, ActivityStatus.STARTED); + } + + @Override + protected void onPause() { + ActivityStatus.onStateChange(this, ActivityStatus.STARTED); + super.onPause(); + } + + @Override + protected void onStop() { + ActivityStatus.onStateChange(this, ActivityStatus.STARTED); + super.onStop(); + } + + @Override + protected void onDestroy() { + ActivityStatus.onStateChange(this, ActivityStatus.DESTROYED); + super.onDestroy(); + } +} |