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

Commit 3ae9fdc5 authored by Makoto Onuki's avatar Makoto Onuki Committed by android-build-merger
Browse files

Merge "Once auto-battery saver is ever set, don't show suggestion" into pi-dev

am: 69ade357

Change-Id: I7820e690da6f5540fb1da368556a98f27df16280
parents 6fb1b0ba 69ade357
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -148,15 +148,32 @@ public class BatterySaverUtils {
        Secure.putInt(context.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1);
    }

    /**
     * Don't show the automatic battery suggestion notification in the future.
     */
    public static void suppressAutoBatterySaver(Context context) {
        Secure.putInt(context.getContentResolver(),
                Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 1);
    }

    public static void scheduleAutoBatterySaver(Context context, int level) {
    /**
     * Set the automatic battery saver trigger level to {@code level}.
     */
    public static void setAutoBatterySaverTriggerLevel(Context context, int level) {
        if (level > 0) {
            suppressAutoBatterySaver(context);
        }
        Global.putInt(context.getContentResolver(), Global.LOW_POWER_MODE_TRIGGER_LEVEL, level);
    }

    /**
     * Set the automatic battery saver trigger level to {@code level}, but only when
     * automatic battery saver isn't enabled yet.
     */
    public static void ensureAutoBatterySaver(Context context, int level) {
        if (Global.getInt(context.getContentResolver(), Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)
                == 0) {
            Global.putInt(context.getContentResolver(), Global.LOW_POWER_MODE_TRIGGER_LEVEL, level);
            setAutoBatterySaverTriggerLevel(context, level);
        }
    }
}
+39 −0
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package com.android.settingslib.fuelgauge;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
@@ -28,6 +30,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings.Global;
import android.provider.Settings.Secure;

import com.android.settingslib.SettingsLibRobolectricTestRunner;
@@ -41,6 +44,9 @@ import org.mockito.MockitoAnnotations;

@RunWith(SettingsLibRobolectricTestRunner.class)
public class BatterySaverUtilsTest {
    final int BATTERY_SAVER_THRESHOLD_1 = 15;
    final int BATTERY_SAVER_THRESHOLD_2 = 20;

    @Mock
    Context mMockContext;

@@ -149,4 +155,37 @@ public class BatterySaverUtilsTest {
        assertEquals(-2,
                Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
    }

    @Test
    public void testEnsureAutoBatterysaver_setNewPositiveValue_doNotOverwrite() throws Exception {
        Global.putString(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, "null");

        BatterySaverUtils.ensureAutoBatterySaver(mMockContext, BATTERY_SAVER_THRESHOLD_1);

        assertThat(Secure.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1))
                .isEqualTo(BATTERY_SAVER_THRESHOLD_1);

        // Once a positive number is set, ensureAutoBatterySaver() won't overwrite it.
        BatterySaverUtils.ensureAutoBatterySaver(mMockContext, BATTERY_SAVER_THRESHOLD_2);
        assertThat(Secure.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1))
                .isEqualTo(BATTERY_SAVER_THRESHOLD_1);
    }

    @Test
    public void testSetAutoBatterySaverTriggerLevel_setSuppressSuggestion() throws Exception {
        Global.putString(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, "null");
        Secure.putString(mMockResolver, Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, "null");

        BatterySaverUtils.setAutoBatterySaverTriggerLevel(mMockContext, 0);
        assertThat(Global.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1))
                .isEqualTo(0);
        assertThat(Secure.getInt(mMockResolver, Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, -1))
                .isEqualTo(-1); // not set.

        BatterySaverUtils.setAutoBatterySaverTriggerLevel(mMockContext, BATTERY_SAVER_THRESHOLD_1 );
        assertThat( Global.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1))
                .isEqualTo(BATTERY_SAVER_THRESHOLD_1);
        assertThat(Secure.getInt(mMockResolver, Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, -1))
                .isEqualTo(1);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -514,7 +514,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
            autoTriggerThreshold = 15;
        }

        BatterySaverUtils.scheduleAutoBatterySaver(mContext, autoTriggerThreshold);
        BatterySaverUtils.ensureAutoBatterySaver(mContext, autoTriggerThreshold);
        showAutoSaverEnabledConfirmation();
    }