diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-01-31 16:45:38 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-01-31 16:45:38 -0300 |
| commit | 787dd817cca2c66b57f0b4494c701cd994c78513 (patch) | |
| tree | 0ab2f8a3973a96b5d2968fa23a49aa77fd48dde3 /android | |
| parent | 6051bd870dcbdc3874607dcb1b6a9ad6ae4b7a85 (diff) | |
| parent | d40999a2be51bf4a58daf6df019ea1bc460fa31e (diff) | |
| download | mullvadvpn-787dd817cca2c66b57f0b4494c701cd994c78513.tar.xz mullvadvpn-787dd817cca2c66b57f0b4494c701cd994c78513.zip | |
Merge branch 'allow-notification-to-be-dismissed'
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 | 22 |
2 files changed, 49 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..0a9a48e3fa 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() = lockedToForeground || !(tunnelState is TunnelState.Disconnected) + private val notificationText: Int get() { val state = tunnelState @@ -138,12 +142,19 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C var onConnect: (() -> Unit)? = null var onDisconnect: (() -> Unit)? = null + var loggedIn = false set(value) { field = value updateNotification() } + var lockedToForeground = false + set(value) { + field = value + updateNotificationForegroundStatus() + } + init { if (Build.VERSION.SDK_INT >= 26) { initChannel() @@ -155,9 +166,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 +177,6 @@ class ForegroundNotificationManager(val service: Service, val connectionProxy: C service.apply { unregisterReceiver(connectReceiver) unregisterReceiver(disconnectReceiver) - - stopForeground(true) } notificationManager.cancel(FOREGROUND_NOTIFICATION_ID) @@ -188,6 +197,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 { 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 db3cbfb7ec..4c24ad62e2 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -24,16 +24,35 @@ class MullvadVpnService : TalpidVpnService() { private var serviceNotifier = EventNotifier<ServiceInstance?>(null) + private var bindCount = 0 + set(value) { + field = value + isBound = bindCount != 0 + } + + private var isBound = false + set(value) { + field = value + + if (this::notificationManager.isInitialized) { + notificationManager.lockedToForeground = value + } + } + override fun onCreate() { super.onCreate() setUp() } override fun onBind(intent: Intent): IBinder { + bindCount += 1 + return super.onBind(intent) ?: binder } override fun onRebind(intent: Intent) { + bindCount += 1 + if (isStopping) { restart() isStopping = false @@ -45,6 +64,8 @@ class MullvadVpnService : TalpidVpnService() { } override fun onUnbind(intent: Intent): Boolean { + bindCount -= 1 + return true } @@ -102,6 +123,7 @@ class MullvadVpnService : TalpidVpnService() { return ForegroundNotificationManager(this, connectionProxy).apply { onConnect = { connectionProxy.connect() } onDisconnect = { connectionProxy.disconnect() } + lockedToForeground = isBound } } |
