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