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

Side by Side Diff: services/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java

Issue 2847523002: Android: Remove GetApplicationContext part 4 (Closed)
Patch Set: Rebase and fix build Created 3 years, 7 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
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.device.battery; 5 package org.chromium.device.battery;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.content.BroadcastReceiver; 8 import android.content.BroadcastReceiver;
9 import android.content.Context; 9 import android.content.Context;
10 import android.content.Intent; 10 import android.content.Intent;
11 import android.content.IntentFilter; 11 import android.content.IntentFilter;
12 import android.os.BatteryManager; 12 import android.os.BatteryManager;
13 import android.os.Build; 13 import android.os.Build;
14 14
15 import org.chromium.base.ContextUtils;
15 import org.chromium.base.Log; 16 import org.chromium.base.Log;
16 import org.chromium.base.VisibleForTesting; 17 import org.chromium.base.VisibleForTesting;
17 import org.chromium.device.mojom.BatteryStatus; 18 import org.chromium.device.mojom.BatteryStatus;
18 19
19 import javax.annotation.Nullable; 20 import javax.annotation.Nullable;
20 21
21 /** 22 /**
22 * Data source for battery status information. This class registers for battery status notifications 23 * Data source for battery status information. This class registers for battery status notifications
23 * from the system and calls the callback passed on construction whenever a noti fication is 24 * from the system and calls the callback passed on construction whenever a noti fication is
24 * received. 25 * received.
25 */ 26 */
26 class BatteryStatusManager { 27 class BatteryStatusManager {
27 private static final String TAG = "BatteryStatusManager"; 28 private static final String TAG = "BatteryStatusManager";
28 29
29 interface BatteryStatusCallback { 30 interface BatteryStatusCallback {
30 void onBatteryStatusChanged(BatteryStatus batteryStatus); 31 void onBatteryStatusChanged(BatteryStatus batteryStatus);
31 } 32 }
32 33
33 // A reference to the application context in order to acquire the SensorServ ice.
34 private final Context mAppContext;
35 private final BatteryStatusCallback mCallback; 34 private final BatteryStatusCallback mCallback;
36 private final IntentFilter mFilter = new IntentFilter(Intent.ACTION_BATTERY_ CHANGED); 35 private final IntentFilter mFilter = new IntentFilter(Intent.ACTION_BATTERY_ CHANGED);
37 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 36 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
38 @Override 37 @Override
39 public void onReceive(Context context, Intent intent) { 38 public void onReceive(Context context, Intent intent) {
40 BatteryStatusManager.this.onReceive(intent); 39 BatteryStatusManager.this.onReceive(intent);
41 } 40 }
42 }; 41 };
43 42
44 // This is to workaround a Galaxy Nexus bug, see the comment in the construc tor. 43 // This is to workaround a Galaxy Nexus bug, see the comment in the construc tor.
(...skipping 11 matching lines...) Expand all
56 protected AndroidBatteryManagerWrapper(BatteryManager batteryManager) { 55 protected AndroidBatteryManagerWrapper(BatteryManager batteryManager) {
57 mBatteryManager = batteryManager; 56 mBatteryManager = batteryManager;
58 } 57 }
59 58
60 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 59 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
61 public int getIntProperty(int id) { 60 public int getIntProperty(int id) {
62 return mBatteryManager.getIntProperty(id); 61 return mBatteryManager.getIntProperty(id);
63 } 62 }
64 } 63 }
65 64
66 private BatteryStatusManager(Context context, BatteryStatusCallback callback , 65 private BatteryStatusManager(BatteryStatusCallback callback, boolean ignoreB atteryPresentState,
67 boolean ignoreBatteryPresentState,
68 @Nullable AndroidBatteryManagerWrapper batteryManager) { 66 @Nullable AndroidBatteryManagerWrapper batteryManager) {
69 mAppContext = context.getApplicationContext();
70 mCallback = callback; 67 mCallback = callback;
71 mIgnoreBatteryPresentState = ignoreBatteryPresentState; 68 mIgnoreBatteryPresentState = ignoreBatteryPresentState;
72 mAndroidBatteryManager = batteryManager; 69 mAndroidBatteryManager = batteryManager;
73 } 70 }
74 71
75 BatteryStatusManager(Context context, BatteryStatusCallback callback) { 72 BatteryStatusManager(BatteryStatusCallback callback) {
76 // BatteryManager.EXTRA_PRESENT appears to be unreliable on Galaxy Nexus , 73 // BatteryManager.EXTRA_PRESENT appears to be unreliable on Galaxy Nexus ,
77 // Android 4.2.1, it always reports false. See http://crbug.com/384348. 74 // Android 4.2.1, it always reports false. See http://crbug.com/384348.
78 this(context, callback, Build.MODEL.equals("Galaxy Nexus"), 75 this(callback, Build.MODEL.equals("Galaxy Nexus"),
79 Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP 76 Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
80 ? new AndroidBatteryManagerWrapper( 77 ? new AndroidBatteryManagerWrapper(
81 (BatteryManager) context.getSystemService(Contex t.BATTERY_SERVICE)) 78 (BatteryManager) ContextUtils.getApplicationCo ntext()
79 .getSystemService(Context.BATTERY_SERV ICE))
82 : null); 80 : null);
83 } 81 }
84 82
85 /** 83 /**
86 * Creates a BatteryStatusManager without the Galaxy Nexus workaround for co nsistency in 84 * Creates a BatteryStatusManager without the Galaxy Nexus workaround for co nsistency in
87 * testing. 85 * testing.
88 */ 86 */
89 static BatteryStatusManager createBatteryStatusManagerForTesting(Context con text, 87 static BatteryStatusManager createBatteryStatusManagerForTesting(Context con text,
90 BatteryStatusCallback callback, @Nullable AndroidBatteryManagerWrapp er batteryManager) { 88 BatteryStatusCallback callback, @Nullable AndroidBatteryManagerWrapp er batteryManager) {
91 return new BatteryStatusManager(context, callback, false, batteryManager ); 89 return new BatteryStatusManager(callback, false, batteryManager);
92 } 90 }
93 91
94 /** 92 /**
95 * Starts listening for intents. 93 * Starts listening for intents.
96 * @return True on success. 94 * @return True on success.
97 */ 95 */
98 boolean start() { 96 boolean start() {
99 if (!mEnabled && mAppContext.registerReceiver(mReceiver, mFilter) != nul l) { 97 if (!mEnabled
98 && ContextUtils.getApplicationContext().registerReceiver(mReceiv er, mFilter)
99 != null) {
100 // success 100 // success
101 mEnabled = true; 101 mEnabled = true;
102 } 102 }
103 return mEnabled; 103 return mEnabled;
104 } 104 }
105 105
106 /** 106 /**
107 * Stops listening to intents. 107 * Stops listening to intents.
108 */ 108 */
109 void stop() { 109 void stop() {
110 if (mEnabled) { 110 if (mEnabled) {
111 mAppContext.unregisterReceiver(mReceiver); 111 ContextUtils.getApplicationContext().unregisterReceiver(mReceiver);
112 mEnabled = false; 112 mEnabled = false;
113 } 113 }
114 } 114 }
115 115
116 @VisibleForTesting 116 @VisibleForTesting
117 void onReceive(Intent intent) { 117 void onReceive(Intent intent) {
118 if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { 118 if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
119 Log.e(TAG, "Unexpected intent."); 119 Log.e(TAG, "Unexpected intent.");
120 return; 120 return;
121 } 121 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 } 183 }
184 } else { 184 } else {
185 if (averageCurrentMicroA < 0) { 185 if (averageCurrentMicroA < 0) {
186 double dischargeFromFullHours = batteryCapacityMicroAh / -averag eCurrentMicroA; 186 double dischargeFromFullHours = batteryCapacityMicroAh / -averag eCurrentMicroA;
187 batteryStatus.dischargingTime = 187 batteryStatus.dischargingTime =
188 Math.floor(remainingCapacityRatio * dischargeFromFullHou rs * 3600.0); 188 Math.floor(remainingCapacityRatio * dischargeFromFullHou rs * 3600.0);
189 } 189 }
190 } 190 }
191 } 191 }
192 } 192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698