mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-11-29 08:27:15 +00:00
fix: request Wi-Fi Aware permissions when enabling it from Debug Settings
Enabling Wi-Fi Aware from the Debug Settings sheet never requested the permissions it needs. The permission flow is reachable only through PermissionManager.getRequiredPermissions(), which gates the Wi-Fi Aware entries behind shouldRequireWifiAwarePermission() — and that returns false unless the debug toggle is already on. Since the toggle defaults to off, onboarding never asks, and flipping it later starts WifiAwareController directly, which only checks the permission and bails. The result was a silent dead end: Wi-Fi Aware could never start, and the controller logged "Missing NEARBY_WIFI_DEVICES permission" on a 5s retry loop indefinitely. Reproduced on a Pixel 9a (Android 17) and a Samsung SM-A366E (Android 16); both needed adb grants to get the transport running at all. The toggle and the Start chip now request the permissions first and only enable the transport once NEARBY_WIFI_DEVICES is granted. ACCESS_LOCAL_NETWORK is treated as best-effort since it does not exist below API 37 — confirmed by `pm grant` rejecting it as an unknown permission on the Android 16 device. The list comes from PermissionManager.wifiAwarePermissions() so the API 37 gate has a single definition. Also corrects the permission-group comments now that both levels are verified on Android 17: grants are tracked independently (granting NEARBY_WIFI_DEVICES alone leaves ACCESS_LOCAL_NETWORK denied), but the two share the NEARBY_DEVICES group so requesting them together produces a single "Nearby devices" prompt. Verified on device: after a clean uninstall/reinstall, toggling Wi-Fi Aware produced one prompt and left both permissions granted with the USER_SET flag. Addresses the second automated review finding on #750. Verified: compileDebugKotlin, testDebugUnitTest (124 tests, 0 failures). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e35fac3c67
commit
ee1ee3ad2e
3 files changed
+50
-11
No files matched your search
@@ -31,8 +31,9 @@
|
||||
Android 17 (API 37) makes local network protection mandatory for apps targeting it.
|
||||
The Wi‑Fi Aware transport reaches peers over link-local IPv6 TCP sockets
|
||||
(WifiAwareMeshService.connectAwareClientSocket), so declare this defensively.
|
||||
It belongs to the NEARBY_DEVICES group, but is granted independently of
|
||||
NEARBY_WIFI_DEVICES, so it must be requested explicitly (see PermissionManager).
|
||||
It is granted independently of NEARBY_WIFI_DEVICES so must be requested explicitly
|
||||
(see PermissionManager), but shares the NEARBY_DEVICES group with it, so the two
|
||||
are requested together in a single "Nearby devices" prompt.
|
||||
-->
|
||||
<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />
|
||||
<!-- Signature permission for internal UI shutdown broadcasts -->
|
||||
|
||||
@@ -46,14 +46,15 @@ class PermissionManager(private val context: Context) {
|
||||
* WifiAwareMeshService reaches peers over link-local IPv6 TCP sockets
|
||||
* (connectAwareClientSocket), so ACCESS_LOCAL_NETWORK is requested defensively there.
|
||||
*
|
||||
* Note that although ACCESS_LOCAL_NETWORK shares the NEARBY_DEVICES group with
|
||||
* NEARBY_WIFI_DEVICES, the two are tracked and granted independently: granting
|
||||
* NEARBY_WIFI_DEVICES alone leaves ACCESS_LOCAL_NETWORK denied (verified on an
|
||||
* Android 17 device). It must therefore be requested explicitly, which is what this
|
||||
* list is for. Whether the runtime dialog bundles the two into a single prompt has
|
||||
* not been verified.
|
||||
* Both were verified on an Android 17 device, and the two levels behave differently:
|
||||
*
|
||||
* - Grants are tracked independently. Granting NEARBY_WIFI_DEVICES alone leaves
|
||||
* ACCESS_LOCAL_NETWORK denied, so it has to be requested explicitly — hence this list.
|
||||
* - The dialog is shared. Because both belong to the NEARBY_DEVICES group, requesting
|
||||
* them together surfaces a single "Nearby devices" prompt rather than two, so adding
|
||||
* ACCESS_LOCAL_NETWORK costs the user nothing.
|
||||
*/
|
||||
private fun wifiAwarePermissions(): List<String> {
|
||||
fun wifiAwarePermissions(): List<String> {
|
||||
val permissions = mutableListOf(Manifest.permission.NEARBY_WIFI_DEVICES)
|
||||
// API 37 == Android 17; no named VERSION_CODES constant is available yet.
|
||||
if (Build.VERSION.SDK_INT >= 37) {
|
||||
|
||||
@@ -37,6 +37,11 @@ import androidx.compose.ui.graphics.nativeCanvas
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.bitchat.android.R
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.bitchat.android.onboarding.PermissionManager
|
||||
import com.bitchat.android.core.ui.component.sheet.BitchatBottomSheet
|
||||
import com.bitchat.android.core.ui.component.sheet.BitchatSheetTopBar
|
||||
import com.bitchat.android.core.ui.component.sheet.BitchatSheetTitle
|
||||
@@ -128,6 +133,36 @@ fun DebugSettingsSheet(
|
||||
val bleEnabled by manager.bleEnabled.collectAsState()
|
||||
val wifiAwareEnabled by manager.wifiAwareEnabled.collectAsState()
|
||||
val wifiAwareVerbose by manager.wifiAwareVerbose.collectAsState()
|
||||
|
||||
// Onboarding only asks for the Wi‑Fi Aware permissions when the feature is already
|
||||
// enabled, and the toggle defaults to off — so enabling it from here has to request
|
||||
// them itself. Without this, WifiAwareController just logs
|
||||
// "Missing NEARBY_WIFI_DEVICES permission" on a 5s retry loop and never starts.
|
||||
val wifiAwarePermissions = remember { PermissionManager(context).wifiAwarePermissions() }
|
||||
val wifiAwarePermissionLauncher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.RequestMultiplePermissions()
|
||||
) { results ->
|
||||
// NEARBY_WIFI_DEVICES is the hard requirement; ACCESS_LOCAL_NETWORK is defensive,
|
||||
// so don't block enabling on it.
|
||||
val nearbyGranted = results[android.Manifest.permission.NEARBY_WIFI_DEVICES] ?: true
|
||||
if (nearbyGranted) {
|
||||
manager.setWifiAwareEnabled(true)
|
||||
} else {
|
||||
manager.addDebugMessage(
|
||||
DebugMessage.SystemMessage("Wi‑Fi Aware needs the Nearby devices permission")
|
||||
)
|
||||
}
|
||||
}
|
||||
val enableWifiAware: () -> Unit = {
|
||||
val missing = wifiAwarePermissions.filter {
|
||||
ContextCompat.checkSelfPermission(context, it) != PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
if (missing.isEmpty()) {
|
||||
manager.setWifiAwareEnabled(true)
|
||||
} else {
|
||||
wifiAwarePermissionLauncher.launch(missing.toTypedArray())
|
||||
}
|
||||
}
|
||||
val wifiAwareDiscovered by manager.wifiAwareDiscovered.collectAsState()
|
||||
val wifiAwareConnected by manager.wifiAwareConnected.collectAsState()
|
||||
val wifiAwareSupported by com.bitchat.android.wifiaware.WifiAwareController.supported.collectAsState()
|
||||
@@ -345,7 +380,9 @@ fun DebugSettingsSheet(
|
||||
Switch(
|
||||
checked = wifiAwareEnabled && wifiAwareSupported,
|
||||
enabled = wifiSwitchEnabled,
|
||||
onCheckedChange = { manager.setWifiAwareEnabled(it) }
|
||||
onCheckedChange = { on ->
|
||||
if (on) enableWifiAware() else manager.setWifiAwareEnabled(false)
|
||||
}
|
||||
)
|
||||
}
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
@@ -643,7 +680,7 @@ fun DebugSettingsSheet(
|
||||
}
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
AssistChip(
|
||||
onClick = { manager.setWifiAwareEnabled(true) },
|
||||
onClick = enableWifiAware,
|
||||
enabled = wifiAwareSupported,
|
||||
label = { Text("Start") }
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user