diff options
Diffstat (limited to 'android/src/main')
3 files changed, 75 insertions, 0 deletions
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 cdfdbac287..49a58662d1 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -9,6 +9,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.dataproxy.ConnectionProxy +import net.mullvad.mullvadvpn.service.tunnelstate.TunnelStateUpdater import net.mullvad.talpid.TalpidVpnService import net.mullvad.talpid.util.EventNotifier @@ -29,6 +30,7 @@ class MullvadVpnService : TalpidVpnService() { private var startDaemonJob: Job? = null private lateinit var notificationManager: ForegroundNotificationManager + private lateinit var tunnelStateUpdater: TunnelStateUpdater var shouldConnect = false set(value) { @@ -56,7 +58,10 @@ class MullvadVpnService : TalpidVpnService() { override fun onCreate() { super.onCreate() + notificationManager = ForegroundNotificationManager(this, serviceNotifier) + tunnelStateUpdater = TunnelStateUpdater(this, serviceNotifier) + setUp() } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/tunnelstate/Persistence.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/tunnelstate/Persistence.kt new file mode 100644 index 0000000000..c3d3be36c0 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/tunnelstate/Persistence.kt @@ -0,0 +1,41 @@ +package net.mullvad.mullvadvpn.service.tunnelstate + +import android.content.Context +import java.net.InetSocketAddress +import net.mullvad.mullvadvpn.model.TunnelState +import net.mullvad.talpid.net.Endpoint +import net.mullvad.talpid.net.TransportProtocol +import net.mullvad.talpid.net.TunnelEndpoint + +private const val SHARED_PREFERENCES = "tunnel_state" +private const val KEY_TUNNEL_STATE = "tunnel_state" + +// TODO: Maybe replace using this with actually persisting the endpoint information +private val dummyTunnelEndpoint = TunnelEndpoint(Endpoint( + InetSocketAddress.createUnresolved("dummy", 53), + TransportProtocol.Tcp +)) + +internal class Persistence(context: Context) { + val sharedPreferences = + context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE) + + var state + get() = loadState() + set(value) { + persistState(value) + } + + private fun loadState(): TunnelState { + val description = sharedPreferences.getString(KEY_TUNNEL_STATE, TunnelState.DISCONNECTED)!! + + return TunnelState.fromString(description, dummyTunnelEndpoint) + } + + private fun persistState(state: TunnelState) { + sharedPreferences + .edit() + .putString(KEY_TUNNEL_STATE, state.toString()) + .commit() + } +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/tunnelstate/TunnelStateUpdater.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/tunnelstate/TunnelStateUpdater.kt new file mode 100644 index 0000000000..133c81506e --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/tunnelstate/TunnelStateUpdater.kt @@ -0,0 +1,29 @@ +package net.mullvad.mullvadvpn.service.tunnelstate + +import android.content.Context +import net.mullvad.mullvadvpn.dataproxy.ConnectionProxy +import net.mullvad.mullvadvpn.service.ServiceInstance +import net.mullvad.talpid.util.EventNotifier + +class TunnelStateUpdater(context: Context, serviceNotifier: EventNotifier<ServiceInstance?>) { + private val persistence = Persistence(context) + + private var connectionProxy: ConnectionProxy? = null + private var stateSubscriptionId: Int? = null + + init { + serviceNotifier.subscribe { serviceInstance -> + onNewServiceInstance(serviceInstance) + } + } + + private fun onNewServiceInstance(serviceInstance: ServiceInstance?) { + stateSubscriptionId?.let { id -> connectionProxy?.onStateChange?.unsubscribe(id) } + + connectionProxy = serviceInstance?.connectionProxy?.apply { + stateSubscriptionId = onStateChange.subscribe { newState -> + persistence.state = newState + } + } + } +} |
