diff --git a/Android/app/src/main/java/pub/doric/demo/MainActivity.java b/Android/app/src/main/java/pub/doric/demo/MainActivity.java index 438c291c..3900c3cc 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -22,24 +22,31 @@ import android.widget.FrameLayout; import androidx.appcompat.app.AppCompatActivity; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + import java.io.IOException; -import pub.doric.dev.DevPanel; -import pub.doric.Doric; import pub.doric.DoricContext; +import pub.doric.DoricDriver; +import pub.doric.dev.DevPanel; import pub.doric.dev.LocalServer; +import pub.doric.dev.event.EnterDebugEvent; import pub.doric.utils.DoricUtils; public class MainActivity extends AppCompatActivity { private DevPanel mDevPanel = new DevPanel(); + private DoricContext doricContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); - DoricContext doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/Snake.js"), "test"); + doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/Snake.js"), "test"); doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); // doricContext.callEntity("log"); doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); @@ -52,6 +59,28 @@ public class MainActivity extends AppCompatActivity { } } + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + + EventBus.getDefault().register(this); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + EventBus.getDefault().unregister(this); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { + ((FrameLayout) findViewById(R.id.root)).removeAllViews(); + DoricDriver.getInstance().changeJSEngine(false); + doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); + } + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { diff --git a/Android/doric/build.gradle b/Android/doric/build.gradle index 24523a49..48f388b7 100644 --- a/Android/doric/build.gradle +++ b/Android/doric/build.gradle @@ -63,7 +63,7 @@ dependencies { implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.7' implementation 'com.github.tbruyelle:rxpermissions:0.10.2' implementation "io.reactivex.rxjava2:rxjava:2.2.14" - implementation 'org.greenrobot:eventbus:3.1.1' + api 'org.greenrobot:eventbus:3.1.1' implementation 'com.lahm.library:easy-protector-release:1.1.0' testImplementation 'junit:junit:4.12' diff --git a/Android/doric/src/main/java/pub/doric/DoricDriver.java b/Android/doric/src/main/java/pub/doric/DoricDriver.java index c1a4c1e8..b831d5e4 100644 --- a/Android/doric/src/main/java/pub/doric/DoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/DoricDriver.java @@ -157,4 +157,8 @@ public class DoricDriver implements IDoricDriver { wsClient.close(); wsClient = null; } + + public void changeJSEngine(boolean isNative) { + doricJSEngine.changeJSEngine(isNative); + } } diff --git a/Android/doric/src/main/java/pub/doric/dev/WSClient.java b/Android/doric/src/main/java/pub/doric/dev/WSClient.java index 7eec5207..9b2d4143 100644 --- a/Android/doric/src/main/java/pub/doric/dev/WSClient.java +++ b/Android/doric/src/main/java/pub/doric/dev/WSClient.java @@ -30,6 +30,7 @@ import okhttp3.WebSocketListener; import pub.doric.DoricContext; import pub.doric.DoricContextManager; import pub.doric.dev.event.EOFEvent; +import pub.doric.dev.event.EnterDebugEvent; import pub.doric.dev.event.OpenEvent; /** @@ -67,13 +68,24 @@ public class WSClient extends WebSocketListener { super.onMessage(webSocket, text); try { JSONObject jsonObject = new JSONObject(text); - String source = jsonObject.optString("source"); - String script = jsonObject.optString("script"); - for (DoricContext context : DoricContextManager.aliveContexts()) { - if (source.contains(context.getSource())) { - context.reload(script); + String cmd = jsonObject.optString("cmd"); + switch (cmd) { + case "RELOAD": { + String source = jsonObject.optString("source"); + String script = jsonObject.optString("script"); + for (DoricContext context : DoricContextManager.aliveContexts()) { + if (source.contains(context.getSource())) { + context.reload(script); + } + } } + break; + case "SWITCH_TO_DEBUG": { + EventBus.getDefault().post(new EnterDebugEvent()); + } + break; } + } catch (JSONException e) { e.printStackTrace(); } diff --git a/Android/doric/src/main/java/pub/doric/dev/event/EnterDebugEvent.java b/Android/doric/src/main/java/pub/doric/dev/event/EnterDebugEvent.java new file mode 100644 index 00000000..2cb0f41c --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/dev/event/EnterDebugEvent.java @@ -0,0 +1,4 @@ +package pub.doric.dev.event; + +public class EnterDebugEvent { +} diff --git a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index c7cc65e0..7d7896bf 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -21,6 +21,12 @@ import android.os.Looper; import android.os.Message; import android.text.TextUtils; +import com.github.pengfeizhou.jscore.JSDecoder; +import com.github.pengfeizhou.jscore.JavaFunction; +import com.github.pengfeizhou.jscore.JavaValue; + +import java.util.ArrayList; + import pub.doric.DoricRegistry; import pub.doric.extension.bridge.DoricBridgeExtension; import pub.doric.extension.timer.DoricTimerExtension; @@ -28,12 +34,6 @@ import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricLog; import pub.doric.utils.DoricUtils; -import com.github.pengfeizhou.jscore.JSDecoder; -import com.github.pengfeizhou.jscore.JavaFunction; -import com.github.pengfeizhou.jscore.JavaValue; - -import java.util.ArrayList; - /** * @Description: Doric * @Author: pengfei.zhou @@ -54,7 +54,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time mJSHandler.post(new Runnable() { @Override public void run() { - initJSExecutor(); + mDoricJSE = new DoricNativeJSExecutor(); + injectGlobal(); initDoricRuntime(); } }); @@ -65,10 +66,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time return mJSHandler; } - - private void initJSExecutor() { - mDoricJSE = new DoricNativeJSExecutor(); -// mDoricJSE = new DoricRemoteJSExecutor(); + private void injectGlobal() { mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() { @Override public JavaValue exec(JSDecoder[] args) { @@ -212,4 +210,14 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time public DoricRegistry getRegistry() { return mDoricRegistry; } + + public void changeJSEngine(boolean isNative) { + mDoricJSE.teardown(); + if (isNative) { + mDoricJSE = new DoricNativeJSExecutor(); + } else { + mDoricJSE = new DoricRemoteJSExecutor(); + } + injectGlobal(); + } } diff --git a/doric-cli/scripts/watcher.js b/doric-cli/scripts/watcher.js index 17d7f284..85257b0d 100644 --- a/doric-cli/scripts/watcher.js +++ b/doric-cli/scripts/watcher.js @@ -19,6 +19,7 @@ setTimeout(() => { const sourceMap = doMerge(path + ".map") ws.connections.forEach(e => { e.sendText(JSON.stringify({ + cmd: 'RELOAD', script: data, source: path.match(/[^/\\]*$/)[0], sourceMap,