diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java index f094cea12c..5165e87ff9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java @@ -4,18 +4,21 @@ import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.text.SpannableStringBuilder; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; -import android.text.SpannableStringBuilder; + import org.session.libsession.messaging.contacts.Contact; +import org.session.libsession.utilities.NotificationPrivacyPreference; +import org.session.libsession.utilities.TextSecurePreferences; +import org.session.libsession.utilities.Util; +import org.session.libsession.utilities.recipients.Recipient; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.loki.activities.HomeActivity; import org.thoughtcrime.securesms.loki.database.SessionContactDatabase; -import org.session.libsession.utilities.NotificationPrivacyPreference; -import org.session.libsession.utilities.recipients.Recipient; -import org.session.libsession.utilities.TextSecurePreferences; -import org.session.libsession.utilities.Util; + import java.util.LinkedList; import java.util.List; @@ -50,14 +53,7 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu public void setMostRecentSender(Recipient recipient, Recipient threadRecipient) { String displayName = recipient.toShortString(); if (threadRecipient.isOpenGroupRecipient()) { - SessionContactDatabase contactDB = DatabaseFactory.getSessionContactDatabase(context); - String sessionID = recipient.getAddress().serialize(); - Contact contact = contactDB.getContactWithSessionID(sessionID); - if (contact != null) { - displayName = contact.displayName(Contact.ContactContext.OPEN_GROUP); - } else { - displayName = sessionID; - } + displayName = getOpenGroupDisplayName(recipient); } if (privacy.isDisplayContact()) { setContentText(context.getString(R.string.MessageNotifier_most_recent_from_s, displayName)); @@ -79,14 +75,7 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu public void addMessageBody(@NonNull Recipient sender, Recipient threadRecipient, @Nullable CharSequence body) { String displayName = sender.toShortString(); if (threadRecipient.isOpenGroupRecipient()) { - SessionContactDatabase contactDB = DatabaseFactory.getSessionContactDatabase(context); - String sessionID = sender.getAddress().serialize(); - Contact contact = contactDB.getContactWithSessionID(sessionID); - if (contact != null) { - displayName = contact.displayName(Contact.ContactContext.OPEN_GROUP); - } else { - displayName = sessionID; - } + displayName = getOpenGroupDisplayName(sender); } if (privacy.isDisplayMessage()) { SpannableStringBuilder builder = new SpannableStringBuilder(); @@ -118,4 +107,17 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu return super.build(); } + + /** + * @param recipient the * individual * recipient for which to get the open group display name. + */ + private String getOpenGroupDisplayName(Recipient recipient) { + SessionContactDatabase contactDB = DatabaseFactory.getSessionContactDatabase(context); + String sessionID = recipient.getAddress().serialize(); + Contact contact = contactDB.getContactWithSessionID(sessionID); + if (contact == null) { return sessionID; } + String displayName = contact.displayName(Contact.ContactContext.OPEN_GROUP); + if (displayName == null) { return sessionID; } + return displayName; + } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java index 008779301b..0b38783be8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java @@ -119,18 +119,8 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); if (privacy.isDisplayContact() && threadRecipients.isOpenGroupRecipient()) { - String displayName; - SessionContactDatabase contactDB = DatabaseFactory.getSessionContactDatabase(context); - String sessionID = individualRecipient.getAddress().serialize(); - Contact contact = contactDB.getContactWithSessionID(sessionID); - if (contact != null) { - displayName = contact.displayName(Contact.ContactContext.OPEN_GROUP); - } else { - displayName = sessionID; - } - if (displayName != null) { - stringBuilder.append(Util.getBoldedString(displayName + ": ")); - } + String displayName = getOpenGroupDisplayName(individualRecipient); + stringBuilder.append(Util.getBoldedString(displayName + ": ")); } if (privacy.isDisplayMessage()) { @@ -215,18 +205,8 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); if (privacy.isDisplayContact() && threadRecipient.isOpenGroupRecipient()) { - String displayName; - SessionContactDatabase contactDB = DatabaseFactory.getSessionContactDatabase(context); - String sessionID = individualRecipient.getAddress().serialize(); - Contact contact = contactDB.getContactWithSessionID(sessionID); - if (contact != null) { - displayName = contact.displayName(Contact.ContactContext.OPEN_GROUP); - } else { - displayName = sessionID; - } - if (displayName != null) { - stringBuilder.append(Util.getBoldedString(displayName + ": ")); - } + String displayName = getOpenGroupDisplayName(individualRecipient); + stringBuilder.append(Util.getBoldedString(displayName + ": ")); } if (privacy.isDisplayMessage()) { @@ -342,4 +322,17 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil String displayName = recipient.getName(); return AvatarPlaceholderGenerator.generate(context, 128, publicKey, displayName); } + + /** + * @param recipient the * individual * recipient for which to get the open group display name. + */ + private String getOpenGroupDisplayName(Recipient recipient) { + SessionContactDatabase contactDB = DatabaseFactory.getSessionContactDatabase(context); + String sessionID = recipient.getAddress().serialize(); + Contact contact = contactDB.getContactWithSessionID(sessionID); + if (contact == null) { return sessionID; } + String displayName = contact.displayName(Contact.ContactContext.OPEN_GROUP); + if (displayName == null) { return sessionID; } + return displayName; + } } diff --git a/libsession/src/main/java/org/session/libsession/utilities/Util.kt b/libsession/src/main/java/org/session/libsession/utilities/Util.kt index 14a2cc9001..1ad209d5d5 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/Util.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/Util.kt @@ -321,6 +321,7 @@ object Util { @JvmStatic fun getBoldedString(value: String?): CharSequence { + if (value.isNullOrEmpty()) { return "" } val spanned = SpannableString(value) spanned.setSpan(StyleSpan(Typeface.BOLD), 0, spanned.length,