add dev kit disconnect event

This commit is contained in:
王劲鹏 2019-11-11 21:03:40 +08:00
parent 9c9fcf4087
commit df0315183c
10 changed files with 46 additions and 56 deletions

View File

@ -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() {

View File

@ -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

View File

@ -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();
}

View File

@ -1,7 +0,0 @@
package pub.doric.dev;
public interface ConnectCallback {
void connected();
void exception(Exception exception);
}

View File

@ -1,4 +0,0 @@
package pub.doric.dev;
public class ConnectEvent {
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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());
}
}
}

View File

@ -0,0 +1,4 @@
package pub.doric.dev.event;
public class EOFEvent {
}

View File

@ -0,0 +1,4 @@
package pub.doric.dev.event;
public class OpenEvent {
}