1 // Copyright 2013 The Chromium Authors 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/base_jni/PowerMonitor_jni.h" 6 #include "base/power_monitor/power_monitor.h" 7 #include "base/power_monitor/power_monitor_device_source.h" 8 #include "base/power_monitor/power_monitor_source.h" 9 #include "base/power_monitor/power_observer.h" 10 11 namespace base { 12 13 // A helper function which is a friend of PowerMonitorSource. ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event)14void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) { 15 PowerMonitorSource::ProcessPowerEvent(event); 16 } 17 18 // A helper function which is a friend of PowerMonitorSource. ProcessThermalEventHelper(PowerThermalObserver::DeviceThermalState new_thermal_state)19void ProcessThermalEventHelper( 20 PowerThermalObserver::DeviceThermalState new_thermal_state) { 21 PowerMonitorSource::ProcessThermalEvent(new_thermal_state); 22 } 23 24 namespace android { 25 26 namespace { 27 using DeviceThermalState = PowerThermalObserver::DeviceThermalState; 28 29 // See 30 // https://developer.android.com/reference/android/os/PowerManager#THERMAL_STATUS_CRITICAL 31 enum AndroidThermalStatus { 32 THERMAL_STATUS_NONE = 0, 33 THERMAL_STATUS_LIGHT = 1, 34 THERMAL_STATUS_MODERATE = 2, 35 THERMAL_STATUS_SEVERE = 3, 36 THERMAL_STATUS_CRITICAL = 4, 37 THERMAL_STATUS_EMERGENCY = 5, 38 THERMAL_STATUS_SHUTDOWN = 6, 39 }; 40 MapToDeviceThermalState(int android_thermal_status)41PowerThermalObserver::DeviceThermalState MapToDeviceThermalState( 42 int android_thermal_status) { 43 switch (android_thermal_status) { 44 case THERMAL_STATUS_NONE: 45 return DeviceThermalState::kNominal; 46 47 case THERMAL_STATUS_LIGHT: 48 case THERMAL_STATUS_MODERATE: 49 return DeviceThermalState::kFair; 50 51 case THERMAL_STATUS_SEVERE: 52 return DeviceThermalState::kSerious; 53 54 case THERMAL_STATUS_CRITICAL: 55 case THERMAL_STATUS_EMERGENCY: 56 case THERMAL_STATUS_SHUTDOWN: 57 return DeviceThermalState::kCritical; 58 59 default: 60 return DeviceThermalState::kUnknown; 61 } 62 } 63 64 } // namespace 65 66 // Native implementation of PowerMonitor.java. Note: This will be invoked by 67 // PowerMonitor.java shortly after startup to set the correct initial value for 68 // "is on battery power." JNI_PowerMonitor_OnBatteryChargingChanged(JNIEnv * env)69void JNI_PowerMonitor_OnBatteryChargingChanged(JNIEnv* env) { 70 ProcessPowerEventHelper(PowerMonitorSource::POWER_STATE_EVENT); 71 } 72 JNI_PowerMonitor_OnThermalStatusChanged(JNIEnv * env,int thermal_status)73void JNI_PowerMonitor_OnThermalStatusChanged(JNIEnv* env, int thermal_status) { 74 ProcessThermalEventHelper(MapToDeviceThermalState(thermal_status)); 75 } 76 77 // Note: Android does not have the concept of suspend / resume as it's known by 78 // other platforms. Thus we do not send Suspend/Resume notifications. See 79 // http://crbug.com/644515 80 81 } // namespace android 82 IsOnBatteryPower()83bool PowerMonitorDeviceSource::IsOnBatteryPower() { 84 JNIEnv* env = base::android::AttachCurrentThread(); 85 return base::android::Java_PowerMonitor_isBatteryPower(env); 86 } 87 GetRemainingBatteryCapacity()88int PowerMonitorDeviceSource::GetRemainingBatteryCapacity() { 89 JNIEnv* env = base::android::AttachCurrentThread(); 90 return base::android::Java_PowerMonitor_getRemainingBatteryCapacity(env); 91 } 92 93 PowerThermalObserver::DeviceThermalState GetCurrentThermalState()94PowerMonitorDeviceSource::GetCurrentThermalState() { 95 JNIEnv* env = base::android::AttachCurrentThread(); 96 return android::MapToDeviceThermalState( 97 android::Java_PowerMonitor_getCurrentThermalStatus(env)); 98 } 99 100 } // namespace base 101