From 93cb80f6f8e7e078d9766ed56d77d03bd690fbd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 22 Nov 2019 11:30:14 +0800 Subject: [PATCH] doric driver multiple instance --- .../java/pub/doric/demo/DemoActivity.java | 26 +--- .../java/pub/doric/demo/MainActivity.java | 41 ------ .../src/main/java/pub/doric/DoricContext.java | 40 ++++-- .../main/java/pub/doric/DoricDebugDriver.java | 129 ++++++++++++++++++ ...oricDriver.java => DoricNativeDriver.java} | 39 +++--- .../doric/engine/ChangeEngineCallback.java | 5 - .../java/pub/doric/engine/DoricJSEngine.java | 26 +--- .../doric/engine/DoricRemoteJSExecutor.java | 4 +- .../pub/doric/engine/IStatusCallback.java | 5 + .../doric/engine/remote/RemoteJSExecutor.java | 4 +- 10 files changed, 195 insertions(+), 124 deletions(-) create mode 100644 Android/doric/src/main/java/pub/doric/DoricDebugDriver.java rename Android/doric/src/main/java/pub/doric/{DoricDriver.java => DoricNativeDriver.java} (90%) delete mode 100644 Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java create mode 100644 Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java diff --git a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java index 2ebc5763..4c773d7c 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -28,12 +28,10 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import pub.doric.DoricContext; -import pub.doric.DoricDriver; import pub.doric.dev.DevPanel; import pub.doric.dev.SensorManagerHelper; import pub.doric.dev.event.EnterDebugEvent; import pub.doric.dev.event.QuitDebugEvent; -import pub.doric.engine.ChangeEngineCallback; import pub.doric.utils.DoricUtils; /** @@ -98,32 +96,12 @@ public class DemoActivity extends AppCompatActivity { @Subscribe(threadMode = ThreadMode.MAIN) public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { - 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); - } - }); - } - }); + doricContext.startDebug(); } @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); - } - }); - } - }); + doricContext.stopDebug(); } @Override 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 17db4e70..6ba6f31e 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -28,20 +28,11 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.ThreadMode; - import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.function.Predicate; -import pub.doric.DoricDriver; import pub.doric.dev.DevPanel; -import pub.doric.dev.event.EnterDebugEvent; -import pub.doric.dev.event.QuitDebugEvent; -import pub.doric.engine.ChangeEngineCallback; import pub.doric.utils.DoricUtils; public class MainActivity extends AppCompatActivity { @@ -57,7 +48,6 @@ public class MainActivity extends AppCompatActivity { try { String[] demos = getAssets().list("demo"); List ret = new ArrayList<>(); - ret.add("Debug Kit"); for (String str : demos) { if (str.endsWith("js")) { ret.add(str); @@ -69,37 +59,6 @@ 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) { - DoricDriver.getInstance().changeJSEngine(false, new ChangeEngineCallback() { - @Override - public void changed() { - } - }); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) { - DoricDriver.getInstance().changeJSEngine(true, new ChangeEngineCallback() { - @Override - public void changed() { - } - }); - } - public class MyAdapter extends RecyclerView.Adapter { private final String[] data; diff --git a/Android/doric/src/main/java/pub/doric/DoricContext.java b/Android/doric/src/main/java/pub/doric/DoricContext.java index 3291eb78..b5430f20 100644 --- a/Android/doric/src/main/java/pub/doric/DoricContext.java +++ b/Android/doric/src/main/java/pub/doric/DoricContext.java @@ -17,12 +17,6 @@ package pub.doric; import android.content.Context; -import pub.doric.async.AsyncResult; -import pub.doric.plugin.DoricJavaPlugin; -import pub.doric.utils.DoricConstant; -import pub.doric.utils.DoricMetaInfo; -import pub.doric.shader.RootNode; - import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSONBuilder; @@ -31,6 +25,13 @@ import org.json.JSONObject; import java.util.HashMap; import java.util.Map; +import pub.doric.async.AsyncResult; +import pub.doric.engine.IStatusCallback; +import pub.doric.plugin.DoricJavaPlugin; +import pub.doric.shader.RootNode; +import pub.doric.utils.DoricConstant; +import pub.doric.utils.DoricMetaInfo; + /** * @Description: Doric * @Author: pengfei.zhou @@ -44,6 +45,8 @@ public class DoricContext { private final String source; private String script; private JSONObject initParams; + public boolean isDebugging = false; + private DoricDebugDriver doricDebugDriver; DoricContext(Context context, String contextId, String source) { this.mContext = context; @@ -70,7 +73,7 @@ public class DoricContext { .put("width", width) .put("height", height).toJSONObject(); callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams); - + callEntity(DoricConstant.DORIC_ENTITY_CREATE); } @@ -79,7 +82,11 @@ public class DoricContext { } public IDoricDriver getDriver() { - return DoricDriver.getInstance(); + if (isDebugging) { + return doricDebugDriver; + } else { + return DoricNativeDriver.getInstance(); + } } public RootNode getRootNode() { @@ -136,4 +143,21 @@ public class DoricContext { public void onHidden() { callEntity(DoricConstant.DORIC_ENTITY_HIDDEN); } + + public void startDebug() { + doricDebugDriver = new DoricDebugDriver(new IStatusCallback() { + @Override + public void start() { + isDebugging=true; + callEntity(DoricConstant.DORIC_ENTITY_INIT, initParams); + callEntity(DoricConstant.DORIC_ENTITY_CREATE); + } + }); + } + + public void stopDebug() { + isDebugging = false; + callEntity(DoricConstant.DORIC_ENTITY_INIT, initParams); + callEntity(DoricConstant.DORIC_ENTITY_CREATE); + } } diff --git a/Android/doric/src/main/java/pub/doric/DoricDebugDriver.java b/Android/doric/src/main/java/pub/doric/DoricDebugDriver.java new file mode 100644 index 00000000..ace507ff --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/DoricDebugDriver.java @@ -0,0 +1,129 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package pub.doric; + +import android.os.Handler; +import android.os.Looper; + +import com.github.pengfeizhou.jscore.JSDecoder; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import pub.doric.async.AsyncCall; +import pub.doric.async.AsyncResult; +import pub.doric.engine.DoricJSEngine; +import pub.doric.engine.IStatusCallback; +import pub.doric.utils.DoricConstant; +import pub.doric.utils.DoricLog; +import pub.doric.utils.ThreadMode; + +/** + * @Description: Doric + * @Author: pengfei.zhou + * @CreateDate: 2019-07-18 + */ +public class DoricDebugDriver implements IDoricDriver { + private final DoricJSEngine doricJSEngine; + private final ExecutorService mBridgeExecutor; + private final Handler mUIHandler; + private final Handler mJSHandler; + + + public DoricDebugDriver(IStatusCallback statusCallback) { + doricJSEngine = new DoricJSEngine(false, statusCallback); + mBridgeExecutor = Executors.newCachedThreadPool(); + mUIHandler = new Handler(Looper.getMainLooper()); + mJSHandler = doricJSEngine.getJSHandler(); + } + + @Override + public AsyncResult invokeContextEntityMethod(final String contextId, final String method, final Object... args) { + final Object[] nArgs = new Object[args.length + 2]; + nArgs[0] = contextId; + nArgs[1] = method; + if (args.length > 0) { + System.arraycopy(args, 0, nArgs, 2, args.length); + } + return invokeDoricMethod(DoricConstant.DORIC_CONTEXT_INVOKE, nArgs); + } + + @Override + public AsyncResult invokeDoricMethod(final String method, final Object... args) { + return AsyncCall.ensureRunInHandler(mJSHandler, new Callable() { + @Override + public JSDecoder call() { + try { + return doricJSEngine.invokeDoricMethod(method, args); + } catch (Exception e) { + DoricLog.e("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage()); + return new JSDecoder(null); + } + } + }); + } + + @Override + public AsyncResult asyncCall(Callable callable, ThreadMode threadMode) { + switch (threadMode) { + case JS: + return AsyncCall.ensureRunInHandler(mJSHandler, callable); + case UI: + return AsyncCall.ensureRunInHandler(mUIHandler, callable); + case INDEPENDENT: + default: + return AsyncCall.ensureRunInExecutor(mBridgeExecutor, callable); + } + } + + @Override + public AsyncResult createContext(final String contextId, final String script, final String source) { + return AsyncCall.ensureRunInHandler(mJSHandler, new Callable() { + @Override + public Boolean call() { + try { + doricJSEngine.prepareContext(contextId, script, source); + return true; + } catch (Exception e) { + DoricLog.e("createContext %s error is %s", source, e.getLocalizedMessage()); + return false; + } + } + }); + } + + @Override + public AsyncResult destroyContext(final String contextId) { + return AsyncCall.ensureRunInHandler(mJSHandler, new Callable() { + @Override + public Boolean call() { + try { + doricJSEngine.destroyContext(contextId); + return true; + } catch (Exception e) { + DoricLog.e("destroyContext %s error is %s", contextId, e.getLocalizedMessage()); + return false; + } + } + }); + } + + @Override + public DoricRegistry getRegistry() { + return doricJSEngine.getRegistry(); + } +} diff --git a/Android/doric/src/main/java/pub/doric/DoricDriver.java b/Android/doric/src/main/java/pub/doric/DoricNativeDriver.java similarity index 90% rename from Android/doric/src/main/java/pub/doric/DoricDriver.java rename to Android/doric/src/main/java/pub/doric/DoricNativeDriver.java index c8c8f47e..8d6cefd3 100644 --- a/Android/doric/src/main/java/pub/doric/DoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/DoricNativeDriver.java @@ -26,7 +26,6 @@ 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; @@ -37,12 +36,27 @@ import pub.doric.utils.ThreadMode; * @Author: pengfei.zhou * @CreateDate: 2019-07-18 */ -public class DoricDriver implements IDoricDriver { +public class DoricNativeDriver implements IDoricDriver { private final DoricJSEngine doricJSEngine; private final ExecutorService mBridgeExecutor; private final Handler mUIHandler; private final Handler mJSHandler; + private static class Inner { + private static final DoricNativeDriver sInstance = new DoricNativeDriver(); + } + + private DoricNativeDriver() { + doricJSEngine = new DoricJSEngine(true, null); + mBridgeExecutor = Executors.newCachedThreadPool(); + mUIHandler = new Handler(Looper.getMainLooper()); + mJSHandler = doricJSEngine.getJSHandler(); + } + + public static DoricNativeDriver getInstance() { + return Inner.sInstance; + } + @Override public AsyncResult invokeContextEntityMethod(final String contextId, final String method, final Object... args) { final Object[] nArgs = new Object[args.length + 2]; @@ -82,23 +96,6 @@ public class DoricDriver implements IDoricDriver { } } - - private static class Inner { - private static final DoricDriver sInstance = new DoricDriver(); - } - - private DoricDriver() { - doricJSEngine = new DoricJSEngine(); - mBridgeExecutor = Executors.newCachedThreadPool(); - mUIHandler = new Handler(Looper.getMainLooper()); - mJSHandler = doricJSEngine.getJSHandler(); - } - - - public static DoricDriver getInstance() { - return Inner.sInstance; - } - @Override public AsyncResult createContext(final String contextId, final String script, final String source) { return AsyncCall.ensureRunInHandler(mJSHandler, new Callable() { @@ -135,8 +132,4 @@ public class DoricDriver implements IDoricDriver { public DoricRegistry getRegistry() { return doricJSEngine.getRegistry(); } - - public void changeJSEngine(boolean isNative, ChangeEngineCallback changeEngineCallback) { - doricJSEngine.changeJSEngine(isNative, changeEngineCallback); - } } diff --git a/Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java b/Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java deleted file mode 100644 index 762e04c9..00000000 --- a/Android/doric/src/main/java/pub/doric/engine/ChangeEngineCallback.java +++ /dev/null @@ -1,5 +0,0 @@ -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 681615fe..49f29773 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -43,11 +43,10 @@ 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(); - public DoricJSEngine() { + public DoricJSEngine(final boolean isNative, final IStatusCallback statusCallback) { HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName()); handlerThread.start(); Looper looper = handlerThread.getLooper(); @@ -55,9 +54,12 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time mJSHandler.post(new Runnable() { @Override public void run() { - doricNativeJSExecutor = new DoricNativeJSExecutor(); + if (isNative) { + mDoricJSE = new DoricNativeJSExecutor(); + } else { + mDoricJSE = new DoricRemoteJSExecutor(statusCallback); + } - mDoricJSE = doricNativeJSExecutor; injectGlobal(); initDoricRuntime(); } @@ -213,20 +215,4 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time public DoricRegistry getRegistry() { return mDoricRegistry; } - - public void changeJSEngine(final boolean isNative, final ChangeEngineCallback changeEngineCallback) { - mJSHandler.post(new Runnable() { - @Override - public void run() { - if (isNative) { - mDoricJSE.teardown(); - mDoricJSE = doricNativeJSExecutor; - } else { - mDoricJSE = new DoricRemoteJSExecutor(); - injectGlobal(); - } - changeEngineCallback.changed(); - } - }); - } } diff --git a/Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java b/Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java index 65af51be..e3fab19c 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java +++ b/Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java @@ -26,8 +26,8 @@ public class DoricRemoteJSExecutor implements IDoricJSE { private final RemoteJSExecutor mRemoteJSExecutor; - public DoricRemoteJSExecutor() { - this.mRemoteJSExecutor = new RemoteJSExecutor(); + public DoricRemoteJSExecutor(IStatusCallback statusCallback) { + this.mRemoteJSExecutor = new RemoteJSExecutor(statusCallback); } @Override diff --git a/Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java b/Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java new file mode 100644 index 00000000..0dcbd70e --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java @@ -0,0 +1,5 @@ +package pub.doric.engine; + +public interface IStatusCallback { + void start(); +} 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 75a96342..f1a82079 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 @@ -25,13 +25,14 @@ import okhttp3.WebSocket; import okhttp3.WebSocketListener; import pub.doric.dev.DevKit; import pub.doric.dev.event.QuitDebugEvent; +import pub.doric.engine.IStatusCallback; public class RemoteJSExecutor { private final WebSocket webSocket; private final Map globalFunctions = new HashMap<>(); private JSDecoder temp; - public RemoteJSExecutor() { + public RemoteJSExecutor(final IStatusCallback statusCallback) { OkHttpClient okHttpClient = new OkHttpClient .Builder() .readTimeout(10, TimeUnit.SECONDS) @@ -44,6 +45,7 @@ public class RemoteJSExecutor { @Override public void onOpen(WebSocket webSocket, Response response) { LockSupport.unpark(current); + statusCallback.start(); } @Override