power_monitor_device_source_android.cc 3.2 KB

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