Search in sources :

Example 1 with Linkify

use of android.text.util.Linkify in project Signal-Android by WhisperSystems.

the class GroupDescriptionUtil method setText.

/**
 * Set a group description.
 *
 * @param description   full description
 * @param emojiTextView Text view to update with description
 * @param linkify       flag indicating if web urls should be linkified
 * @param moreClick     Callback for when truncating and need to show more via another means. Required to enable truncating.
 */
public static void setText(@NonNull Context context, @NonNull EmojiTextView emojiTextView, @NonNull String description, boolean linkify, @Nullable Runnable moreClick) {
    boolean shouldEllipsize = moreClick != null;
    String scrubbedDescription = shouldEllipsize ? description.replaceAll("\\n", " ") : description;
    SpannableString descriptionSpannable = new SpannableString(scrubbedDescription);
    if (linkify) {
        int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS;
        boolean hasLinks = LinkifyCompat.addLinks(descriptionSpannable, linkPattern);
        if (hasLinks) {
            Stream.of(descriptionSpannable.getSpans(0, descriptionSpannable.length(), URLSpan.class)).filterNot(url -> LinkPreviewUtil.isLegalUrl(url.getURL())).forEach(descriptionSpannable::removeSpan);
            URLSpan[] urlSpans = descriptionSpannable.getSpans(0, descriptionSpannable.length(), URLSpan.class);
            for (URLSpan urlSpan : urlSpans) {
                int start = descriptionSpannable.getSpanStart(urlSpan);
                int end = descriptionSpannable.getSpanEnd(urlSpan);
                URLSpan span = new LongClickCopySpan(urlSpan.getURL());
                descriptionSpannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    if (shouldEllipsize) {
        ClickableSpan style = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                moreClick.run();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setTypeface(Typeface.DEFAULT_BOLD);
            }
        };
        emojiTextView.setEllipsize(TextUtils.TruncateAt.END);
        emojiTextView.setMaxLines(2);
        SpannableString overflowText = new SpannableString(context.getString(R.string.ManageGroupActivity_more));
        overflowText.setSpan(style, 0, overflowText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        emojiTextView.setOverflowText(overflowText);
    }
    emojiTextView.setText(descriptionSpannable);
}
Also used : Typeface(android.graphics.Typeface) Context(android.content.Context) Spanned(android.text.Spanned) URLSpan(android.text.style.URLSpan) Stream(com.annimon.stream.Stream) SpannableString(android.text.SpannableString) NonNull(androidx.annotation.NonNull) EmojiTextView(org.thoughtcrime.securesms.components.emoji.EmojiTextView) TextUtils(android.text.TextUtils) ClickableSpan(android.text.style.ClickableSpan) Linkify(android.text.util.Linkify) R(org.thoughtcrime.securesms.R) LinkPreviewUtil(org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil) LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) Nullable(androidx.annotation.Nullable) TextPaint(android.text.TextPaint) LinkifyCompat(androidx.core.text.util.LinkifyCompat) View(android.view.View) LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan) EmojiTextView(org.thoughtcrime.securesms.components.emoji.EmojiTextView) View(android.view.View) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) SpannableString(android.text.SpannableString) NonNull(androidx.annotation.NonNull)

Aggregations

Context (android.content.Context)1 Typeface (android.graphics.Typeface)1 SpannableString (android.text.SpannableString)1 Spanned (android.text.Spanned)1 TextPaint (android.text.TextPaint)1 TextUtils (android.text.TextUtils)1 ClickableSpan (android.text.style.ClickableSpan)1 URLSpan (android.text.style.URLSpan)1 Linkify (android.text.util.Linkify)1 View (android.view.View)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 LinkifyCompat (androidx.core.text.util.LinkifyCompat)1 Stream (com.annimon.stream.Stream)1 R (org.thoughtcrime.securesms.R)1 EmojiTextView (org.thoughtcrime.securesms.components.emoji.EmojiTextView)1 LinkPreviewUtil (org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil)1 LongClickCopySpan (org.thoughtcrime.securesms.util.LongClickCopySpan)1