diff --git a/Android/doric/build.gradle b/Android/doric/build.gradle index de20e78b..8ca243ce 100644 --- a/Android/doric/build.gradle +++ b/Android/doric/build.gradle @@ -58,6 +58,7 @@ dependencies { implementation 'com.github.tbruyelle:rxpermissions:0.10.2' implementation "io.reactivex.rxjava2:rxjava:2.2.14" implementation 'org.greenrobot:eventbus:3.1.1' + implementation 'com.lahm.library:easy-protector-release:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' 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 6c5e9d8a..68148286 100644 --- a/Android/doric/src/main/java/pub/doric/dev/DevPanel.java +++ b/Android/doric/src/main/java/pub/doric/dev/DevPanel.java @@ -13,6 +13,8 @@ import androidx.annotation.Nullable; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.gson.JsonObject; +import com.lahm.library.EasyProtectorLib; +import com.lahm.library.EmulatorCheckCallback; import com.tbruyelle.rxpermissions2.RxPermissions; import org.greenrobot.eventbus.EventBus; @@ -28,10 +30,12 @@ import pub.doric.IDoricDriver; import pub.doric.R; import pub.doric.dev.event.EOFEvent; import pub.doric.dev.event.OpenEvent; +import pub.doric.utils.DoricUtils; public class DevPanel extends BottomSheetDialogFragment { - private boolean isDevConnected = false; + private boolean isRunningInEmulator = false; + static boolean isDevConnected = false; public DevPanel() { @@ -51,22 +55,27 @@ public class DevPanel extends BottomSheetDialogFragment { public void onStart() { super.onStart(); + updateUI(); + 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); - Disposable disposable = rxPermissions - .request(Manifest.permission.CAMERA) - .subscribe(new Consumer() { - @Override - public void accept(Boolean grant) throws Exception { - if (grant) { - Intent intent = new Intent(getContext(), ScanQRCodeActivity.class); - getContext().startActivity(intent); + if (isRunningInEmulator) { + Doric.connectDevKit("ws://" + "10.0.2.2" + ":7777"); + } else { + final RxPermissions rxPermissions = new RxPermissions(DevPanel.this); + Disposable disposable = rxPermissions + .request(Manifest.permission.CAMERA) + .subscribe(new Consumer() { + @Override + public void accept(Boolean grant) throws Exception { + if (grant) { + Intent intent = new Intent(getContext(), ScanQRCodeActivity.class); + getContext().startActivity(intent); + } } - } - }); - + }); + } } }); @@ -86,6 +95,12 @@ public class DevPanel extends BottomSheetDialogFragment { public void onAttach(@NonNull Context context) { super.onAttach(context); EventBus.getDefault().register(this); + isRunningInEmulator = EasyProtectorLib.checkIsRunningInEmulator(context, new EmulatorCheckCallback() { + @Override + public void findEmulator(String emulatorInfo) { + System.out.println(emulatorInfo); + } + }); } @Override @@ -96,19 +111,23 @@ public class DevPanel extends BottomSheetDialogFragment { @Subscribe(threadMode = ThreadMode.MAIN) 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); + updateUI(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEOFEvent(EOFEvent eofEvent) { - isDevConnected = false; + updateUI(); + } - 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); + private void updateUI() { + 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); + } } } 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 7e295d62..7eec5207 100644 --- a/Android/doric/src/main/java/pub/doric/dev/WSClient.java +++ b/Android/doric/src/main/java/pub/doric/dev/WSClient.java @@ -57,6 +57,8 @@ public class WSClient extends WebSocketListener { @Override public void onOpen(WebSocket webSocket, Response response) { super.onOpen(webSocket, response); + + DevPanel.isDevConnected = true; EventBus.getDefault().post(new OpenEvent()); } @@ -93,6 +95,7 @@ public class WSClient extends WebSocketListener { super.onFailure(webSocket, t, response); if (t instanceof EOFException) { + DevPanel.isDevConnected = false; EventBus.getDefault().post(new EOFEvent()); } } diff --git a/Android/doric/src/main/java/pub/doric/utils/DoricUtils.java b/Android/doric/src/main/java/pub/doric/utils/DoricUtils.java index 40f28479..fc6d310b 100644 --- a/Android/doric/src/main/java/pub/doric/utils/DoricUtils.java +++ b/Android/doric/src/main/java/pub/doric/utils/DoricUtils.java @@ -17,14 +17,11 @@ package pub.doric.utils; import android.content.Context; import android.content.res.AssetManager; - -import androidx.annotation.NonNull; - import android.util.DisplayMetrics; import android.view.Display; import android.view.WindowManager; -import pub.doric.Doric; +import androidx.annotation.NonNull; import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSDecoder; @@ -40,6 +37,12 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Array; import java.lang.reflect.Field; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; + +import pub.doric.Doric; /** * @Description: Doric @@ -235,4 +238,20 @@ public class DoricUtils { return sbar; } + public static String getLocalIpAddress() { + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { + NetworkInterface intf = en.nextElement(); + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + return inetAddress.getHostAddress().toString(); + } + } + } + } catch (SocketException ex) { + System.out.println(ex); + } + return null; + } }