add emulator check

This commit is contained in:
王劲鹏 2019-11-12 15:40:01 +08:00
parent 0697ae70fe
commit c4e2c1e0dd
4 changed files with 68 additions and 26 deletions

View File

@ -58,6 +58,7 @@ dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.10.2' implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
implementation "io.reactivex.rxjava2:rxjava:2.2.14" implementation "io.reactivex.rxjava2:rxjava:2.2.14"
implementation 'org.greenrobot:eventbus:3.1.1' implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.lahm.library:easy-protector-release:1.1.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'

View File

@ -13,6 +13,8 @@ import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.lahm.library.EasyProtectorLib;
import com.lahm.library.EmulatorCheckCallback;
import com.tbruyelle.rxpermissions2.RxPermissions; import com.tbruyelle.rxpermissions2.RxPermissions;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -28,10 +30,12 @@ import pub.doric.IDoricDriver;
import pub.doric.R; import pub.doric.R;
import pub.doric.dev.event.EOFEvent; import pub.doric.dev.event.EOFEvent;
import pub.doric.dev.event.OpenEvent; import pub.doric.dev.event.OpenEvent;
import pub.doric.utils.DoricUtils;
public class DevPanel extends BottomSheetDialogFragment { public class DevPanel extends BottomSheetDialogFragment {
private boolean isDevConnected = false; private boolean isRunningInEmulator = false;
static boolean isDevConnected = false;
public DevPanel() { public DevPanel() {
@ -51,9 +55,14 @@ public class DevPanel extends BottomSheetDialogFragment {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
updateUI();
getView().findViewById(R.id.connect_dev_kit_text_view).setOnClickListener(new View.OnClickListener() { getView().findViewById(R.id.connect_dev_kit_text_view).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (isRunningInEmulator) {
Doric.connectDevKit("ws://" + "10.0.2.2" + ":7777");
} else {
final RxPermissions rxPermissions = new RxPermissions(DevPanel.this); final RxPermissions rxPermissions = new RxPermissions(DevPanel.this);
Disposable disposable = rxPermissions Disposable disposable = rxPermissions
.request(Manifest.permission.CAMERA) .request(Manifest.permission.CAMERA)
@ -66,7 +75,7 @@ public class DevPanel extends BottomSheetDialogFragment {
} }
} }
}); });
}
} }
}); });
@ -86,6 +95,12 @@ public class DevPanel extends BottomSheetDialogFragment {
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
isRunningInEmulator = EasyProtectorLib.checkIsRunningInEmulator(context, new EmulatorCheckCallback() {
@Override
public void findEmulator(String emulatorInfo) {
System.out.println(emulatorInfo);
}
});
} }
@Override @Override
@ -96,19 +111,23 @@ public class DevPanel extends BottomSheetDialogFragment {
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onOpenEvent(OpenEvent openEvent) { public void onOpenEvent(OpenEvent openEvent) {
isDevConnected = true; updateUI();
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) @Subscribe(threadMode = ThreadMode.MAIN)
public void onEOFEvent(EOFEvent eofEvent) { public void onEOFEvent(EOFEvent eofEvent) {
isDevConnected = false; updateUI();
}
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.connect_dev_kit_text_view).setVisibility(View.VISIBLE);
getView().findViewById(R.id.debug_text_view).setVisibility(View.GONE); getView().findViewById(R.id.debug_text_view).setVisibility(View.GONE);
getView().findViewById(R.id.hot_reload_text_view).setVisibility(View.GONE); getView().findViewById(R.id.hot_reload_text_view).setVisibility(View.GONE);
} }
} }
}

View File

@ -57,6 +57,8 @@ 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);
DevPanel.isDevConnected = true;
EventBus.getDefault().post(new OpenEvent()); EventBus.getDefault().post(new OpenEvent());
} }
@ -93,6 +95,7 @@ public class WSClient extends WebSocketListener {
super.onFailure(webSocket, t, response); super.onFailure(webSocket, t, response);
if (t instanceof EOFException) { if (t instanceof EOFException) {
DevPanel.isDevConnected = false;
EventBus.getDefault().post(new EOFEvent()); EventBus.getDefault().post(new EOFEvent());
} }
} }

View File

@ -17,14 +17,11 @@ package pub.doric.utils;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import androidx.annotation.NonNull;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.Display; import android.view.Display;
import android.view.WindowManager; import android.view.WindowManager;
import pub.doric.Doric; import androidx.annotation.NonNull;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
@ -40,6 +37,12 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; 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 * @Description: Doric
@ -235,4 +238,20 @@ public class DoricUtils {
return sbar; return sbar;
} }
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> 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;
}
} }