blob: 32dd0d557ce19a02ac50ed72432c650222c6fabe [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
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700123 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public BatteryService(Context context) {
126 mContext = context;
127 mBatteryStats = BatteryStatsService.getService();
128
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400129 mLowBatteryWarningLevel = mContext.getResources().getInteger(
130 com.android.internal.R.integer.config_lowBatteryWarningLevel);
131 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
132 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
133
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400134 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
135
136 // watch for invalid charger messages if the invalid_charger switch exists
137 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
138 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141 // set initial status
142 update();
143 }
144
145 final boolean isPowered() {
146 // 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 -0700147 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
149
150 final boolean isPowered(int plugTypeSet) {
151 // assume we are powered if battery state is unknown so
152 // the "stay on while plugged in" option will work.
153 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
154 return true;
155 }
156 if (plugTypeSet == 0) {
157 return false;
158 }
159 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700160 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
162 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700163 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
165 }
166 return (plugTypeSet & plugTypeBit) != 0;
167 }
168
169 final int getPlugType() {
170 return mPlugType;
171 }
172
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400173 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 @Override
175 public void onUEvent(UEventObserver.UEvent event) {
176 update();
177 }
178 };
179
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400180 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
181 @Override
182 public void onUEvent(UEventObserver.UEvent event) {
183 boolean invalidCharger = "1".equals(event.get("SWITCH_STATE"));
184 if (mInvalidCharger != invalidCharger) {
185 mInvalidCharger = invalidCharger;
186 update();
187 }
188 }
189 };
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 // returns battery level as a percentage
192 final int getBatteryLevel() {
193 return mBatteryLevel;
194 }
195
Mike Lockwood07a500f2009-08-12 09:56:44 -0400196 void systemReady() {
197 // check our power situation now that it is safe to display the shutdown dialog.
198 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700199 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400200 }
201
202 private final void shutdownIfNoPower() {
203 // shut down gracefully if our battery is critically low and we are not powered.
204 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700205 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400206 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
207 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
208 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
209 mContext.startActivity(intent);
210 }
211 }
212
Eric Olsen6a362a92010-03-26 15:38:41 -0700213 private final void shutdownIfOverTemp() {
214 // shut down gracefully if temperature is too high (> 68.0C)
215 // wait until the system has booted before attempting to display the shutdown dialog.
216 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
217 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 private native void native_update();
225
226 private synchronized final void update() {
227 native_update();
228
The Android Open Source Project10592532009-03-18 17:39:46 -0700229 boolean logOutlier = false;
230 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 mBatteryLevelCritical = mBatteryLevel <= CRITICAL_BATTERY_LEVEL;
233 if (mAcOnline) {
234 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
235 } else if (mUsbOnline) {
236 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
237 } else {
238 mPlugType = BATTERY_PLUGGED_NONE;
239 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700240
241 // Let the battery stats keep track of the current level.
242 try {
243 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
244 mPlugType, mBatteryLevel, mBatteryTemperature,
245 mBatteryVoltage);
246 } catch (RemoteException e) {
247 // Should never happen.
248 }
249
250 shutdownIfNoPower();
251 shutdownIfOverTemp();
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 if (mBatteryStatus != mLastBatteryStatus ||
254 mBatteryHealth != mLastBatteryHealth ||
255 mBatteryPresent != mLastBatteryPresent ||
256 mBatteryLevel != mLastBatteryLevel ||
257 mPlugType != mLastPlugType ||
258 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400259 mBatteryTemperature != mLastBatteryTemperature ||
260 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 if (mPlugType != mLastPlugType) {
263 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
264 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 // There's no value in this data unless we've discharged at least once and the
267 // battery level has changed; so don't log until it does.
268 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700269 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
270 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mDischargeStartLevel, mBatteryLevel);
273 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
277 // charging -> discharging or we just powered up
278 mDischargeStartTime = SystemClock.elapsedRealtime();
279 mDischargeStartLevel = mBatteryLevel;
280 }
281 }
282 if (mBatteryStatus != mLastBatteryStatus ||
283 mBatteryHealth != mLastBatteryHealth ||
284 mBatteryPresent != mLastBatteryPresent ||
285 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
288 mPlugType, mBatteryTechnology);
289 }
290 if (mBatteryLevel != mLastBatteryLevel ||
291 mBatteryVoltage != mLastBatteryVoltage ||
292 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
295 }
296 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
297 mPlugType == BATTERY_PLUGGED_NONE) {
298 // We want to make sure we log discharge cycle outliers
299 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700300 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
301 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700304 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
305 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
306
307 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400308 * - is just un-plugged (previously was plugged) and battery level is
309 * less than or equal to WARNING, or
310 * - is not plugged and battery level falls to WARNING boundary
311 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700312 */
313 final boolean sendBatteryLow = !plugged
314 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400315 && mBatteryLevel <= mLowBatteryWarningLevel
316 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700318 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
Christopher Tate06ba5542009-04-09 16:03:56 -0700320 // Separate broadcast is sent for power connected / not connected
321 // since the standard intent will not wake any applications and some
322 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700323 Intent statusIntent = new Intent();
324 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700325 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700326 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
327 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700328 }
329 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700330 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
331 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700332 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200333
Mihai Predaa82842f2009-04-29 15:05:56 +0200334 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700335 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700336 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
337 mContext.sendBroadcast(statusIntent);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400338 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700339 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700340 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
341 mContext.sendBroadcast(statusIntent);
Mihai Predaa82842f2009-04-29 15:05:56 +0200342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project10592532009-03-18 17:39:46 -0700344 // This needs to be done after sendIntent() so that we get the lastest battery stats.
345 if (logOutlier && dischargeDuration != 0) {
346 logOutlier(dischargeDuration);
347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700349 mLastBatteryStatus = mBatteryStatus;
350 mLastBatteryHealth = mBatteryHealth;
351 mLastBatteryPresent = mBatteryPresent;
352 mLastBatteryLevel = mBatteryLevel;
353 mLastPlugType = mPlugType;
354 mLastBatteryVoltage = mBatteryVoltage;
355 mLastBatteryTemperature = mBatteryTemperature;
356 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400357 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359 }
360
361 private final void sendIntent() {
362 // Pack up the values and broadcast them to everyone
363 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800364 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
365 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 int icon = getIcon(mBatteryLevel);
368
Dianne Hackbornedd93162009-09-19 14:03:05 -0700369 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
370 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
371 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
372 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
373 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
374 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
375 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
376 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
377 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
378 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400379 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380
381 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800382 Slog.d(TAG, "updateBattery level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
384 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 " voltage: " + mBatteryVoltage +
386 " temperature: " + mBatteryTemperature +
387 " technology: " + mBatteryTechnology +
388 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400389 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
391
392 ActivityManagerNative.broadcastStickyIntent(intent, null);
393 }
394
395 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800397 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
Dan Egnor18e93962010-02-10 19:27:58 -0800399 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
400 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
401
402 File dumpFile = null;
403 FileOutputStream dumpStream = null;
404 try {
405 // dump the service to a file
406 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
407 dumpStream = new FileOutputStream(dumpFile);
408 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700409 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800410
411 // add dump file to drop box
412 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
413 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800414 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800415 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800416 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800417 } finally {
418 // make sure we clean up
419 if (dumpStream != null) {
420 try {
421 dumpStream.close();
422 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800423 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
Dan Egnor18e93962010-02-10 19:27:58 -0800425 }
426 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800427 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800428 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
430 }
431 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 private final void logOutlier(long duration) {
434 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800435 String dischargeThresholdString = Settings.Secure.getString(cr,
436 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
437 String durationThresholdString = Settings.Secure.getString(cr,
438 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 if (dischargeThresholdString != null && durationThresholdString != null) {
441 try {
442 long durationThreshold = Long.parseLong(durationThresholdString);
443 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
446 // If the discharge cycle is bad enough we want to know about it.
447 logBatteryStats();
448 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800449 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800451 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 (mDischargeStartLevel - mBatteryLevel));
453 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800454 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 durationThresholdString + " or " + dischargeThresholdString);
456 return;
457 }
458 }
459 }
460
461 private final int getIcon(int level) {
462 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
463 return com.android.internal.R.drawable.stat_sys_battery_charge;
464 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING ||
465 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
466 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
467 return com.android.internal.R.drawable.stat_sys_battery;
468 } else {
469 return com.android.internal.R.drawable.stat_sys_battery_unknown;
470 }
471 }
472
473 @Override
474 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
475 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
476 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 pw.println("Permission Denial: can't dump Battery service from from pid="
479 + Binder.getCallingPid()
480 + ", uid=" + Binder.getCallingUid());
481 return;
482 }
483
484 synchronized (this) {
485 pw.println("Current Battery Service state:");
486 pw.println(" AC powered: " + mAcOnline);
487 pw.println(" USB powered: " + mUsbOnline);
488 pw.println(" status: " + mBatteryStatus);
489 pw.println(" health: " + mBatteryHealth);
490 pw.println(" present: " + mBatteryPresent);
491 pw.println(" level: " + mBatteryLevel);
492 pw.println(" scale: " + BATTERY_SCALE);
493 pw.println(" voltage:" + mBatteryVoltage);
494 pw.println(" temperature: " + mBatteryTemperature);
495 pw.println(" technology: " + mBatteryTechnology);
496 }
497 }
498}