From d10bb853a6b3dc52272c84a627b2cb73aabdcd8c Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 29 Oct 2021 23:45:30 +0100 Subject: [PATCH] Build 8 (improve always-on VPN) --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 1 - .../neilalexander/yggdrasil/MainActivity.kt | 37 ++++++++++--------- .../yggdrasil/PacketTunnelProvider.kt | 12 +++--- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a67d07a..2c79707 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { applicationId "eu.neilalexander.yggdrasil" minSdkVersion 21 targetSdkVersion 29 - versionCode 6 + versionCode 7 versionName "0.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f9f16b4..7f14e31 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,6 @@ - diff --git a/app/src/main/java/eu/neilalexander/yggdrasil/MainActivity.kt b/app/src/main/java/eu/neilalexander/yggdrasil/MainActivity.kt index 577c399..769b803 100644 --- a/app/src/main/java/eu/neilalexander/yggdrasil/MainActivity.kt +++ b/app/src/main/java/eu/neilalexander/yggdrasil/MainActivity.kt @@ -1,5 +1,6 @@ package eu.neilalexander.yggdrasil +import android.app.Activity import android.content.BroadcastReceiver import android.content.Context import android.content.Intent @@ -10,6 +11,7 @@ import android.os.Bundle import android.widget.Switch import android.widget.TableRow import android.widget.TextView +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.localbroadcastmanager.content.LocalBroadcastManager import mobile.Mobile @@ -27,6 +29,18 @@ class MainActivity : AppCompatActivity() { private lateinit var peersRow: TableRow private lateinit var settingsRow: TableRow + private fun start() { + val intent = Intent(this, PacketTunnelProvider::class.java) + intent.action = PacketTunnelProvider.ACTION_START + startService(intent) + } + + private var startVpnActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == Activity.RESULT_OK) { + start() + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -44,16 +58,14 @@ class MainActivity : AppCompatActivity() { enabledLabel.setTextColor(Color.GRAY) - VpnService.prepare(this) - enabledSwitch.setOnCheckedChangeListener { _, isChecked -> when (isChecked) { true -> { - val vpnintent = VpnService.prepare(this) - if (vpnintent != null) { - startActivityForResult(vpnintent, 0) + val vpnIntent = VpnService.prepare(this) + if (vpnIntent != null) { + startVpnActivity.launch(vpnIntent) } else { - onActivityResult(0, RESULT_OK, vpnintent) + start() } } false -> { @@ -89,6 +101,7 @@ class MainActivity : AppCompatActivity() { when (intent.getStringExtra("type")) { "state" -> { enabledLabel.text = if (intent.getBooleanExtra("started", false)) { + enabledSwitch.isChecked = true if (state.dhtCount() == 0) { enabledLabel.setTextColor(Color.RED) "No connectivity" @@ -97,6 +110,7 @@ class MainActivity : AppCompatActivity() { "Enabled" } } else { + enabledSwitch.isChecked = false enabledLabel.setTextColor(Color.GRAY) "Not enabled" } @@ -117,15 +131,4 @@ class MainActivity : AppCompatActivity() { super.onPause() LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver) } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (resultCode) { - RESULT_OK -> { - val intent = Intent(this, PacketTunnelProvider::class.java) - intent.action = PacketTunnelProvider.ACTION_START - startService(intent) - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/eu/neilalexander/yggdrasil/PacketTunnelProvider.kt b/app/src/main/java/eu/neilalexander/yggdrasil/PacketTunnelProvider.kt index 3c3dfb8..afdf4d4 100644 --- a/app/src/main/java/eu/neilalexander/yggdrasil/PacketTunnelProvider.kt +++ b/app/src/main/java/eu/neilalexander/yggdrasil/PacketTunnelProvider.kt @@ -1,6 +1,6 @@ package eu.neilalexander.yggdrasil -import android.content.Intent +import android.content.* import android.net.Uri import android.net.VpnService import android.os.Handler @@ -50,17 +50,17 @@ class PacketTunnelProvider: VpnService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { if (intent == null) { + Log.d("PacketTunnelProvider", "Intent is null") return START_NOT_STICKY } return when (intent.action ?: ACTION_STOP) { - ACTION_START -> { - start(); START_STICKY - } ACTION_STOP -> { + Log.d("PacketTunnelProvider", "Stopping...") stop(); START_NOT_STICKY } else -> { - stop(); START_NOT_STICKY + Log.d("PacketTunnelProvider", "Starting...") + start(); START_STICKY } } } @@ -169,7 +169,7 @@ class PacketTunnelProvider: VpnService() { break@updates } try { - Thread.sleep(2000) + Thread.sleep(1000) } catch (e: java.lang.InterruptedException) { return }