From 9c9fcf408732160102260dc6c08ac37e5e7c25f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Mon, 11 Nov 2019 18:51:45 +0800 Subject: [PATCH] scan qrcode to connect dev kit & change dev panel ui --- .../java/pub/doric/demo/MainActivity.java | 2 +- Android/doric/build.gradle | 1 + .../doric/src/main/java/pub/doric/Doric.java | 6 ++- .../src/main/java/pub/doric/DoricDriver.java | 5 ++- .../src/main/java/pub/doric/IDoricDriver.java | 3 +- .../java/pub/doric/dev/ConnectCallback.java | 7 ++++ .../main/java/pub/doric/dev/ConnectEvent.java | 4 ++ .../src/main/java/pub/doric/dev/DevPanel.java | 38 ++++++++++++++++++- .../pub/doric/dev/ScanQRCodeActivity.java | 17 +++++++++ .../src/main/java/pub/doric/dev/WSClient.java | 6 ++- .../doric/engine/remote/RemoteJSExecutor.java | 2 +- .../doric/src/main/res/drawable/divider.xml | 5 +++ .../doric/src/main/res/layout/layout_dev.xml | 26 +++++-------- 13 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 Android/doric/src/main/java/pub/doric/dev/ConnectCallback.java create mode 100644 Android/doric/src/main/java/pub/doric/dev/ConnectEvent.java create mode 100644 Android/doric/src/main/res/drawable/divider.xml 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 cddc1909..438c291c 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -43,7 +43,7 @@ public class MainActivity extends AppCompatActivity { doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); // doricContext.callEntity("log"); doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); - Doric.connectDevKit("ws://192.168.11.38:7777"); + LocalServer localServer = new LocalServer(getApplicationContext(), 8910); try { localServer.start(); diff --git a/Android/doric/build.gradle b/Android/doric/build.gradle index dc75be58..de20e78b 100644 --- a/Android/doric/build.gradle +++ b/Android/doric/build.gradle @@ -57,6 +57,7 @@ dependencies { implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.7' implementation 'com.github.tbruyelle:rxpermissions:0.10.2' implementation "io.reactivex.rxjava2:rxjava:2.2.14" + implementation 'org.greenrobot:eventbus:3.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' diff --git a/Android/doric/src/main/java/pub/doric/Doric.java b/Android/doric/src/main/java/pub/doric/Doric.java index c85dbe5d..e128c0b0 100644 --- a/Android/doric/src/main/java/pub/doric/Doric.java +++ b/Android/doric/src/main/java/pub/doric/Doric.java @@ -17,6 +17,8 @@ package pub.doric; import android.app.Application; +import pub.doric.dev.ConnectCallback; + /** * @Description: Doric * @Author: pengfei.zhou @@ -33,8 +35,8 @@ public class Doric { return sApplication; } - public static void connectDevKit(String url) { - DoricDriver.getInstance().connectDevKit(url); + public static void connectDevKit(String url, ConnectCallback connectCallback) { + DoricDriver.getInstance().connectDevKit(url, connectCallback); } public static void disconnectDevKit() { diff --git a/Android/doric/src/main/java/pub/doric/DoricDriver.java b/Android/doric/src/main/java/pub/doric/DoricDriver.java index e36d67df..900e01e1 100644 --- a/Android/doric/src/main/java/pub/doric/DoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/DoricDriver.java @@ -20,6 +20,7 @@ import android.os.Looper; import pub.doric.async.AsyncCall; import pub.doric.async.AsyncResult; +import pub.doric.dev.ConnectCallback; import pub.doric.dev.WSClient; import pub.doric.engine.DoricJSEngine; import pub.doric.utils.DoricConstant; @@ -138,8 +139,8 @@ public class DoricDriver implements IDoricDriver { } @Override - public void connectDevKit(String url) { - wsClient = new WSClient(url); + public void connectDevKit(String url, ConnectCallback connectCallback) { + wsClient = new WSClient(url, connectCallback); } @Override diff --git a/Android/doric/src/main/java/pub/doric/IDoricDriver.java b/Android/doric/src/main/java/pub/doric/IDoricDriver.java index 1341e16d..0dc212e2 100644 --- a/Android/doric/src/main/java/pub/doric/IDoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/IDoricDriver.java @@ -17,6 +17,7 @@ package pub.doric; import pub.doric.async.AsyncResult; +import pub.doric.dev.ConnectCallback; import pub.doric.utils.ThreadMode; import com.github.pengfeizhou.jscore.JSDecoder; @@ -41,7 +42,7 @@ public interface IDoricDriver { DoricRegistry getRegistry(); - void connectDevKit(String url); + void connectDevKit(String url, ConnectCallback connectCallback); void disconnectDevKit(); } diff --git a/Android/doric/src/main/java/pub/doric/dev/ConnectCallback.java b/Android/doric/src/main/java/pub/doric/dev/ConnectCallback.java new file mode 100644 index 00000000..4e65df33 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/dev/ConnectCallback.java @@ -0,0 +1,7 @@ +package pub.doric.dev; + +public interface ConnectCallback { + void connected(); + + void exception(Exception exception); +} diff --git a/Android/doric/src/main/java/pub/doric/dev/ConnectEvent.java b/Android/doric/src/main/java/pub/doric/dev/ConnectEvent.java new file mode 100644 index 00000000..520d9699 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/dev/ConnectEvent.java @@ -0,0 +1,4 @@ +package pub.doric.dev; + +public class ConnectEvent { +} 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 37713773..d8e84e3d 100644 --- a/Android/doric/src/main/java/pub/doric/dev/DevPanel.java +++ b/Android/doric/src/main/java/pub/doric/dev/DevPanel.java @@ -1,6 +1,7 @@ package pub.doric.dev; import android.Manifest; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; @@ -13,12 +14,18 @@ import androidx.annotation.Nullable; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.tbruyelle.rxpermissions2.RxPermissions; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + import io.reactivex.disposables.Disposable; import io.reactivex.functions.Consumer; import pub.doric.R; public class DevPanel extends BottomSheetDialogFragment { + private boolean isDevConnected = false; + public DevPanel() { } @@ -37,7 +44,7 @@ public class DevPanel extends BottomSheetDialogFragment { public void onStart() { super.onStart(); - getView().findViewById(R.id.menu1_text_view).setOnClickListener(new View.OnClickListener() { + getView().findViewById(R.id.connect_dev_kit_text_view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final RxPermissions rxPermissions = new RxPermissions(DevPanel.this); @@ -46,7 +53,7 @@ public class DevPanel extends BottomSheetDialogFragment { .subscribe(new Consumer() { @Override public void accept(Boolean grant) throws Exception { - if (grant){ + if (grant) { Intent intent = new Intent(getContext(), ScanQRCodeActivity.class); getContext().startActivity(intent); } @@ -55,5 +62,32 @@ public class DevPanel extends BottomSheetDialogFragment { } }); + + if (isDevConnected) { + getView().findViewById(R.id.connect_dev_kit_text_view).setVisibility(View.GONE); + getView().findViewById(R.id.debug_text_view).setVisibility(View.VISIBLE); + getView().findViewById(R.id.hot_reload_text_view).setVisibility(View.VISIBLE); + } else { + getView().findViewById(R.id.connect_dev_kit_text_view).setVisibility(View.VISIBLE); + getView().findViewById(R.id.debug_text_view).setVisibility(View.GONE); + getView().findViewById(R.id.hot_reload_text_view).setVisibility(View.GONE); + } + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + EventBus.getDefault().register(this); + } + + @Override + public void onDetach() { + super.onDetach(); + EventBus.getDefault().unregister(this); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onMessageEvent(ConnectEvent connectEvent) { + isDevConnected = true; } } diff --git a/Android/doric/src/main/java/pub/doric/dev/ScanQRCodeActivity.java b/Android/doric/src/main/java/pub/doric/dev/ScanQRCodeActivity.java index 82312848..80a42eb2 100644 --- a/Android/doric/src/main/java/pub/doric/dev/ScanQRCodeActivity.java +++ b/Android/doric/src/main/java/pub/doric/dev/ScanQRCodeActivity.java @@ -1,12 +1,16 @@ package pub.doric.dev; import android.os.Bundle; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import org.greenrobot.eventbus.EventBus; + import cn.bingoogolapple.qrcode.core.QRCodeView; import cn.bingoogolapple.qrcode.zxing.ZXingView; +import pub.doric.Doric; import pub.doric.R; public class ScanQRCodeActivity extends AppCompatActivity implements QRCodeView.Delegate { @@ -47,6 +51,19 @@ public class ScanQRCodeActivity extends AppCompatActivity implements QRCodeView. @Override public void onScanQRCodeSuccess(String result) { setTitle("扫描结果为:" + result); + Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show(); + Doric.connectDevKit("ws://" + result + ":7777", new ConnectCallback() { + @Override + public void connected() { + EventBus.getDefault().post(new ConnectEvent()); + finish(); + } + + @Override + public void exception(Exception exception) { + + } + }); } @Override diff --git a/Android/doric/src/main/java/pub/doric/dev/WSClient.java b/Android/doric/src/main/java/pub/doric/dev/WSClient.java index 005c5b06..d03a0db6 100644 --- a/Android/doric/src/main/java/pub/doric/dev/WSClient.java +++ b/Android/doric/src/main/java/pub/doric/dev/WSClient.java @@ -37,7 +37,10 @@ import okhttp3.WebSocketListener; public class WSClient extends WebSocketListener { private final WebSocket webSocket; - public WSClient(String url) { + private ConnectCallback connectCallback; + + public WSClient(String url, ConnectCallback connectCallback) { + this.connectCallback = connectCallback; OkHttpClient okHttpClient = new OkHttpClient .Builder() .readTimeout(10, TimeUnit.SECONDS) @@ -54,6 +57,7 @@ public class WSClient extends WebSocketListener { @Override public void onOpen(WebSocket webSocket, Response response) { super.onOpen(webSocket, response); + connectCallback.connected(); } @Override 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 b8ff35f0..4ca281c5 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 @@ -40,7 +40,7 @@ public class RemoteJSExecutor { .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .build(); - final Request request = new Request.Builder().url("ws://192.168.24.221:2080").build(); + final Request request = new Request.Builder().url("ws://192.168.25.175:2080").build(); final Thread current = Thread.currentThread(); webSocket = okHttpClient.newWebSocket(request, new WebSocketListener() { diff --git a/Android/doric/src/main/res/drawable/divider.xml b/Android/doric/src/main/res/drawable/divider.xml new file mode 100644 index 00000000..5efa3f64 --- /dev/null +++ b/Android/doric/src/main/res/drawable/divider.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Android/doric/src/main/res/layout/layout_dev.xml b/Android/doric/src/main/res/layout/layout_dev.xml index 09f24399..2b9e6f63 100644 --- a/Android/doric/src/main/res/layout/layout_dev.xml +++ b/Android/doric/src/main/res/layout/layout_dev.xml @@ -2,39 +2,33 @@ + android:divider="@drawable/divider" + android:orientation="vertical" + android:showDividers="middle"> - - - - + android:textSize="20sp" + android:visibility="gone" /> + android:textSize="20sp" + android:visibility="gone" /> \ No newline at end of file