diff --git a/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/HippyNativeGestureSpan.java b/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/HippyNativeGestureSpan.java index 7e876d35b31..b0463aed3ed 100644 --- a/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/HippyNativeGestureSpan.java +++ b/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/HippyNativeGestureSpan.java @@ -152,6 +152,11 @@ public boolean needHandle(String type) { return false; } + @Override + public void handle(String type, MotionEvent event) { + + } + @Override public void handle(String type, float x, float y) { if (TextUtils.equals(type, NodeProps.ON_PRESS_IN)) { diff --git a/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureDispatcher.java b/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureDispatcher.java index 357622d3af4..0832d703174 100644 --- a/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureDispatcher.java +++ b/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureDispatcher.java @@ -38,6 +38,10 @@ public class NativeGestureDispatcher implements NativeGestureProcessor.Callback private static final String KEY_TAG_ID = "id"; private static final String KEY_PAGE_X = "page_x"; private static final String KEY_PAGE_Y = "page_y"; + + private static final String KEY_RAW_X = "rawx"; + private static final String KEY_RAW_Y = "rawy"; + private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout(); private static final View.OnClickListener mOnClickListener = new View.OnClickListener() { @@ -208,7 +212,7 @@ public static void handlePressOut(HippyEngineContext context, int tagId) { } public static void handleTouchDown(HippyEngineContext context, int mTagId, float x, float y, - int viewId) { + int viewId) { int[] viewCoords = new int[2]; getLocationInWindow(context, viewId, viewCoords); HippyMap params = new HippyMap(); @@ -216,12 +220,27 @@ public static void handleTouchDown(HippyEngineContext context, int mTagId, float params.pushInt(KEY_TAG_ID, mTagId); params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x)); params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y)); + context.getModuleManager().getJavaScriptModule(EventDispatcher.class) + .receiveNativeGesture(params); + } + + public static void handleTouchDown(HippyEngineContext context, int mTagId, MotionEvent event, + int viewId) { + int[] viewCoords = new int[2]; + getLocationInWindow(context, viewId, viewCoords); + HippyMap params = new HippyMap(); + params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_DOWN); + params.pushInt(KEY_TAG_ID, mTagId); + params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX())); + params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY())); + params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX())); + params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY())); context.getModuleManager().getJavaScriptModule(EventDispatcher.class) .receiveNativeGesture(params); } public static void handleTouchMove(HippyEngineContext context, int mTagId, float x, float y, - int viewId) { + int viewId) { int[] viewCoords = new int[2]; getLocationInWindow(context, viewId, viewCoords); HippyMap params = new HippyMap(); @@ -229,12 +248,27 @@ public static void handleTouchMove(HippyEngineContext context, int mTagId, float params.pushInt(KEY_TAG_ID, mTagId); params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x)); params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y)); + context.getModuleManager().getJavaScriptModule(EventDispatcher.class) + .receiveNativeGesture(params); + } + + public static void handleTouchMove(HippyEngineContext context, int mTagId, MotionEvent event, + int viewId) { + int[] viewCoords = new int[2]; + getLocationInWindow(context, viewId, viewCoords); + HippyMap params = new HippyMap(); + params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_MOVE); + params.pushInt(KEY_TAG_ID, mTagId); + params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX())); + params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY())); + params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX())); + params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY())); context.getModuleManager().getJavaScriptModule(EventDispatcher.class) .receiveNativeGesture(params); } public static void handleTouchEnd(HippyEngineContext context, int mTagId, float x, float y, - int viewId) { + int viewId) { int[] viewCoords = new int[2]; getLocationInWindow(context, viewId, viewCoords); HippyMap params = new HippyMap(); @@ -242,12 +276,27 @@ public static void handleTouchEnd(HippyEngineContext context, int mTagId, float params.pushInt(KEY_TAG_ID, mTagId); params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x)); params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y)); + context.getModuleManager().getJavaScriptModule(EventDispatcher.class) + .receiveNativeGesture(params); + } + + public static void handleTouchEnd(HippyEngineContext context, int mTagId, MotionEvent event, + int viewId) { + int[] viewCoords = new int[2]; + getLocationInWindow(context, viewId, viewCoords); + HippyMap params = new HippyMap(); + params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_END); + params.pushInt(KEY_TAG_ID, mTagId); + params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX())); + params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY())); + params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX())); + params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY())); context.getModuleManager().getJavaScriptModule(EventDispatcher.class) .receiveNativeGesture(params); } public static void handleTouchCancel(HippyEngineContext context, int mTagId, float x, float y, - int viewId) { + int viewId) { int[] viewCoords = new int[2]; getLocationInWindow(context, viewId, viewCoords); HippyMap params = new HippyMap(); @@ -255,6 +304,21 @@ public static void handleTouchCancel(HippyEngineContext context, int mTagId, flo params.pushInt(KEY_TAG_ID, mTagId); params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + x)); params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + y)); + context.getModuleManager().getJavaScriptModule(EventDispatcher.class) + .receiveNativeGesture(params); + } + + public static void handleTouchCancel(HippyEngineContext context, int mTagId, MotionEvent event, + int viewId) { + int[] viewCoords = new int[2]; + getLocationInWindow(context, viewId, viewCoords); + HippyMap params = new HippyMap(); + params.pushString(KEY_EVENT_NAME, NodeProps.ON_TOUCH_CANCEL); + params.pushInt(KEY_TAG_ID, mTagId); + params.pushDouble(KEY_RAW_X, PixelUtil.px2dp(event.getRawX())); + params.pushDouble(KEY_RAW_Y, PixelUtil.px2dp(event.getRawY())); + params.pushDouble(KEY_PAGE_X, PixelUtil.px2dp(viewCoords[0] + event.getX())); + params.pushDouble(KEY_PAGE_Y, PixelUtil.px2dp(viewCoords[1] + event.getY())); context.getModuleManager().getJavaScriptModule(EventDispatcher.class) .receiveNativeGesture(params); } @@ -328,16 +392,42 @@ public void handle(String type, float x, float y) { handlePressOut(mEngineContext, mTargetView.getId()); } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_DOWN)) { NativeGestureDispatcher - .handleTouchDown(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + .handleTouchDown(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_MOVE)) { + NativeGestureDispatcher + .handleTouchMove(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_END)) { + NativeGestureDispatcher + .handleTouchEnd(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_CANCEL)) { + NativeGestureDispatcher + .handleTouchCancel(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + } + } + + @Override + public void handle(String type, MotionEvent event) { + if (mTargetView == null) { + LogUtils.e("NativeGestureDispatcher", "handle!!! but view is null!!!!"); + return; + } + + if (TextUtils.equals(type, NodeProps.ON_PRESS_IN)) { + handlePressIn(mEngineContext, mTargetView.getId()); + } else if (TextUtils.equals(type, NodeProps.ON_PRESS_OUT)) { + handlePressOut(mEngineContext, mTargetView.getId()); + } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_DOWN)) { + NativeGestureDispatcher + .handleTouchDown(mEngineContext, mTargetView.getId(), event, mTargetView.getId()); } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_MOVE)) { NativeGestureDispatcher - .handleTouchMove(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + .handleTouchMove(mEngineContext, mTargetView.getId(), event, mTargetView.getId()); } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_END)) { NativeGestureDispatcher - .handleTouchEnd(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + .handleTouchEnd(mEngineContext, mTargetView.getId(), event, mTargetView.getId()); } else if (TextUtils.equals(type, NodeProps.ON_TOUCH_CANCEL)) { NativeGestureDispatcher - .handleTouchCancel(mEngineContext, mTargetView.getId(), x, y, mTargetView.getId()); + .handleTouchCancel(mEngineContext, mTargetView.getId(), event, mTargetView.getId()); } } } diff --git a/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureProcessor.java b/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureProcessor.java index 5bc130506a3..8308153b0a1 100644 --- a/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureProcessor.java +++ b/android/sdk/src/main/java/com/tencent/mtt/hippy/uimanager/NativeGestureProcessor.java @@ -70,7 +70,7 @@ public boolean onTouchEvent(MotionEvent event) { } if (mCallback.needHandle(NodeProps.ON_TOUCH_DOWN)) { - mCallback.handle(NodeProps.ON_TOUCH_DOWN, event.getX(), event.getY()); + mCallback.handle(NodeProps.ON_TOUCH_DOWN, event); handle = true; } @@ -89,7 +89,7 @@ public boolean onTouchEvent(MotionEvent event) { } case MotionEvent.ACTION_MOVE: { if (mCallback.needHandle(NodeProps.ON_TOUCH_MOVE)) { - mCallback.handle(NodeProps.ON_TOUCH_MOVE, event.getX(), event.getY()); + mCallback.handle(NodeProps.ON_TOUCH_MOVE, event); handle = true; } @@ -114,12 +114,12 @@ public boolean onTouchEvent(MotionEvent event) { } case MotionEvent.ACTION_UP: { if (mCallback.needHandle(NodeProps.ON_TOUCH_END)) { - mCallback.handle(NodeProps.ON_TOUCH_END, event.getX(), event.getY()); + mCallback.handle(NodeProps.ON_TOUCH_END, event); handle = true; } if (mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) { - mCallback.handle(NodeProps.ON_PRESS_OUT, event.getX(), event.getY()); + mCallback.handle(NodeProps.ON_PRESS_OUT, event); handle = true; } else if (!mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) { getGestureHandler().sendEmptyMessageDelayed(PRESS_OUT, TAP_TIMEOUT); @@ -131,12 +131,12 @@ public boolean onTouchEvent(MotionEvent event) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_OUTSIDE: { if (mCallback.needHandle(NodeProps.ON_TOUCH_CANCEL)) { - mCallback.handle(NodeProps.ON_TOUCH_CANCEL, event.getX(), event.getY()); + mCallback.handle(NodeProps.ON_TOUCH_CANCEL, event); handle = true; } if (mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) { - mCallback.handle(NodeProps.ON_PRESS_OUT, event.getX(), event.getY()); + mCallback.handle(NodeProps.ON_PRESS_OUT, event); handle = true; } else if (!mNoPressIn && mCallback.needHandle(NodeProps.ON_PRESS_OUT)) { if (getGestureHandler().hasMessages(PRESS_IN)) { @@ -156,6 +156,8 @@ public interface Callback { boolean needHandle(String type); + void handle(String type, MotionEvent event); + void handle(String type, float x, float y); }