diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-02-06 14:13:20 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-02-12 14:29:13 +0000 |
| commit | f49915a6b02e9044765103c853823d1dcea18ff4 (patch) | |
| tree | 38c6ad4ae936da62b24a5f7f324fdc800eaa76b3 /android | |
| parent | fe7a7a41310349860cb33e636273dc6eda82deba (diff) | |
| download | mullvadvpn-f49915a6b02e9044765103c853823d1dcea18ff4.tar.xz mullvadvpn-f49915a6b02e9044765103c853823d1dcea18ff4.zip | |
Listen for service events in notification manager
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt | 31 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt | 2 |
2 files changed, 28 insertions, 5 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt index 34ccb59bfd..b8839811e1 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -16,6 +16,7 @@ import net.mullvad.mullvadvpn.dataproxy.ConnectionProxy import net.mullvad.mullvadvpn.model.TunnelState import net.mullvad.mullvadvpn.ui.MainActivity import net.mullvad.talpid.tunnel.ActionAfterDisconnect +import net.mullvad.talpid.util.EventNotifier val CHANNEL_ID = "vpn_tunnel_status" val FOREGROUND_NOTIFICATION_ID: Int = 1 @@ -23,16 +24,38 @@ val KEY_CONNECT_ACTION = "connect_action" val KEY_DISCONNECT_ACTION = "disconnect_action" val PERMISSION_TUNNEL_ACTION = "net.mullvad.mullvadvpn.permission.TUNNEL_ACTION" -class ForegroundNotificationManager(val service: Service, val connectionProxy: ConnectionProxy) { +class ForegroundNotificationManager( + val service: MullvadVpnService, + val serviceNotifier: EventNotifier<ServiceInstance?> +) { private val notificationManager = service.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - private val listenerId = connectionProxy.onStateChange.subscribe { state -> - tunnelState = state + private val listenerId = serviceNotifier.subscribe { newServiceInstance -> + serviceInstance = newServiceInstance } + private var serviceInstance: ServiceInstance? = null + set(value) { + synchronized(this) { + if (value != null) { + connectionListenerId = value.connectionProxy.onStateChange.subscribe { state -> + tunnelState = state + } + } else { + connectionListenerId?.let { listenerId -> + field?.connectionProxy?.onStateChange?.unsubscribe(listenerId) + } + } + + field = value + } + } + private val badgeColor = service.resources.getColor(R.color.colorPrimary) + private var connectionListenerId: Int? = null + private var onForeground = false private var reconnecting = false private var showingReconnecting = false @@ -172,7 +195,7 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C } fun onDestroy() { - connectionProxy.onStateChange.unsubscribe(listenerId) + serviceNotifier.unsubscribe(listenerId) service.apply { unregisterReceiver(connectReceiver) 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 1623400336..37c728a391 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -120,7 +120,7 @@ class MullvadVpnService : TalpidVpnService() { } private fun startNotificationManager(): ForegroundNotificationManager { - return ForegroundNotificationManager(this, connectionProxy).apply { + return ForegroundNotificationManager(this, serviceNotifier).apply { onConnect = { connectionProxy.connect() } onDisconnect = { connectionProxy.disconnect() } lockedToForeground = isBound |
