Fixing Concurrent Modification Exception in TouchpadDebugView
A race conditions is being hit because the updates in TouchpadVisualizationView are happening from different threads. In this case, hardware state update is received (which happens on the InputReader thread) at the same time as the onDraw (which happens on the UI thread).
So, Handlers is used to start posting the updateHardwareState and and updateGestureInfo in TouchpadDebugView and onDraw and onTouchpadHardwareStateNotified in TouchpadVisualizationView.
Test: $ atest TouchpadDebugViewTest
Test: $ atest TouchpadDebugViewControllerTests
Test: Presubmit checks
Bug: 368743974
Flag: com.android.hardware.input.touchpad_visualizer
Change-Id: I14df74b188b3ba8aadd49e1ce4098e7da1cc1dcb
diff --git a/services/core/java/com/android/server/input/debug/TouchpadDebugViewController.java b/services/core/java/com/android/server/input/debug/TouchpadDebugViewController.java
index 19c802b..9cfbfa64 100644
--- a/services/core/java/com/android/server/input/debug/TouchpadDebugViewController.java
+++ b/services/core/java/com/android/server/input/debug/TouchpadDebugViewController.java
@@ -16,6 +16,7 @@
package com.android.server.input.debug;
+import android.annotation.AnyThread;
import android.annotation.Nullable;
import android.content.Context;
import android.hardware.input.InputManager;
@@ -157,19 +158,28 @@
* @param touchpadHardwareState the hardware state of a touchpad
* @param deviceId the deviceId of the touchpad that is sending the hardware state
*/
+ @AnyThread
public void updateTouchpadHardwareState(TouchpadHardwareState touchpadHardwareState,
int deviceId) {
- if (mTouchpadDebugView != null) {
- mTouchpadDebugView.updateHardwareState(touchpadHardwareState, deviceId);
- }
+ mHandler.post(() -> {
+ if (mTouchpadDebugView != null) {
+ mTouchpadDebugView.post(
+ () -> mTouchpadDebugView.updateHardwareState(touchpadHardwareState,
+ deviceId));
+ }
+ });
}
/**
* Notify the TouchpadDebugView of a new touchpad gesture.
*/
+ @AnyThread
public void updateTouchpadGestureInfo(int gestureType, int deviceId) {
- if (mTouchpadDebugView != null) {
- mTouchpadDebugView.updateGestureInfo(gestureType, deviceId);
- }
+ mHandler.post(() -> {
+ if (mTouchpadDebugView != null) {
+ mTouchpadDebugView.post(
+ () -> mTouchpadDebugView.updateGestureInfo(gestureType, deviceId));
+ }
+ });
}
}