Add error handling of enabled "always-on" during VPN connection (#698)
* Always add awg-go version to the log * Display an error message always when no vpn permission is granted
This commit is contained in:
parent
30bd264f17
commit
7730dd510c
8 changed files with 67 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package org.amnezia.vpn
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.Intent.EXTRA_MIME_TYPES
|
||||
|
|
@ -14,6 +15,7 @@ import android.os.IBinder
|
|||
import android.os.Looper
|
||||
import android.os.Message
|
||||
import android.os.Messenger
|
||||
import android.provider.Settings
|
||||
import android.view.WindowManager.LayoutParams
|
||||
import android.webkit.MimeTypeMap
|
||||
import android.widget.Toast
|
||||
|
|
@ -216,13 +218,13 @@ class AmneziaActivity : QtActivity() {
|
|||
when (resultCode) {
|
||||
RESULT_OK -> {
|
||||
Log.d(TAG, "Vpn permission granted")
|
||||
Toast.makeText(this, "Vpn permission granted", Toast.LENGTH_LONG).show()
|
||||
Toast.makeText(this, resources.getText(R.string.vpnGranted), Toast.LENGTH_LONG).show()
|
||||
checkVpnPermissionCallbacks?.run { onSuccess() }
|
||||
}
|
||||
|
||||
else -> {
|
||||
Log.w(TAG, "Vpn permission denied, resultCode: $resultCode")
|
||||
Toast.makeText(this, "Vpn permission denied", Toast.LENGTH_LONG).show()
|
||||
showOnVpnPermissionRejectDialog()
|
||||
checkVpnPermissionCallbacks?.run { onFail() }
|
||||
}
|
||||
}
|
||||
|
|
@ -280,6 +282,17 @@ class AmneziaActivity : QtActivity() {
|
|||
onSuccess()
|
||||
}
|
||||
|
||||
private fun showOnVpnPermissionRejectDialog() {
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle(R.string.vpnSetupFailed)
|
||||
.setMessage(R.string.vpnSetupFailedMessage)
|
||||
.setNegativeButton(R.string.ok) { _, _ -> }
|
||||
.setPositiveButton(R.string.openVpnSettings) { _, _ ->
|
||||
startActivity(Intent(Settings.ACTION_VPN_SETTINGS))
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
@MainThread
|
||||
private fun startVpn(vpnConfig: String) {
|
||||
if (isServiceConnected) {
|
||||
|
|
|
|||
|
|
@ -215,11 +215,9 @@ class AmneziaVpnService : VpnService() {
|
|||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
val isAlwaysOnCompat =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) isAlwaysOn
|
||||
else intent?.component?.packageName != packageName
|
||||
val isAlwaysOn = intent != null && intent.action == SERVICE_INTERFACE
|
||||
|
||||
if (isAlwaysOnCompat) {
|
||||
if (isAlwaysOn) {
|
||||
Log.d(TAG, "Start service via Always-on")
|
||||
connect()
|
||||
} else if (intent?.getBooleanExtra(AFTER_PERMISSION_CHECK, false) == true) {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
package org.amnezia.vpn
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.app.KeyguardManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.res.Configuration.UI_MODE_NIGHT_MASK
|
||||
import android.content.res.Configuration.UI_MODE_NIGHT_YES
|
||||
import android.net.VpnService
|
||||
import android.os.Bundle
|
||||
import android.provider.Settings
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.result.ActivityResult
|
||||
|
|
@ -52,19 +56,43 @@ class VpnRequestActivity : ComponentActivity() {
|
|||
}
|
||||
|
||||
private fun checkRequestResult(result: ActivityResult) {
|
||||
when (result.resultCode) {
|
||||
RESULT_OK -> onPermissionGranted()
|
||||
else -> Toast.makeText(this, "Vpn permission denied", Toast.LENGTH_LONG).show()
|
||||
when (val resultCode = result.resultCode) {
|
||||
RESULT_OK -> {
|
||||
onPermissionGranted()
|
||||
finish()
|
||||
}
|
||||
|
||||
else -> {
|
||||
Log.w(TAG, "Vpn permission denied, resultCode: $resultCode")
|
||||
showOnVpnPermissionRejectDialog()
|
||||
}
|
||||
}
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun onPermissionGranted() {
|
||||
Toast.makeText(this, "Vpn permission granted", Toast.LENGTH_LONG).show()
|
||||
Toast.makeText(this, resources.getString(R.string.vpnGranted), Toast.LENGTH_LONG).show()
|
||||
Intent(applicationContext, AmneziaVpnService::class.java).apply {
|
||||
putExtra(AFTER_PERMISSION_CHECK, true)
|
||||
}.also {
|
||||
ContextCompat.startForegroundService(this, it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showOnVpnPermissionRejectDialog() {
|
||||
AlertDialog.Builder(this, getDialogTheme())
|
||||
.setTitle(R.string.vpnSetupFailed)
|
||||
.setMessage(R.string.vpnSetupFailedMessage)
|
||||
.setNegativeButton(R.string.ok) { _, _ -> }
|
||||
.setPositiveButton(R.string.openVpnSettings) { _, _ ->
|
||||
startActivity(Intent(Settings.ACTION_VPN_SETTINGS))
|
||||
}
|
||||
.setOnDismissListener { finish() }
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun getDialogTheme(): Int =
|
||||
if (resources.configuration.uiMode and UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES)
|
||||
android.R.style.Theme_DeviceDefault_Dialog_Alert
|
||||
else
|
||||
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue