pass callNativePlugin
This commit is contained in:
parent
36b478bfb3
commit
64ba346d38
@ -12,7 +12,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
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");
|
doricContext.callEntity("log");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.github.pengfeizhou.doric;
|
package com.github.pengfeizhou.doric;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
import com.github.pengfeizhou.doric.async.AsyncResult;
|
import com.github.pengfeizhou.doric.async.AsyncResult;
|
||||||
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
|
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
|
||||||
import com.github.pengfeizhou.doric.extension.bridge.DoricPluginInfo;
|
import com.github.pengfeizhou.doric.extension.bridge.DoricPluginInfo;
|
||||||
@ -16,13 +18,15 @@ import java.util.Map;
|
|||||||
public class DoricContext {
|
public class DoricContext {
|
||||||
private final String mContextId;
|
private final String mContextId;
|
||||||
private final Map<String, DoricNativePlugin> mPluginMap = new HashMap<>();
|
private final Map<String, DoricNativePlugin> mPluginMap = new HashMap<>();
|
||||||
|
private final Context mContext;
|
||||||
|
|
||||||
DoricContext(String contextId) {
|
DoricContext(Context context, String contextId) {
|
||||||
|
this.mContext = context;
|
||||||
this.mContextId = contextId;
|
this.mContextId = contextId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DoricContext createContext(String script, String alias) {
|
public static DoricContext create(Context context, String script, String alias) {
|
||||||
return DoricContextManager.getInstance().createContext(script, alias);
|
return DoricContextManager.getInstance().createContext(context, script, alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) {
|
public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) {
|
||||||
@ -33,6 +37,9 @@ public class DoricContext {
|
|||||||
return DoricDriver.getInstance();
|
return DoricDriver.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Context getContext() {
|
||||||
|
return mContext;
|
||||||
|
}
|
||||||
|
|
||||||
public String getContextId() {
|
public String getContextId() {
|
||||||
return mContextId;
|
return mContextId;
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.github.pengfeizhou.doric;
|
package com.github.pengfeizhou.doric;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
import com.github.pengfeizhou.doric.async.AsyncCall;
|
import com.github.pengfeizhou.doric.async.AsyncCall;
|
||||||
import com.github.pengfeizhou.doric.async.AsyncResult;
|
import com.github.pengfeizhou.doric.async.AsyncResult;
|
||||||
import com.github.pengfeizhou.doric.utils.DoricLog;
|
import com.github.pengfeizhou.doric.utils.DoricLog;
|
||||||
@ -31,9 +33,9 @@ public class DoricContextManager {
|
|||||||
return Inner.sInstance;
|
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 String contextId = String.valueOf(counter.incrementAndGet());
|
||||||
final DoricContext doricContext = new DoricContext(contextId);
|
final DoricContext doricContext = new DoricContext(context, contextId);
|
||||||
doricContextMap.put(contextId, doricContext);
|
doricContextMap.put(contextId, doricContext);
|
||||||
doricContext.getDriver().createContext(contextId, script, source);
|
doricContext.getDriver().createContext(contextId, script, source);
|
||||||
return doricContext;
|
return doricContext;
|
||||||
|
@ -35,7 +35,7 @@ public class DoricPanel extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void config(String script, String alias) {
|
public void config(String script, String alias) {
|
||||||
DoricContext doricContext = DoricContext.createContext(script, alias);
|
DoricContext doricContext = DoricContext.create(getContext(), script, alias);
|
||||||
config(doricContext);
|
config(doricContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,13 +60,13 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
|
|||||||
String message = args[1].string();
|
String message = args[1].string();
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "w":
|
case "w":
|
||||||
DoricLog.w("_js", message);
|
DoricLog.suffix_w("_js", message);
|
||||||
break;
|
break;
|
||||||
case "e":
|
case "e":
|
||||||
DoricLog.e("_js", message);
|
DoricLog.suffix_e("_js", message);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
DoricLog.d("_js", message);
|
DoricLog.suffix_d("_js", message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -2,15 +2,18 @@ package com.github.pengfeizhou.doric.extension.bridge;
|
|||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
import com.github.pengfeizhou.doric.Doric;
|
||||||
import com.github.pengfeizhou.doric.DoricContext;
|
import com.github.pengfeizhou.doric.DoricContext;
|
||||||
import com.github.pengfeizhou.doric.DoricContextManager;
|
import com.github.pengfeizhou.doric.DoricContextManager;
|
||||||
import com.github.pengfeizhou.doric.DoricDriver;
|
import com.github.pengfeizhou.doric.DoricDriver;
|
||||||
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
|
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
|
||||||
import com.github.pengfeizhou.doric.plugin.ModalPlugin;
|
import com.github.pengfeizhou.doric.plugin.ModalPlugin;
|
||||||
import com.github.pengfeizhou.doric.utils.DoricLog;
|
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.JSDecoder;
|
||||||
import com.github.pengfeizhou.jscore.JavaValue;
|
import com.github.pengfeizhou.jscore.JavaValue;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -52,10 +55,35 @@ public class DoricBridgeExtension {
|
|||||||
return new JavaValue(false);
|
return new JavaValue(false);
|
||||||
}
|
}
|
||||||
DoricMethod doricMethod = method.getAnnotation(DoricMethod.class);
|
DoricMethod doricMethod = method.getAnnotation(DoricMethod.class);
|
||||||
|
try {
|
||||||
Class[] classes = method.getParameterTypes();
|
Class[] classes = method.getParameterTypes();
|
||||||
|
Object ret;
|
||||||
|
if (classes.length == 0) {
|
||||||
return new JavaValue(true);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ public class DoricPluginInfo {
|
|||||||
|
|
||||||
public DoricPluginInfo(Class<? extends DoricNativePlugin> pluginClass) {
|
public DoricPluginInfo(Class<? extends DoricNativePlugin> pluginClass) {
|
||||||
try {
|
try {
|
||||||
this.pluginConstructor = pluginClass.getConstructor(DoricContext.class);
|
this.pluginConstructor = pluginClass.getDeclaredConstructor(DoricContext.class);
|
||||||
DoricComponent doricComponent = pluginClass.getAnnotation(DoricComponent.class);
|
DoricComponent doricComponent = pluginClass.getAnnotation(DoricComponent.class);
|
||||||
this.name = doricComponent.name();
|
this.name = doricComponent.name();
|
||||||
Method[] methods = pluginClass.getMethods();
|
Method[] methods = pluginClass.getMethods();
|
||||||
|
@ -10,7 +10,7 @@ import com.github.pengfeizhou.doric.DoricContext;
|
|||||||
public abstract class DoricNativePlugin {
|
public abstract class DoricNativePlugin {
|
||||||
private final DoricContext doricContext;
|
private final DoricContext doricContext;
|
||||||
|
|
||||||
protected DoricNativePlugin(DoricContext doricContext) {
|
public DoricNativePlugin(DoricContext doricContext) {
|
||||||
this.doricContext = doricContext;
|
this.doricContext = doricContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package com.github.pengfeizhou.doric.plugin;
|
package com.github.pengfeizhou.doric.plugin;
|
||||||
|
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.github.pengfeizhou.doric.DoricContext;
|
import com.github.pengfeizhou.doric.DoricContext;
|
||||||
import com.github.pengfeizhou.doric.extension.bridge.DoricComponent;
|
import com.github.pengfeizhou.doric.extension.bridge.DoricComponent;
|
||||||
import com.github.pengfeizhou.doric.extension.bridge.DoricMethod;
|
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;
|
import com.github.pengfeizhou.jscore.JSDecoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -13,12 +17,16 @@ import com.github.pengfeizhou.jscore.JSDecoder;
|
|||||||
@DoricComponent(name = "modal")
|
@DoricComponent(name = "modal")
|
||||||
public class ModalPlugin extends DoricNativePlugin {
|
public class ModalPlugin extends DoricNativePlugin {
|
||||||
|
|
||||||
protected ModalPlugin(DoricContext doricContext) {
|
public ModalPlugin(DoricContext doricContext) {
|
||||||
super(doricContext);
|
super(doricContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DoricMethod(name = "toast", thread = DoricMethod.Mode.UI)
|
@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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,16 +27,15 @@ public class DoricLog {
|
|||||||
Log.e(suffixTag(null), format(message, args));
|
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));
|
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));
|
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));
|
Log.e(suffixTag(suffix), format(message, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,14 +3,21 @@ package com.github.pengfeizhou.doric.utils;
|
|||||||
import android.content.res.AssetManager;
|
import android.content.res.AssetManager;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
import com.github.pengfeizhou.doric.Doric;
|
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 com.github.pengfeizhou.jscore.JavaValue;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: Doric
|
* @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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ log('console', Object.getOwnPropertyNames(console))
|
|||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
log('exec setTimeout')
|
log('exec setTimeout')
|
||||||
|
context.callNative("modal", "toast", "Hello,Doric!")
|
||||||
}, 1000)
|
}, 1000)
|
||||||
const timerId = setInterval(() => {
|
const timerId = setInterval(() => {
|
||||||
log('exec setInterval')
|
log('exec setInterval')
|
||||||
|
Reference in New Issue
Block a user