Use SettingsLib's MainSwitchPreference to replace the Switches which use

LayoutPreference.

Bug: 175181773
Test: Run robotest and apply the widget in Settings and see the ui
Change-Id: I4588d1fc3004970b174e7c5c5d1aa7745f774985
This commit is contained in:
Stanley Wang
2021-01-05 16:09:37 +08:00
parent adcd4f165d
commit 57098f4589
11 changed files with 57 additions and 99 deletions

View File

@@ -1,39 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2020 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!-- For use in a LayoutPreference -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:importantForAccessibility="no">
<com.android.settings.widget.SwitchBar
android:id="@+id/switch_bar"
android:minHeight="?android:attr/actionBarSize"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingStart="0dp"
android:theme="?attr/switchBarTheme"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/actionbar_subsettings_contentInsetStart"
android:layout_marginVertical="16dp"
android:text="@string/emergency_gesture_screen_summary"/>
</LinearLayout>

View File

@@ -12581,4 +12581,8 @@
<string name="category_name_others">Others</string> <string name="category_name_others">Others</string>
<!-- General category name [CHAR LIMIT=none] --> <!-- General category name [CHAR LIMIT=none] -->
<string name="category_name_general">General</string> <string name="category_name_general">General</string>
<!-- Do not translate. Title for prevent ringing main switch. [CHAR LIMIT=50] -->
<string name="prevent_ringing_main_switch_title" translatable="false">Use prevent ringing</string>
</resources> </resources>

View File

@@ -22,9 +22,8 @@
android:key="pref_app_header" android:key="pref_app_header"
android:layout="@layout/settings_entity_header" /> android:layout="@layout/settings_entity_header" />
<com.android.settingslib.widget.LayoutPreference <com.android.settingslib.widget.MainSwitchPreference
android:key="block" android:key="block" />
android:layout="@layout/styled_switch_bar" />
<com.android.settings.notification.app.NotificationFooterPreference <com.android.settings.notification.app.NotificationFooterPreference
android:key="block_desc" /> android:key="block_desc" />

View File

@@ -20,16 +20,18 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/emergency_gesture_screen_title"> android:title="@string/emergency_gesture_screen_title">
<com.android.settingslib.widget.MainSwitchPreference
android:key="gesture_emergency_button_switch_bar"
app:controller="com.android.settings.gestures.EmergencyGesturePreferenceController" />
<com.android.settings.widget.VideoPreference <com.android.settings.widget.VideoPreference
android:key="emergency_gesture_screen_video" android:key="emergency_gesture_screen_video"
app:animation="@raw/gesture_double_tap" app:animation="@raw/gesture_double_tap"
app:preview="@drawable/gesture_double_tap"/> app:preview="@drawable/gesture_double_tap"/>
<com.android.settingslib.widget.LayoutPreference <com.android.settingslib.widget.TopIntroPreference
android:key="gesture_emergency_button_switch_bar" android:title="@string/emergency_gesture_screen_summary"
android:layout="@layout/emergency_gesture_switch_bar" app:allowDividerBelow="true" />
app:controller="com.android.settings.gestures.EmergencyGesturePreferenceController"
app:allowDividerBelow="true"/>
<SwitchPreference <SwitchPreference
android:key="emergency_gesture_sound" android:key="emergency_gesture_sound"

View File

@@ -20,16 +20,15 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/gesture_prevent_ringing_screen_title"> android:title="@string/gesture_prevent_ringing_screen_title">
<com.android.settingslib.widget.MainSwitchPreference
android:key="gesture_prevent_ringing_switch" />
<com.android.settings.widget.VideoPreference <com.android.settings.widget.VideoPreference
android:key="gesture_prevent_ringing_video" android:key="gesture_prevent_ringing_video"
app:animation="@raw/gesture_prevent_ringing" app:animation="@raw/gesture_prevent_ringing"
app:preview="@drawable/gesture_prevent_ringing" app:preview="@drawable/gesture_prevent_ringing"
app:controller="com.android.settings.widget.VideoPreferenceController" /> app:controller="com.android.settings.widget.VideoPreferenceController" />
<com.android.settingslib.widget.LayoutPreference
android:key="gesture_prevent_ringing_switch"
android:layout="@layout/styled_switch_bar" />
<PreferenceCategory <PreferenceCategory
android:key="gesture_prevent_ringing_category" android:key="gesture_prevent_ringing_category"
android:title="@string/gesture_prevent_ringing_title" /> android:title="@string/gesture_prevent_ringing_title" />

View File

@@ -27,13 +27,12 @@
android:selectable="false" android:selectable="false"
settings:allowDividerBelow="true" /> settings:allowDividerBelow="true" />
<com.android.settingslib.widget.MainSwitchPreference
android:key="zen_automatic_rule_switch" />
<com.android.settingslib.widget.ActionButtonsPreference <com.android.settingslib.widget.ActionButtonsPreference
android:key="zen_action_buttons" /> android:key="zen_action_buttons" />
<com.android.settingslib.widget.LayoutPreference
android:key="zen_automatic_rule_switch"
android:layout="@layout/styled_switch_bar" />
<!-- During events for --> <!-- During events for -->
<DropDownPreference <DropDownPreference
android:key="calendar" android:key="calendar"

View File

@@ -27,13 +27,12 @@
android:selectable="false" android:selectable="false"
settings:allowDividerBelow="true"/> settings:allowDividerBelow="true"/>
<com.android.settingslib.widget.MainSwitchPreference
android:key="zen_automatic_rule_switch" />
<com.android.settingslib.widget.ActionButtonsPreference <com.android.settingslib.widget.ActionButtonsPreference
android:key="zen_action_buttons"/> android:key="zen_action_buttons"/>
<com.android.settingslib.widget.LayoutPreference
android:key="zen_automatic_rule_switch"
android:layout="@layout/styled_switch_bar" />
<!-- Days --> <!-- Days -->
<Preference <Preference
android:key="days" android:key="days"

View File

@@ -21,18 +21,19 @@ import android.provider.Settings;
import android.widget.Switch; import android.widget.Switch;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.SwitchBar; import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.OnMainSwitchChangeListener;
/** /**
* Preference controller for emergency gesture setting * Preference controller for emergency gesture setting
*/ */
public class EmergencyGesturePreferenceController extends BasePreferenceController implements public class EmergencyGesturePreferenceController extends BasePreferenceController implements
SwitchBar.OnSwitchChangeListener { OnMainSwitchChangeListener {
@VisibleForTesting @VisibleForTesting
static final int ON = 1; static final int ON = 1;
@@ -41,7 +42,7 @@ public class EmergencyGesturePreferenceController extends BasePreferenceControll
private static final String SECURE_KEY = Settings.Secure.EMERGENCY_GESTURE_ENABLED; private static final String SECURE_KEY = Settings.Secure.EMERGENCY_GESTURE_ENABLED;
private SwitchBar mSwitchBar; private MainSwitchPreference mSwitchBar;
public EmergencyGesturePreferenceController(Context context, String key) { public EmergencyGesturePreferenceController(Context context, String key) {
super(context, key); super(context, key);
@@ -61,13 +62,11 @@ public class EmergencyGesturePreferenceController extends BasePreferenceControll
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
final LayoutPreference pref = screen.findPreference(mPreferenceKey); final Preference pref = screen.findPreference(mPreferenceKey);
mSwitchBar = pref.findViewById(R.id.switch_bar); mSwitchBar = (MainSwitchPreference) pref;
mSwitchBar.setSwitchBarText(R.string.emergency_gesture_switchbar_title, mSwitchBar.setTitle(mContext.getString(R.string.emergency_gesture_switchbar_title));
R.string.emergency_gesture_switchbar_title);
mSwitchBar.addOnSwitchChangeListener(this); mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.setChecked(isChecked()); mSwitchBar.updateStatus(isChecked());
mSwitchBar.show();
} }
@VisibleForTesting @VisibleForTesting

View File

@@ -26,18 +26,18 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
public class PreventRingingSwitchPreferenceController extends AbstractPreferenceController public class PreventRingingSwitchPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, SwitchBar.OnSwitchChangeListener { implements PreferenceControllerMixin, OnMainSwitchChangeListener {
private static final String KEY = "gesture_prevent_ringing_switch"; private static final String KEY = "gesture_prevent_ringing_switch";
private final Context mContext; private final Context mContext;
@VisibleForTesting @VisibleForTesting
SwitchBar mSwitch; MainSwitchPreference mSwitch;
public PreventRingingSwitchPreferenceController(Context context) { public PreventRingingSwitchPreferenceController(Context context) {
super(context); super(context);
@@ -53,7 +53,7 @@ public class PreventRingingSwitchPreferenceController extends AbstractPreference
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
if (isAvailable()) { if (isAvailable()) {
LayoutPreference pref = screen.findPreference(getPreferenceKey()); Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) { if (pref != null) {
pref.setOnPreferenceClickListener(preference -> { pref.setOnPreferenceClickListener(preference -> {
int preventRinging = Settings.Secure.getInt(mContext.getContentResolver(), int preventRinging = Settings.Secure.getInt(mContext.getContentResolver(),
@@ -66,18 +66,17 @@ public class PreventRingingSwitchPreferenceController extends AbstractPreference
: Settings.Secure.VOLUME_HUSH_VIBRATE); : Settings.Secure.VOLUME_HUSH_VIBRATE);
return true; return true;
}); });
mSwitch = pref.findViewById(R.id.switch_bar); mSwitch = (MainSwitchPreference) pref;
if (mSwitch != null) { mSwitch.setTitle(mContext.getString(R.string.prevent_ringing_main_switch_title));
mSwitch.addOnSwitchChangeListener(this); mSwitch.addOnSwitchChangeListener(this);
mSwitch.show(); updateState(mSwitch);
}
} }
} }
} }
public void setChecked(boolean isChecked) { public void setChecked(boolean isChecked) {
if (mSwitch != null) { if (mSwitch != null) {
mSwitch.setChecked(isChecked); mSwitch.updateStatus(isChecked);
} }
} }

View File

@@ -25,18 +25,18 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
public class ZenAutomaticRuleSwitchPreferenceController extends public class ZenAutomaticRuleSwitchPreferenceController extends
AbstractZenModeAutomaticRulePreferenceController implements AbstractZenModeAutomaticRulePreferenceController implements
SwitchBar.OnSwitchChangeListener { OnMainSwitchChangeListener {
private static final String KEY = "zen_automatic_rule_switch"; private static final String KEY = "zen_automatic_rule_switch";
private AutomaticZenRule mRule; private AutomaticZenRule mRule;
private String mId; private String mId;
private SwitchBar mSwitchBar; private MainSwitchPreference mSwitchBar;
public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent, public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent,
Lifecycle lifecycle) { Lifecycle lifecycle) {
@@ -56,12 +56,11 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
LayoutPreference pref = screen.findPreference(KEY); final Preference pref = screen.findPreference(KEY);
mSwitchBar = pref.findViewById(R.id.switch_bar); mSwitchBar = (MainSwitchPreference) pref;
if (mSwitchBar != null) { if (mSwitchBar != null) {
mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule, mSwitchBar.setTitle(mContext.getString(R.string.zen_mode_use_automatic_rule));
R.string.zen_mode_use_automatic_rule);
try { try {
pref.setOnPreferenceClickListener(preference -> { pref.setOnPreferenceClickListener(preference -> {
mRule.setEnabled(!mRule.isEnabled()); mRule.setEnabled(!mRule.isEnabled());
@@ -72,7 +71,6 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// an exception is thrown if you try to add the listener twice // an exception is thrown if you try to add the listener twice
} }
mSwitchBar.show();
} }
} }
@@ -83,7 +81,7 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
public void updateState(Preference preference) { public void updateState(Preference preference) {
if (mRule != null) { if (mRule != null) {
mSwitchBar.setChecked(mRule.isEnabled()); mSwitchBar.updateStatus(mRule.isEnabled());
} }
} }

View File

@@ -36,8 +36,7 @@ import android.provider.Settings;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.widget.SwitchBar; import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -63,7 +62,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled)) when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled))
.thenReturn(true); .thenReturn(true);
mController = new PreventRingingSwitchPreferenceController(mContext); mController = new PreventRingingSwitchPreferenceController(mContext);
mController.mSwitch = mock(SwitchBar.class); mController.mSwitch = mock(MainSwitchPreference.class);
} }
@Test @Test
@@ -89,7 +88,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
verify(mController.mSwitch, times(1)).setChecked(false); verify(mController.mSwitch, times(1)).updateStatus(false);
} }
@Test @Test
@@ -99,7 +98,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
verify(mController.mSwitch, times(1)).setChecked(true); verify(mController.mSwitch, times(1)).updateStatus(true);
} }
@Test @Test
@@ -109,7 +108,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
verify(mController.mSwitch, times(1)).setChecked(true); verify(mController.mSwitch, times(1)).updateStatus(true);
} }
@Test @Test
@@ -161,13 +160,13 @@ public class PreventRingingSwitchPreferenceControllerTest {
@Test @Test
public void testPreferenceClickListenerAttached() { public void testPreferenceClickListenerAttached() {
PreferenceScreen preferenceScreen = mock(PreferenceScreen.class); PreferenceScreen preferenceScreen = mock(PreferenceScreen.class);
LayoutPreference mLayoutPreference = mock(LayoutPreference.class); MainSwitchPreference preference = mock(MainSwitchPreference.class);
when(preferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( when(preferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mLayoutPreference); preference);
mController.displayPreference(preferenceScreen); mController.displayPreference(preferenceScreen);
verify(mLayoutPreference, times(1)) verify(preference, times(1))
.setOnPreferenceClickListener(any()); .setOnPreferenceClickListener(any());
} }
} }