From ea0ba98722428d352be0a420b04be32d32255c8e Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 7 Feb 2020 15:13:12 +1100 Subject: [PATCH 1/3] Display username in notifications instead of public keys fixed --- .../SingleRecipientNotificationBuilder.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java index af0cfc2b7c..670fa9a6c3 100644 --- a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java @@ -226,7 +226,18 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); if (privacy.isDisplayContact() && threadRecipient.isGroupRecipient()) { - stringBuilder.append(Util.getBoldedString(individualRecipient.toShortString() + ": ")); + long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipient); + 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) { + stringBuilder.append(Util.getBoldedString(displayName + ": ")); + } } if (privacy.isDisplayMessage()) { From 3710be72fdf4b5ede8b96fbd129a3f6959b923c9 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 7 Feb 2020 15:39:44 +1100 Subject: [PATCH 2/3] "Clicking on message notifications doesn't mark the messages as read" fixed --- .../thoughtcrime/securesms/notifications/MessageNotifier.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 96dfe547c3..830e43c7ac 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -311,6 +311,7 @@ public class MessageNotifier { builder.setDeleteIntent(notificationState.getDeleteIntent(context)); builder.setOnlyAlertOnce(!signal); builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); + builder.setAutoCancel(true); long timestamp = notifications.get(0).getTimestamp(); if (timestamp != 0) builder.setWhen(timestamp); From 2aafce5f4c54d3917d1ae5707facd1b670c934ef Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Tue, 11 Feb 2020 10:50:24 +1100 Subject: [PATCH 3/3] apply the changes to MultipleRecipientNotification and refactor some code --- .../notifications/MessageNotifier.java | 5 ++-- .../MultipleRecipientNotificationBuilder.java | 24 +++++++++++++++---- .../notifications/NotificationUtilities.kt | 22 +++++++++++++++++ .../SingleRecipientNotificationBuilder.java | 20 ++-------------- 4 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/notifications/NotificationUtilities.kt diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 830e43c7ac..3a3ca331f9 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -359,11 +359,12 @@ public class MessageNotifier { List notifications = notificationState.getNotifications(); 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.setDeleteIntent(notificationState.getDeleteIntent(context)); builder.setOnlyAlertOnce(!signal); builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY); + builder.setAutoCancel(true); long timestamp = notifications.get(0).getTimestamp(); if (timestamp != 0) builder.setWhen(timestamp); @@ -374,7 +375,7 @@ public class MessageNotifier { while(iterator.hasPrevious()) { NotificationItem item = iterator.previous(); - builder.addMessageBody(item.getIndividualRecipient(), item.getText()); + builder.addMessageBody(item.getIndividualRecipient(), item.getRecipient(), item.getText()); } if (signal) { diff --git a/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java index 66a92088ce..99b4733230 100644 --- a/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; +import android.text.SpannableStringBuilder; import org.thoughtcrime.securesms.loki.redesign.activities.HomeActivity; import org.thoughtcrime.securesms.preferences.widgets.NotificationPrivacyPreference; @@ -45,10 +46,14 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu 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()) { setContentText(context.getString(R.string.MessageNotifier_most_recent_from_s, - recipient.toShortString())); + displayName)); } if (recipient.getNotificationChannel() != null) { @@ -64,11 +69,20 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu 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()) { - 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()) { - messageBodies.add(Util.getBoldedString(sender.toShortString())); + messageBodies.add(Util.getBoldedString(displayName)); } if (privacy.isDisplayContact() && sender.getContactUri() != null) { diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationUtilities.kt b/src/org/thoughtcrime/securesms/notifications/NotificationUtilities.kt new file mode 100644 index 0000000000..8ff7c636cc --- /dev/null +++ b/src/org/thoughtcrime/securesms/notifications/NotificationUtilities.kt @@ -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 +} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java index 670fa9a6c3..0da777e700 100644 --- a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java @@ -120,15 +120,7 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); if (privacy.isDisplayContact() && threadRecipients.isGroupRecipient()) { - long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipients); - 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); - } + String displayName = NotificationUtilities.getOpenGroupDisplayName(individualRecipient, threadRecipients, context); if (displayName != null) { stringBuilder.append(Util.getBoldedString(displayName + ": ")); } @@ -226,15 +218,7 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); if (privacy.isDisplayContact() && threadRecipient.isGroupRecipient()) { - long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipient); - 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); - } + String displayName = NotificationUtilities.getOpenGroupDisplayName(individualRecipient, threadRecipient, context); if (displayName != null) { stringBuilder.append(Util.getBoldedString(displayName + ": ")); }