Files
ProLibertateGames/app/src/main/java/org/prolibertate/games/MainActivity.kt
T
Claude 85ec10836b Request only the Bluetooth permission the app actually uses
Lint flagged ACCESS_FINE_LOCATION declared without ACCESS_COARSE_LOCATION,
which Android 12 requires to be requested together.

Rather than add the missing one, drop location entirely: the Bluetooth
transport only talks to already-paired devices, enumerating bonded
devices and opening an RFCOMM socket. Neither is a scan, so no location
access is needed. BLUETOOTH_SCAN and BLUETOOTH_ADVERTISE go for the same
reason — nothing was using them.

Discovery of unpaired devices would bring all of these back; the manifest
notes what that would require.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015qjfMiPUDshdKEUa6vxqzN
2026-08-05 22:56:27 +00:00

74 lines
2.6 KiB
Kotlin

package org.prolibertate.games
import android.Manifest
import android.os.Build
import android.os.Bundle
import android.provider.Settings as AndroidSettings
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import org.prolibertate.games.net.LobbyController
import org.prolibertate.games.settings.SettingsRepository
import org.prolibertate.games.ui.AppEnv
import org.prolibertate.games.ui.AppRoot
import org.prolibertate.games.ui.theme.ProLibertateTheme
import org.prolibertate.games.update.Updater
import java.util.UUID
class MainActivity : ComponentActivity() {
private lateinit var env: AppEnv
/**
* Bluetooth needs runtime consent from Android 12 onwards. The result is
* ignored deliberately: refusing simply means the Bluetooth transport
* reports itself unavailable and the lobby falls back to Wi-Fi.
*/
private val bluetoothPermissions =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
env = AppEnv(
settingsRepository = SettingsRepository(applicationContext),
updater = Updater(this),
lobby = LobbyController(applicationContext, lifecycleScope),
peerId = stablePeerId(),
requestBluetoothPermissions = ::requestBluetoothPermissions,
)
setContent {
ProLibertateTheme {
AppRoot(env)
}
}
}
private fun requestBluetoothPermissions() {
// Only CONNECT: the transport reaches paired devices and never scans.
// See the manifest for what discovery of unpaired devices would add.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
bluetoothPermissions.launch(arrayOf(Manifest.permission.BLUETOOTH_CONNECT))
}
}
/**
* Identifies this device to a host for the length of an install. ANDROID_ID
* is per-app-signing-key and survives restarts, which is all a lobby needs
* to tell two phones apart.
*/
private fun stablePeerId(): String {
val androidId = runCatching {
AndroidSettings.Secure.getString(contentResolver, AndroidSettings.Secure.ANDROID_ID)
}.getOrNull()
return androidId ?: UUID.randomUUID().toString()
}
override fun onDestroy() {
super.onDestroy()
if (::env.isInitialized) env.lobby.stop()
}
}