From c38240b5776cc2e5b1057652787367ffb80eb5a1 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 9 Nov 2021 23:35:21 +0100 Subject: debounce tor notifications to max. 2 per second --- .../se/leap/bitmaskclient/tor/TorNotificationManager.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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); -- cgit v1.2.3