add doric registry

This commit is contained in:
pengfei.zhou 2019-07-20 13:13:35 +08:00
parent bc05a1b8e4
commit 16732340c7
21 changed files with 169 additions and 55 deletions

View File

@ -3,7 +3,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.github.pengfeizhou.doric"
applicationId "com.github.penfeizhou.doric"
minSdkVersion 16
targetSdkVersion 28
versionCode 1

View File

@ -0,0 +1,18 @@
package com.github.penfeizhou.doricdemo;
import com.github.penfeizhou.doric.DoricComponent;
import com.github.penfeizhou.doric.DoricLibrary;
import com.github.penfeizhou.doric.DoricRegistry;
/**
* @Description: com.github.penfeizhou.doricdemo
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
@DoricComponent
public class DemoLibrary extends DoricLibrary {
@Override
public void load(DoricRegistry registry) {
registry.registerNativePlugin(DemoPlugin.class);
}
}

View File

@ -0,0 +1,25 @@
package com.github.penfeizhou.doricdemo;
import android.widget.Toast;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.extension.bridge.DoricMethod;
import com.github.penfeizhou.doric.extension.bridge.DoricPlugin;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
/**
* @Description: com.github.penfeizhou.doricdemo
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
@DoricPlugin(name = "demo")
public class DemoPlugin extends DoricJavaPlugin {
public DemoPlugin(DoricContext doricContext) {
super(doricContext);
}
@DoricMethod
public void test() {
Toast.makeText(getDoricContext().getContext(), "test", Toast.LENGTH_SHORT).show();
}
}

View File

@ -21,6 +21,6 @@ public class ExampleInstrumentedTest {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.github.pengfeizhou.doric.test", appContext.getPackageName());
assertEquals("com.github.penfeizhou.doric.test", appContext.getPackageName());
}
}

View File

@ -2,9 +2,6 @@ package com.github.penfeizhou.doric;
import android.app.Application;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Description: Doric
* @Author: pengfei.zhou
@ -21,13 +18,4 @@ public class Doric {
return sApplication;
}
private static Map<String, String> bundles = new ConcurrentHashMap<>();
public static void registerJSBundle(String name, String bundle) {
bundles.put(name, bundle);
}
public static String getJSBundle(String name) {
return bundles.get(name);
}
}

View File

@ -1,4 +1,4 @@
package com.github.penfeizhou.doric.extension.bridge;
package com.github.penfeizhou.doric;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;

View File

@ -3,7 +3,7 @@ package com.github.penfeizhou.doric;
import android.content.Context;
import com.github.penfeizhou.doric.async.AsyncResult;
import com.github.penfeizhou.doric.plugin.DoricNativePlugin;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.extension.bridge.DoricPluginInfo;
import com.github.pengfeizhou.jscore.JSDecoder;
@ -17,7 +17,7 @@ import java.util.Map;
*/
public class DoricContext {
private final String mContextId;
private final Map<String, DoricNativePlugin> mPluginMap = new HashMap<>();
private final Map<String, DoricJavaPlugin> mPluginMap = new HashMap<>();
private final Context mContext;
DoricContext(Context context, String contextId) {
@ -64,8 +64,8 @@ public class DoricContext {
});
}
public DoricNativePlugin obtainPlugin(DoricPluginInfo doricPluginInfo) {
DoricNativePlugin plugin = mPluginMap.get(doricPluginInfo.getName());
public DoricJavaPlugin obtainPlugin(DoricPluginInfo doricPluginInfo) {
DoricJavaPlugin plugin = mPluginMap.get(doricPluginInfo.getName());
if (plugin == null) {
plugin = doricPluginInfo.createPlugin(this);
mPluginMap.put(doricPluginInfo.getName(), plugin);

View File

@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Description: com.github.pengfeizhou.doric
* @Description: com.github.penfeizhou.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/

View File

@ -0,0 +1,10 @@
package com.github.penfeizhou.doric;
/**
* @Description: com.github.penfeizhou.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
public abstract class DoricLibrary {
public abstract void load(DoricRegistry registry);
}

View File

@ -0,0 +1,59 @@
package com.github.penfeizhou.doric;
import android.text.TextUtils;
import com.github.penfeizhou.doric.extension.bridge.DoricPluginInfo;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.plugin.ModalPlugin;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Description: com.github.penfeizhou.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
public class DoricRegistry {
private static Map<String, String> bundles = new ConcurrentHashMap<>();
private Map<String, DoricPluginInfo> pluginInfoMap = new HashMap<>();
private static Set<DoricLibrary> doricLibraries = new HashSet<>();
private static void initRegistry(DoricRegistry doricRegistry) {
for (DoricLibrary library : doricLibraries) {
library.load(doricRegistry);
}
}
public static void register(DoricLibrary doricLibrary) {
doricLibraries.add(doricLibrary);
}
public DoricRegistry() {
this.registerNativePlugin(ModalPlugin.class);
initRegistry(this);
}
public void registerJSBundle(String name, String bundle) {
bundles.put(name, bundle);
}
public void registerNativePlugin(Class<? extends DoricJavaPlugin> pluginClass) {
DoricPluginInfo doricPluginInfo = new DoricPluginInfo(pluginClass);
if (!TextUtils.isEmpty(doricPluginInfo.getName())) {
pluginInfoMap.put(doricPluginInfo.getName(), doricPluginInfo);
}
}
public DoricPluginInfo acquirePluginInfo(String name) {
return pluginInfoMap.get(name);
}
public String acquireJSBundle(String name) {
return bundles.get(name);
}
}

View File

@ -8,7 +8,7 @@ import com.github.pengfeizhou.jscore.JSDecoder;
import java.util.concurrent.Callable;
/**
* @Description: com.github.pengfeizhou.doric
* @Description: com.github.penfeizhou.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/

View File

@ -7,7 +7,7 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
/**
* @Description: com.github.pengfeizhou.doric.async
* @Description: com.github.penfeizhou.doric.async
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/

View File

@ -1,7 +1,7 @@
package com.github.penfeizhou.doric.async;
/**
* @Description: com.github.pengfeizhou.doric.async
* @Description: com.github.penfeizhou.doric.async
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/

View File

@ -1,7 +1,7 @@
package com.github.penfeizhou.doric.async;
/**
* @Description: com.github.pengfeizhou.doric.async
* @Description: com.github.penfeizhou.doric.async
* @Author: pengfei.zhou
* @CreateDate: 2019-07-18
*/

View File

@ -6,7 +6,7 @@ import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import com.github.penfeizhou.doric.Doric;
import com.github.penfeizhou.doric.DoricRegistry;
import com.github.penfeizhou.doric.extension.bridge.DoricBridgeExtension;
import com.github.penfeizhou.doric.extension.timer.DoricTimerExtension;
import com.github.penfeizhou.doric.utils.DoricConstant;
@ -28,6 +28,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
private final DoricBridgeExtension mDoricBridgeExtension;
private IDoricJSE mDoricJSE;
private DoricTimerExtension mTimerExtension;
private DoricRegistry mDoricRegistry = new DoricRegistry();
public DoricJSEngine() {
HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName());
@ -80,7 +81,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
public JavaValue exec(JSDecoder[] args) {
try {
String name = args[0].string();
String content = Doric.getJSBundle(name);
String content = mDoricRegistry.acquireJSBundle(name);
if (TextUtils.isEmpty(content)) {
DoricLog.e("require js bundle:%s is empty", name);
return new JavaValue(false);

View File

@ -3,9 +3,9 @@ package com.github.penfeizhou.doric.extension.bridge;
import android.text.TextUtils;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.async.AsyncCall;
import com.github.penfeizhou.doric.DoricRegistry;
import com.github.penfeizhou.doric.async.AsyncResult;
import com.github.penfeizhou.doric.plugin.DoricNativePlugin;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.plugin.ModalPlugin;
import com.github.penfeizhou.doric.DoricContextManager;
import com.github.penfeizhou.doric.utils.DoricLog;
@ -24,28 +24,22 @@ import java.util.concurrent.Callable;
* @CreateDate: 2019-07-18
*/
public class DoricBridgeExtension {
private Map<String, DoricPluginInfo> pluginInfoMap = new HashMap<>();
public DoricBridgeExtension() {
registerExtension(ModalPlugin.class);
}
private final DoricRegistry mRegistry;
public void registerExtension(Class<? extends DoricNativePlugin> pluginClass) {
DoricPluginInfo doricPluginInfo = new DoricPluginInfo(pluginClass);
if (!TextUtils.isEmpty(doricPluginInfo.getName())) {
pluginInfoMap.put(doricPluginInfo.getName(), doricPluginInfo);
}
public DoricBridgeExtension(DoricRegistry doricRegistry) {
mRegistry = doricRegistry;
}
public JavaValue callNative(String contextId, String module, String methodName, final String callbackId, final JSDecoder jsDecoder) {
final DoricContext context = DoricContextManager.getContext(contextId);
DoricPluginInfo pluginInfo = pluginInfoMap.get(module);
DoricPluginInfo pluginInfo = mRegistry.acquirePluginInfo(module);
if (pluginInfo == null) {
DoricLog.e("Cannot find plugin class:%s", module);
return new JavaValue(false);
}
final DoricNativePlugin doricNativePlugin = context.obtainPlugin(pluginInfo);
if (doricNativePlugin == null) {
final DoricJavaPlugin doricJavaPlugin = context.obtainPlugin(pluginInfo);
if (doricJavaPlugin == null) {
DoricLog.e("Cannot obtain plugin instance:%s,method:%", module);
return new JavaValue(false);
}
@ -61,11 +55,11 @@ public class DoricBridgeExtension {
Class[] classes = method.getParameterTypes();
Object ret;
if (classes.length == 0) {
ret = method.invoke(doricNativePlugin);
ret = method.invoke(doricJavaPlugin);
} else if (classes.length == 1) {
ret = method.invoke(doricNativePlugin, createParam(context, classes[0], callbackId, jsDecoder));
ret = method.invoke(doricJavaPlugin, createParam(context, classes[0], callbackId, jsDecoder));
} else {
ret = method.invoke(doricNativePlugin,
ret = method.invoke(doricJavaPlugin,
createParam(context, classes[0], callbackId, jsDecoder),
createParam(context, classes[1], callbackId, jsDecoder));
}

View File

@ -0,0 +1,19 @@
package com.github.penfeizhou.doric.extension.bridge;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Description: Doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-18
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DoricPlugin {
String name() default "";
}

View File

@ -3,7 +3,7 @@ package com.github.penfeizhou.doric.extension.bridge;
import android.text.TextUtils;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.plugin.DoricNativePlugin;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.utils.DoricLog;
import java.lang.reflect.Constructor;
@ -18,16 +18,16 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class DoricPluginInfo {
private Constructor<? extends DoricNativePlugin> pluginConstructor;
private Constructor<? extends DoricJavaPlugin> pluginConstructor;
private Map<String, Method> methodMap = new ConcurrentHashMap<>();
private String name;
public DoricPluginInfo(Class<? extends DoricNativePlugin> pluginClass) {
public DoricPluginInfo(Class<? extends DoricJavaPlugin> pluginClass) {
try {
this.pluginConstructor = pluginClass.getDeclaredConstructor(DoricContext.class);
DoricComponent doricComponent = pluginClass.getAnnotation(DoricComponent.class);
this.name = doricComponent.name();
DoricPlugin doricPlugin = pluginClass.getAnnotation(DoricPlugin.class);
this.name = doricPlugin.name();
Method[] methods = pluginClass.getMethods();
for (Method method : methods) {
DoricMethod doricMethod = method.getAnnotation(DoricMethod.class);
@ -48,7 +48,7 @@ public class DoricPluginInfo {
return name;
}
public DoricNativePlugin createPlugin(DoricContext doricContext) {
public DoricJavaPlugin createPlugin(DoricContext doricContext) {
try {
return pluginConstructor.newInstance(doricContext);
} catch (Exception e) {

View File

@ -5,7 +5,7 @@ import com.github.penfeizhou.doric.utils.DoricConstant;
import com.github.pengfeizhou.jscore.JavaValue;
/**
* @Description: com.github.pengfeizhou.doric.extension.bridge
* @Description: com.github.penfeizhou.doric.extension.bridge
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/

View File

@ -7,10 +7,10 @@ import com.github.penfeizhou.doric.DoricContext;
* @Author: pengfei.zhou
* @CreateDate: 2019-07-18
*/
public abstract class DoricNativePlugin {
public abstract class DoricJavaPlugin {
private final DoricContext doricContext;
public DoricNativePlugin(DoricContext doricContext) {
public DoricJavaPlugin(DoricContext doricContext) {
this.doricContext = doricContext;
}

View File

@ -3,7 +3,7 @@ package com.github.penfeizhou.doric.plugin;
import android.widget.Toast;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.extension.bridge.DoricComponent;
import com.github.penfeizhou.doric.extension.bridge.DoricPlugin;
import com.github.penfeizhou.doric.extension.bridge.DoricMethod;
import com.github.penfeizhou.doric.extension.bridge.DoricPromise;
import com.github.penfeizhou.doric.utils.ThreadMode;
@ -15,8 +15,8 @@ import com.github.pengfeizhou.jscore.JSDecoder;
* @Author: pengfei.zhou
* @CreateDate: 2019-07-18
*/
@DoricComponent(name = "modal")
public class ModalPlugin extends DoricNativePlugin {
@DoricPlugin(name = "modal")
public class ModalPlugin extends DoricJavaPlugin {
public ModalPlugin(DoricContext doricContext) {
super(doricContext);