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>
<!-- General category name [CHAR LIMIT=none] -->
<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>

View File

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

View File

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

View File

@@ -20,16 +20,15 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/gesture_prevent_ringing_screen_title">
<com.android.settingslib.widget.MainSwitchPreference
android:key="gesture_prevent_ringing_switch" />
<com.android.settings.widget.VideoPreference
android:key="gesture_prevent_ringing_video"
app:animation="@raw/gesture_prevent_ringing"
app:preview="@drawable/gesture_prevent_ringing"
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
android:key="gesture_prevent_ringing_category"
android:title="@string/gesture_prevent_ringing_title" />

View File

@@ -27,13 +27,12 @@
android:selectable="false"
settings:allowDividerBelow="true" />
<com.android.settingslib.widget.MainSwitchPreference
android:key="zen_automatic_rule_switch" />
<com.android.settingslib.widget.ActionButtonsPreference
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 -->
<DropDownPreference
android:key="calendar"

View File

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

View File

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

View File

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

View File

@@ -25,18 +25,18 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.widget.SwitchBar;
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
AbstractZenModeAutomaticRulePreferenceController implements
SwitchBar.OnSwitchChangeListener {
OnMainSwitchChangeListener {
private static final String KEY = "zen_automatic_rule_switch";
private AutomaticZenRule mRule;
private String mId;
private SwitchBar mSwitchBar;
private MainSwitchPreference mSwitchBar;
public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent,
Lifecycle lifecycle) {
@@ -56,12 +56,11 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
LayoutPreference pref = screen.findPreference(KEY);
mSwitchBar = pref.findViewById(R.id.switch_bar);
final Preference pref = screen.findPreference(KEY);
mSwitchBar = (MainSwitchPreference) pref;
if (mSwitchBar != null) {
mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule,
R.string.zen_mode_use_automatic_rule);
mSwitchBar.setTitle(mContext.getString(R.string.zen_mode_use_automatic_rule));
try {
pref.setOnPreferenceClickListener(preference -> {
mRule.setEnabled(!mRule.isEnabled());
@@ -72,7 +71,6 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
} catch (IllegalStateException e) {
// 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) {
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.PreferenceScreen;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.widget.LayoutPreference;
import com.android.settingslib.widget.MainSwitchPreference;
import org.junit.Before;
import org.junit.Test;
@@ -63,7 +62,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled))
.thenReturn(true);
mController = new PreventRingingSwitchPreferenceController(mContext);
mController.mSwitch = mock(SwitchBar.class);
mController.mSwitch = mock(MainSwitchPreference.class);
}
@Test
@@ -89,7 +88,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
mController.updateState(mPreference);
verify(mController.mSwitch, times(1)).setChecked(false);
verify(mController.mSwitch, times(1)).updateStatus(false);
}
@Test
@@ -99,7 +98,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
mController.updateState(mPreference);
verify(mController.mSwitch, times(1)).setChecked(true);
verify(mController.mSwitch, times(1)).updateStatus(true);
}
@Test
@@ -109,7 +108,7 @@ public class PreventRingingSwitchPreferenceControllerTest {
mController.updateState(mPreference);
verify(mController.mSwitch, times(1)).setChecked(true);
verify(mController.mSwitch, times(1)).updateStatus(true);
}
@Test
@@ -161,13 +160,13 @@ public class PreventRingingSwitchPreferenceControllerTest {
@Test
public void testPreferenceClickListenerAttached() {
PreferenceScreen preferenceScreen = mock(PreferenceScreen.class);
LayoutPreference mLayoutPreference = mock(LayoutPreference.class);
MainSwitchPreference preference = mock(MainSwitchPreference.class);
when(preferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mLayoutPreference);
preference);
mController.displayPreference(preferenceScreen);
verify(mLayoutPreference, times(1))
verify(preference, times(1))
.setOnPreferenceClickListener(any());
}
}