| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.BroadcastReceiver; | 7 import android.content.BroadcastReceiver; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.IntentFilter; | 10 import android.content.IntentFilter; |
| 11 import android.os.BatteryManager; | 11 import android.os.BatteryManager; |
| 12 import android.os.Build; |
| 12 import android.util.Log; | 13 import android.util.Log; |
| 13 | 14 |
| 14 import com.google.common.annotations.VisibleForTesting; | 15 import com.google.common.annotations.VisibleForTesting; |
| 15 | 16 |
| 16 import org.chromium.base.CalledByNative; | 17 import org.chromium.base.CalledByNative; |
| 17 import org.chromium.base.JNINamespace; | 18 import org.chromium.base.JNINamespace; |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * Android implementation of the battery status APIs. | 21 * Android implementation of the battery status APIs. |
| 21 */ | 22 */ |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 | 84 |
| 84 @VisibleForTesting | 85 @VisibleForTesting |
| 85 void onReceive(Intent intent) { | 86 void onReceive(Intent intent) { |
| 86 if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { | 87 if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 87 Log.e(TAG, "Unexpected intent."); | 88 Log.e(TAG, "Unexpected intent."); |
| 88 return; | 89 return; |
| 89 } | 90 } |
| 90 | 91 |
| 92 boolean present = ignoreBatteryPresentState() ? |
| 93 true : intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false
); |
| 94 |
| 95 if (!present) { |
| 96 // No battery, return default values. |
| 97 gotBatteryStatus(true, 0, Double.POSITIVE_INFINITY, 1); |
| 98 return; |
| 99 } |
| 100 |
| 91 int current = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | 101 int current = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); |
| 92 int max = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); | 102 int max = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); |
| 93 double level = (double)current / (double)max; | 103 double level = (double)current / (double)max; |
| 104 if (level < 0 || level > 1) { |
| 105 // Sanity check, assume default value in this case. |
| 106 level = 1.0; |
| 107 } |
| 94 | 108 |
| 95 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); | 109 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); |
| 96 // by default assume a battery is present | 110 boolean charging = !(status == BatteryManager.BATTERY_STATUS_DISCHARGING)
; |
| 97 boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, tr
ue); | |
| 98 boolean charging = (present && status == BatteryManager.BATTERY_STATUS_DI
SCHARGING) | |
| 99 ? false : true; | |
| 100 | 111 |
| 101 //TODO(timvolodine) : add proper projection for chargingTime, discharging
Time. | 112 // TODO(timvolodine) : add proper projection for chargingTime, dischargin
gTime. |
| 102 double chargingTime = (!present || status == BatteryManager.BATTERY_STATU
S_FULL) | 113 double chargingTime = (status == BatteryManager.BATTERY_STATUS_FULL) ? |
| 103 ? 0 : Double.POSITIVE_INFINITY; | 114 0 : Double.POSITIVE_INFINITY; |
| 104 double dischargingTime = Double.POSITIVE_INFINITY; | 115 double dischargingTime = Double.POSITIVE_INFINITY; |
| 105 | 116 |
| 106 gotBatteryStatus(charging, chargingTime, dischargingTime, level); | 117 gotBatteryStatus(charging, chargingTime, dischargingTime, level); |
| 107 } | 118 } |
| 108 | 119 |
| 120 /** |
| 121 * Returns whether the BatteryStatusManager should ignore the battery presen
t state. |
| 122 * It is required for some devices that incorrectly set the EXTRA_PRESENT pr
operty. |
| 123 */ |
| 124 protected boolean ignoreBatteryPresentState() { |
| 125 // BatteryManager.EXTRA_PRESENT appears to be unreliable on Galaxy Nexus
, |
| 126 // Android 4.2.1, it always reports false. See crbug.com/384348. |
| 127 return Build.MODEL.equals("Galaxy Nexus"); |
| 128 } |
| 129 |
| 109 protected void gotBatteryStatus(boolean charging, double chargingTime, | 130 protected void gotBatteryStatus(boolean charging, double chargingTime, |
| 110 double dischargingTime, double level) { | 131 double dischargingTime, double level) { |
| 111 synchronized (mNativePtrLock) { | 132 synchronized (mNativePtrLock) { |
| 112 if (mNativePtr != 0) { | 133 if (mNativePtr != 0) { |
| 113 nativeGotBatteryStatus(mNativePtr, charging, chargingTime, disch
argingTime, level); | 134 nativeGotBatteryStatus(mNativePtr, charging, chargingTime, disch
argingTime, level); |
| 114 } | 135 } |
| 115 } | 136 } |
| 116 } | 137 } |
| 117 | 138 |
| 118 /** | 139 /** |
| 119 * Native JNI call | 140 * Native JNI call |
| 120 * see content/browser/battery_status/battery_status_manager.cc | 141 * see content/browser/battery_status/battery_status_manager.cc |
| 121 */ | 142 */ |
| 122 private native void nativeGotBatteryStatus(long nativeBatteryStatusManager, | 143 private native void nativeGotBatteryStatus(long nativeBatteryStatusManager, |
| 123 boolean charging, double chargingTime, double dischargingTime, doubl
e level); | 144 boolean charging, double chargingTime, double dischargingTime, doubl
e level); |
| 124 } | 145 } |
| OLD | NEW |