Recycle touch events.

Reviewed By: andreicoman11

Differential Revision: D2590149

fb-gh-sync-id: 97c60e12385526097431a192b692e2f92813722d
This commit is contained in:
Krzysztof Magiera 2015-10-28 02:11:09 -07:00 committed by facebook-github-bot-9
parent 3625e074e5
commit 46e67ef428
2 changed files with 38 additions and 16 deletions

View File

@ -145,7 +145,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
mChildIsHandlingNativeGesture = false; mChildIsHandlingNativeGesture = false;
mTargetTag = TouchTargetHelper.findTargetTagForTouch(ev.getY(), ev.getX(), this); mTargetTag = TouchTargetHelper.findTargetTagForTouch(ev.getY(), ev.getX(), this);
eventDispatcher.dispatchEvent( eventDispatcher.dispatchEvent(
new TouchEvent(mTargetTag, SystemClock.uptimeMillis(),TouchEventType.START, ev)); TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.START, ev));
} 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 +161,20 @@ 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(
new TouchEvent(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.END, ev)); TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.END, ev));
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(
new TouchEvent(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.MOVE, ev)); TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.MOVE, ev));
} 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(
new TouchEvent(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.START, ev)); TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.START, ev));
} 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(
new TouchEvent(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.END, ev)); TouchEvent.obtain(mTargetTag, SystemClock.uptimeMillis(), TouchEventType.END, ev));
} else if (action == MotionEvent.ACTION_CANCEL) { } else if (action == MotionEvent.ACTION_CANCEL) {
dispatchCancelEvent(ev); dispatchCancelEvent(ev);
mTargetTag = -1; mTargetTag = -1;
@ -219,7 +219,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
!mChildIsHandlingNativeGesture, !mChildIsHandlingNativeGesture,
"Expected to not have already sent a cancel for this gesture"); "Expected to not have already sent a cancel for this gesture");
Assertions.assertNotNull(eventDispatcher).dispatchEvent( Assertions.assertNotNull(eventDispatcher).dispatchEvent(
new TouchEvent( TouchEvent.obtain(
mTargetTag, mTargetTag,
SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
TouchEventType.CANCEL, TouchEventType.CANCEL,

View File

@ -9,6 +9,7 @@
package com.facebook.react.uimanager.events; package com.facebook.react.uimanager.events;
import android.support.v4.util.Pools;
import android.view.MotionEvent; import android.view.MotionEvent;
/** /**
@ -21,16 +22,42 @@ import android.view.MotionEvent;
*/ */
public class TouchEvent extends Event<TouchEvent> { public class TouchEvent extends Event<TouchEvent> {
private final MotionEvent mMotionEvent; private static final Pools.SynchronizedPool<TouchEvent> EVENTS_POOL =
private final TouchEventType mTouchEventType; new Pools.SynchronizedPool<>(3);
private final short mCoalescingKey;
public TouchEvent( public static TouchEvent obtain(
int viewTag, int viewTag,
long timestampMs, long timestampMs,
TouchEventType touchEventType, TouchEventType touchEventType,
MotionEvent motionEventToCopy) { MotionEvent motionEventToCopy) {
super(viewTag, timestampMs); TouchEvent event = EVENTS_POOL.acquire();
if (event == null) {
event = new TouchEvent();
}
event.init(viewTag, timestampMs, touchEventType, motionEventToCopy);
return event;
}
private MotionEvent mMotionEvent;
private 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; mTouchEventType = touchEventType;
mMotionEvent = MotionEvent.obtain(motionEventToCopy); mMotionEvent = MotionEvent.obtain(motionEventToCopy);
@ -94,9 +121,4 @@ public class TouchEvent extends Event<TouchEvent> {
getViewTag(), getViewTag(),
mMotionEvent); mMotionEvent);
} }
@Override
public void onDispose() {
mMotionEvent.recycle();
}
} }