blob: c9dd116c1b0ebe66576ef55fb5c19ff4226d3390 [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;
Netta Pe2a3cd82017-01-26 18:03:51 -080052import android.service.battery.BatteryServiceDumpProto;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080054import android.util.Slog;
Netta Pe2a3cd82017-01-26 18:03:51 -080055import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57import java.io.File;
58import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.FileOutputStream;
60import java.io.IOException;
61import java.io.PrintWriter;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64/**
65 * <p>BatteryService monitors the charging status, and charge level of the device
66 * battery. When these values change this service broadcasts the new values
67 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
68 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
69 * BATTERY_CHANGED} action.</p>
70 * <p>The new values are stored in the Intent data and can be retrieved by
71 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
72 * following keys:</p>
73 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
74 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
75 * <p>&quot;status&quot; - String, the current charging status.<br />
76 * <p>&quot;health&quot; - String, the current battery health.<br />
77 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
78 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
79 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
80 * into an AC power adapter; 2 if plugged in via USB.</p>
81 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
82 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
83 * a degree Centigrade</p>
84 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070085 *
86 * <p>
87 * The battery service may be called by the power manager while holding its locks so
88 * we take care to post all outcalls into the activity manager to a handler.
89 *
90 * FIXME: Ideally the power manager would perform all of its calls into the battery
91 * service asynchronously itself.
92 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 */
Jeff Brown21392762014-06-13 19:00:36 -070094public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080096
Jeff Browna4d82042012-10-02 19:11:19 -070097 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080098
Jeff Browna4d82042012-10-02 19:11:19 -070099 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101 // Used locally for determining when to make a last ditch effort to log
102 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700103 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700105 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private static final String DUMPSYS_DATA_PATH = "/data/system/";
108
109 // This should probably be exposed in the API, though it's not critical
110 private static final int BATTERY_PLUGGED_NONE = 0;
111
112 private final Context mContext;
113 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700114 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700115 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
Jeff Browna4d82042012-10-02 19:11:19 -0700117 private final Object mLock = new Object();
118
Todd Poynor26faecc2013-05-22 18:54:48 -0700119 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800120 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 private int mLastBatteryStatus;
123 private int mLastBatteryHealth;
124 private boolean mLastBatteryPresent;
125 private int mLastBatteryLevel;
126 private int mLastBatteryVoltage;
127 private int mLastBatteryTemperature;
128 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700129 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700130 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700131 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700132
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800133 private int mSequence = 1;
134
Jeff Browna4d82042012-10-02 19:11:19 -0700135 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700136 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400137
138 private int mLowBatteryWarningLevel;
139 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700140 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 private int mPlugType;
143 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700145 private boolean mBatteryLevelLow;
146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 private long mDischargeStartTime;
148 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700150 private boolean mUpdatesStopped;
151
Joe Onoratode1b3592010-10-25 20:36:47 -0700152 private Led mLed;
153
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700154 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700156 private ActivityManagerInternal mActivityManagerInternal;
157
Jeff Brown21392762014-06-13 19:00:36 -0700158 public BatteryService(Context context) {
159 super(context);
160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700162 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700163 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 mBatteryStats = BatteryStatsService.getService();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700165 mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700167 mCriticalBatteryLevel = mContext.getResources().getInteger(
168 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400169 mLowBatteryWarningLevel = mContext.getResources().getInteger(
170 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700171 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
172 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700173 mShutdownBatteryTemperature = mContext.getResources().getInteger(
174 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400175
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400176 // watch for invalid charger messages if the invalid_charger switch exists
177 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700178 UEventObserver invalidChargerObserver = new UEventObserver() {
179 @Override
180 public void onUEvent(UEvent event) {
181 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
182 synchronized (mLock) {
183 if (mInvalidCharger != invalidCharger) {
184 mInvalidCharger = invalidCharger;
185 }
186 }
187 }
188 };
189 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700190 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400191 }
Jeff Brown21392762014-06-13 19:00:36 -0700192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Jeff Brown21392762014-06-13 19:00:36 -0700194 @Override
195 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700196 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800197 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
198 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700199 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800200 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700201 } catch (RemoteException e) {
202 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700203 }
Jeff Brown21392762014-06-13 19:00:36 -0700204
Dianne Hackborn2e441072015-10-28 18:00:57 -0700205 mBinderService = new BinderService();
206 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700207 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 }
209
Jeff Brown21392762014-06-13 19:00:36 -0700210 @Override
211 public void onBootPhase(int phase) {
212 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
213 // check our power situation now that it is safe to display the shutdown dialog.
214 synchronized (mLock) {
215 ContentObserver obs = new ContentObserver(mHandler) {
216 @Override
217 public void onChange(boolean selfChange) {
218 synchronized (mLock) {
219 updateBatteryWarningLevelLocked();
220 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700221 }
Jeff Brown21392762014-06-13 19:00:36 -0700222 };
223 final ContentResolver resolver = mContext.getContentResolver();
224 resolver.registerContentObserver(Settings.Global.getUriFor(
225 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
226 false, obs, UserHandle.USER_ALL);
227 updateBatteryWarningLevelLocked();
228 }
Jeff Browna4d82042012-10-02 19:11:19 -0700229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231
Jeff Brown21392762014-06-13 19:00:36 -0700232 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700233 final ContentResolver resolver = mContext.getContentResolver();
234 int defWarnLevel = mContext.getResources().getInteger(
235 com.android.internal.R.integer.config_lowBatteryWarningLevel);
236 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
237 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
238 if (mLowBatteryWarningLevel == 0) {
239 mLowBatteryWarningLevel = defWarnLevel;
240 }
241 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
242 mLowBatteryWarningLevel = mCriticalBatteryLevel;
243 }
244 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
245 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
246 processValuesLocked(true);
247 }
248
Jeff Browna4d82042012-10-02 19:11:19 -0700249 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 // assume we are powered if battery state is unknown so
251 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700252 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 return true;
254 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700255 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700256 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700258 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700259 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700261 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700262 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
Jeff Browna4d82042012-10-02 19:11:19 -0700264 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266
Jeff Brown21392762014-06-13 19:00:36 -0700267 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700268 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
269 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
270
271 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
272 * - is just un-plugged (previously was plugged) and battery level is
273 * less than or equal to WARNING, or
274 * - is not plugged and battery level falls to WARNING boundary
275 * (becomes <= mLowBatteryWarningLevel).
276 */
277 return !plugged
278 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
279 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
280 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
281 }
282
Jeff Browna4d82042012-10-02 19:11:19 -0700283 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400284 // shut down gracefully if our battery is critically low and we are not powered.
285 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700286 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700287 mHandler.post(new Runnable() {
288 @Override
289 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700290 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700291 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
292 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
293 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
294 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
295 }
296 }
297 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400298 }
299 }
300
Jeff Browna4d82042012-10-02 19:11:19 -0700301 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700302 // shut down gracefully if temperature is too high (> 68.0C by default)
303 // wait until the system has booted before attempting to display the
304 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700305 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700306 mHandler.post(new Runnable() {
307 @Override
308 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700309 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700310 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
311 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
312 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
313 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
314 }
315 }
316 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700317 }
318 }
319
Todd Poynor26faecc2013-05-22 18:54:48 -0700320 private void update(BatteryProperties props) {
321 synchronized (mLock) {
322 if (!mUpdatesStopped) {
323 mBatteryProps = props;
324 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700325 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800326 } else {
327 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700328 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700329 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
Dianne Hackborn14272302014-06-10 23:13:02 -0700332 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700333 boolean logOutlier = false;
334 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700335
Todd Poynor26faecc2013-05-22 18:54:48 -0700336 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
337 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700339 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700341 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700342 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 } else {
344 mPlugType = BATTERY_PLUGGED_NONE;
345 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700346
Jeff Browna4d82042012-10-02 19:11:19 -0700347 if (DEBUG) {
348 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700349 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
350 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
351 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700352 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700353 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Todd Poynor26faecc2013-05-22 18:54:48 -0700354 + ", batteryStatus=" + mBatteryProps.batteryStatus
355 + ", batteryHealth=" + mBatteryProps.batteryHealth
356 + ", batteryPresent=" + mBatteryProps.batteryPresent
357 + ", batteryLevel=" + mBatteryProps.batteryLevel
358 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
359 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
Adam Lesinskiabcb9f32016-12-09 19:27:39 -0800360 + ", batteryChargeCounter=" + mBatteryProps.batteryChargeCounter
361 + ", batteryFullCharge=" + mBatteryProps.batteryFullCharge
Todd Poynor26faecc2013-05-22 18:54:48 -0700362 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700363 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
364 + ", mPlugType=" + mPlugType);
365 }
366
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700367 // Let the battery stats keep track of the current level.
368 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700369 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
370 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
Adam Lesinski041d9172016-12-12 12:03:56 -0800371 mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter,
372 mBatteryProps.batteryFullCharge);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700373 } catch (RemoteException e) {
374 // Should never happen.
375 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700376
Jeff Browna4d82042012-10-02 19:11:19 -0700377 shutdownIfNoPowerLocked();
378 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700379
Dianne Hackborn14272302014-06-10 23:13:02 -0700380 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700381 mBatteryProps.batteryHealth != mLastBatteryHealth ||
382 mBatteryProps.batteryPresent != mLastBatteryPresent ||
383 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700385 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
386 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700387 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700388 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700389 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700390 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 if (mPlugType != mLastPlugType) {
393 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
394 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 // There's no value in this data unless we've discharged at least once and the
397 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700398 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700399 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
400 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700402 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
407 // charging -> discharging or we just powered up
408 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700409 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700412 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
413 mBatteryProps.batteryHealth != mLastBatteryHealth ||
414 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700417 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
418 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700420 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700421 // Don't do this just from voltage or temperature changes, that is
422 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700424 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 }
426 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
427 mPlugType == BATTERY_PLUGGED_NONE) {
428 // We want to make sure we log discharge cycle outliers
429 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700430 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
431 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800433
Dianne Hackborn14272302014-06-10 23:13:02 -0700434 if (!mBatteryLevelLow) {
435 // Should we now switch in to low battery mode?
436 if (mPlugType == BATTERY_PLUGGED_NONE
437 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
438 mBatteryLevelLow = true;
439 }
440 } else {
441 // Should we now switch out of low battery mode?
442 if (mPlugType != BATTERY_PLUGGED_NONE) {
443 mBatteryLevelLow = false;
444 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
445 mBatteryLevelLow = false;
446 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
447 // If being forced, the previous state doesn't matter, we will just
448 // absolutely check to see if we are now above the warning level.
449 mBatteryLevelLow = false;
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800453 mSequence++;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
Christopher Tate06ba5542009-04-09 16:03:56 -0700455 // Separate broadcast is sent for power connected / not connected
456 // since the standard intent will not wake any applications and some
457 // applications may want to have smart behavior based on this.
458 if (mPlugType != 0 && mLastPlugType == 0) {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800459 final Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
460 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
461 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700462 mHandler.post(new Runnable() {
463 @Override
464 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700465 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
466 }
467 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700468 }
469 else if (mPlugType == 0 && mLastPlugType != 0) {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800470 final Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
471 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
472 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700473 mHandler.post(new Runnable() {
474 @Override
475 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700476 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
477 }
478 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700479 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200480
Dianne Hackborn14272302014-06-10 23:13:02 -0700481 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700482 mSentLowBatteryBroadcast = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800483 final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
484 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
485 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700486 mHandler.post(new Runnable() {
487 @Override
488 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700489 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
490 }
491 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400492 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700493 mSentLowBatteryBroadcast = false;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800494 final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
495 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
496 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700497 mHandler.post(new Runnable() {
498 @Override
499 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700500 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
501 }
502 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800505 // We are doing this after sending the above broadcasts, so anything processing
506 // them will get the new sequence number at that point. (See for example how testing
507 // of JobScheduler's BatteryController works.)
508 sendIntentLocked();
509
Joe Onoratode1b3592010-10-25 20:36:47 -0700510 // Update the battery LED
511 mLed.updateLightsLocked();
512
The Android Open Source Project10592532009-03-18 17:39:46 -0700513 // This needs to be done after sendIntent() so that we get the lastest battery stats.
514 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700515 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800517
Todd Poynor26faecc2013-05-22 18:54:48 -0700518 mLastBatteryStatus = mBatteryProps.batteryStatus;
519 mLastBatteryHealth = mBatteryProps.batteryHealth;
520 mLastBatteryPresent = mBatteryProps.batteryPresent;
521 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700522 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700523 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
524 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700525 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700526 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700527 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700528 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400529 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531 }
532
Jeff Browna4d82042012-10-02 19:11:19 -0700533 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700535 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800536 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
537 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800538
Todd Poynor26faecc2013-05-22 18:54:48 -0700539 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800541 intent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Todd Poynor26faecc2013-05-22 18:54:48 -0700542 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
543 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
544 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
545 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700546 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
547 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
548 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700549 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
550 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
551 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400552 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700553 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700554 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700555 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700556 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700557 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
558 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700559 ", health:" + mBatteryProps.batteryHealth +
560 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700561 ", voltage: " + mBatteryProps.batteryVoltage +
562 ", temperature: " + mBatteryProps.batteryTemperature +
563 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700564 ", AC powered:" + mBatteryProps.chargerAcOnline +
565 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700566 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700567 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700568 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700569 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
570 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
Jeff Brown605ea692012-10-05 16:33:10 -0700573 mHandler.post(new Runnable() {
574 @Override
575 public void run() {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800576 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jeff Brown605ea692012-10-05 16:33:10 -0700577 }
578 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580
Jeff Browna4d82042012-10-02 19:11:19 -0700581 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700582 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800583 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584
Dan Egnor18e93962010-02-10 19:27:58 -0800585 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
586 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
587
588 File dumpFile = null;
589 FileOutputStream dumpStream = null;
590 try {
591 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700592 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800593 dumpStream = new FileOutputStream(dumpFile);
594 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700595 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800596
597 // add dump file to drop box
598 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
599 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800601 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800602 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800603 } finally {
604 // make sure we clean up
605 if (dumpStream != null) {
606 try {
607 dumpStream.close();
608 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
Dan Egnor18e93962010-02-10 19:27:58 -0800611 }
612 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800613 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800614 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616 }
617 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800618
Jeff Browna4d82042012-10-02 19:11:19 -0700619 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700621 String dischargeThresholdString = Settings.Global.getString(cr,
622 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
623 String durationThresholdString = Settings.Global.getString(cr,
624 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 if (dischargeThresholdString != null && durationThresholdString != null) {
627 try {
628 long durationThreshold = Long.parseLong(durationThresholdString);
629 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800630 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700631 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700633 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
Jeff Browna4d82042012-10-02 19:11:19 -0700635 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700637 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700638 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800640 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643 }
644 }
645
Jeff Browna4d82042012-10-02 19:11:19 -0700646 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700647 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700649 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700651 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
652 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700653 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700654 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800655 return com.android.internal.R.drawable.stat_sys_battery_charge;
656 } else {
657 return com.android.internal.R.drawable.stat_sys_battery;
658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 } else {
660 return com.android.internal.R.drawable.stat_sys_battery_unknown;
661 }
662 }
663
Dianne Hackborn2e441072015-10-28 18:00:57 -0700664 class Shell extends ShellCommand {
665 @Override
666 public int onCommand(String cmd) {
667 return onShellCommand(this, cmd);
668 }
669
670 @Override
671 public void onHelp() {
672 PrintWriter pw = getOutPrintWriter();
673 dumpHelp(pw);
674 }
675 }
676
677 static void dumpHelp(PrintWriter pw) {
678 pw.println("Battery service (battery) commands:");
679 pw.println(" help");
680 pw.println(" Print this help text.");
Christopher Tate630d98b2017-03-07 14:12:26 -0800681 pw.println(" set [-f] [ac|usb|wireless|status|level|present|invalid] <value>");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700682 pw.println(" Force a battery property value, freezing battery state.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800683 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
684 pw.println(" unplug [-f]");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700685 pw.println(" Force battery unplugged, freezing battery state.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800686 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
687 pw.println(" reset [-f]");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700688 pw.println(" Unfreeze battery state, returning to current hardware values.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800689 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
690 }
691
692 static final int OPTION_FORCE_UPDATE = 1<<0;
693
694 int parseOptions(Shell shell) {
695 String opt;
696 int opts = 0;
697 while ((opt = shell.getNextOption()) != null) {
698 if ("-f".equals(opt)) {
699 opts |= OPTION_FORCE_UPDATE;
700 }
701 }
702 return opts;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700703 }
704
705 int onShellCommand(Shell shell, String cmd) {
706 if (cmd == null) {
707 return shell.handleDefaultCommands(cmd);
708 }
709 PrintWriter pw = shell.getOutPrintWriter();
710 switch (cmd) {
711 case "unplug": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800712 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700713 getContext().enforceCallingOrSelfPermission(
714 android.Manifest.permission.DEVICE_POWER, null);
715 if (!mUpdatesStopped) {
716 mLastBatteryProps.set(mBatteryProps);
717 }
718 mBatteryProps.chargerAcOnline = false;
719 mBatteryProps.chargerUsbOnline = false;
720 mBatteryProps.chargerWirelessOnline = false;
721 long ident = Binder.clearCallingIdentity();
722 try {
723 mUpdatesStopped = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800724 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700725 } finally {
726 Binder.restoreCallingIdentity(ident);
727 }
728 } break;
729 case "set": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800730 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700731 getContext().enforceCallingOrSelfPermission(
732 android.Manifest.permission.DEVICE_POWER, null);
733 final String key = shell.getNextArg();
734 if (key == null) {
735 pw.println("No property specified");
736 return -1;
737
738 }
739 final String value = shell.getNextArg();
740 if (value == null) {
741 pw.println("No value specified");
742 return -1;
743
744 }
745 try {
746 if (!mUpdatesStopped) {
747 mLastBatteryProps.set(mBatteryProps);
748 }
749 boolean update = true;
750 switch (key) {
Christopher Tate630d98b2017-03-07 14:12:26 -0800751 case "present":
752 mBatteryProps.batteryPresent = Integer.parseInt(value) != 0;
753 break;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700754 case "ac":
755 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
756 break;
757 case "usb":
758 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
759 break;
760 case "wireless":
761 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
762 break;
763 case "status":
764 mBatteryProps.batteryStatus = Integer.parseInt(value);
765 break;
766 case "level":
767 mBatteryProps.batteryLevel = Integer.parseInt(value);
768 break;
769 case "invalid":
770 mInvalidCharger = Integer.parseInt(value);
771 break;
772 default:
773 pw.println("Unknown set option: " + key);
774 update = false;
775 break;
776 }
777 if (update) {
778 long ident = Binder.clearCallingIdentity();
779 try {
780 mUpdatesStopped = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800781 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700782 } finally {
783 Binder.restoreCallingIdentity(ident);
784 }
785 }
786 } catch (NumberFormatException ex) {
787 pw.println("Bad value: " + value);
788 return -1;
789 }
790 } break;
791 case "reset": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800792 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700793 getContext().enforceCallingOrSelfPermission(
794 android.Manifest.permission.DEVICE_POWER, null);
795 long ident = Binder.clearCallingIdentity();
796 try {
797 if (mUpdatesStopped) {
798 mUpdatesStopped = false;
799 mBatteryProps.set(mLastBatteryProps);
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800800 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700801 }
802 } finally {
803 Binder.restoreCallingIdentity(ident);
804 }
805 } break;
806 default:
807 return shell.handleDefaultCommands(cmd);
808 }
809 return 0;
810 }
811
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800812 private void processValuesFromShellLocked(PrintWriter pw, int opts) {
813 processValuesLocked((opts & OPTION_FORCE_UPDATE) != 0);
814 if ((opts & OPTION_FORCE_UPDATE) != 0) {
815 pw.println(mSequence);
816 }
817 }
818
Dianne Hackborn2e441072015-10-28 18:00:57 -0700819 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700820 synchronized (mLock) {
821 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700822 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700823 if (mUpdatesStopped) {
824 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
825 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700826 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
827 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
828 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700829 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700830 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700831 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700832 pw.println(" status: " + mBatteryProps.batteryStatus);
833 pw.println(" health: " + mBatteryProps.batteryHealth);
834 pw.println(" present: " + mBatteryProps.batteryPresent);
835 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700836 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700837 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700838 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
839 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700840 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700841 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700842 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700846
Netta Pe2a3cd82017-01-26 18:03:51 -0800847 private void dumpProto(FileDescriptor fd) {
848 final ProtoOutputStream proto = new ProtoOutputStream(fd);
849
850 synchronized (mLock) {
851 proto.write(BatteryServiceDumpProto.ARE_UPDATES_STOPPED, mUpdatesStopped);
852 int batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_NONE;
853 if (mBatteryProps.chargerAcOnline) {
854 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_AC;
855 } else if (mBatteryProps.chargerUsbOnline) {
856 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_USB;
857 } else if (mBatteryProps.chargerWirelessOnline) {
858 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_WIRELESS;
859 }
860 proto.write(BatteryServiceDumpProto.PLUGGED, batteryPluggedValue);
861 proto.write(BatteryServiceDumpProto.MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
862 proto.write(BatteryServiceDumpProto.MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
863 proto.write(BatteryServiceDumpProto.CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
864 proto.write(BatteryServiceDumpProto.STATUS, mBatteryProps.batteryStatus);
865 proto.write(BatteryServiceDumpProto.HEALTH, mBatteryProps.batteryHealth);
866 proto.write(BatteryServiceDumpProto.IS_PRESENT, mBatteryProps.batteryPresent);
867 proto.write(BatteryServiceDumpProto.LEVEL, mBatteryProps.batteryLevel);
868 proto.write(BatteryServiceDumpProto.SCALE, BATTERY_SCALE);
869 proto.write(BatteryServiceDumpProto.VOLTAGE, mBatteryProps.batteryVoltage);
870 proto.write(BatteryServiceDumpProto.TEMPERATURE, mBatteryProps.batteryTemperature);
871 proto.write(BatteryServiceDumpProto.TECHNOLOGY, mBatteryProps.batteryTechnology);
872 }
873 proto.flush();
874 }
875
Jeff Browna4d82042012-10-02 19:11:19 -0700876 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800877 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700878
Jeff Browna4d82042012-10-02 19:11:19 -0700879 private final int mBatteryLowARGB;
880 private final int mBatteryMediumARGB;
881 private final int mBatteryFullARGB;
882 private final int mBatteryLedOn;
883 private final int mBatteryLedOff;
884
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800885 public Led(Context context, LightsManager lights) {
886 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700887
Jeff Browna4d82042012-10-02 19:11:19 -0700888 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700889 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700890 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700891 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700892 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700893 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700894 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700895 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700896 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700897 com.android.internal.R.integer.config_notificationsBatteryLedOff);
898 }
899
900 /**
901 * Synchronize on BatteryService.
902 */
Jeff Browna4d82042012-10-02 19:11:19 -0700903 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700904 final int level = mBatteryProps.batteryLevel;
905 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700906 if (level < mLowBatteryWarningLevel) {
907 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
908 // Solid red when battery is charging
909 mBatteryLight.setColor(mBatteryLowARGB);
910 } else {
911 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800912 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700913 mBatteryLedOn, mBatteryLedOff);
914 }
915 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
916 || status == BatteryManager.BATTERY_STATUS_FULL) {
917 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
918 // Solid green when full or charging and nearly full
919 mBatteryLight.setColor(mBatteryFullARGB);
920 } else {
921 // Solid orange when charging and halfway full
922 mBatteryLight.setColor(mBatteryMediumARGB);
923 }
924 } else {
925 // No lights if not charging and not low
926 mBatteryLight.turnOff();
927 }
928 }
929 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700930
931 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700932 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800933 final long identity = Binder.clearCallingIdentity();
934 try {
935 BatteryService.this.update(props);
936 } finally {
937 Binder.restoreCallingIdentity(identity);
938 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700939 }
940 }
Jeff Brown21392762014-06-13 19:00:36 -0700941
942 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700943 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Brown21392762014-06-13 19:00:36 -0700944 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
945 != PackageManager.PERMISSION_GRANTED) {
946
947 pw.println("Permission Denial: can't dump Battery service from from pid="
948 + Binder.getCallingPid()
949 + ", uid=" + Binder.getCallingUid());
950 return;
951 }
952
Netta Pe2a3cd82017-01-26 18:03:51 -0800953 if (args.length > 0 && "--proto".equals(args[0])) {
954 dumpProto(fd);
955 } else {
956 dumpInternal(fd, pw, args);
957 }
Dianne Hackborn2e441072015-10-28 18:00:57 -0700958 }
959
960 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700961 FileDescriptor err, String[] args, ShellCallback callback,
962 ResultReceiver resultReceiver) {
963 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700964 }
965 }
966
967 private final class LocalService extends BatteryManagerInternal {
968 @Override
969 public boolean isPowered(int plugTypeSet) {
970 synchronized (mLock) {
971 return isPoweredLocked(plugTypeSet);
972 }
973 }
974
975 @Override
976 public int getPlugType() {
977 synchronized (mLock) {
978 return mPlugType;
979 }
980 }
981
982 @Override
983 public int getBatteryLevel() {
984 synchronized (mLock) {
985 return mBatteryProps.batteryLevel;
986 }
987 }
988
989 @Override
990 public boolean getBatteryLevelLow() {
991 synchronized (mLock) {
992 return mBatteryLevelLow;
993 }
994 }
995
996 @Override
997 public int getInvalidCharger() {
998 synchronized (mLock) {
999 return mInvalidCharger;
1000 }
1001 }
1002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003}