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 <crg7475@mailbox.org>
This commit is contained in:
Carlos Rafael Giani 2023-01-02 18:07:53 +01:00
parent ddba45622c
commit 19871d8f52
3 changed files with 79 additions and 16 deletions

View file

@ -35,6 +35,27 @@ class ComboV2PairingActivity : DaggerAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) 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 // Install an activity result caller for when the user presses
// "deny" or "reject" in the dialog that pops up when Android // "deny" or "reject" in the dialog that pops up when Android
// asks for permission to enable device discovery. In such a // asks for permission to enable device discovery. In such a
@ -54,9 +75,6 @@ class ComboV2PairingActivity : DaggerAppCompatActivity() {
startPairingActivityLauncher.launch(intent) startPairingActivityLauncher.launch(intent)
} }
val binding: Combov2PairingActivityBinding = DataBindingUtil.setContentView(
this, R.layout.combov2_pairing_activity)
binding.combov2PairingFinishedOk.setOnClickListener { binding.combov2PairingFinishedOk.setOnClickListener {
finish() finish()
} }
@ -263,20 +281,26 @@ class ComboV2PairingActivity : DaggerAppCompatActivity() {
} }
override fun onDestroy() { override fun onDestroy() {
// Reset the pairing progress reported to allow for future pairing attempts. // In the NotInitialized state, getPairingProgressFlow() crashes because there
// Do this only after pairing was finished or aborted. onDestroy() can be // is no PumpManager present. But in that state, the pairing progress flow needs
// called in the middle of a pairing process, and we do not want to reset // no reset because no pairing can happen in that state anyway.
// the progress reporter mid-pairing. if (combov2Plugin.driverStateUIFlow.value != ComboV2Plugin.DriverState.NotInitialized) {
when (combov2Plugin.getPairingProgressFlow().value.stage) { // Reset the pairing progress reported to allow for future pairing attempts.
BasicProgressStage.Finished, // Do this only after pairing was finished or aborted. onDestroy() can be
is BasicProgressStage.Aborted -> { // called in the middle of a pairing process, and we do not want to reset
aapsLogger.debug( // the progress reporter mid-pairing.
LTag.PUMP, when (combov2Plugin.getPairingProgressFlow().value.stage) {
"Resetting pairing progress reporter after pairing was finished/aborted" BasicProgressStage.Finished,
) is BasicProgressStage.Aborted -> {
combov2Plugin.resetPairingProgress() aapsLogger.debug(
LTag.PUMP,
"Resetting pairing progress reporter after pairing was finished/aborted"
)
combov2Plugin.resetPairingProgress()
}
else -> Unit
} }
else -> Unit
} }
super.onDestroy() super.onDestroy()

View file

@ -41,6 +41,43 @@
</androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
<ScrollView
android:id="@+id/combov2_pairing_section_cannot_pair_driver_not_initialized"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:singleLine="false"
android:text="@string/combov2_cannot_pair_driver_not_initialized_explanation"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/combov2_cannot_pair_go_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/combov2_go_back" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<ScrollView <ScrollView
android:id="@+id/combov2_pairing_section_initial" android:id="@+id/combov2_pairing_section_initial"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -137,4 +137,6 @@ buttons at the same time to cancel pairing)\n
<string name="combov2_dst_ended">Daylight savings time (DST) ended</string> <string name="combov2_dst_ended">Daylight savings time (DST) ended</string>
<string name="combov2_cannot_connect_pump_error_observed">Cannot connect to pump because the pump reported an error. User must handle the error and then either wait 5 minutes or press the Refresh button in the driver tab.</string> <string name="combov2_cannot_connect_pump_error_observed">Cannot connect to pump because the pump reported an error. User must handle the error and then either wait 5 minutes or press the Refresh button in the driver tab.</string>
<string name="combov2_refresh_pump_status_after_error">Refreshing pump status after the pump reported an error</string> <string name="combov2_refresh_pump_status_after_error">Refreshing pump status after the pump reported an error</string>
<string name="combov2_go_back">Go back</string>
<string name="combov2_cannot_pair_driver_not_initialized_explanation">Cannot perform pairing because the driver is not initialized. This typically happens because the necessary Bluetooth permissions have not been granted. Go back, grant the Bluetooth permissions, then try again to pair.</string>
</resources> </resources>