Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 70de32b

Browse files
authored
Fix non-vd android platform view input event offsets (#52532)
Fixes flutter/flutter#146570, which tracks a regression from #49268 regarding platform view inputs in some specific cases. This PR translates the input event location to be the same as the location we calculated before #49268, returning to the previous behavior, while maintaining the input event's verified status (I checked this manually with the `InputManager`). Tested manually with the reproduction in the linked issue. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 9d631c5 commit 70de32b

3 files changed

Lines changed: 102 additions & 37 deletions

File tree

‎shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,12 @@ public static class PlatformViewTouch {
501501
public final int action;
502502
/** The number of pointers (e.g, fingers) involved in the touch event. */
503503
public final int pointerCount;
504-
/** Properties for each pointer, encoded in a raw format. */
504+
/**
505+
* Properties for each pointer, encoded in a raw format.
506+
* Expected to be formatted as a List[List[Integer]], where each inner list has two items:
507+
* - An id, at index 0, corresponding to {@link android.view.MotionEvent.PointerProperties#id}
508+
* - A tool type, at index 1, corresponding to {@link android.view.MotionEvent.PointerProperties#toolType}.
509+
* */
505510
@NonNull public final Object rawPointerPropertiesList;
506511
/** Coordinates for each pointer, encoded in a raw format. */
507512
@NonNull public final Object rawPointerCoords;

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
package io.flutter.plugin.platform;
66

7-
import static android.view.MotionEvent.PointerCoords;
8-
import static android.view.MotionEvent.PointerProperties;
97
import static io.flutter.Build.API_LEVELS;
108

119
import android.annotation.TargetApi;
@@ -668,32 +666,53 @@ public long configureForTextureLayerComposition(
668666
return textureId;
669667
}
670668

669+
/**
670+
* Translates an original touch event to have the same locations as the ones that Flutter
671+
* calculates (because original + flutter's - original = flutter's).
672+
*
673+
* @param originalEvent The saved original input event.
674+
* @param pointerCoords The coordinates that Flutter thinks the touch is happening at.
675+
*/
676+
private static void translateMotionEvent(
677+
MotionEvent originalEvent, PointerCoords[] pointerCoords) {
678+
if (pointerCoords.length < 1) {
679+
return;
680+
}
681+
682+
float xOffset = pointerCoords[0].x - originalEvent.getX();
683+
float yOffset = pointerCoords[0].y - originalEvent.getY();
684+
685+
originalEvent.offsetLocation(xOffset, yOffset);
686+
}
687+
671688
@VisibleForTesting
672689
public MotionEvent toMotionEvent(
673690
float density, PlatformViewsChannel.PlatformViewTouch touch, boolean usingVirtualDiplay) {
674691
MotionEventTracker.MotionEventId motionEventId =
675692
MotionEventTracker.MotionEventId.from(touch.motionEventId);
676693
MotionEvent trackedEvent = motionEventTracker.pop(motionEventId);
677694

695+
// Pointer coordinates in the tracked events are global to FlutterView
696+
// The framework converts them to be local to a widget, given that
697+
// motion events operate on local coords, we need to replace these in the tracked
698+
// event with their local counterparts.
699+
// Compute this early so it can be used as input to translateNonVirtualDisplayMotionEvent.
700+
PointerCoords[] pointerCoords =
701+
parsePointerCoordsList(touch.rawPointerCoords, density)
702+
.toArray(new PointerCoords[touch.pointerCount]);
703+
678704
if (!usingVirtualDiplay && trackedEvent != null) {
679-
// We have the original event, deliver it as it will pass the verifiable
705+
// We have the original event, deliver it after offsetting as it will pass the verifiable
680706
// input check.
707+
translateMotionEvent(trackedEvent, pointerCoords);
681708
return trackedEvent;
682709
}
683710
// We are in virtual display mode or don't have a reference to the original MotionEvent.
684711
// In this case we manually recreate a MotionEvent to be delivered. This MotionEvent
685712
// will fail the verifiable input check.
686-
687-
// Pointer coordinates in the tracked events are global to FlutterView
688-
// framework converts them to be local to a widget, given that
689-
// motion events operate on local coords, we need to replace these in the tracked
690-
// event with their local counterparts.
691713
PointerProperties[] pointerProperties =
692714
parsePointerPropertiesList(touch.rawPointerPropertiesList)
693715
.toArray(new PointerProperties[touch.pointerCount]);
694-
PointerCoords[] pointerCoords =
695-
parsePointerCoordsList(touch.rawPointerCoords, density)
696-
.toArray(new PointerCoords[touch.pointerCount]);
697716

698717
// TODO (kaushikiska) : warn that we are potentially using an untracked
699718
// event in the platform views.

‎shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java‎

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -361,34 +361,48 @@ public void itUsesActionEventTypeFromFrameworkEventAsActionChanged() {
361361
assertNotEquals(resolvedEvent.getAction(), frameWorkTouch.action);
362362
}
363363

364-
@Ignore
365-
@Test
366-
public void itUsesActionEventTypeFromMotionEventForHybridPlatformViews() {
367-
MotionEventTracker motionEventTracker = MotionEventTracker.getInstance();
368-
PlatformViewsController platformViewsController = new PlatformViewsController();
369-
370-
MotionEvent original =
371-
MotionEvent.obtain(
372-
100, // downTime
373-
100, // eventTime
374-
1, // action
375-
0, // x
376-
0, // y
377-
0 // metaState
378-
);
379-
380-
// track an event that will later get passed to us from framework
364+
private MotionEvent makePlatformViewTouchAndInvokeToMotionEvent(
365+
PlatformViewsController platformViewsController,
366+
MotionEventTracker motionEventTracker,
367+
MotionEvent original,
368+
boolean usingVirtualDisplays) {
381369
MotionEventTracker.MotionEventId motionEventId = motionEventTracker.track(original);
382370

383-
PlatformViewTouch frameWorkTouch =
371+
// Construct a PlatformViewTouch.rawPointerPropertiesList by doing the inverse of
372+
// PlatformViewsController.parsePointerPropertiesList.
373+
List<List<Integer>> pointerProperties =
374+
Arrays.asList(
375+
Arrays.asList(
376+
original.getPointerId(0),
377+
original.getToolType(0)
378+
)
379+
);
380+
// Construct a PlatformViewTouch.rawPointerCoords by doing the inverse of
381+
// PlatformViewsController.parsePointerCoordsList.
382+
List<List<Double>> pointerCoordinates =
383+
Arrays.asList(
384+
Arrays.asList(
385+
(double) original.getOrientation(),
386+
(double) original.getPressure(),
387+
(double) original.getSize(),
388+
(double) original.getToolMajor(),
389+
(double) original.getToolMinor(),
390+
(double) original.getTouchMajor(),
391+
(double) original.getTouchMinor(),
392+
(double) original.getX(),
393+
(double) original.getY()
394+
)
395+
);
396+
// Make a platform view touch from the motion event.
397+
PlatformViewTouch frameWorkTouchNonVd =
384398
new PlatformViewTouch(
385399
0, // viewId
386400
original.getDownTime(),
387401
original.getEventTime(),
388-
2, // action
402+
original.getAction(),
389403
1, // pointerCount
390-
Arrays.asList(Arrays.asList(0, 0)), // pointer properties
391-
Arrays.asList(Arrays.asList(0., 1., 2., 3., 4., 5., 6., 7., 8.)), // pointer coords
404+
pointerProperties, // pointer properties
405+
pointerCoordinates, // pointer coords
392406
original.getMetaState(),
393407
original.getButtonState(),
394408
original.getXPrecision(),
@@ -399,11 +413,38 @@ public void itUsesActionEventTypeFromMotionEventForHybridPlatformViews() {
399413
original.getFlags(),
400414
motionEventId.getId());
401415

402-
MotionEvent resolvedEvent =
403-
platformViewsController.toMotionEvent(
404-
/*density=*/ 1, frameWorkTouch, /*usingVirtualDisplay=*/ false);
416+
return platformViewsController.toMotionEvent(
417+
1, // density
418+
frameWorkTouchNonVd,
419+
usingVirtualDisplays);
420+
}
405421

406-
assertEquals(resolvedEvent.getAction(), frameWorkTouch.action);
422+
@Test
423+
public void toMotionEvent_returnsSameCoordsForVdAndNonVd() {
424+
MotionEventTracker motionEventTracker = MotionEventTracker.getInstance();
425+
PlatformViewsController platformViewsController = new PlatformViewsController();
426+
427+
MotionEvent original =
428+
MotionEvent.obtain(
429+
10, // downTime
430+
10, // eventTime
431+
261, // action
432+
1, // x
433+
1, // y
434+
0 // metaState
435+
);
436+
437+
MotionEvent resolvedNonVdEvent =
438+
makePlatformViewTouchAndInvokeToMotionEvent(
439+
platformViewsController, motionEventTracker, original, false);
440+
441+
MotionEvent resolvedVdEvent =
442+
makePlatformViewTouchAndInvokeToMotionEvent(
443+
platformViewsController, motionEventTracker, original, true);
444+
445+
assertEquals(resolvedVdEvent.getEventTime(), resolvedNonVdEvent.getEventTime());
446+
assertEquals(resolvedVdEvent.getX(), resolvedNonVdEvent.getX(), 0.001f);
447+
assertEquals(resolvedVdEvent.getY(), resolvedNonVdEvent.getY(), 0.001f);
407448
}
408449

409450
@Test

0 commit comments

Comments
 (0)