notification sound throttling setting

This commit is contained in:
kosyak 2024-05-18 13:17:08 +02:00
parent 3db7413bf2
commit de9874fdf7
4 changed files with 80 additions and 1 deletions

View file

@ -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<String, ?> allValues = lastNotificationTimeByConversation.getAll();
Long now = System.currentTimeMillis();
SharedPreferences.Editor editor = lastNotificationTimeByConversation.edit();
allValues.forEach((BiConsumer<String, Object>) (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();

View file

@ -26,6 +26,33 @@
<item>-1</item>
</integer-array>
<string-array name="notification_throttling_periods_values">
<item>0</item>
<item>5000</item>
<item>15000</item>
<item>30000</item>
<item>60000</item>
<item>120000</item>
<item>180000</item>
<item>300000</item>
<item>600000</item>
<item>900000</item>
<item>1800000</item>
</string-array>
<string-array name="notification_throttling_periods">
<item>@string/never</item>
<item>5 sec</item>
<item>15 sec</item>
<item>30 sec</item>
<item>1 min</item>
<item>2 min</item>
<item>3 min</item>
<item>5 min</item>
<item>10 min</item>
<item>15 min</item>
<item>30 min</item>
</string-array>
<string-array name="quick_actions">
<item>@string/none</item>
<item>@string/recently_used</item>

View file

@ -297,6 +297,8 @@
<string name="title_pref_enable_quiet_hours">Enable quiet hours</string>
<string name="pref_quiet_hours_summary">Notifications will be silenced during quiet hours</string>
<string name="pref_expert_options_other">Other</string>
<string name="pref_noisy_notifications_throttling">Throttle noisy notifications</string>
<string name="pref_noisy_notifications_throttling_summary">Throttle sound and vibration for notifications received from one conversation during short period of time</string>
<string name="pref_autojoin">Synchronize bookmarks</string>
<string name="pref_autojoin_summary">Set “autojoin” flag when entering or leaving a MUC and react to modifications made by other clients.</string>
<string name="toast_message_omemo_fingerprint">OMEMO fingerprint copied to clipboard</string>

View file

@ -79,6 +79,13 @@
android:key="grace_period_length"
android:summary="@string/pref_notification_grace_period_summary"
android:title="@string/pref_notification_grace_period" />
<ListPreference
android:defaultValue="0"
android:entries="@array/notification_throttling_periods"
android:entryValues="@array/notification_throttling_periods_values"
android:key="notification_throttling_period"
android:summary="@string/pref_noisy_notifications_throttling_summary"
android:title="@string/pref_noisy_notifications_throttling" />
<PreferenceScreen
android:key="quiet_hours"
android:summary="@string/pref_quiet_hours_summary"