blob: 0ebbd8af0f3fe7150e9918d4a0ced7e5aa020624 [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;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080022import com.android.server.lights.Light;
23import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import android.app.ActivityManagerNative;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
30import android.os.BatteryManager;
Todd Poynor26faecc2013-05-22 18:54:48 -070031import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070033import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070034import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070035import android.os.IBatteryPropertiesListener;
36import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080038import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.RemoteException;
40import android.os.ServiceManager;
41import android.os.SystemClock;
42import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070043import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.provider.Settings;
45import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080046import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48import java.io.File;
49import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.io.FileOutputStream;
51import java.io.IOException;
52import java.io.PrintWriter;
53
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55/**
56 * <p>BatteryService monitors the charging status, and charge level of the device
57 * battery. When these values change this service broadcasts the new values
58 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
59 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
60 * BATTERY_CHANGED} action.</p>
61 * <p>The new values are stored in the Intent data and can be retrieved by
62 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
63 * following keys:</p>
64 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
65 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
66 * <p>&quot;status&quot; - String, the current charging status.<br />
67 * <p>&quot;health&quot; - String, the current battery health.<br />
68 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
69 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
70 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
71 * into an AC power adapter; 2 if plugged in via USB.</p>
72 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
73 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
74 * a degree Centigrade</p>
75 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070076 *
77 * <p>
78 * The battery service may be called by the power manager while holding its locks so
79 * we take care to post all outcalls into the activity manager to a handler.
80 *
81 * FIXME: Ideally the power manager would perform all of its calls into the battery
82 * service asynchronously itself.
83 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
Jeff Browna4d82042012-10-02 19:11:19 -070085public final class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080087
Jeff Browna4d82042012-10-02 19:11:19 -070088 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080089
Jeff Browna4d82042012-10-02 19:11:19 -070090 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 // Used locally for determining when to make a last ditch effort to log
93 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070094 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -070097 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -080098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private static final String DUMPSYS_DATA_PATH = "/data/system/";
100
101 // This should probably be exposed in the API, though it's not critical
102 private static final int BATTERY_PLUGGED_NONE = 0;
103
104 private final Context mContext;
105 private final IBatteryStats mBatteryStats;
Jeff Brown605ea692012-10-05 16:33:10 -0700106 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800107
Jeff Browna4d82042012-10-02 19:11:19 -0700108 private final Object mLock = new Object();
109
Todd Poynor26faecc2013-05-22 18:54:48 -0700110 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800111 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 private int mLastBatteryStatus;
114 private int mLastBatteryHealth;
115 private boolean mLastBatteryPresent;
116 private int mLastBatteryLevel;
117 private int mLastBatteryVoltage;
118 private int mLastBatteryTemperature;
119 private boolean mLastBatteryLevelCritical;
Jeff Browna4d82042012-10-02 19:11:19 -0700120
121 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700122 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400123
124 private int mLowBatteryWarningLevel;
125 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700126 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 private int mPlugType;
129 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 private long mDischargeStartTime;
132 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700134 private boolean mUpdatesStopped;
135
Joe Onoratode1b3592010-10-25 20:36:47 -0700136 private Led mLed;
137
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700138 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800140 public BatteryService(Context context, LightsManager lightsManager) {
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*/);
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800143 mLed = new Led(context, lightsManager);
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 Poynor0edc5b52013-10-22 17:53:13 -0700161 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800162 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
163 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700164 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800165 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700166 } catch (RemoteException e) {
167 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170
Jeff Browna4d82042012-10-02 19:11:19 -0700171 void systemReady() {
172 // check our power situation now that it is safe to display the shutdown dialog.
173 synchronized (mLock) {
174 shutdownIfNoPowerLocked();
175 shutdownIfOverTempLocked();
176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
178
Jeff Browna4d82042012-10-02 19:11:19 -0700179 /**
180 * Returns true if the device is plugged into any of the specified plug types.
181 */
182 public boolean isPowered(int plugTypeSet) {
183 synchronized (mLock) {
184 return isPoweredLocked(plugTypeSet);
185 }
186 }
187
188 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 // assume we are powered if battery state is unknown so
190 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700191 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return true;
193 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700194 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700195 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700197 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700198 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700200 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700201 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
Jeff Browna4d82042012-10-02 19:11:19 -0700203 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205
Jeff Browna4d82042012-10-02 19:11:19 -0700206 /**
Jeff Brownf3fb8952012-10-02 20:57:05 -0700207 * Returns the current plug type.
208 */
209 public int getPlugType() {
210 synchronized (mLock) {
211 return mPlugType;
212 }
213 }
214
215 /**
Jeff Browna4d82042012-10-02 19:11:19 -0700216 * Returns battery level as a percentage.
217 */
218 public int getBatteryLevel() {
219 synchronized (mLock) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700220 return mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
Jeff Browna4d82042012-10-02 19:11:19 -0700222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
Jeff Browna4d82042012-10-02 19:11:19 -0700224 /**
225 * Returns true if battery level is below the first warning threshold.
226 */
227 public boolean isBatteryLow() {
228 synchronized (mLock) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700229 return mBatteryProps.batteryPresent && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232
Svetoslav6a08a122013-05-03 11:24:26 -0700233 /**
234 * Returns a non-zero value if an unsupported charger is attached.
235 */
236 public int getInvalidCharger() {
237 synchronized (mLock) {
238 return mInvalidCharger;
239 }
240 }
241
Jeff Browna4d82042012-10-02 19:11:19 -0700242 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400243 // shut down gracefully if our battery is critically low and we are not powered.
244 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700245 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700246 mHandler.post(new Runnable() {
247 @Override
248 public void run() {
249 if (ActivityManagerNative.isSystemReady()) {
250 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
251 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
252 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
253 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
254 }
255 }
256 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400257 }
258 }
259
Jeff Browna4d82042012-10-02 19:11:19 -0700260 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700261 // shut down gracefully if temperature is too high (> 68.0C by default)
262 // wait until the system has booted before attempting to display the
263 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700264 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700265 mHandler.post(new Runnable() {
266 @Override
267 public void run() {
268 if (ActivityManagerNative.isSystemReady()) {
269 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
270 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
271 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
272 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
273 }
274 }
275 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700276 }
277 }
278
Todd Poynor26faecc2013-05-22 18:54:48 -0700279 private void update(BatteryProperties props) {
280 synchronized (mLock) {
281 if (!mUpdatesStopped) {
282 mBatteryProps = props;
283 // Process the new values.
284 processValuesLocked();
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800285 } else {
286 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700287 }
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
317 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700318 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
319 + ", mPlugType=" + mPlugType);
320 }
321
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700322 // Let the battery stats keep track of the current level.
323 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700324 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
325 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
326 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700327 } catch (RemoteException e) {
328 // Should never happen.
329 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700330
Jeff Browna4d82042012-10-02 19:11:19 -0700331 shutdownIfNoPowerLocked();
332 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700333
Todd Poynor26faecc2013-05-22 18:54:48 -0700334 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
335 mBatteryProps.batteryHealth != mLastBatteryHealth ||
336 mBatteryProps.batteryPresent != mLastBatteryPresent ||
337 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700339 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
340 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400341 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 if (mPlugType != mLastPlugType) {
344 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
345 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 // There's no value in this data unless we've discharged at least once and the
348 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700349 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700350 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
351 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700353 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
357 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
358 // charging -> discharging or we just powered up
359 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700360 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700363 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
364 mBatteryProps.batteryHealth != mLastBatteryHealth ||
365 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700368 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
369 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700371 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700372 // Don't do this just from voltage or temperature changes, that is
373 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700375 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
378 mPlugType == BATTERY_PLUGGED_NONE) {
379 // We want to make sure we log discharge cycle outliers
380 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700381 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
382 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700385 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
386 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
387
388 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400389 * - is just un-plugged (previously was plugged) and battery level is
390 * less than or equal to WARNING, or
391 * - is not plugged and battery level falls to WARNING boundary
392 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700393 */
394 final boolean sendBatteryLow = !plugged
Todd Poynor26faecc2013-05-22 18:54:48 -0700395 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
396 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
Joe Onoratode1b3592010-10-25 20:36:47 -0700397 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
Jeff Browna4d82042012-10-02 19:11:19 -0700399 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
Christopher Tate06ba5542009-04-09 16:03:56 -0700401 // Separate broadcast is sent for power connected / not connected
402 // since the standard intent will not wake any applications and some
403 // applications may want to have smart behavior based on this.
404 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700405 mHandler.post(new Runnable() {
406 @Override
407 public void run() {
408 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
409 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
410 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
411 }
412 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700413 }
414 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700415 mHandler.post(new Runnable() {
416 @Override
417 public void run() {
418 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
419 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
420 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
421 }
422 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700423 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200424
Mihai Predaa82842f2009-04-29 15:05:56 +0200425 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700426 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700427 mHandler.post(new Runnable() {
428 @Override
429 public void run() {
430 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
431 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
432 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
433 }
434 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400435 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700436 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700437 mHandler.post(new Runnable() {
438 @Override
439 public void run() {
440 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
441 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
442 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
443 }
444 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Joe Onoratode1b3592010-10-25 20:36:47 -0700447 // Update the battery LED
448 mLed.updateLightsLocked();
449
The Android Open Source Project10592532009-03-18 17:39:46 -0700450 // This needs to be done after sendIntent() so that we get the lastest battery stats.
451 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700452 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
Todd Poynor26faecc2013-05-22 18:54:48 -0700455 mLastBatteryStatus = mBatteryProps.batteryStatus;
456 mLastBatteryHealth = mBatteryProps.batteryHealth;
457 mLastBatteryPresent = mBatteryProps.batteryPresent;
458 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700459 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700460 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
461 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700462 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400463 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465 }
466
Jeff Browna4d82042012-10-02 19:11:19 -0700467 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700469 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800470 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
471 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
Todd Poynor26faecc2013-05-22 18:54:48 -0700473 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474
Todd Poynor26faecc2013-05-22 18:54:48 -0700475 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
476 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
477 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
478 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700479 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
480 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
481 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700482 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
483 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
484 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400485 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486
Jeff Browna4d82042012-10-02 19:11:19 -0700487 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700488 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
489 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
490 ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
491 ", voltage: " + mBatteryProps.batteryVoltage +
492 ", temperature: " + mBatteryProps.batteryTemperature +
493 ", technology: " + mBatteryProps.batteryTechnology +
494 ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
495 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Jeff Browna4d82042012-10-02 19:11:19 -0700496 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498
Jeff Brown605ea692012-10-05 16:33:10 -0700499 mHandler.post(new Runnable() {
500 @Override
501 public void run() {
502 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
503 }
504 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
506
Jeff Browna4d82042012-10-02 19:11:19 -0700507 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700508 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800509 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
Dan Egnor18e93962010-02-10 19:27:58 -0800511 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
512 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
513
514 File dumpFile = null;
515 FileOutputStream dumpStream = null;
516 try {
517 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700518 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800519 dumpStream = new FileOutputStream(dumpFile);
520 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700521 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800522
523 // add dump file to drop box
524 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
525 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800526 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800527 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800529 } finally {
530 // make sure we clean up
531 if (dumpStream != null) {
532 try {
533 dumpStream.close();
534 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
Dan Egnor18e93962010-02-10 19:27:58 -0800537 }
538 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800539 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800540 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542 }
543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
Jeff Browna4d82042012-10-02 19:11:19 -0700545 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700547 String dischargeThresholdString = Settings.Global.getString(cr,
548 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
549 String durationThresholdString = Settings.Global.getString(cr,
550 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 if (dischargeThresholdString != null && durationThresholdString != null) {
553 try {
554 long durationThreshold = Long.parseLong(durationThresholdString);
555 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700557 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700559 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Jeff Browna4d82042012-10-02 19:11:19 -0700561 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700563 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700564 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800566 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 durationThresholdString + " or " + dischargeThresholdString);
568 return;
569 }
570 }
571 }
572
Jeff Browna4d82042012-10-02 19:11:19 -0700573 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700574 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700576 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700578 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
579 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700580 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700581 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800582 return com.android.internal.R.drawable.stat_sys_battery_charge;
583 } else {
584 return com.android.internal.R.drawable.stat_sys_battery;
585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 } else {
587 return com.android.internal.R.drawable.stat_sys_battery_unknown;
588 }
589 }
590
591 @Override
592 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
593 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
594 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 pw.println("Permission Denial: can't dump Battery service from from pid="
597 + Binder.getCallingPid()
598 + ", uid=" + Binder.getCallingUid());
599 return;
600 }
601
Jeff Browna4d82042012-10-02 19:11:19 -0700602 synchronized (mLock) {
603 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700604 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700605 if (mUpdatesStopped) {
606 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
607 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700608 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
609 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
610 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
611 pw.println(" status: " + mBatteryProps.batteryStatus);
612 pw.println(" health: " + mBatteryProps.batteryHealth);
613 pw.println(" present: " + mBatteryProps.batteryPresent);
614 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700615 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700616 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700617 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
618 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700619 } else if (args.length == 3 && "set".equals(args[0])) {
620 String key = args[1];
621 String value = args[2];
622 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800623 if (!mUpdatesStopped) {
624 mLastBatteryProps.set(mBatteryProps);
625 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700626 boolean update = true;
627 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700628 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700629 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700630 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700631 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700632 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700633 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700634 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700635 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700636 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700637 } else if ("invalid".equals(key)) {
638 mInvalidCharger = Integer.parseInt(value);
639 } else {
640 pw.println("Unknown set option: " + key);
641 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700642 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700643 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700644 long ident = Binder.clearCallingIdentity();
645 try {
646 mUpdatesStopped = true;
647 processValuesLocked();
648 } finally {
649 Binder.restoreCallingIdentity(ident);
650 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700651 }
652 } catch (NumberFormatException ex) {
653 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700654 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700655 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700656 long ident = Binder.clearCallingIdentity();
657 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800658 if (mUpdatesStopped) {
659 mBatteryProps.set(mLastBatteryProps);
660 processValuesLocked();
661 }
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700662 } finally {
663 Binder.restoreCallingIdentity(ident);
664 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700665 } else {
666 pw.println("Dump current battery state, or:");
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800667 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700668 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700672
Jeff Browna4d82042012-10-02 19:11:19 -0700673 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
674 @Override
675 public void onUEvent(UEventObserver.UEvent event) {
676 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
677 synchronized (mLock) {
678 if (mInvalidCharger != invalidCharger) {
679 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700680 }
681 }
682 }
683 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700684
Jeff Browna4d82042012-10-02 19:11:19 -0700685 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800686 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700687
Jeff Browna4d82042012-10-02 19:11:19 -0700688 private final int mBatteryLowARGB;
689 private final int mBatteryMediumARGB;
690 private final int mBatteryFullARGB;
691 private final int mBatteryLedOn;
692 private final int mBatteryLedOff;
693
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800694 public Led(Context context, LightsManager lights) {
695 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700696
Jeff Browna4d82042012-10-02 19:11:19 -0700697 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700698 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700699 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700700 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700701 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700702 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700703 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700704 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700705 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700706 com.android.internal.R.integer.config_notificationsBatteryLedOff);
707 }
708
709 /**
710 * Synchronize on BatteryService.
711 */
Jeff Browna4d82042012-10-02 19:11:19 -0700712 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700713 final int level = mBatteryProps.batteryLevel;
714 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700715 if (level < mLowBatteryWarningLevel) {
716 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
717 // Solid red when battery is charging
718 mBatteryLight.setColor(mBatteryLowARGB);
719 } else {
720 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800721 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700722 mBatteryLedOn, mBatteryLedOff);
723 }
724 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
725 || status == BatteryManager.BATTERY_STATUS_FULL) {
726 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
727 // Solid green when full or charging and nearly full
728 mBatteryLight.setColor(mBatteryFullARGB);
729 } else {
730 // Solid orange when charging and halfway full
731 mBatteryLight.setColor(mBatteryMediumARGB);
732 }
733 } else {
734 // No lights if not charging and not low
735 mBatteryLight.turnOff();
736 }
737 }
738 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700739
740 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800741 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700742 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800743 final long identity = Binder.clearCallingIdentity();
744 try {
745 BatteryService.this.update(props);
746 } finally {
747 Binder.restoreCallingIdentity(identity);
748 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700749 }
750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751}