Files
pda-web/app/src/main/java/chaoran/business/utils/LocalAddressUtil.java

163 lines
5.7 KiB
Java
Raw Normal View History

2022-10-20 14:22:04 +08:00
package chaoran.business.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
2022-10-20 14:22:04 +08:00
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.pm.ActivityInfo;
2022-10-20 14:22:04 +08:00
import android.os.Build;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
2022-10-20 14:22:04 +08:00
import android.webkit.JavascriptInterface;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import chaoran.business.R;
import chaoran.business.activity.MainActivity;
2022-10-20 14:22:04 +08:00
public class LocalAddressUtil {
private Context context;
private Activity activity;
private View view;
private int[] heights;
public LocalAddressUtil(Context context, Activity activity, View view) {
this.context = context;
this.activity = activity;
this.view = view;
this.heights = StatusBarUtil.getStatusBarHeight(context);
}
2022-10-20 14:22:04 +08:00
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
// if(!intf.getDisplayName().equals("eth0")) continue;
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
Log.e("address: local", ex.toString());
}
return null;
}
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getMacAddress(){//可以兼容安卓7以下
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "02:00:00:00:00:02";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:02";
}
return macAddress;
}
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getInfo(int type){
String info;
switch (type) {
case 1: info = Build.BRAND;break;//品牌
case 2: info = Build.MODEL;break;//型号
case 3: info = Build.MANUFACTURER;break;//厂商
case 4: info = Build.DEVICE;break;//设备名
case 5: info = Build.ID;break;//设备硬件id
// case 6: info = Build.SERIAL;break;//序列号,可能获取不到
default: info = "";break;
}
return info;
}
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getHeight(int type) {
String info = "";
switch (type) {
case 1 : info = String.valueOf(this.heights[0]); break;
case 2 : info = String.valueOf(this.heights[1]); break;
}
return info;
2022-10-20 14:22:04 +08:00
}
public void test(){
Log.e("test", "MANUFACTURER=" + Build.MANUFACTURER);
Log.e("test", "BRAND=" + Build.BRAND);
Log.e("test", "MODEL=" + Build.MODEL);
Log.e("test", "VERSION.RELEASE=" + Build.VERSION.RELEASE);
Log.e("test", "VERSION.SDK_INT=" + Build.VERSION.SDK_INT);
Log.e("test", "DEVICE=" + Build.DEVICE);
Log.e("test", "HOST=" + Build.HOST);
Log.e("test", "ID=" + Build.ID);
Log.e("test", "TIME=" + Build.TIME);
Log.e("test", "TYPE=" + Build.TYPE);
Log.e("test", "PRODUCT=" + Build.PRODUCT);
Log.e("test", "BOARD=" + Build.BOARD);
Log.e("test", "DISPLAY=" + Build.DISPLAY);
Log.e("test", "FINGERPRINT=" + Build.FINGERPRINT);
Log.e("test", "HARDWARE=" + Build.HARDWARE);
Log.e("test", "BOOTLOADER=" + Build.BOOTLOADER);
Log.e("test", "TAGS=" + Build.TAGS);
Log.e("test", "UNKNOWN=" + Build.UNKNOWN);
Log.e("test", "USER=" + Build.USER);
}
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public void rotateScreen() {
int angle = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
switch (angle) {
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
default:
break;
}
}
2022-10-20 14:22:04 +08:00
}