Synthesize remove events on PointerChange.ACTION_UP and PointerChange.ACTION_POINTER_UP (flutter/engine#55157)
... when the input device type is touch. Fixes (partially) https://github.com/flutter/flutter/issues/154842 for touch events. Does not fix when using a stylus, that case will require a follow up PR. Without fix: https://github.com/user-attachments/assets/10a8ac73-7b27-498b-a76e-0f2cfc3050d7 With fix: https://github.com/user-attachments/assets/18656f3a-d45a-4b08-9227-cc0abe8c73f7 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -84,7 +84,7 @@ public class AndroidTouchProcessor {
|
||||
|
||||
// This value must match kPointerDataFieldCount in pointer_data.cc. (The
|
||||
// pointer_data.cc also lists other locations that must be kept consistent.)
|
||||
private static final int POINTER_DATA_FIELD_COUNT = 36;
|
||||
@VisibleForTesting static final int POINTER_DATA_FIELD_COUNT = 36;
|
||||
@VisibleForTesting static final int BYTES_PER_FIELD = 8;
|
||||
|
||||
// Default if context is null, chosen to ensure reasonable speed scrolling.
|
||||
@@ -138,15 +138,6 @@ public class AndroidTouchProcessor {
|
||||
* @return True if the event was handled.
|
||||
*/
|
||||
public boolean onTouchEvent(@NonNull MotionEvent event, @NonNull Matrix transformMatrix) {
|
||||
int pointerCount = event.getPointerCount();
|
||||
|
||||
// The following packing code must match the struct in pointer_data.h.
|
||||
|
||||
// Prepare a data packet of the appropriate size and order.
|
||||
ByteBuffer packet =
|
||||
ByteBuffer.allocateDirect(pointerCount * POINTER_DATA_FIELD_COUNT * BYTES_PER_FIELD);
|
||||
packet.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
int maskedAction = event.getActionMasked();
|
||||
int pointerChange = getPointerChangeForAction(event.getActionMasked());
|
||||
boolean updateForSinglePointer =
|
||||
@@ -155,6 +146,22 @@ public class AndroidTouchProcessor {
|
||||
!updateForSinglePointer
|
||||
&& (maskedAction == MotionEvent.ACTION_UP
|
||||
|| maskedAction == MotionEvent.ACTION_POINTER_UP);
|
||||
|
||||
int deviceType = getPointerDeviceTypeForToolType(event.getToolType(event.getActionIndex()));
|
||||
boolean shouldRemovePointer =
|
||||
updateForMultiplePointers && (deviceType == PointerDeviceKind.TOUCH);
|
||||
int originalPointerCount = event.getPointerCount();
|
||||
|
||||
// The following packing code must match the struct in pointer_data.h.
|
||||
|
||||
// Prepare a data packet of the appropriate size and order.
|
||||
// Allocate space for an additional pointer if this is an ACTION_UP or ACTION_POINTER_UP
|
||||
// event taken with device type touch, to handle the synthesized PointerChange.REMOVE event.
|
||||
int totalPointerCount = originalPointerCount + (shouldRemovePointer ? 1 : 0);
|
||||
ByteBuffer packet =
|
||||
ByteBuffer.allocateDirect(totalPointerCount * POINTER_DATA_FIELD_COUNT * BYTES_PER_FIELD);
|
||||
packet.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
if (updateForSinglePointer) {
|
||||
// ACTION_DOWN and ACTION_POINTER_DOWN always apply to a single pointer only.
|
||||
addPointerForIndex(event, event.getActionIndex(), pointerChange, 0, transformMatrix, packet);
|
||||
@@ -163,7 +170,7 @@ public class AndroidTouchProcessor {
|
||||
// We are converting these updates to move events here in order to preserve this data.
|
||||
// We also mark these events with a flag in order to help the framework reassemble
|
||||
// the original Android event later, should it need to forward it to a PlatformView.
|
||||
for (int p = 0; p < pointerCount; p++) {
|
||||
for (int p = 0; p < originalPointerCount; p++) {
|
||||
if (p != event.getActionIndex() && event.getToolType(p) == MotionEvent.TOOL_TYPE_FINGER) {
|
||||
addPointerForIndex(
|
||||
event, p, PointerChange.MOVE, POINTER_DATA_FLAG_BATCHED, transformMatrix, packet);
|
||||
@@ -172,11 +179,24 @@ public class AndroidTouchProcessor {
|
||||
// It's important that we're sending the UP event last. This allows PlatformView
|
||||
// to correctly batch everything back into the original Android event if needed.
|
||||
addPointerForIndex(event, event.getActionIndex(), pointerChange, 0, transformMatrix, packet);
|
||||
|
||||
if (shouldRemovePointer) {
|
||||
// Synthesizes remove events immediately after the UP event so that the touches
|
||||
// are divided into distinct segments, each beginning with a DOWN event and ending with an
|
||||
// UP event.
|
||||
// This approach makes sense since each segment can be considered a separate pointer,
|
||||
// and it prevents Flutter from generating hover events between these segments, which are
|
||||
// not applicable to touch screens, as hovering is not possible.
|
||||
// (Flutter will automatically synthesize an add event before the pointer makes its next
|
||||
// contact.)
|
||||
addPointerForIndex(
|
||||
event, event.getActionIndex(), PointerChange.REMOVE, 0, transformMatrix, packet);
|
||||
}
|
||||
} else {
|
||||
// ACTION_MOVE may not actually mean all pointers have moved
|
||||
// but it's the responsibility of a later part of the system to
|
||||
// ignore 0-deltas if desired.
|
||||
for (int p = 0; p < pointerCount; p++) {
|
||||
for (int p = 0; p < originalPointerCount; p++) {
|
||||
addPointerForIndex(event, p, pointerChange, 0, transformMatrix, packet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,15 +58,39 @@ public class AndroidTouchProcessorTest {
|
||||
}
|
||||
|
||||
private long readPointerChange(ByteBuffer buffer) {
|
||||
return buffer.getLong(2 * AndroidTouchProcessor.BYTES_PER_FIELD);
|
||||
return readPointerChangeForPointer(buffer, 0);
|
||||
}
|
||||
|
||||
private long readPointerChangeForPointer(ByteBuffer buffer, int pointerIndex) {
|
||||
return buffer.getLong(
|
||||
2 * AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
+ (AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
* AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT
|
||||
* pointerIndex));
|
||||
}
|
||||
|
||||
private long readPointerDeviceKind(ByteBuffer buffer) {
|
||||
return buffer.getLong(3 * AndroidTouchProcessor.BYTES_PER_FIELD);
|
||||
return readPointerDeviceKindForPointer(buffer, 0);
|
||||
}
|
||||
|
||||
private long readPointerDeviceKindForPointer(ByteBuffer buffer, int pointerIndex) {
|
||||
return buffer.getLong(
|
||||
3 * AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
+ (AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
* AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT
|
||||
* pointerIndex));
|
||||
}
|
||||
|
||||
private long readPointerSignalKind(ByteBuffer buffer) {
|
||||
return buffer.getLong(4 * AndroidTouchProcessor.BYTES_PER_FIELD);
|
||||
return readPointerSignalKindForPointer(buffer, 0);
|
||||
}
|
||||
|
||||
private long readPointerSignalKindForPointer(ByteBuffer buffer, int pointerIndex) {
|
||||
return buffer.getLong(
|
||||
4 * AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
+ (AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
* AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT
|
||||
* pointerIndex));
|
||||
}
|
||||
|
||||
private long readDevice(ByteBuffer buffer) {
|
||||
@@ -74,11 +98,27 @@ public class AndroidTouchProcessorTest {
|
||||
}
|
||||
|
||||
private double readPointerPhysicalX(ByteBuffer buffer) {
|
||||
return buffer.getDouble(7 * AndroidTouchProcessor.BYTES_PER_FIELD);
|
||||
return readPointerPhysicalXForPointer(buffer, 0);
|
||||
}
|
||||
|
||||
private double readPointerPhysicalXForPointer(ByteBuffer buffer, int pointerIndex) {
|
||||
return buffer.getDouble(
|
||||
7 * AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
+ (AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
* AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT
|
||||
* pointerIndex));
|
||||
}
|
||||
|
||||
private double readPointerPhysicalY(ByteBuffer buffer) {
|
||||
return buffer.getDouble(8 * AndroidTouchProcessor.BYTES_PER_FIELD);
|
||||
return readPointerPhysicalYForPointer(buffer, 0);
|
||||
}
|
||||
|
||||
private double readPointerPhysicalYForPointer(ByteBuffer buffer, int pointerIndex) {
|
||||
return buffer.getDouble(
|
||||
8 * AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
+ (AndroidTouchProcessor.BYTES_PER_FIELD
|
||||
* AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT
|
||||
* pointerIndex));
|
||||
}
|
||||
|
||||
private long readButtons(ByteBuffer buffer) {
|
||||
@@ -257,6 +297,18 @@ public class AndroidTouchProcessorTest {
|
||||
assertEquals(AndroidTouchProcessor.PointerSignalKind.NONE, readPointerSignalKind(packet));
|
||||
assertEquals(10.0, readPointerPhysicalX(packet));
|
||||
assertEquals(5.0, readPointerPhysicalY(packet));
|
||||
assertEquals(
|
||||
AndroidTouchProcessor.PointerChange.REMOVE, readPointerChangeForPointer(packet, 1));
|
||||
assertEquals(
|
||||
AndroidTouchProcessor.PointerDeviceKind.TOUCH, readPointerDeviceKindForPointer(packet, 1));
|
||||
assertEquals(
|
||||
AndroidTouchProcessor.PointerSignalKind.NONE, readPointerSignalKindForPointer(packet, 1));
|
||||
assertEquals(10.0, readPointerPhysicalXForPointer(packet, 1));
|
||||
assertEquals(5.0, readPointerPhysicalYForPointer(packet, 1));
|
||||
// Expect two pointer changes - the original UP and the synthesized REMOVE.
|
||||
assertEquals(
|
||||
AndroidTouchProcessor.BYTES_PER_FIELD * AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT * 2,
|
||||
packet.capacity());
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
@@ -341,6 +393,10 @@ public class AndroidTouchProcessorTest {
|
||||
assertEquals(AndroidTouchProcessor.PointerSignalKind.NONE, readPointerSignalKind(packet));
|
||||
assertEquals(10.0, readPointerPhysicalX(packet));
|
||||
assertEquals(5.0, readPointerPhysicalY(packet));
|
||||
// Ensure that we don't send a synthesized remove when using a mouse.
|
||||
assertEquals(
|
||||
AndroidTouchProcessor.BYTES_PER_FIELD * AndroidTouchProcessor.POINTER_DATA_FIELD_COUNT,
|
||||
packet.capacity());
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user