package chaoran.business.utils; import static androidx.core.content.ContextCompat.getSystemService; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.provider.Settings; import android.util.Log; import android.view.Surface; import android.view.View; import android.view.WindowManager; 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 java.util.Random; import chaoran.business.BuildConfig; public class LocalAddressUtil { public final static String SSO_KEY = "!~CROP@CRTECH@PDA~!"; 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); } @SuppressLint("JavascriptInterface") @JavascriptInterface public String getIpAddress() { try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); // if(!intf.getDisplayName().equals("eth0")) continue; for (Enumeration 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以下 SharedPreferences spf = context.getSharedPreferences("chaoran_mac", Context.MODE_PRIVATE); SharedPreferences.Editor editor1 = context.getSharedPreferences("CrtechPdaConfig", Context.MODE_PRIVATE).edit(); editor1.putString("checkMacTime", String.valueOf(System.currentTimeMillis() - 1 * 60 * 60 * 1000)); editor1.commit(); String saveMac = spf.getString("chaoran_mac", ""); if (!"".equalsIgnoreCase(saveMac)) { return saveMac; } SharedPreferences.Editor editor = spf.edit(); String androidMac = getAndroidMac(); editor.putString("chaoran_mac", androidMac); editor.apply(); return androidMac; } /** * 获取安卓的mac * @return */ private String getAndroidMac() { if (Build.VERSION.SDK_INT >= 34) { // Android 14 is code-named Tiramisu try { return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); }catch (Exception e) { e.printStackTrace(); return generateRandomMacAddress(); } } 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 generateRandomMacAddress(); } 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 (Exception e) { e.printStackTrace(); macAddress = generateRandomMacAddress(); } if (macAddress.length() < 4) { macAddress = generateRandomMacAddress(); } return macAddress; } private static String generateRandomMacAddress() { Random rand = new Random(); byte[] macBytes = new byte[6]; rand.nextBytes(macBytes); macBytes[0] = (byte) (macBytes[0] & 0xfe | 0x02); StringBuilder macAddress = new StringBuilder(); for (int i = 0; i < macBytes.length; i++) { macAddress.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? ":" : "")); } return macAddress.toString(); } @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; } 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; } } @SuppressLint("JavascriptInterface") @JavascriptInterface public String registerMac(String applyMac) { try { String mac = this.getMacAddress(); String md5Hash = MD5.md5(mac + SSO_KEY); if ("Crtech!register@PDA#APK".equals(applyMac) || (md5Hash != null && md5Hash.equals(applyMac))) { // 将允许注册适配的mac写入文件中 SharedPreferences.Editor editor = context.getSharedPreferences("CrtechPdaConfig", Context.MODE_PRIVATE).edit(); editor.putString("checkMac", "success"); if ("Crtech!register@PDA#APK".equals(applyMac)) { editor.putString("checkMacTime", String.valueOf(System.currentTimeMillis())); editor.putString("checkMacType", "1"); }else { editor.putString("checkMacType", "0"); } editor.commit(); return "success"; } }catch (Exception ignored) { } return "error"; } @SuppressLint("JavascriptInterface") @JavascriptInterface public String checkMacRegister() { SharedPreferences sharedPreferences = context.getSharedPreferences("CrtechPdaConfig", Context.MODE_PRIVATE); String checkMac = sharedPreferences.getString("checkMac", "error"); if (!"success".equals(checkMac)) { return "error"; } if ("1".equals(sharedPreferences.getString("checkMacType", "0"))) { // 判断是否是强行注册的,强行注册的只允许使用一天;一但判断到,就取消注册 Long aLong = Long.valueOf(sharedPreferences.getString("checkMacTime", String.valueOf(System.currentTimeMillis()))); if ((aLong + 1 * 60 * 1000) < System.currentTimeMillis()) { SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString("checkMac", "error"); edit.commit(); return "error"; } } return "success"; } @SuppressLint("JavascriptInterface") @JavascriptInterface public String getApkVersion() { return BuildConfig.VERSION_NAME; } /** * 暴露PDA的厂家和型号 * @return String 格式:厂家:型号 */ @SuppressLint("JavascriptInterface") @JavascriptInterface public String getPdaInfo() { return Build.MANUFACTURER + ":" + Build.MODEL.toLowerCase(); } }