From ae52eb3402b9944cd2dd54c230ce1454b23c4f68 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 27 Nov 2022 22:42:33 +0100 Subject: [PATCH] resolve deprecation warnings --- .../comboctl/android/AndroidBluetoothInterface.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pump/combov2/comboctl/src/androidMain/kotlin/info/nightscout/comboctl/android/AndroidBluetoothInterface.kt b/pump/combov2/comboctl/src/androidMain/kotlin/info/nightscout/comboctl/android/AndroidBluetoothInterface.kt index 92e78df629..3f2ee01dd0 100644 --- a/pump/combov2/comboctl/src/androidMain/kotlin/info/nightscout/comboctl/android/AndroidBluetoothInterface.kt +++ b/pump/combov2/comboctl/src/androidMain/kotlin/info/nightscout/comboctl/android/AndroidBluetoothInterface.kt @@ -88,6 +88,13 @@ class AndroidBluetoothInterface(private val androidContext: Context) : Bluetooth override var deviceFilterCallback: (deviceAddress: BluetoothAddress) -> Boolean = { true } + /** + * Safe version of getParcelableExtra depending on Android version running + */ + fun Intent.safeGetParcelableExtra(name: String?, clazz: Class): T? = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz) + else @Suppress("DEPRECATION") getParcelableExtra(name) + fun setup() { val bluetoothManager = androidContext.getSystemService(Context.BLUETOOTH_SERVICE) as SystemBluetoothManager bluetoothAdapter = bluetoothManager.adapter @@ -334,7 +341,7 @@ class AndroidBluetoothInterface(private val androidContext: Context) : Bluetooth // Sanity check to make sure we can actually get // a Bluetooth device out of the intent. Otherwise, // we have to wait for the next notification. - val androidBtDevice = intent.getParcelableExtra(SystemBluetoothDevice.EXTRA_DEVICE) + val androidBtDevice = intent.safeGetParcelableExtra(SystemBluetoothDevice.EXTRA_DEVICE, SystemBluetoothDevice::class.java) if (androidBtDevice == null) { logger(LogLevel.DEBUG) { "Ignoring ACL_CONNECTED intent that has no Bluetooth device" } return @@ -430,7 +437,7 @@ class AndroidBluetoothInterface(private val androidContext: Context) : Bluetooth // If so, the caller may have to update states like // the pump state store accordingly. - val androidBtDevice = intent.getParcelableExtra(SystemBluetoothDevice.EXTRA_DEVICE) + val androidBtDevice = intent.safeGetParcelableExtra(SystemBluetoothDevice.EXTRA_DEVICE, SystemBluetoothDevice::class.java) if (androidBtDevice == null) { logger(LogLevel.DEBUG) { "Ignoring BOND_STATE_CHANGED intent that has no Bluetooth device" } return @@ -486,7 +493,7 @@ class AndroidBluetoothInterface(private val androidContext: Context) : Bluetooth } private fun onPairingRequest(intent: Intent, btPairingPin: String) { - val androidBtDevice = intent.getParcelableExtra(SystemBluetoothDevice.EXTRA_DEVICE) + val androidBtDevice = intent.safeGetParcelableExtra(SystemBluetoothDevice.EXTRA_DEVICE, SystemBluetoothDevice::class.java) if (androidBtDevice == null) { logger(LogLevel.DEBUG) { "Ignoring PAIRING_REQUEST intent that has no Bluetooth device" } return