android:adjust Doric.java and DoricKit.java,optimize api

This commit is contained in:
pengfei.zhou 2020-02-28 14:56:32 +08:00 committed by osborn
parent 239ce9b5b4
commit 7f1b1256ce
4 changed files with 105 additions and 57 deletions

View File

@ -1,8 +1,24 @@
package pub.doric.devkit; package pub.doric.devkit;
import android.widget.Toast;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; 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 class DevKit implements IDevKit {
public static boolean isRunningInEmulator = false; public static boolean isRunningInEmulator = false;
@ -41,4 +57,56 @@ public class DevKit implements IDevKit {
wsClient.close(); wsClient.close();
wsClient = null; 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);
}
}
}
}
} }

View File

@ -1,24 +1,15 @@
package pub.doric.devkit; package pub.doric.devkit;
import android.widget.Toast; import android.content.Intent;
import com.lahm.library.EasyProtectorLib; import com.lahm.library.EasyProtectorLib;
import com.lahm.library.EmulatorCheckCallback; import com.lahm.library.EmulatorCheckCallback;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import pub.doric.Doric; 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.EOFExceptionEvent;
import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.ui.DoricDevActivity;
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 DoricDev { public class DoricDev {
private static class Inner { private static class Inner {
@ -39,54 +30,17 @@ public class DoricDev {
return Inner.sInstance; return Inner.sInstance;
} }
public boolean devKitConnected = false; public void openDevMode() {
private DoricContextDebuggable doricContextDebuggable; Intent intent = new Intent(Doric.application(), DoricDevActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@Subscribe(threadMode = ThreadMode.MAIN) Doric.application().startActivity(intent);
public void onOpenEvent(OpenEvent openEvent) {
devKitConnected = true;
Toast.makeText(Doric.application(), "dev kit connected", Toast.LENGTH_LONG).show();
} }
@Subscribe(threadMode = ThreadMode.MAIN) public void closeDevMode() {
public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) { EventBus.getDefault().post(new EOFExceptionEvent());
devKitConnected = false;
Toast.makeText(Doric.application(), "dev kit eof exception", Toast.LENGTH_LONG).show();
} }
@Subscribe(threadMode = ThreadMode.MAIN) public boolean isInDevMode() {
public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) { return DevKit.getInstance().devKitConnected;
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);
}
}
}
} }
} }

View File

@ -39,7 +39,7 @@ public class DoricDevActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
if (DoricDev.getInstance().devKitConnected) { if (DoricDev.getInstance().isInDevMode()) {
setContentView(R.layout.layout_debug_context); setContentView(R.layout.layout_debug_context);
initViews(); initViews();
} else { } else {

View File

@ -17,6 +17,9 @@ package pub.doric;
import android.app.Application; import android.app.Application;
import pub.doric.loader.DoricJSLoaderManager;
import pub.doric.loader.IDoricJSLoader;
/** /**
* @Description: Doric * @Description: Doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -25,6 +28,11 @@ import android.app.Application;
public class Doric { public class Doric {
private static Application sApplication; private static Application sApplication;
/**
* Init Function,must be called before doric run
*
* @param application Application instance
*/
public static void init(Application application) { public static void init(Application application) {
sApplication = application; sApplication = application;
} }
@ -32,4 +40,22 @@ public class Doric {
public static Application application() { public static Application application() {
return sApplication; 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);
}
} }