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