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 android.app.Application;
import pub.doric.dev.ConnectCallback;
/** /**
* @Description: Doric * @Description: Doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -35,8 +33,8 @@ public class Doric {
return sApplication; return sApplication;
} }
public static void connectDevKit(String url, ConnectCallback connectCallback) { public static void connectDevKit(String url) {
DoricDriver.getInstance().connectDevKit(url, connectCallback); DoricDriver.getInstance().connectDevKit(url);
} }
public static void disconnectDevKit() { public static void disconnectDevKit() {

View File

@ -20,7 +20,6 @@ import android.os.Looper;
import pub.doric.async.AsyncCall; import pub.doric.async.AsyncCall;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.dev.ConnectCallback;
import pub.doric.dev.WSClient; import pub.doric.dev.WSClient;
import pub.doric.engine.DoricJSEngine; import pub.doric.engine.DoricJSEngine;
import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricConstant;
@ -139,8 +138,8 @@ public class DoricDriver implements IDoricDriver {
} }
@Override @Override
public void connectDevKit(String url, ConnectCallback connectCallback) { public void connectDevKit(String url) {
wsClient = new WSClient(url, connectCallback); wsClient = new WSClient(url);
} }
@Override @Override

View File

@ -16,14 +16,13 @@
package pub.doric; 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 com.github.pengfeizhou.jscore.JSDecoder;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import pub.doric.async.AsyncResult;
import pub.doric.utils.ThreadMode;
/** /**
* @Description: com.github.penfeizhou.doric * @Description: com.github.penfeizhou.doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -42,7 +41,7 @@ public interface IDoricDriver {
DoricRegistry getRegistry(); DoricRegistry getRegistry();
void connectDevKit(String url, ConnectCallback connectCallback); void connectDevKit(String url);
void disconnectDevKit(); 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.disposables.Disposable;
import io.reactivex.functions.Consumer; import io.reactivex.functions.Consumer;
import pub.doric.R; import pub.doric.R;
import pub.doric.dev.event.EOFEvent;
import pub.doric.dev.event.OpenEvent;
public class DevPanel extends BottomSheetDialogFragment { 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 @Override
@ -87,7 +79,20 @@ public class DevPanel extends BottomSheetDialogFragment {
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ConnectEvent connectEvent) { public void onOpenEvent(OpenEvent openEvent) {
isDevConnected = true; 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.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import org.greenrobot.eventbus.EventBus;
import cn.bingoogolapple.qrcode.core.QRCodeView; import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView; import cn.bingoogolapple.qrcode.zxing.ZXingView;
import pub.doric.Doric; import pub.doric.Doric;
@ -52,20 +50,10 @@ public class ScanQRCodeActivity extends AppCompatActivity implements QRCodeView.
public void onScanQRCodeSuccess(String result) { public void onScanQRCodeSuccess(String result) {
setTitle("扫描结果为:" + result); setTitle("扫描结果为:" + result);
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show(); Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
Doric.connectDevKit("ws://" + result + ":7777", new ConnectCallback() { Doric.connectDevKit("ws://" + result + ":7777");
@Override
public void connected() {
EventBus.getDefault().post(new ConnectEvent());
finish(); finish();
} }
@Override
public void exception(Exception exception) {
}
});
}
@Override @Override
public void onCameraAmbientBrightnessChanged(boolean isDark) { public void onCameraAmbientBrightnessChanged(boolean isDark) {
String tipText = mZXingView.getScanBoxView().getTipText(); String tipText = mZXingView.getScanBoxView().getTipText();

View File

@ -15,12 +15,11 @@
*/ */
package pub.doric.dev; package pub.doric.dev;
import pub.doric.DoricContext; import org.greenrobot.eventbus.EventBus;
import pub.doric.DoricContextManager;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.EOFException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -28,6 +27,10 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.WebSocket; import okhttp3.WebSocket;
import okhttp3.WebSocketListener; 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 * @Description: com.github.penfeizhou.doric.dev
@ -37,10 +40,7 @@ import okhttp3.WebSocketListener;
public class WSClient extends WebSocketListener { public class WSClient extends WebSocketListener {
private final WebSocket webSocket; private final WebSocket webSocket;
private ConnectCallback connectCallback; public WSClient(String url) {
public WSClient(String url, ConnectCallback connectCallback) {
this.connectCallback = connectCallback;
OkHttpClient okHttpClient = new OkHttpClient OkHttpClient okHttpClient = new OkHttpClient
.Builder() .Builder()
.readTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS)
@ -57,7 +57,7 @@ public class WSClient extends WebSocketListener {
@Override @Override
public void onOpen(WebSocket webSocket, Response response) { public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response); super.onOpen(webSocket, response);
connectCallback.connected(); EventBus.getDefault().post(new OpenEvent());
} }
@Override @Override
@ -91,5 +91,9 @@ public class WSClient extends WebSocketListener {
@Override @Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) { public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, 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 {
}