Java Code Examples for android.text.Spanned#charAt()

The following examples show how to use android.text.Spanned#charAt() . 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: Utilities.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
private static Spanned removeTrailingNewlines(Spanned text) {
  int trailingNewlineCharacterCount = 0;
  for (int i = text.length() - 1; i >= 0; i--) {
    char c = text.charAt(i);
    if ((c == '\n') || (c == '\r')) {
      trailingNewlineCharacterCount++;
    } else {
      break;
    }
  }
  if (trailingNewlineCharacterCount == 0) {
    return text;
  }

  return new SpannedString(
      text.subSequence(0, text.length() - trailingNewlineCharacterCount));
}
 
Example 2
Source File: HighlightingEditor.java    From writeily-pro with MIT License 6 votes vote down vote up
private String createIndentForNextLine(Spanned dest, int dend, int istart) {
    if (istart > -1) {
        int iend;

        for (iend = ++istart;
             iend < dend;
             ++iend) {
            char c = dest.charAt(iend);

            if (c != ' ' &&
                    c != '\t') {
                break;
            }
        }

        return dest.subSequence(istart, iend) + addBulletPointIfNeeded(dest.charAt(iend));
    } else {
        return "";
    }
}
 
Example 3
Source File: KnifeParser.java    From Knife with Apache License 2.0 6 votes vote down vote up
private static void withinContent(StringBuilder out, Spanned text, int start, int end) {
    int next;

    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;
        while (next < end && text.charAt(next) == '\n') {
            next++;
            nl++;
        }

        withinParagraph(out, text, i, next - nl, nl);
    }
}
 
Example 4
Source File: EmailHtmlUtil.java    From RichEditText with Apache License 2.0 6 votes vote down vote up
private static void withinBlockquote(StringBuilder out, Spanned text,
		int start, int end) {
	out.append(getOpenParaTagWithDirection(text, start, end));

	int next;
	for (int i = start; i < end; i = next) {
		next = TextUtils.indexOf(text, '\n', i, end);
		if (next < 0) {
			next = end;
		}

		int nl = 0;

		while (next < end && text.charAt(next) == '\n') {
			nl++;
			next++;
		}

		withinParagraph(out, text, i, next - nl, nl, next == end);
	}

	out.append("</p>\n");
}
 
Example 5
Source File: RecipientsEditor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getNumbers() {
    Spanned sp = mList.getText();
    int len = sp.length();
    List<String> list = new ArrayList<String>();

    int start = 0;
    int i = 0;
    while (i < len + 1) {
        char c;
        if ((i == len) || ((c = sp.charAt(i)) == ',') || (c == ';')) {
            if (i > start) {
                list.add(getNumberAt(sp, start, i, mContext));

                // calculate the recipients total length. This is so if the name contains
                // commas or semis, we'll skip over the whole name to the next
                // recipient, rather than parsing this single name into multiple
                // recipients.
                int spanLen = getSpanLength(sp, start, i, mContext);
                if (spanLen > i) {
                    i = spanLen;
                }
            }

            i++;

            while ((i < len) && (sp.charAt(i) == ' ')) {
                i++;
            }

            start = i;
        } else {
            i++;
        }
    }

    return list;
}
 
Example 6
Source File: AKHtml.java    From Mupdf with Apache License 2.0 5 votes vote down vote up
private static void withinBlockquoteConsecutive(StringBuilder out, Spanned text, int start,
                                                int end) {
    out.append("<p").append(getTextDirection(text, start, end)).append(">");

    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;

        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }

        withinParagraph(out, text, i, next - nl);

        if (nl == 1) {
            out.append("<br>\n");
        } else {
            for (int j = 2; j < nl; j++) {
                out.append("<br>");
            }
            if (next != end) {
                /* Paragraph should be closed and reopened */
                out.append("</p>\n");
                out.append("<p").append(getTextDirection(text, start, end)).append(">");
            }
        }
    }

    out.append("</p>\n");
}
 
Example 7
Source File: Html.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private static void withinBlockquote(StringBuilder out, Spanned text,
        int start, int end) {
    out.append(getOpenParaTagWithDirection(text, start, end));

    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;

        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }

        if (withinParagraph(out, text, i, next - nl, nl, next == end)) {
            /* Paragraph should be closed */
            out.append("</p>\n");
            out.append(getOpenParaTagWithDirection(text, next, end));
        }
    }

    out.append("</p>\n");
}
 
Example 8
Source File: PosOrZeroIntegerRow.java    From dhis2-android-datacapture with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CharSequence filter(CharSequence str, int start, int end,
        Spanned spn, int spStart, int spEnd) {
    
    if ((str.length() > 0) && (spn.length() > 0) && (spn.charAt(0) == '0')) {
        return Field.EMPTY_FIELD;
    }
    
    if ((spn.length() > 0) && (spStart == 0) && (str.length() > 0) && (str.charAt(0) == '0')) {
        return Field.EMPTY_FIELD;
    }
   
    return str;
}
 
Example 9
Source File: HighlightingEditor.java    From writeily-pro with MIT License 5 votes vote down vote up
private int findLineBreakPosition(Spanned dest, int dstart) {
    int istart = dstart - 1;

    for (; istart > -1; --istart) {
        char c = dest.charAt(istart);

        if (c == '\n')
            break;
    }
    return istart;
}
 
Example 10
Source File: RecipientsEditor.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getNumbers() {
    Spanned sp = mList.getText();
    int len = sp.length();
    List<String> list = new ArrayList<String>();

    int start = 0;
    int i = 0;
    while (i < len + 1) {
        char c;
        if ((i == len) || ((c = sp.charAt(i)) == ',') || (c == ';')) {
            if (i > start) {
                list.add(getNumberAt(sp, start, i, mContext));

                // calculate the recipients total length. This is so if the name contains
                // commas or semis, we'll skip over the whole name to the next
                // recipient, rather than parsing this single name into multiple
                // recipients.
                int spanLen = getSpanLength(sp, start, i, mContext);
                if (spanLen > i) {
                    i = spanLen;
                }
            }

            i++;

            while ((i < len) && (sp.charAt(i) == ' ')) {
                i++;
            }

            start = i;
        } else {
            i++;
        }
    }

    return list;
}
 
Example 11
Source File: Html.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private static void withinBlockquote(StringBuilder out, Spanned text,
        int start, int end) {
    out.append(getOpenParaTagWithDirection(text, start, end));

    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;

        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }

        if (withinParagraph(out, text, i, next - nl, nl, next == end)) {
            /* Paragraph should be closed */
            out.append("</p>\n");
            out.append(getOpenParaTagWithDirection(text, next, end));
        }
    }

    out.append("</p>\n");
}
 
Example 12
Source File: MyHtml.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private static void withinBlockquote(StringBuilder out, Spanned text,
                                     int start, int end) {
    out.append(getOpenParaTagWithDirection(text, start, end));

    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;

        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }

        if (withinParagraph(out, text, i, next - nl, nl, next == end)) {
            /* Paragraph should be closed */
            out.append("</p>\n");
            out.append(getOpenParaTagWithDirection(text, next, end));
        }
    }

    out.append("</p>\n");
}
 
Example 13
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
private static void withinBlockquoteConsecutive(StringBuilder out, Spanned text, int start,
                                                int end) {
    out.append("<p").append(getTextDirection(text, start, end)).append(">");
    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }
        int nl = 0;
        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }
        withinParagraph(out, text, i, next - nl);
        if (nl == 1) {
            out.append("<br>\n");
        } else {
            for (int j = 2; j < nl; j++) {
                out.append("<br>");
            }
            if (next != end) {
                /* Paragraph should be closed and reopened */
                out.append("</p>\n");
                out.append("<p").append(getTextDirection(text, start, end)).append(">");
            }
        }
    }
    out.append("</p>\n");
}
 
Example 14
Source File: HtmlCompat.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
private static void withinBlockquoteConsecutive(Context context, StringBuilder out, Spanned text,
                                                int start, int end) {
    out.append("<p").append(getTextDirection(text, start, end)).append(">");
    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }
        int nl = 0;
        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }
        withinParagraph(context, out, text, i, next - nl);
        if (nl == 1) {
            out.append("<br>\n");
        } else {
            for (int j = 2; j < nl; j++) {
                out.append("<br>");
            }
            if (next != end) {
                /* Paragraph should be closed and reopened */
                out.append("</p>\n");
                out.append("<p").append(getTextDirection(text, start, end)).append(">");
            }
        }
    }
    out.append("</p>\n");
}
 
Example 15
Source File: HtmlEx.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private /* static */ void withinBlockquoteConsecutive(StringBuilder out, Spanned text, int start,
                                                int end) {
    out.append("<p").append(getTextDirection(text, start, end)).append(">");

    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;

        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }

        withinParagraph(out, text, i, next - nl);

        if (nl == 1) {
            out.append("<br>\n");
        } else {
            for (int j = 2; j < nl; j++) {
                out.append("<br>");
            }
            if (next != end) {
                /* Paragraph should be closed and reopened */
                out.append("</p>\n");
                out.append("<p").append(getTextDirection(text, start, end)).append(">");
            }
        }
    }

    out.append("</p>\n");
}
 
Example 16
Source File: DecimalDigitsInputFilter.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CharSequence filter(CharSequence source,
                           int start,
                           int end,
                           Spanned dest,
                           int dstart,
                           int dend) {
    int dotPos = -1;
    int len = dest.length();
    for (int i = 0; i < len; i++) {
        char c = dest.charAt(i);
        if (c == '.' || c == ',') {
            dotPos = i;
            break;
        }
    }
    if (dotPos >= 0) { // protects against many dots
        if (source.equals(".") || source.equals(",")) {
            return "";
        } // if the text is entered before the dot
        if (dend <= dotPos) {
            return null;
        }
        if (len - dotPos > decimalDigits) {
            return "";
        }
    }
    return null;
}
 
Example 17
Source File: Html.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private static void withinBlockquote(StringBuilder out, Spanned text,
        int start, int end) {
    out.append(getOpenParaTagWithDirection(text, start, end));

    int next;
    for (int i = start; i < end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        int nl = 0;

        while (next < end && text.charAt(next) == '\n') {
            nl++;
            next++;
        }

        if (withinParagraph(out, text, i, next - nl, nl, next == end)) {
            /* Paragraph should be closed */
            out.append("</p>\n");
            out.append(getOpenParaTagWithDirection(text, next, end));
        }
    }

    out.append("</p>\n");
}
 
Example 18
Source File: RecipientsEditor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getNumbers() {
    Spanned sp = mList.getText();
    int len = sp.length();
    List<String> list = new ArrayList<String>();

    int start = 0;
    int i = 0;
    while (i < len + 1) {
        char c;
        if ((i == len) || ((c = sp.charAt(i)) == ',') || (c == ';')) {
            if (i > start) {
                list.add(getNumberAt(sp, start, i, mContext));

                // calculate the recipients total length. This is so if the name contains
                // commas or semis, we'll skip over the whole name to the next
                // recipient, rather than parsing this single name into multiple
                // recipients.
                int spanLen = getSpanLength(sp, start, i, mContext);
                if (spanLen > i) {
                    i = spanLen;
                }
            }

            i++;

            while ((i < len) && (sp.charAt(i) == ' ')) {
                i++;
            }

            start = i;
        } else {
            i++;
        }
    }

    return list;
}
 
Example 19
Source File: WordTokenizer.java    From Spyglass with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int findTokenStart(final @NonNull Spanned text, final int cursor) {
    int start = getSearchStartIndex(text, cursor);
    int i = cursor;

    // If it is explicit, return the index of the first explicit character
    if (isExplicit(text, cursor)) {
        i--;
        while (i >= start) {
            char currentChar = text.charAt(i);
            if (isExplicitChar(currentChar)) {
                if (i == 0 || isWordBreakingChar(text.charAt(i - 1))) {
                    return i;
                }
            }
            i--;
        }
        // Could not find explicit character before the cursor
        // Note: This case should never happen (means that isExplicit
        // returned true when it should have been false)
        return -1;

    } else {

        // For implicit tokens, we need to go back a certain number of words to find the start
        // of the token (with the max number of words to go back defined in the config)
        int maxNumKeywords = mConfig.MAX_NUM_KEYWORDS;

        // Go back to the start of the word that the cursor is currently in
        while (i > start && !isWordBreakingChar(text.charAt(i - 1))) {
            i--;
        }

        // Cursor is at beginning of current word, go back MaxNumKeywords - 1 now
        for (int j = 0; j < maxNumKeywords - 1; j++) {
            // Decrement through only one word-breaking character, if it exists
            if (i > start && isWordBreakingChar(text.charAt(i - 1))) {
                i--;
            }
            // If there is more than one word-breaking space, break out now
            // Do not consider queries with words separated by more than one word-breaking char
            if (i > start && isWordBreakingChar(text.charAt(i - 1))) {
                break;
            }
            // Decrement until the next space
            while (i > start && !isWordBreakingChar(text.charAt(i - 1))) {
                i--;
            }
        }

        // Ensures that text.char(i) is not a word-breaking or explicit char (i.e. cursor must have a
        // word-breaking char in front of it and a non-word-breaking char behind it)
        while (i < cursor && (isWordBreakingChar(text.charAt(i)) || isExplicitChar(text.charAt(i)))) {
            i++;
        }

        return i;
    }
}
 
Example 20
Source File: Html.java    From tysq-android with GNU General Public License v3.0 4 votes vote down vote up
private static void withinBlockquoteConsecutive(StringBuilder out, Spanned text, int start,
                                                    int end) {
        out.append("<p").append(getTextDirection(text, start, end)).append(">");

        int next;
        for (int i = start; i < end; i = next) {
            next = TextUtils.indexOf(text, '\n', i, end);
            if (next < 0) {
                next = end;
            }

            int nl = 0;

            while (next < end && text.charAt(next) == '\n') {
                nl++;
                next++;
            }

            withinParagraph(out, text, i, next - nl);

            if (nl == 1) {
                // xxx
//                out.append("<br>\n");
                out.append("<br>");
            } else {
                for (int j = 2; j < nl; j++) {
                    out.append("<br>");
                }
                if (next != end) {
                    /* Paragraph should be closed and reopened */
                    // xxx
//                    out.append("</p>\n");
                    out.append("</p>");
                    out.append("<p").append(getTextDirection(text, start, end)).append(">");
                }
            }
        }
        // xxx
//        out.append("</p>\n");
        out.append("</p>");
    }