From 856287e15f6ea75281e9fd271449421aca594b7a Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 18 Nov 2019 11:40:46 +0800 Subject: [PATCH] feat:debug and success to implement nativeChannel on Android --- .../java/pub/doric/plugin/ShaderPlugin.java | 94 +++++++++++++++---- .../main/java/pub/doric/shader/GroupNode.java | 10 ++ .../main/java/pub/doric/shader/SuperNode.java | 2 + .../main/java/pub/doric/shader/ViewNode.java | 11 +++ .../java/pub/doric/shader/list/ListNode.java | 21 +++++ js-framework/src/ui/view.ts | 1 + 6 files changed, 121 insertions(+), 18 deletions(-) diff --git a/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java index 56d86c07..a13e75d6 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java @@ -20,17 +20,21 @@ import pub.doric.async.AsyncResult; import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPromise; +import pub.doric.shader.SuperNode; +import pub.doric.shader.ViewNode; import pub.doric.utils.DoricLog; +import pub.doric.utils.DoricMetaInfo; +import pub.doric.utils.DoricUtils; import pub.doric.utils.ThreadMode; import pub.doric.shader.RootNode; import com.github.pengfeizhou.jscore.ArchiveException; -import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JavaValue; +import java.lang.reflect.Method; import java.util.concurrent.Callable; /** @@ -79,34 +83,88 @@ public class ShaderPlugin extends DoricJavaPlugin { } @DoricMethod - public void command(JSDecoder jsDecoder, final DoricPromise doricPromise) { + public JavaValue command(JSDecoder jsDecoder, final DoricPromise doricPromise) { try { final JSObject jsObject = jsDecoder.decode().asObject(); final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray(); final String name = jsObject.getProperty("name").asString().value(); - getDoricContext().getDriver().asyncCall(new Callable() { - @Override - public JavaValue call() throws Exception { - return new JavaValue("called"); - } - }, ThreadMode.UI).setCallback(new AsyncResult.Callback() { - @Override - public void onResult(JavaValue result) { - doricPromise.resolve(result); + final JSValue args = jsObject.getProperty("args"); + ViewNode viewNode = null; + for (JSValue value : viewIds) { + if (viewNode == null) { + viewNode = getDoricContext().getRootNode(); + } else { + if (value.isString() && viewNode instanceof SuperNode) { + String viewId = value.asString().value(); + viewNode = ((SuperNode) viewNode).getSubNodeById(viewId); + } } + } + if (viewNode == null) { + doricPromise.reject(new JavaValue("Cannot find opposite view")); + } else { + final ViewNode targetViewNode = viewNode; + DoricMetaInfo pluginInfo = getDoricContext().getDriver().getRegistry() + .acquireViewNodeInfo(viewNode.getType()); + final Method method = pluginInfo.getMethod(name); + if (method == null) { + String errMsg = String.format( + "Cannot find plugin method in class:%s,method:%s", + viewNode.getClass(), + name); + doricPromise.reject(new JavaValue(errMsg)); + } else { + DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); + Callable callable = new Callable() { + @Override + public JavaValue call() throws Exception { + Class[] classes = method.getParameterTypes(); + Object ret; + if (classes.length == 0) { + ret = method.invoke(targetViewNode); + } else if (classes.length == 1) { + ret = method.invoke(targetViewNode, + createParam(classes[0], doricPromise, args)); + } else { + ret = method.invoke(targetViewNode, + createParam(classes[0], doricPromise, args), + createParam(classes[1], doricPromise, args)); + } + return DoricUtils.toJavaValue(ret); + } + }; + AsyncResult asyncResult = getDoricContext().getDriver() + .asyncCall(callable, doricMethod == null ? ThreadMode.INDEPENDENT : doricMethod.thread()); + asyncResult.setCallback(new AsyncResult.Callback() { + @Override + public void onResult(JavaValue result) { + doricPromise.resolve(result); + } - @Override - public void onError(Throwable t) { - doricPromise.reject(new JavaValue(t.getLocalizedMessage())); - } + @Override + public void onError(Throwable t) { + doricPromise.resolve(new JavaValue(t.getLocalizedMessage())); + } - @Override - public void onFinish() { + @Override + public void onFinish() { + } + }); } - }); + } } catch (ArchiveException e) { e.printStackTrace(); } + return new JavaValue(true); + } + + + private Object createParam(Class clz, DoricPromise doricPromise, JSValue jsValue) { + if (clz == DoricPromise.class) { + return doricPromise; + } else { + return jsValue; + } } } diff --git a/Android/doric/src/main/java/pub/doric/shader/GroupNode.java b/Android/doric/src/main/java/pub/doric/shader/GroupNode.java index 5d6c9b3a..0220a686 100644 --- a/Android/doric/src/main/java/pub/doric/shader/GroupNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/GroupNode.java @@ -152,4 +152,14 @@ public abstract class GroupNode extends SuperNode { } } } + + @Override + public ViewNode getSubNodeById(String id) { + for (ViewNode node : mChildNodes) { + if (id.equals(node.getId())) { + return node; + } + } + return null; + } } diff --git a/Android/doric/src/main/java/pub/doric/shader/SuperNode.java b/Android/doric/src/main/java/pub/doric/shader/SuperNode.java index 4aece2a7..954b0689 100644 --- a/Android/doric/src/main/java/pub/doric/shader/SuperNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/SuperNode.java @@ -40,6 +40,8 @@ public abstract class SuperNode extends ViewNode { super(doricContext); } + public abstract ViewNode getSubNodeById(String id); + protected ViewGroup.LayoutParams generateDefaultLayoutParams() { return new ViewGroup.LayoutParams(0, 0); } diff --git a/Android/doric/src/main/java/pub/doric/shader/ViewNode.java b/Android/doric/src/main/java/pub/doric/shader/ViewNode.java index 3c26cecf..76e8833e 100644 --- a/Android/doric/src/main/java/pub/doric/shader/ViewNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/ViewNode.java @@ -24,6 +24,7 @@ import android.widget.LinearLayout; import pub.doric.DoricContext; import pub.doric.DoricRegistry; import pub.doric.async.AsyncResult; +import pub.doric.extension.bridge.DoricMethod; import pub.doric.utils.DoricContextHolder; import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricMetaInfo; @@ -243,4 +244,14 @@ public abstract class ViewNode extends DoricContextHolder { mSuperNode.blendSubLayoutConfig(this, layoutConfig); } } + + @DoricMethod + public int getWidth() { + return mView.getWidth(); + } + + @DoricMethod + public int getHeight() { + return mView.getHeight(); + } } diff --git a/Android/doric/src/main/java/pub/doric/shader/list/ListNode.java b/Android/doric/src/main/java/pub/doric/shader/list/ListNode.java index bea96e47..4a8feb43 100644 --- a/Android/doric/src/main/java/pub/doric/shader/list/ListNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/list/ListNode.java @@ -15,6 +15,8 @@ */ package pub.doric.shader.list; +import android.view.View; + import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -91,4 +93,23 @@ public class ListNode extends SuperNode { protected void blendSubLayoutConfig(ViewNode viewNode, JSObject jsObject) { super.blendSubLayoutConfig(viewNode, jsObject); } + + @Override + public ViewNode getSubNodeById(String id) { + RecyclerView.LayoutManager manager = mView.getLayoutManager(); + if (manager == null) { + return null; + } + for (int i = 0; i < manager.getChildCount(); i++) { + View view = manager.getChildAt(i); + if (view == null) { + continue; + } + ListAdapter.DoricViewHolder viewHolder = (ListAdapter.DoricViewHolder) mView.getChildViewHolder(view); + if (id.equals(viewHolder.listItemNode.getId())) { + return viewHolder.listItemNode; + } + } + return null; + } } diff --git a/js-framework/src/ui/view.ts b/js-framework/src/ui/view.ts index 2955fb1f..f473c9fd 100644 --- a/js-framework/src/ui/view.ts +++ b/js-framework/src/ui/view.ts @@ -333,6 +333,7 @@ export abstract class Group extends Superview { } addChild(view: View) { + view.superview = this this.children.push(view) } }