blob: 6d9c977fe4c71a57632eff7675036ea762ae8aba [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070019import android.app.ActivityManagerInternal;
Dianne Hackborn14272302014-06-10 23:13:02 -070020import android.database.ContentObserver;
Dianne Hackborn8c841092013-06-24 13:46:13 -070021import android.os.BatteryStats;
Jeff Brown21392762014-06-13 19:00:36 -070022
Sudheer Shanka292637f2017-09-25 10:36:23 -070023import android.os.PowerManager;
Dianne Hackborn2e441072015-10-28 18:00:57 -070024import android.os.ResultReceiver;
Dianne Hackborn354736e2016-08-22 17:00:05 -070025import android.os.ShellCallback;
Dianne Hackborn2e441072015-10-28 18:00:57 -070026import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.app.IBatteryStats;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060028import com.android.internal.util.DumpUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080030import com.android.server.lights.Light;
31import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Sudheer Shankadc589ac2016-11-10 15:30:17 -080033import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.ContentResolver;
35import android.content.Context;
36import android.content.Intent;
37import android.content.pm.PackageManager;
Yifan Hong932190b2017-10-11 11:00:51 -070038import android.hardware.health.V2_0.HealthInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070040import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070041import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070043import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070044import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070045import android.os.IBatteryPropertiesListener;
46import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080048import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.RemoteException;
50import android.os.ServiceManager;
51import android.os.SystemClock;
52import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070053import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.provider.Settings;
Netta Pe2a3cd82017-01-26 18:03:51 -080055import android.service.battery.BatteryServiceDumpProto;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080057import android.util.Slog;
Netta Pe2a3cd82017-01-26 18:03:51 -080058import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60import java.io.File;
61import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import java.io.FileOutputStream;
63import java.io.IOException;
64import java.io.PrintWriter;
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67/**
68 * <p>BatteryService monitors the charging status, and charge level of the device
69 * battery. When these values change this service broadcasts the new values
70 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
71 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
72 * BATTERY_CHANGED} action.</p>
73 * <p>The new values are stored in the Intent data and can be retrieved by
74 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
75 * following keys:</p>
76 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
77 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
78 * <p>&quot;status&quot; - String, the current charging status.<br />
79 * <p>&quot;health&quot; - String, the current battery health.<br />
80 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
81 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
82 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
83 * into an AC power adapter; 2 if plugged in via USB.</p>
84 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
85 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
86 * a degree Centigrade</p>
87 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070088 *
89 * <p>
90 * The battery service may be called by the power manager while holding its locks so
91 * we take care to post all outcalls into the activity manager to a handler.
92 *
93 * FIXME: Ideally the power manager would perform all of its calls into the battery
94 * service asynchronously itself.
95 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 */
Jeff Brown21392762014-06-13 19:00:36 -070097public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080099
Jeff Browna4d82042012-10-02 19:11:19 -0700100 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800101
Jeff Browna4d82042012-10-02 19:11:19 -0700102 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 // Used locally for determining when to make a last ditch effort to log
105 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700106 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700108 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 private static final String DUMPSYS_DATA_PATH = "/data/system/";
111
112 // This should probably be exposed in the API, though it's not critical
113 private static final int BATTERY_PLUGGED_NONE = 0;
114
115 private final Context mContext;
116 private final IBatteryStats mBatteryStats;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700117 BinderService mBinderService;
Jeff Brown605ea692012-10-05 16:33:10 -0700118 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
Jeff Browna4d82042012-10-02 19:11:19 -0700120 private final Object mLock = new Object();
121
Yifan Hong932190b2017-10-11 11:00:51 -0700122 private HealthInfo mHealthInfo;
123 private final HealthInfo mLastHealthInfo = new HealthInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 private int mLastBatteryStatus;
126 private int mLastBatteryHealth;
127 private boolean mLastBatteryPresent;
128 private int mLastBatteryLevel;
129 private int mLastBatteryVoltage;
130 private int mLastBatteryTemperature;
131 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700132 private int mLastMaxChargingCurrent;
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700133 private int mLastMaxChargingVoltage;
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700134 private int mLastChargeCounter;
Jeff Browna4d82042012-10-02 19:11:19 -0700135
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800136 private int mSequence = 1;
137
Jeff Browna4d82042012-10-02 19:11:19 -0700138 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700139 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400140
141 private int mLowBatteryWarningLevel;
142 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700143 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private int mPlugType;
146 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700148 private boolean mBatteryLevelLow;
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 private long mDischargeStartTime;
151 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700153 private boolean mUpdatesStopped;
154
Joe Onoratode1b3592010-10-25 20:36:47 -0700155 private Led mLed;
156
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700157 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700159 private ActivityManagerInternal mActivityManagerInternal;
160
Jeff Brown21392762014-06-13 19:00:36 -0700161 public BatteryService(Context context) {
162 super(context);
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700165 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700166 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 mBatteryStats = BatteryStatsService.getService();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700168 mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700170 mCriticalBatteryLevel = mContext.getResources().getInteger(
171 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400172 mLowBatteryWarningLevel = mContext.getResources().getInteger(
173 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700174 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
175 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700176 mShutdownBatteryTemperature = mContext.getResources().getInteger(
177 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400178
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400179 // watch for invalid charger messages if the invalid_charger switch exists
180 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700181 UEventObserver invalidChargerObserver = new UEventObserver() {
182 @Override
183 public void onUEvent(UEvent event) {
184 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
185 synchronized (mLock) {
186 if (mInvalidCharger != invalidCharger) {
187 mInvalidCharger = invalidCharger;
188 }
189 }
190 }
191 };
192 invalidChargerObserver.startObserving(
Jeff Browna4d82042012-10-02 19:11:19 -0700193 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400194 }
Jeff Brown21392762014-06-13 19:00:36 -0700195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Jeff Brown21392762014-06-13 19:00:36 -0700197 @Override
198 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700199 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800200 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
201 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700202 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800203 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700204 } catch (RemoteException e) {
205 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700206 }
Jeff Brown21392762014-06-13 19:00:36 -0700207
Dianne Hackborn2e441072015-10-28 18:00:57 -0700208 mBinderService = new BinderService();
209 publishBinderService("battery", mBinderService);
Jeff Brown21392762014-06-13 19:00:36 -0700210 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212
Jeff Brown21392762014-06-13 19:00:36 -0700213 @Override
214 public void onBootPhase(int phase) {
215 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
216 // check our power situation now that it is safe to display the shutdown dialog.
217 synchronized (mLock) {
218 ContentObserver obs = new ContentObserver(mHandler) {
219 @Override
220 public void onChange(boolean selfChange) {
221 synchronized (mLock) {
222 updateBatteryWarningLevelLocked();
223 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700224 }
Jeff Brown21392762014-06-13 19:00:36 -0700225 };
226 final ContentResolver resolver = mContext.getContentResolver();
227 resolver.registerContentObserver(Settings.Global.getUriFor(
228 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
229 false, obs, UserHandle.USER_ALL);
230 updateBatteryWarningLevelLocked();
231 }
Jeff Browna4d82042012-10-02 19:11:19 -0700232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234
Jeff Brown21392762014-06-13 19:00:36 -0700235 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700236 final ContentResolver resolver = mContext.getContentResolver();
237 int defWarnLevel = mContext.getResources().getInteger(
238 com.android.internal.R.integer.config_lowBatteryWarningLevel);
239 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
240 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
241 if (mLowBatteryWarningLevel == 0) {
242 mLowBatteryWarningLevel = defWarnLevel;
243 }
244 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
245 mLowBatteryWarningLevel = mCriticalBatteryLevel;
246 }
247 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
248 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
249 processValuesLocked(true);
250 }
251
Jeff Browna4d82042012-10-02 19:11:19 -0700252 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 // assume we are powered if battery state is unknown so
254 // the "stay on while plugged in" option will work.
Yifan Hong932190b2017-10-11 11:00:51 -0700255 if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 return true;
257 }
Yifan Hong932190b2017-10-11 11:00:51 -0700258 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mHealthInfo.legacy.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700259 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
Yifan Hong932190b2017-10-11 11:00:51 -0700261 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mHealthInfo.legacy.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700262 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
Yifan Hong932190b2017-10-11 11:00:51 -0700264 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mHealthInfo.legacy.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700265 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
Jeff Browna4d82042012-10-02 19:11:19 -0700267 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 }
269
Jeff Brown21392762014-06-13 19:00:36 -0700270 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700271 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
272 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
273
274 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
275 * - is just un-plugged (previously was plugged) and battery level is
276 * less than or equal to WARNING, or
277 * - is not plugged and battery level falls to WARNING boundary
278 * (becomes <= mLowBatteryWarningLevel).
279 */
280 return !plugged
Yifan Hong932190b2017-10-11 11:00:51 -0700281 && mHealthInfo.legacy.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
282 && mHealthInfo.legacy.batteryLevel <= mLowBatteryWarningLevel
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700283 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
284 }
285
Jeff Browna4d82042012-10-02 19:11:19 -0700286 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400287 // shut down gracefully if our battery is critically low and we are not powered.
288 // wait until the system has booted before attempting to display the shutdown dialog.
Yifan Hong932190b2017-10-11 11:00:51 -0700289 if (mHealthInfo.legacy.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700290 mHandler.post(new Runnable() {
291 @Override
292 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700293 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700294 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
295 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
Sudheer Shanka292637f2017-09-25 10:36:23 -0700296 intent.putExtra(Intent.EXTRA_REASON,
297 PowerManager.SHUTDOWN_LOW_BATTERY);
Jeff Brown605ea692012-10-05 16:33:10 -0700298 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
299 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
300 }
301 }
302 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400303 }
304 }
305
Jeff Browna4d82042012-10-02 19:11:19 -0700306 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700307 // shut down gracefully if temperature is too high (> 68.0C by default)
308 // wait until the system has booted before attempting to display the
309 // shutdown dialog.
Yifan Hong932190b2017-10-11 11:00:51 -0700310 if (mHealthInfo.legacy.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700311 mHandler.post(new Runnable() {
312 @Override
313 public void run() {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700314 if (mActivityManagerInternal.isSystemReady()) {
Jeff Brown605ea692012-10-05 16:33:10 -0700315 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
316 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
Sudheer Shanka292637f2017-09-25 10:36:23 -0700317 intent.putExtra(Intent.EXTRA_REASON,
318 PowerManager.SHUTDOWN_BATTERY_THERMAL_STATE);
Jeff Brown605ea692012-10-05 16:33:10 -0700319 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
320 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
321 }
322 }
323 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700324 }
325 }
326
Todd Poynor26faecc2013-05-22 18:54:48 -0700327 private void update(BatteryProperties props) {
328 synchronized (mLock) {
329 if (!mUpdatesStopped) {
Yifan Hong932190b2017-10-11 11:00:51 -0700330 mHealthInfo = new HealthInfo();
331 copy(mHealthInfo, props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700332 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700333 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800334 } else {
Yifan Hong932190b2017-10-11 11:00:51 -0700335 copy(mLastHealthInfo, props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700336 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700337 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339
Yifan Hong932190b2017-10-11 11:00:51 -0700340 private static void copy(HealthInfo dst, HealthInfo src) {
341 dst.legacy.chargerAcOnline = src.legacy.chargerAcOnline;
342 dst.legacy.chargerUsbOnline = src.legacy.chargerUsbOnline;
343 dst.legacy.chargerWirelessOnline = src.legacy.chargerWirelessOnline;
344 dst.legacy.maxChargingCurrent = src.legacy.maxChargingCurrent;
345 dst.legacy.maxChargingVoltage = src.legacy.maxChargingVoltage;
346 dst.legacy.batteryStatus = src.legacy.batteryStatus;
347 dst.legacy.batteryHealth = src.legacy.batteryHealth;
348 dst.legacy.batteryPresent = src.legacy.batteryPresent;
349 dst.legacy.batteryLevel = src.legacy.batteryLevel;
350 dst.legacy.batteryVoltage = src.legacy.batteryVoltage;
351 dst.legacy.batteryTemperature = src.legacy.batteryTemperature;
352 dst.legacy.batteryCurrent = src.legacy.batteryCurrent;
353 dst.legacy.batteryCycleCount = src.legacy.batteryCycleCount;
354 dst.legacy.batteryFullCharge = src.legacy.batteryFullCharge;
355 dst.legacy.batteryChargeCounter = src.legacy.batteryChargeCounter;
356 dst.legacy.batteryTechnology = src.legacy.batteryTechnology;
357 dst.batteryCurrentAverage = src.batteryCurrentAverage;
358 dst.batteryCapacity = src.batteryCapacity;
359 dst.energyCounter = src.energyCounter;
360 }
361
362 // TODO(b/62229583): remove this function when BatteryProperties are completely replaced.
363 private static void copy(HealthInfo dst, BatteryProperties src) {
364 dst.legacy.chargerAcOnline = src.chargerAcOnline;
365 dst.legacy.chargerUsbOnline = src.chargerUsbOnline;
366 dst.legacy.chargerWirelessOnline = src.chargerWirelessOnline;
367 dst.legacy.maxChargingCurrent = src.maxChargingCurrent;
368 dst.legacy.maxChargingVoltage = src.maxChargingVoltage;
369 dst.legacy.batteryStatus = src.batteryStatus;
370 dst.legacy.batteryHealth = src.batteryHealth;
371 dst.legacy.batteryPresent = src.batteryPresent;
372 dst.legacy.batteryLevel = src.batteryLevel;
373 dst.legacy.batteryVoltage = src.batteryVoltage;
374 dst.legacy.batteryTemperature = src.batteryTemperature;
375 dst.legacy.batteryFullCharge = src.batteryFullCharge;
376 dst.legacy.batteryChargeCounter = src.batteryChargeCounter;
377 dst.legacy.batteryTechnology = src.batteryTechnology;
378 }
379
Dianne Hackborn14272302014-06-10 23:13:02 -0700380 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700381 boolean logOutlier = false;
382 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700383
Yifan Hong932190b2017-10-11 11:00:51 -0700384 mBatteryLevelCritical = (mHealthInfo.legacy.batteryLevel <= mCriticalBatteryLevel);
385 if (mHealthInfo.legacy.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Yifan Hong932190b2017-10-11 11:00:51 -0700387 } else if (mHealthInfo.legacy.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Yifan Hong932190b2017-10-11 11:00:51 -0700389 } else if (mHealthInfo.legacy.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700390 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 } else {
392 mPlugType = BATTERY_PLUGGED_NONE;
393 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700394
Jeff Browna4d82042012-10-02 19:11:19 -0700395 if (DEBUG) {
396 Slog.d(TAG, "Processing new values: "
Yifan Hong932190b2017-10-11 11:00:51 -0700397 + "info=" + mHealthInfo
Jeff Browna4d82042012-10-02 19:11:19 -0700398 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
399 + ", mPlugType=" + mPlugType);
400 }
401
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700402 // Let the battery stats keep track of the current level.
403 try {
Yifan Hong932190b2017-10-11 11:00:51 -0700404 mBatteryStats.setBatteryState(mHealthInfo.legacy.batteryStatus, mHealthInfo.legacy.batteryHealth,
405 mPlugType, mHealthInfo.legacy.batteryLevel, mHealthInfo.legacy.batteryTemperature,
406 mHealthInfo.legacy.batteryVoltage, mHealthInfo.legacy.batteryChargeCounter,
407 mHealthInfo.legacy.batteryFullCharge);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700408 } catch (RemoteException e) {
409 // Should never happen.
410 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700411
Jeff Browna4d82042012-10-02 19:11:19 -0700412 shutdownIfNoPowerLocked();
413 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700414
Yifan Hong932190b2017-10-11 11:00:51 -0700415 if (force || (mHealthInfo.legacy.batteryStatus != mLastBatteryStatus ||
416 mHealthInfo.legacy.batteryHealth != mLastBatteryHealth ||
417 mHealthInfo.legacy.batteryPresent != mLastBatteryPresent ||
418 mHealthInfo.legacy.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 mPlugType != mLastPlugType ||
Yifan Hong932190b2017-10-11 11:00:51 -0700420 mHealthInfo.legacy.batteryVoltage != mLastBatteryVoltage ||
421 mHealthInfo.legacy.batteryTemperature != mLastBatteryTemperature ||
422 mHealthInfo.legacy.maxChargingCurrent != mLastMaxChargingCurrent ||
423 mHealthInfo.legacy.maxChargingVoltage != mLastMaxChargingVoltage ||
424 mHealthInfo.legacy.batteryChargeCounter != mLastChargeCounter ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700425 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 if (mPlugType != mLastPlugType) {
428 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
429 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 // There's no value in this data unless we've discharged at least once and the
432 // battery level has changed; so don't log until it does.
Yifan Hong932190b2017-10-11 11:00:51 -0700433 if (mDischargeStartTime != 0 && mDischargeStartLevel != mHealthInfo.legacy.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700434 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
435 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800436 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Yifan Hong932190b2017-10-11 11:00:51 -0700437 mDischargeStartLevel, mHealthInfo.legacy.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
442 // charging -> discharging or we just powered up
443 mDischargeStartTime = SystemClock.elapsedRealtime();
Yifan Hong932190b2017-10-11 11:00:51 -0700444 mDischargeStartLevel = mHealthInfo.legacy.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446 }
Yifan Hong932190b2017-10-11 11:00:51 -0700447 if (mHealthInfo.legacy.batteryStatus != mLastBatteryStatus ||
448 mHealthInfo.legacy.batteryHealth != mLastBatteryHealth ||
449 mHealthInfo.legacy.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Yifan Hong932190b2017-10-11 11:00:51 -0700452 mHealthInfo.legacy.batteryStatus, mHealthInfo.legacy.batteryHealth, mHealthInfo.legacy.batteryPresent ? 1 : 0,
453 mPlugType, mHealthInfo.legacy.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
Yifan Hong932190b2017-10-11 11:00:51 -0700455 if (mHealthInfo.legacy.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700456 // Don't do this just from voltage or temperature changes, that is
457 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Yifan Hong932190b2017-10-11 11:00:51 -0700459 mHealthInfo.legacy.batteryLevel, mHealthInfo.legacy.batteryVoltage, mHealthInfo.legacy.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
462 mPlugType == BATTERY_PLUGGED_NONE) {
463 // We want to make sure we log discharge cycle outliers
464 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700465 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
466 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Dianne Hackborn14272302014-06-10 23:13:02 -0700469 if (!mBatteryLevelLow) {
470 // Should we now switch in to low battery mode?
471 if (mPlugType == BATTERY_PLUGGED_NONE
Yifan Hong932190b2017-10-11 11:00:51 -0700472 && mHealthInfo.legacy.batteryLevel <= mLowBatteryWarningLevel) {
Dianne Hackborn14272302014-06-10 23:13:02 -0700473 mBatteryLevelLow = true;
474 }
475 } else {
476 // Should we now switch out of low battery mode?
477 if (mPlugType != BATTERY_PLUGGED_NONE) {
478 mBatteryLevelLow = false;
Yifan Hong932190b2017-10-11 11:00:51 -0700479 } else if (mHealthInfo.legacy.batteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn14272302014-06-10 23:13:02 -0700480 mBatteryLevelLow = false;
Yifan Hong932190b2017-10-11 11:00:51 -0700481 } else if (force && mHealthInfo.legacy.batteryLevel >= mLowBatteryWarningLevel) {
Dianne Hackborn14272302014-06-10 23:13:02 -0700482 // If being forced, the previous state doesn't matter, we will just
483 // absolutely check to see if we are now above the warning level.
484 mBatteryLevelLow = false;
485 }
486 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800488 mSequence++;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Christopher Tate06ba5542009-04-09 16:03:56 -0700490 // Separate broadcast is sent for power connected / not connected
491 // since the standard intent will not wake any applications and some
492 // applications may want to have smart behavior based on this.
493 if (mPlugType != 0 && mLastPlugType == 0) {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800494 final Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
495 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
496 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700497 mHandler.post(new Runnable() {
498 @Override
499 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700500 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
501 }
502 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700503 }
504 else if (mPlugType == 0 && mLastPlugType != 0) {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800505 final Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
506 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
507 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700508 mHandler.post(new Runnable() {
509 @Override
510 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700511 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
512 }
513 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700514 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200515
Dianne Hackborn14272302014-06-10 23:13:02 -0700516 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700517 mSentLowBatteryBroadcast = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800518 final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
519 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
520 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700521 mHandler.post(new Runnable() {
522 @Override
523 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700524 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
525 }
526 });
Dianne Hackborn532ea262017-03-17 17:50:55 -0700527 } else if (mSentLowBatteryBroadcast &&
Yifan Hong932190b2017-10-11 11:00:51 -0700528 mHealthInfo.legacy.batteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700529 mSentLowBatteryBroadcast = false;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800530 final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
531 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
532 statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Jeff Brown605ea692012-10-05 16:33:10 -0700533 mHandler.post(new Runnable() {
534 @Override
535 public void run() {
Jeff Brown605ea692012-10-05 16:33:10 -0700536 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
537 }
538 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800540
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800541 // We are doing this after sending the above broadcasts, so anything processing
542 // them will get the new sequence number at that point. (See for example how testing
543 // of JobScheduler's BatteryController works.)
544 sendIntentLocked();
545
Joe Onoratode1b3592010-10-25 20:36:47 -0700546 // Update the battery LED
547 mLed.updateLightsLocked();
548
The Android Open Source Project10592532009-03-18 17:39:46 -0700549 // This needs to be done after sendIntent() so that we get the lastest battery stats.
550 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700551 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553
Yifan Hong932190b2017-10-11 11:00:51 -0700554 mLastBatteryStatus = mHealthInfo.legacy.batteryStatus;
555 mLastBatteryHealth = mHealthInfo.legacy.batteryHealth;
556 mLastBatteryPresent = mHealthInfo.legacy.batteryPresent;
557 mLastBatteryLevel = mHealthInfo.legacy.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700558 mLastPlugType = mPlugType;
Yifan Hong932190b2017-10-11 11:00:51 -0700559 mLastBatteryVoltage = mHealthInfo.legacy.batteryVoltage;
560 mLastBatteryTemperature = mHealthInfo.legacy.batteryTemperature;
561 mLastMaxChargingCurrent = mHealthInfo.legacy.maxChargingCurrent;
562 mLastMaxChargingVoltage = mHealthInfo.legacy.maxChargingVoltage;
563 mLastChargeCounter = mHealthInfo.legacy.batteryChargeCounter;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700564 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400565 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 }
568
Jeff Browna4d82042012-10-02 19:11:19 -0700569 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700571 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800572 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
573 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
Yifan Hong932190b2017-10-11 11:00:51 -0700575 int icon = getIconLocked(mHealthInfo.legacy.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800577 intent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence);
Yifan Hong932190b2017-10-11 11:00:51 -0700578 intent.putExtra(BatteryManager.EXTRA_STATUS, mHealthInfo.legacy.batteryStatus);
579 intent.putExtra(BatteryManager.EXTRA_HEALTH, mHealthInfo.legacy.batteryHealth);
580 intent.putExtra(BatteryManager.EXTRA_PRESENT, mHealthInfo.legacy.batteryPresent);
581 intent.putExtra(BatteryManager.EXTRA_LEVEL, mHealthInfo.legacy.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700582 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
583 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
584 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Yifan Hong932190b2017-10-11 11:00:51 -0700585 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mHealthInfo.legacy.batteryVoltage);
586 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mHealthInfo.legacy.batteryTemperature);
587 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mHealthInfo.legacy.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400588 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Yifan Hong932190b2017-10-11 11:00:51 -0700589 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mHealthInfo.legacy.maxChargingCurrent);
590 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mHealthInfo.legacy.maxChargingVoltage);
591 intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mHealthInfo.legacy.batteryChargeCounter);
Jeff Browna4d82042012-10-02 19:11:19 -0700592 if (DEBUG) {
Yifan Hong932190b2017-10-11 11:00:51 -0700593 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. scale:" + BATTERY_SCALE
594 + ", info:" + mHealthInfo.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
Jeff Brown605ea692012-10-05 16:33:10 -0700597 mHandler.post(new Runnable() {
598 @Override
599 public void run() {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800600 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jeff Brown605ea692012-10-05 16:33:10 -0700601 }
602 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604
Jeff Browna4d82042012-10-02 19:11:19 -0700605 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700606 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800607 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
Dan Egnor18e93962010-02-10 19:27:58 -0800609 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
610 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
611
612 File dumpFile = null;
613 FileOutputStream dumpStream = null;
614 try {
615 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700616 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800617 dumpStream = new FileOutputStream(dumpFile);
618 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700619 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800620
621 // add dump file to drop box
622 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
623 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800624 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800625 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800626 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800627 } finally {
628 // make sure we clean up
629 if (dumpStream != null) {
630 try {
631 dumpStream.close();
632 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800633 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
Dan Egnor18e93962010-02-10 19:27:58 -0800635 }
636 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800638 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640 }
641 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800642
Jeff Browna4d82042012-10-02 19:11:19 -0700643 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700645 String dischargeThresholdString = Settings.Global.getString(cr,
646 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
647 String durationThresholdString = Settings.Global.getString(cr,
648 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 if (dischargeThresholdString != null && durationThresholdString != null) {
651 try {
652 long durationThreshold = Long.parseLong(durationThresholdString);
653 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800654 if (duration <= durationThreshold &&
Yifan Hong932190b2017-10-11 11:00:51 -0700655 mDischargeStartLevel - mHealthInfo.legacy.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700657 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
Jeff Browna4d82042012-10-02 19:11:19 -0700659 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700661 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Yifan Hong932190b2017-10-11 11:00:51 -0700662 (mDischargeStartLevel - mHealthInfo.legacy.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800664 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 durationThresholdString + " or " + dischargeThresholdString);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667 }
668 }
669
Jeff Browna4d82042012-10-02 19:11:19 -0700670 private int getIconLocked(int level) {
Yifan Hong932190b2017-10-11 11:00:51 -0700671 if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 return com.android.internal.R.drawable.stat_sys_battery_charge;
Yifan Hong932190b2017-10-11 11:00:51 -0700673 } else if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 return com.android.internal.R.drawable.stat_sys_battery;
Yifan Hong932190b2017-10-11 11:00:51 -0700675 } else if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
676 || mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700677 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Yifan Hong932190b2017-10-11 11:00:51 -0700678 && mHealthInfo.legacy.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800679 return com.android.internal.R.drawable.stat_sys_battery_charge;
680 } else {
681 return com.android.internal.R.drawable.stat_sys_battery;
682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 } else {
684 return com.android.internal.R.drawable.stat_sys_battery_unknown;
685 }
686 }
687
Dianne Hackborn2e441072015-10-28 18:00:57 -0700688 class Shell extends ShellCommand {
689 @Override
690 public int onCommand(String cmd) {
691 return onShellCommand(this, cmd);
692 }
693
694 @Override
695 public void onHelp() {
696 PrintWriter pw = getOutPrintWriter();
697 dumpHelp(pw);
698 }
699 }
700
701 static void dumpHelp(PrintWriter pw) {
702 pw.println("Battery service (battery) commands:");
703 pw.println(" help");
704 pw.println(" Print this help text.");
Adam Lesinski29ddfe52017-03-29 19:29:00 -0700705 pw.println(" set [-f] [ac|usb|wireless|status|level|temp|present|invalid] <value>");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700706 pw.println(" Force a battery property value, freezing battery state.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800707 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
708 pw.println(" unplug [-f]");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700709 pw.println(" Force battery unplugged, freezing battery state.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800710 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
711 pw.println(" reset [-f]");
Dianne Hackborn2e441072015-10-28 18:00:57 -0700712 pw.println(" Unfreeze battery state, returning to current hardware values.");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800713 pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
714 }
715
716 static final int OPTION_FORCE_UPDATE = 1<<0;
717
718 int parseOptions(Shell shell) {
719 String opt;
720 int opts = 0;
721 while ((opt = shell.getNextOption()) != null) {
722 if ("-f".equals(opt)) {
723 opts |= OPTION_FORCE_UPDATE;
724 }
725 }
726 return opts;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700727 }
728
729 int onShellCommand(Shell shell, String cmd) {
730 if (cmd == null) {
731 return shell.handleDefaultCommands(cmd);
732 }
733 PrintWriter pw = shell.getOutPrintWriter();
734 switch (cmd) {
735 case "unplug": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800736 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700737 getContext().enforceCallingOrSelfPermission(
738 android.Manifest.permission.DEVICE_POWER, null);
739 if (!mUpdatesStopped) {
Yifan Hong932190b2017-10-11 11:00:51 -0700740 copy(mLastHealthInfo, mHealthInfo);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700741 }
Yifan Hong932190b2017-10-11 11:00:51 -0700742 mHealthInfo.legacy.chargerAcOnline = false;
743 mHealthInfo.legacy.chargerUsbOnline = false;
744 mHealthInfo.legacy.chargerWirelessOnline = false;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700745 long ident = Binder.clearCallingIdentity();
746 try {
747 mUpdatesStopped = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800748 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700749 } finally {
750 Binder.restoreCallingIdentity(ident);
751 }
752 } break;
753 case "set": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800754 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700755 getContext().enforceCallingOrSelfPermission(
756 android.Manifest.permission.DEVICE_POWER, null);
757 final String key = shell.getNextArg();
758 if (key == null) {
759 pw.println("No property specified");
760 return -1;
761
762 }
763 final String value = shell.getNextArg();
764 if (value == null) {
765 pw.println("No value specified");
766 return -1;
767
768 }
769 try {
770 if (!mUpdatesStopped) {
Yifan Hong932190b2017-10-11 11:00:51 -0700771 copy(mLastHealthInfo, mHealthInfo);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700772 }
773 boolean update = true;
774 switch (key) {
Christopher Tate630d98b2017-03-07 14:12:26 -0800775 case "present":
Yifan Hong932190b2017-10-11 11:00:51 -0700776 mHealthInfo.legacy.batteryPresent = Integer.parseInt(value) != 0;
Christopher Tate630d98b2017-03-07 14:12:26 -0800777 break;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700778 case "ac":
Yifan Hong932190b2017-10-11 11:00:51 -0700779 mHealthInfo.legacy.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700780 break;
781 case "usb":
Yifan Hong932190b2017-10-11 11:00:51 -0700782 mHealthInfo.legacy.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700783 break;
784 case "wireless":
Yifan Hong932190b2017-10-11 11:00:51 -0700785 mHealthInfo.legacy.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700786 break;
787 case "status":
Yifan Hong932190b2017-10-11 11:00:51 -0700788 mHealthInfo.legacy.batteryStatus = Integer.parseInt(value);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700789 break;
790 case "level":
Yifan Hong932190b2017-10-11 11:00:51 -0700791 mHealthInfo.legacy.batteryLevel = Integer.parseInt(value);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700792 break;
Adam Lesinski29ddfe52017-03-29 19:29:00 -0700793 case "temp":
Yifan Hong932190b2017-10-11 11:00:51 -0700794 mHealthInfo.legacy.batteryTemperature = Integer.parseInt(value);
Adam Lesinski29ddfe52017-03-29 19:29:00 -0700795 break;
Dianne Hackborn2e441072015-10-28 18:00:57 -0700796 case "invalid":
797 mInvalidCharger = Integer.parseInt(value);
798 break;
799 default:
800 pw.println("Unknown set option: " + key);
801 update = false;
802 break;
803 }
804 if (update) {
805 long ident = Binder.clearCallingIdentity();
806 try {
807 mUpdatesStopped = true;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800808 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700809 } finally {
810 Binder.restoreCallingIdentity(ident);
811 }
812 }
813 } catch (NumberFormatException ex) {
814 pw.println("Bad value: " + value);
815 return -1;
816 }
817 } break;
818 case "reset": {
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800819 int opts = parseOptions(shell);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700820 getContext().enforceCallingOrSelfPermission(
821 android.Manifest.permission.DEVICE_POWER, null);
822 long ident = Binder.clearCallingIdentity();
823 try {
824 if (mUpdatesStopped) {
825 mUpdatesStopped = false;
Yifan Hong932190b2017-10-11 11:00:51 -0700826 copy(mHealthInfo, mLastHealthInfo);
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800827 processValuesFromShellLocked(pw, opts);
Dianne Hackborn2e441072015-10-28 18:00:57 -0700828 }
829 } finally {
830 Binder.restoreCallingIdentity(ident);
831 }
832 } break;
833 default:
834 return shell.handleDefaultCommands(cmd);
835 }
836 return 0;
837 }
838
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800839 private void processValuesFromShellLocked(PrintWriter pw, int opts) {
840 processValuesLocked((opts & OPTION_FORCE_UPDATE) != 0);
841 if ((opts & OPTION_FORCE_UPDATE) != 0) {
842 pw.println(mSequence);
843 }
844 }
845
Dianne Hackborn2e441072015-10-28 18:00:57 -0700846 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700847 synchronized (mLock) {
848 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700849 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700850 if (mUpdatesStopped) {
851 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
852 }
Yifan Hong932190b2017-10-11 11:00:51 -0700853 pw.println(" AC powered: " + mHealthInfo.legacy.chargerAcOnline);
854 pw.println(" USB powered: " + mHealthInfo.legacy.chargerUsbOnline);
855 pw.println(" Wireless powered: " + mHealthInfo.legacy.chargerWirelessOnline);
856 pw.println(" Max charging current: " + mHealthInfo.legacy.maxChargingCurrent);
857 pw.println(" Max charging voltage: " + mHealthInfo.legacy.maxChargingVoltage);
858 pw.println(" Charge counter: " + mHealthInfo.legacy.batteryChargeCounter);
859 pw.println(" status: " + mHealthInfo.legacy.batteryStatus);
860 pw.println(" health: " + mHealthInfo.legacy.batteryHealth);
861 pw.println(" present: " + mHealthInfo.legacy.batteryPresent);
862 pw.println(" level: " + mHealthInfo.legacy.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700863 pw.println(" scale: " + BATTERY_SCALE);
Yifan Hong932190b2017-10-11 11:00:51 -0700864 pw.println(" voltage: " + mHealthInfo.legacy.batteryVoltage);
865 pw.println(" temperature: " + mHealthInfo.legacy.batteryTemperature);
866 pw.println(" technology: " + mHealthInfo.legacy.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700867 } else {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700868 Shell shell = new Shell();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700869 shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700873
Netta Pe2a3cd82017-01-26 18:03:51 -0800874 private void dumpProto(FileDescriptor fd) {
875 final ProtoOutputStream proto = new ProtoOutputStream(fd);
876
877 synchronized (mLock) {
878 proto.write(BatteryServiceDumpProto.ARE_UPDATES_STOPPED, mUpdatesStopped);
879 int batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_NONE;
Yifan Hong932190b2017-10-11 11:00:51 -0700880 if (mHealthInfo.legacy.chargerAcOnline) {
Netta Pe2a3cd82017-01-26 18:03:51 -0800881 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_AC;
Yifan Hong932190b2017-10-11 11:00:51 -0700882 } else if (mHealthInfo.legacy.chargerUsbOnline) {
Netta Pe2a3cd82017-01-26 18:03:51 -0800883 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_USB;
Yifan Hong932190b2017-10-11 11:00:51 -0700884 } else if (mHealthInfo.legacy.chargerWirelessOnline) {
Netta Pe2a3cd82017-01-26 18:03:51 -0800885 batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_WIRELESS;
886 }
887 proto.write(BatteryServiceDumpProto.PLUGGED, batteryPluggedValue);
Yifan Hong932190b2017-10-11 11:00:51 -0700888 proto.write(BatteryServiceDumpProto.MAX_CHARGING_CURRENT, mHealthInfo.legacy.maxChargingCurrent);
889 proto.write(BatteryServiceDumpProto.MAX_CHARGING_VOLTAGE, mHealthInfo.legacy.maxChargingVoltage);
890 proto.write(BatteryServiceDumpProto.CHARGE_COUNTER, mHealthInfo.legacy.batteryChargeCounter);
891 proto.write(BatteryServiceDumpProto.STATUS, mHealthInfo.legacy.batteryStatus);
892 proto.write(BatteryServiceDumpProto.HEALTH, mHealthInfo.legacy.batteryHealth);
893 proto.write(BatteryServiceDumpProto.IS_PRESENT, mHealthInfo.legacy.batteryPresent);
894 proto.write(BatteryServiceDumpProto.LEVEL, mHealthInfo.legacy.batteryLevel);
Netta Pe2a3cd82017-01-26 18:03:51 -0800895 proto.write(BatteryServiceDumpProto.SCALE, BATTERY_SCALE);
Yifan Hong932190b2017-10-11 11:00:51 -0700896 proto.write(BatteryServiceDumpProto.VOLTAGE, mHealthInfo.legacy.batteryVoltage);
897 proto.write(BatteryServiceDumpProto.TEMPERATURE, mHealthInfo.legacy.batteryTemperature);
898 proto.write(BatteryServiceDumpProto.TECHNOLOGY, mHealthInfo.legacy.batteryTechnology);
Netta Pe2a3cd82017-01-26 18:03:51 -0800899 }
900 proto.flush();
901 }
902
Jeff Browna4d82042012-10-02 19:11:19 -0700903 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800904 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700905
Jeff Browna4d82042012-10-02 19:11:19 -0700906 private final int mBatteryLowARGB;
907 private final int mBatteryMediumARGB;
908 private final int mBatteryFullARGB;
909 private final int mBatteryLedOn;
910 private final int mBatteryLedOff;
911
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800912 public Led(Context context, LightsManager lights) {
913 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700914
Jeff Browna4d82042012-10-02 19:11:19 -0700915 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700916 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700917 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700918 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700919 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700920 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700921 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700922 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700923 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700924 com.android.internal.R.integer.config_notificationsBatteryLedOff);
925 }
926
927 /**
928 * Synchronize on BatteryService.
929 */
Jeff Browna4d82042012-10-02 19:11:19 -0700930 public void updateLightsLocked() {
Yifan Hong932190b2017-10-11 11:00:51 -0700931 final int level = mHealthInfo.legacy.batteryLevel;
932 final int status = mHealthInfo.legacy.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700933 if (level < mLowBatteryWarningLevel) {
934 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
935 // Solid red when battery is charging
936 mBatteryLight.setColor(mBatteryLowARGB);
937 } else {
938 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800939 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700940 mBatteryLedOn, mBatteryLedOff);
941 }
942 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
943 || status == BatteryManager.BATTERY_STATUS_FULL) {
944 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
945 // Solid green when full or charging and nearly full
946 mBatteryLight.setColor(mBatteryFullARGB);
947 } else {
948 // Solid orange when charging and halfway full
949 mBatteryLight.setColor(mBatteryMediumARGB);
950 }
951 } else {
952 // No lights if not charging and not low
953 mBatteryLight.turnOff();
954 }
955 }
956 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700957
958 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700959 @Override public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800960 final long identity = Binder.clearCallingIdentity();
961 try {
962 BatteryService.this.update(props);
963 } finally {
964 Binder.restoreCallingIdentity(identity);
965 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700966 }
967 }
Jeff Brown21392762014-06-13 19:00:36 -0700968
969 private final class BinderService extends Binder {
Dianne Hackborn2e441072015-10-28 18:00:57 -0700970 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600971 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown21392762014-06-13 19:00:36 -0700972
Netta Pe2a3cd82017-01-26 18:03:51 -0800973 if (args.length > 0 && "--proto".equals(args[0])) {
974 dumpProto(fd);
975 } else {
976 dumpInternal(fd, pw, args);
977 }
Dianne Hackborn2e441072015-10-28 18:00:57 -0700978 }
979
980 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700981 FileDescriptor err, String[] args, ShellCallback callback,
982 ResultReceiver resultReceiver) {
983 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Jeff Brown21392762014-06-13 19:00:36 -0700984 }
985 }
986
987 private final class LocalService extends BatteryManagerInternal {
988 @Override
989 public boolean isPowered(int plugTypeSet) {
990 synchronized (mLock) {
991 return isPoweredLocked(plugTypeSet);
992 }
993 }
994
995 @Override
996 public int getPlugType() {
997 synchronized (mLock) {
998 return mPlugType;
999 }
1000 }
1001
1002 @Override
1003 public int getBatteryLevel() {
1004 synchronized (mLock) {
Yifan Hong932190b2017-10-11 11:00:51 -07001005 return mHealthInfo.legacy.batteryLevel;
Jeff Brown21392762014-06-13 19:00:36 -07001006 }
1007 }
1008
1009 @Override
1010 public boolean getBatteryLevelLow() {
1011 synchronized (mLock) {
1012 return mBatteryLevelLow;
1013 }
1014 }
1015
1016 @Override
1017 public int getInvalidCharger() {
1018 synchronized (mLock) {
1019 return mInvalidCharger;
1020 }
1021 }
1022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023}