diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-06 20:28:51 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-14 20:39:01 +0000 |
| commit | be8923f7816b9501d2e40bf4631920b40d0a084c (patch) | |
| tree | 9221992db74634f07b18ed79801c6df6970d591e /android | |
| parent | b163985b9be207261909eb8ced77cd3b4dc48214 (diff) | |
| download | mullvadvpn-be8923f7816b9501d2e40bf4631920b40d0a084c.tar.xz mullvadvpn-be8923f7816b9501d2e40bf4631920b40d0a084c.zip | |
Refactor `TileService` to use flow based API
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt index db9662f5d6..b487e49a0e 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt @@ -1,46 +1,32 @@ package net.mullvad.mullvadvpn.service -import android.content.ComponentName import android.content.Intent import android.graphics.drawable.Icon import android.os.Build -import android.os.IBinder -import android.os.Messenger import android.service.quicksettings.Tile import android.service.quicksettings.TileService import kotlin.properties.Delegates.observable +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.R +import net.mullvad.mullvadvpn.ipc.ServiceConnection import net.mullvad.mullvadvpn.model.TunnelState -import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnection import net.mullvad.talpid.tunnel.ActionAfterDisconnect class MullvadTileService : TileService() { - private val serviceConnectionManager = object : android.content.ServiceConnection { - override fun onServiceConnected(className: ComponentName, binder: IBinder) { - serviceConnection = ServiceConnection(Messenger(binder)) - } - - override fun onServiceDisconnected(className: ComponentName) { - serviceConnection = null - } - } - - private var serviceConnection by observable<ServiceConnection?>( - null - ) { _, oldConnection, newConnection -> - oldConnection?.onDestroy() - - newConnection?.connectionProxy?.run { - onStateChange.subscribe(this@MullvadTileService, ::updateTunnelState) - } - } - private var secured by observable(false) { _, wasSecured, isSecured -> if (wasSecured != isSecured) { updateTileState() } } + private lateinit var scope: CoroutineScope + private lateinit var securedIcon: Icon private lateinit var unsecuredIcon: Icon @@ -54,11 +40,9 @@ class MullvadTileService : TileService() { override fun onStartListening() { super.onStartListening() - val intent = Intent(this, MullvadVpnService::class.java) - - bindService(intent, serviceConnectionManager, BIND_IMPORTANT) + scope = MainScope() - updateTileState() + scope.launch { listenToTunnelState() } } override fun onClick() { @@ -80,12 +64,18 @@ class MullvadTileService : TileService() { } override fun onStopListening() { - unbindService(serviceConnectionManager) - serviceConnection = null - + scope.cancel() super.onStopListening() } + @OptIn(FlowPreview::class) + private suspend fun listenToTunnelState() { + ServiceConnection(this@MullvadTileService, scope) + .tunnelState + .debounce(300L) + .collect(::updateTunnelState) + } + private fun updateTunnelState(tunnelState: TunnelState) { secured = when (tunnelState) { is TunnelState.Disconnected -> false |
