Java Code Examples for android.text.TextUtils.indexOf()

The following are Jave code examples for showing how to use indexOf() of the android.text.TextUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: airgram   File: MessageObject.java   View Source Code Vote up 6 votes
public CharSequence replaceWithLink(CharSequence source, String param, ArrayList<Integer> uids, AbstractMap<Integer, TLRPC.User> usersDict) {
    int start = TextUtils.indexOf(source, param);
    if (start >= 0) {
        SpannableStringBuilder names = new SpannableStringBuilder("");
        for (int a = 0; a < uids.size(); a++) {
            TLRPC.User user = null;
            if (usersDict != null) {
                user = usersDict.get(uids.get(a));
            }
            if (user == null) {
                user = MessagesController.getInstance().getUser(uids.get(a));
            }
            if (user != null) {
                String name = UserObject.getUserName(user);
                start = names.length();
                if (names.length() != 0) {
                    names.append(", ");
                }
                names.append(name);
                names.setSpan(new URLSpanNoUnderlineBold("" + user.id), start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return TextUtils.replace(source, new String[]{param}, new CharSequence[]{names});
    }
    return source;
}
 
Example 2
Project: airgram   File: MessageObject.java   View Source Code Vote up 6 votes
public CharSequence replaceWithLink(CharSequence source, String param, TLObject object) {
    int start = TextUtils.indexOf(source, param);
    if (start >= 0) {
        String name;
        int id;
        if (object instanceof TLRPC.User) {
            name = UserObject.getUserName((TLRPC.User) object);
            id = ((TLRPC.User) object).id;
        } else if (object instanceof TLRPC.Chat) {
            name = ((TLRPC.Chat) object).title;
            id = -((TLRPC.Chat) object).id;
        } else {
            name = "";
            id = 0;
        }
        SpannableStringBuilder builder = new SpannableStringBuilder(TextUtils.replace(source, new String[]{param}, new String[]{name}));
        builder.setSpan(new URLSpanNoUnderlineBold("" + id), start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }
    return source;
}
 
Example 3
Project: BBSSDK-for-Android   File: RichEditor.java   View Source Code Vote up 6 votes
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
	String decode;
	try {
		decode = URLDecoder.decode(url, "UTF-8");
	} catch (UnsupportedEncodingException e) {
		// No handling
		return false;
	}

	if (TextUtils.indexOf(url, CALLBACK_SCHEME) == 0) {
		callback(decode);
		return true;
	} else if (TextUtils.indexOf(url, STATE_SCHEME) == 0) {
		stateCheck(decode);
		return true;
	}

	return super.shouldOverrideUrlLoading(view, url);
}
 
Example 4
Project: riched_editor_x5   File: RichEditor.java   View Source Code Vote up 6 votes
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
  String decode;
  try {
    decode = URLDecoder.decode(url, "UTF-8");
  } catch (UnsupportedEncodingException e) {
    // No handling
    return false;
  }

  if (TextUtils.indexOf(url, CALLBACK_SCHEME) == 0) {
    callback(decode);
    return true;
  } else if (TextUtils.indexOf(url, STATE_SCHEME) == 0) {
    stateCheck(decode);
    return true;
  }

  return super.shouldOverrideUrlLoading(view, url);
}
 
Example 5
Project: IslamicLibraryAndroid   File: SearchResult.java   View Source Code Vote up 6 votes
public CharSequence getformatedSearchSnippet() {
    //TODO implement improved snippet function
    String CleanedSearchString = " " + ArabicUtilities.cleanTextForSearchingWthStingBuilder(searchString) + " ";
    StringBuilder cleanedUnformattedPage = new StringBuilder(ArabicUtilities.cleanTextForSearchingWthStingBuilder(unformatedPage));
    int firstMatchStart = cleanedUnformattedPage.indexOf(CleanedSearchString);
    cleanedUnformattedPage.delete(0, Math.max(firstMatchStart - 100, 0));
    cleanedUnformattedPage.delete(
            Math.min(firstMatchStart + CleanedSearchString.length() + 100, cleanedUnformattedPage.length())
            , cleanedUnformattedPage.length());
    cleanedUnformattedPage.insert(0, "...");
    cleanedUnformattedPage.append("...");

    Spannable snippet = SpannableString.
            valueOf(cleanedUnformattedPage.toString());
    int index = TextUtils.indexOf(snippet, CleanedSearchString);
    while (index >= 0) {

        snippet.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
                + CleanedSearchString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = TextUtils.indexOf(snippet, CleanedSearchString, index + CleanedSearchString.length());
    }

    return snippet;
}
 
Example 6
Project: PlusGram   File: MessageObject.java   View Source Code Vote up 6 votes
public CharSequence replaceWithLink(CharSequence source, String param, ArrayList<Integer> uids, AbstractMap<Integer, TLRPC.User> usersDict) {
    int start = TextUtils.indexOf(source, param);
    if (start >= 0) {
        SpannableStringBuilder names = new SpannableStringBuilder("");
        for (int a = 0; a < uids.size(); a++) {
            TLRPC.User user = null;
            if (usersDict != null) {
                user = usersDict.get(uids.get(a));
            }
            if (user == null) {
                user = MessagesController.getInstance().getUser(uids.get(a));
            }
            if (user != null) {
                String name = UserObject.getUserName(user);
                start = names.length();
                if (names.length() != 0) {
                    names.append(", ");
                }
                names.append(name);
                names.setSpan(new URLSpanNoUnderlineBold("" + user.id), start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return TextUtils.replace(source, new String[]{param}, new CharSequence[]{names});
    }
    return source;
}
 
Example 7
Project: PlusGram   File: MessageObject.java   View Source Code Vote up 6 votes
public CharSequence replaceWithLink(CharSequence source, String param, TLObject object) {
    int start = TextUtils.indexOf(source, param);
    if (start >= 0) {
        String name;
        String id;
        if (object instanceof TLRPC.User) {
            name = UserObject.getUserName((TLRPC.User) object);
            id = "" + ((TLRPC.User) object).id;
        } else if (object instanceof TLRPC.Chat) {
            name = ((TLRPC.Chat) object).title;
            id = "" + -((TLRPC.Chat) object).id;
        } else if (object instanceof TLRPC.TL_game) {
            TLRPC.TL_game game = (TLRPC.TL_game) object;
            name = game.title;
            id = "game";
        } else {
            name = "";
            id = "0";
        }
        SpannableStringBuilder builder = new SpannableStringBuilder(TextUtils.replace(source, new String[]{param}, new String[]{name}));
        builder.setSpan(new URLSpanNoUnderlineBold("" + id), start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }
    return source;
}
 
Example 8
Project: mongol-library   File: MongolLayout.java   View Source Code Vote up 5 votes
/**
 * Return how wide a layout must be in order to display the
 * specified text slice with one line per paragraph.
 */
public static Rect getDesiredSize(CharSequence source,
                                  int start, int end,
                                  TextPaint paint) {

    MongolTextLine tl = MongolTextLine.obtain();

    float longestWidth = 0;
    float heightSum = 0;

    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(source, '\n', i, end);

        if (next < 0)
            next = end;

        tl.set(paint, source, i, next);
        RectF size = tl.measure();
        float width = size.width(); // horizontal line orientation
        heightSum += size.height(); // horizontal line orientation

        if (width > longestWidth)
            longestWidth = width;

        next++;
    }

    MongolTextLine.recycle(tl);

    if (heightSum == 0) {
        heightSum = paint.getFontMetrics().bottom - paint.getFontMetrics().top;
    }

    // returning the size as a vertical line orientation (swapping width and height)
    return new Rect(0, 0, (int) heightSum, (int) longestWidth);
}
 
Example 9
Project: BBSSDK-for-Android   File: RichEditor.java   View Source Code Vote up 5 votes
private void stateCheck(String text) {
	String state = text.replaceFirst(STATE_SCHEME, "").toUpperCase(Locale.ENGLISH);
	List<Type> types = new ArrayList<Type>();
	for (Type type : Type.values()) {
		if (TextUtils.indexOf(state, type.name()) != -1) {
			types.add(type);
		}
	}

	if (decorationStateListener != null) {
		decorationStateListener.onStateChangeListener(state, types);
	}
}
 
Example 10
Project: chromium-for-android-56-debug-video   File: UrlBar.java   View Source Code Vote up 5 votes
private void clearAutocompleteSpanIfInvalid() {
    Editable editableText = getEditableText();
    CharSequence previousUserText = mAutocompleteSpan.mUserText;
    CharSequence previousAutocompleteText = mAutocompleteSpan.mAutocompleteText;
    if (editableText.length()
            != (previousUserText.length() + previousAutocompleteText.length())) {
        mAutocompleteSpan.clearSpan();
    } else if (TextUtils.indexOf(getText(), previousUserText) != 0
            || TextUtils.indexOf(getText(),
                    previousAutocompleteText, previousUserText.length()) != 0) {
        mAutocompleteSpan.clearSpan();
    }
}
 
Example 11
Project: chromium-for-android-56-debug-video   File: CreditCardNumberFormattingTextWatcher.java   View Source Code Vote up 5 votes
public static void removeSeparators(Editable s) {
    int index = TextUtils.indexOf(s, SEPARATOR);
    while (index >= 0) {
        s.delete(index, index + 1);
        index = TextUtils.indexOf(s, SEPARATOR, index + 1);
    }
}
 
Example 12
Project: riched_editor_x5   File: RichEditor.java   View Source Code Vote up 5 votes
private void stateCheck(String text) {
  String state = text.replaceFirst(STATE_SCHEME, "").toUpperCase(Locale.ENGLISH);
  List<Type> types = new ArrayList<>();
  for (Type type : Type.values()) {
    if (TextUtils.indexOf(state, type.name()) != -1) {
      types.add(type);
    }
  }

  if (mDecorationStateListener != null) {
    mDecorationStateListener.onStateChangeListener(state, types);
  }
}
 
Example 13
Project: PlusGram   File: EditTextCaption.java   View Source Code Vote up 5 votes
@SuppressLint({"DrawAllocation"})
protected void onMeasure(int i, int i2) {
    try {
        super.onMeasure(i, i2);
    } catch (Throwable e) {
        FirebaseCrash.report(e);
        setMeasuredDimension(MeasureSpec.getSize(i), AndroidUtilities.dp(51.0f));
        FileLog.e("tmessages", e);
    }
    this.f399b = null;
    if (this.f398a != null && this.f398a.length() > 0) {
        CharSequence text = getText();
        if (text.length() > 1 && text.charAt(0) == '@') {
            int indexOf = TextUtils.indexOf(text, ' ');
            if (indexOf != -1) {
                TextPaint paint = getPaint();
                int ceil = (int) Math.ceil((double) paint.measureText(text, 0, indexOf + 1));
                int measuredWidth = (getMeasuredWidth() - getPaddingLeft()) - getPaddingRight();
                this.f400c = text.subSequence(0, indexOf + 1).length();
                CharSequence ellipsize = TextUtils.ellipsize(this.f398a, paint, (float) (measuredWidth - ceil), TruncateAt.END);
                this.f401d = ceil;
                try {
                    this.f399b = new StaticLayout(ellipsize, getPaint(), measuredWidth - ceil, Alignment.ALIGN_NORMAL, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT, 0.0f, false);
                    if (this.f399b.getLineCount() > 0) {
                        this.f401d = (int) (((float) this.f401d) + (-this.f399b.getLineLeft(0)));
                    }
                    this.f402e = ((getMeasuredHeight() - this.f399b.getLineBottom(0)) / 2) + AndroidUtilities.dp(0.5f);
                } catch (Throwable e2) {
                    FirebaseCrash.report(e2);
                    FileLog.e("tmessages", e2);
                }
            }
        }
    }
}
 
Example 14
Project: HtmlCompat   File: HtmlCompat.java   View Source Code Vote up 5 votes
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
Project: richeditor   File: RichEditor.java   View Source Code Vote up 5 votes
public void stateCheck(String text) {

        String state = text.replaceFirst(STATE_SCHEME, "").toUpperCase(Locale.ENGLISH);
        List<Type> types = new ArrayList<>();
        for (Type type : Type.values()) {
            if (TextUtils.indexOf(state, type.name()) != -1) {
                types.add(type);
            }
        }

        if (mStateChangeListener != null) {
            mStateChangeListener.onStateChangeListener(state, types);
        }
    }
 
Example 16
Project: richeditor   File: RichEditor.java   View Source Code Vote up 5 votes
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    String decode;
    try {
        decode = URLDecoder.decode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // No handling
        return false;
    }

    Log.e("decode",decode);

    if (TextUtils.indexOf(url, CALLBACK_SCHEME) == 0) {
        callback(decode);
        return true;
    } else if (TextUtils.indexOf(url, STATE_SCHEME) == 0) {
        stateCheck(decode);
        return true;
    }
    if(TextUtils.indexOf(url,LINK_CHANGE_SCHEME) == 0){
        linkChangeCallBack(decode);
        return true;
    }

    if(TextUtils.indexOf(url,IMAGE_CLICK_SCHEME) == 0){
        imageClickCallBack(decode);
        return true;
    }

    return super.shouldOverrideUrlLoading(view, url);
}
 
Example 17
Project: chromium-for-android-56-debug-video   File: UrlBar.java   View Source Code Vote up 4 votes
/**
 * Autocompletes the text on the url bar and selects the text that was not entered by the
 * user. Using append() instead of setText() to preserve the soft-keyboard layout.
 * @param userText user The text entered by the user.
 * @param inlineAutocompleteText The suggested autocompletion for the user's text.
 */
public void setAutocompleteText(CharSequence userText, CharSequence inlineAutocompleteText) {
    boolean emptyAutocomplete = TextUtils.isEmpty(inlineAutocompleteText);

    if (!emptyAutocomplete) mDisableTextScrollingFromAutocomplete = true;

    int autocompleteIndex = userText.length();

    String previousText = getQueryText();
    CharSequence newText = TextUtils.concat(userText, inlineAutocompleteText);

    setIgnoreTextChangesForAutocomplete(true);
    mDisableTextAccessibilityEvents = true;

    if (!TextUtils.equals(previousText, newText)) {
        // The previous text may also have included autocomplete text, so we only
        // append the new autocomplete text that has changed.
        if (TextUtils.indexOf(newText, previousText) == 0) {
            append(newText.subSequence(previousText.length(), newText.length()));
        } else {
            setUrl(newText.toString(), null);
        }
    }

    if (getSelectionStart() != autocompleteIndex
            || getSelectionEnd() != getText().length()) {
        setSelection(autocompleteIndex, getText().length());

        if (inlineAutocompleteText.length() != 0) {
            // Sending a TYPE_VIEW_TEXT_SELECTION_CHANGED accessibility event causes the
            // previous TYPE_VIEW_TEXT_CHANGED event to be swallowed. As a result the user
            // hears the autocomplete text but *not* the text they typed. Instead we send a
            // TYPE_ANNOUNCEMENT event, which doesn't swallow the text-changed event.
            announceForAccessibility(inlineAutocompleteText);
        }
    }

    if (emptyAutocomplete) {
        mAutocompleteSpan.clearSpan();
    } else {
        mAutocompleteSpan.setSpan(userText, inlineAutocompleteText);
    }

    setIgnoreTextChangesForAutocomplete(false);
    mDisableTextAccessibilityEvents = false;
}
 
Example 18
Project: chromium-for-android-56-debug-video   File: CreditCardNumberFormattingTextWatcher.java   View Source Code Vote up 4 votes
public static boolean hasDashOrSpace(final CharSequence s, final int start,
        final int count) {
    return TextUtils.indexOf(s, " ", start, start + count) != -1
            || TextUtils.indexOf(s, "-", start, start + count) != -1;
}
 
Example 19
Project: HtmlCompat   File: HtmlCompat.java   View Source Code Vote up 4 votes
private static void withinBlockquoteIndividual(Context context, StringBuilder out, Spanned text,
                                               int start, int end) {
    boolean isInList = false;
    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }
        if (next == i) {
            if (isInList) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            out.append("<br>\n");
        } else {
            boolean isListItem = false;
            ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
            for (ParagraphStyle paragraphStyle : paragraphStyles) {
                final int spanFlags = text.getSpanFlags(paragraphStyle);
                if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                        && paragraphStyle instanceof BulletSpan) {
                    isListItem = true;
                    break;
                }
            }
            if (isListItem && !isInList) {
                // Current paragraph is the first item in a list
                isInList = true;
                out.append("<ul")
                   .append(getTextStyles(text, i, next, true, false))
                   .append(">\n");
            }
            if (isInList && !isListItem) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            String tagType = isListItem ? "li" : "p";
            out.append("<").append(tagType)
               .append(getTextDirection(text, i, next))
               .append(getTextStyles(text, i, next, !isListItem, true))
               .append(">");
            withinParagraph(context, out, text, i, next);
            out.append("</");
            out.append(tagType);
            out.append(">\n");
            if (next == end && isInList) {
                isInList = false;
                out.append("</ul>\n");
            }
        }
        next++;
    }
}