diff --git a/Android/app/build.gradle b/Android/app/build.gradle index d17cfdde..21194703 100644 --- a/Android/app/build.gradle +++ b/Android/app/build.gradle @@ -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 diff --git a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoLibrary.java b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoLibrary.java new file mode 100644 index 00000000..1f441125 --- /dev/null +++ b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoLibrary.java @@ -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); + } +} diff --git a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java new file mode 100644 index 00000000..38be8d6c --- /dev/null +++ b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/DemoPlugin.java @@ -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(); + } +} diff --git a/Android/doric/src/androidTest/java/com/github/penfeizhou/doric/test/ExampleInstrumentedTest.java b/Android/doric/src/androidTest/java/com/github/penfeizhou/doric/test/ExampleInstrumentedTest.java index 41ef6112..80085ed4 100644 --- a/Android/doric/src/androidTest/java/com/github/penfeizhou/doric/test/ExampleInstrumentedTest.java +++ b/Android/doric/src/androidTest/java/com/github/penfeizhou/doric/test/ExampleInstrumentedTest.java @@ -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()); } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java index 66154fe6..5ec14f8c 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java @@ -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 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); - } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricComponent.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricComponent.java similarity index 88% rename from Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricComponent.java rename to Android/doric/src/main/java/com/github/penfeizhou/doric/DoricComponent.java index 16726f10..c0adf11b 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricComponent.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricComponent.java @@ -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; diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java index c34882ec..52d9b9d8 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java @@ -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 mPluginMap = new HashMap<>(); + private final Map 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); diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java index 1249c3f6..a531397f 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java @@ -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 */ diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricLibrary.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricLibrary.java new file mode 100644 index 00000000..8ce5cb3b --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricLibrary.java @@ -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); +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java new file mode 100644 index 00000000..f4337499 --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java @@ -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 bundles = new ConcurrentHashMap<>(); + private Map pluginInfoMap = new HashMap<>(); + private static Set 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 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); + } +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java index b902ef02..008daf8d 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java @@ -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 */ diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncCall.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncCall.java index d5f23cab..8b247bbf 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncCall.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncCall.java @@ -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 */ diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncResult.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncResult.java index ac5a6af5..f9c1df41 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncResult.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/async/AsyncResult.java @@ -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 */ diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/async/SettableFuture.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/async/SettableFuture.java index 589e40f4..c0355a6a 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/async/SettableFuture.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/async/SettableFuture.java @@ -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 */ diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java index fd966a4f..d44669af 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java @@ -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); diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java index 7ae86468..d3a84f72 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java @@ -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 pluginInfoMap = new HashMap<>(); - public DoricBridgeExtension() { - registerExtension(ModalPlugin.class); - } + private final DoricRegistry mRegistry; - public void registerExtension(Class 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)); } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPlugin.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPlugin.java new file mode 100644 index 00000000..037ac69d --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPlugin.java @@ -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 ""; +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPluginInfo.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPluginInfo.java index d84d57da..3ec0101a 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPluginInfo.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPluginInfo.java @@ -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 pluginConstructor; + private Constructor pluginConstructor; private Map methodMap = new ConcurrentHashMap<>(); private String name; - public DoricPluginInfo(Class pluginClass) { + public DoricPluginInfo(Class 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) { diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java index a17677cc..1a94c72a 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricPromise.java @@ -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 */ diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/DoricNativePlugin.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/DoricJavaPlugin.java similarity index 77% rename from Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/DoricNativePlugin.java rename to Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/DoricJavaPlugin.java index cb4511f9..d4b2e6b6 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/DoricNativePlugin.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/DoricJavaPlugin.java @@ -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; } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/ModalPlugin.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/ModalPlugin.java index 7da68c09..147e6547 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/ModalPlugin.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/plugin/ModalPlugin.java @@ -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);