using Android.Content; using Android.Content.PM; using Android.OS; using System.Properties; using AndroidApplication = Android.App.Application; using JString = Java.Lang.String; namespace System.Application.Security { /// /// 设备安全检查 /// public static class DeviceSecurityCheckUtil { /// /// 是否运行在兼容的PC中,例如Chromebook中的Chrome OS /// /// /// static bool IsCompatiblePC(Context context) { const string ARC_DEVICE_PATTERN = ".+_cheets|cheets_.+"; var device = Build.Device; if (device != null) { var j_device = new JString(device); if (j_device.Matches(ARC_DEVICE_PATTERN)) { return true; } } var packageManager = context.PackageManager; if (packageManager != null) { if (packageManager.HasSystemFeature("org.chromium.arc.device_management")) { return true; } if (packageManager.HasSystemFeature(PackageManager.FeaturePc)) { return true; } } return false; } /// /// 当前设备是否经过了安全检查 /// /// /// public static bool IsSupported(bool enableEmulator) { if (!enableEmulator && AndroidROM.IsEmulator) return Exit(ExitCode.IsEmulator); if (!enableEmulator && SecurityCheckUtil.CheckIsDebuggerConnected()) return Exit(ExitCode.IsDebuggerConnected); if (SecurityCheckUtil.IsXposedExists()) return Exit(ExitCode.IsXposedExists); if (SecurityCheckUtil.IsRoot()) return Exit(ExitCode.IsRoot); var context = AndroidApplication.Context; if (!enableEmulator && SecurityCheckUtil.CheckIsDebugVersion(context)) return Exit(ExitCode.IsDebugVersion); if (IsCompatiblePC(context)) return Exit(ExitCode.IsCompatiblePC); if (VirtualApkCheckUtil.Check(context)) return Exit(ExitCode.IsVirtualApk); return true; static bool Exit(ExitCode code) { var str = code switch { ExitCode.IsVirtualApk => SR.ExitCode_IsVirtualApk, ExitCode.IsRoot => SR.ExitCode_IsRoot, ExitCode.IsXposedExists => SR.ExitCode_IsXposedExists, _ => null, }; if (str != null) { Toast.Show(str, ToastLength.Long); } else { Log.Error("Security", "exit code: {0}", (int)code); } return false; } } enum ExitCode { /// /// (423)不支持运行在 [模拟器] 中 /// IsEmulator = 423, /// /// (451)不支持运行在 [兼容的PC] 中 /// IsCompatiblePC = 451, /// /// (452)不允许链接上调试器运行 /// IsDebuggerConnected, /// /// (453)当前是调试版本不允许在非调试环境下允许 /// IsDebugVersion, /// /// (454)不允许多开/分身 /// IsVirtualApk, /// /// (455)存在Xposed框架 /// IsXposedExists, /// /// (456)设备已开放Root权限 /// IsRoot, /// /// (457)设备是手表⌚ /// IsWear, } } }