Skip to content

Commit

Permalink
android: request VPN permissions on launch (#219)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
barnstar authored Mar 19, 2024
1 parent 113a7c6 commit f12439f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions android/src/main/java/com/tailscale/ipn/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavType
Expand Down Expand Up @@ -45,6 +46,7 @@ import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
private var notifierScope: CoroutineScope? = null
private lateinit var requestVpnPermission: ActivityResultLauncher<Unit>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -150,14 +152,15 @@ class MainActivity : ComponentActivity() {
val vpnIntent = VpnService.prepare(this)
if (vpnIntent != null) {
val contract = VpnPermissionContract()
registerForActivityResult(contract) { granted ->
Notifier.vpnPermissionGranted.set(granted)
if (granted) {
Log.i("VPN", "VPN permission granted")
} else {
Log.i("VPN", "VPN permission not granted")
}
}
requestVpnPermission =
registerForActivityResult(contract) { granted ->
Notifier.vpnPermissionGranted.set(granted)
Log.i("VPN", "VPN permission ${if (granted) "granted" else "denied"}")
}
requestVpnPermission.launch(Unit)
} else {
Notifier.vpnPermissionGranted.set(true)
Log.i("VPN", "VPN permission granted")
}
}
}
Expand Down

0 comments on commit f12439f

Please sign in to comment.