diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-26 11:44:24 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-26 11:44:24 +0200 |
commit | 32778d1c03807b658010e73a4483b61a0e89f7bf (patch) | |
tree | d019adc5466fa6cfc74e3a79646b2856bc3ff05d /ui/src/main/java/com | |
parent | a870bf6e04fd67a1d539b9f2cc44588507923f9b (diff) |
ui: request intent permissions from hidden activity
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt b/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt index 9b801202..fc57b3bd 100644 --- a/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt @@ -10,20 +10,23 @@ import android.os.Bundle import android.service.quicksettings.TileService import android.util.Log import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import com.wireguard.android.Application import com.wireguard.android.QuickTileService import com.wireguard.android.R +import com.wireguard.android.backend.GoBackend import com.wireguard.android.backend.Tunnel import com.wireguard.android.util.ErrorMessages import kotlinx.coroutines.launch @RequiresApi(Build.VERSION_CODES.N) class TunnelToggleActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + private val permissionActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { toggleTunnelWithPermissionsResult() } + + private fun toggleTunnelWithPermissionsResult() { val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return lifecycleScope.launch { try { @@ -42,6 +45,20 @@ class TunnelToggleActivity : AppCompatActivity() { } } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + lifecycleScope.launch { + if (Application.getBackend() is GoBackend) { + val intent = GoBackend.VpnService.prepare(this@TunnelToggleActivity) + if (intent != null) { + permissionActivityResultLauncher.launch(intent) + return@launch + } + } + toggleTunnelWithPermissionsResult() + } + } + companion object { private const val TAG = "WireGuard/TunnelToggleActivity" } |