From 7b8a61e068385207723a5cd865741b4f633cab4d Mon Sep 17 00:00:00 2001 From: kosyak Date: Wed, 22 May 2024 21:29:23 +0200 Subject: [PATCH] per contact notifications throttling settings --- .../conversations/entities/Conversation.java | 10 ++++++ .../services/NotificationService.java | 8 ++++- .../ui/ConversationFragment.java | 34 +++++++++++++++++++ src/main/res/menu/conversations.xml | 8 +++++ src/main/res/menu/fragment_conversation.xml | 10 ++++-- src/main/res/values/arrays.xml | 19 ++--------- src/main/res/xml/preferences.xml | 7 ---- 7 files changed, 70 insertions(+), 26 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 8ab8b1769..7639eaf68 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -154,6 +154,8 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl public static final String NEXT_COUNTERPART = "next_counterpart"; public static final String ATTRIBUTE_MUTED_TILL = "muted_till"; + + public static final String ATTRIBUTE_NOTIFICATIONS_THROTTLING_PERIOD = "notifications_throlttling_period"; public static final String ATTRIBUTE_ALWAYS_NOTIFY = "always_notify"; public static final String ATTRIBUTE_LAST_CLEAR_HISTORY = "last_clear_history"; public static final String ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS = "formerly_private_non_anonymous"; @@ -1108,6 +1110,14 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl return System.currentTimeMillis() < this.getLongAttribute(ATTRIBUTE_MUTED_TILL, 0); } + public void setNotificationThrottlingPeriod(long value) { + this.setAttribute(ATTRIBUTE_NOTIFICATIONS_THROTTLING_PERIOD, String.valueOf(value)); + } + + public long getNotificationThrottlingPeriod() { + return this.getLongAttribute(ATTRIBUTE_NOTIFICATIONS_THROTTLING_PERIOD, -1); + } + public boolean alwaysNotify() { return mode == MODE_SINGLE || getBooleanAttribute(ATTRIBUTE_ALWAYS_NOTIFY, Config.ALWAYS_NOTIFY_BY_DEFAULT || isPrivateAndNonAnonymous()); } diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 6ee478939..9df0c09ad 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -961,7 +961,13 @@ public class NotificationService { return false; } - long throttlingPeriod = mXmppConnectionService.getLongPreference("notification_throttling_period", R.integer.default_notification_throttling_period); + Conversation c = mXmppConnectionService.findConversationByUuid(conversationUuid); + + if (c == null) { + return false; + } + + long throttlingPeriod = c.getNotificationThrottlingPeriod(); if (throttlingPeriod <= 0) { return false; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index e5fef0d2e..119420596 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -2023,6 +2023,9 @@ public class ConversationFragment extends XmppFragment case R.id.action_unmute: unMuteConversation(conversation); break; + case R.id.action_throttle: + throttleNoisyNoftificationsDialog(conversation); + break; case R.id.action_block: case R.id.action_unblock: final Activity activity = getActivity(); @@ -2513,6 +2516,37 @@ public class ConversationFragment extends XmppFragment builder.create().show(); } + protected void throttleNoisyNoftificationsDialog(final Conversation conversation) { + final AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(R.string.pref_noisy_notifications_throttling); + final int[] durations = activity.getResources().getIntArray(R.array.notification_throttling_periods_values); + final CharSequence[] labels = new CharSequence[durations.length]; + int checkedIndex = -1; + long period = conversation.getNotificationThrottlingPeriod(); + for (int i = 0; i < durations.length; ++i) { + if (period == durations[i]) { + checkedIndex = i; + } + + if (durations[i] == -1) { + labels[i] = activity.getString(R.string.never); + } else { + labels[i] = TimeFrameUtils.resolve(activity, durations[i]); + } + } + builder.setSingleChoiceItems( + labels, + checkedIndex, + (dialog, which) -> { + conversation.setNotificationThrottlingPeriod(durations[which]); + activity.xmppConnectionService.updateConversation(conversation); + activity.onConversationsListItemUpdated(); + refresh(); + activity.invalidateOptionsMenu(); + }); + builder.create().show(); + } + private boolean hasPermissions(int requestCode, List permissions) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { final List missingPermissions = new ArrayList<>(); diff --git a/src/main/res/menu/conversations.xml b/src/main/res/menu/conversations.xml index 445c5d539..93fb34819 100644 --- a/src/main/res/menu/conversations.xml +++ b/src/main/res/menu/conversations.xml @@ -11,11 +11,19 @@ android:orderInCategory="20" android:title="@string/disable_notifications" app:showAsAction="never" /> + + + + + + diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index 1037bae40..b09030556 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -26,8 +26,8 @@ -1 - - 0 + + -1 5000 15000 30000 @@ -38,20 +38,7 @@ 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 diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index 8570cd1d7..fcf2fbb0c 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -79,13 +79,6 @@ android:key="grace_period_length" android:summary="@string/pref_notification_grace_period_summary" android:title="@string/pref_notification_grace_period" /> -