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; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 26 | import android.media.AudioAttributes; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 28 | import android.os.Looper; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 29 | import android.os.PowerManager; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 30 | import android.os.UserHandle; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 31 | import android.support.annotation.VisibleForTesting; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 32 | import android.util.Slog; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 33 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 34 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 35 | import com.android.settingslib.Utils; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 36 | import com.android.settingslib.fuelgauge.BatterySaverUtils; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 37 | import com.android.settingslib.utils.PowerUtil; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 38 | import com.android.systemui.R; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 39 | import com.android.systemui.SystemUI; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 40 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 41 | import com.android.systemui.util.NotificationChannels; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 42 | |
| 43 | import java.io.PrintWriter; |
Elliott Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 44 | import java.text.NumberFormat; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 45 | |
| 46 | public class PowerNotificationWarnings implements PowerUI.WarningsUI { |
| 47 | private static final String TAG = PowerUI.TAG + ".Notification"; |
| 48 | private static final boolean DEBUG = PowerUI.DEBUG; |
| 49 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 50 | private static final String TAG_BATTERY = "low_battery"; |
| 51 | private static final String TAG_TEMPERATURE = "high_temp"; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 52 | private static final String TAG_AUTO_SAVER = "auto_saver"; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 53 | |
| 54 | private static final int SHOWING_NOTHING = 0; |
| 55 | private static final int SHOWING_WARNING = 1; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 56 | private static final int SHOWING_INVALID_CHARGER = 3; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 57 | private static final int SHOWING_AUTO_SAVER_SUGGESTION = 4; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 58 | private static final String[] SHOWING_STRINGS = { |
| 59 | "SHOWING_NOTHING", |
| 60 | "SHOWING_WARNING", |
| 61 | "SHOWING_SAVER", |
| 62 | "SHOWING_INVALID_CHARGER", |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 63 | "SHOWING_AUTO_SAVER_SUGGESTION", |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 64 | }; |
| 65 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 66 | private static final String ACTION_SHOW_BATTERY_SETTINGS = "PNW.batterySettings"; |
| 67 | private static final String ACTION_START_SAVER = "PNW.startSaver"; |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 68 | private static final String ACTION_DISMISSED_WARNING = "PNW.dismissedWarning"; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 69 | private static final String ACTION_CLICKED_TEMP_WARNING = "PNW.clickedTempWarning"; |
| 70 | private static final String ACTION_DISMISSED_TEMP_WARNING = "PNW.dismissedTempWarning"; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 71 | private static final String ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING = |
| 72 | "PNW.clickedThermalShutdownWarning"; |
| 73 | private static final String ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING = |
| 74 | "PNW.dismissedThermalShutdownWarning"; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 75 | private static final String ACTION_SHOW_START_SAVER_CONFIRMATION = |
| 76 | BatterySaverUtils.ACTION_SHOW_START_SAVER_CONFIRMATION; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 77 | private static final String ACTION_SHOW_AUTO_SAVER_SUGGESTION = |
| 78 | BatterySaverUtils.ACTION_SHOW_AUTO_SAVER_SUGGESTION; |
| 79 | private static final String ACTION_DISMISS_AUTO_SAVER_SUGGESTION = |
| 80 | "PNW.dismissAutoSaverSuggestion"; |
| 81 | |
| 82 | private static final String ACTION_ENABLE_AUTO_SAVER = |
| 83 | "PNW.enableAutoSaver"; |
| 84 | private static final String ACTION_AUTO_SAVER_NO_THANKS = |
| 85 | "PNW.autoSaverNoThanks"; |
| 86 | |
| 87 | private static final String SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING = |
| 88 | "android.settings.BATTERY_SAVER_SETTINGS"; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 89 | |
| 90 | private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder() |
| 91 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 92 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 93 | .build(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 94 | |
| 95 | private final Context mContext; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 96 | private final NotificationManager mNoMan; |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 97 | private final PowerManager mPowerMan; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 98 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 99 | private final Receiver mReceiver = new Receiver(); |
| 100 | private final Intent mOpenBatterySettings = settings(Intent.ACTION_POWER_USAGE_SUMMARY); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 101 | |
| 102 | private int mBatteryLevel; |
| 103 | private int mBucket; |
| 104 | private long mScreenOffTime; |
| 105 | private int mShowing; |
| 106 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 107 | private long mWarningTriggerTimeMs; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 108 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 109 | private Estimate mEstimate; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 110 | private long mLowWarningThreshold; |
| 111 | private long mSevereWarningThreshold; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 112 | private boolean mWarning; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 113 | private boolean mShowAutoSaverSuggestion; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 114 | private boolean mPlaySound; |
| 115 | private boolean mInvalidCharger; |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 116 | private SystemUIDialog mSaverConfirmation; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 117 | private SystemUIDialog mSaverEnabledConfirmation; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 118 | private boolean mHighTempWarning; |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 119 | private SystemUIDialog mHighTempDialog; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 120 | private SystemUIDialog mThermalShutdownDialog; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 121 | |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 122 | public PowerNotificationWarnings(Context context) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 123 | mContext = context; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 124 | mNoMan = mContext.getSystemService(NotificationManager.class); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 125 | mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 126 | mReceiver.init(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void dump(PrintWriter pw) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 131 | pw.print("mWarning="); pw.println(mWarning); |
| 132 | pw.print("mPlaySound="); pw.println(mPlaySound); |
| 133 | pw.print("mInvalidCharger="); pw.println(mInvalidCharger); |
| 134 | pw.print("mShowing="); pw.println(SHOWING_STRINGS[mShowing]); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 135 | pw.print("mSaverConfirmation="); pw.println(mSaverConfirmation != null ? "not null" : null); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 136 | pw.print("mSaverEnabledConfirmation="); |
| 137 | pw.println(mSaverEnabledConfirmation != null ? "not null" : null); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 138 | pw.print("mHighTempWarning="); pw.println(mHighTempWarning); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 139 | pw.print("mHighTempDialog="); pw.println(mHighTempDialog != null ? "not null" : null); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 140 | pw.print("mThermalShutdownDialog="); |
| 141 | pw.println(mThermalShutdownDialog != null ? "not null" : null); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 144 | private int getLowBatteryAutoTriggerDefaultLevel() { |
| 145 | return mContext.getResources().getInteger( |
| 146 | com.android.internal.R.integer.config_lowBatteryAutoTriggerDefaultLevel); |
| 147 | } |
| 148 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 149 | @Override |
| 150 | public void update(int batteryLevel, int bucket, long screenOffTime) { |
| 151 | mBatteryLevel = batteryLevel; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 152 | if (bucket >= 0) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 153 | mWarningTriggerTimeMs = 0; |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 154 | } else if (bucket < mBucket) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 155 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
Christoph Studer | 65fa0a9 | 2014-06-26 16:50:09 +0200 | [diff] [blame] | 156 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 157 | mBucket = bucket; |
| 158 | mScreenOffTime = screenOffTime; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 161 | @Override |
| 162 | public void updateEstimate(Estimate estimate) { |
| 163 | mEstimate = estimate; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 164 | if (estimate.estimateMillis <= mLowWarningThreshold) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 165 | mWarningTriggerTimeMs = System.currentTimeMillis(); |
| 166 | } |
| 167 | } |
| 168 | |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 169 | @Override |
| 170 | public void updateThresholds(long lowThreshold, long severeThreshold) { |
| 171 | mLowWarningThreshold = lowThreshold; |
| 172 | mSevereWarningThreshold = severeThreshold; |
| 173 | } |
| 174 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 175 | private void updateNotification() { |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 176 | if (DEBUG) Slog.d(TAG, "updateNotification mWarning=" + mWarning + " mPlaySound=" |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 177 | + mPlaySound + " mInvalidCharger=" + mInvalidCharger); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 178 | if (mInvalidCharger) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 179 | showInvalidChargerNotification(); |
| 180 | mShowing = SHOWING_INVALID_CHARGER; |
| 181 | } else if (mWarning) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 182 | showWarningNotification(); |
| 183 | mShowing = SHOWING_WARNING; |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 184 | } else if (mShowAutoSaverSuggestion) { |
| 185 | showAutoSaverSuggestionNotification(); |
| 186 | mShowing = SHOWING_AUTO_SAVER_SUGGESTION; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 187 | } else { |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 188 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 189 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 190 | mNoMan.cancelAsUser(TAG_AUTO_SAVER, |
| 191 | SystemMessage.NOTE_AUTO_SAVER_SUGGESTION, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 192 | mShowing = SHOWING_NOTHING; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private void showInvalidChargerNotification() { |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 197 | final Notification.Builder nb = |
| 198 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 199 | .setSmallIcon(R.drawable.ic_power_low) |
| 200 | .setWhen(0) |
| 201 | .setShowWhen(false) |
| 202 | .setOngoing(true) |
| 203 | .setContentTitle(mContext.getString(R.string.invalid_charger_title)) |
| 204 | .setContentText(mContext.getString(R.string.invalid_charger_text)) |
| 205 | .setColor(mContext.getColor( |
| 206 | com.android.internal.R.color.system_notification_accent_color)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 207 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 208 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 209 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, UserHandle.ALL); |
| 210 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 213 | protected void showWarningNotification() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 214 | final String percentage = NumberFormat.getPercentInstance() |
| 215 | .format((double) mBatteryLevel / 100.0); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 216 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 217 | // get standard notification copy |
| 218 | String title = mContext.getString(R.string.battery_low_title); |
| 219 | String contentText = mContext.getString(R.string.battery_low_percent_format, percentage); |
| 220 | |
| 221 | // override notification copy if hybrid notification enabled |
| 222 | if (mEstimate != null) { |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 223 | contentText = getHybridContentString(percentage); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 226 | final Notification.Builder nb = |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 227 | new Notification.Builder(mContext, NotificationChannels.BATTERY) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 228 | .setSmallIcon(R.drawable.ic_power_low) |
| 229 | // Bump the notification when the bucket dropped. |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 230 | .setWhen(mWarningTriggerTimeMs) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 231 | .setShowWhen(false) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 232 | .setContentText(contentText) |
Salvador Martinez | 086ab74 | 2018-04-03 13:05:39 -0700 | [diff] [blame] | 233 | .setContentTitle(title) |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 234 | .setOnlyAlertOnce(true) |
| 235 | .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_WARNING)) |
Salvador Martinez | 086ab74 | 2018-04-03 13:05:39 -0700 | [diff] [blame] | 236 | .setStyle(new Notification.BigTextStyle().bigText(contentText)) |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 237 | .setVisibility(Notification.VISIBILITY_PUBLIC); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 238 | if (hasBatterySettings()) { |
| 239 | nb.setContentIntent(pendingBroadcast(ACTION_SHOW_BATTERY_SETTINGS)); |
| 240 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 241 | // Make the notification red if the percentage goes below a certain amount or the time |
| 242 | // remaining estimate is disabled |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 243 | if (mEstimate == null || mBucket < 0 |
| 244 | || mEstimate.estimateMillis < mSevereWarningThreshold) { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 245 | nb.setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
| 246 | } |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 247 | nb.addAction(0, |
| 248 | mContext.getString(R.string.battery_saver_start_action), |
| 249 | pendingBroadcast(ACTION_START_SAVER)); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 250 | nb.setOnlyAlertOnce(!mPlaySound); |
| 251 | mPlaySound = false; |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 252 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 253 | final Notification n = nb.build(); |
| 254 | mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); |
| 255 | mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 258 | private void showAutoSaverSuggestionNotification() { |
| 259 | final Notification.Builder nb = |
| 260 | new Notification.Builder(mContext, NotificationChannels.HINTS) |
| 261 | .setSmallIcon(R.drawable.ic_power_saver) |
| 262 | .setWhen(0) |
| 263 | .setShowWhen(false) |
| 264 | .setContentTitle(mContext.getString(R.string.auto_saver_title)) |
| 265 | .setContentText(mContext.getString(R.string.auto_saver_text, |
| 266 | getLowBatteryAutoTriggerDefaultLevel())); |
| 267 | nb.setContentIntent(pendingBroadcast(ACTION_ENABLE_AUTO_SAVER)); |
| 268 | nb.setDeleteIntent(pendingBroadcast(ACTION_DISMISS_AUTO_SAVER_SUGGESTION)); |
| 269 | nb.addAction(0, |
| 270 | mContext.getString(R.string.no_auto_saver_action), |
| 271 | pendingBroadcast(ACTION_AUTO_SAVER_NO_THANKS)); |
| 272 | |
| 273 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
| 274 | |
| 275 | final Notification n = nb.build(); |
| 276 | mNoMan.notifyAsUser( |
| 277 | TAG_AUTO_SAVER, SystemMessage.NOTE_AUTO_SAVER_SUGGESTION, n, UserHandle.ALL); |
| 278 | } |
| 279 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 280 | private String getHybridContentString(String percentage) { |
| 281 | return PowerUtil.getBatteryRemainingStringFormatted( |
| 282 | mContext, |
| 283 | mEstimate.estimateMillis, |
| 284 | percentage, |
| 285 | mEstimate.isBasedOnUsage); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 286 | } |
| 287 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 288 | private PendingIntent pendingBroadcast(String action) { |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 289 | return PendingIntent.getBroadcastAsUser(mContext, 0, |
| 290 | new Intent(action).setPackage(mContext.getPackageName()), 0, UserHandle.CURRENT); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | private static Intent settings(String action) { |
| 294 | return new Intent(action).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 295 | | Intent.FLAG_ACTIVITY_MULTIPLE_TASK |
| 296 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
| 297 | | Intent.FLAG_ACTIVITY_NO_HISTORY |
| 298 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public boolean isInvalidChargerWarningShowing() { |
| 303 | return mInvalidCharger; |
| 304 | } |
| 305 | |
| 306 | @Override |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 307 | public void dismissHighTemperatureWarning() { |
| 308 | if (!mHighTempWarning) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 309 | return; |
| 310 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 311 | mHighTempWarning = false; |
| 312 | dismissHighTemperatureWarningInternal(); |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 316 | * Internal only version of {@link #dismissHighTemperatureWarning()} that simply dismisses |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 317 | * the notification. As such, the notification will not show again until |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 318 | * {@link #dismissHighTemperatureWarning()} is called. |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 319 | */ |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 320 | private void dismissHighTemperatureWarningInternal() { |
Andrew Sapperstein | 97bfa0f | 2017-01-24 16:38:50 -0800 | [diff] [blame] | 321 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, UserHandle.ALL); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 325 | public void showHighTemperatureWarning() { |
| 326 | if (mHighTempWarning) { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 327 | return; |
| 328 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 329 | mHighTempWarning = true; |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 330 | final Notification.Builder nb = |
| 331 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 332 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 333 | .setWhen(0) |
| 334 | .setShowWhen(false) |
| 335 | .setContentTitle(mContext.getString(R.string.high_temp_title)) |
| 336 | .setContentText(mContext.getString(R.string.high_temp_notif_message)) |
| 337 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 338 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_TEMP_WARNING)) |
| 339 | .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_TEMP_WARNING)) |
Jason Monk | 58be7a6 | 2017-02-01 20:17:51 -0500 | [diff] [blame] | 340 | .setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 341 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 342 | final Notification n = nb.build(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 343 | mNoMan.notifyAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_HIGH_TEMP, n, UserHandle.ALL); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 346 | private void showHighTemperatureDialog() { |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 347 | if (mHighTempDialog != null) return; |
| 348 | final SystemUIDialog d = new SystemUIDialog(mContext); |
Andrew Sapperstein | e26dc3d | 2017-01-04 11:25:20 -0800 | [diff] [blame] | 349 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 350 | d.setTitle(R.string.high_temp_title); |
| 351 | d.setMessage(R.string.high_temp_dialog_message); |
| 352 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 353 | d.setShowForAllUsers(true); |
| 354 | d.setOnDismissListener(dialog -> mHighTempDialog = null); |
| 355 | d.show(); |
| 356 | mHighTempDialog = d; |
| 357 | } |
| 358 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 359 | @VisibleForTesting |
| 360 | void dismissThermalShutdownWarning() { |
| 361 | mNoMan.cancelAsUser(TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, UserHandle.ALL); |
| 362 | } |
| 363 | |
| 364 | private void showThermalShutdownDialog() { |
| 365 | if (mThermalShutdownDialog != null) return; |
| 366 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 367 | d.setIconAttribute(android.R.attr.alertDialogIcon); |
| 368 | d.setTitle(R.string.thermal_shutdown_title); |
| 369 | d.setMessage(R.string.thermal_shutdown_dialog_message); |
| 370 | d.setPositiveButton(com.android.internal.R.string.ok, null); |
| 371 | d.setShowForAllUsers(true); |
| 372 | d.setOnDismissListener(dialog -> mThermalShutdownDialog = null); |
| 373 | d.show(); |
| 374 | mThermalShutdownDialog = d; |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public void showThermalShutdownWarning() { |
| 379 | final Notification.Builder nb = |
| 380 | new Notification.Builder(mContext, NotificationChannels.ALERTS) |
| 381 | .setSmallIcon(R.drawable.ic_device_thermostat_24) |
| 382 | .setWhen(0) |
| 383 | .setShowWhen(false) |
| 384 | .setContentTitle(mContext.getString(R.string.thermal_shutdown_title)) |
| 385 | .setContentText(mContext.getString(R.string.thermal_shutdown_message)) |
| 386 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 387 | .setContentIntent(pendingBroadcast(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING)) |
| 388 | .setDeleteIntent( |
| 389 | pendingBroadcast(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING)) |
| 390 | .setColor(Utils.getColorAttr(mContext, android.R.attr.colorError)); |
Julia Reynolds | 037d808 | 2018-03-18 15:25:19 -0400 | [diff] [blame] | 391 | SystemUI.overrideNotificationAppName(mContext, nb, false); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 392 | final Notification n = nb.build(); |
| 393 | mNoMan.notifyAsUser( |
| 394 | TAG_TEMPERATURE, SystemMessage.NOTE_THERMAL_SHUTDOWN, n, UserHandle.ALL); |
| 395 | } |
| 396 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 397 | @Override |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 398 | public void updateLowBatteryWarning() { |
| 399 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public void dismissLowBatteryWarning() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 404 | if (DEBUG) Slog.d(TAG, "dismissing low battery warning: level=" + mBatteryLevel); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 405 | dismissLowBatteryNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | private void dismissLowBatteryNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 409 | if (mWarning) Slog.i(TAG, "dismissing low battery notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 410 | mWarning = false; |
| 411 | updateNotification(); |
| 412 | } |
| 413 | |
| 414 | private boolean hasBatterySettings() { |
| 415 | return mOpenBatterySettings.resolveActivity(mContext.getPackageManager()) != null; |
| 416 | } |
| 417 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 418 | @Override |
| 419 | public void showLowBatteryWarning(boolean playSound) { |
| 420 | Slog.i(TAG, |
| 421 | "show low battery warning: level=" + mBatteryLevel |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 422 | + " [" + mBucket + "] playSound=" + playSound); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 423 | mPlaySound = playSound; |
| 424 | mWarning = true; |
| 425 | updateNotification(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 426 | } |
| 427 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 428 | @Override |
| 429 | public void dismissInvalidChargerWarning() { |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 430 | dismissInvalidChargerNotification(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | private void dismissInvalidChargerNotification() { |
John Spurlock | 3ff2de6 | 2014-06-16 13:32:48 -0400 | [diff] [blame] | 434 | if (mInvalidCharger) Slog.i(TAG, "dismissing invalid charger notification"); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 435 | mInvalidCharger = false; |
| 436 | updateNotification(); |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void showInvalidChargerWarning() { |
| 441 | mInvalidCharger = true; |
| 442 | updateNotification(); |
| 443 | } |
| 444 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 445 | private void showAutoSaverSuggestion() { |
| 446 | mShowAutoSaverSuggestion = true; |
| 447 | updateNotification(); |
| 448 | } |
| 449 | |
| 450 | private void dismissAutoSaverSuggestion() { |
| 451 | mShowAutoSaverSuggestion = false; |
| 452 | updateNotification(); |
| 453 | } |
| 454 | |
John Spurlock | ecbc5e8 | 2014-10-22 09:05:51 -0400 | [diff] [blame] | 455 | @Override |
| 456 | public void userSwitched() { |
| 457 | updateNotification(); |
| 458 | } |
| 459 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 460 | private void showStartSaverConfirmation() { |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 461 | if (mSaverConfirmation != null) return; |
| 462 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 463 | d.setTitle(R.string.battery_saver_confirmation_title); |
| 464 | d.setMessage(com.android.internal.R.string.battery_saver_description); |
| 465 | d.setNegativeButton(android.R.string.cancel, null); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 466 | d.setPositiveButton(R.string.battery_saver_confirmation_ok, |
| 467 | (dialog, which) -> setSaverMode(true, false)); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 468 | d.setShowForAllUsers(true); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 469 | d.setOnDismissListener((dialog) -> mSaverConfirmation = null); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 470 | d.show(); |
John Spurlock | 1bb480a | 2014-08-02 17:12:43 -0400 | [diff] [blame] | 471 | mSaverConfirmation = d; |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 474 | private void showAutoSaverEnabledConfirmation() { |
| 475 | if (mSaverEnabledConfirmation != null) return; |
| 476 | |
| 477 | // Open the Battery Saver setting page. |
| 478 | final Intent actionBatterySaverSetting = |
| 479 | new Intent(SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING) |
| 480 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 481 | |
| 482 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 483 | d.setTitle(R.string.auto_saver_enabled_title); |
| 484 | d.setMessage(mContext.getString(R.string.auto_saver_enabled_text, |
| 485 | getLowBatteryAutoTriggerDefaultLevel())); |
| 486 | |
Makoto Onuki | 41d5ccf | 2018-04-03 11:46:02 -0700 | [diff] [blame] | 487 | // "Got it". Just close the dialog. Automatic battery has been enabled already. |
| 488 | d.setPositiveButton(R.string.auto_saver_okay_action, |
| 489 | (dialog, which) -> onAutoSaverEnabledConfirmationClosed()); |
| 490 | |
| 491 | // "Settings" -> Opens the battery saver settings activity. |
| 492 | d.setNeutralButton(R.string.open_saver_setting_action, (dialog, which) -> { |
| 493 | mContext.startActivity(actionBatterySaverSetting); |
| 494 | onAutoSaverEnabledConfirmationClosed(); |
| 495 | }); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 496 | d.setShowForAllUsers(true); |
Makoto Onuki | 41d5ccf | 2018-04-03 11:46:02 -0700 | [diff] [blame] | 497 | d.setOnDismissListener((dialog) -> onAutoSaverEnabledConfirmationClosed()); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 498 | d.show(); |
| 499 | mSaverEnabledConfirmation = d; |
| 500 | } |
| 501 | |
Makoto Onuki | 41d5ccf | 2018-04-03 11:46:02 -0700 | [diff] [blame] | 502 | private void onAutoSaverEnabledConfirmationClosed() { |
| 503 | mSaverEnabledConfirmation = null; |
| 504 | } |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 505 | |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 506 | private void setSaverMode(boolean mode, boolean needFirstTimeWarning) { |
| 507 | BatterySaverUtils.setPowerSaveMode(mContext, mode, needFirstTimeWarning); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 510 | private void scheduleAutoBatterySaver() { |
| 511 | int autoTriggerThreshold = mContext.getResources().getInteger( |
| 512 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
| 513 | if (autoTriggerThreshold == 0) { |
| 514 | autoTriggerThreshold = 15; |
| 515 | } |
| 516 | |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 517 | BatterySaverUtils.ensureAutoBatterySaver(mContext, autoTriggerThreshold); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 518 | showAutoSaverEnabledConfirmation(); |
| 519 | } |
| 520 | |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 521 | private final class Receiver extends BroadcastReceiver { |
| 522 | |
| 523 | public void init() { |
| 524 | IntentFilter filter = new IntentFilter(); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 525 | filter.addAction(ACTION_SHOW_BATTERY_SETTINGS); |
| 526 | filter.addAction(ACTION_START_SAVER); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 527 | filter.addAction(ACTION_DISMISSED_WARNING); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 528 | filter.addAction(ACTION_CLICKED_TEMP_WARNING); |
| 529 | filter.addAction(ACTION_DISMISSED_TEMP_WARNING); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 530 | filter.addAction(ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING); |
| 531 | filter.addAction(ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING); |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 532 | filter.addAction(ACTION_SHOW_START_SAVER_CONFIRMATION); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 533 | filter.addAction(ACTION_SHOW_AUTO_SAVER_SUGGESTION); |
| 534 | filter.addAction(ACTION_ENABLE_AUTO_SAVER); |
| 535 | filter.addAction(ACTION_AUTO_SAVER_NO_THANKS); |
Makoto Onuki | 5ac8d5e | 2018-04-13 15:53:57 -0700 | [diff] [blame] | 536 | filter.addAction(ACTION_DISMISS_AUTO_SAVER_SUGGESTION); |
John Spurlock | 05e0705 | 2015-06-01 10:56:42 -0400 | [diff] [blame] | 537 | mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 538 | android.Manifest.permission.DEVICE_POWER, mHandler); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public void onReceive(Context context, Intent intent) { |
| 543 | final String action = intent.getAction(); |
John Spurlock | eb44a7d | 2014-06-12 13:00:55 -0400 | [diff] [blame] | 544 | Slog.i(TAG, "Received " + action); |
John Spurlock | 86c3de8 | 2014-08-19 13:37:44 -0400 | [diff] [blame] | 545 | if (action.equals(ACTION_SHOW_BATTERY_SETTINGS)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 546 | dismissLowBatteryNotification(); |
| 547 | mContext.startActivityAsUser(mOpenBatterySettings, UserHandle.CURRENT); |
| 548 | } else if (action.equals(ACTION_START_SAVER)) { |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 549 | setSaverMode(true, true); |
| 550 | dismissLowBatteryNotification(); |
| 551 | } else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) { |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 552 | dismissLowBatteryNotification(); |
| 553 | showStartSaverConfirmation(); |
John Spurlock | 42bfc9a | 2014-10-29 11:13:01 -0400 | [diff] [blame] | 554 | } else if (action.equals(ACTION_DISMISSED_WARNING)) { |
| 555 | dismissLowBatteryWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 556 | } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 557 | dismissHighTemperatureWarningInternal(); |
| 558 | showHighTemperatureDialog(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 559 | } else if (ACTION_DISMISSED_TEMP_WARNING.equals(action)) { |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 560 | dismissHighTemperatureWarningInternal(); |
| 561 | } else if (ACTION_CLICKED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 562 | dismissThermalShutdownWarning(); |
| 563 | showThermalShutdownDialog(); |
| 564 | } else if (ACTION_DISMISSED_THERMAL_SHUTDOWN_WARNING.equals(action)) { |
| 565 | dismissThermalShutdownWarning(); |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 566 | } else if (ACTION_SHOW_AUTO_SAVER_SUGGESTION.equals(action)) { |
| 567 | showAutoSaverSuggestion(); |
| 568 | } else if (ACTION_DISMISS_AUTO_SAVER_SUGGESTION.equals(action)) { |
| 569 | dismissAutoSaverSuggestion(); |
| 570 | } else if (ACTION_ENABLE_AUTO_SAVER.equals(action)) { |
| 571 | dismissAutoSaverSuggestion(); |
| 572 | scheduleAutoBatterySaver(); |
| 573 | } else if (ACTION_AUTO_SAVER_NO_THANKS.equals(action)) { |
| 574 | dismissAutoSaverSuggestion(); |
| 575 | BatterySaverUtils.suppressAutoBatterySaver(context); |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
John Spurlock | 3332ba5 | 2014-03-10 17:44:07 -0400 | [diff] [blame] | 579 | } |