blob: 5106c8d731ff0db693e6260546dbb4927ab995da [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
Sudheer Shanka292637f2017-09-25 10:36:23 -070023import android.os.PowerManager;
Dianne Hackborn2e441072015-10-28 18:00:57 -070024import android.os.ResultReceiver;
Dianne Hackborn354736e2016-08-22 17:00:05 -070025import android.os.ShellCallback;
Dianne Hackborn2e441072015-10-28 18:00:57 -070026import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.app.IBatteryStats;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060028import com.android.internal.util.DumpUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080030import com.android.server.lights.Light;
31import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Sudheer Shankadc589ac2016-11-10 15:30:17 -080033import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.ContentResolver;
35import android.content.Context;
36import android.content.Intent;
37import android.content.pm.PackageManager;
38import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070039import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070040import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070042import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070043import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070044import android.os.IBatteryPropertiesListener;
45import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080047import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.RemoteException;
49import android.os.ServiceManager;
50import android.os.SystemClock;
51import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070052import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.provider.Settings;
Netta Pe2a3cd82017-01-26 18:03:51 -080054import android.service.battery.BatteryServiceDumpProto;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080056import android.util.Slog;
Netta Pe2a3cd82017-01-26 18:03:51 -080057import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59import java.io.File;
60import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import java.io.FileOutputStream;
62import java.io.IOException;
63import java.io.PrintWriter;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66/**
67 * <p>BatteryService monitors the charging status, and charge level of the device
68 * battery. When these values change this service broadcasts the new values
69 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
70 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
71 * BATTERY_CHANGED} action.</p>
72 * <p>The new values are stored in the Intent data and can be retrieved by
73 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
74 * following keys:</p>
75 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
76 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
77 * <p>&quot;status&quot; - String, the current charging status.<br />
78 * <p>&quot;health&quot; - String, the current battery health.<br />
79 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
80 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
81 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
82 * into an AC power adapter; 2 if plugged in via USB.</p>
83 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
84 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
85 * a degree Centigrade</p>
86 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070087 *
88 * <p>
89 * The battery service may be called by the power manager while holding its locks so
90 * we take care to post all outcalls into the activity manager to a handler.
91 *
92 * FIXME: Ideally the power manager would perform all of its calls into the battery
93 * service asynchronously itself.
94 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
Jeff Brown21392762014-06-13 19:00:36 -070096public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080098
Jeff Browna4d82042012-10-02 19:11:19 -070099 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800100
Jeff Browna4d82042012-10-02 19:11:19 -0700101 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103 // Used locally for determining when to make a last ditch effort to log
104 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700105 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700107 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private static final String DUMPSYS_DATA_PATH = "/data/system/";
110
111 // This should probably be exposed in the API, though it's not critical
112 private static final int BATTERY_PLUGGED_NONE = 0;
113
114 private final Context mContext;
115 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700116 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700117 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
Jeff Browna4d82042012-10-02 19:11:19 -0700119 private final Object mLock = new Object();
120
Todd Poynor26faecc2013-05-22 18:54:48 -0700121 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800122 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private int mLastBatteryStatus;
125 private int mLastBatteryHealth;
126 private boolean mLastBatteryPresent;
127 private int mLastBatteryLevel;
128 private int mLastBatteryVoltage;
129 private int mLastBatteryTemperature;
130 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700131 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700132 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700133 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700134
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800135 private int mSequence = 1;
136
Jeff Browna4d82042012-10-02 19:11:19 -0700137 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700138 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400139
140 private int mLowBatteryWarningLevel;
141 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700142 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 private int mPlugType;
145 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700147 private boolean mBatteryLevelLow;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 private long mDischargeStartTime;
150 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700152 private boolean mUpdatesStopped;
153
Joe Onoratode1b3592010-10-25 20:36:47 -0700154 private Led mLed;
155
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700156 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700158 private ActivityManagerInternal mActivityManagerInternal;
159
Jeff Brown21392762014-06-13 19:00:36 -0700160 public BatteryService(Context context) {
161 super(context);
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700164 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700165 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 mBatteryStats = BatteryStatsService.getService();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700167 mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700169 mCriticalBatteryLevel = mContext.getResources().getInteger(
170 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400171 mLowBatteryWarningLevel = mContext.getResources().getInteger(
172 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700173 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
174 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700175 mShutdownBatteryTemperature = mContext.getResources().getInteger(
176 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400177
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400178 // watch for invalid charger messages if the invalid_charger switch exists
179 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700180 UEventObserver invalidChargerObserver = new UEventObserver() {
181 @Override
182 public void onUEvent(UEvent event) {
183 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
184 synchronized (mLock) {
185 if (mInvalidCharger != invalidCharger) {
186 mInvalidCharger = invalidCharger;
187 }
188 }
189 }
190 };
191 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700192 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400193 }
Jeff Brown21392762014-06-13 19:00:36 -0700194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
Jeff Brown21392762014-06-13 19:00:36 -0700196 @Override
197 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700198 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800199 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
200 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700201 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800202 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700203 } catch (RemoteException e) {
204 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700205 }
Jeff Brown21392762014-06-13 19:00:36 -0700206
Dianne Hackborn2e441072015-10-28 18:00:57 -0700207 mBinderService = new BinderService();
208 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700209 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
Jeff Brown21392762014-06-13 19:00:36 -0700212 @Override
213 public void onBootPhase(int phase) {
214 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
215 // check our power situation now that it is safe to display the shutdown dialog.
216 synchronized (mLock) {
217 ContentObserver obs = new ContentObserver(mHandler) {
218 @Override
219 public void onChange(boolean selfChange) {
220 synchronized (mLock) {
221 updateBatteryWarningLevelLocked();
222 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700223 }
Jeff Brown21392762014-06-13 19:00:36 -0700224 };
225 final ContentResolver resolver = mContext.getContentResolver();
226 resolver.registerContentObserver(Settings.Global.getUriFor(
227 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
228 false, obs, UserHandle.USER_ALL);
229 updateBatteryWarningLevelLocked();
230 }
Jeff Browna4d82042012-10-02 19:11:19 -0700231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233
Jeff Brown21392762014-06-13 19:00:36 -0700234 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700235 final ContentResolver resolver = mContext.getContentResolver();
236 int defWarnLevel = mContext.getResources().getInteger(
237 com.android.internal.R.integer.config_lowBatteryWarningLevel);
238 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
239 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
240 if (mLowBatteryWarningLevel == 0) {
241 mLowBatteryWarningLevel = defWarnLevel;
242 }
243 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
244 mLowBatteryWarningLevel = mCriticalBatteryLevel;
245 }
246 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
247 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
248 processValuesLocked(true);
249 }
250
Jeff Browna4d82042012-10-02 19:11:19 -0700251 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 // assume we are powered if battery state is unknown so
253 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700254 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 return true;
256 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700257 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700258 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700260 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700261 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700263 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700264 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Jeff Browna4d82042012-10-02 19:11:19 -0700266 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 }
268
Jeff Brown21392762014-06-13 19:00:36 -0700269 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700270 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
271 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
272
273 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
274 * - is just un-plugged (previously was plugged) and battery level is
275 * less than or equal to WARNING, or
276 * - is not plugged and battery level falls to WARNING boundary
277 * (becomes <= mLowBatteryWarningLevel).
278 */
279 return !plugged
280 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
281 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
282 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
283 }
284
Jeff Browna4d82042012-10-02 19:11:19 -0700285 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400286 // shut down gracefully if our battery is critically low and we are not powered.
287 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700288 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700289 mHandler.post(new Runnable() {
290 @Override
291 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700292 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700293 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
294 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
Sudheer Shanka292637f2017-09-25 10:36:23 -0700295 intent.putExtra(Intent.EXTRA_REASON,
296 PowerManager.SHUTDOWN_LOW_BATTERY);
Jeff Brown605ea692012-10-05 16:33:10 -0700297 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
298 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
299 }
300 }
301 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400302 }
303 }
304
Jeff Browna4d82042012-10-02 19:11:19 -0700305 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700306 // shut down gracefully if temperature is too high (> 68.0C by default)
307 // wait until the system has booted before attempting to display the
308 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700309 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700310 mHandler.post(new Runnable() {
311 @Override
312 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700313 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700314 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
315 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
Sudheer Shanka292637f2017-09-25 10:36:23 -0700316 intent.putExtra(Intent.EXTRA_REASON,
317 PowerManager.SHUTDOWN_BATTERY_THERMAL_STATE);
Jeff Brown605ea692012-10-05 16:33:10 -0700318 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
319 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
320 }
321 }
322 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700323 }
324 }
325
Todd Poynor26faecc2013-05-22 18:54:48 -0700326 private void update(BatteryProperties props) {
327 synchronized (mLock) {
328 if (!mUpdatesStopped) {
329 mBatteryProps = props;
330 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700331 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800332 } else {
333 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700334 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700335 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
Dianne Hackborn14272302014-06-10 23:13:02 -0700338 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700339 boolean logOutlier = false;
340 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700341
Todd Poynor26faecc2013-05-22 18:54:48 -0700342 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
343 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700345 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700347 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700348 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 } else {
350 mPlugType = BATTERY_PLUGGED_NONE;
351 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700352
Jeff Browna4d82042012-10-02 19:11:19 -0700353 if (DEBUG) {
354 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700355 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
356 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
357 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700358 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700359 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Todd Poynor26faecc2013-05-22 18:54:48 -0700360 + ", batteryStatus=" + mBatteryProps.batteryStatus
361 + ", batteryHealth=" + mBatteryProps.batteryHealth
362 + ", batteryPresent=" + mBatteryProps.batteryPresent
363 + ", batteryLevel=" + mBatteryProps.batteryLevel
364 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
365 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
Adam Lesinskiabcb9f32016-12-09 19:27:39 -0800366 + ", batteryChargeCounter=" + mBatteryProps.batteryChargeCounter
367 + ", batteryFullCharge=" + mBatteryProps.batteryFullCharge
Todd Poynor26faecc2013-05-22 18:54:48 -0700368 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700369 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
370 + ", mPlugType=" + mPlugType);
371 }
372
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700373 // Let the battery stats keep track of the current level.
374 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700375 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
376 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
Adam Lesinski041d9172016-12-12 12:03:56 -0800377 mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter,
378 mBatteryProps.batteryFullCharge);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700379 } catch (RemoteException e) {
380 // Should never happen.
381 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700382
Jeff Browna4d82042012-10-02 19:11:19 -0700383 shutdownIfNoPowerLocked();
384 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700385
Dianne Hackborn14272302014-06-10 23:13:02 -0700386 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700387 mBatteryProps.batteryHealth != mLastBatteryHealth ||
388 mBatteryProps.batteryPresent != mLastBatteryPresent ||
389 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700391 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
392 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700393 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700394 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700395 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700396 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 if (mPlugType != mLastPlugType) {
399 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
400 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 // There's no value in this data unless we've discharged at least once and the
403 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700404 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700405 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
406 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700408 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
413 // charging -> discharging or we just powered up
414 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700415 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700418 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
419 mBatteryProps.batteryHealth != mLastBatteryHealth ||
420 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700423 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
424 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700426 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700427 // Don't do this just from voltage or temperature changes, that is
428 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700430 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
433 mPlugType == BATTERY_PLUGGED_NONE) {
434 // We want to make sure we log discharge cycle outliers
435 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700436 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
437 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
Dianne Hackborn14272302014-06-10 23:13:02 -0700440 if (!mBatteryLevelLow) {
441 // Should we now switch in to low battery mode?
442 if (mPlugType == BATTERY_PLUGGED_NONE
443 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
444 mBatteryLevelLow = true;
445 }
446 } else {
447 // Should we now switch out of low battery mode?
448 if (mPlugType != BATTERY_PLUGGED_NONE) {
449 mBatteryLevelLow = false;
450 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
451 mBatteryLevelLow = false;
452 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
453 // If being forced, the previous state doesn't matter, we will just
454 // absolutely check to see if we are now above the warning level.
455 mBatteryLevelLow = false;
456 }
457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800459 mSequence++;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Christopher Tate06ba5542009-04-09 16:03:56 -0700461 // Separate broadcast is sent for power connected / not connected
462 // since the standard intent will not wake any applications and some
463 // applications may want to have smart behavior based on this.
464 if (mPlugType != 0 && mLastPlugType == 0) {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800465 final Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
466 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
467 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700468 mHandler.post(new Runnable() {
469 @Override
470 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700471 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
472 }
473 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700474 }
475 else if (mPlugType == 0 && mLastPlugType != 0) {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800476 final Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
477 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
478 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700479 mHandler.post(new Runnable() {
480 @Override
481 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700482 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
483 }
484 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700485 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200486
Dianne Hackborn14272302014-06-10 23:13:02 -0700487 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700488 mSentLowBatteryBroadcast = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800489 final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
490 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
491 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700492 mHandler.post(new Runnable() {
493 @Override
494 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700495 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
496 }
497 });
Dianne Hackborn532ea262017-03-17 17:50:55 -0700498 } else if (mSentLowBatteryBroadcast &&
499 mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700500 mSentLowBatteryBroadcast = false;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800501 final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
502 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
503 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700504 mHandler.post(new Runnable() {
505 @Override
506 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700507 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
508 }
509 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200510 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800511
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800512 // We are doing this after sending the above broadcasts, so anything processing
513 // them will get the new sequence number at that point. (See for example how testing
514 // of JobScheduler's BatteryController works.)
515 sendIntentLocked();
516
Joe Onoratode1b3592010-10-25 20:36:47 -0700517 // Update the battery LED
518 mLed.updateLightsLocked();
519
The Android Open Source Project10592532009-03-18 17:39:46 -0700520 // This needs to be done after sendIntent() so that we get the lastest battery stats.
521 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700522 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
Todd Poynor26faecc2013-05-22 18:54:48 -0700525 mLastBatteryStatus = mBatteryProps.batteryStatus;
526 mLastBatteryHealth = mBatteryProps.batteryHealth;
527 mLastBatteryPresent = mBatteryProps.batteryPresent;
528 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700529 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700530 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
531 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700532 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700533 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700534 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700535 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400536 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538 }
539
Jeff Browna4d82042012-10-02 19:11:19 -0700540 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700542 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800543 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
544 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
Todd Poynor26faecc2013-05-22 18:54:48 -0700546 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800548 intent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Todd Poynor26faecc2013-05-22 18:54:48 -0700549 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
550 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
551 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
552 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700553 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
554 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
555 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700556 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
557 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
558 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400559 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700560 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700561 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700562 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700563 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700564 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
565 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700566 ", health:" + mBatteryProps.batteryHealth +
567 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700568 ", voltage: " + mBatteryProps.batteryVoltage +
569 ", temperature: " + mBatteryProps.batteryTemperature +
570 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700571 ", AC powered:" + mBatteryProps.chargerAcOnline +
572 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700573 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700574 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700575 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700576 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
577 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
Jeff Brown605ea692012-10-05 16:33:10 -0700580 mHandler.post(new Runnable() {
581 @Override
582 public void run() {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800583 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jeff Brown605ea692012-10-05 16:33:10 -0700584 }
585 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587
Jeff Browna4d82042012-10-02 19:11:19 -0700588 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700589 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800590 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591
Dan Egnor18e93962010-02-10 19:27:58 -0800592 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
593 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
594
595 File dumpFile = null;
596 FileOutputStream dumpStream = null;
597 try {
598 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700599 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800600 dumpStream = new FileOutputStream(dumpFile);
601 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700602 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800603
604 // add dump file to drop box
605 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
606 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800607 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800608 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800610 } finally {
611 // make sure we clean up
612 if (dumpStream != null) {
613 try {
614 dumpStream.close();
615 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800616 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
Dan Egnor18e93962010-02-10 19:27:58 -0800618 }
619 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800621 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
Jeff Browna4d82042012-10-02 19:11:19 -0700626 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700628 String dischargeThresholdString = Settings.Global.getString(cr,
629 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
630 String durationThresholdString = Settings.Global.getString(cr,
631 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 if (dischargeThresholdString != null && durationThresholdString != null) {
634 try {
635 long durationThreshold = Long.parseLong(durationThresholdString);
636 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700638 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700640 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
Jeff Browna4d82042012-10-02 19:11:19 -0700642 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700644 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700645 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800647 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650 }
651 }
652
Jeff Browna4d82042012-10-02 19:11:19 -0700653 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700654 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700656 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700658 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
659 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700660 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700661 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800662 return com.android.internal.R.drawable.stat_sys_battery_charge;
663 } else {
664 return com.android.internal.R.drawable.stat_sys_battery;
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 } else {
667 return com.android.internal.R.drawable.stat_sys_battery_unknown;
668 }
669 }
670
Dianne Hackborn2e441072015-10-28 18:00:57 -0700671 class Shell extends ShellCommand {
672 @Override
673 public int onCommand(String cmd) {
674 return onShellCommand(this, cmd);
675 }
676
677 @Override
678 public void onHelp() {
679 PrintWriter pw = getOutPrintWriter();
680 dumpHelp(pw);
681 }
682 }
683
684 static void dumpHelp(PrintWriter pw) {
685 pw.println("Battery service (battery) commands:");
686 pw.println(" help");
687 pw.println(" Print this help text.");
Adam Lesinski29ddfe52017-03-29 19:29:00 -0700688 pw.println(" set [-f] [ac|usb|wireless|status|level|temp|present|invalid] <value>");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700689 pw.println(" Force a battery property value, freezing battery state.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800690 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
691 pw.println(" unplug [-f]");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700692 pw.println(" Force battery unplugged, freezing battery state.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800693 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
694 pw.println(" reset [-f]");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700695 pw.println(" Unfreeze battery state, returning to current hardware values.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800696 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
697 }
698
699 static final int OPTION_FORCE_UPDATE = 1<<0;
700
701 int parseOptions(Shell shell) {
702 String opt;
703 int opts = 0;
704 while ((opt = shell.getNextOption()) != null) {
705 if ("-f".equals(opt)) {
706 opts |= OPTION_FORCE_UPDATE;
707 }
708 }
709 return opts;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700710 }
711
712 int onShellCommand(Shell shell, String cmd) {
713 if (cmd == null) {
714 return shell.handleDefaultCommands(cmd);
715 }
716 PrintWriter pw = shell.getOutPrintWriter();
717 switch (cmd) {
718 case "unplug": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800719 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700720 getContext().enforceCallingOrSelfPermission(
721 android.Manifest.permission.DEVICE_POWER, null);
722 if (!mUpdatesStopped) {
723 mLastBatteryProps.set(mBatteryProps);
724 }
725 mBatteryProps.chargerAcOnline = false;
726 mBatteryProps.chargerUsbOnline = false;
727 mBatteryProps.chargerWirelessOnline = false;
728 long ident = Binder.clearCallingIdentity();
729 try {
730 mUpdatesStopped = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800731 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700732 } finally {
733 Binder.restoreCallingIdentity(ident);
734 }
735 } break;
736 case "set": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800737 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700738 getContext().enforceCallingOrSelfPermission(
739 android.Manifest.permission.DEVICE_POWER, null);
740 final String key = shell.getNextArg();
741 if (key == null) {
742 pw.println("No property specified");
743 return -1;
744
745 }
746 final String value = shell.getNextArg();
747 if (value == null) {
748 pw.println("No value specified");
749 return -1;
750
751 }
752 try {
753 if (!mUpdatesStopped) {
754 mLastBatteryProps.set(mBatteryProps);
755 }
756 boolean update = true;
757 switch (key) {
Christopher Tate630d98b2017-03-07 14:12:26 -0800758 case "present":
759 mBatteryProps.batteryPresent = Integer.parseInt(value) != 0;
760 break;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700761 case "ac":
762 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
763 break;
764 case "usb":
765 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
766 break;
767 case "wireless":
768 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
769 break;
770 case "status":
771 mBatteryProps.batteryStatus = Integer.parseInt(value);
772 break;
773 case "level":
774 mBatteryProps.batteryLevel = Integer.parseInt(value);
775 break;
Adam Lesinski29ddfe52017-03-29 19:29:00 -0700776 case "temp":
777 mBatteryProps.batteryTemperature = Integer.parseInt(value);
778 break;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700779 case "invalid":
780 mInvalidCharger = Integer.parseInt(value);
781 break;
782 default:
783 pw.println("Unknown set option: " + key);
784 update = false;
785 break;
786 }
787 if (update) {
788 long ident = Binder.clearCallingIdentity();
789 try {
790 mUpdatesStopped = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800791 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700792 } finally {
793 Binder.restoreCallingIdentity(ident);
794 }
795 }
796 } catch (NumberFormatException ex) {
797 pw.println("Bad value: " + value);
798 return -1;
799 }
800 } break;
801 case "reset": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800802 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700803 getContext().enforceCallingOrSelfPermission(
804 android.Manifest.permission.DEVICE_POWER, null);
805 long ident = Binder.clearCallingIdentity();
806 try {
807 if (mUpdatesStopped) {
808 mUpdatesStopped = false;
809 mBatteryProps.set(mLastBatteryProps);
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800810 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700811 }
812 } finally {
813 Binder.restoreCallingIdentity(ident);
814 }
815 } break;
816 default:
817 return shell.handleDefaultCommands(cmd);
818 }
819 return 0;
820 }
821
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800822 private void processValuesFromShellLocked(PrintWriter pw, int opts) {
823 processValuesLocked((opts & OPTION_FORCE_UPDATE) != 0);
824 if ((opts & OPTION_FORCE_UPDATE) != 0) {
825 pw.println(mSequence);
826 }
827 }
828
Dianne Hackborn2e441072015-10-28 18:00:57 -0700829 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700830 synchronized (mLock) {
831 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700832 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700833 if (mUpdatesStopped) {
834 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
835 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700836 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
837 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
838 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700839 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700840 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700841 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700842 pw.println(" status: " + mBatteryProps.batteryStatus);
843 pw.println(" health: " + mBatteryProps.batteryHealth);
844 pw.println(" present: " + mBatteryProps.batteryPresent);
845 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700846 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700847 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700848 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
849 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700850 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700851 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700852 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700856
Netta Pe2a3cd82017-01-26 18:03:51 -0800857 private void dumpProto(FileDescriptor fd) {
858 final ProtoOutputStream proto = new ProtoOutputStream(fd);
859
860 synchronized (mLock) {
861 proto.write(BatteryServiceDumpProto.ARE_UPDATES_STOPPED, mUpdatesStopped);
862 int batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_NONE;
863 if (mBatteryProps.chargerAcOnline) {
864 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_AC;
865 } else if (mBatteryProps.chargerUsbOnline) {
866 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_USB;
867 } else if (mBatteryProps.chargerWirelessOnline) {
868 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_WIRELESS;
869 }
870 proto.write(BatteryServiceDumpProto.PLUGGED, batteryPluggedValue);
871 proto.write(BatteryServiceDumpProto.MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
872 proto.write(BatteryServiceDumpProto.MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
873 proto.write(BatteryServiceDumpProto.CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
874 proto.write(BatteryServiceDumpProto.STATUS, mBatteryProps.batteryStatus);
875 proto.write(BatteryServiceDumpProto.HEALTH, mBatteryProps.batteryHealth);
876 proto.write(BatteryServiceDumpProto.IS_PRESENT, mBatteryProps.batteryPresent);
877 proto.write(BatteryServiceDumpProto.LEVEL, mBatteryProps.batteryLevel);
878 proto.write(BatteryServiceDumpProto.SCALE, BATTERY_SCALE);
879 proto.write(BatteryServiceDumpProto.VOLTAGE, mBatteryProps.batteryVoltage);
880 proto.write(BatteryServiceDumpProto.TEMPERATURE, mBatteryProps.batteryTemperature);
881 proto.write(BatteryServiceDumpProto.TECHNOLOGY, mBatteryProps.batteryTechnology);
882 }
883 proto.flush();
884 }
885
Jeff Browna4d82042012-10-02 19:11:19 -0700886 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800887 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700888
Jeff Browna4d82042012-10-02 19:11:19 -0700889 private final int mBatteryLowARGB;
890 private final int mBatteryMediumARGB;
891 private final int mBatteryFullARGB;
892 private final int mBatteryLedOn;
893 private final int mBatteryLedOff;
894
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800895 public Led(Context context, LightsManager lights) {
896 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700897
Jeff Browna4d82042012-10-02 19:11:19 -0700898 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700899 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700900 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700901 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700902 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700903 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700904 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700905 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700906 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700907 com.android.internal.R.integer.config_notificationsBatteryLedOff);
908 }
909
910 /**
911 * Synchronize on BatteryService.
912 */
Jeff Browna4d82042012-10-02 19:11:19 -0700913 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700914 final int level = mBatteryProps.batteryLevel;
915 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700916 if (level < mLowBatteryWarningLevel) {
917 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
918 // Solid red when battery is charging
919 mBatteryLight.setColor(mBatteryLowARGB);
920 } else {
921 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800922 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700923 mBatteryLedOn, mBatteryLedOff);
924 }
925 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
926 || status == BatteryManager.BATTERY_STATUS_FULL) {
927 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
928 // Solid green when full or charging and nearly full
929 mBatteryLight.setColor(mBatteryFullARGB);
930 } else {
931 // Solid orange when charging and halfway full
932 mBatteryLight.setColor(mBatteryMediumARGB);
933 }
934 } else {
935 // No lights if not charging and not low
936 mBatteryLight.turnOff();
937 }
938 }
939 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700940
941 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700942 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800943 final long identity = Binder.clearCallingIdentity();
944 try {
945 BatteryService.this.update(props);
946 } finally {
947 Binder.restoreCallingIdentity(identity);
948 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700949 }
950 }
Jeff Brown21392762014-06-13 19:00:36 -0700951
952 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700953 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600954 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown21392762014-06-13 19:00:36 -0700955
Netta Pe2a3cd82017-01-26 18:03:51 -0800956 if (args.length > 0 && "--proto".equals(args[0])) {
957 dumpProto(fd);
958 } else {
959 dumpInternal(fd, pw, args);
960 }
Dianne Hackborn2e441072015-10-28 18:00:57 -0700961 }
962
963 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700964 FileDescriptor err, String[] args, ShellCallback callback,
965 ResultReceiver resultReceiver) {
966 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700967 }
968 }
969
970 private final class LocalService extends BatteryManagerInternal {
971 @Override
972 public boolean isPowered(int plugTypeSet) {
973 synchronized (mLock) {
974 return isPoweredLocked(plugTypeSet);
975 }
976 }
977
978 @Override
979 public int getPlugType() {
980 synchronized (mLock) {
981 return mPlugType;
982 }
983 }
984
985 @Override
986 public int getBatteryLevel() {
987 synchronized (mLock) {
988 return mBatteryProps.batteryLevel;
989 }
990 }
991
992 @Override
993 public boolean getBatteryLevelLow() {
994 synchronized (mLock) {
995 return mBatteryLevelLow;
996 }
997 }
998
999 @Override
1000 public int getInvalidCharger() {
1001 synchronized (mLock) {
1002 return mInvalidCharger;
1003 }
1004 }
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006}