Add Slow keys A11Y settings to PK settings page

Test: manual
Bug: 294546335
Change-Id: I9e20973e648d04b907e4d2225bd49b78e42d2089
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 8185828..8a56f1f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4285,6 +4285,10 @@
     <string name="bounce_keys">Bounce keys</string>
     <!-- Summary text for the 'Bounce keys' preference sub-screen. [CHAR LIMIT=100] -->
     <string name="bounce_keys_summary">Enable Bounce keys for physical keyboard accessibility</string>
+    <!-- Title for the 'Slow keys' preference switch. [CHAR LIMIT=35] -->
+    <string name="slow_keys">Slow keys</string>
+    <!-- Summary text for the 'Slow keys' preference sub-screen. [CHAR LIMIT=100] -->
+    <string name="slow_keys_summary">Enable Slow keys for physical keyboard accessibility</string>
     <!-- Title for the 'Sticky keys' preference switch. [CHAR LIMIT=35] -->
     <string name="sticky_keys">Sticky keys</string>
     <!-- Summary text for the 'Sticky keys' preference sub-screen. [CHAR LIMIT=100] -->
diff --git a/res/xml/physical_keyboard_settings.xml b/res/xml/physical_keyboard_settings.xml
index dc424d1..b95f23e 100644
--- a/res/xml/physical_keyboard_settings.xml
+++ b/res/xml/physical_keyboard_settings.xml
@@ -44,16 +44,22 @@
         android:title="@string/keyboard_a11y_category">
 
         <SwitchPreference
-            android:key="accessibility_bounce_keys"
-            android:title="@string/bounce_keys"
-            android:summary="@string/bounce_keys_summary"
-            android:defaultValue="false" />
-
-        <SwitchPreference
             android:key="accessibility_sticky_keys"
             android:title="@string/sticky_keys"
             android:summary="@string/sticky_keys_summary"
             android:defaultValue="false" />
 
+        <SwitchPreference
+            android:key="accessibility_slow_keys"
+            android:title="@string/slow_keys"
+            android:summary="@string/slow_keys_summary"
+            android:defaultValue="false" />
+
+        <SwitchPreference
+            android:key="accessibility_bounce_keys"
+            android:title="@string/bounce_keys"
+            android:summary="@string/bounce_keys_summary"
+            android:defaultValue="false" />
+
     </PreferenceCategory>
 </PreferenceScreen>
diff --git a/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java b/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
index 7de505e..b06edb2 100644
--- a/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
+++ b/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
@@ -70,6 +70,7 @@
     private static final String KEYBOARD_A11Y_CATEGORY = "keyboard_a11y_category";
     private static final String SHOW_VIRTUAL_KEYBOARD_SWITCH = "show_virtual_keyboard_switch";
     private static final String ACCESSIBILITY_BOUNCE_KEYS = "accessibility_bounce_keys";
+    private static final String ACCESSIBILITY_SLOW_KEYS = "accessibility_slow_keys";
     private static final String ACCESSIBILITY_STICKY_KEYS = "accessibility_sticky_keys";
     private static final String KEYBOARD_SHORTCUTS_HELPER = "keyboard_shortcuts_helper";
     private static final String MODIFIER_KEYS_SETTINGS = "modifier_keys_settings";
@@ -78,6 +79,8 @@
             Secure.SHOW_IME_WITH_HARD_KEYBOARD);
     private static final Uri sAccessibilityBounceKeysUri = Secure.getUriFor(
             Secure.ACCESSIBILITY_BOUNCE_KEYS);
+    private static final Uri sAccessibilitySlowKeysUri = Secure.getUriFor(
+            Secure.ACCESSIBILITY_SLOW_KEYS);
     private static final Uri sAccessibilityStickyKeysUri = Secure.getUriFor(
             Secure.ACCESSIBILITY_STICKY_KEYS);
 
@@ -97,6 +100,8 @@
     @Nullable
     private TwoStatePreference mAccessibilityBounceKeys = null;
     @Nullable
+    private TwoStatePreference mAccessibilitySlowKeys = null;
+    @Nullable
     private TwoStatePreference mAccessibilityStickyKeys = null;
 
 
@@ -127,6 +132,8 @@
         mKeyboardA11yCategory = Objects.requireNonNull(findPreference(KEYBOARD_A11Y_CATEGORY));
         mAccessibilityBounceKeys = Objects.requireNonNull(
                 mKeyboardA11yCategory.findPreference(ACCESSIBILITY_BOUNCE_KEYS));
+        mAccessibilitySlowKeys = Objects.requireNonNull(
+                mKeyboardA11yCategory.findPreference(ACCESSIBILITY_SLOW_KEYS));
         mAccessibilityStickyKeys = Objects.requireNonNull(
                 mKeyboardA11yCategory.findPreference(ACCESSIBILITY_STICKY_KEYS));
 
@@ -147,6 +154,9 @@
         if (!InputSettings.isAccessibilityBounceKeysFeatureEnabled()) {
             mKeyboardA11yCategory.removePreference(mAccessibilityBounceKeys);
         }
+        if (!InputSettings.isAccessibilitySlowKeysFeatureFlagEnabled()) {
+            mKeyboardA11yCategory.removePreference(mAccessibilitySlowKeys);
+        }
         if (!InputSettings.isAccessibilityStickyKeysFeatureEnabled()) {
             mKeyboardA11yCategory.removePreference(mAccessibilityStickyKeys);
         }
@@ -196,6 +206,8 @@
                 mShowVirtualKeyboardSwitchPreferenceChangeListener);
         Objects.requireNonNull(mAccessibilityBounceKeys).setOnPreferenceChangeListener(
                 mAccessibilityBounceKeysSwitchPreferenceChangeListener);
+        Objects.requireNonNull(mAccessibilitySlowKeys).setOnPreferenceChangeListener(
+                mAccessibilitySlowKeysSwitchPreferenceChangeListener);
         Objects.requireNonNull(mAccessibilityStickyKeys).setOnPreferenceChangeListener(
                 mAccessibilityStickyKeysSwitchPreferenceChangeListener);
         registerSettingsObserver();
@@ -208,6 +220,7 @@
         mIm.unregisterInputDeviceListener(this);
         Objects.requireNonNull(mShowVirtualKeyboardSwitch).setOnPreferenceChangeListener(null);
         Objects.requireNonNull(mAccessibilityBounceKeys).setOnPreferenceChangeListener(null);
+        Objects.requireNonNull(mAccessibilitySlowKeys).setOnPreferenceChangeListener(null);
         Objects.requireNonNull(mAccessibilityStickyKeys).setOnPreferenceChangeListener(null);
         unregisterSettingsObserver();
     }
@@ -315,10 +328,12 @@
         updateShowVirtualKeyboardSwitch();
 
         if (InputSettings.isAccessibilityBounceKeysFeatureEnabled()
-                || InputSettings.isAccessibilityStickyKeysFeatureEnabled()) {
+                || InputSettings.isAccessibilityStickyKeysFeatureEnabled()
+                || InputSettings.isAccessibilitySlowKeysFeatureFlagEnabled()) {
             Objects.requireNonNull(mKeyboardA11yCategory).setOrder(2);
             preferenceScreen.addPreference(mKeyboardA11yCategory);
             updateAccessibilityBounceKeysSwitch();
+            updateAccessibilitySlowKeysSwitch();
             updateAccessibilityStickyKeysSwitch();
         }
     }
@@ -356,6 +371,13 @@
                     mContentObserver,
                     UserHandle.myUserId());
         }
+        if (InputSettings.isAccessibilitySlowKeysFeatureFlagEnabled()) {
+            contentResolver.registerContentObserver(
+                    sAccessibilitySlowKeysUri,
+                    false,
+                    mContentObserver,
+                    UserHandle.myUserId());
+        }
         if (InputSettings.isAccessibilityStickyKeysFeatureEnabled()) {
             contentResolver.registerContentObserver(
                     sAccessibilityStickyKeysUri,
@@ -365,6 +387,7 @@
         }
         updateShowVirtualKeyboardSwitch();
         updateAccessibilityBounceKeysSwitch();
+        updateAccessibilitySlowKeysSwitch();
         updateAccessibilityStickyKeysSwitch();
     }
 
@@ -385,6 +408,14 @@
                 InputSettings.isAccessibilityBounceKeysEnabled(getContext()));
     }
 
+    private void updateAccessibilitySlowKeysSwitch() {
+        if (!InputSettings.isAccessibilitySlowKeysFeatureFlagEnabled()) {
+            return;
+        }
+        Objects.requireNonNull(mAccessibilitySlowKeys).setChecked(
+                InputSettings.isAccessibilitySlowKeysEnabled(getContext()));
+    }
+
     private void updateAccessibilityStickyKeysSwitch() {
         if (!InputSettings.isAccessibilityStickyKeysFeatureEnabled()) {
             return;
@@ -414,6 +445,13 @@
             };
 
     private final OnPreferenceChangeListener
+            mAccessibilitySlowKeysSwitchPreferenceChangeListener = (preference, newValue) -> {
+                InputSettings.setAccessibilitySlowKeysThreshold(getContext(),
+                        ((Boolean) newValue) ? 500 : 0);
+                return true;
+            };
+
+    private final OnPreferenceChangeListener
             mAccessibilityStickyKeysSwitchPreferenceChangeListener = (preference, newValue) -> {
                 InputSettings.setAccessibilityStickyKeysEnabled(getContext(), (Boolean) newValue);
                 return true;
@@ -426,6 +464,8 @@
                 updateShowVirtualKeyboardSwitch();
             } else if (sAccessibilityBounceKeysUri.equals(uri)) {
                 updateAccessibilityBounceKeysSwitch();
+            } else if (sAccessibilitySlowKeysUri.equals(uri)) {
+                updateAccessibilitySlowKeysSwitch();
             } else if (sAccessibilityStickyKeysUri.equals(uri)) {
                 updateAccessibilityStickyKeysSwitch();
             }