OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_ANDROID_ACTIVITY_STATUS_H_ | |
6 #define BASE_ANDROID_ACTIVITY_STATUS_H_ | |
7 | |
8 #include <jni.h> | |
9 | |
10 #include "base/android/jni_android.h" | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/observer_list_threadsafe.h" | |
15 | |
16 namespace base { | |
17 namespace android { | |
18 | |
19 // Define activity state values like ACTIVITY_STATE_CREATED in a | |
20 // way that ensures they're always the same than their Java counterpart. | |
21 enum ActivityState { | |
22 #define DEFINE_ACTIVITY_STATE(x, y) ACTIVITY_STATE_##x = y, | |
23 #include "base/android/activity_state_list.h" | |
24 #undef DEFINE_ACTIVITY_STATE | |
25 }; | |
26 | |
27 // A native helper class to listen to state changes of the current | |
28 // Android Activity. This mirrors org.chromium.base.ActivityStatus. | |
29 // any thread. | |
30 // | |
31 // To start listening, create a new instance, passing a callback to a | |
32 // function that takes an ActivityState parameter. To stop listening, | |
33 // simply delete the listener object. The implementation guarantees | |
34 // that the callback will always be called on the thread that created | |
35 // the listener. | |
36 // | |
37 // Example: | |
38 // | |
39 // void OnActivityStateChange(ActivityState state) { | |
40 // ... | |
41 // } | |
42 // | |
43 // // Start listening. | |
44 // ActivityStatus::Listener* my_listener = | |
45 // new ActivityStatus::Listener(base::Bind(&OnActivityStateChange)); | |
46 // | |
47 // ... | |
48 // | |
49 // // Stop listening. | |
50 // delete my_listener | |
51 // | |
52 BASE_EXPORT class ActivityStatus { | |
tfarina
2013/05/06 14:22:16
I think it is more common class BASE_EXPORT, no?
Philippe
2013/05/06 14:40:02
Indeed, that's why I recently landed https://code
| |
53 public: | |
54 typedef base::Callback<void(ActivityState)> StateChangeCallback; | |
55 | |
56 class Listener { | |
tfarina
2013/05/06 14:22:16
could you move this into its own header file? Acti
Philippe
2013/05/06 14:40:02
Sure. This will be a good change. Clients don't ne
| |
57 public: | |
58 explicit Listener(const StateChangeCallback& callback); | |
59 ~Listener(); | |
60 | |
61 private: | |
62 friend class ActivityStatus; | |
63 | |
64 void Notify(ActivityState state); | |
65 | |
66 StateChangeCallback callback_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(Listener); | |
69 }; | |
70 | |
71 // NOTE: The Java ActivityStatus is a singleton too. | |
72 static ActivityStatus* GetInstance(); | |
73 | |
74 // Internal use: must be public to be called from base_jni_registrar.cc | |
75 static bool RegisterBindings(JNIEnv* env); | |
76 | |
77 // Internal use only: must be public to be called from JNI. | |
78 void OnActivityStateChange(ActivityState new_state); | |
79 | |
80 private: | |
81 friend struct DefaultSingletonTraits<ActivityStatus>; | |
82 | |
83 ActivityStatus(); | |
84 ~ActivityStatus(); | |
85 | |
86 void RegisterListener(Listener* listener); | |
87 void UnregisterListener(Listener* listener); | |
88 | |
89 scoped_refptr<ObserverListThreadSafe<Listener> > observers_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(ActivityStatus); | |
92 }; | |
93 | |
94 } // namespace android | |
95 } // namespace base | |
96 | |
97 #endif // BASE_ANDROID_ACTIVITY_STATUS_H_ | |
OLD | NEW |