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; | |
Philippe
2012/12/04 17:49:21
Nit: I don't know what the general consensus is ab
joth
2012/12/04 22:42:09
In java (contrary to c++), an if() statement must
| |
54 | |
55 sActivityState = newState; | |
56 getInstance().changeState(newState); | |
57 | |
58 if (newState == DESTROYED) | |
59 sActivity = null; | |
60 } | |
61 | |
62 private void changeState(int newState) { | |
63 for (StateListener listener : mStateListeners) { | |
64 listener.onActivityStateChange(newState); | |
65 } | |
66 if (newState == PAUSED || newState == RESUMED) { | |
67 boolean paused = (newState == PAUSED); | |
68 for (Listener listener : mListeners) { | |
69 listener.onActivityStatusChanged(paused); | |
70 } | |
71 } | |
72 } | |
73 | |
74 // This interface can only be used to listen to PAUSED and RESUMED | |
Philippe
2012/12/04 17:49:21
Nit: I think that Javadoc comments are preferred a
joth
2012/12/04 22:42:09
yes,
Java DOC if it's something the caller needs t
| |
75 // events. Deprecated, new code should use StateListener instead. | |
76 // TODO(digit): Remove once all users have switched to StateListener. | |
77 @Deprecated | |
15 public interface Listener { | 78 public interface Listener { |
16 /** | 79 /** |
17 * Called when the activity's status changes. | 80 * Called when the activity is paused or resumed only. |
18 * @param isPaused true if the activity is paused, false if not. | 81 * @param isPaused true if the activity is paused, false if not. |
19 */ | 82 */ |
20 public void onActivityStatusChanged(boolean isPaused); | 83 public void onActivityStatusChanged(boolean isPaused); |
21 } | 84 } |
22 | 85 |
23 private boolean mIsPaused = false; | 86 // Use this interface to listen to all state changes. |
24 private ArrayList<Listener> mListeners = new ArrayList<Listener>(); | 87 public interface StateListener { |
25 private static ActivityStatus sActivityStatus; | 88 /** |
26 | 89 * Called when the activity's state changes. |
27 private ActivityStatus() { | 90 * @param newState New activity state. |
91 */ | |
92 public void onActivityStateChange(int newState); | |
28 } | 93 } |
29 | 94 |
30 public static ActivityStatus getInstance() { | 95 public static ActivityStatus getInstance() { |
31 // Can only be called on the UI thread. | 96 // Can only be called on the UI thread. |
32 assert Looper.myLooper() == Looper.getMainLooper(); | 97 assert Looper.myLooper() == Looper.getMainLooper(); |
33 if (sActivityStatus == null) { | 98 if (sInstance == null) { |
34 sActivityStatus = new ActivityStatus(); | 99 sInstance = new ActivityStatus(); |
35 } | 100 } |
36 return sActivityStatus; | 101 return sInstance; |
37 } | 102 } |
38 | 103 |
39 /** | 104 /** |
40 * Indicates that the parent activity was paused. | 105 * Indicates that the parent activity was paused. |
41 */ | 106 */ |
42 public void onPause() { | 107 public void onPause() { |
43 mIsPaused = true; | 108 changeState(PAUSED); |
44 informAllListeners(); | |
45 } | 109 } |
46 | 110 |
47 /** | 111 /** |
48 * Indicates that the parent activity was resumed. | 112 * Indicates that the parent activity was resumed. |
49 */ | 113 */ |
50 public void onResume() { | 114 public void onResume() { |
51 mIsPaused = false; | 115 changeState(RESUMED); |
52 informAllListeners(); | |
53 } | 116 } |
54 | 117 |
55 /** | 118 /** |
56 * Indicates that the parent activity is currently paused. | 119 * Indicates that the parent activity is currently paused. |
57 */ | 120 */ |
58 public boolean isPaused() { | 121 public boolean isPaused() { |
59 return mIsPaused; | 122 return sActivityState == PAUSED; |
60 } | 123 } |
61 | 124 |
62 /** | 125 /** |
63 * Registers the given listener to receive activity status updates. | 126 * Returns the current main application activity. |
127 */ | |
128 public static Activity getActivity() { | |
129 return sActivity; | |
130 } | |
131 | |
132 /** | |
133 * Returns the current main application activity's state. | |
134 */ | |
135 public static int getState() { | |
136 // To simplify unit testing, don't check sActivity for null. | |
137 return sActivityState; | |
138 } | |
139 | |
140 /** | |
141 * Registers the given pause/resume listener to receive activity | |
142 * status updates. Use registerStateListener() instead. | |
64 * @param listener Listener to receive status updates. | 143 * @param listener Listener to receive status updates. |
65 */ | 144 */ |
66 public void registerListener(Listener listener) { | 145 public void registerListener(Listener listener) { |
67 mListeners.add(listener); | 146 mListeners.add(listener); |
68 } | 147 } |
69 | 148 |
70 /** | 149 /** |
71 * Unregisters the given listener from receiving activity status updates. | 150 * Unregisters the given pause/resume listener from receiving activity |
151 * status updates. Use unregisterStateListener() instead. | |
72 * @param listener Listener that doesn't want to receive status updates. | 152 * @param listener Listener that doesn't want to receive status updates. |
73 */ | 153 */ |
74 public void unregisterListener(Listener listener) { | 154 public void unregisterListener(Listener listener) { |
75 mListeners.remove(listener); | 155 mListeners.remove(listener); |
76 } | 156 } |
77 | 157 |
78 private void informAllListeners() { | 158 /** |
79 for (Listener listener : mListeners) { | 159 * Registers the given listener to receive activity state changes. |
80 listener.onActivityStatusChanged(mIsPaused); | 160 * @param listener Listener to receive state changes. |
81 } | 161 */ |
162 public static void registerStateListener(StateListener listener) { | |
163 ActivityStatus status = getInstance(); | |
164 status.mStateListeners.add(listener); | |
165 } | |
166 | |
167 /** | |
168 * Unregisters the given listener from receiving activity state changes. | |
169 * @param listener Listener that doesn't want to receive state changes. | |
170 */ | |
171 public static void unregisterStateListener(StateListener listener) { | |
172 ActivityStatus status = getInstance(); | |
173 status.mStateListeners.remove(listener); | |
82 } | 174 } |
83 } | 175 } |
OLD | NEW |