feat:debug and success to implement nativeChannel on Android

This commit is contained in:
pengfei.zhou 2019-11-18 11:40:46 +08:00
parent 8c83c3d13e
commit 856287e15f
6 changed files with 121 additions and 18 deletions

View File

@ -20,17 +20,21 @@ import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricPromise; 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.DoricLog;
import pub.doric.utils.DoricMetaInfo;
import pub.doric.utils.DoricUtils;
import pub.doric.utils.ThreadMode; import pub.doric.utils.ThreadMode;
import pub.doric.shader.RootNode; import pub.doric.shader.RootNode;
import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import java.lang.reflect.Method;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
/** /**
@ -79,34 +83,88 @@ public class ShaderPlugin extends DoricJavaPlugin {
} }
@DoricMethod @DoricMethod
public void command(JSDecoder jsDecoder, final DoricPromise doricPromise) { public JavaValue command(JSDecoder jsDecoder, final DoricPromise doricPromise) {
try { try {
final JSObject jsObject = jsDecoder.decode().asObject(); final JSObject jsObject = jsDecoder.decode().asObject();
final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray(); final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray();
final String name = jsObject.getProperty("name").asString().value(); final String name = jsObject.getProperty("name").asString().value();
getDoricContext().getDriver().asyncCall(new Callable<JavaValue>() { final JSValue args = jsObject.getProperty("args");
@Override ViewNode viewNode = null;
public JavaValue call() throws Exception { for (JSValue value : viewIds) {
return new JavaValue("called"); if (viewNode == null) {
} viewNode = getDoricContext().getRootNode();
}, ThreadMode.UI).setCallback(new AsyncResult.Callback<JavaValue>() { } else {
@Override if (value.isString() && viewNode instanceof SuperNode) {
public void onResult(JavaValue result) { String viewId = value.asString().value();
doricPromise.resolve(result); viewNode = ((SuperNode) viewNode).getSubNodeById(viewId);
}
} }
}
if (viewNode == null) {
doricPromise.reject(new JavaValue("Cannot find opposite view"));
} else {
final ViewNode targetViewNode = viewNode;
DoricMetaInfo<ViewNode> 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<JavaValue> callable = new Callable<JavaValue>() {
@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<JavaValue> asyncResult = getDoricContext().getDriver()
.asyncCall(callable, doricMethod == null ? ThreadMode.INDEPENDENT : doricMethod.thread());
asyncResult.setCallback(new AsyncResult.Callback<JavaValue>() {
@Override
public void onResult(JavaValue result) {
doricPromise.resolve(result);
}
@Override @Override
public void onError(Throwable t) { public void onError(Throwable t) {
doricPromise.reject(new JavaValue(t.getLocalizedMessage())); doricPromise.resolve(new JavaValue(t.getLocalizedMessage()));
} }
@Override @Override
public void onFinish() { public void onFinish() {
}
});
} }
}); }
} catch (ArchiveException e) { } catch (ArchiveException e) {
e.printStackTrace(); e.printStackTrace();
} }
return new JavaValue(true);
}
private Object createParam(Class clz, DoricPromise doricPromise, JSValue jsValue) {
if (clz == DoricPromise.class) {
return doricPromise;
} else {
return jsValue;
}
} }
} }

View File

@ -152,4 +152,14 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
} }
} }
} }
@Override
public ViewNode getSubNodeById(String id) {
for (ViewNode node : mChildNodes) {
if (id.equals(node.getId())) {
return node;
}
}
return null;
}
} }

View File

@ -40,6 +40,8 @@ public abstract class SuperNode<V extends View> extends ViewNode<V> {
super(doricContext); super(doricContext);
} }
public abstract ViewNode getSubNodeById(String id);
protected ViewGroup.LayoutParams generateDefaultLayoutParams() { protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new ViewGroup.LayoutParams(0, 0); return new ViewGroup.LayoutParams(0, 0);
} }

View File

@ -24,6 +24,7 @@ import android.widget.LinearLayout;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricRegistry; import pub.doric.DoricRegistry;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.utils.DoricContextHolder; import pub.doric.utils.DoricContextHolder;
import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricConstant;
import pub.doric.utils.DoricMetaInfo; import pub.doric.utils.DoricMetaInfo;
@ -243,4 +244,14 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
mSuperNode.blendSubLayoutConfig(this, layoutConfig); mSuperNode.blendSubLayoutConfig(this, layoutConfig);
} }
} }
@DoricMethod
public int getWidth() {
return mView.getWidth();
}
@DoricMethod
public int getHeight() {
return mView.getHeight();
}
} }

View File

@ -15,6 +15,8 @@
*/ */
package pub.doric.shader.list; package pub.doric.shader.list;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -91,4 +93,23 @@ public class ListNode extends SuperNode<RecyclerView> {
protected void blendSubLayoutConfig(ViewNode viewNode, JSObject jsObject) { protected void blendSubLayoutConfig(ViewNode viewNode, JSObject jsObject) {
super.blendSubLayoutConfig(viewNode, 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;
}
} }

View File

@ -333,6 +333,7 @@ export abstract class Group extends Superview {
} }
addChild(view: View) { addChild(view: View) {
view.superview = this
this.children.push(view) this.children.push(view)
} }
} }