diff --git a/Android/app/build.gradle b/Android/app/build.gradle index 97fb3689..8f3a79bd 100644 --- a/Android/app/build.gradle +++ b/Android/app/build.gradle @@ -23,7 +23,7 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation "com.google.android.material:material:1.0.0" - implementation project(':doric') + implementation project(':devkit') implementation 'com.github.bumptech.glide:glide:4.10.0' implementation 'com.github.bumptech.glide:annotations:4.10.0' implementation 'com.github.penfeizhou.android.animation:glide-plugin:1.3.1' 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 e9048e58..1265d56d 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -28,11 +28,9 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; -import java.util.ArrayList; - import pub.doric.DoricContext; import pub.doric.DoricContextManager; -import pub.doric.devkit.DataModel; +import pub.doric.devkit.DoricContextDebuggable; import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.event.QuitDebugEvent; import pub.doric.devkit.event.ReloadEvent; @@ -48,6 +46,7 @@ import pub.doric.utils.DoricUtils; public class DemoActivity extends AppCompatActivity { private DoricContext doricContext; private SensorManagerHelper sensorHelper; + private DoricContextDebuggable doricContextDebuggable; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -57,6 +56,7 @@ public class DemoActivity extends AppCompatActivity { addContentView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/" + source), source); + doricContextDebuggable = new DoricContextDebuggable(doricContext); doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); doricContext.getRootNode().setRootView(frameLayout); @@ -68,12 +68,7 @@ public class DemoActivity extends AppCompatActivity { if (devPanel != null && devPanel.isAdded()) { return; } - - ArrayList<DataModel> dataModels = new ArrayList<>(); - for (DoricContext doricContext : DoricContextManager.aliveContexts()) { - dataModels.add(new DataModel(doricContext.getContextId(), doricContext.getSource())); - } - new DevPanel(dataModels).show(getSupportFragmentManager(), "DevPanel"); + new DevPanel().show(getSupportFragmentManager(), "DevPanel"); } }); } @@ -107,7 +102,7 @@ public class DemoActivity extends AppCompatActivity { @Subscribe(threadMode = ThreadMode.MAIN) public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { - doricContext.startDebug(); + doricContextDebuggable.startDebug(); } @Subscribe(threadMode = ThreadMode.MAIN) @@ -121,17 +116,13 @@ public class DemoActivity extends AppCompatActivity { @Subscribe(threadMode = ThreadMode.MAIN) public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) { - doricContext.stopDebug(); + doricContextDebuggable.stopDebug(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { - ArrayList<DataModel> dataModels = new ArrayList<>(); - for (DoricContext doricContext : DoricContextManager.aliveContexts()) { - dataModels.add(new DataModel(doricContext.getContextId(), doricContext.getSource())); - } - new DevPanel(dataModels).show(getSupportFragmentManager(), "DevPanel"); + new DevPanel().show(getSupportFragmentManager(), "DevPanel"); } return super.onKeyDown(keyCode, event); } diff --git a/Android/app/src/main/java/pub/doric/demo/DemoPlugin.java b/Android/app/src/main/java/pub/doric/demo/DemoPlugin.java index 568c0ecd..3fbe6658 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoPlugin.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoPlugin.java @@ -17,6 +17,8 @@ package pub.doric.demo; import android.widget.Toast; +import com.github.pengfeizhou.jscore.JavaValue; + import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricPlugin; @@ -24,8 +26,6 @@ import pub.doric.extension.bridge.DoricPromise; import pub.doric.plugin.DoricJavaPlugin; import pub.doric.utils.ThreadMode; -import com.github.pengfeizhou.jscore.JavaValue; - @DoricPlugin(name = "demo") public class DemoPlugin extends DoricJavaPlugin { public DemoPlugin(DoricContext doricContext) { diff --git a/Android/devkit/build.gradle b/Android/devkit/build.gradle index 3a38d83f..e24a2d60 100644 --- a/Android/devkit/build.gradle +++ b/Android/devkit/build.gradle @@ -44,6 +44,7 @@ dependencies { api 'org.greenrobot:eventbus:3.1.1' implementation 'com.lahm.library:easy-protector-release:1.1.0' api 'org.nanohttpd:nanohttpd:2.3.1' + api project(':doric') testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' diff --git a/Android/devkit/src/main/java/pub/doric/devkit/DataModel.java b/Android/devkit/src/main/java/pub/doric/devkit/DataModel.java deleted file mode 100644 index 0f1e7759..00000000 --- a/Android/devkit/src/main/java/pub/doric/devkit/DataModel.java +++ /dev/null @@ -1,11 +0,0 @@ -package pub.doric.devkit; - -public class DataModel { - public String contextId; - public String source; - - public DataModel(String contextId, String source) { - this.contextId = contextId; - this.source = source; - } - } \ No newline at end of file diff --git a/Android/devkit/src/main/java/pub/doric/devkit/DoricContextDebuggable.java b/Android/devkit/src/main/java/pub/doric/devkit/DoricContextDebuggable.java new file mode 100644 index 00000000..fe13b9a8 --- /dev/null +++ b/Android/devkit/src/main/java/pub/doric/devkit/DoricContextDebuggable.java @@ -0,0 +1,30 @@ +package pub.doric.devkit; + +import pub.doric.DoricContext; +import pub.doric.DoricNativeDriver; +import pub.doric.engine.IStatusCallback; + +public class DoricContextDebuggable { + private DoricContext doricContext; + private DoricDebugDriver doricDebugDriver; + + public DoricContextDebuggable(DoricContext doricContext) { + this.doricContext = doricContext; + } + + public void startDebug() { + doricDebugDriver = new DoricDebugDriver(new IStatusCallback() { + @Override + public void start() { + doricContext.setDriver(doricDebugDriver); + doricContext.reInit(); + } + }); + } + + public void stopDebug() { + doricDebugDriver.destroy(); + doricContext.setDriver(DoricNativeDriver.getInstance()); + doricContext.reInit(); + } +} diff --git a/Android/doric/src/main/java/pub/doric/DoricDebugDriver.java b/Android/devkit/src/main/java/pub/doric/devkit/DoricDebugDriver.java similarity index 95% rename from Android/doric/src/main/java/pub/doric/DoricDebugDriver.java rename to Android/devkit/src/main/java/pub/doric/devkit/DoricDebugDriver.java index d07b54e5..0439b9c8 100644 --- a/Android/doric/src/main/java/pub/doric/DoricDebugDriver.java +++ b/Android/devkit/src/main/java/pub/doric/devkit/DoricDebugDriver.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package pub.doric; +package pub.doric.devkit; import android.os.Handler; import android.os.Looper; @@ -24,10 +24,12 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import pub.doric.DoricRegistry; +import pub.doric.IDoricDriver; import pub.doric.async.AsyncCall; import pub.doric.async.AsyncResult; -import pub.doric.devkit.IStatusCallback; 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; @@ -45,7 +47,7 @@ public class DoricDebugDriver implements IDoricDriver { public DoricDebugDriver(IStatusCallback statusCallback) { - doricJSEngine = new DoricJSEngine(false, statusCallback); + doricJSEngine = new DoricDebugJSEngine(statusCallback); mBridgeExecutor = Executors.newCachedThreadPool(); mUIHandler = new Handler(Looper.getMainLooper()); mJSHandler = doricJSEngine.getJSHandler(); diff --git a/Android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java b/Android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java new file mode 100644 index 00000000..7794dee3 --- /dev/null +++ b/Android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java @@ -0,0 +1,20 @@ +package pub.doric.devkit; + +import pub.doric.devkit.remote.DoricRemoteJSExecutor; +import pub.doric.engine.DoricJSEngine; +import pub.doric.engine.IStatusCallback; + +public class DoricDebugJSEngine extends DoricJSEngine { + + private IStatusCallback statusCallback; + + public DoricDebugJSEngine(IStatusCallback statusCallback) { + super(); + this.statusCallback = statusCallback; + } + + @Override + protected void initJSEngine() { + mDoricJSE = new DoricRemoteJSExecutor(statusCallback); + } +} diff --git a/Android/doric/src/main/java/pub/doric/utils/LocalServer.java b/Android/devkit/src/main/java/pub/doric/devkit/LocalServer.java similarity index 99% rename from Android/doric/src/main/java/pub/doric/utils/LocalServer.java rename to Android/devkit/src/main/java/pub/doric/devkit/LocalServer.java index 87457d7f..a85417f6 100644 --- a/Android/doric/src/main/java/pub/doric/utils/LocalServer.java +++ b/Android/devkit/src/main/java/pub/doric/devkit/LocalServer.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package pub.doric.utils; +package pub.doric.devkit; import android.content.Context; import android.content.res.AssetManager; diff --git a/Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java b/Android/devkit/src/main/java/pub/doric/devkit/remote/DoricRemoteJSExecutor.java similarity index 94% rename from Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java rename to Android/devkit/src/main/java/pub/doric/devkit/remote/DoricRemoteJSExecutor.java index ced51e6c..8e51f545 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricRemoteJSExecutor.java +++ b/Android/devkit/src/main/java/pub/doric/devkit/remote/DoricRemoteJSExecutor.java @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package pub.doric.engine; +package pub.doric.devkit.remote; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSRuntimeException; import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaValue; -import pub.doric.devkit.IStatusCallback; -import pub.doric.devkit.remote.RemoteJSExecutor; +import pub.doric.engine.IDoricJSE; +import pub.doric.engine.IStatusCallback; public class DoricRemoteJSExecutor implements IDoricJSE { diff --git a/Android/devkit/src/main/java/pub/doric/devkit/remote/RemoteJSExecutor.java b/Android/devkit/src/main/java/pub/doric/devkit/remote/RemoteJSExecutor.java index add8bff2..677c3d98 100644 --- a/Android/devkit/src/main/java/pub/doric/devkit/remote/RemoteJSExecutor.java +++ b/Android/devkit/src/main/java/pub/doric/devkit/remote/RemoteJSExecutor.java @@ -25,7 +25,7 @@ import okhttp3.WebSocket; import okhttp3.WebSocketListener; import pub.doric.devkit.DevKit; import pub.doric.devkit.event.QuitDebugEvent; -import pub.doric.devkit.IStatusCallback; +import pub.doric.engine.IStatusCallback; public class RemoteJSExecutor { private final WebSocket webSocket; diff --git a/Android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java b/Android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java index 8203a979..3e372463 100644 --- a/Android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java +++ b/Android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java @@ -15,20 +15,16 @@ import androidx.fragment.app.DialogFragment; import com.google.gson.JsonObject; -import java.util.ArrayList; - +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; import pub.doric.devkit.BuildConfig; -import pub.doric.devkit.DataModel; import pub.doric.devkit.DoricDev; import pub.doric.devkit.IDevKit; import pub.doric.devkit.R; public class DebugContextPanel extends DialogFragment { - private ArrayList<DataModel> dataModels; - - public DebugContextPanel(ArrayList<DataModel> dataModels) { - this.dataModels = dataModels; + public DebugContextPanel() { } @Nullable @@ -61,22 +57,22 @@ public class DebugContextPanel extends DialogFragment { LinearLayout container = getView().findViewById(R.id.container); LayoutInflater inflater = LayoutInflater.from(getContext()); - for (final DataModel dataModel : dataModels) { + for (final DoricContext doricContext : DoricContextManager.aliveContexts()) { View cell = inflater.inflate(R.layout.layout_debug_context_cell, container, false); TextView contextIdTextView = cell.findViewById(R.id.context_id_text_view); - contextIdTextView.setText(dataModel.contextId); + contextIdTextView.setText(doricContext.getContextId()); TextView sourceTextView = cell.findViewById(R.id.source_text_view); - sourceTextView.setText(dataModel.source); + sourceTextView.setText(doricContext.getSource()); cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("contextId", dataModel.contextId); + jsonObject.addProperty("contextId", doricContext.getContextId()); jsonObject.addProperty("projectHome", BuildConfig.PROJECT_HOME); - jsonObject.addProperty("source", dataModel.source.replace(".js", ".ts")); + jsonObject.addProperty("source", doricContext.getSource().replace(".js", ".ts")); DoricDev.sendDevCommand(IDevKit.Command.DEBUG, jsonObject); dismissAllowingStateLoss(); } diff --git a/Android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java b/Android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java index 02fad431..5c3c925f 100644 --- a/Android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java +++ b/Android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java @@ -21,11 +21,8 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; -import java.util.ArrayList; - import io.reactivex.disposables.Disposable; import io.reactivex.functions.Consumer; -import pub.doric.devkit.DataModel; import pub.doric.devkit.DevKit; import pub.doric.devkit.DoricDev; import pub.doric.devkit.R; @@ -37,10 +34,7 @@ public class DevPanel extends BottomSheetDialogFragment { public static boolean isDevConnected = false; - ArrayList<DataModel> dataModels; - - public DevPanel(ArrayList<DataModel> dataModels) { - this.dataModels = dataModels; + public DevPanel() { } @Nullable @@ -85,7 +79,7 @@ public class DevPanel extends BottomSheetDialogFragment { getView().findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - DebugContextPanel debugContextPanel = new DebugContextPanel(dataModels); + DebugContextPanel debugContextPanel = new DebugContextPanel(); debugContextPanel.show(getActivity().getSupportFragmentManager(), "DebugContextPanel"); dismissAllowingStateLoss(); } diff --git a/Android/doric/build.gradle b/Android/doric/build.gradle index 90e95e5e..1b925089 100644 --- a/Android/doric/build.gradle +++ b/Android/doric/build.gradle @@ -46,7 +46,6 @@ task buildDebugger(type: Exec) { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - api project(':devkit') implementation 'androidx.appcompat:appcompat:1.1.0' api 'com.github.pengfeizhou:jsc4a:0.1.0' implementation 'com.squareup.okhttp3:okhttp:4.2.2' diff --git a/Android/doric/src/main/java/pub/doric/DoricContext.java b/Android/doric/src/main/java/pub/doric/DoricContext.java index 1fa78734..f9152e21 100644 --- a/Android/doric/src/main/java/pub/doric/DoricContext.java +++ b/Android/doric/src/main/java/pub/doric/DoricContext.java @@ -26,7 +26,6 @@ import java.util.HashMap; import java.util.Map; import pub.doric.async.AsyncResult; -import pub.doric.devkit.IStatusCallback; import pub.doric.plugin.DoricJavaPlugin; import pub.doric.shader.RootNode; import pub.doric.utils.DoricConstant; @@ -45,10 +44,9 @@ public class DoricContext { private final String source; private String script; private JSONObject initParams; - public boolean isDebugging = false; - private DoricDebugDriver doricDebugDriver; + private IDoricDriver doricDriver; - DoricContext(Context context, String contextId, String source) { + protected DoricContext(Context context, String contextId, String source) { this.mContext = context; this.mContextId = contextId; this.source = source; @@ -73,7 +71,11 @@ public class DoricContext { .put("width", width) .put("height", height).toJSONObject(); callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams); + callEntity(DoricConstant.DORIC_ENTITY_CREATE); + } + public void reInit() { + callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams); callEntity(DoricConstant.DORIC_ENTITY_CREATE); } @@ -82,11 +84,14 @@ public class DoricContext { } public IDoricDriver getDriver() { - if (isDebugging) { - return doricDebugDriver; - } else { - return DoricNativeDriver.getInstance(); + if (doricDriver == null) { + doricDriver = DoricNativeDriver.getInstance(); } + return doricDriver; + } + + public void setDriver(IDoricDriver doricDriver) { + this.doricDriver = doricDriver; } public RootNode getRootNode() { @@ -143,22 +148,4 @@ 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() { - doricDebugDriver.destroy(); - isDebugging = false; - callEntity(DoricConstant.DORIC_ENTITY_INIT, initParams); - callEntity(DoricConstant.DORIC_ENTITY_CREATE); - } } diff --git a/Android/doric/src/main/java/pub/doric/DoricNativeDriver.java b/Android/doric/src/main/java/pub/doric/DoricNativeDriver.java index 8d6cefd3..7fb4d328 100644 --- a/Android/doric/src/main/java/pub/doric/DoricNativeDriver.java +++ b/Android/doric/src/main/java/pub/doric/DoricNativeDriver.java @@ -47,7 +47,7 @@ public class DoricNativeDriver implements IDoricDriver { } private DoricNativeDriver() { - doricJSEngine = new DoricJSEngine(true, null); + doricJSEngine = new DoricJSEngine(); mBridgeExecutor = Executors.newCachedThreadPool(); mUIHandler = new Handler(Looper.getMainLooper()); mJSHandler = doricJSEngine.getJSHandler(); 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 1a93f5ae..7cc40445 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -28,7 +28,6 @@ import com.github.pengfeizhou.jscore.JavaValue; import java.util.ArrayList; import pub.doric.DoricRegistry; -import pub.doric.devkit.IStatusCallback; import pub.doric.extension.bridge.DoricBridgeExtension; import pub.doric.extension.timer.DoricTimerExtension; import pub.doric.utils.DoricConstant; @@ -45,11 +44,11 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time private HandlerThread handlerThread; private final Handler mJSHandler; private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); - private IDoricJSE mDoricJSE; + protected IDoricJSE mDoricJSE; private final DoricTimerExtension mTimerExtension; private final DoricRegistry mDoricRegistry = new DoricRegistry(); - public DoricJSEngine(final boolean isNative, final IStatusCallback statusCallback) { + public DoricJSEngine() { handlerThread = new HandlerThread(this.getClass().getSimpleName()); handlerThread.start(); Looper looper = handlerThread.getLooper(); @@ -57,12 +56,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time mJSHandler.post(new Runnable() { @Override public void run() { - if (isNative) { - mDoricJSE = new DoricNativeJSExecutor(); - } else { - mDoricJSE = new DoricRemoteJSExecutor(statusCallback); - } - + initJSEngine(); injectGlobal(); initDoricRuntime(); } @@ -74,6 +68,10 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time return mJSHandler; } + protected void initJSEngine() { + mDoricJSE = new DoricNativeJSExecutor(); + } + private void injectGlobal() { mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() { @Override diff --git a/Android/devkit/src/main/java/pub/doric/devkit/IStatusCallback.java b/Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java similarity index 68% rename from Android/devkit/src/main/java/pub/doric/devkit/IStatusCallback.java rename to Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java index 88c9a701..0dcbd70e 100644 --- a/Android/devkit/src/main/java/pub/doric/devkit/IStatusCallback.java +++ b/Android/doric/src/main/java/pub/doric/engine/IStatusCallback.java @@ -1,4 +1,4 @@ -package pub.doric.devkit; +package pub.doric.engine; public interface IStatusCallback { void start();