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 | |
18 // StateListener.onStateChange | |
Philippe
2012/12/04 16:12:42
Nit: I think this can fit on the line above. Gener
digit1
2012/12/04 17:31:23
Done.
| |
19 public static final int CREATED = 1; | |
20 public static final int STARTED = 2; | |
21 public static final int RESUMED = 3; | |
22 public static final int PAUSED = 4; | |
23 public static final int STOPPED = 5; | |
24 public static final int DESTROYED = 6; | |
25 | |
26 // Current activity, or null if none. | |
27 private static Activity sActivity; | |
28 | |
29 // Current activity's state. This can be set to PAUSED or RESUMED | |
30 // even if there sActivity is null to facilite unit testing. | |
31 private static int sActivityState; | |
32 | |
33 // Current activity instance, or null. | |
34 private static ActivityStatus sInstance; | |
35 | |
36 // List of deprecated pause/resume listeners. | |
digit1
2012/12/04 15:45:34
Ignore the "deprecated" here, I'll remove it.
digit1
2012/12/04 17:31:23
Done.
| |
37 private ArrayList<Listener> mListeners = new ArrayList<Listener>(); | |
Philippe
2012/12/04 16:12:42
Nit: you can make this field and the one below 'fi
digit1
2012/12/04 17:31:23
Done.
| |
38 | |
39 // List of state listeners. | |
40 private ArrayList<StateListener> mStateListeners = new ArrayList<StateListen er>(); | |
41 | |
42 /** | |
43 * Must be called by the activity changes state. | |
44 * @param activity Current activity. | |
45 * @param newState New state value. | |
46 */ | |
47 public static void onStateChange(Activity activity, int newState) { | |
48 if (newState == CREATED) | |
49 sActivity = activity; | |
Philippe
2012/12/04 16:12:42
Nit: 4 space indentation here and in some other pl
digit1
2012/12/04 17:31:23
Done.
| |
50 | |
51 sActivityState = newState; | |
52 getInstance().changeState(newState); | |
53 | |
54 if (newState == DESTROYED) | |
55 sActivity = null; | |
56 } | |
57 | |
58 private void changeState(int newState) { | |
59 for (StateListener listener : mStateListeners) { | |
60 listener.onActivityStateChange(newState); | |
61 } | |
62 if (newState == PAUSED || newState == RESUMED) { | |
63 boolean paused = (newState == PAUSED); | |
64 for (Listener listener : mListeners) { | |
65 listener.onActivityStatusChanged(paused); | |
66 } | |
67 } | |
68 } | |
69 | |
70 // This interface can only be used to listen to PAUSED and RESUMED events. | |
15 public interface Listener { | 71 public interface Listener { |
Philippe
2012/12/04 16:12:42
Nit: Maybe add a comment here to say that this lis
digit1
2012/12/04 17:31:23
Done.
| |
16 /** | 72 /** |
17 * Called when the activity's status changes. | 73 * Called when the activity's status changes. |
18 * @param isPaused true if the activity is paused, false if not. | 74 * @param isPaused true if the activity is paused, false if not. |
19 */ | 75 */ |
20 public void onActivityStatusChanged(boolean isPaused); | 76 public void onActivityStatusChanged(boolean isPaused); |
digit1
2012/12/04 15:41:55
Note that I'm pretty sure that the Android convent
| |
21 } | 77 } |
22 | 78 |
23 private boolean mIsPaused = false; | 79 // Use this interface to listen to all state changes. |
24 private ArrayList<Listener> mListeners = new ArrayList<Listener>(); | 80 public interface StateListener { |
25 private static ActivityStatus sActivityStatus; | 81 /** |
26 | 82 * Called when the activity's state changes. |
27 private ActivityStatus() { | 83 * @param activity Current activity. |
Philippe
2012/12/04 16:12:42
Nit: It seems that you got rid of |activity| and |
digit1
2012/12/04 17:31:23
Done.
| |
84 * @param oldState Previous activity state. | |
85 * @param newState New activity state. | |
86 */ | |
87 public void onActivityStateChange(int newState); | |
28 } | 88 } |
29 | 89 |
30 public static ActivityStatus getInstance() { | 90 public static ActivityStatus getInstance() { |
31 // Can only be called on the UI thread. | 91 // Can only be called on the UI thread. |
32 assert Looper.myLooper() == Looper.getMainLooper(); | 92 assert Looper.myLooper() == Looper.getMainLooper(); |
33 if (sActivityStatus == null) { | 93 if (sInstance == null) { |
34 sActivityStatus = new ActivityStatus(); | 94 sInstance = new ActivityStatus(); |
Philippe
2012/12/04 16:12:42
Nit: as a bonus you could add a protected construc
digit1
2012/12/04 17:31:23
Done.
| |
35 } | 95 } |
36 return sActivityStatus; | 96 return sInstance; |
37 } | 97 } |
38 | 98 |
39 /** | 99 /** |
40 * Indicates that the parent activity was paused. | 100 * Indicates that the parent activity was paused. |
41 */ | 101 */ |
42 public void onPause() { | 102 public void onPause() { |
43 mIsPaused = true; | 103 changeState(PAUSED); |
44 informAllListeners(); | |
45 } | 104 } |
46 | 105 |
47 /** | 106 /** |
48 * Indicates that the parent activity was resumed. | 107 * Indicates that the parent activity was resumed. |
49 */ | 108 */ |
50 public void onResume() { | 109 public void onResume() { |
51 mIsPaused = false; | 110 changeState(RESUMED); |
52 informAllListeners(); | |
53 } | 111 } |
54 | 112 |
55 /** | 113 /** |
56 * Indicates that the parent activity is currently paused. | 114 * Indicates that the parent activity is currently paused. |
57 */ | 115 */ |
58 public boolean isPaused() { | 116 public boolean isPaused() { |
59 return mIsPaused; | 117 return sActivityState == PAUSED; |
60 } | 118 } |
61 | 119 |
62 /** | 120 /** |
63 * Registers the given listener to receive activity status updates. | 121 * Returns the current main application activity. |
122 */ | |
123 static public Activity getActivity() { | |
Philippe
2012/12/04 16:12:42
Nit: I think 'public static' is more common.
digit1
2012/12/04 17:31:23
Done.
| |
124 return sActivity; | |
125 } | |
126 | |
127 /** | |
128 * Returns the current main application activity's state. | |
129 */ | |
130 static public int getState() { | |
131 if (sActivity == null) | |
132 return DESTROYED; | |
133 else | |
134 return sActivityState; | |
135 } | |
136 | |
137 /** | |
138 * Registers the given (deprecated) listener to receive activity | |
139 * status updates. Use registerStateListener() instead. | |
64 * @param listener Listener to receive status updates. | 140 * @param listener Listener to receive status updates. |
65 */ | 141 */ |
66 public void registerListener(Listener listener) { | 142 public void registerListener(Listener listener) { |
67 mListeners.add(listener); | 143 mListeners.add(listener); |
68 } | 144 } |
69 | 145 |
70 /** | 146 /** |
71 * Unregisters the given listener from receiving activity status updates. | 147 * Unregisters the given deprecated listener from receiving activity |
148 * status updates. Use unregisterStateListener() instead. | |
72 * @param listener Listener that doesn't want to receive status updates. | 149 * @param listener Listener that doesn't want to receive status updates. |
73 */ | 150 */ |
74 public void unregisterListener(Listener listener) { | 151 public void unregisterListener(Listener listener) { |
75 mListeners.remove(listener); | 152 mListeners.remove(listener); |
76 } | 153 } |
77 | 154 |
78 private void informAllListeners() { | 155 /** |
79 for (Listener listener : mListeners) { | 156 * Registers the given listener to receive activity state changes. |
80 listener.onActivityStatusChanged(mIsPaused); | 157 * @param listener Listener to receive state changes. |
81 } | 158 */ |
159 public static void registerStateListener(StateListener listener) { | |
160 ActivityStatus status = getInstance(); | |
161 status.mStateListeners.add(listener); | |
162 } | |
163 | |
164 /** | |
165 * Unregisters the given listener from receiving activity state changes. | |
166 * @param listener Listener that doesn't want to receive state changes. | |
167 */ | |
168 public static void unregisterStateListener(StateListener listener) { | |
169 ActivityStatus status = getInstance(); | |
170 status.mStateListeners.remove(listener); | |
82 } | 171 } |
83 } | 172 } |
OLD | NEW |