summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-01-30 19:19:49 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-01-31 19:33:42 +0000
commit3a0963e2c7253c2bcf0e302b766e7e95106890b2 (patch)
treec7862fe3e952c47371f6ce68089903b908653f98 /android
parent6051bd870dcbdc3874607dcb1b6a9ad6ae4b7a85 (diff)
downloadmullvadvpn-3a0963e2c7253c2bcf0e302b766e7e95106890b2.tar.xz
mullvadvpn-3a0963e2c7253c2bcf0e302b766e7e95106890b2.zip
Remove service from foreground when disconnected
Diffstat (limited to 'android')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/ForegroundNotificationManager.kt24
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 {