blob: fe5c2ef3ad06f375e5cdaad66f2948d752627458 [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
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700131 private boolean mBatteryLevelLow;
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 private long mDischargeStartTime;
134 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700136 private boolean mUpdatesStopped;
137
Joe Onoratode1b3592010-10-25 20:36:47 -0700138 private Led mLed;
139
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700140 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800141
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800142 public BatteryService(Context context, LightsManager lightsManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700144 mHandler = new Handler(true /*async*/);
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800145 mLed = new Led(context, lightsManager);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 mBatteryStats = BatteryStatsService.getService();
147
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700148 mCriticalBatteryLevel = mContext.getResources().getInteger(
149 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400150 mLowBatteryWarningLevel = mContext.getResources().getInteger(
151 com.android.internal.R.integer.config_lowBatteryWarningLevel);
152 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
153 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700154 mShutdownBatteryTemperature = mContext.getResources().getInteger(
155 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400156
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400157 // watch for invalid charger messages if the invalid_charger switch exists
158 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700159 mInvalidChargerObserver.startObserving(
160 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Todd Poynor0edc5b52013-10-22 17:53:13 -0700163 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800164 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
165 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700166 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800167 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700168 } 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 /**
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700227 * Returns whether we currently consider the battery level to be low.
Jeff Browna4d82042012-10-02 19:11:19 -0700228 */
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700229 public boolean getBatteryLevelLow() {
Jeff Browna4d82042012-10-02 19:11:19 -0700230 synchronized (mLock) {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700231 return mBatteryLevelLow;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700235 public boolean isBatteryLowLocked() {
236 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
237 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
238
239 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
240 * - is just un-plugged (previously was plugged) and battery level is
241 * less than or equal to WARNING, or
242 * - is not plugged and battery level falls to WARNING boundary
243 * (becomes <= mLowBatteryWarningLevel).
244 */
245 return !plugged
246 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
247 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
248 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
249 }
250
Svetoslav6a08a122013-05-03 11:24:26 -0700251 /**
252 * Returns a non-zero value if an unsupported charger is attached.
253 */
254 public int getInvalidCharger() {
255 synchronized (mLock) {
256 return mInvalidCharger;
257 }
258 }
259
Jeff Browna4d82042012-10-02 19:11:19 -0700260 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400261 // shut down gracefully if our battery is critically low and we are not powered.
262 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700263 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700264 mHandler.post(new Runnable() {
265 @Override
266 public void run() {
267 if (ActivityManagerNative.isSystemReady()) {
268 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
269 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
270 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
271 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
272 }
273 }
274 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400275 }
276 }
277
Jeff Browna4d82042012-10-02 19:11:19 -0700278 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700279 // shut down gracefully if temperature is too high (> 68.0C by default)
280 // wait until the system has booted before attempting to display the
281 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700282 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700283 mHandler.post(new Runnable() {
284 @Override
285 public void run() {
286 if (ActivityManagerNative.isSystemReady()) {
287 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
288 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
289 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
290 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
291 }
292 }
293 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700294 }
295 }
296
Todd Poynor26faecc2013-05-22 18:54:48 -0700297 private void update(BatteryProperties props) {
298 synchronized (mLock) {
299 if (!mUpdatesStopped) {
300 mBatteryProps = props;
301 // Process the new values.
302 processValuesLocked();
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800303 } else {
304 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700305 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700306 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Jeff Browna4d82042012-10-02 19:11:19 -0700309 private void processValuesLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700310 boolean logOutlier = false;
311 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700312
Todd Poynor26faecc2013-05-22 18:54:48 -0700313 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
314 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700316 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700318 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700319 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 } else {
321 mPlugType = BATTERY_PLUGGED_NONE;
322 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700323
Jeff Browna4d82042012-10-02 19:11:19 -0700324 if (DEBUG) {
325 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700326 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
327 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
328 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
329 + ", batteryStatus=" + mBatteryProps.batteryStatus
330 + ", batteryHealth=" + mBatteryProps.batteryHealth
331 + ", batteryPresent=" + mBatteryProps.batteryPresent
332 + ", batteryLevel=" + mBatteryProps.batteryLevel
333 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
334 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
335 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700336 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
337 + ", mPlugType=" + mPlugType);
338 }
339
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700340 // Let the battery stats keep track of the current level.
341 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700342 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
343 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
344 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700345 } catch (RemoteException e) {
346 // Should never happen.
347 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700348
Jeff Browna4d82042012-10-02 19:11:19 -0700349 shutdownIfNoPowerLocked();
350 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700351
Todd Poynor26faecc2013-05-22 18:54:48 -0700352 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
353 mBatteryProps.batteryHealth != mLastBatteryHealth ||
354 mBatteryProps.batteryPresent != mLastBatteryPresent ||
355 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700357 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
358 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400359 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 if (mPlugType != mLastPlugType) {
362 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
363 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 // There's no value in this data unless we've discharged at least once and the
366 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700367 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700368 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
369 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700371 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
376 // charging -> discharging or we just powered up
377 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700378 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
380 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700381 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
382 mBatteryProps.batteryHealth != mLastBatteryHealth ||
383 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700386 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
387 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700389 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700390 // Don't do this just from voltage or temperature changes, that is
391 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700393 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
396 mPlugType == BATTERY_PLUGGED_NONE) {
397 // We want to make sure we log discharge cycle outliers
398 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700399 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
400 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700403 mBatteryLevelLow = isBatteryLowLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
Jeff Browna4d82042012-10-02 19:11:19 -0700405 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
Christopher Tate06ba5542009-04-09 16:03:56 -0700407 // Separate broadcast is sent for power connected / not connected
408 // since the standard intent will not wake any applications and some
409 // applications may want to have smart behavior based on this.
410 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700411 mHandler.post(new Runnable() {
412 @Override
413 public void run() {
414 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
415 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
416 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
417 }
418 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700419 }
420 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700421 mHandler.post(new Runnable() {
422 @Override
423 public void run() {
424 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
425 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
426 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
427 }
428 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700429 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200430
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700431 if (mBatteryLevelLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700432 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700433 mHandler.post(new Runnable() {
434 @Override
435 public void run() {
436 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
437 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
438 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
439 }
440 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400441 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700442 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700443 mHandler.post(new Runnable() {
444 @Override
445 public void run() {
446 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
447 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
448 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
449 }
450 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Joe Onoratode1b3592010-10-25 20:36:47 -0700453 // Update the battery LED
454 mLed.updateLightsLocked();
455
The Android Open Source Project10592532009-03-18 17:39:46 -0700456 // This needs to be done after sendIntent() so that we get the lastest battery stats.
457 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700458 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Todd Poynor26faecc2013-05-22 18:54:48 -0700461 mLastBatteryStatus = mBatteryProps.batteryStatus;
462 mLastBatteryHealth = mBatteryProps.batteryHealth;
463 mLastBatteryPresent = mBatteryProps.batteryPresent;
464 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700465 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700466 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
467 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700468 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400469 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471 }
472
Jeff Browna4d82042012-10-02 19:11:19 -0700473 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700475 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800476 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
477 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800478
Todd Poynor26faecc2013-05-22 18:54:48 -0700479 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480
Todd Poynor26faecc2013-05-22 18:54:48 -0700481 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
482 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
483 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
484 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700485 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
486 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
487 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700488 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
489 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
490 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400491 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
Jeff Browna4d82042012-10-02 19:11:19 -0700493 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700494 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
495 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
496 ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
497 ", voltage: " + mBatteryProps.batteryVoltage +
498 ", temperature: " + mBatteryProps.batteryTemperature +
499 ", technology: " + mBatteryProps.batteryTechnology +
500 ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
501 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Jeff Browna4d82042012-10-02 19:11:19 -0700502 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504
Jeff Brown605ea692012-10-05 16:33:10 -0700505 mHandler.post(new Runnable() {
506 @Override
507 public void run() {
508 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
509 }
510 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512
Jeff Browna4d82042012-10-02 19:11:19 -0700513 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700514 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800515 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
Dan Egnor18e93962010-02-10 19:27:58 -0800517 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
518 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
519
520 File dumpFile = null;
521 FileOutputStream dumpStream = null;
522 try {
523 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700524 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800525 dumpStream = new FileOutputStream(dumpFile);
526 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700527 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800528
529 // add dump file to drop box
530 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
531 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800533 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800534 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800535 } finally {
536 // make sure we clean up
537 if (dumpStream != null) {
538 try {
539 dumpStream.close();
540 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800541 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
Dan Egnor18e93962010-02-10 19:27:58 -0800543 }
544 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800546 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548 }
549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
Jeff Browna4d82042012-10-02 19:11:19 -0700551 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700553 String dischargeThresholdString = Settings.Global.getString(cr,
554 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
555 String durationThresholdString = Settings.Global.getString(cr,
556 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 if (dischargeThresholdString != null && durationThresholdString != null) {
559 try {
560 long durationThreshold = Long.parseLong(durationThresholdString);
561 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800562 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700563 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700565 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
Jeff Browna4d82042012-10-02 19:11:19 -0700567 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700569 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700570 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800572 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 durationThresholdString + " or " + dischargeThresholdString);
574 return;
575 }
576 }
577 }
578
Jeff Browna4d82042012-10-02 19:11:19 -0700579 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700580 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700582 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700584 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
585 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700586 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700587 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800588 return com.android.internal.R.drawable.stat_sys_battery_charge;
589 } else {
590 return com.android.internal.R.drawable.stat_sys_battery;
591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 } else {
593 return com.android.internal.R.drawable.stat_sys_battery_unknown;
594 }
595 }
596
597 @Override
598 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
599 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
600 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 pw.println("Permission Denial: can't dump Battery service from from pid="
603 + Binder.getCallingPid()
604 + ", uid=" + Binder.getCallingUid());
605 return;
606 }
607
Jeff Browna4d82042012-10-02 19:11:19 -0700608 synchronized (mLock) {
609 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700610 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700611 if (mUpdatesStopped) {
612 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
613 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700614 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
615 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
616 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
617 pw.println(" status: " + mBatteryProps.batteryStatus);
618 pw.println(" health: " + mBatteryProps.batteryHealth);
619 pw.println(" present: " + mBatteryProps.batteryPresent);
620 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700621 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700622 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700623 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
624 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700625 } else if (args.length == 3 && "set".equals(args[0])) {
626 String key = args[1];
627 String value = args[2];
628 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800629 if (!mUpdatesStopped) {
630 mLastBatteryProps.set(mBatteryProps);
631 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700632 boolean update = true;
633 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700634 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700635 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700636 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700637 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700638 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700639 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700640 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700641 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700642 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700643 } else if ("invalid".equals(key)) {
644 mInvalidCharger = Integer.parseInt(value);
645 } else {
646 pw.println("Unknown set option: " + key);
647 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700648 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700649 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700650 long ident = Binder.clearCallingIdentity();
651 try {
652 mUpdatesStopped = true;
653 processValuesLocked();
654 } finally {
655 Binder.restoreCallingIdentity(ident);
656 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700657 }
658 } catch (NumberFormatException ex) {
659 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700660 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700661 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700662 long ident = Binder.clearCallingIdentity();
663 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800664 if (mUpdatesStopped) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800665 mUpdatesStopped = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800666 mBatteryProps.set(mLastBatteryProps);
667 processValuesLocked();
668 }
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700669 } finally {
670 Binder.restoreCallingIdentity(ident);
671 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700672 } else {
673 pw.println("Dump current battery state, or:");
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800674 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700675 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700679
Jeff Browna4d82042012-10-02 19:11:19 -0700680 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
681 @Override
682 public void onUEvent(UEventObserver.UEvent event) {
683 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
684 synchronized (mLock) {
685 if (mInvalidCharger != invalidCharger) {
686 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700687 }
688 }
689 }
690 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700691
Jeff Browna4d82042012-10-02 19:11:19 -0700692 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800693 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700694
Jeff Browna4d82042012-10-02 19:11:19 -0700695 private final int mBatteryLowARGB;
696 private final int mBatteryMediumARGB;
697 private final int mBatteryFullARGB;
698 private final int mBatteryLedOn;
699 private final int mBatteryLedOff;
700
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800701 public Led(Context context, LightsManager lights) {
702 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700703
Jeff Browna4d82042012-10-02 19:11:19 -0700704 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700705 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700706 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700707 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700708 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700709 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700710 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700711 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700712 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700713 com.android.internal.R.integer.config_notificationsBatteryLedOff);
714 }
715
716 /**
717 * Synchronize on BatteryService.
718 */
Jeff Browna4d82042012-10-02 19:11:19 -0700719 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700720 final int level = mBatteryProps.batteryLevel;
721 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700722 if (level < mLowBatteryWarningLevel) {
723 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
724 // Solid red when battery is charging
725 mBatteryLight.setColor(mBatteryLowARGB);
726 } else {
727 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800728 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700729 mBatteryLedOn, mBatteryLedOff);
730 }
731 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
732 || status == BatteryManager.BATTERY_STATUS_FULL) {
733 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
734 // Solid green when full or charging and nearly full
735 mBatteryLight.setColor(mBatteryFullARGB);
736 } else {
737 // Solid orange when charging and halfway full
738 mBatteryLight.setColor(mBatteryMediumARGB);
739 }
740 } else {
741 // No lights if not charging and not low
742 mBatteryLight.turnOff();
743 }
744 }
745 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700746
747 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800748 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700749 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800750 final long identity = Binder.clearCallingIdentity();
751 try {
752 BatteryService.this.update(props);
753 } finally {
754 Binder.restoreCallingIdentity(identity);
755 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700756 }
757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758}