diff options
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt | 24 |
1 files changed, 20 insertions, 4 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 29c6681cd1..f73a64d849 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt @@ -33,6 +33,7 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C private val badgeColor = service.resources.getColor(R.color.colorPrimary) + private var onForeground = false private var reconnecting = false private var showingReconnecting = false @@ -48,6 +49,9 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C updateNotification() } + private val shouldBeOnForeground + get() = !(tunnelState is TunnelState.Disconnected) + private val notificationText: Int get() { val state = tunnelState @@ -155,9 +159,9 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C registerReceiver(connectReceiver, connectFilter, PERMISSION_TUNNEL_ACTION, null) registerReceiver(disconnectReceiver, disconnectFilter, PERMISSION_TUNNEL_ACTION, null) - - startForeground(FOREGROUND_NOTIFICATION_ID, buildNotification()) } + + updateNotification() } fun onDestroy() { @@ -166,8 +170,6 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C service.apply { unregisterReceiver(connectReceiver) unregisterReceiver(disconnectReceiver) - - stopForeground(true) } notificationManager.cancel(FOREGROUND_NOTIFICATION_ID) @@ -188,6 +190,20 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C if (!reconnecting || !showingReconnecting) { notificationManager.notify(FOREGROUND_NOTIFICATION_ID, buildNotification()) } + + updateNotificationForegroundStatus() + } + + private fun updateNotificationForegroundStatus() { + if (shouldBeOnForeground != onForeground) { + if (shouldBeOnForeground) { + service.startForeground(FOREGROUND_NOTIFICATION_ID, buildNotification()) + onForeground = true + } else if (!shouldBeOnForeground) { + service.stopForeground(Service.STOP_FOREGROUND_DETACH) + onForeground = false + } + } } private fun buildNotification(): Notification { |
