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

Commit cf50ac99 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Update battery saver warning"

parents a9cb33dc f422b1a4
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">Battery Saver turns off or restricts background activity, some visual effects \u0026 other high-power features to extend battery life. <annotation id="url">Learn More</annotation></string>
+1 −0
Original line number Diff line number Diff line
@@ -3604,6 +3604,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)) {