Skip to content

Commit b9db4f9

Browse files
kaluli123123vamgmackall
authored
Fix pointer id/coordinate mismatch in Android platform view multi-touch (flutter#191631)
## Summary - `_AndroidMotionEventConverter.toAndroidMotionEvent` built its pointer list from `pointerPositions.keys.toList()`, which iterates in Flutter pointer *insertion* order. - The engine (`PlatformViewsController.toMotionEvent` / `translateMotionEvent`, Java) pairs the coordinates sent from the framework with the original `MotionEvent`'s pointers *by array position*, on the assumption that this order matches ascending Android pointer id. - Those two orders agree until a released Android pointer id gets recycled by a new touch-down, at which point insertion order and id order diverge — corrupting the derived action index and pairing each pointer with the wrong coordinates (two fingers can end up reporting identical/reflected coordinates). - Fix: sort the pointer list by `AndroidPointerProperties.id` (the Android pointer id) before deriving the action index and building the `pointerProperties`/`pointerCoords` arrays, so both sides agree on what array order means. Fixes flutter#191105 ## Test plan - [x] Added a regression test in `packages/flutter/test/services/platform_views_test.dart` that recycles a released Android pointer id and asserts the resulting `MotionEvent`'s pointer order, action index, and coordinates are keyed by Android pointer id, not insertion order. - [x] Red/green verified: reverting the fix makes the new test fail with a corrupted action-index value; restoring the fix makes it pass. - [x] `flutter test packages/flutter/test/services/platform_views_test.dart` — 27/27 passing. - [x] `flutter analyze` on the two changed files — no issues. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: vam <[email protected]> Co-authored-by: Gray Mackall <[email protected]>
1 parent 5f6a879 commit b9db4f9

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎packages/flutter/lib/src/services/platform_views.dart‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,16 @@ class _AndroidMotionEventConverter {
627627
}
628628

629629
AndroidMotionEvent? toAndroidMotionEvent(PointerEvent event) {
630-
final List<int> pointers = pointerPositions.keys.toList();
630+
// Android orders the pointers within a MotionEvent by Android pointer id, and
631+
// the engine pairs the coordinates sent from here with the pointers of the
632+
// original MotionEvent by array position. `pointerPositions` is keyed by
633+
// Flutter pointer and iterates in insertion order, which stops matching
634+
// Android's order as soon as a released Android pointer id is recycled by
635+
// [handlePointerDownEvent]. The action index and the batching check below
636+
// are relative to this order as well.
637+
// See https://github.com/flutter/flutter/issues/191105.
638+
final List<int> pointers = pointerPositions.keys.toList()
639+
..sort((int a, int b) => pointerProperties[a]!.id.compareTo(pointerProperties[b]!.id));
631640
final int pointerIdx = pointers.indexOf(event.pointer);
632641
final int numPointers = pointers.length;
633642

‎packages/flutter/test/services/platform_views_test.dart‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,93 @@ void main() {
496496
final moveArgs = moveCalls.single.arguments as List<dynamic>;
497497
expect(moveArgs[kAndroidMotionEventListIndexPointerCount], equals(pointerCount));
498498
});
499+
500+
// Regression test for https://github.com/flutter/flutter/issues/191105.
501+
testWidgets('motion event converter orders pointers by Android pointer id', (
502+
WidgetTester tester,
503+
) async {
504+
final log = <MethodCall>[];
505+
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
506+
SystemChannels.platform_views,
507+
(MethodCall methodCall) async {
508+
log.add(methodCall);
509+
return null;
510+
},
511+
);
512+
513+
final AndroidViewController viewController = PlatformViewsService.initSurfaceAndroidView(
514+
id: 7,
515+
viewType: 'web',
516+
layoutDirection: TextDirection.ltr,
517+
);
518+
viewController.pointTransformer = (Offset offset) => offset;
519+
520+
// Indexes in the list returned by AndroidMotionEvent._asList.
521+
const kIndexAction = 3;
522+
const kIndexPointerCount = 4;
523+
const kIndexPointerProperties = 5;
524+
const kIndexPointerCoords = 6;
525+
// Index of x within the list returned by AndroidPointerCoords._asList.
526+
const kCoordsIndexX = 7;
527+
528+
List<double> xOf(MethodCall call) {
529+
final args = call.arguments as List<dynamic>;
530+
return (args[kIndexPointerCoords] as List<dynamic>)
531+
.map<double>((dynamic coords) => (coords as List<dynamic>)[kCoordsIndexX] as double)
532+
.toList();
533+
}
534+
535+
// Flutter pointer 1 takes Android pointer id 0, Flutter pointer 2 takes id 1.
536+
await viewController.dispatchPointerEvent(
537+
const PointerDownEvent(pointer: 1, position: Offset(10, 10)),
538+
);
539+
await viewController.dispatchPointerEvent(
540+
const PointerDownEvent(pointer: 2, position: Offset(20, 20)),
541+
);
542+
// Lifting Flutter pointer 1 releases Android pointer id 0 for reuse.
543+
await viewController.dispatchPointerEvent(
544+
const PointerUpEvent(pointer: 1, position: Offset(10, 10)),
545+
);
546+
log.clear();
547+
// Flutter pointer 3 is assigned the recycled Android pointer id 0, so it is
548+
// last in insertion order but first in Android pointer id order.
549+
await viewController.dispatchPointerEvent(
550+
const PointerDownEvent(pointer: 3, position: Offset(30, 30)),
551+
);
552+
553+
final MethodCall downCall = log.singleWhere((MethodCall call) => call.method == 'touch');
554+
final downArgs = downCall.arguments as List<dynamic>;
555+
expect(downArgs[kIndexPointerCount], 2);
556+
// The action index is the position of the pressed pointer in Android pointer id order.
557+
expect(
558+
downArgs[kIndexAction],
559+
AndroidViewController.pointerAction(0, AndroidViewController.kActionPointerDown),
560+
);
561+
expect(downArgs[kIndexPointerProperties], <List<int>>[
562+
<int>[0, AndroidPointerProperties.kToolTypeFinger],
563+
<int>[1, AndroidPointerProperties.kToolTypeFinger],
564+
]);
565+
// Android pointer id 0 is Flutter pointer 3, id 1 is Flutter pointer 2.
566+
expect(xOf(downCall), <double>[30, 20]);
567+
568+
// The engine splits one Android move across its pointers in Android pointer
569+
// id order, flagging each with the original pointer count. The converter must
570+
// send a single MotionEvent once the last of them has arrived.
571+
const kPointerDataFlagMultiple = 2;
572+
const int platformData = kPointerDataFlagMultiple | (2 << 8);
573+
log.clear();
574+
await viewController.dispatchPointerEvent(
575+
const PointerMoveEvent(pointer: 3, position: Offset(31, 31), platformData: platformData),
576+
);
577+
await viewController.dispatchPointerEvent(
578+
const PointerMoveEvent(pointer: 2, position: Offset(21, 21), platformData: platformData),
579+
);
580+
581+
final MethodCall moveCall = log.singleWhere((MethodCall call) => call.method == 'touch');
582+
final moveArgs = moveCall.arguments as List<dynamic>;
583+
expect(moveArgs[kIndexAction], AndroidViewController.kActionMove);
584+
expect(xOf(moveCall), <double>[31, 21]);
585+
});
499586
});
500587

501588
group('iOS', () {

0 commit comments

Comments
 (0)