From ac8d111f7bb1bc9ba4506b396c0cfa0445a6552d Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 19 Jul 2019 11:31:51 +0800 Subject: [PATCH] doric plugin --- .../pengfeizhou/doric/DoricContext.java | 4 +-- .../github/pengfeizhou/doric/DoricDriver.java | 4 +-- ...BaseModule.java => DoricNativePlugin.java} | 8 +++--- .../{ModalModule.java => ModalPlugin.java} | 13 ++++------ .../doric/engine/DoricJSEngine.java | 22 +--------------- .../{DoricModule.java => DoricComponent.java} | 2 +- .../pengfeizhou/doric/utils/DoricUtils.java | 25 +++++++++++++++++++ 7 files changed, 41 insertions(+), 37 deletions(-) rename Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/{BaseModule.java => DoricNativePlugin.java} (60%) rename Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/{ModalModule.java => ModalPlugin.java} (61%) rename Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/{DoricModule.java => DoricComponent.java} (92%) diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java index 30e2ed92..3a987f02 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java @@ -20,7 +20,7 @@ public class DoricContext { public static DoricContext createContext(String script, String alias) { String contextId = String.valueOf(sCounter.incrementAndGet()); - DoricDriver.getInstance().createPage(contextId, script, alias); + DoricDriver.getInstance().createContext(contextId, script, alias); return new DoricContext(contextId); } @@ -29,6 +29,6 @@ public class DoricContext { } public void teardown() { - DoricDriver.getInstance().destoryContext(mContextId); + DoricDriver.getInstance().destroyContext(mContextId); } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java index ca113b74..44ae5feb 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java @@ -28,11 +28,11 @@ public class DoricDriver { return Inner.sInstance; } - public void createPage(final String contextId, final String script, final String source) { + public void createContext(final String contextId, final String script, final String source) { doricJSEngine.prepareContext(contextId, script, source); } - public void destoryContext(String contextId) { + public void destroyContext(String contextId) { doricJSEngine.destroyContext(contextId); } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/BaseModule.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/DoricNativePlugin.java similarity index 60% rename from Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/BaseModule.java rename to Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/DoricNativePlugin.java index cfcbe9e8..8344f564 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/BaseModule.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/DoricNativePlugin.java @@ -7,12 +7,14 @@ import com.github.pengfeizhou.doric.DoricContext; * @Author: pengfei.zhou * @CreateDate: 2019-07-18 */ -public abstract class BaseModule { +public abstract class DoricNativePlugin { private final DoricContext doricContext; - protected BaseModule(DoricContext doricContext) { + protected DoricNativePlugin(DoricContext doricContext) { this.doricContext = doricContext; } - public abstract String moduleName(); + public DoricContext getDoricContext() { + return doricContext; + } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/ModalModule.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/ModalPlugin.java similarity index 61% rename from Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/ModalModule.java rename to Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/ModalPlugin.java index 092faa75..0a0760ea 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/ModalModule.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/bridge/ModalPlugin.java @@ -1,6 +1,7 @@ package com.github.pengfeizhou.doric.bridge; import com.github.pengfeizhou.doric.DoricContext; +import com.github.pengfeizhou.doric.extension.DoricComponent; import com.github.pengfeizhou.doric.extension.DoricMethod; /** @@ -8,19 +9,15 @@ import com.github.pengfeizhou.doric.extension.DoricMethod; * @Author: pengfei.zhou * @CreateDate: 2019-07-18 */ -public class ModalModule extends BaseModule { +@DoricComponent(name = "modal") +public class ModalPlugin extends DoricNativePlugin { - protected ModalModule(DoricContext doricContext) { + protected ModalPlugin(DoricContext doricContext) { super(doricContext); } - @Override - public String moduleName() { - return "modal"; - } - @DoricMethod(name = "toast") public void toast() { - + } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java index 2417eae6..b9b791f3 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java @@ -17,8 +17,6 @@ import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaValue; -import org.json.JSONObject; - import java.util.ArrayList; /** @@ -214,25 +212,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time public void run() { ArrayList values = new ArrayList<>(); for (Object arg : args) { - if (arg == null) { - values.add(new JavaValue()); - } else if (arg instanceof JSONObject) { - values.add(new JavaValue((JSONObject) arg)); - } else if (arg instanceof String) { - values.add(new JavaValue((String) arg)); - } else if (arg instanceof Integer) { - values.add(new JavaValue((Integer) arg)); - } else if (arg instanceof Long) { - values.add(new JavaValue((Long) arg)); - } else if (arg instanceof Double) { - values.add(new JavaValue((Double) arg)); - } else if (arg instanceof Boolean) { - values.add(new JavaValue((Boolean) arg)); - } else if (arg instanceof JavaValue) { - values.add((JavaValue) arg); - } else { - values.add(new JavaValue(String.valueOf(arg))); - } + values.add(DoricUtils.toJavaValue(arg)); } settableFuture.set(mDoricJSE.invokeMethod(DoricConstant.GLOBAL_DORIC, method, values.toArray(new JavaValue[values.size()]), true)); diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricModule.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricComponent.java similarity index 92% rename from Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricModule.java rename to Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricComponent.java index 1350cfd7..1bbde0c0 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricModule.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricComponent.java @@ -14,6 +14,6 @@ import java.lang.annotation.Target; @Documented @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -public @interface DoricModule { +public @interface DoricComponent { String name() default ""; } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricUtils.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricUtils.java index dd0e2e5d..d2656106 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricUtils.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricUtils.java @@ -3,6 +3,9 @@ package com.github.pengfeizhou.doric.utils; import android.content.res.AssetManager; import com.github.pengfeizhou.doric.Doric; +import com.github.pengfeizhou.jscore.JavaValue; + +import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; @@ -35,4 +38,26 @@ public class DoricUtils { } return ""; } + + public static JavaValue toJavaValue(Object arg) { + if (arg == null) { + return new JavaValue(); + } else if (arg instanceof JSONObject) { + return new JavaValue((JSONObject) arg); + } else if (arg instanceof String) { + return new JavaValue((String) arg); + } else if (arg instanceof Integer) { + return new JavaValue((Integer) arg); + } else if (arg instanceof Long) { + return new JavaValue((Long) arg); + } else if (arg instanceof Double) { + return new JavaValue((Double) arg); + } else if (arg instanceof Boolean) { + return new JavaValue((Boolean) arg); + } else if (arg instanceof JavaValue) { + return (JavaValue) arg; + } else { + return new JavaValue(String.valueOf(arg)); + } + } }