Skip to content

Commit

Permalink
Retrieve touch dispatch information from event directly
Browse files Browse the repository at this point in the history
Summary:
Simplifies logic of touch dispatch by retrieving surface id and other require info from the event directly.

Changelog: [Android][Internal] - Simplify logic of dispatching touches

Reviewed By: cortinico

Differential Revision: D31583314

fbshipit-source-id: c6b6e131a759c2ebe0cf4441c3aeb1a8b9f5781e
  • Loading branch information
Andrei Shikov authored and facebook-github-bot committed Oct 14, 2021
1 parent 689bfed commit 53fd0f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void dispatchModern(RCTModernEventEmitter rctEventEmitter) {
if (getSurfaceId() != -1) {
WritableMap eventData = getEventData();
if (eventData != null) {
rctEventEmitter.receiveEvent(getSurfaceId(), getViewTag(), getEventName(), getEventData());
rctEventEmitter.receiveEvent(getSurfaceId(), getViewTag(), getEventName(), eventData);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,8 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
"Cannot dispatch a TouchEvent that has no MotionEvent; the TouchEvent has been recycled"));
return;
}
TouchesHelper.sendTouchEvent(
rctEventEmitter,
Assertions.assertNotNull(mTouchEventType),
getSurfaceId(),
getViewTag(),
this);

TouchesHelper.sendTouchEvent(rctEventEmitter, this);
}

@Override
Expand All @@ -210,6 +206,10 @@ private boolean hasMotionEvent() {
return mMotionEvent != null;
}

public TouchEventType getTouchEventType() {
return Assertions.assertNotNull(mTouchEventType);
}

public float getViewX() {
return mViewX;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public class TouchesHelper {
* given {@param event} instance. This method use {@param reactTarget} parameter to set as a
* target view id associated with current gesture.
*/
private static WritableArray createsPointersArray(
int surfaceId, int reactTarget, TouchEvent event) {
private static WritableArray createsPointersArray(TouchEvent event) {
WritableArray touches = Arguments.createArray();
MotionEvent motionEvent = event.getMotionEvent();

Expand All @@ -60,8 +59,8 @@ private static WritableArray createsPointersArray(
float locationY = motionEvent.getY(index) - targetViewCoordinateY;
touch.putDouble(LOCATION_X_KEY, PixelUtil.toDIPFromPixel(locationX));
touch.putDouble(LOCATION_Y_KEY, PixelUtil.toDIPFromPixel(locationY));
touch.putInt(TARGET_SURFACE_KEY, surfaceId);
touch.putInt(TARGET_KEY, reactTarget);
touch.putInt(TARGET_SURFACE_KEY, event.getSurfaceId());
touch.putInt(TARGET_KEY, event.getViewTag());
touch.putDouble(TIMESTAMP_KEY, event.getTimestampMs());
touch.putDouble(POINTER_IDENTIFIER_KEY, motionEvent.getPointerId(index));
touches.pushMap(touch);
Expand All @@ -75,17 +74,11 @@ private static WritableArray createsPointersArray(
* context}. Touch event can encode multiple concurrent touches (pointers).
*
* @param rctEventEmitter Event emitter used to execute JS module call
* @param type type of the touch event (see {@link TouchEventType})
* @param reactTarget target view react id associated with this gesture
* @param touchEvent native touch event to read pointers count and coordinates from
*/
public static void sendTouchEvent(
RCTEventEmitter rctEventEmitter,
TouchEventType type,
int surfaceId,
int reactTarget,
TouchEvent touchEvent) {
WritableArray pointers = createsPointersArray(surfaceId, reactTarget, touchEvent);
public static void sendTouchEvent(RCTEventEmitter rctEventEmitter, TouchEvent touchEvent) {
TouchEventType type = touchEvent.getTouchEventType();
WritableArray pointers = createsPointersArray(touchEvent);
MotionEvent motionEvent = touchEvent.getMotionEvent();

// For START and END events send only index of the pointer that is associated with that event
Expand Down

0 comments on commit 53fd0f4

Please sign in to comment.