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