blob: 87539921be78f4613e4fb84b20ff2a0619497369 [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;
23import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.app.IBatteryStats;
25import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080026import com.android.server.lights.Light;
27import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29import android.app.ActivityManagerNative;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070035import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070036import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070038import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070039import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070040import android.os.IBatteryPropertiesListener;
41import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080043import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.SystemClock;
47import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070048import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.provider.Settings;
50import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080051import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53import java.io.File;
54import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import java.io.FileOutputStream;
56import java.io.IOException;
57import java.io.PrintWriter;
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60/**
61 * <p>BatteryService monitors the charging status, and charge level of the device
62 * battery. When these values change this service broadcasts the new values
63 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
64 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
65 * BATTERY_CHANGED} action.</p>
66 * <p>The new values are stored in the Intent data and can be retrieved by
67 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
68 * following keys:</p>
69 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
70 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
71 * <p>&quot;status&quot; - String, the current charging status.<br />
72 * <p>&quot;health&quot; - String, the current battery health.<br />
73 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
74 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
75 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
76 * into an AC power adapter; 2 if plugged in via USB.</p>
77 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
78 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
79 * a degree Centigrade</p>
80 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070081 *
82 * <p>
83 * The battery service may be called by the power manager while holding its locks so
84 * we take care to post all outcalls into the activity manager to a handler.
85 *
86 * FIXME: Ideally the power manager would perform all of its calls into the battery
87 * service asynchronously itself.
88 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 */
Jeff Brown21392762014-06-13 19:00:36 -070090public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080092
Jeff Browna4d82042012-10-02 19:11:19 -070093 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080094
Jeff Browna4d82042012-10-02 19:11:19 -070095 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 // Used locally for determining when to make a last ditch effort to log
98 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070099 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700101 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private static final String DUMPSYS_DATA_PATH = "/data/system/";
104
105 // This should probably be exposed in the API, though it's not critical
106 private static final int BATTERY_PLUGGED_NONE = 0;
107
108 private final Context mContext;
109 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700110 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700111 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
Jeff Browna4d82042012-10-02 19:11:19 -0700113 private final Object mLock = new Object();
114
Todd Poynor26faecc2013-05-22 18:54:48 -0700115 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800116 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private int mLastBatteryStatus;
119 private int mLastBatteryHealth;
120 private boolean mLastBatteryPresent;
121 private int mLastBatteryLevel;
122 private int mLastBatteryVoltage;
123 private int mLastBatteryTemperature;
124 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700125 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700126 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700127 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700128
129 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700130 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400131
132 private int mLowBatteryWarningLevel;
133 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700134 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 private int mPlugType;
137 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700139 private boolean mBatteryLevelLow;
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 private long mDischargeStartTime;
142 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700144 private boolean mUpdatesStopped;
145
Joe Onoratode1b3592010-10-25 20:36:47 -0700146 private Led mLed;
147
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700148 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
Jeff Brown21392762014-06-13 19:00:36 -0700150 public BatteryService(Context context) {
151 super(context);
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700154 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700155 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 mBatteryStats = BatteryStatsService.getService();
157
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700158 mCriticalBatteryLevel = mContext.getResources().getInteger(
159 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400160 mLowBatteryWarningLevel = mContext.getResources().getInteger(
161 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700162 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
163 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700164 mShutdownBatteryTemperature = mContext.getResources().getInteger(
165 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400166
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400167 // watch for invalid charger messages if the invalid_charger switch exists
168 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700169 UEventObserver invalidChargerObserver = new UEventObserver() {
170 @Override
171 public void onUEvent(UEvent event) {
172 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
173 synchronized (mLock) {
174 if (mInvalidCharger != invalidCharger) {
175 mInvalidCharger = invalidCharger;
176 }
177 }
178 }
179 };
180 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700181 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400182 }
Jeff Brown21392762014-06-13 19:00:36 -0700183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
Jeff Brown21392762014-06-13 19:00:36 -0700185 @Override
186 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700187 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800188 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
189 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700190 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800191 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700192 } catch (RemoteException e) {
193 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700194 }
Jeff Brown21392762014-06-13 19:00:36 -0700195
Dianne Hackborn2e441072015-10-28 18:00:57 -0700196 mBinderService = new BinderService();
197 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700198 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200
Jeff Brown21392762014-06-13 19:00:36 -0700201 @Override
202 public void onBootPhase(int phase) {
203 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
204 // check our power situation now that it is safe to display the shutdown dialog.
205 synchronized (mLock) {
206 ContentObserver obs = new ContentObserver(mHandler) {
207 @Override
208 public void onChange(boolean selfChange) {
209 synchronized (mLock) {
210 updateBatteryWarningLevelLocked();
211 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700212 }
Jeff Brown21392762014-06-13 19:00:36 -0700213 };
214 final ContentResolver resolver = mContext.getContentResolver();
215 resolver.registerContentObserver(Settings.Global.getUriFor(
216 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
217 false, obs, UserHandle.USER_ALL);
218 updateBatteryWarningLevelLocked();
219 }
Jeff Browna4d82042012-10-02 19:11:19 -0700220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222
Jeff Brown21392762014-06-13 19:00:36 -0700223 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700224 final ContentResolver resolver = mContext.getContentResolver();
225 int defWarnLevel = mContext.getResources().getInteger(
226 com.android.internal.R.integer.config_lowBatteryWarningLevel);
227 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
228 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
229 if (mLowBatteryWarningLevel == 0) {
230 mLowBatteryWarningLevel = defWarnLevel;
231 }
232 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
233 mLowBatteryWarningLevel = mCriticalBatteryLevel;
234 }
235 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
236 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
237 processValuesLocked(true);
238 }
239
Jeff Browna4d82042012-10-02 19:11:19 -0700240 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 // assume we are powered if battery state is unknown so
242 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700243 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 return true;
245 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700246 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700247 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700249 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700250 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700252 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700253 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Jeff Browna4d82042012-10-02 19:11:19 -0700255 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257
Jeff Brown21392762014-06-13 19:00:36 -0700258 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700259 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
260 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
261
262 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
263 * - is just un-plugged (previously was plugged) and battery level is
264 * less than or equal to WARNING, or
265 * - is not plugged and battery level falls to WARNING boundary
266 * (becomes <= mLowBatteryWarningLevel).
267 */
268 return !plugged
269 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
270 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
271 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
272 }
273
Jeff Browna4d82042012-10-02 19:11:19 -0700274 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400275 // shut down gracefully if our battery is critically low and we are not powered.
276 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700277 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700278 mHandler.post(new Runnable() {
279 @Override
280 public void run() {
281 if (ActivityManagerNative.isSystemReady()) {
282 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
283 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
284 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
285 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
286 }
287 }
288 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400289 }
290 }
291
Jeff Browna4d82042012-10-02 19:11:19 -0700292 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700293 // shut down gracefully if temperature is too high (> 68.0C by default)
294 // wait until the system has booted before attempting to display the
295 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700296 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700297 mHandler.post(new Runnable() {
298 @Override
299 public void run() {
300 if (ActivityManagerNative.isSystemReady()) {
301 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
302 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
303 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
304 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
305 }
306 }
307 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700308 }
309 }
310
Todd Poynor26faecc2013-05-22 18:54:48 -0700311 private void update(BatteryProperties props) {
312 synchronized (mLock) {
313 if (!mUpdatesStopped) {
314 mBatteryProps = props;
315 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700316 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800317 } else {
318 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700319 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700320 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
Dianne Hackborn14272302014-06-10 23:13:02 -0700323 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700324 boolean logOutlier = false;
325 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700326
Todd Poynor26faecc2013-05-22 18:54:48 -0700327 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
328 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700330 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700332 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700333 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 } else {
335 mPlugType = BATTERY_PLUGGED_NONE;
336 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700337
Jeff Browna4d82042012-10-02 19:11:19 -0700338 if (DEBUG) {
339 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700340 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
341 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
342 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700343 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700344 + ", maxChargingVoltage" + mBatteryProps.maxChargingVoltage
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700345 + ", chargeCounter" + mBatteryProps.batteryChargeCounter
Todd Poynor26faecc2013-05-22 18:54:48 -0700346 + ", batteryStatus=" + mBatteryProps.batteryStatus
347 + ", batteryHealth=" + mBatteryProps.batteryHealth
348 + ", batteryPresent=" + mBatteryProps.batteryPresent
349 + ", batteryLevel=" + mBatteryProps.batteryLevel
350 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
351 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
352 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700353 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
354 + ", mPlugType=" + mPlugType);
355 }
356
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700357 // Let the battery stats keep track of the current level.
358 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700359 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
360 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
361 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700362 } catch (RemoteException e) {
363 // Should never happen.
364 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700365
Jeff Browna4d82042012-10-02 19:11:19 -0700366 shutdownIfNoPowerLocked();
367 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700368
Dianne Hackborn14272302014-06-10 23:13:02 -0700369 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700370 mBatteryProps.batteryHealth != mLastBatteryHealth ||
371 mBatteryProps.batteryPresent != mLastBatteryPresent ||
372 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700374 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
375 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700376 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700377 mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700378 mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700379 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 if (mPlugType != mLastPlugType) {
382 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
383 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 // There's no value in this data unless we've discharged at least once and the
386 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700387 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700388 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
389 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700391 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
396 // charging -> discharging or we just powered up
397 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700398 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700401 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
402 mBatteryProps.batteryHealth != mLastBatteryHealth ||
403 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700406 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
407 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700409 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700410 // Don't do this just from voltage or temperature changes, that is
411 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700413 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
416 mPlugType == BATTERY_PLUGGED_NONE) {
417 // We want to make sure we log discharge cycle outliers
418 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700419 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
420 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
Dianne Hackborn14272302014-06-10 23:13:02 -0700423 if (!mBatteryLevelLow) {
424 // Should we now switch in to low battery mode?
425 if (mPlugType == BATTERY_PLUGGED_NONE
426 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
427 mBatteryLevelLow = true;
428 }
429 } else {
430 // Should we now switch out of low battery mode?
431 if (mPlugType != BATTERY_PLUGGED_NONE) {
432 mBatteryLevelLow = false;
433 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
434 mBatteryLevelLow = false;
435 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
436 // If being forced, the previous state doesn't matter, we will just
437 // absolutely check to see if we are now above the warning level.
438 mBatteryLevelLow = false;
439 }
440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Jeff Browna4d82042012-10-02 19:11:19 -0700442 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
Christopher Tate06ba5542009-04-09 16:03:56 -0700444 // Separate broadcast is sent for power connected / not connected
445 // since the standard intent will not wake any applications and some
446 // applications may want to have smart behavior based on this.
447 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700448 mHandler.post(new Runnable() {
449 @Override
450 public void run() {
451 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
452 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
453 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
454 }
455 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700456 }
457 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700458 mHandler.post(new Runnable() {
459 @Override
460 public void run() {
461 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
462 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
463 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
464 }
465 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700466 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200467
Dianne Hackborn14272302014-06-10 23:13:02 -0700468 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700469 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700470 mHandler.post(new Runnable() {
471 @Override
472 public void run() {
473 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
474 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
475 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
476 }
477 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400478 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700479 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700480 mHandler.post(new Runnable() {
481 @Override
482 public void run() {
483 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
484 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
485 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
486 }
487 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Joe Onoratode1b3592010-10-25 20:36:47 -0700490 // Update the battery LED
491 mLed.updateLightsLocked();
492
The Android Open Source Project10592532009-03-18 17:39:46 -0700493 // This needs to be done after sendIntent() so that we get the lastest battery stats.
494 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700495 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
Todd Poynor26faecc2013-05-22 18:54:48 -0700498 mLastBatteryStatus = mBatteryProps.batteryStatus;
499 mLastBatteryHealth = mBatteryProps.batteryHealth;
500 mLastBatteryPresent = mBatteryProps.batteryPresent;
501 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700502 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700503 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
504 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700505 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700506 mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700507 mLastChargeCounter = mBatteryProps.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700508 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400509 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 }
512
Jeff Browna4d82042012-10-02 19:11:19 -0700513 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700515 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800516 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
517 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Todd Poynor26faecc2013-05-22 18:54:48 -0700519 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
Todd Poynor26faecc2013-05-22 18:54:48 -0700521 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
522 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
523 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
524 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700525 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
526 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
527 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700528 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
529 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
530 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400531 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700532 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700533 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700534 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700535 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700536 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
537 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700538 ", health:" + mBatteryProps.batteryHealth +
539 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700540 ", voltage: " + mBatteryProps.batteryVoltage +
541 ", temperature: " + mBatteryProps.batteryTemperature +
542 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700543 ", AC powered:" + mBatteryProps.chargerAcOnline +
544 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700545 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700546 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700547 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent +
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700548 ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage +
549 ", chargeCounter:" + mBatteryProps.batteryChargeCounter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551
Jeff Brown605ea692012-10-05 16:33:10 -0700552 mHandler.post(new Runnable() {
553 @Override
554 public void run() {
555 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
556 }
557 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559
Jeff Browna4d82042012-10-02 19:11:19 -0700560 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700561 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800562 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
Dan Egnor18e93962010-02-10 19:27:58 -0800564 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
565 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
566
567 File dumpFile = null;
568 FileOutputStream dumpStream = null;
569 try {
570 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700571 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800572 dumpStream = new FileOutputStream(dumpFile);
573 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700574 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800575
576 // add dump file to drop box
577 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
578 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800579 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800580 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800581 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800582 } finally {
583 // make sure we clean up
584 if (dumpStream != null) {
585 try {
586 dumpStream.close();
587 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800588 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
Dan Egnor18e93962010-02-10 19:27:58 -0800590 }
591 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800592 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800593 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
Jeff Browna4d82042012-10-02 19:11:19 -0700598 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700600 String dischargeThresholdString = Settings.Global.getString(cr,
601 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
602 String durationThresholdString = Settings.Global.getString(cr,
603 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 if (dischargeThresholdString != null && durationThresholdString != null) {
606 try {
607 long durationThreshold = Long.parseLong(durationThresholdString);
608 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800609 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700610 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700612 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 }
Jeff Browna4d82042012-10-02 19:11:19 -0700614 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700616 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700617 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800619 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622 }
623 }
624
Jeff Browna4d82042012-10-02 19:11:19 -0700625 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700626 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700628 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700630 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
631 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700632 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700633 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800634 return com.android.internal.R.drawable.stat_sys_battery_charge;
635 } else {
636 return com.android.internal.R.drawable.stat_sys_battery;
637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 } else {
639 return com.android.internal.R.drawable.stat_sys_battery_unknown;
640 }
641 }
642
Dianne Hackborn2e441072015-10-28 18:00:57 -0700643 class Shell extends ShellCommand {
644 @Override
645 public int onCommand(String cmd) {
646 return onShellCommand(this, cmd);
647 }
648
649 @Override
650 public void onHelp() {
651 PrintWriter pw = getOutPrintWriter();
652 dumpHelp(pw);
653 }
654 }
655
656 static void dumpHelp(PrintWriter pw) {
657 pw.println("Battery service (battery) commands:");
658 pw.println(" help");
659 pw.println(" Print this help text.");
660 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
661 pw.println(" Force a battery property value, freezing battery state.");
662 pw.println(" unplug");
663 pw.println(" Force battery unplugged, freezing battery state.");
664 pw.println(" reset");
665 pw.println(" Unfreeze battery state, returning to current hardware values.");
666 }
667
668 int onShellCommand(Shell shell, String cmd) {
669 if (cmd == null) {
670 return shell.handleDefaultCommands(cmd);
671 }
672 PrintWriter pw = shell.getOutPrintWriter();
673 switch (cmd) {
674 case "unplug": {
675 getContext().enforceCallingOrSelfPermission(
676 android.Manifest.permission.DEVICE_POWER, null);
677 if (!mUpdatesStopped) {
678 mLastBatteryProps.set(mBatteryProps);
679 }
680 mBatteryProps.chargerAcOnline = false;
681 mBatteryProps.chargerUsbOnline = false;
682 mBatteryProps.chargerWirelessOnline = false;
683 long ident = Binder.clearCallingIdentity();
684 try {
685 mUpdatesStopped = true;
686 processValuesLocked(false);
687 } finally {
688 Binder.restoreCallingIdentity(ident);
689 }
690 } break;
691 case "set": {
692 getContext().enforceCallingOrSelfPermission(
693 android.Manifest.permission.DEVICE_POWER, null);
694 final String key = shell.getNextArg();
695 if (key == null) {
696 pw.println("No property specified");
697 return -1;
698
699 }
700 final String value = shell.getNextArg();
701 if (value == null) {
702 pw.println("No value specified");
703 return -1;
704
705 }
706 try {
707 if (!mUpdatesStopped) {
708 mLastBatteryProps.set(mBatteryProps);
709 }
710 boolean update = true;
711 switch (key) {
712 case "ac":
713 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
714 break;
715 case "usb":
716 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
717 break;
718 case "wireless":
719 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
720 break;
721 case "status":
722 mBatteryProps.batteryStatus = Integer.parseInt(value);
723 break;
724 case "level":
725 mBatteryProps.batteryLevel = Integer.parseInt(value);
726 break;
727 case "invalid":
728 mInvalidCharger = Integer.parseInt(value);
729 break;
730 default:
731 pw.println("Unknown set option: " + key);
732 update = false;
733 break;
734 }
735 if (update) {
736 long ident = Binder.clearCallingIdentity();
737 try {
738 mUpdatesStopped = true;
739 processValuesLocked(false);
740 } finally {
741 Binder.restoreCallingIdentity(ident);
742 }
743 }
744 } catch (NumberFormatException ex) {
745 pw.println("Bad value: " + value);
746 return -1;
747 }
748 } break;
749 case "reset": {
750 getContext().enforceCallingOrSelfPermission(
751 android.Manifest.permission.DEVICE_POWER, null);
752 long ident = Binder.clearCallingIdentity();
753 try {
754 if (mUpdatesStopped) {
755 mUpdatesStopped = false;
756 mBatteryProps.set(mLastBatteryProps);
757 processValuesLocked(false);
758 }
759 } finally {
760 Binder.restoreCallingIdentity(ident);
761 }
762 } break;
763 default:
764 return shell.handleDefaultCommands(cmd);
765 }
766 return 0;
767 }
768
769 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700770 synchronized (mLock) {
771 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700772 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700773 if (mUpdatesStopped) {
774 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
775 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700776 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
777 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
778 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700779 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700780 pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage);
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700781 pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter);
Todd Poynor26faecc2013-05-22 18:54:48 -0700782 pw.println(" status: " + mBatteryProps.batteryStatus);
783 pw.println(" health: " + mBatteryProps.batteryHealth);
784 pw.println(" present: " + mBatteryProps.batteryPresent);
785 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700786 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700787 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700788 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
789 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700790 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700791 Shell shell = new Shell();
792 shell.exec(mBinderService, null, fd, null, args, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700796
Jeff Browna4d82042012-10-02 19:11:19 -0700797 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800798 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700799
Jeff Browna4d82042012-10-02 19:11:19 -0700800 private final int mBatteryLowARGB;
801 private final int mBatteryMediumARGB;
802 private final int mBatteryFullARGB;
803 private final int mBatteryLedOn;
804 private final int mBatteryLedOff;
805
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800806 public Led(Context context, LightsManager lights) {
807 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700808
Jeff Browna4d82042012-10-02 19:11:19 -0700809 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700810 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700811 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700812 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700813 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700814 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700815 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700816 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700817 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700818 com.android.internal.R.integer.config_notificationsBatteryLedOff);
819 }
820
821 /**
822 * Synchronize on BatteryService.
823 */
Jeff Browna4d82042012-10-02 19:11:19 -0700824 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700825 final int level = mBatteryProps.batteryLevel;
826 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700827 if (level < mLowBatteryWarningLevel) {
828 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
829 // Solid red when battery is charging
830 mBatteryLight.setColor(mBatteryLowARGB);
831 } else {
832 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800833 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700834 mBatteryLedOn, mBatteryLedOff);
835 }
836 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
837 || status == BatteryManager.BATTERY_STATUS_FULL) {
838 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
839 // Solid green when full or charging and nearly full
840 mBatteryLight.setColor(mBatteryFullARGB);
841 } else {
842 // Solid orange when charging and halfway full
843 mBatteryLight.setColor(mBatteryMediumARGB);
844 }
845 } else {
846 // No lights if not charging and not low
847 mBatteryLight.turnOff();
848 }
849 }
850 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700851
852 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700853 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800854 final long identity = Binder.clearCallingIdentity();
855 try {
856 BatteryService.this.update(props);
857 } finally {
858 Binder.restoreCallingIdentity(identity);
859 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700860 }
861 }
Jeff Brown21392762014-06-13 19:00:36 -0700862
863 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700864 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Brown21392762014-06-13 19:00:36 -0700865 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
866 != PackageManager.PERMISSION_GRANTED) {
867
868 pw.println("Permission Denial: can't dump Battery service from from pid="
869 + Binder.getCallingPid()
870 + ", uid=" + Binder.getCallingUid());
871 return;
872 }
873
Dianne Hackborn2e441072015-10-28 18:00:57 -0700874 dumpInternal(fd, pw, args);
875 }
876
877 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
878 FileDescriptor err, String[] args, ResultReceiver resultReceiver) {
879 (new Shell()).exec(this, in, out, err, args, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700880 }
881 }
882
883 private final class LocalService extends BatteryManagerInternal {
884 @Override
885 public boolean isPowered(int plugTypeSet) {
886 synchronized (mLock) {
887 return isPoweredLocked(plugTypeSet);
888 }
889 }
890
891 @Override
892 public int getPlugType() {
893 synchronized (mLock) {
894 return mPlugType;
895 }
896 }
897
898 @Override
899 public int getBatteryLevel() {
900 synchronized (mLock) {
901 return mBatteryProps.batteryLevel;
902 }
903 }
904
905 @Override
906 public boolean getBatteryLevelLow() {
907 synchronized (mLock) {
908 return mBatteryLevelLow;
909 }
910 }
911
912 @Override
913 public int getInvalidCharger() {
914 synchronized (mLock) {
915 return mInvalidCharger;
916 }
917 }
918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919}