From a6b35b2a87527e46ea2addc016941df53c3bdc02 Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Tue, 29 Jan 2019 14:02:52 +0100 Subject: [PATCH 1/3] removed obsolete code #451 --- .../notifications/MessageNotifier.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 066e0d748..3839c6acb 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -152,15 +152,6 @@ public class MessageNotifier { updateNotification(context, true, 0); } - /** - * @deprecated use updateNotificatio(Context, int) instead. - */ - @Deprecated - public static void updateNotification(@NonNull Context context, long threadId) - { - throw new IllegalStateException("old signal code called. thread -> chat, long -> int"); - } - public static void updateNotification(@NonNull Context context, int threadId) { if (System.currentTimeMillis() - lastDesktopActivityTimestamp < DESKTOP_ACTIVITY_PERIOD) { @@ -171,16 +162,6 @@ public class MessageNotifier { } } - /** - * @deprecated used updateNotification(Context, int, boolean) instead. - */ - @Deprecated - public static void updateNotification(@NonNull Context context, - long threadId, - boolean signal) { - throw new IllegalStateException("Old signal code called. thread -> chat & long -> int"); - } - public static void updateNotification(@NonNull Context context, int chatId, boolean signal) From 4e031e260cce6db21453741a238fbd765b6f7fa2 Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Tue, 29 Jan 2019 14:34:46 +0100 Subject: [PATCH 2/3] reducing the importance of notifications while a chat is displayed #451 --- .../securesms/ConversationActivity.java | 2 +- .../securesms/notifications/MessageNotifier.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 73b3abb9b..a0a6737ca 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -290,7 +290,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity @Override protected void onPause() { super.onPause(); - MessageNotifier.setVisibleThread(-1L); + MessageNotifier.setVisibleThread(MessageNotifier.NO_VISIBLE_THREAD); if (isFinishing()) overridePendingTransition(R.anim.fade_scale_in, R.anim.slide_to_right); quickAttachmentDrawer.onPause(); inputPanel.onPause(); diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 3839c6acb..06b5c8046 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -18,6 +18,7 @@ package org.thoughtcrime.securesms.notifications; import android.annotation.SuppressLint; import android.app.AlarmManager; +import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -33,6 +34,7 @@ import android.os.Build; import android.service.notification.StatusBarNotification; import android.support.annotation.NonNull; import android.support.v4.app.NotificationManagerCompat; +import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; @@ -74,6 +76,7 @@ public class MessageNotifier { static final String EXTRA_REMOTE_REPLY = "extra_remote_reply"; + public static final long NO_VISIBLE_THREAD = -1L; private static final int SUMMARY_NOTIFICATION_ID = 1338; private static final int PENDING_MESSAGES_ID = 1111; private static final String NOTIFICATION_GROUP = "messages"; @@ -186,6 +189,7 @@ public class MessageNotifier { } } + // @param signal: true to beep, false to stay silent. private static void updateNotification(@NonNull Context context, boolean signal, int reminderCount) @@ -243,6 +247,7 @@ public class MessageNotifier { Recipient recipient = notifications.get(0).getRecipient(); int notificationId = (SUMMARY_NOTIFICATION_ID + (bundled ? notifications.get(0).getChatId() : 0)); + setPriority(builder); builder.setThread(notifications.get(0).getRecipient()); builder.setMessageCount(notificationState.getMessageCount()); @@ -282,6 +287,14 @@ public class MessageNotifier { NotificationManagerCompat.from(context).notify(notificationId, builder.build()); } + private static void setPriority(AbstractNotificationBuilder builder) { + if(visibleThread == NO_VISIBLE_THREAD) { // not currently showing any thread + builder.setPriority(NotificationManager.IMPORTANCE_DEFAULT); + } else { // currently showing a different thread. + builder.setPriority(NotificationManager.IMPORTANCE_LOW); + } // if we were to show the current thread, this function would not be called in the first place. + } + private static void sendMultipleThreadNotification(@NonNull Context context, @NonNull NotificationState notificationState, boolean signal) @@ -289,6 +302,7 @@ public class MessageNotifier { MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context)); List notifications = notificationState.getNotifications(); + setPriority(builder); builder.setMessageCount(notificationState.getMessageCount(), notificationState.getThreadCount()); builder.setMostRecentSender(notifications.get(0).getIndividualRecipient()); builder.setGroup(NOTIFICATION_GROUP); From 3dd9a247ddbbeeb6de43669a4e515b38f7f24e58 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 1 Feb 2019 00:52:10 +0100 Subject: [PATCH 3/3] #451 hide incoming heads-up notifications as long as a thread is visible --- .../securesms/ConversationActivity.java | 7 ++-- .../notifications/MessageNotifier.java | 37 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index a0a6737ca..07ef60d50 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -79,7 +79,6 @@ import org.thoughtcrime.securesms.components.camera.QuickAttachmentDrawer; import org.thoughtcrime.securesms.components.camera.QuickAttachmentDrawer.AttachmentDrawerListener; import org.thoughtcrime.securesms.components.camera.QuickAttachmentDrawer.DrawerState; import org.thoughtcrime.securesms.components.emoji.EmojiDrawer; -import org.thoughtcrime.securesms.components.location.SignalPlace; import org.thoughtcrime.securesms.components.reminder.ReminderView; import org.thoughtcrime.securesms.connect.ApplicationDcContext; import org.thoughtcrime.securesms.connect.DcHelper; @@ -283,14 +282,14 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity titleView.setTitle(glideRequests, dcChat); - MessageNotifier.setVisibleThread(threadId); + MessageNotifier.updateVisibleThread(this, threadId); markThreadAsRead(); } @Override protected void onPause() { super.onPause(); - MessageNotifier.setVisibleThread(MessageNotifier.NO_VISIBLE_THREAD); + MessageNotifier.updateVisibleThread(this, MessageNotifier.NO_VISIBLE_THREAD); if (isFinishing()) overridePendingTransition(R.anim.fade_scale_in, R.anim.slide_to_right); quickAttachmentDrawer.onPause(); inputPanel.onPause(); @@ -988,7 +987,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity if (refreshFragment) { fragment.reload(recipient, threadId); - MessageNotifier.setVisibleThread(threadId); + MessageNotifier.updateVisibleThread(this, threadId); } fragment.scrollToBottom(); diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 06b5c8046..2eeea8923 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -18,7 +18,6 @@ package org.thoughtcrime.securesms.notifications; import android.annotation.SuppressLint; import android.app.AlarmManager; -import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -34,7 +33,6 @@ import android.os.Build; import android.service.notification.StatusBarNotification; import android.support.annotation.NonNull; import android.support.v4.app.NotificationManagerCompat; -import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; @@ -46,12 +44,14 @@ import org.thoughtcrime.securesms.connect.DcHelper; import org.thoughtcrime.securesms.connect.KeepAliveService; import org.thoughtcrime.securesms.mms.SlideDeck; import org.thoughtcrime.securesms.recipients.Recipient; +import org.thoughtcrime.securesms.util.Pair; import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.SpanUtil; import org.thoughtcrime.securesms.util.Prefs; import org.thoughtcrime.securesms.util.Util; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Set; @@ -88,8 +88,13 @@ public class MessageNotifier { private volatile static long lastAudibleNotification = -1; private static final CancelableExecutor executor = new CancelableExecutor(); - public static void setVisibleThread(long threadId) { + private static LinkedList> pendingNotifications = new LinkedList<>(); + + public static void updateVisibleThread(Context context, long threadId) { visibleThread = threadId; + if (visibleThread == NO_VISIBLE_THREAD && pendingNotifications.size() > 0) { + updatePendingNotifications(context); + } } public static void cancelDelayedNotifications() { @@ -166,24 +171,26 @@ public class MessageNotifier { } public static void updateNotification(@NonNull Context context, - int chatId, + int threadId, boolean signal) { - boolean isVisible = visibleThread == chatId; + boolean isVisible = visibleThread == threadId; ApplicationDcContext dcContext = DcHelper.getContext(context); if (isVisible) { - dcContext.marknoticedChat(chatId); + dcContext.marknoticedChat(threadId); } if (!Prefs.isNotificationsEnabled(context) || - Prefs.isChatMuted(context, chatId)) + Prefs.isChatMuted(context, threadId)) { return; } if (isVisible && signal) { - sendInThreadNotification(context, chatId); + sendInThreadNotification(context, threadId); + } else if (visibleThread != NO_VISIBLE_THREAD) { + pendingNotifications.push(new Pair<>(threadId, signal)); } else { updateNotification(context, signal, 0); } @@ -247,8 +254,6 @@ public class MessageNotifier { Recipient recipient = notifications.get(0).getRecipient(); int notificationId = (SUMMARY_NOTIFICATION_ID + (bundled ? notifications.get(0).getChatId() : 0)); - setPriority(builder); - builder.setThread(notifications.get(0).getRecipient()); builder.setMessageCount(notificationState.getMessageCount()); builder.setPrimaryMessageBody(recipient, notifications.get(0).getIndividualRecipient(), @@ -287,12 +292,11 @@ public class MessageNotifier { NotificationManagerCompat.from(context).notify(notificationId, builder.build()); } - private static void setPriority(AbstractNotificationBuilder builder) { - if(visibleThread == NO_VISIBLE_THREAD) { // not currently showing any thread - builder.setPriority(NotificationManager.IMPORTANCE_DEFAULT); - } else { // currently showing a different thread. - builder.setPriority(NotificationManager.IMPORTANCE_LOW); - } // if we were to show the current thread, this function would not be called in the first place. + private static void updatePendingNotifications(Context context) { + while (pendingNotifications.size() > 0) { + Pair threadSignalPair = pendingNotifications.pop(); + updateNotification(context, threadSignalPair.first(), threadSignalPair.second()); + } } private static void sendMultipleThreadNotification(@NonNull Context context, @@ -302,7 +306,6 @@ public class MessageNotifier { MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context)); List notifications = notificationState.getNotifications(); - setPriority(builder); builder.setMessageCount(notificationState.getMessageCount(), notificationState.getThreadCount()); builder.setMostRecentSender(notifications.get(0).getIndividualRecipient()); builder.setGroup(NOTIFICATION_GROUP);