From 0f140e8c7dfd3047714cf061ca6b130b5b1e128b Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Tue, 30 Jun 2020 14:36:36 -0700 Subject: [PATCH] Synthesize touch events for hybrid views --- .../platform/PlatformViewsController.java | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index 054befa0a65f3..5041db03dda2a 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -244,37 +244,18 @@ public void run() { @Override public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) { - PointerProperties[] pointerProperties = - parsePointerPropertiesList(touch.rawPointerPropertiesList) - .toArray(new PointerProperties[touch.pointerCount]); - PointerCoords[] pointerCoords = - parsePointerCoordsList(touch.rawPointerCoords, getDisplayDensity()) - .toArray(new PointerCoords[touch.pointerCount]); - - if (!vdControllers.containsKey(touch.viewId)) { - throw new IllegalStateException( - "Sending touch to an unknown view with id: " + touch.viewId); - } - - MotionEvent event = - MotionEvent.obtain( - touch.downTime.longValue(), - touch.eventTime.longValue(), - touch.action, - touch.pointerCount, - pointerProperties, - pointerCoords, - touch.metaState, - touch.buttonState, - touch.xPrecision, - touch.yPrecision, - touch.deviceId, - touch.edgeFlags, - touch.source, - touch.flags); - + final int viewId = touch.viewId; + float density = context.getResources().getDisplayMetrics().density; ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); - vdControllers.get(touch.viewId).dispatchTouchEvent(event); + final MotionEvent event = toMotionEvent(density, touch); + if (vdControllers.containsKey(viewId)) { + vdControllers.get(touch.viewId).dispatchTouchEvent(event); + } else if (platformViews.get(viewId) != null) { + View view = platformViews.get(touch.viewId); + view.dispatchTouchEvent(event); + } else { + throw new IllegalStateException("Sending touch to an unknown view with id: " + viewId); + } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @@ -317,6 +298,32 @@ private void ensureValidAndroidVersion(int minSdkVersion) { } }; + private static MotionEvent toMotionEvent( + float density, PlatformViewsChannel.PlatformViewTouch touch) { + PointerProperties[] pointerProperties = + parsePointerPropertiesList(touch.rawPointerPropertiesList) + .toArray(new PointerProperties[touch.pointerCount]); + PointerCoords[] pointerCoords = + parsePointerCoordsList(touch.rawPointerCoords, density) + .toArray(new PointerCoords[touch.pointerCount]); + + return MotionEvent.obtain( + touch.downTime.longValue(), + touch.eventTime.longValue(), + touch.action, + touch.pointerCount, + pointerProperties, + pointerCoords, + touch.metaState, + touch.buttonState, + touch.xPrecision, + touch.yPrecision, + touch.deviceId, + touch.edgeFlags, + touch.source, + touch.flags); + } + public PlatformViewsController() { registry = new PlatformViewRegistryImpl(); vdControllers = new HashMap<>();