Java Code Examples for android.text.Spannable#toString()

The following examples show how to use android.text.Spannable#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowViewModel.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onChanged(Spannable aUrl) {
    String url = aUrl.toString();
    if (UrlUtils.isPrivateAboutPage(getApplication(), url) ||
            (UrlUtils.isDataUri(url) && isPrivateSession.getValue().get()) ||
            UrlUtils.isHomeUri(getApplication(), aUrl.toString()) ||
            isLibraryVisible.getValue().get() ||
            UrlUtils.isBlankUri(getApplication(), aUrl.toString())) {
        navigationBarUrl.postValue("");

    } else {
        navigationBarUrl.postValue(url);
    }

    hint.postValue(getHintValue());
}
 
Example 2
Source File: RichEditText.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Spannable matchTopic(Spannable spannable) {
    String text = spannable.toString();

    Pattern pattern = Pattern.compile(MATCH_TOPIC);
    Matcher matcher = pattern.matcher(text);

    while (matcher.find()) {
        String str = matcher.group();
        int matcherStart = matcher.start();
        int matcherEnd = matcher.end();
        spannable.setSpan(new TagSpan(str), matcherStart, matcherEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        log("matchTopic:" + str + " " + matcherStart + " " + matcherEnd);
    }

    return spannable;
}
 
Example 3
Source File: QBMessageTextClickMovement.java    From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String getLinkText(final TextView widget, final Spannable buffer, final MotionEvent event) {

            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

            if (link.length != 0) {
                return buffer.subSequence(buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0])).toString();
            }

            return buffer.toString();
        }
 
Example 4
Source File: DisplayUtils.java    From nextcloud-notes with GNU General Public License v3.0 6 votes vote down vote up
public static Spannable searchAndColor(Spannable spannable, CharSequence searchText, @NonNull Context context, @Nullable Integer current, @ColorInt int mainColor, @ColorInt int textColor) {
    CharSequence text = spannable.toString();

    Object[] spansToRemove = spannable.getSpans(0, text.length(), Object.class);
    for (Object span : spansToRemove) {
        if (span instanceof SearchSpan)
            spannable.removeSpan(span);
    }

    if (TextUtils.isEmpty(text) || TextUtils.isEmpty(searchText)) {
        return spannable;
    }

    Matcher m = Pattern.compile(searchText.toString(), Pattern.CASE_INSENSITIVE | Pattern.LITERAL)
            .matcher(text);

    int i = 1;
    while (m.find()) {
        int start = m.start();
        int end = m.end();
        spannable.setSpan(new SearchSpan(context, mainColor, textColor, (current != null && i == current)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        i++;
    }

    return spannable;
}
 
Example 5
Source File: WindowViewModel.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void onChanged(Spannable aUrl) {
    String url = aUrl.toString();
    if (isBookmarksVisible.getValue().get()) {
        url = getApplication().getString(R.string.url_bookmarks_title);

    } else if (isHistoryVisible.getValue().get()) {
        url = getApplication().getString(R.string.url_history_title);

    } else if (isDownloadsVisible.getValue().get()) {
        url = getApplication().getString(R.string.url_downloads_title);

    } else {
        if (UrlUtils.isPrivateAboutPage(getApplication(), url) ||
                (UrlUtils.isDataUri(url) && isPrivateSession.getValue().get())) {
            url = getApplication().getString(R.string.private_browsing_title);

        } else if (UrlUtils.isHomeUri(getApplication(), aUrl.toString())) {
            url = getApplication().getString(R.string.url_home_title, getApplication().getString(R.string.app_name));

        } else if (UrlUtils.isBlankUri(getApplication(), aUrl.toString())) {
            url = "";
        }
    }

    titleBarUrl.postValue(UrlUtils.titleBarUrl(url));
}
 
Example 6
Source File: RichEditText.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Spannable matchMention(Spannable spannable) {
    String text = spannable.toString();

    Pattern pattern = Pattern.compile(MATCH_MENTION);
    Matcher matcher = pattern.matcher(text);

    while (matcher.find()) {
        String str = matcher.group();
        int matcherStart = matcher.start();
        int matcherEnd = matcher.end();
        spannable.setSpan(new TagSpan(str), matcherStart, matcherEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        log("matchMention:" + str + " " + matcherStart + " " + matcherEnd);
    }
    return spannable;
}
 
Example 7
Source File: MessageFormatSettingsActivity.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private CharSequence prepareFormat(Spannable s) {
    SpannableString ret = new SpannableString(s.toString());
    for (Object span : s.getSpans(0, s.length(), CharacterStyle.class)) {
        if ((ret.getSpanFlags(span) & Spannable.SPAN_COMPOSING) != 0)
            continue;
        ret.setSpan(span, s.getSpanStart(span), s.getSpanEnd(span), s.getSpanFlags(span) & Spanned.SPAN_PRIORITY);
    }
    return ret;
}
 
Example 8
Source File: ListView.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Simple event to raise when the component is clicked. Implementation of
 * AdapterView.OnItemClickListener
 */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  Spannable item = (Spannable) parent.getAdapter().getItem(position);
  this.selection = item.toString();
  this.selectionIndex = adapterCopy.getPosition(item) + 1; // AI lists are 1-based

  AfterPicking();
}
 
Example 9
Source File: StyleSmallDecimal.java    From px-android with MIT License 5 votes vote down vote up
@Override
public Spannable apply(@Nullable final CharSequence charSequence) {
    final Currency currency = amountFormatter.currencyFormatter.currency;
    final Character decimalSeparator = currency.getDecimalSeparator();
    final Spannable localizedAmount = amountFormatter.apply(charSequence);
    final String totalText = localizedAmount.toString();
    return makeSmallAfterSeparator(decimalSeparator, totalText);
}
 
Example 10
Source File: MessageFormatter.java    From talk-android with MIT License 5 votes vote down vote up
/**
 * 将待发送文本格式化为纯文本
 *
 * @param s 待发送文本
 * @return 包含DSL的数据字符串
 */
public static String formatToPost(Spannable s) {
    if (s == null) {
        return "";
    }
    String result;
    // replace ActionSpan with DSL
    ActionSpan[] spans = s.getSpans(0, s.length(), ActionSpan.class);
    if (spans != null && spans.length > 0) {
        List<Integer> positions = new ArrayList<>();
        List<String> data = new ArrayList<>();
        for (ActionSpan span : spans) {
            positions.add(s.getSpanStart(span));
            data.add(span.getSource());
        }
        result = s.toString().substring(0, positions.get(0));
        for (int i = 0; i < positions.size(); i++) {
            result += data.get(i) + s.toString().substring(positions.get(i) + 1,
                    i == positions.size() - 1 ? s.toString().length() :
                            positions.get(i + 1));
        }
    } else {
        result = s.toString();
    }
    //replace emoji
    result = EmojiUtil.replaceUnicodeEmojis(result);
    return result;
}
 
Example 11
Source File: DataWrapper.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("SameParameterValue")
static String getProfileNameWithManualIndicatorAsString(
        Profile profile, boolean addEventName, String indicators, boolean addDuration, boolean multiLine,
        boolean durationInNextLine, DataWrapper dataWrapper) {
    Spannable sProfileName = getProfileNameWithManualIndicator(profile, addEventName, indicators, addDuration, multiLine, durationInNextLine, dataWrapper);
    Spannable sbt = new SpannableString(sProfileName);
    Object[] spansToRemove = sbt.getSpans(0, sProfileName.length(), Object.class);
    for (Object span : spansToRemove) {
        if (span instanceof CharacterStyle)
            sbt.removeSpan(span);
    }
    return sbt.toString();
}
 
Example 12
Source File: LinkifyCompat.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
private static final void gatherMapLinks(final ArrayList<LinkSpec> links,
        final Spannable s) {
    String string = s.toString();
    String address;
    int base = 0;

    while ((address = WebView.findAddress(string)) != null) {
        final int start = string.indexOf(address);

        if (start < 0) {
            break;
        }

        final LinkSpec spec = new LinkSpec();
        final int length = address.length();
        final int end = start + length;

        spec.start = base + start;
        spec.end = base + end;
        string = string.substring(end);
        base += end;

        String encodedAddress = null;

        try {
            encodedAddress = URLEncoder.encode(address, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            continue;
        }

        spec.url = "geo:0,0?q=" + encodedAddress;
        links.add(spec);
    }
}
 
Example 13
Source File: TextLinksParams.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Annotates the given text with the generated links. It will fail if the provided text doesn't
 * match the original text used to crete the TextLinks.
 *
 * @param text the text to apply the links to. Must match the original text
 * @param textLinks the links to apply to the text
 *
 * @return a status code indicating whether or not the links were successfully applied
 * @hide
 */
@TextLinks.Status
public int apply(@NonNull Spannable text, @NonNull TextLinks textLinks) {
    Preconditions.checkNotNull(text);
    Preconditions.checkNotNull(textLinks);

    final String textString = text.toString();

    if (Linkify.containsUnsupportedCharacters(textString)) {
        // Do not apply links to text containing unsupported characters.
        android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, "");
        return TextLinks.STATUS_NO_LINKS_APPLIED;
    }

    if (!textString.startsWith(textLinks.getText())) {
        return TextLinks.STATUS_DIFFERENT_TEXT;
    }
    if (textLinks.getLinks().isEmpty()) {
        return TextLinks.STATUS_NO_LINKS_FOUND;
    }

    int applyCount = 0;
    for (TextLink link : textLinks.getLinks()) {
        final TextLinkSpan span = mSpanFactory.apply(link);
        if (span != null) {
            final ClickableSpan[] existingSpans = text.getSpans(
                    link.getStart(), link.getEnd(), ClickableSpan.class);
            if (existingSpans.length > 0) {
                if (mApplyStrategy == TextLinks.APPLY_STRATEGY_REPLACE) {
                    for (ClickableSpan existingSpan : existingSpans) {
                        text.removeSpan(existingSpan);
                    }
                    text.setSpan(span, link.getStart(), link.getEnd(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    applyCount++;
                }
            } else {
                text.setSpan(span, link.getStart(), link.getEnd(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                applyCount++;
            }
        }
    }
    if (applyCount == 0) {
        return TextLinks.STATUS_NO_LINKS_APPLIED;
    }
    return TextLinks.STATUS_LINKS_APPLIED;
}
 
Example 14
Source File: AccessibilityIterators.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public void initialize(Spannable text, Layout layout) {
    mText = text.toString();
    mLayout = layout;
}
 
Example 15
Source File: WindowViewModel.java    From FirefoxReality with Mozilla Public License 2.0 4 votes vote down vote up
public void setUrl(@Nullable Spannable url) {
    if (url == null) {
        return;
    }

    String aURL = url.toString();

    int index = -1;
    try {
        aURL = URLDecoder.decode(aURL, "UTF-8");

    } catch (UnsupportedEncodingException | IllegalArgumentException e) {
        e.printStackTrace();
        aURL = "";
    }
    if (aURL.startsWith("jar:")) {
        return;

    } else if (aURL.startsWith("resource:") || UrlUtils.isHomeUri(getApplication().getBaseContext(), aURL)) {
        aURL = "";

    } else if (aURL.startsWith("data:") && isPrivateSession.getValue().get()) {
        aURL = "";

    } else if (aURL.startsWith(getApplication().getBaseContext().getString(R.string.about_blank))) {
        aURL = "";

    } else {
        index = aURL.indexOf("://");
    }

    // Update the URL bar only if the URL is different than the current one and
    // the URL bar is not focused to avoid override user input
    if (!getUrl().getValue().toString().equalsIgnoreCase(aURL) && !getIsFocused().getValue().get()) {
        this.url.postValue(new SpannableString(aURL));
        if (index > 0) {
            SpannableString spannable = new SpannableString(aURL);
            ForegroundColorSpan color1 = new ForegroundColorSpan(mURLProtocolColor);
            ForegroundColorSpan color2 = new ForegroundColorSpan(mURLWebsiteColor);
            spannable.setSpan(color1, 0, index + 3, 0);
            spannable.setSpan(color2, index + 3, aURL.length(), 0);
            this.url.postValue(url);

        } else {
            this.url.postValue(url);
        }
    }

    this.url.postValue(url);
}