The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 19 | import android.os.BatteryStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.app.IBatteryStats; |
| 21 | import com.android.server.am.BatteryStatsService; |
| 22 | |
| 23 | import android.app.ActivityManagerNative; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.os.BatteryManager; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 29 | import android.os.BatteryProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.Binder; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 31 | import android.os.FileUtils; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 33 | import android.os.IBatteryPropertiesListener; |
| 34 | import android.os.IBatteryPropertiesRegistrar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.IBinder; |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 36 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.RemoteException; |
| 38 | import android.os.ServiceManager; |
| 39 | import android.os.SystemClock; |
| 40 | import android.os.UEventObserver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 41 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.provider.Settings; |
| 43 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 44 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 46 | import java.io.File; |
| 47 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.io.FileOutputStream; |
| 49 | import java.io.IOException; |
| 50 | import java.io.PrintWriter; |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * <p>BatteryService monitors the charging status, and charge level of the device |
| 55 | * battery. When these values change this service broadcasts the new values |
| 56 | * to all {@link android.content.BroadcastReceiver IntentReceivers} that are |
| 57 | * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED |
| 58 | * BATTERY_CHANGED} action.</p> |
| 59 | * <p>The new values are stored in the Intent data and can be retrieved by |
| 60 | * calling {@link android.content.Intent#getExtra Intent.getExtra} with the |
| 61 | * following keys:</p> |
| 62 | * <p>"scale" - int, the maximum value for the charge level</p> |
| 63 | * <p>"level" - int, charge level, from 0 through "scale" inclusive</p> |
| 64 | * <p>"status" - String, the current charging status.<br /> |
| 65 | * <p>"health" - String, the current battery health.<br /> |
| 66 | * <p>"present" - boolean, true if the battery is present<br /> |
| 67 | * <p>"icon-small" - int, suggested small icon to use for this state</p> |
| 68 | * <p>"plugged" - int, 0 if the device is not plugged in; 1 if plugged |
| 69 | * into an AC power adapter; 2 if plugged in via USB.</p> |
| 70 | * <p>"voltage" - int, current battery voltage in millivolts</p> |
| 71 | * <p>"temperature" - int, current battery temperature in tenths of |
| 72 | * a degree Centigrade</p> |
| 73 | * <p>"technology" - String, the type of battery installed, e.g. "Li-ion"</p> |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 74 | * |
| 75 | * <p> |
| 76 | * The battery service may be called by the power manager while holding its locks so |
| 77 | * we take care to post all outcalls into the activity manager to a handler. |
| 78 | * |
| 79 | * FIXME: Ideally the power manager would perform all of its calls into the battery |
| 80 | * service asynchronously itself. |
| 81 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | */ |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 83 | public final class BatteryService extends Binder { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private static final String TAG = BatteryService.class.getSimpleName(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 85 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 86 | private static final boolean DEBUG = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 87 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 88 | private static final int BATTERY_SCALE = 100; // battery capacity is a percentage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | // Used locally for determining when to make a last ditch effort to log |
| 91 | // discharge stats before the device dies. |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 92 | private int mCriticalBatteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
| 94 | private static final int DUMP_MAX_LENGTH = 24 * 1024; |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 95 | private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" }; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | private static final String DUMPSYS_DATA_PATH = "/data/system/"; |
| 98 | |
| 99 | // This should probably be exposed in the API, though it's not critical |
| 100 | private static final int BATTERY_PLUGGED_NONE = 0; |
| 101 | |
| 102 | private final Context mContext; |
| 103 | private final IBatteryStats mBatteryStats; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 104 | private final Handler mHandler; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 105 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 106 | private final Object mLock = new Object(); |
| 107 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 108 | private BatteryProperties mBatteryProps; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | private boolean mBatteryLevelCritical; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private int mLastBatteryStatus; |
| 111 | private int mLastBatteryHealth; |
| 112 | private boolean mLastBatteryPresent; |
| 113 | private int mLastBatteryLevel; |
| 114 | private int mLastBatteryVoltage; |
| 115 | private int mLastBatteryTemperature; |
| 116 | private boolean mLastBatteryLevelCritical; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 117 | |
| 118 | private int mInvalidCharger; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 119 | private int mLastInvalidCharger; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 120 | |
| 121 | private int mLowBatteryWarningLevel; |
| 122 | private int mLowBatteryCloseWarningLevel; |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 123 | private int mShutdownBatteryTemperature; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | private int mPlugType; |
| 126 | private int mLastPlugType = -1; // Extra state so we can detect first run |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | private long mDischargeStartTime; |
| 129 | private int mDischargeStartLevel; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 130 | |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 131 | private boolean mUpdatesStopped; |
| 132 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 133 | private Led mLed; |
| 134 | |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 135 | private boolean mSentLowBatteryBroadcast = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 136 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 137 | private BatteryListener mBatteryPropertiesListener; |
| 138 | private IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 139 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 140 | public BatteryService(Context context, LightsService lights) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | mContext = context; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 142 | mHandler = new Handler(true /*async*/); |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 143 | mLed = new Led(context, lights); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mBatteryStats = BatteryStatsService.getService(); |
| 145 | |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 146 | mCriticalBatteryLevel = mContext.getResources().getInteger( |
| 147 | com.android.internal.R.integer.config_criticalBatteryWarningLevel); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 148 | mLowBatteryWarningLevel = mContext.getResources().getInteger( |
| 149 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
| 150 | mLowBatteryCloseWarningLevel = mContext.getResources().getInteger( |
| 151 | com.android.internal.R.integer.config_lowBatteryCloseWarningLevel); |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 152 | mShutdownBatteryTemperature = mContext.getResources().getInteger( |
| 153 | com.android.internal.R.integer.config_shutdownBatteryTemperature); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 154 | |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 155 | // watch for invalid charger messages if the invalid_charger switch exists |
| 156 | if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 157 | mInvalidChargerObserver.startObserving( |
| 158 | "DEVPATH=/devices/virtual/switch/invalid_charger"); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 161 | mBatteryPropertiesListener = new BatteryListener(); |
| 162 | |
Todd Poynor | 0edc5b5 | 2013-10-22 17:53:13 -0700 | [diff] [blame^] | 163 | IBinder b = ServiceManager.getService("batteryproperties"); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 164 | mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(b); |
| 165 | |
| 166 | try { |
| 167 | mBatteryPropertiesRegistrar.registerListener(mBatteryPropertiesListener); |
| 168 | } catch (RemoteException e) { |
| 169 | // Should never happen. |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 170 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 173 | void systemReady() { |
| 174 | // check our power situation now that it is safe to display the shutdown dialog. |
| 175 | synchronized (mLock) { |
| 176 | shutdownIfNoPowerLocked(); |
| 177 | shutdownIfOverTempLocked(); |
| 178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Returns true if the device is plugged into any of the specified plug types. |
| 183 | */ |
| 184 | public boolean isPowered(int plugTypeSet) { |
| 185 | synchronized (mLock) { |
| 186 | return isPoweredLocked(plugTypeSet); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private boolean isPoweredLocked(int plugTypeSet) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | // assume we are powered if battery state is unknown so |
| 192 | // the "stay on while plugged in" option will work. |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 193 | if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | return true; |
| 195 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 196 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mBatteryProps.chargerAcOnline) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 197 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 199 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mBatteryProps.chargerUsbOnline) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 200 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 202 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mBatteryProps.chargerWirelessOnline) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 203 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 205 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 208 | /** |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 209 | * Returns the current plug type. |
| 210 | */ |
| 211 | public int getPlugType() { |
| 212 | synchronized (mLock) { |
| 213 | return mPlugType; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 218 | * Returns battery level as a percentage. |
| 219 | */ |
| 220 | public int getBatteryLevel() { |
| 221 | synchronized (mLock) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 222 | return mBatteryProps.batteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 224 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Returns true if battery level is below the first warning threshold. |
| 228 | */ |
| 229 | public boolean isBatteryLow() { |
| 230 | synchronized (mLock) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 231 | return mBatteryProps.batteryPresent && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel; |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 232 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Svetoslav | 6a08a12 | 2013-05-03 11:24:26 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Returns a non-zero value if an unsupported charger is attached. |
| 237 | */ |
| 238 | public int getInvalidCharger() { |
| 239 | synchronized (mLock) { |
| 240 | return mInvalidCharger; |
| 241 | } |
| 242 | } |
| 243 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 244 | private void shutdownIfNoPowerLocked() { |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 245 | // shut down gracefully if our battery is critically low and we are not powered. |
| 246 | // wait until the system has booted before attempting to display the shutdown dialog. |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 247 | if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 248 | mHandler.post(new Runnable() { |
| 249 | @Override |
| 250 | public void run() { |
| 251 | if (ActivityManagerNative.isSystemReady()) { |
| 252 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 253 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
| 254 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 255 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 256 | } |
| 257 | } |
| 258 | }); |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 262 | private void shutdownIfOverTempLocked() { |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 263 | // shut down gracefully if temperature is too high (> 68.0C by default) |
| 264 | // wait until the system has booted before attempting to display the |
| 265 | // shutdown dialog. |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 266 | if (mBatteryProps.batteryTemperature > mShutdownBatteryTemperature) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 267 | mHandler.post(new Runnable() { |
| 268 | @Override |
| 269 | public void run() { |
| 270 | if (ActivityManagerNative.isSystemReady()) { |
| 271 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 272 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
| 273 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 274 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 275 | } |
| 276 | } |
| 277 | }); |
Eric Olsen | 6a362a9 | 2010-03-26 15:38:41 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 281 | private void update(BatteryProperties props) { |
| 282 | synchronized (mLock) { |
| 283 | if (!mUpdatesStopped) { |
| 284 | mBatteryProps = props; |
| 285 | // Process the new values. |
| 286 | processValuesLocked(); |
| 287 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 288 | } |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 291 | private void processValuesLocked() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 292 | boolean logOutlier = false; |
| 293 | long dischargeDuration = 0; |
Joe Onorato | a7e4cf9b | 2009-07-28 18:18:20 -0700 | [diff] [blame] | 294 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 295 | mBatteryLevelCritical = (mBatteryProps.batteryLevel <= mCriticalBatteryLevel); |
| 296 | if (mBatteryProps.chargerAcOnline) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | mPlugType = BatteryManager.BATTERY_PLUGGED_AC; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 298 | } else if (mBatteryProps.chargerUsbOnline) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | mPlugType = BatteryManager.BATTERY_PLUGGED_USB; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 300 | } else if (mBatteryProps.chargerWirelessOnline) { |
Brian Muramatsu | 37a37f4 | 2012-08-14 15:21:02 -0700 | [diff] [blame] | 301 | mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } else { |
| 303 | mPlugType = BATTERY_PLUGGED_NONE; |
| 304 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 305 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 306 | if (DEBUG) { |
| 307 | Slog.d(TAG, "Processing new values: " |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 308 | + "chargerAcOnline=" + mBatteryProps.chargerAcOnline |
| 309 | + ", chargerUsbOnline=" + mBatteryProps.chargerUsbOnline |
| 310 | + ", chargerWirelessOnline=" + mBatteryProps.chargerWirelessOnline |
| 311 | + ", batteryStatus=" + mBatteryProps.batteryStatus |
| 312 | + ", batteryHealth=" + mBatteryProps.batteryHealth |
| 313 | + ", batteryPresent=" + mBatteryProps.batteryPresent |
| 314 | + ", batteryLevel=" + mBatteryProps.batteryLevel |
| 315 | + ", batteryTechnology=" + mBatteryProps.batteryTechnology |
| 316 | + ", batteryVoltage=" + mBatteryProps.batteryVoltage |
| 317 | + ", batteryTemperature=" + mBatteryProps.batteryTemperature |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 318 | + ", mBatteryLevelCritical=" + mBatteryLevelCritical |
| 319 | + ", mPlugType=" + mPlugType); |
| 320 | } |
| 321 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 322 | // Let the battery stats keep track of the current level. |
| 323 | try { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 324 | mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, |
| 325 | mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature, |
| 326 | mBatteryProps.batteryVoltage); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 327 | } catch (RemoteException e) { |
| 328 | // Should never happen. |
| 329 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 330 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 331 | shutdownIfNoPowerLocked(); |
| 332 | shutdownIfOverTempLocked(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 333 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 334 | if (mBatteryProps.batteryStatus != mLastBatteryStatus || |
| 335 | mBatteryProps.batteryHealth != mLastBatteryHealth || |
| 336 | mBatteryProps.batteryPresent != mLastBatteryPresent || |
| 337 | mBatteryProps.batteryLevel != mLastBatteryLevel || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | mPlugType != mLastPlugType || |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 339 | mBatteryProps.batteryVoltage != mLastBatteryVoltage || |
| 340 | mBatteryProps.batteryTemperature != mLastBatteryTemperature || |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 341 | mInvalidCharger != mLastInvalidCharger) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | if (mPlugType != mLastPlugType) { |
| 344 | if (mLastPlugType == BATTERY_PLUGGED_NONE) { |
| 345 | // discharging -> charging |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | // There's no value in this data unless we've discharged at least once and the |
| 348 | // battery level has changed; so don't log until it does. |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 349 | if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 350 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 351 | logOutlier = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 352 | EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration, |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 353 | mDischargeStartLevel, mBatteryProps.batteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | // make sure we see a discharge event before logging again |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 355 | mDischargeStartTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
| 357 | } else if (mPlugType == BATTERY_PLUGGED_NONE) { |
| 358 | // charging -> discharging or we just powered up |
| 359 | mDischargeStartTime = SystemClock.elapsedRealtime(); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 360 | mDischargeStartLevel = mBatteryProps.batteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 363 | if (mBatteryProps.batteryStatus != mLastBatteryStatus || |
| 364 | mBatteryProps.batteryHealth != mLastBatteryHealth || |
| 365 | mBatteryProps.batteryPresent != mLastBatteryPresent || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | mPlugType != mLastPlugType) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 367 | EventLog.writeEvent(EventLogTags.BATTERY_STATUS, |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 368 | mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0, |
| 369 | mPlugType, mBatteryProps.batteryTechnology); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 371 | if (mBatteryProps.batteryLevel != mLastBatteryLevel) { |
Dianne Hackborn | cf117164 | 2013-07-12 17:26:02 -0700 | [diff] [blame] | 372 | // Don't do this just from voltage or temperature changes, that is |
| 373 | // too noisy. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 374 | EventLog.writeEvent(EventLogTags.BATTERY_LEVEL, |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 375 | mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| 377 | if (mBatteryLevelCritical && !mLastBatteryLevelCritical && |
| 378 | mPlugType == BATTERY_PLUGGED_NONE) { |
| 379 | // We want to make sure we log discharge cycle outliers |
| 380 | // if the battery is about to die. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 381 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 382 | logOutlier = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 384 | |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 385 | final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE; |
| 386 | final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE; |
| 387 | |
| 388 | /* The ACTION_BATTERY_LOW broadcast is sent in these situations: |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 389 | * - is just un-plugged (previously was plugged) and battery level is |
| 390 | * less than or equal to WARNING, or |
| 391 | * - is not plugged and battery level falls to WARNING boundary |
| 392 | * (becomes <= mLowBatteryWarningLevel). |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 393 | */ |
| 394 | final boolean sendBatteryLow = !plugged |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 395 | && mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN |
| 396 | && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 397 | && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 398 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 399 | sendIntentLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 400 | |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 401 | // Separate broadcast is sent for power connected / not connected |
| 402 | // since the standard intent will not wake any applications and some |
| 403 | // applications may want to have smart behavior based on this. |
| 404 | if (mPlugType != 0 && mLastPlugType == 0) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 405 | mHandler.post(new Runnable() { |
| 406 | @Override |
| 407 | public void run() { |
| 408 | Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED); |
| 409 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 410 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 411 | } |
| 412 | }); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 413 | } |
| 414 | else if (mPlugType == 0 && mLastPlugType != 0) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 415 | mHandler.post(new Runnable() { |
| 416 | @Override |
| 417 | public void run() { |
| 418 | Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED); |
| 419 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 420 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 421 | } |
| 422 | }); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 423 | } |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 424 | |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 425 | if (sendBatteryLow) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 426 | mSentLowBatteryBroadcast = true; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 427 | mHandler.post(new Runnable() { |
| 428 | @Override |
| 429 | public void run() { |
| 430 | Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW); |
| 431 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 432 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 433 | } |
| 434 | }); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 435 | } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 436 | mSentLowBatteryBroadcast = false; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 437 | mHandler.post(new Runnable() { |
| 438 | @Override |
| 439 | public void run() { |
| 440 | Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY); |
| 441 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 442 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 443 | } |
| 444 | }); |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 445 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 446 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 447 | // Update the battery LED |
| 448 | mLed.updateLightsLocked(); |
| 449 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 450 | // This needs to be done after sendIntent() so that we get the lastest battery stats. |
| 451 | if (logOutlier && dischargeDuration != 0) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 452 | logOutlierLocked(dischargeDuration); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 453 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 454 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 455 | mLastBatteryStatus = mBatteryProps.batteryStatus; |
| 456 | mLastBatteryHealth = mBatteryProps.batteryHealth; |
| 457 | mLastBatteryPresent = mBatteryProps.batteryPresent; |
| 458 | mLastBatteryLevel = mBatteryProps.batteryLevel; |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 459 | mLastPlugType = mPlugType; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 460 | mLastBatteryVoltage = mBatteryProps.batteryVoltage; |
| 461 | mLastBatteryTemperature = mBatteryProps.batteryTemperature; |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 462 | mLastBatteryLevelCritical = mBatteryLevelCritical; |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 463 | mLastInvalidCharger = mInvalidCharger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 467 | private void sendIntentLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | // Pack up the values and broadcast them to everyone |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 469 | final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 470 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 471 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 472 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 473 | int icon = getIconLocked(mBatteryProps.batteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 475 | intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus); |
| 476 | intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth); |
| 477 | intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent); |
| 478 | intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel); |
Dianne Hackborn | edd9316 | 2009-09-19 14:03:05 -0700 | [diff] [blame] | 479 | intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE); |
| 480 | intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon); |
| 481 | intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 482 | intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage); |
| 483 | intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature); |
| 484 | intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 485 | intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 487 | if (DEBUG) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 488 | Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel + |
| 489 | ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus + |
| 490 | ", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent + |
| 491 | ", voltage: " + mBatteryProps.batteryVoltage + |
| 492 | ", temperature: " + mBatteryProps.batteryTemperature + |
| 493 | ", technology: " + mBatteryProps.batteryTechnology + |
| 494 | ", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline + |
| 495 | ", Wireless powered:" + mBatteryProps.chargerWirelessOnline + |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 496 | ", icon:" + icon + ", invalid charger:" + mInvalidCharger); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 499 | mHandler.post(new Runnable() { |
| 500 | @Override |
| 501 | public void run() { |
| 502 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 503 | } |
| 504 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 507 | private void logBatteryStatsLocked() { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 508 | IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 509 | if (batteryInfoService == null) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 511 | DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 512 | if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return; |
| 513 | |
| 514 | File dumpFile = null; |
| 515 | FileOutputStream dumpStream = null; |
| 516 | try { |
| 517 | // dump the service to a file |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 518 | dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump"); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 519 | dumpStream = new FileOutputStream(dumpFile); |
| 520 | batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS); |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 521 | FileUtils.sync(dumpStream); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 522 | |
| 523 | // add dump file to drop box |
| 524 | db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT); |
| 525 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 526 | Slog.e(TAG, "failed to dump battery service", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 527 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 528 | Slog.e(TAG, "failed to write dumpsys file", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 529 | } finally { |
| 530 | // make sure we clean up |
| 531 | if (dumpStream != null) { |
| 532 | try { |
| 533 | dumpStream.close(); |
| 534 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 535 | Slog.e(TAG, "failed to close dumpsys output stream"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | } |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 537 | } |
| 538 | if (dumpFile != null && !dumpFile.delete()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 539 | Slog.e(TAG, "failed to delete temporary dumpsys file: " |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 540 | + dumpFile.getAbsolutePath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 544 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 545 | private void logOutlierLocked(long duration) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | ContentResolver cr = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 547 | String dischargeThresholdString = Settings.Global.getString(cr, |
| 548 | Settings.Global.BATTERY_DISCHARGE_THRESHOLD); |
| 549 | String durationThresholdString = Settings.Global.getString(cr, |
| 550 | Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | if (dischargeThresholdString != null && durationThresholdString != null) { |
| 553 | try { |
| 554 | long durationThreshold = Long.parseLong(durationThresholdString); |
| 555 | int dischargeThreshold = Integer.parseInt(dischargeThresholdString); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 556 | if (duration <= durationThreshold && |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 557 | mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | // If the discharge cycle is bad enough we want to know about it. |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 559 | logBatteryStatsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 561 | if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | " discharge threshold: " + dischargeThreshold); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 563 | if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " + |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 564 | (mDischargeStartLevel - mBatteryProps.batteryLevel)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 566 | Slog.e(TAG, "Invalid DischargeThresholds GService string: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | durationThresholdString + " or " + dischargeThresholdString); |
| 568 | return; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 573 | private int getIconLocked(int level) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 574 | if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 576 | } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | return com.android.internal.R.drawable.stat_sys_battery; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 578 | } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING |
| 579 | || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 580 | if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY) |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 581 | && mBatteryProps.batteryLevel >= 100) { |
Joe Onorato | 794be40 | 2010-11-21 19:22:25 -0800 | [diff] [blame] | 582 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
| 583 | } else { |
| 584 | return com.android.internal.R.drawable.stat_sys_battery; |
| 585 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } else { |
| 587 | return com.android.internal.R.drawable.stat_sys_battery_unknown; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 593 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 594 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | pw.println("Permission Denial: can't dump Battery service from from pid=" |
| 597 | + Binder.getCallingPid() |
| 598 | + ", uid=" + Binder.getCallingUid()); |
| 599 | return; |
| 600 | } |
| 601 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 602 | synchronized (mLock) { |
| 603 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 604 | pw.println("Current Battery Service state:"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 605 | if (mUpdatesStopped) { |
| 606 | pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); |
| 607 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 608 | pw.println(" AC powered: " + mBatteryProps.chargerAcOnline); |
| 609 | pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline); |
| 610 | pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline); |
| 611 | pw.println(" status: " + mBatteryProps.batteryStatus); |
| 612 | pw.println(" health: " + mBatteryProps.batteryHealth); |
| 613 | pw.println(" present: " + mBatteryProps.batteryPresent); |
| 614 | pw.println(" level: " + mBatteryProps.batteryLevel); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 615 | pw.println(" scale: " + BATTERY_SCALE); |
Todd Poynor | df89ca3 | 2013-07-30 20:33:27 -0700 | [diff] [blame] | 616 | pw.println(" voltage: " + mBatteryProps.batteryVoltage); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 617 | pw.println(" temperature: " + mBatteryProps.batteryTemperature); |
| 618 | pw.println(" technology: " + mBatteryProps.batteryTechnology); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 619 | } else if (args.length == 3 && "set".equals(args[0])) { |
| 620 | String key = args[1]; |
| 621 | String value = args[2]; |
| 622 | try { |
| 623 | boolean update = true; |
| 624 | if ("ac".equals(key)) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 625 | mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0; |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 626 | } else if ("usb".equals(key)) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 627 | mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0; |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 628 | } else if ("wireless".equals(key)) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 629 | mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0; |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 630 | } else if ("status".equals(key)) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 631 | mBatteryProps.batteryStatus = Integer.parseInt(value); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 632 | } else if ("level".equals(key)) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 633 | mBatteryProps.batteryLevel = Integer.parseInt(value); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 634 | } else if ("invalid".equals(key)) { |
| 635 | mInvalidCharger = Integer.parseInt(value); |
| 636 | } else { |
| 637 | pw.println("Unknown set option: " + key); |
| 638 | update = false; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 639 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 640 | if (update) { |
Dianne Hackborn | 053f61d | 2013-06-26 18:07:43 -0700 | [diff] [blame] | 641 | long ident = Binder.clearCallingIdentity(); |
| 642 | try { |
| 643 | mUpdatesStopped = true; |
| 644 | processValuesLocked(); |
| 645 | } finally { |
| 646 | Binder.restoreCallingIdentity(ident); |
| 647 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 648 | } |
| 649 | } catch (NumberFormatException ex) { |
| 650 | pw.println("Bad value: " + value); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 651 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 652 | } else if (args.length == 1 && "reset".equals(args[0])) { |
Dianne Hackborn | 053f61d | 2013-06-26 18:07:43 -0700 | [diff] [blame] | 653 | long ident = Binder.clearCallingIdentity(); |
| 654 | try { |
| 655 | mUpdatesStopped = false; |
Dianne Hackborn | 053f61d | 2013-06-26 18:07:43 -0700 | [diff] [blame] | 656 | } finally { |
| 657 | Binder.restoreCallingIdentity(ident); |
| 658 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 659 | } else { |
| 660 | pw.println("Dump current battery state, or:"); |
| 661 | pw.println(" set ac|usb|wireless|status|level|invalid <value>"); |
| 662 | pw.println(" reset"); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 666 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 667 | private final UEventObserver mInvalidChargerObserver = new UEventObserver() { |
| 668 | @Override |
| 669 | public void onUEvent(UEventObserver.UEvent event) { |
| 670 | final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0; |
| 671 | synchronized (mLock) { |
| 672 | if (mInvalidCharger != invalidCharger) { |
| 673 | mInvalidCharger = invalidCharger; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } |
| 677 | }; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 678 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 679 | private final class Led { |
| 680 | private final LightsService.Light mBatteryLight; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 681 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 682 | private final int mBatteryLowARGB; |
| 683 | private final int mBatteryMediumARGB; |
| 684 | private final int mBatteryFullARGB; |
| 685 | private final int mBatteryLedOn; |
| 686 | private final int mBatteryLedOff; |
| 687 | |
| 688 | public Led(Context context, LightsService lights) { |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 689 | mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY); |
| 690 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 691 | mBatteryLowARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 692 | com.android.internal.R.integer.config_notificationsBatteryLowARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 693 | mBatteryMediumARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 694 | com.android.internal.R.integer.config_notificationsBatteryMediumARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 695 | mBatteryFullARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 696 | com.android.internal.R.integer.config_notificationsBatteryFullARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 697 | mBatteryLedOn = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 698 | com.android.internal.R.integer.config_notificationsBatteryLedOn); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 699 | mBatteryLedOff = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 700 | com.android.internal.R.integer.config_notificationsBatteryLedOff); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Synchronize on BatteryService. |
| 705 | */ |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 706 | public void updateLightsLocked() { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 707 | final int level = mBatteryProps.batteryLevel; |
| 708 | final int status = mBatteryProps.batteryStatus; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 709 | if (level < mLowBatteryWarningLevel) { |
| 710 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 711 | // Solid red when battery is charging |
| 712 | mBatteryLight.setColor(mBatteryLowARGB); |
| 713 | } else { |
| 714 | // Flash red when battery is low and not charging |
| 715 | mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED, |
| 716 | mBatteryLedOn, mBatteryLedOff); |
| 717 | } |
| 718 | } else if (status == BatteryManager.BATTERY_STATUS_CHARGING |
| 719 | || status == BatteryManager.BATTERY_STATUS_FULL) { |
| 720 | if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) { |
| 721 | // Solid green when full or charging and nearly full |
| 722 | mBatteryLight.setColor(mBatteryFullARGB); |
| 723 | } else { |
| 724 | // Solid orange when charging and halfway full |
| 725 | mBatteryLight.setColor(mBatteryMediumARGB); |
| 726 | } |
| 727 | } else { |
| 728 | // No lights if not charging and not low |
| 729 | mBatteryLight.turnOff(); |
| 730 | } |
| 731 | } |
| 732 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 733 | |
| 734 | private final class BatteryListener extends IBatteryPropertiesListener.Stub { |
| 735 | public void batteryPropertiesChanged(BatteryProperties props) { |
| 736 | BatteryService.this.update(props); |
| 737 | } |
| 738 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | } |