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 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 19 | import static android.content.DialogInterface.BUTTON_NEGATIVE; |
| 20 | import static android.content.DialogInterface.BUTTON_POSITIVE; |
| 21 | |
| 22 | import android.app.KeyguardManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 23 | import android.app.Notification; |
| 24 | import android.app.NotificationManager; |
| 25 | import android.app.PendingIntent; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 26 | import android.content.ActivityNotFoundException; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 28 | import android.content.Context; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 31 | import android.media.AudioAttributes; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 33 | import android.os.Handler; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 34 | import android.os.Looper; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 35 | import android.os.PowerManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 36 | import android.os.UserHandle; |
Aurimas Liutikas | d366771 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 37 | import androidx.annotation.VisibleForTesting; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 38 | import android.text.Annotation; |
| 39 | import android.text.Layout; |
| 40 | import android.text.SpannableString; |
| 41 | import android.text.SpannableStringBuilder; |
| 42 | import android.text.TextPaint; |
| 43 | import android.text.TextUtils; |
| 44 | import android.text.method.LinkMovementMethod; |
| 45 | import android.text.style.URLSpan; |
| 46 | import android.util.Log; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 47 | import android.util.Slog; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 48 | import android.view.View; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 49 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 50 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 51 | import com.android.settingslib.Utils; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 52 | import com.android.settingslib.fuelgauge.BatterySaverUtils; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 53 | import com.android.settingslib.utils.PowerUtil; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 54 | import com.android.systemui.Dependency; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 55 | import com.android.systemui.R; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 56 | import com.android.systemui.SystemUI; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 57 | import com.android.systemui.plugins.ActivityStarter; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 58 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 59 | import com.android.systemui.util.NotificationChannels; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 60 | import com.android.systemui.volume.Events; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 61 | |
| 62 | import java.io.PrintWriter; |
Elliott Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 63 | import java.text.NumberFormat; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 64 | import java.util.Locale; |
| 65 | import java.util.Objects; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 66 | |
| 67 | public class PowerNotificationWarnings implements PowerUI.WarningsUI { |
| 68 | private static final String TAG = PowerUI.TAG + ".Notification"; |
| 69 | private static final boolean DEBUG = PowerUI.DEBUG; |
| 70 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 71 | private static final String TAG_BATTERY = "low_battery"; |
| 72 | private static final String TAG_TEMPERATURE = "high_temp"; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 73 | private static final String TAG_AUTO_SAVER = "auto_saver"; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 74 | |
| 75 | private static final int SHOWING_NOTHING = 0; |
| 76 | private static final int SHOWING_WARNING = 1; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 77 | private static final int SHOWING_INVALID_CHARGER = 3; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 78 | private static final int SHOWING_AUTO_SAVER_SUGGESTION = 4; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 79 | private static final String[] SHOWING_STRINGS = { |
| 80 | "SHOWING_NOTHING", |
| 81 | "SHOWING_WARNING", |
| 82 | "SHOWING_SAVER", |
| 83 | "SHOWING_INVALID_CHARGER", |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 84 | "SHOWING_AUTO_SAVER_SUGGESTION", |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 85 | }; |
| 86 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 87 | private static final String ACTION_SHOW_BATTERY_SETTINGS = "PNW.batterySettings"; |
| 88 | private static final String ACTION_START_SAVER = "PNW.startSaver"; |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 89 | private static final String ACTION_DISMISSED_WARNING = "PNW.dismissedWarning"; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 90 | private static final String ACTION_CLICKED_TEMP_WARNING = "PNW.clickedTempWarning"; |
| 91 | private static final String ACTION_DISMISSED_TEMP_WARNING = "PNW.dismissedTempWarning"; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 92 | private static final String ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING = |
| 93 | "PNW.clickedThermalShutdownWarning"; |
| 94 | private static final String ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING = |
| 95 | "PNW.dismissedThermalShutdownWarning"; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 96 | private static final String ACTION_SHOW_START_SAVER_CONFIRMATION = |
| 97 | BatterySaverUtils.ACTION_SHOW_START_SAVER_CONFIRMATION; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 98 | private static final String ACTION_SHOW_AUTO_SAVER_SUGGESTION = |
| 99 | BatterySaverUtils.ACTION_SHOW_AUTO_SAVER_SUGGESTION; |
| 100 | private static final String ACTION_DISMISS_AUTO_SAVER_SUGGESTION = |
| 101 | "PNW.dismissAutoSaverSuggestion"; |
| 102 | |
| 103 | private static final String ACTION_ENABLE_AUTO_SAVER = |
| 104 | "PNW.enableAutoSaver"; |
| 105 | private static final String ACTION_AUTO_SAVER_NO_THANKS = |
| 106 | "PNW.autoSaverNoThanks"; |
| 107 | |
| 108 | private static final String SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING = |
| 109 | "android.settings.BATTERY_SAVER_SETTINGS"; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 110 | |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 111 | private static final String BATTERY_SAVER_DESCRIPTION_URL_KEY = "url"; |
| 112 | |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 113 | private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder() |
| 114 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 115 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 116 | .build(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 117 | |
| 118 | private final Context mContext; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 119 | private final NotificationManager mNoMan; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 120 | private final PowerManager mPowerMan; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 121 | private final KeyguardManager mKeyguard; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 122 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 123 | private final Receiver mReceiver = new Receiver(); |
| 124 | private final Intent mOpenBatterySettings = settings(Intent.ACTION_POWER_USAGE_SUMMARY); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 125 | |
| 126 | private int mBatteryLevel; |
| 127 | private int mBucket; |
| 128 | private long mScreenOffTime; |
| 129 | private int mShowing; |
| 130 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 131 | private long mWarningTriggerTimeMs; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 132 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 133 | private Estimate mEstimate; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 134 | private long mLowWarningThreshold; |
| 135 | private long mSevereWarningThreshold; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 136 | private boolean mWarning; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 137 | private boolean mShowAutoSaverSuggestion; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 138 | private boolean mPlaySound; |
| 139 | private boolean mInvalidCharger; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 140 | private SystemUIDialog mSaverConfirmation; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 141 | private SystemUIDialog mSaverEnabledConfirmation; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 142 | private boolean mHighTempWarning; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 143 | private SystemUIDialog mHighTempDialog; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 144 | private SystemUIDialog mThermalShutdownDialog; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 145 | @VisibleForTesting |
| 146 | protected OverheatAlarmDialog mOverheatAlarmDialog; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 147 | |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 148 | public PowerNotificationWarnings(Context context) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 149 | mContext = context; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 150 | mNoMan = mContext.getSystemService(NotificationManager.class); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 151 | mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 152 | mKeyguard = mContext.getSystemService(KeyguardManager.class); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 153 | mReceiver.init(); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void dump(PrintWriter pw) { |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 158 | pw.print("mWarning="); |
| 159 | pw.println(mWarning); |
| 160 | pw.print("mPlaySound="); |
| 161 | pw.println(mPlaySound); |
| 162 | pw.print("mInvalidCharger="); |
| 163 | pw.println(mInvalidCharger); |
| 164 | pw.print("mShowing="); |
| 165 | pw.println(SHOWING_STRINGS[mShowing]); |
| 166 | pw.print("mSaverConfirmation="); |
| 167 | pw.println(mSaverConfirmation != null ? "not null" : null); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 168 | pw.print("mSaverEnabledConfirmation="); |
| 169 | pw.println(mSaverEnabledConfirmation != null ? "not null" : null); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 170 | pw.print("mHighTempWarning="); |
| 171 | pw.println(mHighTempWarning); |
| 172 | pw.print("mHighTempDialog="); |
| 173 | pw.println(mHighTempDialog != null ? "not null" : null); |
| 174 | pw.print("mOverheatAlarmDialog="); |
| 175 | pw.println(mOverheatAlarmDialog != null ? "not null" : null); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 176 | pw.print("mThermalShutdownDialog="); |
| 177 | pw.println(mThermalShutdownDialog != null ? "not null" : null); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 180 | private int getLowBatteryAutoTriggerDefaultLevel() { |
| 181 | return mContext.getResources().getInteger( |
| 182 | com.android.internal.R.integer.config_lowBatteryAutoTriggerDefaultLevel); |
| 183 | } |
| 184 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 185 | @Override |
| 186 | public void update(int batteryLevel, int bucket, long screenOffTime) { |
| 187 | mBatteryLevel = batteryLevel; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 188 | if (bucket >= 0) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 189 | mWarningTriggerTimeMs = 0; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 190 | } else if (bucket < mBucket) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 191 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 192 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 193 | mBucket = bucket; |
| 194 | mScreenOffTime = screenOffTime; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 197 | @Override |
| 198 | public void updateEstimate(Estimate estimate) { |
| 199 | mEstimate = estimate; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 200 | if (estimate.estimateMillis <= mLowWarningThreshold) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 201 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
| 202 | } |
| 203 | } |
| 204 | |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 205 | @Override |
| 206 | public void updateThresholds(long lowThreshold, long severeThreshold) { |
| 207 | mLowWarningThreshold = lowThreshold; |
| 208 | mSevereWarningThreshold = severeThreshold; |
| 209 | } |
| 210 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 211 | private void updateNotification() { |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 212 | if (DEBUG) Slog.d(TAG, "updateNotification mWarning=" + mWarning + " mPlaySound=" |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 213 | + mPlaySound + " mInvalidCharger=" + mInvalidCharger); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 214 | if (mInvalidCharger) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 215 | showInvalidChargerNotification(); |
| 216 | mShowing = SHOWING_INVALID_CHARGER; |
| 217 | } else if (mWarning) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 218 | showWarningNotification(); |
| 219 | mShowing = SHOWING_WARNING; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 220 | } else if (mShowAutoSaverSuggestion) { |
Makoto Onuki | 60b8f18 | 2018-05-07 13:56:40 -0700 | [diff] [blame] | 221 | // Once we showed the notification, don't show it again until it goes SHOWING_NOTHING. |
| 222 | // This shouldn't be needed, because we have a delete intent on this notification |
| 223 | // so when it's dismissed we should notice it and clear mShowAutoSaverSuggestion, |
| 224 | // However we double check here just in case the dismiss intent broadcast is delayed. |
| 225 | if (mShowing != SHOWING_AUTO_SAVER_SUGGESTION) { |
| 226 | showAutoSaverSuggestionNotification(); |
| 227 | } |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 228 | mShowing = SHOWING_AUTO_SAVER_SUGGESTION; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 229 | } else { |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 230 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 231 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 232 | mNoMan.cancelAsUser(TAG_AUTO_SAVER, |
| 233 | SystemMessage.NOTE_AUTO_SAVER_SUGGESTION, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 234 | mShowing = SHOWING_NOTHING; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private void showInvalidChargerNotification() { |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 239 | final Notification.Builder nb = |
| 240 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 241 | .setSmallIcon(R.drawable.ic_power_low) |
| 242 | .setWhen(0) |
| 243 | .setShowWhen(false) |
| 244 | .setOngoing(true) |
| 245 | .setContentTitle(mContext.getString(R.string.invalid_charger_title)) |
| 246 | .setContentText(mContext.getString(R.string.invalid_charger_text)) |
| 247 | .setColor(mContext.getColor( |
| 248 | com.android.internal.R.color.system_notification_accent_color)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 249 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 250 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 251 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
| 252 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 255 | protected void showWarningNotification() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 256 | final String percentage = NumberFormat.getPercentInstance() |
| 257 | .format((double) mBatteryLevel / 100.0); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 258 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 259 | // get standard notification copy |
| 260 | String title = mContext.getString(R.string.battery_low_title); |
| 261 | String contentText = mContext.getString(R.string.battery_low_percent_format, percentage); |
| 262 | |
| 263 | // override notification copy if hybrid notification enabled |
| 264 | if (mEstimate != null) { |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 265 | contentText = getHybridContentString(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 | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 285 | if (mEstimate == null || mBucket < 0 |
| 286 | || mEstimate.estimateMillis < mSevereWarningThreshold) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 287 | nb.setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
| 288 | } |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 289 | |
| 290 | if (!mPowerMan.isPowerSaveMode()) { |
| 291 | nb.addAction(0, |
| 292 | mContext.getString(R.string.battery_saver_start_action), |
| 293 | pendingBroadcast(ACTION_START_SAVER)); |
| 294 | } |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 295 | nb.setOnlyAlertOnce(!mPlaySound); |
| 296 | mPlaySound = false; |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 297 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 298 | final Notification n = nb.build(); |
| 299 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 300 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 303 | private void showAutoSaverSuggestionNotification() { |
| 304 | final Notification.Builder nb = |
| 305 | new Notification.Builder(mContext, NotificationChannels.HINTS) |
| 306 | .setSmallIcon(R.drawable.ic_power_saver) |
| 307 | .setWhen(0) |
| 308 | .setShowWhen(false) |
| 309 | .setContentTitle(mContext.getString(R.string.auto_saver_title)) |
| 310 | .setContentText(mContext.getString(R.string.auto_saver_text, |
| 311 | getLowBatteryAutoTriggerDefaultLevel())); |
| 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( |
| 327 | mContext, |
| 328 | mEstimate.estimateMillis, |
| 329 | percentage, |
| 330 | mEstimate.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 | mHighTempWarning = false; |
| 359 | dismissHighTemperatureWarningInternal(); |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 363 | * Internal only version of {@link #dismissHighTemperatureWarning()} that simply dismisses |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 364 | * the notification. As such, the notification will not show again until |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 365 | * {@link #dismissHighTemperatureWarning()} is called. |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 366 | */ |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 367 | private void dismissHighTemperatureWarningInternal() { |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 368 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, UserHandle.ALL); |
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 Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 387 | .setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 388 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 389 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 390 | mNoMan.notifyAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, n, UserHandle.ALL); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 393 | /** |
| 394 | * PowerUI detect thermal overheat, notify to popup alarm dialog. |
| 395 | * Alarm with beep sound, showing overheat alarm dialog until user click OK or link of help. |
| 396 | * Do not auto dismiss even temperature drop. |
| 397 | * |
| 398 | * @param overheat true if device overheat, temperature >= threshold. |
| 399 | * false if device temperature <= threshold tolerance after overheat |
| 400 | * alarmed. |
| 401 | * @param shouldBeepSound true alarm beep sound until user interactive with device |
| 402 | * false showing alarm dialog only |
| 403 | */ |
| 404 | @Override |
| 405 | public void notifyHighTemperatureAlarm(boolean overheat, boolean shouldBeepSound) { |
| 406 | // Overheat and non-null dialog are XOR(exclusive or) relationship |
| 407 | if (overheat ^ (mOverheatAlarmDialog != null)) { |
| 408 | // b/120188825 Since notifyHighTemperatureAlarm() could be triggered by |
| 409 | // non-ui thread such as ThermalEventListener.notifyThrottling() |
| 410 | mHandler.post(() -> setOverheatAlarmDialogShowing(overheat)); |
| 411 | setAlarmShouldSound(shouldBeepSound); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Showing overheat alarm dialog until user click OK button or link of help to dismiss |
| 417 | * |
| 418 | * @param shouldShow whether to show overheat alarm dialog. |
| 419 | */ |
| 420 | protected void setOverheatAlarmDialogShowing(boolean shouldShow) { |
| 421 | if (shouldShow && mOverheatAlarmDialog == null) { |
| 422 | OverheatAlarmDialog d = new OverheatAlarmDialog(mContext); |
| 423 | d.setCancelable(false); |
| 424 | d.setButton(BUTTON_NEGATIVE, |
| 425 | mContext.getString(R.string.high_temp_alarm_help_care_steps), |
| 426 | (dialogInterface, i) -> { |
| 427 | final String contextString = mContext.getString( |
| 428 | R.string.high_temp_alarm_help_url); |
| 429 | final Intent helpIntent = new Intent(); |
| 430 | helpIntent.setClassName("com.android.settings", |
| 431 | "com.android.settings.HelpTrampoline"); |
| 432 | helpIntent.putExtra(Intent.EXTRA_TEXT, contextString); |
| 433 | Dependency.get(ActivityStarter.class).startActivity(helpIntent, |
| 434 | true /* dismissShade */, resultCode -> { |
| 435 | mOverheatAlarmDialog = null; |
| 436 | }); |
| 437 | }); |
| 438 | d.setButton(BUTTON_POSITIVE, mContext.getString(com.android.internal.R.string.ok), |
| 439 | (dialogInterface, i) -> mOverheatAlarmDialog = null); |
| 440 | d.setOnDismissListener(dialogInterface -> { |
| 441 | mOverheatAlarmDialog = null; |
| 442 | Events.writeEvent(mContext, Events.EVENT_DISMISS_OVERHEAT_ALARM, |
| 443 | Events.DISMISS_REASON_DONE_CLICKED, |
| 444 | mKeyguard.isKeyguardLocked()); |
| 445 | }); |
| 446 | d.show(); |
| 447 | Events.writeEvent(mContext, Events.EVENT_SHOW_OVERHEAT_ALARM, |
| 448 | Events.SHOW_REASON_OVERHEAD_ALARM_CHANGED, |
| 449 | mKeyguard.isKeyguardLocked()); |
| 450 | mOverheatAlarmDialog = d; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Whether to alarm beep sound when overheat dialog showing. |
| 456 | * |
| 457 | * @param shouldSound whether to alarm beep sound. |
| 458 | */ |
| 459 | protected void setAlarmShouldSound(boolean shouldSound) { |
| 460 | Log.d(TAG, "setAlarmShouldSound, " + shouldSound); |
| 461 | if (shouldSound) { |
| 462 | OverheatAlarmController.getInstance(mContext).startAlarm(mContext); |
| 463 | } else { |
| 464 | OverheatAlarmController.getInstance(mContext).stopAlarm(); |
| 465 | } |
| 466 | } |
| 467 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 468 | private void showHighTemperatureDialog() { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 469 | if (mHighTempDialog != null) return; |
| 470 | final SystemUIDialog d = new SystemUIDialog(mContext); |
Andrew Sapperstein | e26dc3d | 2017-01-04 11:25:20 -0800 | [diff] [blame] | 471 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 472 | d.setTitle(R.string.high_temp_title); |
| 473 | d.setMessage(R.string.high_temp_dialog_message); |
| 474 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 475 | d.setShowForAllUsers(true); |
| 476 | d.setOnDismissListener(dialog -> mHighTempDialog = null); |
| 477 | d.show(); |
| 478 | mHighTempDialog = d; |
| 479 | } |
| 480 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 481 | @VisibleForTesting |
| 482 | void dismissThermalShutdownWarning() { |
| 483 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, UserHandle.ALL); |
| 484 | } |
| 485 | |
| 486 | private void showThermalShutdownDialog() { |
| 487 | if (mThermalShutdownDialog != null) return; |
| 488 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 489 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
| 490 | d.setTitle(R.string.thermal_shutdown_title); |
| 491 | d.setMessage(R.string.thermal_shutdown_dialog_message); |
| 492 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 493 | d.setShowForAllUsers(true); |
| 494 | d.setOnDismissListener(dialog -> mThermalShutdownDialog = null); |
| 495 | d.show(); |
| 496 | mThermalShutdownDialog = d; |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public void showThermalShutdownWarning() { |
| 501 | final Notification.Builder nb = |
| 502 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 503 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 504 | .setWhen(0) |
| 505 | .setShowWhen(false) |
| 506 | .setContentTitle(mContext.getString(R.string.thermal_shutdown_title)) |
| 507 | .setContentText(mContext.getString(R.string.thermal_shutdown_message)) |
| 508 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 509 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING)) |
| 510 | .setDeleteIntent( |
| 511 | pendingBroadcast(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING)) |
| 512 | .setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 513 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 514 | final Notification n = nb.build(); |
| 515 | mNoMan.notifyAsUser( |
| 516 | TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, n, UserHandle.ALL); |
| 517 | } |
| 518 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 519 | @Override |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 520 | public void updateLowBatteryWarning() { |
| 521 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public void dismissLowBatteryWarning() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 526 | if (DEBUG) Slog.d(TAG, "dismissing low battery warning: level=" + mBatteryLevel); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 527 | dismissLowBatteryNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | private void dismissLowBatteryNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 531 | if (mWarning) Slog.i(TAG, "dismissing low battery notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 532 | mWarning = false; |
| 533 | updateNotification(); |
| 534 | } |
| 535 | |
| 536 | private boolean hasBatterySettings() { |
| 537 | return mOpenBatterySettings.resolveActivity(mContext.getPackageManager()) != null; |
| 538 | } |
| 539 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 540 | @Override |
| 541 | public void showLowBatteryWarning(boolean playSound) { |
| 542 | Slog.i(TAG, |
| 543 | "show low battery warning: level=" + mBatteryLevel |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 544 | + " [" + mBucket + "] playSound=" + playSound); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 545 | mPlaySound = playSound; |
| 546 | mWarning = true; |
| 547 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 548 | } |
| 549 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 550 | @Override |
| 551 | public void dismissInvalidChargerWarning() { |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 552 | dismissInvalidChargerNotification(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | private void dismissInvalidChargerNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 556 | if (mInvalidCharger) Slog.i(TAG, "dismissing invalid charger notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 557 | mInvalidCharger = false; |
| 558 | updateNotification(); |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void showInvalidChargerWarning() { |
| 563 | mInvalidCharger = true; |
| 564 | updateNotification(); |
| 565 | } |
| 566 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 567 | private void showAutoSaverSuggestion() { |
| 568 | mShowAutoSaverSuggestion = true; |
| 569 | updateNotification(); |
| 570 | } |
| 571 | |
| 572 | private void dismissAutoSaverSuggestion() { |
| 573 | mShowAutoSaverSuggestion = false; |
| 574 | updateNotification(); |
| 575 | } |
| 576 | |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 577 | @Override |
| 578 | public void userSwitched() { |
| 579 | updateNotification(); |
| 580 | } |
| 581 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 582 | private void showStartSaverConfirmation() { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 583 | if (mSaverConfirmation != null) return; |
| 584 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 585 | d.setTitle(R.string.battery_saver_confirmation_title); |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 586 | d.setMessage(getBatterySaverDescription()); |
| 587 | |
| 588 | // Sad hack for http://b/78261259 and http://b/78298335. Otherwise "Battery" may be split |
| 589 | // into "Bat-tery". |
| 590 | if (isEnglishLocale()) { |
| 591 | d.setMessageHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE); |
| 592 | } |
| 593 | // We need to set LinkMovementMethod to make the link clickable. |
| 594 | d.setMessageMovementMethod(LinkMovementMethod.getInstance()); |
| 595 | |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 596 | d.setNegativeButton(android.R.string.cancel, null); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 597 | d.setPositiveButton(R.string.battery_saver_confirmation_ok, |
| 598 | (dialog, which) -> setSaverMode(true, false)); |
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 | 778ce66 | 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 | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 678 | private void showAutoSaverEnabledConfirmation() { |
| 679 | if (mSaverEnabledConfirmation != null) return; |
| 680 | |
| 681 | // Open the Battery Saver setting page. |
| 682 | final Intent actionBatterySaverSetting = |
| 683 | new Intent(SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING) |
| 684 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 685 | |
| 686 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 687 | d.setTitle(R.string.auto_saver_enabled_title); |
| 688 | d.setMessage(mContext.getString(R.string.auto_saver_enabled_text, |
| 689 | getLowBatteryAutoTriggerDefaultLevel())); |
| 690 | |
Makoto Onuki | 41d5ccf | 2018-04-03 11:46:02 -0700 | [diff] [blame] | 691 | // "Got it". Just close the dialog. Automatic battery has been enabled already. |
| 692 | d.setPositiveButton(R.string.auto_saver_okay_action, |
| 693 | (dialog, which) -> onAutoSaverEnabledConfirmationClosed()); |
| 694 | |
| 695 | // "Settings" -> Opens the battery saver settings activity. |
| 696 | d.setNeutralButton(R.string.open_saver_setting_action, (dialog, which) -> { |
| 697 | mContext.startActivity(actionBatterySaverSetting); |
| 698 | onAutoSaverEnabledConfirmationClosed(); |
| 699 | }); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 700 | d.setShowForAllUsers(true); |
Makoto Onuki | 41d5ccf | 2018-04-03 11:46:02 -0700 | [diff] [blame] | 701 | d.setOnDismissListener((dialog) -> onAutoSaverEnabledConfirmationClosed()); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 702 | d.show(); |
| 703 | mSaverEnabledConfirmation = d; |
| 704 | } |
| 705 | |
Makoto Onuki | 41d5ccf | 2018-04-03 11:46:02 -0700 | [diff] [blame] | 706 | private void onAutoSaverEnabledConfirmationClosed() { |
| 707 | mSaverEnabledConfirmation = null; |
| 708 | } |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 709 | |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 710 | private void setSaverMode(boolean mode, boolean needFirstTimeWarning) { |
| 711 | BatterySaverUtils.setPowerSaveMode(mContext, mode, needFirstTimeWarning); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 712 | } |
| 713 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 714 | private void scheduleAutoBatterySaver() { |
| 715 | int autoTriggerThreshold = mContext.getResources().getInteger( |
| 716 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
| 717 | if (autoTriggerThreshold == 0) { |
| 718 | autoTriggerThreshold = 15; |
| 719 | } |
| 720 | |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 721 | BatterySaverUtils.ensureAutoBatterySaver(mContext, autoTriggerThreshold); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 722 | showAutoSaverEnabledConfirmation(); |
| 723 | } |
| 724 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 725 | private final class Receiver extends BroadcastReceiver { |
| 726 | |
| 727 | public void init() { |
| 728 | IntentFilter filter = new IntentFilter(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 729 | filter.addAction(ACTION_SHOW_BATTERY_SETTINGS); |
| 730 | filter.addAction(ACTION_START_SAVER); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 731 | filter.addAction(ACTION_DISMISSED_WARNING); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 732 | filter.addAction(ACTION_CLICKED_TEMP_WARNING); |
| 733 | filter.addAction(ACTION_DISMISSED_TEMP_WARNING); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 734 | filter.addAction(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING); |
| 735 | filter.addAction(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING); |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 736 | filter.addAction(ACTION_SHOW_START_SAVER_CONFIRMATION); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 737 | filter.addAction(ACTION_SHOW_AUTO_SAVER_SUGGESTION); |
| 738 | filter.addAction(ACTION_ENABLE_AUTO_SAVER); |
| 739 | filter.addAction(ACTION_AUTO_SAVER_NO_THANKS); |
Makoto Onuki | 5ac8d5e | 2018-04-13 15:53:57 -0700 | [diff] [blame] | 740 | filter.addAction(ACTION_DISMISS_AUTO_SAVER_SUGGESTION); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 741 | filter.addAction(Intent.ACTION_ALARM_CHANGED); |
John Spurlock | 05e0705 | 2015-06-01 10:56:42 -0400 | [diff] [blame] | 742 | mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 743 | android.Manifest.permission.DEVICE_POWER, mHandler); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | @Override |
| 747 | public void onReceive(Context context, Intent intent) { |
| 748 | final String action = intent.getAction(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 749 | Slog.i(TAG, "Received " + action); |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 750 | if (action.equals(ACTION_SHOW_BATTERY_SETTINGS)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 751 | dismissLowBatteryNotification(); |
| 752 | mContext.startActivityAsUser(mOpenBatterySettings, UserHandle.CURRENT); |
| 753 | } else if (action.equals(ACTION_START_SAVER)) { |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 754 | setSaverMode(true, true); |
| 755 | dismissLowBatteryNotification(); |
| 756 | } else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 757 | dismissLowBatteryNotification(); |
| 758 | showStartSaverConfirmation(); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 759 | } else if (action.equals(ACTION_DISMISSED_WARNING)) { |
| 760 | dismissLowBatteryWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 761 | } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 762 | dismissHighTemperatureWarningInternal(); |
| 763 | showHighTemperatureDialog(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 764 | } else if (ACTION_DISMISSED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 765 | dismissHighTemperatureWarningInternal(); |
| 766 | } else if (ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 767 | dismissThermalShutdownWarning(); |
| 768 | showThermalShutdownDialog(); |
| 769 | } else if (ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 770 | dismissThermalShutdownWarning(); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 771 | } else if (ACTION_SHOW_AUTO_SAVER_SUGGESTION.equals(action)) { |
| 772 | showAutoSaverSuggestion(); |
| 773 | } else if (ACTION_DISMISS_AUTO_SAVER_SUGGESTION.equals(action)) { |
| 774 | dismissAutoSaverSuggestion(); |
| 775 | } else if (ACTION_ENABLE_AUTO_SAVER.equals(action)) { |
| 776 | dismissAutoSaverSuggestion(); |
| 777 | scheduleAutoBatterySaver(); |
| 778 | } else if (ACTION_AUTO_SAVER_NO_THANKS.equals(action)) { |
| 779 | dismissAutoSaverSuggestion(); |
| 780 | BatterySaverUtils.suppressAutoBatterySaver(context); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 781 | } else if (Intent.ACTION_ALARM_CHANGED.equals(action)) { |
| 782 | setAlarmShouldSound(false /* mHasUserInteracted */); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 786 | } |