diff options
| author | David Göransson <david.goransson@mullvad.net> | 2024-08-30 10:47:35 +0200 |
|---|---|---|
| committer | David Göransson <david.goransson@mullvad.net> | 2024-08-30 14:04:10 +0200 |
| commit | d3db0402cbeb2b6c866487d5dfafd79ca2bf002b (patch) | |
| tree | ddb46379a642c09da2b39d4663c034fd3aa2c1f7 /android/service/src | |
| parent | b0c1bcc5b49aa83c80dc125980f8ab745008397e (diff) | |
| download | mullvadvpn-d3db0402cbeb2b6c866487d5dfafd79ca2bf002b.tar.xz mullvadvpn-d3db0402cbeb2b6c866487d5dfafd79ca2bf002b.zip | |
Add notification throttling
Diffstat (limited to 'android/service/src')
| -rw-r--r-- | android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationManager.kt | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationManager.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationManager.kt index 3d58e571fd..ed2d2f053a 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationManager.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/NotificationManager.kt @@ -5,7 +5,10 @@ import android.content.Context import android.content.pm.PackageManager import androidx.core.app.ActivityCompat import androidx.core.app.NotificationManagerCompat +import kotlin.time.Duration.Companion.milliseconds import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.merge import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.lib.model.Notification @@ -13,6 +16,7 @@ import net.mullvad.mullvadvpn.lib.model.NotificationUpdate import net.mullvad.mullvadvpn.service.notifications.accountexpiry.toNotification import net.mullvad.mullvadvpn.service.notifications.tunnelstate.toNotification +@OptIn(FlowPreview::class) class NotificationManager( private val notificationManagerCompat: NotificationManagerCompat, notificationProviders: List<NotificationProvider<Notification>>, @@ -23,7 +27,7 @@ class NotificationManager( init { scope.launch { notificationProviders - .map { it.notifications } + .map { it.notifications.debounce(NOTIFICATION_DEBOUNCE) } .merge() .collect { notificationUpdate -> when (notificationUpdate) { @@ -31,6 +35,7 @@ class NotificationManager( notificationManagerCompat.cancel( notificationUpdate.notificationId.value ) + is NotificationUpdate.Notify -> { val notification = notificationUpdate.value val androidNotification = notification.toAndroidNotification(context) @@ -56,4 +61,11 @@ class NotificationManager( is Notification.Tunnel -> toNotification(context) is Notification.AccountExpiry -> toNotification(context) } + + companion object { + // According to testing we are only allowed to send 5 notifications per second at most, + // otherwise the system will start dropping them. To ensure we don't drop the latest + // notification debounce if we spam too much. + val NOTIFICATION_DEBOUNCE = 200.milliseconds + } } |
