Add @Nullables to TouchEvent.

Reviewed By: mkonicek

Differential Revision: D2591989

fb-gh-sync-id: 84f139b91ec21e656157a9c54cd616ee15673991
This commit is contained in:
Krzysztof Magiera
2015-10-28 14:50:21 -07:00
committed by facebook-github-bot-7
parent 03d7c7a6a1
commit ffc6db2f2c

View File

@@ -9,9 +9,13 @@
package com.facebook.react.uimanager.events; package com.facebook.react.uimanager.events;
import javax.annotation.Nullable;
import android.support.v4.util.Pools; import android.support.v4.util.Pools;
import android.view.MotionEvent; import android.view.MotionEvent;
import com.facebook.infer.annotation.Assertions;
/** /**
* An event representing the start, end or movement of a touch. Corresponds to a single * An event representing the start, end or movement of a touch. Corresponds to a single
* {@link android.view.MotionEvent}. * {@link android.view.MotionEvent}.
@@ -22,8 +26,10 @@ import android.view.MotionEvent;
*/ */
public class TouchEvent extends Event<TouchEvent> { public class TouchEvent extends Event<TouchEvent> {
private static final int TOUCH_EVENTS_POOL_SIZE = 3;
private static final Pools.SynchronizedPool<TouchEvent> EVENTS_POOL = private static final Pools.SynchronizedPool<TouchEvent> EVENTS_POOL =
new Pools.SynchronizedPool<>(3); new Pools.SynchronizedPool<>(TOUCH_EVENTS_POOL_SIZE);
public static TouchEvent obtain( public static TouchEvent obtain(
int viewTag, int viewTag,
@@ -38,57 +44,58 @@ public class TouchEvent extends Event<TouchEvent> {
return event; return event;
} }
private MotionEvent mMotionEvent; private @Nullable MotionEvent mMotionEvent;
private TouchEventType mTouchEventType; private @Nullable TouchEventType mTouchEventType;
private short mCoalescingKey; private short mCoalescingKey;
private TouchEvent() { private TouchEvent() {
} }
@Override
public void onDispose() {
mMotionEvent.recycle();
mMotionEvent = null;
EVENTS_POOL.release(this);
}
private void init( private void init(
int viewTag, int viewTag,
long timestampMs, long timestampMs,
TouchEventType touchEventType, TouchEventType touchEventType,
MotionEvent motionEventToCopy) { MotionEvent motionEventToCopy) {
super.init(viewTag, timestampMs); super.init(viewTag, timestampMs);
mTouchEventType = touchEventType;
mMotionEvent = MotionEvent.obtain(motionEventToCopy);
short coalescingKey = 0; short coalescingKey = 0;
int action = (mMotionEvent.getAction() & MotionEvent.ACTION_MASK); int action = (motionEventToCopy.getAction() & MotionEvent.ACTION_MASK);
switch (action) { switch (action) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
TouchEventCoalescingKeyHelper.addCoalescingKey(mMotionEvent.getDownTime()); TouchEventCoalescingKeyHelper.addCoalescingKey(motionEventToCopy.getDownTime());
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
TouchEventCoalescingKeyHelper.removeCoalescingKey(mMotionEvent.getDownTime()); TouchEventCoalescingKeyHelper.removeCoalescingKey(motionEventToCopy.getDownTime());
break; break;
case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_UP:
TouchEventCoalescingKeyHelper.incrementCoalescingKey(mMotionEvent.getDownTime()); TouchEventCoalescingKeyHelper.incrementCoalescingKey(motionEventToCopy.getDownTime());
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
coalescingKey = TouchEventCoalescingKeyHelper.getCoalescingKey(mMotionEvent.getDownTime()); coalescingKey =
TouchEventCoalescingKeyHelper.getCoalescingKey(motionEventToCopy.getDownTime());
break; break;
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
TouchEventCoalescingKeyHelper.removeCoalescingKey(mMotionEvent.getDownTime()); TouchEventCoalescingKeyHelper.removeCoalescingKey(motionEventToCopy.getDownTime());
break; break;
default: default:
throw new RuntimeException("Unhandled MotionEvent action: " + action); throw new RuntimeException("Unhandled MotionEvent action: " + action);
} }
mTouchEventType = touchEventType;
mMotionEvent = MotionEvent.obtain(motionEventToCopy);
mCoalescingKey = coalescingKey; mCoalescingKey = coalescingKey;
} }
@Override
public void onDispose() {
Assertions.assertNotNull(mMotionEvent).recycle();
mMotionEvent = null;
EVENTS_POOL.release(this);
}
@Override @Override
public String getEventName() { public String getEventName() {
return mTouchEventType.getJSEventName(); return Assertions.assertNotNull(mTouchEventType).getJSEventName();
} }
@Override @Override
@@ -96,7 +103,7 @@ public class TouchEvent extends Event<TouchEvent> {
// We can coalesce move events but not start/end events. Coalescing move events should probably // We can coalesce move events but not start/end events. Coalescing move events should probably
// append historical move data like MotionEvent batching does. This is left as an exercise for // append historical move data like MotionEvent batching does. This is left as an exercise for
// the reader. // the reader.
switch (mTouchEventType) { switch (Assertions.assertNotNull(mTouchEventType)) {
case START: case START:
case END: case END:
case CANCEL: case CANCEL:
@@ -117,8 +124,8 @@ public class TouchEvent extends Event<TouchEvent> {
public void dispatch(RCTEventEmitter rctEventEmitter) { public void dispatch(RCTEventEmitter rctEventEmitter) {
TouchesHelper.sendTouchEvent( TouchesHelper.sendTouchEvent(
rctEventEmitter, rctEventEmitter,
mTouchEventType, Assertions.assertNotNull(mTouchEventType),
getViewTag(), getViewTag(),
mMotionEvent); Assertions.assertNotNull(mMotionEvent));
} }
} }