Merge "[Physical Keyboard] Add Repeat key toggle" into main
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5326fa5..97a7e23 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4696,6 +4696,10 @@
     <string name="keyboard_a11y_settings">Physical keyboard accessibility</string>
     <!-- Summary for the button to trigger the 'Physical keyboard accessibility' page. [CHAR LIMIT=NONE] -->
     <string name="keyboard_a11y_settings_summary">Sticky keys, Bounce keys, Mouse keys</string>
+    <!-- Title for the keyboard repeat key option. [CHAR LIMIT=60] -->
+    <string name="keyboard_repeat_key_title">Repeat Keys</string>
+    <!-- Summary for the keyboard repeat key option. [CHAR LIMIT=NONE] -->
+    <string name="keyboard_repeat_key_summary">Hold down a key to repeat its character until the key is released</string>
 
     <!-- Title text for per IME subtype keyboard layout. [CHAR LIMIT=35] -->
     <string name="ime_label_title"><xliff:g id="ime_label" example="Gboard">%s</xliff:g> layout</string>
diff --git a/res/xml/physical_keyboard_settings.xml b/res/xml/physical_keyboard_settings.xml
index 1527ff5..5d2c5fc 100644
--- a/res/xml/physical_keyboard_settings.xml
+++ b/res/xml/physical_keyboard_settings.xml
@@ -15,6 +15,7 @@
 -->
 <PreferenceScreen
     xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res-auto"
     android:title="@string/physical_keyboard_title">
     <!-- Additional preference screens are inserted here programmatically
          with low order values to set the key map of each attached keyboard. -->
@@ -31,6 +32,14 @@
             android:title="@string/modifier_keys_settings"
             android:summary="@string/modifier_keys_settings_summary"
             android:fragment="com.android.settings.inputmethod.ModifierKeysSettings" />
+
+        <SwitchPreferenceCompat
+            android:key="physical_keyboard_repeat_key"
+            android:title="@string/keyboard_repeat_key_title"
+            android:summary="@string/keyboard_repeat_key_summary"
+            android:defaultValue="false"
+            settings:controller="com.android.settings.inputmethod.KeyboardRepeatKeysController" />
+
         <Preference
             android:key="physical_keyboard_a11y"
             android:title="@string/keyboard_a11y_settings"
diff --git a/src/com/android/settings/inputmethod/KeyboardRepeatKeysController.java b/src/com/android/settings/inputmethod/KeyboardRepeatKeysController.java
new file mode 100644
index 0000000..a232098
--- /dev/null
+++ b/src/com/android/settings/inputmethod/KeyboardRepeatKeysController.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2024 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.inputmethod;
+
+import android.content.Context;
+import android.hardware.input.InputSettings;
+import android.net.Uri;
+import android.provider.Settings;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreferenceCompat;
+
+public class KeyboardRepeatKeysController extends
+        InputSettingPreferenceController implements
+        LifecycleObserver {
+
+    @Nullable
+    private SwitchPreferenceCompat mSwitchPreferenceCompat;
+
+    public KeyboardRepeatKeysController(@NonNull Context context,
+            @NonNull String key) {
+        super(context, key);
+    }
+
+    @Override
+    public void displayPreference(@NonNull PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mSwitchPreferenceCompat = screen.findPreference(getPreferenceKey());
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return InputSettings.isRepeatKeysFeatureFlagEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return InputSettings.isRepeatKeysEnabled(mContext);
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        InputSettings.setRepeatKeysEnabled(mContext, isChecked);
+        return true;
+    }
+
+    @Override
+    protected void onInputSettingUpdated() {
+        if (mSwitchPreferenceCompat != null) {
+            mSwitchPreferenceCompat.setChecked(InputSettings.isRepeatKeysEnabled(mContext));
+        }
+    }
+
+    @Override
+    protected Uri getSettingUri() {
+        return Settings.Secure.getUriFor(
+                Settings.Secure.KEY_REPEAT_ENABLED);
+    }
+}
diff --git a/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java b/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
index a000f9e..48100a3 100644
--- a/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
+++ b/src/com/android/settings/inputmethod/PhysicalKeyboardFragment.java
@@ -48,8 +48,8 @@
 import com.android.internal.util.Preconditions;
 import com.android.settings.R;
 import com.android.settings.Settings;
-import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.core.SubSettingLauncher;
+import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.keyboard.Flags;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
@@ -65,7 +65,7 @@
 // TODO(b/327638540): Update implementation of preference here and reuse key preferences and
 //  controllers between here and A11y Setting page.
 @SearchIndexable
-public final class PhysicalKeyboardFragment extends SettingsPreferenceFragment
+public final class PhysicalKeyboardFragment extends DashboardFragment
         implements InputManager.InputDeviceListener,
         KeyboardLayoutDialogFragment.OnSetupKeyboardLayoutsListener {
 
@@ -79,6 +79,7 @@
     private static final String KEYBOARD_SHORTCUTS_HELPER = "keyboard_shortcuts_helper";
     private static final String MODIFIER_KEYS_SETTINGS = "modifier_keys_settings";
     private static final String EXTRA_AUTO_SELECTION = "auto_selection";
+    private static final String TAG = "KeyboardAndTouchA11yFragment";
     private static final Uri sVirtualKeyboardSettingsUri = Secure.getUriFor(
             Secure.SHOW_IME_WITH_HARD_KEYBOARD);
     private static final Uri sAccessibilityBounceKeysUri = Secure.getUriFor(
@@ -119,6 +120,16 @@
     private String mBluetoothAddress;
 
     @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.physical_keyboard_settings;
+    }
+
+    @Override
     public void onSaveInstanceState(Bundle outState) {
         outState.putParcelable(EXTRA_AUTO_SELECTION, mAutoInputDeviceIdentifier);
         super.onSaveInstanceState(outState);
@@ -126,6 +137,7 @@
 
     @Override
     public void onCreatePreferences(Bundle bundle, String s) {
+        super.onCreatePreferences(bundle, s);
         Activity activity = Preconditions.checkNotNull(getActivity());
         addPreferencesFromResource(R.xml.physical_keyboard_settings);
         mIm = Preconditions.checkNotNull(activity.getSystemService(InputManager.class));
diff --git a/tests/robotests/src/com/android/settings/inputmethod/KeyboardRepeatKeysControllerTest.java b/tests/robotests/src/com/android/settings/inputmethod/KeyboardRepeatKeysControllerTest.java
new file mode 100644
index 0000000..e1b4ffd
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/inputmethod/KeyboardRepeatKeysControllerTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2024 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.inputmethod;
+
+import static com.android.input.flags.Flags.FLAG_KEYBOARD_REPEAT_KEYS;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.hardware.input.InputSettings;
+import android.platform.test.annotations.DisableFlags;
+import android.platform.test.annotations.EnableFlags;
+import android.platform.test.flag.junit.SetFlagsRule;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = {
+        com.android.settings.testutils.shadow.ShadowFragment.class,
+})
+public class KeyboardRepeatKeysControllerTest {
+    @Rule
+    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
+    private Context mContext;
+
+    private KeyboardRepeatKeysController mKeyboardRepeatKeysController;
+
+    @Before
+    public void setUp() {
+        mContext = RuntimeEnvironment.application;
+        mKeyboardRepeatKeysController = new KeyboardRepeatKeysController(mContext,
+                "physical_keyboard_repeat_key");
+    }
+
+    @Test
+    @EnableFlags(FLAG_KEYBOARD_REPEAT_KEYS)
+    public void getAvailabilityStatus_flagIsEnabled_isAvailable() {
+        assertThat(mKeyboardRepeatKeysController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    @DisableFlags(FLAG_KEYBOARD_REPEAT_KEYS)
+    public void getAvailabilityStatus_flagIsDisabled_notSupport() {
+        assertThat(mKeyboardRepeatKeysController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
+    }
+
+    @Test
+    public void isChecked_sameWithInputSettingValue() {
+        boolean isRepeatKeysEnabled = InputSettings.isRepeatKeysEnabled(mContext);
+        assertThat(mKeyboardRepeatKeysController.isChecked()).isEqualTo(isRepeatKeysEnabled);
+    }
+
+    @Test
+    public void setChecked_updatesInputSettingValue() {
+        mKeyboardRepeatKeysController.setChecked(false);
+
+        assertThat(InputSettings.isRepeatKeysEnabled(mContext)).isEqualTo(false);
+    }
+}