Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84b5bff3 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Update battery saver suggestion to go to right page

Now that there are several different versions of automatic
battery saver, this notification should just take you to the
battery saver schedule page rather than setting it to 15%
automatic triggering.

Test: adb shell am broadcast -a PNW.autoSaverSuggestion
      --ez extra_confirm_only false
Bug: 122599859

Change-Id: I3f8cd73b146d7528bf4125e40efa564a96b0ee6c
parent c3bfdf52
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2362,7 +2362,7 @@
    <string name="auto_saver_title">Tap to schedule Battery Saver</string>

    <!-- The content of the notification to suggest enabling automatic battery saver.  [CHAR LIMIT=NONE]-->
    <string name="auto_saver_text">Turn on automatically when battery is at <xliff:g id="percentage">%d</xliff:g>%%</string>
    <string name="auto_saver_text">Turn on when battery is likely to run out</string>

    <!-- An action on the notification to suggest enabling automatic battery saver: Do not turn on automatic battery saver.  [CHAR LIMIT=NONE]-->
    <string name="no_auto_saver_action">No thanks</string>
+11 −46
Original line number Diff line number Diff line
@@ -118,6 +118,8 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {

    private static final String SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING =
            "android.settings.BATTERY_SAVER_SETTINGS";
    public static final String BATTERY_SAVER_SCHEDULE_SCREEN_INTENT_ACTION =
            "com.android.settings.BATTERY_SAVER_SCHEDULE_SETTINGS";

    private static final String BATTERY_SAVER_DESCRIPTION_URL_KEY = "url";

@@ -152,16 +154,18 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
    private SystemUIDialog mThermalShutdownDialog;
    @VisibleForTesting SystemUIDialog mUsbHighTempDialog;
    private BatteryStateSnapshot mCurrentBatterySnapshot;
    private ActivityStarter mActivityStarter;

    /**
     */
    @Inject
    public PowerNotificationWarnings(Context context) {
    public PowerNotificationWarnings(Context context, ActivityStarter activityStarter) {
        mContext = context;
        mNoMan = mContext.getSystemService(NotificationManager.class);
        mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mKeyguard = mContext.getSystemService(KeyguardManager.class);
        mReceiver.init();
        mActivityStarter = activityStarter;
    }

    @Override
@@ -172,7 +176,6 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
        pw.print("mShowing="); pw.println(SHOWING_STRINGS[mShowing]);
        pw.print("mSaverConfirmation="); pw.println(mSaverConfirmation != null ? "not null" : null);
        pw.print("mSaverEnabledConfirmation=");
        pw.println(mSaverEnabledConfirmation != null ? "not null" : null);
        pw.print("mHighTempWarning="); pw.println(mHighTempWarning);
        pw.print("mHighTempDialog="); pw.println(mHighTempDialog != null ? "not null" : null);
        pw.print("mThermalShutdownDialog=");
@@ -305,8 +308,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
                        .setWhen(0)
                        .setShowWhen(false)
                        .setContentTitle(mContext.getString(R.string.auto_saver_title))
                        .setContentText(mContext.getString(R.string.auto_saver_text,
                                getLowBatteryAutoTriggerDefaultLevel()));
                        .setContentText(mContext.getString(R.string.auto_saver_text));
        nb.setContentIntent(pendingBroadcast(ACTION_ENABLE_AUTO_SAVER));
        nb.setDeleteIntent(pendingBroadcast(ACTION_DISMISS_AUTO_SAVER_SUGGESTION));
        nb.addAction(0,
@@ -673,51 +675,14 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
        return builder;
    }

    private void showAutoSaverEnabledConfirmation() {
        if (mSaverEnabledConfirmation != null) return;

        // Open the Battery Saver setting page.
        final Intent actionBatterySaverSetting =
                new Intent(SETTINGS_ACTION_OPEN_BATTERY_SAVER_SETTING)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        final SystemUIDialog d = new SystemUIDialog(mContext);
        d.setTitle(R.string.auto_saver_enabled_title);
        d.setMessage(mContext.getString(R.string.auto_saver_enabled_text,
                getLowBatteryAutoTriggerDefaultLevel()));

        // "Got it". Just close the dialog. Automatic battery has been enabled already.
        d.setPositiveButton(R.string.auto_saver_okay_action,
                (dialog, which) -> onAutoSaverEnabledConfirmationClosed());

        // "Settings" -> Opens the battery saver settings activity.
        d.setNeutralButton(R.string.open_saver_setting_action, (dialog, which) -> {
            mContext.startActivity(actionBatterySaverSetting);
            onAutoSaverEnabledConfirmationClosed();
        });
        d.setShowForAllUsers(true);
        d.setOnDismissListener((dialog) -> onAutoSaverEnabledConfirmationClosed());
        d.show();
        mSaverEnabledConfirmation = d;
    }

    private void onAutoSaverEnabledConfirmationClosed() {
        mSaverEnabledConfirmation = null;
    }

    private void setSaverMode(boolean mode, boolean needFirstTimeWarning) {
        BatterySaverUtils.setPowerSaveMode(mContext, mode, needFirstTimeWarning);
    }

    private void scheduleAutoBatterySaver() {
        int autoTriggerThreshold = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_lowBatteryWarningLevel);
        if (autoTriggerThreshold == 0) {
            autoTriggerThreshold = 15;
        }

        BatterySaverUtils.ensureAutoBatterySaver(mContext, autoTriggerThreshold);
        showAutoSaverEnabledConfirmation();
    private void startBatterySaverSchedulePage() {
        Intent intent = new Intent(BATTERY_SAVER_SCHEDULE_SCREEN_INTENT_ACTION);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        mActivityStarter.startActivity(intent, true /* dismissShade */);
    }

    private final class Receiver extends BroadcastReceiver {
@@ -771,7 +736,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
                dismissAutoSaverSuggestion();
            } else if (ACTION_ENABLE_AUTO_SAVER.equals(action)) {
                dismissAutoSaverSuggestion();
                scheduleAutoBatterySaver();
                startBatterySaverSchedulePage();
            } else if (ACTION_AUTO_SAVER_NO_THANKS.equals(action)) {
                dismissAutoSaverSuggestion();
                BatterySaverUtils.suppressAutoBatterySaver(context);
+3 −1
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.util.NotificationChannels;

import org.junit.Before;
@@ -57,7 +58,8 @@ public class PowerNotificationWarningsTest extends SysuiTestCase {
    public void setUp() throws Exception {
        // Test Instance.
        mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager);
        mPowerNotificationWarnings = new PowerNotificationWarnings(mContext);
        ActivityStarter starter = mDependency.injectMockDependency(ActivityStarter.class);
        mPowerNotificationWarnings = new PowerNotificationWarnings(mContext, starter);
        BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1,
                BatteryManager.BATTERY_HEALTH_GOOD, 5, 15);
        mPowerNotificationWarnings.updateSnapshot(snapshot);