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; |
Andrew Sapperstein | e335256 | 2017-01-20 15:41:03 -0800 | [diff] [blame] | 26 | import android.content.res.Resources; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 27 | import android.database.ContentObserver; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 28 | import android.os.BatteryManager; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 29 | import android.os.Handler; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 30 | import android.os.HardwarePropertiesManager; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 31 | import android.os.IBinder; |
| 32 | import android.os.IThermalEventListener; |
| 33 | import android.os.IThermalService; |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 34 | import android.os.PowerManager; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 36 | import android.os.ServiceManager; |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 37 | import android.os.SystemClock; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 38 | import android.os.Temperature; |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 41 | import android.text.format.DateUtils; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 42 | import android.util.Log; |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 43 | import android.util.Slog; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 44 | |
| 45 | import com.android.internal.annotations.VisibleForTesting; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 46 | import com.android.internal.logging.MetricsLogger; |
Salvador Martinez | 110f9a1 | 2018-01-31 09:57:17 -0800 | [diff] [blame] | 47 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 48 | import com.android.systemui.Dependency; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 49 | import com.android.systemui.R; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 50 | import com.android.systemui.SystemUI; |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.phone.StatusBar; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 52 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 53 | import java.io.FileDescriptor; |
| 54 | import java.io.PrintWriter; |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 55 | import java.time.Duration; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 56 | import java.util.Arrays; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 57 | import java.util.Locale; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 58 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 59 | public class PowerUI extends SystemUI { |
| 60 | static final String TAG = "PowerUI"; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 61 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 62 | private static final long TEMPERATURE_INTERVAL = 30 * DateUtils.SECOND_IN_MILLIS; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 63 | private static final long TEMPERATURE_LOGGING_INTERVAL = DateUtils.HOUR_IN_MILLIS; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 64 | private static final int TEMPERATURE_OVERHEAT_WARNING = 0; |
| 65 | private static final int TEMPERATURE_OVERHEAT_ALARM = 1; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 66 | 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] | 67 | static final long THREE_HOURS_IN_MILLIS = DateUtils.HOUR_IN_MILLIS * 3; |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 68 | private static final int CHARGE_CYCLE_PERCENT_RESET = 45; |
| 69 | private static final long SIX_HOURS_MILLIS = Duration.ofHours(6).toMillis(); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 70 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 71 | private final Handler mHandler = new Handler(); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 72 | @VisibleForTesting |
| 73 | final Receiver mReceiver = new Receiver(); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 74 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 75 | private PowerManager mPowerManager; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 76 | private HardwarePropertiesManager mHardwarePropertiesManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 77 | private WarningsUI mWarnings; |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 78 | private final Configuration mLastConfiguration = new Configuration(); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 79 | private long mTimeRemaining = Long.MAX_VALUE; |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 80 | private int mPlugType = 0; |
| 81 | private int mInvalidCharger = 0; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 82 | private EnhancedEstimates mEnhancedEstimates; |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 83 | private Estimate mLastEstimate; |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 84 | private boolean mLowWarningShownThisChargeCycle; |
| 85 | private boolean mSevereWarningShownThisChargeCycle; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 86 | private boolean mEnableTemperatureWarning; |
| 87 | private boolean mEnableTemperatureAlarm; |
| 88 | private boolean mIsOverheatAlarming; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 89 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 90 | private int mLowBatteryAlertCloseLevel; |
| 91 | private final int[] mLowBatteryReminderLevels = new int[2]; |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 92 | |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 93 | private long mScreenOffTime = -1; |
| 94 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 95 | private float mThresholdWarningTemp; |
| 96 | private float mThresholdAlarmTemp; |
| 97 | private float mThresholdAlarmTempTolerance; |
| 98 | private float[] mRecentSkinTemps = new float[MAX_RECENT_TEMPS]; |
| 99 | private float[] mRecentAlarmTemps = new float[MAX_RECENT_TEMPS]; |
| 100 | private int mWarningNumTemps; |
| 101 | private int mAlarmNumTemps; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 102 | private long mNextLogTime; |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 103 | private IThermalService mThermalService; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 104 | |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 105 | @VisibleForTesting int mBatteryLevel = 100; |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 106 | @VisibleForTesting int mBatteryStatus = BatteryManager.BATTERY_STATUS_UNKNOWN; |
| 107 | |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 108 | // by using the same instance (method references are not guaranteed to be the same object |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 109 | // We create a method reference here so that we are guaranteed that we can remove a callback |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 110 | // each time they are created). |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 111 | private final Runnable mUpdateTempCallback = this::updateTemperature; |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 112 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 113 | public void start() { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 114 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 115 | mHardwarePropertiesManager = (HardwarePropertiesManager) |
| 116 | mContext.getSystemService(Context.HARDWARE_PROPERTIES_SERVICE); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 117 | mScreenOffTime = mPowerManager.isScreenOn() ? -1 : SystemClock.elapsedRealtime(); |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 118 | mWarnings = Dependency.get(WarningsUI.class); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 119 | mEnhancedEstimates = Dependency.get(EnhancedEstimates.class); |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 120 | mLastConfiguration.setTo(mContext.getResources().getConfiguration()); |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 121 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 122 | ContentObserver obs = new ContentObserver(mHandler) { |
| 123 | @Override |
| 124 | public void onChange(boolean selfChange) { |
| 125 | updateBatteryWarningLevels(); |
| 126 | } |
| 127 | }; |
| 128 | final ContentResolver resolver = mContext.getContentResolver(); |
| 129 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 130 | Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL), |
| 131 | false, obs, UserHandle.USER_ALL); |
| 132 | updateBatteryWarningLevels(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 133 | mReceiver.init(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 134 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 135 | // Check to see if we need to let the user know that the phone previously shut down due |
| 136 | // to the temperature being too high. |
| 137 | showThermalShutdownDialog(); |
| 138 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 139 | initTemperature(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 140 | } |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 141 | |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 142 | @Override |
| 143 | protected void onConfigurationChanged(Configuration newConfig) { |
| 144 | final int mask = ActivityInfo.CONFIG_MCC | ActivityInfo.CONFIG_MNC; |
| 145 | |
| 146 | // Safe to modify mLastConfiguration here as it's only updated by the main thread (here). |
| 147 | if ((mLastConfiguration.updateFrom(newConfig) & mask) != 0) { |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 148 | mHandler.post(this::initTemperature); |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 152 | void updateBatteryWarningLevels() { |
| 153 | int critLevel = mContext.getResources().getInteger( |
| 154 | com.android.internal.R.integer.config_criticalBatteryWarningLevel); |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 155 | int warnLevel = mContext.getResources().getInteger( |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 156 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
Makoto Onuki | 4a9036b | 2018-01-11 14:48:20 -0800 | [diff] [blame] | 157 | |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 158 | if (warnLevel < critLevel) { |
| 159 | warnLevel = critLevel; |
| 160 | } |
| 161 | |
| 162 | mLowBatteryReminderLevels[0] = warnLevel; |
| 163 | mLowBatteryReminderLevels[1] = critLevel; |
| 164 | mLowBatteryAlertCloseLevel = mLowBatteryReminderLevels[0] |
| 165 | + mContext.getResources().getInteger( |
| 166 | com.android.internal.R.integer.config_lowBatteryCloseWarningBump); |
| 167 | } |
| 168 | |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Buckets the battery level. |
| 171 | * |
| 172 | * The code in this function is a little weird because I couldn't comprehend |
| 173 | * the bucket going up when the battery level was going down. --joeo |
| 174 | * |
| 175 | * 1 means that the battery is "ok" |
| 176 | * 0 means that the battery is between "ok" and what we should warn about. |
| 177 | * less than 0 means that the battery is low |
| 178 | */ |
| 179 | private int findBatteryLevelBucket(int level) { |
| 180 | if (level >= mLowBatteryAlertCloseLevel) { |
| 181 | return 1; |
| 182 | } |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 183 | if (level > mLowBatteryReminderLevels[0]) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | final int N = mLowBatteryReminderLevels.length; |
| 187 | for (int i=N-1; i>=0; i--) { |
| 188 | if (level <= mLowBatteryReminderLevels[i]) { |
| 189 | return -1-i; |
| 190 | } |
| 191 | } |
| 192 | throw new RuntimeException("not possible!"); |
| 193 | } |
| 194 | |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 195 | @VisibleForTesting |
| 196 | final class Receiver extends BroadcastReceiver { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 197 | |
| 198 | public void init() { |
| 199 | // Register for Intent broadcasts for... |
| 200 | IntentFilter filter = new IntentFilter(); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 201 | filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 202 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 203 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 204 | filter.addAction(Intent.ACTION_SCREEN_ON); |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 205 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 206 | filter.addAction(Intent.ACTION_POWER_DISCONNECTED); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 207 | mContext.registerReceiver(this, filter, null, mHandler); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public void onReceive(Context context, Intent intent) { |
| 212 | String action = intent.getAction(); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 213 | if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) { |
| 214 | ThreadUtils.postOnBackgroundThread(() -> { |
| 215 | if (mPowerManager.isPowerSaveMode()) { |
| 216 | mWarnings.dismissLowBatteryWarning(); |
| 217 | } |
| 218 | }); |
| 219 | } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 220 | final int oldBatteryLevel = mBatteryLevel; |
| 221 | mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100); |
| 222 | final int oldBatteryStatus = mBatteryStatus; |
| 223 | mBatteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, |
| 224 | BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 225 | final int oldPlugType = mPlugType; |
| 226 | mPlugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 1); |
| 227 | final int oldInvalidCharger = mInvalidCharger; |
| 228 | mInvalidCharger = intent.getIntExtra(BatteryManager.EXTRA_INVALID_CHARGER, 0); |
| 229 | |
| 230 | final boolean plugged = mPlugType != 0; |
| 231 | final boolean oldPlugged = oldPlugType != 0; |
| 232 | |
| 233 | int oldBucket = findBatteryLevelBucket(oldBatteryLevel); |
| 234 | int bucket = findBatteryLevelBucket(mBatteryLevel); |
| 235 | |
Daniel Sandler | 7198662 | 2011-07-26 13:06:49 -0400 | [diff] [blame] | 236 | if (DEBUG) { |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 237 | Slog.d(TAG, "buckets ....." + mLowBatteryAlertCloseLevel |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 238 | + " .. " + mLowBatteryReminderLevels[0] |
| 239 | + " .. " + mLowBatteryReminderLevels[1]); |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 240 | Slog.d(TAG, "level " + oldBatteryLevel + " --> " + mBatteryLevel); |
| 241 | Slog.d(TAG, "status " + oldBatteryStatus + " --> " + mBatteryStatus); |
| 242 | Slog.d(TAG, "plugType " + oldPlugType + " --> " + mPlugType); |
| 243 | Slog.d(TAG, "invalidCharger " + oldInvalidCharger + " --> " + mInvalidCharger); |
| 244 | Slog.d(TAG, "bucket " + oldBucket + " --> " + bucket); |
| 245 | Slog.d(TAG, "plugged " + oldPlugged + " --> " + plugged); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 246 | } |
| 247 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 248 | mWarnings.update(mBatteryLevel, bucket, mScreenOffTime); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 249 | if (oldInvalidCharger == 0 && mInvalidCharger != 0) { |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 250 | Slog.d(TAG, "showing invalid charger warning"); |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 251 | mWarnings.showInvalidChargerWarning(); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 252 | return; |
| 253 | } else if (oldInvalidCharger != 0 && mInvalidCharger == 0) { |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 254 | mWarnings.dismissInvalidChargerWarning(); |
| 255 | } else if (mWarnings.isInvalidChargerWarningShowing()) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 256 | // if invalid charger is showing, don't show low battery |
| 257 | return; |
| 258 | } |
| 259 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 260 | // Show the correct version of low battery warning if needed |
Salvador Martinez | 110f9a1 | 2018-01-31 09:57:17 -0800 | [diff] [blame] | 261 | ThreadUtils.postOnBackgroundThread(() -> { |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 262 | maybeShowBatteryWarning( |
| 263 | oldBatteryLevel, plugged, oldPlugged, oldBucket, bucket); |
Salvador Martinez | 110f9a1 | 2018-01-31 09:57:17 -0800 | [diff] [blame] | 264 | }); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 265 | |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 266 | } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 267 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 268 | } else if (Intent.ACTION_SCREEN_ON.equals(action)) { |
| 269 | mScreenOffTime = -1; |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 270 | } else if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 271 | mWarnings.userSwitched(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 272 | } else if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) { |
| 273 | updateTemperature(); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 274 | } else { |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 275 | Slog.w(TAG, "unknown intent: " + intent); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 280 | protected void maybeShowBatteryWarning(int oldBatteryLevel, boolean plugged, boolean oldPlugged, |
| 281 | int oldBucket, int bucket) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 282 | boolean isPowerSaver = mPowerManager.isPowerSaveMode(); |
| 283 | // only play SFX when the dialog comes up or the bucket changes |
| 284 | final boolean playSound = bucket != oldBucket || oldPlugged; |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 285 | final boolean hybridEnabled = mEnhancedEstimates.isHybridNotificationEnabled(); |
| 286 | if (hybridEnabled) { |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 287 | Estimate estimate = mLastEstimate; |
| 288 | if (estimate == null || mBatteryLevel != oldBatteryLevel) { |
| 289 | estimate = mEnhancedEstimates.getEstimate(); |
| 290 | mLastEstimate = estimate; |
| 291 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 292 | // Turbo is not always booted once SysUI is running so we have ot make sure we actually |
| 293 | // get data back |
| 294 | if (estimate != null) { |
| 295 | mTimeRemaining = estimate.estimateMillis; |
| 296 | mWarnings.updateEstimate(estimate); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 297 | mWarnings.updateThresholds(mEnhancedEstimates.getLowWarningThreshold(), |
| 298 | mEnhancedEstimates.getSevereWarningThreshold()); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 299 | |
| 300 | // if we are now over 45% battery & 6 hours remaining we can trigger hybrid |
| 301 | // notification again |
| 302 | if (mBatteryLevel >= CHARGE_CYCLE_PERCENT_RESET |
| 303 | && mTimeRemaining > SIX_HOURS_MILLIS) { |
| 304 | mLowWarningShownThisChargeCycle = false; |
| 305 | mSevereWarningShownThisChargeCycle = false; |
| 306 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 310 | if (shouldShowLowBatteryWarning(plugged, oldPlugged, oldBucket, bucket, |
| 311 | mTimeRemaining, isPowerSaver, mBatteryStatus)) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 312 | mWarnings.showLowBatteryWarning(playSound); |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 313 | |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 314 | // mark if we've already shown a warning this cycle. This will prevent the notification |
| 315 | // trigger from spamming users by only showing low/critical warnings once per cycle |
| 316 | if (hybridEnabled) { |
| 317 | if (mTimeRemaining < mEnhancedEstimates.getSevereWarningThreshold() |
| 318 | || mBatteryLevel < mLowBatteryReminderLevels[1]) { |
| 319 | mSevereWarningShownThisChargeCycle = true; |
| 320 | } else { |
| 321 | mLowWarningShownThisChargeCycle = true; |
| 322 | } |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 323 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 324 | } else if (shouldDismissLowBatteryWarning(plugged, oldBucket, bucket, mTimeRemaining, |
| 325 | isPowerSaver)) { |
| 326 | mWarnings.dismissLowBatteryWarning(); |
| 327 | } else { |
| 328 | mWarnings.updateLowBatteryWarning(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | @VisibleForTesting |
| 333 | boolean shouldShowLowBatteryWarning(boolean plugged, boolean oldPlugged, int oldBucket, |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 334 | int bucket, long timeRemaining, boolean isPowerSaver, int batteryStatus) { |
| 335 | if (mEnhancedEstimates.isHybridNotificationEnabled()) { |
| 336 | // triggering logic when enhanced estimate is available |
| 337 | return isEnhancedTrigger(plugged, timeRemaining, isPowerSaver, batteryStatus); |
| 338 | } |
| 339 | // legacy triggering logic |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 340 | return !plugged |
| 341 | && !isPowerSaver |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 342 | && (((bucket < oldBucket || oldPlugged) && bucket < 0)) |
| 343 | && batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 346 | @VisibleForTesting |
| 347 | boolean shouldDismissLowBatteryWarning(boolean plugged, int oldBucket, int bucket, |
| 348 | long timeRemaining, boolean isPowerSaver) { |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 349 | final boolean hybridEnabled = mEnhancedEstimates.isHybridNotificationEnabled(); |
| 350 | final boolean hybridWouldDismiss = hybridEnabled |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 351 | && timeRemaining > mEnhancedEstimates.getLowWarningThreshold(); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 352 | final boolean standardWouldDismiss = (bucket > oldBucket && bucket > 0); |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 353 | return (isPowerSaver && !hybridEnabled) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 354 | || plugged |
| 355 | || (standardWouldDismiss && (!mEnhancedEstimates.isHybridNotificationEnabled() |
| 356 | || hybridWouldDismiss)); |
| 357 | } |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 358 | |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 359 | private boolean isEnhancedTrigger(boolean plugged, long timeRemaining, boolean isPowerSaver, |
| 360 | int batteryStatus) { |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 361 | if (plugged || batteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 362 | return false; |
| 363 | } |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 364 | int warnLevel = mLowBatteryReminderLevels[0]; |
| 365 | int critLevel = mLowBatteryReminderLevels[1]; |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 366 | |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 367 | // Only show the low warning once per charge cycle & no battery saver |
| 368 | final boolean canShowWarning = !mLowWarningShownThisChargeCycle && !isPowerSaver |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 369 | && (timeRemaining < mEnhancedEstimates.getLowWarningThreshold() |
| 370 | || mBatteryLevel <= warnLevel); |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 371 | |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 372 | // Only show the severe warning once per charge cycle |
| 373 | final boolean canShowSevereWarning = !mSevereWarningShownThisChargeCycle |
| 374 | && (timeRemaining < mEnhancedEstimates.getSevereWarningThreshold() |
| 375 | || mBatteryLevel <= critLevel); |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 376 | |
| 377 | return canShowWarning || canShowSevereWarning; |
| 378 | } |
| 379 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 380 | private void initTemperature() { |
| 381 | initTemperatureWarning(); |
| 382 | initTemperatureAlarm(); |
| 383 | if (mEnableTemperatureWarning || mEnableTemperatureAlarm) { |
| 384 | bindThermalService(); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | private void initTemperatureAlarm() { |
| 389 | mEnableTemperatureAlarm = mContext.getResources().getInteger( |
| 390 | R.integer.config_showTemperatureAlarm) != 0; |
| 391 | if (!mEnableTemperatureAlarm) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 395 | float[] throttlingTemps = mHardwarePropertiesManager.getDeviceTemperatures( |
| 396 | HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN, |
| 397 | HardwarePropertiesManager.TEMPERATURE_THROTTLING); |
| 398 | if (throttlingTemps == null || throttlingTemps.length < TEMPERATURE_OVERHEAT_ALARM + 1) { |
| 399 | mThresholdAlarmTemp = mContext.getResources().getInteger( |
| 400 | R.integer.config_alarmTemperature); |
| 401 | } else { |
| 402 | mThresholdAlarmTemp = throttlingTemps[TEMPERATURE_OVERHEAT_ALARM]; |
| 403 | } |
| 404 | mThresholdAlarmTempTolerance = mThresholdAlarmTemp - mContext.getResources().getInteger( |
| 405 | R.integer.config_alarmTemperatureTolerance); |
| 406 | Log.d(TAG, "mThresholdAlarmTemp=" + mThresholdAlarmTemp + ", mThresholdAlarmTempTolerance=" |
| 407 | + mThresholdAlarmTempTolerance); |
| 408 | } |
| 409 | |
| 410 | private void initTemperatureWarning() { |
| 411 | ContentResolver resolver = mContext.getContentResolver(); |
| 412 | Resources resources = mContext.getResources(); |
| 413 | mEnableTemperatureWarning = Settings.Global.getInt(resolver, |
| 414 | Settings.Global.SHOW_TEMPERATURE_WARNING, |
| 415 | resources.getInteger(R.integer.config_showTemperatureWarning)) != 0; |
| 416 | if (!mEnableTemperatureWarning) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | mThresholdWarningTemp = Settings.Global.getFloat(resolver, |
| 421 | Settings.Global.WARNING_TEMPERATURE, |
Andrew Sapperstein | e335256 | 2017-01-20 15:41:03 -0800 | [diff] [blame] | 422 | resources.getInteger(R.integer.config_warningTemperature)); |
Andrew Sapperstein | 7518471 | 2017-01-05 16:45:37 -0800 | [diff] [blame] | 423 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 424 | if (mThresholdWarningTemp < 0f) { |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 425 | // Get the shutdown temperature, adjust for warning tolerance. |
Andrew Sapperstein | 7518471 | 2017-01-05 16:45:37 -0800 | [diff] [blame] | 426 | float[] throttlingTemps = mHardwarePropertiesManager.getDeviceTemperatures( |
| 427 | HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN, |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 428 | HardwarePropertiesManager.TEMPERATURE_SHUTDOWN); |
Andrew Sapperstein | 7518471 | 2017-01-05 16:45:37 -0800 | [diff] [blame] | 429 | if (throttlingTemps == null |
| 430 | || throttlingTemps.length == 0 |
| 431 | || throttlingTemps[0] == HardwarePropertiesManager.UNDEFINED_TEMPERATURE) { |
| 432 | return; |
| 433 | } |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 434 | mThresholdWarningTemp = throttlingTemps[0] - resources.getInteger( |
| 435 | R.integer.config_warningTemperatureTolerance); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 436 | } |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 437 | } |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 438 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 439 | private void bindThermalService() { |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 440 | if (mThermalService == null) { |
| 441 | // Enable push notifications of throttling from vendor thermal |
| 442 | // management subsystem via thermalservice, in addition to our |
| 443 | // usual polling, to react to temperature jumps more quickly. |
| 444 | IBinder b = ServiceManager.getService("thermalservice"); |
| 445 | |
| 446 | if (b != null) { |
| 447 | mThermalService = IThermalService.Stub.asInterface(b); |
| 448 | try { |
| 449 | mThermalService.registerThermalEventListener( |
| 450 | new ThermalEventListener()); |
| 451 | } catch (RemoteException e) { |
| 452 | // Should never happen. |
| 453 | } |
| 454 | } else { |
| 455 | Slog.w(TAG, "cannot find thermalservice, no throttling push notifications"); |
| 456 | } |
| 457 | } |
| 458 | |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 459 | setNextLogTime(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 460 | |
Adam Lesinski | 1aab3fa | 2017-07-31 13:42:00 -0700 | [diff] [blame] | 461 | // This initialization method may be called on a configuration change. Only one set of |
| 462 | // ongoing callbacks should be occurring, so remove any now. updateTemperatureWarning will |
| 463 | // schedule an ongoing callback. |
| 464 | mHandler.removeCallbacks(mUpdateTempCallback); |
| 465 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 466 | // We have passed all of the checks, start checking the temp |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 467 | updateTemperature(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 470 | private void showThermalShutdownDialog() { |
| 471 | if (mPowerManager.getLastShutdownReason() |
| 472 | == PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN) { |
| 473 | mWarnings.showThermalShutdownWarning(); |
| 474 | } |
| 475 | } |
| 476 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 477 | /** |
| 478 | * Update temperature depend on config, there are type types of messages by design |
| 479 | * TEMPERATURE_OVERHEAT_WARNING Send generic notification to notify user |
| 480 | * TEMPERATURE_OVERHEAT_ALARM popup emergency Dialog for user |
| 481 | */ |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 482 | @VisibleForTesting |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 483 | protected void updateTemperature() { |
| 484 | if (!mEnableTemperatureWarning && !mEnableTemperatureAlarm) { |
| 485 | return; |
| 486 | } |
| 487 | |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 488 | float[] temps = mHardwarePropertiesManager.getDeviceTemperatures( |
| 489 | HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN, |
| 490 | HardwarePropertiesManager.TEMPERATURE_CURRENT); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 491 | if (temps == null) { |
| 492 | Log.e(TAG, "Can't query current temperature value from HardProps SKIN type"); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | final float[] temps_compat; |
| 497 | if (temps.length < TEMPERATURE_OVERHEAT_ALARM + 1) { |
| 498 | temps_compat = new float[] { temps[0], temps[0] }; |
| 499 | } else { |
| 500 | temps_compat = temps; |
| 501 | } |
| 502 | |
| 503 | if (mEnableTemperatureWarning) { |
| 504 | updateTemperatureWarning(temps_compat); |
| 505 | logTemperatureStats(TEMPERATURE_OVERHEAT_WARNING); |
| 506 | } |
| 507 | |
| 508 | if (mEnableTemperatureAlarm) { |
| 509 | updateTemperatureAlarm(temps_compat); |
| 510 | logTemperatureStats(TEMPERATURE_OVERHEAT_ALARM); |
| 511 | } |
| 512 | |
| 513 | mHandler.postDelayed(mUpdateTempCallback, TEMPERATURE_INTERVAL); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Update legacy overheat warning notification from skin temperatures |
| 518 | * |
| 519 | * @param temps the array include two types of value from DEVICE_TEMPERATURE_SKIN |
| 520 | * this function only obtain the value of temps[TEMPERATURE_OVERHEAT_WARNING] |
| 521 | */ |
| 522 | @VisibleForTesting |
| 523 | protected void updateTemperatureWarning(float[] temps) { |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 524 | if (temps.length != 0) { |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 525 | float temp = temps[TEMPERATURE_OVERHEAT_WARNING]; |
| 526 | mRecentSkinTemps[mWarningNumTemps++] = temp; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 527 | |
| 528 | StatusBar statusBar = getComponent(StatusBar.class); |
| 529 | if (statusBar != null && !statusBar.isDeviceInVrMode() |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 530 | && temp >= mThresholdWarningTemp) { |
| 531 | logAtTemperatureThreshold(TEMPERATURE_OVERHEAT_WARNING, temp, |
| 532 | mThresholdWarningTemp); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 533 | mWarnings.showHighTemperatureWarning(); |
Andrew Sapperstein | 65d8a5f | 2016-12-19 14:36:33 -0800 | [diff] [blame] | 534 | } else { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 535 | mWarnings.dismissHighTemperatureWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 540 | /** |
| 541 | * Update overheat alarm from skin temperatures |
| 542 | * OEM can config alarm with beep sound by config_alarmTemperatureBeepSound |
| 543 | * |
| 544 | * @param temps the array include two types of value from DEVICE_TEMPERATURE_SKIN |
| 545 | * this function only obtain the value of temps[TEMPERATURE_OVERHEAT_ALARM] |
| 546 | */ |
| 547 | @VisibleForTesting |
| 548 | protected void updateTemperatureAlarm(float[] temps) { |
| 549 | if (temps.length != 0) { |
| 550 | final float temp = temps[TEMPERATURE_OVERHEAT_ALARM]; |
| 551 | final boolean shouldBeepSound = mContext.getResources().getBoolean( |
| 552 | R.bool.config_alarmTemperatureBeepSound); |
| 553 | mRecentAlarmTemps[mAlarmNumTemps++] = temp; |
| 554 | if (temp >= mThresholdAlarmTemp && !mIsOverheatAlarming) { |
| 555 | mWarnings.notifyHighTemperatureAlarm(true /* overheat */, shouldBeepSound); |
| 556 | mIsOverheatAlarming = true; |
| 557 | } else if (temp <= mThresholdAlarmTempTolerance && mIsOverheatAlarming) { |
| 558 | mWarnings.notifyHighTemperatureAlarm(false /* overheat */, false /* beepSound */); |
| 559 | mIsOverheatAlarming = false; |
| 560 | } |
| 561 | logAtTemperatureThreshold(TEMPERATURE_OVERHEAT_ALARM, temp, mThresholdAlarmTemp); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | private void logAtTemperatureThreshold(int type, float temp, float thresholdTemp) { |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 566 | StringBuilder sb = new StringBuilder(); |
| 567 | sb.append("currentTemp=").append(temp) |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 568 | .append(",overheatType=").append(type) |
| 569 | .append(",isOverheatAlarm=").append(mIsOverheatAlarming) |
| 570 | .append(",thresholdTemp=").append(thresholdTemp) |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 571 | .append(",batteryStatus=").append(mBatteryStatus) |
| 572 | .append(",recentTemps="); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 573 | if (type == TEMPERATURE_OVERHEAT_WARNING) { |
| 574 | for (int i = 0; i < mWarningNumTemps; i++) { |
| 575 | sb.append(mRecentSkinTemps[i]).append(','); |
| 576 | } |
| 577 | } else { |
| 578 | for (int i = 0; i < mAlarmNumTemps; i++) { |
| 579 | sb.append(mRecentAlarmTemps[i]).append(','); |
| 580 | } |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 581 | } |
| 582 | Slog.i(TAG, sb.toString()); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Calculates and logs min, max, and average |
| 587 | * {@link HardwarePropertiesManager#DEVICE_TEMPERATURE_SKIN} over the past |
| 588 | * {@link #TEMPERATURE_LOGGING_INTERVAL}. |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 589 | * @param type TEMPERATURE_OVERHEAT_WARNING Send generic notification to notify user |
| 590 | * TEMPERATURE_OVERHEAT_ALARM Popup emergency Dialog for user |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 591 | */ |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 592 | private void logTemperatureStats(int type) { |
| 593 | int numTemp = type == TEMPERATURE_OVERHEAT_ALARM ? mAlarmNumTemps : mWarningNumTemps; |
| 594 | if (mNextLogTime > System.currentTimeMillis() && numTemp != MAX_RECENT_TEMPS) { |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 595 | return; |
| 596 | } |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 597 | float[] recentTemps = |
| 598 | type == TEMPERATURE_OVERHEAT_ALARM ? mRecentAlarmTemps : mRecentSkinTemps; |
| 599 | if (numTemp > 0) { |
| 600 | float sum = recentTemps[0], min = recentTemps[0], max = recentTemps[0]; |
| 601 | for (int i = 1; i < numTemp; i++) { |
| 602 | float temp = recentTemps[i]; |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 603 | sum += temp; |
| 604 | if (temp > max) { |
| 605 | max = temp; |
| 606 | } |
| 607 | if (temp < min) { |
| 608 | min = temp; |
| 609 | } |
| 610 | } |
| 611 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 612 | float avg = sum / numTemp; |
| 613 | Slog.i(TAG, "Type=" + type + ",avg=" + avg + ",min=" + min + ",max=" + max); |
| 614 | String t = type == TEMPERATURE_OVERHEAT_WARNING ? "skin" : "alarm"; |
| 615 | MetricsLogger.histogram(mContext, |
| 616 | String.format(Locale.ENGLISH, "device_%1$s_temp_avg", t), (int) avg); |
| 617 | MetricsLogger.histogram(mContext, |
| 618 | String.format(Locale.ENGLISH, "device_%1$s_temp_min", t), (int) min); |
| 619 | MetricsLogger.histogram(mContext, |
| 620 | String.format(Locale.ENGLISH, "device_%1$s_temp_max", t), (int) max); |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 621 | } |
| 622 | setNextLogTime(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 623 | if (type == TEMPERATURE_OVERHEAT_ALARM) { |
| 624 | mAlarmNumTemps = 0; |
| 625 | } else { |
| 626 | mWarningNumTemps = 0; |
| 627 | } |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | private void setNextLogTime() { |
| 631 | mNextLogTime = System.currentTimeMillis() + TEMPERATURE_LOGGING_INTERVAL; |
| 632 | } |
| 633 | |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 634 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 635 | pw.print("mLowBatteryAlertCloseLevel="); |
| 636 | pw.println(mLowBatteryAlertCloseLevel); |
| 637 | pw.print("mLowBatteryReminderLevels="); |
| 638 | pw.println(Arrays.toString(mLowBatteryReminderLevels)); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 639 | pw.print("mBatteryLevel="); |
| 640 | pw.println(Integer.toString(mBatteryLevel)); |
| 641 | pw.print("mBatteryStatus="); |
| 642 | pw.println(Integer.toString(mBatteryStatus)); |
| 643 | pw.print("mPlugType="); |
| 644 | pw.println(Integer.toString(mPlugType)); |
| 645 | pw.print("mInvalidCharger="); |
| 646 | pw.println(Integer.toString(mInvalidCharger)); |
Daniel Sandler | dea6462 | 2013-09-23 16:05:57 -0400 | [diff] [blame] | 647 | pw.print("mScreenOffTime="); |
| 648 | pw.print(mScreenOffTime); |
| 649 | if (mScreenOffTime >= 0) { |
| 650 | pw.print(" ("); |
| 651 | pw.print(SystemClock.elapsedRealtime() - mScreenOffTime); |
| 652 | pw.print(" ago)"); |
| 653 | } |
| 654 | pw.println(); |
| 655 | pw.print("soundTimeout="); |
| 656 | pw.println(Settings.Global.getInt(mContext.getContentResolver(), |
| 657 | Settings.Global.LOW_BATTERY_SOUND_TIMEOUT, 0)); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 658 | pw.print("bucket: "); |
| 659 | pw.println(Integer.toString(findBatteryLevelBucket(mBatteryLevel))); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 660 | pw.print("mThresholdWarningTemp="); |
| 661 | pw.println(Float.toString(mThresholdWarningTemp)); |
| 662 | pw.print("mThresholdAlarmTemp="); |
| 663 | pw.println(Float.toString(mThresholdAlarmTemp)); |
Andrew Sapperstein | a6ed7f8 | 2017-02-01 17:13:08 -0800 | [diff] [blame] | 664 | pw.print("mNextLogTime="); |
| 665 | pw.println(Long.toString(mNextLogTime)); |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 666 | mWarnings.dump(pw); |
| 667 | } |
| 668 | |
| 669 | public interface WarningsUI { |
| 670 | void update(int batteryLevel, int bucket, long screenOffTime); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 671 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 672 | void updateEstimate(Estimate estimate); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 673 | |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 674 | void updateThresholds(long lowThreshold, long severeThreshold); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 675 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 676 | void dismissLowBatteryWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 677 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 678 | void showLowBatteryWarning(boolean playSound); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 679 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 680 | void dismissInvalidChargerWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 681 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 682 | void showInvalidChargerWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 683 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 684 | void updateLowBatteryWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 685 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 686 | boolean isInvalidChargerWarningShowing(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 687 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 688 | void dismissHighTemperatureWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 689 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 690 | void showHighTemperatureWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 691 | |
| 692 | /** |
| 693 | * PowerUI detect thermal overheat, notify to popup alert dialog strongly. |
| 694 | * Alarm with beep sound with un-dismissible dialog until device cool down below threshold, |
| 695 | * then popup another dismissible dialog to user. |
| 696 | * |
| 697 | * @param overheat whether device temperature over threshold |
| 698 | * @param beepSound should beep sound once overheat |
| 699 | */ |
| 700 | void notifyHighTemperatureAlarm(boolean overheat, boolean beepSound); |
| 701 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 702 | void showThermalShutdownWarning(); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 703 | |
John Spurlock | ed452c5 | 2014-03-06 12:02:31 -0500 | [diff] [blame] | 704 | void dump(PrintWriter pw); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 705 | |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 706 | void userSwitched(); |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 707 | } |
Joe Onorato | 10523b4d | 2010-10-25 10:42:46 -0700 | [diff] [blame] | 708 | |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 709 | // Thermal event received from vendor thermal management subsystem |
| 710 | private final class ThermalEventListener extends IThermalEventListener.Stub { |
| 711 | @Override public void notifyThrottling(boolean isThrottling, Temperature temp) { |
| 712 | // Trigger an update of the temperature warning. Only one |
| 713 | // callback can be enabled at a time, so remove any existing |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 714 | // callback; updateTemperature will schedule another one. |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 715 | mHandler.removeCallbacks(mUpdateTempCallback); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 716 | updateTemperature(); |
Todd Poynor | fb95e12 | 2017-10-04 13:00:32 -0700 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |