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