blob: 40883bddf4996c774e5996515dfefcd9cf3defbd [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 */
71class BatteryService extends Binder {
72 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;
96 private int mBatteryStatus;
97 private int mBatteryHealth;
98 private boolean mBatteryPresent;
99 private int mBatteryLevel;
100 private int mBatteryVoltage;
101 private int mBatteryTemperature;
102 private String mBatteryTechnology;
103 private boolean mBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700104 private int mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106 private int mLastBatteryStatus;
107 private int mLastBatteryHealth;
108 private boolean mLastBatteryPresent;
109 private int mLastBatteryLevel;
110 private int mLastBatteryVoltage;
111 private int mLastBatteryTemperature;
112 private boolean mLastBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700113 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400114
115 private int mLowBatteryWarningLevel;
116 private int mLowBatteryCloseWarningLevel;
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private int mPlugType;
119 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 private long mDischargeStartTime;
122 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800123
Joe Onoratode1b3592010-10-25 20:36:47 -0700124 private Led mLed;
125
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700126 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800127
Joe Onoratode1b3592010-10-25 20:36:47 -0700128 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700130 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 mBatteryStats = BatteryStatsService.getService();
132
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700133 mCriticalBatteryLevel = mContext.getResources().getInteger(
134 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400135 mLowBatteryWarningLevel = mContext.getResources().getInteger(
136 com.android.internal.R.integer.config_lowBatteryWarningLevel);
137 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
138 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
139
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400140 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
141
142 // watch for invalid charger messages if the invalid_charger switch exists
143 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
144 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
147 // set initial status
148 update();
149 }
150
151 final boolean isPowered() {
152 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700153 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 }
155
156 final boolean isPowered(int plugTypeSet) {
157 // assume we are powered if battery state is unknown so
158 // the "stay on while plugged in" option will work.
159 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
160 return true;
161 }
162 if (plugTypeSet == 0) {
163 return false;
164 }
165 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700166 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
168 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700169 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
171 }
172 return (plugTypeSet & plugTypeBit) != 0;
173 }
174
175 final int getPlugType() {
176 return mPlugType;
177 }
178
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400179 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 @Override
181 public void onUEvent(UEventObserver.UEvent event) {
182 update();
183 }
184 };
185
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400186 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
187 @Override
188 public void onUEvent(UEventObserver.UEvent event) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700189 int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400190 if (mInvalidCharger != invalidCharger) {
191 mInvalidCharger = invalidCharger;
192 update();
193 }
194 }
195 };
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 // returns battery level as a percentage
198 final int getBatteryLevel() {
199 return mBatteryLevel;
200 }
201
Mike Lockwood07a500f2009-08-12 09:56:44 -0400202 void systemReady() {
203 // check our power situation now that it is safe to display the shutdown dialog.
204 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700205 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400206 }
207
208 private final void shutdownIfNoPower() {
209 // shut down gracefully if our battery is critically low and we are not powered.
210 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700211 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400212 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
213 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
214 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
215 mContext.startActivity(intent);
216 }
217 }
218
Eric Olsen6a362a92010-03-26 15:38:41 -0700219 private final void shutdownIfOverTemp() {
220 // shut down gracefully if temperature is too high (> 68.0C)
221 // wait until the system has booted before attempting to display the shutdown dialog.
222 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
223 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 private native void native_update();
231
232 private synchronized final void update() {
233 native_update();
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700234 processValues();
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700237 private void processValues() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700238 boolean logOutlier = false;
239 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700240
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700241 mBatteryLevelCritical = mBatteryLevel <= mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 if (mAcOnline) {
243 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
244 } else if (mUsbOnline) {
245 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
246 } else {
247 mPlugType = BATTERY_PLUGGED_NONE;
248 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700249
250 // Let the battery stats keep track of the current level.
251 try {
252 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
253 mPlugType, mBatteryLevel, mBatteryTemperature,
254 mBatteryVoltage);
255 } catch (RemoteException e) {
256 // Should never happen.
257 }
258
259 shutdownIfNoPower();
260 shutdownIfOverTemp();
261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 if (mBatteryStatus != mLastBatteryStatus ||
263 mBatteryHealth != mLastBatteryHealth ||
264 mBatteryPresent != mLastBatteryPresent ||
265 mBatteryLevel != mLastBatteryLevel ||
266 mPlugType != mLastPlugType ||
267 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400268 mBatteryTemperature != mLastBatteryTemperature ||
269 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 if (mPlugType != mLastPlugType) {
272 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
273 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 // There's no value in this data unless we've discharged at least once and the
276 // battery level has changed; so don't log until it does.
277 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700278 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
279 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 mDischargeStartLevel, mBatteryLevel);
282 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
286 // charging -> discharging or we just powered up
287 mDischargeStartTime = SystemClock.elapsedRealtime();
288 mDischargeStartLevel = mBatteryLevel;
289 }
290 }
291 if (mBatteryStatus != mLastBatteryStatus ||
292 mBatteryHealth != mLastBatteryHealth ||
293 mBatteryPresent != mLastBatteryPresent ||
294 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
297 mPlugType, mBatteryTechnology);
298 }
299 if (mBatteryLevel != mLastBatteryLevel ||
300 mBatteryVoltage != mLastBatteryVoltage ||
301 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
304 }
305 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
306 mPlugType == BATTERY_PLUGGED_NONE) {
307 // We want to make sure we log discharge cycle outliers
308 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700309 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
310 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700313 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
314 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
315
316 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400317 * - is just un-plugged (previously was plugged) and battery level is
318 * less than or equal to WARNING, or
319 * - is not plugged and battery level falls to WARNING boundary
320 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700321 */
322 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700323 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
324 && mBatteryLevel <= mLowBatteryWarningLevel
325 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700327 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
Christopher Tate06ba5542009-04-09 16:03:56 -0700329 // Separate broadcast is sent for power connected / not connected
330 // since the standard intent will not wake any applications and some
331 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700332 Intent statusIntent = new Intent();
333 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700334 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700335 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
336 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700337 }
338 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700339 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
340 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700341 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200342
Mihai Predaa82842f2009-04-29 15:05:56 +0200343 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700344 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700345 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
346 mContext.sendBroadcast(statusIntent);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400347 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700348 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700349 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
350 mContext.sendBroadcast(statusIntent);
Mihai Predaa82842f2009-04-29 15:05:56 +0200351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Joe Onoratode1b3592010-10-25 20:36:47 -0700353 // Update the battery LED
354 mLed.updateLightsLocked();
355
The Android Open Source Project10592532009-03-18 17:39:46 -0700356 // This needs to be done after sendIntent() so that we get the lastest battery stats.
357 if (logOutlier && dischargeDuration != 0) {
358 logOutlier(dischargeDuration);
359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700361 mLastBatteryStatus = mBatteryStatus;
362 mLastBatteryHealth = mBatteryHealth;
363 mLastBatteryPresent = mBatteryPresent;
364 mLastBatteryLevel = mBatteryLevel;
365 mLastPlugType = mPlugType;
366 mLastBatteryVoltage = mBatteryVoltage;
367 mLastBatteryTemperature = mBatteryTemperature;
368 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400369 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371 }
372
373 private final void sendIntent() {
374 // Pack up the values and broadcast them to everyone
375 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800376 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
377 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 int icon = getIcon(mBatteryLevel);
380
Dianne Hackbornedd93162009-09-19 14:03:05 -0700381 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
382 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
383 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
384 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
385 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
386 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
387 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
388 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
389 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
390 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400391 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700393 if (true) {
394 Slog.d(TAG, "level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
396 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 " voltage: " + mBatteryVoltage +
398 " temperature: " + mBatteryTemperature +
399 " technology: " + mBatteryTechnology +
400 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400401 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
403
404 ActivityManagerNative.broadcastStickyIntent(intent, null);
405 }
406
407 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800409 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410
Dan Egnor18e93962010-02-10 19:27:58 -0800411 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
412 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
413
414 File dumpFile = null;
415 FileOutputStream dumpStream = null;
416 try {
417 // dump the service to a file
418 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
419 dumpStream = new FileOutputStream(dumpFile);
420 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700421 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800422
423 // add dump file to drop box
424 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
425 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800426 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800427 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800428 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800429 } finally {
430 // make sure we clean up
431 if (dumpStream != null) {
432 try {
433 dumpStream.close();
434 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800435 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
Dan Egnor18e93962010-02-10 19:27:58 -0800437 }
438 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800439 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800440 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 private final void logOutlier(long duration) {
446 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800447 String dischargeThresholdString = Settings.Secure.getString(cr,
448 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
449 String durationThresholdString = Settings.Secure.getString(cr,
450 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 if (dischargeThresholdString != null && durationThresholdString != null) {
453 try {
454 long durationThreshold = Long.parseLong(durationThresholdString);
455 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
458 // If the discharge cycle is bad enough we want to know about it.
459 logBatteryStats();
460 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800461 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800463 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 (mDischargeStartLevel - mBatteryLevel));
465 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800466 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 durationThresholdString + " or " + dischargeThresholdString);
468 return;
469 }
470 }
471 }
472
473 private final int getIcon(int level) {
474 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
475 return com.android.internal.R.drawable.stat_sys_battery_charge;
476 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING ||
477 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
478 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
479 return com.android.internal.R.drawable.stat_sys_battery;
480 } else {
481 return com.android.internal.R.drawable.stat_sys_battery_unknown;
482 }
483 }
484
485 @Override
486 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
487 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
488 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 pw.println("Permission Denial: can't dump Battery service from from pid="
491 + Binder.getCallingPid()
492 + ", uid=" + Binder.getCallingUid());
493 return;
494 }
495
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700496 if (args == null || args.length == 0) {
497 synchronized (this) {
498 pw.println("Current Battery Service state:");
499 pw.println(" AC powered: " + mAcOnline);
500 pw.println(" USB powered: " + mUsbOnline);
501 pw.println(" status: " + mBatteryStatus);
502 pw.println(" health: " + mBatteryHealth);
503 pw.println(" present: " + mBatteryPresent);
504 pw.println(" level: " + mBatteryLevel);
505 pw.println(" scale: " + BATTERY_SCALE);
506 pw.println(" voltage:" + mBatteryVoltage);
507 pw.println(" temperature: " + mBatteryTemperature);
508 pw.println(" technology: " + mBatteryTechnology);
509 }
510 } else if (false) {
511 // DO NOT SUBMIT WITH THIS TURNED ON
512 if (args.length == 3 && "set".equals(args[0])) {
513 String key = args[1];
514 String value = args[2];
515 try {
516 boolean update = true;
517 if ("ac".equals(key)) {
518 mAcOnline = Integer.parseInt(value) != 0;
519 } else if ("usb".equals(key)) {
520 mUsbOnline = Integer.parseInt(value) != 0;
521 } else if ("status".equals(key)) {
522 mBatteryStatus = Integer.parseInt(value);
523 } else if ("level".equals(key)) {
524 mBatteryLevel = Integer.parseInt(value);
525 } else if ("invalid".equals(key)) {
526 mInvalidCharger = Integer.parseInt(value);
527 } else {
528 update = false;
529 }
530 if (update) {
531 processValues();
532 }
533 } catch (NumberFormatException ex) {
534 pw.println("Bad value: " + value);
535 }
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700539
540 class Led {
541 private LightsService mLightsService;
542 private LightsService.Light mBatteryLight;
543
544 private int mBatteryLowARGB;
545 private int mBatteryMediumARGB;
546 private int mBatteryFullARGB;
547 private int mBatteryLedOn;
548 private int mBatteryLedOff;
549
550 private boolean mBatteryCharging;
551 private boolean mBatteryLow;
552 private boolean mBatteryFull;
553
554 Led(Context context, LightsService lights) {
555 mLightsService = lights;
556 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
557
558 mBatteryLowARGB = mContext.getResources().getInteger(
559 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
560 mBatteryMediumARGB = mContext.getResources().getInteger(
561 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
562 mBatteryFullARGB = mContext.getResources().getInteger(
563 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
564 mBatteryLedOn = mContext.getResources().getInteger(
565 com.android.internal.R.integer.config_notificationsBatteryLedOn);
566 mBatteryLedOff = mContext.getResources().getInteger(
567 com.android.internal.R.integer.config_notificationsBatteryLedOff);
568 }
569
570 /**
571 * Synchronize on BatteryService.
572 */
573 void updateLightsLocked() {
574 final int level = mBatteryLevel;
575 final int status = mBatteryStatus;
576 if (level < mLowBatteryWarningLevel) {
577 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
578 // Solid red when battery is charging
579 mBatteryLight.setColor(mBatteryLowARGB);
580 } else {
581 // Flash red when battery is low and not charging
582 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
583 mBatteryLedOn, mBatteryLedOff);
584 }
585 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
586 || status == BatteryManager.BATTERY_STATUS_FULL) {
587 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
588 // Solid green when full or charging and nearly full
589 mBatteryLight.setColor(mBatteryFullARGB);
590 } else {
591 // Solid orange when charging and halfway full
592 mBatteryLight.setColor(mBatteryMediumARGB);
593 }
594 } else {
595 // No lights if not charging and not low
596 mBatteryLight.turnOff();
597 }
598 }
599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600}
Joe Onoratode1b3592010-10-25 20:36:47 -0700601