optimize event dispatching code

This commit is contained in:
Horcrux
2016-08-12 14:37:38 +08:00
parent 1dbe898d88
commit a4c0c60b7f
@@ -55,6 +55,7 @@ public class RNSVGSvgView extends ViewGroup {
private @Nullable Bitmap mBitmap; private @Nullable Bitmap mBitmap;
private RCTEventEmitter mEventEmitter; private RCTEventEmitter mEventEmitter;
private EventDispatcher mEventDispatcher;
private RNSVGSvgViewShadowNode mSvgViewShadowNode; private RNSVGSvgViewShadowNode mSvgViewShadowNode;
private int mTargetTag; private int mTargetTag;
@@ -68,6 +69,7 @@ public class RNSVGSvgView extends ViewGroup {
public RNSVGSvgView(ReactContext reactContext) { public RNSVGSvgView(ReactContext reactContext) {
super(reactContext); super(reactContext);
mEventEmitter = reactContext.getJSModule(RCTEventEmitter.class); mEventEmitter = reactContext.getJSModule(RCTEventEmitter.class);
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
} }
public void setBitmap(Bitmap bitmap) { public void setBitmap(Bitmap bitmap) {
@@ -90,8 +92,7 @@ public class RNSVGSvgView extends ViewGroup {
public boolean dispatchTouchEvent(MotionEvent event) { public boolean dispatchTouchEvent(MotionEvent event) {
mTargetTag = mSvgViewShadowNode.hitTest(new Point((int) event.getX(), (int) event.getY()), this); mTargetTag = mSvgViewShadowNode.hitTest(new Point((int) event.getX(), (int) event.getY()), this);
if (mTargetTag != -1) { if (mTargetTag != -1) {
EventDispatcher eventDispatcher = ((ThemedReactContext) this.getContext()).getNativeModule(UIManagerModule.class).getEventDispatcher(); handleTouchEvent(event);
handleTouchEvent(event, eventDispatcher);
return true; return true;
} }
@@ -108,18 +109,22 @@ public class RNSVGSvgView extends ViewGroup {
shadowNode.setSvgView(this); shadowNode.setSvgView(this);
} }
public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) { private void dispatch(MotionEvent ev, TouchEventType type) {
mEventDispatcher.dispatchEvent(
TouchEvent.obtain(
mTargetTag,
SystemClock.nanoTime(),
type,
ev,
ev.getX(),
ev.getX(),
mTouchEventCoalescingKeyHelper));
}
public void handleTouchEvent(MotionEvent ev) {
int action = ev.getAction() & MotionEvent.ACTION_MASK; int action = ev.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) { if (action == MotionEvent.ACTION_DOWN) {
eventDispatcher.dispatchEvent( dispatch(ev, TouchEventType.START);
TouchEvent.obtain(
mTargetTag,
SystemClock.nanoTime(),
TouchEventType.START,
ev,
ev.getX(),
ev.getX(),
mTouchEventCoalescingKeyHelper));
} else if (mTargetTag == -1) { } else if (mTargetTag == -1) {
// All the subsequent action types are expected to be called after ACTION_DOWN thus target // All the subsequent action types are expected to be called after ACTION_DOWN thus target
// is supposed to be set for them. // is supposed to be set for them.
@@ -130,51 +135,20 @@ public class RNSVGSvgView extends ViewGroup {
} else if (action == MotionEvent.ACTION_UP) { } else if (action == MotionEvent.ACTION_UP) {
// 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( dispatch(ev, TouchEventType.END);
TouchEvent.obtain(
mTargetTag,
SystemClock.nanoTime(),
TouchEventType.END,
ev,
ev.getX(),
ev.getY(),
mTouchEventCoalescingKeyHelper));
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( dispatch(ev, TouchEventType.MOVE);
TouchEvent.obtain(
mTargetTag,
SystemClock.nanoTime(),
TouchEventType.MOVE,
ev,
ev.getX(),
ev.getY(),
mTouchEventCoalescingKeyHelper));
} 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( dispatch(ev, TouchEventType.START);
TouchEvent.obtain(
mTargetTag,
SystemClock.nanoTime(),
TouchEventType.START,
ev,
ev.getX(),
ev.getY(),
mTouchEventCoalescingKeyHelper));
} 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( dispatch(ev, TouchEventType.END);
TouchEvent.obtain( mTargetTag = -1;
mTargetTag,
SystemClock.nanoTime(),
TouchEventType.END,
ev,
ev.getX(),
ev.getY(),
mTouchEventCoalescingKeyHelper));
} else if (action == MotionEvent.ACTION_CANCEL) { } else if (action == MotionEvent.ACTION_CANCEL) {
dispatchCancelEvent(ev, eventDispatcher); dispatchCancelEvent(ev);
mTargetTag = -1; mTargetTag = -1;
} else { } else {
Log.w( Log.w(
@@ -183,7 +157,7 @@ public class RNSVGSvgView extends ViewGroup {
} }
} }
private void dispatchCancelEvent(MotionEvent androidEvent, EventDispatcher eventDispatcher) { private void dispatchCancelEvent(MotionEvent ev) {
// This means the gesture has already ended, via some other CANCEL or UP event. This is not // This means the gesture has already ended, via some other CANCEL or UP event. This is not
// expected to happen very often as it would mean some child View has decided to intercept the // expected to happen very often as it would mean some child View has decided to intercept the
// touch stream and start a native gesture only upon receiving the UP/CANCEL event. // touch stream and start a native gesture only upon receiving the UP/CANCEL event.
@@ -195,15 +169,7 @@ public class RNSVGSvgView extends ViewGroup {
return; return;
} }
Assertions.assertNotNull(eventDispatcher).dispatchEvent( dispatch(ev, TouchEventType.CANCEL);
TouchEvent.obtain(
mTargetTag,
SystemClock.nanoTime(),
TouchEventType.CANCEL,
androidEvent,
androidEvent.getX(),
androidEvent.getY(),
mTouchEventCoalescingKeyHelper));
} }
public void onDataURL() { public void onDataURL() {