Merge pull request #3377 from deltachat/r10s/notify-reactions

notify reactions added
This commit is contained in:
adb 2024-11-23 18:22:49 +01:00 committed by GitHub
commit cd159bac6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 23 deletions

View file

@ -12,6 +12,7 @@ public class DcContext {
public final static int DC_EVENT_ERROR_SELF_NOT_IN_GROUP = 410; public final static int DC_EVENT_ERROR_SELF_NOT_IN_GROUP = 410;
public final static int DC_EVENT_MSGS_CHANGED = 2000; public final static int DC_EVENT_MSGS_CHANGED = 2000;
public final static int DC_EVENT_REACTIONS_CHANGED = 2001; public final static int DC_EVENT_REACTIONS_CHANGED = 2001;
public final static int DC_EVENT_INCOMING_REACTION = 2002;
public final static int DC_EVENT_INCOMING_MSG = 2005; public final static int DC_EVENT_INCOMING_MSG = 2005;
public final static int DC_EVENT_MSGS_NOTICED = 2008; public final static int DC_EVENT_MSGS_NOTICED = 2008;
public final static int DC_EVENT_MSG_DELIVERED = 2010; public final static int DC_EVENT_MSG_DELIVERED = 2010;

View file

@ -171,7 +171,11 @@ public class DcEventCenter {
switch (id) { switch (id) {
case DcContext.DC_EVENT_INCOMING_MSG: case DcContext.DC_EVENT_INCOMING_MSG:
DcHelper.getNotificationCenter(context).addNotification(accountId, event.getData1Int(), event.getData2Int()); DcHelper.getNotificationCenter(context).notifyMessage(accountId, event.getData1Int(), event.getData2Int());
break;
case DcContext.DC_EVENT_INCOMING_REACTION:
DcHelper.getNotificationCenter(context).notifyReaction(accountId, event.getData1Int(), event.getData2Int(), event.getData2Str());
break; break;
case DcContext.DC_EVENT_MSGS_NOTICED: case DcContext.DC_EVENT_MSGS_NOTICED:

View file

@ -20,12 +20,14 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.RemoteInput; import androidx.core.app.RemoteInput;
import androidx.core.app.TaskStackBuilder; import androidx.core.app.TaskStackBuilder;
import com.b44t.messenger.DcChat; import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcContact;
import com.b44t.messenger.DcContext; import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcMsg; import com.b44t.messenger.DcMsg;
import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.DiskCacheStrategy;
@ -331,11 +333,48 @@ public class NotificationCenter {
// add notifications & co. // add notifications & co.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
public void addNotification(int accountId, int chatId, int msgId) { public void notifyMessage(int accountId, int chatId, int msgId) {
Util.runOnAnyBackgroundThread(() -> { Util.runOnAnyBackgroundThread(() -> {
DcContext dcContext = context.dcAccounts.getAccount(accountId);
DcChat dcChat = dcContext.getChat(chatId);
DcMsg dcMsg = dcContext.getMsg(msgId);
NotificationPrivacyPreference privacy = Prefs.getNotificationPrivacy(context);
String shortLine = privacy.isDisplayMessage()? dcMsg.getSummarytext(2000) : context.getString(R.string.notify_new_message);
if (dcChat.isMultiUser() && privacy.isDisplayContact()) {
shortLine = dcMsg.getSenderName(dcContext.getContact(dcMsg.getFromId()), false) + ": " + shortLine;
}
String tickerLine = shortLine;
if (!dcChat.isMultiUser() && privacy.isDisplayContact()) {
tickerLine = dcMsg.getSenderName(dcContext.getContact(dcMsg.getFromId()), false) + ": " + tickerLine;
}
maybeAddNotification(accountId, dcChat, msgId, shortLine, tickerLine, true);
});
}
public void notifyReaction(int accountId, int contactId, int msgId, String reaction) {
Util.runOnAnyBackgroundThread(() -> {
DcContext dcContext = context.dcAccounts.getAccount(accountId);
DcMsg dcMsg = dcContext.getMsg(msgId);
NotificationPrivacyPreference privacy = Prefs.getNotificationPrivacy(context);
if (!privacy.isDisplayContact() || !privacy.isDisplayMessage()) {
return; // showing "New Message" is wrong and showing "New Reaction" is already content. just do nothing.
}
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);
});
}
@WorkerThread
private void maybeAddNotification(int accountId, DcChat dcChat, int msgId, String shortLine, String tickerLine, boolean playInChatSound) {
DcContext dcContext = context.dcAccounts.getAccount(accountId); DcContext dcContext = context.dcAccounts.getAccount(accountId);
DcChat dcChat = dcContext.getChat(chatId); int chatId = dcChat.getId();
ChatData chatData = new ChatData(accountId, chatId); ChatData chatData = new ChatData(accountId, chatId);
if (dcContext.isMuted() || dcChat.isMuted()) { if (dcContext.isMuted() || dcChat.isMuted()) {
@ -348,22 +387,13 @@ public class NotificationCenter {
} }
if (Util.equals(visibleChat, chatData)) { if (Util.equals(visibleChat, chatData)) {
if (Prefs.isInChatNotifications(context)) { if (playInChatSound && Prefs.isInChatNotifications(context)) {
InChatSounds.getInstance(context).playIncomingSound(); InChatSounds.getInstance(context).playIncomingSound();
} }
return; return;
} }
// get notification text as a single line
NotificationPrivacyPreference privacy = Prefs.getNotificationPrivacy(context); NotificationPrivacyPreference privacy = Prefs.getNotificationPrivacy(context);
DcMsg dcMsg = dcContext.getMsg(msgId);
String line = privacy.isDisplayMessage()? dcMsg.getSummarytext(2000) : context.getString(R.string.notify_new_message);
if (dcChat.isMultiUser() && privacy.isDisplayContact()) {
line = dcMsg.getSenderName(dcContext.getContact(dcMsg.getFromId()), false) + ": " + line;
}
// play signal?
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
boolean signal = (now - lastAudibleNotification) > MIN_AUDIBLE_PERIOD_MILLIS; boolean signal = (now - lastAudibleNotification) > MIN_AUDIBLE_PERIOD_MILLIS;
if (signal) { if (signal) {
@ -381,7 +411,7 @@ public class NotificationCenter {
.setPriority(Prefs.getNotificationPriority(context)) .setPriority(Prefs.getNotificationPriority(context))
.setCategory(NotificationCompat.CATEGORY_MESSAGE) .setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOnlyAlertOnce(!signal) .setOnlyAlertOnce(!signal)
.setContentText(line) .setContentText(shortLine)
.setDeleteIntent(getMarkAsReadIntent(chatData, msgId, false)) .setDeleteIntent(getMarkAsReadIntent(chatData, msgId, false))
.setContentIntent(getOpenChatIntent(chatData)); .setContentIntent(getOpenChatIntent(chatData));
@ -401,12 +431,6 @@ public class NotificationCenter {
} }
} }
// if privacy allows, for better accessibility,
// prepend the sender in the ticker also for one-to-one chats (for group-chats, this is already done)
String tickerLine = line;
if (!dcChat.isMultiUser() && privacy.isDisplayContact()) {
line = dcMsg.getSenderName(dcContext.getContact(dcMsg.getFromId()), false) + ": " + line;
}
builder.setTicker(tickerLine); builder.setTicker(tickerLine);
// set sound, vibrate, led for systems that do not have notification channels // set sound, vibrate, led for systems that do not have notification channels
@ -502,7 +526,7 @@ public class NotificationCenter {
lines = new ArrayList<>(); lines = new ArrayList<>();
accountInbox.put(chatId, lines); accountInbox.put(chatId, lines);
} }
lines.add(line); lines.add(tickerLine);
for (int l = lines.size() - 1; l >= 0; l--) { for (int l = lines.size() - 1; l >= 0; l--) {
inboxStyle.addLine(lines.get(l)); inboxStyle.addLine(lines.get(l));
@ -550,7 +574,6 @@ public class NotificationCenter {
Log.e(TAG, "cannot add notification summary", e); Log.e(TAG, "cannot add notification summary", e);
} }
} }
});
} }
public void removeNotifications(int accountId, int chatId) { public void removeNotifications(int accountId, int chatId) {