| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.os.Looper; | 8 import android.os.Looper; |
| 9 | 9 |
| 10 import java.util.ArrayList; | 10 import java.util.ArrayList; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Provides information about the parent activity's status. | 13 * Provides information about the parent activity's status. |
| 14 */ | 14 */ |
| 15 public class ActivityStatus { | 15 public class ActivityStatus { |
| 16 | 16 |
| 17 // Constants matching activity states reported to StateListener.onStateChang
e | 17 // Constants matching activity states reported to StateListener.onStateChang
e |
| 18 public static final int CREATED = 1; | 18 public static final int CREATED = 1; |
| 19 public static final int STARTED = 2; | 19 public static final int STARTED = 2; |
| 20 public static final int RESUMED = 3; | 20 public static final int RESUMED = 3; |
| 21 public static final int PAUSED = 4; | 21 public static final int PAUSED = 4; |
| 22 public static final int STOPPED = 5; | 22 public static final int STOPPED = 5; |
| 23 public static final int DESTROYED = 6; | 23 public static final int DESTROYED = 6; |
| 24 | 24 |
| 25 // Current main activity, or null if none. | |
| 26 private static Activity sActivity; | |
| 27 | |
| 28 // Current main activity's state. This can be set even if sActivity | |
| 29 // is null, to simplify unit testing. | |
| 30 private static int sActivityState; | 25 private static int sActivityState; |
| 31 | 26 private static final ArrayList<StateListener> sStateListeners = new ArrayLis
t<StateListener>(); |
| 32 // Current activity instance, or null. | |
| 33 private static ActivityStatus sInstance; | |
| 34 | |
| 35 // List of pause/resume listeners. | |
| 36 private final ArrayList<Listener> mListeners; | |
| 37 | |
| 38 // List of state listeners. | |
| 39 private final ArrayList<StateListener> mStateListeners; | |
| 40 | |
| 41 protected ActivityStatus() { | |
| 42 mListeners = new ArrayList<Listener>(); | |
| 43 mStateListeners = new ArrayList<StateListener>(); | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Must be called by the main activity when it changes state. | |
| 48 * @param activity Current activity. | |
| 49 * @param newState New state value. | |
| 50 */ | |
| 51 public static void onStateChange(Activity activity, int newState) { | |
| 52 if (newState == CREATED) { | |
| 53 sActivity = activity; | |
| 54 } | |
| 55 sActivityState = newState; | |
| 56 getInstance().changeState(newState); | |
| 57 | |
| 58 if (newState == DESTROYED) { | |
| 59 sActivity = null; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 private void changeState(int newState) { | |
| 64 for (StateListener listener : mStateListeners) { | |
| 65 listener.onActivityStateChange(newState); | |
| 66 } | |
| 67 if (newState == PAUSED || newState == RESUMED) { | |
| 68 boolean paused = (newState == PAUSED); | |
| 69 for (Listener listener : mListeners) { | |
| 70 listener.onActivityStatusChanged(paused); | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 // This interface can only be used to listen to PAUSED and RESUMED | |
| 76 // events. Deprecated, new code should use StateListener instead. | |
| 77 // TODO(digit): Remove once all users have switched to StateListener. | |
| 78 @Deprecated | |
| 79 public interface Listener { | |
| 80 /** | |
| 81 * Called when the activity is paused or resumed only. | |
| 82 * @param isPaused true if the activity is paused, false if not. | |
| 83 */ | |
| 84 public void onActivityStatusChanged(boolean isPaused); | |
| 85 } | |
| 86 | 27 |
| 87 // Use this interface to listen to all state changes. | 28 // Use this interface to listen to all state changes. |
| 88 public interface StateListener { | 29 public interface StateListener { |
| 89 /** | 30 /** |
| 90 * Called when the activity's state changes. | 31 * Called when the activity's state changes. |
| 91 * @param newState New activity state. | 32 * @param newState New activity state. |
| 92 */ | 33 */ |
| 93 public void onActivityStateChange(int newState); | 34 public void onActivityStateChange(Activity activity, int newState); |
| 94 } | 35 } |
| 95 | 36 |
| 96 /** | 37 /** |
| 97 * Returns the current ActivityStatus instance. | 38 * Must be called by the main activity when it changes state. |
| 39 * @param activity Current activity. |
| 40 * @param newState New state value. |
| 98 */ | 41 */ |
| 99 public static ActivityStatus getInstance() { | 42 public static void onStateChange(Activity activity, int newState) { |
| 100 // Can only be called on the UI thread. | 43 sActivityState = newState; |
| 101 assert Looper.myLooper() == Looper.getMainLooper(); | 44 for (StateListener listener : sStateListeners) { |
| 102 if (sInstance == null) { | 45 listener.onActivityStateChange(activity, newState); |
| 103 sInstance = new ActivityStatus(); | |
| 104 } | 46 } |
| 105 return sInstance; | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * Indicates that the parent activity was paused. | |
| 110 */ | |
| 111 public void onPause() { | |
| 112 changeState(PAUSED); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Indicates that the parent activity was resumed. | |
| 117 */ | |
| 118 public void onResume() { | |
| 119 changeState(RESUMED); | |
| 120 } | 47 } |
| 121 | 48 |
| 122 /** | 49 /** |
| 123 * Indicates that the parent activity is currently paused. | 50 * Indicates that the parent activity is currently paused. |
| 124 */ | 51 */ |
| 125 public boolean isPaused() { | 52 public static boolean isPaused() { |
| 126 return sActivityState == PAUSED; | 53 return sActivityState == PAUSED; |
| 127 } | 54 } |
| 128 | 55 |
| 129 /** | 56 /** |
| 130 * Returns the current main application activity. | |
| 131 */ | |
| 132 public static Activity getActivity() { | |
| 133 return sActivity; | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Returns the current main application activity's state. | 57 * Returns the current main application activity's state. |
| 138 */ | 58 */ |
| 139 public static int getState() { | 59 public static int getState() { |
| 140 // To simplify unit testing, don't check sActivity for null. | |
| 141 return sActivityState; | 60 return sActivityState; |
| 142 } | 61 } |
| 143 | 62 |
| 144 /** | 63 /** |
| 145 * Registers the given pause/resume listener to receive activity | |
| 146 * status updates. Use registerStateListener() instead. | |
| 147 * @param listener Listener to receive status updates. | |
| 148 */ | |
| 149 public void registerListener(Listener listener) { | |
| 150 mListeners.add(listener); | |
| 151 } | |
| 152 | |
| 153 /** | |
| 154 * Unregisters the given pause/resume listener from receiving activity | |
| 155 * status updates. Use unregisterStateListener() instead. | |
| 156 * @param listener Listener that doesn't want to receive status updates. | |
| 157 */ | |
| 158 public void unregisterListener(Listener listener) { | |
| 159 mListeners.remove(listener); | |
| 160 } | |
| 161 | |
| 162 /** | |
| 163 * Registers the given listener to receive activity state changes. | 64 * Registers the given listener to receive activity state changes. |
| 164 * @param listener Listener to receive state changes. | 65 * @param listener Listener to receive state changes. |
| 165 */ | 66 */ |
| 166 public static void registerStateListener(StateListener listener) { | 67 public static void registerStateListener(StateListener listener) { |
| 167 ActivityStatus status = getInstance(); | 68 sStateListeners.add(listener); |
| 168 status.mStateListeners.add(listener); | |
| 169 } | 69 } |
| 170 | 70 |
| 171 /** | 71 /** |
| 172 * Unregisters the given listener from receiving activity state changes. | 72 * Unregisters the given listener from receiving activity state changes. |
| 173 * @param listener Listener that doesn't want to receive state changes. | 73 * @param listener Listener that doesn't want to receive state changes. |
| 174 */ | 74 */ |
| 175 public static void unregisterStateListener(StateListener listener) { | 75 public static void unregisterStateListener(StateListener listener) { |
| 176 ActivityStatus status = getInstance(); | 76 sStateListeners.remove(listener); |
| 177 status.mStateListeners.remove(listener); | |
| 178 } | 77 } |
| 179 } | 78 } |
| OLD | NEW |