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

Commit f422b1a4 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Update battery saver warning

This warning needs to be shown if a user tries to set a Battery
Saver schedule without having seen it so we make the show dialog
method public. Addtionally we make it possible to trigger a more
generic version of the notification that blocks whatever action
the user was trying to take and only marks if they confirmed
the notification or not. Once the user has acknowledged the
notification they can repeat their action to get the desired
result. Text is updated in the generic version to make it
more clear that the dialog is just information and has no
effect.

Test: robotests pass, manual verification
Bug: 128924009
Change-Id: Ia4e7935de029f8e74e5236d8bf1651a53c5021c6
parent c4ef6428
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4835,7 +4835,7 @@
    <string name="package_deleted_device_owner">Deleted by your admin</string>

    <!-- [CHAR LIMIT=25] String for confirmation button to enable a feature gated by the battery saver warning-->
    <string name="confirm_battery_saver">Confirm</string>
    <string name="confirm_battery_saver">OK</string>

    <!-- [CHAR_LIMIT=NONE] Battery saver: Feature description, with a "learn more" link. -->
    <string name="battery_saver_description_with_learn_more">To extend your battery life, Battery Saver turns off some device features and restricts apps. <annotation id="url">Learn More</annotation></string>
+1 −0
Original line number Diff line number Diff line
@@ -3602,6 +3602,7 @@
  <java-symbol type="style" name="Theme.DeviceDefault.Light.Dialog.Alert.UserSwitchingDialog" />

  <java-symbol type="string" name="battery_saver_description_with_learn_more" />
  <java-symbol type="string" name="confirm_battery_saver" />

  <java-symbol type="attr" name="opticalInsetLeft" />
  <java-symbol type="attr" name="opticalInsetTop" />
+22 −7
Original line number Diff line number Diff line
@@ -31,7 +31,9 @@ import android.util.Slog;
 * Utilities related to battery saver.
 */
public class BatterySaverUtils {

    private static final String TAG = "BatterySaverUtils";
    public static final String EXTRA_CONFIRM_ONLY = "extra_confirm_only";

    private BatterySaverUtils() {
    }
@@ -96,7 +98,7 @@ public class BatterySaverUtils {
        }
        final ContentResolver cr = context.getContentResolver();

        if (enable && needFirstTimeWarning && maybeShowBatterySaverConfirmation(context)) {
        if (enable && needFirstTimeWarning && maybeShowBatterySaverConfirmation(context, false)) {
            return false;
        }
        if (enable && !needFirstTimeWarning) {
@@ -116,7 +118,7 @@ public class BatterySaverUtils {
                        && Global.getInt(cr, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) == 0
                        && Secure.getInt(cr,
                        Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0) == 0) {
                    showAutoBatterySaverSuggestion(context);
                    showAutoBatterySaverSuggestion(context, false);
                }
            }

@@ -125,23 +127,36 @@ public class BatterySaverUtils {
        return false;
    }

    private static boolean maybeShowBatterySaverConfirmation(Context context) {
    /**
     * Shows the battery saver confirmation warning if it hasn't been acknowledged by the user in
     * the past before. When confirmOnly is true, the dialog will have generic info about battery
     * saver but will only update that the user has been shown the notification and take no
     * further action. if confirmOnly is false it will show a more specific version of the dialog
     * that toggles battery saver when acknowledged
     * @param context A valid context
     * @param confirmOnly Whether to show the actionless generic dialog (true) or the specific one
     * that toggles battery saver (false)
     * @return True if it showed the notification because it has not been previously acknowledged.
     */
    public static boolean maybeShowBatterySaverConfirmation(Context context, boolean confirmOnly) {
        if (Secure.getInt(context.getContentResolver(),
                Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0) != 0) {
            return false; // Already shown.
        }
        context.sendBroadcast(getSystemUiBroadcast(ACTION_SHOW_START_SAVER_CONFIRMATION));
        context.sendBroadcast(
                getSystemUiBroadcast(ACTION_SHOW_START_SAVER_CONFIRMATION, confirmOnly));
        return true;
    }

    private static void showAutoBatterySaverSuggestion(Context context) {
        context.sendBroadcast(getSystemUiBroadcast(ACTION_SHOW_AUTO_SAVER_SUGGESTION));
    private static void showAutoBatterySaverSuggestion(Context context, boolean confirmOnly) {
        context.sendBroadcast(getSystemUiBroadcast(ACTION_SHOW_AUTO_SAVER_SUGGESTION, confirmOnly));
    }

    private static Intent getSystemUiBroadcast(String action) {
    private static Intent getSystemUiBroadcast(String action, boolean confirmOnly) {
        final Intent i = new Intent(action);
        i.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        i.setPackage(SYSUI_PACKAGE);
        i.putExtra(EXTRA_CONFIRM_ONLY, confirmOnly);
        return i;
    }

+3 −0
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@
    <!-- Battery saver confirmation dialog title [CHAR LIMIT=NONE]-->
    <string name="battery_saver_confirmation_title">Turn on Battery Saver?</string>

    <!-- The more generic version of the battery saver confirmation dialog title [CHAR LIMIT=NONE] -->
    <string name="battery_saver_confirmation_title_generic">About Battery Saver</string>

    <!-- Battery saver confirmation dialog ok text [CHAR LIMIT=40]-->
    <string name="battery_saver_confirmation_ok">Turn on</string>

+17 −5
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.os.UserHandle;
import android.provider.Settings.Secure;
import android.text.Annotation;
import android.text.Layout;
import android.text.SpannableString;
@@ -70,6 +71,7 @@ import javax.inject.Singleton;
 */
@Singleton
public class PowerNotificationWarnings implements PowerUI.WarningsUI {

    private static final String TAG = PowerUI.TAG + ".Notification";
    private static final boolean DEBUG = PowerUI.DEBUG;

@@ -119,6 +121,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
            .build();
    public static final String EXTRA_CONFIRM_ONLY = "extra_confirm_only";

    private final Context mContext;
    private final NotificationManager mNoMan;
@@ -544,10 +547,9 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
        updateNotification();
    }

    private void showStartSaverConfirmation() {
    private void showStartSaverConfirmation(boolean confirmOnly) {
        if (mSaverConfirmation != null) return;
        final SystemUIDialog d = new SystemUIDialog(mContext);
        d.setTitle(R.string.battery_saver_confirmation_title);
        d.setMessage(getBatterySaverDescription());

        // Sad hack for http://b/78261259 and http://b/78298335. Otherwise "Battery" may be split
@@ -558,9 +560,19 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
        // We need to set LinkMovementMethod to make the link clickable.
        d.setMessageMovementMethod(LinkMovementMethod.getInstance());

        d.setNegativeButton(android.R.string.cancel, null);
        if (confirmOnly) {
            d.setTitle(R.string.battery_saver_confirmation_title_generic);
            d.setPositiveButton(com.android.internal.R.string.confirm_battery_saver,
                    (dialog, which) -> Secure.putInt(
                            mContext.getContentResolver(),
                            Secure.LOW_POWER_WARNING_ACKNOWLEDGED,
                            1));
        } else {
            d.setTitle(R.string.battery_saver_confirmation_title);
            d.setPositiveButton(R.string.battery_saver_confirmation_ok,
                (dialog, which) -> setSaverMode(true, false));
            d.setNegativeButton(android.R.string.cancel, null);
        }
        d.setShowForAllUsers(true);
        d.setOnDismissListener((dialog) -> mSaverConfirmation = null);
        d.show();
@@ -719,7 +731,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
                dismissLowBatteryNotification();
            } else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) {
                dismissLowBatteryNotification();
                showStartSaverConfirmation();
                showStartSaverConfirmation(intent.getBooleanExtra(EXTRA_CONFIRM_ONLY, false));
            } else if (action.equals(ACTION_DISMISSED_WARNING)) {
                dismissLowBatteryWarning();
            } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) {