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.Handler; |
8 import android.os.Looper; | 9 import android.os.Looper; |
9 | 10 |
10 /** | 11 /** |
11 * Provides information about the parent activity's status. | 12 * Provides information about the parent activity's status. |
12 */ | 13 */ |
| 14 @JNINamespace("base::android") |
13 public class ActivityStatus { | 15 public class ActivityStatus { |
14 | 16 |
15 // Constants matching activity states reported to StateListener.onStateChang
e | 17 // Constants matching activity states reported to StateListener.onStateChang
e |
16 public static final int CREATED = 1; | 18 // As an implementation detail, these are now defined in the auto-generated |
17 public static final int STARTED = 2; | 19 // ActivityState interface, to be shared with C++. |
18 public static final int RESUMED = 3; | 20 public static final int CREATED = ActivityState.CREATED; |
19 public static final int PAUSED = 4; | 21 public static final int STARTED = ActivityState.STARTED; |
20 public static final int STOPPED = 5; | 22 public static final int RESUMED = ActivityState.RESUMED; |
21 public static final int DESTROYED = 6; | 23 public static final int PAUSED = ActivityState.PAUSED; |
| 24 public static final int STOPPED = ActivityState.STOPPED; |
| 25 public static final int DESTROYED = ActivityState.DESTROYED; |
22 | 26 |
23 // Current main activity, or null if none. | 27 // Current main activity, or null if none. |
24 private static Activity sActivity; | 28 private static Activity sActivity; |
25 | 29 |
26 // Current main activity's state. This can be set even if sActivity is null,
to simplify unit | 30 // Current main activity's state. This can be set even if sActivity is null,
to simplify unit |
27 // testing. | 31 // testing. |
28 private static int sActivityState; | 32 private static int sActivityState; |
29 | 33 |
30 private static final ObserverList<StateListener> sStateListeners = | 34 private static final ObserverList<StateListener> sStateListeners = |
31 new ObserverList<StateListener>(); | 35 new ObserverList<StateListener>(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 sStateListeners.addObserver(listener); | 99 sStateListeners.addObserver(listener); |
96 } | 100 } |
97 | 101 |
98 /** | 102 /** |
99 * Unregisters the given listener from receiving activity state changes. | 103 * Unregisters the given listener from receiving activity state changes. |
100 * @param listener Listener that doesn't want to receive state changes. | 104 * @param listener Listener that doesn't want to receive state changes. |
101 */ | 105 */ |
102 public static void unregisterStateListener(StateListener listener) { | 106 public static void unregisterStateListener(StateListener listener) { |
103 sStateListeners.removeObserver(listener); | 107 sStateListeners.removeObserver(listener); |
104 } | 108 } |
| 109 |
| 110 /** |
| 111 * Registers the single thread-safe native activity status listener. |
| 112 * This handles the case where the caller is not on the main thread. |
| 113 * Note that this is used by a leaky singleton object from the native |
| 114 * side, hence lifecycle management is greatly simplified. |
| 115 */ |
| 116 @CalledByNative |
| 117 private static void registerThreadSafeNativeStateListener() { |
| 118 Looper mainLooper = Looper.getMainLooper(); |
| 119 // If not on the main thread, post a Handler to perform the registration
on it |
| 120 // then return immediately. Note that registration will happen later. |
| 121 if (Looper.myLooper() != mainLooper) { |
| 122 new Handler(mainLooper).post(new Runnable() { |
| 123 @Override |
| 124 public void run() { |
| 125 // Note that this native method has to be static |
| 126 registerThreadSafeNativeStateListener(); |
| 127 } |
| 128 }); |
| 129 return; |
| 130 } |
| 131 |
| 132 // Otherwise register a new listener that calls nativeOnActivityStateCha
nge. |
| 133 sStateListeners.addObserver(new StateListener() { |
| 134 @Override |
| 135 public void onActivityStateChange(int newState) { |
| 136 nativeOnActivityStateChange(newState); |
| 137 } |
| 138 }); |
| 139 } |
| 140 |
| 141 // Called to notify the native side of state changes. |
| 142 // IMPORTANT: This is always called on the main thread! |
| 143 private static native void nativeOnActivityStateChange(int newState); |
105 } | 144 } |
OLD | NEW |