improve notifications handling

This commit is contained in:
adbenitez
2024-11-29 20:10:49 +01:00
parent 7131f5774e
commit 4d19a59cb6
2 changed files with 48 additions and 9 deletions
@@ -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);
@@ -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<Integer, Integer> 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();