From 7306e0286f63f099dc223fea054fc397a7c15146 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Mar 2017 10:50:46 +0100 Subject: [PATCH] do not notify for messages from strangers by default --- .../conversations/entities/Conversation.java | 20 +++++++++++++++++++ .../services/NotificationService.java | 12 ++++++++--- src/main/res/values/defaults.xml | 1 + src/main/res/values/strings.xml | 2 ++ src/main/res/xml/preferences.xml | 6 ++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 0290c9710..f8b354915 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -992,6 +992,26 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl } } + private int sentMessagesCount() { + int count = 0; + synchronized (this.messages) { + for(Message message : messages) { + if (message.getStatus() != Message.STATUS_RECEIVED) { + ++count; + } + } + } + return count; + } + + public boolean isWithStranger() { + if (mode == MODE_SINGLE) { + return !getContact().mutualPresenceSubscription() && sentMessagesCount() == 0; + } else { + return false; + } + } + public class Smp { public static final int STATUS_NONE = 0; public static final int STATUS_CONTACT_REQUESTED = 1; diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 7302afb96..c83136d3a 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -59,17 +59,23 @@ public class NotificationService { } public boolean notify(final Message message) { - return (message.getStatus() == Message.STATUS_RECEIVED) + return message.getStatus() == Message.STATUS_RECEIVED && notificationsEnabled() && !message.getConversation().isMuted() - && (message.getConversation().alwaysNotify() || wasHighlightedOrPrivate(message) - ); + && (message.getConversation().alwaysNotify() || wasHighlightedOrPrivate(message)) + && (!message.getConversation().isWithStranger() || notificationsFromStrangers()) + ; } public boolean notificationsEnabled() { return mXmppConnectionService.getPreferences().getBoolean("show_notification", true); } + private boolean notificationsFromStrangers() { + return mXmppConnectionService.getPreferences().getBoolean("notifications_from_strangers", + mXmppConnectionService.getResources().getBoolean(R.bool.notifications_from_strangers)); + } + public boolean isQuietHours() { if (!mXmppConnectionService.getPreferences().getBoolean("enable_quiet_hours", false)) { return false; diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index eea700694..66dfdf347 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -3,4 +3,5 @@ Phone true false + false \ No newline at end of file diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 0fd356da7..826d02e03 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -736,4 +736,6 @@ Compressing video (%s%% completed) Corresponding conversations closed. Contact blocked. + Notifications from strangers + Notify for messages received from strangers. diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index 3d98dedef..684b98136 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -41,6 +41,12 @@ android:key="show_notification" android:summary="@string/pref_notifications_summary" android:title="@string/pref_notifications"/> +