blob: 4192a9349f40730d4acdf43074dabe70af518c78 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import com.android.internal.app.IBatteryStats;
20import com.android.server.am.BatteryStatsService;
21
22import android.app.ActivityManagerNative;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.pm.PackageManager;
27import android.os.BatteryManager;
28import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070029import android.os.FileUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080031import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.os.SystemClock;
35import android.os.UEventObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.provider.Settings;
37import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080038import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import java.io.File;
41import java.io.FileDescriptor;
42import java.io.FileInputStream;
43import java.io.FileOutputStream;
44import java.io.IOException;
45import java.io.PrintWriter;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070046import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49/**
50 * <p>BatteryService monitors the charging status, and charge level of the device
51 * battery. When these values change this service broadcasts the new values
52 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
53 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
54 * BATTERY_CHANGED} action.</p>
55 * <p>The new values are stored in the Intent data and can be retrieved by
56 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
57 * following keys:</p>
58 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
59 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
60 * <p>&quot;status&quot; - String, the current charging status.<br />
61 * <p>&quot;health&quot; - String, the current battery health.<br />
62 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
63 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
64 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
65 * into an AC power adapter; 2 if plugged in via USB.</p>
66 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
67 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
68 * a degree Centigrade</p>
69 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
70 */
Jeff Brown4f8ecd82012-06-18 18:29:13 -070071public class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private static final boolean LOCAL_LOGV = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 static final int BATTERY_SCALE = 100; // battery capacity is a percentage
77
78 // Used locally for determining when to make a last ditch effort to log
79 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070080 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Dianne Hackborn6447ca32009-04-07 19:50:08 -070083 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo";
Doug Zongkerab5c49c2009-12-04 10:31:43 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String DUMPSYS_DATA_PATH = "/data/system/";
87
88 // This should probably be exposed in the API, though it's not critical
89 private static final int BATTERY_PLUGGED_NONE = 0;
90
91 private final Context mContext;
92 private final IBatteryStats mBatteryStats;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private boolean mAcOnline;
95 private boolean mUsbOnline;
Brian Muramatsu37a37f42012-08-14 15:21:02 -070096 private boolean mWirelessOnline;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private int mBatteryStatus;
98 private int mBatteryHealth;
99 private boolean mBatteryPresent;
100 private int mBatteryLevel;
101 private int mBatteryVoltage;
102 private int mBatteryTemperature;
103 private String mBatteryTechnology;
104 private boolean mBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700105 private int mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 private int mLastBatteryStatus;
108 private int mLastBatteryHealth;
109 private boolean mLastBatteryPresent;
110 private int mLastBatteryLevel;
111 private int mLastBatteryVoltage;
112 private int mLastBatteryTemperature;
113 private boolean mLastBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700114 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400115
116 private int mLowBatteryWarningLevel;
117 private int mLowBatteryCloseWarningLevel;
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private int mPlugType;
120 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 private long mDischargeStartTime;
123 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800124
Joe Onoratode1b3592010-10-25 20:36:47 -0700125 private Led mLed;
126
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700127 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
Joe Onoratode1b3592010-10-25 20:36:47 -0700129 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700131 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mBatteryStats = BatteryStatsService.getService();
133
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700134 mCriticalBatteryLevel = mContext.getResources().getInteger(
135 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400136 mLowBatteryWarningLevel = mContext.getResources().getInteger(
137 com.android.internal.R.integer.config_lowBatteryWarningLevel);
138 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
139 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
140
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400141 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
142
143 // watch for invalid charger messages if the invalid_charger switch exists
144 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
145 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 // set initial status
149 update();
150 }
151
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700152 public final boolean isPowered() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work.
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700154 return (mAcOnline || mUsbOnline || mWirelessOnline
155 || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
157
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700158 public final boolean isPowered(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // assume we are powered if battery state is unknown so
160 // the "stay on while plugged in" option will work.
161 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
162 return true;
163 }
164 if (plugTypeSet == 0) {
165 return false;
166 }
167 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700168 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
170 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700171 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
173 }
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700174 if (mWirelessOnline) {
175 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_WIRELESS;
176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 return (plugTypeSet & plugTypeBit) != 0;
178 }
179
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700180 public final int getPlugType() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 return mPlugType;
182 }
183
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400184 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 @Override
186 public void onUEvent(UEventObserver.UEvent event) {
187 update();
188 }
189 };
190
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400191 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
192 @Override
193 public void onUEvent(UEventObserver.UEvent event) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700194 int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400195 if (mInvalidCharger != invalidCharger) {
196 mInvalidCharger = invalidCharger;
197 update();
198 }
199 }
200 };
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 // returns battery level as a percentage
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700203 public final int getBatteryLevel() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 return mBatteryLevel;
205 }
206
John Spurlock10fb2242012-08-23 15:32:28 -0400207 // true if battery level is below the first warning threshold
208 public final boolean isBatteryLow() {
209 return mBatteryPresent && mBatteryLevel <= mLowBatteryWarningLevel;
210 }
211
Mike Lockwood07a500f2009-08-12 09:56:44 -0400212 void systemReady() {
213 // check our power situation now that it is safe to display the shutdown dialog.
214 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700215 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400216 }
217
218 private final void shutdownIfNoPower() {
219 // shut down gracefully if our battery is critically low and we are not powered.
220 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700221 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400222 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
223 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
224 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
225 mContext.startActivity(intent);
226 }
227 }
228
Eric Olsen6a362a92010-03-26 15:38:41 -0700229 private final void shutdownIfOverTemp() {
230 // shut down gracefully if temperature is too high (> 68.0C)
231 // wait until the system has booted before attempting to display the shutdown dialog.
232 if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
233 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
234 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
235 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
236 mContext.startActivity(intent);
237 }
238 }
239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 private native void native_update();
241
242 private synchronized final void update() {
243 native_update();
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700244 processValues();
245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700247 private void processValues() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700248 boolean logOutlier = false;
249 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700250
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700251 mBatteryLevelCritical = mBatteryLevel <= mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 if (mAcOnline) {
253 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
254 } else if (mUsbOnline) {
255 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700256 } else if (mWirelessOnline) {
257 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 } else {
259 mPlugType = BATTERY_PLUGGED_NONE;
260 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700261
262 // Let the battery stats keep track of the current level.
263 try {
264 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
265 mPlugType, mBatteryLevel, mBatteryTemperature,
266 mBatteryVoltage);
267 } catch (RemoteException e) {
268 // Should never happen.
269 }
270
271 shutdownIfNoPower();
272 shutdownIfOverTemp();
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 if (mBatteryStatus != mLastBatteryStatus ||
275 mBatteryHealth != mLastBatteryHealth ||
276 mBatteryPresent != mLastBatteryPresent ||
277 mBatteryLevel != mLastBatteryLevel ||
278 mPlugType != mLastPlugType ||
279 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400280 mBatteryTemperature != mLastBatteryTemperature ||
281 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 if (mPlugType != mLastPlugType) {
284 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
285 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 // There's no value in this data unless we've discharged at least once and the
288 // battery level has changed; so don't log until it does.
289 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700290 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
291 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 mDischargeStartLevel, mBatteryLevel);
294 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
298 // charging -> discharging or we just powered up
299 mDischargeStartTime = SystemClock.elapsedRealtime();
300 mDischargeStartLevel = mBatteryLevel;
301 }
302 }
303 if (mBatteryStatus != mLastBatteryStatus ||
304 mBatteryHealth != mLastBatteryHealth ||
305 mBatteryPresent != mLastBatteryPresent ||
306 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
309 mPlugType, mBatteryTechnology);
310 }
311 if (mBatteryLevel != mLastBatteryLevel ||
312 mBatteryVoltage != mLastBatteryVoltage ||
313 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
316 }
317 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
318 mPlugType == BATTERY_PLUGGED_NONE) {
319 // We want to make sure we log discharge cycle outliers
320 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700321 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
322 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700325 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
326 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
327
328 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400329 * - is just un-plugged (previously was plugged) and battery level is
330 * less than or equal to WARNING, or
331 * - is not plugged and battery level falls to WARNING boundary
332 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700333 */
334 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700335 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
336 && mBatteryLevel <= mLowBatteryWarningLevel
337 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700339 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
Christopher Tate06ba5542009-04-09 16:03:56 -0700341 // Separate broadcast is sent for power connected / not connected
342 // since the standard intent will not wake any applications and some
343 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700344 Intent statusIntent = new Intent();
345 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700346 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700347 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
348 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700349 }
350 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700351 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
352 mContext.sendBroadcast(statusIntent);
Christopher Tate06ba5542009-04-09 16:03:56 -0700353 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200354
Mihai Predaa82842f2009-04-29 15:05:56 +0200355 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700356 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700357 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
358 mContext.sendBroadcast(statusIntent);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400359 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700360 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700361 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
362 mContext.sendBroadcast(statusIntent);
Mihai Predaa82842f2009-04-29 15:05:56 +0200363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
Joe Onoratode1b3592010-10-25 20:36:47 -0700365 // Update the battery LED
366 mLed.updateLightsLocked();
367
The Android Open Source Project10592532009-03-18 17:39:46 -0700368 // This needs to be done after sendIntent() so that we get the lastest battery stats.
369 if (logOutlier && dischargeDuration != 0) {
370 logOutlier(dischargeDuration);
371 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700373 mLastBatteryStatus = mBatteryStatus;
374 mLastBatteryHealth = mBatteryHealth;
375 mLastBatteryPresent = mBatteryPresent;
376 mLastBatteryLevel = mBatteryLevel;
377 mLastPlugType = mPlugType;
378 mLastBatteryVoltage = mBatteryVoltage;
379 mLastBatteryTemperature = mBatteryTemperature;
380 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400381 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383 }
384
385 private final void sendIntent() {
386 // Pack up the values and broadcast them to everyone
387 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800388 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
389 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 int icon = getIcon(mBatteryLevel);
392
Dianne Hackbornedd93162009-09-19 14:03:05 -0700393 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
394 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
395 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
396 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
397 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
398 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
399 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
400 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
401 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
402 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400403 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404
Joe Onorato53859742011-04-06 18:27:43 -0700405 if (false) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700406 Slog.d(TAG, "level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
408 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 " voltage: " + mBatteryVoltage +
410 " temperature: " + mBatteryTemperature +
411 " technology: " + mBatteryTechnology +
412 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700413 " Wireless powered:" + mWirelessOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400414 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416
417 ActivityManagerNative.broadcastStickyIntent(intent, null);
418 }
419
420 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800422 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Dan Egnor18e93962010-02-10 19:27:58 -0800424 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
425 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
426
427 File dumpFile = null;
428 FileOutputStream dumpStream = null;
429 try {
430 // dump the service to a file
431 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
432 dumpStream = new FileOutputStream(dumpFile);
433 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700434 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800435
436 // add dump file to drop box
437 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
438 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800439 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800440 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800441 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800442 } finally {
443 // make sure we clean up
444 if (dumpStream != null) {
445 try {
446 dumpStream.close();
447 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800448 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
Dan Egnor18e93962010-02-10 19:27:58 -0800450 }
451 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800452 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800453 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455 }
456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 private final void logOutlier(long duration) {
459 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800460 String dischargeThresholdString = Settings.Secure.getString(cr,
461 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
462 String durationThresholdString = Settings.Secure.getString(cr,
463 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 if (dischargeThresholdString != null && durationThresholdString != null) {
466 try {
467 long durationThreshold = Long.parseLong(durationThresholdString);
468 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
471 // If the discharge cycle is bad enough we want to know about it.
472 logBatteryStats();
473 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800474 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 (mDischargeStartLevel - mBatteryLevel));
478 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800479 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 durationThresholdString + " or " + dischargeThresholdString);
481 return;
482 }
483 }
484 }
485
486 private final int getIcon(int level) {
487 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
488 return com.android.internal.R.drawable.stat_sys_battery_charge;
Joe Onorato794be402010-11-21 19:22:25 -0800489 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 return com.android.internal.R.drawable.stat_sys_battery;
Joe Onorato794be402010-11-21 19:22:25 -0800491 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
492 || mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
493 if (isPowered() && mBatteryLevel >= 100) {
494 return com.android.internal.R.drawable.stat_sys_battery_charge;
495 } else {
496 return com.android.internal.R.drawable.stat_sys_battery;
497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 } else {
499 return com.android.internal.R.drawable.stat_sys_battery_unknown;
500 }
501 }
502
503 @Override
504 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
505 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
506 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 pw.println("Permission Denial: can't dump Battery service from from pid="
509 + Binder.getCallingPid()
510 + ", uid=" + Binder.getCallingUid());
511 return;
512 }
513
Mike Lockwoode8174042011-08-16 12:53:43 -0700514 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700515 synchronized (this) {
516 pw.println("Current Battery Service state:");
517 pw.println(" AC powered: " + mAcOnline);
518 pw.println(" USB powered: " + mUsbOnline);
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700519 pw.println(" Wireless powered: " + mWirelessOnline);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700520 pw.println(" status: " + mBatteryStatus);
521 pw.println(" health: " + mBatteryHealth);
522 pw.println(" present: " + mBatteryPresent);
523 pw.println(" level: " + mBatteryLevel);
524 pw.println(" scale: " + BATTERY_SCALE);
525 pw.println(" voltage:" + mBatteryVoltage);
526 pw.println(" temperature: " + mBatteryTemperature);
527 pw.println(" technology: " + mBatteryTechnology);
528 }
529 } else if (false) {
530 // DO NOT SUBMIT WITH THIS TURNED ON
531 if (args.length == 3 && "set".equals(args[0])) {
532 String key = args[1];
533 String value = args[2];
534 try {
535 boolean update = true;
536 if ("ac".equals(key)) {
537 mAcOnline = Integer.parseInt(value) != 0;
538 } else if ("usb".equals(key)) {
539 mUsbOnline = Integer.parseInt(value) != 0;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700540 } else if ("wireless".equals(key)) {
541 mWirelessOnline = Integer.parseInt(value) != 0;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700542 } else if ("status".equals(key)) {
543 mBatteryStatus = Integer.parseInt(value);
544 } else if ("level".equals(key)) {
545 mBatteryLevel = Integer.parseInt(value);
546 } else if ("invalid".equals(key)) {
547 mInvalidCharger = Integer.parseInt(value);
548 } else {
549 update = false;
550 }
551 if (update) {
552 processValues();
553 }
554 } catch (NumberFormatException ex) {
555 pw.println("Bad value: " + value);
556 }
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700560
561 class Led {
562 private LightsService mLightsService;
563 private LightsService.Light mBatteryLight;
564
565 private int mBatteryLowARGB;
566 private int mBatteryMediumARGB;
567 private int mBatteryFullARGB;
568 private int mBatteryLedOn;
569 private int mBatteryLedOff;
570
571 private boolean mBatteryCharging;
572 private boolean mBatteryLow;
573 private boolean mBatteryFull;
574
575 Led(Context context, LightsService lights) {
576 mLightsService = lights;
577 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
578
579 mBatteryLowARGB = mContext.getResources().getInteger(
580 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
581 mBatteryMediumARGB = mContext.getResources().getInteger(
582 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
583 mBatteryFullARGB = mContext.getResources().getInteger(
584 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
585 mBatteryLedOn = mContext.getResources().getInteger(
586 com.android.internal.R.integer.config_notificationsBatteryLedOn);
587 mBatteryLedOff = mContext.getResources().getInteger(
588 com.android.internal.R.integer.config_notificationsBatteryLedOff);
589 }
590
591 /**
592 * Synchronize on BatteryService.
593 */
594 void updateLightsLocked() {
595 final int level = mBatteryLevel;
596 final int status = mBatteryStatus;
597 if (level < mLowBatteryWarningLevel) {
598 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
599 // Solid red when battery is charging
600 mBatteryLight.setColor(mBatteryLowARGB);
601 } else {
602 // Flash red when battery is low and not charging
603 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
604 mBatteryLedOn, mBatteryLedOff);
605 }
606 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
607 || status == BatteryManager.BATTERY_STATUS_FULL) {
608 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
609 // Solid green when full or charging and nearly full
610 mBatteryLight.setColor(mBatteryFullARGB);
611 } else {
612 // Solid orange when charging and halfway full
613 mBatteryLight.setColor(mBatteryMediumARGB);
614 }
615 } else {
616 // No lights if not charging and not low
617 mBatteryLight.turnOff();
618 }
619 }
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621}