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