Search in sources :

Example 1 with LongClickCopySpan

use of org.thoughtcrime.securesms.util.LongClickCopySpan in project Signal-Android by signalapp.

the class SubmitDebugLogActivity method presentResultDialog.

private void presentResultDialog(@NonNull String url) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(R.string.SubmitDebugLogActivity_success).setCancelable(false).setNeutralButton(android.R.string.ok, (d, w) -> finish()).setPositiveButton(R.string.SubmitDebugLogActivity_share, (d, w) -> {
        ShareCompat.IntentBuilder.from(this).setText(url).setType("text/plain").setEmailTo(new String[] { "support@signal.org" }).startChooser();
    });
    String dialogText = getResources().getString(R.string.SubmitDebugLogActivity_copy_this_url_and_add_it_to_your_issue, url);
    SpannableString spannableDialogText = new SpannableString(dialogText);
    TextView dialogView = new TextView(builder.getContext());
    LongClickCopySpan longClickUrl = new LongClickCopySpan(url);
    LinkifyCompat.addLinks(spannableDialogText, Linkify.WEB_URLS);
    URLSpan[] spans = spannableDialogText.getSpans(0, spannableDialogText.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = spannableDialogText.getSpanStart(span);
        int end = spannableDialogText.getSpanEnd(span);
        spannableDialogText.setSpan(longClickUrl, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    dialogView.setText(spannableDialogText);
    dialogView.setMovementMethod(LongClickMovementMethod.getInstance(this));
    ViewUtil.setPadding(dialogView, (int) ThemeUtil.getThemedDimen(this, R.attr.dialogPreferredPadding));
    builder.setView(dialogView);
    builder.show();
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) Bundle(android.os.Bundle) Spanned(android.text.Spanned) AlertDialog(androidx.appcompat.app.AlertDialog) NonNull(androidx.annotation.NonNull) SimpleProgressDialog(org.thoughtcrime.securesms.util.views.SimpleProgressDialog) LongClickMovementMethod(org.thoughtcrime.securesms.util.LongClickMovementMethod) ShareCompat(androidx.core.app.ShareCompat) ViewUtil(org.thoughtcrime.securesms.util.ViewUtil) Linkify(android.text.util.Linkify) R(org.thoughtcrime.securesms.R) MenuItem(android.view.MenuItem) DynamicTheme(org.thoughtcrime.securesms.util.DynamicTheme) LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) Toast(android.widget.Toast) Menu(android.view.Menu) View(android.view.View) ViewModelProviders(androidx.lifecycle.ViewModelProviders) RecyclerView(androidx.recyclerview.widget.RecyclerView) BaseActivity(org.thoughtcrime.securesms.BaseActivity) CircularProgressButton(com.dd.CircularProgressButton) URLSpan(android.text.style.URLSpan) SpannableString(android.text.SpannableString) SearchView(androidx.appcompat.widget.SearchView) List(java.util.List) TextView(android.widget.TextView) ThemeUtil(org.thoughtcrime.securesms.util.ThemeUtil) LinkifyCompat(androidx.core.text.util.LinkifyCompat) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SpannableString(android.text.SpannableString) LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) TextView(android.widget.TextView) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan)

Example 2 with LongClickCopySpan

use of org.thoughtcrime.securesms.util.LongClickCopySpan in project Signal-Android by signalapp.

the class ConversationItem method linkifyMessageBody.

private SpannableString linkifyMessageBody(SpannableString messageBody, boolean shouldLinkifyAllLinks) {
    boolean hasLinks = Linkify.addLinks(messageBody, shouldLinkifyAllLinks ? Linkify.ALL : 0);
    if (hasLinks) {
        URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
        for (URLSpan urlSpan : urlSpans) {
            int start = messageBody.getSpanStart(urlSpan);
            int end = messageBody.getSpanEnd(urlSpan);
            messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return messageBody;
}
Also used : LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) URLSpan(android.text.style.URLSpan) SuppressLint(android.annotation.SuppressLint)

Example 3 with LongClickCopySpan

use of org.thoughtcrime.securesms.util.LongClickCopySpan in project Signal-Android by WhisperSystems.

the class SubmitDebugLogActivity method presentResultDialog.

private void presentResultDialog(@NonNull String url) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(R.string.SubmitDebugLogActivity_success).setCancelable(false).setNeutralButton(android.R.string.ok, (d, w) -> finish()).setPositiveButton(R.string.SubmitDebugLogActivity_share, (d, w) -> {
        ShareCompat.IntentBuilder.from(this).setText(url).setType("text/plain").setEmailTo(new String[] { "support@signal.org" }).startChooser();
    });
    String dialogText = getResources().getString(R.string.SubmitDebugLogActivity_copy_this_url_and_add_it_to_your_issue, url);
    SpannableString spannableDialogText = new SpannableString(dialogText);
    TextView dialogView = new TextView(builder.getContext());
    LongClickCopySpan longClickUrl = new LongClickCopySpan(url);
    LinkifyCompat.addLinks(spannableDialogText, Linkify.WEB_URLS);
    URLSpan[] spans = spannableDialogText.getSpans(0, spannableDialogText.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = spannableDialogText.getSpanStart(span);
        int end = spannableDialogText.getSpanEnd(span);
        spannableDialogText.setSpan(longClickUrl, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    dialogView.setText(spannableDialogText);
    dialogView.setMovementMethod(LongClickMovementMethod.getInstance(this));
    ViewUtil.setPadding(dialogView, (int) ThemeUtil.getThemedDimen(this, R.attr.dialogPreferredPadding));
    builder.setView(dialogView);
    builder.show();
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) Bundle(android.os.Bundle) Spanned(android.text.Spanned) AlertDialog(androidx.appcompat.app.AlertDialog) NonNull(androidx.annotation.NonNull) SimpleProgressDialog(org.thoughtcrime.securesms.util.views.SimpleProgressDialog) LongClickMovementMethod(org.thoughtcrime.securesms.util.LongClickMovementMethod) ShareCompat(androidx.core.app.ShareCompat) ViewUtil(org.thoughtcrime.securesms.util.ViewUtil) Linkify(android.text.util.Linkify) R(org.thoughtcrime.securesms.R) MenuItem(android.view.MenuItem) DynamicTheme(org.thoughtcrime.securesms.util.DynamicTheme) LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) Toast(android.widget.Toast) Menu(android.view.Menu) View(android.view.View) ViewModelProviders(androidx.lifecycle.ViewModelProviders) RecyclerView(androidx.recyclerview.widget.RecyclerView) BaseActivity(org.thoughtcrime.securesms.BaseActivity) CircularProgressButton(com.dd.CircularProgressButton) URLSpan(android.text.style.URLSpan) SpannableString(android.text.SpannableString) SearchView(androidx.appcompat.widget.SearchView) List(java.util.List) TextView(android.widget.TextView) ThemeUtil(org.thoughtcrime.securesms.util.ThemeUtil) LinkifyCompat(androidx.core.text.util.LinkifyCompat) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SpannableString(android.text.SpannableString) LongClickCopySpan(org.thoughtcrime.securesms.util.LongClickCopySpan) TextView(android.widget.TextView) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan)

Example 4 with LongClickCopySpan

use of org.thoughtcrime.securesms.util.LongClickCopySpan 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)

Example 5 with LongClickCopySpan

use of org.thoughtcrime.securesms.util.LongClickCopySpan in project Signal-Android by signalapp.

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

URLSpan (android.text.style.URLSpan)5 LongClickCopySpan (org.thoughtcrime.securesms.util.LongClickCopySpan)5 SpannableString (android.text.SpannableString)4 Spanned (android.text.Spanned)4 Linkify (android.text.util.Linkify)4 View (android.view.View)4 NonNull (androidx.annotation.NonNull)4 LinkifyCompat (androidx.core.text.util.LinkifyCompat)4 R (org.thoughtcrime.securesms.R)4 Context (android.content.Context)2 Typeface (android.graphics.Typeface)2 Bundle (android.os.Bundle)2 TextPaint (android.text.TextPaint)2 TextUtils (android.text.TextUtils)2 ClickableSpan (android.text.style.ClickableSpan)2 Menu (android.view.Menu)2 MenuItem (android.view.MenuItem)2 TextView (android.widget.TextView)2 Toast (android.widget.Toast)2 Nullable (androidx.annotation.Nullable)2