Skip to content

Commit

Permalink
Merge pull request #19 from andot/master
Browse files Browse the repository at this point in the history
Add supported method.
  • Loading branch information
liyujiang-gzu authored Apr 8, 2021
2 parents 3366b57 + f5fca59 commit 96d0ca7
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions OAID_IMPL/src/main/java/com/github/gzuliyujiang/oaid/DeviceID.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
*/
public final class DeviceID {
private static String clientId;
private static IOAID oaid;

private DeviceID() {
super();
Expand Down Expand Up @@ -122,40 +123,65 @@ public static String getClientIdSHA1() {
return clientId == null ? "" : calculateHash(clientId, "SHA-1");
}

private static IOAID getIOAID(@NonNull Context context) {
if (OAIDRom.isLenovo() || OAIDRom.isMotolora()) {
return new LenovoImpl(context);
}
if (OAIDRom.isMeizu()) {
return new MeizuImpl(context);
}
if (OAIDRom.isNubia()) {
return new NubiaImpl(context);
}
if (OAIDRom.isXiaomi() || OAIDRom.isBlackShark()) {
return new XiaomiImpl(context);
}
if (OAIDRom.isSamsung()) {
return new SamsungImpl(context);
}
if (OAIDRom.isVivo()) {
return new VivoImpl(context);
}
if (OAIDRom.isASUS()) {
return new AsusImpl(context);
}
if (OAIDRom.isHuawei()) {
return new HuaweiImpl(context);
}
if (OAIDRom.isOppo() || OAIDRom.isOnePlus()) {
return new OppoImpl(context);
}
if (OAIDRom.isZTE() || OAIDRom.isFreeme() || OAIDRom.isSSUI()) {
return new MsaImpl(context);
}
return new DefaultImpl();
}

/**
* 异步获取OAID
*
* @param context 上下文
* @param getter 回调
*/
public static void getOAID(@NonNull Context context, @NonNull IGetter getter) {
IOAID oaid;
if (OAIDRom.isLenovo() || OAIDRom.isMotolora()) {
oaid = new LenovoImpl(context);
} else if (OAIDRom.isMeizu()) {
oaid = new MeizuImpl(context);
} else if (OAIDRom.isNubia()) {
oaid = new NubiaImpl(context);
} else if (OAIDRom.isXiaomi() || OAIDRom.isBlackShark()) {
oaid = new XiaomiImpl(context);
} else if (OAIDRom.isSamsung()) {
oaid = new SamsungImpl(context);
} else if (OAIDRom.isVivo()) {
oaid = new VivoImpl(context);
} else if (OAIDRom.isASUS()) {
oaid = new AsusImpl(context);
} else if (OAIDRom.isHuawei()) {
oaid = new HuaweiImpl(context);
} else if (OAIDRom.isOppo() || OAIDRom.isOnePlus()) {
oaid = new OppoImpl(context);
} else if (OAIDRom.isZTE() || OAIDRom.isFreeme() || OAIDRom.isSSUI()) {
oaid = new MsaImpl(context);
} else {
oaid = new DefaultImpl();
if (oaid == null) {
oaid = getIOAID(context);
}
oaid.doGet(getter);
}

/**
* 判断是否支持OAID
*
* @param context 上下文
*/
public static boolean supported(@NonNull Context context) {
if (oaid == null) {
oaid = getIOAID(context);
}
return oaid.supported();
}

/**
* 获取唯一设备标识
*
Expand All @@ -170,9 +196,9 @@ public static String getUniqueID(@NonNull Context context) {
// Android 10+ 不允许获取 IMEI、MEID 之类的设备唯一标识
return "";
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
context.checkPermission(Manifest.permission.READ_PHONE_STATE, Process.myPid(),
Process.myUid()) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& context.checkPermission(Manifest.permission.READ_PHONE_STATE, Process.myPid(),
Process.myUid()) != PackageManager.PERMISSION_GRANTED) {
// Android 6-9 需要申请电话权限才能获取设备唯一标识
return "";
}
Expand Down Expand Up @@ -255,7 +281,7 @@ public static String getPseudoID() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
sb.append(Arrays.deepToString(Build.SUPPORTED_ABIS).length() % 10);
} else {
//noinspection deprecation
// noinspection deprecation
sb.append(Build.CPU_ABI.length() % 10);
}
sb.append(Build.DEVICE.length() % 10);
Expand Down Expand Up @@ -291,8 +317,8 @@ public static String getGUID(@NonNull Context context) {
}

/**
* 计算哈希值,算法可以是MD2、MD5、SHA-1、SHA-224、SHA-256、SHA-512等。
* 支持的算法见 https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest
* 计算哈希值,算法可以是MD2、MD5、SHA-1、SHA-224、SHA-256、SHA-512等。 支持的算法见
* https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest
*/
public static String calculateHash(String str, String algorithm) {
try {
Expand Down

0 comments on commit 96d0ca7

Please sign in to comment.