Reset smart battery page switch component

- Reset the switch to default design

Bug: 178199757
Test: make RunSettingsRoboTests -j40
Change-Id: I698493a946d7c8daaac73bfd3aaab807244997b0
This commit is contained in:
Wesley.CW Wang
2021-03-29 18:47:57 +08:00
committed by Wesley Wang
parent 6a8ca4062e
commit 5c8caa3154
3 changed files with 24 additions and 32 deletions

View File

@@ -22,11 +22,6 @@
android:title="@string/smart_battery_manager_title" android:title="@string/smart_battery_manager_title"
settings:searchable="false"> settings:searchable="false">
<com.android.settingslib.widget.MainSwitchPreference
android:key="smart_battery"
android:title="@string/adaptive_battery_main_switch_title"
settings:controller="com.android.settings.fuelgauge.SmartBatteryPreferenceController"/>
<com.android.settings.widget.VideoPreference <com.android.settings.widget.VideoPreference
android:key="auto_awesome_battery" android:key="auto_awesome_battery"
android:title="@string/summary_placeholder" android:title="@string/summary_placeholder"
@@ -34,6 +29,13 @@
settings:preview="@drawable/auto_awesome_battery" settings:preview="@drawable/auto_awesome_battery"
settings:controller="com.android.settings.widget.VideoPreferenceController"/> settings:controller="com.android.settings.widget.VideoPreferenceController"/>
<SwitchPreference
android:key="smart_battery"
android:title="@string/smart_battery_title"
android:summary="@string/smart_battery_summary"
settings:controller="com.android.settings.fuelgauge.SmartBatteryPreferenceController"
settings:allowDividerAbove="true"/>
<SwitchPreference <SwitchPreference
android:key="auto_restriction" android:key="auto_restriction"
android:title="@string/battery_auto_restriction_title" android:title="@string/battery_auto_restriction_title"

View File

@@ -20,27 +20,23 @@ package com.android.settings.fuelgauge;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.widget.Switch;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreference;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
/** /**
* Controller to change and update the smart battery toggle * Controller to change and update the smart battery toggle
*/ */
public class SmartBatteryPreferenceController extends BasePreferenceController implements public class SmartBatteryPreferenceController extends BasePreferenceController implements
OnMainSwitchChangeListener { Preference.OnPreferenceChangeListener {
private static final String KEY_SMART_BATTERY = "smart_battery"; private static final String KEY_SMART_BATTERY = "smart_battery";
private static final int ON = 1; private static final int ON = 1;
private static final int OFF = 0; private static final int OFF = 0;
private PowerUsageFeatureProvider mPowerUsageFeatureProvider; private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
private MainSwitchPreference mPreference;
public SmartBatteryPreferenceController(Context context) { public SmartBatteryPreferenceController(Context context) {
super(context, KEY_SMART_BATTERY); super(context, KEY_SMART_BATTERY);
@@ -70,19 +66,14 @@ public class SmartBatteryPreferenceController extends BasePreferenceController i
super.updateState(preference); super.updateState(preference);
final boolean smartBatteryOn = Settings.Global.getInt(mContext.getContentResolver(), final boolean smartBatteryOn = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON) == ON; Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON) == ON;
((MainSwitchPreference) preference).updateStatus(smartBatteryOn); ((SwitchPreference) preference).setChecked(smartBatteryOn);
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public boolean onPreferenceChange(Preference preference, Object newValue) {
super.displayPreference(screen); final boolean smartBatteryOn = (Boolean) newValue;
mPreference = (MainSwitchPreference) screen.findPreference(getPreferenceKey());
mPreference.addOnSwitchChangeListener(this);
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, isChecked ? ON : OFF); Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, smartBatteryOn ? ON : OFF);
return true;
} }
} }

View File

@@ -21,9 +21,10 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import androidx.preference.SwitchPreference;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.widget.MainSwitchPreference; import com.android.settingslib.widget.MainSwitchPreference;
@@ -42,50 +43,48 @@ public class SmartBatteryPreferenceControllerTest {
private static final int OFF = 0; private static final int OFF = 0;
private SmartBatteryPreferenceController mController; private SmartBatteryPreferenceController mController;
private SwitchPreference mPreference;
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
private Context mContext;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest(); mFeatureFactory = FakeFeatureFactory.setupForTest();
mContentResolver = RuntimeEnvironment.application.getContentResolver(); mContentResolver = RuntimeEnvironment.application.getContentResolver();
mController = new SmartBatteryPreferenceController(RuntimeEnvironment.application); mController = new SmartBatteryPreferenceController(RuntimeEnvironment.application);
mPreference = new SwitchPreference(RuntimeEnvironment.application);
} }
@Test @Test
public void testUpdateState_smartBatteryOn_preferenceChecked() { public void testUpdateState_smartBatteryOn_preferenceChecked() {
putSmartBatteryValue(ON); putSmartBatteryValue(ON);
final MainSwitchPreference preference = new MainSwitchPreference(mContext);
mController.updateState(preference); mController.updateState(mPreference);
assertThat(preference.isChecked()).isTrue(); assertThat(mPreference.isChecked()).isTrue();
} }
@Test @Test
public void testUpdateState_smartBatteryOff_preferenceUnchecked() { public void testUpdateState_smartBatteryOff_preferenceUnchecked() {
putSmartBatteryValue(OFF); putSmartBatteryValue(OFF);
final MainSwitchPreference preference = new MainSwitchPreference(mContext);
mController.updateState(preference); mController.updateState(mPreference);
assertThat(preference.isChecked()).isFalse(); assertThat(mPreference.isChecked()).isFalse();
} }
@Test @Test
public void testUpdateState_checkPreference_smartBatteryOn() { public void testUpdateState_checkPreference_smartBatteryOn() {
mController.onSwitchChanged(null, true); mController.onPreferenceChange(mPreference, true);
assertThat(getSmartBatteryValue()).isEqualTo(ON); assertThat(getSmartBatteryValue()).isEqualTo(ON);
} }
@Test @Test
public void testUpdateState_unCheckPreference_smartBatteryOff() { public void testUpdateState_unCheckPreference_smartBatteryOff() {
mController.onSwitchChanged(null, false); mController.onPreferenceChange(mPreference, false);
assertThat(getSmartBatteryValue()).isEqualTo(OFF); assertThat(getSmartBatteryValue()).isEqualTo(OFF);
} }