OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef BASE_ANDROID_ACTIVITY_STATUS_H_ | 5 #ifndef BASE_ANDROID_ACTIVITY_STATUS_H_ |
6 #define BASE_ANDROID_ACTIVITY_STATUS_H_ | 6 #define BASE_ANDROID_ACTIVITY_STATUS_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 | 9 |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/observer_list_threadsafe.h" | 15 #include "base/observer_list_threadsafe.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 namespace android { | 18 namespace android { |
19 | 19 |
20 // Define activity state values like ACTIVITY_STATE_CREATED in a | 20 // Define activity state values like APPLICATION_STATE_VISIBLE in a |
21 // way that ensures they're always the same than their Java counterpart. | 21 // way that ensures they're always the same than their Java counterpart. |
22 enum ActivityState { | 22 enum ApplicationState { |
23 #define DEFINE_ACTIVITY_STATE(x, y) ACTIVITY_STATE_##x = y, | 23 #define DEFINE_APPLICATION_STATE(x, y) APPLICATION_STATE_##x = y, |
24 #include "base/android/activity_state_list.h" | 24 #include "base/android/application_state_list.h" |
25 #undef DEFINE_ACTIVITY_STATE | 25 #undef DEFINE_APPLICATION_STATE |
26 }; | 26 }; |
27 | 27 |
28 // A native helper class to listen to state changes of the current | 28 // A native helper class to listen to state changes of the Android |
29 // Android Activity. This mirrors org.chromium.base.ActivityStatus. | 29 // Application. This mirrors org.chromium.base.ActivityStatus. |
30 // any thread. | 30 // any thread. |
31 // | 31 // |
32 // To start listening, create a new instance, passing a callback to a | 32 // To start listening, create a new instance, passing a callback to a |
33 // function that takes an ActivityState parameter. To stop listening, | 33 // function that takes two boolean parameters. To stop listening, |
34 // simply delete the listener object. The implementation guarantees | 34 // simply delete the listener object. The implementation guarantees |
35 // that the callback will always be called on the thread that created | 35 // that the callback will always be called on the thread that created |
36 // the listener. | 36 // the listener. |
37 // | 37 // |
38 // Example: | 38 // Example: |
39 // | 39 // |
40 // void OnActivityStateChange(ActivityState state) { | 40 // void OnApplicationStateChange(bool foreground, bool active) { |
Ted C
2014/02/13 04:38:44
update since it's no longer two booleans
same in
David Trainor- moved to gerrit
2014/02/14 19:13:36
Done.
| |
41 // ... | 41 // ... |
42 // } | 42 // } |
43 // | 43 // |
44 // // Start listening. | 44 // // Start listening. |
45 // ActivityStatus::Listener* my_listener = | 45 // ActivityStatus::Listener* my_listener = |
46 // new ActivityStatus::Listener(base::Bind(&OnActivityStateChange)); | 46 // new ActivityStatus::Listener(base::Bind(&OnApplicationStateChange)); |
47 // | 47 // |
48 // ... | 48 // ... |
49 // | 49 // |
50 // // Stop listening. | 50 // // Stop listening. |
51 // delete my_listener | 51 // delete my_listener |
52 // | 52 // |
53 class BASE_EXPORT ActivityStatus { | 53 class BASE_EXPORT ActivityStatus { |
Ted C
2014/02/13 04:38:44
This should probably be called ApplicationStatus,
bulach
2014/02/13 11:17:48
yes, ApplicationStatus please :)
imho, either:
- k
| |
54 public: | 54 public: |
55 typedef base::Callback<void(ActivityState)> StateChangeCallback; | 55 typedef base::Callback<void(ApplicationState)> ApplicationStateChangeCallback; |
56 | 56 |
57 class Listener { | 57 class Listener { |
58 public: | 58 public: |
59 explicit Listener(const StateChangeCallback& callback); | 59 explicit Listener(const ApplicationStateChangeCallback& callback); |
60 ~Listener(); | 60 ~Listener(); |
61 | 61 |
62 private: | 62 private: |
63 friend class ActivityStatus; | 63 friend class ActivityStatus; |
64 | 64 |
65 void Notify(ActivityState state); | 65 void Notify(ApplicationState state); |
66 | 66 |
67 StateChangeCallback callback_; | 67 ApplicationStateChangeCallback callback_; |
68 | 68 |
69 DISALLOW_COPY_AND_ASSIGN(Listener); | 69 DISALLOW_COPY_AND_ASSIGN(Listener); |
70 }; | 70 }; |
71 | 71 |
72 // NOTE: The Java ActivityStatus is a singleton too. | 72 // NOTE: The Java ActivityStatus is a singleton too. |
73 static ActivityStatus* GetInstance(); | 73 static ActivityStatus* GetInstance(); |
74 | 74 |
75 // Internal use: must be public to be called from base_jni_registrar.cc | 75 // Internal use: must be public to be called from base_jni_registrar.cc |
76 static bool RegisterBindings(JNIEnv* env); | 76 static bool RegisterBindings(JNIEnv* env); |
77 | 77 |
78 // Internal use only: must be public to be called from JNI and unit tests. | 78 // Internal use only: must be public to be called from JNI and unit tests. |
79 void OnActivityStateChange(ActivityState new_state); | 79 void OnApplicationStateChange(ApplicationState state); |
80 | 80 |
81 private: | 81 private: |
82 friend struct DefaultSingletonTraits<ActivityStatus>; | 82 friend struct DefaultSingletonTraits<ActivityStatus>; |
83 | 83 |
84 ActivityStatus(); | 84 ActivityStatus(); |
85 ~ActivityStatus(); | 85 ~ActivityStatus(); |
86 | 86 |
87 void RegisterListener(Listener* listener); | 87 void RegisterListener(Listener* listener); |
88 void UnregisterListener(Listener* listener); | 88 void UnregisterListener(Listener* listener); |
89 | 89 |
90 scoped_refptr<ObserverListThreadSafe<Listener> > observers_; | 90 scoped_refptr<ObserverListThreadSafe<Listener> > observers_; |
91 | 91 |
92 DISALLOW_COPY_AND_ASSIGN(ActivityStatus); | 92 DISALLOW_COPY_AND_ASSIGN(ActivityStatus); |
93 }; | 93 }; |
94 | 94 |
95 } // namespace android | 95 } // namespace android |
96 } // namespace base | 96 } // namespace base |
97 | 97 |
98 #endif // BASE_ANDROID_ACTIVITY_STATUS_H_ | 98 #endif // BASE_ANDROID_ACTIVITY_STATUS_H_ |
OLD | NEW |