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.
5 #include "base/android/activity_status.h"
9 #include "base/memory/singleton.h"
10 #include "jni/ActivityStatus_jni.h"
15 ActivityStatus::Listener::Listener(
16 const ActivityStatus::StateChangeCallback
& callback
)
17 : callback_(callback
) {
18 ActivityStatus::GetInstance()->RegisterListener(this);
21 ActivityStatus::Listener::~Listener() {
22 ActivityStatus::GetInstance()->UnregisterListener(this);
25 void ActivityStatus::Listener::Notify(ActivityState state
) {
30 ActivityStatus
* ActivityStatus::GetInstance() {
31 return Singleton
<ActivityStatus
,
32 LeakySingletonTraits
<ActivityStatus
> >::get();
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
);
41 bool ActivityStatus::RegisterBindings(JNIEnv
* env
) {
42 return RegisterNativesImpl(env
);
45 ActivityStatus::ActivityStatus()
46 : observers_(new ObserverListThreadSafe
<Listener
>()) {
47 Java_ActivityStatus_registerThreadSafeNativeStateListener(
48 base::android::AttachCurrentThread());
51 ActivityStatus::~ActivityStatus() {}
53 void ActivityStatus::RegisterListener(Listener
* listener
) {
54 observers_
->AddObserver(listener
);
57 void ActivityStatus::UnregisterListener(Listener
* listener
) {
58 observers_
->RemoveObserver(listener
);
61 void ActivityStatus::OnActivityStateChange(ActivityState new_state
) {
62 observers_
->Notify(&ActivityStatus::Listener::Notify
, new_state
);
65 } // namespace android