Update Android Touch events

Summary: public

This moves Android touch events to parity with iOS.  The locationX,Y value passed to js now is view relative to the view that is handling the touch.
The pageX,Y is now relative to the root view.

Reviewed By: andreicoman11

Differential Revision: D2670028

fb-gh-sync-id: 5438640d6c78633629b9a308a59cc306bb07815e
This commit is contained in:
Dave Miller
2015-11-23 08:00:46 -08:00
committed by facebook-github-bot-3
parent e0d53e1c48
commit 0c2ee5d480
4 changed files with 125 additions and 35 deletions

View File

@@ -61,6 +61,9 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
private @Nullable String mJSModuleName; private @Nullable String mJSModuleName;
private @Nullable Bundle mLaunchOptions; private @Nullable Bundle mLaunchOptions;
private int mTargetTag = -1; private int mTargetTag = -1;
// Note mTargetCoordinates are Y,X
// TODO: t9136625 tracks moving to X,Y
private final float[] mTargetCoordinates = new float[2];
private boolean mChildIsHandlingNativeGesture = false; private boolean mChildIsHandlingNativeGesture = false;
private boolean mWasMeasured = false; private boolean mWasMeasured = false;
private boolean mAttachScheduled = false; private boolean mAttachScheduled = false;
@@ -143,9 +146,19 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
// {@link #findTargetTagForTouch} to find react view ID that will be responsible for handling // {@link #findTargetTagForTouch} to find react view ID that will be responsible for handling
// this gesture // this gesture
mChildIsHandlingNativeGesture = false; mChildIsHandlingNativeGesture = false;
mTargetTag = TouchTargetHelper.findTargetTagForTouch(ev.getY(), ev.getX(), this); mTargetTag = TouchTargetHelper.findTargetTagAndCoordinatesForTouch(
ev.getY(),
ev.getX(),
this,
mTargetCoordinates);
eventDispatcher.dispatchEvent( eventDispatcher.dispatchEvent(
TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.START, ev)); TouchEvent.obtain(
mTargetTag,
SystemClock.uptimeMillis(),
TouchEventType.START,
ev,
mTargetCoordinates[1],
mTargetCoordinates[0]));
} else if (mChildIsHandlingNativeGesture) { } else if (mChildIsHandlingNativeGesture) {
// If the touch was intercepted by a child, we've already sent a cancel event to JS for this // If the touch was intercepted by a child, we've already sent a cancel event to JS for this
// gesture, so we shouldn't send any more touches related to it. // gesture, so we shouldn't send any more touches related to it.
@@ -161,20 +174,44 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
// End of the gesture. We reset target tag to -1 and expect no further event associated with // End of the gesture. We reset target tag to -1 and expect no further event associated with
// this gesture. // this gesture.
eventDispatcher.dispatchEvent( eventDispatcher.dispatchEvent(
TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.END, ev)); TouchEvent.obtain(
mTargetTag,
SystemClock.uptimeMillis(),
TouchEventType.END,
ev,
mTargetCoordinates[1],
mTargetCoordinates[0]));
mTargetTag = -1; mTargetTag = -1;
} else if (action == MotionEvent.ACTION_MOVE) { } else if (action == MotionEvent.ACTION_MOVE) {
// Update pointer position for current gesture // Update pointer position for current gesture
eventDispatcher.dispatchEvent( eventDispatcher.dispatchEvent(
TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.MOVE, ev)); TouchEvent.obtain(
mTargetTag,
SystemClock.uptimeMillis(),
TouchEventType.MOVE,
ev,
mTargetCoordinates[1],
mTargetCoordinates[0]));
} else if (action == MotionEvent.ACTION_POINTER_DOWN) { } else if (action == MotionEvent.ACTION_POINTER_DOWN) {
// New pointer goes down, this can only happen after ACTION_DOWN is sent for the first pointer // New pointer goes down, this can only happen after ACTION_DOWN is sent for the first pointer
eventDispatcher.dispatchEvent( eventDispatcher.dispatchEvent(
TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.START, ev)); TouchEvent.obtain(
mTargetTag,
SystemClock.uptimeMillis(),
TouchEventType.START,
ev,
mTargetCoordinates[1],
mTargetCoordinates[0]));
} else if (action == MotionEvent.ACTION_POINTER_UP) { } else if (action == MotionEvent.ACTION_POINTER_UP) {
// Exactly onw of the pointers goes up // Exactly onw of the pointers goes up
eventDispatcher.dispatchEvent( eventDispatcher.dispatchEvent(
TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.END, ev)); TouchEvent.obtain(
mTargetTag,
SystemClock.uptimeMillis(),
TouchEventType.END,
ev,
mTargetCoordinates[1],
mTargetCoordinates[0]));
} else if (action == MotionEvent.ACTION_CANCEL) { } else if (action == MotionEvent.ACTION_CANCEL) {
dispatchCancelEvent(ev); dispatchCancelEvent(ev);
mTargetTag = -1; mTargetTag = -1;
@@ -223,7 +260,9 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
mTargetTag, mTargetTag,
SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
TouchEventType.CANCEL, TouchEventType.CANCEL,
androidEvent)); androidEvent,
mTargetCoordinates[1],
mTargetCoordinates[0]));
} }
@Override @Override

View File

@@ -39,17 +39,34 @@ public class TouchTargetHelper {
float eventY, float eventY,
float eventX, float eventX,
ViewGroup viewGroup) { ViewGroup viewGroup) {
return findTargetTagAndCoordinatesForTouch(eventY, eventX, viewGroup, mEventCoords);
}
/**
* Find touch event target view within the provided container given the coordinates provided
* via {@link MotionEvent}.
*
* @param eventY the Y screen coordinate of the touch location
* @param eventX the X screen coordinate of the touch location
* @param viewGroup the container view to traverse
* @param viewCoords an out parameter that will return the Y,X value in the target view
* @return the react tag ID of the child view that should handle the event
*/
public static int findTargetTagAndCoordinatesForTouch(
float eventY,
float eventX,
ViewGroup viewGroup,
float[] viewCoords) {
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
int targetTag = viewGroup.getId(); int targetTag = viewGroup.getId();
// Store eventCoords in array so that they are modified to be relative to the targetView found. // Store eventCoords in array so that they are modified to be relative to the targetView found.
float[] eventCoords = mEventCoords; viewCoords[0] = eventY;
eventCoords[0] = eventY; viewCoords[1] = eventX;
eventCoords[1] = eventX; View nativeTargetView = findTouchTargetView(viewCoords, viewGroup);
View nativeTargetView = findTouchTargetView(eventCoords, viewGroup);
if (nativeTargetView != null) { if (nativeTargetView != null) {
View reactTargetView = findClosestReactAncestor(nativeTargetView); View reactTargetView = findClosestReactAncestor(nativeTargetView);
if (reactTargetView != null) { if (reactTargetView != null) {
targetTag = getTouchTargetForView(reactTargetView, eventCoords[0], eventCoords[1]); targetTag = getTouchTargetForView(reactTargetView, viewCoords[0], viewCoords[1]);
} }
} }
return targetTag; return targetTag;

View File

@@ -35,12 +35,14 @@ public class TouchEvent extends Event<TouchEvent> {
int viewTag, int viewTag,
long timestampMs, long timestampMs,
TouchEventType touchEventType, TouchEventType touchEventType,
MotionEvent motionEventToCopy) { MotionEvent motionEventToCopy,
float viewX,
float viewY) {
TouchEvent event = EVENTS_POOL.acquire(); TouchEvent event = EVENTS_POOL.acquire();
if (event == null) { if (event == null) {
event = new TouchEvent(); event = new TouchEvent();
} }
event.init(viewTag, timestampMs, touchEventType, motionEventToCopy); event.init(viewTag, timestampMs, touchEventType, motionEventToCopy, viewX, viewY);
return event; return event;
} }
@@ -48,6 +50,10 @@ public class TouchEvent extends Event<TouchEvent> {
private @Nullable TouchEventType mTouchEventType; private @Nullable TouchEventType mTouchEventType;
private short mCoalescingKey; private short mCoalescingKey;
// Coordinates in the ViewTag coordinate space
private float mViewX;
private float mViewY;
private TouchEvent() { private TouchEvent() {
} }
@@ -55,7 +61,9 @@ public class TouchEvent extends Event<TouchEvent> {
int viewTag, int viewTag,
long timestampMs, long timestampMs,
TouchEventType touchEventType, TouchEventType touchEventType,
MotionEvent motionEventToCopy) { MotionEvent motionEventToCopy,
float viewX,
float viewY) {
super.init(viewTag, timestampMs); super.init(viewTag, timestampMs);
short coalescingKey = 0; short coalescingKey = 0;
@@ -84,6 +92,8 @@ public class TouchEvent extends Event<TouchEvent> {
mTouchEventType = touchEventType; mTouchEventType = touchEventType;
mMotionEvent = MotionEvent.obtain(motionEventToCopy); mMotionEvent = MotionEvent.obtain(motionEventToCopy);
mCoalescingKey = coalescingKey; mCoalescingKey = coalescingKey;
mViewX = viewX;
mViewY = viewY;
} }
@Override @Override
@@ -126,6 +136,19 @@ public class TouchEvent extends Event<TouchEvent> {
rctEventEmitter, rctEventEmitter,
Assertions.assertNotNull(mTouchEventType), Assertions.assertNotNull(mTouchEventType),
getViewTag(), getViewTag(),
Assertions.assertNotNull(mMotionEvent)); this);
}
public MotionEvent getMotionEvent() {
Assertions.assertNotNull(mMotionEvent);
return mMotionEvent;
}
public float getViewX() {
return mViewX;
}
public float getViewY() {
return mViewY;
} }
} }

View File

@@ -27,8 +27,6 @@ import com.facebook.react.uimanager.PixelUtil;
private static final String TIMESTAMP_KEY = "timeStamp"; private static final String TIMESTAMP_KEY = "timeStamp";
private static final String POINTER_IDENTIFIER_KEY = "identifier"; private static final String POINTER_IDENTIFIER_KEY = "identifier";
// TODO(7351435): remove when we standardize touchEvent payload, since iOS uses locationXYZ but
// Android uses pageXYZ. As a temporary solution, Android currently sends both.
private static final String LOCATION_X_KEY = "locationX"; private static final String LOCATION_X_KEY = "locationX";
private static final String LOCATION_Y_KEY = "locationY"; private static final String LOCATION_Y_KEY = "locationY";
@@ -37,23 +35,35 @@ import com.facebook.react.uimanager.PixelUtil;
* given {@param event} instance. This method use {@param reactTarget} parameter to set as a * given {@param event} instance. This method use {@param reactTarget} parameter to set as a
* target view id associated with current gesture. * target view id associated with current gesture.
*/ */
private static WritableArray createsPointersArray(int reactTarget, MotionEvent event) { private static WritableArray createsPointersArray(int reactTarget, TouchEvent event) {
WritableArray touches = Arguments.createArray(); WritableArray touches = Arguments.createArray();
MotionEvent motionEvent = event.getMotionEvent();
// Calculate raw-to-relative offset as getRawX() and getRawY() can only return values for the // Calculate the coordinates for the target view.
// pointer at index 0. We use those value to calculate "raw" coordinates for other pointers // The MotionEvent contains the X,Y of the touch in the coordinate space of the root view
float offsetX = event.getRawX() - event.getX(); // The TouchEvent contains the X,Y of the touch in the coordinate space of the target view
float offsetY = event.getRawY() - event.getY(); // Subtracting them allows us to get the coordinates of the target view's top left corner
// We then use this when computing the view specific touches below
// Since only one view is actually handling even multiple touches, the values are all relative
// to this one target view.
float targetViewCoordinateX = motionEvent.getX() - event.getViewX();
float targetViewCoordinateY = motionEvent.getY() - event.getViewY();
for (int index = 0; index < event.getPointerCount(); index++) { for (int index = 0; index < motionEvent.getPointerCount(); index++) {
WritableMap touch = Arguments.createMap(); WritableMap touch = Arguments.createMap();
touch.putDouble(PAGE_X_KEY, PixelUtil.toDIPFromPixel(event.getX(index) + offsetX)); // pageX,Y values are relative to the RootReactView
touch.putDouble(PAGE_Y_KEY, PixelUtil.toDIPFromPixel(event.getY(index) + offsetY)); // the motionEvent already contains coordinates in that view
touch.putDouble(LOCATION_X_KEY, PixelUtil.toDIPFromPixel(event.getX(index))); touch.putDouble(PAGE_X_KEY, PixelUtil.toDIPFromPixel(motionEvent.getX(index)));
touch.putDouble(LOCATION_Y_KEY, PixelUtil.toDIPFromPixel(event.getY(index))); touch.putDouble(PAGE_Y_KEY, PixelUtil.toDIPFromPixel(motionEvent.getY(index)));
// locationX,Y values are relative to the target view
// To compute the values for the view, we subtract that views location from the event X,Y
float locationX = motionEvent.getX(index) - targetViewCoordinateX;
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_KEY, reactTarget); touch.putInt(TARGET_KEY, reactTarget);
touch.putDouble(TIMESTAMP_KEY, event.getEventTime()); touch.putDouble(TIMESTAMP_KEY, motionEvent.getEventTime());
touch.putDouble(POINTER_IDENTIFIER_KEY, event.getPointerId(index)); touch.putDouble(POINTER_IDENTIFIER_KEY, motionEvent.getPointerId(index));
touches.pushMap(touch); touches.pushMap(touch);
} }
@@ -67,25 +77,26 @@ import com.facebook.react.uimanager.PixelUtil;
* @param rctEventEmitter Event emitter used to execute JS module call * @param rctEventEmitter Event emitter used to execute JS module call
* @param type type of the touch event (see {@link TouchEventType}) * @param type type of the touch event (see {@link TouchEventType})
* @param reactTarget target view react id associated with this gesture * @param reactTarget target view react id associated with this gesture
* @param androidMotionEvent native touch event to read pointers count and coordinates from * @param touchEvent native touch event to read pointers count and coordinates from
*/ */
public static void sendTouchEvent( public static void sendTouchEvent(
RCTEventEmitter rctEventEmitter, RCTEventEmitter rctEventEmitter,
TouchEventType type, TouchEventType type,
int reactTarget, int reactTarget,
MotionEvent androidMotionEvent) { TouchEvent touchEvent) {
WritableArray pointers = createsPointersArray(reactTarget, androidMotionEvent); WritableArray pointers = createsPointersArray(reactTarget, touchEvent);
MotionEvent motionEvent = touchEvent.getMotionEvent();
// For START and END events send only index of the pointer that is associated with that event // For START and END events send only index of the pointer that is associated with that event
// For MOVE and CANCEL events 'changedIndices' array should contain all the pointers indices // For MOVE and CANCEL events 'changedIndices' array should contain all the pointers indices
WritableArray changedIndices = Arguments.createArray(); WritableArray changedIndices = Arguments.createArray();
if (type == TouchEventType.MOVE || type == TouchEventType.CANCEL) { if (type == TouchEventType.MOVE || type == TouchEventType.CANCEL) {
for (int i = 0; i < androidMotionEvent.getPointerCount(); i++) { for (int i = 0; i < motionEvent.getPointerCount(); i++) {
changedIndices.pushInt(i); changedIndices.pushInt(i);
} }
} else if (type == TouchEventType.START || type == TouchEventType.END) { } else if (type == TouchEventType.START || type == TouchEventType.END) {
changedIndices.pushInt(androidMotionEvent.getActionIndex()); changedIndices.pushInt(motionEvent.getActionIndex());
} else { } else {
throw new RuntimeException("Unknown touch type: " + type); throw new RuntimeException("Unknown touch type: " + type);
} }