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
final override lateinit var context: Context
override val display: ByteArray get() = Build.DISPLAY.toByteArray()
override val product: ByteArray get() = Build.PRODUCT.toByteArray()
override val device: ByteArray get() = Build.DEVICE.toByteArray()
override val board: ByteArray get() = Build.BOARD.toByteArray()
override val brand: ByteArray get() = Build.BRAND.toByteArray()
override val model: ByteArray get() = Build.MODEL.toByteArray()
override val bootloader: ByteArray get() = Build.BOOTLOADER.toByteArray()
override val display: ByteArray get() = Build.DISPLAY.orEmpty().toByteArray()
override val product: ByteArray get() = Build.PRODUCT.orEmpty().toByteArray()
override val device: ByteArray get() = Build.DEVICE.orEmpty().toByteArray()
override val board: ByteArray get() = Build.BOARD.orEmpty().toByteArray()
override val brand: ByteArray get() = Build.BRAND.orEmpty().toByteArray()
override val model: ByteArray get() = Build.MODEL.orEmpty().toByteArray()
override val bootloader: ByteArray get() = Build.BOOTLOADER.orEmpty().toByteArray()
override val baseBand: ByteArray
@SuppressLint("PrivateApi")
@ -72,17 +72,17 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
Class.forName("android.os.SystemProperties").let { clazz ->
clazz.getMethod("get", String::class.java, String::class.java)
.invoke(clazz.newInstance(), "gsm.version.baseband", "no message")
?.toString() ?: ""
?.toString().orEmpty()
}
}.getOrElse { "" }.toByteArray()
override val fingerprint: ByteArray get() = Build.FINGERPRINT.toByteArray()
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() }
override val bootId: ByteArray
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()
override val version: DeviceInfo.Version get() = Version
@ -91,7 +91,7 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
val telephonyManager =
context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (telephonyManager.simState == 5) {
telephonyManager.simOperatorName.toByteArray()
telephonyManager.simOperatorName.orEmpty().toByteArray()
} else byteArrayOf()
}.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 wifiBSSID: ByteArray?
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()
override val wifiSSID: ByteArray?
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()
@OptIn(MiraiInternalAPI::class)
override val imsiMd5: ByteArray
@SuppressLint("HardwareIds")
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()
override val imei: String
@ -122,7 +125,7 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
} else {
@Suppress("DEPRECATION")
(context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).deviceId
}
}.orEmpty()
}.getOrElse { "" }
override val androidId: ByteArray get() = Build.ID.toByteArray()
@ -130,9 +133,9 @@ actual open class SystemDeviceInfo actual constructor() : DeviceInfo() {
@Serializable
actual object Version : DeviceInfo.Version {
override val incremental: ByteArray get() = Build.VERSION.INCREMENTAL.toByteArray()
override val release: ByteArray get() = Build.VERSION.RELEASE.toByteArray()
override val codename: ByteArray get() = Build.VERSION.CODENAME.toByteArray()
override val incremental: ByteArray get() = Build.VERSION.INCREMENTAL.orEmpty().toByteArray()
override val release: ByteArray get() = Build.VERSION.RELEASE.orEmpty().toByteArray()
override val codename: ByteArray get() = Build.VERSION.CODENAME.orEmpty().toByteArray()
override val sdk: Int get() = Build.VERSION.SDK_INT
}
}