blob: 6e72e24de330b143c852896b2d2ad76f9c2001f8 [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;
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 Lesinskief2ea1f2013-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 Lesinskief2ea1f2013-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 Poynor0edc5b52013-10-22 17:53:13 -0700160 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800161 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
162 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700163 try {
Adam Lesinskief2ea1f2013-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
314 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700315 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
316 + ", mPlugType=" + mPlugType);
317 }
318
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700319 // Let the battery stats keep track of the current level.
320 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700321 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
322 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
323 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700324 } catch (RemoteException e) {
325 // Should never happen.
326 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700327
Jeff Browna4d82042012-10-02 19:11:19 -0700328 shutdownIfNoPowerLocked();
329 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700330
Todd Poynor26faecc2013-05-22 18:54:48 -0700331 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
332 mBatteryProps.batteryHealth != mLastBatteryHealth ||
333 mBatteryProps.batteryPresent != mLastBatteryPresent ||
334 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700336 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
337 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400338 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 if (mPlugType != mLastPlugType) {
341 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
342 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 // There's no value in this data unless we've discharged at least once and the
345 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700346 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700347 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
348 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700350 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
355 // charging -> discharging or we just powered up
356 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700357 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700360 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
361 mBatteryProps.batteryHealth != mLastBatteryHealth ||
362 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700365 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
366 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700368 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700369 // Don't do this just from voltage or temperature changes, that is
370 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700372 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
374 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
375 mPlugType == BATTERY_PLUGGED_NONE) {
376 // We want to make sure we log discharge cycle outliers
377 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700378 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
379 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700382 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
383 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
384
385 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400386 * - is just un-plugged (previously was plugged) and battery level is
387 * less than or equal to WARNING, or
388 * - is not plugged and battery level falls to WARNING boundary
389 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700390 */
391 final boolean sendBatteryLow = !plugged
Todd Poynor26faecc2013-05-22 18:54:48 -0700392 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
393 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
Joe Onoratode1b3592010-10-25 20:36:47 -0700394 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Jeff Browna4d82042012-10-02 19:11:19 -0700396 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Christopher Tate06ba5542009-04-09 16:03:56 -0700398 // Separate broadcast is sent for power connected / not connected
399 // since the standard intent will not wake any applications and some
400 // applications may want to have smart behavior based on this.
401 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700402 mHandler.post(new Runnable() {
403 @Override
404 public void run() {
405 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
406 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
407 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
408 }
409 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700410 }
411 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700412 mHandler.post(new Runnable() {
413 @Override
414 public void run() {
415 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
416 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
417 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
418 }
419 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700420 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200421
Mihai Predaa82842f2009-04-29 15:05:56 +0200422 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700423 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700424 mHandler.post(new Runnable() {
425 @Override
426 public void run() {
427 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
428 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
429 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
430 }
431 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400432 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700433 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700434 mHandler.post(new Runnable() {
435 @Override
436 public void run() {
437 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
438 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
439 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
440 }
441 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
Joe Onoratode1b3592010-10-25 20:36:47 -0700444 // Update the battery LED
445 mLed.updateLightsLocked();
446
The Android Open Source Project10592532009-03-18 17:39:46 -0700447 // This needs to be done after sendIntent() so that we get the lastest battery stats.
448 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700449 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Todd Poynor26faecc2013-05-22 18:54:48 -0700452 mLastBatteryStatus = mBatteryProps.batteryStatus;
453 mLastBatteryHealth = mBatteryProps.batteryHealth;
454 mLastBatteryPresent = mBatteryProps.batteryPresent;
455 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700456 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700457 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
458 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700459 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400460 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
463
Jeff Browna4d82042012-10-02 19:11:19 -0700464 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700466 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800467 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
468 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
Todd Poynor26faecc2013-05-22 18:54:48 -0700470 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
Todd Poynor26faecc2013-05-22 18:54:48 -0700472 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
473 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
474 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
475 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700476 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
477 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
478 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700479 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
480 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
481 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400482 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
Jeff Browna4d82042012-10-02 19:11:19 -0700484 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700485 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
486 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
487 ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
488 ", voltage: " + mBatteryProps.batteryVoltage +
489 ", temperature: " + mBatteryProps.batteryTemperature +
490 ", technology: " + mBatteryProps.batteryTechnology +
491 ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
492 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Jeff Browna4d82042012-10-02 19:11:19 -0700493 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495
Jeff Brown605ea692012-10-05 16:33:10 -0700496 mHandler.post(new Runnable() {
497 @Override
498 public void run() {
499 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
500 }
501 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
Jeff Browna4d82042012-10-02 19:11:19 -0700504 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700505 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800506 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
Dan Egnor18e93962010-02-10 19:27:58 -0800508 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
509 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
510
511 File dumpFile = null;
512 FileOutputStream dumpStream = null;
513 try {
514 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700515 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800516 dumpStream = new FileOutputStream(dumpFile);
517 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700518 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800519
520 // add dump file to drop box
521 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
522 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800524 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800526 } finally {
527 // make sure we clean up
528 if (dumpStream != null) {
529 try {
530 dumpStream.close();
531 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
Dan Egnor18e93962010-02-10 19:27:58 -0800534 }
535 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800537 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 }
540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Jeff Browna4d82042012-10-02 19:11:19 -0700542 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700544 String dischargeThresholdString = Settings.Global.getString(cr,
545 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
546 String durationThresholdString = Settings.Global.getString(cr,
547 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 if (dischargeThresholdString != null && durationThresholdString != null) {
550 try {
551 long durationThreshold = Long.parseLong(durationThresholdString);
552 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700554 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700556 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
Jeff Browna4d82042012-10-02 19:11:19 -0700558 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700560 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700561 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800563 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 durationThresholdString + " or " + dischargeThresholdString);
565 return;
566 }
567 }
568 }
569
Jeff Browna4d82042012-10-02 19:11:19 -0700570 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700571 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700573 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700575 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
576 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700577 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700578 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800579 return com.android.internal.R.drawable.stat_sys_battery_charge;
580 } else {
581 return com.android.internal.R.drawable.stat_sys_battery;
582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 } else {
584 return com.android.internal.R.drawable.stat_sys_battery_unknown;
585 }
586 }
587
588 @Override
589 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
590 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
591 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 pw.println("Permission Denial: can't dump Battery service from from pid="
594 + Binder.getCallingPid()
595 + ", uid=" + Binder.getCallingUid());
596 return;
597 }
598
Jeff Browna4d82042012-10-02 19:11:19 -0700599 synchronized (mLock) {
600 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700601 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700602 if (mUpdatesStopped) {
603 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
604 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700605 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
606 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
607 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
608 pw.println(" status: " + mBatteryProps.batteryStatus);
609 pw.println(" health: " + mBatteryProps.batteryHealth);
610 pw.println(" present: " + mBatteryProps.batteryPresent);
611 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700612 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700613 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700614 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
615 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700616 } else if (args.length == 3 && "set".equals(args[0])) {
617 String key = args[1];
618 String value = args[2];
619 try {
620 boolean update = true;
621 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700622 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700623 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700624 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700625 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700626 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700627 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700628 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700629 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700630 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700631 } else if ("invalid".equals(key)) {
632 mInvalidCharger = Integer.parseInt(value);
633 } else {
634 pw.println("Unknown set option: " + key);
635 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700636 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700637 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700638 long ident = Binder.clearCallingIdentity();
639 try {
640 mUpdatesStopped = true;
641 processValuesLocked();
642 } finally {
643 Binder.restoreCallingIdentity(ident);
644 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700645 }
646 } catch (NumberFormatException ex) {
647 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700648 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700649 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700650 long ident = Binder.clearCallingIdentity();
651 try {
652 mUpdatesStopped = false;
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700653 } finally {
654 Binder.restoreCallingIdentity(ident);
655 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700656 } else {
657 pw.println("Dump current battery state, or:");
658 pw.println(" set ac|usb|wireless|status|level|invalid <value>");
659 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700663
Jeff Browna4d82042012-10-02 19:11:19 -0700664 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
665 @Override
666 public void onUEvent(UEventObserver.UEvent event) {
667 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
668 synchronized (mLock) {
669 if (mInvalidCharger != invalidCharger) {
670 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700671 }
672 }
673 }
674 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700675
Jeff Browna4d82042012-10-02 19:11:19 -0700676 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800677 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700678
Jeff Browna4d82042012-10-02 19:11:19 -0700679 private final int mBatteryLowARGB;
680 private final int mBatteryMediumARGB;
681 private final int mBatteryFullARGB;
682 private final int mBatteryLedOn;
683 private final int mBatteryLedOff;
684
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800685 public Led(Context context, LightsManager lights) {
686 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700687
Jeff Browna4d82042012-10-02 19:11:19 -0700688 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700689 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700690 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700691 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700692 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700693 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700694 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700695 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700696 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700697 com.android.internal.R.integer.config_notificationsBatteryLedOff);
698 }
699
700 /**
701 * Synchronize on BatteryService.
702 */
Jeff Browna4d82042012-10-02 19:11:19 -0700703 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700704 final int level = mBatteryProps.batteryLevel;
705 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700706 if (level < mLowBatteryWarningLevel) {
707 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
708 // Solid red when battery is charging
709 mBatteryLight.setColor(mBatteryLowARGB);
710 } else {
711 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800712 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700713 mBatteryLedOn, mBatteryLedOff);
714 }
715 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
716 || status == BatteryManager.BATTERY_STATUS_FULL) {
717 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
718 // Solid green when full or charging and nearly full
719 mBatteryLight.setColor(mBatteryFullARGB);
720 } else {
721 // Solid orange when charging and halfway full
722 mBatteryLight.setColor(mBatteryMediumARGB);
723 }
724 } else {
725 // No lights if not charging and not low
726 mBatteryLight.turnOff();
727 }
728 }
729 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700730
731 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800732 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700733 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800734 final long identity = Binder.clearCallingIdentity();
735 try {
736 BatteryService.this.update(props);
737 } finally {
738 Binder.restoreCallingIdentity(identity);
739 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700740 }
741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742}