resolve getParcelableExtra issue

This commit is contained in:
Milos Kozak 2022-09-21 11:30:40 +02:00
parent 6ab3760dce
commit 0a2c5fec87
5 changed files with 18 additions and 6 deletions

View file

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import dagger.android.DaggerBroadcastReceiver
import info.nightscout.androidaps.events.EventBTChange
import info.nightscout.androidaps.extensions.safeGetParcelableExtra
import info.nightscout.androidaps.plugins.bus.RxBus
import javax.inject.Inject
@ -14,7 +15,7 @@ class BTReceiver : DaggerBroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java) ?: return
val device = intent.safeGetParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java) ?: return
when (intent.action) {
BluetoothDevice.ACTION_ACL_CONNECTED ->

View file

@ -0,0 +1,7 @@
package info.nightscout.androidaps.extensions
import android.content.Intent
fun <T> Intent.safeGetParcelableExtra(name: String?, clazz: Class<T>): T? =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz)
else @Suppress("DEPRECATION") getParcelableExtra(name)

View file

@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import androidx.fragment.app.FragmentActivity
import info.nightscout.androidaps.extensions.safeGetParcelableExtra
import info.nightscout.androidaps.plugins.general.maintenance.activities.PrefImportListActivity
class PrefsFileContract : ActivityResultContract<Void?, PrefsFile?>() {
@ -15,7 +16,7 @@ class PrefsFileContract : ActivityResultContract<Void?, PrefsFile?>() {
override fun parseResult(resultCode: Int, intent: Intent?): PrefsFile? {
return when (resultCode) {
FragmentActivity.RESULT_OK -> intent?.getParcelableExtra(OUTPUT_PARAM, PrefsFile::class.java)
FragmentActivity.RESULT_OK -> intent?.safeGetParcelableExtra(OUTPUT_PARAM, PrefsFile::class.java)
else -> null
}
}

View file

@ -39,6 +39,7 @@ import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck;
import info.nightscout.androidaps.plugins.pump.insight.connection_service.InsightConnectionService;
import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState;
import info.nightscout.androidaps.plugins.pump.insight.utils.ExceptionTranslator;
import info.nightscout.androidaps.extensions.IntentExtensionKt;
public class InsightPairingActivity extends NoSplashAppCompatActivity implements InsightConnectionService.StateCallback, View.OnClickListener, InsightConnectionService.ExceptionCallback {
@ -234,7 +235,8 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().startDiscovery();
else if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BluetoothDevice bluetoothDevice =
IntentExtensionKt.safeGetParcelableExtra(intent, BluetoothDevice.EXTRA_DEVICE, BluetoothDevice.class);
deviceAdapter.addDevice(bluetoothDevice);
}
}

View file

@ -6,6 +6,7 @@ import android.content.Intent
import androidx.annotation.StringRes
import com.google.gson.Gson
import dagger.android.DaggerBroadcastReceiver
import info.nightscout.androidaps.extensions.safeGetParcelableExtra
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.pump.common.events.EventPumpConnectionParametersChanged
@ -17,8 +18,8 @@ import javax.inject.Inject
class BondStateReceiver(
@StringRes var deviceAddress: Int,
@StringRes var bondedFlag: Int,
var targetDevice: String,
var targetState: Int
private var targetDevice: String,
private var targetState: Int
) : DaggerBroadcastReceiver() {
@Inject lateinit var sp: SP
@ -33,7 +34,7 @@ class BondStateReceiver(
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
val action = intent.action
val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
val device = intent.safeGetParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
aapsLogger.info(LTag.PUMPBTCOMM, "in onReceive: INTENT" + gson.toJson(intent))
if (device == null) {
aapsLogger.error(LTag.PUMPBTCOMM, "onReceive. Device is null. Exiting.")