blob: 5f3f894ca68e6d8f1cae7305aab8e7d54f62ecbd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn8c841092013-06-24 13:46:13 -070019import android.os.BatteryStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.app.IBatteryStats;
21import com.android.server.am.BatteryStatsService;
22
23import android.app.ActivityManagerNative;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.os.BatteryManager;
Todd Poynor26faecc2013-05-22 18:54:48 -070029import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070031import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070032import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070033import android.os.IBatteryPropertiesListener;
34import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080036import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.os.SystemClock;
40import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070041import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.provider.Settings;
43import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080044import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46import java.io.File;
47import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.io.FileOutputStream;
49import java.io.IOException;
50import java.io.PrintWriter;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53/**
54 * <p>BatteryService monitors the charging status, and charge level of the device
55 * battery. When these values change this service broadcasts the new values
56 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
57 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
58 * BATTERY_CHANGED} action.</p>
59 * <p>The new values are stored in the Intent data and can be retrieved by
60 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
61 * following keys:</p>
62 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
63 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
64 * <p>&quot;status&quot; - String, the current charging status.<br />
65 * <p>&quot;health&quot; - String, the current battery health.<br />
66 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
67 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
68 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
69 * into an AC power adapter; 2 if plugged in via USB.</p>
70 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
71 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
72 * a degree Centigrade</p>
73 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070074 *
75 * <p>
76 * The battery service may be called by the power manager while holding its locks so
77 * we take care to post all outcalls into the activity manager to a handler.
78 *
79 * FIXME: Ideally the power manager would perform all of its calls into the battery
80 * service asynchronously itself.
81 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 */
Jeff Browna4d82042012-10-02 19:11:19 -070083public final class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080085
Jeff Browna4d82042012-10-02 19:11:19 -070086 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080087
Jeff Browna4d82042012-10-02 19:11:19 -070088 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 // Used locally for determining when to make a last ditch effort to log
91 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070092 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -070095 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -080096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static final String DUMPSYS_DATA_PATH = "/data/system/";
98
99 // This should probably be exposed in the API, though it's not critical
100 private static final int BATTERY_PLUGGED_NONE = 0;
101
102 private final Context mContext;
103 private final IBatteryStats mBatteryStats;
Jeff Brown605ea692012-10-05 16:33:10 -0700104 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800105
Jeff Browna4d82042012-10-02 19:11:19 -0700106 private final Object mLock = new Object();
107
Todd Poynor26faecc2013-05-22 18:54:48 -0700108 private BatteryProperties mBatteryProps;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 private int mLastBatteryStatus;
111 private int mLastBatteryHealth;
112 private boolean mLastBatteryPresent;
113 private int mLastBatteryLevel;
114 private int mLastBatteryVoltage;
115 private int mLastBatteryTemperature;
116 private boolean mLastBatteryLevelCritical;
Jeff Browna4d82042012-10-02 19:11:19 -0700117
118 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700119 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400120
121 private int mLowBatteryWarningLevel;
122 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700123 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 private int mPlugType;
126 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 private long mDischargeStartTime;
129 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700131 private boolean mUpdatesStopped;
132
Joe Onoratode1b3592010-10-25 20:36:47 -0700133 private Led mLed;
134
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700135 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
Todd Poynor26faecc2013-05-22 18:54:48 -0700137 private BatteryListener mBatteryPropertiesListener;
138 private IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
Jeff Browna4d82042012-10-02 19:11:19 -0700139
Joe Onoratode1b3592010-10-25 20:36:47 -0700140 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700142 mHandler = new Handler(true /*async*/);
Joe Onoratode1b3592010-10-25 20:36:47 -0700143 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 mBatteryStats = BatteryStatsService.getService();
145
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700146 mCriticalBatteryLevel = mContext.getResources().getInteger(
147 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400148 mLowBatteryWarningLevel = mContext.getResources().getInteger(
149 com.android.internal.R.integer.config_lowBatteryWarningLevel);
150 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
151 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700152 mShutdownBatteryTemperature = mContext.getResources().getInteger(
153 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400154
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400155 // watch for invalid charger messages if the invalid_charger switch exists
156 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700157 mInvalidChargerObserver.startObserving(
158 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Todd Poynor26faecc2013-05-22 18:54:48 -0700161 mBatteryPropertiesListener = new BatteryListener();
162
163 IBinder b = ServiceManager.getService("batterypropreg");
164 mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(b);
165
166 try {
167 mBatteryPropertiesRegistrar.registerListener(mBatteryPropertiesListener);
168 } catch (RemoteException e) {
169 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 }
172
Jeff Browna4d82042012-10-02 19:11:19 -0700173 void systemReady() {
174 // check our power situation now that it is safe to display the shutdown dialog.
175 synchronized (mLock) {
176 shutdownIfNoPowerLocked();
177 shutdownIfOverTempLocked();
178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180
Jeff Browna4d82042012-10-02 19:11:19 -0700181 /**
182 * Returns true if the device is plugged into any of the specified plug types.
183 */
184 public boolean isPowered(int plugTypeSet) {
185 synchronized (mLock) {
186 return isPoweredLocked(plugTypeSet);
187 }
188 }
189
190 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 // assume we are powered if battery state is unknown so
192 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700193 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 return true;
195 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700196 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700197 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700199 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700200 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700202 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700203 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
Jeff Browna4d82042012-10-02 19:11:19 -0700205 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
207
Jeff Browna4d82042012-10-02 19:11:19 -0700208 /**
Jeff Brownf3fb8952012-10-02 20:57:05 -0700209 * Returns the current plug type.
210 */
211 public int getPlugType() {
212 synchronized (mLock) {
213 return mPlugType;
214 }
215 }
216
217 /**
Jeff Browna4d82042012-10-02 19:11:19 -0700218 * Returns battery level as a percentage.
219 */
220 public int getBatteryLevel() {
221 synchronized (mLock) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700222 return mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
Jeff Browna4d82042012-10-02 19:11:19 -0700224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Jeff Browna4d82042012-10-02 19:11:19 -0700226 /**
227 * Returns true if battery level is below the first warning threshold.
228 */
229 public boolean isBatteryLow() {
230 synchronized (mLock) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700231 return mBatteryProps.batteryPresent && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234
Svetoslav6a08a122013-05-03 11:24:26 -0700235 /**
236 * Returns a non-zero value if an unsupported charger is attached.
237 */
238 public int getInvalidCharger() {
239 synchronized (mLock) {
240 return mInvalidCharger;
241 }
242 }
243
Jeff Browna4d82042012-10-02 19:11:19 -0700244 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400245 // shut down gracefully if our battery is critically low and we are not powered.
246 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700247 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700248 mHandler.post(new Runnable() {
249 @Override
250 public void run() {
251 if (ActivityManagerNative.isSystemReady()) {
252 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
253 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
254 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
255 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
256 }
257 }
258 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400259 }
260 }
261
Jeff Browna4d82042012-10-02 19:11:19 -0700262 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700263 // shut down gracefully if temperature is too high (> 68.0C by default)
264 // wait until the system has booted before attempting to display the
265 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700266 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700267 mHandler.post(new Runnable() {
268 @Override
269 public void run() {
270 if (ActivityManagerNative.isSystemReady()) {
271 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
272 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
273 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
274 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
275 }
276 }
277 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700278 }
279 }
280
Todd Poynor26faecc2013-05-22 18:54:48 -0700281 private void update(BatteryProperties props) {
282 synchronized (mLock) {
283 if (!mUpdatesStopped) {
284 mBatteryProps = props;
285 // Process the new values.
286 processValuesLocked();
287 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700288 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
Jeff Browna4d82042012-10-02 19:11:19 -0700291 private void processValuesLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700292 boolean logOutlier = false;
293 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700294
Todd Poynor26faecc2013-05-22 18:54:48 -0700295 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
296 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700298 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700300 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700301 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 } else {
303 mPlugType = BATTERY_PLUGGED_NONE;
304 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700305
Jeff Browna4d82042012-10-02 19:11:19 -0700306 if (DEBUG) {
307 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700308 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
309 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
310 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
311 + ", batteryStatus=" + mBatteryProps.batteryStatus
312 + ", batteryHealth=" + mBatteryProps.batteryHealth
313 + ", batteryPresent=" + mBatteryProps.batteryPresent
314 + ", batteryLevel=" + mBatteryProps.batteryLevel
315 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
316 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
Todd Poynordf89ca32013-07-30 20:33:27 -0700317 + ", batteryCurrentNow=" + mBatteryProps.batteryCurrentNow
318 + ", batteryChargeCounter=" + mBatteryProps.batteryChargeCounter
Todd Poynor26faecc2013-05-22 18:54:48 -0700319 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700320 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
321 + ", mPlugType=" + mPlugType);
322 }
323
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700324 // Let the battery stats keep track of the current level.
325 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700326 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
327 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
328 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700329 } catch (RemoteException e) {
330 // Should never happen.
331 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700332
Jeff Browna4d82042012-10-02 19:11:19 -0700333 shutdownIfNoPowerLocked();
334 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700335
Todd Poynor26faecc2013-05-22 18:54:48 -0700336 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
337 mBatteryProps.batteryHealth != mLastBatteryHealth ||
338 mBatteryProps.batteryPresent != mLastBatteryPresent ||
339 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700341 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
342 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400343 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 if (mPlugType != mLastPlugType) {
346 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
347 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 // There's no value in this data unless we've discharged at least once and the
350 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700351 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700352 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
353 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700355 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
360 // charging -> discharging or we just powered up
361 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700362 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700365 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
366 mBatteryProps.batteryHealth != mLastBatteryHealth ||
367 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700370 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
371 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700373 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700374 // Don't do this just from voltage or temperature changes, that is
375 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700377 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
380 mPlugType == BATTERY_PLUGGED_NONE) {
381 // We want to make sure we log discharge cycle outliers
382 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700383 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
384 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700387 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
388 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
389
390 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400391 * - is just un-plugged (previously was plugged) and battery level is
392 * less than or equal to WARNING, or
393 * - is not plugged and battery level falls to WARNING boundary
394 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700395 */
396 final boolean sendBatteryLow = !plugged
Todd Poynor26faecc2013-05-22 18:54:48 -0700397 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
398 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
Joe Onoratode1b3592010-10-25 20:36:47 -0700399 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
Jeff Browna4d82042012-10-02 19:11:19 -0700401 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
Christopher Tate06ba5542009-04-09 16:03:56 -0700403 // Separate broadcast is sent for power connected / not connected
404 // since the standard intent will not wake any applications and some
405 // applications may want to have smart behavior based on this.
406 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700407 mHandler.post(new Runnable() {
408 @Override
409 public void run() {
410 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
411 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
412 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
413 }
414 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700415 }
416 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700417 mHandler.post(new Runnable() {
418 @Override
419 public void run() {
420 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
421 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
422 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
423 }
424 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700425 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200426
Mihai Predaa82842f2009-04-29 15:05:56 +0200427 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700428 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700429 mHandler.post(new Runnable() {
430 @Override
431 public void run() {
432 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
433 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
434 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
435 }
436 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400437 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700438 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700439 mHandler.post(new Runnable() {
440 @Override
441 public void run() {
442 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
443 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
444 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
445 }
446 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Joe Onoratode1b3592010-10-25 20:36:47 -0700449 // Update the battery LED
450 mLed.updateLightsLocked();
451
The Android Open Source Project10592532009-03-18 17:39:46 -0700452 // This needs to be done after sendIntent() so that we get the lastest battery stats.
453 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700454 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Todd Poynor26faecc2013-05-22 18:54:48 -0700457 mLastBatteryStatus = mBatteryProps.batteryStatus;
458 mLastBatteryHealth = mBatteryProps.batteryHealth;
459 mLastBatteryPresent = mBatteryProps.batteryPresent;
460 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700461 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700462 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
463 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700464 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400465 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468
Jeff Browna4d82042012-10-02 19:11:19 -0700469 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700471 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800472 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
473 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
Todd Poynor26faecc2013-05-22 18:54:48 -0700475 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Todd Poynor26faecc2013-05-22 18:54:48 -0700477 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
478 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
479 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
480 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700481 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
482 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
483 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700484 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
485 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
486 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400487 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
Jeff Browna4d82042012-10-02 19:11:19 -0700489 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700490 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
491 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
492 ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
493 ", voltage: " + mBatteryProps.batteryVoltage +
494 ", temperature: " + mBatteryProps.batteryTemperature +
495 ", technology: " + mBatteryProps.batteryTechnology +
496 ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
497 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Jeff Browna4d82042012-10-02 19:11:19 -0700498 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
Jeff Brown605ea692012-10-05 16:33:10 -0700501 mHandler.post(new Runnable() {
502 @Override
503 public void run() {
504 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
505 }
506 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508
Jeff Browna4d82042012-10-02 19:11:19 -0700509 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700510 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800511 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
Dan Egnor18e93962010-02-10 19:27:58 -0800513 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
514 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
515
516 File dumpFile = null;
517 FileOutputStream dumpStream = null;
518 try {
519 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700520 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800521 dumpStream = new FileOutputStream(dumpFile);
522 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700523 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800524
525 // add dump file to drop box
526 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
527 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800529 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800531 } finally {
532 // make sure we clean up
533 if (dumpStream != null) {
534 try {
535 dumpStream.close();
536 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
Dan Egnor18e93962010-02-10 19:27:58 -0800539 }
540 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800541 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800542 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 }
545 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
Jeff Browna4d82042012-10-02 19:11:19 -0700547 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700549 String dischargeThresholdString = Settings.Global.getString(cr,
550 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
551 String durationThresholdString = Settings.Global.getString(cr,
552 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 if (dischargeThresholdString != null && durationThresholdString != null) {
555 try {
556 long durationThreshold = Long.parseLong(durationThresholdString);
557 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800558 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700559 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700561 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
Jeff Browna4d82042012-10-02 19:11:19 -0700563 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700565 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700566 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800568 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 durationThresholdString + " or " + dischargeThresholdString);
570 return;
571 }
572 }
573 }
574
Jeff Browna4d82042012-10-02 19:11:19 -0700575 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700576 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700578 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700580 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
581 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700582 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700583 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800584 return com.android.internal.R.drawable.stat_sys_battery_charge;
585 } else {
586 return com.android.internal.R.drawable.stat_sys_battery;
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 } else {
589 return com.android.internal.R.drawable.stat_sys_battery_unknown;
590 }
591 }
592
593 @Override
594 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
595 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
596 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 pw.println("Permission Denial: can't dump Battery service from from pid="
599 + Binder.getCallingPid()
600 + ", uid=" + Binder.getCallingUid());
601 return;
602 }
603
Jeff Browna4d82042012-10-02 19:11:19 -0700604 synchronized (mLock) {
605 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700606 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700607 if (mUpdatesStopped) {
608 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
609 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700610 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
611 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
612 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
613 pw.println(" status: " + mBatteryProps.batteryStatus);
614 pw.println(" health: " + mBatteryProps.batteryHealth);
615 pw.println(" present: " + mBatteryProps.batteryPresent);
616 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700617 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700618 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
619
620 if (mBatteryProps.batteryCurrentNow != Integer.MIN_VALUE) {
621 pw.println(" current now: " + mBatteryProps.batteryCurrentNow);
622 }
623
624 if (mBatteryProps.batteryChargeCounter != Integer.MIN_VALUE) {
625 pw.println(" charge counter: " + mBatteryProps.batteryChargeCounter);
626 }
627
Todd Poynor26faecc2013-05-22 18:54:48 -0700628 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
629 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700630 } else if (args.length == 3 && "set".equals(args[0])) {
631 String key = args[1];
632 String value = args[2];
633 try {
634 boolean update = true;
635 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700636 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700637 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700638 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700639 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700640 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700641 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700642 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700643 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700644 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700645 } else if ("invalid".equals(key)) {
646 mInvalidCharger = Integer.parseInt(value);
647 } else {
648 pw.println("Unknown set option: " + key);
649 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700650 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700651 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700652 long ident = Binder.clearCallingIdentity();
653 try {
654 mUpdatesStopped = true;
655 processValuesLocked();
656 } finally {
657 Binder.restoreCallingIdentity(ident);
658 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700659 }
660 } catch (NumberFormatException ex) {
661 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700662 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700663 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700664 long ident = Binder.clearCallingIdentity();
665 try {
666 mUpdatesStopped = false;
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700667 } finally {
668 Binder.restoreCallingIdentity(ident);
669 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700670 } else {
671 pw.println("Dump current battery state, or:");
672 pw.println(" set ac|usb|wireless|status|level|invalid <value>");
673 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700677
Jeff Browna4d82042012-10-02 19:11:19 -0700678 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
679 @Override
680 public void onUEvent(UEventObserver.UEvent event) {
681 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
682 synchronized (mLock) {
683 if (mInvalidCharger != invalidCharger) {
684 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700685 }
686 }
687 }
688 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700689
Jeff Browna4d82042012-10-02 19:11:19 -0700690 private final class Led {
691 private final LightsService.Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700692
Jeff Browna4d82042012-10-02 19:11:19 -0700693 private final int mBatteryLowARGB;
694 private final int mBatteryMediumARGB;
695 private final int mBatteryFullARGB;
696 private final int mBatteryLedOn;
697 private final int mBatteryLedOff;
698
699 public Led(Context context, LightsService lights) {
Joe Onoratode1b3592010-10-25 20:36:47 -0700700 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
701
Jeff Browna4d82042012-10-02 19:11:19 -0700702 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700703 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700704 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700705 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700706 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700707 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700708 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700709 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700710 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700711 com.android.internal.R.integer.config_notificationsBatteryLedOff);
712 }
713
714 /**
715 * Synchronize on BatteryService.
716 */
Jeff Browna4d82042012-10-02 19:11:19 -0700717 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700718 final int level = mBatteryProps.batteryLevel;
719 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700720 if (level < mLowBatteryWarningLevel) {
721 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
722 // Solid red when battery is charging
723 mBatteryLight.setColor(mBatteryLowARGB);
724 } else {
725 // Flash red when battery is low and not charging
726 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
727 mBatteryLedOn, mBatteryLedOff);
728 }
729 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
730 || status == BatteryManager.BATTERY_STATUS_FULL) {
731 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
732 // Solid green when full or charging and nearly full
733 mBatteryLight.setColor(mBatteryFullARGB);
734 } else {
735 // Solid orange when charging and halfway full
736 mBatteryLight.setColor(mBatteryMediumARGB);
737 }
738 } else {
739 // No lights if not charging and not low
740 mBatteryLight.turnOff();
741 }
742 }
743 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700744
745 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
746 public void batteryPropertiesChanged(BatteryProperties props) {
747 BatteryService.this.update(props);
748 }
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750}