blob: 13db75cff75eda3f859982ef9866784269b7f1f5 [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
Dianne Hackborn14272302014-06-10 23:13:02 -070019import android.database.ContentObserver;
Dianne Hackborn8c841092013-06-24 13:46:13 -070020import android.os.BatteryStats;
Jeff Brown21392762014-06-13 19:00:36 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.app.IBatteryStats;
23import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080024import com.android.server.lights.Light;
25import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27import android.app.ActivityManagerNative;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070033import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070034import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070036import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070037import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070038import android.os.IBatteryPropertiesListener;
39import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080041import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.SystemClock;
45import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070046import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.provider.Settings;
48import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080049import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51import java.io.File;
52import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import java.io.FileOutputStream;
54import java.io.IOException;
55import java.io.PrintWriter;
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58/**
59 * <p>BatteryService monitors the charging status, and charge level of the device
60 * battery. When these values change this service broadcasts the new values
61 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
62 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
63 * BATTERY_CHANGED} action.</p>
64 * <p>The new values are stored in the Intent data and can be retrieved by
65 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
66 * following keys:</p>
67 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
68 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
69 * <p>&quot;status&quot; - String, the current charging status.<br />
70 * <p>&quot;health&quot; - String, the current battery health.<br />
71 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
72 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
73 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
74 * into an AC power adapter; 2 if plugged in via USB.</p>
75 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
76 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
77 * a degree Centigrade</p>
78 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070079 *
80 * <p>
81 * The battery service may be called by the power manager while holding its locks so
82 * we take care to post all outcalls into the activity manager to a handler.
83 *
84 * FIXME: Ideally the power manager would perform all of its calls into the battery
85 * service asynchronously itself.
86 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
Jeff Brown21392762014-06-13 19:00:36 -070088public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080090
Jeff Browna4d82042012-10-02 19:11:19 -070091 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080092
Jeff Browna4d82042012-10-02 19:11:19 -070093 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 // Used locally for determining when to make a last ditch effort to log
96 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070097 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700100 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private static final String DUMPSYS_DATA_PATH = "/data/system/";
103
104 // This should probably be exposed in the API, though it's not critical
105 private static final int BATTERY_PLUGGED_NONE = 0;
106
107 private final Context mContext;
108 private final IBatteryStats mBatteryStats;
Jeff Brown605ea692012-10-05 16:33:10 -0700109 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
Jeff Browna4d82042012-10-02 19:11:19 -0700111 private final Object mLock = new Object();
112
Todd Poynor26faecc2013-05-22 18:54:48 -0700113 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800114 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 private int mLastBatteryStatus;
117 private int mLastBatteryHealth;
118 private boolean mLastBatteryPresent;
119 private int mLastBatteryLevel;
120 private int mLastBatteryVoltage;
121 private int mLastBatteryTemperature;
122 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700123 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700124 private int mLastMaxChargingVoltage;
Jeff Browna4d82042012-10-02 19:11:19 -0700125
126 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700127 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400128
129 private int mLowBatteryWarningLevel;
130 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700131 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 private int mPlugType;
134 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700136 private boolean mBatteryLevelLow;
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 private long mDischargeStartTime;
139 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700141 private boolean mUpdatesStopped;
142
Joe Onoratode1b3592010-10-25 20:36:47 -0700143 private Led mLed;
144
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700145 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
Jeff Brown21392762014-06-13 19:00:36 -0700147 public BatteryService(Context context) {
148 super(context);
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700151 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700152 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 mBatteryStats = BatteryStatsService.getService();
154
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700155 mCriticalBatteryLevel = mContext.getResources().getInteger(
156 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400157 mLowBatteryWarningLevel = mContext.getResources().getInteger(
158 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700159 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
160 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700161 mShutdownBatteryTemperature = mContext.getResources().getInteger(
162 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400163
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400164 // watch for invalid charger messages if the invalid_charger switch exists
165 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700166 mInvalidChargerObserver.startObserving(
167 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400168 }
Jeff Brown21392762014-06-13 19:00:36 -0700169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Jeff Brown21392762014-06-13 19:00:36 -0700171 @Override
172 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700173 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800174 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
175 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700176 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800177 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700178 } catch (RemoteException e) {
179 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700180 }
Jeff Brown21392762014-06-13 19:00:36 -0700181
182 publishBinderService("battery", new BinderService());
183 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
185
Jeff Brown21392762014-06-13 19:00:36 -0700186 @Override
187 public void onBootPhase(int phase) {
188 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
189 // check our power situation now that it is safe to display the shutdown dialog.
190 synchronized (mLock) {
191 ContentObserver obs = new ContentObserver(mHandler) {
192 @Override
193 public void onChange(boolean selfChange) {
194 synchronized (mLock) {
195 updateBatteryWarningLevelLocked();
196 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700197 }
Jeff Brown21392762014-06-13 19:00:36 -0700198 };
199 final ContentResolver resolver = mContext.getContentResolver();
200 resolver.registerContentObserver(Settings.Global.getUriFor(
201 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
202 false, obs, UserHandle.USER_ALL);
203 updateBatteryWarningLevelLocked();
204 }
Jeff Browna4d82042012-10-02 19:11:19 -0700205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
207
Jeff Brown21392762014-06-13 19:00:36 -0700208 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700209 final ContentResolver resolver = mContext.getContentResolver();
210 int defWarnLevel = mContext.getResources().getInteger(
211 com.android.internal.R.integer.config_lowBatteryWarningLevel);
212 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
213 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
214 if (mLowBatteryWarningLevel == 0) {
215 mLowBatteryWarningLevel = defWarnLevel;
216 }
217 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
218 mLowBatteryWarningLevel = mCriticalBatteryLevel;
219 }
220 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
221 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
222 processValuesLocked(true);
223 }
224
Jeff Browna4d82042012-10-02 19:11:19 -0700225 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 // assume we are powered if battery state is unknown so
227 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700228 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 return true;
230 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700231 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700232 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700234 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700235 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700237 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700238 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
Jeff Browna4d82042012-10-02 19:11:19 -0700240 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
Jeff Brown21392762014-06-13 19:00:36 -0700243 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700244 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
245 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
246
247 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
248 * - is just un-plugged (previously was plugged) and battery level is
249 * less than or equal to WARNING, or
250 * - is not plugged and battery level falls to WARNING boundary
251 * (becomes <= mLowBatteryWarningLevel).
252 */
253 return !plugged
254 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
255 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
256 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
257 }
258
Jeff Browna4d82042012-10-02 19:11:19 -0700259 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400260 // shut down gracefully if our battery is critically low and we are not powered.
261 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700262 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700263 mHandler.post(new Runnable() {
264 @Override
265 public void run() {
266 if (ActivityManagerNative.isSystemReady()) {
267 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
268 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
269 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
270 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
271 }
272 }
273 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400274 }
275 }
276
Jeff Browna4d82042012-10-02 19:11:19 -0700277 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700278 // shut down gracefully if temperature is too high (> 68.0C by default)
279 // wait until the system has booted before attempting to display the
280 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700281 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700282 mHandler.post(new Runnable() {
283 @Override
284 public void run() {
285 if (ActivityManagerNative.isSystemReady()) {
286 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
287 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
288 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
289 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
290 }
291 }
292 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700293 }
294 }
295
Todd Poynor26faecc2013-05-22 18:54:48 -0700296 private void update(BatteryProperties props) {
297 synchronized (mLock) {
298 if (!mUpdatesStopped) {
299 mBatteryProps = props;
300 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700301 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800302 } else {
303 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700304 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700305 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Dianne Hackborn14272302014-06-10 23:13:02 -0700308 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700309 boolean logOutlier = false;
310 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700311
Todd Poynor26faecc2013-05-22 18:54:48 -0700312 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
313 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700315 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700317 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700318 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 } else {
320 mPlugType = BATTERY_PLUGGED_NONE;
321 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700322
Jeff Browna4d82042012-10-02 19:11:19 -0700323 if (DEBUG) {
324 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700325 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
326 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
327 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700328 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700329 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Todd Poynor26faecc2013-05-22 18:54:48 -0700330 + ", batteryStatus=" + mBatteryProps.batteryStatus
331 + ", batteryHealth=" + mBatteryProps.batteryHealth
332 + ", batteryPresent=" + mBatteryProps.batteryPresent
333 + ", batteryLevel=" + mBatteryProps.batteryLevel
334 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
335 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
336 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700337 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
338 + ", mPlugType=" + mPlugType);
339 }
340
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700341 // Let the battery stats keep track of the current level.
342 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700343 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
344 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
345 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700346 } catch (RemoteException e) {
347 // Should never happen.
348 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700349
Jeff Browna4d82042012-10-02 19:11:19 -0700350 shutdownIfNoPowerLocked();
351 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700352
Dianne Hackborn14272302014-06-10 23:13:02 -0700353 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700354 mBatteryProps.batteryHealth != mLastBatteryHealth ||
355 mBatteryProps.batteryPresent != mLastBatteryPresent ||
356 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700358 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
359 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700360 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700361 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700362 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 if (mPlugType != mLastPlugType) {
365 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
366 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 // There's no value in this data unless we've discharged at least once and the
369 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700370 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700371 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
372 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700374 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
379 // charging -> discharging or we just powered up
380 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700381 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700384 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
385 mBatteryProps.batteryHealth != mLastBatteryHealth ||
386 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800388 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700389 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
390 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700392 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700393 // Don't do this just from voltage or temperature changes, that is
394 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700396 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
399 mPlugType == BATTERY_PLUGGED_NONE) {
400 // We want to make sure we log discharge cycle outliers
401 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700402 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
403 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
Dianne Hackborn14272302014-06-10 23:13:02 -0700406 if (!mBatteryLevelLow) {
407 // Should we now switch in to low battery mode?
408 if (mPlugType == BATTERY_PLUGGED_NONE
409 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
410 mBatteryLevelLow = true;
411 }
412 } else {
413 // Should we now switch out of low battery mode?
414 if (mPlugType != BATTERY_PLUGGED_NONE) {
415 mBatteryLevelLow = false;
416 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
417 mBatteryLevelLow = false;
418 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
419 // If being forced, the previous state doesn't matter, we will just
420 // absolutely check to see if we are now above the warning level.
421 mBatteryLevelLow = false;
422 }
423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Jeff Browna4d82042012-10-02 19:11:19 -0700425 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
Christopher Tate06ba5542009-04-09 16:03:56 -0700427 // Separate broadcast is sent for power connected / not connected
428 // since the standard intent will not wake any applications and some
429 // applications may want to have smart behavior based on this.
430 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700431 mHandler.post(new Runnable() {
432 @Override
433 public void run() {
434 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
435 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
436 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
437 }
438 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700439 }
440 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700441 mHandler.post(new Runnable() {
442 @Override
443 public void run() {
444 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
445 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
446 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
447 }
448 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700449 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200450
Dianne Hackborn14272302014-06-10 23:13:02 -0700451 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700452 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700453 mHandler.post(new Runnable() {
454 @Override
455 public void run() {
456 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
457 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
458 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
459 }
460 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400461 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700462 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700463 mHandler.post(new Runnable() {
464 @Override
465 public void run() {
466 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
467 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
468 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
469 }
470 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
Joe Onoratode1b3592010-10-25 20:36:47 -0700473 // Update the battery LED
474 mLed.updateLightsLocked();
475
The Android Open Source Project10592532009-03-18 17:39:46 -0700476 // This needs to be done after sendIntent() so that we get the lastest battery stats.
477 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700478 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Todd Poynor26faecc2013-05-22 18:54:48 -0700481 mLastBatteryStatus = mBatteryProps.batteryStatus;
482 mLastBatteryHealth = mBatteryProps.batteryHealth;
483 mLastBatteryPresent = mBatteryProps.batteryPresent;
484 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700485 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700486 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
487 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700488 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700489 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700490 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400491 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493 }
494
Jeff Browna4d82042012-10-02 19:11:19 -0700495 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700497 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800498 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
499 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800500
Todd Poynor26faecc2013-05-22 18:54:48 -0700501 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
Todd Poynor26faecc2013-05-22 18:54:48 -0700503 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
504 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
505 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
506 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700507 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
508 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
509 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700510 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
511 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
512 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400513 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700514 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700515 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Jeff Browna4d82042012-10-02 19:11:19 -0700516 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700517 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
518 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700519 ", health:" + mBatteryProps.batteryHealth +
520 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700521 ", voltage: " + mBatteryProps.batteryVoltage +
522 ", temperature: " + mBatteryProps.batteryTemperature +
523 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700524 ", AC powered:" + mBatteryProps.chargerAcOnline +
525 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700526 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700527 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700528 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
529 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531
Jeff Brown605ea692012-10-05 16:33:10 -0700532 mHandler.post(new Runnable() {
533 @Override
534 public void run() {
535 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
536 }
537 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539
Jeff Browna4d82042012-10-02 19:11:19 -0700540 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700541 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800542 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
Dan Egnor18e93962010-02-10 19:27:58 -0800544 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
545 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
546
547 File dumpFile = null;
548 FileOutputStream dumpStream = null;
549 try {
550 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700551 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800552 dumpStream = new FileOutputStream(dumpFile);
553 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700554 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800555
556 // add dump file to drop box
557 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
558 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800559 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800560 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800561 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800562 } finally {
563 // make sure we clean up
564 if (dumpStream != null) {
565 try {
566 dumpStream.close();
567 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800568 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
Dan Egnor18e93962010-02-10 19:27:58 -0800570 }
571 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800572 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800573 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575 }
576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
Jeff Browna4d82042012-10-02 19:11:19 -0700578 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700580 String dischargeThresholdString = Settings.Global.getString(cr,
581 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
582 String durationThresholdString = Settings.Global.getString(cr,
583 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 if (dischargeThresholdString != null && durationThresholdString != null) {
586 try {
587 long durationThreshold = Long.parseLong(durationThresholdString);
588 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700590 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700592 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
Jeff Browna4d82042012-10-02 19:11:19 -0700594 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700596 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700597 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800599 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 durationThresholdString + " or " + dischargeThresholdString);
601 return;
602 }
603 }
604 }
605
Jeff Browna4d82042012-10-02 19:11:19 -0700606 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700607 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700609 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700611 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
612 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700613 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700614 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800615 return com.android.internal.R.drawable.stat_sys_battery_charge;
616 } else {
617 return com.android.internal.R.drawable.stat_sys_battery;
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 } else {
620 return com.android.internal.R.drawable.stat_sys_battery_unknown;
621 }
622 }
623
Jeff Brown21392762014-06-13 19:00:36 -0700624 private void dumpInternal(PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700625 synchronized (mLock) {
626 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700627 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700628 if (mUpdatesStopped) {
629 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
630 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700631 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
632 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
633 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700634 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700635 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700636 pw.println(" status: " + mBatteryProps.batteryStatus);
637 pw.println(" health: " + mBatteryProps.batteryHealth);
638 pw.println(" present: " + mBatteryProps.batteryPresent);
639 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700640 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700641 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700642 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
643 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800644
645 } else if ("unplug".equals(args[0])) {
646 if (!mUpdatesStopped) {
647 mLastBatteryProps.set(mBatteryProps);
648 }
649 mBatteryProps.chargerAcOnline = false;
650 mBatteryProps.chargerUsbOnline = false;
651 mBatteryProps.chargerWirelessOnline = false;
652 long ident = Binder.clearCallingIdentity();
653 try {
654 mUpdatesStopped = true;
655 processValuesLocked(false);
656 } finally {
657 Binder.restoreCallingIdentity(ident);
658 }
659
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700660 } else if (args.length == 3 && "set".equals(args[0])) {
661 String key = args[1];
662 String value = args[2];
663 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800664 if (!mUpdatesStopped) {
665 mLastBatteryProps.set(mBatteryProps);
666 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700667 boolean update = true;
668 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700669 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700670 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700671 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700672 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700673 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700674 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700675 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700676 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700677 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700678 } else if ("invalid".equals(key)) {
679 mInvalidCharger = Integer.parseInt(value);
680 } else {
681 pw.println("Unknown set option: " + key);
682 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700683 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700684 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700685 long ident = Binder.clearCallingIdentity();
686 try {
687 mUpdatesStopped = true;
Dianne Hackborn14272302014-06-10 23:13:02 -0700688 processValuesLocked(false);
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700689 } finally {
690 Binder.restoreCallingIdentity(ident);
691 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700692 }
693 } catch (NumberFormatException ex) {
694 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700695 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800696
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700697 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700698 long ident = Binder.clearCallingIdentity();
699 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800700 if (mUpdatesStopped) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800701 mUpdatesStopped = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800702 mBatteryProps.set(mLastBatteryProps);
Dianne Hackborn14272302014-06-10 23:13:02 -0700703 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800704 }
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700705 } finally {
706 Binder.restoreCallingIdentity(ident);
707 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700708 } else {
709 pw.println("Dump current battery state, or:");
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800710 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800711 pw.println(" unplug");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700712 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700716
Jeff Browna4d82042012-10-02 19:11:19 -0700717 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
718 @Override
719 public void onUEvent(UEventObserver.UEvent event) {
720 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
721 synchronized (mLock) {
722 if (mInvalidCharger != invalidCharger) {
723 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700724 }
725 }
726 }
727 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700728
Jeff Browna4d82042012-10-02 19:11:19 -0700729 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800730 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700731
Jeff Browna4d82042012-10-02 19:11:19 -0700732 private final int mBatteryLowARGB;
733 private final int mBatteryMediumARGB;
734 private final int mBatteryFullARGB;
735 private final int mBatteryLedOn;
736 private final int mBatteryLedOff;
737
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800738 public Led(Context context, LightsManager lights) {
739 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700740
Jeff Browna4d82042012-10-02 19:11:19 -0700741 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700742 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700743 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700744 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700745 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700746 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700747 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700748 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700749 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700750 com.android.internal.R.integer.config_notificationsBatteryLedOff);
751 }
752
753 /**
754 * Synchronize on BatteryService.
755 */
Jeff Browna4d82042012-10-02 19:11:19 -0700756 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700757 final int level = mBatteryProps.batteryLevel;
758 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700759 if (level < mLowBatteryWarningLevel) {
760 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
761 // Solid red when battery is charging
762 mBatteryLight.setColor(mBatteryLowARGB);
763 } else {
764 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800765 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700766 mBatteryLedOn, mBatteryLedOff);
767 }
768 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
769 || status == BatteryManager.BATTERY_STATUS_FULL) {
770 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
771 // Solid green when full or charging and nearly full
772 mBatteryLight.setColor(mBatteryFullARGB);
773 } else {
774 // Solid orange when charging and halfway full
775 mBatteryLight.setColor(mBatteryMediumARGB);
776 }
777 } else {
778 // No lights if not charging and not low
779 mBatteryLight.turnOff();
780 }
781 }
782 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700783
784 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800785 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700786 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800787 final long identity = Binder.clearCallingIdentity();
788 try {
789 BatteryService.this.update(props);
790 } finally {
791 Binder.restoreCallingIdentity(identity);
792 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700793 }
794 }
Jeff Brown21392762014-06-13 19:00:36 -0700795
796 private final class BinderService extends Binder {
797 @Override
798 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
799 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
800 != PackageManager.PERMISSION_GRANTED) {
801
802 pw.println("Permission Denial: can't dump Battery service from from pid="
803 + Binder.getCallingPid()
804 + ", uid=" + Binder.getCallingUid());
805 return;
806 }
807
808 dumpInternal(pw, args);
809 }
810 }
811
812 private final class LocalService extends BatteryManagerInternal {
813 @Override
814 public boolean isPowered(int plugTypeSet) {
815 synchronized (mLock) {
816 return isPoweredLocked(plugTypeSet);
817 }
818 }
819
820 @Override
821 public int getPlugType() {
822 synchronized (mLock) {
823 return mPlugType;
824 }
825 }
826
827 @Override
828 public int getBatteryLevel() {
829 synchronized (mLock) {
830 return mBatteryProps.batteryLevel;
831 }
832 }
833
834 @Override
835 public boolean getBatteryLevelLow() {
836 synchronized (mLock) {
837 return mBatteryLevelLow;
838 }
839 }
840
841 @Override
842 public int getInvalidCharger() {
843 synchronized (mLock) {
844 return mInvalidCharger;
845 }
846 }
847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848}