Skip to content

Commit c1283d1

Browse files
vamclaude
andcommitted
Fix pointer id/coordinate mismatch in Android platform view multi-touch
_AndroidMotionEventConverter.toAndroidMotionEvent built the pointer list from pointerPositions.keys.toList(), which iterates in Flutter pointer insertion order. The engine's PlatformViewsController pairs the coordinates sent here with the original MotionEvent's pointers by array position, assuming ascending Android pointer id order. Once a released Android pointer id is recycled by a new touch, insertion order and id order diverge, corrupting the action index and pairing each pointer with the wrong coordinates. Sort the pointer list by Android pointer id before building the action index and motion event arrays so both sides agree on what the array order means. Fixes flutter#191105 Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 40b3290 commit c1283d1

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,18 @@ 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(
640+
(int a, int b) => (pointerProperties[a]?.id ?? 0).compareTo(pointerProperties[b]?.id ?? 0),
641+
);
631642
final int pointerIdx = pointers.indexOf(event.pointer);
632643
final int numPointers = pointers.length;
633644

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,96 @@ 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(
562+
downArgs[kIndexPointerProperties],
563+
<List<int>>[
564+
<int>[0, AndroidPointerProperties.kToolTypeFinger],
565+
<int>[1, AndroidPointerProperties.kToolTypeFinger],
566+
],
567+
);
568+
// Android pointer id 0 is Flutter pointer 3, id 1 is Flutter pointer 2.
569+
expect(xOf(downCall), <double>[30, 20]);
570+
571+
// The engine splits one Android move across its pointers in Android pointer
572+
// id order, flagging each with the original pointer count. The converter must
573+
// send a single MotionEvent once the last of them has arrived.
574+
const kPointerDataFlagMultiple = 2;
575+
const int platformData = kPointerDataFlagMultiple | (2 << 8);
576+
log.clear();
577+
await viewController.dispatchPointerEvent(
578+
const PointerMoveEvent(pointer: 3, position: Offset(31, 31), platformData: platformData),
579+
);
580+
await viewController.dispatchPointerEvent(
581+
const PointerMoveEvent(pointer: 2, position: Offset(21, 21), platformData: platformData),
582+
);
583+
584+
final MethodCall moveCall = log.singleWhere((MethodCall call) => call.method == 'touch');
585+
final moveArgs = moveCall.arguments as List<dynamic>;
586+
expect(moveArgs[kIndexAction], AndroidViewController.kActionMove);
587+
expect(xOf(moveCall), <double>[31, 21]);
588+
});
499589
});
500590

501591
group('iOS', () {

0 commit comments

Comments
 (0)