package com.example.myapplication;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.util.Locale;
/**
* AB面判断逻辑 https://zhuanlan.zhihu.com/p/717396587
* <p>
* 1.开机时间不超过48小时 进A面
* 2.开启了代理 进A面
* 3.连上了USB 进A面
* 4.打开了ADB调试 进A面
* 5.是模拟器 进A面
* 6.SIM卡对应国家,非目标国家,进A面
* 6.开启了Root 进A面
* <p>
* 上述7种情况都不符合,进B面
*/
public class DeviceUtil {
public static boolean checkBusiness(Context context) {
//1.开机时间判断
Log.e("xxx", "开机时间(hour):"+DeviceUtil.elapsedRealTime());
if(DeviceUtil.elapsedRealTime()<=48){
Log.e("xxx","开机时间不到48小时,进入A面");
return false;
}else{
Log.e("xxx","开机到了48小时,下一步");
}
//2.代理判断
if(isProxy(context)){
Log.e("xxx","开了代理,进入A面");
return false;
}else{
Log.e("xxx","没有开启代理,下一步");
}
//3.usb判断
if(hasConnectUsb(context)){
Log.e("xxx","连上了usb,进入A面");
Toast.makeText(context, "连上了Usb,进入A面", Toast.LENGTH_SHORT).show();
return false;
}else{
Log.e("xxx","没有边连上usb,下一步");
Toast.makeText(context, "没有Usb", Toast.LENGTH_SHORT).show();
}
//4.ADB判断
if(hasOpenAdb(context)){
Log.e("xxx","打开了ADB,进入A面");
Toast.makeText(context, "打开了ADB", Toast.LENGTH_SHORT).show();
return false;
}else{
Log.e("xxx","没有打开ADB");
Toast.makeText(context, "没有打开ADB,下一步", Toast.LENGTH_SHORT).show();
}
//5.模拟器判断
if (isEmulator(context)) {
Toast.makeText(context, "是模拟器,进入A面", Toast.LENGTH_SHORT).show();
Log.e("xxx", "是模拟器");
return false;
} else {
Toast.makeText(context, "不是模拟器", Toast.LENGTH_SHORT).show();
Log.e("xxx", "不是模拟器,下一步");
}
//6.SIM卡国家判断
if(!is_country(context,"VN")){
Toast.makeText(context, "不是目标国家,进入A面", Toast.LENGTH_SHORT).show();
Log.e("xxx","不是目标国家");
return false;
}else{
Toast.makeText(context, "是目标国家", Toast.LENGTH_SHORT).show();
Log.e("xxx","目标国家,下一步");
}
//7.Root判断
if (isRoot()) {
Toast.makeText(context, "开启了Root权限,进入A面", Toast.LENGTH_SHORT).show();
Log.e("xxx", "开启了Root权限");
return false;
} else {
Toast.makeText(context, "没有Root,进入B面", Toast.LENGTH_SHORT).show();
Log.e("xxx", "没有Root,进入B面");
}
return true;
}
//1.是否开了代理
public static boolean isProxy(Context context) {
String property = System.getProperty("http.proxyHost");
return property != null && !property.isEmpty();
}
//2.最近一次开机多少小时
public static long elapsedRealTime() {
return SystemClock.elapsedRealtime() / (1000 * 60 * 60);
}
//3.是否连上了USB
public static boolean hasConnectUsb(Context context) {
return context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE")).getExtras().getBoolean("connected");
}
//4.是否开启了adb
public static boolean hasOpenAdb(Context context) {
if (Build.VERSION.SDK_INT >= 17) {
try {
return Settings.Global.getInt(context.getContentResolver(), "adb_enabled") == 1;
} catch (Settings.SettingNotFoundException e) {
throw new RuntimeException(e);
}
} else if (Build.VERSION.SDK_INT > 3) {
try {
return Settings.Secure.getInt(context.getContentResolver(), "adb_enabled") == 1;
} catch (Settings.SettingNotFoundException e) {
throw new RuntimeException(e);
}
} else {
return false;
}
}
//5.是不是模拟器
public static boolean isEmulator(Context context) {
if (Build.DEVICE.contains("emu")) {
return true;
}
if (Build.PRODUCT.contains("sdk") || Build.PRODUCT.contains("x86")) {
return true;
}
if(Build.FINGERPRINT.contains("generic")){
return true;
}
return false;
}
//6.sim 所在卡国家
public static boolean is_country(Context context,String country) {
try {
final TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String simCountryIso = telephonyManager.getSimCountryIso();
simCountryIso = simCountryIso.toUpperCase(Locale.US);
Log.e("xxx","aaa:"+simCountryIso+":xxx");
if (country.equals(simCountryIso)) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//7. 判断是否被root 检查已知的Root文件
//一个常见的方法是检查设备上是否存在一些已知的Root文件或目录。例如,/system/app/Superuser.apk
// 是CyanogenMod等一些定制ROM中用于管理Root权限的应用程序。
public static boolean isRoot() {
String[] paths = {
"/system/app/Superuser.apk",
"/sbin/su",
"/system/bin/su",
"/system/xbin/su",
"/data/local/xbin/su",
"/data/local/bin/su",
"/su/bin/su"
};
for (String path : paths) {
if (new File(path).exists()) {
return true;
}
}
return false;
}
}
//使用方法
if(DeviceUtil.checkBusiness(this)){
//todo 进入B面
Toast.makeText(this,"进入B面",0).show();
}else{
//todo 进入A面
Toast.makeText(this,"进入A面",0).show();
}