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

This commit is contained in:
Makoto Onuki
2018-04-09 20:29:30 +00:00
committed by Android (Google) Code Review
3 changed files with 59 additions and 3 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -514,7 +514,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
autoTriggerThreshold = 15;
}
BatterySaverUtils.scheduleAutoBatterySaver(mContext, autoTriggerThreshold);
BatterySaverUtils.ensureAutoBatterySaver(mContext, autoTriggerThreshold);
showAutoSaverEnabledConfirmation();
}