From 4d8d79a385e0dbae0d9b0642b1cb09f618f29d49 Mon Sep 17 00:00:00 2001 From: vadym Date: Sat, 8 Aug 2020 01:05:35 -0700 Subject: [PATCH] 1. implemented network state receiver --- .../chronosx88/yggdrasil/MainActivity.kt | 37 ++++++++++++++-- .../yggdrasil/models/config/NetworkUtils.kt | 44 +++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/io/github/chronosx88/yggdrasil/models/config/NetworkUtils.kt 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 dcd786b..b96e71d 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt @@ -3,6 +3,8 @@ package io.github.chronosx88.yggdrasil import android.app.Activity import android.app.ActivityManager import android.content.* +import android.net.ConnectivityManager +import android.net.Network import android.net.VpnService import android.os.Build import android.os.Bundle @@ -16,6 +18,7 @@ import dalvik.system.DexFile 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.NetworkUtils import io.github.chronosx88.yggdrasil.models.config.PeerInfoListAdapter import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializePeerStringList2PeerInfoSet import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2DNSInfoSet @@ -24,7 +27,6 @@ import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeS import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringSet2PeerInfoSet import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializeDNSInfoSet2StringList import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializePeerInfoSet2StringList -import java.lang.reflect.Method import kotlin.concurrent.thread @@ -65,7 +67,7 @@ class MainActivity : AppCompatActivity() { private var currentPeers = setOf() private var currentDNS = setOf() - private var meshPeersReceiver: BroadcastReceiver? = null + private var networkStateReceiver: BroadcastReceiver? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -141,6 +143,33 @@ class MainActivity : AppCompatActivity() { ipLayout.visibility = View.VISIBLE findViewById(R.id.ip).text = address } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + connectivityManager?.let { + it.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() { + override fun onAvailable(network: Network) { + startVpn() + } + override fun onLost(network: Network?) { + stopVpn() + } + }) + } + } else { + networkStateReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val status: Int = NetworkUtils.getConnectivityStatusString(context) + Log.i(TAG, "Network state has been changed") + if ("android.net.conn.CONNECTIVITY_CHANGE" == intent.action) { + if (status == NetworkUtils.NETWORK_STATUS_NOT_CONNECTED) { + stopVpn() + } else { + startVpn() + } + } + } + } + } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { val sourceDir: String = this.applicationInfo.sourceDir val dexFile = DexFile(sourceDir) @@ -335,8 +364,8 @@ class MainActivity : AppCompatActivity() { override fun onDestroy() { super.onDestroy() - if (meshPeersReceiver != null){ - unregisterReceiver(meshPeersReceiver); + if (networkStateReceiver != null){ + unregisterReceiver(networkStateReceiver); } } diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/NetworkUtils.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/NetworkUtils.kt new file mode 100644 index 0000000..5239389 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/NetworkUtils.kt @@ -0,0 +1,44 @@ +package io.github.chronosx88.yggdrasil.models.config + +import android.content.Context +import android.net.ConnectivityManager + + +class NetworkUtils { + + companion object { + + val TYPE_WIFI = 1 + val TYPE_MOBILE = 2 + val TYPE_NOT_CONNECTED = 0 + val NETWORK_STATUS_NOT_CONNECTED = 0 + val NETWORK_STATUS_WIFI = 1 + val NETWORK_STATUS_MOBILE = 2 + + @JvmStatic + fun getConnectivityStatus(context: Context): Int { + val cm = + context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetwork = cm.activeNetworkInfo + if (null != activeNetwork) { + if (activeNetwork.type == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI + if (activeNetwork.type == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE + } + return TYPE_NOT_CONNECTED + } + + @JvmStatic + fun getConnectivityStatusString(context: Context): Int { + val conn: Int = getConnectivityStatus(context) + var status = 0 + if (conn == TYPE_WIFI) { + status = NETWORK_STATUS_WIFI + } else if (conn == TYPE_MOBILE) { + status = NETWORK_STATUS_MOBILE + } else if (conn == TYPE_NOT_CONNECTED) { + status = NETWORK_STATUS_NOT_CONNECTED + } + return status + } + } +} \ No newline at end of file