blob: dd550f22f99267d0ec0acf6bca944c6cd707c521 [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 Lesinski041d9172016-12-12 12:03:56 -0800367 mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter,
368 mBatteryProps.batteryFullCharge);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700369 } catch (RemoteException e) {
370 // Should never happen.
371 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700372
Jeff Browna4d82042012-10-02 19:11:19 -0700373 shutdownIfNoPowerLocked();
374 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700375
Dianne Hackborn14272302014-06-10 23:13:02 -0700376 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700377 mBatteryProps.batteryHealth != mLastBatteryHealth ||
378 mBatteryProps.batteryPresent != mLastBatteryPresent ||
379 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700381 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
382 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700383 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700384 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700385 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700386 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 if (mPlugType != mLastPlugType) {
389 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
390 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 // There's no value in this data unless we've discharged at least once and the
393 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700394 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700395 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
396 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700398 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
403 // charging -> discharging or we just powered up
404 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700405 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700408 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
409 mBatteryProps.batteryHealth != mLastBatteryHealth ||
410 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700413 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
414 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700416 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700417 // Don't do this just from voltage or temperature changes, that is
418 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700420 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
422 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
423 mPlugType == BATTERY_PLUGGED_NONE) {
424 // We want to make sure we log discharge cycle outliers
425 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700426 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
427 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
Dianne Hackborn14272302014-06-10 23:13:02 -0700430 if (!mBatteryLevelLow) {
431 // Should we now switch in to low battery mode?
432 if (mPlugType == BATTERY_PLUGGED_NONE
433 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
434 mBatteryLevelLow = true;
435 }
436 } else {
437 // Should we now switch out of low battery mode?
438 if (mPlugType != BATTERY_PLUGGED_NONE) {
439 mBatteryLevelLow = false;
440 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
441 mBatteryLevelLow = false;
442 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
443 // If being forced, the previous state doesn't matter, we will just
444 // absolutely check to see if we are now above the warning level.
445 mBatteryLevelLow = false;
446 }
447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Jeff Browna4d82042012-10-02 19:11:19 -0700449 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
Christopher Tate06ba5542009-04-09 16:03:56 -0700451 // Separate broadcast is sent for power connected / not connected
452 // since the standard intent will not wake any applications and some
453 // applications may want to have smart behavior based on this.
454 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700455 mHandler.post(new Runnable() {
456 @Override
457 public void run() {
458 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
459 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
460 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
461 }
462 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700463 }
464 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700465 mHandler.post(new Runnable() {
466 @Override
467 public void run() {
468 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
469 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
470 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
471 }
472 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700473 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200474
Dianne Hackborn14272302014-06-10 23:13:02 -0700475 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700476 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700477 mHandler.post(new Runnable() {
478 @Override
479 public void run() {
480 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
481 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
482 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
483 }
484 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400485 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700486 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700487 mHandler.post(new Runnable() {
488 @Override
489 public void run() {
490 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
491 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
492 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
493 }
494 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
Joe Onoratode1b3592010-10-25 20:36:47 -0700497 // Update the battery LED
498 mLed.updateLightsLocked();
499
The Android Open Source Project10592532009-03-18 17:39:46 -0700500 // This needs to be done after sendIntent() so that we get the lastest battery stats.
501 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700502 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
Todd Poynor26faecc2013-05-22 18:54:48 -0700505 mLastBatteryStatus = mBatteryProps.batteryStatus;
506 mLastBatteryHealth = mBatteryProps.batteryHealth;
507 mLastBatteryPresent = mBatteryProps.batteryPresent;
508 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700509 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700510 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
511 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700512 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700513 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700514 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700515 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400516 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518 }
519
Jeff Browna4d82042012-10-02 19:11:19 -0700520 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700522 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800523 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
524 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800525
Todd Poynor26faecc2013-05-22 18:54:48 -0700526 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527
Todd Poynor26faecc2013-05-22 18:54:48 -0700528 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
529 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
530 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
531 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700532 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
533 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
534 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700535 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
536 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
537 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400538 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700539 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700540 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700541 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700542 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700543 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
544 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700545 ", health:" + mBatteryProps.batteryHealth +
546 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700547 ", voltage: " + mBatteryProps.batteryVoltage +
548 ", temperature: " + mBatteryProps.batteryTemperature +
549 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700550 ", AC powered:" + mBatteryProps.chargerAcOnline +
551 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700552 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700553 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700554 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700555 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
556 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558
Jeff Brown605ea692012-10-05 16:33:10 -0700559 mHandler.post(new Runnable() {
560 @Override
561 public void run() {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800562 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jeff Brown605ea692012-10-05 16:33:10 -0700563 }
564 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
Jeff Browna4d82042012-10-02 19:11:19 -0700567 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700568 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800569 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Dan Egnor18e93962010-02-10 19:27:58 -0800571 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
572 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
573
574 File dumpFile = null;
575 FileOutputStream dumpStream = null;
576 try {
577 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700578 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800579 dumpStream = new FileOutputStream(dumpFile);
580 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700581 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800582
583 // add dump file to drop box
584 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
585 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800586 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800587 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800588 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800589 } finally {
590 // make sure we clean up
591 if (dumpStream != null) {
592 try {
593 dumpStream.close();
594 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800595 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
Dan Egnor18e93962010-02-10 19:27:58 -0800597 }
598 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800599 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800600 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602 }
603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
Jeff Browna4d82042012-10-02 19:11:19 -0700605 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700607 String dischargeThresholdString = Settings.Global.getString(cr,
608 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
609 String durationThresholdString = Settings.Global.getString(cr,
610 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 if (dischargeThresholdString != null && durationThresholdString != null) {
613 try {
614 long durationThreshold = Long.parseLong(durationThresholdString);
615 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800616 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700617 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700619 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
Jeff Browna4d82042012-10-02 19:11:19 -0700621 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700623 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700624 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800626 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629 }
630 }
631
Jeff Browna4d82042012-10-02 19:11:19 -0700632 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700633 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700635 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700637 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
638 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700639 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700640 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800641 return com.android.internal.R.drawable.stat_sys_battery_charge;
642 } else {
643 return com.android.internal.R.drawable.stat_sys_battery;
644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 } else {
646 return com.android.internal.R.drawable.stat_sys_battery_unknown;
647 }
648 }
649
Dianne Hackborn2e441072015-10-28 18:00:57 -0700650 class Shell extends ShellCommand {
651 @Override
652 public int onCommand(String cmd) {
653 return onShellCommand(this, cmd);
654 }
655
656 @Override
657 public void onHelp() {
658 PrintWriter pw = getOutPrintWriter();
659 dumpHelp(pw);
660 }
661 }
662
663 static void dumpHelp(PrintWriter pw) {
664 pw.println("Battery service (battery) commands:");
665 pw.println(" help");
666 pw.println(" Print this help text.");
667 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
668 pw.println(" Force a battery property value, freezing battery state.");
669 pw.println(" unplug");
670 pw.println(" Force battery unplugged, freezing battery state.");
671 pw.println(" reset");
672 pw.println(" Unfreeze battery state, returning to current hardware values.");
673 }
674
675 int onShellCommand(Shell shell, String cmd) {
676 if (cmd == null) {
677 return shell.handleDefaultCommands(cmd);
678 }
679 PrintWriter pw = shell.getOutPrintWriter();
680 switch (cmd) {
681 case "unplug": {
682 getContext().enforceCallingOrSelfPermission(
683 android.Manifest.permission.DEVICE_POWER, null);
684 if (!mUpdatesStopped) {
685 mLastBatteryProps.set(mBatteryProps);
686 }
687 mBatteryProps.chargerAcOnline = false;
688 mBatteryProps.chargerUsbOnline = false;
689 mBatteryProps.chargerWirelessOnline = false;
690 long ident = Binder.clearCallingIdentity();
691 try {
692 mUpdatesStopped = true;
693 processValuesLocked(false);
694 } finally {
695 Binder.restoreCallingIdentity(ident);
696 }
697 } break;
698 case "set": {
699 getContext().enforceCallingOrSelfPermission(
700 android.Manifest.permission.DEVICE_POWER, null);
701 final String key = shell.getNextArg();
702 if (key == null) {
703 pw.println("No property specified");
704 return -1;
705
706 }
707 final String value = shell.getNextArg();
708 if (value == null) {
709 pw.println("No value specified");
710 return -1;
711
712 }
713 try {
714 if (!mUpdatesStopped) {
715 mLastBatteryProps.set(mBatteryProps);
716 }
717 boolean update = true;
718 switch (key) {
719 case "ac":
720 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
721 break;
722 case "usb":
723 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
724 break;
725 case "wireless":
726 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
727 break;
728 case "status":
729 mBatteryProps.batteryStatus = Integer.parseInt(value);
730 break;
731 case "level":
732 mBatteryProps.batteryLevel = Integer.parseInt(value);
733 break;
734 case "invalid":
735 mInvalidCharger = Integer.parseInt(value);
736 break;
737 default:
738 pw.println("Unknown set option: " + key);
739 update = false;
740 break;
741 }
742 if (update) {
743 long ident = Binder.clearCallingIdentity();
744 try {
745 mUpdatesStopped = true;
746 processValuesLocked(false);
747 } finally {
748 Binder.restoreCallingIdentity(ident);
749 }
750 }
751 } catch (NumberFormatException ex) {
752 pw.println("Bad value: " + value);
753 return -1;
754 }
755 } break;
756 case "reset": {
757 getContext().enforceCallingOrSelfPermission(
758 android.Manifest.permission.DEVICE_POWER, null);
759 long ident = Binder.clearCallingIdentity();
760 try {
761 if (mUpdatesStopped) {
762 mUpdatesStopped = false;
763 mBatteryProps.set(mLastBatteryProps);
764 processValuesLocked(false);
765 }
766 } finally {
767 Binder.restoreCallingIdentity(ident);
768 }
769 } break;
770 default:
771 return shell.handleDefaultCommands(cmd);
772 }
773 return 0;
774 }
775
776 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700777 synchronized (mLock) {
778 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700779 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700780 if (mUpdatesStopped) {
781 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
782 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700783 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
784 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
785 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700786 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700787 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700788 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700789 pw.println(" status: " + mBatteryProps.batteryStatus);
790 pw.println(" health: " + mBatteryProps.batteryHealth);
791 pw.println(" present: " + mBatteryProps.batteryPresent);
792 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700793 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700794 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700795 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
796 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700797 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700798 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700799 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700803
Jeff Browna4d82042012-10-02 19:11:19 -0700804 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800805 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700806
Jeff Browna4d82042012-10-02 19:11:19 -0700807 private final int mBatteryLowARGB;
808 private final int mBatteryMediumARGB;
809 private final int mBatteryFullARGB;
810 private final int mBatteryLedOn;
811 private final int mBatteryLedOff;
812
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800813 public Led(Context context, LightsManager lights) {
814 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700815
Jeff Browna4d82042012-10-02 19:11:19 -0700816 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700817 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700818 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700819 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700820 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700821 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700822 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700823 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700824 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700825 com.android.internal.R.integer.config_notificationsBatteryLedOff);
826 }
827
828 /**
829 * Synchronize on BatteryService.
830 */
Jeff Browna4d82042012-10-02 19:11:19 -0700831 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700832 final int level = mBatteryProps.batteryLevel;
833 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700834 if (level < mLowBatteryWarningLevel) {
835 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
836 // Solid red when battery is charging
837 mBatteryLight.setColor(mBatteryLowARGB);
838 } else {
839 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800840 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700841 mBatteryLedOn, mBatteryLedOff);
842 }
843 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
844 || status == BatteryManager.BATTERY_STATUS_FULL) {
845 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
846 // Solid green when full or charging and nearly full
847 mBatteryLight.setColor(mBatteryFullARGB);
848 } else {
849 // Solid orange when charging and halfway full
850 mBatteryLight.setColor(mBatteryMediumARGB);
851 }
852 } else {
853 // No lights if not charging and not low
854 mBatteryLight.turnOff();
855 }
856 }
857 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700858
859 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700860 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800861 final long identity = Binder.clearCallingIdentity();
862 try {
863 BatteryService.this.update(props);
864 } finally {
865 Binder.restoreCallingIdentity(identity);
866 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700867 }
868 }
Jeff Brown21392762014-06-13 19:00:36 -0700869
870 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700871 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Brown21392762014-06-13 19:00:36 -0700872 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
873 != PackageManager.PERMISSION_GRANTED) {
874
875 pw.println("Permission Denial: can't dump Battery service from from pid="
876 + Binder.getCallingPid()
877 + ", uid=" + Binder.getCallingUid());
878 return;
879 }
880
Dianne Hackborn2e441072015-10-28 18:00:57 -0700881 dumpInternal(fd, pw, args);
882 }
883
884 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700885 FileDescriptor err, String[] args, ShellCallback callback,
886 ResultReceiver resultReceiver) {
887 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700888 }
889 }
890
891 private final class LocalService extends BatteryManagerInternal {
892 @Override
893 public boolean isPowered(int plugTypeSet) {
894 synchronized (mLock) {
895 return isPoweredLocked(plugTypeSet);
896 }
897 }
898
899 @Override
900 public int getPlugType() {
901 synchronized (mLock) {
902 return mPlugType;
903 }
904 }
905
906 @Override
907 public int getBatteryLevel() {
908 synchronized (mLock) {
909 return mBatteryProps.batteryLevel;
910 }
911 }
912
913 @Override
914 public boolean getBatteryLevelLow() {
915 synchronized (mLock) {
916 return mBatteryLevelLow;
917 }
918 }
919
920 @Override
921 public int getInvalidCharger() {
922 synchronized (mLock) {
923 return mInvalidCharger;
924 }
925 }
926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927}