Fix crash

This commit is contained in:
nielsandriesse 2021-05-31 13:13:25 +10:00
parent f872c2b9be
commit e1f3362c2a
3 changed files with 41 additions and 45 deletions

View File

@ -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;
}
}

View File

@ -119,19 +119,9 @@ 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) {
String displayName = getOpenGroupDisplayName(individualRecipient);
stringBuilder.append(Util.getBoldedString(displayName + ": "));
}
}
if (privacy.isDisplayMessage()) {
setContentText(stringBuilder.append(message));
@ -215,19 +205,9 @@ 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) {
String displayName = getOpenGroupDisplayName(individualRecipient);
stringBuilder.append(Util.getBoldedString(displayName + ": "));
}
}
if (privacy.isDisplayMessage()) {
messageBodies.add(stringBuilder.append(messageBody == null ? "" : messageBody));
@ -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;
}
}

View File

@ -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,