Extreme battery saver: Show warning notification instead of toast

(Just for now; we'll remove it before shipping.)

Bug: 68769804
Test: manual test
Change-Id: Ib1d58fbc88f79a62030a2e59d1dce15e9ff1323a
This commit is contained in:
Makoto Onuki
2017-11-29 17:46:36 -08:00
parent 229f0d0305
commit 036cc818a9
4 changed files with 70 additions and 5 deletions

View File

@@ -4753,5 +4753,8 @@
<string name="shortcut_restore_unknown_issue">Couldn\u2019t restore shortcut</string>
<!--Battery saver warning. STOPSHIP: Remove it eventually. -->
<string name="battery_saver_warning" translatable="false">Battery saver activated.\n\nSee go/extreme-battery-saver.\n\nThis contains aggressive experimental changes for P and may affect background app behavior.\n</string>
<string name="battery_saver_warning" translatable="false">\"Extreme\" battery saver activated.\n\nSee the details at: go/extreme-battery-saver\n\nEBS aggressively throttles background apps and changes screen-off behavior.\n</string>
<!--Battery saver warning. STOPSHIP: Remove it eventually. -->
<string name="battery_saver_warning_title" translatable="false">Extreme battery saver</string>
</resources>

View File

@@ -3158,4 +3158,6 @@
<java-symbol type="layout" name="unsupported_compile_sdk_dialog_content" />
<java-symbol type="string" name="unsupported_compile_sdk_message" />
<java-symbol type="string" name="unsupported_compile_sdk_check_update" />
<java-symbol type="string" name="battery_saver_warning_title" />
</resources>

View File

@@ -223,6 +223,9 @@ message SystemMessage {
// Package: com.android.systemui
NOTE_TV_PIP = 1100;
// Extreme battery saver notifiaction.
NOTE_BATTERY_SAVER_WARNING = 1200;
// Notify the user that open Wi-Fi networks are available.
// Package: android
NOTE_NETWORK_AVAILABLE = 17303299;

View File

@@ -16,12 +16,18 @@
package com.android.server.power.batterysaver;
import android.Manifest;
import android.app.ActivityManager;
import android.app.ActivityManagerInternal;
import android.app.Notification;
import android.app.Notification.BigTextStyle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.power.V1_0.PowerHint;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@@ -32,9 +38,11 @@ import android.os.PowerSaveState;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Slog;
import android.widget.Toast;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.internal.notification.SystemNotificationChannels;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;
@@ -61,6 +69,8 @@ public class BatterySaverController implements BatterySaverPolicyListener {
private final BatterySaverPolicy mBatterySaverPolicy;
private static final String WARNING_LINK_URL = "http://goto.google.com/extreme-battery-saver";
@GuardedBy("mLock")
private final ArrayList<LowPowerModeListener> mListeners = new ArrayList<>();
@@ -230,9 +240,9 @@ public class BatterySaverController implements BatterySaverPolicyListener {
if (sendBroadcast) {
if (enabled) {
// STOPSHIP Remove the toast.
Toast.makeText(mContext,
com.android.internal.R.string.battery_saver_warning,
Toast.LENGTH_LONG).show();
postWarningNotification();
} else {
cancelWarningNotification();
}
if (DEBUG) {
@@ -265,4 +275,51 @@ public class BatterySaverController implements BatterySaverPolicyListener {
}
}
}
private void postWarningNotification() {
final UserHandle foregroundUser = UserHandle.of(ActivityManager.getCurrentUser());
final PendingIntent pendingIntent = PendingIntent
.getActivityAsUser(mContext, 0,
new Intent(Intent.ACTION_VIEW, Uri.parse(WARNING_LINK_URL)),
PendingIntent.FLAG_CANCEL_CURRENT, null,
foregroundUser);
final CharSequence title = mContext.getString
(com.android.internal.R.string.battery_saver_warning_title);
final CharSequence text = mContext.getString
(com.android.internal.R.string.battery_saver_warning);
final Notification notification =
new Notification.Builder(mContext, SystemNotificationChannels.ALERTS)
.setSmallIcon(R.drawable.stat_notify_error)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pendingIntent)
.setStyle(new BigTextStyle().bigText(text))
.build();
final NotificationManager nm = mContext.getSystemService(NotificationManager.class);
if (nm != null) {
nm.notifyAsUser(title.toString(),
SystemMessage.NOTE_BATTERY_SAVER_WARNING,
notification,
foregroundUser);
}
}
private void cancelWarningNotification() {
final UserHandle foregroundUser = UserHandle.of(ActivityManager.getCurrentUser());
final CharSequence title = mContext.getString
(com.android.internal.R.string.battery_saver_warning_title);
final NotificationManager nm = mContext.getSystemService(NotificationManager.class);
if (nm != null) {
nm.cancelAsUser(title.toString(), SystemMessage.NOTE_BATTERY_SAVER_WARNING,
foregroundUser);
}
}
}