[Change]
增加获取数字版权管理设备ID方法,解决因为系统升级等原因造成获取的SN被改变的问题 Signed-off-by: donghongyu <donghongyu@zhidaoauto.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mogo.eagle.core.utilcode.util">
|
||||
|
||||
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
|
||||
<application>
|
||||
|
||||
<activity
|
||||
|
||||
@@ -3,38 +3,40 @@ package com.mogo.eagle.core.utilcode.util;
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.MediaDrm;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.elegant.utils.storage.SharedPrefsMgr;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.elegant.utils.storage.SharedPrefsMgr;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class DeviceIdUtils {
|
||||
|
||||
public static final String KEY_DEVICE_ID = "deviceId";
|
||||
|
||||
private DeviceIdUtils() {}
|
||||
private DeviceIdUtils() {
|
||||
}
|
||||
|
||||
private static void saveDeviceId( Context context, String deviceId){
|
||||
private static void saveDeviceId(Context context, String deviceId) {
|
||||
SharedPrefsMgr.getInstance(context).putString(KEY_DEVICE_ID, deviceId);
|
||||
}
|
||||
|
||||
public static String getDeviceId( Context context) {
|
||||
if(context == null){
|
||||
public static String getDeviceId(Context context) {
|
||||
if (context == null) {
|
||||
throw new NullPointerException("context must not be null.");
|
||||
}
|
||||
|
||||
final Context appContext = context.getApplicationContext();
|
||||
String deviceId = SharedPrefsMgr.getInstance( context ).getString( KEY_DEVICE_ID );
|
||||
String deviceId = SharedPrefsMgr.getInstance(context).getString(KEY_DEVICE_ID);
|
||||
|
||||
if ( TextUtils.isEmpty( deviceId )) {
|
||||
if (TextUtils.isEmpty(deviceId)) {
|
||||
deviceId = getDeviceIdInternal(appContext);
|
||||
if (TextUtils.isEmpty(deviceId)) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
@@ -54,35 +56,37 @@ public final class DeviceIdUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
saveDeviceId(appContext,deviceId);
|
||||
saveDeviceId(appContext, deviceId);
|
||||
}
|
||||
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
private static String getDeviceIdInternal( Context context) {
|
||||
private static String getDeviceIdInternal(Context context) {
|
||||
String id = "";
|
||||
|
||||
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
|
||||
if ( ContextCompat.checkSelfPermission( context, Manifest.permission.READ_PHONE_STATE ) != PackageManager.PERMISSION_GRANTED ) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
TelephonyManager telephonymanager = ( TelephonyManager ) context.getSystemService( Context.TELEPHONY_SERVICE);
|
||||
TelephonyManager telephonymanager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonymanager != null) {
|
||||
id = telephonymanager.getDeviceId();
|
||||
if ( TextUtils.isEmpty(id))
|
||||
if (TextUtils.isEmpty(id)) {
|
||||
id = "";
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
private static String getAndroidId( Context context) {
|
||||
private static String getAndroidId(Context context) {
|
||||
String s = "";
|
||||
s = Settings.Secure.getString(context.getContentResolver(), "android_id");
|
||||
if ( TextUtils.isEmpty(s))
|
||||
if (TextUtils.isEmpty(s)) {
|
||||
s = "";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -95,16 +99,63 @@ public final class DeviceIdUtils {
|
||||
if (!method.isAccessible()) {
|
||||
method.setAccessible(true);
|
||||
}
|
||||
serial = ( String ) method.invoke(new Build(), "ro.serialno");
|
||||
} catch ( ClassNotFoundException e) {
|
||||
serial = (String) method.invoke(new Build(), "ro.serialno");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch ( NoSuchMethodException e) {
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch ( InvocationTargetException e) {
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch ( IllegalAccessException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return serial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数字版权管理设备ID
|
||||
*
|
||||
* @return WidevineID,可能为空
|
||||
*/
|
||||
public static String getWidevineID(Context context) {
|
||||
try {
|
||||
//See https://stackoverflow.com/questions/16369818/how-to-get-crypto-scheme-uuid
|
||||
//You can find some UUIDs in the https://github.com/google/ExoPlayer source code
|
||||
final UUID WIDEVINE_UUID = new UUID(0xEDEF8BA979D64ACEL, 0xA3C827DCD51D21EDL);
|
||||
MediaDrm mediaDrm = new MediaDrm(WIDEVINE_UUID);
|
||||
byte[] widevineId = mediaDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID);
|
||||
if (widevineId == null) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte aByte : widevineId) {
|
||||
sb.append(String.format("%02x", aByte));
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数字版权管理设备ID,进行MD5加密,获取32位的唯一标记,给后台生成SN
|
||||
*
|
||||
* @return WidevineID,可能为空
|
||||
*/
|
||||
public static String getWidevineIDWithMd5(Context context) {
|
||||
try {
|
||||
String widevineId = getWidevineID(context);
|
||||
if (!TextUtils.isEmpty(widevineId)) {
|
||||
widevineId = EncryptUtils.encryptHmacMD5ToString(widevineId, "MoGoAuto");
|
||||
return widevineId;
|
||||
} else {
|
||||
return getDeviceId(context);
|
||||
}
|
||||
} catch (Exception | Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getDeviceId(context);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user