scan qrcode to connect dev kit & change dev panel ui

This commit is contained in:
王劲鹏 2019-11-11 18:51:45 +08:00
parent 8d0d78a12f
commit 9c9fcf4087
13 changed files with 96 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#cccccc" />
<size android:height="1dp" />
</shape>

View File

@ -2,39 +2,33 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:divider="@drawable/divider"
android:orientation="vertical"
android:showDividers="middle">
<TextView
android:id="@+id/menu1_text_view"
android:id="@+id/connect_dev_kit_text_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="Dev Kit"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc" />
<TextView
android:id="@+id/menu2_text_view"
android:id="@+id/debug_text_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="Debug via Visual Studio Code"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc" />
android:textSize="20sp"
android:visibility="gone" />
<TextView
android:id="@+id/menu3_text_view"
android:id="@+id/hot_reload_text_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="Enable Hot Reload"
android:textSize="20sp" />
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>