blob: 6ae16a481dcd92e4b04db918f303a90554b52285 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.provider.Settings;
37import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080038import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import java.io.File;
41import java.io.FileDescriptor;
42import java.io.FileInputStream;
43import java.io.FileOutputStream;
44import java.io.IOException;
45import java.io.PrintWriter;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070046import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49/**
50 * <p>BatteryService monitors the charging status, and charge level of the device
51 * battery. When these values change this service broadcasts the new values
52 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
53 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
54 * BATTERY_CHANGED} action.</p>
55 * <p>The new values are stored in the Intent data and can be retrieved by
56 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
57 * following keys:</p>
58 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
59 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
60 * <p>&quot;status&quot; - String, the current charging status.<br />
61 * <p>&quot;health&quot; - String, the current battery health.<br />
62 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
63 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
64 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
65 * into an AC power adapter; 2 if plugged in via USB.</p>
66 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
67 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
68 * a degree Centigrade</p>
69 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
70 */
Jeff Brown4f8ecd82012-06-18 18:29:13 -070071public class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private static final boolean LOCAL_LOGV = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 static final int BATTERY_SCALE = 100; // battery capacity is a percentage
77
78 // Used locally for determining when to make a last ditch effort to log
79 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070080 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Dianne Hackborn6447ca32009-04-07 19:50:08 -070083 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo";
Doug Zongkerab5c49c2009-12-04 10:31:43 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String DUMPSYS_DATA_PATH = "/data/system/";
87
88 // This should probably be exposed in the API, though it's not critical
89 private static final int BATTERY_PLUGGED_NONE = 0;
90
91 private final Context mContext;
92 private final IBatteryStats mBatteryStats;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private boolean mAcOnline;
95 private boolean mUsbOnline;
Brian Muramatsu37a37f42012-08-14 15:21:02 -070096 private boolean mWirelessOnline;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private int mBatteryStatus;
98 private int mBatteryHealth;
99 private boolean mBatteryPresent;
100 private int mBatteryLevel;
101 private int mBatteryVoltage;
102 private int mBatteryTemperature;
103 private String mBatteryTechnology;
104 private boolean mBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700105 private int mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 private int mLastBatteryStatus;
108 private int mLastBatteryHealth;
109 private boolean mLastBatteryPresent;
110 private int mLastBatteryLevel;
111 private int mLastBatteryVoltage;
112 private int mLastBatteryTemperature;
113 private boolean mLastBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700114 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400115
116 private int mLowBatteryWarningLevel;
117 private int mLowBatteryCloseWarningLevel;
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private int mPlugType;
120 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 private long mDischargeStartTime;
123 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800124
Joe Onoratode1b3592010-10-25 20:36:47 -0700125 private Led mLed;
126
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700127 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
Joe Onoratode1b3592010-10-25 20:36:47 -0700129 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700131 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mBatteryStats = BatteryStatsService.getService();
133
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700134 mCriticalBatteryLevel = mContext.getResources().getInteger(
135 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400136 mLowBatteryWarningLevel = mContext.getResources().getInteger(
137 com.android.internal.R.integer.config_lowBatteryWarningLevel);
138 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
139 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
140
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400141 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
142
143 // watch for invalid charger messages if the invalid_charger switch exists
144 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
145 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 // set initial status
149 update();
150 }
151
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700152 public final boolean isPowered() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 // 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 -0700154 return (mAcOnline || mUsbOnline || mWirelessOnline
155 || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
157
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700158 public final boolean isPowered(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // assume we are powered if battery state is unknown so
160 // the "stay on while plugged in" option will work.
161 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
162 return true;
163 }
164 if (plugTypeSet == 0) {
165 return false;
166 }
167 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700168 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
170 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700171 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
173 }
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700174 if (mWirelessOnline) {
175 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_WIRELESS;
176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 return (plugTypeSet & plugTypeBit) != 0;
178 }
179
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700180 public final int getPlugType() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 return mPlugType;
182 }
183
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400184 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 @Override
186 public void onUEvent(UEventObserver.UEvent event) {
187 update();
188 }
189 };
190
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400191 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
192 @Override
193 public void onUEvent(UEventObserver.UEvent event) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700194 int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400195 if (mInvalidCharger != invalidCharger) {
196 mInvalidCharger = invalidCharger;
197 update();
198 }
199 }
200 };
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 // returns battery level as a percentage
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700203 public final int getBatteryLevel() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 return mBatteryLevel;
205 }
206
Mike Lockwood07a500f2009-08-12 09:56:44 -0400207 void systemReady() {
208 // check our power situation now that it is safe to display the shutdown dialog.
209 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700210 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400211 }
212
213 private final void shutdownIfNoPower() {
214 // shut down gracefully if our battery is critically low and we are not powered.
215 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700216 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400217 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
218 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
219 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
220 mContext.startActivity(intent);
221 }
222 }
223
Eric Olsen6a362a92010-03-26 15:38:41 -0700224 private final void shutdownIfOverTemp() {
225 // shut down gracefully if temperature is too high (> 68.0C)
226 // wait until the system has booted before attempting to display the shutdown dialog.
227 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
228 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
229 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
230 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
231 mContext.startActivity(intent);
232 }
233 }
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 private native void native_update();
236
237 private synchronized final void update() {
238 native_update();
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700239 processValues();
240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700242 private void processValues() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700243 boolean logOutlier = false;
244 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700245
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700246 mBatteryLevelCritical = mBatteryLevel <= mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 if (mAcOnline) {
248 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
249 } else if (mUsbOnline) {
250 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700251 } else if (mWirelessOnline) {
252 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 } else {
254 mPlugType = BATTERY_PLUGGED_NONE;
255 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700256
257 // Let the battery stats keep track of the current level.
258 try {
259 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
260 mPlugType, mBatteryLevel, mBatteryTemperature,
261 mBatteryVoltage);
262 } catch (RemoteException e) {
263 // Should never happen.
264 }
265
266 shutdownIfNoPower();
267 shutdownIfOverTemp();
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 if (mBatteryStatus != mLastBatteryStatus ||
270 mBatteryHealth != mLastBatteryHealth ||
271 mBatteryPresent != mLastBatteryPresent ||
272 mBatteryLevel != mLastBatteryLevel ||
273 mPlugType != mLastPlugType ||
274 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400275 mBatteryTemperature != mLastBatteryTemperature ||
276 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 if (mPlugType != mLastPlugType) {
279 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
280 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 // There's no value in this data unless we've discharged at least once and the
283 // battery level has changed; so don't log until it does.
284 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700285 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
286 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 mDischargeStartLevel, mBatteryLevel);
289 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 }
292 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
293 // charging -> discharging or we just powered up
294 mDischargeStartTime = SystemClock.elapsedRealtime();
295 mDischargeStartLevel = mBatteryLevel;
296 }
297 }
298 if (mBatteryStatus != mLastBatteryStatus ||
299 mBatteryHealth != mLastBatteryHealth ||
300 mBatteryPresent != mLastBatteryPresent ||
301 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
304 mPlugType, mBatteryTechnology);
305 }
306 if (mBatteryLevel != mLastBatteryLevel ||
307 mBatteryVoltage != mLastBatteryVoltage ||
308 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
311 }
312 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
313 mPlugType == BATTERY_PLUGGED_NONE) {
314 // We want to make sure we log discharge cycle outliers
315 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700316 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
317 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700320 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
321 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
322
323 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400324 * - is just un-plugged (previously was plugged) and battery level is
325 * less than or equal to WARNING, or
326 * - is not plugged and battery level falls to WARNING boundary
327 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700328 */
329 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700330 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
331 && mBatteryLevel <= mLowBatteryWarningLevel
332 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700334 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
Christopher Tate06ba5542009-04-09 16:03:56 -0700336 // Separate broadcast is sent for power connected / not connected
337 // since the standard intent will not wake any applications and some
338 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700339 Intent statusIntent = new Intent();
340 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700341 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700342 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
343 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700344 }
345 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700346 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
347 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700348 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200349
Mihai Predaa82842f2009-04-29 15:05:56 +0200350 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700351 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700352 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
353 mContext.sendBroadcast(statusIntent);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400354 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700355 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700356 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
357 mContext.sendBroadcast(statusIntent);
Mihai Predaa82842f2009-04-29 15:05:56 +0200358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
Joe Onoratode1b3592010-10-25 20:36:47 -0700360 // Update the battery LED
361 mLed.updateLightsLocked();
362
The Android Open Source Project10592532009-03-18 17:39:46 -0700363 // This needs to be done after sendIntent() so that we get the lastest battery stats.
364 if (logOutlier && dischargeDuration != 0) {
365 logOutlier(dischargeDuration);
366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700368 mLastBatteryStatus = mBatteryStatus;
369 mLastBatteryHealth = mBatteryHealth;
370 mLastBatteryPresent = mBatteryPresent;
371 mLastBatteryLevel = mBatteryLevel;
372 mLastPlugType = mPlugType;
373 mLastBatteryVoltage = mBatteryVoltage;
374 mLastBatteryTemperature = mBatteryTemperature;
375 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400376 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
379
380 private final void sendIntent() {
381 // Pack up the values and broadcast them to everyone
382 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800383 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
384 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 int icon = getIcon(mBatteryLevel);
387
Dianne Hackbornedd93162009-09-19 14:03:05 -0700388 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
389 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
390 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
391 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
392 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
393 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
394 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
395 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
396 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
397 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400398 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Joe Onorato53859742011-04-06 18:27:43 -0700400 if (false) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700401 Slog.d(TAG, "level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
403 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 " voltage: " + mBatteryVoltage +
405 " temperature: " + mBatteryTemperature +
406 " technology: " + mBatteryTechnology +
407 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700408 " Wireless powered:" + mWirelessOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400409 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411
412 ActivityManagerNative.broadcastStickyIntent(intent, null);
413 }
414
415 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800417 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
Dan Egnor18e93962010-02-10 19:27:58 -0800419 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
420 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
421
422 File dumpFile = null;
423 FileOutputStream dumpStream = null;
424 try {
425 // dump the service to a file
426 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
427 dumpStream = new FileOutputStream(dumpFile);
428 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700429 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800430
431 // add dump file to drop box
432 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
433 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800434 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800435 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800436 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800437 } finally {
438 // make sure we clean up
439 if (dumpStream != null) {
440 try {
441 dumpStream.close();
442 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800443 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
Dan Egnor18e93962010-02-10 19:27:58 -0800445 }
446 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800447 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800448 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 private final void logOutlier(long duration) {
454 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800455 String dischargeThresholdString = Settings.Secure.getString(cr,
456 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
457 String durationThresholdString = Settings.Secure.getString(cr,
458 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 if (dischargeThresholdString != null && durationThresholdString != null) {
461 try {
462 long durationThreshold = Long.parseLong(durationThresholdString);
463 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
466 // If the discharge cycle is bad enough we want to know about it.
467 logBatteryStats();
468 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800469 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800471 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 (mDischargeStartLevel - mBatteryLevel));
473 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800474 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 durationThresholdString + " or " + dischargeThresholdString);
476 return;
477 }
478 }
479 }
480
481 private final int getIcon(int level) {
482 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
483 return com.android.internal.R.drawable.stat_sys_battery_charge;
Joe Onorato794be402010-11-21 19:22:25 -0800484 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 return com.android.internal.R.drawable.stat_sys_battery;
Joe Onorato794be402010-11-21 19:22:25 -0800486 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
487 || mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
488 if (isPowered() && mBatteryLevel >= 100) {
489 return com.android.internal.R.drawable.stat_sys_battery_charge;
490 } else {
491 return com.android.internal.R.drawable.stat_sys_battery;
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 } else {
494 return com.android.internal.R.drawable.stat_sys_battery_unknown;
495 }
496 }
497
498 @Override
499 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
500 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
501 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 pw.println("Permission Denial: can't dump Battery service from from pid="
504 + Binder.getCallingPid()
505 + ", uid=" + Binder.getCallingUid());
506 return;
507 }
508
Mike Lockwoode8174042011-08-16 12:53:43 -0700509 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700510 synchronized (this) {
511 pw.println("Current Battery Service state:");
512 pw.println(" AC powered: " + mAcOnline);
513 pw.println(" USB powered: " + mUsbOnline);
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700514 pw.println(" Wireless powered: " + mWirelessOnline);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700515 pw.println(" status: " + mBatteryStatus);
516 pw.println(" health: " + mBatteryHealth);
517 pw.println(" present: " + mBatteryPresent);
518 pw.println(" level: " + mBatteryLevel);
519 pw.println(" scale: " + BATTERY_SCALE);
520 pw.println(" voltage:" + mBatteryVoltage);
521 pw.println(" temperature: " + mBatteryTemperature);
522 pw.println(" technology: " + mBatteryTechnology);
523 }
524 } else if (false) {
525 // DO NOT SUBMIT WITH THIS TURNED ON
526 if (args.length == 3 && "set".equals(args[0])) {
527 String key = args[1];
528 String value = args[2];
529 try {
530 boolean update = true;
531 if ("ac".equals(key)) {
532 mAcOnline = Integer.parseInt(value) != 0;
533 } else if ("usb".equals(key)) {
534 mUsbOnline = Integer.parseInt(value) != 0;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700535 } else if ("wireless".equals(key)) {
536 mWirelessOnline = Integer.parseInt(value) != 0;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700537 } else if ("status".equals(key)) {
538 mBatteryStatus = Integer.parseInt(value);
539 } else if ("level".equals(key)) {
540 mBatteryLevel = Integer.parseInt(value);
541 } else if ("invalid".equals(key)) {
542 mInvalidCharger = Integer.parseInt(value);
543 } else {
544 update = false;
545 }
546 if (update) {
547 processValues();
548 }
549 } catch (NumberFormatException ex) {
550 pw.println("Bad value: " + value);
551 }
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700555
556 class Led {
557 private LightsService mLightsService;
558 private LightsService.Light mBatteryLight;
559
560 private int mBatteryLowARGB;
561 private int mBatteryMediumARGB;
562 private int mBatteryFullARGB;
563 private int mBatteryLedOn;
564 private int mBatteryLedOff;
565
566 private boolean mBatteryCharging;
567 private boolean mBatteryLow;
568 private boolean mBatteryFull;
569
570 Led(Context context, LightsService lights) {
571 mLightsService = lights;
572 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
573
574 mBatteryLowARGB = mContext.getResources().getInteger(
575 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
576 mBatteryMediumARGB = mContext.getResources().getInteger(
577 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
578 mBatteryFullARGB = mContext.getResources().getInteger(
579 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
580 mBatteryLedOn = mContext.getResources().getInteger(
581 com.android.internal.R.integer.config_notificationsBatteryLedOn);
582 mBatteryLedOff = mContext.getResources().getInteger(
583 com.android.internal.R.integer.config_notificationsBatteryLedOff);
584 }
585
586 /**
587 * Synchronize on BatteryService.
588 */
589 void updateLightsLocked() {
590 final int level = mBatteryLevel;
591 final int status = mBatteryStatus;
592 if (level < mLowBatteryWarningLevel) {
593 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
594 // Solid red when battery is charging
595 mBatteryLight.setColor(mBatteryLowARGB);
596 } else {
597 // Flash red when battery is low and not charging
598 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
599 mBatteryLedOn, mBatteryLedOff);
600 }
601 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
602 || status == BatteryManager.BATTERY_STATUS_FULL) {
603 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
604 // Solid green when full or charging and nearly full
605 mBatteryLight.setColor(mBatteryFullARGB);
606 } else {
607 // Solid orange when charging and halfway full
608 mBatteryLight.setColor(mBatteryMediumARGB);
609 }
610 } else {
611 // No lights if not charging and not low
612 mBatteryLight.turnOff();
613 }
614 }
615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616}