Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.systemui.power; |
| 18 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
| 25 | import android.content.res.Configuration; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 26 | import android.database.ContentObserver; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 27 | import android.os.BatteryManager; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 29 | import android.os.IThermalEventListener; |
| 30 | import android.os.IThermalService; |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 31 | import android.os.PowerManager; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
| 33 | import android.os.ServiceManager; |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 34 | import android.os.SystemClock; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 35 | import android.os.Temperature; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 37 | import android.provider.Settings; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 38 | import android.text.format.DateUtils; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 39 | import android.util.Log; |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 40 | import android.util.Slog; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 41 | |
| 42 | import com.android.internal.annotations.VisibleForTesting; |
Salvador Martinez | 580098fe | 2019-04-11 10:42:15 -0700 | [diff] [blame] | 43 | import com.android.settingslib.fuelgauge.Estimate; |
Salvador Martinez | 110f9a1 | 2018-01-31 09:57:17 -0800 | [diff] [blame] | 44 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 45 | import com.android.systemui.Dependency; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 46 | import com.android.systemui.R; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 47 | import com.android.systemui.SystemUI; |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 48 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 49 | import com.android.systemui.statusbar.CommandQueue; |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 50 | import com.android.systemui.statusbar.phone.StatusBar; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 51 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 52 | import java.io.FileDescriptor; |
| 53 | import java.io.PrintWriter; |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 54 | import java.time.Duration; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 55 | import java.util.Arrays; |
Salvador Martinez | 7ad2c17 | 2019-02-11 16:09:28 -0800 | [diff] [blame] | 56 | import java.util.concurrent.Future; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 57 | |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 58 | import javax.inject.Inject; |
Dave Mankoff | 9a22a2f | 2019-10-23 16:12:30 -0400 | [diff] [blame] | 59 | import javax.inject.Singleton; |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 60 | |
Dave Mankoff | 9a22a2f | 2019-10-23 16:12:30 -0400 | [diff] [blame] | 61 | import dagger.Lazy; |
| 62 | |
| 63 | @Singleton |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 64 | public class PowerUI extends SystemUI implements CommandQueue.Callbacks { |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 65 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 66 | static final String TAG = "PowerUI"; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 67 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 68 | private static final long TEMPERATURE_INTERVAL = 30 * DateUtils.SECOND_IN_MILLIS; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 69 | private static final long TEMPERATURE_LOGGING_INTERVAL = DateUtils.HOUR_IN_MILLIS; |
| 70 | private static final int MAX_RECENT_TEMPS = 125; // TEMPERATURE_LOGGING_INTERVAL plus a buffer |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 71 | static final long THREE_HOURS_IN_MILLIS = DateUtils.HOUR_IN_MILLIS * 3; |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 72 | private static final int CHARGE_CYCLE_PERCENT_RESET = 45; |
| 73 | private static final long SIX_HOURS_MILLIS = Duration.ofHours(6).toMillis(); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 74 | public static final int NO_ESTIMATE_AVAILABLE = -1; |
Sherry Huang | 9b78673 | 2019-08-03 02:36:44 +0800 | [diff] [blame] | 75 | private static final String BOOT_COUNT_KEY = "boot_count"; |
| 76 | private static final String PREFS = "powerui_prefs"; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 77 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 78 | private final Handler mHandler = new Handler(); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 79 | @VisibleForTesting |
| 80 | final Receiver mReceiver = new Receiver(); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 81 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 82 | private PowerManager mPowerManager; |
| 83 | private WarningsUI mWarnings; |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 84 | private InattentiveSleepWarningView mOverlayView; |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 85 | private final Configuration mLastConfiguration = new Configuration(); |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 86 | private int mPlugType = 0; |
| 87 | private int mInvalidCharger = 0; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 88 | private EnhancedEstimates mEnhancedEstimates; |
Salvador Martinez | 7ad2c17 | 2019-02-11 16:09:28 -0800 | [diff] [blame] | 89 | private Future mLastShowWarningTask; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 90 | private boolean mEnableSkinTemperatureWarning; |
| 91 | private boolean mEnableUsbTemperatureAlarm; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 92 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 93 | private int mLowBatteryAlertCloseLevel; |
| 94 | private final int[] mLowBatteryReminderLevels = new int[2]; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 95 | |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 96 | private long mScreenOffTime = -1; |
| 97 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 98 | @VisibleForTesting boolean mLowWarningShownThisChargeCycle; |
| 99 | @VisibleForTesting boolean mSevereWarningShownThisChargeCycle; |
| 100 | @VisibleForTesting BatteryStateSnapshot mCurrentBatteryStateSnapshot; |
| 101 | @VisibleForTesting BatteryStateSnapshot mLastBatteryStateSnapshot; |
Wei Wang | bf05e60 | 2018-11-21 11:46:48 -0800 | [diff] [blame] | 102 | @VisibleForTesting IThermalService mThermalService; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 103 | |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 104 | @VisibleForTesting int mBatteryLevel = 100; |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 105 | @VisibleForTesting int mBatteryStatus = BatteryManager.BATTERY_STATUS_UNKNOWN; |
| 106 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 107 | private IThermalEventListener mSkinThermalEventListener; |
| 108 | private IThermalEventListener mUsbThermalEventListener; |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 109 | private final BroadcastDispatcher mBroadcastDispatcher; |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 110 | private final CommandQueue mCommandQueue; |
Dave Mankoff | 9a22a2f | 2019-10-23 16:12:30 -0400 | [diff] [blame] | 111 | private final Lazy<StatusBar> mStatusBarLazy; |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 112 | |
| 113 | @Inject |
Dave Mankoff | 9a22a2f | 2019-10-23 16:12:30 -0400 | [diff] [blame] | 114 | public PowerUI(Context context, BroadcastDispatcher broadcastDispatcher, |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 115 | CommandQueue commandQueue, Lazy<StatusBar> statusBarLazy) { |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 116 | super(context); |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 117 | mBroadcastDispatcher = broadcastDispatcher; |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 118 | mCommandQueue = commandQueue; |
Dave Mankoff | 9a22a2f | 2019-10-23 16:12:30 -0400 | [diff] [blame] | 119 | mStatusBarLazy = statusBarLazy; |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 120 | } |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 121 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 122 | public void start() { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 123 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 124 | mScreenOffTime = mPowerManager.isScreenOn() ? -1 : SystemClock.elapsedRealtime(); |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 125 | mWarnings = Dependency.get(WarningsUI.class); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 126 | mEnhancedEstimates = Dependency.get(EnhancedEstimates.class); |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 127 | mLastConfiguration.setTo(mContext.getResources().getConfiguration()); |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 128 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 129 | ContentObserver obs = new ContentObserver(mHandler) { |
| 130 | @Override |
| 131 | public void onChange(boolean selfChange) { |
| 132 | updateBatteryWarningLevels(); |
| 133 | } |
| 134 | }; |
| 135 | final ContentResolver resolver = mContext.getContentResolver(); |
| 136 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 137 | Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL), |
| 138 | false, obs, UserHandle.USER_ALL); |
| 139 | updateBatteryWarningLevels(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 140 | mReceiver.init(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 141 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 142 | // Check to see if we need to let the user know that the phone previously shut down due |
| 143 | // to the temperature being too high. |
Sherry Huang | 9b78673 | 2019-08-03 02:36:44 +0800 | [diff] [blame] | 144 | showWarnOnThermalShutdown(); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 145 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 146 | // Register an observer to configure mEnableSkinTemperatureWarning and perform the |
| 147 | // registration of skin thermal event listener upon Settings change. |
| 148 | resolver.registerContentObserver( |
| 149 | Settings.Global.getUriFor(Settings.Global.SHOW_TEMPERATURE_WARNING), |
| 150 | false /*notifyForDescendants*/, |
| 151 | new ContentObserver(mHandler) { |
| 152 | @Override |
| 153 | public void onChange(boolean selfChange) { |
| 154 | doSkinThermalEventListenerRegistration(); |
| 155 | } |
| 156 | }); |
| 157 | // Register an observer to configure mEnableUsbTemperatureAlarm and perform the |
| 158 | // registration of usb thermal event listener upon Settings change. |
| 159 | resolver.registerContentObserver( |
| 160 | Settings.Global.getUriFor(Settings.Global.SHOW_USB_TEMPERATURE_ALARM), |
| 161 | false /*notifyForDescendants*/, |
| 162 | new ContentObserver(mHandler) { |
| 163 | @Override |
| 164 | public void onChange(boolean selfChange) { |
| 165 | doUsbThermalEventListenerRegistration(); |
| 166 | } |
| 167 | }); |
| 168 | initThermalEventListeners(); |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 169 | mCommandQueue.addCallback(this); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 170 | } |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 172 | @Override |
| 173 | protected void onConfigurationChanged(Configuration newConfig) { |
| 174 | final int mask = ActivityInfo.CONFIG_MCC | ActivityInfo.CONFIG_MNC; |
| 175 | |
| 176 | // Safe to modify mLastConfiguration here as it's only updated by the main thread (here). |
| 177 | if ((mLastConfiguration.updateFrom(newConfig) & mask) != 0) { |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 178 | mHandler.post(this::initThermalEventListeners); |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 182 | void updateBatteryWarningLevels() { |
| 183 | int critLevel = mContext.getResources().getInteger( |
| 184 | com.android.internal.R.integer.config_criticalBatteryWarningLevel); |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 185 | int warnLevel = mContext.getResources().getInteger( |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 186 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
Makoto Onuki | 4a9036b | 2018-01-11 14:48:20 -0800 | [diff] [blame] | 187 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 188 | if (warnLevel < critLevel) { |
| 189 | warnLevel = critLevel; |
| 190 | } |
| 191 | |
| 192 | mLowBatteryReminderLevels[0] = warnLevel; |
| 193 | mLowBatteryReminderLevels[1] = critLevel; |
| 194 | mLowBatteryAlertCloseLevel = mLowBatteryReminderLevels[0] |
| 195 | + mContext.getResources().getInteger( |
| 196 | com.android.internal.R.integer.config_lowBatteryCloseWarningBump); |
| 197 | } |
| 198 | |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Buckets the battery level. |
| 201 | * |
| 202 | * The code in this function is a little weird because I couldn't comprehend |
| 203 | * the bucket going up when the battery level was going down. --joeo |
| 204 | * |
| 205 | * 1 means that the battery is "ok" |
| 206 | * 0 means that the battery is between "ok" and what we should warn about. |
| 207 | * less than 0 means that the battery is low |
| 208 | */ |
| 209 | private int findBatteryLevelBucket(int level) { |
| 210 | if (level >= mLowBatteryAlertCloseLevel) { |
| 211 | return 1; |
| 212 | } |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 213 | if (level > mLowBatteryReminderLevels[0]) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | final int N = mLowBatteryReminderLevels.length; |
| 217 | for (int i=N-1; i>=0; i--) { |
| 218 | if (level <= mLowBatteryReminderLevels[i]) { |
| 219 | return -1-i; |
| 220 | } |
| 221 | } |
| 222 | throw new RuntimeException("not possible!"); |
| 223 | } |
| 224 | |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 225 | @VisibleForTesting |
| 226 | final class Receiver extends BroadcastReceiver { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 227 | |
| 228 | public void init() { |
| 229 | // Register for Intent broadcasts for... |
| 230 | IntentFilter filter = new IntentFilter(); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 231 | filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 232 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 233 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 234 | filter.addAction(Intent.ACTION_SCREEN_ON); |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 235 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Fabian Kozynski | 5e92c6f | 2020-01-03 13:56:17 -0500 | [diff] [blame] | 236 | mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mHandler); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void onReceive(Context context, Intent intent) { |
| 241 | String action = intent.getAction(); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 242 | if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) { |
| 243 | ThreadUtils.postOnBackgroundThread(() -> { |
| 244 | if (mPowerManager.isPowerSaveMode()) { |
| 245 | mWarnings.dismissLowBatteryWarning(); |
| 246 | } |
| 247 | }); |
| 248 | } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 249 | final int oldBatteryLevel = mBatteryLevel; |
| 250 | mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100); |
| 251 | final int oldBatteryStatus = mBatteryStatus; |
| 252 | mBatteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, |
| 253 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 254 | final int oldPlugType = mPlugType; |
| 255 | mPlugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 1); |
| 256 | final int oldInvalidCharger = mInvalidCharger; |
| 257 | mInvalidCharger = intent.getIntExtra(BatteryManager.EXTRA_INVALID_CHARGER, 0); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 258 | mLastBatteryStateSnapshot = mCurrentBatteryStateSnapshot; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 259 | |
| 260 | final boolean plugged = mPlugType != 0; |
| 261 | final boolean oldPlugged = oldPlugType != 0; |
| 262 | |
| 263 | int oldBucket = findBatteryLevelBucket(oldBatteryLevel); |
| 264 | int bucket = findBatteryLevelBucket(mBatteryLevel); |
| 265 | |
Daniel Sandler | 7198662 | 2011-07-26 13:06:49 -0400 | [diff] [blame] | 266 | if (DEBUG) { |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 267 | Slog.d(TAG, "buckets ....." + mLowBatteryAlertCloseLevel |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 268 | + " .. " + mLowBatteryReminderLevels[0] |
| 269 | + " .. " + mLowBatteryReminderLevels[1]); |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 270 | Slog.d(TAG, "level " + oldBatteryLevel + " --> " + mBatteryLevel); |
| 271 | Slog.d(TAG, "status " + oldBatteryStatus + " --> " + mBatteryStatus); |
| 272 | Slog.d(TAG, "plugType " + oldPlugType + " --> " + mPlugType); |
| 273 | Slog.d(TAG, "invalidCharger " + oldInvalidCharger + " --> " + mInvalidCharger); |
| 274 | Slog.d(TAG, "bucket " + oldBucket + " --> " + bucket); |
| 275 | Slog.d(TAG, "plugged " + oldPlugged + " --> " + plugged); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 276 | } |
| 277 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 278 | mWarnings.update(mBatteryLevel, bucket, mScreenOffTime); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 279 | if (oldInvalidCharger == 0 && mInvalidCharger != 0) { |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 280 | Slog.d(TAG, "showing invalid charger warning"); |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 281 | mWarnings.showInvalidChargerWarning(); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 282 | return; |
| 283 | } else if (oldInvalidCharger != 0 && mInvalidCharger == 0) { |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 284 | mWarnings.dismissInvalidChargerWarning(); |
| 285 | } else if (mWarnings.isInvalidChargerWarningShowing()) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 286 | // if invalid charger is showing, don't show low battery |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 287 | if (DEBUG) { |
| 288 | Slog.d(TAG, "Bad Charger"); |
| 289 | } |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 293 | // Show the correct version of low battery warning if needed |
Salvador Martinez | 7ad2c17 | 2019-02-11 16:09:28 -0800 | [diff] [blame] | 294 | if (mLastShowWarningTask != null) { |
| 295 | mLastShowWarningTask.cancel(true); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 296 | if (DEBUG) { |
| 297 | Slog.d(TAG, "cancelled task"); |
| 298 | } |
Salvador Martinez | 7ad2c17 | 2019-02-11 16:09:28 -0800 | [diff] [blame] | 299 | } |
| 300 | mLastShowWarningTask = ThreadUtils.postOnBackgroundThread(() -> { |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 301 | maybeShowBatteryWarningV2( |
| 302 | plugged, bucket); |
Salvador Martinez | 110f9a1 | 2018-01-31 09:57:17 -0800 | [diff] [blame] | 303 | }); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 304 | |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 305 | } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 306 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 307 | } else if (Intent.ACTION_SCREEN_ON.equals(action)) { |
| 308 | mScreenOffTime = -1; |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 309 | } else if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 310 | mWarnings.userSwitched(); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 311 | } else { |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 312 | Slog.w(TAG, "unknown intent: " + intent); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 317 | protected void maybeShowBatteryWarningV2(boolean plugged, int bucket) { |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 318 | final boolean hybridEnabled = mEnhancedEstimates.isHybridNotificationEnabled(); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 319 | final boolean isPowerSaverMode = mPowerManager.isPowerSaveMode(); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 320 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 321 | // Stick current battery state into an immutable container to determine if we should show |
| 322 | // a warning. |
| 323 | if (DEBUG) { |
| 324 | Slog.d(TAG, "evaluating which notification to show"); |
| 325 | } |
| 326 | if (hybridEnabled) { |
| 327 | if (DEBUG) { |
| 328 | Slog.d(TAG, "using hybrid"); |
| 329 | } |
| 330 | Estimate estimate = refreshEstimateIfNeeded(); |
| 331 | mCurrentBatteryStateSnapshot = new BatteryStateSnapshot(mBatteryLevel, isPowerSaverMode, |
| 332 | plugged, bucket, mBatteryStatus, mLowBatteryReminderLevels[1], |
| 333 | mLowBatteryReminderLevels[0], estimate.getEstimateMillis(), |
Salvador Martinez | 580098fe | 2019-04-11 10:42:15 -0700 | [diff] [blame] | 334 | estimate.getAverageDischargeTime(), |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 335 | mEnhancedEstimates.getSevereWarningThreshold(), |
Salvador Martinez | d73c5aa | 2019-03-26 12:38:09 -0700 | [diff] [blame] | 336 | mEnhancedEstimates.getLowWarningThreshold(), estimate.isBasedOnUsage(), |
| 337 | mEnhancedEstimates.getLowWarningEnabled()); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 338 | } else { |
| 339 | if (DEBUG) { |
| 340 | Slog.d(TAG, "using standard"); |
| 341 | } |
| 342 | mCurrentBatteryStateSnapshot = new BatteryStateSnapshot(mBatteryLevel, isPowerSaverMode, |
| 343 | plugged, bucket, mBatteryStatus, mLowBatteryReminderLevels[1], |
| 344 | mLowBatteryReminderLevels[0]); |
| 345 | } |
| 346 | |
| 347 | mWarnings.updateSnapshot(mCurrentBatteryStateSnapshot); |
| 348 | if (mCurrentBatteryStateSnapshot.isHybrid()) { |
| 349 | maybeShowHybridWarning(mCurrentBatteryStateSnapshot, mLastBatteryStateSnapshot); |
| 350 | } else { |
| 351 | maybeShowBatteryWarning(mCurrentBatteryStateSnapshot, mLastBatteryStateSnapshot); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // updates the time estimate if we don't have one or battery level has changed. |
| 356 | @VisibleForTesting |
| 357 | Estimate refreshEstimateIfNeeded() { |
| 358 | if (mLastBatteryStateSnapshot == null |
| 359 | || mLastBatteryStateSnapshot.getTimeRemainingMillis() == NO_ESTIMATE_AVAILABLE |
| 360 | || mBatteryLevel != mLastBatteryStateSnapshot.getBatteryLevel()) { |
| 361 | final Estimate estimate = mEnhancedEstimates.getEstimate(); |
| 362 | if (DEBUG) { |
| 363 | Slog.d(TAG, "updated estimate: " + estimate.getEstimateMillis()); |
| 364 | } |
| 365 | return estimate; |
| 366 | } |
| 367 | return new Estimate(mLastBatteryStateSnapshot.getTimeRemainingMillis(), |
Salvador Martinez | 580098fe | 2019-04-11 10:42:15 -0700 | [diff] [blame] | 368 | mLastBatteryStateSnapshot.isBasedOnUsage(), |
| 369 | mLastBatteryStateSnapshot.getAverageTimeToDischargeMillis()); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | @VisibleForTesting |
| 373 | void maybeShowHybridWarning(BatteryStateSnapshot currentSnapshot, |
| 374 | BatteryStateSnapshot lastSnapshot) { |
| 375 | // if we are now over 45% battery & 6 hours remaining so we can trigger hybrid |
| 376 | // notification again |
| 377 | if (currentSnapshot.getBatteryLevel() >= CHARGE_CYCLE_PERCENT_RESET |
| 378 | && currentSnapshot.getTimeRemainingMillis() > SIX_HOURS_MILLIS) { |
| 379 | mLowWarningShownThisChargeCycle = false; |
| 380 | mSevereWarningShownThisChargeCycle = false; |
| 381 | if (DEBUG) { |
| 382 | Slog.d(TAG, "Charge cycle reset! Can show warnings again"); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 386 | final boolean playSound = currentSnapshot.getBucket() != lastSnapshot.getBucket() |
| 387 | || lastSnapshot.getPlugged(); |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 388 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 389 | if (shouldShowHybridWarning(currentSnapshot)) { |
| 390 | mWarnings.showLowBatteryWarning(playSound); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 391 | // mark if we've already shown a warning this cycle. This will prevent the notification |
| 392 | // trigger from spamming users by only showing low/critical warnings once per cycle |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 393 | if (currentSnapshot.getTimeRemainingMillis() |
Salvador Martinez | 290496e | 2019-04-04 10:30:19 -0700 | [diff] [blame] | 394 | <= currentSnapshot.getSevereThresholdMillis() |
| 395 | || currentSnapshot.getBatteryLevel() |
| 396 | <= currentSnapshot.getSevereLevelThreshold()) { |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 397 | mSevereWarningShownThisChargeCycle = true; |
| 398 | mLowWarningShownThisChargeCycle = true; |
| 399 | if (DEBUG) { |
| 400 | Slog.d(TAG, "Severe warning marked as shown this cycle"); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 401 | } |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 402 | } else { |
| 403 | Slog.d(TAG, "Low warning marked as shown this cycle"); |
| 404 | mLowWarningShownThisChargeCycle = true; |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 405 | } |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 406 | } else if (shouldDismissHybridWarning(currentSnapshot)) { |
| 407 | if (DEBUG) { |
| 408 | Slog.d(TAG, "Dismissing warning"); |
| 409 | } |
| 410 | mWarnings.dismissLowBatteryWarning(); |
| 411 | } else { |
| 412 | if (DEBUG) { |
| 413 | Slog.d(TAG, "Updating warning"); |
| 414 | } |
| 415 | mWarnings.updateLowBatteryWarning(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | @VisibleForTesting |
| 420 | boolean shouldShowHybridWarning(BatteryStateSnapshot snapshot) { |
| 421 | if (snapshot.getPlugged() |
| 422 | || snapshot.getBatteryStatus() == BatteryManager.BATTERY_STATUS_UNKNOWN) { |
| 423 | Slog.d(TAG, "can't show warning due to - plugged: " + snapshot.getPlugged() |
| 424 | + " status unknown: " |
| 425 | + (snapshot.getBatteryStatus() == BatteryManager.BATTERY_STATUS_UNKNOWN)); |
| 426 | return false; |
| 427 | } |
| 428 | |
Salvador Martinez | d73c5aa | 2019-03-26 12:38:09 -0700 | [diff] [blame] | 429 | // Only show the low warning if enabled once per charge cycle & no battery saver |
| 430 | final boolean canShowWarning = snapshot.isLowWarningEnabled() |
| 431 | && !mLowWarningShownThisChargeCycle && !snapshot.isPowerSaver() |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 432 | && (snapshot.getTimeRemainingMillis() < snapshot.getLowThresholdMillis() |
| 433 | || snapshot.getBatteryLevel() <= snapshot.getLowLevelThreshold()); |
| 434 | |
| 435 | // Only show the severe warning once per charge cycle |
| 436 | final boolean canShowSevereWarning = !mSevereWarningShownThisChargeCycle |
| 437 | && (snapshot.getTimeRemainingMillis() < snapshot.getSevereThresholdMillis() |
| 438 | || snapshot.getBatteryLevel() <= snapshot.getSevereLevelThreshold()); |
| 439 | |
| 440 | final boolean canShow = canShowWarning || canShowSevereWarning; |
Salvador Martinez | d73c5aa | 2019-03-26 12:38:09 -0700 | [diff] [blame] | 441 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 442 | if (DEBUG) { |
| 443 | Slog.d(TAG, "Enhanced trigger is: " + canShow + "\nwith battery snapshot:" |
| 444 | + " mLowWarningShownThisChargeCycle: " + mLowWarningShownThisChargeCycle |
| 445 | + " mSevereWarningShownThisChargeCycle: " + mSevereWarningShownThisChargeCycle |
| 446 | + "\n" + snapshot.toString()); |
| 447 | } |
| 448 | return canShow; |
| 449 | } |
| 450 | |
| 451 | @VisibleForTesting |
| 452 | boolean shouldDismissHybridWarning(BatteryStateSnapshot snapshot) { |
| 453 | return snapshot.getPlugged() |
| 454 | || snapshot.getTimeRemainingMillis() > snapshot.getLowThresholdMillis(); |
| 455 | } |
| 456 | |
| 457 | protected void maybeShowBatteryWarning( |
| 458 | BatteryStateSnapshot currentSnapshot, |
| 459 | BatteryStateSnapshot lastSnapshot) { |
| 460 | final boolean playSound = currentSnapshot.getBucket() != lastSnapshot.getBucket() |
| 461 | || lastSnapshot.getPlugged(); |
| 462 | |
| 463 | if (shouldShowLowBatteryWarning(currentSnapshot, lastSnapshot)) { |
| 464 | mWarnings.showLowBatteryWarning(playSound); |
| 465 | } else if (shouldDismissLowBatteryWarning(currentSnapshot, lastSnapshot)) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 466 | mWarnings.dismissLowBatteryWarning(); |
| 467 | } else { |
| 468 | mWarnings.updateLowBatteryWarning(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | @VisibleForTesting |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 473 | boolean shouldShowLowBatteryWarning( |
| 474 | BatteryStateSnapshot currentSnapshot, |
| 475 | BatteryStateSnapshot lastSnapshot) { |
| 476 | return !currentSnapshot.getPlugged() |
| 477 | && !currentSnapshot.isPowerSaver() |
| 478 | && (((currentSnapshot.getBucket() < lastSnapshot.getBucket() |
| 479 | || lastSnapshot.getPlugged()) |
| 480 | && currentSnapshot.getBucket() < 0)) |
| 481 | && currentSnapshot.getBatteryStatus() != BatteryManager.BATTERY_STATUS_UNKNOWN; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 484 | @VisibleForTesting |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 485 | boolean shouldDismissLowBatteryWarning( |
| 486 | BatteryStateSnapshot currentSnapshot, |
| 487 | BatteryStateSnapshot lastSnapshot) { |
| 488 | return currentSnapshot.isPowerSaver() |
| 489 | || currentSnapshot.getPlugged() |
| 490 | || (currentSnapshot.getBucket() > lastSnapshot.getBucket() |
| 491 | && currentSnapshot.getBucket() > 0); |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 494 | private void initThermalEventListeners() { |
| 495 | doSkinThermalEventListenerRegistration(); |
| 496 | doUsbThermalEventListenerRegistration(); |
| 497 | } |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 498 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 499 | @VisibleForTesting |
| 500 | synchronized void doSkinThermalEventListenerRegistration() { |
| 501 | final boolean oldEnableSkinTemperatureWarning = mEnableSkinTemperatureWarning; |
| 502 | boolean ret = false; |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 503 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 504 | mEnableSkinTemperatureWarning = Settings.Global.getInt(mContext.getContentResolver(), |
| 505 | Settings.Global.SHOW_TEMPERATURE_WARNING, |
| 506 | mContext.getResources().getInteger(R.integer.config_showTemperatureWarning)) != 0; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 507 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 508 | if (mEnableSkinTemperatureWarning != oldEnableSkinTemperatureWarning) { |
| 509 | try { |
| 510 | if (mSkinThermalEventListener == null) { |
| 511 | mSkinThermalEventListener = new SkinThermalEventListener(); |
| 512 | } |
| 513 | if (mThermalService == null) { |
| 514 | mThermalService = IThermalService.Stub.asInterface( |
| 515 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 516 | } |
| 517 | if (mEnableSkinTemperatureWarning) { |
| 518 | ret = mThermalService.registerThermalEventListenerWithType( |
| 519 | mSkinThermalEventListener, Temperature.TYPE_SKIN); |
| 520 | } else { |
| 521 | ret = mThermalService.unregisterThermalEventListener(mSkinThermalEventListener); |
| 522 | } |
| 523 | } catch (RemoteException e) { |
| 524 | Slog.e(TAG, "Exception while (un)registering skin thermal event listener.", e); |
| 525 | } |
| 526 | |
| 527 | if (!ret) { |
| 528 | mEnableSkinTemperatureWarning = !mEnableSkinTemperatureWarning; |
| 529 | Slog.e(TAG, "Failed to register or unregister skin thermal event listener."); |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 532 | } |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 533 | |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 534 | @VisibleForTesting |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 535 | synchronized void doUsbThermalEventListenerRegistration() { |
| 536 | final boolean oldEnableUsbTemperatureAlarm = mEnableUsbTemperatureAlarm; |
| 537 | boolean ret = false; |
| 538 | |
| 539 | mEnableUsbTemperatureAlarm = Settings.Global.getInt(mContext.getContentResolver(), |
| 540 | Settings.Global.SHOW_USB_TEMPERATURE_ALARM, |
| 541 | mContext.getResources().getInteger(R.integer.config_showUsbPortAlarm)) != 0; |
| 542 | |
| 543 | if (mEnableUsbTemperatureAlarm != oldEnableUsbTemperatureAlarm) { |
| 544 | try { |
| 545 | if (mUsbThermalEventListener == null) { |
| 546 | mUsbThermalEventListener = new UsbThermalEventListener(); |
| 547 | } |
| 548 | if (mThermalService == null) { |
| 549 | mThermalService = IThermalService.Stub.asInterface( |
| 550 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 551 | } |
| 552 | if (mEnableUsbTemperatureAlarm) { |
| 553 | ret = mThermalService.registerThermalEventListenerWithType( |
| 554 | mUsbThermalEventListener, Temperature.TYPE_USB_PORT); |
| 555 | } else { |
| 556 | ret = mThermalService.unregisterThermalEventListener(mUsbThermalEventListener); |
| 557 | } |
| 558 | } catch (RemoteException e) { |
| 559 | Slog.e(TAG, "Exception while (un)registering usb thermal event listener.", e); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 560 | } |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 561 | |
| 562 | if (!ret) { |
| 563 | mEnableUsbTemperatureAlarm = !mEnableUsbTemperatureAlarm; |
| 564 | Slog.e(TAG, "Failed to register or unregister usb thermal event listener."); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 565 | } |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 566 | } |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Sherry Huang | 9b78673 | 2019-08-03 02:36:44 +0800 | [diff] [blame] | 569 | private void showWarnOnThermalShutdown() { |
| 570 | int bootCount = -1; |
| 571 | int lastReboot = mContext.getSharedPreferences(PREFS, 0).getInt(BOOT_COUNT_KEY, -1); |
| 572 | try { |
| 573 | bootCount = Settings.Global.getInt(mContext.getContentResolver(), |
| 574 | Settings.Global.BOOT_COUNT); |
| 575 | } catch (Settings.SettingNotFoundException e) { |
| 576 | Slog.e(TAG, "Failed to read system boot count from Settings.Global.BOOT_COUNT"); |
| 577 | } |
| 578 | // Only show the thermal shutdown warning when there is a thermal reboot. |
| 579 | if (bootCount > lastReboot) { |
| 580 | mContext.getSharedPreferences(PREFS, 0).edit().putInt(BOOT_COUNT_KEY, |
| 581 | bootCount).apply(); |
| 582 | if (mPowerManager.getLastShutdownReason() |
| 583 | == PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN) { |
| 584 | mWarnings.showThermalShutdownWarning(); |
| 585 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Robert Horvath | 121e025 | 2019-11-19 14:09:48 +0100 | [diff] [blame] | 589 | @Override |
| 590 | public void showInattentiveSleepWarning() { |
| 591 | if (mOverlayView == null) { |
| 592 | mOverlayView = new InattentiveSleepWarningView(mContext); |
| 593 | } |
| 594 | |
| 595 | mOverlayView.show(); |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public void dismissInattentiveSleepWarning(boolean animated) { |
| 600 | if (mOverlayView != null) { |
| 601 | mOverlayView.dismiss(animated); |
| 602 | } |
| 603 | } |
| 604 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 605 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 606 | pw.print("mLowBatteryAlertCloseLevel="); |
| 607 | pw.println(mLowBatteryAlertCloseLevel); |
| 608 | pw.print("mLowBatteryReminderLevels="); |
| 609 | pw.println(Arrays.toString(mLowBatteryReminderLevels)); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 610 | pw.print("mBatteryLevel="); |
| 611 | pw.println(Integer.toString(mBatteryLevel)); |
| 612 | pw.print("mBatteryStatus="); |
| 613 | pw.println(Integer.toString(mBatteryStatus)); |
| 614 | pw.print("mPlugType="); |
| 615 | pw.println(Integer.toString(mPlugType)); |
| 616 | pw.print("mInvalidCharger="); |
| 617 | pw.println(Integer.toString(mInvalidCharger)); |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 618 | pw.print("mScreenOffTime="); |
| 619 | pw.print(mScreenOffTime); |
| 620 | if (mScreenOffTime >= 0) { |
| 621 | pw.print(" ("); |
| 622 | pw.print(SystemClock.elapsedRealtime() - mScreenOffTime); |
| 623 | pw.print(" ago)"); |
| 624 | } |
| 625 | pw.println(); |
| 626 | pw.print("soundTimeout="); |
| 627 | pw.println(Settings.Global.getInt(mContext.getContentResolver(), |
| 628 | Settings.Global.LOW_BATTERY_SOUND_TIMEOUT, 0)); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 629 | pw.print("bucket: "); |
| 630 | pw.println(Integer.toString(findBatteryLevelBucket(mBatteryLevel))); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 631 | pw.print("mEnableSkinTemperatureWarning="); |
| 632 | pw.println(mEnableSkinTemperatureWarning); |
| 633 | pw.print("mEnableUsbTemperatureAlarm="); |
| 634 | pw.println(mEnableUsbTemperatureAlarm); |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 635 | mWarnings.dump(pw); |
| 636 | } |
| 637 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 638 | /** |
| 639 | * The interface to allow PowerUI to communicate with whatever implementation of WarningsUI |
| 640 | * is being used by the system. |
| 641 | */ |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 642 | public interface WarningsUI { |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 643 | |
| 644 | /** |
| 645 | * Updates battery and screen info for determining whether to trigger battery warnings or |
| 646 | * not. |
| 647 | * @param batteryLevel The current battery level |
| 648 | * @param bucket The current battery bucket |
| 649 | * @param screenOffTime How long the screen has been off in millis |
| 650 | */ |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 651 | void update(int batteryLevel, int bucket, long screenOffTime); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 652 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 653 | void dismissLowBatteryWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 654 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 655 | void showLowBatteryWarning(boolean playSound); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 656 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 657 | void dismissInvalidChargerWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 658 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 659 | void showInvalidChargerWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 660 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 661 | void updateLowBatteryWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 662 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 663 | boolean isInvalidChargerWarningShowing(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 664 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 665 | void dismissHighTemperatureWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 666 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 667 | void showHighTemperatureWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 668 | |
| 669 | /** |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 670 | * Display USB port overheat alarm |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 671 | */ |
| 672 | void showUsbHighTemperatureAlarm(); |
| 673 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 674 | void showThermalShutdownWarning(); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 675 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 676 | void dump(PrintWriter pw); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 677 | |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 678 | void userSwitched(); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 679 | |
| 680 | /** |
| 681 | * Updates the snapshot of battery state used for evaluating battery warnings |
| 682 | * @param snapshot object containing relevant values for making battery warning decisions. |
| 683 | */ |
| 684 | void updateSnapshot(BatteryStateSnapshot snapshot); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 685 | } |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 686 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 687 | // Skin thermal event received from thermal service manager subsystem |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 688 | @VisibleForTesting |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 689 | final class SkinThermalEventListener extends IThermalEventListener.Stub { |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 690 | @Override public void notifyThrottling(Temperature temp) { |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 691 | int status = temp.getStatus(); |
| 692 | |
| 693 | if (status >= Temperature.THROTTLING_EMERGENCY) { |
Dave Mankoff | 9a22a2f | 2019-10-23 16:12:30 -0400 | [diff] [blame] | 694 | if (!mStatusBarLazy.get().isDeviceInVrMode()) { |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 695 | mWarnings.showHighTemperatureWarning(); |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 696 | Slog.d(TAG, "SkinThermalEventListener: notifyThrottling was called " |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 697 | + ", current skin status = " + status |
| 698 | + ", temperature = " + temp.getValue()); |
| 699 | } |
| 700 | } else { |
| 701 | mWarnings.dismissHighTemperatureWarning(); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 706 | // Usb thermal event received from thermal service manager subsystem |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 707 | @VisibleForTesting |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 708 | final class UsbThermalEventListener extends IThermalEventListener.Stub { |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 709 | @Override public void notifyThrottling(Temperature temp) { |
| 710 | int status = temp.getStatus(); |
| 711 | |
| 712 | if (status >= Temperature.THROTTLING_EMERGENCY) { |
| 713 | mWarnings.showUsbHighTemperatureAlarm(); |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 714 | Slog.d(TAG, "UsbThermalEventListener: notifyThrottling was called " |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 715 | + ", current usb port status = " + status |
| 716 | + ", temperature = " + temp.getValue()); |
| 717 | } |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | } |