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 #include "base/android/activity_status.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/memory/singleton.h" | |
10 #include "jni/ActivityStatus_jni.h" | |
11 | |
12 namespace base { | |
13 namespace android { | |
14 | |
15 ActivityStatus::Listener::Listener( | |
16 const ActivityStatus::StateChangeCallback& callback) | |
17 : callback_(callback) { | |
18 ActivityStatus::GetInstance()->RegisterListener(this); | |
19 } | |
20 | |
21 ActivityStatus::Listener::~Listener() { | |
22 ActivityStatus::GetInstance()->UnregisterListener(this); | |
23 } | |
24 | |
25 void ActivityStatus::Listener::Notify(ActivityState state) { | |
26 callback_.Run(state); | |
27 } | |
28 | |
29 // static | |
30 ActivityStatus* ActivityStatus::GetInstance() { | |
31 return Singleton<ActivityStatus, | |
32 LeakySingletonTraits<ActivityStatus> >::get(); | |
33 } | |
34 | |
35 static void OnActivityStateChange(JNIEnv* env, jclass clazz, int new_state) { | |
36 ActivityStatus* activity_status = ActivityStatus::GetInstance(); | |
37 ActivityState activity_state = static_cast<ActivityState>(new_state); | |
38 activity_status->OnActivityStateChange(activity_state); | |
39 } | |
40 | |
41 bool ActivityStatus::RegisterBindings(JNIEnv* env) { | |
42 return RegisterNativesImpl(env); | |
43 } | |
44 | |
45 ActivityStatus::ActivityStatus() | |
46 : observers_(new ObserverListThreadSafe<Listener>()) { | |
47 Java_ActivityStatus_registerThreadSafeNativeStateListener( | |
48 base::android::AttachCurrentThread()); | |
49 } | |
50 | |
51 ActivityStatus::~ActivityStatus() {} | |
52 | |
53 void ActivityStatus::RegisterListener(Listener* listener) { | |
54 observers_->AddObserver(listener); | |
55 } | |
56 | |
57 void ActivityStatus::UnregisterListener(Listener* listener) { | |
58 observers_->RemoveObserver(listener); | |
59 } | |
60 | |
61 void ActivityStatus::OnActivityStateChange(ActivityState new_state) { | |
62 observers_->Notify(&ActivityStatus::Listener::Notify, new_state); | |
63 } | |
64 | |
65 } // namespace android | |
66 } // namespace base | |
OLD | NEW |