Skip to content

Commit

Permalink
Fix crash in request vpn permission
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Pururun committed Jul 31, 2024
1 parent 563f107 commit e3de3f4
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import androidx.activity.result.contract.ActivityResultContract

class RequestVpnPermission : ActivityResultContract<Unit, Boolean>() {
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<Boolean>? {
return if (VpnService.prepare(context) == null) {
SynchronousResult(true)
} else {
null
}
}
}

0 comments on commit e3de3f4

Please sign in to comment.