diff --git a/core/mogo-core-utils/src/main/AndroidManifest.xml b/core/mogo-core-utils/src/main/AndroidManifest.xml index c953b8a6ce..11c86de1fd 100644 --- a/core/mogo-core-utils/src/main/AndroidManifest.xml +++ b/core/mogo-core-utils/src/main/AndroidManifest.xml @@ -1,6 +1,8 @@ + + = 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); + } + } \ No newline at end of file