From df0315183c585a57e9a77b34e84d550314a112b5 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 21:03:40 +0800 Subject: [PATCH] add dev kit disconnect event --- .../doric/src/main/java/pub/doric/Doric.java | 6 ++--- .../src/main/java/pub/doric/DoricDriver.java | 5 ++-- .../src/main/java/pub/doric/IDoricDriver.java | 9 +++---- .../java/pub/doric/dev/ConnectCallback.java | 7 ----- .../main/java/pub/doric/dev/ConnectEvent.java | 4 --- .../src/main/java/pub/doric/dev/DevPanel.java | 27 +++++++++++-------- .../pub/doric/dev/ScanQRCodeActivity.java | 16 ++--------- .../src/main/java/pub/doric/dev/WSClient.java | 20 ++++++++------ .../java/pub/doric/dev/event/EOFEvent.java | 4 +++ .../java/pub/doric/dev/event/OpenEvent.java | 4 +++ 10 files changed, 46 insertions(+), 56 deletions(-) delete mode 100644 Android/doric/src/main/java/pub/doric/dev/ConnectCallback.java delete mode 100644 Android/doric/src/main/java/pub/doric/dev/ConnectEvent.java create mode 100644 Android/doric/src/main/java/pub/doric/dev/event/EOFEvent.java create mode 100644 Android/doric/src/main/java/pub/doric/dev/event/OpenEvent.java diff --git a/Android/doric/src/main/java/pub/doric/Doric.java b/Android/doric/src/main/java/pub/doric/Doric.java index e128c0b0..c85dbe5d 100644 --- a/Android/doric/src/main/java/pub/doric/Doric.java +++ b/Android/doric/src/main/java/pub/doric/Doric.java @@ -17,8 +17,6 @@ package pub.doric; import android.app.Application; -import pub.doric.dev.ConnectCallback; - /** * @Description: Doric * @Author: pengfei.zhou @@ -35,8 +33,8 @@ public class Doric { return sApplication; } - public static void connectDevKit(String url, ConnectCallback connectCallback) { - DoricDriver.getInstance().connectDevKit(url, connectCallback); + public static void connectDevKit(String url) { + DoricDriver.getInstance().connectDevKit(url); } 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 900e01e1..e36d67df 100644 --- a/Android/doric/src/main/java/pub/doric/DoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/DoricDriver.java @@ -20,7 +20,6 @@ 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; @@ -139,8 +138,8 @@ public class DoricDriver implements IDoricDriver { } @Override - public void connectDevKit(String url, ConnectCallback connectCallback) { - wsClient = new WSClient(url, connectCallback); + public void connectDevKit(String url) { + wsClient = new WSClient(url); } @Override diff --git a/Android/doric/src/main/java/pub/doric/IDoricDriver.java b/Android/doric/src/main/java/pub/doric/IDoricDriver.java index 0dc212e2..d503e8f5 100644 --- a/Android/doric/src/main/java/pub/doric/IDoricDriver.java +++ b/Android/doric/src/main/java/pub/doric/IDoricDriver.java @@ -16,14 +16,13 @@ package pub.doric; -import pub.doric.async.AsyncResult; -import pub.doric.dev.ConnectCallback; -import pub.doric.utils.ThreadMode; - import com.github.pengfeizhou.jscore.JSDecoder; import java.util.concurrent.Callable; +import pub.doric.async.AsyncResult; +import pub.doric.utils.ThreadMode; + /** * @Description: com.github.penfeizhou.doric * @Author: pengfei.zhou @@ -42,7 +41,7 @@ public interface IDoricDriver { DoricRegistry getRegistry(); - void connectDevKit(String url, ConnectCallback connectCallback); + void connectDevKit(String url); 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 deleted file mode 100644 index 4e65df33..00000000 --- a/Android/doric/src/main/java/pub/doric/dev/ConnectCallback.java +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 520d9699..00000000 --- a/Android/doric/src/main/java/pub/doric/dev/ConnectEvent.java +++ /dev/null @@ -1,4 +0,0 @@ -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 d8e84e3d..ed831c44 100644 --- a/Android/doric/src/main/java/pub/doric/dev/DevPanel.java +++ b/Android/doric/src/main/java/pub/doric/dev/DevPanel.java @@ -21,6 +21,8 @@ import org.greenrobot.eventbus.ThreadMode; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Consumer; import pub.doric.R; +import pub.doric.dev.event.EOFEvent; +import pub.doric.dev.event.OpenEvent; public class DevPanel extends BottomSheetDialogFragment { @@ -62,16 +64,6 @@ 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 @@ -87,7 +79,20 @@ public class DevPanel extends BottomSheetDialogFragment { } @Subscribe(threadMode = ThreadMode.MAIN) - public void onMessageEvent(ConnectEvent connectEvent) { + public void onOpenEvent(OpenEvent openEvent) { isDevConnected = true; + + 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); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEOFEvent(EOFEvent eofEvent) { + isDevConnected = false; + + 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); } } 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 80a42eb2..0df9e81b 100644 --- a/Android/doric/src/main/java/pub/doric/dev/ScanQRCodeActivity.java +++ b/Android/doric/src/main/java/pub/doric/dev/ScanQRCodeActivity.java @@ -6,8 +6,6 @@ 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; @@ -52,18 +50,8 @@ public class ScanQRCodeActivity extends AppCompatActivity implements QRCodeView. 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) { - - } - }); + Doric.connectDevKit("ws://" + result + ":7777"); + finish(); } @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 d03a0db6..43b9df6d 100644 --- a/Android/doric/src/main/java/pub/doric/dev/WSClient.java +++ b/Android/doric/src/main/java/pub/doric/dev/WSClient.java @@ -15,12 +15,11 @@ */ package pub.doric.dev; -import pub.doric.DoricContext; -import pub.doric.DoricContextManager; - +import org.greenrobot.eventbus.EventBus; import org.json.JSONException; import org.json.JSONObject; +import java.io.EOFException; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; @@ -28,6 +27,10 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.WebSocket; import okhttp3.WebSocketListener; +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; +import pub.doric.dev.event.EOFEvent; +import pub.doric.dev.event.OpenEvent; /** * @Description: com.github.penfeizhou.doric.dev @@ -37,10 +40,7 @@ import okhttp3.WebSocketListener; public class WSClient extends WebSocketListener { private final WebSocket webSocket; - private ConnectCallback connectCallback; - - public WSClient(String url, ConnectCallback connectCallback) { - this.connectCallback = connectCallback; + public WSClient(String url) { OkHttpClient okHttpClient = new OkHttpClient .Builder() .readTimeout(10, TimeUnit.SECONDS) @@ -57,7 +57,7 @@ public class WSClient extends WebSocketListener { @Override public void onOpen(WebSocket webSocket, Response response) { super.onOpen(webSocket, response); - connectCallback.connected(); + EventBus.getDefault().post(new OpenEvent()); } @Override @@ -91,5 +91,9 @@ public class WSClient extends WebSocketListener { @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { super.onFailure(webSocket, t, response); + + if (t instanceof EOFException) { + EventBus.getDefault().post(new EOFEvent()); + } } } diff --git a/Android/doric/src/main/java/pub/doric/dev/event/EOFEvent.java b/Android/doric/src/main/java/pub/doric/dev/event/EOFEvent.java new file mode 100644 index 00000000..aabb4931 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/dev/event/EOFEvent.java @@ -0,0 +1,4 @@ +package pub.doric.dev.event; + +public class EOFEvent { +} diff --git a/Android/doric/src/main/java/pub/doric/dev/event/OpenEvent.java b/Android/doric/src/main/java/pub/doric/dev/event/OpenEvent.java new file mode 100644 index 00000000..b99ccff4 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/dev/event/OpenEvent.java @@ -0,0 +1,4 @@ +package pub.doric.dev.event; + +public class OpenEvent { +}