Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/BatteryStatusManager.java

Issue 330333003: [Android] Battery Status API: some tweaks to the BatteryStatusManager.java. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tweaks to battery present detection Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/public/android/javatests/src/org/chromium/content/browser/BatteryStatusManagerTest.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, fa lse)
93 || batteryPresentByDefault();
mlamouri (slow - plz ping) 2014/06/19 18:54:07 What about this: boolean present = ignoreBatteryP
timvolodine 2014/06/20 12:32:43 yes that's slightly different semantics so I've ma
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 protected boolean batteryPresentByDefault() {
mlamouri (slow - plz ping) 2014/06/19 18:54:07 Could you add a small comment explaining at a high
timvolodine 2014/06/20 12:32:43 Done.
121 // BatteryManager.EXTRA_PRESENT appears to be unreliable on Galaxy Nexus ,
122 // Android 4.2.1, it always reports false. See crbug.com/384348.
123 return Build.MODEL.equals("Galaxy Nexus");
124 }
125
109 protected void gotBatteryStatus(boolean charging, double chargingTime, 126 protected void gotBatteryStatus(boolean charging, double chargingTime,
110 double dischargingTime, double level) { 127 double dischargingTime, double level) {
111 synchronized (mNativePtrLock) { 128 synchronized (mNativePtrLock) {
112 if (mNativePtr != 0) { 129 if (mNativePtr != 0) {
113 nativeGotBatteryStatus(mNativePtr, charging, chargingTime, disch argingTime, level); 130 nativeGotBatteryStatus(mNativePtr, charging, chargingTime, disch argingTime, level);
114 } 131 }
115 } 132 }
116 } 133 }
117 134
118 /** 135 /**
119 * Native JNI call 136 * Native JNI call
120 * see content/browser/battery_status/battery_status_manager.cc 137 * see content/browser/battery_status/battery_status_manager.cc
121 */ 138 */
122 private native void nativeGotBatteryStatus(long nativeBatteryStatusManager, 139 private native void nativeGotBatteryStatus(long nativeBatteryStatusManager,
123 boolean charging, double chargingTime, double dischargingTime, doubl e level); 140 boolean charging, double chargingTime, double dischargingTime, doubl e level);
124 } 141 }
OLDNEW
« no previous file with comments | « no previous file | content/public/android/javatests/src/org/chromium/content/browser/BatteryStatusManagerTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698