提交 21ae9c08 authored 作者: 史晓晨's avatar 史晓晨

fix:转换kotlin,修复无法获取设备id问题

上级 1b4c625a
package com.clx.devideId.device_id_plugin;
package com.clx.devideId.device_id_plugin
import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import java.util.UUID;
import android.annotation.SuppressLint
import android.content.Context
import android.provider.Settings
import java.util.UUID
/**
* <pre>
......@@ -14,126 +13,132 @@ import java.util.UUID;
* desc : utils about device
* </pre>
*/
public final class DeviceUtils {
object DeviceUtils {
@SuppressLint("StaticFieldLeak")
private var mContext: Context? = null
private DeviceUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
private const val KEY_UDID = "KEY_UDID"
@SuppressLint("StaticFieldLeak")
private static Context mContext;
@Volatile
private var udid: String? = null
public static void init(Context context) {
mContext = context;
/**
* 初始化设备工具类
*/
fun init(context: Context?) {
if (context == null) return
mContext = context.applicationContext
}
/**
* Return the android id of device.
*
* @return the android id of device
* 获取 Android ID
*/
@SuppressLint("HardwareIds")
public static String getAndroidID() {
String id = Settings.Secure.getString(
mContext.getContentResolver(),
Settings.Secure.ANDROID_ID
);
if ("9774d56d682e549c".equals(id)) return "";
return id == null ? "" : id;
}
fun getAndroidID(): String {
val context = mContext ?: return ""
private static final String KEY_UDID = "KEY_UDID";
private volatile static String udid;
val id = Settings.Secure.getString(
context.contentResolver, Settings.Secure.ANDROID_ID
)
return when {
id == "9774d56d682e549c" -> "" // 过滤无效 Android ID
id.isNullOrEmpty() -> ""
else -> id
}
}
/**
* Return the unique device id.
* <pre>{1}{UUID(macAddress)}</pre>
* <pre>{2}{UUID(androidId )}</pre>
* <pre>{9}{UUID(random )}</pre>
*
* @return the unique device id
* 获取唯一设备 ID(默认前缀空、使用缓存)
*/
public static String getUniqueDeviceId() {
return getUniqueDeviceId("", true);
fun getUniqueDeviceId(): String {
return getUniqueDeviceId("", true)
}
/**
* Return the unique device id.
* <pre>android 10 deprecated {prefix}{1}{UUID(macAddress)}</pre>
* <pre>{prefix}{2}{UUID(androidId )}</pre>
* <pre>{prefix}{9}{UUID(random )}</pre>
*
* @param prefix The prefix of the unique device id.
* @return the unique device id
* 获取唯一设备 ID(指定前缀、使用缓存)
* @param prefix 设备 ID 前缀
*/
public static String getUniqueDeviceId(String prefix) {
return getUniqueDeviceId(prefix, true);
fun getUniqueDeviceId(prefix: String?): String {
return getUniqueDeviceId(prefix, true)
}
/**
* Return the unique device id.
* <pre>{1}{UUID(macAddress)}</pre>
* <pre>{2}{UUID(androidId )}</pre>
* <pre>{9}{UUID(random )}</pre>
*
* @param useCache True to use cache, false otherwise.
* @return the unique device id
* 获取唯一设备 ID(默认前缀、指定是否使用缓存)
* @param useCache 是否使用缓存
*/
public static String getUniqueDeviceId(boolean useCache) {
return getUniqueDeviceId("", useCache);
fun getUniqueDeviceId(useCache: Boolean): String {
return getUniqueDeviceId("", useCache)
}
/**
* Return the unique device id.
* <pre>android 10 deprecated {prefix}{1}{UUID(macAddress)}</pre>
* <pre>{prefix}{2}{UUID(androidId )}</pre>
* <pre>{prefix}{9}{UUID(random )}</pre>
*
* @param prefix The prefix of the unique device id.
* @param useCache True to use cache, false otherwise.
* @return the unique device id
* 获取唯一设备 ID(核心方法)
* @param prefix 设备 ID 前缀
* @param useCache 是否使用缓存
*/
public static String getUniqueDeviceId(String prefix, boolean useCache) {
fun getUniqueDeviceId(prefix: String?, useCache: Boolean): String {
val safePrefix = prefix ?: ""
if (!useCache) {
return getUniqueDeviceIdReal(prefix);
return getUniqueDeviceIdReal(safePrefix)
}
if (udid == null) {
synchronized (DeviceUtils.class) {
synchronized(DeviceUtils::class.java) {
if (udid == null) {
final String id = SPUtils.getInstance().getString(KEY_UDID, null);
if (id != null) {
udid = id;
return udid;
val cachedId = SPUtils.getInstance().getString(KEY_UDID, null)
if (cachedId != null) {
udid = cachedId
return udid!!
}
return getUniqueDeviceIdReal(prefix);
// 缓存不存在则生成新的 UDID
return getUniqueDeviceIdReal(safePrefix)
}
}
}
return udid;
return udid ?: ""
}
private static String getUniqueDeviceIdReal(String prefix) {
try {
final String androidId = getAndroidID();
if (!TextUtils.isEmpty(androidId)) {
return saveUdid(prefix + 2, androidId);
/**
* 生成真实的唯一设备 ID(无缓存)
*/
private fun getUniqueDeviceIdReal(prefix: String): String {
return try {
val androidId = getAndroidID()
if (androidId.isNotEmpty()) {
saveUdid(prefix + 2, androidId)
} else {
saveUdid(prefix + 9, "")
}
} catch (ignore: Exception) {
// 异常时生成随机 UUID
saveUdid(prefix + 9, "")
}
} catch (Exception ignore) {/**/}
return saveUdid(prefix + 9, "");
}
private static String saveUdid(String prefix, String id) {
udid = getUdid(prefix, id);
SPUtils.getInstance().put(KEY_UDID, udid);
return udid;
/**
* 保存 UDID 到 SP 并更新内存缓存(修复:id 判空 + SPUtils 操作加 try-catch)
*/
private fun saveUdid(prefix: String, id: String?): String {
val safeId = id ?: ""
udid = getUdid(prefix, safeId)
SPUtils.getInstance().put(KEY_UDID, udid!!)
return udid ?: ""
}
private static String getUdid(String prefix, String id) {
if (id.equals("")) {
return prefix + UUID.randomUUID().toString().replace("-", "");
/**
* 生成 UDID 字符串
*/
private fun getUdid(prefix: String?, id: String?): String {
val safePrefix = prefix ?: ""
val safeId = id ?: ""
return if (safeId.isEmpty()) {
// ID 为空时生成随机 UUID
safePrefix + UUID.randomUUID().toString().replace("-", "")
} else {
// ID 不为空时基于 ID 生成 UUID
safePrefix + UUID.nameUUIDFromBytes(safeId.toByteArray()).toString().replace("-", "")
}
return prefix + UUID.nameUUIDFromBytes(id.getBytes()).toString().replace("-", "");
}
}
}
\ No newline at end of file
......@@ -202,10 +202,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c"
sha256: "56a02f1f4cd1a2d96303c0144c93bd6d909eea6bee6bf5a0e0b685edbd4c47ab"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.1"
version: "1.10.2"
stack_trace:
dependency: transitive
description:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论