blob: 38c6fb61fa4e866014d590357f66e2c8c1e2ee51 [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
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070019import android.app.ActivityManagerInternal;
Dianne Hackborn14272302014-06-10 23:13:02 -070020import android.database.ContentObserver;
Dianne Hackborn8c841092013-06-24 13:46:13 -070021import android.os.BatteryStats;
Jeff Brown21392762014-06-13 19:00:36 -070022
Dianne Hackborn2e441072015-10-28 18:00:57 -070023import android.os.ResultReceiver;
Dianne Hackborn354736e2016-08-22 17:00:05 -070024import android.os.ShellCallback;
Dianne Hackborn2e441072015-10-28 18:00:57 -070025import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.app.IBatteryStats;
27import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080028import com.android.server.lights.Light;
29import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Sudheer Shankadc589ac2016-11-10 15:30:17 -080031import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ContentResolver;
33import android.content.Context;
34import android.content.Intent;
35import android.content.pm.PackageManager;
36import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070037import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070038import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070040import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070041import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070042import android.os.IBatteryPropertiesListener;
43import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080045import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemClock;
49import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070050import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.provider.Settings;
52import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080053import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55import java.io.File;
56import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.io.FileOutputStream;
58import java.io.IOException;
59import java.io.PrintWriter;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62/**
63 * <p>BatteryService monitors the charging status, and charge level of the device
64 * battery. When these values change this service broadcasts the new values
65 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
66 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
67 * BATTERY_CHANGED} action.</p>
68 * <p>The new values are stored in the Intent data and can be retrieved by
69 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
70 * following keys:</p>
71 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
72 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
73 * <p>&quot;status&quot; - String, the current charging status.<br />
74 * <p>&quot;health&quot; - String, the current battery health.<br />
75 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
76 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
77 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
78 * into an AC power adapter; 2 if plugged in via USB.</p>
79 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
80 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
81 * a degree Centigrade</p>
82 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070083 *
84 * <p>
85 * The battery service may be called by the power manager while holding its locks so
86 * we take care to post all outcalls into the activity manager to a handler.
87 *
88 * FIXME: Ideally the power manager would perform all of its calls into the battery
89 * service asynchronously itself.
90 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 */
Jeff Brown21392762014-06-13 19:00:36 -070092public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080094
Jeff Browna4d82042012-10-02 19:11:19 -070095 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080096
Jeff Browna4d82042012-10-02 19:11:19 -070097 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 // Used locally for determining when to make a last ditch effort to log
100 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700101 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700103 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private static final String DUMPSYS_DATA_PATH = "/data/system/";
106
107 // This should probably be exposed in the API, though it's not critical
108 private static final int BATTERY_PLUGGED_NONE = 0;
109
110 private final Context mContext;
111 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700112 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700113 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800114
Jeff Browna4d82042012-10-02 19:11:19 -0700115 private final Object mLock = new Object();
116
Todd Poynor26faecc2013-05-22 18:54:48 -0700117 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800118 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 private int mLastBatteryStatus;
121 private int mLastBatteryHealth;
122 private boolean mLastBatteryPresent;
123 private int mLastBatteryLevel;
124 private int mLastBatteryVoltage;
125 private int mLastBatteryTemperature;
126 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700127 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700128 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700129 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700130
131 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700132 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400133
134 private int mLowBatteryWarningLevel;
135 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700136 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 private int mPlugType;
139 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700141 private boolean mBatteryLevelLow;
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private long mDischargeStartTime;
144 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700146 private boolean mUpdatesStopped;
147
Joe Onoratode1b3592010-10-25 20:36:47 -0700148 private Led mLed;
149
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700150 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700152 private ActivityManagerInternal mActivityManagerInternal;
153
Jeff Brown21392762014-06-13 19:00:36 -0700154 public BatteryService(Context context) {
155 super(context);
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700158 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700159 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 mBatteryStats = BatteryStatsService.getService();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700161 mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700163 mCriticalBatteryLevel = mContext.getResources().getInteger(
164 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400165 mLowBatteryWarningLevel = mContext.getResources().getInteger(
166 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700167 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
168 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700169 mShutdownBatteryTemperature = mContext.getResources().getInteger(
170 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400171
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400172 // watch for invalid charger messages if the invalid_charger switch exists
173 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700174 UEventObserver invalidChargerObserver = new UEventObserver() {
175 @Override
176 public void onUEvent(UEvent event) {
177 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
178 synchronized (mLock) {
179 if (mInvalidCharger != invalidCharger) {
180 mInvalidCharger = invalidCharger;
181 }
182 }
183 }
184 };
185 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700186 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400187 }
Jeff Brown21392762014-06-13 19:00:36 -0700188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Jeff Brown21392762014-06-13 19:00:36 -0700190 @Override
191 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700192 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800193 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
194 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700195 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800196 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700197 } catch (RemoteException e) {
198 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700199 }
Jeff Brown21392762014-06-13 19:00:36 -0700200
Dianne Hackborn2e441072015-10-28 18:00:57 -0700201 mBinderService = new BinderService();
202 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700203 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205
Jeff Brown21392762014-06-13 19:00:36 -0700206 @Override
207 public void onBootPhase(int phase) {
208 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
209 // check our power situation now that it is safe to display the shutdown dialog.
210 synchronized (mLock) {
211 ContentObserver obs = new ContentObserver(mHandler) {
212 @Override
213 public void onChange(boolean selfChange) {
214 synchronized (mLock) {
215 updateBatteryWarningLevelLocked();
216 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700217 }
Jeff Brown21392762014-06-13 19:00:36 -0700218 };
219 final ContentResolver resolver = mContext.getContentResolver();
220 resolver.registerContentObserver(Settings.Global.getUriFor(
221 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
222 false, obs, UserHandle.USER_ALL);
223 updateBatteryWarningLevelLocked();
224 }
Jeff Browna4d82042012-10-02 19:11:19 -0700225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 }
227
Jeff Brown21392762014-06-13 19:00:36 -0700228 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700229 final ContentResolver resolver = mContext.getContentResolver();
230 int defWarnLevel = mContext.getResources().getInteger(
231 com.android.internal.R.integer.config_lowBatteryWarningLevel);
232 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
233 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
234 if (mLowBatteryWarningLevel == 0) {
235 mLowBatteryWarningLevel = defWarnLevel;
236 }
237 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
238 mLowBatteryWarningLevel = mCriticalBatteryLevel;
239 }
240 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
241 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
242 processValuesLocked(true);
243 }
244
Jeff Browna4d82042012-10-02 19:11:19 -0700245 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 // assume we are powered if battery state is unknown so
247 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700248 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 return true;
250 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700251 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700252 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700254 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700255 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700257 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700258 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
Jeff Browna4d82042012-10-02 19:11:19 -0700260 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262
Jeff Brown21392762014-06-13 19:00:36 -0700263 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700264 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
265 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
266
267 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
268 * - is just un-plugged (previously was plugged) and battery level is
269 * less than or equal to WARNING, or
270 * - is not plugged and battery level falls to WARNING boundary
271 * (becomes <= mLowBatteryWarningLevel).
272 */
273 return !plugged
274 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
275 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
276 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
277 }
278
Jeff Browna4d82042012-10-02 19:11:19 -0700279 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400280 // shut down gracefully if our battery is critically low and we are not powered.
281 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700282 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700283 mHandler.post(new Runnable() {
284 @Override
285 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700286 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700287 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
288 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
289 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
290 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
291 }
292 }
293 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400294 }
295 }
296
Jeff Browna4d82042012-10-02 19:11:19 -0700297 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700298 // shut down gracefully if temperature is too high (> 68.0C by default)
299 // wait until the system has booted before attempting to display the
300 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700301 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700302 mHandler.post(new Runnable() {
303 @Override
304 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700305 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700306 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
307 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
308 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
309 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
310 }
311 }
312 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700313 }
314 }
315
Todd Poynor26faecc2013-05-22 18:54:48 -0700316 private void update(BatteryProperties props) {
317 synchronized (mLock) {
318 if (!mUpdatesStopped) {
319 mBatteryProps = props;
320 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700321 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800322 } else {
323 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700324 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700325 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
Dianne Hackborn14272302014-06-10 23:13:02 -0700328 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700329 boolean logOutlier = false;
330 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700331
Todd Poynor26faecc2013-05-22 18:54:48 -0700332 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
333 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700335 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700337 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700338 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 } else {
340 mPlugType = BATTERY_PLUGGED_NONE;
341 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700342
Jeff Browna4d82042012-10-02 19:11:19 -0700343 if (DEBUG) {
344 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700345 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
346 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
347 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700348 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700349 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Todd Poynor26faecc2013-05-22 18:54:48 -0700350 + ", batteryStatus=" + mBatteryProps.batteryStatus
351 + ", batteryHealth=" + mBatteryProps.batteryHealth
352 + ", batteryPresent=" + mBatteryProps.batteryPresent
353 + ", batteryLevel=" + mBatteryProps.batteryLevel
354 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
355 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
Adam Lesinskiabcb9f32016-12-09 19:27:39 -0800356 + ", batteryChargeCounter=" + mBatteryProps.batteryChargeCounter
357 + ", batteryFullCharge=" + mBatteryProps.batteryFullCharge
Todd Poynor26faecc2013-05-22 18:54:48 -0700358 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700359 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
360 + ", mPlugType=" + mPlugType);
361 }
362
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700363 // Let the battery stats keep track of the current level.
364 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700365 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
366 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
Adam Lesinski926969b2016-04-28 17:31:12 -0700367 mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700368 } catch (RemoteException e) {
369 // Should never happen.
370 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700371
Jeff Browna4d82042012-10-02 19:11:19 -0700372 shutdownIfNoPowerLocked();
373 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700374
Dianne Hackborn14272302014-06-10 23:13:02 -0700375 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700376 mBatteryProps.batteryHealth != mLastBatteryHealth ||
377 mBatteryProps.batteryPresent != mLastBatteryPresent ||
378 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700380 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
381 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700382 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700383 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700384 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700385 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 if (mPlugType != mLastPlugType) {
388 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
389 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 // There's no value in this data unless we've discharged at least once and the
392 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700393 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700394 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
395 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700397 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
401 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
402 // charging -> discharging or we just powered up
403 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700404 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700407 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
408 mBatteryProps.batteryHealth != mLastBatteryHealth ||
409 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700412 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
413 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700415 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700416 // Don't do this just from voltage or temperature changes, that is
417 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700419 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
422 mPlugType == BATTERY_PLUGGED_NONE) {
423 // We want to make sure we log discharge cycle outliers
424 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700425 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
426 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
Dianne Hackborn14272302014-06-10 23:13:02 -0700429 if (!mBatteryLevelLow) {
430 // Should we now switch in to low battery mode?
431 if (mPlugType == BATTERY_PLUGGED_NONE
432 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
433 mBatteryLevelLow = true;
434 }
435 } else {
436 // Should we now switch out of low battery mode?
437 if (mPlugType != BATTERY_PLUGGED_NONE) {
438 mBatteryLevelLow = false;
439 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
440 mBatteryLevelLow = false;
441 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
442 // If being forced, the previous state doesn't matter, we will just
443 // absolutely check to see if we are now above the warning level.
444 mBatteryLevelLow = false;
445 }
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Jeff Browna4d82042012-10-02 19:11:19 -0700448 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
Christopher Tate06ba5542009-04-09 16:03:56 -0700450 // Separate broadcast is sent for power connected / not connected
451 // since the standard intent will not wake any applications and some
452 // applications may want to have smart behavior based on this.
453 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700454 mHandler.post(new Runnable() {
455 @Override
456 public void run() {
457 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
458 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
459 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
460 }
461 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700462 }
463 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700464 mHandler.post(new Runnable() {
465 @Override
466 public void run() {
467 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
468 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
469 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
470 }
471 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700472 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200473
Dianne Hackborn14272302014-06-10 23:13:02 -0700474 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700475 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700476 mHandler.post(new Runnable() {
477 @Override
478 public void run() {
479 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
480 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
481 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
482 }
483 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400484 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700485 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700486 mHandler.post(new Runnable() {
487 @Override
488 public void run() {
489 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
490 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
491 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
492 }
493 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
Joe Onoratode1b3592010-10-25 20:36:47 -0700496 // Update the battery LED
497 mLed.updateLightsLocked();
498
The Android Open Source Project10592532009-03-18 17:39:46 -0700499 // This needs to be done after sendIntent() so that we get the lastest battery stats.
500 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700501 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800503
Todd Poynor26faecc2013-05-22 18:54:48 -0700504 mLastBatteryStatus = mBatteryProps.batteryStatus;
505 mLastBatteryHealth = mBatteryProps.batteryHealth;
506 mLastBatteryPresent = mBatteryProps.batteryPresent;
507 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700508 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700509 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
510 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700511 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700512 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700513 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700514 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400515 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517 }
518
Jeff Browna4d82042012-10-02 19:11:19 -0700519 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700521 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800522 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
523 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
Todd Poynor26faecc2013-05-22 18:54:48 -0700525 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526
Todd Poynor26faecc2013-05-22 18:54:48 -0700527 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
528 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
529 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
530 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700531 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
532 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
533 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700534 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
535 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
536 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400537 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700538 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700539 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700540 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700541 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700542 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
543 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700544 ", health:" + mBatteryProps.batteryHealth +
545 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700546 ", voltage: " + mBatteryProps.batteryVoltage +
547 ", temperature: " + mBatteryProps.batteryTemperature +
548 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700549 ", AC powered:" + mBatteryProps.chargerAcOnline +
550 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700551 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700552 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700553 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700554 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
555 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557
Jeff Brown605ea692012-10-05 16:33:10 -0700558 mHandler.post(new Runnable() {
559 @Override
560 public void run() {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800561 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jeff Brown605ea692012-10-05 16:33:10 -0700562 }
563 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Jeff Browna4d82042012-10-02 19:11:19 -0700566 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700567 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800568 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
Dan Egnor18e93962010-02-10 19:27:58 -0800570 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
571 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
572
573 File dumpFile = null;
574 FileOutputStream dumpStream = null;
575 try {
576 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700577 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800578 dumpStream = new FileOutputStream(dumpFile);
579 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700580 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800581
582 // add dump file to drop box
583 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
584 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800585 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800586 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800587 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800588 } finally {
589 // make sure we clean up
590 if (dumpStream != null) {
591 try {
592 dumpStream.close();
593 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800594 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
Dan Egnor18e93962010-02-10 19:27:58 -0800596 }
597 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800598 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800599 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601 }
602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800603
Jeff Browna4d82042012-10-02 19:11:19 -0700604 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700606 String dischargeThresholdString = Settings.Global.getString(cr,
607 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
608 String durationThresholdString = Settings.Global.getString(cr,
609 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 if (dischargeThresholdString != null && durationThresholdString != null) {
612 try {
613 long durationThreshold = Long.parseLong(durationThresholdString);
614 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800615 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700616 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700618 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
Jeff Browna4d82042012-10-02 19:11:19 -0700620 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700622 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700623 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800625 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628 }
629 }
630
Jeff Browna4d82042012-10-02 19:11:19 -0700631 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700632 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700634 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700636 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
637 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700638 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700639 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800640 return com.android.internal.R.drawable.stat_sys_battery_charge;
641 } else {
642 return com.android.internal.R.drawable.stat_sys_battery;
643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 } else {
645 return com.android.internal.R.drawable.stat_sys_battery_unknown;
646 }
647 }
648
Dianne Hackborn2e441072015-10-28 18:00:57 -0700649 class Shell extends ShellCommand {
650 @Override
651 public int onCommand(String cmd) {
652 return onShellCommand(this, cmd);
653 }
654
655 @Override
656 public void onHelp() {
657 PrintWriter pw = getOutPrintWriter();
658 dumpHelp(pw);
659 }
660 }
661
662 static void dumpHelp(PrintWriter pw) {
663 pw.println("Battery service (battery) commands:");
664 pw.println(" help");
665 pw.println(" Print this help text.");
666 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
667 pw.println(" Force a battery property value, freezing battery state.");
668 pw.println(" unplug");
669 pw.println(" Force battery unplugged, freezing battery state.");
670 pw.println(" reset");
671 pw.println(" Unfreeze battery state, returning to current hardware values.");
672 }
673
674 int onShellCommand(Shell shell, String cmd) {
675 if (cmd == null) {
676 return shell.handleDefaultCommands(cmd);
677 }
678 PrintWriter pw = shell.getOutPrintWriter();
679 switch (cmd) {
680 case "unplug": {
681 getContext().enforceCallingOrSelfPermission(
682 android.Manifest.permission.DEVICE_POWER, null);
683 if (!mUpdatesStopped) {
684 mLastBatteryProps.set(mBatteryProps);
685 }
686 mBatteryProps.chargerAcOnline = false;
687 mBatteryProps.chargerUsbOnline = false;
688 mBatteryProps.chargerWirelessOnline = false;
689 long ident = Binder.clearCallingIdentity();
690 try {
691 mUpdatesStopped = true;
692 processValuesLocked(false);
693 } finally {
694 Binder.restoreCallingIdentity(ident);
695 }
696 } break;
697 case "set": {
698 getContext().enforceCallingOrSelfPermission(
699 android.Manifest.permission.DEVICE_POWER, null);
700 final String key = shell.getNextArg();
701 if (key == null) {
702 pw.println("No property specified");
703 return -1;
704
705 }
706 final String value = shell.getNextArg();
707 if (value == null) {
708 pw.println("No value specified");
709 return -1;
710
711 }
712 try {
713 if (!mUpdatesStopped) {
714 mLastBatteryProps.set(mBatteryProps);
715 }
716 boolean update = true;
717 switch (key) {
718 case "ac":
719 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
720 break;
721 case "usb":
722 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
723 break;
724 case "wireless":
725 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
726 break;
727 case "status":
728 mBatteryProps.batteryStatus = Integer.parseInt(value);
729 break;
730 case "level":
731 mBatteryProps.batteryLevel = Integer.parseInt(value);
732 break;
733 case "invalid":
734 mInvalidCharger = Integer.parseInt(value);
735 break;
736 default:
737 pw.println("Unknown set option: " + key);
738 update = false;
739 break;
740 }
741 if (update) {
742 long ident = Binder.clearCallingIdentity();
743 try {
744 mUpdatesStopped = true;
745 processValuesLocked(false);
746 } finally {
747 Binder.restoreCallingIdentity(ident);
748 }
749 }
750 } catch (NumberFormatException ex) {
751 pw.println("Bad value: " + value);
752 return -1;
753 }
754 } break;
755 case "reset": {
756 getContext().enforceCallingOrSelfPermission(
757 android.Manifest.permission.DEVICE_POWER, null);
758 long ident = Binder.clearCallingIdentity();
759 try {
760 if (mUpdatesStopped) {
761 mUpdatesStopped = false;
762 mBatteryProps.set(mLastBatteryProps);
763 processValuesLocked(false);
764 }
765 } finally {
766 Binder.restoreCallingIdentity(ident);
767 }
768 } break;
769 default:
770 return shell.handleDefaultCommands(cmd);
771 }
772 return 0;
773 }
774
775 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700776 synchronized (mLock) {
777 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700778 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700779 if (mUpdatesStopped) {
780 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
781 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700782 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
783 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
784 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700785 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700786 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700787 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700788 pw.println(" status: " + mBatteryProps.batteryStatus);
789 pw.println(" health: " + mBatteryProps.batteryHealth);
790 pw.println(" present: " + mBatteryProps.batteryPresent);
791 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700792 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700793 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700794 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
795 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700796 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700797 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700798 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
801 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700802
Jeff Browna4d82042012-10-02 19:11:19 -0700803 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800804 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700805
Jeff Browna4d82042012-10-02 19:11:19 -0700806 private final int mBatteryLowARGB;
807 private final int mBatteryMediumARGB;
808 private final int mBatteryFullARGB;
809 private final int mBatteryLedOn;
810 private final int mBatteryLedOff;
811
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800812 public Led(Context context, LightsManager lights) {
813 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700814
Jeff Browna4d82042012-10-02 19:11:19 -0700815 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700816 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700817 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700818 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700819 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700820 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700821 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700822 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700823 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700824 com.android.internal.R.integer.config_notificationsBatteryLedOff);
825 }
826
827 /**
828 * Synchronize on BatteryService.
829 */
Jeff Browna4d82042012-10-02 19:11:19 -0700830 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700831 final int level = mBatteryProps.batteryLevel;
832 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700833 if (level < mLowBatteryWarningLevel) {
834 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
835 // Solid red when battery is charging
836 mBatteryLight.setColor(mBatteryLowARGB);
837 } else {
838 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800839 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700840 mBatteryLedOn, mBatteryLedOff);
841 }
842 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
843 || status == BatteryManager.BATTERY_STATUS_FULL) {
844 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
845 // Solid green when full or charging and nearly full
846 mBatteryLight.setColor(mBatteryFullARGB);
847 } else {
848 // Solid orange when charging and halfway full
849 mBatteryLight.setColor(mBatteryMediumARGB);
850 }
851 } else {
852 // No lights if not charging and not low
853 mBatteryLight.turnOff();
854 }
855 }
856 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700857
858 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700859 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800860 final long identity = Binder.clearCallingIdentity();
861 try {
862 BatteryService.this.update(props);
863 } finally {
864 Binder.restoreCallingIdentity(identity);
865 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700866 }
867 }
Jeff Brown21392762014-06-13 19:00:36 -0700868
869 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700870 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Brown21392762014-06-13 19:00:36 -0700871 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
872 != PackageManager.PERMISSION_GRANTED) {
873
874 pw.println("Permission Denial: can't dump Battery service from from pid="
875 + Binder.getCallingPid()
876 + ", uid=" + Binder.getCallingUid());
877 return;
878 }
879
Dianne Hackborn2e441072015-10-28 18:00:57 -0700880 dumpInternal(fd, pw, args);
881 }
882
883 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700884 FileDescriptor err, String[] args, ShellCallback callback,
885 ResultReceiver resultReceiver) {
886 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700887 }
888 }
889
890 private final class LocalService extends BatteryManagerInternal {
891 @Override
892 public boolean isPowered(int plugTypeSet) {
893 synchronized (mLock) {
894 return isPoweredLocked(plugTypeSet);
895 }
896 }
897
898 @Override
899 public int getPlugType() {
900 synchronized (mLock) {
901 return mPlugType;
902 }
903 }
904
905 @Override
906 public int getBatteryLevel() {
907 synchronized (mLock) {
908 return mBatteryProps.batteryLevel;
909 }
910 }
911
912 @Override
913 public boolean getBatteryLevelLow() {
914 synchronized (mLock) {
915 return mBatteryLevelLow;
916 }
917 }
918
919 @Override
920 public int getInvalidCharger() {
921 synchronized (mLock) {
922 return mInvalidCharger;
923 }
924 }
925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926}