diff options
Diffstat (limited to 'android/src/main')
3 files changed, 64 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt index 67fe711a6c..91bc269ba2 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/MullvadVpnService.kt @@ -23,6 +23,7 @@ class MullvadVpnService : TalpidVpnService() { private lateinit var notificationManager: ForegroundNotificationManager override fun onCreate() { + super.onCreate() setUp() created.complete(Unit) } @@ -47,6 +48,7 @@ class MullvadVpnService : TalpidVpnService() { tearDown() daemon.cancel() created.cancel() + super.onDestroy() } inner class LocalBinder : Binder() { diff --git a/android/src/main/kotlin/net/mullvad/talpid/ConnectivityListener.kt b/android/src/main/kotlin/net/mullvad/talpid/ConnectivityListener.kt new file mode 100644 index 0000000000..143587d340 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/talpid/ConnectivityListener.kt @@ -0,0 +1,52 @@ +package net.mullvad.talpid + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.net.ConnectivityManager +import android.net.NetworkInfo +import android.net.NetworkInfo.DetailedState + +class ConnectivityListener : BroadcastReceiver() { + var isConnected = true + private set + + fun register(context: Context) { + val intentFilter = IntentFilter() + + intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION) + context.registerReceiver(this, intentFilter) + + checkConnectionState(context) + } + + fun unregister(context: Context) { + context.unregisterReceiver(this) + } + + override fun onReceive(context: Context, intent: Intent) { + val networkInfo = + intent.getParcelableExtra<NetworkInfo>(ConnectivityManager.EXTRA_NETWORK_INFO) + + if (networkInfo.type != ConnectivityManager.TYPE_VPN) { + if (networkInfo.detailedState == DetailedState.DISCONNECTED) { + checkConnectionState(context) + } else if (networkInfo.detailedState == DetailedState.CONNECTED) { + isConnected = true + } + } + } + + private fun checkConnectionState(context: Context) { + val connectivityManager = + context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + + isConnected = connectivityManager.allNetworks + .map({ network -> connectivityManager.getNetworkInfo(network) }) + .any({ networkInfo -> + networkInfo.type != ConnectivityManager.TYPE_VPN && + networkInfo.detailedState == DetailedState.CONNECTED + }) + } +} diff --git a/android/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt b/android/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt index 96de4082cc..185f401f7e 100644 --- a/android/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/talpid/TalpidVpnService.kt @@ -4,6 +4,16 @@ import android.net.VpnService import net.mullvad.talpid.tun_provider.TunConfig open class TalpidVpnService : VpnService() { + val connectivityListener = ConnectivityListener() + + override fun onCreate() { + connectivityListener.register(this) + } + + override fun onDestroy() { + connectivityListener.unregister(this) + } + fun createTun(config: TunConfig): Int { val builder = Builder().apply { for (address in config.addresses) { |
