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