blob: 0045f4a8d97fdf567e07a45ac8b491310ded0e2f [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
19import com.android.internal.app.IBatteryStats;
20import com.android.server.am.BatteryStatsService;
21
22import android.app.ActivityManagerNative;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.pm.PackageManager;
27import android.os.BatteryManager;
28import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070029import android.os.FileUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080031import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.os.SystemClock;
35import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070036import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.provider.Settings;
38import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080039import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import java.io.File;
42import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import java.io.FileOutputStream;
44import java.io.IOException;
45import java.io.PrintWriter;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48/**
49 * <p>BatteryService monitors the charging status, and charge level of the device
50 * battery. When these values change this service broadcasts the new values
51 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
52 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
53 * BATTERY_CHANGED} action.</p>
54 * <p>The new values are stored in the Intent data and can be retrieved by
55 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
56 * following keys:</p>
57 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
58 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
59 * <p>&quot;status&quot; - String, the current charging status.<br />
60 * <p>&quot;health&quot; - String, the current battery health.<br />
61 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
62 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
63 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
64 * into an AC power adapter; 2 if plugged in via USB.</p>
65 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
66 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
67 * a degree Centigrade</p>
68 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
69 */
Jeff Browna4d82042012-10-02 19:11:19 -070070public final class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080072
Jeff Browna4d82042012-10-02 19:11:19 -070073 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080074
Jeff Browna4d82042012-10-02 19:11:19 -070075 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77 // Used locally for determining when to make a last ditch effort to log
78 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070079 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Dianne Hackborn6447ca32009-04-07 19:50:08 -070082 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo";
Doug Zongkerab5c49c2009-12-04 10:31:43 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final String DUMPSYS_DATA_PATH = "/data/system/";
86
87 // This should probably be exposed in the API, though it's not critical
88 private static final int BATTERY_PLUGGED_NONE = 0;
89
90 private final Context mContext;
91 private final IBatteryStats mBatteryStats;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080092
Jeff Browna4d82042012-10-02 19:11:19 -070093 private final Object mLock = new Object();
94
95 /* Begin native fields: All of these fields are set by native code. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private boolean mAcOnline;
97 private boolean mUsbOnline;
Brian Muramatsu37a37f42012-08-14 15:21:02 -070098 private boolean mWirelessOnline;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private int mBatteryStatus;
100 private int mBatteryHealth;
101 private boolean mBatteryPresent;
102 private int mBatteryLevel;
103 private int mBatteryVoltage;
104 private int mBatteryTemperature;
105 private String mBatteryTechnology;
106 private boolean mBatteryLevelCritical;
Jeff Browna4d82042012-10-02 19:11:19 -0700107 /* End native fields. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 private int mLastBatteryStatus;
110 private int mLastBatteryHealth;
111 private boolean mLastBatteryPresent;
112 private int mLastBatteryLevel;
113 private int mLastBatteryVoltage;
114 private int mLastBatteryTemperature;
115 private boolean mLastBatteryLevelCritical;
Jeff Browna4d82042012-10-02 19:11:19 -0700116
117 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700118 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400119
120 private int mLowBatteryWarningLevel;
121 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700122 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private int mPlugType;
125 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 private long mDischargeStartTime;
128 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
Joe Onoratode1b3592010-10-25 20:36:47 -0700130 private Led mLed;
131
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700132 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
Jeff Browna4d82042012-10-02 19:11:19 -0700134 private native void native_update();
135
Joe Onoratode1b3592010-10-25 20:36:47 -0700136 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700138 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 mBatteryStats = BatteryStatsService.getService();
140
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700141 mCriticalBatteryLevel = mContext.getResources().getInteger(
142 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400143 mLowBatteryWarningLevel = mContext.getResources().getInteger(
144 com.android.internal.R.integer.config_lowBatteryWarningLevel);
145 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
146 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700147 mShutdownBatteryTemperature = mContext.getResources().getInteger(
148 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400149
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400150 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
151
152 // watch for invalid charger messages if the invalid_charger switch exists
153 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700154 mInvalidChargerObserver.startObserving(
155 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 // set initial status
Jeff Browna4d82042012-10-02 19:11:19 -0700159 synchronized (mLock) {
160 updateLocked();
161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 }
163
Jeff Browna4d82042012-10-02 19:11:19 -0700164 void systemReady() {
165 // check our power situation now that it is safe to display the shutdown dialog.
166 synchronized (mLock) {
167 shutdownIfNoPowerLocked();
168 shutdownIfOverTempLocked();
169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171
Jeff Browna4d82042012-10-02 19:11:19 -0700172 /**
173 * Returns true if the device is plugged into any of the specified plug types.
174 */
175 public boolean isPowered(int plugTypeSet) {
176 synchronized (mLock) {
177 return isPoweredLocked(plugTypeSet);
178 }
179 }
180
181 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 // assume we are powered if battery state is unknown so
183 // the "stay on while plugged in" option will work.
184 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
185 return true;
186 }
Jeff Browna4d82042012-10-02 19:11:19 -0700187 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mAcOnline) {
188 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 }
Jeff Browna4d82042012-10-02 19:11:19 -0700190 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mUsbOnline) {
191 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 }
Jeff Browna4d82042012-10-02 19:11:19 -0700193 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mWirelessOnline) {
194 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
Jeff Browna4d82042012-10-02 19:11:19 -0700196 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 }
198
Jeff Browna4d82042012-10-02 19:11:19 -0700199 /**
Jeff Brownf3fb8952012-10-02 20:57:05 -0700200 * Returns the current plug type.
201 */
202 public int getPlugType() {
203 synchronized (mLock) {
204 return mPlugType;
205 }
206 }
207
208 /**
Jeff Browna4d82042012-10-02 19:11:19 -0700209 * Returns battery level as a percentage.
210 */
211 public int getBatteryLevel() {
212 synchronized (mLock) {
213 return mBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
Jeff Browna4d82042012-10-02 19:11:19 -0700215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
Jeff Browna4d82042012-10-02 19:11:19 -0700217 /**
218 * Returns true if battery level is below the first warning threshold.
219 */
220 public boolean isBatteryLow() {
221 synchronized (mLock) {
222 return mBatteryPresent && mBatteryLevel <= mLowBatteryWarningLevel;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 }
225
Jeff Browna4d82042012-10-02 19:11:19 -0700226 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400227 // shut down gracefully if our battery is critically low and we are not powered.
228 // wait until the system has booted before attempting to display the shutdown dialog.
Jeff Browna4d82042012-10-02 19:11:19 -0700229 if (mBatteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
230 && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400231 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
232 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
233 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
234 mContext.startActivity(intent);
235 }
236 }
237
Jeff Browna4d82042012-10-02 19:11:19 -0700238 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700239 // shut down gracefully if temperature is too high (> 68.0C by default)
240 // wait until the system has booted before attempting to display the
241 // shutdown dialog.
242 if (mBatteryTemperature > mShutdownBatteryTemperature
243 && ActivityManagerNative.isSystemReady()) {
Eric Olsen6a362a92010-03-26 15:38:41 -0700244 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
245 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
246 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
247 mContext.startActivity(intent);
248 }
249 }
250
Jeff Browna4d82042012-10-02 19:11:19 -0700251 private void updateLocked() {
252 // Update the values of mAcOnline, et. all.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 native_update();
Jeff Browna4d82042012-10-02 19:11:19 -0700254
255 // Process the new values.
256 processValuesLocked();
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Jeff Browna4d82042012-10-02 19:11:19 -0700259 private void processValuesLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700260 boolean logOutlier = false;
261 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700262
Jeff Browna4d82042012-10-02 19:11:19 -0700263 mBatteryLevelCritical = (mBatteryLevel <= mCriticalBatteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 if (mAcOnline) {
265 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
266 } else if (mUsbOnline) {
267 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700268 } else if (mWirelessOnline) {
269 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 } else {
271 mPlugType = BATTERY_PLUGGED_NONE;
272 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700273
Jeff Browna4d82042012-10-02 19:11:19 -0700274 if (DEBUG) {
275 Slog.d(TAG, "Processing new values: "
276 + "mAcOnline=" + mAcOnline
277 + ", mUsbOnline=" + mUsbOnline
278 + ", mWirelessOnline=" + mWirelessOnline
279 + ", mBatteryStatus=" + mBatteryStatus
280 + ", mBatteryHealth=" + mBatteryHealth
281 + ", mBatteryPresent=" + mBatteryPresent
282 + ", mBatteryLevel=" + mBatteryLevel
283 + ", mBatteryTechnology=" + mBatteryTechnology
284 + ", mBatteryVoltage=" + mBatteryVoltage
285 + ", mBatteryTemperature=" + mBatteryTemperature
286 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
287 + ", mPlugType=" + mPlugType);
288 }
289
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700290 // Let the battery stats keep track of the current level.
291 try {
292 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
293 mPlugType, mBatteryLevel, mBatteryTemperature,
294 mBatteryVoltage);
295 } catch (RemoteException e) {
296 // Should never happen.
297 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700298
Jeff Browna4d82042012-10-02 19:11:19 -0700299 shutdownIfNoPowerLocked();
300 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 if (mBatteryStatus != mLastBatteryStatus ||
303 mBatteryHealth != mLastBatteryHealth ||
304 mBatteryPresent != mLastBatteryPresent ||
305 mBatteryLevel != mLastBatteryLevel ||
306 mPlugType != mLastPlugType ||
307 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400308 mBatteryTemperature != mLastBatteryTemperature ||
309 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 if (mPlugType != mLastPlugType) {
312 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
313 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 // There's no value in this data unless we've discharged at least once and the
316 // battery level has changed; so don't log until it does.
317 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700318 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
319 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 mDischargeStartLevel, mBatteryLevel);
322 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
325 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
326 // charging -> discharging or we just powered up
327 mDischargeStartTime = SystemClock.elapsedRealtime();
328 mDischargeStartLevel = mBatteryLevel;
329 }
330 }
331 if (mBatteryStatus != mLastBatteryStatus ||
332 mBatteryHealth != mLastBatteryHealth ||
333 mBatteryPresent != mLastBatteryPresent ||
334 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
337 mPlugType, mBatteryTechnology);
338 }
339 if (mBatteryLevel != mLastBatteryLevel ||
340 mBatteryVoltage != mLastBatteryVoltage ||
341 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
344 }
345 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
346 mPlugType == BATTERY_PLUGGED_NONE) {
347 // We want to make sure we log discharge cycle outliers
348 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700349 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
350 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700353 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
354 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
355
356 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400357 * - is just un-plugged (previously was plugged) and battery level is
358 * less than or equal to WARNING, or
359 * - is not plugged and battery level falls to WARNING boundary
360 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700361 */
362 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700363 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
364 && mBatteryLevel <= mLowBatteryWarningLevel
365 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
Jeff Browna4d82042012-10-02 19:11:19 -0700367 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
Christopher Tate06ba5542009-04-09 16:03:56 -0700369 // Separate broadcast is sent for power connected / not connected
370 // since the standard intent will not wake any applications and some
371 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700372 Intent statusIntent = new Intent();
373 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700374 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700375 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700376 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Christopher Tate06ba5542009-04-09 16:03:56 -0700377 }
378 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700379 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700380 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Christopher Tate06ba5542009-04-09 16:03:56 -0700381 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200382
Mihai Predaa82842f2009-04-29 15:05:56 +0200383 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700384 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700385 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700386 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400387 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700388 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700389 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700390 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Mihai Predaa82842f2009-04-29 15:05:56 +0200391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
Joe Onoratode1b3592010-10-25 20:36:47 -0700393 // Update the battery LED
394 mLed.updateLightsLocked();
395
The Android Open Source Project10592532009-03-18 17:39:46 -0700396 // This needs to be done after sendIntent() so that we get the lastest battery stats.
397 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700398 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700401 mLastBatteryStatus = mBatteryStatus;
402 mLastBatteryHealth = mBatteryHealth;
403 mLastBatteryPresent = mBatteryPresent;
404 mLastBatteryLevel = mBatteryLevel;
405 mLastPlugType = mPlugType;
406 mLastBatteryVoltage = mBatteryVoltage;
407 mLastBatteryTemperature = mBatteryTemperature;
408 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400409 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 }
412
Jeff Browna4d82042012-10-02 19:11:19 -0700413 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 // Pack up the values and broadcast them to everyone
415 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800416 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
417 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
Jeff Browna4d82042012-10-02 19:11:19 -0700419 int icon = getIconLocked(mBatteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
Dianne Hackbornedd93162009-09-19 14:03:05 -0700421 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
422 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
423 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
424 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
425 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
426 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
427 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
428 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
429 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
430 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400431 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
Jeff Browna4d82042012-10-02 19:11:19 -0700433 if (DEBUG) {
434 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryLevel +
435 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryStatus +
436 ", health:" + mBatteryHealth + ", present:" + mBatteryPresent +
437 ", voltage: " + mBatteryVoltage +
438 ", temperature: " + mBatteryTemperature +
439 ", technology: " + mBatteryTechnology +
440 ", AC powered:" + mAcOnline + ", USB powered:" + mUsbOnline +
441 ", Wireless powered:" + mWirelessOnline +
442 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700445 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447
Jeff Browna4d82042012-10-02 19:11:19 -0700448 private void logBatteryStatsLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800450 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451
Dan Egnor18e93962010-02-10 19:27:58 -0800452 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
453 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
454
455 File dumpFile = null;
456 FileOutputStream dumpStream = null;
457 try {
458 // dump the service to a file
459 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
460 dumpStream = new FileOutputStream(dumpFile);
461 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700462 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800463
464 // add dump file to drop box
465 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
466 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800467 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800468 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800469 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800470 } finally {
471 // make sure we clean up
472 if (dumpStream != null) {
473 try {
474 dumpStream.close();
475 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
Dan Egnor18e93962010-02-10 19:27:58 -0800478 }
479 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800480 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800481 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483 }
484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800485
Jeff Browna4d82042012-10-02 19:11:19 -0700486 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700488 String dischargeThresholdString = Settings.Global.getString(cr,
489 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
490 String durationThresholdString = Settings.Global.getString(cr,
491 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 if (dischargeThresholdString != null && durationThresholdString != null) {
494 try {
495 long durationThreshold = Long.parseLong(durationThresholdString);
496 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
499 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700500 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
Jeff Browna4d82042012-10-02 19:11:19 -0700502 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700504 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 (mDischargeStartLevel - mBatteryLevel));
506 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 durationThresholdString + " or " + dischargeThresholdString);
509 return;
510 }
511 }
512 }
513
Jeff Browna4d82042012-10-02 19:11:19 -0700514 private int getIconLocked(int level) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
516 return com.android.internal.R.drawable.stat_sys_battery_charge;
Joe Onorato794be402010-11-21 19:22:25 -0800517 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return com.android.internal.R.drawable.stat_sys_battery;
Joe Onorato794be402010-11-21 19:22:25 -0800519 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
520 || mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700521 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
522 && mBatteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800523 return com.android.internal.R.drawable.stat_sys_battery_charge;
524 } else {
525 return com.android.internal.R.drawable.stat_sys_battery;
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 } else {
528 return com.android.internal.R.drawable.stat_sys_battery_unknown;
529 }
530 }
531
532 @Override
533 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
534 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
535 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 pw.println("Permission Denial: can't dump Battery service from from pid="
538 + Binder.getCallingPid()
539 + ", uid=" + Binder.getCallingUid());
540 return;
541 }
542
Jeff Browna4d82042012-10-02 19:11:19 -0700543 synchronized (mLock) {
544 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700545 pw.println("Current Battery Service state:");
546 pw.println(" AC powered: " + mAcOnline);
547 pw.println(" USB powered: " + mUsbOnline);
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700548 pw.println(" Wireless powered: " + mWirelessOnline);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700549 pw.println(" status: " + mBatteryStatus);
550 pw.println(" health: " + mBatteryHealth);
551 pw.println(" present: " + mBatteryPresent);
552 pw.println(" level: " + mBatteryLevel);
553 pw.println(" scale: " + BATTERY_SCALE);
554 pw.println(" voltage:" + mBatteryVoltage);
555 pw.println(" temperature: " + mBatteryTemperature);
556 pw.println(" technology: " + mBatteryTechnology);
Jeff Browna4d82042012-10-02 19:11:19 -0700557 } else if (false) {
558 // DO NOT SUBMIT WITH THIS TURNED ON
559 if (args.length == 3 && "set".equals(args[0])) {
560 String key = args[1];
561 String value = args[2];
562 try {
563 boolean update = true;
564 if ("ac".equals(key)) {
565 mAcOnline = Integer.parseInt(value) != 0;
566 } else if ("usb".equals(key)) {
567 mUsbOnline = Integer.parseInt(value) != 0;
568 } else if ("wireless".equals(key)) {
569 mWirelessOnline = Integer.parseInt(value) != 0;
570 } else if ("status".equals(key)) {
571 mBatteryStatus = Integer.parseInt(value);
572 } else if ("level".equals(key)) {
573 mBatteryLevel = Integer.parseInt(value);
574 } else if ("invalid".equals(key)) {
575 mInvalidCharger = Integer.parseInt(value);
576 } else {
577 update = false;
578 }
579 if (update) {
580 processValuesLocked();
581 }
582 } catch (NumberFormatException ex) {
583 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700584 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700585 }
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700589
Jeff Browna4d82042012-10-02 19:11:19 -0700590 private final UEventObserver mPowerSupplyObserver = new UEventObserver() {
591 @Override
592 public void onUEvent(UEventObserver.UEvent event) {
593 synchronized (mLock) {
594 updateLocked();
595 }
596 }
597 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700598
Jeff Browna4d82042012-10-02 19:11:19 -0700599 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
600 @Override
601 public void onUEvent(UEventObserver.UEvent event) {
602 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
603 synchronized (mLock) {
604 if (mInvalidCharger != invalidCharger) {
605 mInvalidCharger = invalidCharger;
606 updateLocked();
607 }
608 }
609 }
610 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700611
Jeff Browna4d82042012-10-02 19:11:19 -0700612 private final class Led {
613 private final LightsService.Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700614
Jeff Browna4d82042012-10-02 19:11:19 -0700615 private final int mBatteryLowARGB;
616 private final int mBatteryMediumARGB;
617 private final int mBatteryFullARGB;
618 private final int mBatteryLedOn;
619 private final int mBatteryLedOff;
620
621 public Led(Context context, LightsService lights) {
Joe Onoratode1b3592010-10-25 20:36:47 -0700622 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
623
Jeff Browna4d82042012-10-02 19:11:19 -0700624 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700625 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700626 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700627 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700628 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700629 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700630 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700631 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700632 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700633 com.android.internal.R.integer.config_notificationsBatteryLedOff);
634 }
635
636 /**
637 * Synchronize on BatteryService.
638 */
Jeff Browna4d82042012-10-02 19:11:19 -0700639 public void updateLightsLocked() {
Joe Onoratode1b3592010-10-25 20:36:47 -0700640 final int level = mBatteryLevel;
641 final int status = mBatteryStatus;
642 if (level < mLowBatteryWarningLevel) {
643 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
644 // Solid red when battery is charging
645 mBatteryLight.setColor(mBatteryLowARGB);
646 } else {
647 // Flash red when battery is low and not charging
648 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
649 mBatteryLedOn, mBatteryLedOff);
650 }
651 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
652 || status == BatteryManager.BATTERY_STATUS_FULL) {
653 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
654 // Solid green when full or charging and nearly full
655 mBatteryLight.setColor(mBatteryFullARGB);
656 } else {
657 // Solid orange when charging and halfway full
658 mBatteryLight.setColor(mBatteryMediumARGB);
659 }
660 } else {
661 // No lights if not charging and not low
662 mBatteryLight.turnOff();
663 }
664 }
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666}