Avoid NPE from Android

This commit is contained in:
Him188 2020-04-04 16:10:49 +08:00
parent eb482a4303
commit 9f7088d4a4

View File

@ -57,13 +57,13 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
@Transient @Transient
final override lateinit var context: Context final override lateinit var context: Context
override val display: ByteArray get() = Build.DISPLAY.toByteArray() override val display: ByteArray get() = Build.DISPLAY.orEmpty().toByteArray()
override val product: ByteArray get() = Build.PRODUCT.toByteArray() override val product: ByteArray get() = Build.PRODUCT.orEmpty().toByteArray()
override val device: ByteArray get() = Build.DEVICE.toByteArray() override val device: ByteArray get() = Build.DEVICE.orEmpty().toByteArray()
override val board: ByteArray get() = Build.BOARD.toByteArray() override val board: ByteArray get() = Build.BOARD.orEmpty().toByteArray()
override val brand: ByteArray get() = Build.BRAND.toByteArray() override val brand: ByteArray get() = Build.BRAND.orEmpty().toByteArray()
override val model: ByteArray get() = Build.MODEL.toByteArray() override val model: ByteArray get() = Build.MODEL.orEmpty().toByteArray()
override val bootloader: ByteArray get() = Build.BOOTLOADER.toByteArray() override val bootloader: ByteArray get() = Build.BOOTLOADER.orEmpty().toByteArray()
override val baseBand: ByteArray override val baseBand: ByteArray
@SuppressLint("PrivateApi") @SuppressLint("PrivateApi")
@ -72,17 +72,17 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
Class.forName("android.os.SystemProperties").let { clazz -> Class.forName("android.os.SystemProperties").let { clazz ->
clazz.getMethod("get", String::class.java, String::class.java) clazz.getMethod("get", String::class.java, String::class.java)
.invoke(clazz.newInstance(), "gsm.version.baseband", "no message") .invoke(clazz.newInstance(), "gsm.version.baseband", "no message")
?.toString() ?: "" ?.toString().orEmpty()
} }
}.getOrElse { "" }.toByteArray() }.getOrElse { "" }.toByteArray()
override val fingerprint: ByteArray get() = Build.FINGERPRINT.toByteArray() override val fingerprint: ByteArray get() = Build.FINGERPRINT.toByteArray()
override val procVersion: ByteArray override val procVersion: ByteArray
get() = kotlin.runCatching { File("/proc/version").useLines { it.firstOrNull() ?: "" }.toByteArray() } get() = kotlin.runCatching { File("/proc/version").useLines { it.firstOrNull().orEmpty() }.toByteArray() }
.getOrElse { byteArrayOf() } .getOrElse { byteArrayOf() }
override val bootId: ByteArray override val bootId: ByteArray
get() = kotlin.runCatching { get() = kotlin.runCatching {
File("/proc/sys/kernel/random/boot_id").useLines { it.firstOrNull() ?: "" }.toByteArray() File("/proc/sys/kernel/random/boot_id").useLines { it.firstOrNull().orEmpty() }.toByteArray()
}.getOrEmpty() }.getOrEmpty()
override val version: DeviceInfo.Version get() = Version override val version: DeviceInfo.Version get() = Version
@ -91,7 +91,7 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
val telephonyManager = val telephonyManager =
context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (telephonyManager.simState == 5) { if (telephonyManager.simState == 5) {
telephonyManager.simOperatorName.toByteArray() telephonyManager.simOperatorName.orEmpty().toByteArray()
} else byteArrayOf() } else byteArrayOf()
}.getOrEmpty() }.getOrEmpty()
@ -99,19 +99,22 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
override val macAddress: ByteArray get() = "02:00:00:00:00:00".toByteArray() override val macAddress: ByteArray get() = "02:00:00:00:00:00".toByteArray()
override val wifiBSSID: ByteArray? override val wifiBSSID: ByteArray?
get() = kotlin.runCatching { get() = kotlin.runCatching {
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.bssid.toByteArray() (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.bssid.orEmpty()
.toByteArray()
}.getOrEmpty() }.getOrEmpty()
override val wifiSSID: ByteArray? override val wifiSSID: ByteArray?
get() = kotlin.runCatching { get() = kotlin.runCatching {
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.ssid.toByteArray() (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.ssid.orEmpty()
.toByteArray()
}.getOrEmpty() }.getOrEmpty()
@OptIn(MiraiInternalAPI::class) @OptIn(MiraiInternalAPI::class)
override val imsiMd5: ByteArray override val imsiMd5: ByteArray
@SuppressLint("HardwareIds") @SuppressLint("HardwareIds")
get() = kotlin.runCatching { get() = kotlin.runCatching {
(context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).subscriberId.toByteArray() (context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).subscriberId.orEmpty()
.toByteArray()
}.getOrEmpty().md5() }.getOrEmpty().md5()
override val imei: String override val imei: String
@ -122,7 +125,7 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
} else { } else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
(context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).deviceId (context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).deviceId
} }.orEmpty()
}.getOrElse { "" } }.getOrElse { "" }
override val androidId: ByteArray get() = Build.ID.toByteArray() override val androidId: ByteArray get() = Build.ID.toByteArray()
@ -130,9 +133,9 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
@Serializable @Serializable
actual object Version : DeviceInfo.Version { actual object Version : DeviceInfo.Version {
override val incremental: ByteArray get() = Build.VERSION.INCREMENTAL.toByteArray() override val incremental: ByteArray get() = Build.VERSION.INCREMENTAL.orEmpty().toByteArray()
override val release: ByteArray get() = Build.VERSION.RELEASE.toByteArray() override val release: ByteArray get() = Build.VERSION.RELEASE.orEmpty().toByteArray()
override val codename: ByteArray get() = Build.VERSION.CODENAME.toByteArray() override val codename: ByteArray get() = Build.VERSION.CODENAME.orEmpty().toByteArray()
override val sdk: Int get() = Build.VERSION.SDK_INT override val sdk: Int get() = Build.VERSION.SDK_INT
} }
} }