From 7f1b1256ce43b2dfbc035e8e2669422a3c2f0b7c Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 28 Feb 2020 14:56:32 +0800 Subject: [PATCH] android:adjust Doric.java and DoricKit.java,optimize api --- .../main/java/pub/doric/devkit/DevKit.java | 68 +++++++++++++++++++ .../main/java/pub/doric/devkit/DoricDev.java | 66 +++--------------- .../pub/doric/devkit/ui/DoricDevActivity.java | 2 +- .../doric/src/main/java/pub/doric/Doric.java | 26 +++++++ 4 files changed, 105 insertions(+), 57 deletions(-) diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java index 332b171d..42d06f03 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java @@ -1,8 +1,24 @@ package pub.doric.devkit; +import android.widget.Toast; + import com.google.gson.Gson; import com.google.gson.JsonObject; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import pub.doric.Doric; +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; +import pub.doric.devkit.event.ConnectExceptionEvent; +import pub.doric.devkit.event.EOFExceptionEvent; +import pub.doric.devkit.event.EnterDebugEvent; +import pub.doric.devkit.event.OpenEvent; +import pub.doric.devkit.event.ReloadEvent; +import pub.doric.devkit.event.StartDebugEvent; +import pub.doric.devkit.event.StopDebugEvent; + public class DevKit implements IDevKit { public static boolean isRunningInEmulator = false; @@ -41,4 +57,56 @@ public class DevKit implements IDevKit { wsClient.close(); wsClient = null; } + + boolean devKitConnected = false; + + private DoricContextDebuggable doricContextDebuggable; + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onOpenEvent(OpenEvent openEvent) { + devKitConnected = true; + Toast.makeText(Doric.application(), "dev kit connected", Toast.LENGTH_LONG).show(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) { + devKitConnected = false; + Toast.makeText(Doric.application(), "dev kit eof exception", Toast.LENGTH_LONG).show(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) { + devKitConnected = false; + Toast.makeText(Doric.application(), "dev kit connection exception", Toast.LENGTH_LONG).show(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onStartDebugEvent(StartDebugEvent startDebugEvent) { + doricContextDebuggable = new DoricContextDebuggable(startDebugEvent.getContextId()); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { + doricContextDebuggable.startDebug(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) { + doricContextDebuggable.stopDebug(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onReloadEvent(ReloadEvent reloadEvent) { + for (DoricContext context : DoricContextManager.aliveContexts()) { + if (reloadEvent.source.contains(context.getSource())) { + if (doricContextDebuggable != null && + doricContextDebuggable.isDebugging && + doricContextDebuggable.getContext().getContextId().equals(context.getContextId())) { + System.out.println("is debugging context id: " + context.getContextId()); + } else { + context.reload(reloadEvent.script); + } + } + } + } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java index 859c6e4d..21bd2308 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java @@ -1,24 +1,15 @@ package pub.doric.devkit; -import android.widget.Toast; +import android.content.Intent; import com.lahm.library.EasyProtectorLib; import com.lahm.library.EmulatorCheckCallback; import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.ThreadMode; import pub.doric.Doric; -import pub.doric.DoricContext; -import pub.doric.DoricContextManager; -import pub.doric.devkit.event.ConnectExceptionEvent; import pub.doric.devkit.event.EOFExceptionEvent; -import pub.doric.devkit.event.EnterDebugEvent; -import pub.doric.devkit.event.OpenEvent; -import pub.doric.devkit.event.ReloadEvent; -import pub.doric.devkit.event.StartDebugEvent; -import pub.doric.devkit.event.StopDebugEvent; +import pub.doric.devkit.ui.DoricDevActivity; public class DoricDev { private static class Inner { @@ -39,54 +30,17 @@ public class DoricDev { return Inner.sInstance; } - public boolean devKitConnected = false; - private DoricContextDebuggable doricContextDebuggable; - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onOpenEvent(OpenEvent openEvent) { - devKitConnected = true; - Toast.makeText(Doric.application(), "dev kit connected", Toast.LENGTH_LONG).show(); + public void openDevMode() { + Intent intent = new Intent(Doric.application(), DoricDevActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Doric.application().startActivity(intent); } - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) { - devKitConnected = false; - Toast.makeText(Doric.application(), "dev kit eof exception", Toast.LENGTH_LONG).show(); + public void closeDevMode() { + EventBus.getDefault().post(new EOFExceptionEvent()); } - @Subscribe(threadMode = ThreadMode.MAIN) - public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) { - devKitConnected = false; - Toast.makeText(Doric.application(), "dev kit connection exception", Toast.LENGTH_LONG).show(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onStartDebugEvent(StartDebugEvent startDebugEvent) { - doricContextDebuggable = new DoricContextDebuggable(startDebugEvent.getContextId()); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { - doricContextDebuggable.startDebug(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) { - doricContextDebuggable.stopDebug(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onReloadEvent(ReloadEvent reloadEvent) { - for (DoricContext context : DoricContextManager.aliveContexts()) { - if (reloadEvent.source.contains(context.getSource())) { - if (doricContextDebuggable != null && - doricContextDebuggable.isDebugging && - doricContextDebuggable.getContext().getContextId().equals(context.getContextId())) { - System.out.println("is debugging context id: " + context.getContextId()); - } else { - context.reload(reloadEvent.script); - } - } - } + public boolean isInDevMode() { + return DevKit.getInstance().devKitConnected; } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java index 64e10489..67bf9cff 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -39,7 +39,7 @@ public class DoricDevActivity extends AppCompatActivity { super.onCreate(savedInstanceState); EventBus.getDefault().register(this); - if (DoricDev.getInstance().devKitConnected) { + if (DoricDev.getInstance().isInDevMode()) { setContentView(R.layout.layout_debug_context); initViews(); } else { diff --git a/doric-android/doric/src/main/java/pub/doric/Doric.java b/doric-android/doric/src/main/java/pub/doric/Doric.java index 5ecdfec7..a17124fb 100644 --- a/doric-android/doric/src/main/java/pub/doric/Doric.java +++ b/doric-android/doric/src/main/java/pub/doric/Doric.java @@ -17,6 +17,9 @@ package pub.doric; import android.app.Application; +import pub.doric.loader.DoricJSLoaderManager; +import pub.doric.loader.IDoricJSLoader; + /** * @Description: Doric * @Author: pengfei.zhou @@ -25,6 +28,11 @@ import android.app.Application; public class Doric { private static Application sApplication; + /** + * Init Function,must be called before doric run + * + * @param application Application instance + */ public static void init(Application application) { sApplication = application; } @@ -32,4 +40,22 @@ public class Doric { public static Application application() { return sApplication; } + + /** + * Register DoricLibrary For Extended ViewNode and Native Plugins + * + * @param doricLibrary Which registered in global + */ + public static void registerLibrary(DoricLibrary doricLibrary) { + DoricRegistry.register(doricLibrary); + } + + /** + * Add DoricJSLoader For Loading JS bundles + * + * @param jsLoader Which added in global + */ + public static void addJSLoader(IDoricJSLoader jsLoader) { + DoricJSLoaderManager.getInstance().addJSLoader(jsLoader); + } }