From e60e9a21ccdd7b4589c4d3993dc711567f4065cc Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Sun, 25 Dec 2022 16:13:28 +0100 Subject: [PATCH] combov2: Re-run Bluetooth connection attempts if permissions are missing Signed-off-by: Carlos Rafael Giani --- .../nightscout/pump/combov2/ComboV2Plugin.kt | 100 ++++++++++++------ .../info/nightscout/pump/combov2/Utility.kt | 43 +++++++- 2 files changed, 107 insertions(+), 36 deletions(-) diff --git a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt index 5a5b3d3afc..6b38b859a9 100644 --- a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt +++ b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt @@ -25,6 +25,8 @@ import info.nightscout.comboctl.parser.BatteryState import info.nightscout.comboctl.parser.ReservoirState import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.core.ui.toast.ToastUtils +import info.nightscout.interfaces.AndroidPermission +import info.nightscout.interfaces.Config import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraints import info.nightscout.interfaces.notifications.Notification @@ -109,7 +111,9 @@ class ComboV2Plugin @Inject constructor ( private val sp: SP, private val pumpSync: PumpSync, private val dateUtil: DateUtil, - private val uiInteraction: UiInteraction + private val uiInteraction: UiInteraction, + private val androidPermission: AndroidPermission, + private val config: Config ) : PumpPluginBase( PluginDescription() @@ -286,32 +290,46 @@ class ComboV2Plugin @Inject constructor ( aapsLogger.debug(LTag.PUMP, "Creating bluetooth interface") bluetoothInterface = AndroidBluetoothInterface(context) - aapsLogger.debug(LTag.PUMP, "Setting up bluetooth interface") - bluetoothInterface!!.setup() + // Continue initialization in a separate coroutine. This allows us to call + // runWithPermissionCheck(), which will keep trying to run the code block + // until either the necessary Bluetooth permissios are granted, or the + // coroutine is cancelled (see onStop() below). + pumpCoroutineScope.launch { + runWithPermissionCheck( + context, config, aapsLogger, androidPermission, + permissionsToCheckFor = listOf("android.permission.BLUETOOTH_CONNECT") + ) { + aapsLogger.debug(LTag.PUMP, "Setting up bluetooth interface") + bluetoothInterface!!.setup() - aapsLogger.debug(LTag.PUMP, "Setting up pump manager") - pumpManager = ComboCtlPumpManager(bluetoothInterface!!, pumpStateStore) - pumpManager!!.setup { - _pairedStateUIFlow.value = false - unpairing = false + aapsLogger.debug(LTag.PUMP, "Setting up pump manager") + pumpManager = ComboCtlPumpManager(bluetoothInterface!!, pumpStateStore) + pumpManager!!.setup { + _pairedStateUIFlow.value = false + unpairing = false + } + + // UI flows that must have defined values right + // at start are initialized here. + + // The paired state UI flow is special in that it is also + // used as the backing store for the isPaired() function, + // so setting up that UI state flow equals updating that + // paired state. + val paired = pumpManager!!.getPairedPumpAddresses().isNotEmpty() + _pairedStateUIFlow.value = paired + + setDriverState(DriverState.Disconnected) + + // NOTE: EventInitializationChanged is sent in getPumpStatus() . + } } - - // UI flows that must have defined values right - // at start are initialized here. - - // The paired state UI flow is special in that it is also - // used as the backing store for the isPaired() function, - // so setting up that UI state flow equals updating that - // paired state. - val paired = pumpManager!!.getPairedPumpAddresses().isNotEmpty() - _pairedStateUIFlow.value = paired - - setDriverState(DriverState.Disconnected) - - // NOTE: EventInitializationChanged is sent in getPumpStatus() . } override fun onStop() { + // Cancel any ongoing background coroutines. This includes an ongoing + // unfinished initialization that still waits for the user to grant + // Bluetooth permissions. pumpCoroutineScope.cancel() runBlocking { @@ -580,10 +598,15 @@ class ComboV2Plugin @Inject constructor ( var forciblyDisconnectDueToError = false try { - // Set maxNumAttempts to null to turn off the connection attempt limit inside the connect() call. - // The AAPS queue thread will anyway cause the connectionSetupJob to be canceled when its - // connection timeout expires, so the Pump class' own connection attempt limiter is redundant. - pump?.connect(maxNumAttempts = null) + runWithPermissionCheck( + context, config, aapsLogger, androidPermission, + permissionsToCheckFor = listOf("android.permission.BLUETOOTH_CONNECT") + ) { + // Set maxNumAttempts to null to turn off the connection attempt limit inside the connect() call. + // The AAPS queue thread will anyway cause the connectionSetupJob to be canceled when its + // connection timeout expires, so the Pump class' own connection attempt limiter is redundant. + pump?.connect(maxNumAttempts = null) + } // No need to set the driver state here, since the pump's stateFlow will announce that. @@ -1486,15 +1509,22 @@ class ComboV2Plugin @Inject constructor ( pairingJob = pumpCoroutineScope.async { try { - val pairingResult = pumpManager?.pairWithNewPump(discoveryDuration) { newPumpAddress, previousAttemptFailed -> - aapsLogger.info( - LTag.PUMP, - "New pairing PIN request from Combo pump with Bluetooth " + - "address $newPumpAddress (previous attempt failed: $previousAttemptFailed)" - ) - _previousPairingAttemptFailedFlow.value = previousAttemptFailed - newPINChannel.receive() - } ?: throw IllegalStateException("Attempting to access uninitialized pump manager") + // Do the pairing attempt within runWithPermissionCheck() + // since pairing requires Bluetooth permissions. + val pairingResult = runWithPermissionCheck( + context, config, aapsLogger, androidPermission, + permissionsToCheckFor = listOf("android.permission.BLUETOOTH_CONNECT") + ) { + pumpManager?.pairWithNewPump(discoveryDuration) { newPumpAddress, previousAttemptFailed -> + aapsLogger.info( + LTag.PUMP, + "New pairing PIN request from Combo pump with Bluetooth " + + "address $newPumpAddress (previous attempt failed: $previousAttemptFailed)" + ) + _previousPairingAttemptFailedFlow.value = previousAttemptFailed + newPINChannel.receive() + } ?: throw IllegalStateException("Attempting to access uninitialized pump manager") + } if (pairingResult !is ComboCtlPumpManager.PairingResult.Success) return@async diff --git a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/Utility.kt b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/Utility.kt index 971d88e900..03576e4401 100644 --- a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/Utility.kt +++ b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/Utility.kt @@ -1,8 +1,16 @@ package info.nightscout.pump.combov2 +import android.content.Context +import android.os.Build +import info.nightscout.comboctl.android.AndroidBluetoothPermissionException import info.nightscout.comboctl.main.BasalProfile import info.nightscout.comboctl.main.NUM_COMBO_BASAL_PROFILE_FACTORS +import info.nightscout.interfaces.AndroidPermission +import info.nightscout.interfaces.Config import info.nightscout.interfaces.profile.Profile as AAPSProfile +import info.nightscout.rx.logging.AAPSLogger +import info.nightscout.rx.logging.LTag +import kotlinx.coroutines.delay // Utility extension functions for clearer conversion between // ComboCtl units and AAPS units. ComboCtl uses integer-encoded @@ -22,4 +30,37 @@ fun AAPSProfile.toComboCtlBasalProfile(): BasalProfile { (this.getBasalTimeFromMidnight(hour * 60 * 60) * 1000.0).toInt() } return BasalProfile(factors) -} \ No newline at end of file +} + +suspend fun runWithPermissionCheck( + context: Context, + config: Config, + aapsLogger: AAPSLogger, + androidPermission: AndroidPermission, + permissionsToCheckFor: Collection, + block: suspend () -> T +): T { + var permissions = permissionsToCheckFor + while (true) { + try { + if (config.PUMPDRIVERS && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + val notAllPermissionsGranted = permissions.fold(initial = false) { currentResult, permission -> + return@fold if (androidPermission.permissionNotGranted(context, permission)) { + aapsLogger.debug(LTag.PUMP, "permission $permission was not granted by the user") + true + } else + currentResult + } + + if (notAllPermissionsGranted) { + delay(1000) // Wait a little bit before retrying to avoid 100% CPU usage + continue + } + } + + return block.invoke() + } catch (e: AndroidBluetoothPermissionException) { + permissions = permissionsToCheckFor union e.missingPermissions + } + } +}