blob: 2eeaec96b58187481c1f5dc4a8ada790bf923064 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.app.IBatteryStats;
23import com.android.server.am.BatteryStatsService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080024import com.android.server.lights.Light;
25import com.android.server.lights.LightsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27import android.app.ActivityManagerNative;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.os.BatteryManager;
Jeff Brown21392762014-06-13 19:00:36 -070033import android.os.BatteryManagerInternal;
Todd Poynor26faecc2013-05-22 18:54:48 -070034import android.os.BatteryProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Binder;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070036import android.os.FileUtils;
Jeff Brown605ea692012-10-05 16:33:10 -070037import android.os.Handler;
Todd Poynor26faecc2013-05-22 18:54:48 -070038import android.os.IBatteryPropertiesListener;
39import android.os.IBatteryPropertiesRegistrar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.IBinder;
Dan Egnor18e93962010-02-10 19:27:58 -080041import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.SystemClock;
45import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070046import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.provider.Settings;
48import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080049import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51import java.io.File;
52import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import java.io.FileOutputStream;
54import java.io.IOException;
55import java.io.PrintWriter;
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58/**
59 * <p>BatteryService monitors the charging status, and charge level of the device
60 * battery. When these values change this service broadcasts the new values
61 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
62 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
63 * BATTERY_CHANGED} action.</p>
64 * <p>The new values are stored in the Intent data and can be retrieved by
65 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
66 * following keys:</p>
67 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
68 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
69 * <p>&quot;status&quot; - String, the current charging status.<br />
70 * <p>&quot;health&quot; - String, the current battery health.<br />
71 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
72 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
73 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
74 * into an AC power adapter; 2 if plugged in via USB.</p>
75 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
76 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
77 * a degree Centigrade</p>
78 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
Jeff Brown605ea692012-10-05 16:33:10 -070079 *
80 * <p>
81 * The battery service may be called by the power manager while holding its locks so
82 * we take care to post all outcalls into the activity manager to a handler.
83 *
84 * FIXME: Ideally the power manager would perform all of its calls into the battery
85 * service asynchronously itself.
86 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
Jeff Brown21392762014-06-13 19:00:36 -070088public final class BatteryService extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private static final String TAG = BatteryService.class.getSimpleName();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080090
Jeff Browna4d82042012-10-02 19:11:19 -070091 private static final boolean DEBUG = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080092
Jeff Browna4d82042012-10-02 19:11:19 -070093 private static final int BATTERY_SCALE = 100; // battery capacity is a percentage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 // Used locally for determining when to make a last ditch effort to log
96 // discharge stats before the device dies.
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070097 private int mCriticalBatteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 private static final int DUMP_MAX_LENGTH = 24 * 1024;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700100 private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" };
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private static final String DUMPSYS_DATA_PATH = "/data/system/";
103
104 // This should probably be exposed in the API, though it's not critical
105 private static final int BATTERY_PLUGGED_NONE = 0;
106
107 private final Context mContext;
108 private final IBatteryStats mBatteryStats;
Jeff Brown605ea692012-10-05 16:33:10 -0700109 private final Handler mHandler;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
Jeff Browna4d82042012-10-02 19:11:19 -0700111 private final Object mLock = new Object();
112
Todd Poynor26faecc2013-05-22 18:54:48 -0700113 private BatteryProperties mBatteryProps;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800114 private final BatteryProperties mLastBatteryProps = new BatteryProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private boolean mBatteryLevelCritical;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 private int mLastBatteryStatus;
117 private int mLastBatteryHealth;
118 private boolean mLastBatteryPresent;
119 private int mLastBatteryLevel;
120 private int mLastBatteryVoltage;
121 private int mLastBatteryTemperature;
122 private boolean mLastBatteryLevelCritical;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700123 private int mLastMaxChargingCurrent;
Jeff Browna4d82042012-10-02 19:11:19 -0700124
125 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700126 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400127
128 private int mLowBatteryWarningLevel;
129 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700130 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 private int mPlugType;
133 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700135 private boolean mBatteryLevelLow;
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private long mDischargeStartTime;
138 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700140 private boolean mUpdatesStopped;
141
Joe Onoratode1b3592010-10-25 20:36:47 -0700142 private Led mLed;
143
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700144 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
Jeff Brown21392762014-06-13 19:00:36 -0700146 public BatteryService(Context context) {
147 super(context);
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700150 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700151 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 mBatteryStats = BatteryStatsService.getService();
153
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700154 mCriticalBatteryLevel = mContext.getResources().getInteger(
155 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400156 mLowBatteryWarningLevel = mContext.getResources().getInteger(
157 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700158 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
159 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700160 mShutdownBatteryTemperature = mContext.getResources().getInteger(
161 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400162
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400163 // watch for invalid charger messages if the invalid_charger switch exists
164 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700165 mInvalidChargerObserver.startObserving(
166 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400167 }
Jeff Brown21392762014-06-13 19:00:36 -0700168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Jeff Brown21392762014-06-13 19:00:36 -0700170 @Override
171 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700172 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800173 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
174 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700175 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800176 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700177 } catch (RemoteException e) {
178 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700179 }
Jeff Brown21392762014-06-13 19:00:36 -0700180
181 publishBinderService("battery", new BinderService());
182 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 }
184
Jeff Brown21392762014-06-13 19:00:36 -0700185 @Override
186 public void onBootPhase(int phase) {
187 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
188 // check our power situation now that it is safe to display the shutdown dialog.
189 synchronized (mLock) {
190 ContentObserver obs = new ContentObserver(mHandler) {
191 @Override
192 public void onChange(boolean selfChange) {
193 synchronized (mLock) {
194 updateBatteryWarningLevelLocked();
195 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700196 }
Jeff Brown21392762014-06-13 19:00:36 -0700197 };
198 final ContentResolver resolver = mContext.getContentResolver();
199 resolver.registerContentObserver(Settings.Global.getUriFor(
200 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
201 false, obs, UserHandle.USER_ALL);
202 updateBatteryWarningLevelLocked();
203 }
Jeff Browna4d82042012-10-02 19:11:19 -0700204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206
Jeff Brown21392762014-06-13 19:00:36 -0700207 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700208 final ContentResolver resolver = mContext.getContentResolver();
209 int defWarnLevel = mContext.getResources().getInteger(
210 com.android.internal.R.integer.config_lowBatteryWarningLevel);
211 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
212 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
213 if (mLowBatteryWarningLevel == 0) {
214 mLowBatteryWarningLevel = defWarnLevel;
215 }
216 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
217 mLowBatteryWarningLevel = mCriticalBatteryLevel;
218 }
219 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
220 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
221 processValuesLocked(true);
222 }
223
Jeff Browna4d82042012-10-02 19:11:19 -0700224 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 // assume we are powered if battery state is unknown so
226 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700227 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 return true;
229 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700230 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700231 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700233 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700234 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700236 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700237 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
Jeff Browna4d82042012-10-02 19:11:19 -0700239 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241
Jeff Brown21392762014-06-13 19:00:36 -0700242 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700243 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
244 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
245
246 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
247 * - is just un-plugged (previously was plugged) and battery level is
248 * less than or equal to WARNING, or
249 * - is not plugged and battery level falls to WARNING boundary
250 * (becomes <= mLowBatteryWarningLevel).
251 */
252 return !plugged
253 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
254 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
255 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
256 }
257
Jeff Browna4d82042012-10-02 19:11:19 -0700258 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400259 // shut down gracefully if our battery is critically low and we are not powered.
260 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700261 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700262 mHandler.post(new Runnable() {
263 @Override
264 public void run() {
265 if (ActivityManagerNative.isSystemReady()) {
266 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
267 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
268 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
269 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
270 }
271 }
272 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400273 }
274 }
275
Jeff Browna4d82042012-10-02 19:11:19 -0700276 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700277 // shut down gracefully if temperature is too high (> 68.0C by default)
278 // wait until the system has booted before attempting to display the
279 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700280 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700281 mHandler.post(new Runnable() {
282 @Override
283 public void run() {
284 if (ActivityManagerNative.isSystemReady()) {
285 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
286 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
287 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
288 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
289 }
290 }
291 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700292 }
293 }
294
Todd Poynor26faecc2013-05-22 18:54:48 -0700295 private void update(BatteryProperties props) {
296 synchronized (mLock) {
297 if (!mUpdatesStopped) {
298 mBatteryProps = props;
299 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700300 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800301 } else {
302 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700303 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700304 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
Dianne Hackborn14272302014-06-10 23:13:02 -0700307 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700308 boolean logOutlier = false;
309 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700310
Todd Poynor26faecc2013-05-22 18:54:48 -0700311 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
312 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700314 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700316 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700317 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 } else {
319 mPlugType = BATTERY_PLUGGED_NONE;
320 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700321
Jeff Browna4d82042012-10-02 19:11:19 -0700322 if (DEBUG) {
323 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700324 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
325 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
326 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
Adrian Roos7b043112015-07-10 13:00:33 -0700327 + ", maxChargingCurrent" + mBatteryProps.maxChargingCurrent
Todd Poynor26faecc2013-05-22 18:54:48 -0700328 + ", batteryStatus=" + mBatteryProps.batteryStatus
329 + ", batteryHealth=" + mBatteryProps.batteryHealth
330 + ", batteryPresent=" + mBatteryProps.batteryPresent
331 + ", batteryLevel=" + mBatteryProps.batteryLevel
332 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
333 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
334 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700335 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
336 + ", mPlugType=" + mPlugType);
337 }
338
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700339 // Let the battery stats keep track of the current level.
340 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700341 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
342 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
343 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700344 } catch (RemoteException e) {
345 // Should never happen.
346 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700347
Jeff Browna4d82042012-10-02 19:11:19 -0700348 shutdownIfNoPowerLocked();
349 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700350
Dianne Hackborn14272302014-06-10 23:13:02 -0700351 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700352 mBatteryProps.batteryHealth != mLastBatteryHealth ||
353 mBatteryProps.batteryPresent != mLastBatteryPresent ||
354 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700356 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
357 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Adrian Roos76dc5a52015-07-21 16:20:36 -0700358 mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700359 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 if (mPlugType != mLastPlugType) {
362 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
363 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 // There's no value in this data unless we've discharged at least once and the
366 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700367 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700368 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
369 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700371 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
376 // charging -> discharging or we just powered up
377 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700378 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
380 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700381 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
382 mBatteryProps.batteryHealth != mLastBatteryHealth ||
383 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700386 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
387 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700389 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700390 // Don't do this just from voltage or temperature changes, that is
391 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700393 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
396 mPlugType == BATTERY_PLUGGED_NONE) {
397 // We want to make sure we log discharge cycle outliers
398 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700399 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
400 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
Dianne Hackborn14272302014-06-10 23:13:02 -0700403 if (!mBatteryLevelLow) {
404 // Should we now switch in to low battery mode?
405 if (mPlugType == BATTERY_PLUGGED_NONE
406 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
407 mBatteryLevelLow = true;
408 }
409 } else {
410 // Should we now switch out of low battery mode?
411 if (mPlugType != BATTERY_PLUGGED_NONE) {
412 mBatteryLevelLow = false;
413 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
414 mBatteryLevelLow = false;
415 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
416 // If being forced, the previous state doesn't matter, we will just
417 // absolutely check to see if we are now above the warning level.
418 mBatteryLevelLow = false;
419 }
420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
Jeff Browna4d82042012-10-02 19:11:19 -0700422 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
Christopher Tate06ba5542009-04-09 16:03:56 -0700424 // Separate broadcast is sent for power connected / not connected
425 // since the standard intent will not wake any applications and some
426 // applications may want to have smart behavior based on this.
427 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700428 mHandler.post(new Runnable() {
429 @Override
430 public void run() {
431 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
432 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
433 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
434 }
435 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700436 }
437 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700438 mHandler.post(new Runnable() {
439 @Override
440 public void run() {
441 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
442 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
443 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
444 }
445 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700446 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200447
Dianne Hackborn14272302014-06-10 23:13:02 -0700448 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700449 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700450 mHandler.post(new Runnable() {
451 @Override
452 public void run() {
453 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
454 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
455 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
456 }
457 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400458 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700459 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700460 mHandler.post(new Runnable() {
461 @Override
462 public void run() {
463 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
464 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
465 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
466 }
467 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
Joe Onoratode1b3592010-10-25 20:36:47 -0700470 // Update the battery LED
471 mLed.updateLightsLocked();
472
The Android Open Source Project10592532009-03-18 17:39:46 -0700473 // This needs to be done after sendIntent() so that we get the lastest battery stats.
474 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700475 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
Todd Poynor26faecc2013-05-22 18:54:48 -0700478 mLastBatteryStatus = mBatteryProps.batteryStatus;
479 mLastBatteryHealth = mBatteryProps.batteryHealth;
480 mLastBatteryPresent = mBatteryProps.batteryPresent;
481 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700482 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700483 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
484 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Adrian Roos76dc5a52015-07-21 16:20:36 -0700485 mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700486 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400487 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489 }
490
Jeff Browna4d82042012-10-02 19:11:19 -0700491 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700493 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800494 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
495 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
Todd Poynor26faecc2013-05-22 18:54:48 -0700497 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
Todd Poynor26faecc2013-05-22 18:54:48 -0700499 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
500 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
501 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
502 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700503 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
504 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
505 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700506 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
507 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
508 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400509 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
Adrian Roos7b043112015-07-10 13:00:33 -0700510 intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
Jeff Browna4d82042012-10-02 19:11:19 -0700512 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700513 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
514 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
Adrian Roos7b043112015-07-10 13:00:33 -0700515 ", health:" + mBatteryProps.batteryHealth +
516 ", present:" + mBatteryProps.batteryPresent +
Todd Poynor26faecc2013-05-22 18:54:48 -0700517 ", voltage: " + mBatteryProps.batteryVoltage +
518 ", temperature: " + mBatteryProps.batteryTemperature +
519 ", technology: " + mBatteryProps.batteryTechnology +
Adrian Roos7b043112015-07-10 13:00:33 -0700520 ", AC powered:" + mBatteryProps.chargerAcOnline +
521 ", USB powered:" + mBatteryProps.chargerUsbOnline +
Todd Poynor26faecc2013-05-22 18:54:48 -0700522 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Adrian Roos7b043112015-07-10 13:00:33 -0700523 ", icon:" + icon + ", invalid charger:" + mInvalidCharger +
524 ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526
Jeff Brown605ea692012-10-05 16:33:10 -0700527 mHandler.post(new Runnable() {
528 @Override
529 public void run() {
530 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
531 }
532 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
534
Jeff Browna4d82042012-10-02 19:11:19 -0700535 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700536 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800537 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
Dan Egnor18e93962010-02-10 19:27:58 -0800539 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
540 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
541
542 File dumpFile = null;
543 FileOutputStream dumpStream = null;
544 try {
545 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700546 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800547 dumpStream = new FileOutputStream(dumpFile);
548 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700549 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800550
551 // add dump file to drop box
552 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
553 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800554 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800555 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800556 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800557 } finally {
558 // make sure we clean up
559 if (dumpStream != null) {
560 try {
561 dumpStream.close();
562 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800563 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
Dan Egnor18e93962010-02-10 19:27:58 -0800565 }
566 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800567 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800568 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
Jeff Browna4d82042012-10-02 19:11:19 -0700573 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700575 String dischargeThresholdString = Settings.Global.getString(cr,
576 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
577 String durationThresholdString = Settings.Global.getString(cr,
578 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (dischargeThresholdString != null && durationThresholdString != null) {
581 try {
582 long durationThreshold = Long.parseLong(durationThresholdString);
583 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700585 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700587 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
Jeff Browna4d82042012-10-02 19:11:19 -0700589 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700591 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700592 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800594 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 durationThresholdString + " or " + dischargeThresholdString);
596 return;
597 }
598 }
599 }
600
Jeff Browna4d82042012-10-02 19:11:19 -0700601 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700602 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700604 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700606 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
607 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700608 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700609 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800610 return com.android.internal.R.drawable.stat_sys_battery_charge;
611 } else {
612 return com.android.internal.R.drawable.stat_sys_battery;
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 } else {
615 return com.android.internal.R.drawable.stat_sys_battery_unknown;
616 }
617 }
618
Jeff Brown21392762014-06-13 19:00:36 -0700619 private void dumpInternal(PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700620 synchronized (mLock) {
621 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700622 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700623 if (mUpdatesStopped) {
624 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
625 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700626 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
627 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
628 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
Adrian Roos7b043112015-07-10 13:00:33 -0700629 pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent);
Todd Poynor26faecc2013-05-22 18:54:48 -0700630 pw.println(" status: " + mBatteryProps.batteryStatus);
631 pw.println(" health: " + mBatteryProps.batteryHealth);
632 pw.println(" present: " + mBatteryProps.batteryPresent);
633 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700634 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700635 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700636 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
637 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800638
639 } else if ("unplug".equals(args[0])) {
640 if (!mUpdatesStopped) {
641 mLastBatteryProps.set(mBatteryProps);
642 }
643 mBatteryProps.chargerAcOnline = false;
644 mBatteryProps.chargerUsbOnline = false;
645 mBatteryProps.chargerWirelessOnline = false;
646 long ident = Binder.clearCallingIdentity();
647 try {
648 mUpdatesStopped = true;
649 processValuesLocked(false);
650 } finally {
651 Binder.restoreCallingIdentity(ident);
652 }
653
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700654 } else if (args.length == 3 && "set".equals(args[0])) {
655 String key = args[1];
656 String value = args[2];
657 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800658 if (!mUpdatesStopped) {
659 mLastBatteryProps.set(mBatteryProps);
660 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700661 boolean update = true;
662 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700663 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700664 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700665 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700666 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700667 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700668 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700669 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700670 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700671 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700672 } else if ("invalid".equals(key)) {
673 mInvalidCharger = Integer.parseInt(value);
674 } else {
675 pw.println("Unknown set option: " + key);
676 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700677 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700678 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700679 long ident = Binder.clearCallingIdentity();
680 try {
681 mUpdatesStopped = true;
Dianne Hackborn14272302014-06-10 23:13:02 -0700682 processValuesLocked(false);
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700683 } finally {
684 Binder.restoreCallingIdentity(ident);
685 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700686 }
687 } catch (NumberFormatException ex) {
688 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700689 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800690
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700691 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700692 long ident = Binder.clearCallingIdentity();
693 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800694 if (mUpdatesStopped) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800695 mUpdatesStopped = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800696 mBatteryProps.set(mLastBatteryProps);
Dianne Hackborn14272302014-06-10 23:13:02 -0700697 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800698 }
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700699 } finally {
700 Binder.restoreCallingIdentity(ident);
701 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700702 } else {
703 pw.println("Dump current battery state, or:");
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800704 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800705 pw.println(" unplug");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700706 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700710
Jeff Browna4d82042012-10-02 19:11:19 -0700711 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
712 @Override
713 public void onUEvent(UEventObserver.UEvent event) {
714 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
715 synchronized (mLock) {
716 if (mInvalidCharger != invalidCharger) {
717 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700718 }
719 }
720 }
721 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700722
Jeff Browna4d82042012-10-02 19:11:19 -0700723 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800724 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700725
Jeff Browna4d82042012-10-02 19:11:19 -0700726 private final int mBatteryLowARGB;
727 private final int mBatteryMediumARGB;
728 private final int mBatteryFullARGB;
729 private final int mBatteryLedOn;
730 private final int mBatteryLedOff;
731
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800732 public Led(Context context, LightsManager lights) {
733 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700734
Jeff Browna4d82042012-10-02 19:11:19 -0700735 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700736 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700737 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700738 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700739 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700740 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700741 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700742 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700743 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700744 com.android.internal.R.integer.config_notificationsBatteryLedOff);
745 }
746
747 /**
748 * Synchronize on BatteryService.
749 */
Jeff Browna4d82042012-10-02 19:11:19 -0700750 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700751 final int level = mBatteryProps.batteryLevel;
752 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700753 if (level < mLowBatteryWarningLevel) {
754 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
755 // Solid red when battery is charging
756 mBatteryLight.setColor(mBatteryLowARGB);
757 } else {
758 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800759 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700760 mBatteryLedOn, mBatteryLedOff);
761 }
762 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
763 || status == BatteryManager.BATTERY_STATUS_FULL) {
764 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
765 // Solid green when full or charging and nearly full
766 mBatteryLight.setColor(mBatteryFullARGB);
767 } else {
768 // Solid orange when charging and halfway full
769 mBatteryLight.setColor(mBatteryMediumARGB);
770 }
771 } else {
772 // No lights if not charging and not low
773 mBatteryLight.turnOff();
774 }
775 }
776 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700777
778 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800779 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700780 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800781 final long identity = Binder.clearCallingIdentity();
782 try {
783 BatteryService.this.update(props);
784 } finally {
785 Binder.restoreCallingIdentity(identity);
786 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700787 }
788 }
Jeff Brown21392762014-06-13 19:00:36 -0700789
790 private final class BinderService extends Binder {
791 @Override
792 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
793 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
794 != PackageManager.PERMISSION_GRANTED) {
795
796 pw.println("Permission Denial: can't dump Battery service from from pid="
797 + Binder.getCallingPid()
798 + ", uid=" + Binder.getCallingUid());
799 return;
800 }
801
802 dumpInternal(pw, args);
803 }
804 }
805
806 private final class LocalService extends BatteryManagerInternal {
807 @Override
808 public boolean isPowered(int plugTypeSet) {
809 synchronized (mLock) {
810 return isPoweredLocked(plugTypeSet);
811 }
812 }
813
814 @Override
815 public int getPlugType() {
816 synchronized (mLock) {
817 return mPlugType;
818 }
819 }
820
821 @Override
822 public int getBatteryLevel() {
823 synchronized (mLock) {
824 return mBatteryProps.batteryLevel;
825 }
826 }
827
828 @Override
829 public boolean getBatteryLevelLow() {
830 synchronized (mLock) {
831 return mBatteryLevelLow;
832 }
833 }
834
835 @Override
836 public int getInvalidCharger() {
837 synchronized (mLock) {
838 return mInvalidCharger;
839 }
840 }
841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842}