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