From 3911e101ce9d51ed7e12ab8c357f7d14deaa5fc3 Mon Sep 17 00:00:00 2001 From: Nergi Rahardi Date: Thu, 29 Jun 2023 13:36:34 +0900 Subject: [PATCH] Prevent sending the same KeyRepeatInfo multiple times Bug: 286078544 Test: atest InputManagerServiceTests Change-Id: Ifab820d35399b7ea76681d7b137bed2994d974e4 --- .../server/input/InputSettingsObserver.java | 24 ++++++++++++++++++- .../server/input/InputManagerServiceTests.kt | 3 +-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/input/InputSettingsObserver.java b/services/core/java/com/android/server/input/InputSettingsObserver.java index 232979037d79a..3716e1f5cc331 100644 --- a/services/core/java/com/android/server/input/InputSettingsObserver.java +++ b/services/core/java/com/android/server/input/InputSettingsObserver.java @@ -47,6 +47,9 @@ class InputSettingsObserver extends ContentObserver { private final NativeInputManagerService mNative; private final Map> mObservers; + // Cache prevent notifying same KeyRepeatInfo data to native code multiple times. + private KeyRepeatInfo mLastKeyRepeatInfoSettingsUpdate; + InputSettingsObserver(Context context, Handler handler, InputManagerService service, NativeInputManagerService nativeIms) { super(handler); @@ -195,7 +198,11 @@ class InputSettingsObserver extends ContentObserver { final int delayMs = Settings.Secure.getIntForUser(mContext.getContentResolver(), Settings.Secure.KEY_REPEAT_DELAY_MS, ViewConfiguration.getKeyRepeatDelay(), UserHandle.USER_CURRENT); - mNative.setKeyRepeatConfiguration(timeoutMs, delayMs); + if (mLastKeyRepeatInfoSettingsUpdate == null || !mLastKeyRepeatInfoSettingsUpdate.isEqualTo( + timeoutMs, delayMs)) { + mNative.setKeyRepeatConfiguration(timeoutMs, delayMs); + mLastKeyRepeatInfoSettingsUpdate = new KeyRepeatInfo(timeoutMs, delayMs); + } } // Not using ViewConfiguration.getLongPressTimeout here because it may return a stale value. @@ -214,4 +221,19 @@ class InputSettingsObserver extends ContentObserver { } mNative.setMaximumObscuringOpacityForTouch(opacity); } + + private static class KeyRepeatInfo { + private final int mKeyRepeatTimeoutMs; + private final int mKeyRepeatDelayMs; + + private KeyRepeatInfo(int keyRepeatTimeoutMs, int keyRepeatDelayMs) { + this.mKeyRepeatTimeoutMs = keyRepeatTimeoutMs; + this.mKeyRepeatDelayMs = keyRepeatDelayMs; + } + + public boolean isEqualTo(int keyRepeatTimeoutMs, int keyRepeatDelayMs) { + return mKeyRepeatTimeoutMs == keyRepeatTimeoutMs + && mKeyRepeatDelayMs == keyRepeatDelayMs; + } + } } diff --git a/services/tests/servicestests/src/com/android/server/input/InputManagerServiceTests.kt b/services/tests/servicestests/src/com/android/server/input/InputManagerServiceTests.kt index 2f1a3760512b3..498776db3ac8d 100644 --- a/services/tests/servicestests/src/com/android/server/input/InputManagerServiceTests.kt +++ b/services/tests/servicestests/src/com/android/server/input/InputManagerServiceTests.kt @@ -133,8 +133,7 @@ class InputManagerServiceTests { verify(native).setMotionClassifierEnabled(anyBoolean()) verify(native).setMaximumObscuringOpacityForTouch(anyFloat()) verify(native).setStylusPointerIconEnabled(anyBoolean()) - // TODO(b/286078544): There is no need to call this more than once. - verify(native, times(3)).setKeyRepeatConfiguration(anyInt(), anyInt()) + verify(native).setKeyRepeatConfiguration(anyInt(), anyInt()) } @Test