Merge "Map pointer speed slider on mouse page to control mouse speed" into main
diff --git a/res/xml/mouse_settings.xml b/res/xml/mouse_settings.xml
index e4b3f1c..ec1c39d 100644
--- a/res/xml/mouse_settings.xml
+++ b/res/xml/mouse_settings.xml
@@ -28,11 +28,11 @@
         settings:controller="com.android.settings.inputmethod.MousePointerAccelerationPreferenceController" />
 
     <com.android.settings.widget.SeekBarPreference
-        android:key="trackpad_pointer_speed"
-        android:title="@string/trackpad_pointer_speed"
+        android:key="pointer_speed"
+        android:title="@string/pointer_speed"
         android:order="20"
         android:selectable="false"
-        settings:controller="com.android.settings.inputmethod.TouchpadPointerSpeedPreferenceController"/>
+        settings:controller="com.android.settings.inputmethod.MousePointerSpeedPreferenceController"/>
 
     <SwitchPreferenceCompat
         android:key="mouse_swap_primary_button"
diff --git a/src/com/android/settings/inputmethod/MousePointerSpeedPreferenceController.java b/src/com/android/settings/inputmethod/MousePointerSpeedPreferenceController.java
new file mode 100644
index 0000000..bb91b3c
--- /dev/null
+++ b/src/com/android/settings/inputmethod/MousePointerSpeedPreferenceController.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2025 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.app.settings.SettingsEnums;
+import android.content.Context;
+import android.hardware.input.InputSettings;
+
+import androidx.annotation.NonNull;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.core.SliderPreferenceController;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.widget.SeekBarPreference;
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
+
+public class MousePointerSpeedPreferenceController extends SliderPreferenceController {
+
+    private final MetricsFeatureProvider mMetricsFeatureProvider;
+
+    public MousePointerSpeedPreferenceController(@NonNull Context context, @NonNull String key) {
+        super(context, key);
+        mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        SeekBarPreference preference = screen.findPreference(getPreferenceKey());
+        preference.setMax(getMax());
+        preference.setMin(getMin());
+        preference.setProgress(getSliderPosition());
+        updateState(preference);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public boolean setSliderPosition(int position) {
+        if (position < getMin() || position > getMax()) {
+            return false;
+        }
+        InputSettings.setPointerSpeed(mContext, position);
+        mMetricsFeatureProvider.action(
+                mContext, SettingsEnums.ACTION_GESTURE_POINTER_SPEED_CHANGED, position);
+        return true;
+    }
+
+    @Override
+    public int getSliderPosition() {
+        return InputSettings.getPointerSpeed(mContext);
+    }
+
+    @Override
+    public int getMin() {
+        return InputSettings.MIN_POINTER_SPEED;
+    }
+
+    @Override
+    public int getMax() {
+        return InputSettings.MAX_POINTER_SPEED;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/inputmethod/MousePointerSpeedPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/inputmethod/MousePointerSpeedPreferenceControllerTest.java
new file mode 100644
index 0000000..82afec2
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/inputmethod/MousePointerSpeedPreferenceControllerTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2025 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.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+import android.hardware.input.InputSettings;
+import android.os.UserHandle;
+import android.provider.Settings;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+
+/** Tests for {@link MousePointerSpeedPreferenceController} */
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = {
+        com.android.settings.testutils.shadow.ShadowSystemSettings.class,
+})
+public class MousePointerSpeedPreferenceControllerTest {
+    @Rule
+    public MockitoRule rule = MockitoJUnit.rule();
+
+    private static final String PREFERENCE_KEY = "pointer_speed";
+    private static final String SETTING_KEY = Settings.System.POINTER_SPEED;
+
+    private MousePointerSpeedPreferenceController mController;
+    private int mDefaultSpeed;
+    private FakeFeatureFactory mFeatureFactory;
+
+    @Before
+    public void setUp() {
+        Context context = ApplicationProvider.getApplicationContext();
+        mFeatureFactory = FakeFeatureFactory.setupForTest();
+        mController = new MousePointerSpeedPreferenceController(context, PREFERENCE_KEY);
+        mDefaultSpeed = Settings.System.getIntForUser(
+                context.getContentResolver(),
+                SETTING_KEY,
+                InputSettings.DEFAULT_POINTER_SPEED,
+                UserHandle.USER_CURRENT);
+    }
+
+    @Test
+    public void setSliderPosition_speedValue1_shouldReturnTrue() {
+        int inputSpeed = 1;
+
+        boolean result = mController.setSliderPosition(inputSpeed);
+
+        assertThat(result).isTrue();
+        assertThat(mController.getSliderPosition()).isEqualTo(inputSpeed);
+        verify(mFeatureFactory.metricsFeatureProvider).action(
+                any(),
+                eq(SettingsEnums.ACTION_GESTURE_POINTER_SPEED_CHANGED),
+                eq(1));
+    }
+
+    @Test
+    public void setSliderPosition_speedValueOverMaxValue_shouldReturnFalse() {
+        int inputSpeed = InputSettings.MAX_POINTER_SPEED + 1;
+
+        boolean result = mController.setSliderPosition(inputSpeed);
+
+        assertThat(result).isFalse();
+        assertThat(mController.getSliderPosition()).isEqualTo(mDefaultSpeed);
+    }
+
+    @Test
+    public void setSliderPosition_speedValueOverMinValue_shouldReturnFalse() {
+        int inputSpeed = InputSettings.MIN_POINTER_SPEED - 1;
+
+        boolean result = mController.setSliderPosition(inputSpeed);
+
+        assertThat(result).isFalse();
+        assertThat(mController.getSliderPosition()).isEqualTo(mDefaultSpeed);
+    }
+}