combov2: Show UI notifications and toaster messages when BT is not enabled

This commit is contained in:
Carlos Rafael Giani 2023-03-11 18:57:09 +01:00
parent 6d998c8081
commit 377188353a
2 changed files with 35 additions and 10 deletions

View file

@ -133,6 +133,7 @@ open class Notification {
const val EOELOW_PATCH_ALERTS = 79
const val COMBO_PUMP_SUSPENDED = 80
const val COMBO_UNKNOWN_TBR = 81
const val BLUETOOTH_NOT_ENABLED = 82
const val USER_MESSAGE = 1000

View file

@ -66,7 +66,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
@ -310,6 +309,8 @@ class ComboV2Plugin @Inject constructor (
try {
bluetoothInterface!!.setup()
rxBus.send(EventDismissNotification(Notification.BLUETOOTH_NOT_ENABLED))
aapsLogger.debug(LTag.PUMP, "Setting up pump manager")
pumpManager = ComboCtlPumpManager(bluetoothInterface!!, pumpStateStore)
pumpManager!!.setup {
@ -327,6 +328,12 @@ class ComboV2Plugin @Inject constructor (
val paired = pumpManager!!.getPairedPumpAddresses().isNotEmpty()
_pairedStateUIFlow.value = paired
} catch (_: BluetoothNotEnabledException) {
uiInteraction.addNotification(
Notification.BLUETOOTH_NOT_ENABLED,
text = rh.gs(info.nightscout.core.ui.R.string.ble_not_enabled),
level = Notification.INFO
)
// If the user currently has Bluetooth disabled, retry until
// the user turns it on. AAPS will automatically show a dialog
// box which requests the user to enable Bluetooth. Upon
@ -555,6 +562,8 @@ class ComboV2Plugin @Inject constructor (
_bluetoothAddressUIFlow.value = bluetoothAddress.toString()
_serialNumberUIFlow.value = pumpManager!!.getPumpID(bluetoothAddress)
rxBus.send(EventDismissNotification(Notification.BLUETOOTH_NOT_ENABLED))
// Erase any display frame that may be left over from a previous connection.
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
_displayFrameUIFlow.resetReplayCache()
@ -729,6 +738,12 @@ class ComboV2Plugin @Inject constructor (
executePendingDisconnect()
}
}
} catch (_: BluetoothNotEnabledException) {
uiInteraction.addNotification(
Notification.BLUETOOTH_NOT_ENABLED,
text = rh.gs(info.nightscout.core.ui.R.string.ble_not_enabled),
level = Notification.INFO
)
} catch (e: Exception) {
aapsLogger.error(LTag.PUMP, "Connection failure: $e")
ToastUtils.showToastInUiThread(context, rh.gs(R.string.combov2_could_not_connect))
@ -1575,15 +1590,24 @@ class ComboV2Plugin @Inject constructor (
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")
try {
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")
} catch (e: BluetoothNotEnabledException) {
// If Bluetooth is turned off during pairing, show a toaster message.
// Notifications on the AAPS overview fragment are not useful here
// because the pairing activity obscures that fragment. So, instead,
// alert the user by showing the notification via the toaster.
ToastUtils.errorToast(context, info.nightscout.core.ui.R.string.ble_not_enabled)
ComboCtlPumpManager.PairingResult.ExceptionDuringPairing(e)
}
}
if (pairingResult !is ComboCtlPumpManager.PairingResult.Success)