16
16
/** Sends touch information from Android to Flutter in a format that Flutter understands. */
17
17
public class AndroidTouchProcessor {
18
18
19
+ private static final String TAG = "AndroidTouchProcessor" ;
19
20
// Must match the PointerChange enum in pointer.dart.
20
21
@ IntDef ({
21
22
PointerChange .CANCEL ,
@@ -186,29 +187,31 @@ public boolean onTouchEvent(@NonNull MotionEvent event, @NonNull Matrix transfor
186
187
public boolean onGenericMotionEvent (@ NonNull MotionEvent event ) {
187
188
// Method isFromSource is only available in API 18+ (Jelly Bean MR2)
188
189
// Mouse hover support is not implemented for API < 18.
190
+
189
191
boolean isPointerEvent =
190
192
Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR2
191
193
&& event .isFromSource (InputDevice .SOURCE_CLASS_POINTER );
194
+
192
195
boolean isMovementEvent =
193
196
(event .getActionMasked () == MotionEvent .ACTION_HOVER_MOVE
194
197
|| event .getActionMasked () == MotionEvent .ACTION_SCROLL );
195
- if (!isPointerEvent || !isMovementEvent ) {
196
- return false ;
197
- }
198
-
199
- int pointerChange = getPointerChangeForAction (event .getActionMasked ());
200
- ByteBuffer packet =
201
- ByteBuffer .allocateDirect (
202
- event .getPointerCount () * POINTER_DATA_FIELD_COUNT * BYTES_PER_FIELD );
203
- packet .order (ByteOrder .LITTLE_ENDIAN );
204
198
205
- // ACTION_HOVER_MOVE always applies to a single pointer only.
206
- addPointerForIndex (event , event .getActionIndex (), pointerChange , 0 , IDENTITY_TRANSFORM , packet );
207
- if (packet .position () % (POINTER_DATA_FIELD_COUNT * BYTES_PER_FIELD ) != 0 ) {
208
- throw new AssertionError ("Packet position is not on field boundary." );
199
+ if (isPointerEvent || isMovementEvent ) {
200
+ int pointerChange = getPointerChangeForAction (event .getActionMasked ());
201
+ ByteBuffer packet =
202
+ ByteBuffer .allocateDirect (
203
+ event .getPointerCount () * POINTER_DATA_FIELD_COUNT * BYTES_PER_FIELD );
204
+ packet .order (ByteOrder .LITTLE_ENDIAN );
205
+
206
+ // ACTION_HOVER_MOVE always applies to a single pointer only.
207
+ addPointerForIndex (event , event .getActionIndex (), pointerChange , 0 , IDENTITY_TRANSFORM , packet );
208
+ if (packet .position () % (POINTER_DATA_FIELD_COUNT * BYTES_PER_FIELD ) != 0 ) {
209
+ throw new AssertionError ("Packet position is not on field boundary." );
210
+ }
211
+ renderer .dispatchPointerDataPacket (packet , packet .position ());
212
+ return true ;
209
213
}
210
- renderer .dispatchPointerDataPacket (packet , packet .position ());
211
- return true ;
214
+ return false ;
212
215
}
213
216
214
217
// TODO(mattcarroll): consider creating a PointerPacket class instead of using a procedure that
@@ -252,6 +255,7 @@ private void addPointerForIndex(
252
255
buttons = 0 ;
253
256
}
254
257
258
+ int panZoomType = -1 ;
255
259
boolean isTrackpadPan = ongoingPans .containsKey (event .getPointerId (pointerIndex ));
256
260
257
261
int signalKind =
@@ -264,8 +268,9 @@ private void addPointerForIndex(
264
268
packet .putLong (motionEventId ); // motionEventId
265
269
packet .putLong (timeStamp ); // time_stamp
266
270
if (isTrackpadPan ) {
267
- packet .putLong (getPointerChangeForPanZoom (pointerChange )); // change
268
- packet .putLong (PointerDeviceKind .TRACKPAD ); // kind
271
+ panZoomType = getPointerChangeForPanZoom (pointerChange );
272
+ packet .putLong (panZoomType ); // change
273
+ packet .putLong (PointerDeviceKind .TRACKPAD ); // kind
269
274
} else {
270
275
packet .putLong (pointerChange ); // change
271
276
packet .putLong (pointerKind ); // kind
@@ -355,7 +360,7 @@ private void addPointerForIndex(
355
360
packet .putDouble (1.0 ); // scale
356
361
packet .putDouble (0.0 ); // rotation
357
362
358
- if (isTrackpadPan && getPointerChangeForPanZoom ( pointerChange ) == PointerChange .PAN_ZOOM_END ) {
363
+ if (isTrackpadPan && ( panZoomType == PointerChange .PAN_ZOOM_END ) ) {
359
364
ongoingPans .remove (event .getPointerId (pointerIndex ));
360
365
}
361
366
}
@@ -396,12 +401,12 @@ private int getPointerChangeForAction(int maskedAction) {
396
401
private int getPointerChangeForPanZoom (int pointerChange ) {
397
402
if (pointerChange == PointerChange .DOWN ) {
398
403
return PointerChange .PAN_ZOOM_START ;
399
- } else if (pointerChange == PointerChange .MOVE ) {
404
+ } else if (pointerChange == PointerChange .MOVE || pointerChange == PointerChange . HOVER ) {
400
405
return PointerChange .PAN_ZOOM_UPDATE ;
401
406
} else if (pointerChange == PointerChange .UP || pointerChange == PointerChange .CANCEL ) {
402
407
return PointerChange .PAN_ZOOM_END ;
403
408
}
404
- throw new AssertionError ( "Unexpected pointer change" ) ;
409
+ return - 1 ;
405
410
}
406
411
407
412
@ PointerDeviceKind
0 commit comments