blob: d2cfb6d51002548cdb8a4209a5b18d82825377fd [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
Dianne Hackborn14272302014-06-10 23:13:02 -070019import android.database.ContentObserver;
Dianne Hackborn8c841092013-06-24 13:46:13 -070020import android.os.BatteryStats;
Jeff Brown21392762014-06-13 19:00:36 -070021
Dianne Hackborn2e441072015-10-28 18:00:57 -070022import android.os.ResultReceiver;
Dianne Hackborn354736e2016-08-22 17:00:05 -070023import android.os.ShellCallback;
Dianne Hackborn2e441072015-10-28 18:00:57 -070024import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.app.IBatteryStats;
26import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080027import com.android.server.lights.Light;
28import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30import android.app.ActivityManagerNative;
31import android.content.ContentResolver;
32import android.content.Context;
33import android.content.Intent;
34import android.content.pm.PackageManager;
35import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070036import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070037import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070039import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070040import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070041import android.os.IBatteryPropertiesListener;
42import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080044import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.SystemClock;
48import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070049import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.provider.Settings;
51import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080052import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54import java.io.File;
55import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.FileOutputStream;
57import java.io.IOException;
58import java.io.PrintWriter;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61/**
62 * <p>BatteryService monitors the charging status, and charge level of the device
63 * battery. When these values change this service broadcasts the new values
64 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
65 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
66 * BATTERY_CHANGED} action.</p>
67 * <p>The new values are stored in the Intent data and can be retrieved by
68 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
69 * following keys:</p>
70 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
71 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
72 * <p>&quot;status&quot; - String, the current charging status.<br />
73 * <p>&quot;health&quot; - String, the current battery health.<br />
74 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
75 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
76 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
77 * into an AC power adapter; 2 if plugged in via USB.</p>
78 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
79 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
80 * a degree Centigrade</p>
81 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070082 *
83 * <p>
84 * The battery service may be called by the power manager while holding its locks so
85 * we take care to post all outcalls into the activity manager to a handler.
86 *
87 * FIXME: Ideally the power manager would perform all of its calls into the battery
88 * service asynchronously itself.
89 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
Jeff Brown21392762014-06-13 19:00:36 -070091public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080093
Jeff Browna4d82042012-10-02 19:11:19 -070094 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080095
Jeff Browna4d82042012-10-02 19:11:19 -070096 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
98 // Used locally for determining when to make a last ditch effort to log
99 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700100 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700102 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private static final String DUMPSYS_DATA_PATH = "/data/system/";
105
106 // This should probably be exposed in the API, though it's not critical
107 private static final int BATTERY_PLUGGED_NONE = 0;
108
109 private final Context mContext;
110 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700111 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700112 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
Jeff Browna4d82042012-10-02 19:11:19 -0700114 private final Object mLock = new Object();
115
Todd Poynor26faecc2013-05-22 18:54:48 -0700116 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800117 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private int mLastBatteryStatus;
120 private int mLastBatteryHealth;
121 private boolean mLastBatteryPresent;
122 private int mLastBatteryLevel;
123 private int mLastBatteryVoltage;
124 private int mLastBatteryTemperature;
125 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700126 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700127 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700128 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700129
130 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700131 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400132
133 private int mLowBatteryWarningLevel;
134 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700135 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private int mPlugType;
138 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700140 private boolean mBatteryLevelLow;
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 private long mDischargeStartTime;
143 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700145 private boolean mUpdatesStopped;
146
Joe Onoratode1b3592010-10-25 20:36:47 -0700147 private Led mLed;
148
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700149 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
Jeff Brown21392762014-06-13 19:00:36 -0700151 public BatteryService(Context context) {
152 super(context);
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700155 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700156 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 mBatteryStats = BatteryStatsService.getService();
158
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700159 mCriticalBatteryLevel = mContext.getResources().getInteger(
160 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400161 mLowBatteryWarningLevel = mContext.getResources().getInteger(
162 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700163 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
164 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700165 mShutdownBatteryTemperature = mContext.getResources().getInteger(
166 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400167
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400168 // watch for invalid charger messages if the invalid_charger switch exists
169 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700170 UEventObserver invalidChargerObserver = new UEventObserver() {
171 @Override
172 public void onUEvent(UEvent event) {
173 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
174 synchronized (mLock) {
175 if (mInvalidCharger != invalidCharger) {
176 mInvalidCharger = invalidCharger;
177 }
178 }
179 }
180 };
181 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700182 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400183 }
Jeff Brown21392762014-06-13 19:00:36 -0700184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Jeff Brown21392762014-06-13 19:00:36 -0700186 @Override
187 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700188 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800189 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
190 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700191 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800192 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700193 } catch (RemoteException e) {
194 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700195 }
Jeff Brown21392762014-06-13 19:00:36 -0700196
Dianne Hackborn2e441072015-10-28 18:00:57 -0700197 mBinderService = new BinderService();
198 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700199 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
201
Jeff Brown21392762014-06-13 19:00:36 -0700202 @Override
203 public void onBootPhase(int phase) {
204 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
205 // check our power situation now that it is safe to display the shutdown dialog.
206 synchronized (mLock) {
207 ContentObserver obs = new ContentObserver(mHandler) {
208 @Override
209 public void onChange(boolean selfChange) {
210 synchronized (mLock) {
211 updateBatteryWarningLevelLocked();
212 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700213 }
Jeff Brown21392762014-06-13 19:00:36 -0700214 };
215 final ContentResolver resolver = mContext.getContentResolver();
216 resolver.registerContentObserver(Settings.Global.getUriFor(
217 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
218 false, obs, UserHandle.USER_ALL);
219 updateBatteryWarningLevelLocked();
220 }
Jeff Browna4d82042012-10-02 19:11:19 -0700221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
223
Jeff Brown21392762014-06-13 19:00:36 -0700224 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700225 final ContentResolver resolver = mContext.getContentResolver();
226 int defWarnLevel = mContext.getResources().getInteger(
227 com.android.internal.R.integer.config_lowBatteryWarningLevel);
228 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
229 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
230 if (mLowBatteryWarningLevel == 0) {
231 mLowBatteryWarningLevel = defWarnLevel;
232 }
233 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
234 mLowBatteryWarningLevel = mCriticalBatteryLevel;
235 }
236 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
237 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
238 processValuesLocked(true);
239 }
240
Jeff Browna4d82042012-10-02 19:11:19 -0700241 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 // assume we are powered if battery state is unknown so
243 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700244 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 return true;
246 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700247 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700248 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700250 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700251 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700253 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700254 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
Jeff Browna4d82042012-10-02 19:11:19 -0700256 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258
Jeff Brown21392762014-06-13 19:00:36 -0700259 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700260 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
261 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
262
263 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
264 * - is just un-plugged (previously was plugged) and battery level is
265 * less than or equal to WARNING, or
266 * - is not plugged and battery level falls to WARNING boundary
267 * (becomes <= mLowBatteryWarningLevel).
268 */
269 return !plugged
270 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
271 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
272 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
273 }
274
Jeff Browna4d82042012-10-02 19:11:19 -0700275 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400276 // shut down gracefully if our battery is critically low and we are not powered.
277 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700278 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700279 mHandler.post(new Runnable() {
280 @Override
281 public void run() {
282 if (ActivityManagerNative.isSystemReady()) {
283 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
284 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
285 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
286 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
287 }
288 }
289 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400290 }
291 }
292
Jeff Browna4d82042012-10-02 19:11:19 -0700293 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700294 // shut down gracefully if temperature is too high (> 68.0C by default)
295 // wait until the system has booted before attempting to display the
296 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700297 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700298 mHandler.post(new Runnable() {
299 @Override
300 public void run() {
301 if (ActivityManagerNative.isSystemReady()) {
302 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
303 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
304 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
305 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
306 }
307 }
308 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700309 }
310 }
311
Todd Poynor26faecc2013-05-22 18:54:48 -0700312 private void update(BatteryProperties props) {
313 synchronized (mLock) {
314 if (!mUpdatesStopped) {
315 mBatteryProps = props;
316 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700317 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800318 } else {
319 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700320 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700321 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323
Dianne Hackborn14272302014-06-10 23:13:02 -0700324 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700325 boolean logOutlier = false;
326 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700327
Todd Poynor26faecc2013-05-22 18:54:48 -0700328 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
329 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700331 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700333 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700334 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 } else {
336 mPlugType = BATTERY_PLUGGED_NONE;
337 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700338
Jeff Browna4d82042012-10-02 19:11:19 -0700339 if (DEBUG) {
340 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700341 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
342 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
343 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700344 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700345 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700346 + ", chargeCounter" + mBatteryProps.batteryChargeCounter
Todd Poynor26faecc2013-05-22 18:54:48 -0700347 + ", batteryStatus=" + mBatteryProps.batteryStatus
348 + ", batteryHealth=" + mBatteryProps.batteryHealth
349 + ", batteryPresent=" + mBatteryProps.batteryPresent
350 + ", batteryLevel=" + mBatteryProps.batteryLevel
351 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
352 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
353 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700354 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
355 + ", mPlugType=" + mPlugType);
356 }
357
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700358 // Let the battery stats keep track of the current level.
359 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700360 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
361 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
Adam Lesinski926969b2016-04-28 17:31:12 -0700362 mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700363 } catch (RemoteException e) {
364 // Should never happen.
365 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700366
Jeff Browna4d82042012-10-02 19:11:19 -0700367 shutdownIfNoPowerLocked();
368 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700369
Dianne Hackborn14272302014-06-10 23:13:02 -0700370 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700371 mBatteryProps.batteryHealth != mLastBatteryHealth ||
372 mBatteryProps.batteryPresent != mLastBatteryPresent ||
373 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700375 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
376 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700377 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700378 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700379 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700380 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 if (mPlugType != mLastPlugType) {
383 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
384 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 // There's no value in this data unless we've discharged at least once and the
387 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700388 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700389 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
390 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700392 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
397 // charging -> discharging or we just powered up
398 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700399 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
401 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700402 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
403 mBatteryProps.batteryHealth != mLastBatteryHealth ||
404 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700407 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
408 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700410 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700411 // Don't do this just from voltage or temperature changes, that is
412 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700414 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
417 mPlugType == BATTERY_PLUGGED_NONE) {
418 // We want to make sure we log discharge cycle outliers
419 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700420 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
421 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
Dianne Hackborn14272302014-06-10 23:13:02 -0700424 if (!mBatteryLevelLow) {
425 // Should we now switch in to low battery mode?
426 if (mPlugType == BATTERY_PLUGGED_NONE
427 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
428 mBatteryLevelLow = true;
429 }
430 } else {
431 // Should we now switch out of low battery mode?
432 if (mPlugType != BATTERY_PLUGGED_NONE) {
433 mBatteryLevelLow = false;
434 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
435 mBatteryLevelLow = false;
436 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
437 // If being forced, the previous state doesn't matter, we will just
438 // absolutely check to see if we are now above the warning level.
439 mBatteryLevelLow = false;
440 }
441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Jeff Browna4d82042012-10-02 19:11:19 -0700443 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
Christopher Tate06ba5542009-04-09 16:03:56 -0700445 // Separate broadcast is sent for power connected / not connected
446 // since the standard intent will not wake any applications and some
447 // applications may want to have smart behavior based on this.
448 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700449 mHandler.post(new Runnable() {
450 @Override
451 public void run() {
452 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
453 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
454 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
455 }
456 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700457 }
458 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700459 mHandler.post(new Runnable() {
460 @Override
461 public void run() {
462 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
463 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
464 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
465 }
466 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700467 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200468
Dianne Hackborn14272302014-06-10 23:13:02 -0700469 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700470 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700471 mHandler.post(new Runnable() {
472 @Override
473 public void run() {
474 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
475 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
476 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
477 }
478 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400479 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700480 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700481 mHandler.post(new Runnable() {
482 @Override
483 public void run() {
484 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
485 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
486 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
487 }
488 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800490
Joe Onoratode1b3592010-10-25 20:36:47 -0700491 // Update the battery LED
492 mLed.updateLightsLocked();
493
The Android Open Source Project10592532009-03-18 17:39:46 -0700494 // This needs to be done after sendIntent() so that we get the lastest battery stats.
495 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700496 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
Todd Poynor26faecc2013-05-22 18:54:48 -0700499 mLastBatteryStatus = mBatteryProps.batteryStatus;
500 mLastBatteryHealth = mBatteryProps.batteryHealth;
501 mLastBatteryPresent = mBatteryProps.batteryPresent;
502 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700503 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700504 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
505 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700506 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700507 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700508 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700509 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400510 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
513
Jeff Browna4d82042012-10-02 19:11:19 -0700514 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700516 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800517 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
518 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800519
Todd Poynor26faecc2013-05-22 18:54:48 -0700520 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
Todd Poynor26faecc2013-05-22 18:54:48 -0700522 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
523 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
524 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
525 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700526 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
527 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
528 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700529 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
530 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
531 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400532 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700533 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700534 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700535 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700536 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700537 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
538 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700539 ", health:" + mBatteryProps.batteryHealth +
540 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700541 ", voltage: " + mBatteryProps.batteryVoltage +
542 ", temperature: " + mBatteryProps.batteryTemperature +
543 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700544 ", AC powered:" + mBatteryProps.chargerAcOnline +
545 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700546 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700547 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700548 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700549 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
550 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552
Jeff Brown605ea692012-10-05 16:33:10 -0700553 mHandler.post(new Runnable() {
554 @Override
555 public void run() {
556 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
557 }
558 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560
Jeff Browna4d82042012-10-02 19:11:19 -0700561 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700562 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800563 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564
Dan Egnor18e93962010-02-10 19:27:58 -0800565 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
566 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
567
568 File dumpFile = null;
569 FileOutputStream dumpStream = null;
570 try {
571 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700572 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800573 dumpStream = new FileOutputStream(dumpFile);
574 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700575 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800576
577 // add dump file to drop box
578 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
579 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800580 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800581 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800582 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800583 } finally {
584 // make sure we clean up
585 if (dumpStream != null) {
586 try {
587 dumpStream.close();
588 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800589 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
Dan Egnor18e93962010-02-10 19:27:58 -0800591 }
592 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800593 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800594 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596 }
597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800598
Jeff Browna4d82042012-10-02 19:11:19 -0700599 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700601 String dischargeThresholdString = Settings.Global.getString(cr,
602 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
603 String durationThresholdString = Settings.Global.getString(cr,
604 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if (dischargeThresholdString != null && durationThresholdString != null) {
607 try {
608 long durationThreshold = Long.parseLong(durationThresholdString);
609 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800610 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700611 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700613 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
Jeff Browna4d82042012-10-02 19:11:19 -0700615 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700617 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700618 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624 }
625
Jeff Browna4d82042012-10-02 19:11:19 -0700626 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700627 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700629 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700631 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
632 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700633 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700634 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800635 return com.android.internal.R.drawable.stat_sys_battery_charge;
636 } else {
637 return com.android.internal.R.drawable.stat_sys_battery;
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 } else {
640 return com.android.internal.R.drawable.stat_sys_battery_unknown;
641 }
642 }
643
Dianne Hackborn2e441072015-10-28 18:00:57 -0700644 class Shell extends ShellCommand {
645 @Override
646 public int onCommand(String cmd) {
647 return onShellCommand(this, cmd);
648 }
649
650 @Override
651 public void onHelp() {
652 PrintWriter pw = getOutPrintWriter();
653 dumpHelp(pw);
654 }
655 }
656
657 static void dumpHelp(PrintWriter pw) {
658 pw.println("Battery service (battery) commands:");
659 pw.println(" help");
660 pw.println(" Print this help text.");
661 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
662 pw.println(" Force a battery property value, freezing battery state.");
663 pw.println(" unplug");
664 pw.println(" Force battery unplugged, freezing battery state.");
665 pw.println(" reset");
666 pw.println(" Unfreeze battery state, returning to current hardware values.");
667 }
668
669 int onShellCommand(Shell shell, String cmd) {
670 if (cmd == null) {
671 return shell.handleDefaultCommands(cmd);
672 }
673 PrintWriter pw = shell.getOutPrintWriter();
674 switch (cmd) {
675 case "unplug": {
676 getContext().enforceCallingOrSelfPermission(
677 android.Manifest.permission.DEVICE_POWER, null);
678 if (!mUpdatesStopped) {
679 mLastBatteryProps.set(mBatteryProps);
680 }
681 mBatteryProps.chargerAcOnline = false;
682 mBatteryProps.chargerUsbOnline = false;
683 mBatteryProps.chargerWirelessOnline = false;
684 long ident = Binder.clearCallingIdentity();
685 try {
686 mUpdatesStopped = true;
687 processValuesLocked(false);
688 } finally {
689 Binder.restoreCallingIdentity(ident);
690 }
691 } break;
692 case "set": {
693 getContext().enforceCallingOrSelfPermission(
694 android.Manifest.permission.DEVICE_POWER, null);
695 final String key = shell.getNextArg();
696 if (key == null) {
697 pw.println("No property specified");
698 return -1;
699
700 }
701 final String value = shell.getNextArg();
702 if (value == null) {
703 pw.println("No value specified");
704 return -1;
705
706 }
707 try {
708 if (!mUpdatesStopped) {
709 mLastBatteryProps.set(mBatteryProps);
710 }
711 boolean update = true;
712 switch (key) {
713 case "ac":
714 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
715 break;
716 case "usb":
717 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
718 break;
719 case "wireless":
720 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
721 break;
722 case "status":
723 mBatteryProps.batteryStatus = Integer.parseInt(value);
724 break;
725 case "level":
726 mBatteryProps.batteryLevel = Integer.parseInt(value);
727 break;
728 case "invalid":
729 mInvalidCharger = Integer.parseInt(value);
730 break;
731 default:
732 pw.println("Unknown set option: " + key);
733 update = false;
734 break;
735 }
736 if (update) {
737 long ident = Binder.clearCallingIdentity();
738 try {
739 mUpdatesStopped = true;
740 processValuesLocked(false);
741 } finally {
742 Binder.restoreCallingIdentity(ident);
743 }
744 }
745 } catch (NumberFormatException ex) {
746 pw.println("Bad value: " + value);
747 return -1;
748 }
749 } break;
750 case "reset": {
751 getContext().enforceCallingOrSelfPermission(
752 android.Manifest.permission.DEVICE_POWER, null);
753 long ident = Binder.clearCallingIdentity();
754 try {
755 if (mUpdatesStopped) {
756 mUpdatesStopped = false;
757 mBatteryProps.set(mLastBatteryProps);
758 processValuesLocked(false);
759 }
760 } finally {
761 Binder.restoreCallingIdentity(ident);
762 }
763 } break;
764 default:
765 return shell.handleDefaultCommands(cmd);
766 }
767 return 0;
768 }
769
770 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700771 synchronized (mLock) {
772 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700773 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700774 if (mUpdatesStopped) {
775 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
776 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700777 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
778 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
779 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700780 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700781 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700782 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700783 pw.println(" status: " + mBatteryProps.batteryStatus);
784 pw.println(" health: " + mBatteryProps.batteryHealth);
785 pw.println(" present: " + mBatteryProps.batteryPresent);
786 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700787 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700788 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700789 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
790 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700791 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700792 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700793 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700797
Jeff Browna4d82042012-10-02 19:11:19 -0700798 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800799 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700800
Jeff Browna4d82042012-10-02 19:11:19 -0700801 private final int mBatteryLowARGB;
802 private final int mBatteryMediumARGB;
803 private final int mBatteryFullARGB;
804 private final int mBatteryLedOn;
805 private final int mBatteryLedOff;
806
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800807 public Led(Context context, LightsManager lights) {
808 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700809
Jeff Browna4d82042012-10-02 19:11:19 -0700810 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700811 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700812 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700813 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700814 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700815 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700816 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700817 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700818 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700819 com.android.internal.R.integer.config_notificationsBatteryLedOff);
820 }
821
822 /**
823 * Synchronize on BatteryService.
824 */
Jeff Browna4d82042012-10-02 19:11:19 -0700825 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700826 final int level = mBatteryProps.batteryLevel;
827 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700828 if (level < mLowBatteryWarningLevel) {
829 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
830 // Solid red when battery is charging
831 mBatteryLight.setColor(mBatteryLowARGB);
832 } else {
833 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800834 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700835 mBatteryLedOn, mBatteryLedOff);
836 }
837 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
838 || status == BatteryManager.BATTERY_STATUS_FULL) {
839 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
840 // Solid green when full or charging and nearly full
841 mBatteryLight.setColor(mBatteryFullARGB);
842 } else {
843 // Solid orange when charging and halfway full
844 mBatteryLight.setColor(mBatteryMediumARGB);
845 }
846 } else {
847 // No lights if not charging and not low
848 mBatteryLight.turnOff();
849 }
850 }
851 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700852
853 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700854 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800855 final long identity = Binder.clearCallingIdentity();
856 try {
857 BatteryService.this.update(props);
858 } finally {
859 Binder.restoreCallingIdentity(identity);
860 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700861 }
862 }
Jeff Brown21392762014-06-13 19:00:36 -0700863
864 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700865 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Brown21392762014-06-13 19:00:36 -0700866 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
867 != PackageManager.PERMISSION_GRANTED) {
868
869 pw.println("Permission Denial: can't dump Battery service from from pid="
870 + Binder.getCallingPid()
871 + ", uid=" + Binder.getCallingUid());
872 return;
873 }
874
Dianne Hackborn2e441072015-10-28 18:00:57 -0700875 dumpInternal(fd, pw, args);
876 }
877
878 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700879 FileDescriptor err, String[] args, ShellCallback callback,
880 ResultReceiver resultReceiver) {
881 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700882 }
883 }
884
885 private final class LocalService extends BatteryManagerInternal {
886 @Override
887 public boolean isPowered(int plugTypeSet) {
888 synchronized (mLock) {
889 return isPoweredLocked(plugTypeSet);
890 }
891 }
892
893 @Override
894 public int getPlugType() {
895 synchronized (mLock) {
896 return mPlugType;
897 }
898 }
899
900 @Override
901 public int getBatteryLevel() {
902 synchronized (mLock) {
903 return mBatteryProps.batteryLevel;
904 }
905 }
906
907 @Override
908 public boolean getBatteryLevelLow() {
909 synchronized (mLock) {
910 return mBatteryLevelLow;
911 }
912 }
913
914 @Override
915 public int getInvalidCharger() {
916 synchronized (mLock) {
917 return mInvalidCharger;
918 }
919 }
920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921}