2023-11-24 21:51:09 +03:00
|
|
|
package org.amnezia.vpn
|
|
|
|
|
|
|
|
|
|
import android.app.KeyguardManager
|
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.IntentFilter
|
|
|
|
|
import android.net.VpnService
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.activity.ComponentActivity
|
|
|
|
|
import androidx.activity.result.ActivityResult
|
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import androidx.core.content.getSystemService
|
2023-11-28 19:01:44 +03:00
|
|
|
import org.amnezia.vpn.util.Log
|
2023-11-24 21:51:09 +03:00
|
|
|
|
|
|
|
|
private const val TAG = "VpnRequestActivity"
|
|
|
|
|
|
|
|
|
|
class VpnRequestActivity : ComponentActivity() {
|
|
|
|
|
|
|
|
|
|
private var userPresentReceiver: BroadcastReceiver? = null
|
|
|
|
|
private val requestLauncher =
|
|
|
|
|
registerForActivityResult(StartActivityForResult(), ::checkRequestResult)
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
2024-01-20 16:40:12 +03:00
|
|
|
Log.d(TAG, "Start request activity")
|
2023-11-24 21:51:09 +03:00
|
|
|
val requestIntent = VpnService.prepare(applicationContext)
|
|
|
|
|
if (requestIntent != null) {
|
|
|
|
|
if (getSystemService<KeyguardManager>()!!.isKeyguardLocked) {
|
|
|
|
|
userPresentReceiver = object : BroadcastReceiver() {
|
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) =
|
|
|
|
|
requestLauncher.launch(requestIntent)
|
|
|
|
|
}
|
|
|
|
|
registerReceiver(userPresentReceiver, IntentFilter(Intent.ACTION_USER_PRESENT))
|
|
|
|
|
} else {
|
|
|
|
|
requestLauncher.launch(requestIntent)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
onPermissionGranted()
|
|
|
|
|
finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
|
userPresentReceiver?.let {
|
|
|
|
|
unregisterReceiver(it)
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun checkRequestResult(result: ActivityResult) {
|
|
|
|
|
when (result.resultCode) {
|
|
|
|
|
RESULT_OK -> onPermissionGranted()
|
|
|
|
|
else -> Toast.makeText(this, "Vpn permission denied", Toast.LENGTH_LONG).show()
|
|
|
|
|
}
|
|
|
|
|
finish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun onPermissionGranted() {
|
|
|
|
|
Toast.makeText(this, "Vpn permission granted", Toast.LENGTH_LONG).show()
|
|
|
|
|
Intent(applicationContext, AmneziaVpnService::class.java).apply {
|
|
|
|
|
putExtra(AFTER_PERMISSION_CHECK, true)
|
|
|
|
|
}.also {
|
|
|
|
|
ContextCompat.startForegroundService(this, it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|