diff options
8 files changed, 104 insertions, 0 deletions
diff --git a/core/java/android/hardware/input/InputSettings.java b/core/java/android/hardware/input/InputSettings.java index 34c88e91a979..8da630c95135 100644 --- a/core/java/android/hardware/input/InputSettings.java +++ b/core/java/android/hardware/input/InputSettings.java @@ -28,6 +28,7 @@ import static com.android.hardware.input.Flags.keyboardA11yStickyKeysFlag; import static com.android.hardware.input.Flags.mouseScrollingAcceleration; import static com.android.hardware.input.Flags.mouseReverseVerticalScrolling; import static com.android.hardware.input.Flags.mouseSwapPrimaryButton; +import static com.android.hardware.input.Flags.pointerAcceleration; import static com.android.hardware.input.Flags.touchpadSystemGestureDisable; import static com.android.hardware.input.Flags.touchpadThreeFingerTapShortcut; import static com.android.hardware.input.Flags.touchpadVisualizer; @@ -418,6 +419,15 @@ public class InputSettings { } /** + * Returns true if the feature flag for the pointer acceleration toggle is + * enabled. + * @hide + */ + public static boolean isPointerAccelerationFeatureFlagEnabled() { + return pointerAcceleration(); + } + + /** * Returns true if the touchpad visualizer is allowed to appear. * * @param context The application context. @@ -720,6 +730,47 @@ public class InputSettings { } /** + * Whether cursor acceleration is enabled or not for connected mice. + * + * @param context The application context. + * + * @hide + */ + public static boolean isMousePointerAccelerationEnabled(@NonNull Context context) { + if (!isPointerAccelerationFeatureFlagEnabled()) { + return false; + } + + return Settings.System.getIntForUser(context.getContentResolver(), + Settings.System.MOUSE_POINTER_ACCELERATION_ENABLED, 1, UserHandle.USER_CURRENT) + == 1; + } + + /** + * Sets whether mouse acceleration is enabled. + * + * When enabled, the mouse cursor moves farther when it is moved faster. + * When disabled, the mouse cursor speed becomes directly proportional to + * the speed at which the mouse is moved. + * + * @param context The application context. + * @param enabled Will enable mouse acceleration if true, disable it if + * false. + * @hide + */ + @RequiresPermission(Manifest.permission.WRITE_SETTINGS) + public static void setMouseAccelerationEnabled(@NonNull Context context, + boolean enabled) { + if (!isPointerAccelerationFeatureFlagEnabled()) { + return; + } + Settings.System.putIntForUser(context.getContentResolver(), + Settings.System.MOUSE_POINTER_ACCELERATION_ENABLED, enabled ? 1 : 0, + UserHandle.USER_CURRENT); + } + + + /** * Whether Accessibility bounce keys feature is enabled. * * <p> diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 2ad6669979b7..6e58780ac7a7 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -6362,6 +6362,16 @@ public final class Settings { public static final String MOUSE_SCROLLING_ACCELERATION = "mouse_scrolling_acceleration"; /** + * Whether mouse acceleration is enabled. + * + * When enabled, the mouse cursor will accelerate as the mouse moves faster. + * + * @hide + */ + public static final String MOUSE_POINTER_ACCELERATION_ENABLED = + "mouse_pointer_acceleration_enabled"; + + /** * Pointer fill style, specified by * {@link android.view.PointerIcon.PointerIconVectorStyleFill} constants. * @@ -6610,6 +6620,7 @@ public final class Settings { PRIVATE_SETTINGS.add(DEFAULT_DEVICE_FONT_SCALE); PRIVATE_SETTINGS.add(MOUSE_REVERSE_VERTICAL_SCROLLING); PRIVATE_SETTINGS.add(MOUSE_SWAP_PRIMARY_BUTTON); + PRIVATE_SETTINGS.add(MOUSE_POINTER_ACCELERATION_ENABLED); PRIVATE_SETTINGS.add(PREFERRED_REGION); PRIVATE_SETTINGS.add(MOUSE_SCROLLING_ACCELERATION); } diff --git a/core/proto/android/providers/settings/system.proto b/core/proto/android/providers/settings/system.proto index dd9bfa51c634..0d99200f4e6f 100644 --- a/core/proto/android/providers/settings/system.proto +++ b/core/proto/android/providers/settings/system.proto @@ -228,6 +228,7 @@ message SystemSettingsProto { optional SettingProto reverse_vertical_scrolling = 1 [ (android.privacy).dest = DEST_AUTOMATIC ]; optional SettingProto swap_primary_button = 2 [ (android.privacy).dest = DEST_AUTOMATIC ]; optional SettingProto scrolling_acceleration = 3 [ (android.privacy).dest = DEST_AUTOMATIC ]; + optional SettingProto pointer_acceleration_enabled = 4 [ (android.privacy).dest = DEST_AUTOMATIC ]; } optional Mouse mouse = 38; diff --git a/packages/SettingsProvider/src/android/provider/settings/backup/SystemSettings.java b/packages/SettingsProvider/src/android/provider/settings/backup/SystemSettings.java index f1bbfc62681a..5b4ee8bdb339 100644 --- a/packages/SettingsProvider/src/android/provider/settings/backup/SystemSettings.java +++ b/packages/SettingsProvider/src/android/provider/settings/backup/SystemSettings.java @@ -110,6 +110,7 @@ public class SystemSettings { Settings.System.MOUSE_REVERSE_VERTICAL_SCROLLING, Settings.System.MOUSE_SCROLLING_ACCELERATION, Settings.System.MOUSE_SWAP_PRIMARY_BUTTON, + Settings.System.MOUSE_POINTER_ACCELERATION_ENABLED, Settings.System.TOUCHPAD_POINTER_SPEED, Settings.System.TOUCHPAD_NATURAL_SCROLLING, Settings.System.TOUCHPAD_TAP_TO_CLICK, diff --git a/packages/SettingsProvider/src/android/provider/settings/validators/SystemSettingsValidators.java b/packages/SettingsProvider/src/android/provider/settings/validators/SystemSettingsValidators.java index 6abd9b73e26d..0432eeacec4d 100644 --- a/packages/SettingsProvider/src/android/provider/settings/validators/SystemSettingsValidators.java +++ b/packages/SettingsProvider/src/android/provider/settings/validators/SystemSettingsValidators.java @@ -226,6 +226,7 @@ public class SystemSettingsValidators { VALIDATORS.put(System.MOUSE_REVERSE_VERTICAL_SCROLLING, BOOLEAN_VALIDATOR); VALIDATORS.put(System.MOUSE_SWAP_PRIMARY_BUTTON, BOOLEAN_VALIDATOR); VALIDATORS.put(System.MOUSE_SCROLLING_ACCELERATION, BOOLEAN_VALIDATOR); + VALIDATORS.put(System.MOUSE_POINTER_ACCELERATION_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.TOUCHPAD_POINTER_SPEED, new InclusiveIntegerRangeValidator(-7, 7)); VALIDATORS.put(System.TOUCHPAD_NATURAL_SCROLLING, BOOLEAN_VALIDATOR); VALIDATORS.put(System.TOUCHPAD_TAP_TO_CLICK, BOOLEAN_VALIDATOR); diff --git a/services/core/java/com/android/server/input/InputSettingsObserver.java b/services/core/java/com/android/server/input/InputSettingsObserver.java index 56cb6f49f6c4..febf24edc294 100644 --- a/services/core/java/com/android/server/input/InputSettingsObserver.java +++ b/services/core/java/com/android/server/input/InputSettingsObserver.java @@ -71,6 +71,9 @@ class InputSettingsObserver extends ContentObserver { (reason) -> updateMouseSwapPrimaryButton()), Map.entry(Settings.System.getUriFor(Settings.System.MOUSE_SCROLLING_ACCELERATION), (reason) -> updateMouseScrollingAcceleration()), + Map.entry(Settings.System.getUriFor( + Settings.System.MOUSE_POINTER_ACCELERATION_ENABLED), + (reason) -> updateMouseAccelerationEnabled()), Map.entry(Settings.System.getUriFor(Settings.System.TOUCHPAD_POINTER_SPEED), (reason) -> updateTouchpadPointerSpeed()), Map.entry(Settings.System.getUriFor(Settings.System.TOUCHPAD_NATURAL_SCROLLING), @@ -191,6 +194,11 @@ class InputSettingsObserver extends ContentObserver { InputSettings.isMouseScrollingAccelerationEnabled(mContext)); } + private void updateMouseAccelerationEnabled() { + mNative.setMouseAccelerationEnabled( + InputSettings.isMousePointerAccelerationEnabled(mContext)); + } + private void updateTouchpadPointerSpeed() { mNative.setTouchpadPointerSpeed( constrainPointerSpeedValue(InputSettings.getTouchpadPointerSpeed(mContext))); diff --git a/services/core/java/com/android/server/input/NativeInputManagerService.java b/services/core/java/com/android/server/input/NativeInputManagerService.java index ab5a680867e9..7dbde64a6412 100644 --- a/services/core/java/com/android/server/input/NativeInputManagerService.java +++ b/services/core/java/com/android/server/input/NativeInputManagerService.java @@ -138,6 +138,8 @@ interface NativeInputManagerService { void setMouseSwapPrimaryButtonEnabled(boolean enabled); + void setMouseAccelerationEnabled(boolean enabled); + void setTouchpadPointerSpeed(int speed); void setTouchpadNaturalScrollingEnabled(boolean enabled); @@ -429,6 +431,9 @@ interface NativeInputManagerService { public native void setMouseSwapPrimaryButtonEnabled(boolean enabled); @Override + public native void setMouseAccelerationEnabled(boolean enabled); + + @Override public native void setTouchpadPointerSpeed(int speed); @Override diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp index 813fec1454a7..f634beb77329 100644 --- a/services/core/jni/com_android_server_input_InputManagerService.cpp +++ b/services/core/jni/com_android_server_input_InputManagerService.cpp @@ -345,6 +345,7 @@ public: void setMouseReverseVerticalScrollingEnabled(bool enabled); void setMouseScrollingAccelerationEnabled(bool enabled); void setMouseSwapPrimaryButtonEnabled(bool enabled); + void setMouseAccelerationEnabled(bool enabled); void setTouchpadPointerSpeed(int32_t speed); void setTouchpadNaturalScrollingEnabled(bool enabled); void setTouchpadTapToClickEnabled(bool enabled); @@ -502,6 +503,9 @@ private: // True if the mouse primary button is swapped (left/right buttons). bool mouseSwapPrimaryButtonEnabled{false}; + // True if the mouse cursor will accelerate as the mouse moves faster. + bool mousePointerAccelerationEnabled{true}; + // The touchpad pointer speed, as a number from -7 (slowest) to 7 (fastest). int32_t touchpadPointerSpeed{0}; @@ -847,6 +851,7 @@ void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outCon outConfig->mouseReverseVerticalScrollingEnabled = mLocked.mouseReverseVerticalScrollingEnabled; outConfig->mouseSwapPrimaryButtonEnabled = mLocked.mouseSwapPrimaryButtonEnabled; + outConfig->mousePointerAccelerationEnabled = mLocked.mousePointerAccelerationEnabled; outConfig->touchpadPointerSpeed = mLocked.touchpadPointerSpeed; outConfig->touchpadNaturalScrollingEnabled = mLocked.touchpadNaturalScrollingEnabled; @@ -1458,6 +1463,21 @@ void NativeInputManager::setMouseSwapPrimaryButtonEnabled(bool enabled) { InputReaderConfiguration::Change::MOUSE_SETTINGS); } +void NativeInputManager::setMouseAccelerationEnabled(bool enabled) { + { // acquire lock + std::scoped_lock _l(mLock); + + if (mLocked.mousePointerAccelerationEnabled == enabled) { + return; + } + + mLocked.mousePointerAccelerationEnabled = enabled; + } // release lock + + mInputManager->getReader().requestRefreshConfiguration( + InputReaderConfiguration::Change::POINTER_SPEED); +} + void NativeInputManager::setPointerSpeed(int32_t speed) { { // acquire lock std::scoped_lock _l(mLock); @@ -3220,6 +3240,11 @@ static void nativeSetMouseSwapPrimaryButtonEnabled(JNIEnv* env, jobject nativeIm im->setMouseSwapPrimaryButtonEnabled(enabled); } +static void nativeSetMouseAccelerationEnabled(JNIEnv* env, jobject nativeImplObj, bool enabled) { + NativeInputManager* im = getNativeInputManager(env, nativeImplObj); + im->setMouseAccelerationEnabled(enabled); +} + static jboolean nativeSetKernelWakeEnabled(JNIEnv* env, jobject nativeImplObj, jint deviceId, jboolean enabled) { NativeInputManager* im = getNativeInputManager(env, nativeImplObj); @@ -3280,6 +3305,7 @@ static const JNINativeMethod gInputManagerMethods[] = { {"setMouseScrollingAccelerationEnabled", "(Z)V", (void*)nativeSetMouseScrollingAccelerationEnabled}, {"setMouseSwapPrimaryButtonEnabled", "(Z)V", (void*)nativeSetMouseSwapPrimaryButtonEnabled}, + {"setMouseAccelerationEnabled", "(Z)V", (void*)nativeSetMouseAccelerationEnabled}, {"setTouchpadPointerSpeed", "(I)V", (void*)nativeSetTouchpadPointerSpeed}, {"setTouchpadNaturalScrollingEnabled", "(Z)V", (void*)nativeSetTouchpadNaturalScrollingEnabled}, |