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; |
| 22 | import android.content.BroadcastReceiver; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.DialogInterface.OnClickListener; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 26 | import android.content.DialogInterface.OnDismissListener; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 29 | import android.icu.text.MeasureFormat; |
| 30 | import android.icu.text.MeasureFormat.FormatWidth; |
| 31 | import android.icu.util.Measure; |
| 32 | import android.icu.util.MeasureUnit; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 33 | import android.media.AudioAttributes; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 34 | import android.os.AsyncTask; |
| 35 | import android.os.Handler; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 36 | import android.os.Looper; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 37 | import android.os.PowerManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 38 | import android.os.UserHandle; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 39 | import android.support.annotation.VisibleForTesting; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 40 | import android.text.format.DateUtils; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 41 | import android.util.Slog; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 42 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 43 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 44 | import com.android.settingslib.Utils; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 45 | import com.android.systemui.R; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 46 | import com.android.systemui.SystemUI; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 47 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 48 | import com.android.systemui.util.NotificationChannels; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 49 | |
| 50 | import java.io.PrintWriter; |
Elliott Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 51 | import java.text.NumberFormat; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 52 | import java.util.Locale; |
| 53 | import java.util.concurrent.TimeUnit; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 54 | |
| 55 | public class PowerNotificationWarnings implements PowerUI.WarningsUI { |
| 56 | private static final String TAG = PowerUI.TAG + ".Notification"; |
| 57 | private static final boolean DEBUG = PowerUI.DEBUG; |
| 58 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 59 | private static final String TAG_BATTERY = "low_battery"; |
| 60 | private static final String TAG_TEMPERATURE = "high_temp"; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 61 | |
| 62 | private static final int SHOWING_NOTHING = 0; |
| 63 | private static final int SHOWING_WARNING = 1; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 64 | private static final int SHOWING_INVALID_CHARGER = 3; |
| 65 | private static final String[] SHOWING_STRINGS = { |
| 66 | "SHOWING_NOTHING", |
| 67 | "SHOWING_WARNING", |
| 68 | "SHOWING_SAVER", |
| 69 | "SHOWING_INVALID_CHARGER", |
| 70 | }; |
| 71 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 72 | private static final String ACTION_SHOW_BATTERY_SETTINGS = "PNW.batterySettings"; |
| 73 | private static final String ACTION_START_SAVER = "PNW.startSaver"; |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 74 | private static final String ACTION_DISMISSED_WARNING = "PNW.dismissedWarning"; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 75 | private static final String ACTION_CLICKED_TEMP_WARNING = "PNW.clickedTempWarning"; |
| 76 | private static final String ACTION_DISMISSED_TEMP_WARNING = "PNW.dismissedTempWarning"; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 77 | private static final String ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING = |
| 78 | "PNW.clickedThermalShutdownWarning"; |
| 79 | private static final String ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING = |
| 80 | "PNW.dismissedThermalShutdownWarning"; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 81 | |
| 82 | private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder() |
| 83 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 84 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 85 | .build(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 86 | |
| 87 | private final Context mContext; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 88 | private final NotificationManager mNoMan; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 89 | private final PowerManager mPowerMan; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 90 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 91 | private final Receiver mReceiver = new Receiver(); |
| 92 | private final Intent mOpenBatterySettings = settings(Intent.ACTION_POWER_USAGE_SUMMARY); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 93 | |
| 94 | private int mBatteryLevel; |
| 95 | private int mBucket; |
| 96 | private long mScreenOffTime; |
| 97 | private int mShowing; |
| 98 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 99 | private long mWarningTriggerTimeMs; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 100 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 101 | private Estimate mEstimate; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 102 | private boolean mWarning; |
| 103 | private boolean mPlaySound; |
| 104 | private boolean mInvalidCharger; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 105 | private SystemUIDialog mSaverConfirmation; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 106 | private boolean mHighTempWarning; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 107 | private SystemUIDialog mHighTempDialog; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 108 | private SystemUIDialog mThermalShutdownDialog; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 109 | |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 110 | public PowerNotificationWarnings(Context context) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 111 | mContext = context; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 112 | mNoMan = mContext.getSystemService(NotificationManager.class); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 113 | mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 114 | mReceiver.init(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void dump(PrintWriter pw) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 119 | pw.print("mWarning="); pw.println(mWarning); |
| 120 | pw.print("mPlaySound="); pw.println(mPlaySound); |
| 121 | pw.print("mInvalidCharger="); pw.println(mInvalidCharger); |
| 122 | pw.print("mShowing="); pw.println(SHOWING_STRINGS[mShowing]); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 123 | pw.print("mSaverConfirmation="); pw.println(mSaverConfirmation != null ? "not null" : null); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 124 | pw.print("mHighTempWarning="); pw.println(mHighTempWarning); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 125 | pw.print("mHighTempDialog="); pw.println(mHighTempDialog != null ? "not null" : null); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 126 | pw.print("mThermalShutdownDialog="); |
| 127 | pw.println(mThermalShutdownDialog != null ? "not null" : null); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void update(int batteryLevel, int bucket, long screenOffTime) { |
| 132 | mBatteryLevel = batteryLevel; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 133 | if (bucket >= 0) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 134 | mWarningTriggerTimeMs = 0; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 135 | } else if (bucket < mBucket) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 136 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 137 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 138 | mBucket = bucket; |
| 139 | mScreenOffTime = screenOffTime; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public void updateEstimate(Estimate estimate) { |
| 144 | mEstimate = estimate; |
| 145 | if (estimate.estimateMillis <= PowerUI.THREE_HOURS_IN_MILLIS) { |
| 146 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
| 147 | } |
| 148 | } |
| 149 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 150 | private void updateNotification() { |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 151 | if (DEBUG) Slog.d(TAG, "updateNotification mWarning=" + mWarning + " mPlaySound=" |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 152 | + mPlaySound + " mInvalidCharger=" + mInvalidCharger); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 153 | if (mInvalidCharger) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 154 | showInvalidChargerNotification(); |
| 155 | mShowing = SHOWING_INVALID_CHARGER; |
| 156 | } else if (mWarning) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 157 | showWarningNotification(); |
| 158 | mShowing = SHOWING_WARNING; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 159 | } else { |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 160 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 161 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 162 | mShowing = SHOWING_NOTHING; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private void showInvalidChargerNotification() { |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 167 | final Notification.Builder nb = |
| 168 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 169 | .setSmallIcon(R.drawable.ic_power_low) |
| 170 | .setWhen(0) |
| 171 | .setShowWhen(false) |
| 172 | .setOngoing(true) |
| 173 | .setContentTitle(mContext.getString(R.string.invalid_charger_title)) |
| 174 | .setContentText(mContext.getString(R.string.invalid_charger_text)) |
| 175 | .setColor(mContext.getColor( |
| 176 | com.android.internal.R.color.system_notification_accent_color)); |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 177 | SystemUI.overrideNotificationAppName(mContext, nb); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 178 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 179 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
| 180 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 183 | protected void showWarningNotification() { |
Elliott Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 184 | final String percentage = NumberFormat.getPercentInstance().format((double) mBatteryLevel / 100.0); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 185 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 186 | // get standard notification copy |
| 187 | String title = mContext.getString(R.string.battery_low_title); |
| 188 | String contentText = mContext.getString(R.string.battery_low_percent_format, percentage); |
| 189 | |
| 190 | // override notification copy if hybrid notification enabled |
| 191 | if (mEstimate != null) { |
| 192 | title = mContext.getString(R.string.battery_low_title_hybrid); |
| 193 | contentText = mContext.getString( |
| 194 | mEstimate.isBasedOnUsage |
| 195 | ? R.string.battery_low_percent_format_hybrid |
| 196 | : R.string.battery_low_percent_format_hybrid_short, |
| 197 | percentage, |
| 198 | getTimeRemainingFormatted()); |
| 199 | } |
| 200 | |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 201 | final Notification.Builder nb = |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 202 | new Notification.Builder(mContext, NotificationChannels.BATTERY) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 203 | .setSmallIcon(R.drawable.ic_power_low) |
| 204 | // Bump the notification when the bucket dropped. |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 205 | .setWhen(mWarningTriggerTimeMs) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 206 | .setShowWhen(false) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 207 | .setContentTitle(title) |
| 208 | .setContentText(contentText) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 209 | .setOnlyAlertOnce(true) |
| 210 | .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_WARNING)) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 211 | .setVisibility(Notification.VISIBILITY_PUBLIC); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 212 | if (hasBatterySettings()) { |
| 213 | nb.setContentIntent(pendingBroadcast(ACTION_SHOW_BATTERY_SETTINGS)); |
| 214 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 215 | // Make the notification red if the percentage goes below a certain amount or the time |
| 216 | // remaining estimate is disabled |
| 217 | if (mEstimate == null || mBucket < 0) { |
| 218 | nb.setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
| 219 | } |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 220 | nb.addAction(0, |
| 221 | mContext.getString(R.string.battery_saver_start_action), |
| 222 | pendingBroadcast(ACTION_START_SAVER)); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 223 | nb.setOnlyAlertOnce(!mPlaySound); |
| 224 | mPlaySound = false; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 225 | SystemUI.overrideNotificationAppName(mContext, nb); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 226 | final Notification n = nb.build(); |
| 227 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 228 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 231 | @VisibleForTesting |
| 232 | String getTimeRemainingFormatted() { |
| 233 | final Locale currentLocale = mContext.getResources().getConfiguration().getLocales().get(0); |
| 234 | MeasureFormat frmt = MeasureFormat.getInstance(currentLocale, FormatWidth.NARROW); |
| 235 | |
| 236 | final long remainder = mEstimate.estimateMillis % DateUtils.HOUR_IN_MILLIS; |
| 237 | final long hours = TimeUnit.MILLISECONDS.toHours( |
| 238 | mEstimate.estimateMillis - remainder); |
| 239 | // round down to the nearest 15 min for now to not appear overly precise |
| 240 | final long minutes = TimeUnit.MILLISECONDS.toMinutes( |
| 241 | remainder - (remainder % TimeUnit.MINUTES.toMillis(15))); |
| 242 | final Measure hoursMeasure = new Measure(hours, MeasureUnit.HOUR); |
| 243 | final Measure minutesMeasure = new Measure(minutes, MeasureUnit.MINUTE); |
| 244 | |
| 245 | return frmt.formatMeasures(hoursMeasure, minutesMeasure); |
| 246 | } |
| 247 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 248 | private PendingIntent pendingBroadcast(String action) { |
| 249 | return PendingIntent.getBroadcastAsUser(mContext, |
| 250 | 0, new Intent(action), 0, UserHandle.CURRENT); |
| 251 | } |
| 252 | |
| 253 | private static Intent settings(String action) { |
| 254 | return new Intent(action).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 255 | | Intent.FLAG_ACTIVITY_MULTIPLE_TASK |
| 256 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
| 257 | | Intent.FLAG_ACTIVITY_NO_HISTORY |
| 258 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public boolean isInvalidChargerWarningShowing() { |
| 263 | return mInvalidCharger; |
| 264 | } |
| 265 | |
| 266 | @Override |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 267 | public void dismissHighTemperatureWarning() { |
| 268 | if (!mHighTempWarning) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 269 | return; |
| 270 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 271 | mHighTempWarning = false; |
| 272 | dismissHighTemperatureWarningInternal(); |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 276 | * Internal only version of {@link #dismissHighTemperatureWarning()} that simply dismisses |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 277 | * the notification. As such, the notification will not show again until |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 278 | * {@link #dismissHighTemperatureWarning()} is called. |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 279 | */ |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 280 | private void dismissHighTemperatureWarningInternal() { |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 281 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, UserHandle.ALL); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 285 | public void showHighTemperatureWarning() { |
| 286 | if (mHighTempWarning) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 287 | return; |
| 288 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 289 | mHighTempWarning = true; |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 290 | final Notification.Builder nb = |
| 291 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 292 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 293 | .setWhen(0) |
| 294 | .setShowWhen(false) |
| 295 | .setContentTitle(mContext.getString(R.string.high_temp_title)) |
| 296 | .setContentText(mContext.getString(R.string.high_temp_notif_message)) |
| 297 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 298 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_TEMP_WARNING)) |
| 299 | .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_TEMP_WARNING)) |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 300 | .setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 301 | SystemUI.overrideNotificationAppName(mContext, nb); |
| 302 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 303 | mNoMan.notifyAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, n, UserHandle.ALL); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 306 | private void showHighTemperatureDialog() { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 307 | if (mHighTempDialog != null) return; |
| 308 | final SystemUIDialog d = new SystemUIDialog(mContext); |
Andrew Sapperstein | e26dc3d | 2017-01-04 11:25:20 -0800 | [diff] [blame] | 309 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 310 | d.setTitle(R.string.high_temp_title); |
| 311 | d.setMessage(R.string.high_temp_dialog_message); |
| 312 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 313 | d.setShowForAllUsers(true); |
| 314 | d.setOnDismissListener(dialog -> mHighTempDialog = null); |
| 315 | d.show(); |
| 316 | mHighTempDialog = d; |
| 317 | } |
| 318 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 319 | @VisibleForTesting |
| 320 | void dismissThermalShutdownWarning() { |
| 321 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, UserHandle.ALL); |
| 322 | } |
| 323 | |
| 324 | private void showThermalShutdownDialog() { |
| 325 | if (mThermalShutdownDialog != null) return; |
| 326 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 327 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
| 328 | d.setTitle(R.string.thermal_shutdown_title); |
| 329 | d.setMessage(R.string.thermal_shutdown_dialog_message); |
| 330 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 331 | d.setShowForAllUsers(true); |
| 332 | d.setOnDismissListener(dialog -> mThermalShutdownDialog = null); |
| 333 | d.show(); |
| 334 | mThermalShutdownDialog = d; |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void showThermalShutdownWarning() { |
| 339 | final Notification.Builder nb = |
| 340 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 341 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 342 | .setWhen(0) |
| 343 | .setShowWhen(false) |
| 344 | .setContentTitle(mContext.getString(R.string.thermal_shutdown_title)) |
| 345 | .setContentText(mContext.getString(R.string.thermal_shutdown_message)) |
| 346 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 347 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING)) |
| 348 | .setDeleteIntent( |
| 349 | pendingBroadcast(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING)) |
| 350 | .setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
| 351 | SystemUI.overrideNotificationAppName(mContext, nb); |
| 352 | final Notification n = nb.build(); |
| 353 | mNoMan.notifyAsUser( |
| 354 | TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, n, UserHandle.ALL); |
| 355 | } |
| 356 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 357 | @Override |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 358 | public void updateLowBatteryWarning() { |
| 359 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public void dismissLowBatteryWarning() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 364 | if (DEBUG) Slog.d(TAG, "dismissing low battery warning: level=" + mBatteryLevel); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 365 | dismissLowBatteryNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private void dismissLowBatteryNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 369 | if (mWarning) Slog.i(TAG, "dismissing low battery notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 370 | mWarning = false; |
| 371 | updateNotification(); |
| 372 | } |
| 373 | |
| 374 | private boolean hasBatterySettings() { |
| 375 | return mOpenBatterySettings.resolveActivity(mContext.getPackageManager()) != null; |
| 376 | } |
| 377 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 378 | @Override |
| 379 | public void showLowBatteryWarning(boolean playSound) { |
| 380 | Slog.i(TAG, |
| 381 | "show low battery warning: level=" + mBatteryLevel |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 382 | + " [" + mBucket + "] playSound=" + playSound); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 383 | mPlaySound = playSound; |
| 384 | mWarning = true; |
| 385 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 386 | } |
| 387 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 388 | @Override |
| 389 | public void dismissInvalidChargerWarning() { |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 390 | dismissInvalidChargerNotification(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | private void dismissInvalidChargerNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 394 | if (mInvalidCharger) Slog.i(TAG, "dismissing invalid charger notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 395 | mInvalidCharger = false; |
| 396 | updateNotification(); |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public void showInvalidChargerWarning() { |
| 401 | mInvalidCharger = true; |
| 402 | updateNotification(); |
| 403 | } |
| 404 | |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 405 | @Override |
| 406 | public void userSwitched() { |
| 407 | updateNotification(); |
| 408 | } |
| 409 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 410 | private void showStartSaverConfirmation() { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 411 | if (mSaverConfirmation != null) return; |
| 412 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 413 | d.setTitle(R.string.battery_saver_confirmation_title); |
| 414 | d.setMessage(com.android.internal.R.string.battery_saver_description); |
| 415 | d.setNegativeButton(android.R.string.cancel, null); |
| 416 | d.setPositiveButton(R.string.battery_saver_confirmation_ok, mStartSaverMode); |
| 417 | d.setShowForAllUsers(true); |
| 418 | d.setOnDismissListener(new OnDismissListener() { |
| 419 | @Override |
| 420 | public void onDismiss(DialogInterface dialog) { |
| 421 | mSaverConfirmation = null; |
| 422 | } |
| 423 | }); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 424 | d.show(); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 425 | mSaverConfirmation = d; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 426 | } |
| 427 | |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 428 | private void setSaverMode(boolean mode) { |
| 429 | mPowerMan.setPowerSaveMode(mode); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | private final class Receiver extends BroadcastReceiver { |
| 433 | |
| 434 | public void init() { |
| 435 | IntentFilter filter = new IntentFilter(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 436 | filter.addAction(ACTION_SHOW_BATTERY_SETTINGS); |
| 437 | filter.addAction(ACTION_START_SAVER); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 438 | filter.addAction(ACTION_DISMISSED_WARNING); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 439 | filter.addAction(ACTION_CLICKED_TEMP_WARNING); |
| 440 | filter.addAction(ACTION_DISMISSED_TEMP_WARNING); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 441 | filter.addAction(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING); |
| 442 | filter.addAction(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING); |
John Spurlock | 05e0705 | 2015-06-01 10:56:42 -0400 | [diff] [blame] | 443 | mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, |
| 444 | android.Manifest.permission.STATUS_BAR_SERVICE, mHandler); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public void onReceive(Context context, Intent intent) { |
| 449 | final String action = intent.getAction(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 450 | Slog.i(TAG, "Received " + action); |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 451 | if (action.equals(ACTION_SHOW_BATTERY_SETTINGS)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 452 | dismissLowBatteryNotification(); |
| 453 | mContext.startActivityAsUser(mOpenBatterySettings, UserHandle.CURRENT); |
| 454 | } else if (action.equals(ACTION_START_SAVER)) { |
| 455 | dismissLowBatteryNotification(); |
| 456 | showStartSaverConfirmation(); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 457 | } else if (action.equals(ACTION_DISMISSED_WARNING)) { |
| 458 | dismissLowBatteryWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 459 | } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 460 | dismissHighTemperatureWarningInternal(); |
| 461 | showHighTemperatureDialog(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 462 | } else if (ACTION_DISMISSED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 463 | dismissHighTemperatureWarningInternal(); |
| 464 | } else if (ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 465 | dismissThermalShutdownWarning(); |
| 466 | showThermalShutdownDialog(); |
| 467 | } else if (ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 468 | dismissThermalShutdownWarning(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | private final OnClickListener mStartSaverMode = new OnClickListener() { |
| 474 | @Override |
| 475 | public void onClick(DialogInterface dialog, int which) { |
| 476 | AsyncTask.execute(new Runnable() { |
| 477 | @Override |
| 478 | public void run() { |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 479 | setSaverMode(true); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 480 | } |
| 481 | }); |
| 482 | } |
| 483 | }; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 484 | } |