blob: 912a181ef944c297bb795db0f3bf5cb7204fa31d [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;
Jeff Browna4d82042012-10-02 19:11:19 -0700123
124 private int mInvalidCharger;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700125 private int mLastInvalidCharger;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400126
127 private int mLowBatteryWarningLevel;
128 private int mLowBatteryCloseWarningLevel;
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700129 private int mShutdownBatteryTemperature;
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 private int mPlugType;
132 private int mLastPlugType = -1; // Extra state so we can detect first run
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700134 private boolean mBatteryLevelLow;
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 private long mDischargeStartTime;
137 private int mDischargeStartLevel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700139 private boolean mUpdatesStopped;
140
Joe Onoratode1b3592010-10-25 20:36:47 -0700141 private Led mLed;
142
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700143 private boolean mSentLowBatteryBroadcast = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
Jeff Brown21392762014-06-13 19:00:36 -0700145 public BatteryService(Context context) {
146 super(context);
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 mContext = context;
Jeff Brown605ea692012-10-05 16:33:10 -0700149 mHandler = new Handler(true /*async*/);
Jeff Brown21392762014-06-13 19:00:36 -0700150 mLed = new Led(context, getLocalService(LightsManager.class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 mBatteryStats = BatteryStatsService.getService();
152
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700153 mCriticalBatteryLevel = mContext.getResources().getInteger(
154 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400155 mLowBatteryWarningLevel = mContext.getResources().getInteger(
156 com.android.internal.R.integer.config_lowBatteryWarningLevel);
Dianne Hackborn14272302014-06-10 23:13:02 -0700157 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
158 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700159 mShutdownBatteryTemperature = mContext.getResources().getInteger(
160 com.android.internal.R.integer.config_shutdownBatteryTemperature);
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400161
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400162 // watch for invalid charger messages if the invalid_charger switch exists
163 if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) {
Jeff Browna4d82042012-10-02 19:11:19 -0700164 mInvalidChargerObserver.startObserving(
165 "DEVPATH=/devices/virtual/switch/invalid_charger");
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400166 }
Jeff Brown21392762014-06-13 19:00:36 -0700167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Jeff Brown21392762014-06-13 19:00:36 -0700169 @Override
170 public void onStart() {
Todd Poynor0edc5b52013-10-22 17:53:13 -0700171 IBinder b = ServiceManager.getService("batteryproperties");
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800172 final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
173 IBatteryPropertiesRegistrar.Stub.asInterface(b);
Todd Poynor26faecc2013-05-22 18:54:48 -0700174 try {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800175 batteryPropertiesRegistrar.registerListener(new BatteryListener());
Todd Poynor26faecc2013-05-22 18:54:48 -0700176 } catch (RemoteException e) {
177 // Should never happen.
Jeff Browna4d82042012-10-02 19:11:19 -0700178 }
Jeff Brown21392762014-06-13 19:00:36 -0700179
180 publishBinderService("battery", new BinderService());
181 publishLocalService(BatteryManagerInternal.class, new LocalService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
183
Jeff Brown21392762014-06-13 19:00:36 -0700184 @Override
185 public void onBootPhase(int phase) {
186 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
187 // check our power situation now that it is safe to display the shutdown dialog.
188 synchronized (mLock) {
189 ContentObserver obs = new ContentObserver(mHandler) {
190 @Override
191 public void onChange(boolean selfChange) {
192 synchronized (mLock) {
193 updateBatteryWarningLevelLocked();
194 }
Dianne Hackborn14272302014-06-10 23:13:02 -0700195 }
Jeff Brown21392762014-06-13 19:00:36 -0700196 };
197 final ContentResolver resolver = mContext.getContentResolver();
198 resolver.registerContentObserver(Settings.Global.getUriFor(
199 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
200 false, obs, UserHandle.USER_ALL);
201 updateBatteryWarningLevelLocked();
202 }
Jeff Browna4d82042012-10-02 19:11:19 -0700203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205
Jeff Brown21392762014-06-13 19:00:36 -0700206 private void updateBatteryWarningLevelLocked() {
Dianne Hackborn14272302014-06-10 23:13:02 -0700207 final ContentResolver resolver = mContext.getContentResolver();
208 int defWarnLevel = mContext.getResources().getInteger(
209 com.android.internal.R.integer.config_lowBatteryWarningLevel);
210 mLowBatteryWarningLevel = Settings.Global.getInt(resolver,
211 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel);
212 if (mLowBatteryWarningLevel == 0) {
213 mLowBatteryWarningLevel = defWarnLevel;
214 }
215 if (mLowBatteryWarningLevel < mCriticalBatteryLevel) {
216 mLowBatteryWarningLevel = mCriticalBatteryLevel;
217 }
218 mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
219 com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
220 processValuesLocked(true);
221 }
222
Jeff Browna4d82042012-10-02 19:11:19 -0700223 private boolean isPoweredLocked(int plugTypeSet) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 // assume we are powered if battery state is unknown so
225 // the "stay on while plugged in" option will work.
Todd Poynor26faecc2013-05-22 18:54:48 -0700226 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 return true;
228 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700229 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700230 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700232 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700233 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700235 if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) {
Jeff Browna4d82042012-10-02 19:11:19 -0700236 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
Jeff Browna4d82042012-10-02 19:11:19 -0700238 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
240
Jeff Brown21392762014-06-13 19:00:36 -0700241 private boolean shouldSendBatteryLowLocked() {
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700242 final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;
243 final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;
244
245 /* The ACTION_BATTERY_LOW broadcast is sent in these situations:
246 * - is just un-plugged (previously was plugged) and battery level is
247 * less than or equal to WARNING, or
248 * - is not plugged and battery level falls to WARNING boundary
249 * (becomes <= mLowBatteryWarningLevel).
250 */
251 return !plugged
252 && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
253 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel
254 && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);
255 }
256
Jeff Browna4d82042012-10-02 19:11:19 -0700257 private void shutdownIfNoPowerLocked() {
Mike Lockwood07a500f2009-08-12 09:56:44 -0400258 // shut down gracefully if our battery is critically low and we are not powered.
259 // wait until the system has booted before attempting to display the shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700260 if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
Jeff Brown605ea692012-10-05 16:33:10 -0700261 mHandler.post(new Runnable() {
262 @Override
263 public void run() {
264 if (ActivityManagerNative.isSystemReady()) {
265 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
266 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
267 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
268 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
269 }
270 }
271 });
Mike Lockwood07a500f2009-08-12 09:56:44 -0400272 }
273 }
274
Jeff Browna4d82042012-10-02 19:11:19 -0700275 private void shutdownIfOverTempLocked() {
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700276 // shut down gracefully if temperature is too high (> 68.0C by default)
277 // wait until the system has booted before attempting to display the
278 // shutdown dialog.
Todd Poynor26faecc2013-05-22 18:54:48 -0700279 if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) {
Jeff Brown605ea692012-10-05 16:33:10 -0700280 mHandler.post(new Runnable() {
281 @Override
282 public void run() {
283 if (ActivityManagerNative.isSystemReady()) {
284 Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
285 intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
286 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
287 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
288 }
289 }
290 });
Eric Olsen6a362a92010-03-26 15:38:41 -0700291 }
292 }
293
Todd Poynor26faecc2013-05-22 18:54:48 -0700294 private void update(BatteryProperties props) {
295 synchronized (mLock) {
296 if (!mUpdatesStopped) {
297 mBatteryProps = props;
298 // Process the new values.
Dianne Hackborn14272302014-06-10 23:13:02 -0700299 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800300 } else {
301 mLastBatteryProps.set(props);
Todd Poynor26faecc2013-05-22 18:54:48 -0700302 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700303 }
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305
Dianne Hackborn14272302014-06-10 23:13:02 -0700306 private void processValuesLocked(boolean force) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700307 boolean logOutlier = false;
308 long dischargeDuration = 0;
Joe Onoratoa7e4cf9b2009-07-28 18:18:20 -0700309
Todd Poynor26faecc2013-05-22 18:54:48 -0700310 mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
311 if (mBatteryProps.chargerAcOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
Todd Poynor26faecc2013-05-22 18:54:48 -0700313 } else if (mBatteryProps.chargerUsbOnline) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
Todd Poynor26faecc2013-05-22 18:54:48 -0700315 } else if (mBatteryProps.chargerWirelessOnline) {
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700316 mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 } else {
318 mPlugType = BATTERY_PLUGGED_NONE;
319 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700320
Jeff Browna4d82042012-10-02 19:11:19 -0700321 if (DEBUG) {
322 Slog.d(TAG, "Processing new values: "
Todd Poynor26faecc2013-05-22 18:54:48 -0700323 + "chargerAcOnline=" + mBatteryProps.chargerAcOnline
324 + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline
325 + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline
326 + ", batteryStatus=" + mBatteryProps.batteryStatus
327 + ", batteryHealth=" + mBatteryProps.batteryHealth
328 + ", batteryPresent=" + mBatteryProps.batteryPresent
329 + ", batteryLevel=" + mBatteryProps.batteryLevel
330 + ", batteryTechnology=" + mBatteryProps.batteryTechnology
331 + ", batteryVoltage=" + mBatteryProps.batteryVoltage
332 + ", batteryTemperature=" + mBatteryProps.batteryTemperature
Jeff Browna4d82042012-10-02 19:11:19 -0700333 + ", mBatteryLevelCritical=" + mBatteryLevelCritical
334 + ", mPlugType=" + mPlugType);
335 }
336
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700337 // Let the battery stats keep track of the current level.
338 try {
Todd Poynor26faecc2013-05-22 18:54:48 -0700339 mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
340 mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
341 mBatteryProps.batteryVoltage);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700342 } catch (RemoteException e) {
343 // Should never happen.
344 }
Brian Muramatsuf3c74f32012-08-31 15:14:48 -0700345
Jeff Browna4d82042012-10-02 19:11:19 -0700346 shutdownIfNoPowerLocked();
347 shutdownIfOverTempLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700348
Dianne Hackborn14272302014-06-10 23:13:02 -0700349 if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700350 mBatteryProps.batteryHealth != mLastBatteryHealth ||
351 mBatteryProps.batteryPresent != mLastBatteryPresent ||
352 mBatteryProps.batteryLevel != mLastBatteryLevel ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 mPlugType != mLastPlugType ||
Todd Poynor26faecc2013-05-22 18:54:48 -0700354 mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
355 mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
Dianne Hackborn14272302014-06-10 23:13:02 -0700356 mInvalidCharger != mLastInvalidCharger)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 if (mPlugType != mLastPlugType) {
359 if (mLastPlugType == BATTERY_PLUGGED_NONE) {
360 // discharging -> charging
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 // There's no value in this data unless we've discharged at least once and the
363 // battery level has changed; so don't log until it does.
Todd Poynor26faecc2013-05-22 18:54:48 -0700364 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700365 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
366 logOutlier = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367 EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
Todd Poynor26faecc2013-05-22 18:54:48 -0700368 mDischargeStartLevel, mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 // make sure we see a discharge event before logging again
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370 mDischargeStartTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
372 } else if (mPlugType == BATTERY_PLUGGED_NONE) {
373 // charging -> discharging or we just powered up
374 mDischargeStartTime = SystemClock.elapsedRealtime();
Todd Poynor26faecc2013-05-22 18:54:48 -0700375 mDischargeStartLevel = mBatteryProps.batteryLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700378 if (mBatteryProps.batteryStatus != mLastBatteryStatus ||
379 mBatteryProps.batteryHealth != mLastBatteryHealth ||
380 mBatteryProps.batteryPresent != mLastBatteryPresent ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 mPlugType != mLastPlugType) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382 EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
Todd Poynor26faecc2013-05-22 18:54:48 -0700383 mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0,
384 mPlugType, mBatteryProps.batteryTechnology);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700386 if (mBatteryProps.batteryLevel != mLastBatteryLevel) {
Dianne Hackborncf1171642013-07-12 17:26:02 -0700387 // Don't do this just from voltage or temperature changes, that is
388 // too noisy.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389 EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
Todd Poynor26faecc2013-05-22 18:54:48 -0700390 mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392 if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
393 mPlugType == BATTERY_PLUGGED_NONE) {
394 // We want to make sure we log discharge cycle outliers
395 // if the battery is about to die.
The Android Open Source Project10592532009-03-18 17:39:46 -0700396 dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime;
397 logOutlier = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
Dianne Hackborn14272302014-06-10 23:13:02 -0700400 if (!mBatteryLevelLow) {
401 // Should we now switch in to low battery mode?
402 if (mPlugType == BATTERY_PLUGGED_NONE
403 && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) {
404 mBatteryLevelLow = true;
405 }
406 } else {
407 // Should we now switch out of low battery mode?
408 if (mPlugType != BATTERY_PLUGGED_NONE) {
409 mBatteryLevelLow = false;
410 } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) {
411 mBatteryLevelLow = false;
412 } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) {
413 // If being forced, the previous state doesn't matter, we will just
414 // absolutely check to see if we are now above the warning level.
415 mBatteryLevelLow = false;
416 }
417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
Jeff Browna4d82042012-10-02 19:11:19 -0700419 sendIntentLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
Christopher Tate06ba5542009-04-09 16:03:56 -0700421 // Separate broadcast is sent for power connected / not connected
422 // since the standard intent will not wake any applications and some
423 // applications may want to have smart behavior based on this.
424 if (mPlugType != 0 && mLastPlugType == 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700425 mHandler.post(new Runnable() {
426 @Override
427 public void run() {
428 Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
429 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
430 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
431 }
432 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700433 }
434 else if (mPlugType == 0 && mLastPlugType != 0) {
Jeff Brown605ea692012-10-05 16:33:10 -0700435 mHandler.post(new Runnable() {
436 @Override
437 public void run() {
438 Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED);
439 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
440 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
441 }
442 });
Christopher Tate06ba5542009-04-09 16:03:56 -0700443 }
Mihai Predaa82842f2009-04-29 15:05:56 +0200444
Dianne Hackborn14272302014-06-10 23:13:02 -0700445 if (shouldSendBatteryLowLocked()) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700446 mSentLowBatteryBroadcast = true;
Jeff Brown605ea692012-10-05 16:33:10 -0700447 mHandler.post(new Runnable() {
448 @Override
449 public void run() {
450 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);
451 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
452 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
453 }
454 });
Mike Lockwoodd81b1f42009-09-25 09:32:19 -0400455 } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) {
Dianne Hackborn8ec5b832009-07-01 21:19:35 -0700456 mSentLowBatteryBroadcast = false;
Jeff Brown605ea692012-10-05 16:33:10 -0700457 mHandler.post(new Runnable() {
458 @Override
459 public void run() {
460 Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY);
461 statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
462 mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);
463 }
464 });
Mihai Predaa82842f2009-04-29 15:05:56 +0200465 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
Joe Onoratode1b3592010-10-25 20:36:47 -0700467 // Update the battery LED
468 mLed.updateLightsLocked();
469
The Android Open Source Project10592532009-03-18 17:39:46 -0700470 // This needs to be done after sendIntent() so that we get the lastest battery stats.
471 if (logOutlier && dischargeDuration != 0) {
Jeff Browna4d82042012-10-02 19:11:19 -0700472 logOutlierLocked(dischargeDuration);
The Android Open Source Project10592532009-03-18 17:39:46 -0700473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
Todd Poynor26faecc2013-05-22 18:54:48 -0700475 mLastBatteryStatus = mBatteryProps.batteryStatus;
476 mLastBatteryHealth = mBatteryProps.batteryHealth;
477 mLastBatteryPresent = mBatteryProps.batteryPresent;
478 mLastBatteryLevel = mBatteryProps.batteryLevel;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700479 mLastPlugType = mPlugType;
Todd Poynor26faecc2013-05-22 18:54:48 -0700480 mLastBatteryVoltage = mBatteryProps.batteryVoltage;
481 mLastBatteryTemperature = mBatteryProps.batteryTemperature;
Dianne Hackborn99f7eb452009-09-22 17:27:53 -0700482 mLastBatteryLevelCritical = mBatteryLevelCritical;
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400483 mLastInvalidCharger = mInvalidCharger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485 }
486
Jeff Browna4d82042012-10-02 19:11:19 -0700487 private void sendIntentLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 // Pack up the values and broadcast them to everyone
Jeff Brown605ea692012-10-05 16:33:10 -0700489 final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800490 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
491 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
Todd Poynor26faecc2013-05-22 18:54:48 -0700493 int icon = getIconLocked(mBatteryProps.batteryLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
Todd Poynor26faecc2013-05-22 18:54:48 -0700495 intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
496 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
497 intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
498 intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
Dianne Hackbornedd93162009-09-19 14:03:05 -0700499 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
500 intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
501 intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
Todd Poynor26faecc2013-05-22 18:54:48 -0700502 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
503 intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
504 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400505 intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
Jeff Browna4d82042012-10-02 19:11:19 -0700507 if (DEBUG) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700508 Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
509 ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus +
510 ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
511 ", voltage: " + mBatteryProps.batteryVoltage +
512 ", temperature: " + mBatteryProps.batteryTemperature +
513 ", technology: " + mBatteryProps.batteryTechnology +
514 ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
515 ", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
Jeff Browna4d82042012-10-02 19:11:19 -0700516 ", icon:" + icon + ", invalid charger:" + mInvalidCharger);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
Jeff Brown605ea692012-10-05 16:33:10 -0700519 mHandler.post(new Runnable() {
520 @Override
521 public void run() {
522 ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
523 }
524 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526
Jeff Browna4d82042012-10-02 19:11:19 -0700527 private void logBatteryStatsLocked() {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700528 IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
Dan Egnor18e93962010-02-10 19:27:58 -0800529 if (batteryInfoService == null) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530
Dan Egnor18e93962010-02-10 19:27:58 -0800531 DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
532 if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;
533
534 File dumpFile = null;
535 FileOutputStream dumpStream = null;
536 try {
537 // dump the service to a file
Dianne Hackborn8c841092013-06-24 13:46:13 -0700538 dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump");
Dan Egnor18e93962010-02-10 19:27:58 -0800539 dumpStream = new FileOutputStream(dumpFile);
540 batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700541 FileUtils.sync(dumpStream);
Dan Egnor18e93962010-02-10 19:27:58 -0800542
543 // add dump file to drop box
544 db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
545 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 Slog.e(TAG, "failed to dump battery service", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800547 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.e(TAG, "failed to write dumpsys file", e);
Dan Egnor18e93962010-02-10 19:27:58 -0800549 } finally {
550 // make sure we clean up
551 if (dumpStream != null) {
552 try {
553 dumpStream.close();
554 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800555 Slog.e(TAG, "failed to close dumpsys output stream");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
Dan Egnor18e93962010-02-10 19:27:58 -0800557 }
558 if (dumpFile != null && !dumpFile.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800559 Slog.e(TAG, "failed to delete temporary dumpsys file: "
Dan Egnor18e93962010-02-10 19:27:58 -0800560 + dumpFile.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 }
562 }
563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800564
Jeff Browna4d82042012-10-02 19:11:19 -0700565 private void logOutlierLocked(long duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 ContentResolver cr = mContext.getContentResolver();
Jeff Sharkey625239a2012-09-26 22:03:49 -0700567 String dischargeThresholdString = Settings.Global.getString(cr,
568 Settings.Global.BATTERY_DISCHARGE_THRESHOLD);
569 String durationThresholdString = Settings.Global.getString(cr,
570 Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 if (dischargeThresholdString != null && durationThresholdString != null) {
573 try {
574 long durationThreshold = Long.parseLong(durationThresholdString);
575 int dischargeThreshold = Integer.parseInt(dischargeThresholdString);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800576 if (duration <= durationThreshold &&
Todd Poynor26faecc2013-05-22 18:54:48 -0700577 mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 // If the discharge cycle is bad enough we want to know about it.
Jeff Browna4d82042012-10-02 19:11:19 -0700579 logBatteryStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
Jeff Browna4d82042012-10-02 19:11:19 -0700581 if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 " discharge threshold: " + dischargeThreshold);
Jeff Browna4d82042012-10-02 19:11:19 -0700583 if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " +
Todd Poynor26faecc2013-05-22 18:54:48 -0700584 (mDischargeStartLevel - mBatteryProps.batteryLevel));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800586 Slog.e(TAG, "Invalid DischargeThresholds GService string: " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 durationThresholdString + " or " + dischargeThresholdString);
588 return;
589 }
590 }
591 }
592
Jeff Browna4d82042012-10-02 19:11:19 -0700593 private int getIconLocked(int level) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700594 if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 return com.android.internal.R.drawable.stat_sys_battery_charge;
Todd Poynor26faecc2013-05-22 18:54:48 -0700596 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 return com.android.internal.R.drawable.stat_sys_battery;
Todd Poynor26faecc2013-05-22 18:54:48 -0700598 } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
599 || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
Jeff Browna4d82042012-10-02 19:11:19 -0700600 if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
Todd Poynor26faecc2013-05-22 18:54:48 -0700601 && mBatteryProps.batteryLevel >= 100) {
Joe Onorato794be402010-11-21 19:22:25 -0800602 return com.android.internal.R.drawable.stat_sys_battery_charge;
603 } else {
604 return com.android.internal.R.drawable.stat_sys_battery;
605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 } else {
607 return com.android.internal.R.drawable.stat_sys_battery_unknown;
608 }
609 }
610
Jeff Brown21392762014-06-13 19:00:36 -0700611 private void dumpInternal(PrintWriter pw, String[] args) {
Jeff Browna4d82042012-10-02 19:11:19 -0700612 synchronized (mLock) {
613 if (args == null || args.length == 0 || "-a".equals(args[0])) {
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700614 pw.println("Current Battery Service state:");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700615 if (mUpdatesStopped) {
616 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
617 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700618 pw.println(" AC powered: " + mBatteryProps.chargerAcOnline);
619 pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline);
620 pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline);
621 pw.println(" status: " + mBatteryProps.batteryStatus);
622 pw.println(" health: " + mBatteryProps.batteryHealth);
623 pw.println(" present: " + mBatteryProps.batteryPresent);
624 pw.println(" level: " + mBatteryProps.batteryLevel);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700625 pw.println(" scale: " + BATTERY_SCALE);
Todd Poynordf89ca32013-07-30 20:33:27 -0700626 pw.println(" voltage: " + mBatteryProps.batteryVoltage);
Todd Poynor26faecc2013-05-22 18:54:48 -0700627 pw.println(" temperature: " + mBatteryProps.batteryTemperature);
628 pw.println(" technology: " + mBatteryProps.batteryTechnology);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700629 } else if (args.length == 3 && "set".equals(args[0])) {
630 String key = args[1];
631 String value = args[2];
632 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800633 if (!mUpdatesStopped) {
634 mLastBatteryProps.set(mBatteryProps);
635 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700636 boolean update = true;
637 if ("ac".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700638 mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700639 } else if ("usb".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700640 mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700641 } else if ("wireless".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700642 mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0;
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700643 } else if ("status".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700644 mBatteryProps.batteryStatus = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700645 } else if ("level".equals(key)) {
Todd Poynor26faecc2013-05-22 18:54:48 -0700646 mBatteryProps.batteryLevel = Integer.parseInt(value);
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700647 } else if ("invalid".equals(key)) {
648 mInvalidCharger = Integer.parseInt(value);
649 } else {
650 pw.println("Unknown set option: " + key);
651 update = false;
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700652 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700653 if (update) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700654 long ident = Binder.clearCallingIdentity();
655 try {
656 mUpdatesStopped = true;
Dianne Hackborn14272302014-06-10 23:13:02 -0700657 processValuesLocked(false);
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700658 } finally {
659 Binder.restoreCallingIdentity(ident);
660 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700661 }
662 } catch (NumberFormatException ex) {
663 pw.println("Bad value: " + value);
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700664 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700665 } else if (args.length == 1 && "reset".equals(args[0])) {
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700666 long ident = Binder.clearCallingIdentity();
667 try {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800668 if (mUpdatesStopped) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800669 mUpdatesStopped = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800670 mBatteryProps.set(mLastBatteryProps);
Dianne Hackborn14272302014-06-10 23:13:02 -0700671 processValuesLocked(false);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800672 }
Dianne Hackborn053f61d2013-06-26 18:07:43 -0700673 } finally {
674 Binder.restoreCallingIdentity(ident);
675 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700676 } else {
677 pw.println("Dump current battery state, or:");
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800678 pw.println(" set [ac|usb|wireless|status|level|invalid] <value>");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700679 pw.println(" reset");
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 }
Joe Onoratode1b3592010-10-25 20:36:47 -0700683
Jeff Browna4d82042012-10-02 19:11:19 -0700684 private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
685 @Override
686 public void onUEvent(UEventObserver.UEvent event) {
687 final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
688 synchronized (mLock) {
689 if (mInvalidCharger != invalidCharger) {
690 mInvalidCharger = invalidCharger;
Jeff Browna4d82042012-10-02 19:11:19 -0700691 }
692 }
693 }
694 };
Joe Onoratode1b3592010-10-25 20:36:47 -0700695
Jeff Browna4d82042012-10-02 19:11:19 -0700696 private final class Led {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800697 private final Light mBatteryLight;
Joe Onoratode1b3592010-10-25 20:36:47 -0700698
Jeff Browna4d82042012-10-02 19:11:19 -0700699 private final int mBatteryLowARGB;
700 private final int mBatteryMediumARGB;
701 private final int mBatteryFullARGB;
702 private final int mBatteryLedOn;
703 private final int mBatteryLedOff;
704
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800705 public Led(Context context, LightsManager lights) {
706 mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);
Joe Onoratode1b3592010-10-25 20:36:47 -0700707
Jeff Browna4d82042012-10-02 19:11:19 -0700708 mBatteryLowARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700709 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700710 mBatteryMediumARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700711 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700712 mBatteryFullARGB = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700713 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
Jeff Browna4d82042012-10-02 19:11:19 -0700714 mBatteryLedOn = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700715 com.android.internal.R.integer.config_notificationsBatteryLedOn);
Jeff Browna4d82042012-10-02 19:11:19 -0700716 mBatteryLedOff = context.getResources().getInteger(
Joe Onoratode1b3592010-10-25 20:36:47 -0700717 com.android.internal.R.integer.config_notificationsBatteryLedOff);
718 }
719
720 /**
721 * Synchronize on BatteryService.
722 */
Jeff Browna4d82042012-10-02 19:11:19 -0700723 public void updateLightsLocked() {
Todd Poynor26faecc2013-05-22 18:54:48 -0700724 final int level = mBatteryProps.batteryLevel;
725 final int status = mBatteryProps.batteryStatus;
Joe Onoratode1b3592010-10-25 20:36:47 -0700726 if (level < mLowBatteryWarningLevel) {
727 if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
728 // Solid red when battery is charging
729 mBatteryLight.setColor(mBatteryLowARGB);
730 } else {
731 // Flash red when battery is low and not charging
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800732 mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,
Joe Onoratode1b3592010-10-25 20:36:47 -0700733 mBatteryLedOn, mBatteryLedOff);
734 }
735 } else if (status == BatteryManager.BATTERY_STATUS_CHARGING
736 || status == BatteryManager.BATTERY_STATUS_FULL) {
737 if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) {
738 // Solid green when full or charging and nearly full
739 mBatteryLight.setColor(mBatteryFullARGB);
740 } else {
741 // Solid orange when charging and halfway full
742 mBatteryLight.setColor(mBatteryMediumARGB);
743 }
744 } else {
745 // No lights if not charging and not low
746 mBatteryLight.turnOff();
747 }
748 }
749 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700750
751 private final class BatteryListener extends IBatteryPropertiesListener.Stub {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800752 @Override
Todd Poynor26faecc2013-05-22 18:54:48 -0700753 public void batteryPropertiesChanged(BatteryProperties props) {
Adam Lesinskief2ea1f2013-12-05 16:48:06 -0800754 final long identity = Binder.clearCallingIdentity();
755 try {
756 BatteryService.this.update(props);
757 } finally {
758 Binder.restoreCallingIdentity(identity);
759 }
Todd Poynor26faecc2013-05-22 18:54:48 -0700760 }
761 }
Jeff Brown21392762014-06-13 19:00:36 -0700762
763 private final class BinderService extends Binder {
764 @Override
765 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
766 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
767 != PackageManager.PERMISSION_GRANTED) {
768
769 pw.println("Permission Denial: can't dump Battery service from from pid="
770 + Binder.getCallingPid()
771 + ", uid=" + Binder.getCallingUid());
772 return;
773 }
774
775 dumpInternal(pw, args);
776 }
777 }
778
779 private final class LocalService extends BatteryManagerInternal {
780 @Override
781 public boolean isPowered(int plugTypeSet) {
782 synchronized (mLock) {
783 return isPoweredLocked(plugTypeSet);
784 }
785 }
786
787 @Override
788 public int getPlugType() {
789 synchronized (mLock) {
790 return mPlugType;
791 }
792 }
793
794 @Override
795 public int getBatteryLevel() {
796 synchronized (mLock) {
797 return mBatteryProps.batteryLevel;
798 }
799 }
800
801 @Override
802 public boolean getBatteryLevelLow() {
803 synchronized (mLock) {
804 return mBatteryLevelLow;
805 }
806 }
807
808 @Override
809 public int getInvalidCharger() {
810 synchronized (mLock) {
811 return mInvalidCharger;
812 }
813 }
814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815}