John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 19 | import android.app.KeyguardManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 20 | import android.app.Notification; |
| 21 | import android.app.NotificationManager; |
| 22 | import android.app.PendingIntent; |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 23 | import android.content.ActivityNotFoundException; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 26 | import android.content.Context; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 29 | import android.media.AudioAttributes; |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 32 | import android.os.Handler; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 33 | import android.os.Looper; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 34 | import android.os.PowerManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 35 | import android.os.UserHandle; |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 36 | import android.provider.Settings; |
| 37 | import android.provider.Settings.Global; |
Salvador Martinez | f422b1a | 2019-03-27 10:35:57 -0700 | [diff] [blame] | 38 | import android.provider.Settings.Secure; |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 39 | import android.text.Annotation; |
| 40 | import android.text.Layout; |
| 41 | import android.text.SpannableString; |
| 42 | import android.text.SpannableStringBuilder; |
| 43 | import android.text.TextPaint; |
| 44 | import android.text.TextUtils; |
| 45 | import android.text.method.LinkMovementMethod; |
| 46 | import android.text.style.URLSpan; |
| 47 | import android.util.Log; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 48 | import android.util.Slog; |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 49 | import android.view.View; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 50 | import android.view.WindowManager; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 51 | |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 52 | import androidx.annotation.VisibleForTesting; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 53 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 54 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 55 | import com.android.settingslib.Utils; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 56 | import com.android.settingslib.fuelgauge.BatterySaverUtils; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 57 | import com.android.settingslib.utils.PowerUtil; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 58 | import com.android.systemui.Dependency; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 59 | import com.android.systemui.R; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 60 | import com.android.systemui.SystemUI; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 61 | import com.android.systemui.plugins.ActivityStarter; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 62 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 63 | import com.android.systemui.util.NotificationChannels; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 64 | import com.android.systemui.volume.Events; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 65 | |
| 66 | import java.io.PrintWriter; |
Elliott Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 67 | import java.text.NumberFormat; |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 68 | import java.util.Locale; |
| 69 | import java.util.Objects; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 70 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 71 | import javax.inject.Inject; |
| 72 | import javax.inject.Singleton; |
| 73 | |
| 74 | /** |
| 75 | */ |
| 76 | @Singleton |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 77 | public class PowerNotificationWarnings implements PowerUI.WarningsUI { |
Salvador Martinez | f422b1a | 2019-03-27 10:35:57 -0700 | [diff] [blame] | 78 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 79 | private static final String TAG = PowerUI.TAG + ".Notification"; |
| 80 | private static final boolean DEBUG = PowerUI.DEBUG; |
| 81 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 82 | private static final String TAG_BATTERY = "low_battery"; |
| 83 | private static final String TAG_TEMPERATURE = "high_temp"; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 84 | private static final String TAG_AUTO_SAVER = "auto_saver"; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 85 | |
| 86 | private static final int SHOWING_NOTHING = 0; |
| 87 | private static final int SHOWING_WARNING = 1; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 88 | private static final int SHOWING_INVALID_CHARGER = 3; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 89 | private static final int SHOWING_AUTO_SAVER_SUGGESTION = 4; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 90 | private static final String[] SHOWING_STRINGS = { |
| 91 | "SHOWING_NOTHING", |
| 92 | "SHOWING_WARNING", |
| 93 | "SHOWING_SAVER", |
| 94 | "SHOWING_INVALID_CHARGER", |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 95 | "SHOWING_AUTO_SAVER_SUGGESTION", |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 98 | private static final String ACTION_SHOW_BATTERY_SETTINGS = "PNW.batterySettings"; |
| 99 | private static final String ACTION_START_SAVER = "PNW.startSaver"; |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 100 | private static final String ACTION_DISMISSED_WARNING = "PNW.dismissedWarning"; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 101 | private static final String ACTION_CLICKED_TEMP_WARNING = "PNW.clickedTempWarning"; |
| 102 | private static final String ACTION_DISMISSED_TEMP_WARNING = "PNW.dismissedTempWarning"; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 103 | private static final String ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING = |
| 104 | "PNW.clickedThermalShutdownWarning"; |
| 105 | private static final String ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING = |
| 106 | "PNW.dismissedThermalShutdownWarning"; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 107 | private static final String ACTION_SHOW_START_SAVER_CONFIRMATION = |
| 108 | BatterySaverUtils.ACTION_SHOW_START_SAVER_CONFIRMATION; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 109 | private static final String ACTION_SHOW_AUTO_SAVER_SUGGESTION = |
| 110 | BatterySaverUtils.ACTION_SHOW_AUTO_SAVER_SUGGESTION; |
| 111 | private static final String ACTION_DISMISS_AUTO_SAVER_SUGGESTION = |
| 112 | "PNW.dismissAutoSaverSuggestion"; |
| 113 | |
| 114 | private static final String ACTION_ENABLE_AUTO_SAVER = |
| 115 | "PNW.enableAutoSaver"; |
| 116 | private static final String ACTION_AUTO_SAVER_NO_THANKS = |
| 117 | "PNW.autoSaverNoThanks"; |
| 118 | |
| 119 | private static final String SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING = |
| 120 | "android.settings.BATTERY_SAVER_SETTINGS"; |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 121 | public static final String BATTERY_SAVER_SCHEDULE_SCREEN_INTENT_ACTION = |
| 122 | "com.android.settings.BATTERY_SAVER_SCHEDULE_SETTINGS"; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 123 | |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 124 | private static final String BATTERY_SAVER_DESCRIPTION_URL_KEY = "url"; |
| 125 | |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 126 | private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder() |
| 127 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 128 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 129 | .build(); |
Salvador Martinez | f422b1a | 2019-03-27 10:35:57 -0700 | [diff] [blame] | 130 | public static final String EXTRA_CONFIRM_ONLY = "extra_confirm_only"; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 131 | |
| 132 | private final Context mContext; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 133 | private final NotificationManager mNoMan; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 134 | private final PowerManager mPowerMan; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 135 | private final KeyguardManager mKeyguard; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 136 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 137 | private final Receiver mReceiver = new Receiver(); |
| 138 | private final Intent mOpenBatterySettings = settings(Intent.ACTION_POWER_USAGE_SUMMARY); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 139 | |
| 140 | private int mBatteryLevel; |
| 141 | private int mBucket; |
| 142 | private long mScreenOffTime; |
| 143 | private int mShowing; |
| 144 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 145 | private long mWarningTriggerTimeMs; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 146 | private boolean mWarning; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 147 | private boolean mShowAutoSaverSuggestion; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 148 | private boolean mPlaySound; |
| 149 | private boolean mInvalidCharger; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 150 | private SystemUIDialog mSaverConfirmation; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 151 | private SystemUIDialog mSaverEnabledConfirmation; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 152 | private boolean mHighTempWarning; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 153 | private SystemUIDialog mHighTempDialog; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 154 | private SystemUIDialog mThermalShutdownDialog; |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 155 | @VisibleForTesting SystemUIDialog mUsbHighTempDialog; |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 156 | private BatteryStateSnapshot mCurrentBatterySnapshot; |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 157 | private ActivityStarter mActivityStarter; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 158 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 159 | /** |
| 160 | */ |
| 161 | @Inject |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 162 | public PowerNotificationWarnings(Context context, ActivityStarter activityStarter) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 163 | mContext = context; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 164 | mNoMan = mContext.getSystemService(NotificationManager.class); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 165 | mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 166 | mKeyguard = mContext.getSystemService(KeyguardManager.class); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 167 | mReceiver.init(); |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 168 | mActivityStarter = activityStarter; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void dump(PrintWriter pw) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 173 | pw.print("mWarning="); pw.println(mWarning); |
| 174 | pw.print("mPlaySound="); pw.println(mPlaySound); |
| 175 | pw.print("mInvalidCharger="); pw.println(mInvalidCharger); |
| 176 | pw.print("mShowing="); pw.println(SHOWING_STRINGS[mShowing]); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 177 | pw.print("mSaverConfirmation="); pw.println(mSaverConfirmation != null ? "not null" : null); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 178 | pw.print("mSaverEnabledConfirmation="); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 179 | pw.print("mHighTempWarning="); pw.println(mHighTempWarning); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 180 | pw.print("mHighTempDialog="); pw.println(mHighTempDialog != null ? "not null" : null); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 181 | pw.print("mThermalShutdownDialog="); |
| 182 | pw.println(mThermalShutdownDialog != null ? "not null" : null); |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 183 | pw.print("mUsbHighTempDialog="); |
| 184 | pw.println(mUsbHighTempDialog != null ? "not null" : null); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 187 | private int getLowBatteryAutoTriggerDefaultLevel() { |
| 188 | return mContext.getResources().getInteger( |
| 189 | com.android.internal.R.integer.config_lowBatteryAutoTriggerDefaultLevel); |
| 190 | } |
| 191 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 192 | @Override |
| 193 | public void update(int batteryLevel, int bucket, long screenOffTime) { |
| 194 | mBatteryLevel = batteryLevel; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 195 | if (bucket >= 0) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 196 | mWarningTriggerTimeMs = 0; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 197 | } else if (bucket < mBucket) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 198 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 199 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 200 | mBucket = bucket; |
| 201 | mScreenOffTime = screenOffTime; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 204 | @Override |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 205 | public void updateSnapshot(BatteryStateSnapshot snapshot) { |
| 206 | mCurrentBatterySnapshot = snapshot; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 207 | } |
| 208 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 209 | private void updateNotification() { |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 210 | if (DEBUG) Slog.d(TAG, "updateNotification mWarning=" + mWarning + " mPlaySound=" |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 211 | + mPlaySound + " mInvalidCharger=" + mInvalidCharger); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 212 | if (mInvalidCharger) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 213 | showInvalidChargerNotification(); |
| 214 | mShowing = SHOWING_INVALID_CHARGER; |
| 215 | } else if (mWarning) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 216 | showWarningNotification(); |
| 217 | mShowing = SHOWING_WARNING; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 218 | } else if (mShowAutoSaverSuggestion) { |
Makoto Onuki | 60b8f18 | 2018-05-07 13:56:40 -0700 | [diff] [blame] | 219 | // Once we showed the notification, don't show it again until it goes SHOWING_NOTHING. |
| 220 | // This shouldn't be needed, because we have a delete intent on this notification |
| 221 | // so when it's dismissed we should notice it and clear mShowAutoSaverSuggestion, |
| 222 | // However we double check here just in case the dismiss intent broadcast is delayed. |
| 223 | if (mShowing != SHOWING_AUTO_SAVER_SUGGESTION) { |
| 224 | showAutoSaverSuggestionNotification(); |
| 225 | } |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 226 | mShowing = SHOWING_AUTO_SAVER_SUGGESTION; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 227 | } else { |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 228 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 229 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 230 | mNoMan.cancelAsUser(TAG_AUTO_SAVER, |
| 231 | SystemMessage.NOTE_AUTO_SAVER_SUGGESTION, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 232 | mShowing = SHOWING_NOTHING; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private void showInvalidChargerNotification() { |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 237 | final Notification.Builder nb = |
| 238 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 239 | .setSmallIcon(R.drawable.ic_power_low) |
| 240 | .setWhen(0) |
| 241 | .setShowWhen(false) |
| 242 | .setOngoing(true) |
| 243 | .setContentTitle(mContext.getString(R.string.invalid_charger_title)) |
| 244 | .setContentText(mContext.getString(R.string.invalid_charger_text)) |
| 245 | .setColor(mContext.getColor( |
| 246 | com.android.internal.R.color.system_notification_accent_color)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 247 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 248 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 249 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
| 250 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 253 | protected void showWarningNotification() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 254 | final String percentage = NumberFormat.getPercentInstance() |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 255 | .format((double) mCurrentBatterySnapshot.getBatteryLevel() / 100.0); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 256 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 257 | // get shared standard notification copy |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 258 | String title = mContext.getString(R.string.battery_low_title); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 259 | String contentText; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 260 | |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 261 | // get correct content text if notification is hybrid or not |
| 262 | if (mCurrentBatterySnapshot.isHybrid()) { |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 263 | contentText = getHybridContentString(percentage); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 264 | } else { |
| 265 | contentText = mContext.getString(R.string.battery_low_percent_format, percentage); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 268 | final Notification.Builder nb = |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 269 | new Notification.Builder(mContext, NotificationChannels.BATTERY) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 270 | .setSmallIcon(R.drawable.ic_power_low) |
| 271 | // Bump the notification when the bucket dropped. |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 272 | .setWhen(mWarningTriggerTimeMs) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 273 | .setShowWhen(false) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 274 | .setContentText(contentText) |
Salvador Martinez | 086ab74 | 2018-04-03 13:05:39 -0700 | [diff] [blame] | 275 | .setContentTitle(title) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 276 | .setOnlyAlertOnce(true) |
| 277 | .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_WARNING)) |
Salvador Martinez | 086ab74 | 2018-04-03 13:05:39 -0700 | [diff] [blame] | 278 | .setStyle(new Notification.BigTextStyle().bigText(contentText)) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 279 | .setVisibility(Notification.VISIBILITY_PUBLIC); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 280 | if (hasBatterySettings()) { |
| 281 | nb.setContentIntent(pendingBroadcast(ACTION_SHOW_BATTERY_SETTINGS)); |
| 282 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 283 | // Make the notification red if the percentage goes below a certain amount or the time |
| 284 | // remaining estimate is disabled |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 285 | if (!mCurrentBatterySnapshot.isHybrid() || mBucket < 0 |
| 286 | || mCurrentBatterySnapshot.getTimeRemainingMillis() |
| 287 | < mCurrentBatterySnapshot.getSevereThresholdMillis()) { |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 288 | nb.setColor(Utils.getColorAttrDefaultColor(mContext, android.R.attr.colorError)); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 289 | } |
Salvador Martinez | c25604b | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 290 | |
| 291 | if (!mPowerMan.isPowerSaveMode()) { |
| 292 | nb.addAction(0, |
| 293 | mContext.getString(R.string.battery_saver_start_action), |
| 294 | pendingBroadcast(ACTION_START_SAVER)); |
| 295 | } |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 296 | nb.setOnlyAlertOnce(!mPlaySound); |
| 297 | mPlaySound = false; |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 298 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 299 | final Notification n = nb.build(); |
| 300 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 301 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 304 | private void showAutoSaverSuggestionNotification() { |
| 305 | final Notification.Builder nb = |
| 306 | new Notification.Builder(mContext, NotificationChannels.HINTS) |
| 307 | .setSmallIcon(R.drawable.ic_power_saver) |
| 308 | .setWhen(0) |
| 309 | .setShowWhen(false) |
| 310 | .setContentTitle(mContext.getString(R.string.auto_saver_title)) |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 311 | .setContentText(mContext.getString(R.string.auto_saver_text)); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 312 | nb.setContentIntent(pendingBroadcast(ACTION_ENABLE_AUTO_SAVER)); |
| 313 | nb.setDeleteIntent(pendingBroadcast(ACTION_DISMISS_AUTO_SAVER_SUGGESTION)); |
| 314 | nb.addAction(0, |
| 315 | mContext.getString(R.string.no_auto_saver_action), |
| 316 | pendingBroadcast(ACTION_AUTO_SAVER_NO_THANKS)); |
| 317 | |
| 318 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
| 319 | |
| 320 | final Notification n = nb.build(); |
| 321 | mNoMan.notifyAsUser( |
| 322 | TAG_AUTO_SAVER, SystemMessage.NOTE_AUTO_SAVER_SUGGESTION, n, UserHandle.ALL); |
| 323 | } |
| 324 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 325 | private String getHybridContentString(String percentage) { |
| 326 | return PowerUtil.getBatteryRemainingStringFormatted( |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 327 | mContext, |
| 328 | mCurrentBatterySnapshot.getTimeRemainingMillis(), |
| 329 | percentage, |
| 330 | mCurrentBatterySnapshot.isBasedOnUsage()); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 331 | } |
| 332 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 333 | private PendingIntent pendingBroadcast(String action) { |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 334 | return PendingIntent.getBroadcastAsUser(mContext, 0, |
Makoto Onuki | 60b8f18 | 2018-05-07 13:56:40 -0700 | [diff] [blame] | 335 | new Intent(action).setPackage(mContext.getPackageName()) |
| 336 | .setFlags(Intent.FLAG_RECEIVER_FOREGROUND), |
| 337 | 0, UserHandle.CURRENT); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | private static Intent settings(String action) { |
| 341 | return new Intent(action).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 342 | | Intent.FLAG_ACTIVITY_MULTIPLE_TASK |
| 343 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
| 344 | | Intent.FLAG_ACTIVITY_NO_HISTORY |
| 345 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public boolean isInvalidChargerWarningShowing() { |
| 350 | return mInvalidCharger; |
| 351 | } |
| 352 | |
| 353 | @Override |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 354 | public void dismissHighTemperatureWarning() { |
| 355 | if (!mHighTempWarning) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 356 | return; |
| 357 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 358 | dismissHighTemperatureWarningInternal(); |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 362 | * Internal only version of {@link #dismissHighTemperatureWarning()} that simply dismisses |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 363 | * the notification. As such, the notification will not show again until |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 364 | * {@link #dismissHighTemperatureWarning()} is called. |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 365 | */ |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 366 | private void dismissHighTemperatureWarningInternal() { |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 367 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, UserHandle.ALL); |
Sherry Huang | 18d2704 | 2019-03-19 21:45:28 +0800 | [diff] [blame] | 368 | mHighTempWarning = false; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 372 | public void showHighTemperatureWarning() { |
| 373 | if (mHighTempWarning) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 374 | return; |
| 375 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 376 | mHighTempWarning = true; |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 377 | final Notification.Builder nb = |
| 378 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 379 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 380 | .setWhen(0) |
| 381 | .setShowWhen(false) |
| 382 | .setContentTitle(mContext.getString(R.string.high_temp_title)) |
| 383 | .setContentText(mContext.getString(R.string.high_temp_notif_message)) |
| 384 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 385 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_TEMP_WARNING)) |
| 386 | .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_TEMP_WARNING)) |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 387 | .setColor(Utils.getColorAttrDefaultColor(mContext, |
| 388 | android.R.attr.colorError)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 389 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 390 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 391 | mNoMan.notifyAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, n, UserHandle.ALL); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 394 | private void showHighTemperatureDialog() { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 395 | if (mHighTempDialog != null) return; |
| 396 | final SystemUIDialog d = new SystemUIDialog(mContext); |
Andrew Sapperstein | e26dc3d | 2017-01-04 11:25:20 -0800 | [diff] [blame] | 397 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 398 | d.setTitle(R.string.high_temp_title); |
| 399 | d.setMessage(R.string.high_temp_dialog_message); |
| 400 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 401 | d.setShowForAllUsers(true); |
| 402 | d.setOnDismissListener(dialog -> mHighTempDialog = null); |
| 403 | d.show(); |
| 404 | mHighTempDialog = d; |
| 405 | } |
| 406 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 407 | @VisibleForTesting |
| 408 | void dismissThermalShutdownWarning() { |
| 409 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, UserHandle.ALL); |
| 410 | } |
| 411 | |
| 412 | private void showThermalShutdownDialog() { |
| 413 | if (mThermalShutdownDialog != null) return; |
| 414 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 415 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
| 416 | d.setTitle(R.string.thermal_shutdown_title); |
| 417 | d.setMessage(R.string.thermal_shutdown_dialog_message); |
| 418 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 419 | d.setShowForAllUsers(true); |
| 420 | d.setOnDismissListener(dialog -> mThermalShutdownDialog = null); |
| 421 | d.show(); |
| 422 | mThermalShutdownDialog = d; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void showThermalShutdownWarning() { |
| 427 | final Notification.Builder nb = |
| 428 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 429 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 430 | .setWhen(0) |
| 431 | .setShowWhen(false) |
| 432 | .setContentTitle(mContext.getString(R.string.thermal_shutdown_title)) |
| 433 | .setContentText(mContext.getString(R.string.thermal_shutdown_message)) |
| 434 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 435 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING)) |
| 436 | .setDeleteIntent( |
| 437 | pendingBroadcast(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING)) |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 438 | .setColor(Utils.getColorAttrDefaultColor(mContext, |
| 439 | android.R.attr.colorError)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 440 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 441 | final Notification n = nb.build(); |
| 442 | mNoMan.notifyAsUser( |
| 443 | TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, n, UserHandle.ALL); |
| 444 | } |
| 445 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 446 | @Override |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 447 | public void showUsbHighTemperatureAlarm() { |
| 448 | mHandler.post(() -> showUsbHighTemperatureAlarmInternal()); |
| 449 | } |
| 450 | |
| 451 | private void showUsbHighTemperatureAlarmInternal() { |
| 452 | if (mUsbHighTempDialog != null) { |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | final SystemUIDialog d = new SystemUIDialog(mContext, R.style.Theme_SystemUI_Dialog_Alert); |
| 457 | d.setCancelable(false); |
| 458 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
| 459 | d.setTitle(R.string.high_temp_alarm_title); |
| 460 | d.setShowForAllUsers(true); |
| 461 | d.setMessage(mContext.getString(R.string.high_temp_alarm_notify_message, "")); |
| 462 | d.setPositiveButton((com.android.internal.R.string.ok), |
| 463 | (dialogInterface, which) -> mUsbHighTempDialog = null); |
| 464 | d.setNegativeButton((R.string.high_temp_alarm_help_care_steps), |
| 465 | (dialogInterface, which) -> { |
| 466 | final String contextString = mContext.getString( |
| 467 | R.string.high_temp_alarm_help_url); |
| 468 | final Intent helpIntent = new Intent(); |
| 469 | helpIntent.setClassName("com.android.settings", |
| 470 | "com.android.settings.HelpTrampoline"); |
| 471 | helpIntent.putExtra(Intent.EXTRA_TEXT, contextString); |
| 472 | Dependency.get(ActivityStarter.class).startActivity(helpIntent, |
| 473 | true /* dismissShade */, resultCode -> { |
| 474 | mUsbHighTempDialog = null; |
| 475 | }); |
| 476 | }); |
| 477 | d.setOnDismissListener(dialogInterface -> { |
| 478 | mUsbHighTempDialog = null; |
Will Brockman | 1d716b2 | 2019-11-26 10:10:07 -0500 | [diff] [blame] | 479 | Events.writeEvent(Events.EVENT_DISMISS_USB_OVERHEAT_ALARM, |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 480 | Events.DISMISS_REASON_USB_OVERHEAD_ALARM_CHANGED, |
| 481 | mKeyguard.isKeyguardLocked()); |
| 482 | }); |
| 483 | d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
| 484 | | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 485 | d.show(); |
| 486 | mUsbHighTempDialog = d; |
| 487 | |
Will Brockman | 1d716b2 | 2019-11-26 10:10:07 -0500 | [diff] [blame] | 488 | Events.writeEvent(Events.EVENT_SHOW_USB_OVERHEAT_ALARM, |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 489 | Events.SHOW_REASON_USB_OVERHEAD_ALARM_CHANGED, |
| 490 | mKeyguard.isKeyguardLocked()); |
| 491 | } |
| 492 | |
| 493 | @Override |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 494 | public void updateLowBatteryWarning() { |
| 495 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void dismissLowBatteryWarning() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 500 | if (DEBUG) Slog.d(TAG, "dismissing low battery warning: level=" + mBatteryLevel); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 501 | dismissLowBatteryNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | private void dismissLowBatteryNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 505 | if (mWarning) Slog.i(TAG, "dismissing low battery notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 506 | mWarning = false; |
| 507 | updateNotification(); |
| 508 | } |
| 509 | |
| 510 | private boolean hasBatterySettings() { |
| 511 | return mOpenBatterySettings.resolveActivity(mContext.getPackageManager()) != null; |
| 512 | } |
| 513 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 514 | @Override |
| 515 | public void showLowBatteryWarning(boolean playSound) { |
| 516 | Slog.i(TAG, |
| 517 | "show low battery warning: level=" + mBatteryLevel |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 518 | + " [" + mBucket + "] playSound=" + playSound); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 519 | mPlaySound = playSound; |
| 520 | mWarning = true; |
| 521 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 522 | } |
| 523 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 524 | @Override |
| 525 | public void dismissInvalidChargerWarning() { |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 526 | dismissInvalidChargerNotification(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | private void dismissInvalidChargerNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 530 | if (mInvalidCharger) Slog.i(TAG, "dismissing invalid charger notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 531 | mInvalidCharger = false; |
| 532 | updateNotification(); |
| 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public void showInvalidChargerWarning() { |
| 537 | mInvalidCharger = true; |
| 538 | updateNotification(); |
| 539 | } |
| 540 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 541 | private void showAutoSaverSuggestion() { |
| 542 | mShowAutoSaverSuggestion = true; |
| 543 | updateNotification(); |
| 544 | } |
| 545 | |
| 546 | private void dismissAutoSaverSuggestion() { |
| 547 | mShowAutoSaverSuggestion = false; |
| 548 | updateNotification(); |
| 549 | } |
| 550 | |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 551 | @Override |
| 552 | public void userSwitched() { |
| 553 | updateNotification(); |
| 554 | } |
| 555 | |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 556 | private void showStartSaverConfirmation(Bundle extras) { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 557 | if (mSaverConfirmation != null) return; |
| 558 | final SystemUIDialog d = new SystemUIDialog(mContext); |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 559 | final boolean confirmOnly = extras.getBoolean(BatterySaverUtils.EXTRA_CONFIRM_TEXT_ONLY); |
| 560 | final int batterySaverTriggerMode = |
| 561 | extras.getInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER, |
| 562 | PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); |
| 563 | final int batterySaverTriggerLevel = |
| 564 | extras.getInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER_LEVEL, 0); |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 565 | d.setMessage(getBatterySaverDescription()); |
| 566 | |
| 567 | // Sad hack for http://b/78261259 and http://b/78298335. Otherwise "Battery" may be split |
| 568 | // into "Bat-tery". |
| 569 | if (isEnglishLocale()) { |
| 570 | d.setMessageHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE); |
| 571 | } |
| 572 | // We need to set LinkMovementMethod to make the link clickable. |
| 573 | d.setMessageMovementMethod(LinkMovementMethod.getInstance()); |
| 574 | |
Salvador Martinez | f422b1a | 2019-03-27 10:35:57 -0700 | [diff] [blame] | 575 | if (confirmOnly) { |
| 576 | d.setTitle(R.string.battery_saver_confirmation_title_generic); |
| 577 | d.setPositiveButton(com.android.internal.R.string.confirm_battery_saver, |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 578 | (dialog, which) -> { |
| 579 | final ContentResolver resolver = mContext.getContentResolver(); |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 580 | Settings.Global.putInt( |
| 581 | resolver, |
| 582 | Global.AUTOMATIC_POWER_SAVE_MODE, |
| 583 | batterySaverTriggerMode); |
| 584 | Settings.Global.putInt( |
| 585 | resolver, |
| 586 | Global.LOW_POWER_MODE_TRIGGER_LEVEL, |
| 587 | batterySaverTriggerLevel); |
Raff Tsai | 92cdc54 | 2019-12-23 09:11:04 +0800 | [diff] [blame] | 588 | Secure.putIntForUser( |
Salvador Martinez | f3cae28 | 2019-05-01 13:12:50 -0700 | [diff] [blame] | 589 | resolver, |
| 590 | Secure.LOW_POWER_WARNING_ACKNOWLEDGED, |
Raff Tsai | 92cdc54 | 2019-12-23 09:11:04 +0800 | [diff] [blame] | 591 | 1, UserHandle.USER_CURRENT); |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 592 | }); |
Salvador Martinez | f422b1a | 2019-03-27 10:35:57 -0700 | [diff] [blame] | 593 | } else { |
| 594 | d.setTitle(R.string.battery_saver_confirmation_title); |
| 595 | d.setPositiveButton(R.string.battery_saver_confirmation_ok, |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 596 | (dialog, which) -> setSaverMode(true, false)); |
Salvador Martinez | f422b1a | 2019-03-27 10:35:57 -0700 | [diff] [blame] | 597 | d.setNegativeButton(android.R.string.cancel, null); |
| 598 | } |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 599 | d.setShowForAllUsers(true); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 600 | d.setOnDismissListener((dialog) -> mSaverConfirmation = null); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 601 | d.show(); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 602 | mSaverConfirmation = d; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Makoto Onuki | e8bbf95 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 605 | private boolean isEnglishLocale() { |
| 606 | return Objects.equals(Locale.getDefault().getLanguage(), |
| 607 | Locale.ENGLISH.getLanguage()); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Generates the message for the "want to start battery saver?" dialog with a "learn more" link. |
| 612 | */ |
| 613 | private CharSequence getBatterySaverDescription() { |
| 614 | final String learnMoreUrl = mContext.getText( |
| 615 | R.string.help_uri_battery_saver_learn_more_link_target).toString(); |
| 616 | |
| 617 | // If there's no link, use the string with no "learn more". |
| 618 | if (TextUtils.isEmpty(learnMoreUrl)) { |
| 619 | return mContext.getText( |
| 620 | com.android.internal.R.string.battery_saver_description); |
| 621 | } |
| 622 | |
| 623 | // If we have a link, use the string with the "learn more" link. |
| 624 | final CharSequence rawText = mContext.getText( |
| 625 | com.android.internal.R.string.battery_saver_description_with_learn_more); |
| 626 | final SpannableString message = new SpannableString(rawText); |
| 627 | final SpannableStringBuilder builder = new SpannableStringBuilder(message); |
| 628 | |
| 629 | // Look for the "learn more" part of the string, and set a URL span on it. |
| 630 | // We use a customized URLSpan to add FLAG_RECEIVER_FOREGROUND to the intent, and |
| 631 | // also to close the dialog. |
| 632 | for (Annotation annotation : message.getSpans(0, message.length(), Annotation.class)) { |
| 633 | final String key = annotation.getValue(); |
| 634 | |
| 635 | if (!BATTERY_SAVER_DESCRIPTION_URL_KEY.equals(key)) { |
| 636 | continue; |
| 637 | } |
| 638 | final int start = message.getSpanStart(annotation); |
| 639 | final int end = message.getSpanEnd(annotation); |
| 640 | |
| 641 | // Replace the "learn more" with a custom URL span, with |
| 642 | // - No underline. |
| 643 | // - When clicked, close the dialog and the notification shade. |
| 644 | final URLSpan urlSpan = new URLSpan(learnMoreUrl) { |
| 645 | @Override |
| 646 | public void updateDrawState(TextPaint ds) { |
| 647 | super.updateDrawState(ds); |
| 648 | ds.setUnderlineText(false); |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public void onClick(View widget) { |
| 653 | // Close the parent dialog. |
| 654 | if (mSaverConfirmation != null) { |
| 655 | mSaverConfirmation.dismiss(); |
| 656 | } |
| 657 | // Also close the notification shade, if it's open. |
| 658 | mContext.sendBroadcast( |
| 659 | new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) |
| 660 | .setFlags(Intent.FLAG_RECEIVER_FOREGROUND)); |
| 661 | |
| 662 | final Uri uri = Uri.parse(getURL()); |
| 663 | Context context = widget.getContext(); |
| 664 | Intent intent = new Intent(Intent.ACTION_VIEW, uri) |
| 665 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 666 | try { |
| 667 | context.startActivity(intent); |
| 668 | } catch (ActivityNotFoundException e) { |
| 669 | Log.w(TAG, "Activity was not found for intent, " + intent.toString()); |
| 670 | } |
| 671 | } |
| 672 | }; |
| 673 | builder.setSpan(urlSpan, start, end, message.getSpanFlags(urlSpan)); |
| 674 | } |
| 675 | return builder; |
| 676 | } |
| 677 | |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 678 | private void setSaverMode(boolean mode, boolean needFirstTimeWarning) { |
| 679 | BatterySaverUtils.setPowerSaveMode(mContext, mode, needFirstTimeWarning); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 682 | private void startBatterySaverSchedulePage() { |
| 683 | Intent intent = new Intent(BATTERY_SAVER_SCHEDULE_SCREEN_INTENT_ACTION); |
| 684 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 685 | mActivityStarter.startActivity(intent, true /* dismissShade */); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 686 | } |
| 687 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 688 | private final class Receiver extends BroadcastReceiver { |
| 689 | |
| 690 | public void init() { |
| 691 | IntentFilter filter = new IntentFilter(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 692 | filter.addAction(ACTION_SHOW_BATTERY_SETTINGS); |
| 693 | filter.addAction(ACTION_START_SAVER); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 694 | filter.addAction(ACTION_DISMISSED_WARNING); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 695 | filter.addAction(ACTION_CLICKED_TEMP_WARNING); |
| 696 | filter.addAction(ACTION_DISMISSED_TEMP_WARNING); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 697 | filter.addAction(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING); |
| 698 | filter.addAction(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING); |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 699 | filter.addAction(ACTION_SHOW_START_SAVER_CONFIRMATION); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 700 | filter.addAction(ACTION_SHOW_AUTO_SAVER_SUGGESTION); |
| 701 | filter.addAction(ACTION_ENABLE_AUTO_SAVER); |
| 702 | filter.addAction(ACTION_AUTO_SAVER_NO_THANKS); |
Makoto Onuki | 5ac8d5e | 2018-04-13 15:53:57 -0700 | [diff] [blame] | 703 | filter.addAction(ACTION_DISMISS_AUTO_SAVER_SUGGESTION); |
John Spurlock | 05e0705 | 2015-06-01 10:56:42 -0400 | [diff] [blame] | 704 | mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 705 | android.Manifest.permission.DEVICE_POWER, mHandler); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public void onReceive(Context context, Intent intent) { |
| 710 | final String action = intent.getAction(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 711 | Slog.i(TAG, "Received " + action); |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 712 | if (action.equals(ACTION_SHOW_BATTERY_SETTINGS)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 713 | dismissLowBatteryNotification(); |
| 714 | mContext.startActivityAsUser(mOpenBatterySettings, UserHandle.CURRENT); |
| 715 | } else if (action.equals(ACTION_START_SAVER)) { |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 716 | setSaverMode(true, true); |
| 717 | dismissLowBatteryNotification(); |
| 718 | } else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 719 | dismissLowBatteryNotification(); |
Salvador Martinez | df3a553 | 2019-04-05 13:09:18 -0700 | [diff] [blame] | 720 | showStartSaverConfirmation(intent.getExtras()); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 721 | } else if (action.equals(ACTION_DISMISSED_WARNING)) { |
| 722 | dismissLowBatteryWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 723 | } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 724 | dismissHighTemperatureWarningInternal(); |
| 725 | showHighTemperatureDialog(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 726 | } else if (ACTION_DISMISSED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 727 | dismissHighTemperatureWarningInternal(); |
| 728 | } else if (ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 729 | dismissThermalShutdownWarning(); |
| 730 | showThermalShutdownDialog(); |
| 731 | } else if (ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 732 | dismissThermalShutdownWarning(); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 733 | } else if (ACTION_SHOW_AUTO_SAVER_SUGGESTION.equals(action)) { |
| 734 | showAutoSaverSuggestion(); |
| 735 | } else if (ACTION_DISMISS_AUTO_SAVER_SUGGESTION.equals(action)) { |
| 736 | dismissAutoSaverSuggestion(); |
| 737 | } else if (ACTION_ENABLE_AUTO_SAVER.equals(action)) { |
| 738 | dismissAutoSaverSuggestion(); |
Salvador Martinez | 84b5bff | 2019-05-16 13:03:55 -0700 | [diff] [blame] | 739 | startBatterySaverSchedulePage(); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 740 | } else if (ACTION_AUTO_SAVER_NO_THANKS.equals(action)) { |
| 741 | dismissAutoSaverSuggestion(); |
| 742 | BatterySaverUtils.suppressAutoBatterySaver(context); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 746 | } |