blob: 23bd5c8a0fa90004408184b2f0aea1766e8b0266 [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;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48/**
49 * <p>BatteryService monitors the charging status, and charge level of the device
50 * battery. When these values change this service broadcasts the new values
51 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
52 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
53 * BATTERY_CHANGED} action.</p>
54 * <p>The new values are stored in the Intent data and can be retrieved by
55 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
56 * following keys:</p>
57 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
58 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
59 * <p>&quot;status&quot; - String, the current charging status.<br />
60 * <p>&quot;health&quot; - String, the current battery health.<br />
61 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
62 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
63 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
64 * into an AC power adapter; 2 if plugged in via USB.</p>
65 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
66 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
67 * a degree Centigrade</p>
68 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
69 */
70class BatteryService extends Binder {
71 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static final boolean LOCAL_LOGV = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 static final int BATTERY_SCALE = 100; // battery capacity is a percentage
76
77 // Used locally for determining when to make a last ditch effort to log
78 // discharge stats before the device dies.
Doug Zongkerab5c49c2009-12-04 10:31:43 -080079 private static final int CRITICAL_BATTERY_LEVEL = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Dianne Hackborn6447ca32009-04-07 19:50:08 -070082 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo";
Doug Zongkerab5c49c2009-12-04 10:31:43 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final String DUMPSYS_DATA_PATH = "/data/system/";
86
87 // This should probably be exposed in the API, though it's not critical
88 private static final int BATTERY_PLUGGED_NONE = 0;
89
90 private final Context mContext;
91 private final IBatteryStats mBatteryStats;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private boolean mAcOnline;
94 private boolean mUsbOnline;
95 private int mBatteryStatus;
96 private int mBatteryHealth;
97 private boolean mBatteryPresent;
98 private int mBatteryLevel;
99 private int mBatteryVoltage;
100 private int mBatteryTemperature;
101 private String mBatteryTechnology;
102 private boolean mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400103 private boolean mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105 private int mLastBatteryStatus;
106 private int mLastBatteryHealth;
107 private boolean mLastBatteryPresent;
108 private int mLastBatteryLevel;
109 private int mLastBatteryVoltage;
110 private int mLastBatteryTemperature;
111 private boolean mLastBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400112 private boolean mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400113
114 private int mLowBatteryWarningLevel;
115 private int mLowBatteryCloseWarningLevel;
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private int mPlugType;
118 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 private long mDischargeStartTime;
121 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
Joe Onoratode1b3592010-10-25 20:36:47 -0700123 private Led mLed;
124
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700125 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800126
Joe Onoratode1b3592010-10-25 20:36:47 -0700127 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700129 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 mBatteryStats = BatteryStatsService.getService();
131
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400132 mLowBatteryWarningLevel = mContext.getResources().getInteger(
133 com.android.internal.R.integer.config_lowBatteryWarningLevel);
134 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
135 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
136
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400137 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
138
139 // watch for invalid charger messages if the invalid_charger switch exists
140 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
141 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
144 // set initial status
145 update();
146 }
147
148 final boolean isPowered() {
149 // 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 -0700150 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 }
152
153 final boolean isPowered(int plugTypeSet) {
154 // assume we are powered if battery state is unknown so
155 // the "stay on while plugged in" option will work.
156 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
157 return true;
158 }
159 if (plugTypeSet == 0) {
160 return false;
161 }
162 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700163 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
165 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700166 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
168 }
169 return (plugTypeSet & plugTypeBit) != 0;
170 }
171
172 final int getPlugType() {
173 return mPlugType;
174 }
175
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400176 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 @Override
178 public void onUEvent(UEventObserver.UEvent event) {
179 update();
180 }
181 };
182
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400183 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
184 @Override
185 public void onUEvent(UEventObserver.UEvent event) {
186 boolean invalidCharger = "1".equals(event.get("SWITCH_STATE"));
187 if (mInvalidCharger != invalidCharger) {
188 mInvalidCharger = invalidCharger;
189 update();
190 }
191 }
192 };
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 // returns battery level as a percentage
195 final int getBatteryLevel() {
196 return mBatteryLevel;
197 }
198
Mike Lockwood07a500f2009-08-12 09:56:44 -0400199 void systemReady() {
200 // check our power situation now that it is safe to display the shutdown dialog.
201 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700202 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400203 }
204
205 private final void shutdownIfNoPower() {
206 // shut down gracefully if our battery is critically low and we are not powered.
207 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700208 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400209 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
210 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
211 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
212 mContext.startActivity(intent);
213 }
214 }
215
Eric Olsen6a362a92010-03-26 15:38:41 -0700216 private final void shutdownIfOverTemp() {
217 // shut down gracefully if temperature is too high (> 68.0C)
218 // wait until the system has booted before attempting to display the shutdown dialog.
219 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
220 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
221 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
222 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
223 mContext.startActivity(intent);
224 }
225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 private native void native_update();
228
229 private synchronized final void update() {
230 native_update();
231
The Android Open Source Project10592532009-03-18 17:39:46 -0700232 boolean logOutlier = false;
233 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 mBatteryLevelCritical = mBatteryLevel <= CRITICAL_BATTERY_LEVEL;
236 if (mAcOnline) {
237 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
238 } else if (mUsbOnline) {
239 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
240 } else {
241 mPlugType = BATTERY_PLUGGED_NONE;
242 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700243
244 // Let the battery stats keep track of the current level.
245 try {
246 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
247 mPlugType, mBatteryLevel, mBatteryTemperature,
248 mBatteryVoltage);
249 } catch (RemoteException e) {
250 // Should never happen.
251 }
252
253 shutdownIfNoPower();
254 shutdownIfOverTemp();
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 if (mBatteryStatus != mLastBatteryStatus ||
257 mBatteryHealth != mLastBatteryHealth ||
258 mBatteryPresent != mLastBatteryPresent ||
259 mBatteryLevel != mLastBatteryLevel ||
260 mPlugType != mLastPlugType ||
261 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400262 mBatteryTemperature != mLastBatteryTemperature ||
263 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 if (mPlugType != mLastPlugType) {
266 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
267 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 // There's no value in this data unless we've discharged at least once and the
270 // battery level has changed; so don't log until it does.
271 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700272 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
273 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mDischargeStartLevel, mBatteryLevel);
276 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
280 // charging -> discharging or we just powered up
281 mDischargeStartTime = SystemClock.elapsedRealtime();
282 mDischargeStartLevel = mBatteryLevel;
283 }
284 }
285 if (mBatteryStatus != mLastBatteryStatus ||
286 mBatteryHealth != mLastBatteryHealth ||
287 mBatteryPresent != mLastBatteryPresent ||
288 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
291 mPlugType, mBatteryTechnology);
292 }
293 if (mBatteryLevel != mLastBatteryLevel ||
294 mBatteryVoltage != mLastBatteryVoltage ||
295 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
298 }
299 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
300 mPlugType == BATTERY_PLUGGED_NONE) {
301 // We want to make sure we log discharge cycle outliers
302 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700303 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
304 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700307 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
308 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
309
310 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400311 * - is just un-plugged (previously was plugged) and battery level is
312 * less than or equal to WARNING, or
313 * - is not plugged and battery level falls to WARNING boundary
314 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700315 */
316 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700317 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
318 && mBatteryLevel <= mLowBatteryWarningLevel
319 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700321 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
Christopher Tate06ba5542009-04-09 16:03:56 -0700323 // Separate broadcast is sent for power connected / not connected
324 // since the standard intent will not wake any applications and some
325 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700326 Intent statusIntent = new Intent();
327 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700328 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700329 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
330 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700331 }
332 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700333 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
334 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700335 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200336
Mihai Predaa82842f2009-04-29 15:05:56 +0200337 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700338 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700339 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
340 mContext.sendBroadcast(statusIntent);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400341 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700342 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700343 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
344 mContext.sendBroadcast(statusIntent);
Mihai Predaa82842f2009-04-29 15:05:56 +0200345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
Joe Onoratode1b3592010-10-25 20:36:47 -0700347 // Update the battery LED
348 mLed.updateLightsLocked();
349
The Android Open Source Project10592532009-03-18 17:39:46 -0700350 // This needs to be done after sendIntent() so that we get the lastest battery stats.
351 if (logOutlier && dischargeDuration != 0) {
352 logOutlier(dischargeDuration);
353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700355 mLastBatteryStatus = mBatteryStatus;
356 mLastBatteryHealth = mBatteryHealth;
357 mLastBatteryPresent = mBatteryPresent;
358 mLastBatteryLevel = mBatteryLevel;
359 mLastPlugType = mPlugType;
360 mLastBatteryVoltage = mBatteryVoltage;
361 mLastBatteryTemperature = mBatteryTemperature;
362 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400363 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365 }
366
367 private final void sendIntent() {
368 // Pack up the values and broadcast them to everyone
369 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800370 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
371 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 int icon = getIcon(mBatteryLevel);
374
Dianne Hackbornedd93162009-09-19 14:03:05 -0700375 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
376 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
377 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
378 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
379 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
380 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
381 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
382 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
383 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
384 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400385 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
387 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800388 Slog.d(TAG, "updateBattery level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
390 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 " voltage: " + mBatteryVoltage +
392 " temperature: " + mBatteryTemperature +
393 " technology: " + mBatteryTechnology +
394 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400395 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397
398 ActivityManagerNative.broadcastStickyIntent(intent, null);
399 }
400
401 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800403 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404
Dan Egnor18e93962010-02-10 19:27:58 -0800405 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
406 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
407
408 File dumpFile = null;
409 FileOutputStream dumpStream = null;
410 try {
411 // dump the service to a file
412 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
413 dumpStream = new FileOutputStream(dumpFile);
414 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700415 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800416
417 // add dump file to drop box
418 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
419 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800420 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800421 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800422 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800423 } finally {
424 // make sure we clean up
425 if (dumpStream != null) {
426 try {
427 dumpStream.close();
428 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800429 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
Dan Egnor18e93962010-02-10 19:27:58 -0800431 }
432 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800433 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800434 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436 }
437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 private final void logOutlier(long duration) {
440 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800441 String dischargeThresholdString = Settings.Secure.getString(cr,
442 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
443 String durationThresholdString = Settings.Secure.getString(cr,
444 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 if (dischargeThresholdString != null && durationThresholdString != null) {
447 try {
448 long durationThreshold = Long.parseLong(durationThresholdString);
449 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
452 // If the discharge cycle is bad enough we want to know about it.
453 logBatteryStats();
454 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800455 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800457 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 (mDischargeStartLevel - mBatteryLevel));
459 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800460 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 durationThresholdString + " or " + dischargeThresholdString);
462 return;
463 }
464 }
465 }
466
467 private final int getIcon(int level) {
468 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
469 return com.android.internal.R.drawable.stat_sys_battery_charge;
470 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING ||
471 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
472 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
473 return com.android.internal.R.drawable.stat_sys_battery;
474 } else {
475 return com.android.internal.R.drawable.stat_sys_battery_unknown;
476 }
477 }
478
479 @Override
480 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
481 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
482 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 pw.println("Permission Denial: can't dump Battery service from from pid="
485 + Binder.getCallingPid()
486 + ", uid=" + Binder.getCallingUid());
487 return;
488 }
489
490 synchronized (this) {
491 pw.println("Current Battery Service state:");
492 pw.println(" AC powered: " + mAcOnline);
493 pw.println(" USB powered: " + mUsbOnline);
494 pw.println(" status: " + mBatteryStatus);
495 pw.println(" health: " + mBatteryHealth);
496 pw.println(" present: " + mBatteryPresent);
497 pw.println(" level: " + mBatteryLevel);
498 pw.println(" scale: " + BATTERY_SCALE);
499 pw.println(" voltage:" + mBatteryVoltage);
500 pw.println(" temperature: " + mBatteryTemperature);
501 pw.println(" technology: " + mBatteryTechnology);
502 }
503 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700504
505 class Led {
506 private LightsService mLightsService;
507 private LightsService.Light mBatteryLight;
508
509 private int mBatteryLowARGB;
510 private int mBatteryMediumARGB;
511 private int mBatteryFullARGB;
512 private int mBatteryLedOn;
513 private int mBatteryLedOff;
514
515 private boolean mBatteryCharging;
516 private boolean mBatteryLow;
517 private boolean mBatteryFull;
518
519 Led(Context context, LightsService lights) {
520 mLightsService = lights;
521 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
522
523 mBatteryLowARGB = mContext.getResources().getInteger(
524 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
525 mBatteryMediumARGB = mContext.getResources().getInteger(
526 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
527 mBatteryFullARGB = mContext.getResources().getInteger(
528 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
529 mBatteryLedOn = mContext.getResources().getInteger(
530 com.android.internal.R.integer.config_notificationsBatteryLedOn);
531 mBatteryLedOff = mContext.getResources().getInteger(
532 com.android.internal.R.integer.config_notificationsBatteryLedOff);
533 }
534
535 /**
536 * Synchronize on BatteryService.
537 */
538 void updateLightsLocked() {
539 final int level = mBatteryLevel;
540 final int status = mBatteryStatus;
541 if (level < mLowBatteryWarningLevel) {
542 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
543 // Solid red when battery is charging
544 mBatteryLight.setColor(mBatteryLowARGB);
545 } else {
546 // Flash red when battery is low and not charging
547 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
548 mBatteryLedOn, mBatteryLedOff);
549 }
550 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
551 || status == BatteryManager.BATTERY_STATUS_FULL) {
552 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
553 // Solid green when full or charging and nearly full
554 mBatteryLight.setColor(mBatteryFullARGB);
555 } else {
556 // Solid orange when charging and halfway full
557 mBatteryLight.setColor(mBatteryMediumARGB);
558 }
559 } else {
560 // No lights if not charging and not low
561 mBatteryLight.turnOff();
562 }
563 }
564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565}
Joe Onoratode1b3592010-10-25 20:36:47 -0700566