From ffc6db2f2c31045fcb0ebb550ef5633a0dc44d23 Mon Sep 17 00:00:00 2001 From: Krzysztof Magiera Date: Wed, 28 Oct 2015 14:50:21 -0700 Subject: [PATCH] Add @Nullables to TouchEvent. Reviewed By: mkonicek Differential Revision: D2591989 fb-gh-sync-id: 84f139b91ec21e656157a9c54cd616ee15673991 --- .../react/uimanager/events/TouchEvent.java | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java index 10a4bda3a..407b8c691 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java @@ -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 { + private static final int TOUCH_EVENTS_POOL_SIZE = 3; + private static final Pools.SynchronizedPool 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 { 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 { // 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 { public void dispatch(RCTEventEmitter rctEventEmitter) { TouchesHelper.sendTouchEvent( rctEventEmitter, - mTouchEventType, + Assertions.assertNotNull(mTouchEventType), getViewTag(), - mMotionEvent); + Assertions.assertNotNull(mMotionEvent)); } }