debug switch to native

This commit is contained in:
王劲鹏 2019-11-13 18:06:40 +08:00
parent fd8fe277a4
commit cafec58778
7 changed files with 57 additions and 11 deletions

View File

@ -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,9 +78,33 @@ public class MainActivity extends AppCompatActivity {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) {
DoricDriver.getInstance().changeJSEngine(false);
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
public boolean onKeyDown(int keyCode, KeyEvent event) {

View File

@ -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);
}
}

View File

@ -87,6 +87,7 @@ public class DevPanel extends BottomSheetDialogFragment {
jsonObject.addProperty("projectHome", BuildConfig.PROJECT_HOME);
Doric.sendDevCommand(IDevKit.Command.DEBUG, jsonObject);
}
dismissAllowingStateLoss();
}
});
}

View File

@ -0,0 +1,4 @@
package pub.doric.dev.event;
public class QuitDebugEvent {
}

View File

@ -0,0 +1,5 @@
package pub.doric.engine;
public interface ChangeEngineCallback {
void changed();
}

View File

@ -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,18 +214,19 @@ 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();
}
changeEngineCallback.changed();
}
});
}
}

View File

@ -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");
}
}