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