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