diff options
| author | Albin <albin@mullvad.net> | 2022-06-23 12:00:52 +0200 |
|---|---|---|
| committer | Albin <albin@mullvad.net> | 2022-06-27 17:21:36 +0200 |
| commit | 6d7571d428c975f969f4bac18994734564b24c1e (patch) | |
| tree | 739689ae5a90842331d16653f26ab0a526d1a39c /android/app/src | |
| parent | 3584435cb0692833bc2c234fc39fd6501501ed1a (diff) | |
| download | mullvadvpn-6d7571d428c975f969f4bac18994734564b24c1e.tar.xz mullvadvpn-6d7571d428c975f969f4bac18994734564b24c1e.zip | |
Refactor tile state
Diffstat (limited to 'android/app/src')
| -rw-r--r-- | android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt index 0ced540e3e..7913ae9edf 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadTileService.kt @@ -5,12 +5,12 @@ import android.graphics.drawable.Icon import android.os.Build import android.service.quicksettings.Tile import android.service.quicksettings.TileService -import kotlin.properties.Delegates.observable import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.MainScope import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.ipc.ServiceConnection @@ -19,10 +19,6 @@ import net.mullvad.mullvadvpn.model.TunnelState import net.mullvad.talpid.tunnel.ActionAfterDisconnect class MullvadTileService : TileService() { - private var secured by observable(false) { _, _, _ -> - updateTileState() - } - private val scope = MainScope() private var listenerJob: Job? = null @@ -38,7 +34,7 @@ class MullvadTileService : TileService() { override fun onClick() { val intent = Intent(this, MullvadVpnService::class.java).apply { - action = if (secured) { + action = if (qsTile.state == Tile.STATE_ACTIVE) { MullvadVpnService.KEY_DISCONNECT_ACTION } else { MullvadVpnService.KEY_CONNECT_ACTION @@ -60,31 +56,44 @@ class MullvadTileService : TileService() { ServiceConnection(this@MullvadTileService, scope) .tunnelState .debounce(300L) - .collect { updateTunnelState(it.first, it.second) } + .map { (tunnelState, connectionState) -> mapToTileState(tunnelState, connectionState) } + .collect { + updateTileState(it) + } } - private fun updateTunnelState( + private fun mapToTileState( tunnelState: TunnelState, connectionState: ServiceResult.ConnectionState - ) { - secured = if (connectionState == ServiceResult.ConnectionState.CONNECTED) { + ): Int { + return if (connectionState == ServiceResult.ConnectionState.CONNECTED) { when (tunnelState) { - is TunnelState.Disconnected -> false - is TunnelState.Connecting -> true - is TunnelState.Connected -> true + is TunnelState.Disconnected -> Tile.STATE_INACTIVE + is TunnelState.Connecting -> Tile.STATE_ACTIVE + is TunnelState.Connected -> Tile.STATE_ACTIVE is TunnelState.Disconnecting -> { - tunnelState.actionAfterDisconnect == ActionAfterDisconnect.Reconnect + if (tunnelState.actionAfterDisconnect == ActionAfterDisconnect.Reconnect) { + Tile.STATE_ACTIVE + } else { + Tile.STATE_INACTIVE + } + } + is TunnelState.Error -> { + if (tunnelState.errorState.isBlocking) { + Tile.STATE_ACTIVE + } else { + Tile.STATE_INACTIVE + } } - is TunnelState.Error -> tunnelState.errorState.isBlocking } } else { - false + Tile.STATE_INACTIVE } } - private fun updateTileState() { + private fun updateTileState(newState: Int) { qsTile?.apply { - if (secured) { + if (newState == Tile.STATE_ACTIVE) { state = Tile.STATE_ACTIVE icon = securedIcon @@ -99,7 +108,6 @@ class MullvadTileService : TileService() { subtitle = resources.getText(R.string.unsecured) } } - updateTile() } } |
