From 4c593253bba10c9c221ac267a1f8b0abd0dddcab Mon Sep 17 00:00:00 2001 From: maxli Date: Mon, 22 Jan 2024 14:23:41 +0800 Subject: [PATCH 1/2] feat(android): add disallowInterceptTouchEvent for hippy view group --- .../com/tencent/mtt/hippy/dom/node/NodeProps.java | 1 + .../tencent/mtt/hippy/views/view/HippyViewGroup.java | 12 ++++++++++++ .../hippy/views/view/HippyViewGroupController.java | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/dom/node/NodeProps.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/dom/node/NodeProps.java index a813ccf7cbb..c980e4b76f2 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/dom/node/NodeProps.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/dom/node/NodeProps.java @@ -122,6 +122,7 @@ public class NodeProps { public static final String ON_TOUCH_CANCEL = "touchcancel"; public static final String ON_INTERCEPT_TOUCH_EVENT = "onInterceptTouchEvent"; public static final String ON_INTERCEPT_PULL_UP_EVENT = "onInterceptPullUpEvent"; + public static final String DISALLOW_INTERCEPT_TOUCH_EVENT = "disallowInterceptTouchEvent"; public static final String ON_ATTACHED_TO_WINDOW = "attachedtowindow"; public static final String ON_DETACHED_FROM_WINDOW = "detachedfromwindow"; diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroup.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroup.java index 42e01b69887..74a6049c203 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroup.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroup.java @@ -17,6 +17,7 @@ package com.tencent.mtt.hippy.views.view; import android.view.ViewGroup; +import android.view.ViewParent; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.tencent.mtt.hippy.dom.node.NodeProps; @@ -36,6 +37,7 @@ public class HippyViewGroup extends FlatViewGroup implements HippyViewBase { float mDownX = 0; float mDownY = 0; boolean isHandlePullUp = false; + private boolean mDisallowInterceptTouchEvent = false; private ViewConfiguration mViewConfiguration; @Nullable protected NativeGestureDispatcher mGestureDispatcher; @@ -73,6 +75,10 @@ public void setOverflow(String overflow) { setOverflow(overflow, this); } + public void setDisallowInterceptTouchEvent(boolean disallow) { + mDisallowInterceptTouchEvent = disallow; + } + public static void setOverflow(@NonNull String overflow, @NonNull ViewGroup viewGroup) { switch (overflow) { case NodeProps.VISIBLE: @@ -91,6 +97,12 @@ public static void setOverflow(@NonNull String overflow, @NonNull ViewGroup view public boolean onInterceptTouchEvent(MotionEvent ev) { int action = ev.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_DOWN) { + if (mDisallowInterceptTouchEvent) { + ViewParent parent = getParent(); + if (parent != null) { + parent.requestDisallowInterceptTouchEvent(true); + } + } mDownX = ev.getX(); mDownY = ev.getY(); isHandlePullUp = false; diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroupController.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroupController.java index f8a2ac2b0b1..5d71eb49421 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroupController.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/view/HippyViewGroupController.java @@ -53,6 +53,11 @@ public void setOverflow(HippyViewGroup viewGroup, String overflow) { viewGroup.setOverflow(overflow); } + @HippyControllerProps(name = NodeProps.DISALLOW_INTERCEPT_TOUCH_EVENT, defaultType = HippyControllerProps.BOOLEAN) + public void setDisallowInterceptTouchEvent(HippyViewGroup view, boolean disallow) { + view.setDisallowInterceptTouchEvent(disallow); + } + @SuppressWarnings("deprecation") @Override public void dispatchFunction(@NonNull HippyViewGroup viewGroup, @NonNull String functionName, From e51d24ff77a5bfb447290132efc1ef3f0506a777 Mon Sep 17 00:00:00 2001 From: maxli Date: Mon, 22 Jan 2024 18:04:23 +0800 Subject: [PATCH 2/2] fix(android): add thread check before argumentToBytes --- .../renderer/NativeRenderProvider.java | 27 +++++++++++++++++-- .../component/text/TextImageSpan.java | 12 ++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderProvider.java b/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderProvider.java index e1835a5e41a..4a38920618a 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderProvider.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderProvider.java @@ -16,6 +16,7 @@ package com.tencent.renderer; +import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -24,6 +25,7 @@ import com.tencent.mtt.hippy.serialization.nio.writer.SafeHeapWriter; import com.tencent.mtt.hippy.serialization.string.InternalizedStringTable; import com.tencent.mtt.hippy.utils.PixelUtil; +import com.tencent.mtt.hippy.utils.UIThreadUtils; import com.tencent.renderer.annotation.CalledByNative; import com.tencent.renderer.serialization.Deserializer; import com.tencent.renderer.serialization.Serializer; @@ -351,7 +353,18 @@ public void onSizeChanged(int rootId, int nodeId, int width, int height, boolean * @param nodeId the dom node id * @param params parameters to be return to js */ - public void doPromiseCallBack(int result, long callbackId, @NonNull String functionName, + public void doPromiseCallBack(final int result, final long callbackId, @NonNull final String functionName, + final int rootId, final int nodeId, @Nullable final Object params) { + if (UIThreadUtils.isOnUiThread()) { + doPromiseCallBackImpl(result, callbackId, functionName, rootId, nodeId, params); + } else { + UIThreadUtils.runOnUiThread( + () -> doPromiseCallBackImpl(result, callbackId, functionName, rootId, nodeId, params)); + } + } + + @MainThread + private void doPromiseCallBackImpl(int result, long callbackId, @NonNull String functionName, int rootId, int nodeId, @Nullable Object params) { byte[] bytes = null; int offset = 0; @@ -375,7 +388,17 @@ public void doPromiseCallBack(int result, long callbackId, @NonNull String funct length); } - public void dispatchEvent(int rootId, int nodeId, @NonNull String eventName, + public void dispatchEvent(final int rootId, final int nodeId, @NonNull final String eventName, + @Nullable final Object params, final boolean useCapture, final boolean useBubble) { + if (UIThreadUtils.isOnUiThread()) { + dispatchEventImpl(rootId, nodeId, eventName, params, useCapture, useBubble); + } else { + UIThreadUtils.runOnUiThread( + () -> dispatchEventImpl(rootId, nodeId, eventName, params, useCapture, useBubble)); + } + } + + private void dispatchEventImpl(int rootId, int nodeId, @NonNull String eventName, @Nullable Object params, boolean useCapture, boolean useBubble) { byte[] bytes = null; int offset = 0; diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextImageSpan.java b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextImageSpan.java index dd217528b1b..2fe91cb85b1 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextImageSpan.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextImageSpan.java @@ -130,8 +130,18 @@ public TextImageSpan(Drawable drawable, String source, @NonNull ImageVirtualNode } public void setUrl(@Nullable final String url) { - if (!TextUtils.isEmpty(url)) { + if (TextUtils.isEmpty(url)) { + return; + } + if (UIThreadUtils.isOnUiThread()) { loadImageWithUrl(url); + } else { + UIThreadUtils.runOnUiThread(new Runnable() { + @Override + public void run() { + loadImageWithUrl(url); + } + }); } }