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