From e3de3f4958d918feb99463d412844536f75bfc61 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Tue, 30 Jul 2024 17:39:25 +0200 Subject: [PATCH] Fix crash in request vpn permission This crash could occur if create intent in request vpn permission was called while vpn permission was already approved Fixed by returning true immediately in those cases --- .../compose/util/RequestVpnPermission.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RequestVpnPermission.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RequestVpnPermission.kt index 13817db4bc6d..f198a3159c68 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RequestVpnPermission.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RequestVpnPermission.kt @@ -8,13 +8,21 @@ import androidx.activity.result.contract.ActivityResultContract class RequestVpnPermission : ActivityResultContract() { override fun createIntent(context: Context, input: Unit): Intent { - // We expect this permission to only be requested when the permission is missing, however, - // if it for some reason is called incorrectly we should return an empty intent so we avoid - // a crash. - return VpnService.prepare(context) ?: Intent() + return VpnService.prepare(context)!! } override fun parseResult(resultCode: Int, intent: Intent?): Boolean { return resultCode == Activity.RESULT_OK } + + // We expect this permission to only be requested when the permission is missing. However, + // if it for some reason is called incorrectly we will skip the call to create intent + // to avoid crashing. The app will then proceed as the user accepted the permission. + override fun getSynchronousResult(context: Context, input: Unit): SynchronousResult? { + return if (VpnService.prepare(context) == null) { + SynchronousResult(true) + } else { + null + } + } }