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