From a35a7a6a96cf68f7b44749f1f3482adac5b1d17e Mon Sep 17 00:00:00 2001 From: alansley Date: Thu, 29 Aug 2024 08:55:16 +1000 Subject: [PATCH] Converted MessageReceipientNotificationBuilder to Kotlin & updated Phrase usage --- .../MultipleRecipientNotificationBuilder.java | 129 ------------------ .../MultipleRecipientNotificationBuilder.kt | 112 +++++++++++++++ 2 files changed, 112 insertions(+), 129 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java create mode 100644 app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java deleted file mode 100644 index 2ee1fd2c17..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.thoughtcrime.securesms.notifications; - -import static org.session.libsession.utilities.StringSubstitutionConstants.CONVERSATION_COUNT_KEY; -import static org.session.libsession.utilities.StringSubstitutionConstants.MESSAGE_COUNT_KEY; -import static org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY; - -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 com.squareup.phrase.Phrase; -import java.util.LinkedList; -import java.util.List; -import network.loki.messenger.R; -import org.session.libsession.messaging.contacts.Contact; -import org.session.libsession.utilities.NotificationPrivacyPreference; -import org.session.libsession.utilities.Util; -import org.session.libsession.utilities.recipients.Recipient; -import org.thoughtcrime.securesms.database.SessionContactDatabase; -import org.thoughtcrime.securesms.dependencies.DatabaseComponent; -import org.thoughtcrime.securesms.home.HomeActivity; - -public class MultipleRecipientNotificationBuilder extends AbstractNotificationBuilder { - - private final List messageBodies = new LinkedList<>(); - - public MultipleRecipientNotificationBuilder(Context context, NotificationPrivacyPreference privacy) { - super(context, privacy); - - setColor(context.getResources().getColor(R.color.textsecure_primary)); - setSmallIcon(R.drawable.ic_notification); - setContentTitle(context.getString(R.string.app_name)); - setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, HomeActivity.class), PendingIntent.FLAG_IMMUTABLE)); - setCategory(NotificationCompat.CATEGORY_MESSAGE); - setGroupSummary(true); - } - - public void setMessageCount(int messageCount, int threadCount) { - String txt = Phrase.from(context, R.string.notificationsSystem) - .put(MESSAGE_COUNT_KEY, messageCount) - .put(CONVERSATION_COUNT_KEY, threadCount) - .format().toString(); - setSubText(txt); - setNumber(messageCount); - } - - public void setMostRecentSender(Recipient recipient, Recipient threadRecipient) { - String displayName = recipient.toShortString(); - if (threadRecipient.isGroupRecipient()) { - displayName = getGroupDisplayName(recipient, threadRecipient.isCommunityRecipient()); - } - if (privacy.isDisplayContact()) { - String txt = Phrase.from(context, R.string.notificationsMostRecent) - .put(NAME_KEY, displayName) - .format().toString(); - setContentText(txt); - } - - if (recipient.getNotificationChannel() != null) { - setChannelId(recipient.getNotificationChannel()); - } - } - - public void addActions(PendingIntent markAsReadIntent) { - NotificationCompat.Action markAllAsReadAction = new NotificationCompat.Action(R.drawable.check, - context.getString(R.string.messageMarkRead), - markAsReadIntent); - addAction(markAllAsReadAction); - extend(new NotificationCompat.WearableExtender().addAction(markAllAsReadAction)); - } - - public void putStringExtra(String key, String value) { - extras.putString(key,value); - } - - public void addMessageBody(@NonNull Recipient sender, Recipient threadRecipient, @Nullable CharSequence body) { - String displayName = sender.toShortString(); - if (threadRecipient.isGroupRecipient()) { - displayName = getGroupDisplayName(sender, threadRecipient.isCommunityRecipient()); - } - if (privacy.isDisplayMessage()) { - 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(displayName)); - } - - if (privacy.isDisplayContact() && sender.getContactUri() != null) { -// addPerson(sender.getContactUri().toString()); - } - } - - @Override - public Notification build() { - if (privacy.isDisplayMessage() || privacy.isDisplayContact()) { - NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); - - for (CharSequence body : messageBodies) { - style.addLine(trimToDisplayLength(body)); - } - - setStyle(style); - } - - return super.build(); - } - - /** - * @param recipient the * individual * recipient for which to get the display name. - * @param openGroupRecipient whether in an open group context - */ - private String getGroupDisplayName(Recipient recipient, boolean openGroupRecipient) { - SessionContactDatabase contactDB = DatabaseComponent.get(context).sessionContactDatabase(); - String accountID = recipient.getAddress().serialize(); - Contact contact = contactDB.getContactWithAccountID(accountID); - if (contact == null) { return accountID; } - String displayName = contact.displayName(openGroupRecipient ? Contact.ContactContext.OPEN_GROUP : Contact.ContactContext.REGULAR); - if (displayName == null) { return accountID; } - return displayName; - } -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.kt new file mode 100644 index 0000000000..a526f9e8f2 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.kt @@ -0,0 +1,112 @@ +package org.thoughtcrime.securesms.notifications + +import android.app.Notification +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.text.SpannableStringBuilder +import androidx.core.app.NotificationCompat +import com.squareup.phrase.Phrase +import network.loki.messenger.R +import org.session.libsession.messaging.contacts.Contact +import org.session.libsession.utilities.NotificationPrivacyPreference +import org.session.libsession.utilities.StringSubstitutionConstants.CONVERSATION_COUNT_KEY +import org.session.libsession.utilities.StringSubstitutionConstants.MESSAGE_COUNT_KEY +import org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY +import org.session.libsession.utilities.Util.getBoldedString +import org.session.libsession.utilities.recipients.Recipient +import org.thoughtcrime.securesms.dependencies.DatabaseComponent.Companion.get +import org.thoughtcrime.securesms.home.HomeActivity +import org.thoughtcrime.securesms.ui.getSubbedString +import java.util.LinkedList + +class MultipleRecipientNotificationBuilder(context: Context, privacy: NotificationPrivacyPreference?) : AbstractNotificationBuilder(context, privacy) { + private val messageBodies: MutableList = LinkedList() + + init { + color = context.resources.getColor(R.color.textsecure_primary) + setSmallIcon(R.drawable.ic_notification) + setContentTitle(context.getString(R.string.app_name)) + setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, HomeActivity::class.java), PendingIntent.FLAG_IMMUTABLE)) + setCategory(NotificationCompat.CATEGORY_MESSAGE) + setGroupSummary(true) + } + + fun setMessageCount(messageCount: Int, threadCount: Int) { + val txt = context.getSubbedString(R.string.notificationsSystem, MESSAGE_COUNT_KEY to messageCount.toString(), CONVERSATION_COUNT_KEY to threadCount.toString()) + setSubText(txt) + setNumber(messageCount) + } + + fun setMostRecentSender(recipient: Recipient, threadRecipient: Recipient) { + var displayName = recipient.toShortString() + if (threadRecipient.isGroupRecipient) { + displayName = getGroupDisplayName(recipient, threadRecipient.isCommunityRecipient) + } + if (privacy.isDisplayContact) { + val txt = Phrase.from(context, R.string.notificationsMostRecent) + .put(NAME_KEY, displayName) + .format().toString() + setContentText(txt) + } + + if (recipient.notificationChannel != null) { + setChannelId(recipient.notificationChannel!!) + } + } + + fun addActions(markAsReadIntent: PendingIntent?) { + val markAllAsReadAction = NotificationCompat.Action( + R.drawable.check, + context.getString(R.string.messageMarkRead), + markAsReadIntent + ) + addAction(markAllAsReadAction) + extend(NotificationCompat.WearableExtender().addAction(markAllAsReadAction)) + } + + fun putStringExtra(key: String?, value: String?) { extras.putString(key, value) } + + fun addMessageBody(sender: Recipient, threadRecipient: Recipient, body: CharSequence?) { + var displayName = sender.toShortString() + if (threadRecipient.isGroupRecipient) { + displayName = getGroupDisplayName(sender, threadRecipient.isCommunityRecipient) + } + if (privacy.isDisplayMessage) { + val builder = SpannableStringBuilder() + builder.append(getBoldedString(displayName)) + builder.append(": ") + builder.append(body ?: "") + messageBodies.add(builder) + } else if (privacy.isDisplayContact) { + messageBodies.add(getBoldedString(displayName)) + } + + // TODO: What on earth is this? Why is it commented out? It's also commented out in dev... remove? -ACL 2024-08-29 + if (privacy.isDisplayContact && sender.contactUri != null) { +// addPerson(sender.getContactUri().toString()); + } + } + + override fun build(): Notification { + if (privacy.isDisplayMessage || privacy.isDisplayContact) { + val style = NotificationCompat.InboxStyle() + for (body in messageBodies) { style.addLine(trimToDisplayLength(body)) } + setStyle(style) + } + return super.build() + } + + /** + * @param recipient the * individual * recipient for which to get the display name. + * @param openGroupRecipient whether in an open group context + */ + private fun getGroupDisplayName(recipient: Recipient, openGroupRecipient: Boolean): String { + val contactDB = get(context).sessionContactDatabase() + val accountID = recipient.address.serialize() + val contact = contactDB.getContactWithAccountID(accountID) ?: return accountID + val displayName = contact.displayName(if (openGroupRecipient) Contact.ContactContext.OPEN_GROUP else Contact.ContactContext.REGULAR) + if (displayName == null) { return accountID } + return displayName + } +}