1. corrected VPN service status on GUI

This commit is contained in:
vadym 2020-06-27 09:53:48 -07:00
parent 38fb87b08d
commit bee63041f4
3 changed files with 36 additions and 17 deletions

View File

@ -32,10 +32,10 @@
</intent-filter>
</service>
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:launchMode= "singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

View File

@ -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<String>): MutableSet<PeerInfo> {
var gson = Gson()
@ -105,13 +109,13 @@ class MainActivity : AppCompatActivity() {
}
}
private var startVpnFlag = false
private var currentPeers = setOf<PeerInfo>()
private var currentDNS = setOf<DNSInfo>()
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<ListView>(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<Switch>(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
}
}
}

View File

@ -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"))
}
}
}