From de9874fdf7c83be6e3cc572d9507ab2171cbe5fb Mon Sep 17 00:00:00 2001 From: kosyak Date: Sat, 18 May 2024 13:17:08 +0200 Subject: [PATCH] notification sound throttling setting --- .../services/NotificationService.java | 45 ++++++++++++++++++- src/main/res/values/arrays.xml | 27 +++++++++++ src/main/res/values/strings.xml | 2 + src/main/res/xml/preferences.xml | 7 +++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 0742215d1..2de48f338 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -28,6 +28,7 @@ import android.text.style.StyleSpan; import android.util.DisplayMetrics; import android.util.Log; +import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat.BigPictureStyle; @@ -61,6 +62,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -120,6 +122,8 @@ public class NotificationService { private Ringtone currentlyPlayingRingtone = null; private ScheduledFuture vibrationFuture; + private SharedPreferences lastNotificationTimeByConversation = null; + NotificationService(final XmppConnectionService service) { this.mXmppConnectionService = service; } @@ -390,6 +394,7 @@ public class NotificationService { conversations = getBacklogConversations(account); count = getBacklogMessageCount(account); } + updateNotification(count > 0, conversations); } } @@ -727,8 +732,13 @@ public class NotificationService { final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || isScreenLocked) && !account.inGracePeriod() - && !this.inMiniGracePeriod(account); + && !this.inMiniGracePeriod(account) && + !shouldNotificationSoundBeThrottled(conversation); + updateNotification(doNotify, Collections.singletonList(conversation.getUuid())); + + ensureLastNotificationTimeByConversation() + .edit().putLong(conversation.getUuid(), System.currentTimeMillis()).apply(); } } @@ -930,6 +940,39 @@ public class NotificationService { } } + private boolean shouldNotificationSoundBeThrottled(@Nullable Conversational conversation) { + if (conversation == null) { + return false; + } + + long throttlingPeriod = mXmppConnectionService.getLongPreference("notification_throttling_period", 0); + + if (throttlingPeriod <= 0) { + return false; + } + + long lastNotificationTimestamp = ensureLastNotificationTimeByConversation().getLong(conversation.getUuid(), 0L); + + return (System.currentTimeMillis() - lastNotificationTimestamp) > throttlingPeriod; + } + + private SharedPreferences ensureLastNotificationTimeByConversation() { + if (lastNotificationTimeByConversation == null) { + lastNotificationTimeByConversation = mXmppConnectionService.getApplicationContext().getSharedPreferences("lastNotificationTimeByConversation", Context.MODE_PRIVATE); + + Map allValues = lastNotificationTimeByConversation.getAll(); + + Long now = System.currentTimeMillis(); + SharedPreferences.Editor editor = lastNotificationTimeByConversation.edit(); + allValues.forEach((BiConsumer) (s, o) -> { + if (o instanceof Long && (now - (Long) o) > 3600000) { + editor.remove(s); + } + }); + editor.apply(); + } + } + private void modifyForSoundVibrationAndLight( Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences) { final Resources resources = mXmppConnectionService.getResources(); diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index 64865db75..1037bae40 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -26,6 +26,33 @@ -1 + + 0 + 5000 + 15000 + 30000 + 60000 + 120000 + 180000 + 300000 + 600000 + 900000 + 1800000 + + + @string/never + 5 sec + 15 sec + 30 sec + 1 min + 2 min + 3 min + 5 min + 10 min + 15 min + 30 min + + @string/none @string/recently_used diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 27bc5b5e5..3b943362f 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -297,6 +297,8 @@ Enable quiet hours Notifications will be silenced during quiet hours Other + Throttle noisy notifications + Throttle sound and vibration for notifications received from one conversation during short period of time Synchronize bookmarks Set “autojoin” flag when entering or leaving a MUC and react to modifications made by other clients. OMEMO fingerprint copied to clipboard diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index fcf2fbb0c..8cad42374 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -79,6 +79,13 @@ android:key="grace_period_length" android:summary="@string/pref_notification_grace_period_summary" android:title="@string/pref_notification_grace_period" /> +