summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-06 20:28:16 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-14 20:39:01 +0000
commitb163985b9be207261909eb8ced77cd3b4dc48214 (patch)
tree354da4d6eb9975a89b13f1f2cad0959f6aad0b99 /android
parentaf707a7101ca38713dfefba1ff1c4b964be0b19e (diff)
downloadmullvadvpn-b163985b9be207261909eb8ced77cd3b4dc48214.tar.xz
mullvadvpn-b163985b9be207261909eb8ced77cd3b4dc48214.zip
Provide flow of tunnel states
Diffstat (limited to 'android')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ipc/ServiceConnection.kt17
1 files changed, 17 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ipc/ServiceConnection.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ipc/ServiceConnection.kt
index 6683b7459a..751bcc9bc3 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ipc/ServiceConnection.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ipc/ServiceConnection.kt
@@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
+import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.service.MullvadVpnService
import net.mullvad.mullvadvpn.util.DispatchingFlow
import net.mullvad.mullvadvpn.util.bindServiceFlow
@@ -29,6 +30,9 @@ class ServiceConnection(context: Context, scope: CoroutineScope) {
private lateinit var listenerRegistrations: StateFlow<Pair<Messenger, Int>?>
+ lateinit var tunnelState: StateFlow<TunnelState>
+ private set
+
init {
val dispatcher = handler
.filterNotNull()
@@ -36,6 +40,12 @@ class ServiceConnection(context: Context, scope: CoroutineScope) {
listenerRegistrations = subscribeToState(Event.ListenerReady::class, scope) {
Pair(connection, listenerId)
}
+
+ tunnelState = subscribeToState(
+ Event.TunnelStateChange::class,
+ scope,
+ TunnelState.Disconnected
+ ) { tunnelState }
}
scope.launch { connect(context) }
@@ -83,4 +93,11 @@ class ServiceConnection(context: Context, scope: CoroutineScope) {
scope: CoroutineScope,
dataExtractor: suspend V.() -> D
) = subscribe(event).map(dataExtractor).stateIn(scope, SharingStarted.Lazily, null)
+
+ private fun <V : Any, D> DispatchingFlow<in V>.subscribeToState(
+ event: KClass<V>,
+ scope: CoroutineScope,
+ initialValue: D,
+ dataExtractor: suspend V.() -> D
+ ) = subscribe(event).map(dataExtractor).stateIn(scope, SharingStarted.Lazily, initialValue)
}