diff options
author | cyBerta <cyberta@riseup.net> | 2021-11-09 23:35:21 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2021-11-09 23:35:21 +0100 |
commit | c38240b5776cc2e5b1057652787367ffb80eb5a1 (patch) | |
tree | 8e03fac48af134c930c3b6a1f51a97829e6c6d7a /app/src | |
parent | 7cd76c3eb08f89be94642a57eca7d929d8d72c6d (diff) |
debounce tor notifications to max. 2 per second
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 507de4ae..3f3fbf4f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -30,6 +30,9 @@ import se.leap.bitmaskclient.R; public class TorNotificationManager { public final static int TOR_SERVICE_NOTIFICATION_ID = 10; static final String NOTIFICATION_CHANNEL_NEWSTATUS_ID = "bitmask_tor_service_news"; + private long lastNotificationTime = 0; + // debounce timeout in milliseconds + private final static long NOTIFICATION_DEBOUNCE_TIME = 500; public TorNotificationManager() {} @@ -48,6 +51,9 @@ public class TorNotificationManager { } public void buildTorNotification(Context context, String state, String message, int progress) { + if (shouldDropNotification()) { + return; + } NotificationManager notificationManager = initNotificationManager(context); if (notificationManager == null) { return; @@ -69,6 +75,15 @@ public class TorNotificationManager { notificationManager.notify(TOR_SERVICE_NOTIFICATION_ID, notificationBuilder.build()); } + private boolean shouldDropNotification() { + long now = System.currentTimeMillis(); + if (now - lastNotificationTime < NOTIFICATION_DEBOUNCE_TIME) { + return true; + } + lastNotificationTime = now; + return false; + } + private static NotificationManager initNotificationManager(Context context) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |