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