apply the changes to MultipleRecipientNotification and refactor some code

This commit is contained in:
Ryan ZHAO 2020-02-11 10:50:24 +11:00
parent 3710be72fd
commit 2aafce5f4c
4 changed files with 46 additions and 25 deletions

View File

@ -359,11 +359,12 @@ public class MessageNotifier {
List<NotificationItem> notifications = notificationState.getNotifications(); List<NotificationItem> notifications = notificationState.getNotifications();
builder.setMessageCount(notificationState.getMessageCount(), notificationState.getThreadCount()); builder.setMessageCount(notificationState.getMessageCount(), notificationState.getThreadCount());
builder.setMostRecentSender(notifications.get(0).getIndividualRecipient()); builder.setMostRecentSender(notifications.get(0).getIndividualRecipient(), notifications.get(0).getRecipient());
builder.setGroup(NOTIFICATION_GROUP); builder.setGroup(NOTIFICATION_GROUP);
builder.setDeleteIntent(notificationState.getDeleteIntent(context)); builder.setDeleteIntent(notificationState.getDeleteIntent(context));
builder.setOnlyAlertOnce(!signal); builder.setOnlyAlertOnce(!signal);
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
builder.setAutoCancel(true);
long timestamp = notifications.get(0).getTimestamp(); long timestamp = notifications.get(0).getTimestamp();
if (timestamp != 0) builder.setWhen(timestamp); if (timestamp != 0) builder.setWhen(timestamp);
@ -374,7 +375,7 @@ public class MessageNotifier {
while(iterator.hasPrevious()) { while(iterator.hasPrevious()) {
NotificationItem item = iterator.previous(); NotificationItem item = iterator.previous();
builder.addMessageBody(item.getIndividualRecipient(), item.getText()); builder.addMessageBody(item.getIndividualRecipient(), item.getRecipient(), item.getText());
} }
if (signal) { if (signal) {

View File

@ -7,6 +7,7 @@ import android.content.Intent;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.text.SpannableStringBuilder;
import org.thoughtcrime.securesms.loki.redesign.activities.HomeActivity; import org.thoughtcrime.securesms.loki.redesign.activities.HomeActivity;
import org.thoughtcrime.securesms.preferences.widgets.NotificationPrivacyPreference; import org.thoughtcrime.securesms.preferences.widgets.NotificationPrivacyPreference;
@ -45,10 +46,14 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu
setNumber(messageCount); setNumber(messageCount);
} }
public void setMostRecentSender(Recipient recipient) { public void setMostRecentSender(Recipient recipient, Recipient threadRecipient) {
String displayName = recipient.toShortString();
if (threadRecipient.isGroupRecipient()) {
displayName = NotificationUtilities.getOpenGroupDisplayName(recipient, threadRecipient, context);
}
if (privacy.isDisplayContact()) { if (privacy.isDisplayContact()) {
setContentText(context.getString(R.string.MessageNotifier_most_recent_from_s, setContentText(context.getString(R.string.MessageNotifier_most_recent_from_s,
recipient.toShortString())); displayName));
} }
if (recipient.getNotificationChannel() != null) { if (recipient.getNotificationChannel() != null) {
@ -64,11 +69,20 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu
extend(new NotificationCompat.WearableExtender().addAction(markAllAsReadAction)); extend(new NotificationCompat.WearableExtender().addAction(markAllAsReadAction));
} }
public void addMessageBody(@NonNull Recipient sender, @Nullable CharSequence body) { public void addMessageBody(@NonNull Recipient sender, Recipient threadRecipient, @Nullable CharSequence body) {
String displayName = sender.toShortString();
if (threadRecipient.isGroupRecipient()) {
displayName = NotificationUtilities.getOpenGroupDisplayName(sender, threadRecipient, context);
}
if (privacy.isDisplayMessage()) { if (privacy.isDisplayMessage()) {
messageBodies.add(getStyledMessage(sender, body)); SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(Util.getBoldedString(displayName));
builder.append(": ");
builder.append(body == null ? "" : body);
messageBodies.add(builder);
} else if (privacy.isDisplayContact()) { } else if (privacy.isDisplayContact()) {
messageBodies.add(Util.getBoldedString(sender.toShortString())); messageBodies.add(Util.getBoldedString(displayName));
} }
if (privacy.isDisplayContact() && sender.getContactUri() != null) { if (privacy.isDisplayContact() && sender.getContactUri() != null) {

View File

@ -0,0 +1,22 @@
@file:JvmName("NotificationUtilities")
package org.thoughtcrime.securesms.notifications
import android.content.Context
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.recipients.Recipient
fun getOpenGroupDisplayName(recipient: Recipient, threadRecipient: Recipient, context: Context): String {
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipient)
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
val hexEncodedPublicKey = recipient.address.toString()
val displayName: String?
displayName = if (publicChat != null) {
DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(publicChat.id, hexEncodedPublicKey)
} else {
DatabaseFactory.getLokiUserDatabase(context).getDisplayName(hexEncodedPublicKey)
}
if (displayName == null) {
return hexEncodedPublicKey
}
return displayName
}

View File

@ -120,15 +120,7 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
if (privacy.isDisplayContact() && threadRecipients.isGroupRecipient()) { if (privacy.isDisplayContact() && threadRecipients.isGroupRecipient()) {
long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipients); String displayName = NotificationUtilities.getOpenGroupDisplayName(individualRecipient, threadRecipients, context);
LokiPublicChat publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID);
String hexEncodedPublicKey = individualRecipient.getAddress().toString();
String displayName;
if (publicChat != null) {
displayName = DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(publicChat.getId(), hexEncodedPublicKey);
} else {
displayName = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(hexEncodedPublicKey);
}
if (displayName != null) { if (displayName != null) {
stringBuilder.append(Util.getBoldedString(displayName + ": ")); stringBuilder.append(Util.getBoldedString(displayName + ": "));
} }
@ -226,15 +218,7 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
if (privacy.isDisplayContact() && threadRecipient.isGroupRecipient()) { if (privacy.isDisplayContact() && threadRecipient.isGroupRecipient()) {
long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipient); String displayName = NotificationUtilities.getOpenGroupDisplayName(individualRecipient, threadRecipient, context);
LokiPublicChat publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID);
String hexEncodedPublicKey = individualRecipient.getAddress().toString();
String displayName;
if (publicChat != null) {
displayName = DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(publicChat.getId(), hexEncodedPublicKey);
} else {
displayName = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(hexEncodedPublicKey);
}
if (displayName != null) { if (displayName != null) {
stringBuilder.append(Util.getBoldedString(displayName + ": ")); stringBuilder.append(Util.getBoldedString(displayName + ": "));
} }