Merge "Extend character limit for accessibility_toggle_primary_mono_summary"
diff --git a/res/layout/emergency_gesture_switch_bar.xml b/res/layout/emergency_gesture_switch_bar.xml
deleted file mode 100644
index 2524601..0000000
--- a/res/layout/emergency_gesture_switch_bar.xml
+++ /dev/null
@@ -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>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index dd515c9..de4e811 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -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>
diff --git a/res/xml/app_notification_settings.xml b/res/xml/app_notification_settings.xml
index b3a1117..877b38b 100644
--- a/res/xml/app_notification_settings.xml
+++ b/res/xml/app_notification_settings.xml
@@ -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" />
diff --git a/res/xml/emergency_gesture_settings.xml b/res/xml/emergency_gesture_settings.xml
index 2e1a259..2f53a90 100644
--- a/res/xml/emergency_gesture_settings.xml
+++ b/res/xml/emergency_gesture_settings.xml
@@ -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"
diff --git a/res/xml/prevent_ringing_gesture_settings.xml b/res/xml/prevent_ringing_gesture_settings.xml
index 67a9c9b..4080375 100644
--- a/res/xml/prevent_ringing_gesture_settings.xml
+++ b/res/xml/prevent_ringing_gesture_settings.xml
@@ -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" />
diff --git a/res/xml/zen_mode_event_rule_settings.xml b/res/xml/zen_mode_event_rule_settings.xml
index 46b95e1..d6a0e68 100644
--- a/res/xml/zen_mode_event_rule_settings.xml
+++ b/res/xml/zen_mode_event_rule_settings.xml
@@ -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"
diff --git a/res/xml/zen_mode_schedule_rule_settings.xml b/res/xml/zen_mode_schedule_rule_settings.xml
index cab8731..874f5c9 100644
--- a/res/xml/zen_mode_schedule_rule_settings.xml
+++ b/res/xml/zen_mode_schedule_rule_settings.xml
@@ -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"
diff --git a/src/com/android/settings/TestingSettingsBroadcastReceiver.java b/src/com/android/settings/TestingSettingsBroadcastReceiver.java
index 0e1296b..30a0d79 100644
--- a/src/com/android/settings/TestingSettingsBroadcastReceiver.java
+++ b/src/com/android/settings/TestingSettingsBroadcastReceiver.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
 package com.android.settings;
 
 import android.content.BroadcastReceiver;
@@ -15,7 +31,8 @@
 
     @Override
     public void onReceive(Context context, Intent intent) {
-        if (intent.getAction().equals(TelephonyManager.ACTION_SECRET_CODE)) {
+        if (intent != null && intent.getAction() != null
+                && intent.getAction().equals(TelephonyManager.ACTION_SECRET_CODE)) {
             Intent i = new Intent(Intent.ACTION_MAIN);
             i.setClass(context, TestingSettingsActivity.class);
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
diff --git a/src/com/android/settings/gestures/EmergencyGesturePreferenceController.java b/src/com/android/settings/gestures/EmergencyGesturePreferenceController.java
index 6f0357f..2415526 100644
--- a/src/com/android/settings/gestures/EmergencyGesturePreferenceController.java
+++ b/src/com/android/settings/gestures/EmergencyGesturePreferenceController.java
@@ -21,18 +21,19 @@
 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 @@
 
     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 @@
     @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
diff --git a/src/com/android/settings/gestures/PreventRingingSwitchPreferenceController.java b/src/com/android/settings/gestures/PreventRingingSwitchPreferenceController.java
index be67b5a..642623a 100644
--- a/src/com/android/settings/gestures/PreventRingingSwitchPreferenceController.java
+++ b/src/com/android/settings/gestures/PreventRingingSwitchPreferenceController.java
@@ -26,18 +26,18 @@
 
 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 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 @@
                                     : 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);
         }
     }
 
diff --git a/src/com/android/settings/notification/zen/ZenAutomaticRuleSwitchPreferenceController.java b/src/com/android/settings/notification/zen/ZenAutomaticRuleSwitchPreferenceController.java
index cafb0d1..703ffae 100644
--- a/src/com/android/settings/notification/zen/ZenAutomaticRuleSwitchPreferenceController.java
+++ b/src/com/android/settings/notification/zen/ZenAutomaticRuleSwitchPreferenceController.java
@@ -25,18 +25,18 @@
 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 @@
     @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 @@
             } catch (IllegalStateException e) {
                 // an exception is thrown if you try to add the listener twice
             }
-            mSwitchBar.show();
         }
     }
 
@@ -83,7 +81,7 @@
 
     public void updateState(Preference preference) {
         if (mRule != null) {
-            mSwitchBar.setChecked(mRule.isEnabled());
+            mSwitchBar.updateStatus(mRule.isEnabled());
         }
     }
 
diff --git a/tests/robotests/src/com/android/settings/TestingSettingsBroadcastReceiverTest.java b/tests/robotests/src/com/android/settings/TestingSettingsBroadcastReceiverTest.java
new file mode 100644
index 0000000..10fdffb
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/TestingSettingsBroadcastReceiverTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+package com.android.settings;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.app.Application;
+import android.content.Context;
+import android.content.Intent;
+import android.telephony.TelephonyManager;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.Shadows;
+
+@RunWith(RobolectricTestRunner.class)
+public class TestingSettingsBroadcastReceiverTest {
+
+    private Context mContext;
+    private Application mApplication;
+    private TestingSettingsBroadcastReceiver mReceiver;
+
+    @Before
+    public void setUp() {
+        mContext = RuntimeEnvironment.application;
+        mApplication = RuntimeEnvironment.application;
+        mReceiver = new TestingSettingsBroadcastReceiver();
+    }
+
+    @Test
+    public void onReceive_nullIntent_shouldNotCrash() {
+        final Intent intent = new Intent();
+
+        mReceiver.onReceive(mContext, null);
+        mReceiver.onReceive(mContext, intent);
+
+        final Intent next = Shadows.shadowOf(mApplication).getNextStartedActivity();
+        assertThat(next).isNull();
+    }
+
+    @Test
+    public void onReceive_wrongIntent_shouldNotStartActivity() {
+        final Intent intent = new Intent();
+        intent.setAction("");
+
+        mReceiver.onReceive(mContext, intent);
+
+        final Intent next = Shadows.shadowOf(mApplication).getNextStartedActivity();
+        assertThat(next).isNull();
+    }
+
+    @Test
+    public void onReceive_correctIntent_shouldStartActivity() {
+        final Intent intent = new Intent();
+        intent.setAction(TelephonyManager.ACTION_SECRET_CODE);
+
+        mReceiver.onReceive(mContext, intent);
+
+        final Intent next = Shadows.shadowOf(mApplication).getNextStartedActivity();
+        assertThat(next).isNotNull();
+        final String dest = next.getComponent().getClassName();
+        assertThat(dest).isEqualTo(Settings.TestingSettingsActivity.class.getName());
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/gestures/PreventRingingSwitchPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/PreventRingingSwitchPreferenceControllerTest.java
index ccb2bf6..5f25759 100644
--- a/tests/robotests/src/com/android/settings/gestures/PreventRingingSwitchPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/gestures/PreventRingingSwitchPreferenceControllerTest.java
@@ -36,8 +36,7 @@
 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 @@
         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 @@
 
         mController.updateState(mPreference);
 
-        verify(mController.mSwitch, times(1)).setChecked(false);
+        verify(mController.mSwitch, times(1)).updateStatus(false);
     }
 
     @Test
@@ -99,7 +98,7 @@
 
         mController.updateState(mPreference);
 
-        verify(mController.mSwitch, times(1)).setChecked(true);
+        verify(mController.mSwitch, times(1)).updateStatus(true);
     }
 
     @Test
@@ -109,7 +108,7 @@
 
         mController.updateState(mPreference);
 
-        verify(mController.mSwitch, times(1)).setChecked(true);
+        verify(mController.mSwitch, times(1)).updateStatus(true);
     }
 
     @Test
@@ -161,13 +160,13 @@
     @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());
     }
 }