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 | |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 23 | import android.os.PowerManager; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 24 | import android.os.ResultReceiver; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 25 | import android.os.ShellCallback; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 26 | import android.os.ShellCommand; |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 27 | import com.android.internal.annotations.VisibleForTesting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import com.android.internal.app.IBatteryStats; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 29 | import com.android.internal.util.DumpUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import com.android.server.am.BatteryStatsService; |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 31 | import com.android.server.lights.Light; |
| 32 | import com.android.server.lights.LightsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 34 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.ContentResolver; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.pm.PackageManager; |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 39 | import android.hidl.manager.V1_0.IServiceManager; |
| 40 | import android.hidl.manager.V1_0.IServiceNotification; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 41 | import android.hardware.health.V2_0.HealthInfo; |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 42 | import android.hardware.health.V2_0.IHealthInfoCallback; |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 43 | import android.hardware.health.V2_0.IHealth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.os.BatteryManager; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 45 | import android.os.BatteryManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 47 | import android.os.FileUtils; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 48 | import android.os.Handler; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 49 | import android.os.IBatteryPropertiesListener; |
| 50 | import android.os.IBatteryPropertiesRegistrar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.os.IBinder; |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 52 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.RemoteException; |
| 54 | import android.os.ServiceManager; |
| 55 | import android.os.SystemClock; |
| 56 | import android.os.UEventObserver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 57 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.provider.Settings; |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 59 | import android.service.battery.BatteryServiceDumpProto; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 61 | import android.util.Slog; |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 62 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | import java.io.File; |
| 65 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import java.io.FileOutputStream; |
| 67 | import java.io.IOException; |
| 68 | import java.io.PrintWriter; |
| 69 | |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 70 | import java.util.Arrays; |
| 71 | import java.util.List; |
| 72 | import java.util.NoSuchElementException; |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 73 | import java.util.concurrent.atomic.AtomicReference; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * <p>BatteryService monitors the charging status, and charge level of the device |
| 77 | * battery. When these values change this service broadcasts the new values |
| 78 | * to all {@link android.content.BroadcastReceiver IntentReceivers} that are |
| 79 | * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED |
| 80 | * BATTERY_CHANGED} action.</p> |
| 81 | * <p>The new values are stored in the Intent data and can be retrieved by |
| 82 | * calling {@link android.content.Intent#getExtra Intent.getExtra} with the |
| 83 | * following keys:</p> |
| 84 | * <p>"scale" - int, the maximum value for the charge level</p> |
| 85 | * <p>"level" - int, charge level, from 0 through "scale" inclusive</p> |
| 86 | * <p>"status" - String, the current charging status.<br /> |
| 87 | * <p>"health" - String, the current battery health.<br /> |
| 88 | * <p>"present" - boolean, true if the battery is present<br /> |
| 89 | * <p>"icon-small" - int, suggested small icon to use for this state</p> |
| 90 | * <p>"plugged" - int, 0 if the device is not plugged in; 1 if plugged |
| 91 | * into an AC power adapter; 2 if plugged in via USB.</p> |
| 92 | * <p>"voltage" - int, current battery voltage in millivolts</p> |
| 93 | * <p>"temperature" - int, current battery temperature in tenths of |
| 94 | * a degree Centigrade</p> |
| 95 | * <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] | 96 | * |
| 97 | * <p> |
| 98 | * The battery service may be called by the power manager while holding its locks so |
| 99 | * we take care to post all outcalls into the activity manager to a handler. |
| 100 | * |
| 101 | * FIXME: Ideally the power manager would perform all of its calls into the battery |
| 102 | * service asynchronously itself. |
| 103 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | */ |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 105 | public final class BatteryService extends SystemService { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | private static final String TAG = BatteryService.class.getSimpleName(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 107 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 108 | private static final boolean DEBUG = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 109 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 110 | 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] | 111 | |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 112 | private static final long HEALTH_HAL_WAIT_MS = 1000; |
| 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | // Used locally for determining when to make a last ditch effort to log |
| 115 | // discharge stats before the device dies. |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 116 | private int mCriticalBatteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 118 | private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "--unplugged" }; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | private static final String DUMPSYS_DATA_PATH = "/data/system/"; |
| 121 | |
| 122 | // This should probably be exposed in the API, though it's not critical |
| 123 | private static final int BATTERY_PLUGGED_NONE = 0; |
| 124 | |
| 125 | private final Context mContext; |
| 126 | private final IBatteryStats mBatteryStats; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 127 | BinderService mBinderService; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 128 | private final Handler mHandler; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 129 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 130 | private final Object mLock = new Object(); |
| 131 | |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 132 | private HealthInfo mHealthInfo; |
| 133 | private final HealthInfo mLastHealthInfo = new HealthInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | private boolean mBatteryLevelCritical; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | private int mLastBatteryStatus; |
| 136 | private int mLastBatteryHealth; |
| 137 | private boolean mLastBatteryPresent; |
| 138 | private int mLastBatteryLevel; |
| 139 | private int mLastBatteryVoltage; |
| 140 | private int mLastBatteryTemperature; |
| 141 | private boolean mLastBatteryLevelCritical; |
Adrian Roos | 76dc5a5 | 2015-07-21 16:20:36 -0700 | [diff] [blame] | 142 | private int mLastMaxChargingCurrent; |
Badhri Jagan Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 143 | private int mLastMaxChargingVoltage; |
Ruchi Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 144 | private int mLastChargeCounter; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 145 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 146 | private int mSequence = 1; |
| 147 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 148 | private int mInvalidCharger; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 149 | private int mLastInvalidCharger; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 150 | |
| 151 | private int mLowBatteryWarningLevel; |
| 152 | private int mLowBatteryCloseWarningLevel; |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 153 | private int mShutdownBatteryTemperature; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | private int mPlugType; |
| 156 | private int mLastPlugType = -1; // Extra state so we can detect first run |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 157 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 158 | private boolean mBatteryLevelLow; |
| 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | private long mDischargeStartTime; |
| 161 | private int mDischargeStartLevel; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 162 | |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 163 | private boolean mUpdatesStopped; |
| 164 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 165 | private Led mLed; |
| 166 | |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 167 | private boolean mSentLowBatteryBroadcast = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 168 | |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 169 | private ActivityManagerInternal mActivityManagerInternal; |
| 170 | |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 171 | private HealthServiceWrapper mHealthServiceWrapper; |
| 172 | private HealthHalCallback mHealthHalCallback; |
| 173 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 174 | public BatteryService(Context context) { |
| 175 | super(context); |
| 176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | mContext = context; |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 178 | mHandler = new Handler(true /*async*/); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 179 | mLed = new Led(context, getLocalService(LightsManager.class)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | mBatteryStats = BatteryStatsService.getService(); |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 181 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 183 | mCriticalBatteryLevel = mContext.getResources().getInteger( |
| 184 | com.android.internal.R.integer.config_criticalBatteryWarningLevel); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 185 | mLowBatteryWarningLevel = mContext.getResources().getInteger( |
| 186 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 187 | mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger( |
| 188 | com.android.internal.R.integer.config_lowBatteryCloseWarningBump); |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 189 | mShutdownBatteryTemperature = mContext.getResources().getInteger( |
| 190 | com.android.internal.R.integer.config_shutdownBatteryTemperature); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 191 | |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 192 | // watch for invalid charger messages if the invalid_charger switch exists |
| 193 | if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) { |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 194 | UEventObserver invalidChargerObserver = new UEventObserver() { |
| 195 | @Override |
| 196 | public void onUEvent(UEvent event) { |
| 197 | final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0; |
| 198 | synchronized (mLock) { |
| 199 | if (mInvalidCharger != invalidCharger) { |
| 200 | mInvalidCharger = invalidCharger; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | }; |
| 205 | invalidChargerObserver.startObserving( |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 206 | "DEVPATH=/devices/virtual/switch/invalid_charger"); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 207 | } |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 208 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public void onStart() { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 212 | registerHealthCallback(); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 213 | |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 214 | mBinderService = new BinderService(); |
| 215 | publishBinderService("battery", mBinderService); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 216 | publishLocalService(BatteryManagerInternal.class, new LocalService()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public void onBootPhase(int phase) { |
| 221 | if (phase == PHASE_ACTIVITY_MANAGER_READY) { |
| 222 | // check our power situation now that it is safe to display the shutdown dialog. |
| 223 | synchronized (mLock) { |
| 224 | ContentObserver obs = new ContentObserver(mHandler) { |
| 225 | @Override |
| 226 | public void onChange(boolean selfChange) { |
| 227 | synchronized (mLock) { |
| 228 | updateBatteryWarningLevelLocked(); |
| 229 | } |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 230 | } |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 231 | }; |
| 232 | final ContentResolver resolver = mContext.getContentResolver(); |
| 233 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 234 | Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL), |
| 235 | false, obs, UserHandle.USER_ALL); |
| 236 | updateBatteryWarningLevelLocked(); |
| 237 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 238 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 241 | private void registerHealthCallback() { |
| 242 | mHealthServiceWrapper = new HealthServiceWrapper(); |
| 243 | mHealthHalCallback = new HealthHalCallback(); |
| 244 | // IHealth is lazily retrieved. |
| 245 | try { |
| 246 | mHealthServiceWrapper.init(mHealthHalCallback, |
| 247 | new HealthServiceWrapper.IServiceManagerSupplier() {}, |
| 248 | new HealthServiceWrapper.IHealthSupplier() {}); |
| 249 | } catch (RemoteException | NoSuchElementException ex) { |
| 250 | Slog.w(TAG, "health: cannot register callback. " |
| 251 | + "BatteryService will be started with dummy values. Reason: " |
| 252 | + ex.getClass().getSimpleName() + ": " + ex.getMessage()); |
| 253 | update(new HealthInfo()); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | // init register for new service notifications, and IServiceManager should return the |
| 258 | // existing service in a near future. Wait for this.update() to instantiate |
| 259 | // the initial mHealthInfo. |
| 260 | long timeWaited = 0; |
| 261 | synchronized (mLock) { |
| 262 | long beforeWait = SystemClock.uptimeMillis(); |
| 263 | while (mHealthInfo == null && |
| 264 | (timeWaited = SystemClock.uptimeMillis() - beforeWait) < HEALTH_HAL_WAIT_MS) { |
| 265 | try { |
| 266 | mLock.wait(HEALTH_HAL_WAIT_MS - timeWaited); |
| 267 | } catch (InterruptedException ex) { |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | if (mHealthInfo == null) { |
| 272 | Slog.w(TAG, "health: Waited " + timeWaited + "ms for callbacks but received " |
| 273 | + "nothing. BatteryService will be started with dummy values."); |
| 274 | update(new HealthInfo()); |
| 275 | return; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (DEBUG) { |
| 280 | Slog.d(TAG, "health: Waited " + timeWaited + "ms and received the update."); |
| 281 | } |
| 282 | } |
| 283 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 284 | private void updateBatteryWarningLevelLocked() { |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 285 | final ContentResolver resolver = mContext.getContentResolver(); |
| 286 | int defWarnLevel = mContext.getResources().getInteger( |
| 287 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
| 288 | mLowBatteryWarningLevel = Settings.Global.getInt(resolver, |
| 289 | Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, defWarnLevel); |
| 290 | if (mLowBatteryWarningLevel == 0) { |
| 291 | mLowBatteryWarningLevel = defWarnLevel; |
| 292 | } |
| 293 | if (mLowBatteryWarningLevel < mCriticalBatteryLevel) { |
| 294 | mLowBatteryWarningLevel = mCriticalBatteryLevel; |
| 295 | } |
| 296 | mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger( |
| 297 | com.android.internal.R.integer.config_lowBatteryCloseWarningBump); |
| 298 | processValuesLocked(true); |
| 299 | } |
| 300 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 301 | private boolean isPoweredLocked(int plugTypeSet) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | // assume we are powered if battery state is unknown so |
| 303 | // the "stay on while plugged in" option will work. |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 304 | if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | return true; |
| 306 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 307 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mHealthInfo.legacy.chargerAcOnline) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 308 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 310 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mHealthInfo.legacy.chargerUsbOnline) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 311 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 313 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mHealthInfo.legacy.chargerWirelessOnline) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 314 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 316 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 319 | private boolean shouldSendBatteryLowLocked() { |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 320 | final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE; |
| 321 | final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE; |
| 322 | |
| 323 | /* The ACTION_BATTERY_LOW broadcast is sent in these situations: |
| 324 | * - is just un-plugged (previously was plugged) and battery level is |
| 325 | * less than or equal to WARNING, or |
| 326 | * - is not plugged and battery level falls to WARNING boundary |
| 327 | * (becomes <= mLowBatteryWarningLevel). |
| 328 | */ |
| 329 | return !plugged |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 330 | && mHealthInfo.legacy.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN |
| 331 | && mHealthInfo.legacy.batteryLevel <= mLowBatteryWarningLevel |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 332 | && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel); |
| 333 | } |
| 334 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 335 | private void shutdownIfNoPowerLocked() { |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 336 | // shut down gracefully if our battery is critically low and we are not powered. |
| 337 | // wait until the system has booted before attempting to display the shutdown dialog. |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 338 | if (mHealthInfo.legacy.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 339 | mHandler.post(new Runnable() { |
| 340 | @Override |
| 341 | public void run() { |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 342 | if (mActivityManagerInternal.isSystemReady()) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 343 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 344 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 345 | intent.putExtra(Intent.EXTRA_REASON, |
| 346 | PowerManager.SHUTDOWN_LOW_BATTERY); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 347 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 348 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 349 | } |
| 350 | } |
| 351 | }); |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 355 | private void shutdownIfOverTempLocked() { |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 356 | // shut down gracefully if temperature is too high (> 68.0C by default) |
| 357 | // wait until the system has booted before attempting to display the |
| 358 | // shutdown dialog. |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 359 | if (mHealthInfo.legacy.batteryTemperature > mShutdownBatteryTemperature) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 360 | mHandler.post(new Runnable() { |
| 361 | @Override |
| 362 | public void run() { |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 363 | if (mActivityManagerInternal.isSystemReady()) { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 364 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 365 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 366 | intent.putExtra(Intent.EXTRA_REASON, |
| 367 | PowerManager.SHUTDOWN_BATTERY_THERMAL_STATE); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 368 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 369 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 370 | } |
| 371 | } |
| 372 | }); |
Eric Olsen | 6a362a9 | 2010-03-26 15:38:41 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 376 | private void update(HealthInfo info) { |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 377 | synchronized (mLock) { |
| 378 | if (!mUpdatesStopped) { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 379 | mHealthInfo = info; |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 380 | // Process the new values. |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 381 | processValuesLocked(false); |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 382 | mLock.notifyAll(); // for any waiters on new info |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 383 | } else { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 384 | copy(mLastHealthInfo, info); |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 385 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 386 | } |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 389 | private static void copy(HealthInfo dst, HealthInfo src) { |
| 390 | dst.legacy.chargerAcOnline = src.legacy.chargerAcOnline; |
| 391 | dst.legacy.chargerUsbOnline = src.legacy.chargerUsbOnline; |
| 392 | dst.legacy.chargerWirelessOnline = src.legacy.chargerWirelessOnline; |
| 393 | dst.legacy.maxChargingCurrent = src.legacy.maxChargingCurrent; |
| 394 | dst.legacy.maxChargingVoltage = src.legacy.maxChargingVoltage; |
| 395 | dst.legacy.batteryStatus = src.legacy.batteryStatus; |
| 396 | dst.legacy.batteryHealth = src.legacy.batteryHealth; |
| 397 | dst.legacy.batteryPresent = src.legacy.batteryPresent; |
| 398 | dst.legacy.batteryLevel = src.legacy.batteryLevel; |
| 399 | dst.legacy.batteryVoltage = src.legacy.batteryVoltage; |
| 400 | dst.legacy.batteryTemperature = src.legacy.batteryTemperature; |
| 401 | dst.legacy.batteryCurrent = src.legacy.batteryCurrent; |
| 402 | dst.legacy.batteryCycleCount = src.legacy.batteryCycleCount; |
| 403 | dst.legacy.batteryFullCharge = src.legacy.batteryFullCharge; |
| 404 | dst.legacy.batteryChargeCounter = src.legacy.batteryChargeCounter; |
| 405 | dst.legacy.batteryTechnology = src.legacy.batteryTechnology; |
| 406 | dst.batteryCurrentAverage = src.batteryCurrentAverage; |
| 407 | dst.batteryCapacity = src.batteryCapacity; |
| 408 | dst.energyCounter = src.energyCounter; |
| 409 | } |
| 410 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 411 | private void processValuesLocked(boolean force) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 412 | boolean logOutlier = false; |
| 413 | long dischargeDuration = 0; |
Joe Onorato | a7e4cf9b | 2009-07-28 18:18:20 -0700 | [diff] [blame] | 414 | |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 415 | mBatteryLevelCritical = (mHealthInfo.legacy.batteryLevel <= mCriticalBatteryLevel); |
| 416 | if (mHealthInfo.legacy.chargerAcOnline) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | mPlugType = BatteryManager.BATTERY_PLUGGED_AC; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 418 | } else if (mHealthInfo.legacy.chargerUsbOnline) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | mPlugType = BatteryManager.BATTERY_PLUGGED_USB; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 420 | } else if (mHealthInfo.legacy.chargerWirelessOnline) { |
Brian Muramatsu | 37a37f4 | 2012-08-14 15:21:02 -0700 | [diff] [blame] | 421 | mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } else { |
| 423 | mPlugType = BATTERY_PLUGGED_NONE; |
| 424 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 425 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 426 | if (DEBUG) { |
| 427 | Slog.d(TAG, "Processing new values: " |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 428 | + "info=" + mHealthInfo |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 429 | + ", mBatteryLevelCritical=" + mBatteryLevelCritical |
| 430 | + ", mPlugType=" + mPlugType); |
| 431 | } |
| 432 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 433 | // Let the battery stats keep track of the current level. |
| 434 | try { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 435 | mBatteryStats.setBatteryState(mHealthInfo.legacy.batteryStatus, mHealthInfo.legacy.batteryHealth, |
| 436 | mPlugType, mHealthInfo.legacy.batteryLevel, mHealthInfo.legacy.batteryTemperature, |
| 437 | mHealthInfo.legacy.batteryVoltage, mHealthInfo.legacy.batteryChargeCounter, |
| 438 | mHealthInfo.legacy.batteryFullCharge); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 439 | } catch (RemoteException e) { |
| 440 | // Should never happen. |
| 441 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 442 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 443 | shutdownIfNoPowerLocked(); |
| 444 | shutdownIfOverTempLocked(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 445 | |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 446 | if (force || (mHealthInfo.legacy.batteryStatus != mLastBatteryStatus || |
| 447 | mHealthInfo.legacy.batteryHealth != mLastBatteryHealth || |
| 448 | mHealthInfo.legacy.batteryPresent != mLastBatteryPresent || |
| 449 | mHealthInfo.legacy.batteryLevel != mLastBatteryLevel || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | mPlugType != mLastPlugType || |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 451 | mHealthInfo.legacy.batteryVoltage != mLastBatteryVoltage || |
| 452 | mHealthInfo.legacy.batteryTemperature != mLastBatteryTemperature || |
| 453 | mHealthInfo.legacy.maxChargingCurrent != mLastMaxChargingCurrent || |
| 454 | mHealthInfo.legacy.maxChargingVoltage != mLastMaxChargingVoltage || |
| 455 | mHealthInfo.legacy.batteryChargeCounter != mLastChargeCounter || |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 456 | mInvalidCharger != mLastInvalidCharger)) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 457 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | if (mPlugType != mLastPlugType) { |
| 459 | if (mLastPlugType == BATTERY_PLUGGED_NONE) { |
| 460 | // discharging -> charging |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 461 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | // There's no value in this data unless we've discharged at least once and the |
| 463 | // battery level has changed; so don't log until it does. |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 464 | if (mDischargeStartTime != 0 && mDischargeStartLevel != mHealthInfo.legacy.batteryLevel) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 465 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 466 | logOutlier = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 467 | EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration, |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 468 | mDischargeStartLevel, mHealthInfo.legacy.batteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | // make sure we see a discharge event before logging again |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 470 | mDischargeStartTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } |
| 472 | } else if (mPlugType == BATTERY_PLUGGED_NONE) { |
| 473 | // charging -> discharging or we just powered up |
| 474 | mDischargeStartTime = SystemClock.elapsedRealtime(); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 475 | mDischargeStartLevel = mHealthInfo.legacy.batteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 478 | if (mHealthInfo.legacy.batteryStatus != mLastBatteryStatus || |
| 479 | mHealthInfo.legacy.batteryHealth != mLastBatteryHealth || |
| 480 | mHealthInfo.legacy.batteryPresent != mLastBatteryPresent || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | mPlugType != mLastPlugType) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 482 | EventLog.writeEvent(EventLogTags.BATTERY_STATUS, |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 483 | mHealthInfo.legacy.batteryStatus, mHealthInfo.legacy.batteryHealth, mHealthInfo.legacy.batteryPresent ? 1 : 0, |
| 484 | mPlugType, mHealthInfo.legacy.batteryTechnology); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 486 | if (mHealthInfo.legacy.batteryLevel != mLastBatteryLevel) { |
Dianne Hackborn | cf117164 | 2013-07-12 17:26:02 -0700 | [diff] [blame] | 487 | // Don't do this just from voltage or temperature changes, that is |
| 488 | // too noisy. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 489 | EventLog.writeEvent(EventLogTags.BATTERY_LEVEL, |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 490 | mHealthInfo.legacy.batteryLevel, mHealthInfo.legacy.batteryVoltage, mHealthInfo.legacy.batteryTemperature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | } |
| 492 | if (mBatteryLevelCritical && !mLastBatteryLevelCritical && |
| 493 | mPlugType == BATTERY_PLUGGED_NONE) { |
| 494 | // We want to make sure we log discharge cycle outliers |
| 495 | // if the battery is about to die. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 496 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 497 | logOutlier = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 499 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 500 | if (!mBatteryLevelLow) { |
| 501 | // Should we now switch in to low battery mode? |
| 502 | if (mPlugType == BATTERY_PLUGGED_NONE |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 503 | && mHealthInfo.legacy.batteryLevel <= mLowBatteryWarningLevel) { |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 504 | mBatteryLevelLow = true; |
| 505 | } |
| 506 | } else { |
| 507 | // Should we now switch out of low battery mode? |
| 508 | if (mPlugType != BATTERY_PLUGGED_NONE) { |
| 509 | mBatteryLevelLow = false; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 510 | } else if (mHealthInfo.legacy.batteryLevel >= mLowBatteryCloseWarningLevel) { |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 511 | mBatteryLevelLow = false; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 512 | } else if (force && mHealthInfo.legacy.batteryLevel >= mLowBatteryWarningLevel) { |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 513 | // If being forced, the previous state doesn't matter, we will just |
| 514 | // absolutely check to see if we are now above the warning level. |
| 515 | mBatteryLevelLow = false; |
| 516 | } |
| 517 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 518 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 519 | mSequence++; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 520 | |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 521 | // Separate broadcast is sent for power connected / not connected |
| 522 | // since the standard intent will not wake any applications and some |
| 523 | // applications may want to have smart behavior based on this. |
| 524 | if (mPlugType != 0 && mLastPlugType == 0) { |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 525 | final Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED); |
| 526 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 527 | statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 528 | mHandler.post(new Runnable() { |
| 529 | @Override |
| 530 | public void run() { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 531 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 532 | } |
| 533 | }); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 534 | } |
| 535 | else if (mPlugType == 0 && mLastPlugType != 0) { |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 536 | final Intent statusIntent = new Intent(Intent.ACTION_POWER_DISCONNECTED); |
| 537 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 538 | statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 539 | mHandler.post(new Runnable() { |
| 540 | @Override |
| 541 | public void run() { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 542 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 543 | } |
| 544 | }); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 545 | } |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 546 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 547 | if (shouldSendBatteryLowLocked()) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 548 | mSentLowBatteryBroadcast = true; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 549 | final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW); |
| 550 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 551 | statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 552 | mHandler.post(new Runnable() { |
| 553 | @Override |
| 554 | public void run() { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 555 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 556 | } |
| 557 | }); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 558 | } else if (mSentLowBatteryBroadcast && |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 559 | mHealthInfo.legacy.batteryLevel >= mLowBatteryCloseWarningLevel) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 560 | mSentLowBatteryBroadcast = false; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 561 | final Intent statusIntent = new Intent(Intent.ACTION_BATTERY_OKAY); |
| 562 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 563 | statusIntent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 564 | mHandler.post(new Runnable() { |
| 565 | @Override |
| 566 | public void run() { |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 567 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
| 568 | } |
| 569 | }); |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 570 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 571 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 572 | // We are doing this after sending the above broadcasts, so anything processing |
| 573 | // them will get the new sequence number at that point. (See for example how testing |
| 574 | // of JobScheduler's BatteryController works.) |
| 575 | sendIntentLocked(); |
| 576 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 577 | // Update the battery LED |
| 578 | mLed.updateLightsLocked(); |
| 579 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 580 | // This needs to be done after sendIntent() so that we get the lastest battery stats. |
| 581 | if (logOutlier && dischargeDuration != 0) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 582 | logOutlierLocked(dischargeDuration); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 583 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 584 | |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 585 | mLastBatteryStatus = mHealthInfo.legacy.batteryStatus; |
| 586 | mLastBatteryHealth = mHealthInfo.legacy.batteryHealth; |
| 587 | mLastBatteryPresent = mHealthInfo.legacy.batteryPresent; |
| 588 | mLastBatteryLevel = mHealthInfo.legacy.batteryLevel; |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 589 | mLastPlugType = mPlugType; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 590 | mLastBatteryVoltage = mHealthInfo.legacy.batteryVoltage; |
| 591 | mLastBatteryTemperature = mHealthInfo.legacy.batteryTemperature; |
| 592 | mLastMaxChargingCurrent = mHealthInfo.legacy.maxChargingCurrent; |
| 593 | mLastMaxChargingVoltage = mHealthInfo.legacy.maxChargingVoltage; |
| 594 | mLastChargeCounter = mHealthInfo.legacy.batteryChargeCounter; |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 595 | mLastBatteryLevelCritical = mBatteryLevelCritical; |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 596 | mLastInvalidCharger = mInvalidCharger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 600 | private void sendIntentLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | // Pack up the values and broadcast them to everyone |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 602 | final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 603 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 604 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 605 | |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 606 | int icon = getIconLocked(mHealthInfo.legacy.batteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 608 | intent.putExtra(BatteryManager.EXTRA_SEQUENCE, mSequence); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 609 | intent.putExtra(BatteryManager.EXTRA_STATUS, mHealthInfo.legacy.batteryStatus); |
| 610 | intent.putExtra(BatteryManager.EXTRA_HEALTH, mHealthInfo.legacy.batteryHealth); |
| 611 | intent.putExtra(BatteryManager.EXTRA_PRESENT, mHealthInfo.legacy.batteryPresent); |
| 612 | intent.putExtra(BatteryManager.EXTRA_LEVEL, mHealthInfo.legacy.batteryLevel); |
Dianne Hackborn | edd9316 | 2009-09-19 14:03:05 -0700 | [diff] [blame] | 613 | intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE); |
| 614 | intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon); |
| 615 | intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 616 | intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mHealthInfo.legacy.batteryVoltage); |
| 617 | intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mHealthInfo.legacy.batteryTemperature); |
| 618 | intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mHealthInfo.legacy.batteryTechnology); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 619 | intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 620 | intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mHealthInfo.legacy.maxChargingCurrent); |
| 621 | intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mHealthInfo.legacy.maxChargingVoltage); |
| 622 | intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mHealthInfo.legacy.batteryChargeCounter); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 623 | if (DEBUG) { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 624 | Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. scale:" + BATTERY_SCALE |
| 625 | + ", info:" + mHealthInfo.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 628 | mHandler.post(new Runnable() { |
| 629 | @Override |
| 630 | public void run() { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 631 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Jeff Brown | 605ea69 | 2012-10-05 16:33:10 -0700 | [diff] [blame] | 632 | } |
| 633 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 636 | private void logBatteryStatsLocked() { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 637 | IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 638 | if (batteryInfoService == null) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 640 | DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 641 | if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return; |
| 642 | |
| 643 | File dumpFile = null; |
| 644 | FileOutputStream dumpStream = null; |
| 645 | try { |
| 646 | // dump the service to a file |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 647 | dumpFile = new File(DUMPSYS_DATA_PATH + BatteryStats.SERVICE_NAME + ".dump"); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 648 | dumpStream = new FileOutputStream(dumpFile); |
| 649 | batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS); |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 650 | FileUtils.sync(dumpStream); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 651 | |
| 652 | // add dump file to drop box |
| 653 | db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT); |
| 654 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 655 | Slog.e(TAG, "failed to dump battery service", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 656 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 657 | Slog.e(TAG, "failed to write dumpsys file", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 658 | } finally { |
| 659 | // make sure we clean up |
| 660 | if (dumpStream != null) { |
| 661 | try { |
| 662 | dumpStream.close(); |
| 663 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 664 | Slog.e(TAG, "failed to close dumpsys output stream"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | } |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 666 | } |
| 667 | if (dumpFile != null && !dumpFile.delete()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 668 | Slog.e(TAG, "failed to delete temporary dumpsys file: " |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 669 | + dumpFile.getAbsolutePath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 673 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 674 | private void logOutlierLocked(long duration) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | ContentResolver cr = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 676 | String dischargeThresholdString = Settings.Global.getString(cr, |
| 677 | Settings.Global.BATTERY_DISCHARGE_THRESHOLD); |
| 678 | String durationThresholdString = Settings.Global.getString(cr, |
| 679 | Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | if (dischargeThresholdString != null && durationThresholdString != null) { |
| 682 | try { |
| 683 | long durationThreshold = Long.parseLong(durationThresholdString); |
| 684 | int dischargeThreshold = Integer.parseInt(dischargeThresholdString); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 685 | if (duration <= durationThreshold && |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 686 | mDischargeStartLevel - mHealthInfo.legacy.batteryLevel >= dischargeThreshold) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | // 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] | 688 | logBatteryStatsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 690 | if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | " discharge threshold: " + dischargeThreshold); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 692 | if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " + |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 693 | (mDischargeStartLevel - mHealthInfo.legacy.batteryLevel)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 695 | Slog.e(TAG, "Invalid DischargeThresholds GService string: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | durationThresholdString + " or " + dischargeThresholdString); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 701 | private int getIconLocked(int level) { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 702 | if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 704 | } else if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | return com.android.internal.R.drawable.stat_sys_battery; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 706 | } else if (mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING |
| 707 | || mHealthInfo.legacy.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 708 | if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY) |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 709 | && mHealthInfo.legacy.batteryLevel >= 100) { |
Joe Onorato | 794be40 | 2010-11-21 19:22:25 -0800 | [diff] [blame] | 710 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
| 711 | } else { |
| 712 | return com.android.internal.R.drawable.stat_sys_battery; |
| 713 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } else { |
| 715 | return com.android.internal.R.drawable.stat_sys_battery_unknown; |
| 716 | } |
| 717 | } |
| 718 | |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 719 | class Shell extends ShellCommand { |
| 720 | @Override |
| 721 | public int onCommand(String cmd) { |
| 722 | return onShellCommand(this, cmd); |
| 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public void onHelp() { |
| 727 | PrintWriter pw = getOutPrintWriter(); |
| 728 | dumpHelp(pw); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | static void dumpHelp(PrintWriter pw) { |
| 733 | pw.println("Battery service (battery) commands:"); |
| 734 | pw.println(" help"); |
| 735 | pw.println(" Print this help text."); |
Adam Lesinski | 29ddfe5 | 2017-03-29 19:29:00 -0700 | [diff] [blame] | 736 | pw.println(" set [-f] [ac|usb|wireless|status|level|temp|present|invalid] <value>"); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 737 | pw.println(" Force a battery property value, freezing battery state."); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 738 | pw.println(" -f: force a battery change broadcast be sent, prints new sequence."); |
| 739 | pw.println(" unplug [-f]"); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 740 | pw.println(" Force battery unplugged, freezing battery state."); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 741 | pw.println(" -f: force a battery change broadcast be sent, prints new sequence."); |
| 742 | pw.println(" reset [-f]"); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 743 | pw.println(" Unfreeze battery state, returning to current hardware values."); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 744 | pw.println(" -f: force a battery change broadcast be sent, prints new sequence."); |
| 745 | } |
| 746 | |
| 747 | static final int OPTION_FORCE_UPDATE = 1<<0; |
| 748 | |
| 749 | int parseOptions(Shell shell) { |
| 750 | String opt; |
| 751 | int opts = 0; |
| 752 | while ((opt = shell.getNextOption()) != null) { |
| 753 | if ("-f".equals(opt)) { |
| 754 | opts |= OPTION_FORCE_UPDATE; |
| 755 | } |
| 756 | } |
| 757 | return opts; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | int onShellCommand(Shell shell, String cmd) { |
| 761 | if (cmd == null) { |
| 762 | return shell.handleDefaultCommands(cmd); |
| 763 | } |
| 764 | PrintWriter pw = shell.getOutPrintWriter(); |
| 765 | switch (cmd) { |
| 766 | case "unplug": { |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 767 | int opts = parseOptions(shell); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 768 | getContext().enforceCallingOrSelfPermission( |
| 769 | android.Manifest.permission.DEVICE_POWER, null); |
| 770 | if (!mUpdatesStopped) { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 771 | copy(mLastHealthInfo, mHealthInfo); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 772 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 773 | mHealthInfo.legacy.chargerAcOnline = false; |
| 774 | mHealthInfo.legacy.chargerUsbOnline = false; |
| 775 | mHealthInfo.legacy.chargerWirelessOnline = false; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 776 | long ident = Binder.clearCallingIdentity(); |
| 777 | try { |
| 778 | mUpdatesStopped = true; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 779 | processValuesFromShellLocked(pw, opts); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 780 | } finally { |
| 781 | Binder.restoreCallingIdentity(ident); |
| 782 | } |
| 783 | } break; |
| 784 | case "set": { |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 785 | int opts = parseOptions(shell); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 786 | getContext().enforceCallingOrSelfPermission( |
| 787 | android.Manifest.permission.DEVICE_POWER, null); |
| 788 | final String key = shell.getNextArg(); |
| 789 | if (key == null) { |
| 790 | pw.println("No property specified"); |
| 791 | return -1; |
| 792 | |
| 793 | } |
| 794 | final String value = shell.getNextArg(); |
| 795 | if (value == null) { |
| 796 | pw.println("No value specified"); |
| 797 | return -1; |
| 798 | |
| 799 | } |
| 800 | try { |
| 801 | if (!mUpdatesStopped) { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 802 | copy(mLastHealthInfo, mHealthInfo); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 803 | } |
| 804 | boolean update = true; |
| 805 | switch (key) { |
Christopher Tate | 630d98b | 2017-03-07 14:12:26 -0800 | [diff] [blame] | 806 | case "present": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 807 | mHealthInfo.legacy.batteryPresent = Integer.parseInt(value) != 0; |
Christopher Tate | 630d98b | 2017-03-07 14:12:26 -0800 | [diff] [blame] | 808 | break; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 809 | case "ac": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 810 | mHealthInfo.legacy.chargerAcOnline = Integer.parseInt(value) != 0; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 811 | break; |
| 812 | case "usb": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 813 | mHealthInfo.legacy.chargerUsbOnline = Integer.parseInt(value) != 0; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 814 | break; |
| 815 | case "wireless": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 816 | mHealthInfo.legacy.chargerWirelessOnline = Integer.parseInt(value) != 0; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 817 | break; |
| 818 | case "status": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 819 | mHealthInfo.legacy.batteryStatus = Integer.parseInt(value); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 820 | break; |
| 821 | case "level": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 822 | mHealthInfo.legacy.batteryLevel = Integer.parseInt(value); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 823 | break; |
Adam Lesinski | 29ddfe5 | 2017-03-29 19:29:00 -0700 | [diff] [blame] | 824 | case "temp": |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 825 | mHealthInfo.legacy.batteryTemperature = Integer.parseInt(value); |
Adam Lesinski | 29ddfe5 | 2017-03-29 19:29:00 -0700 | [diff] [blame] | 826 | break; |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 827 | case "invalid": |
| 828 | mInvalidCharger = Integer.parseInt(value); |
| 829 | break; |
| 830 | default: |
| 831 | pw.println("Unknown set option: " + key); |
| 832 | update = false; |
| 833 | break; |
| 834 | } |
| 835 | if (update) { |
| 836 | long ident = Binder.clearCallingIdentity(); |
| 837 | try { |
| 838 | mUpdatesStopped = true; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 839 | processValuesFromShellLocked(pw, opts); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 840 | } finally { |
| 841 | Binder.restoreCallingIdentity(ident); |
| 842 | } |
| 843 | } |
| 844 | } catch (NumberFormatException ex) { |
| 845 | pw.println("Bad value: " + value); |
| 846 | return -1; |
| 847 | } |
| 848 | } break; |
| 849 | case "reset": { |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 850 | int opts = parseOptions(shell); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 851 | getContext().enforceCallingOrSelfPermission( |
| 852 | android.Manifest.permission.DEVICE_POWER, null); |
| 853 | long ident = Binder.clearCallingIdentity(); |
| 854 | try { |
| 855 | if (mUpdatesStopped) { |
| 856 | mUpdatesStopped = false; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 857 | copy(mHealthInfo, mLastHealthInfo); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 858 | processValuesFromShellLocked(pw, opts); |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 859 | } |
| 860 | } finally { |
| 861 | Binder.restoreCallingIdentity(ident); |
| 862 | } |
| 863 | } break; |
| 864 | default: |
| 865 | return shell.handleDefaultCommands(cmd); |
| 866 | } |
| 867 | return 0; |
| 868 | } |
| 869 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 870 | private void processValuesFromShellLocked(PrintWriter pw, int opts) { |
| 871 | processValuesLocked((opts & OPTION_FORCE_UPDATE) != 0); |
| 872 | if ((opts & OPTION_FORCE_UPDATE) != 0) { |
| 873 | pw.println(mSequence); |
| 874 | } |
| 875 | } |
| 876 | |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 877 | private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 878 | synchronized (mLock) { |
| 879 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 880 | pw.println("Current Battery Service state:"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 881 | if (mUpdatesStopped) { |
| 882 | pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); |
| 883 | } |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 884 | pw.println(" AC powered: " + mHealthInfo.legacy.chargerAcOnline); |
| 885 | pw.println(" USB powered: " + mHealthInfo.legacy.chargerUsbOnline); |
| 886 | pw.println(" Wireless powered: " + mHealthInfo.legacy.chargerWirelessOnline); |
| 887 | pw.println(" Max charging current: " + mHealthInfo.legacy.maxChargingCurrent); |
| 888 | pw.println(" Max charging voltage: " + mHealthInfo.legacy.maxChargingVoltage); |
| 889 | pw.println(" Charge counter: " + mHealthInfo.legacy.batteryChargeCounter); |
| 890 | pw.println(" status: " + mHealthInfo.legacy.batteryStatus); |
| 891 | pw.println(" health: " + mHealthInfo.legacy.batteryHealth); |
| 892 | pw.println(" present: " + mHealthInfo.legacy.batteryPresent); |
| 893 | pw.println(" level: " + mHealthInfo.legacy.batteryLevel); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 894 | pw.println(" scale: " + BATTERY_SCALE); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 895 | pw.println(" voltage: " + mHealthInfo.legacy.batteryVoltage); |
| 896 | pw.println(" temperature: " + mHealthInfo.legacy.batteryTemperature); |
| 897 | pw.println(" technology: " + mHealthInfo.legacy.batteryTechnology); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 898 | } else { |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 899 | Shell shell = new Shell(); |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 900 | shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null)); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | } |
| 903 | } |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 904 | |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 905 | private void dumpProto(FileDescriptor fd) { |
| 906 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 907 | |
| 908 | synchronized (mLock) { |
| 909 | proto.write(BatteryServiceDumpProto.ARE_UPDATES_STOPPED, mUpdatesStopped); |
| 910 | int batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_NONE; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 911 | if (mHealthInfo.legacy.chargerAcOnline) { |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 912 | batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_AC; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 913 | } else if (mHealthInfo.legacy.chargerUsbOnline) { |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 914 | batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_USB; |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 915 | } else if (mHealthInfo.legacy.chargerWirelessOnline) { |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 916 | batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_WIRELESS; |
| 917 | } |
| 918 | proto.write(BatteryServiceDumpProto.PLUGGED, batteryPluggedValue); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 919 | proto.write(BatteryServiceDumpProto.MAX_CHARGING_CURRENT, mHealthInfo.legacy.maxChargingCurrent); |
| 920 | proto.write(BatteryServiceDumpProto.MAX_CHARGING_VOLTAGE, mHealthInfo.legacy.maxChargingVoltage); |
| 921 | proto.write(BatteryServiceDumpProto.CHARGE_COUNTER, mHealthInfo.legacy.batteryChargeCounter); |
| 922 | proto.write(BatteryServiceDumpProto.STATUS, mHealthInfo.legacy.batteryStatus); |
| 923 | proto.write(BatteryServiceDumpProto.HEALTH, mHealthInfo.legacy.batteryHealth); |
| 924 | proto.write(BatteryServiceDumpProto.IS_PRESENT, mHealthInfo.legacy.batteryPresent); |
| 925 | proto.write(BatteryServiceDumpProto.LEVEL, mHealthInfo.legacy.batteryLevel); |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 926 | proto.write(BatteryServiceDumpProto.SCALE, BATTERY_SCALE); |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 927 | proto.write(BatteryServiceDumpProto.VOLTAGE, mHealthInfo.legacy.batteryVoltage); |
| 928 | proto.write(BatteryServiceDumpProto.TEMPERATURE, mHealthInfo.legacy.batteryTemperature); |
| 929 | proto.write(BatteryServiceDumpProto.TECHNOLOGY, mHealthInfo.legacy.batteryTechnology); |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 930 | } |
| 931 | proto.flush(); |
| 932 | } |
| 933 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 934 | private final class Led { |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 935 | private final Light mBatteryLight; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 936 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 937 | private final int mBatteryLowARGB; |
| 938 | private final int mBatteryMediumARGB; |
| 939 | private final int mBatteryFullARGB; |
| 940 | private final int mBatteryLedOn; |
| 941 | private final int mBatteryLedOff; |
| 942 | |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 943 | public Led(Context context, LightsManager lights) { |
| 944 | mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY); |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 945 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 946 | mBatteryLowARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 947 | com.android.internal.R.integer.config_notificationsBatteryLowARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 948 | mBatteryMediumARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 949 | com.android.internal.R.integer.config_notificationsBatteryMediumARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 950 | mBatteryFullARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 951 | com.android.internal.R.integer.config_notificationsBatteryFullARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 952 | mBatteryLedOn = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 953 | com.android.internal.R.integer.config_notificationsBatteryLedOn); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 954 | mBatteryLedOff = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 955 | com.android.internal.R.integer.config_notificationsBatteryLedOff); |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Synchronize on BatteryService. |
| 960 | */ |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 961 | public void updateLightsLocked() { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 962 | final int level = mHealthInfo.legacy.batteryLevel; |
| 963 | final int status = mHealthInfo.legacy.batteryStatus; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 964 | if (level < mLowBatteryWarningLevel) { |
| 965 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 966 | // Solid red when battery is charging |
| 967 | mBatteryLight.setColor(mBatteryLowARGB); |
| 968 | } else { |
| 969 | // Flash red when battery is low and not charging |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 970 | mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED, |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 971 | mBatteryLedOn, mBatteryLedOff); |
| 972 | } |
| 973 | } else if (status == BatteryManager.BATTERY_STATUS_CHARGING |
| 974 | || status == BatteryManager.BATTERY_STATUS_FULL) { |
| 975 | if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) { |
| 976 | // Solid green when full or charging and nearly full |
| 977 | mBatteryLight.setColor(mBatteryFullARGB); |
| 978 | } else { |
| 979 | // Solid orange when charging and halfway full |
| 980 | mBatteryLight.setColor(mBatteryMediumARGB); |
| 981 | } |
| 982 | } else { |
| 983 | // No lights if not charging and not low |
| 984 | mBatteryLight.turnOff(); |
| 985 | } |
| 986 | } |
| 987 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 988 | |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 989 | private final class HealthHalCallback extends IHealthInfoCallback.Stub |
| 990 | implements HealthServiceWrapper.Callback { |
| 991 | @Override public void healthInfoChanged(HealthInfo props) { |
| 992 | BatteryService.this.update(props); |
| 993 | } |
| 994 | // on new service registered |
| 995 | @Override public void onRegistration(IHealth oldService, IHealth newService, |
| 996 | String instance) { |
| 997 | if (newService == null) return; |
| 998 | |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 999 | try { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1000 | if (oldService != null) { |
| 1001 | int r = oldService.unregisterCallback(this); |
| 1002 | if (r != Result.SUCCESS) { |
| 1003 | Slog.w(TAG, "health: cannot unregister previous callback: " + |
| 1004 | Result.toString(r)); |
| 1005 | } |
| 1006 | } |
| 1007 | } catch (RemoteException ex) { |
| 1008 | Slog.w(TAG, "health: cannot unregister previous callback (transaction error): " |
| 1009 | + ex.getMessage()); |
Adam Lesinski | ef2ea1f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1010 | } |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1011 | |
| 1012 | try { |
| 1013 | int r = newService.registerCallback(this); |
| 1014 | if (r != Result.SUCCESS) { |
| 1015 | Slog.w(TAG, "health: cannot register callback: " + Result.toString(r)); |
| 1016 | return; |
| 1017 | } |
| 1018 | // registerCallback does NOT guarantee that update is called |
| 1019 | // immediately, so request a manual update here. |
| 1020 | newService.update(); |
| 1021 | } catch (RemoteException ex) { |
| 1022 | Slog.e(TAG, "health: cannot register callback (transaction error): " |
| 1023 | + ex.getMessage()); |
| 1024 | } |
| 1025 | } |
Todd Poynor | 26faecc | 2013-05-22 18:54:48 -0700 | [diff] [blame] | 1026 | } |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | private final class BinderService extends Binder { |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 1029 | @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1030 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1031 | |
Netta P | e2a3cd8 | 2017-01-26 18:03:51 -0800 | [diff] [blame] | 1032 | if (args.length > 0 && "--proto".equals(args[0])) { |
| 1033 | dumpProto(fd); |
| 1034 | } else { |
| 1035 | dumpInternal(fd, pw, args); |
| 1036 | } |
Dianne Hackborn | 2e44107 | 2015-10-28 18:00:57 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 1040 | FileDescriptor err, String[] args, ShellCallback callback, |
| 1041 | ResultReceiver resultReceiver) { |
| 1042 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | private final class LocalService extends BatteryManagerInternal { |
| 1047 | @Override |
| 1048 | public boolean isPowered(int plugTypeSet) { |
| 1049 | synchronized (mLock) { |
| 1050 | return isPoweredLocked(plugTypeSet); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | @Override |
| 1055 | public int getPlugType() { |
| 1056 | synchronized (mLock) { |
| 1057 | return mPlugType; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | @Override |
| 1062 | public int getBatteryLevel() { |
| 1063 | synchronized (mLock) { |
Yifan Hong | 932190b | 2017-10-11 11:00:51 -0700 | [diff] [blame] | 1064 | return mHealthInfo.legacy.batteryLevel; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | @Override |
| 1069 | public boolean getBatteryLevelLow() { |
| 1070 | synchronized (mLock) { |
| 1071 | return mBatteryLevelLow; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | @Override |
| 1076 | public int getInvalidCharger() { |
| 1077 | synchronized (mLock) { |
| 1078 | return mInvalidCharger; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 1082 | |
| 1083 | /** |
| 1084 | * HealthServiceWrapper wraps the internal IHealth service and refreshes the service when |
| 1085 | * necessary. |
| 1086 | * |
| 1087 | * On new registration of IHealth service, {@link #onRegistration onRegistration} is called and |
| 1088 | * the internal service is refreshed. |
| 1089 | * On death of an existing IHealth service, the internal service is NOT cleared to avoid |
| 1090 | * race condition between death notification and new service notification. Hence, |
| 1091 | * a caller must check for transaction errors when calling into the service. |
| 1092 | * |
| 1093 | * @hide Should only be used internally. |
| 1094 | */ |
| 1095 | @VisibleForTesting |
| 1096 | static final class HealthServiceWrapper { |
| 1097 | private static final String TAG = "HealthServiceWrapper"; |
| 1098 | public static final String INSTANCE_HEALTHD = "backup"; |
| 1099 | public static final String INSTANCE_VENDOR = "default"; |
| 1100 | // All interesting instances, sorted by priority high -> low. |
| 1101 | private static final List<String> sAllInstances = |
| 1102 | Arrays.asList(INSTANCE_VENDOR, INSTANCE_HEALTHD); |
| 1103 | |
| 1104 | private final IServiceNotification mNotification = new Notification(); |
| 1105 | private Callback mCallback; |
| 1106 | private IHealthSupplier mHealthSupplier; |
| 1107 | |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1108 | private final Object mLastServiceSetLock = new Object(); |
| 1109 | // Last IHealth service received. |
| 1110 | // set must be also be guarded with mLastServiceSetLock to ensure ordering. |
| 1111 | private final AtomicReference<IHealth> mLastService = new AtomicReference<>(); |
| 1112 | |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 1113 | /** |
| 1114 | * init should be called after constructor. For testing purposes, init is not called by |
| 1115 | * constructor. |
| 1116 | */ |
| 1117 | HealthServiceWrapper() { |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Start monitoring registration of new IHealth services. Only instances that are in |
| 1122 | * {@code sAllInstances} and in device / framework manifest are used. This function should |
| 1123 | * only be called once. |
| 1124 | * @throws RemoteException transaction error when talking to IServiceManager |
| 1125 | * @throws NoSuchElementException if one of the following cases: |
| 1126 | * - No service manager; |
| 1127 | * - none of {@code sAllInstances} are in manifests (i.e. not |
| 1128 | * available on this device), or none of these instances are available to current |
| 1129 | * process. |
| 1130 | * @throws NullPointerException when callback is null or supplier is null |
| 1131 | */ |
| 1132 | void init(Callback callback, |
| 1133 | IServiceManagerSupplier managerSupplier, |
| 1134 | IHealthSupplier healthSupplier) |
| 1135 | throws RemoteException, NoSuchElementException, NullPointerException { |
| 1136 | if (callback == null || managerSupplier == null || healthSupplier == null) |
| 1137 | throw new NullPointerException(); |
| 1138 | |
| 1139 | mCallback = callback; |
| 1140 | mHealthSupplier = healthSupplier; |
| 1141 | |
| 1142 | IServiceManager manager = managerSupplier.get(); |
| 1143 | for (String name : sAllInstances) { |
| 1144 | if (manager.getTransport(IHealth.kInterfaceName, name) == |
| 1145 | IServiceManager.Transport.EMPTY) { |
| 1146 | continue; |
| 1147 | } |
| 1148 | |
| 1149 | manager.registerForNotifications(IHealth.kInterfaceName, name, mNotification); |
| 1150 | Slog.i(TAG, "health: HealthServiceWrapper listening to instance " + name); |
| 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | throw new NoSuchElementException(String.format( |
| 1155 | "No IHealth service instance among %s is available. Perhaps no permission?", |
| 1156 | sAllInstances.toString())); |
| 1157 | } |
| 1158 | |
| 1159 | interface Callback { |
| 1160 | /** |
| 1161 | * This function is invoked asynchronously when a new and related IServiceNotification |
| 1162 | * is received. |
| 1163 | * @param service the recently retrieved service from IServiceManager. |
| 1164 | * Can be a dead service before service notification of a new service is delivered. |
| 1165 | * Implementation must handle cases for {@link RemoteException}s when calling |
| 1166 | * into service. |
| 1167 | * @param instance instance name. |
| 1168 | */ |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1169 | void onRegistration(IHealth oldService, IHealth newService, String instance); |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * Supplier of services. |
| 1174 | * Must not return null; throw {@link NoSuchElementException} if a service is not available. |
| 1175 | */ |
| 1176 | interface IServiceManagerSupplier { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1177 | default IServiceManager get() throws NoSuchElementException, RemoteException { |
| 1178 | return IServiceManager.getService(); |
| 1179 | } |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 1180 | } |
| 1181 | /** |
| 1182 | * Supplier of services. |
| 1183 | * Must not return null; throw {@link NoSuchElementException} if a service is not available. |
| 1184 | */ |
| 1185 | interface IHealthSupplier { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1186 | default IHealth get(String name) throws NoSuchElementException, RemoteException { |
| 1187 | return IHealth.getService(name); |
| 1188 | } |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | private class Notification extends IServiceNotification.Stub { |
| 1192 | @Override |
| 1193 | public final void onRegistration(String interfaceName, String instanceName, |
| 1194 | boolean preexisting) { |
| 1195 | if (!IHealth.kInterfaceName.equals(interfaceName)) return; |
| 1196 | if (!sAllInstances.contains(instanceName)) return; |
| 1197 | try { |
Yifan Hong | 89d55c1 | 2017-10-11 11:29:01 -0700 | [diff] [blame^] | 1198 | // ensures the order of multiple onRegistration on different threads. |
| 1199 | synchronized (mLastServiceSetLock) { |
| 1200 | IHealth newService = mHealthSupplier.get(instanceName); |
| 1201 | IHealth oldService = mLastService.getAndSet(newService); |
| 1202 | Slog.i(TAG, "health: new instance registered " + instanceName); |
| 1203 | mCallback.onRegistration(oldService, newService, instanceName); |
| 1204 | } |
Yifan Hong | 9885279 | 2017-10-12 11:35:14 -0700 | [diff] [blame] | 1205 | } catch (NoSuchElementException | RemoteException ex) { |
| 1206 | Slog.e(TAG, "health: Cannot get instance '" + instanceName + "': " + |
| 1207 | ex.getMessage() + ". Perhaps no permission?"); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | } |