Accessibility Service & Shortcut Redesign - Primary action - toggle button design

Implements DividerSwitchPreference to replace ToggleSwitch

Bug: 142528112
Test: Manual test

Change-Id: I0d80d16bfa941f7ee333a2c5680a25a26c42809d
diff --git a/res/xml/accessibility_daltonizer_settings.xml b/res/xml/accessibility_daltonizer_settings.xml
index 0d9911c..b2ab5ef 100644
--- a/res/xml/accessibility_daltonizer_settings.xml
+++ b/res/xml/accessibility_daltonizer_settings.xml
@@ -53,7 +53,7 @@
     </PreferenceCategory>
 
     <PreferenceCategory
-        android:key="daltonizer_footer_category" >
+        android:key="daltonizer_footer_category">
 
         <com.android.settingslib.widget.FooterPreference
             android:key="daltonizer_footer"
diff --git a/res/xml/accessibility_shortcut_settings.xml b/res/xml/accessibility_shortcut_settings.xml
index f431dec..0bfd6b6 100644
--- a/res/xml/accessibility_shortcut_settings.xml
+++ b/res/xml/accessibility_shortcut_settings.xml
@@ -14,21 +14,22 @@
      limitations under the License.
 -->
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-                  xmlns:settings="http://schemas.android.com/apk/res-auto"
-                  android:title="@string/accessibility_global_gesture_preference_title" >
+    xmlns:settings="http://schemas.android.com/apk/res-auto"
+    android:title="@string/accessibility_global_gesture_preference_title">
 
     <Preference
-            android:key="accessibility_shortcut_service"
-            android:title="@string/accessibility_shortcut_service_title"
-            android:fragment="com.android.settings.accessibility.ShortcutServicePickerFragment"/>
+        android:fragment="com.android.settings.accessibility.ShortcutServicePickerFragment"
+        android:key="accessibility_shortcut_service"
+        android:title="@string/accessibility_shortcut_service_title" />
 
     <SwitchPreference
-            android:key="accessibility_shortcut_on_lock_screen"
-            android:title="@string/accessibility_shortcut_service_on_lock_screen_title"/>
+        android:key="accessibility_shortcut_on_lock_screen"
+        android:title="@string/accessibility_shortcut_service_on_lock_screen_title" />
 
     <com.android.settingslib.widget.FooterPreference
         android:key="accessibility_shortcut_footer"
-        android:title="@string/accessibility_shortcut_description"
         android:selectable="false"
-        settings:searchable="false"/>
-</PreferenceScreen>
\ No newline at end of file
+        android:title="@string/accessibility_shortcut_description"
+        settings:searchable="false" />
+
+</PreferenceScreen>
diff --git a/src/com/android/settings/accessibility/AccessibilityShortcutPreferenceFragment.java b/src/com/android/settings/accessibility/AccessibilityShortcutPreferenceFragment.java
index 55edf50..659eaca 100644
--- a/src/com/android/settings/accessibility/AccessibilityShortcutPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/AccessibilityShortcutPreferenceFragment.java
@@ -15,6 +15,9 @@
  */
 package com.android.settings.accessibility;
 
+import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
+import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
+
 import android.accessibilityservice.AccessibilityServiceInfo;
 import android.annotation.Nullable;
 import android.app.settings.SettingsEnums;
@@ -28,15 +31,14 @@
 import android.provider.Settings;
 import android.view.View;
 import android.view.accessibility.AccessibilityManager;
-import android.widget.Switch;
 
 import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
 import androidx.preference.SwitchPreference;
 
 import com.android.internal.accessibility.AccessibilityShortcutController;
 import com.android.settings.R;
 import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settings.widget.SwitchBar;
 import com.android.settingslib.accessibility.AccessibilityUtils;
 import com.android.settingslib.search.Indexable;
 import com.android.settingslib.search.SearchIndexable;
@@ -79,7 +81,7 @@
         mOnLockScreenSwitchPreference.setOnPreferenceChangeListener((Preference p, Object o) -> {
             Settings.Secure.putInt(getContentResolver(),
                     Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN,
-                    ((Boolean) o) ? 1 : 0);
+                    ((Boolean) o) ? ON : OFF);
             return true;
         });
     }
@@ -87,7 +89,12 @@
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
-        mShortcutPreference.setVisible(false);
+
+        final PreferenceScreen preferenceScreen = getPreferenceScreen();
+        preferenceScreen.findPreference(KEY_GENERAL_CATEGORY).setVisible(false);
+
+        preferenceScreen.setOrderingAsAdded(false);
+        mToggleServiceDividerSwitchPreference.setOrder(mServicePreference.getOrder() - 1);
     }
 
     @Override
@@ -111,34 +118,42 @@
     }
 
     @Override
-    protected void onInstallSwitchBarToggleSwitch() {
-        super.onInstallSwitchBarToggleSwitch();
-        mSwitchBar.addOnSwitchChangeListener((Switch switchView, boolean enabled) -> {
+    protected void onRemoveSwitchPreferenceToggleSwitch() {
+        super.onRemoveSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null);
+    }
+
+    @Override
+    protected void onInstallSwitchPreferenceToggleSwitch() {
+        super.onInstallSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener((preference) -> {
+            boolean enabled = ((SwitchPreference) preference).isChecked();
             Context context = getContext();
             if (enabled && !shortcutFeatureAvailable(context)) {
-                // If no service is configured, we'll disable the shortcut shortly. Give the
-                // user a chance to select a service. We'll update the preferences when we resume.
-                Settings.Secure.putInt(
-                        getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 1);
+                // If no service is configured, we'll disable the shortcut shortly. Give the user
+                // a chance to select a service. We'll update the preferences when we resume.
+                Settings.Secure.putInt(getContentResolver(),
+                        Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON);
                 mServicePreference.setEnabled(true);
                 mServicePreference.performClick();
             } else {
                 onPreferenceToggled(Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, enabled);
             }
+            return true;
         });
     }
 
     @Override
     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
-        Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? 1 : 0);
+        Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? ON : OFF);
         updatePreferences();
     }
 
     @Override
-    protected void updateSwitchBarText(SwitchBar switchBar) {
+    protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
         final String switchBarText = getString(R.string.accessibility_service_master_switch_title,
                 getString(R.string.accessibility_global_gesture_preference_title));
-        switchBar.setSwitchBarText(switchBarText, switchBarText);
+        switchPreference.setTitle(switchBarText);
     }
 
     private void updatePreferences() {
@@ -148,21 +163,21 @@
         if (!shortcutFeatureAvailable(context)) {
             // If no service is configured, make sure the overall shortcut is turned off
             Settings.Secure.putInt(
-                    getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 0);
+                    getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, OFF);
         }
         boolean isEnabled = Settings.Secure
-                .getInt(cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 1) == 1;
-        mSwitchBar.setChecked(isEnabled);
+                .getInt(cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON) == ON;
+        mToggleServiceDividerSwitchPreference.setChecked(isEnabled);
         // The shortcut is enabled by default on the lock screen as long as the user has
         // enabled the shortcut with the warning dialog
         final int dialogShown = Settings.Secure.getInt(
-                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0);
+                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, OFF);
         final boolean enabledFromLockScreen = Settings.Secure.getInt(
-                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, dialogShown) == 1;
+                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, dialogShown) == ON;
         mOnLockScreenSwitchPreference.setChecked(enabledFromLockScreen);
         // Only enable changing the service and lock screen behavior if the shortcut is on
-        mServicePreference.setEnabled(mToggleSwitch.isChecked());
-        mOnLockScreenSwitchPreference.setEnabled(mToggleSwitch.isChecked());
+        mServicePreference.setEnabled(mToggleServiceDividerSwitchPreference.isChecked());
+        mOnLockScreenSwitchPreference.setEnabled(mToggleServiceDividerSwitchPreference.isChecked());
     }
 
     /**
diff --git a/src/com/android/settings/accessibility/DividerSwitchPreference.java b/src/com/android/settings/accessibility/DividerSwitchPreference.java
new file mode 100644
index 0000000..34b0429
--- /dev/null
+++ b/src/com/android/settings/accessibility/DividerSwitchPreference.java
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+
+package com.android.settings.accessibility;
+
+import android.content.Context;
+
+import androidx.preference.PreferenceViewHolder;
+import androidx.preference.SwitchPreference;
+
+/**
+ * A switch preference that has a divider below and above. Used for Accessibility Settings use
+ * service.
+ */
+public final class DividerSwitchPreference extends SwitchPreference {
+
+    private Boolean mDividerAllowedAbove;
+    private Boolean mDividerAllowBelow;
+
+    public DividerSwitchPreference(Context context) {
+        super(context);
+        mDividerAllowedAbove = true;
+        mDividerAllowBelow = true;
+    }
+
+    /**
+     * Sets divider whether to show in preference above.
+     *
+     * @param allowed true will be drawn on above this item
+     */
+    public void setDividerAllowedAbove(boolean allowed) {
+        if (mDividerAllowedAbove != allowed) {
+            mDividerAllowedAbove = allowed;
+            notifyChanged();
+        }
+    }
+
+    /**
+     * Sets divider whether to show in preference below.
+     *
+     * @param allowed true will be drawn on below this item
+     */
+    public void setDividerAllowedBelow(boolean allowed) {
+        if (mDividerAllowedAbove != allowed) {
+            mDividerAllowBelow = allowed;
+            notifyChanged();
+        }
+    }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+        holder.setDividerAllowedAbove(mDividerAllowedAbove);
+        holder.setDividerAllowedBelow(mDividerAllowBelow);
+    }
+}
diff --git a/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java
index a48b71f..93064c5 100644
--- a/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java
@@ -17,29 +17,27 @@
 package com.android.settings.accessibility;
 
 import android.accessibilityservice.AccessibilityServiceInfo;
-import android.os.Bundle;
 import android.view.View;
 
 import com.android.settings.R;
-import com.android.settings.SettingsActivity;
-import com.android.settings.widget.SwitchBar;
 import com.android.settingslib.accessibility.AccessibilityUtils;
 
 /**
- * For accessibility services that target SDK > Q, and
- * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON}
- * is set.
+ * Fragment that does not have toggle bar to turn on service to use.
+ *
+ * <p>The child {@link ToggleAccessibilityServicePreferenceFragment} shows the actual UI for
+ * providing basic accessibility service setup.
+ *
+ * <p>For accessibility services that target SDK > Q, and
+ * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
  */
 public class InvisibleToggleAccessibilityServicePreferenceFragment extends
         ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickListener{
 
     @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-
-        final SettingsActivity activity = (SettingsActivity) getActivity();
-        final SwitchBar mSwitchBar = activity.getSwitchBar();
-        mSwitchBar.hide();
+    protected void onInstallSwitchPreferenceToggleSwitch() {
+        super.onInstallSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setVisible(false);
     }
 
     /**
diff --git a/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java
index 0a65709..2739b6b 100644
--- a/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java
@@ -25,7 +25,14 @@
 
 import com.google.common.collect.ImmutableSet;
 
-/** For accessibility services that target SDK <= Q. */
+/**
+ * Fragment that only allowed hardware {@link UserShortcutType} for shortcut to open.
+ *
+ * <p>The child {@link ToggleAccessibilityServicePreferenceFragment} shows the actual UI for
+ * providing basic accessibility service setup.
+ *
+ * <p>For accessibility services that target SDK <= Q.
+ */
 public class LegacyAccessibilityServicePreferenceFragment extends
         ToggleAccessibilityServicePreferenceFragment {
 
@@ -38,7 +45,6 @@
         mShortcutPreference.setSummary(hardwareTitle);
         mShortcutPreference.setSettingsVisibility(View.GONE);
 
-        // Only allowed hardware PreferredShortcutType in this fragment.
         setAllowedPreferredShortcutType(UserShortcutType.HARDWARE);
     }
 
diff --git a/src/com/android/settings/accessibility/LegacyToggleScreenReaderPreferenceFragmentForSetupWizard.java b/src/com/android/settings/accessibility/LegacyToggleScreenReaderPreferenceFragmentForSetupWizard.java
index c9257a9..82d80e0 100644
--- a/src/com/android/settings/accessibility/LegacyToggleScreenReaderPreferenceFragmentForSetupWizard.java
+++ b/src/com/android/settings/accessibility/LegacyToggleScreenReaderPreferenceFragmentForSetupWizard.java
@@ -28,7 +28,7 @@
     @Override
     protected void onProcessArguments(Bundle arguments) {
         super.onProcessArguments(arguments);
-        mToggleSwitchWasInitiallyChecked = mToggleSwitch.isChecked();
+        mToggleSwitchWasInitiallyChecked = mToggleServiceDividerSwitchPreference.isChecked();
     }
 
     @Override
@@ -39,10 +39,10 @@
     @Override
     public void onStop() {
         // Log the final choice in value if it's different from the previous value.
-        if (mToggleSwitch.isChecked() != mToggleSwitchWasInitiallyChecked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() != mToggleSwitchWasInitiallyChecked) {
             mMetricsFeatureProvider.action(getContext(),
                     SettingsEnums.SUW_ACCESSIBILITY_TOGGLE_SCREEN_READER,
-                    mToggleSwitch.isChecked());
+                    mToggleServiceDividerSwitchPreference.isChecked());
         }
 
         super.onStop();
diff --git a/src/com/android/settings/accessibility/LegacyToggleSelectToSpeakPreferenceFragmentForSetupWizard.java b/src/com/android/settings/accessibility/LegacyToggleSelectToSpeakPreferenceFragmentForSetupWizard.java
index 2c8485b..9953faf 100644
--- a/src/com/android/settings/accessibility/LegacyToggleSelectToSpeakPreferenceFragmentForSetupWizard.java
+++ b/src/com/android/settings/accessibility/LegacyToggleSelectToSpeakPreferenceFragmentForSetupWizard.java
@@ -28,7 +28,7 @@
     @Override
     protected void onProcessArguments(Bundle arguments) {
         super.onProcessArguments(arguments);
-        mToggleSwitchWasInitiallyChecked = mToggleSwitch.isChecked();
+        mToggleSwitchWasInitiallyChecked = mToggleServiceDividerSwitchPreference.isChecked();
     }
 
     @Override
@@ -39,10 +39,10 @@
     @Override
     public void onStop() {
         // Log the final choice in value if it's different from the previous value.
-        if (mToggleSwitch.isChecked() != mToggleSwitchWasInitiallyChecked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() != mToggleSwitchWasInitiallyChecked) {
             mMetricsFeatureProvider.action(getContext(),
                     SettingsEnums.SUW_ACCESSIBILITY_TOGGLE_SELECT_TO_SPEAK,
-                    mToggleSwitch.isChecked());
+                    mToggleServiceDividerSwitchPreference.isChecked());
         }
 
         super.onStop();
diff --git a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
index 6d64a23..a75c536 100644
--- a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
@@ -39,12 +39,13 @@
 import android.view.View;
 import android.view.accessibility.AccessibilityManager;
 
+import androidx.preference.Preference;
+import androidx.preference.SwitchPreference;
+
 import com.android.internal.widget.LockPatternUtils;
 import com.android.settings.R;
 import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
 import com.android.settings.password.ConfirmDeviceCredentialActivity;
-import com.android.settings.widget.SwitchBar;
-import com.android.settings.widget.ToggleSwitch;
 import com.android.settingslib.accessibility.AccessibilityUtils;
 
 import java.util.List;
@@ -186,21 +187,21 @@
     }
 
     @Override
-    protected void updateSwitchBarText(SwitchBar switchBar) {
+    protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
         final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
         final String switchBarText = (info == null) ? "" :
                 getString(R.string.accessibility_service_master_switch_title,
                 info.getResolveInfo().loadLabel(getPackageManager()));
-        switchBar.setSwitchBarText(switchBarText, switchBarText);
+        switchPreference.setTitle(switchBarText);
     }
 
     private void updateSwitchBarToggleSwitch() {
         final boolean checked = AccessibilityUtils.getEnabledServicesFromSettings(getPrefContext())
                 .contains(mComponentName);
-        if (mSwitchBar.isChecked() == checked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() == checked) {
             return;
         }
-        mSwitchBar.setCheckedInternal(checked);
+        mToggleServiceDividerSwitchPreference.setChecked(checked);
     }
 
     /**
@@ -251,7 +252,7 @@
     }
 
     private void handleConfirmServiceEnabled(boolean confirmed) {
-        mSwitchBar.setCheckedInternal(confirmed);
+        mToggleServiceDividerSwitchPreference.setChecked(confirmed);
         getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, confirmed);
         onPreferenceToggled(mPreferenceKey, confirmed);
     }
@@ -274,9 +275,9 @@
     }
 
     @Override
-    protected void onInstallSwitchBarToggleSwitch() {
-        super.onInstallSwitchBarToggleSwitch();
-        mToggleSwitch.setOnBeforeCheckedChangeListener(this::onBeforeCheckedChanged);
+    protected void onInstallSwitchPreferenceToggleSwitch() {
+        super.onInstallSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(this::onPreferenceClick);
     }
 
     @Override
@@ -410,9 +411,10 @@
         mDialog.dismiss();
     }
 
-    private boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
+    private boolean onPreferenceClick(Preference preference) {
+        boolean checked = ((DividerSwitchPreference) preference).isChecked();
         if (checked) {
-            mSwitchBar.setCheckedInternal(false);
+            mToggleServiceDividerSwitchPreference.setChecked(false);
             getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED,
                     /* disableService */ false);
             if (!mShortcutPreference.getChecked()) {
@@ -424,7 +426,7 @@
                 }
             }
         } else {
-            mSwitchBar.setCheckedInternal(true);
+            mToggleServiceDividerSwitchPreference.setChecked(true);
             getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED,
                     /* enableService */ true);
             showDialog(DialogEnums.DISABLE_WARNING_FROM_TOGGLE);
diff --git a/src/com/android/settings/accessibility/ToggleColorInversionPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleColorInversionPreferenceFragment.java
index d25be83..ffde1e3 100644
--- a/src/com/android/settings/accessibility/ToggleColorInversionPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleColorInversionPreferenceFragment.java
@@ -31,10 +31,10 @@
 
 import androidx.preference.PreferenceCategory;
 import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
 
 import com.android.settings.R;
 import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settings.widget.SwitchBar;
 import com.android.settingslib.search.SearchIndexable;
 
 import java.util.ArrayList;
@@ -66,22 +66,22 @@
     }
 
     @Override
-    protected void onRemoveSwitchBarToggleSwitch() {
-        super.onRemoveSwitchBarToggleSwitch();
-        mToggleSwitch.setOnBeforeCheckedChangeListener(null);
+    protected void onRemoveSwitchPreferenceToggleSwitch() {
+        super.onRemoveSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null);
     }
 
     @Override
-    protected void updateSwitchBarText(SwitchBar switchBar) {
-        switchBar.setSwitchBarText(R.string.accessibility_display_inversion_switch_title,
-                R.string.accessibility_display_inversion_switch_title);
+    protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
+        switchPreference.setTitle(R.string.accessibility_display_inversion_switch_title);
     }
 
     @Override
-    protected void onInstallSwitchBarToggleSwitch() {
-        super.onInstallSwitchBarToggleSwitch();
+    protected void onInstallSwitchPreferenceToggleSwitch() {
+        super.onInstallSwitchPreferenceToggleSwitch();
         updateSwitchBarToggleSwitch();
-        mToggleSwitch.setOnBeforeCheckedChangeListener((toggleSwitch, checked) -> {
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener((preference) -> {
+            boolean checked = ((SwitchPreference) preference).isChecked();
             onPreferenceToggled(mPreferenceKey, checked);
             return false;
         });
@@ -135,10 +135,10 @@
 
     private void updateSwitchBarToggleSwitch() {
         final boolean checked = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
-        if (mSwitchBar.isChecked() == checked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() == checked) {
             return;
         }
-        mSwitchBar.setCheckedInternal(checked);
+        mToggleServiceDividerSwitchPreference.setChecked(checked);
     }
 
     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
diff --git a/src/com/android/settings/accessibility/ToggleDaltonizerPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleDaltonizerPreferenceFragment.java
index 9a6aefd..f2b84e5 100644
--- a/src/com/android/settings/accessibility/ToggleDaltonizerPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleDaltonizerPreferenceFragment.java
@@ -34,10 +34,10 @@
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
 import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
 
 import com.android.settings.R;
 import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settings.widget.SwitchBar;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
 import com.android.settingslib.search.SearchIndexable;
@@ -51,6 +51,7 @@
 
     private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED;
     private static final String CATEGORY_FOOTER_KEY = "daltonizer_footer_category";
+    private static final String CATEGORY_MODE_KEY = "daltonizer_mode_category";
     private static final List<AbstractPreferenceController> sControllers = new ArrayList<>();
     private final Handler mHandler = new Handler();
     private SettingsContentObserver mSettingsContentObserver;
@@ -98,6 +99,11 @@
         super.onViewCreated(view, savedInstanceState);
         final PreferenceScreen preferenceScreen = getPreferenceScreen();
         preferenceScreen.setOrderingAsAdded(false);
+
+        final PreferenceCategory modeCategory = preferenceScreen.findPreference(
+                CATEGORY_MODE_KEY);
+        modeCategory.setOrder(Integer.MAX_VALUE - 1);
+
         final PreferenceCategory footerCategory = preferenceScreen.findPreference(
                 CATEGORY_FOOTER_KEY);
         footerCategory.setOrder(Integer.MAX_VALUE);
@@ -148,22 +154,22 @@
     }
 
     @Override
-    protected void onRemoveSwitchBarToggleSwitch() {
-        super.onRemoveSwitchBarToggleSwitch();
-        mToggleSwitch.setOnBeforeCheckedChangeListener(null);
+    protected void onRemoveSwitchPreferenceToggleSwitch() {
+        super.onRemoveSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null);
     }
 
     @Override
-    protected void updateSwitchBarText(SwitchBar switchBar) {
-        switchBar.setSwitchBarText(R.string.accessibility_daltonizer_master_switch_title,
-                R.string.accessibility_daltonizer_master_switch_title);
+    protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
+        switchPreference.setTitle(R.string.accessibility_daltonizer_master_switch_title);
     }
 
     @Override
-    protected void onInstallSwitchBarToggleSwitch() {
-        super.onInstallSwitchBarToggleSwitch();
+    protected void onInstallSwitchPreferenceToggleSwitch() {
+        super.onInstallSwitchPreferenceToggleSwitch();
         updateSwitchBarToggleSwitch();
-        mToggleSwitch.setOnBeforeCheckedChangeListener((toggleSwitch, checked) -> {
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener((preference) -> {
+            boolean checked = ((SwitchPreference) preference).isChecked();
             onPreferenceToggled(mPreferenceKey, checked);
             return false;
         });
@@ -177,10 +183,10 @@
 
     private void updateSwitchBarToggleSwitch() {
         final boolean checked = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
-        if (mSwitchBar.isChecked() == checked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() == checked) {
             return;
         }
-        mSwitchBar.setCheckedInternal(checked);
+        mToggleServiceDividerSwitchPreference.setChecked(checked);
     }
 
     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
index 8764b12..3521aa5 100644
--- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
@@ -40,13 +40,13 @@
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
 import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
 
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
 import com.android.settings.widget.SwitchBar;
-import com.android.settings.widget.ToggleSwitch;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -64,8 +64,7 @@
 public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment
         implements ShortcutPreference.OnClickListener {
 
-    protected SwitchBar mSwitchBar;
-    protected ToggleSwitch mToggleSwitch;
+    protected DividerSwitchPreference mToggleServiceDividerSwitchPreference;
     protected ShortcutPreference mShortcutPreference;
     protected Preference mSettingsPreference;
     protected String mPreferenceKey;
@@ -80,7 +79,7 @@
     protected CharSequence mHtmlDescription;
     private static final String ANCHOR_TAG = "a";
     private static final String DRAWABLE_FOLDER = "drawable";
-
+    protected static final String KEY_GENERAL_CATEGORY = "categories";
     private static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
     private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type";
     private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
@@ -133,12 +132,9 @@
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
 
-        SettingsActivity activity = (SettingsActivity) getActivity();
-        mSwitchBar = activity.getSwitchBar();
-        mToggleSwitch = mSwitchBar.getSwitch();
-
-        onProcessArguments(getArguments());
-        updateSwitchBarText(mSwitchBar);
+        final SettingsActivity activity = (SettingsActivity) getActivity();
+        final SwitchBar switchBar = activity.getSwitchBar();
+        switchBar.hide();
 
         PreferenceScreen preferenceScreen = getPreferenceScreen();
         if (mImageUri != null) {
@@ -149,12 +145,19 @@
             preferenceScreen.addPreference(animatedImagePreference);
         }
 
-        final PreferenceCategory category = new PreferenceCategory(getPrefContext());
-        category.setTitle(R.string.accessibility_screen_option);
-        preferenceScreen.addPreference(category);
+        mToggleServiceDividerSwitchPreference = new DividerSwitchPreference(getPrefContext());
+        preferenceScreen.addPreference(mToggleServiceDividerSwitchPreference);
+
+        onProcessArguments(getArguments());
+        updateToggleServiceTitle(mToggleServiceDividerSwitchPreference);
+
+        final PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
+        groupCategory.setKey(KEY_GENERAL_CATEGORY);
+        groupCategory.setTitle(R.string.accessibility_screen_option);
+        preferenceScreen.addPreference(groupCategory);
 
         initShortcutPreference(savedInstanceState);
-        category.addPreference(mShortcutPreference);
+        groupCategory.addPreference(mShortcutPreference);
 
         // Show the "Settings" menu as if it were a preference screen.
         if (mSettingsTitle != null && mSettingsIntent != null) {
@@ -166,7 +169,7 @@
 
         // The downloaded app may not show Settings. The framework app has Settings.
         if (mSettingsPreference != null) {
-            category.addPreference(mSettingsPreference);
+            groupCategory.addPreference(mSettingsPreference);
         }
 
         if (mStaticDescription != null ||  mHtmlDescription != null) {
@@ -311,31 +314,27 @@
         removeActionBarToggleSwitch();
     }
 
-    protected void updateSwitchBarText(SwitchBar switchBar) {
-        // Implement this to provide meaningful text in switch bar.
-        switchBar.setSwitchBarText(R.string.accessibility_service_master_switch_title,
-                R.string.accessibility_service_master_switch_title);
+    protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
+        switchPreference.setTitle(R.string.accessibility_service_master_switch_title);
     }
 
     protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled);
 
-    protected void onInstallSwitchBarToggleSwitch() {
+    protected void onInstallSwitchPreferenceToggleSwitch() {
         // Implement this to set a checked listener.
     }
 
-    protected void onRemoveSwitchBarToggleSwitch() {
+    protected void onRemoveSwitchPreferenceToggleSwitch() {
         // Implement this to reset a checked listener.
     }
 
     private void installActionBarToggleSwitch() {
-        mSwitchBar.show();
-        onInstallSwitchBarToggleSwitch();
+        onInstallSwitchPreferenceToggleSwitch();
     }
 
     private void removeActionBarToggleSwitch() {
-        mToggleSwitch.setOnBeforeCheckedChangeListener(null);
-        onRemoveSwitchBarToggleSwitch();
-        mSwitchBar.hide();
+        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null);
+        onRemoveSwitchPreferenceToggleSwitch();
     }
 
     public void setTitle(String title) {
@@ -349,7 +348,7 @@
         // Enabled.
         if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
             final boolean enabled = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED);
-            mSwitchBar.setCheckedInternal(enabled);
+            mToggleServiceDividerSwitchPreference.setChecked(enabled);
         }
 
         // Title.
@@ -493,8 +492,9 @@
         if (info.isEmpty()) {
             info = new HashSet<>();
         } else {
-            final Set<String> filtered = info.stream().filter(
-                    str -> str.contains(componentName)).collect(Collectors.toSet());
+            final Set<String> filtered = info.stream()
+                    .filter(str -> str.contains(componentName))
+                    .collect(Collectors.toSet());
             info.removeAll(filtered);
         }
         final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
@@ -538,9 +538,9 @@
 
         final Set<String> info = SharedPreferenceUtils.getUserShortcutType(context);
         final String componentName = mComponentName.flattenToString();
-        final Set<String> filtered = info.stream().filter(
-                str -> str.contains(componentName)).collect(
-                Collectors.toSet());
+        final Set<String> filtered = info.stream()
+                .filter(str -> str.contains(componentName))
+                .collect(Collectors.toSet());
         if (filtered.isEmpty()) {
             return defaultValue;
         }
diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
index 5182593..7ca2abf 100644
--- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
@@ -60,10 +60,13 @@
 import java.util.StringJoiner;
 import java.util.stream.Collectors;
 
+/**
+ * Fragment that shows the actual UI for providing basic magnification accessibility service setup
+ * and does not have toggle bar to turn on service to use.
+ */
 public class ToggleScreenMagnificationPreferenceFragment extends
         ToggleFeaturePreferenceFragment {
 
-    private static final String SETTINGS_KEY = "screen_magnification_settings";
     private static final String EXTRA_SHORTCUT_TYPE = "shortcut_type";
     private static final String KEY_SHORTCUT_PREFERENCE = "shortcut_preference";
     private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
@@ -429,11 +432,9 @@
     }
 
     @Override
-    protected void onInstallSwitchBarToggleSwitch() {
-        super.onInstallSwitchBarToggleSwitch();
-
-        // Magnify is temporary-use app which uses shortcut to magnify screen, not by toggle.
-        mSwitchBar.hide();
+    protected void onInstallSwitchPreferenceToggleSwitch() {
+        super.onInstallSwitchPreferenceToggleSwitch();
+        mToggleServiceDividerSwitchPreference.setVisible(false);
     }
 
     @Override
diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentForSetupWizard.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentForSetupWizard.java
index 5fe62a7..3b786ff 100644
--- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentForSetupWizard.java
+++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragmentForSetupWizard.java
@@ -32,11 +32,12 @@
         // Log the final choice in value if it's different from the previous value.
         Bundle args = getArguments();
         if ((args != null) && args.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
-            if (mToggleSwitch.isChecked() != args.getBoolean(AccessibilitySettings.EXTRA_CHECKED)) {
+            if (mToggleServiceDividerSwitchPreference.isChecked() != args.getBoolean(
+                    AccessibilitySettings.EXTRA_CHECKED)) {
                 // TODO: Distinguish between magnification modes
                 mMetricsFeatureProvider.action(getContext(),
                         SettingsEnums.SUW_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFICATION,
-                        mToggleSwitch.isChecked());
+                        mToggleServiceDividerSwitchPreference.isChecked());
             }
         }
         super.onStop();
diff --git a/src/com/android/settings/accessibility/ToggleScreenReaderPreferenceFragmentForSetupWizard.java b/src/com/android/settings/accessibility/ToggleScreenReaderPreferenceFragmentForSetupWizard.java
index 29c031d..0f114fd 100644
--- a/src/com/android/settings/accessibility/ToggleScreenReaderPreferenceFragmentForSetupWizard.java
+++ b/src/com/android/settings/accessibility/ToggleScreenReaderPreferenceFragmentForSetupWizard.java
@@ -27,7 +27,7 @@
     @Override
     protected void onProcessArguments(Bundle arguments) {
         super.onProcessArguments(arguments);
-        mToggleSwitchWasInitiallyChecked = mToggleSwitch.isChecked();
+        mToggleSwitchWasInitiallyChecked = mToggleServiceDividerSwitchPreference.isChecked();
     }
 
     @Override
@@ -38,11 +38,11 @@
     @Override
     public void onStop() {
         // Log the final choice in value if it's different from the previous value.
-        if (mToggleSwitch.isChecked() != mToggleSwitchWasInitiallyChecked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() != mToggleSwitchWasInitiallyChecked) {
             mMetricsFeatureProvider.action(getContext(),
-                    SettingsEnums.SUW_ACCESSIBILITY_TOGGLE_SCREEN_READER, mToggleSwitch.isChecked());
+                    SettingsEnums.SUW_ACCESSIBILITY_TOGGLE_SCREEN_READER,
+                    mToggleServiceDividerSwitchPreference.isChecked());
         }
-
         super.onStop();
     }
 }
diff --git a/src/com/android/settings/accessibility/ToggleSelectToSpeakPreferenceFragmentForSetupWizard.java b/src/com/android/settings/accessibility/ToggleSelectToSpeakPreferenceFragmentForSetupWizard.java
index da94abc..09598f6 100644
--- a/src/com/android/settings/accessibility/ToggleSelectToSpeakPreferenceFragmentForSetupWizard.java
+++ b/src/com/android/settings/accessibility/ToggleSelectToSpeakPreferenceFragmentForSetupWizard.java
@@ -27,7 +27,7 @@
     @Override
     protected void onProcessArguments(Bundle arguments) {
         super.onProcessArguments(arguments);
-        mToggleSwitchWasInitiallyChecked = mToggleSwitch.isChecked();
+        mToggleSwitchWasInitiallyChecked = mToggleServiceDividerSwitchPreference.isChecked();
     }
 
     @Override
@@ -38,10 +38,10 @@
     @Override
     public void onStop() {
         // Log the final choice in value if it's different from the previous value.
-        if (mToggleSwitch.isChecked() != mToggleSwitchWasInitiallyChecked) {
+        if (mToggleServiceDividerSwitchPreference.isChecked() != mToggleSwitchWasInitiallyChecked) {
             mMetricsFeatureProvider.action(getContext(),
                     SettingsEnums.SUW_ACCESSIBILITY_TOGGLE_SELECT_TO_SPEAK,
-                    mToggleSwitch.isChecked());
+                    mToggleServiceDividerSwitchPreference.isChecked());
         }
 
         super.onStop();
diff --git a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java
index 9bebc9a..35a2d19 100644
--- a/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragmentTest.java
@@ -32,7 +32,6 @@
 
 import com.android.settings.R;
 import com.android.settings.accessibility.ToggleFeaturePreferenceFragment.AccessibilityUserShortcutType;
-import com.android.settings.widget.SwitchBar;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -82,9 +81,9 @@
         mySet.add(TEST_SERVICE_KEY_2);
         mySet.add(TEST_SERVICE_KEY_3);
 
-        final Set<String> filtered = mySet.stream().filter(
-                str -> str.contains(TEST_SERVICE_NAME_1)).collect(
-                Collectors.toSet());
+        final Set<String> filtered = mySet.stream()
+                .filter(str -> str.contains(TEST_SERVICE_NAME_1))
+                .collect(Collectors.toSet());
         mySet.removeAll(filtered);
 
         assertThat(mySet).doesNotContain(TEST_SERVICE_KEY_1);
@@ -93,28 +92,26 @@
 
     @Test
     public void stringSet_convertA11yUserShortcutType_shouldReturnPreferredShortcut() {
-        int type = 0;
         Set<String> mySet = new HashSet<>();
         mySet.add(TEST_SERVICE_KEY_1);
         mySet.add(TEST_SERVICE_KEY_2);
         mySet.add(TEST_SERVICE_KEY_3);
 
-        final Set<String> filtered = mySet.stream().filter(
-                str -> str.contains(TEST_SERVICE_NAME_1)).collect(
-                Collectors.toSet());
-        for (String str : filtered) {
-            final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
-            type = shortcut.getUserShortcutType();
-        }
+        final Set<String> filtered = mySet.stream()
+                .filter(str -> str.contains(TEST_SERVICE_NAME_1))
+                .collect(Collectors.toSet());
 
+        final String str = (String) filtered.toArray()[0];
+        final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(str);
+        final int type = shortcut.getUserShortcutType();
         assertThat(type).isEqualTo(TEST_SERVICE_VALUE_1);
     }
 
     @Test
     public void createFragment_shouldOnlyAddPreferencesOnce() {
         mFragment = spy(new ToggleFeaturePreferenceFragmentTestable());
-        FragmentController.setupFragment(mFragment, FragmentActivity.class, 0 /* containerViewId*/,
-                null /* bundle */);
+        FragmentController.setupFragment(mFragment, FragmentActivity.class,
+                /* containerViewId= */ 0, /* bundle= */null);
 
         // execute exactly once
         verify(mFragment).addPreferencesFromResource(R.xml.placeholder_prefs);
@@ -152,11 +149,5 @@
         public void onViewCreated(View view, Bundle savedInstanceState) {
             // do nothing
         }
-
-        @Override
-        public void onActivityCreated(Bundle savedInstanceState) {
-            mSwitchBar = mock(SwitchBar.class);
-            super.onActivityCreated(savedInstanceState);
-        }
     }
 }