From 7e791d63dc004fd29b391c1892a2f31b8d5dfbd8 Mon Sep 17 00:00:00 2001 From: Harris Date: Thu, 12 Aug 2021 15:36:08 +1000 Subject: [PATCH] fix: other view types have clickable links, clickable links in the long message. Ordering of highlight mentions before getting clickable spans fixes #664 --- .../v2/components/AlbumThumbnailView.kt | 15 ++++++++-- .../v2/messages/VisibleMessageContentView.kt | 30 ++++++++++++------- .../longmessage/LongMessageActivity.java | 15 +++------- .../main/res/layout/album_thumbnail_view.xml | 2 +- .../main/res/layout/longmessage_activity.xml | 2 ++ 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/AlbumThumbnailView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/AlbumThumbnailView.kt index 7cd78cda2b..b1283e1a2f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/AlbumThumbnailView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/AlbumThumbnailView.kt @@ -21,13 +21,14 @@ import org.session.libsession.utilities.ViewUtil import org.session.libsession.utilities.recipients.Recipient import org.thoughtcrime.securesms.MediaPreviewActivity import org.thoughtcrime.securesms.components.CornerMask +import org.thoughtcrime.securesms.conversation.v2.messages.VisibleMessageContentView import org.thoughtcrime.securesms.conversation.v2.utilities.KThumbnailView +import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities.getIntersectedModalSpans import org.thoughtcrime.securesms.database.model.MmsMessageRecord -import org.thoughtcrime.securesms.util.ActivityDispatcher import org.thoughtcrime.securesms.longmessage.LongMessageActivity import org.thoughtcrime.securesms.mms.GlideRequests import org.thoughtcrime.securesms.mms.Slide -import org.thoughtcrime.securesms.video.exo.AttachmentDataSource +import org.thoughtcrime.securesms.util.ActivityDispatcher import kotlin.math.roundToInt class AlbumThumbnailView : FrameLayout { @@ -80,6 +81,13 @@ class AlbumThumbnailView : FrameLayout { } return } + val intersectedSpans = albumCellBodyText.getIntersectedModalSpans(eventRect) + if (intersectedSpans.isNotEmpty()) { + intersectedSpans.forEach { span -> + span.onClick(albumCellBodyText) + } + return + } // test each album child albumCellContainer.findViewById(R.id.album_thumbnail_root)?.children?.forEachIndexed { index, child -> child.getGlobalVisibleRect(testRect) @@ -130,7 +138,8 @@ class AlbumThumbnailView : FrameLayout { thumbnailView.setImageResource(glideRequests, slide, isPreview = false, mms = message) } albumCellBodyParent.isVisible = message.body.isNotEmpty() - albumCellBodyText.text = message.body + val body = VisibleMessageContentView.getBodySpans(context, message, null) + albumCellBodyText.text = body post { // post to await layout of text albumCellBodyText.layout?.let { layout -> diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt index 8f2410652d..8db00031f9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Color import android.graphics.Rect import android.graphics.drawable.Drawable +import android.text.Spannable import android.text.style.BackgroundColorSpan import android.text.style.ForegroundColorSpan import android.text.style.URLSpan @@ -28,21 +29,21 @@ import okhttp3.HttpUrl import org.session.libsession.utilities.ThemeUtil import org.session.libsession.utilities.ViewUtil import org.session.libsession.utilities.recipients.Recipient -import org.thoughtcrime.securesms.conversation.v2.components.AlbumThumbnailView import org.thoughtcrime.securesms.components.emoji.EmojiTextView import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2 +import org.thoughtcrime.securesms.conversation.v2.components.AlbumThumbnailView import org.thoughtcrime.securesms.conversation.v2.dialogs.OpenURLDialog import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities import org.thoughtcrime.securesms.conversation.v2.utilities.ModalURLSpan import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities.getIntersectedModalSpans import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MmsMessageRecord -import org.thoughtcrime.securesms.util.* import org.thoughtcrime.securesms.mms.GlideRequests import org.thoughtcrime.securesms.util.SearchUtil import org.thoughtcrime.securesms.util.SearchUtil.StyleFactory import org.thoughtcrime.securesms.util.UiModeUtilities -import java.net.IDN +import org.thoughtcrime.securesms.util.getColorWithID +import org.thoughtcrime.securesms.util.toPx import java.util.* import kotlin.math.roundToInt @@ -106,6 +107,10 @@ class VisibleMessageContentView : LinearLayout { quoteView.getGlobalVisibleRect(r) if (r.contains(event.rawX.roundToInt(), event.rawY.roundToInt())) { delegate?.scrollToMessageIfPossible(quote.id) + } else { + bodyTextView.getIntersectedModalSpans(event).forEach { span -> + span.onClick(bodyTextView) + } } } } else if (message is MmsMessageRecord && message.slideDeck.audioSlide != null) { @@ -209,7 +214,18 @@ class VisibleMessageContentView : LinearLayout { val color = getTextColor(context, message) result.setTextColor(color) result.setLinkTextColor(color) + val body = getBodySpans(context, message, searchQuery) + result.text = body + return result + } + + fun getBodySpans(context: Context, message: MessageRecord, searchQuery: String?): Spannable { var body = message.body.toSpannable() + + body = MentionUtilities.highlightMentions(body, message.isOutgoing, message.threadId, context) + body = SearchUtil.getHighlightedSpan(Locale.getDefault(), StyleFactory { BackgroundColorSpan(Color.WHITE) }, body, searchQuery) + body = SearchUtil.getHighlightedSpan(Locale.getDefault(), StyleFactory { ForegroundColorSpan(Color.BLACK) }, body, searchQuery) + Linkify.addLinks(body, Linkify.WEB_URLS) // replace URLSpans with ModalURLSpans @@ -225,13 +241,7 @@ class VisibleMessageContentView : LinearLayout { body.removeSpan(urlSpan) body.setSpan(replacementSpan, start, end, flags) } - - body = MentionUtilities.highlightMentions(body, message.isOutgoing, message.threadId, context) - body = SearchUtil.getHighlightedSpan(Locale.getDefault(), StyleFactory { BackgroundColorSpan(Color.WHITE) }, body, searchQuery) - body = SearchUtil.getHighlightedSpan(Locale.getDefault(), StyleFactory { ForegroundColorSpan(Color.BLACK) }, body, searchQuery) - - result.text = body - return result + return body } @ColorInt diff --git a/app/src/main/java/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java b/app/src/main/java/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java index 04edf1194a..68c568bb33 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.longmessage; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.text.Spannable; import android.text.method.LinkMovementMethod; import android.view.MenuItem; import android.widget.TextView; @@ -15,7 +16,7 @@ import org.session.libsession.utilities.Address; import org.session.libsession.utilities.Util; import org.session.libsession.utilities.recipients.Recipient; import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity; -import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities; +import org.thoughtcrime.securesms.conversation.v2.messages.VisibleMessageContentView; import network.loki.messenger.R; @@ -81,18 +82,10 @@ public class LongMessageActivity extends PassphraseRequiredActionBarActivity { String name = Util.getFirstNonEmpty(recipient.getName(), recipient.getProfileName(), recipient.getAddress().serialize()); getSupportActionBar().setTitle(getString(R.string.LongMessageActivity_message_from_s, name)); } - - String trimmedBody = getTrimmedBody(message.get().getFullBody()); - String mentionBody = MentionUtilities.highlightMentions(trimmedBody, message.get().getMessageRecord().getThreadId(), this); - - textBody.setText(mentionBody); + Spannable bodySpans = VisibleMessageContentView.Companion.getBodySpans(this, message.get().getMessageRecord(), null); + textBody.setText(bodySpans); textBody.setMovementMethod(LinkMovementMethod.getInstance()); }); } - private String getTrimmedBody(@NonNull String text) { - return text.length() <= MAX_DISPLAY_LENGTH ? text - : text.substring(0, MAX_DISPLAY_LENGTH); - } - } diff --git a/app/src/main/res/layout/album_thumbnail_view.xml b/app/src/main/res/layout/album_thumbnail_view.xml index 9c7ed02213..706964a205 100644 --- a/app/src/main/res/layout/album_thumbnail_view.xml +++ b/app/src/main/res/layout/album_thumbnail_view.xml @@ -52,7 +52,7 @@ android:layout_width="@dimen/accent_line_thickness" android:layout_height="match_parent" android:background="@color/accent"/> -