blob: cc9055dd480e5d089f9edce2b76fa91ed13eb601 [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 Lesinski182f73f2013-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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 private int mLastBatteryStatus;
113 private int mLastBatteryHealth;
114 private boolean mLastBatteryPresent;
115 private int mLastBatteryLevel;
116 private int mLastBatteryVoltage;
117 private int mLastBatteryTemperature;
118 private boolean mLastBatteryLevelCritical;
Jeff Browna4d82042012-10-02 19:11:19 -0700119
120 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700121 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400122
123 private int mLowBatteryWarningLevel;
124 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700125 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 private int mPlugType;
128 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 private long mDischargeStartTime;
131 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700133 private boolean mUpdatesStopped;
134
Joe Onoratode1b3592010-10-25 20:36:47 -0700135 private Led mLed;
136
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700137 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
Adam Lesinski182f73f2013-12-05 16:48:06 -0800139 public BatteryService(Context context, LightsManager lightsManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700141 mHandler = new Handler(true /*async*/);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800142 mLed = new Led(context, lightsManager);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 mBatteryStats = BatteryStatsService.getService();
144
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700145 mCriticalBatteryLevel = mContext.getResources().getInteger(
146 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400147 mLowBatteryWarningLevel = mContext.getResources().getInteger(
148 com.android.internal.R.integer.config_lowBatteryWarningLevel);
149 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
150 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700151 mShutdownBatteryTemperature = mContext.getResources().getInteger(
152 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400153
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400154 // watch for invalid charger messages if the invalid_charger switch exists
155 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700156 mInvalidChargerObserver.startObserving(
157 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Todd Poynor26faecc2013-05-22 18:54:48 -0700160 IBinder b = ServiceManager.getService("batterypropreg");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800161 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
162 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700163 try {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800164 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700165 } catch (RemoteException e) {
166 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 }
169
Jeff Browna4d82042012-10-02 19:11:19 -0700170 void systemReady() {
171 // check our power situation now that it is safe to display the shutdown dialog.
172 synchronized (mLock) {
173 shutdownIfNoPowerLocked();
174 shutdownIfOverTempLocked();
175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 }
177
Jeff Browna4d82042012-10-02 19:11:19 -0700178 /**
179 * Returns true if the device is plugged into any of the specified plug types.
180 */
181 public boolean isPowered(int plugTypeSet) {
182 synchronized (mLock) {
183 return isPoweredLocked(plugTypeSet);
184 }
185 }
186
187 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 // assume we are powered if battery state is unknown so
189 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700190 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return true;
192 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700193 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700194 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700196 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
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_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700200 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
Jeff Browna4d82042012-10-02 19:11:19 -0700202 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204
Jeff Browna4d82042012-10-02 19:11:19 -0700205 /**
Jeff Brownf3fb8952012-10-02 20:57:05 -0700206 * Returns the current plug type.
207 */
208 public int getPlugType() {
209 synchronized (mLock) {
210 return mPlugType;
211 }
212 }
213
214 /**
Jeff Browna4d82042012-10-02 19:11:19 -0700215 * Returns battery level as a percentage.
216 */
217 public int getBatteryLevel() {
218 synchronized (mLock) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700219 return mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
Jeff Browna4d82042012-10-02 19:11:19 -0700221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Jeff Browna4d82042012-10-02 19:11:19 -0700223 /**
224 * Returns true if battery level is below the first warning threshold.
225 */
226 public boolean isBatteryLow() {
227 synchronized (mLock) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700228 return mBatteryProps.batteryPresent && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231
Svetoslav6a08a122013-05-03 11:24:26 -0700232 /**
233 * Returns a non-zero value if an unsupported charger is attached.
234 */
235 public int getInvalidCharger() {
236 synchronized (mLock) {
237 return mInvalidCharger;
238 }
239 }
240
Jeff Browna4d82042012-10-02 19:11:19 -0700241 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400242 // shut down gracefully if our battery is critically low and we are not powered.
243 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700244 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700245 mHandler.post(new Runnable() {
246 @Override
247 public void run() {
248 if (ActivityManagerNative.isSystemReady()) {
249 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
250 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
251 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
252 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
253 }
254 }
255 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400256 }
257 }
258
Jeff Browna4d82042012-10-02 19:11:19 -0700259 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700260 // shut down gracefully if temperature is too high (> 68.0C by default)
261 // wait until the system has booted before attempting to display the
262 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700263 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
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 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700275 }
276 }
277
Todd Poynor26faecc2013-05-22 18:54:48 -0700278 private void update(BatteryProperties props) {
279 synchronized (mLock) {
280 if (!mUpdatesStopped) {
281 mBatteryProps = props;
282 // Process the new values.
283 processValuesLocked();
284 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700285 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
Jeff Browna4d82042012-10-02 19:11:19 -0700288 private void processValuesLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700289 boolean logOutlier = false;
290 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700291
Todd Poynor26faecc2013-05-22 18:54:48 -0700292 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
293 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700295 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700297 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700298 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 } else {
300 mPlugType = BATTERY_PLUGGED_NONE;
301 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700302
Jeff Browna4d82042012-10-02 19:11:19 -0700303 if (DEBUG) {
304 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700305 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
306 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
307 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
308 + ", batteryStatus=" + mBatteryProps.batteryStatus
309 + ", batteryHealth=" + mBatteryProps.batteryHealth
310 + ", batteryPresent=" + mBatteryProps.batteryPresent
311 + ", batteryLevel=" + mBatteryProps.batteryLevel
312 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
313 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
Todd Poynordf89ca32013-07-30 20:33:27 -0700314 + ", batteryCurrentNow=" + mBatteryProps.batteryCurrentNow
315 + ", batteryChargeCounter=" + mBatteryProps.batteryChargeCounter
Todd Poynor26faecc2013-05-22 18:54:48 -0700316 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700317 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
318 + ", mPlugType=" + mPlugType);
319 }
320
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700321 // Let the battery stats keep track of the current level.
322 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700323 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
324 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
325 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700326 } catch (RemoteException e) {
327 // Should never happen.
328 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700329
Jeff Browna4d82042012-10-02 19:11:19 -0700330 shutdownIfNoPowerLocked();
331 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700332
Todd Poynor26faecc2013-05-22 18:54:48 -0700333 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
334 mBatteryProps.batteryHealth != mLastBatteryHealth ||
335 mBatteryProps.batteryPresent != mLastBatteryPresent ||
336 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700338 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
339 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400340 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 if (mPlugType != mLastPlugType) {
343 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
344 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 // There's no value in this data unless we've discharged at least once and the
347 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700348 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700349 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
350 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700352 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
357 // charging -> discharging or we just powered up
358 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700359 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
361 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700362 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
363 mBatteryProps.batteryHealth != mLastBatteryHealth ||
364 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700367 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
368 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700370 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700371 // Don't do this just from voltage or temperature changes, that is
372 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700374 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 }
376 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
377 mPlugType == BATTERY_PLUGGED_NONE) {
378 // We want to make sure we log discharge cycle outliers
379 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700380 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
381 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700384 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
385 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
386
387 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400388 * - is just un-plugged (previously was plugged) and battery level is
389 * less than or equal to WARNING, or
390 * - is not plugged and battery level falls to WARNING boundary
391 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700392 */
393 final boolean sendBatteryLow = !plugged
Todd Poynor26faecc2013-05-22 18:54:48 -0700394 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
395 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
Joe Onoratode1b3592010-10-25 20:36:47 -0700396 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Jeff Browna4d82042012-10-02 19:11:19 -0700398 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
Christopher Tate06ba5542009-04-09 16:03:56 -0700400 // Separate broadcast is sent for power connected / not connected
401 // since the standard intent will not wake any applications and some
402 // applications may want to have smart behavior based on this.
403 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700404 mHandler.post(new Runnable() {
405 @Override
406 public void run() {
407 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
408 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
409 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
410 }
411 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700412 }
413 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700414 mHandler.post(new Runnable() {
415 @Override
416 public void run() {
417 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
418 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
419 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
420 }
421 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700422 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200423
Mihai Predaa82842f2009-04-29 15:05:56 +0200424 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700425 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700426 mHandler.post(new Runnable() {
427 @Override
428 public void run() {
429 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
430 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
431 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
432 }
433 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400434 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700435 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700436 mHandler.post(new Runnable() {
437 @Override
438 public void run() {
439 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
440 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
441 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
442 }
443 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
Joe Onoratode1b3592010-10-25 20:36:47 -0700446 // Update the battery LED
447 mLed.updateLightsLocked();
448
The Android Open Source Project10592532009-03-18 17:39:46 -0700449 // This needs to be done after sendIntent() so that we get the lastest battery stats.
450 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700451 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
Todd Poynor26faecc2013-05-22 18:54:48 -0700454 mLastBatteryStatus = mBatteryProps.batteryStatus;
455 mLastBatteryHealth = mBatteryProps.batteryHealth;
456 mLastBatteryPresent = mBatteryProps.batteryPresent;
457 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700458 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700459 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
460 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700461 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400462 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
464 }
465
Jeff Browna4d82042012-10-02 19:11:19 -0700466 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700468 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800469 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
470 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Todd Poynor26faecc2013-05-22 18:54:48 -0700472 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
Todd Poynor26faecc2013-05-22 18:54:48 -0700474 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
475 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
476 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
477 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700478 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
479 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
480 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700481 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
482 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
483 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400484 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
Jeff Browna4d82042012-10-02 19:11:19 -0700486 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700487 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
488 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
489 ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
490 ", voltage: " + mBatteryProps.batteryVoltage +
491 ", temperature: " + mBatteryProps.batteryTemperature +
492 ", technology: " + mBatteryProps.batteryTechnology +
493 ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
494 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Jeff Browna4d82042012-10-02 19:11:19 -0700495 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497
Jeff Brown605ea692012-10-05 16:33:10 -0700498 mHandler.post(new Runnable() {
499 @Override
500 public void run() {
501 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
502 }
503 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
Jeff Browna4d82042012-10-02 19:11:19 -0700506 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700507 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800508 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509
Dan Egnor18e93962010-02-10 19:27:58 -0800510 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
511 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
512
513 File dumpFile = null;
514 FileOutputStream dumpStream = null;
515 try {
516 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700517 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800518 dumpStream = new FileOutputStream(dumpFile);
519 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700520 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800521
522 // add dump file to drop box
523 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
524 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800526 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800528 } finally {
529 // make sure we clean up
530 if (dumpStream != null) {
531 try {
532 dumpStream.close();
533 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800534 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
Dan Egnor18e93962010-02-10 19:27:58 -0800536 }
537 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800539 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
541 }
542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
Jeff Browna4d82042012-10-02 19:11:19 -0700544 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700546 String dischargeThresholdString = Settings.Global.getString(cr,
547 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
548 String durationThresholdString = Settings.Global.getString(cr,
549 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 if (dischargeThresholdString != null && durationThresholdString != null) {
552 try {
553 long durationThreshold = Long.parseLong(durationThresholdString);
554 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700556 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700558 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Jeff Browna4d82042012-10-02 19:11:19 -0700560 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700562 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700563 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 durationThresholdString + " or " + dischargeThresholdString);
567 return;
568 }
569 }
570 }
571
Jeff Browna4d82042012-10-02 19:11:19 -0700572 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700573 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700575 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700577 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
578 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700579 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700580 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800581 return com.android.internal.R.drawable.stat_sys_battery_charge;
582 } else {
583 return com.android.internal.R.drawable.stat_sys_battery;
584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 } else {
586 return com.android.internal.R.drawable.stat_sys_battery_unknown;
587 }
588 }
589
590 @Override
591 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
592 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
593 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 pw.println("Permission Denial: can't dump Battery service from from pid="
596 + Binder.getCallingPid()
597 + ", uid=" + Binder.getCallingUid());
598 return;
599 }
600
Jeff Browna4d82042012-10-02 19:11:19 -0700601 synchronized (mLock) {
602 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700603 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700604 if (mUpdatesStopped) {
605 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
606 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700607 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
608 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
609 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
610 pw.println(" status: " + mBatteryProps.batteryStatus);
611 pw.println(" health: " + mBatteryProps.batteryHealth);
612 pw.println(" present: " + mBatteryProps.batteryPresent);
613 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700614 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700615 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
616
617 if (mBatteryProps.batteryCurrentNow != Integer.MIN_VALUE) {
618 pw.println(" current now: " + mBatteryProps.batteryCurrentNow);
619 }
620
621 if (mBatteryProps.batteryChargeCounter != Integer.MIN_VALUE) {
622 pw.println(" charge counter: " + mBatteryProps.batteryChargeCounter);
623 }
624
Todd Poynor26faecc2013-05-22 18:54:48 -0700625 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
626 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700627 } else if (args.length == 3 && "set".equals(args[0])) {
628 String key = args[1];
629 String value = args[2];
630 try {
631 boolean update = true;
632 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700633 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700634 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700635 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700636 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700637 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700638 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700639 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700640 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700641 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700642 } else if ("invalid".equals(key)) {
643 mInvalidCharger = Integer.parseInt(value);
644 } else {
645 pw.println("Unknown set option: " + key);
646 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700647 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700648 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700649 long ident = Binder.clearCallingIdentity();
650 try {
651 mUpdatesStopped = true;
652 processValuesLocked();
653 } finally {
654 Binder.restoreCallingIdentity(ident);
655 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700656 }
657 } catch (NumberFormatException ex) {
658 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700659 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700660 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700661 long ident = Binder.clearCallingIdentity();
662 try {
663 mUpdatesStopped = false;
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700664 } finally {
665 Binder.restoreCallingIdentity(ident);
666 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700667 } else {
668 pw.println("Dump current battery state, or:");
669 pw.println(" set ac|usb|wireless|status|level|invalid <value>");
670 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700674
Jeff Browna4d82042012-10-02 19:11:19 -0700675 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
676 @Override
677 public void onUEvent(UEventObserver.UEvent event) {
678 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
679 synchronized (mLock) {
680 if (mInvalidCharger != invalidCharger) {
681 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700682 }
683 }
684 }
685 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700686
Jeff Browna4d82042012-10-02 19:11:19 -0700687 private final class Led {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800688 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700689
Jeff Browna4d82042012-10-02 19:11:19 -0700690 private final int mBatteryLowARGB;
691 private final int mBatteryMediumARGB;
692 private final int mBatteryFullARGB;
693 private final int mBatteryLedOn;
694 private final int mBatteryLedOff;
695
Adam Lesinski182f73f2013-12-05 16:48:06 -0800696 public Led(Context context, LightsManager lights) {
697 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700698
Jeff Browna4d82042012-10-02 19:11:19 -0700699 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700700 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700701 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700702 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700703 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700704 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700705 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700706 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700707 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700708 com.android.internal.R.integer.config_notificationsBatteryLedOff);
709 }
710
711 /**
712 * Synchronize on BatteryService.
713 */
Jeff Browna4d82042012-10-02 19:11:19 -0700714 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700715 final int level = mBatteryProps.batteryLevel;
716 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700717 if (level < mLowBatteryWarningLevel) {
718 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
719 // Solid red when battery is charging
720 mBatteryLight.setColor(mBatteryLowARGB);
721 } else {
722 // Flash red when battery is low and not charging
Adam Lesinski182f73f2013-12-05 16:48:06 -0800723 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700724 mBatteryLedOn, mBatteryLedOff);
725 }
726 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
727 || status == BatteryManager.BATTERY_STATUS_FULL) {
728 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
729 // Solid green when full or charging and nearly full
730 mBatteryLight.setColor(mBatteryFullARGB);
731 } else {
732 // Solid orange when charging and halfway full
733 mBatteryLight.setColor(mBatteryMediumARGB);
734 }
735 } else {
736 // No lights if not charging and not low
737 mBatteryLight.turnOff();
738 }
739 }
740 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700741
742 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800743 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700744 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800745 final long identity = Binder.clearCallingIdentity();
746 try {
747 BatteryService.this.update(props);
748 } finally {
749 Binder.restoreCallingIdentity(identity);
750 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700751 }
752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753}