diff --git a/Android/app/src/main/java/com/github/pengfeizhou/doricdemo/MainActivity.java b/Android/app/src/main/java/com/github/pengfeizhou/doricdemo/MainActivity.java index 320e122c..327a7743 100644 --- a/Android/app/src/main/java/com/github/pengfeizhou/doricdemo/MainActivity.java +++ b/Android/app/src/main/java/com/github/pengfeizhou/doricdemo/MainActivity.java @@ -12,7 +12,7 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - DoricContext doricContext = DoricContext.createContext(DoricUtils.readAssetFile("demo.js"), "demo"); + DoricContext doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo.js"), "demo"); doricContext.callEntity("log"); } } 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 e76bac82..4e6468d3 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 @@ -1,5 +1,7 @@ package com.github.pengfeizhou.doric; +import android.content.Context; + import com.github.pengfeizhou.doric.async.AsyncResult; import com.github.pengfeizhou.doric.plugin.DoricNativePlugin; import com.github.pengfeizhou.doric.extension.bridge.DoricPluginInfo; @@ -16,13 +18,15 @@ import java.util.Map; public class DoricContext { private final String mContextId; private final Map mPluginMap = new HashMap<>(); + private final Context mContext; - DoricContext(String contextId) { + DoricContext(Context context, String contextId) { + this.mContext = context; this.mContextId = contextId; } - public static DoricContext createContext(String script, String alias) { - return DoricContextManager.getInstance().createContext(script, alias); + public static DoricContext create(Context context, String script, String alias) { + return DoricContextManager.getInstance().createContext(context, script, alias); } public AsyncResult callEntity(String methodName, Object... args) { @@ -33,6 +37,9 @@ public class DoricContext { return DoricDriver.getInstance(); } + public Context getContext() { + return mContext; + } public String getContextId() { return mContextId; diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContextManager.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContextManager.java index 58b6f83b..c23d9f26 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContextManager.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContextManager.java @@ -1,5 +1,7 @@ package com.github.pengfeizhou.doric; +import android.content.Context; + import com.github.pengfeizhou.doric.async.AsyncCall; import com.github.pengfeizhou.doric.async.AsyncResult; import com.github.pengfeizhou.doric.utils.DoricLog; @@ -31,9 +33,9 @@ public class DoricContextManager { return Inner.sInstance; } - DoricContext createContext(final String script, final String source) { + DoricContext createContext(Context context, final String script, final String source) { final String contextId = String.valueOf(counter.incrementAndGet()); - final DoricContext doricContext = new DoricContext(contextId); + final DoricContext doricContext = new DoricContext(context, contextId); doricContextMap.put(contextId, doricContext); doricContext.getDriver().createContext(contextId, script, source); return doricContext; diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricPanel.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricPanel.java index cda83018..5faaa61a 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricPanel.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricPanel.java @@ -35,7 +35,7 @@ public class DoricPanel extends FrameLayout { } public void config(String script, String alias) { - DoricContext doricContext = DoricContext.createContext(script, alias); + DoricContext doricContext = DoricContext.create(getContext(), script, alias); config(doricContext); } 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 4f489a5f..71a904eb 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 @@ -60,13 +60,13 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time String message = args[1].string(); switch (type) { case "w": - DoricLog.w("_js", message); + DoricLog.suffix_w("_js", message); break; case "e": - DoricLog.e("_js", message); + DoricLog.suffix_e("_js", message); break; default: - DoricLog.d("_js", message); + DoricLog.suffix_d("_js", message); break; } } catch (Exception e) { diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricBridgeExtension.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricBridgeExtension.java index 44872d91..5b782ff9 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricBridgeExtension.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricBridgeExtension.java @@ -2,15 +2,18 @@ package com.github.pengfeizhou.doric.extension.bridge; import android.text.TextUtils; +import com.github.pengfeizhou.doric.Doric; import com.github.pengfeizhou.doric.DoricContext; import com.github.pengfeizhou.doric.DoricContextManager; import com.github.pengfeizhou.doric.DoricDriver; import com.github.pengfeizhou.doric.plugin.DoricNativePlugin; import com.github.pengfeizhou.doric.plugin.ModalPlugin; import com.github.pengfeizhou.doric.utils.DoricLog; +import com.github.pengfeizhou.doric.utils.DoricUtils; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaValue; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -52,10 +55,35 @@ public class DoricBridgeExtension { return new JavaValue(false); } DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); + try { + Class[] classes = method.getParameterTypes(); + Object ret; + if (classes.length == 0) { + ret = method.invoke(doricNativePlugin); + } else if (classes.length == 1) { + ret = method.invoke(doricNativePlugin, createParam(context, classes[0], callbackId, jsDecoder)); + } else { + ret = method.invoke(doricNativePlugin, + createParam(context, classes[0], callbackId, jsDecoder), + createParam(context, classes[1], callbackId, jsDecoder)); + } + return DoricUtils.toJavaValue(ret); + } catch (Exception e) { + DoricLog.e("callNative error:%s", e.getLocalizedMessage()); + return new JavaValue(false); + } + } - Class[] classes = method.getParameterTypes(); - - - return new JavaValue(true); + private Object createParam(DoricContext context, Class clz, String callbackId, JSDecoder jsDecoder) { + if (clz == DoricPromise.class) { + return new DoricPromise(context, callbackId); + } else { + try { + return DoricUtils.toJavaObject(clz, jsDecoder); + } catch (Exception e) { + DoricLog.e("createParam error:%s", e.getLocalizedMessage()); + } + return null; + } } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricPluginInfo.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricPluginInfo.java index 890893bd..78984dc1 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricPluginInfo.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/bridge/DoricPluginInfo.java @@ -25,7 +25,7 @@ public class DoricPluginInfo { public DoricPluginInfo(Class pluginClass) { try { - this.pluginConstructor = pluginClass.getConstructor(DoricContext.class); + this.pluginConstructor = pluginClass.getDeclaredConstructor(DoricContext.class); DoricComponent doricComponent = pluginClass.getAnnotation(DoricComponent.class); this.name = doricComponent.name(); Method[] methods = pluginClass.getMethods(); diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/DoricNativePlugin.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/DoricNativePlugin.java index f3bbe602..14e96000 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/DoricNativePlugin.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/DoricNativePlugin.java @@ -10,7 +10,7 @@ import com.github.pengfeizhou.doric.DoricContext; public abstract class DoricNativePlugin { private final DoricContext doricContext; - protected DoricNativePlugin(DoricContext doricContext) { + public DoricNativePlugin(DoricContext doricContext) { this.doricContext = doricContext; } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/ModalPlugin.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/ModalPlugin.java index 63f3e39d..13ea2517 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/ModalPlugin.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/plugin/ModalPlugin.java @@ -1,8 +1,12 @@ package com.github.pengfeizhou.doric.plugin; +import android.widget.Toast; + import com.github.pengfeizhou.doric.DoricContext; import com.github.pengfeizhou.doric.extension.bridge.DoricComponent; import com.github.pengfeizhou.doric.extension.bridge.DoricMethod; +import com.github.pengfeizhou.doric.extension.bridge.DoricPromise; +import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.JSDecoder; /** @@ -13,12 +17,16 @@ import com.github.pengfeizhou.jscore.JSDecoder; @DoricComponent(name = "modal") public class ModalPlugin extends DoricNativePlugin { - protected ModalPlugin(DoricContext doricContext) { + public ModalPlugin(DoricContext doricContext) { super(doricContext); } @DoricMethod(name = "toast", thread = DoricMethod.Mode.UI) - public void toast(JSDecoder decoder) { - + public void toast(JSDecoder decoder, DoricPromise promise) { + try { + Toast.makeText(getDoricContext().getContext(), decoder.string(), Toast.LENGTH_SHORT).show(); + } catch (ArchiveException e) { + e.printStackTrace(); + } } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java index 14e1ea95..b560aaac 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java @@ -27,16 +27,15 @@ public class DoricLog { Log.e(suffixTag(null), format(message, args)); } - public static void d(String suffix, String message, Object... args) { + public static void suffix_d(String suffix, String message, Object... args) { Log.d(suffixTag(suffix), format(message, args)); } - public static void w(String suffix, String message, Object... args) { + public static void suffix_w(String suffix, String message, Object... args) { Log.w(suffixTag(suffix), format(message, args)); - } - public static void e(String suffix, String message, Object... args) { + public static void suffix_e(String suffix, String message, Object... args) { Log.e(suffixTag(suffix), format(message, args)); } 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 11c40c42..41bb2b59 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,14 +3,21 @@ package com.github.pengfeizhou.doric.utils; import android.content.res.AssetManager; import android.os.Handler; import android.os.Looper; +import android.support.annotation.NonNull; import com.github.pengfeizhou.doric.Doric; +import com.github.pengfeizhou.jscore.ArchiveException; +import com.github.pengfeizhou.jscore.JSArray; +import com.github.pengfeizhou.jscore.JSDecoder; +import com.github.pengfeizhou.jscore.JSNull; +import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JavaValue; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Array; /** * @Description: Doric @@ -63,4 +70,45 @@ public class DoricUtils { } } + public static Object toJavaObject(@NonNull Class clz, JSDecoder decoder) throws Exception { + if (clz == JSDecoder.class) { + return decoder; + } else { + return toJavaObject(clz, decoder.decode()); + } + } + + public static Object toJavaObject(@NonNull Class clz, JSValue jsValue) throws Exception { + if (clz == JSValue.class || JSValue.class.isAssignableFrom(clz)) { + return jsValue; + } else if (clz == String.class) { + return jsValue.asString(); + } else if (clz == boolean.class || clz == Boolean.class) { + return jsValue.asBoolean(); + } else if (clz == int.class || clz == Integer.class) { + return jsValue.asNumber().toInt(); + } else if (clz == long.class || clz == Long.class) { + return jsValue.asNumber().toLong(); + } else if (clz == float.class || clz == Float.class) { + return jsValue.asNumber().toFloat(); + } else if (clz == double.class || clz == Double.class) { + return jsValue.asNumber().toDouble(); + } else if (clz.isArray()) { + Class elementClass = clz.getComponentType(); + Object ret; + if (jsValue.isArray()) { + JSArray jsArray = jsValue.asArray(); + ret = Array.newInstance(clz, jsArray.size()); + for (int i = 0; i < jsArray.size(); i++) { + Array.set(ret, i, toJavaObject(elementClass, jsArray.get(i))); + } + } else if (jsValue.isNull()) { + ret = Array.newInstance(clz, 0); + } else { + ret = null; + } + return ret; + } + return null; + } } diff --git a/js-framework/demo.ts b/js-framework/demo.ts index b5518743..2b169d7b 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -19,6 +19,7 @@ log('console', Object.getOwnPropertyNames(console)) setTimeout(() => { log('exec setTimeout') + context.callNative("modal", "toast", "Hello,Doric!") }, 1000) const timerId = setInterval(() => { log('exec setInterval')