From c158d0c7f4dbfd9b36957cde92442c95c34b9407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Sun, 24 Nov 2019 11:11:41 +0800 Subject: [PATCH] refactor debug sample to devkit --- .../java/pub/doric/demo/DemoActivity.java | 68 ----------- .../java/pub/doric/demo/MainActivity.java | 3 +- Android/devkit/src/main/AndroidManifest.xml | 1 + .../doric/devkit/ui/DemoDebugActivity.java | 113 ++++++++++++++++++ 4 files changed, 116 insertions(+), 69 deletions(-) create mode 100644 Android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.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 6703c846..1ec7b59a 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -16,26 +16,13 @@ package pub.doric.demo; import android.os.Bundle; -import android.view.KeyEvent; import android.view.ViewGroup; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.Fragment; - -import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.ThreadMode; import pub.doric.DoricContext; -import pub.doric.DoricContextManager; import pub.doric.DoricPanel; -import pub.doric.devkit.DoricContextDebuggable; -import pub.doric.devkit.event.EnterDebugEvent; -import pub.doric.devkit.event.QuitDebugEvent; -import pub.doric.devkit.event.ReloadEvent; -import pub.doric.devkit.ui.DevPanel; -import pub.doric.devkit.util.SensorManagerHelper; import pub.doric.utils.DoricUtils; /** @@ -45,8 +32,6 @@ 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,58 +42,5 @@ public class DemoActivity extends AppCompatActivity { ViewGroup.LayoutParams.MATCH_PARENT)); doricPanel.config(DoricUtils.readAssetFile("demo/" + source), source); doricContext = doricPanel.getDoricContext(); - doricContextDebuggable = new DoricContextDebuggable(doricContext); - sensorHelper = new SensorManagerHelper(this); - sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() { - @Override - public void onShake() { - Fragment devPanel = getSupportFragmentManager().findFragmentByTag("DevPanel"); - if (devPanel != null && devPanel.isAdded()) { - return; - } - new DevPanel().show(getSupportFragmentManager(), "DevPanel"); - } - }); - } - - @Override - public void onAttachedToWindow() { - super.onAttachedToWindow(); - - EventBus.getDefault().register(this); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - EventBus.getDefault().unregister(this); - sensorHelper.stop(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { - doricContextDebuggable.startDebug(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onReloadEvent(ReloadEvent reloadEvent) { - for (DoricContext context : DoricContextManager.aliveContexts()) { - if (reloadEvent.source.contains(context.getSource())) { - context.reload(reloadEvent.script); - } - } - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) { - doricContextDebuggable.stopDebug(); - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { - new DevPanel().show(getSupportFragmentManager(), "DevPanel"); - } - return super.onKeyDown(keyCode, event); } } 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 a370254c..ad23198d 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,7 @@ import java.util.ArrayList; import java.util.List; import pub.doric.DoricActivity; +import pub.doric.devkit.ui.DemoDebugActivity; import pub.doric.utils.DoricUtils; public class MainActivity extends AppCompatActivity { @@ -97,7 +98,7 @@ public class MainActivity extends AppCompatActivity { tv.getContext().startActivity(intent); return; } - Intent intent = new Intent(tv.getContext(), DemoActivity.class); + Intent intent = new Intent(tv.getContext(), DemoDebugActivity.class); intent.putExtra("source", data[position]); tv.getContext().startActivity(intent); } diff --git a/Android/devkit/src/main/AndroidManifest.xml b/Android/devkit/src/main/AndroidManifest.xml index 60a25b75..70d23e89 100644 --- a/Android/devkit/src/main/AndroidManifest.xml +++ b/Android/devkit/src/main/AndroidManifest.xml @@ -5,5 +5,6 @@ + diff --git a/Android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.java b/Android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.java new file mode 100644 index 00000000..135e4cf0 --- /dev/null +++ b/Android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.java @@ -0,0 +1,113 @@ +/* + * 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.devkit.ui; + +import android.os.Bundle; +import android.view.KeyEvent; +import android.view.ViewGroup; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; +import pub.doric.DoricPanel; +import pub.doric.devkit.DoricContextDebuggable; +import pub.doric.devkit.event.EnterDebugEvent; +import pub.doric.devkit.event.QuitDebugEvent; +import pub.doric.devkit.event.ReloadEvent; +import pub.doric.devkit.util.SensorManagerHelper; +import pub.doric.utils.DoricUtils; + +/** + * @Description: pub.doric.demo + * @Author: pengfei.zhou + * @CreateDate: 2019-11-19 + */ +public class DemoDebugActivity extends AppCompatActivity { + private DoricContext doricContext; + private SensorManagerHelper sensorHelper; + private DoricContextDebuggable doricContextDebuggable; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + String source = getIntent().getStringExtra("source"); + DoricPanel doricPanel = new DoricPanel(this); + addContentView(doricPanel, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT)); + doricPanel.config(DoricUtils.readAssetFile("demo/" + source), source); + doricContext = doricPanel.getDoricContext(); + doricContextDebuggable = new DoricContextDebuggable(doricContext); + sensorHelper = new SensorManagerHelper(this); + sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() { + @Override + public void onShake() { + Fragment devPanel = getSupportFragmentManager().findFragmentByTag("DevPanel"); + if (devPanel != null && devPanel.isAdded()) { + return; + } + new DevPanel().show(getSupportFragmentManager(), "DevPanel"); + } + }); + } + + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + + EventBus.getDefault().register(this); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + sensorHelper.stop(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { + doricContextDebuggable.startDebug(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onReloadEvent(ReloadEvent reloadEvent) { + for (DoricContext context : DoricContextManager.aliveContexts()) { + if (reloadEvent.source.contains(context.getSource())) { + context.reload(reloadEvent.script); + } + } + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) { + doricContextDebuggable.stopDebug(); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { + new DevPanel().show(getSupportFragmentManager(), "DevPanel"); + } + return super.onKeyDown(keyCode, event); + } +}