From 4d19a59cb6764ef76f8e568a167f80ce83e72b78 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Fri, 29 Nov 2024 20:10:49 +0100 Subject: [PATCH] improve notifications handling --- .../securesms/WebxdcActivity.java | 12 +++++ .../notifications/NotificationCenter.java | 45 +++++++++++++++---- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/thoughtcrime/securesms/WebxdcActivity.java b/src/main/java/org/thoughtcrime/securesms/WebxdcActivity.java index 75219ad42..28b4e225d 100644 --- a/src/main/java/org/thoughtcrime/securesms/WebxdcActivity.java +++ b/src/main/java/org/thoughtcrime/securesms/WebxdcActivity.java @@ -234,6 +234,18 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE }); } + @Override + public void onResume() { + super.onResume(); + DcHelper.getNotificationCenter(this).updateVisibleWebxdc(dcContext.getAccountId(), dcAppMsg.getId()); + } + + @Override + protected void onPause() { + super.onPause(); + DcHelper.getNotificationCenter(this).clearVisibleWebxdc(); + } + @Override protected void onDestroy() { DcHelper.getEventCenter(this.getApplicationContext()).removeObservers(this); diff --git a/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java b/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java index 5a162ee2a..e9b893058 100644 --- a/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java +++ b/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java @@ -44,6 +44,7 @@ import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.IntentUtils; import org.thoughtcrime.securesms.util.JsonUtils; +import org.thoughtcrime.securesms.util.Pair; import org.thoughtcrime.securesms.util.Prefs; import org.thoughtcrime.securesms.util.Util; @@ -58,6 +59,7 @@ public class NotificationCenter { private static final String TAG = NotificationCenter.class.getSimpleName(); @NonNull private final ApplicationContext context; private volatile ChatData visibleChat = null; + private volatile Pair visibleWebxdc = null; private volatile long lastAudibleNotification = 0; private static final long MIN_AUDIBLE_PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(2); @@ -358,7 +360,10 @@ public class NotificationCenter { } } - maybeAddNotification(accountId, dcChat, msgId, shortLine, tickerLine, true); + DcMsg quotedMsg = dcMsg.getQuotedMsg(); + boolean isMention = quotedMsg != null && dcChat.isMultiUser() && quotedMsg.isOutgoing(); + + maybeAddNotification(accountId, dcChat, msgId, shortLine, tickerLine, true, isMention); }); } @@ -374,7 +379,7 @@ public class NotificationCenter { DcContact sender = dcContext.getContact(contactId); String shortLine = context.getString(R.string.reaction_by_other, sender.getDisplayName(), reaction, dcMsg.getSummarytext(2000)); - maybeAddNotification(accountId, dcContext.getChat(dcMsg.getChatId()), msgId, shortLine, shortLine, false); + maybeAddNotification(accountId, dcContext.getChat(dcMsg.getChatId()), msgId, shortLine, shortLine, false, true); }); } @@ -387,26 +392,32 @@ public class NotificationCenter { DcContext dcContext = context.dcAccounts.getAccount(accountId); DcMsg dcMsg = dcContext.getMsg(msgId); - JSONObject info; + DcMsg parentMsg; if(dcMsg.getType() == DcMsg.DC_MSG_WEBXDC) { - info = dcMsg.getWebxdcInfo(); - } else { // info message, get from parent xdc - info = dcMsg.getParent() != null? dcMsg.getParent().getWebxdcInfo() : new JSONObject(); + parentMsg = dcMsg; + } else { // info message, get parent xdc + parentMsg = dcMsg.getParent() != null? dcMsg.getParent() : dcMsg; } + + if (Util.equals(visibleWebxdc, new Pair<>(accountId, parentMsg.getId()))) { + return; // do not notify if the app is already open + } + + JSONObject info = parentMsg.getWebxdcInfo(); final String name = JsonUtils.optString(info, "name"); String shortLine = name.isEmpty()? text : (name + ": " + text); - maybeAddNotification(accountId, dcContext.getChat(dcMsg.getChatId()), msgId, shortLine, shortLine, false); + maybeAddNotification(accountId, dcContext.getChat(dcMsg.getChatId()), msgId, shortLine, shortLine, false, true); }); } @WorkerThread - private void maybeAddNotification(int accountId, DcChat dcChat, int msgId, String shortLine, String tickerLine, boolean playInChatSound) { + private void maybeAddNotification(int accountId, DcChat dcChat, int msgId, String shortLine, String tickerLine, boolean playInChatSound, boolean isMention) { DcContext dcContext = context.dcAccounts.getAccount(accountId); int chatId = dcChat.getId(); ChatData chatData = new ChatData(accountId, chatId); - if (dcContext.isMuted() || dcChat.isMuted()) { + if (dcContext.isMuted() || (!isMention && dcChat.isMuted())) { return; } @@ -660,6 +671,22 @@ public class NotificationCenter { visibleChat = null; } + public void updateVisibleWebxdc(int accountId, int msgId) { + Util.runOnAnyBackgroundThread(() -> { + + if (accountId != 0 && msgId != 0) { + visibleWebxdc = new Pair<>(accountId, msgId); + } else { + visibleWebxdc = null; + } + + }); + } + + public void clearVisibleWebxdc() { + visibleWebxdc = null; + } + public void maybePlaySendSound(DcChat dcChat) { if (Prefs.isInChatNotifications(context) && !dcChat.isMuted()) { InChatSounds.getInstance(context).playSendSound();