From 733d54e339b8f2dd07e26e20c176163a1ea954ca Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 4 Apr 2019 10:09:32 -0400 Subject: [PATCH] Linkify links in long message view. Fixes #8735 --- .../longmessage/LongMessageActivity.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java b/src/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java index e7efcaa081..c8171520fd 100644 --- a/src/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java +++ b/src/org/thoughtcrime/securesms/longmessage/LongMessageActivity.java @@ -8,17 +8,24 @@ import android.graphics.drawable.ColorDrawable; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; +import android.text.SpannableString; +import android.text.method.LinkMovementMethod; +import android.text.style.URLSpan; +import android.text.util.Linkify; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; +import com.annimon.stream.Stream; + import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.color.MaterialColor; import org.thoughtcrime.securesms.components.ConversationItemFooter; import org.thoughtcrime.securesms.database.Address; +import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientModifiedListener; import org.thoughtcrime.securesms.util.DynamicLanguage; @@ -142,8 +149,21 @@ public class LongMessageActivity extends PassphraseRequiredActionBarActivity imp ConversationItemFooter footer = bubble.findViewById(R.id.longmessage_footer); bubble.setVisibility(View.VISIBLE); - text.setText(message.get().getFullBody()); + text.setText(linkifyMessageBody(new SpannableString(message.get().getFullBody()))); + text.setMovementMethod(LinkMovementMethod.getInstance()); footer.setMessageRecord(message.get().getMessageRecord(), dynamicLanguage.getCurrentLocale()); }); } + + private SpannableString linkifyMessageBody(SpannableString messageBody) { + int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS; + boolean hasLinks = Linkify.addLinks(messageBody, linkPattern); + + if (hasLinks) { + Stream.of(messageBody.getSpans(0, messageBody.length(), URLSpan.class)) + .filterNot(url -> LinkPreviewUtil.isLegalUrl(url.getURL())) + .forEach(messageBody::removeSpan); + } + return messageBody; + } }