blob: fc4e06f3b4e154b3e77ba63557c5e0aeea3f9b7f [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;
103
104 private int mLastBatteryStatus;
105 private int mLastBatteryHealth;
106 private boolean mLastBatteryPresent;
107 private int mLastBatteryLevel;
108 private int mLastBatteryVoltage;
109 private int mLastBatteryTemperature;
110 private boolean mLastBatteryLevelCritical;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400111
112 private int mLowBatteryWarningLevel;
113 private int mLowBatteryCloseWarningLevel;
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private int mPlugType;
116 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private long mDischargeStartTime;
119 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800120
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700121 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public BatteryService(Context context) {
124 mContext = context;
125 mBatteryStats = BatteryStatsService.getService();
126
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400127 mLowBatteryWarningLevel = mContext.getResources().getInteger(
128 com.android.internal.R.integer.config_lowBatteryWarningLevel);
129 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
130 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mUEventObserver.startObserving("SUBSYSTEM=power_supply");
133
134 // set initial status
135 update();
136 }
137
138 final boolean isPowered() {
139 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work.
140 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
141 }
142
143 final boolean isPowered(int plugTypeSet) {
144 // assume we are powered if battery state is unknown so
145 // the "stay on while plugged in" option will work.
146 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
147 return true;
148 }
149 if (plugTypeSet == 0) {
150 return false;
151 }
152 int plugTypeBit = 0;
153 if (mAcOnline) {
154 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
155 }
156 if (mUsbOnline) {
157 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
158 }
159 return (plugTypeSet & plugTypeBit) != 0;
160 }
161
162 final int getPlugType() {
163 return mPlugType;
164 }
165
166 private UEventObserver mUEventObserver = new UEventObserver() {
167 @Override
168 public void onUEvent(UEventObserver.UEvent event) {
169 update();
170 }
171 };
172
173 // returns battery level as a percentage
174 final int getBatteryLevel() {
175 return mBatteryLevel;
176 }
177
Mike Lockwood07a500f2009-08-12 09:56:44 -0400178 void systemReady() {
179 // check our power situation now that it is safe to display the shutdown dialog.
180 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700181 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400182 }
183
184 private final void shutdownIfNoPower() {
185 // shut down gracefully if our battery is critically low and we are not powered.
186 // wait until the system has booted before attempting to display the shutdown dialog.
187 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
188 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
189 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
190 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
191 mContext.startActivity(intent);
192 }
193 }
194
Eric Olsen6a362a92010-03-26 15:38:41 -0700195 private final void shutdownIfOverTemp() {
196 // shut down gracefully if temperature is too high (> 68.0C)
197 // wait until the system has booted before attempting to display the shutdown dialog.
198 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
199 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
200 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
201 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
202 mContext.startActivity(intent);
203 }
204 }
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 private native void native_update();
207
208 private synchronized final void update() {
209 native_update();
210
The Android Open Source Project10592532009-03-18 17:39:46 -0700211 boolean logOutlier = false;
212 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 mBatteryLevelCritical = mBatteryLevel <= CRITICAL_BATTERY_LEVEL;
215 if (mAcOnline) {
216 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
217 } else if (mUsbOnline) {
218 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
219 } else {
220 mPlugType = BATTERY_PLUGGED_NONE;
221 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700222
223 // Let the battery stats keep track of the current level.
224 try {
225 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
226 mPlugType, mBatteryLevel, mBatteryTemperature,
227 mBatteryVoltage);
228 } catch (RemoteException e) {
229 // Should never happen.
230 }
231
232 shutdownIfNoPower();
233 shutdownIfOverTemp();
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 if (mBatteryStatus != mLastBatteryStatus ||
236 mBatteryHealth != mLastBatteryHealth ||
237 mBatteryPresent != mLastBatteryPresent ||
238 mBatteryLevel != mLastBatteryLevel ||
239 mPlugType != mLastPlugType ||
240 mBatteryVoltage != mLastBatteryVoltage ||
241 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 if (mPlugType != mLastPlugType) {
244 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
245 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 // There's no value in this data unless we've discharged at least once and the
248 // battery level has changed; so don't log until it does.
249 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700250 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
251 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 mDischargeStartLevel, mBatteryLevel);
254 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
258 // charging -> discharging or we just powered up
259 mDischargeStartTime = SystemClock.elapsedRealtime();
260 mDischargeStartLevel = mBatteryLevel;
261 }
262 }
263 if (mBatteryStatus != mLastBatteryStatus ||
264 mBatteryHealth != mLastBatteryHealth ||
265 mBatteryPresent != mLastBatteryPresent ||
266 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
269 mPlugType, mBatteryTechnology);
270 }
271 if (mBatteryLevel != mLastBatteryLevel ||
272 mBatteryVoltage != mLastBatteryVoltage ||
273 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
276 }
277 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
278 mPlugType == BATTERY_PLUGGED_NONE) {
279 // We want to make sure we log discharge cycle outliers
280 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700281 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
282 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700285 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
286 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
287
288 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400289 * - is just un-plugged (previously was plugged) and battery level is
290 * less than or equal to WARNING, or
291 * - is not plugged and battery level falls to WARNING boundary
292 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700293 */
294 final boolean sendBatteryLow = !plugged
295 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400296 && mBatteryLevel <= mLowBatteryWarningLevel
297 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700299 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
Christopher Tate06ba5542009-04-09 16:03:56 -0700301 // Separate broadcast is sent for power connected / not connected
302 // since the standard intent will not wake any applications and some
303 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700304 Intent statusIntent = new Intent();
305 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700306 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700307 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
308 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700309 }
310 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700311 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
312 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700313 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200314
Mihai Predaa82842f2009-04-29 15:05:56 +0200315 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700316 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700317 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
318 mContext.sendBroadcast(statusIntent);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400319 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700320 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700321 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
322 mContext.sendBroadcast(statusIntent);
Mihai Predaa82842f2009-04-29 15:05:56 +0200323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project10592532009-03-18 17:39:46 -0700325 // This needs to be done after sendIntent() so that we get the lastest battery stats.
326 if (logOutlier && dischargeDuration != 0) {
327 logOutlier(dischargeDuration);
328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700330 mLastBatteryStatus = mBatteryStatus;
331 mLastBatteryHealth = mBatteryHealth;
332 mLastBatteryPresent = mBatteryPresent;
333 mLastBatteryLevel = mBatteryLevel;
334 mLastPlugType = mPlugType;
335 mLastBatteryVoltage = mBatteryVoltage;
336 mLastBatteryTemperature = mBatteryTemperature;
337 mLastBatteryLevelCritical = mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339 }
340
341 private final void sendIntent() {
342 // Pack up the values and broadcast them to everyone
343 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800344 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
345 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 int icon = getIcon(mBatteryLevel);
348
Dianne Hackbornedd93162009-09-19 14:03:05 -0700349 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
350 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
351 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
352 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
353 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
354 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
355 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
356 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
357 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
358 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
360 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800361 Slog.d(TAG, "updateBattery level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
363 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 " voltage: " + mBatteryVoltage +
365 " temperature: " + mBatteryTemperature +
366 " technology: " + mBatteryTechnology +
367 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
368 " icon:" + icon );
369 }
370
371 ActivityManagerNative.broadcastStickyIntent(intent, null);
372 }
373
374 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800376 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Dan Egnor18e93962010-02-10 19:27:58 -0800378 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
379 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
380
381 File dumpFile = null;
382 FileOutputStream dumpStream = null;
383 try {
384 // dump the service to a file
385 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
386 dumpStream = new FileOutputStream(dumpFile);
387 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700388 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800389
390 // add dump file to drop box
391 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
392 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800393 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800394 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800395 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800396 } finally {
397 // make sure we clean up
398 if (dumpStream != null) {
399 try {
400 dumpStream.close();
401 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800402 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
Dan Egnor18e93962010-02-10 19:27:58 -0800404 }
405 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800406 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800407 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 }
410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 private final void logOutlier(long duration) {
413 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800414 String dischargeThresholdString = Settings.Secure.getString(cr,
415 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
416 String durationThresholdString = Settings.Secure.getString(cr,
417 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 if (dischargeThresholdString != null && durationThresholdString != null) {
420 try {
421 long durationThreshold = Long.parseLong(durationThresholdString);
422 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
425 // If the discharge cycle is bad enough we want to know about it.
426 logBatteryStats();
427 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800428 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800430 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 (mDischargeStartLevel - mBatteryLevel));
432 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800433 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 durationThresholdString + " or " + dischargeThresholdString);
435 return;
436 }
437 }
438 }
439
440 private final int getIcon(int level) {
441 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
442 return com.android.internal.R.drawable.stat_sys_battery_charge;
443 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING ||
444 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
445 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
446 return com.android.internal.R.drawable.stat_sys_battery;
447 } else {
448 return com.android.internal.R.drawable.stat_sys_battery_unknown;
449 }
450 }
451
452 @Override
453 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
454 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
455 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 pw.println("Permission Denial: can't dump Battery service from from pid="
458 + Binder.getCallingPid()
459 + ", uid=" + Binder.getCallingUid());
460 return;
461 }
462
463 synchronized (this) {
464 pw.println("Current Battery Service state:");
465 pw.println(" AC powered: " + mAcOnline);
466 pw.println(" USB powered: " + mUsbOnline);
467 pw.println(" status: " + mBatteryStatus);
468 pw.println(" health: " + mBatteryHealth);
469 pw.println(" present: " + mBatteryPresent);
470 pw.println(" level: " + mBatteryLevel);
471 pw.println(" scale: " + BATTERY_SCALE);
472 pw.println(" voltage:" + mBatteryVoltage);
473 pw.println(" temperature: " + mBatteryTemperature);
474 pw.println(" technology: " + mBatteryTechnology);
475 }
476 }
477}