1 // Copyright 2014 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 "content/browser/battery_status/battery_status_manager_android.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/scoped_java_ref.h"
9 #include "base/metrics/histogram.h"
10 #include "jni/BatteryStatusManager_jni.h"
11
12 using base::android::AttachCurrentThread;
13
14 namespace content {
15
BatteryStatusManagerAndroid(const BatteryStatusService::BatteryUpdateCallback & callback)16 BatteryStatusManagerAndroid::BatteryStatusManagerAndroid(
17 const BatteryStatusService::BatteryUpdateCallback& callback)
18 : callback_(callback) {
19 j_manager_.Reset(
20 Java_BatteryStatusManager_getInstance(
21 AttachCurrentThread(), base::android::GetApplicationContext()));
22 }
23
~BatteryStatusManagerAndroid()24 BatteryStatusManagerAndroid::~BatteryStatusManagerAndroid() {
25 }
26
27 // static
Register(JNIEnv * env)28 bool BatteryStatusManagerAndroid::Register(JNIEnv* env) {
29 return RegisterNativesImpl(env);
30 }
31
GotBatteryStatus(JNIEnv *,jobject,jboolean charging,jdouble charging_time,jdouble discharging_time,jdouble level)32 void BatteryStatusManagerAndroid::GotBatteryStatus(JNIEnv*,
33 jobject,
34 jboolean charging,
35 jdouble charging_time,
36 jdouble discharging_time,
37 jdouble level) {
38 blink::WebBatteryStatus status;
39 status.charging = charging;
40 status.chargingTime = charging_time;
41 status.dischargingTime = discharging_time;
42 status.level = level;
43 callback_.Run(status);
44 }
45
StartListeningBatteryChange()46 bool BatteryStatusManagerAndroid::StartListeningBatteryChange() {
47 bool result = Java_BatteryStatusManager_start(AttachCurrentThread(),
48 j_manager_.obj(), reinterpret_cast<intptr_t>(this));
49 UMA_HISTOGRAM_BOOLEAN("BatteryStatus.StartAndroid", result);
50 return result;
51 }
52
StopListeningBatteryChange()53 void BatteryStatusManagerAndroid::StopListeningBatteryChange() {
54 Java_BatteryStatusManager_stop(
55 AttachCurrentThread(), j_manager_.obj());
56 }
57
58 // static
Create(const BatteryStatusService::BatteryUpdateCallback & callback)59 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create(
60 const BatteryStatusService::BatteryUpdateCallback& callback) {
61 return scoped_ptr<BatteryStatusManager>(
62 new BatteryStatusManagerAndroid(callback));
63 }
64
65 } // namespace content
66