From 19871d8f5259186392fb4bdbad4e5e57939674ae Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Mon, 2 Jan 2023 18:07:53 +0100 Subject: [PATCH] combov2: Disable pairing UI and show message when driver is not initialized Without this check, AAPS crashes, because the code tries to access the pairing flow, which depends on the pump manager. But that manager does not exist until the user grants Bluetooth permissions. Signed-off-by: Carlos Rafael Giani --- .../activities/ComboV2PairingActivity.kt | 56 +++++++++++++------ .../res/layout/combov2_pairing_activity.xml | 37 ++++++++++++ pump/combov2/src/main/res/values/strings.xml | 2 + 3 files changed, 79 insertions(+), 16 deletions(-) diff --git a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt index 61c72a3c95..44daeb1f0f 100644 --- a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt +++ b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/activities/ComboV2PairingActivity.kt @@ -35,6 +35,27 @@ class ComboV2PairingActivity : DaggerAppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + val binding: Combov2PairingActivityBinding = DataBindingUtil.setContentView( + this, R.layout.combov2_pairing_activity) + + // In the NotInitialized state, the PumpManager is unavailable because it cannot + // function without Bluetooth permissions. Several of ComboV2Plugin's functions + // such as getPairingProgressFlow() depend on PumpManager though. To prevent UI + // controls from becoming active without having a PumpManager, show instead a + // view on the activity that explains why pairing is currently not possible. + if (combov2Plugin.driverStateUIFlow.value == ComboV2Plugin.DriverState.NotInitialized) { + aapsLogger.info(LTag.PUMP, "Cannot pair right now; disabling pairing UI controls, showing message instead") + + binding.combov2PairingSectionInitial.visibility = View.GONE + binding.combov2PairingSectionCannotPairDriverNotInitialized.visibility = View.VISIBLE + + binding.combov2CannotPairGoBack.setOnClickListener { + finish() + } + + return + } + // Install an activity result caller for when the user presses // "deny" or "reject" in the dialog that pops up when Android // asks for permission to enable device discovery. In such a @@ -54,9 +75,6 @@ class ComboV2PairingActivity : DaggerAppCompatActivity() { startPairingActivityLauncher.launch(intent) } - val binding: Combov2PairingActivityBinding = DataBindingUtil.setContentView( - this, R.layout.combov2_pairing_activity) - binding.combov2PairingFinishedOk.setOnClickListener { finish() } @@ -263,20 +281,26 @@ class ComboV2PairingActivity : DaggerAppCompatActivity() { } override fun onDestroy() { - // Reset the pairing progress reported to allow for future pairing attempts. - // Do this only after pairing was finished or aborted. onDestroy() can be - // called in the middle of a pairing process, and we do not want to reset - // the progress reporter mid-pairing. - when (combov2Plugin.getPairingProgressFlow().value.stage) { - BasicProgressStage.Finished, - is BasicProgressStage.Aborted -> { - aapsLogger.debug( - LTag.PUMP, - "Resetting pairing progress reporter after pairing was finished/aborted" - ) - combov2Plugin.resetPairingProgress() + // In the NotInitialized state, getPairingProgressFlow() crashes because there + // is no PumpManager present. But in that state, the pairing progress flow needs + // no reset because no pairing can happen in that state anyway. + if (combov2Plugin.driverStateUIFlow.value != ComboV2Plugin.DriverState.NotInitialized) { + // Reset the pairing progress reported to allow for future pairing attempts. + // Do this only after pairing was finished or aborted. onDestroy() can be + // called in the middle of a pairing process, and we do not want to reset + // the progress reporter mid-pairing. + when (combov2Plugin.getPairingProgressFlow().value.stage) { + BasicProgressStage.Finished, + is BasicProgressStage.Aborted -> { + aapsLogger.debug( + LTag.PUMP, + "Resetting pairing progress reporter after pairing was finished/aborted" + ) + combov2Plugin.resetPairingProgress() + } + + else -> Unit } - else -> Unit } super.onDestroy() diff --git a/pump/combov2/src/main/res/layout/combov2_pairing_activity.xml b/pump/combov2/src/main/res/layout/combov2_pairing_activity.xml index 960e9ccb5d..f3c7664f6f 100644 --- a/pump/combov2/src/main/res/layout/combov2_pairing_activity.xml +++ b/pump/combov2/src/main/res/layout/combov2_pairing_activity.xml @@ -41,6 +41,43 @@ + + + + + + + + +