summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
Diffstat (limited to 'android')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt36
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt5
2 files changed, 17 insertions, 24 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt
index 5a05a261cd..16b34b137d 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt
@@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import android.net.VpnService
import kotlinx.coroutines.CompletableDeferred
-import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
@@ -18,15 +17,13 @@ import net.mullvad.talpid.util.EventNotifier
val ANTICIPATED_STATE_TIMEOUT_MS = 1500L
-class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) {
+class ConnectionProxy(val context: Context, val daemon: MullvadDaemon) {
var mainActivity: MainActivity? = null
private var activeAction: Job? = null
private var resetAnticipatedStateJob: Job? = null
- private val attachListenerJob = attachListener()
private val fetchInitialStateJob = fetchInitialState()
-
private val initialState: TunnelState = TunnelState.Disconnected()
var state = initialState
@@ -47,6 +44,14 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>)
var onStateChange = EventNotifier(state)
var vpnPermission = CompletableDeferred<Boolean>()
+ init {
+ daemon.onTunnelStateChange = { newState ->
+ synchronized(this) {
+ state = newState
+ }
+ }
+ }
+
fun connect() {
if (anticipateConnectingState()) {
cancelActiveAction()
@@ -55,7 +60,7 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>)
activeAction = GlobalScope.launch(Dispatchers.Default) {
if (vpnPermission.await()) {
- daemon.await().connect()
+ daemon.connect()
}
}
}
@@ -65,7 +70,7 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>)
if (anticipateDisconnectingState()) {
cancelActiveAction()
activeAction = GlobalScope.launch(Dispatchers.Default) {
- daemon.await().disconnect()
+ daemon.disconnect()
}
}
}
@@ -75,10 +80,11 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>)
}
fun onDestroy() {
+ daemon.onTunnelStateChange = null
+
onUiStateChange.unsubscribeAll()
onStateChange.unsubscribeAll()
- attachListenerJob.cancel()
- detachListener()
+
fetchInitialStateJob.cancel()
cancelActiveAction()
}
@@ -152,7 +158,7 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>)
}
private fun fetchInitialState() = GlobalScope.launch(Dispatchers.Default) {
- val currentState = daemon.await().getState()
+ val currentState = daemon.getState()
synchronized(this) {
if (state === initialState) {
@@ -160,16 +166,4 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>)
}
}
}
-
- private fun attachListener() = GlobalScope.launch(Dispatchers.Default) {
- daemon.await().onTunnelStateChange = { newState ->
- synchronized(this) {
- state = newState
- }
- }
- }
-
- private fun detachListener() = GlobalScope.launch(Dispatchers.Default) {
- daemon.await().onTunnelStateChange = null
- }
}
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
index f41a3a510c..680b6b4eb0 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt
@@ -20,7 +20,6 @@ class MullvadVpnService : TalpidVpnService() {
private var isStopping = false
private lateinit var daemon: Deferred<MullvadDaemon>
- private lateinit var connectionProxy: ConnectionProxy
private lateinit var notificationManager: ForegroundNotificationManager
private var bindCount = 0
@@ -91,7 +90,6 @@ class MullvadVpnService : TalpidVpnService() {
private fun setUp() {
daemon = startDaemon()
- connectionProxy = ConnectionProxy(this, daemon)
}
private fun startDaemon() = GlobalScope.async(Dispatchers.Default) {
@@ -111,6 +109,8 @@ class MullvadVpnService : TalpidVpnService() {
}
}
+ val connectionProxy = ConnectionProxy(this@MullvadVpnService, daemon)
+
serviceNotifier.notify(ServiceInstance(daemon, connectionProxy, connectivityListener))
daemon
@@ -132,7 +132,6 @@ class MullvadVpnService : TalpidVpnService() {
private fun tearDown() {
stopDaemon()
- connectionProxy.onDestroy()
}
private fun restart() {