blob: f0989e74dadb7bdad69a5a20d5ef4669f174b005 [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;
43import java.io.FileInputStream;
44import java.io.FileOutputStream;
45import java.io.IOException;
46import java.io.PrintWriter;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070047import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50/**
51 * <p>BatteryService monitors the charging status, and charge level of the device
52 * battery. When these values change this service broadcasts the new values
53 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
54 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
55 * BATTERY_CHANGED} action.</p>
56 * <p>The new values are stored in the Intent data and can be retrieved by
57 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
58 * following keys:</p>
59 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
60 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
61 * <p>&quot;status&quot; - String, the current charging status.<br />
62 * <p>&quot;health&quot; - String, the current battery health.<br />
63 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
64 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
65 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
66 * into an AC power adapter; 2 if plugged in via USB.</p>
67 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
68 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
69 * a degree Centigrade</p>
70 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
71 */
Jeff Brown4f8ecd82012-06-18 18:29:13 -070072public class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private static final boolean LOCAL_LOGV = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 static final int BATTERY_SCALE = 100; // battery capacity is a percentage
78
79 // Used locally for determining when to make a last ditch effort to log
80 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070081 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Dianne Hackborn6447ca32009-04-07 19:50:08 -070084 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo";
Doug Zongkerab5c49c2009-12-04 10:31:43 -080086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private static final String DUMPSYS_DATA_PATH = "/data/system/";
88
89 // This should probably be exposed in the API, though it's not critical
90 private static final int BATTERY_PLUGGED_NONE = 0;
91
92 private final Context mContext;
93 private final IBatteryStats mBatteryStats;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private boolean mAcOnline;
96 private boolean mUsbOnline;
Brian Muramatsu37a37f42012-08-14 15:21:02 -070097 private boolean mWirelessOnline;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private int mBatteryStatus;
99 private int mBatteryHealth;
100 private boolean mBatteryPresent;
101 private int mBatteryLevel;
102 private int mBatteryVoltage;
103 private int mBatteryTemperature;
104 private String mBatteryTechnology;
105 private boolean mBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700106 private int mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private int mLastBatteryStatus;
109 private int mLastBatteryHealth;
110 private boolean mLastBatteryPresent;
111 private int mLastBatteryLevel;
112 private int mLastBatteryVoltage;
113 private int mLastBatteryTemperature;
114 private boolean mLastBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700115 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400116
117 private int mLowBatteryWarningLevel;
118 private int mLowBatteryCloseWarningLevel;
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 private int mPlugType;
121 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 private long mDischargeStartTime;
124 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800125
Joe Onoratode1b3592010-10-25 20:36:47 -0700126 private Led mLed;
127
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700128 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
Joe Onoratode1b3592010-10-25 20:36:47 -0700130 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700132 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 mBatteryStats = BatteryStatsService.getService();
134
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700135 mCriticalBatteryLevel = mContext.getResources().getInteger(
136 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400137 mLowBatteryWarningLevel = mContext.getResources().getInteger(
138 com.android.internal.R.integer.config_lowBatteryWarningLevel);
139 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
140 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
141
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400142 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
143
144 // watch for invalid charger messages if the invalid_charger switch exists
145 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
146 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
149 // set initial status
150 update();
151 }
152
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700153 public final boolean isPowered() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work.
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700155 return (mAcOnline || mUsbOnline || mWirelessOnline
156 || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700159 public final boolean isPowered(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 // assume we are powered if battery state is unknown so
161 // the "stay on while plugged in" option will work.
162 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
163 return true;
164 }
165 if (plugTypeSet == 0) {
166 return false;
167 }
168 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700169 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
171 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700172 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
174 }
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700175 if (mWirelessOnline) {
176 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_WIRELESS;
177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 return (plugTypeSet & plugTypeBit) != 0;
179 }
180
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700181 public final int getPlugType() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 return mPlugType;
183 }
184
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400185 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 @Override
187 public void onUEvent(UEventObserver.UEvent event) {
188 update();
189 }
190 };
191
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400192 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
193 @Override
194 public void onUEvent(UEventObserver.UEvent event) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700195 int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400196 if (mInvalidCharger != invalidCharger) {
197 mInvalidCharger = invalidCharger;
198 update();
199 }
200 }
201 };
202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 // returns battery level as a percentage
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700204 public final int getBatteryLevel() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 return mBatteryLevel;
206 }
207
John Spurlock10fb2242012-08-23 15:32:28 -0400208 // true if battery level is below the first warning threshold
209 public final boolean isBatteryLow() {
210 return mBatteryPresent && mBatteryLevel <= mLowBatteryWarningLevel;
211 }
212
Mike Lockwood07a500f2009-08-12 09:56:44 -0400213 void systemReady() {
214 // check our power situation now that it is safe to display the shutdown dialog.
215 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700216 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400217 }
218
219 private final void shutdownIfNoPower() {
220 // shut down gracefully if our battery is critically low and we are not powered.
221 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700222 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400223 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
224 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
225 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
226 mContext.startActivity(intent);
227 }
228 }
229
Eric Olsen6a362a92010-03-26 15:38:41 -0700230 private final void shutdownIfOverTemp() {
231 // shut down gracefully if temperature is too high (> 68.0C)
232 // wait until the system has booted before attempting to display the shutdown dialog.
233 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
234 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
235 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
236 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
237 mContext.startActivity(intent);
238 }
239 }
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 private native void native_update();
242
243 private synchronized final void update() {
244 native_update();
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700245 processValues();
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700248 private void processValues() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700249 boolean logOutlier = false;
250 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700251
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700252 mBatteryLevelCritical = mBatteryLevel <= mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 if (mAcOnline) {
254 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
255 } else if (mUsbOnline) {
256 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700257 } else if (mWirelessOnline) {
258 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 } else {
260 mPlugType = BATTERY_PLUGGED_NONE;
261 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700262
263 // Let the battery stats keep track of the current level.
264 try {
265 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
266 mPlugType, mBatteryLevel, mBatteryTemperature,
267 mBatteryVoltage);
268 } catch (RemoteException e) {
269 // Should never happen.
270 }
271
272 shutdownIfNoPower();
273 shutdownIfOverTemp();
274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 if (mBatteryStatus != mLastBatteryStatus ||
276 mBatteryHealth != mLastBatteryHealth ||
277 mBatteryPresent != mLastBatteryPresent ||
278 mBatteryLevel != mLastBatteryLevel ||
279 mPlugType != mLastPlugType ||
280 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400281 mBatteryTemperature != mLastBatteryTemperature ||
282 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 if (mPlugType != mLastPlugType) {
285 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
286 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 // There's no value in this data unless we've discharged at least once and the
289 // battery level has changed; so don't log until it does.
290 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700291 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
292 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 mDischargeStartLevel, mBatteryLevel);
295 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
299 // charging -> discharging or we just powered up
300 mDischargeStartTime = SystemClock.elapsedRealtime();
301 mDischargeStartLevel = mBatteryLevel;
302 }
303 }
304 if (mBatteryStatus != mLastBatteryStatus ||
305 mBatteryHealth != mLastBatteryHealth ||
306 mBatteryPresent != mLastBatteryPresent ||
307 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
310 mPlugType, mBatteryTechnology);
311 }
312 if (mBatteryLevel != mLastBatteryLevel ||
313 mBatteryVoltage != mLastBatteryVoltage ||
314 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
317 }
318 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
319 mPlugType == BATTERY_PLUGGED_NONE) {
320 // We want to make sure we log discharge cycle outliers
321 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700322 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
323 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700326 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
327 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
328
329 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400330 * - is just un-plugged (previously was plugged) and battery level is
331 * less than or equal to WARNING, or
332 * - is not plugged and battery level falls to WARNING boundary
333 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700334 */
335 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700336 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
337 && mBatteryLevel <= mLowBatteryWarningLevel
338 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700340 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
Christopher Tate06ba5542009-04-09 16:03:56 -0700342 // Separate broadcast is sent for power connected / not connected
343 // since the standard intent will not wake any applications and some
344 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700345 Intent statusIntent = new Intent();
346 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700347 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700348 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700349 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Christopher Tate06ba5542009-04-09 16:03:56 -0700350 }
351 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700352 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700353 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Christopher Tate06ba5542009-04-09 16:03:56 -0700354 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200355
Mihai Predaa82842f2009-04-29 15:05:56 +0200356 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700357 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700358 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700359 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400360 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700361 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700362 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700363 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Mihai Predaa82842f2009-04-29 15:05:56 +0200364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
Joe Onoratode1b3592010-10-25 20:36:47 -0700366 // Update the battery LED
367 mLed.updateLightsLocked();
368
The Android Open Source Project10592532009-03-18 17:39:46 -0700369 // This needs to be done after sendIntent() so that we get the lastest battery stats.
370 if (logOutlier && dischargeDuration != 0) {
371 logOutlier(dischargeDuration);
372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700374 mLastBatteryStatus = mBatteryStatus;
375 mLastBatteryHealth = mBatteryHealth;
376 mLastBatteryPresent = mBatteryPresent;
377 mLastBatteryLevel = mBatteryLevel;
378 mLastPlugType = mPlugType;
379 mLastBatteryVoltage = mBatteryVoltage;
380 mLastBatteryTemperature = mBatteryTemperature;
381 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400382 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384 }
385
386 private final void sendIntent() {
387 // Pack up the values and broadcast them to everyone
388 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800389 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
390 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 int icon = getIcon(mBatteryLevel);
393
Dianne Hackbornedd93162009-09-19 14:03:05 -0700394 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
395 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
396 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
397 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
398 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
399 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
400 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
401 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
402 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
403 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400404 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
Joe Onorato53859742011-04-06 18:27:43 -0700406 if (false) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700407 Slog.d(TAG, "level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
409 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 " voltage: " + mBatteryVoltage +
411 " temperature: " + mBatteryTemperature +
412 " technology: " + mBatteryTechnology +
413 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700414 " Wireless powered:" + mWirelessOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400415 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700418 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420
421 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800423 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424
Dan Egnor18e93962010-02-10 19:27:58 -0800425 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
426 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
427
428 File dumpFile = null;
429 FileOutputStream dumpStream = null;
430 try {
431 // dump the service to a file
432 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
433 dumpStream = new FileOutputStream(dumpFile);
434 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700435 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800436
437 // add dump file to drop box
438 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
439 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800440 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800441 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800442 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800443 } finally {
444 // make sure we clean up
445 if (dumpStream != null) {
446 try {
447 dumpStream.close();
448 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800449 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
Dan Egnor18e93962010-02-10 19:27:58 -0800451 }
452 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800453 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800454 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456 }
457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 private final void logOutlier(long duration) {
460 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800461 String dischargeThresholdString = Settings.Secure.getString(cr,
462 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
463 String durationThresholdString = Settings.Secure.getString(cr,
464 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 if (dischargeThresholdString != null && durationThresholdString != null) {
467 try {
468 long durationThreshold = Long.parseLong(durationThresholdString);
469 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
472 // If the discharge cycle is bad enough we want to know about it.
473 logBatteryStats();
474 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800475 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800477 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 (mDischargeStartLevel - mBatteryLevel));
479 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800480 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 durationThresholdString + " or " + dischargeThresholdString);
482 return;
483 }
484 }
485 }
486
487 private final int getIcon(int level) {
488 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
489 return com.android.internal.R.drawable.stat_sys_battery_charge;
Joe Onorato794be402010-11-21 19:22:25 -0800490 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 return com.android.internal.R.drawable.stat_sys_battery;
Joe Onorato794be402010-11-21 19:22:25 -0800492 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
493 || mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
494 if (isPowered() && mBatteryLevel >= 100) {
495 return com.android.internal.R.drawable.stat_sys_battery_charge;
496 } else {
497 return com.android.internal.R.drawable.stat_sys_battery;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 } else {
500 return com.android.internal.R.drawable.stat_sys_battery_unknown;
501 }
502 }
503
504 @Override
505 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
506 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
507 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 pw.println("Permission Denial: can't dump Battery service from from pid="
510 + Binder.getCallingPid()
511 + ", uid=" + Binder.getCallingUid());
512 return;
513 }
514
Mike Lockwoode8174042011-08-16 12:53:43 -0700515 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700516 synchronized (this) {
517 pw.println("Current Battery Service state:");
518 pw.println(" AC powered: " + mAcOnline);
519 pw.println(" USB powered: " + mUsbOnline);
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700520 pw.println(" Wireless powered: " + mWirelessOnline);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700521 pw.println(" status: " + mBatteryStatus);
522 pw.println(" health: " + mBatteryHealth);
523 pw.println(" present: " + mBatteryPresent);
524 pw.println(" level: " + mBatteryLevel);
525 pw.println(" scale: " + BATTERY_SCALE);
526 pw.println(" voltage:" + mBatteryVoltage);
527 pw.println(" temperature: " + mBatteryTemperature);
528 pw.println(" technology: " + mBatteryTechnology);
529 }
530 } else if (false) {
531 // DO NOT SUBMIT WITH THIS TURNED ON
532 if (args.length == 3 && "set".equals(args[0])) {
533 String key = args[1];
534 String value = args[2];
535 try {
536 boolean update = true;
537 if ("ac".equals(key)) {
538 mAcOnline = Integer.parseInt(value) != 0;
539 } else if ("usb".equals(key)) {
540 mUsbOnline = Integer.parseInt(value) != 0;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700541 } else if ("wireless".equals(key)) {
542 mWirelessOnline = Integer.parseInt(value) != 0;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700543 } else if ("status".equals(key)) {
544 mBatteryStatus = Integer.parseInt(value);
545 } else if ("level".equals(key)) {
546 mBatteryLevel = Integer.parseInt(value);
547 } else if ("invalid".equals(key)) {
548 mInvalidCharger = Integer.parseInt(value);
549 } else {
550 update = false;
551 }
552 if (update) {
553 processValues();
554 }
555 } catch (NumberFormatException ex) {
556 pw.println("Bad value: " + value);
557 }
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700561
562 class Led {
563 private LightsService mLightsService;
564 private LightsService.Light mBatteryLight;
565
566 private int mBatteryLowARGB;
567 private int mBatteryMediumARGB;
568 private int mBatteryFullARGB;
569 private int mBatteryLedOn;
570 private int mBatteryLedOff;
571
572 private boolean mBatteryCharging;
573 private boolean mBatteryLow;
574 private boolean mBatteryFull;
575
576 Led(Context context, LightsService lights) {
577 mLightsService = lights;
578 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
579
580 mBatteryLowARGB = mContext.getResources().getInteger(
581 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
582 mBatteryMediumARGB = mContext.getResources().getInteger(
583 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
584 mBatteryFullARGB = mContext.getResources().getInteger(
585 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
586 mBatteryLedOn = mContext.getResources().getInteger(
587 com.android.internal.R.integer.config_notificationsBatteryLedOn);
588 mBatteryLedOff = mContext.getResources().getInteger(
589 com.android.internal.R.integer.config_notificationsBatteryLedOff);
590 }
591
592 /**
593 * Synchronize on BatteryService.
594 */
595 void updateLightsLocked() {
596 final int level = mBatteryLevel;
597 final int status = mBatteryStatus;
598 if (level < mLowBatteryWarningLevel) {
599 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
600 // Solid red when battery is charging
601 mBatteryLight.setColor(mBatteryLowARGB);
602 } else {
603 // Flash red when battery is low and not charging
604 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
605 mBatteryLedOn, mBatteryLedOff);
606 }
607 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
608 || status == BatteryManager.BATTERY_STATUS_FULL) {
609 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
610 // Solid green when full or charging and nearly full
611 mBatteryLight.setColor(mBatteryFullARGB);
612 } else {
613 // Solid orange when charging and halfway full
614 mBatteryLight.setColor(mBatteryMediumARGB);
615 }
616 } else {
617 // No lights if not charging and not low
618 mBatteryLight.turnOff();
619 }
620 }
621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622}