android: request VPN permissions on launch (#219)

Updates tailscale/corp#18202

The actual requesting of VPN permissions was lost in a rebase. This change should prompt you on start.

Signed-off-by: Jonathan Nobels <jonathan@tailscale.com>
pull/220/head
Jonathan Nobels 8 months ago committed by GitHub
parent 113a7c6f9d
commit f12439f9a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -13,6 +13,7 @@ import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract import androidx.activity.result.contract.ActivityResultContract
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavType import androidx.navigation.NavType
@ -45,6 +46,7 @@ import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private var notifierScope: CoroutineScope? = null private var notifierScope: CoroutineScope? = null
private lateinit var requestVpnPermission: ActivityResultLauncher<Unit>
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -150,14 +152,15 @@ class MainActivity : ComponentActivity() {
val vpnIntent = VpnService.prepare(this) val vpnIntent = VpnService.prepare(this)
if (vpnIntent != null) { if (vpnIntent != null) {
val contract = VpnPermissionContract() val contract = VpnPermissionContract()
registerForActivityResult(contract) { granted -> requestVpnPermission =
Notifier.vpnPermissionGranted.set(granted) registerForActivityResult(contract) { granted ->
if (granted) { Notifier.vpnPermissionGranted.set(granted)
Log.i("VPN", "VPN permission granted") Log.i("VPN", "VPN permission ${if (granted) "granted" else "denied"}")
} else { }
Log.i("VPN", "VPN permission not granted") requestVpnPermission.launch(Unit)
} } else {
} Notifier.vpnPermissionGranted.set(true)
Log.i("VPN", "VPN permission granted")
} }
} }
} }

Loading…
Cancel
Save