Skip to content

Commit f8bbcc3

Browse files
iskakaushikKaushik Iska
and
Kaushik Iska
authored
Synthesize touch events for hybrid views (flutter#19427)
Co-authored-by: Kaushik Iska <[email protected]>
1 parent f74c0f0 commit f8bbcc3

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -244,37 +244,18 @@ public void run() {
244244

245245
@Override
246246
public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) {
247-
PointerProperties[] pointerProperties =
248-
parsePointerPropertiesList(touch.rawPointerPropertiesList)
249-
.toArray(new PointerProperties[touch.pointerCount]);
250-
PointerCoords[] pointerCoords =
251-
parsePointerCoordsList(touch.rawPointerCoords, getDisplayDensity())
252-
.toArray(new PointerCoords[touch.pointerCount]);
253-
254-
if (!vdControllers.containsKey(touch.viewId)) {
255-
throw new IllegalStateException(
256-
"Sending touch to an unknown view with id: " + touch.viewId);
257-
}
258-
259-
MotionEvent event =
260-
MotionEvent.obtain(
261-
touch.downTime.longValue(),
262-
touch.eventTime.longValue(),
263-
touch.action,
264-
touch.pointerCount,
265-
pointerProperties,
266-
pointerCoords,
267-
touch.metaState,
268-
touch.buttonState,
269-
touch.xPrecision,
270-
touch.yPrecision,
271-
touch.deviceId,
272-
touch.edgeFlags,
273-
touch.source,
274-
touch.flags);
275-
247+
final int viewId = touch.viewId;
248+
float density = context.getResources().getDisplayMetrics().density;
276249
ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH);
277-
vdControllers.get(touch.viewId).dispatchTouchEvent(event);
250+
final MotionEvent event = toMotionEvent(density, touch);
251+
if (vdControllers.containsKey(viewId)) {
252+
vdControllers.get(touch.viewId).dispatchTouchEvent(event);
253+
} else if (platformViews.get(viewId) != null) {
254+
View view = platformViews.get(touch.viewId);
255+
view.dispatchTouchEvent(event);
256+
} else {
257+
throw new IllegalStateException("Sending touch to an unknown view with id: " + viewId);
258+
}
278259
}
279260

280261
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@@ -317,6 +298,32 @@ private void ensureValidAndroidVersion(int minSdkVersion) {
317298
}
318299
};
319300

301+
private static MotionEvent toMotionEvent(
302+
float density, PlatformViewsChannel.PlatformViewTouch touch) {
303+
PointerProperties[] pointerProperties =
304+
parsePointerPropertiesList(touch.rawPointerPropertiesList)
305+
.toArray(new PointerProperties[touch.pointerCount]);
306+
PointerCoords[] pointerCoords =
307+
parsePointerCoordsList(touch.rawPointerCoords, density)
308+
.toArray(new PointerCoords[touch.pointerCount]);
309+
310+
return MotionEvent.obtain(
311+
touch.downTime.longValue(),
312+
touch.eventTime.longValue(),
313+
touch.action,
314+
touch.pointerCount,
315+
pointerProperties,
316+
pointerCoords,
317+
touch.metaState,
318+
touch.buttonState,
319+
touch.xPrecision,
320+
touch.yPrecision,
321+
touch.deviceId,
322+
touch.edgeFlags,
323+
touch.source,
324+
touch.flags);
325+
}
326+
320327
public PlatformViewsController() {
321328
registry = new PlatformViewRegistryImpl();
322329
vdControllers = new HashMap<>();

0 commit comments

Comments
 (0)