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 ece1b1a1..ada3a72a 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -33,6 +33,8 @@ import pub.doric.DoricDriver; import pub.doric.dev.DevPanel; import pub.doric.dev.LocalServer; import pub.doric.dev.event.EnterDebugEvent; +import pub.doric.dev.event.QuitDebugEvent; +import pub.doric.engine.ChangeEngineCallback; import pub.doric.utils.DoricUtils; @@ -76,8 +78,32 @@ public class MainActivity extends AppCompatActivity { @Subscribe(threadMode = ThreadMode.MAIN) public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { - DoricDriver.getInstance().changeJSEngine(false); - doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + DoricDriver.getInstance().changeJSEngine(false, new ChangeEngineCallback() { + @Override + public void changed() { + runOnUiThread(new Runnable() { + @Override + public void run() { + doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + } + }); + } + }); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) { + DoricDriver.getInstance().changeJSEngine(true, new ChangeEngineCallback() { + @Override + public void changed() { + runOnUiThread(new Runnable() { + @Override + public void run() { + doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + } + }); + } + }); } @Override diff --git a/Android/doric/src/main/java/pub/doric/DoricDriver.java b/Android/doric/src/main/java/pub/doric/DoricDriver.java index d4d562b8..c8c8f47e 100644 --- a/Android/doric/src/main/java/pub/doric/DoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/DoricDriver.java @@ -26,6 +26,7 @@ import java.util.concurrent.Executors; import pub.doric.async.AsyncCall; import pub.doric.async.AsyncResult; +import pub.doric.engine.ChangeEngineCallback; import pub.doric.engine.DoricJSEngine; import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricLog; @@ -135,7 +136,7 @@ public class DoricDriver implements IDoricDriver { return doricJSEngine.getRegistry(); } - public void changeJSEngine(boolean isNative) { - doricJSEngine.changeJSEngine(isNative); + public void changeJSEngine(boolean isNative, ChangeEngineCallback changeEngineCallback) { + doricJSEngine.changeJSEngine(isNative, changeEngineCallback); } } diff --git a/Android/doric/src/main/java/pub/doric/dev/DevPanel.java b/Android/doric/src/main/java/pub/doric/dev/DevPanel.java index 1037b715..416de906 100644 --- a/Android/doric/src/main/java/pub/doric/dev/DevPanel.java +++ b/Android/doric/src/main/java/pub/doric/dev/DevPanel.java @@ -87,6 +87,7 @@ public class DevPanel extends BottomSheetDialogFragment { jsonObject.addProperty("projectHome", BuildConfig.PROJECT_HOME); Doric.sendDevCommand(IDevKit.Command.DEBUG, jsonObject); } + dismissAllowingStateLoss(); } }); } diff --git a/Android/doric/src/main/java/pub/doric/dev/event/QuitDebugEvent.java b/Android/doric/src/main/java/pub/doric/dev/event/QuitDebugEvent.java new file mode 100644 index 00000000..4a891395 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/dev/event/QuitDebugEvent.java @@ -0,0 +1,4 @@ +package pub.doric.dev.event; + +public class QuitDebugEvent { +} diff --git a/Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java b/Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java new file mode 100644 index 00000000..762e04c9 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java @@ -0,0 +1,5 @@ +package pub.doric.engine; + +public interface ChangeEngineCallback { + void changed(); +} 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 e7965961..1509932c 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -43,6 +43,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time private final Handler mJSHandler; private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); private IDoricJSE mDoricJSE; + private DoricNativeJSExecutor doricNativeJSExecutor; private final DoricTimerExtension mTimerExtension; private final DoricRegistry mDoricRegistry = new DoricRegistry(); @@ -54,7 +55,9 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time mJSHandler.post(new Runnable() { @Override public void run() { - mDoricJSE = new DoricNativeJSExecutor(); + doricNativeJSExecutor = new DoricNativeJSExecutor(); + + mDoricJSE = doricNativeJSExecutor; injectGlobal(); initDoricRuntime(); } @@ -211,17 +214,18 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time return mDoricRegistry; } - public void changeJSEngine(final boolean isNative) { + public void changeJSEngine(final boolean isNative, final ChangeEngineCallback changeEngineCallback) { mJSHandler.post(new Runnable() { @Override public void run() { - mDoricJSE.teardown(); if (isNative) { - mDoricJSE = new DoricNativeJSExecutor(); + mDoricJSE.teardown(); + mDoricJSE = doricNativeJSExecutor; } else { mDoricJSE = new DoricRemoteJSExecutor(); + injectGlobal(); } - injectGlobal(); + changeEngineCallback.changed(); } }); } diff --git a/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java b/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java index ef78b5b0..d2e3d994 100644 --- a/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java +++ b/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java @@ -8,6 +8,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import org.greenrobot.eventbus.EventBus; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; @@ -23,6 +24,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.WebSocket; import okhttp3.WebSocketListener; +import pub.doric.dev.event.QuitDebugEvent; import pub.doric.utils.DoricUtils; public class RemoteJSExecutor { @@ -56,7 +58,10 @@ public class RemoteJSExecutor { throw new RuntimeException("remote js executor cannot connect"); } else if (t instanceof EOFException) { // 被远端强制断开 - throw new RuntimeException("remote js executor eof"); + System.out.println("remote js executor eof"); + + LockSupport.park(current); + EventBus.getDefault().post(new QuitDebugEvent()); } } @@ -169,6 +174,6 @@ public class RemoteJSExecutor { } public void destroy() { - webSocket.close(0, "destroy"); + webSocket.close(1000, "destroy"); } }