diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index f39b64bf7..5eb9efc7f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -50,6 +50,7 @@ import android.support.v7.graphics.Palette; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; import java.util.List; import java.util.Objects; @@ -86,6 +87,8 @@ public class NotificationListener extends NotificationListenerService { private LimitedQueue mActionLookup = new LimitedQueue(16); + private HashMap notificationTimes = new HashMap<>(); + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override @@ -191,6 +194,8 @@ public class NotificationListener extends NotificationListenerService { if (shouldIgnore(sbn)) return; + Prefs prefs = GBApplication.getPrefs(); + switch (GBApplication.getGrantedInterruptionFilter()) { case NotificationManager.INTERRUPTION_FILTER_ALL: break; @@ -265,6 +270,18 @@ public class NotificationListener extends NotificationListenerService { return; } + // Ignore too frequent notifications, according to user preference + long min_timeout = prefs.getInt("notifications_timeout", 0) * 1000; + Long cur_time = System.currentTimeMillis(); + if (notificationTimes.containsKey(source)) { + Long last_time = notificationTimes.get(source); + if (cur_time - last_time < min_timeout) { + LOG.info("Ignoring frequent notification, last one was " + (cur_time - last_time) + "ms ago"); + return; + } + } + notificationTimes.put(source, cur_time); + GBApplication.deviceService().onNotification(notificationSpec); } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 35a6381d6..39dfa2fc0 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -387,4 +387,28 @@ 1800 3600 + + + @string/no_limit + @string/seconds_5 + @string/seconds_10 + @string/seconds_20 + @string/seconds_30 + @string/minutes_1 + @string/minutes_5 + @string/minutes_10 + @string/minutes_30 + + + 0 + 5 + 10 + 20 + 30 + 60 + 300 + 600 + 1800 + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b0e9ca866..66e48cfc7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -94,6 +94,7 @@ Repetitions Phone Calls SMS + Minimum time between notifications Pebble Messages Support for apps that send notifications to the Pebble via PebbleKit. Generic notification support @@ -368,6 +369,15 @@ Heart rate Battery + No limit + 5 seconds + 10 seconds + 20 seconds + 30 seconds + 1 minute + 5 minutes + 10 minutes + 30 minutes Live activity Steps today, target: %1$s diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index f63e066af..6744c4e4b 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -155,6 +155,14 @@ android:key="notifications_generic_whenscreenon" android:title="@string/pref_title_whenscreenon" /> + +