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
1 changed files with 29 additions and 22 deletions

View File

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