blob: fe8529b0430f501f0570dbf94d62fa9e81968560 [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;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070036import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.provider.Settings;
38import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080039import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import java.io.File;
42import java.io.FileDescriptor;
43import java.io.FileInputStream;
44import java.io.FileOutputStream;
45import java.io.IOException;
46import java.io.PrintWriter;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070047import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50/**
51 * <p>BatteryService monitors the charging status, and charge level of the device
52 * battery. When these values change this service broadcasts the new values
53 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
54 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
55 * BATTERY_CHANGED} action.</p>
56 * <p>The new values are stored in the Intent data and can be retrieved by
57 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
58 * following keys:</p>
59 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
60 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
61 * <p>&quot;status&quot; - String, the current charging status.<br />
62 * <p>&quot;health&quot; - String, the current battery health.<br />
63 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
64 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
65 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
66 * into an AC power adapter; 2 if plugged in via USB.</p>
67 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
68 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
69 * a degree Centigrade</p>
70 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
71 */
Jeff Brown4f8ecd82012-06-18 18:29:13 -070072public class BatteryService extends Binder {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private static final boolean LOCAL_LOGV = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 static final int BATTERY_SCALE = 100; // battery capacity is a percentage
78
79 // Used locally for determining when to make a last ditch effort to log
80 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070081 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Dianne Hackborn6447ca32009-04-07 19:50:08 -070084 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo";
Doug Zongkerab5c49c2009-12-04 10:31:43 -080086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private static final String DUMPSYS_DATA_PATH = "/data/system/";
88
89 // This should probably be exposed in the API, though it's not critical
90 private static final int BATTERY_PLUGGED_NONE = 0;
91
92 private final Context mContext;
93 private final IBatteryStats mBatteryStats;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private boolean mAcOnline;
96 private boolean mUsbOnline;
Brian Muramatsu37a37f42012-08-14 15:21:02 -070097 private boolean mWirelessOnline;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private int mBatteryStatus;
99 private int mBatteryHealth;
100 private boolean mBatteryPresent;
101 private int mBatteryLevel;
102 private int mBatteryVoltage;
103 private int mBatteryTemperature;
104 private String mBatteryTechnology;
105 private boolean mBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700106 private int mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private int mLastBatteryStatus;
109 private int mLastBatteryHealth;
110 private boolean mLastBatteryPresent;
111 private int mLastBatteryLevel;
112 private int mLastBatteryVoltage;
113 private int mLastBatteryTemperature;
114 private boolean mLastBatteryLevelCritical;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700115 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400116
117 private int mLowBatteryWarningLevel;
118 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700119 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 private int mPlugType;
122 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private long mDischargeStartTime;
125 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800126
Joe Onoratode1b3592010-10-25 20:36:47 -0700127 private Led mLed;
128
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700129 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
Joe Onoratode1b3592010-10-25 20:36:47 -0700131 public BatteryService(Context context, LightsService lights) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mContext = context;
Joe Onoratode1b3592010-10-25 20:36:47 -0700133 mLed = new Led(context, lights);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 mBatteryStats = BatteryStatsService.getService();
135
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700136 mCriticalBatteryLevel = mContext.getResources().getInteger(
137 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400138 mLowBatteryWarningLevel = mContext.getResources().getInteger(
139 com.android.internal.R.integer.config_lowBatteryWarningLevel);
140 mLowBatteryCloseWarningLevel = mContext.getResources().getInteger(
141 com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700142 mShutdownBatteryTemperature = mContext.getResources().getInteger(
143 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400144
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400145 mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply");
146
147 // watch for invalid charger messages if the invalid_charger switch exists
148 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
149 mInvalidChargerObserver.startObserving("DEVPATH=/devices/virtual/switch/invalid_charger");
150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 // set initial status
153 update();
154 }
155
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700156 public final boolean isPowered() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 // 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 -0700158 return (mAcOnline || mUsbOnline || mWirelessOnline
159 || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700162 public final boolean isPowered(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 // assume we are powered if battery state is unknown so
164 // the "stay on while plugged in" option will work.
165 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
166 return true;
167 }
168 if (plugTypeSet == 0) {
169 return false;
170 }
171 int plugTypeBit = 0;
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700172 if (mAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
174 }
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700175 if (mUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
177 }
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700178 if (mWirelessOnline) {
179 plugTypeBit |= BatteryManager.BATTERY_PLUGGED_WIRELESS;
180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 return (plugTypeSet & plugTypeBit) != 0;
182 }
183
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700184 public final int getPlugType() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 return mPlugType;
186 }
187
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400188 private UEventObserver mPowerSupplyObserver = new UEventObserver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 @Override
190 public void onUEvent(UEventObserver.UEvent event) {
191 update();
192 }
193 };
194
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400195 private UEventObserver mInvalidChargerObserver = new UEventObserver() {
196 @Override
197 public void onUEvent(UEventObserver.UEvent event) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700198 int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400199 if (mInvalidCharger != invalidCharger) {
200 mInvalidCharger = invalidCharger;
201 update();
202 }
203 }
204 };
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 // returns battery level as a percentage
Jeff Brown4f8ecd82012-06-18 18:29:13 -0700207 public final int getBatteryLevel() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 return mBatteryLevel;
209 }
210
John Spurlock10fb2242012-08-23 15:32:28 -0400211 // true if battery level is below the first warning threshold
212 public final boolean isBatteryLow() {
213 return mBatteryPresent && mBatteryLevel <= mLowBatteryWarningLevel;
214 }
215
Mike Lockwood07a500f2009-08-12 09:56:44 -0400216 void systemReady() {
217 // check our power situation now that it is safe to display the shutdown dialog.
218 shutdownIfNoPower();
Eric Olsen6a362a92010-03-26 15:38:41 -0700219 shutdownIfOverTemp();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400220 }
221
222 private final void shutdownIfNoPower() {
223 // shut down gracefully if our battery is critically low and we are not powered.
224 // wait until the system has booted before attempting to display the shutdown dialog.
Jean-Baptiste Querud3e803a2010-08-31 12:29:16 -0700225 if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400226 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
227 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
228 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
229 mContext.startActivity(intent);
230 }
231 }
232
Eric Olsen6a362a92010-03-26 15:38:41 -0700233 private final void shutdownIfOverTemp() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700234 // shut down gracefully if temperature is too high (> 68.0C by default)
235 // wait until the system has booted before attempting to display the
236 // shutdown dialog.
237 if (mBatteryTemperature > mShutdownBatteryTemperature
238 && ActivityManagerNative.isSystemReady()) {
Eric Olsen6a362a92010-03-26 15:38:41 -0700239 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
240 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
241 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
242 mContext.startActivity(intent);
243 }
244 }
245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 private native void native_update();
247
248 private synchronized final void update() {
249 native_update();
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700250 processValues();
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700253 private void processValues() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700254 boolean logOutlier = false;
255 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700256
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700257 mBatteryLevelCritical = mBatteryLevel <= mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 if (mAcOnline) {
259 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
260 } else if (mUsbOnline) {
261 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700262 } else if (mWirelessOnline) {
263 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 } else {
265 mPlugType = BATTERY_PLUGGED_NONE;
266 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700267
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700268 // Let the battery stats keep track of the current level.
269 try {
270 mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth,
271 mPlugType, mBatteryLevel, mBatteryTemperature,
272 mBatteryVoltage);
273 } catch (RemoteException e) {
274 // Should never happen.
275 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700276
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700277 shutdownIfNoPower();
278 shutdownIfOverTemp();
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 if (mBatteryStatus != mLastBatteryStatus ||
281 mBatteryHealth != mLastBatteryHealth ||
282 mBatteryPresent != mLastBatteryPresent ||
283 mBatteryLevel != mLastBatteryLevel ||
284 mPlugType != mLastPlugType ||
285 mBatteryVoltage != mLastBatteryVoltage ||
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400286 mBatteryTemperature != mLastBatteryTemperature ||
287 mInvalidCharger != mLastInvalidCharger) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 if (mPlugType != mLastPlugType) {
290 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
291 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 // There's no value in this data unless we've discharged at least once and the
294 // battery level has changed; so don't log until it does.
295 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700296 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
297 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 mDischargeStartLevel, mBatteryLevel);
300 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
304 // charging -> discharging or we just powered up
305 mDischargeStartTime = SystemClock.elapsedRealtime();
306 mDischargeStartLevel = mBatteryLevel;
307 }
308 }
309 if (mBatteryStatus != mLastBatteryStatus ||
310 mBatteryHealth != mLastBatteryHealth ||
311 mBatteryPresent != mLastBatteryPresent ||
312 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
315 mPlugType, mBatteryTechnology);
316 }
317 if (mBatteryLevel != mLastBatteryLevel ||
318 mBatteryVoltage != mLastBatteryVoltage ||
319 mBatteryTemperature != mLastBatteryTemperature) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
322 }
323 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
324 mPlugType == BATTERY_PLUGGED_NONE) {
325 // We want to make sure we log discharge cycle outliers
326 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700327 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
328 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700331 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
332 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
333
334 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400335 * - is just un-plugged (previously was plugged) and battery level is
336 * less than or equal to WARNING, or
337 * - is not plugged and battery level falls to WARNING boundary
338 * (becomes <= mLowBatteryWarningLevel).
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700339 */
340 final boolean sendBatteryLow = !plugged
Joe Onoratode1b3592010-10-25 20:36:47 -0700341 && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
342 && mBatteryLevel <= mLowBatteryWarningLevel
343 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700345 sendIntent();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
Christopher Tate06ba5542009-04-09 16:03:56 -0700347 // Separate broadcast is sent for power connected / not connected
348 // since the standard intent will not wake any applications and some
349 // applications may want to have smart behavior based on this.
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700350 Intent statusIntent = new Intent();
351 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Christopher Tate06ba5542009-04-09 16:03:56 -0700352 if (mPlugType != 0 && mLastPlugType == 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700353 statusIntent.setAction(Intent.ACTION_POWER_CONNECTED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700354 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Christopher Tate06ba5542009-04-09 16:03:56 -0700355 }
356 else if (mPlugType == 0 && mLastPlugType != 0) {
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700357 statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700358 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Christopher Tate06ba5542009-04-09 16:03:56 -0700359 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200360
Mihai Predaa82842f2009-04-29 15:05:56 +0200361 if (sendBatteryLow) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700362 mSentLowBatteryBroadcast = true;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700363 statusIntent.setAction(Intent.ACTION_BATTERY_LOW);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700364 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400365 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700366 mSentLowBatteryBroadcast = false;
Christopher Tate93dc9fe2009-07-16 13:25:49 -0700367 statusIntent.setAction(Intent.ACTION_BATTERY_OKAY);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700368 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
Mihai Predaa82842f2009-04-29 15:05:56 +0200369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Joe Onoratode1b3592010-10-25 20:36:47 -0700371 // Update the battery LED
372 mLed.updateLightsLocked();
373
The Android Open Source Project10592532009-03-18 17:39:46 -0700374 // This needs to be done after sendIntent() so that we get the lastest battery stats.
375 if (logOutlier && dischargeDuration != 0) {
376 logOutlier(dischargeDuration);
377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700379 mLastBatteryStatus = mBatteryStatus;
380 mLastBatteryHealth = mBatteryHealth;
381 mLastBatteryPresent = mBatteryPresent;
382 mLastBatteryLevel = mBatteryLevel;
383 mLastPlugType = mPlugType;
384 mLastBatteryVoltage = mBatteryVoltage;
385 mLastBatteryTemperature = mBatteryTemperature;
386 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400387 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
389 }
390
391 private final void sendIntent() {
392 // Pack up the values and broadcast them to everyone
393 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800394 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
395 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 int icon = getIcon(mBatteryLevel);
398
Dianne Hackbornedd93162009-09-19 14:03:05 -0700399 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus);
400 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth);
401 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent);
402 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel);
403 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
404 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
405 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
406 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage);
407 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature);
408 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400409 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410
Joe Onorato53859742011-04-06 18:27:43 -0700411 if (false) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700412 Slog.d(TAG, "level:" + mBatteryLevel +
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
414 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 " voltage: " + mBatteryVoltage +
416 " temperature: " + mBatteryTemperature +
417 " technology: " + mBatteryTechnology +
418 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700419 " Wireless powered:" + mWirelessOnline +
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400420 " icon:" + icon + " invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
422
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700423 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425
426 private final void logBatteryStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800428 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429
Dan Egnor18e93962010-02-10 19:27:58 -0800430 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
431 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
432
433 File dumpFile = null;
434 FileOutputStream dumpStream = null;
435 try {
436 // dump the service to a file
437 dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
438 dumpStream = new FileOutputStream(dumpFile);
439 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700440 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800441
442 // add dump file to drop box
443 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
444 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800445 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800446 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800447 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800448 } finally {
449 // make sure we clean up
450 if (dumpStream != null) {
451 try {
452 dumpStream.close();
453 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800454 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
Dan Egnor18e93962010-02-10 19:27:58 -0800456 }
457 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800458 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800459 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 }
462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 private final void logOutlier(long duration) {
465 ContentResolver cr = mContext.getContentResolver();
Doug Zongker43866e02010-01-07 12:09:54 -0800466 String dischargeThresholdString = Settings.Secure.getString(cr,
467 Settings.Secure.BATTERY_DISCHARGE_THRESHOLD);
468 String durationThresholdString = Settings.Secure.getString(cr,
469 Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 if (dischargeThresholdString != null && durationThresholdString != null) {
472 try {
473 long durationThreshold = Long.parseLong(durationThresholdString);
474 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800475 if (duration <= durationThreshold &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) {
477 // If the discharge cycle is bad enough we want to know about it.
478 logBatteryStats();
479 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800480 if (LOCAL_LOGV) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 " discharge threshold: " + dischargeThreshold);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800482 if (LOCAL_LOGV) Slog.v(TAG, "duration: " + duration + " discharge: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 (mDischargeStartLevel - mBatteryLevel));
484 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800485 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 durationThresholdString + " or " + dischargeThresholdString);
487 return;
488 }
489 }
490 }
491
492 private final int getIcon(int level) {
493 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
494 return com.android.internal.R.drawable.stat_sys_battery_charge;
Joe Onorato794be402010-11-21 19:22:25 -0800495 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 return com.android.internal.R.drawable.stat_sys_battery;
Joe Onorato794be402010-11-21 19:22:25 -0800497 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
498 || mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
499 if (isPowered() && mBatteryLevel >= 100) {
500 return com.android.internal.R.drawable.stat_sys_battery_charge;
501 } else {
502 return com.android.internal.R.drawable.stat_sys_battery;
503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 } else {
505 return com.android.internal.R.drawable.stat_sys_battery_unknown;
506 }
507 }
508
509 @Override
510 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
511 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
512 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 pw.println("Permission Denial: can't dump Battery service from from pid="
515 + Binder.getCallingPid()
516 + ", uid=" + Binder.getCallingUid());
517 return;
518 }
519
Mike Lockwoode8174042011-08-16 12:53:43 -0700520 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700521 synchronized (this) {
522 pw.println("Current Battery Service state:");
523 pw.println(" AC powered: " + mAcOnline);
524 pw.println(" USB powered: " + mUsbOnline);
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700525 pw.println(" Wireless powered: " + mWirelessOnline);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700526 pw.println(" status: " + mBatteryStatus);
527 pw.println(" health: " + mBatteryHealth);
528 pw.println(" present: " + mBatteryPresent);
529 pw.println(" level: " + mBatteryLevel);
530 pw.println(" scale: " + BATTERY_SCALE);
531 pw.println(" voltage:" + mBatteryVoltage);
532 pw.println(" temperature: " + mBatteryTemperature);
533 pw.println(" technology: " + mBatteryTechnology);
534 }
535 } else if (false) {
536 // DO NOT SUBMIT WITH THIS TURNED ON
537 if (args.length == 3 && "set".equals(args[0])) {
538 String key = args[1];
539 String value = args[2];
540 try {
541 boolean update = true;
542 if ("ac".equals(key)) {
543 mAcOnline = Integer.parseInt(value) != 0;
544 } else if ("usb".equals(key)) {
545 mUsbOnline = Integer.parseInt(value) != 0;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700546 } else if ("wireless".equals(key)) {
547 mWirelessOnline = Integer.parseInt(value) != 0;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700548 } else if ("status".equals(key)) {
549 mBatteryStatus = Integer.parseInt(value);
550 } else if ("level".equals(key)) {
551 mBatteryLevel = Integer.parseInt(value);
552 } else if ("invalid".equals(key)) {
553 mInvalidCharger = Integer.parseInt(value);
554 } else {
555 update = false;
556 }
557 if (update) {
558 processValues();
559 }
560 } catch (NumberFormatException ex) {
561 pw.println("Bad value: " + value);
562 }
563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700566
567 class Led {
568 private LightsService mLightsService;
569 private LightsService.Light mBatteryLight;
570
571 private int mBatteryLowARGB;
572 private int mBatteryMediumARGB;
573 private int mBatteryFullARGB;
574 private int mBatteryLedOn;
575 private int mBatteryLedOff;
576
577 private boolean mBatteryCharging;
578 private boolean mBatteryLow;
579 private boolean mBatteryFull;
580
581 Led(Context context, LightsService lights) {
582 mLightsService = lights;
583 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
584
585 mBatteryLowARGB = mContext.getResources().getInteger(
586 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
587 mBatteryMediumARGB = mContext.getResources().getInteger(
588 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
589 mBatteryFullARGB = mContext.getResources().getInteger(
590 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
591 mBatteryLedOn = mContext.getResources().getInteger(
592 com.android.internal.R.integer.config_notificationsBatteryLedOn);
593 mBatteryLedOff = mContext.getResources().getInteger(
594 com.android.internal.R.integer.config_notificationsBatteryLedOff);
595 }
596
597 /**
598 * Synchronize on BatteryService.
599 */
600 void updateLightsLocked() {
601 final int level = mBatteryLevel;
602 final int status = mBatteryStatus;
603 if (level < mLowBatteryWarningLevel) {
604 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
605 // Solid red when battery is charging
606 mBatteryLight.setColor(mBatteryLowARGB);
607 } else {
608 // Flash red when battery is low and not charging
609 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
610 mBatteryLedOn, mBatteryLedOff);
611 }
612 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
613 || status == BatteryManager.BATTERY_STATUS_FULL) {
614 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
615 // Solid green when full or charging and nearly full
616 mBatteryLight.setColor(mBatteryFullARGB);
617 } else {
618 // Solid orange when charging and halfway full
619 mBatteryLight.setColor(mBatteryMediumARGB);
620 }
621 } else {
622 // No lights if not charging and not low
623 mBatteryLight.turnOff();
624 }
625 }
626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627}