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 | 926969b | 2016-04-28 17:31:12 -0700 | [diff] [blame] | 367 | mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 368 | } catch (RemoteException e) { |
| 369 | // Should never happen. |
| 370 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 371 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 372 | shutdownIfNoPowerLocked(); |
| 373 | shutdownIfOverTempLocked(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 374 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 375 | if (force || (mBatteryProps.batteryStatus != mLastBatteryStatus || |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 376 | mBatteryProps.batteryHealth != mLastBatteryHealth || |
| 377 | mBatteryProps.batteryPresent != mLastBatteryPresent || |
| 378 | mBatteryProps.batteryLevel != mLastBatteryLevel || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | mPlugType != mLastPlugType || |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 380 | mBatteryProps.batteryVoltage != mLastBatteryVoltage || |
| 381 | mBatteryProps.batteryTemperature != mLastBatteryTemperature || |
Adrian Roos | 76dc5a5 | 2015-07-21 16:20:36 -0700 | [diff] [blame] | 382 | mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent || |
Badhri Jagan Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 383 | mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage || |
Ruchi Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 384 | mBatteryProps.batteryChargeCounter != mLastChargeCounter || |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 385 | mInvalidCharger != mLastInvalidCharger)) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | if (mPlugType != mLastPlugType) { |
| 388 | if (mLastPlugType == BATTERY_PLUGGED_NONE) { |
| 389 | // discharging -> charging |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | // There's no value in this data unless we've discharged at least once and the |
| 392 | // battery level has changed; so don't log until it does. |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 393 | if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryProps.batteryLevel) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 394 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 395 | logOutlier = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 396 | EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration, |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 397 | mDischargeStartLevel, mBatteryProps.batteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | // make sure we see a discharge event before logging again |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 399 | mDischargeStartTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | } |
| 401 | } else if (mPlugType == BATTERY_PLUGGED_NONE) { |
| 402 | // charging -> discharging or we just powered up |
| 403 | mDischargeStartTime = SystemClock.elapsedRealtime(); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 404 | mDischargeStartLevel = mBatteryProps.batteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 407 | if (mBatteryProps.batteryStatus != mLastBatteryStatus || |
| 408 | mBatteryProps.batteryHealth != mLastBatteryHealth || |
| 409 | mBatteryProps.batteryPresent != mLastBatteryPresent || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | mPlugType != mLastPlugType) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 411 | EventLog.writeEvent(EventLogTags.BATTERY_STATUS, |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 412 | mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ? 1 : 0, |
| 413 | mPlugType, mBatteryProps.batteryTechnology); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 415 | if (mBatteryProps.batteryLevel != mLastBatteryLevel) { |
Dianne Hackborn | cf117164 | 2013-07-12 17:26:02 -0700 | [diff] [blame] | 416 | // Don't do this just from voltage or temperature changes, that is |
| 417 | // too noisy. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 418 | EventLog.writeEvent(EventLogTags.BATTERY_LEVEL, |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 419 | mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | } |
| 421 | if (mBatteryLevelCritical && !mLastBatteryLevelCritical && |
| 422 | mPlugType == BATTERY_PLUGGED_NONE) { |
| 423 | // We want to make sure we log discharge cycle outliers |
| 424 | // if the battery is about to die. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 425 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 426 | logOutlier = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 428 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 429 | if (!mBatteryLevelLow) { |
| 430 | // Should we now switch in to low battery mode? |
| 431 | if (mPlugType == BATTERY_PLUGGED_NONE |
| 432 | && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel) { |
| 433 | mBatteryLevelLow = true; |
| 434 | } |
| 435 | } else { |
| 436 | // Should we now switch out of low battery mode? |
| 437 | if (mPlugType != BATTERY_PLUGGED_NONE) { |
| 438 | mBatteryLevelLow = false; |
| 439 | } else if (mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel) { |
| 440 | mBatteryLevelLow = false; |
| 441 | } else if (force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel) { |
| 442 | // If being forced, the previous state doesn't matter, we will just |
| 443 | // absolutely check to see if we are now above the warning level. |
| 444 | mBatteryLevelLow = false; |
| 445 | } |
| 446 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 447 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 448 | sendIntentLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 449 | |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 450 | // Separate broadcast is sent for power connected / not connected |
| 451 | // since the standard intent will not wake any applications and some |
| 452 | // applications may want to have smart behavior based on this. |
| 453 | if (mPlugType != 0 && mLastPlugType == 0) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 454 | mHandler.post(new Runnable() { |
| 455 | @Override |
| 456 | public void run() { |
| 457 | Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED); |
| 458 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 459 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 460 | } |
| 461 | }); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 462 | } |
| 463 | else if (mPlugType == 0 && mLastPlugType != 0) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 464 | mHandler.post(new Runnable() { |
| 465 | @Override |
| 466 | public void run() { |
| 467 | Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED); |
| 468 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 469 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 470 | } |
| 471 | }); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 472 | } |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 473 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 474 | if (shouldSendBatteryLowLocked()) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 475 | mSentLowBatteryBroadcast = true; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 476 | mHandler.post(new Runnable() { |
| 477 | @Override |
| 478 | public void run() { |
| 479 | Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW); |
| 480 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 481 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 482 | } |
| 483 | }); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 484 | } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 485 | mSentLowBatteryBroadcast = false; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 486 | mHandler.post(new Runnable() { |
| 487 | @Override |
| 488 | public void run() { |
| 489 | Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY); |
| 490 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 491 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 492 | } |
| 493 | }); |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 494 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 495 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 496 | // Update the battery LED |
| 497 | mLed.updateLightsLocked(); |
| 498 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 499 | // This needs to be done after sendIntent() so that we get the lastest battery stats. |
| 500 | if (logOutlier && dischargeDuration != 0) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 501 | logOutlierLocked(dischargeDuration); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 502 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 503 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 504 | mLastBatteryStatus = mBatteryProps.batteryStatus; |
| 505 | mLastBatteryHealth = mBatteryProps.batteryHealth; |
| 506 | mLastBatteryPresent = mBatteryProps.batteryPresent; |
| 507 | mLastBatteryLevel = mBatteryProps.batteryLevel; |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 508 | mLastPlugType = mPlugType; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 509 | mLastBatteryVoltage = mBatteryProps.batteryVoltage; |
| 510 | mLastBatteryTemperature = mBatteryProps.batteryTemperature; |
Adrian Roos | 76dc5a5 | 2015-07-21 16:20:36 -0700 | [diff] [blame] | 511 | mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent; |
Badhri Jagan Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 512 | mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage; |
Ruchi Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 513 | mLastChargeCounter = mBatteryProps.batteryChargeCounter; |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 514 | mLastBatteryLevelCritical = mBatteryLevelCritical; |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 515 | mLastInvalidCharger = mInvalidCharger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 519 | private void sendIntentLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | // Pack up the values and broadcast them to everyone |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 521 | final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 522 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 523 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 524 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 525 | int icon = getIconLocked(mBatteryProps.batteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 527 | intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus); |
| 528 | intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth); |
| 529 | intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent); |
| 530 | intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel); |
Dianne Hackborn | edd9316 | 2009-09-19 14:03:05 -0700 | [diff] [blame] | 531 | intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE); |
| 532 | intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon); |
| 533 | intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 534 | intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage); |
| 535 | intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature); |
| 536 | intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 537 | intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger); |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 538 | intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent); |
Badhri Jagan Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 539 | intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage); |
Ruchi Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 540 | intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mBatteryProps.batteryChargeCounter); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 541 | if (DEBUG) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 542 | Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel + |
| 543 | ", scale:" + BATTERY_SCALE + ", status:" + mBatteryProps.batteryStatus + |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 544 | ", health:" + mBatteryProps.batteryHealth + |
| 545 | ", present:" + mBatteryProps.batteryPresent + |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 546 | ", voltage: " + mBatteryProps.batteryVoltage + |
| 547 | ", temperature: " + mBatteryProps.batteryTemperature + |
| 548 | ", technology: " + mBatteryProps.batteryTechnology + |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 549 | ", AC powered:" + mBatteryProps.chargerAcOnline + |
| 550 | ", USB powered:" + mBatteryProps.chargerUsbOnline + |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 551 | ", Wireless powered:" + mBatteryProps.chargerWirelessOnline + |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 552 | ", icon:" + icon + ", invalid charger:" + mInvalidCharger + |
Badhri Jagan Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 553 | ", maxChargingCurrent:" + mBatteryProps.maxChargingCurrent + |
Ruchi Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 554 | ", maxChargingVoltage:" + mBatteryProps.maxChargingVoltage + |
| 555 | ", chargeCounter:" + mBatteryProps.batteryChargeCounter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 558 | mHandler.post(new Runnable() { |
| 559 | @Override |
| 560 | public void run() { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 561 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 562 | } |
| 563 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 566 | private void logBatteryStatsLocked() { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 567 | IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 568 | if (batteryInfoService == null) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 570 | DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 571 | if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return; |
| 572 | |
| 573 | File dumpFile = null; |
| 574 | FileOutputStream dumpStream = null; |
| 575 | try { |
| 576 | // dump the service to a file |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 577 | dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump"); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 578 | dumpStream = new FileOutputStream(dumpFile); |
| 579 | batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS); |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 580 | FileUtils.sync(dumpStream); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 581 | |
| 582 | // add dump file to drop box |
| 583 | db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT); |
| 584 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 585 | Slog.e(TAG, "failed to dump battery service", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 586 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 587 | Slog.e(TAG, "failed to write dumpsys file", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 588 | } finally { |
| 589 | // make sure we clean up |
| 590 | if (dumpStream != null) { |
| 591 | try { |
| 592 | dumpStream.close(); |
| 593 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 594 | Slog.e(TAG, "failed to close dumpsys output stream"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | } |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 596 | } |
| 597 | if (dumpFile != null && !dumpFile.delete()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 598 | Slog.e(TAG, "failed to delete temporary dumpsys file: " |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 599 | + dumpFile.getAbsolutePath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 603 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 604 | private void logOutlierLocked(long duration) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | ContentResolver cr = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 606 | String dischargeThresholdString = Settings.Global.getString(cr, |
| 607 | Settings.Global.BATTERY_DISCHARGE_THRESHOLD); |
| 608 | String durationThresholdString = Settings.Global.getString(cr, |
| 609 | Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | if (dischargeThresholdString != null && durationThresholdString != null) { |
| 612 | try { |
| 613 | long durationThreshold = Long.parseLong(durationThresholdString); |
| 614 | int dischargeThreshold = Integer.parseInt(dischargeThresholdString); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 615 | if (duration <= durationThreshold && |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 616 | mDischargeStartLevel - mBatteryProps.batteryLevel >= dischargeThreshold) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | // 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] | 618 | logBatteryStatsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 620 | if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | " discharge threshold: " + dischargeThreshold); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 622 | if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " + |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 623 | (mDischargeStartLevel - mBatteryProps.batteryLevel)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 625 | Slog.e(TAG, "Invalid DischargeThresholds GService string: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | durationThresholdString + " or " + dischargeThresholdString); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 631 | private int getIconLocked(int level) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 632 | if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 634 | } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | return com.android.internal.R.drawable.stat_sys_battery; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 636 | } else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING |
| 637 | || mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 638 | if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY) |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 639 | && mBatteryProps.batteryLevel >= 100) { |
Joe Onorato | 794be40 | 2010-11-21 19:22:25 -0800 | [diff] [blame] | 640 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
| 641 | } else { |
| 642 | return com.android.internal.R.drawable.stat_sys_battery; |
| 643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | } else { |
| 645 | return com.android.internal.R.drawable.stat_sys_battery_unknown; |
| 646 | } |
| 647 | } |
| 648 | |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 649 | class Shell extends ShellCommand { |
| 650 | @Override |
| 651 | public int onCommand(String cmd) { |
| 652 | return onShellCommand(this, cmd); |
| 653 | } |
| 654 | |
| 655 | @Override |
| 656 | public void onHelp() { |
| 657 | PrintWriter pw = getOutPrintWriter(); |
| 658 | dumpHelp(pw); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | static void dumpHelp(PrintWriter pw) { |
| 663 | pw.println("Battery service (battery) commands:"); |
| 664 | pw.println(" help"); |
| 665 | pw.println(" Print this help text."); |
| 666 | pw.println(" set [ac|usb|wireless|status|level|invalid] <value>"); |
| 667 | pw.println(" Force a battery property value, freezing battery state."); |
| 668 | pw.println(" unplug"); |
| 669 | pw.println(" Force battery unplugged, freezing battery state."); |
| 670 | pw.println(" reset"); |
| 671 | pw.println(" Unfreeze battery state, returning to current hardware values."); |
| 672 | } |
| 673 | |
| 674 | int onShellCommand(Shell shell, String cmd) { |
| 675 | if (cmd == null) { |
| 676 | return shell.handleDefaultCommands(cmd); |
| 677 | } |
| 678 | PrintWriter pw = shell.getOutPrintWriter(); |
| 679 | switch (cmd) { |
| 680 | case "unplug": { |
| 681 | getContext().enforceCallingOrSelfPermission( |
| 682 | android.Manifest.permission.DEVICE_POWER, null); |
| 683 | if (!mUpdatesStopped) { |
| 684 | mLastBatteryProps.set(mBatteryProps); |
| 685 | } |
| 686 | mBatteryProps.chargerAcOnline = false; |
| 687 | mBatteryProps.chargerUsbOnline = false; |
| 688 | mBatteryProps.chargerWirelessOnline = false; |
| 689 | long ident = Binder.clearCallingIdentity(); |
| 690 | try { |
| 691 | mUpdatesStopped = true; |
| 692 | processValuesLocked(false); |
| 693 | } finally { |
| 694 | Binder.restoreCallingIdentity(ident); |
| 695 | } |
| 696 | } break; |
| 697 | case "set": { |
| 698 | getContext().enforceCallingOrSelfPermission( |
| 699 | android.Manifest.permission.DEVICE_POWER, null); |
| 700 | final String key = shell.getNextArg(); |
| 701 | if (key == null) { |
| 702 | pw.println("No property specified"); |
| 703 | return -1; |
| 704 | |
| 705 | } |
| 706 | final String value = shell.getNextArg(); |
| 707 | if (value == null) { |
| 708 | pw.println("No value specified"); |
| 709 | return -1; |
| 710 | |
| 711 | } |
| 712 | try { |
| 713 | if (!mUpdatesStopped) { |
| 714 | mLastBatteryProps.set(mBatteryProps); |
| 715 | } |
| 716 | boolean update = true; |
| 717 | switch (key) { |
| 718 | case "ac": |
| 719 | mBatteryProps.chargerAcOnline = Integer.parseInt(value) != 0; |
| 720 | break; |
| 721 | case "usb": |
| 722 | mBatteryProps.chargerUsbOnline = Integer.parseInt(value) != 0; |
| 723 | break; |
| 724 | case "wireless": |
| 725 | mBatteryProps.chargerWirelessOnline = Integer.parseInt(value) != 0; |
| 726 | break; |
| 727 | case "status": |
| 728 | mBatteryProps.batteryStatus = Integer.parseInt(value); |
| 729 | break; |
| 730 | case "level": |
| 731 | mBatteryProps.batteryLevel = Integer.parseInt(value); |
| 732 | break; |
| 733 | case "invalid": |
| 734 | mInvalidCharger = Integer.parseInt(value); |
| 735 | break; |
| 736 | default: |
| 737 | pw.println("Unknown set option: " + key); |
| 738 | update = false; |
| 739 | break; |
| 740 | } |
| 741 | if (update) { |
| 742 | long ident = Binder.clearCallingIdentity(); |
| 743 | try { |
| 744 | mUpdatesStopped = true; |
| 745 | processValuesLocked(false); |
| 746 | } finally { |
| 747 | Binder.restoreCallingIdentity(ident); |
| 748 | } |
| 749 | } |
| 750 | } catch (NumberFormatException ex) { |
| 751 | pw.println("Bad value: " + value); |
| 752 | return -1; |
| 753 | } |
| 754 | } break; |
| 755 | case "reset": { |
| 756 | getContext().enforceCallingOrSelfPermission( |
| 757 | android.Manifest.permission.DEVICE_POWER, null); |
| 758 | long ident = Binder.clearCallingIdentity(); |
| 759 | try { |
| 760 | if (mUpdatesStopped) { |
| 761 | mUpdatesStopped = false; |
| 762 | mBatteryProps.set(mLastBatteryProps); |
| 763 | processValuesLocked(false); |
| 764 | } |
| 765 | } finally { |
| 766 | Binder.restoreCallingIdentity(ident); |
| 767 | } |
| 768 | } break; |
| 769 | default: |
| 770 | return shell.handleDefaultCommands(cmd); |
| 771 | } |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 776 | synchronized (mLock) { |
| 777 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 778 | pw.println("Current Battery Service state:"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 779 | if (mUpdatesStopped) { |
| 780 | pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); |
| 781 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 782 | pw.println(" AC powered: " + mBatteryProps.chargerAcOnline); |
| 783 | pw.println(" USB powered: " + mBatteryProps.chargerUsbOnline); |
| 784 | pw.println(" Wireless powered: " + mBatteryProps.chargerWirelessOnline); |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 785 | pw.println(" Max charging current: " + mBatteryProps.maxChargingCurrent); |
Badhri Jagan Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 786 | pw.println(" Max charging voltage: " + mBatteryProps.maxChargingVoltage); |
Ruchi Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 787 | pw.println(" Charge counter: " + mBatteryProps.batteryChargeCounter); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 788 | pw.println(" status: " + mBatteryProps.batteryStatus); |
| 789 | pw.println(" health: " + mBatteryProps.batteryHealth); |
| 790 | pw.println(" present: " + mBatteryProps.batteryPresent); |
| 791 | pw.println(" level: " + mBatteryProps.batteryLevel); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 792 | pw.println(" scale: " + BATTERY_SCALE); |
Todd Poynor | df89ca3 | 2013-07-30 20:33:27 -0700 | [diff] [blame] | 793 | pw.println(" voltage: " + mBatteryProps.batteryVoltage); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 794 | pw.println(" temperature: " + mBatteryProps.batteryTemperature); |
| 795 | pw.println(" technology: " + mBatteryProps.batteryTechnology); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 796 | } else { |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 797 | Shell shell = new Shell(); |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 798 | shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null)); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 799 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | } |
| 801 | } |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 802 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 803 | private final class Led { |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 804 | private final Light mBatteryLight; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 805 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 806 | private final int mBatteryLowARGB; |
| 807 | private final int mBatteryMediumARGB; |
| 808 | private final int mBatteryFullARGB; |
| 809 | private final int mBatteryLedOn; |
| 810 | private final int mBatteryLedOff; |
| 811 | |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 812 | public Led(Context context, LightsManager lights) { |
| 813 | mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY); |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 814 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 815 | mBatteryLowARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 816 | com.android.internal.R.integer.config_notificationsBatteryLowARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 817 | mBatteryMediumARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 818 | com.android.internal.R.integer.config_notificationsBatteryMediumARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 819 | mBatteryFullARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 820 | com.android.internal.R.integer.config_notificationsBatteryFullARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 821 | mBatteryLedOn = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 822 | com.android.internal.R.integer.config_notificationsBatteryLedOn); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 823 | mBatteryLedOff = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 824 | com.android.internal.R.integer.config_notificationsBatteryLedOff); |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Synchronize on BatteryService. |
| 829 | */ |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 830 | public void updateLightsLocked() { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 831 | final int level = mBatteryProps.batteryLevel; |
| 832 | final int status = mBatteryProps.batteryStatus; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 833 | if (level < mLowBatteryWarningLevel) { |
| 834 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 835 | // Solid red when battery is charging |
| 836 | mBatteryLight.setColor(mBatteryLowARGB); |
| 837 | } else { |
| 838 | // Flash red when battery is low and not charging |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 839 | mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED, |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 840 | mBatteryLedOn, mBatteryLedOff); |
| 841 | } |
| 842 | } else if (status == BatteryManager.BATTERY_STATUS_CHARGING |
| 843 | || status == BatteryManager.BATTERY_STATUS_FULL) { |
| 844 | if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) { |
| 845 | // Solid green when full or charging and nearly full |
| 846 | mBatteryLight.setColor(mBatteryFullARGB); |
| 847 | } else { |
| 848 | // Solid orange when charging and halfway full |
| 849 | mBatteryLight.setColor(mBatteryMediumARGB); |
| 850 | } |
| 851 | } else { |
| 852 | // No lights if not charging and not low |
| 853 | mBatteryLight.turnOff(); |
| 854 | } |
| 855 | } |
| 856 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 857 | |
| 858 | private final class BatteryListener extends IBatteryPropertiesListener.Stub { |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 859 | @Override public void batteryPropertiesChanged(BatteryProperties props) { |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 860 | final long identity = Binder.clearCallingIdentity(); |
| 861 | try { |
| 862 | BatteryService.this.update(props); |
| 863 | } finally { |
| 864 | Binder.restoreCallingIdentity(identity); |
| 865 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 868 | |
| 869 | private final class BinderService extends Binder { |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 870 | @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 871 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 872 | != PackageManager.PERMISSION_GRANTED) { |
| 873 | |
| 874 | pw.println("Permission Denial: can't dump Battery service from from pid=" |
| 875 | + Binder.getCallingPid() |
| 876 | + ", uid=" + Binder.getCallingUid()); |
| 877 | return; |
| 878 | } |
| 879 | |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 880 | dumpInternal(fd, pw, args); |
| 881 | } |
| 882 | |
| 883 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 884 | FileDescriptor err, String[] args, ShellCallback callback, |
| 885 | ResultReceiver resultReceiver) { |
| 886 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | private final class LocalService extends BatteryManagerInternal { |
| 891 | @Override |
| 892 | public boolean isPowered(int plugTypeSet) { |
| 893 | synchronized (mLock) { |
| 894 | return isPoweredLocked(plugTypeSet); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | @Override |
| 899 | public int getPlugType() { |
| 900 | synchronized (mLock) { |
| 901 | return mPlugType; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public int getBatteryLevel() { |
| 907 | synchronized (mLock) { |
| 908 | return mBatteryProps.batteryLevel; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | @Override |
| 913 | public boolean getBatteryLevelLow() { |
| 914 | synchronized (mLock) { |
| 915 | return mBatteryLevelLow; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | @Override |
| 920 | public int getInvalidCharger() { |
| 921 | synchronized (mLock) { |
| 922 | return mInvalidCharger; |
| 923 | } |
| 924 | } |
| 925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } |