Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: base/android/activity_status.h

Issue 14373019: Add base/android/activity_status.cc (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address Marcus' nits + formatting Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 template <typename T> struct LeakySingletonTraits;
joth 2013/04/26 20:29:10 is this needed?
digit1 2013/04/29 12:22:10 Good catch, no it isn't, it's a remnant of a previ
17
18 namespace base {
19 namespace android {
20
21 // Define activity state values like ACTIVITY_STATE_CREATED in a
22 // way that ensures they're always the same than their Java counterpart.
23 enum ActivityState {
24 #define DEFINE_ACTIVITY_STATE(x,y) ACTIVITY_STATE_ ## x = y,
25 #include "base/android/activity_state_list.h"
26 #undef DEFINE_ACTIVITY_STATE
27 };
28
29 // A native helper class to listen to activity state changes from
joth 2013/04/26 20:29:10 maybe helpful to spell out that these are Android
digit1 2013/04/29 12:22:10 it's the current one (when active), since there ca
30 // any thread.
31 //
32 // To start listening, create a new instance, passing a callback to a
33 // function that takes an ActivityState parameter. To stop listening,
34 // simply delete the listener object. The implementation guarantees
35 // that the callback will always be called on the same thread than
joth 2013/04/26 20:29:10 nit: on the same thread _that was_ the one that cr
digit1 2013/04/29 12:22:10 Done.
36 // the one that created the listener.
37 //
38 // Example:
39 //
40 // void OnActivityStateChange(ActivityState state) {
41 // ...
42 // }
43 //
44 // // Start listening.
45 // ActivityStatus::Listener* my_listener =
46 // new ActivityStatus::Listener(base::Bind(&OnActivityStateChange));
47 //
48 // ...
49 //
50 // // Stop listening.
51 // delete my_listener
52 //
53 BASE_EXPORT class ActivityStatus {
54 public:
55 typedef base::Callback<void(ActivityState)> StateChangeCallback;
56
57 class Listener {
58 public:
59 explicit Listener(const StateChangeCallback& callback);
60 ~Listener();
61
62 private:
63 friend class ActivityStatus;
64
65 void Notify(ActivityState state);
66
67 StateChangeCallback callback_;
68
69 DISALLOW_COPY_AND_ASSIGN(Listener);
70 };
71
72 // NOTE: The Java ActivityStatus is a singleton too.
73 static ActivityStatus* GetInstance();
74
75 // Internal use: must be public to be called from base_jni_registrar.cc
76 static bool RegisterBindings(JNIEnv* env);
77
78 // Internal use only: must be public to be called from JNI.
79 void OnActivityStateChange(ActivityState new_state);
80
81 private:
82 friend struct DefaultSingletonTraits<ActivityStatus>;
83
84 ActivityStatus();
85 ~ActivityStatus();
86
87 void RegisterListener(Listener* listener);
88 void UnregisterListener(Listener* listener);
89
90 scoped_refptr<ObserverListThreadSafe<Listener> > observers_;
91
92 DISALLOW_COPY_AND_ASSIGN(ActivityStatus);
93 };
94
95 } // namespace android
96 } // namespace base
97
98 #endif // BASE_ANDROID_ACTIVITY_STATUS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698