From bee63041f4e1f81d5ac67b82ffc763ac73ab3ab9 Mon Sep 17 00:00:00 2001 From: vadym Date: Sat, 27 Jun 2020 09:53:48 -0700 Subject: [PATCH] 1. corrected VPN service status on GUI --- app/src/main/AndroidManifest.xml | 4 +-- .../chronosx88/yggdrasil/MainActivity.kt | 29 ++++++++++--------- .../yggdrasil/YggdrasilTunService.kt | 20 ++++++++++++- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 32c387d..3c85d9c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,10 +32,10 @@ - + - diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt index d15c230..2f64c05 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt @@ -1,7 +1,10 @@ package io.github.chronosx88.yggdrasil import android.app.Activity +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.util.Log @@ -11,14 +14,13 @@ import android.view.MenuItem import android.view.View import android.widget.* import androidx.appcompat.app.AppCompatActivity +import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.preference.PreferenceManager import com.google.gson.Gson import io.github.chronosx88.yggdrasil.models.DNSInfo import io.github.chronosx88.yggdrasil.models.PeerInfo import io.github.chronosx88.yggdrasil.models.config.DNSInfoListAdapter import io.github.chronosx88.yggdrasil.models.config.PeerInfoListAdapter -import kotlin.collections.ArrayList -import kotlin.collections.HashSet class MainActivity : AppCompatActivity() { @@ -44,6 +46,8 @@ class MainActivity : AppCompatActivity() { private const val TAG="Yggdrasil" private const val VPN_REQUEST_CODE = 0x0F + @JvmStatic var isStarted = false + @JvmStatic fun deserializeStringList2PeerInfoSet(list: List): MutableSet { var gson = Gson() @@ -105,13 +109,13 @@ class MainActivity : AppCompatActivity() { } } - private var startVpnFlag = false private var currentPeers = setOf() private var currentDNS = setOf() - private var isStarted = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + LocalBroadcastManager.getInstance(this).registerReceiver(ServiceEchoReceiver(), IntentFilter("pong")); + LocalBroadcastManager.getInstance(this).sendBroadcastSync(Intent("ping")); setContentView(R.layout.activity_main) val listView = findViewById(R.id.peers) //save to shared preferences @@ -146,9 +150,6 @@ class MainActivity : AppCompatActivity() { intent.putStringArrayListExtra(DNS_LIST, serializeDNSInfoSet2StringList(currentDNS)) startActivityForResult(intent, DNS_LIST_CODE) } - if(intent.extras!==null) { - startVpnFlag = intent.extras!!.getBoolean(START_VPN, false) - } } private fun stopVpn(){ @@ -212,7 +213,6 @@ class MainActivity : AppCompatActivity() { i!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) i.putExtra(START_VPN, true) - finish() startActivity(i) } } @@ -241,7 +241,6 @@ class MainActivity : AppCompatActivity() { i!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) i.putExtra(START_VPN, true) - finish() startActivity(i) } } @@ -275,12 +274,8 @@ class MainActivity : AppCompatActivity() { item.setActionView(R.layout.menu_switch) val switchOn = item .actionView.findViewById(R.id.switchOn) - if(startVpnFlag){ + if(isStarted){ switchOn.isChecked = true - startVpnFlag = false - startVpn() - } else { - switchOn.isChecked = false } switchOn.setOnCheckedChangeListener { _, isChecked -> if (isChecked) { @@ -299,4 +294,10 @@ class MainActivity : AppCompatActivity() { toast.show() } + private class ServiceEchoReceiver : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + isStarted = true + } + } + } diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt index f0a2ce9..3fc051f 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt @@ -1,11 +1,15 @@ package io.github.chronosx88.yggdrasil import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.net.VpnService import android.os.ParcelFileDescriptor import android.system.OsConstants import android.util.Log +import androidx.localbroadcastmanager.content.LocalBroadcastManager import com.google.gson.Gson import dummy.ConduitEndpoint import io.github.chronosx88.yggdrasil.models.DNSInfo @@ -15,7 +19,6 @@ import mobile.Mobile import mobile.Yggdrasil import java.io.* import java.nio.ByteBuffer -import kotlin.coroutines.CoroutineContext class YggdrasilTunService : VpnService() { @@ -43,6 +46,13 @@ class YggdrasilTunService : VpnService() { private var tunOutputStream: OutputStream? = null private var scope: CoroutineScope? = null + override fun onCreate() { + super.onCreate() + LocalBroadcastManager + .getInstance(this) + .registerReceiver(ServiceEchoReceiver(), IntentFilter("ping")) + } + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.STOP) { @@ -184,4 +194,12 @@ class YggdrasilTunService : VpnService() { super.onDestroy() stopSelf() } + + private class ServiceEchoReceiver : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + LocalBroadcastManager + .getInstance(context!!) + .sendBroadcastSync(Intent("pong")) + } + } }