blob: 573ad631891e988c6d92db911f16a999e5a4bc85 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070019import android.app.ActivityManagerInternal;
Dianne Hackborn14272302014-06-10 23:13:02 -070020import android.database.ContentObserver;
Dianne Hackborn8c841092013-06-24 13:46:13 -070021import android.os.BatteryStats;
Jeff Brown21392762014-06-13 19:00:36 -070022
Dianne Hackborn2e441072015-10-28 18:00:57 -070023import android.os.ResultReceiver;
Dianne Hackborn354736e2016-08-22 17:00:05 -070024import android.os.ShellCallback;
Dianne Hackborn2e441072015-10-28 18:00:57 -070025import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.app.IBatteryStats;
27import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080028import com.android.server.lights.Light;
29import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Sudheer Shankadc589ac2016-11-10 15:30:17 -080031import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ContentResolver;
33import android.content.Context;
34import android.content.Intent;
35import android.content.pm.PackageManager;
36import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070037import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070038import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070040import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070041import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070042import android.os.IBatteryPropertiesListener;
43import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080045import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemClock;
49import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070050import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.provider.Settings;
52import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080053import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55import java.io.File;
56import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.io.FileOutputStream;
58import java.io.IOException;
59import java.io.PrintWriter;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62/**
63 * <p>BatteryService monitors the charging status, and charge level of the device
64 * battery. When these values change this service broadcasts the new values
65 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
66 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
67 * BATTERY_CHANGED} action.</p>
68 * <p>The new values are stored in the Intent data and can be retrieved by
69 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
70 * following keys:</p>
71 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
72 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
73 * <p>&quot;status&quot; - String, the current charging status.<br />
74 * <p>&quot;health&quot; - String, the current battery health.<br />
75 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
76 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
77 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
78 * into an AC power adapter; 2 if plugged in via USB.</p>
79 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
80 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
81 * a degree Centigrade</p>
82 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070083 *
84 * <p>
85 * The battery service may be called by the power manager while holding its locks so
86 * we take care to post all outcalls into the activity manager to a handler.
87 *
88 * FIXME: Ideally the power manager would perform all of its calls into the battery
89 * service asynchronously itself.
90 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 */
Jeff Brown21392762014-06-13 19:00:36 -070092public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080094
Jeff Browna4d82042012-10-02 19:11:19 -070095 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080096
Jeff Browna4d82042012-10-02 19:11:19 -070097 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 // Used locally for determining when to make a last ditch effort to log
100 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700101 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700103 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private static final String DUMPSYS_DATA_PATH = "/data/system/";
106
107 // This should probably be exposed in the API, though it's not critical
108 private static final int BATTERY_PLUGGED_NONE = 0;
109
110 private final Context mContext;
111 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700112 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700113 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800114
Jeff Browna4d82042012-10-02 19:11:19 -0700115 private final Object mLock = new Object();
116
Todd Poynor26faecc2013-05-22 18:54:48 -0700117 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800118 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 private int mLastBatteryStatus;
121 private int mLastBatteryHealth;
122 private boolean mLastBatteryPresent;
123 private int mLastBatteryLevel;
124 private int mLastBatteryVoltage;
125 private int mLastBatteryTemperature;
126 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700127 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700128 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700129 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700130
131 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700132 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400133
134 private int mLowBatteryWarningLevel;
135 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700136 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 private int mPlugType;
139 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700141 private boolean mBatteryLevelLow;
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private long mDischargeStartTime;
144 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700146 private boolean mUpdatesStopped;
147
Joe Onoratode1b3592010-10-25 20:36:47 -0700148 private Led mLed;
149
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700150 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700152 private ActivityManagerInternal mActivityManagerInternal;
153
Jeff Brown21392762014-06-13 19:00:36 -0700154 public BatteryService(Context context) {
155 super(context);
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700158 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700159 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 mBatteryStats = BatteryStatsService.getService();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700161 mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700163 mCriticalBatteryLevel = mContext.getResources().getInteger(
164 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400165 mLowBatteryWarningLevel = mContext.getResources().getInteger(
166 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700167 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
168 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700169 mShutdownBatteryTemperature = mContext.getResources().getInteger(
170 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400171
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400172 // watch for invalid charger messages if the invalid_charger switch exists
173 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700174 UEventObserver invalidChargerObserver = new UEventObserver() {
175 @Override
176 public void onUEvent(UEvent event) {
177 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
178 synchronized (mLock) {
179 if (mInvalidCharger != invalidCharger) {
180 mInvalidCharger = invalidCharger;
181 }
182 }
183 }
184 };
185 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700186 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400187 }
Jeff Brown21392762014-06-13 19:00:36 -0700188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Jeff Brown21392762014-06-13 19:00:36 -0700190 @Override
191 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700192 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800193 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
194 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700195 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800196 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700197 } catch (RemoteException e) {
198 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700199 }
Jeff Brown21392762014-06-13 19:00:36 -0700200
Dianne Hackborn2e441072015-10-28 18:00:57 -0700201 mBinderService = new BinderService();
202 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700203 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205
Jeff Brown21392762014-06-13 19:00:36 -0700206 @Override
207 public void onBootPhase(int phase) {
208 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
209 // check our power situation now that it is safe to display the shutdown dialog.
210 synchronized (mLock) {
211 ContentObserver obs = new ContentObserver(mHandler) {
212 @Override
213 public void onChange(boolean selfChange) {
214 synchronized (mLock) {
215 updateBatteryWarningLevelLocked();
216 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700217 }
Jeff Brown21392762014-06-13 19:00:36 -0700218 };
219 final ContentResolver resolver = mContext.getContentResolver();
220 resolver.registerContentObserver(Settings.Global.getUriFor(
221 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
222 false, obs, UserHandle.USER_ALL);
223 updateBatteryWarningLevelLocked();
224 }
Jeff Browna4d82042012-10-02 19:11:19 -0700225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 }
227
Jeff Brown21392762014-06-13 19:00:36 -0700228 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700229 final ContentResolver resolver = mContext.getContentResolver();
230 int defWarnLevel = mContext.getResources().getInteger(
231 com.android.internal.R.integer.config_lowBatteryWarningLevel);
232 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
233 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
234 if (mLowBatteryWarningLevel == 0) {
235 mLowBatteryWarningLevel = defWarnLevel;
236 }
237 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
238 mLowBatteryWarningLevel = mCriticalBatteryLevel;
239 }
240 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
241 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
242 processValuesLocked(true);
243 }
244
Jeff Browna4d82042012-10-02 19:11:19 -0700245 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 // assume we are powered if battery state is unknown so
247 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700248 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 return true;
250 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700251 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700252 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700254 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700255 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700257 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700258 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 }
Jeff Browna4d82042012-10-02 19:11:19 -0700260 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262
Jeff Brown21392762014-06-13 19:00:36 -0700263 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700264 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
265 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
266
267 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
268 * - is just un-plugged (previously was plugged) and battery level is
269 * less than or equal to WARNING, or
270 * - is not plugged and battery level falls to WARNING boundary
271 * (becomes <= mLowBatteryWarningLevel).
272 */
273 return !plugged
274 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
275 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
276 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
277 }
278
Jeff Browna4d82042012-10-02 19:11:19 -0700279 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400280 // shut down gracefully if our battery is critically low and we are not powered.
281 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700282 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700283 mHandler.post(new Runnable() {
284 @Override
285 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700286 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700287 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
288 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
289 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
290 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
291 }
292 }
293 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400294 }
295 }
296
Jeff Browna4d82042012-10-02 19:11:19 -0700297 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700298 // shut down gracefully if temperature is too high (> 68.0C by default)
299 // wait until the system has booted before attempting to display the
300 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700301 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700302 mHandler.post(new Runnable() {
303 @Override
304 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700305 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700306 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
307 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
308 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
309 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
310 }
311 }
312 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700313 }
314 }
315
Todd Poynor26faecc2013-05-22 18:54:48 -0700316 private void update(BatteryProperties props) {
317 synchronized (mLock) {
318 if (!mUpdatesStopped) {
319 mBatteryProps = props;
320 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700321 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800322 } else {
323 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700324 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700325 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
Dianne Hackborn14272302014-06-10 23:13:02 -0700328 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700329 boolean logOutlier = false;
330 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700331
Todd Poynor26faecc2013-05-22 18:54:48 -0700332 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
333 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700335 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700337 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700338 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 } else {
340 mPlugType = BATTERY_PLUGGED_NONE;
341 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700342
Jeff Browna4d82042012-10-02 19:11:19 -0700343 if (DEBUG) {
344 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700345 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
346 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
347 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700348 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700349 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700350 + ", chargeCounter" + mBatteryProps.batteryChargeCounter
Todd Poynor26faecc2013-05-22 18:54:48 -0700351 + ", batteryStatus=" + mBatteryProps.batteryStatus
352 + ", batteryHealth=" + mBatteryProps.batteryHealth
353 + ", batteryPresent=" + mBatteryProps.batteryPresent
354 + ", batteryLevel=" + mBatteryProps.batteryLevel
355 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
356 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
357 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700358 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
359 + ", mPlugType=" + mPlugType);
360 }
361
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700362 // Let the battery stats keep track of the current level.
363 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700364 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
365 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
Adam Lesinski926969b2016-04-28 17:31:12 -0700366 mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700367 } catch (RemoteException e) {
368 // Should never happen.
369 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700370
Jeff Browna4d82042012-10-02 19:11:19 -0700371 shutdownIfNoPowerLocked();
372 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700373
Dianne Hackborn14272302014-06-10 23:13:02 -0700374 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700375 mBatteryProps.batteryHealth != mLastBatteryHealth ||
376 mBatteryProps.batteryPresent != mLastBatteryPresent ||
377 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700379 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
380 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700381 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700382 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700383 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700384 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 if (mPlugType != mLastPlugType) {
387 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
388 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 // There's no value in this data unless we've discharged at least once and the
391 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700392 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700393 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
394 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700396 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
401 // charging -> discharging or we just powered up
402 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700403 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
405 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700406 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
407 mBatteryProps.batteryHealth != mLastBatteryHealth ||
408 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700411 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
412 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700414 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700415 // Don't do this just from voltage or temperature changes, that is
416 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700418 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
421 mPlugType == BATTERY_PLUGGED_NONE) {
422 // We want to make sure we log discharge cycle outliers
423 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700424 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
425 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
Dianne Hackborn14272302014-06-10 23:13:02 -0700428 if (!mBatteryLevelLow) {
429 // Should we now switch in to low battery mode?
430 if (mPlugType == BATTERY_PLUGGED_NONE
431 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
432 mBatteryLevelLow = true;
433 }
434 } else {
435 // Should we now switch out of low battery mode?
436 if (mPlugType != BATTERY_PLUGGED_NONE) {
437 mBatteryLevelLow = false;
438 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
439 mBatteryLevelLow = false;
440 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
441 // If being forced, the previous state doesn't matter, we will just
442 // absolutely check to see if we are now above the warning level.
443 mBatteryLevelLow = false;
444 }
445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Jeff Browna4d82042012-10-02 19:11:19 -0700447 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Christopher Tate06ba5542009-04-09 16:03:56 -0700449 // Separate broadcast is sent for power connected / not connected
450 // since the standard intent will not wake any applications and some
451 // applications may want to have smart behavior based on this.
452 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700453 mHandler.post(new Runnable() {
454 @Override
455 public void run() {
456 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
457 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
458 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
459 }
460 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700461 }
462 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700463 mHandler.post(new Runnable() {
464 @Override
465 public void run() {
466 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
467 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
468 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
469 }
470 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700471 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200472
Dianne Hackborn14272302014-06-10 23:13:02 -0700473 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700474 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700475 mHandler.post(new Runnable() {
476 @Override
477 public void run() {
478 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
479 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
480 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
481 }
482 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400483 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700484 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700485 mHandler.post(new Runnable() {
486 @Override
487 public void run() {
488 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
489 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
490 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
491 }
492 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800494
Joe Onoratode1b3592010-10-25 20:36:47 -0700495 // Update the battery LED
496 mLed.updateLightsLocked();
497
The Android Open Source Project10592532009-03-18 17:39:46 -0700498 // This needs to be done after sendIntent() so that we get the lastest battery stats.
499 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700500 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
Todd Poynor26faecc2013-05-22 18:54:48 -0700503 mLastBatteryStatus = mBatteryProps.batteryStatus;
504 mLastBatteryHealth = mBatteryProps.batteryHealth;
505 mLastBatteryPresent = mBatteryProps.batteryPresent;
506 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700507 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700508 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
509 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700510 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700511 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700512 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700513 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400514 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516 }
517
Jeff Browna4d82042012-10-02 19:11:19 -0700518 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700520 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800521 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
522 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800523
Todd Poynor26faecc2013-05-22 18:54:48 -0700524 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525
Todd Poynor26faecc2013-05-22 18:54:48 -0700526 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
527 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
528 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
529 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700530 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
531 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
532 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700533 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
534 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
535 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400536 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700537 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700538 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700539 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700540 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700541 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
542 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700543 ", health:" + mBatteryProps.batteryHealth +
544 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700545 ", voltage: " + mBatteryProps.batteryVoltage +
546 ", temperature: " + mBatteryProps.batteryTemperature +
547 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700548 ", AC powered:" + mBatteryProps.chargerAcOnline +
549 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700550 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700551 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700552 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700553 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
554 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556
Jeff Brown605ea692012-10-05 16:33:10 -0700557 mHandler.post(new Runnable() {
558 @Override
559 public void run() {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800560 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jeff Brown605ea692012-10-05 16:33:10 -0700561 }
562 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564
Jeff Browna4d82042012-10-02 19:11:19 -0700565 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700566 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800567 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
Dan Egnor18e93962010-02-10 19:27:58 -0800569 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
570 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
571
572 File dumpFile = null;
573 FileOutputStream dumpStream = null;
574 try {
575 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700576 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800577 dumpStream = new FileOutputStream(dumpFile);
578 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700579 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800580
581 // add dump file to drop box
582 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
583 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800584 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800585 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800586 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800587 } finally {
588 // make sure we clean up
589 if (dumpStream != null) {
590 try {
591 dumpStream.close();
592 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800593 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Dan Egnor18e93962010-02-10 19:27:58 -0800595 }
596 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800597 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800598 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600 }
601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800602
Jeff Browna4d82042012-10-02 19:11:19 -0700603 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700605 String dischargeThresholdString = Settings.Global.getString(cr,
606 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
607 String durationThresholdString = Settings.Global.getString(cr,
608 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 if (dischargeThresholdString != null && durationThresholdString != null) {
611 try {
612 long durationThreshold = Long.parseLong(durationThresholdString);
613 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800614 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700615 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700617 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
Jeff Browna4d82042012-10-02 19:11:19 -0700619 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700621 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700622 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800624 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627 }
628 }
629
Jeff Browna4d82042012-10-02 19:11:19 -0700630 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700631 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700633 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700635 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
636 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700637 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700638 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800639 return com.android.internal.R.drawable.stat_sys_battery_charge;
640 } else {
641 return com.android.internal.R.drawable.stat_sys_battery;
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 } else {
644 return com.android.internal.R.drawable.stat_sys_battery_unknown;
645 }
646 }
647
Dianne Hackborn2e441072015-10-28 18:00:57 -0700648 class Shell extends ShellCommand {
649 @Override
650 public int onCommand(String cmd) {
651 return onShellCommand(this, cmd);
652 }
653
654 @Override
655 public void onHelp() {
656 PrintWriter pw = getOutPrintWriter();
657 dumpHelp(pw);
658 }
659 }
660
661 static void dumpHelp(PrintWriter pw) {
662 pw.println("Battery service (battery) commands:");
663 pw.println(" help");
664 pw.println(" Print this help text.");
665 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
666 pw.println(" Force a battery property value, freezing battery state.");
667 pw.println(" unplug");
668 pw.println(" Force battery unplugged, freezing battery state.");
669 pw.println(" reset");
670 pw.println(" Unfreeze battery state, returning to current hardware values.");
671 }
672
673 int onShellCommand(Shell shell, String cmd) {
674 if (cmd == null) {
675 return shell.handleDefaultCommands(cmd);
676 }
677 PrintWriter pw = shell.getOutPrintWriter();
678 switch (cmd) {
679 case "unplug": {
680 getContext().enforceCallingOrSelfPermission(
681 android.Manifest.permission.DEVICE_POWER, null);
682 if (!mUpdatesStopped) {
683 mLastBatteryProps.set(mBatteryProps);
684 }
685 mBatteryProps.chargerAcOnline = false;
686 mBatteryProps.chargerUsbOnline = false;
687 mBatteryProps.chargerWirelessOnline = false;
688 long ident = Binder.clearCallingIdentity();
689 try {
690 mUpdatesStopped = true;
691 processValuesLocked(false);
692 } finally {
693 Binder.restoreCallingIdentity(ident);
694 }
695 } break;
696 case "set": {
697 getContext().enforceCallingOrSelfPermission(
698 android.Manifest.permission.DEVICE_POWER, null);
699 final String key = shell.getNextArg();
700 if (key == null) {
701 pw.println("No property specified");
702 return -1;
703
704 }
705 final String value = shell.getNextArg();
706 if (value == null) {
707 pw.println("No value specified");
708 return -1;
709
710 }
711 try {
712 if (!mUpdatesStopped) {
713 mLastBatteryProps.set(mBatteryProps);
714 }
715 boolean update = true;
716 switch (key) {
717 case "ac":
718 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
719 break;
720 case "usb":
721 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
722 break;
723 case "wireless":
724 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
725 break;
726 case "status":
727 mBatteryProps.batteryStatus = Integer.parseInt(value);
728 break;
729 case "level":
730 mBatteryProps.batteryLevel = Integer.parseInt(value);
731 break;
732 case "invalid":
733 mInvalidCharger = Integer.parseInt(value);
734 break;
735 default:
736 pw.println("Unknown set option: " + key);
737 update = false;
738 break;
739 }
740 if (update) {
741 long ident = Binder.clearCallingIdentity();
742 try {
743 mUpdatesStopped = true;
744 processValuesLocked(false);
745 } finally {
746 Binder.restoreCallingIdentity(ident);
747 }
748 }
749 } catch (NumberFormatException ex) {
750 pw.println("Bad value: " + value);
751 return -1;
752 }
753 } break;
754 case "reset": {
755 getContext().enforceCallingOrSelfPermission(
756 android.Manifest.permission.DEVICE_POWER, null);
757 long ident = Binder.clearCallingIdentity();
758 try {
759 if (mUpdatesStopped) {
760 mUpdatesStopped = false;
761 mBatteryProps.set(mLastBatteryProps);
762 processValuesLocked(false);
763 }
764 } finally {
765 Binder.restoreCallingIdentity(ident);
766 }
767 } break;
768 default:
769 return shell.handleDefaultCommands(cmd);
770 }
771 return 0;
772 }
773
774 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700775 synchronized (mLock) {
776 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700777 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700778 if (mUpdatesStopped) {
779 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
780 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700781 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
782 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
783 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700784 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700785 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700786 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700787 pw.println(" status: " + mBatteryProps.batteryStatus);
788 pw.println(" health: " + mBatteryProps.batteryHealth);
789 pw.println(" present: " + mBatteryProps.batteryPresent);
790 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700791 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700792 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700793 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
794 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700795 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700796 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700797 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700801
Jeff Browna4d82042012-10-02 19:11:19 -0700802 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800803 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700804
Jeff Browna4d82042012-10-02 19:11:19 -0700805 private final int mBatteryLowARGB;
806 private final int mBatteryMediumARGB;
807 private final int mBatteryFullARGB;
808 private final int mBatteryLedOn;
809 private final int mBatteryLedOff;
810
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800811 public Led(Context context, LightsManager lights) {
812 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700813
Jeff Browna4d82042012-10-02 19:11:19 -0700814 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700815 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700816 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700817 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700818 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700819 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700820 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700821 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700822 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700823 com.android.internal.R.integer.config_notificationsBatteryLedOff);
824 }
825
826 /**
827 * Synchronize on BatteryService.
828 */
Jeff Browna4d82042012-10-02 19:11:19 -0700829 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700830 final int level = mBatteryProps.batteryLevel;
831 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700832 if (level < mLowBatteryWarningLevel) {
833 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
834 // Solid red when battery is charging
835 mBatteryLight.setColor(mBatteryLowARGB);
836 } else {
837 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800838 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700839 mBatteryLedOn, mBatteryLedOff);
840 }
841 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
842 || status == BatteryManager.BATTERY_STATUS_FULL) {
843 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
844 // Solid green when full or charging and nearly full
845 mBatteryLight.setColor(mBatteryFullARGB);
846 } else {
847 // Solid orange when charging and halfway full
848 mBatteryLight.setColor(mBatteryMediumARGB);
849 }
850 } else {
851 // No lights if not charging and not low
852 mBatteryLight.turnOff();
853 }
854 }
855 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700856
857 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700858 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800859 final long identity = Binder.clearCallingIdentity();
860 try {
861 BatteryService.this.update(props);
862 } finally {
863 Binder.restoreCallingIdentity(identity);
864 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700865 }
866 }
Jeff Brown21392762014-06-13 19:00:36 -0700867
868 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700869 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Brown21392762014-06-13 19:00:36 -0700870 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
871 != PackageManager.PERMISSION_GRANTED) {
872
873 pw.println("Permission Denial: can't dump Battery service from from pid="
874 + Binder.getCallingPid()
875 + ", uid=" + Binder.getCallingUid());
876 return;
877 }
878
Dianne Hackborn2e441072015-10-28 18:00:57 -0700879 dumpInternal(fd, pw, args);
880 }
881
882 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700883 FileDescriptor err, String[] args, ShellCallback callback,
884 ResultReceiver resultReceiver) {
885 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700886 }
887 }
888
889 private final class LocalService extends BatteryManagerInternal {
890 @Override
891 public boolean isPowered(int plugTypeSet) {
892 synchronized (mLock) {
893 return isPoweredLocked(plugTypeSet);
894 }
895 }
896
897 @Override
898 public int getPlugType() {
899 synchronized (mLock) {
900 return mPlugType;
901 }
902 }
903
904 @Override
905 public int getBatteryLevel() {
906 synchronized (mLock) {
907 return mBatteryProps.batteryLevel;
908 }
909 }
910
911 @Override
912 public boolean getBatteryLevelLow() {
913 synchronized (mLock) {
914 return mBatteryLevelLow;
915 }
916 }
917
918 @Override
919 public int getInvalidCharger() {
920 synchronized (mLock) {
921 return mInvalidCharger;
922 }
923 }
924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925}