diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-09-16 11:04:42 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-09-16 13:42:11 +0000 |
| commit | 1a443fadad4f684ecc357b33fdd8ce59731ad473 (patch) | |
| tree | 0d4df6c8a43b9ac6cf4585c1c33341efcd41953a /android/src | |
| parent | 0f204502615afcdebbaac7b16eff40f0bb70f2cf (diff) | |
| download | mullvadvpn-1a443fadad4f684ecc357b33fdd8ce59731ad473.tar.xz mullvadvpn-1a443fadad4f684ecc357b33fdd8ce59731ad473.zip | |
Reset state if anticipated state doesn't arrive
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt | 26 |
1 files changed, 26 insertions, 0 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 1919e2c820..67bbb44b0e 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/ConnectionProxy.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.net.VpnService +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Deferred @@ -17,10 +18,13 @@ import net.mullvad.mullvadvpn.model.ActionAfterDisconnect import net.mullvad.mullvadvpn.model.TunnelState import net.mullvad.mullvadvpn.util.EventNotifier +val ANTICIPATED_STATE_TIMEOUT_MS = 1500L + class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) { var mainActivity: MainActivity? = null private var activeAction: Job? = null + private var resetAnticipatedStateJob: Job? = null private val attachListenerJob = attachListener() private val fetchInitialStateJob = fetchInitialState() @@ -30,6 +34,7 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) var state = initialState set(value) { field = value + resetAnticipatedStateJob?.cancel() onStateChange.notify(value) uiState = value } @@ -87,6 +92,7 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) if (currentState is TunnelState.Connecting || currentState is TunnelState.Connected) { return false } else { + scheduleToResetAnticipatedState() uiState = TunnelState.Connecting(null, null) return true } @@ -100,12 +106,32 @@ class ConnectionProxy(val context: Context, val daemon: Deferred<MullvadDaemon>) if (currentState is TunnelState.Disconnected) { return false } else { + scheduleToResetAnticipatedState() uiState = TunnelState.Disconnecting(ActionAfterDisconnect.Nothing()) return true } } } + private fun scheduleToResetAnticipatedState() { + resetAnticipatedStateJob?.cancel() + + var currentJob: Job? = null + + val newJob = GlobalScope.launch(Dispatchers.Default) { + delay(ANTICIPATED_STATE_TIMEOUT_MS) + + synchronized(this@ConnectionProxy) { + if (!currentJob!!.isCancelled) { + uiState = state + } + } + } + + currentJob = newJob + resetAnticipatedStateJob = newJob + } + private fun requestVpnPermission() { val intent = VpnService.prepare(context) |
