passwords: pre-flight bluetooth check for cable passkeys (#783)
- Commit
ac8ca60d364aa5404e4f8403af8673b5a3907993- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-13 19:41 UTC (1 month ago)
- Parents
47ee143
Diff
passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableActivity.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableActivity.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableActivity.ktindex c5df35a149..28e721100d 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableActivity.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableActivity.kt@@ -2,6 +2,7 @@ package com.vayunmathur.passwords.platform.cable import com.vayunmathur.library.ui.R as UiR import android.Manifest+import android.bluetooth.BluetoothAdapter import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle@@ -11,6 +12,7 @@ import androidx.biometric.BiometricManager import androidx.biometric.BiometricPrompt import androidx.core.content.ContextCompat import androidx.fragment.app.FragmentActivity+import com.vayunmathur.library.util.AppMessages import com.vayunmathur.passwords.R /**@@ -28,7 +30,18 @@ class CableActivity : FragmentActivity() { private val permissionLauncher = registerForActivityResult( ActivityResultContracts.RequestMultiplePermissions()- ) { startSessionAndFinish() }+ ) { ensureBluetoothThenStart() }++ private val enableBluetoothLauncher = registerForActivityResult(+ ActivityResultContracts.StartActivityForResult()+ ) {+ if (CableBluetooth.isEnabled(this)) {+ startSessionAndFinish()+ } else {+ AppMessages.show(getString(R.string.cable_bluetooth_disabled_message))+ finish()+ }+ } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)@@ -89,10 +102,29 @@ class CableActivity : FragmentActivity() { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED } - if (needed.isEmpty()) startSessionAndFinish()+ if (needed.isEmpty()) ensureBluetoothThenStart() else permissionLauncher.launch(needed.toTypedArray()) } + /**+ * Pre-flight check before the caBLE flow: prompt to enable Bluetooth (using the existing+ * permission state — BLUETOOTH_CONNECT is granted by now) so a disabled radio shows a+ * dialog/message instead of failing silently in the service notification.+ */+ private fun ensureBluetoothThenStart() {+ if (!CableBluetooth.isSupported(this)) {+ Log.error(TAG, "Bluetooth not supported; cannot run caBLE session")+ AppMessages.show(getString(R.string.cable_bluetooth_unsupported_message))+ finish()+ return+ }+ if (!CableBluetooth.isEnabled(this)) {+ enableBluetoothLauncher.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))+ return+ }+ startSessionAndFinish()+ }+ private fun startSessionAndFinish() { val uri = fidoUri if (uri != null) {
passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableBluetooth.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableBluetooth.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableBluetooth.ktnew file mode 100644index 0000000000..ba8135351c--- /dev/null+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/cable/CableBluetooth.kt@@ -0,0 +1,36 @@+package com.vayunmathur.passwords.platform.cable++import android.annotation.SuppressLint+import android.bluetooth.BluetoothAdapter+import android.bluetooth.BluetoothManager+import android.content.Context++/**+ * Pre-flight Bluetooth state for the caBLE flow.+ *+ * [CableActivity] already owns the BLUETOOTH_ADVERTISE / BLUETOOTH_CONNECT / POST_NOTIFICATIONS+ * runtime-permission state; these helpers only read the adapter so the activity can prompt to+ * enable Bluetooth *before* starting [CableService] instead of failing silently in its+ * notification.+ */+object CableBluetooth {++ fun adapter(context: Context): BluetoothAdapter? =+ context.getSystemService(BluetoothManager::class.java)?.adapter++ /** False when the device has no Bluetooth adapter at all. */+ fun isSupported(context: Context): Boolean = adapter(context) != null++ /**+ * True when Bluetooth is on. Returns true when the state cannot be read (e.g. the+ * BLUETOOTH_CONNECT runtime permission was denied) so callers fall through to the existing+ * session behavior instead of blocking on an unknown state.+ */+ @SuppressLint("MissingPermission")+ fun isEnabled(context: Context): Boolean =+ try {+ adapter(context)?.isEnabled == true+ } catch (expected: SecurityException) {+ true+ }+}
passwords/src/main/res/values/strings.xml
diff --git a/passwords/src/main/res/values/strings.xml b/passwords/src/main/res/values/strings.xmlindex a83b4e8b83..34202bd9cf 100644--- a/passwords/src/main/res/values/strings.xml+++ b/passwords/src/main/res/values/strings.xml@@ -122,6 +122,10 @@ <string name="cable_status_failed">Request failed</string> <!-- Notification text when the other device cancelled the request --> <string name="cable_status_cancelled">Cancelled by the other device</string>+ <!-- Message when Bluetooth is off and the user dismisses the enable-Bluetooth prompt instead of turning it on -->+ <string name="cable_bluetooth_disabled_message">Bluetooth is off. Turn it on to continue with this passkey request.</string>+ <!-- Message when the device has no Bluetooth adapter, so cross-device passkeys cannot run -->+ <string name="cable_bluetooth_unsupported_message">This device does not support Bluetooth, so cross-device sign-in is unavailable.</string> <string name="name_required">Name cannot be empty</string> <!-- Section header for the two-way KeePass file sync settings. KDBX = KeePass database file extension, keep as-is -->