Example usage for android.text SpannableStringBuilder charAt

List of usage examples for android.text SpannableStringBuilder charAt

Introduction

In this page you can find the example usage for android.text SpannableStringBuilder charAt.

Prototype

public char charAt(int where) 

Source Link

Document

Return the char at the specified offset within the buffer.

Usage

From source file:Main.java

/**
 * Invoked when the end of a paragraph is encountered. Adds a newline if there are one or more
 * non-space characters since the previous newline.
 *
 * @param builder The builder./*from w ww . j  a v a 2  s  .c o  m*/
 */
/* package */static void endParagraph(SpannableStringBuilder builder) {
    int position = builder.length() - 1;
    while (position >= 0 && builder.charAt(position) == ' ') {
        position--;
    }
    if (position >= 0 && builder.charAt(position) != '\n') {
        builder.append('\n');
    }
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void handleP(SpannableStringBuilder text) {
    int len = text.length();

    if (len >= 1 && text.charAt(len - 1) == '\n') {
        if (len >= 2 && text.charAt(len - 2) == '\n') {
            return;
        }/*from w ww  . ja va  2s .c  om*/

        text.append("\n");
        return;
    }

    if (len != 0) {
        text.append("\n\n");
    }
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void handleLi(SpannableStringBuilder text, Object tag, int level) {
    if (tag == null)
        return;/*from   w w w .j  ava2  s.  c o m*/

    int len = text.length();
    if (len >= 1 && text.charAt(len - 1) != '\n')
        text.append("\n");
    for (int i = 1; i < level; ++i)
        text.append("\t");
    if (tag instanceof OlTag)
        text.append(Integer.toString(((OlTag) tag).curIndex++) + ". ");
    else if (tag instanceof UlTag)
        text.append("\u2022 ");
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void handleP(SpannableStringBuilder text, int startLength, int[] lastPTagLengthRefs) {
    lastPTagLengthRefs[0] = text.length();
    int len = text.length() - startLength;

    if (len >= 1 && text.charAt(text.length() - 1) == '\n') {
        if (len >= 2 && text.charAt(text.length() - 2) == '\n') {
            lastPTagLengthRefs[1] = text.length();
            return;
        }/*from  ww w  .j ava 2 s. co  m*/

        text.append("\n");
        lastPTagLengthRefs[1] = text.length();
        return;
    }

    if (len != 0) {
        text.append("\n\n");
    }
    lastPTagLengthRefs[1] = text.length();
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void applyParagraphSpacing(TextView textView, float multiplier) {
    final SpannableStringBuilder builder = SpannableStringBuilder.valueOf(textView.getText());
    int prevLineBreak, currLineBreak = 0;
    for (int i = 0, j = builder.length(); i < j; i++) {
        if (builder.charAt(i) == '\n') {
            prevLineBreak = currLineBreak;
            currLineBreak = i;//from   w ww  .ja v a 2 s.  c om
            if (currLineBreak > 0) {
                builder.setSpan(new ParagraphSpacingSpan(multiplier), prevLineBreak, currLineBreak,
                        Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
        }
    }
    textView.setText(builder);
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void endHeader(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Header.class);

    int where = text.getSpanStart(obj);

    text.removeSpan(obj);/*from  w ww . ja  v a  2s.  c o m*/

    // Back off not to change only the text, not the blank line.
    while (len > where && text.charAt(len - 1) == '\n') {
        len--;
    }

    if (where != len) {
        Header h = (Header) obj;

        text.setSpan(new RelativeSizeSpan(HEADER_SIZES[h.mLevel]), where, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new StyleSpan(Typeface.BOLD), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * Replace instances of "^1", "^2", etc. in the
 * <code>template</code> CharSequence with the corresponding
 * <code>values</code>.  "^^" is used to produce a single caret in
 * the output.  Only up to 9 replacement values are supported,
 * "^10" will be produce the first replacement value followed by a
 * '0'./*w w w  .j  a va 2s  .  com*/
 *
 * @param template the input text containing "^1"-style
 * placeholder values.  This object is not modified; a copy is
 * returned.
 *
 * @param values CharSequences substituted into the template.  The
 * first is substituted for "^1", the second for "^2", and so on.
 *
 * @return the new CharSequence produced by doing the replacement
 *
 * @throws IllegalArgumentException if the template requests a
 * value that was not provided, or if more than 9 values are
 * provided.
 */
public static CharSequence expandTemplate(CharSequence template, CharSequence... values) {
    if (values.length > 9) {
        throw new IllegalArgumentException("max of 9 values are supported");
    }

    SpannableStringBuilder ssb = new SpannableStringBuilder(template);

    try {
        int i = 0;
        while (i < ssb.length()) {
            if (ssb.charAt(i) == '^') {
                char next = ssb.charAt(i + 1);
                if (next == '^') {
                    ssb.delete(i + 1, i + 2);
                    ++i;
                    continue;
                } else if (Character.isDigit(next)) {
                    int which = Character.getNumericValue(next) - 1;
                    if (which < 0) {
                        throw new IllegalArgumentException("template requests value ^" + (which + 1));
                    }
                    if (which >= values.length) {
                        throw new IllegalArgumentException("template requests value ^" + (which + 1) + "; only "
                                + values.length + " provided");
                    }
                    ssb.replace(i, i + 2, values[which]);
                    i += values[which].length();
                    continue;
                }
            }
            ++i;
        }
    } catch (IndexOutOfBoundsException ignore) {
        // happens when ^ is the last character in the string.
    }
    return ssb;
}

From source file:org.telegram.ui.Components.ChatActivityEnterView.java

public void setEditingMessageObject(MessageObject messageObject, boolean caption) {
    if (audioToSend != null || editingMessageObject == messageObject) {
        return;// ww w .ja va 2s.co  m
    }
    if (editingMessageReqId != 0) {
        ConnectionsManager.getInstance().cancelRequest(editingMessageReqId, true);
        editingMessageReqId = 0;
    }
    editingMessageObject = messageObject;
    editingCaption = caption;
    if (editingMessageObject != null) {
        if (doneButtonAnimation != null) {
            doneButtonAnimation.cancel();
            doneButtonAnimation = null;
        }
        doneButtonContainer.setVisibility(View.VISIBLE);
        showEditDoneProgress(true, false);

        InputFilter[] inputFilters = new InputFilter[1];
        if (caption) {
            inputFilters[0] = new InputFilter.LengthFilter(200);
            if (editingMessageObject.caption != null) {
                setFieldText(Emoji.replaceEmoji(
                        new SpannableStringBuilder(editingMessageObject.caption.toString()),
                        messageEditText.getPaint().getFontMetricsInt(), AndroidUtilities.dp(20), false));
            } else {
                setFieldText("");
            }
        } else {
            inputFilters[0] = new InputFilter.LengthFilter(4096);
            if (editingMessageObject.messageText != null) {
                ArrayList<TLRPC.MessageEntity> entities = editingMessageObject.messageOwner.entities;//MessagesQuery.getEntities(message);
                MessagesQuery.sortEntities(entities);
                SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                        editingMessageObject.messageText);
                Object spansToRemove[] = stringBuilder.getSpans(0, stringBuilder.length(), Object.class);
                if (spansToRemove != null && spansToRemove.length > 0) {
                    for (int a = 0; a < spansToRemove.length; a++) {
                        stringBuilder.removeSpan(spansToRemove[a]);
                    }
                }
                if (entities != null) {
                    int addToOffset = 0;
                    try {
                        for (int a = 0; a < entities.size(); a++) {
                            TLRPC.MessageEntity entity = entities.get(a);
                            if (entity.offset + entity.length + addToOffset > stringBuilder.length()) {
                                continue;
                            }
                            if (entity instanceof TLRPC.TL_inputMessageEntityMentionName) {
                                if (entity.offset + entity.length + addToOffset < stringBuilder.length()
                                        && stringBuilder
                                                .charAt(entity.offset + entity.length + addToOffset) == ' ') {
                                    entity.length++;
                                }
                                stringBuilder.setSpan(new URLSpanUserMention(
                                        "" + ((TLRPC.TL_inputMessageEntityMentionName) entity).user_id.user_id,
                                        true), entity.offset + addToOffset,
                                        entity.offset + entity.length + addToOffset,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            } else if (entity instanceof TLRPC.TL_messageEntityCode) {
                                stringBuilder.insert(entity.offset + entity.length + addToOffset, "`");
                                stringBuilder.insert(entity.offset + addToOffset, "`");
                                addToOffset += 2;
                            } else if (entity instanceof TLRPC.TL_messageEntityPre) {
                                stringBuilder.insert(entity.offset + entity.length + addToOffset, "```");
                                stringBuilder.insert(entity.offset + addToOffset, "```");
                                addToOffset += 6;
                            } else if (entity instanceof TLRPC.TL_messageEntityBold) {
                                stringBuilder.setSpan(
                                        new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")),
                                        entity.offset + addToOffset,
                                        entity.offset + entity.length + addToOffset,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            } else if (entity instanceof TLRPC.TL_messageEntityItalic) {
                                stringBuilder.setSpan(
                                        new TypefaceSpan(AndroidUtilities.getTypeface("fonts/ritalic.ttf")),
                                        entity.offset + addToOffset,
                                        entity.offset + entity.length + addToOffset,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                }
                setFieldText(Emoji.replaceEmoji(stringBuilder, messageEditText.getPaint().getFontMetricsInt(),
                        AndroidUtilities.dp(20), false));
            } else {
                setFieldText("");
            }
        }
        messageEditText.setFilters(inputFilters);
        openKeyboard();
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) messageEditText.getLayoutParams();
        layoutParams.rightMargin = AndroidUtilities.dp(4);
        messageEditText.setLayoutParams(layoutParams);
        sendButton.setVisibility(GONE);
        cancelBotButton.setVisibility(GONE);
        audioVideoButtonContainer.setVisibility(GONE);
        attachLayout.setVisibility(GONE);
        sendButtonContainer.setVisibility(GONE);
    } else {
        doneButtonContainer.setVisibility(View.GONE);
        messageEditText.setFilters(new InputFilter[0]);
        delegate.onMessageEditEnd(false);
        audioVideoButtonContainer.setVisibility(VISIBLE);
        attachLayout.setVisibility(VISIBLE);
        sendButtonContainer.setVisibility(VISIBLE);
        attachLayout.setScaleX(1.0f);
        attachLayout.setAlpha(1.0f);
        sendButton.setScaleX(0.1f);
        sendButton.setScaleY(0.1f);
        sendButton.setAlpha(0.0f);
        cancelBotButton.setScaleX(0.1f);
        cancelBotButton.setScaleY(0.1f);
        cancelBotButton.setAlpha(0.0f);
        audioVideoButtonContainer.setScaleX(1.0f);
        audioVideoButtonContainer.setScaleY(1.0f);
        audioVideoButtonContainer.setAlpha(1.0f);
        sendButton.setVisibility(GONE);
        cancelBotButton.setVisibility(GONE);
        messageEditText.setText("");
        if (getVisibility() == VISIBLE) {
            delegate.onAttachButtonShow();
        }
        updateFieldRight(1);
    }
    updateFieldHint();
}

From source file:kr.wdream.ui.ChatActivity.java

private void applyDraftMaybe(boolean canClear) {
    if (chatActivityEnterView == null) {
        return;//from  ww w . j a  v a2 s  . c o m
    }
    TLRPC.DraftMessage draftMessage = DraftQuery.getDraft(dialog_id);
    TLRPC.Message draftReplyMessage = draftMessage != null && draftMessage.reply_to_msg_id != 0
            ? DraftQuery.getDraftMessage(dialog_id)
            : null;
    if (chatActivityEnterView.getFieldText() == null) {
        if (draftMessage != null) {
            chatActivityEnterView.setWebPage(null, !draftMessage.no_webpage);
            CharSequence message;
            if (!draftMessage.entities.isEmpty()) {
                SpannableStringBuilder stringBuilder = SpannableStringBuilder.valueOf(draftMessage.message);
                MessagesQuery.sortEntities(draftMessage.entities);
                int addToOffset = 0;
                for (int a = 0; a < draftMessage.entities.size(); a++) {
                    TLRPC.MessageEntity entity = draftMessage.entities.get(a);
                    if (entity instanceof TLRPC.TL_inputMessageEntityMentionName
                            || entity instanceof TLRPC.TL_messageEntityMentionName) {
                        int user_id;
                        if (entity instanceof TLRPC.TL_inputMessageEntityMentionName) {
                            user_id = ((TLRPC.TL_inputMessageEntityMentionName) entity).user_id.user_id;
                        } else {
                            user_id = ((TLRPC.TL_messageEntityMentionName) entity).user_id;
                        }
                        if (entity.offset + addToOffset + entity.length < stringBuilder.length()
                                && stringBuilder.charAt(entity.offset + addToOffset + entity.length) == ' ') {
                            entity.length++;
                        }
                        stringBuilder.setSpan(new URLSpanUserMention("" + user_id), entity.offset + addToOffset,
                                entity.offset + addToOffset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else if (entity instanceof TLRPC.TL_messageEntityCode) {
                        stringBuilder.insert(entity.offset + entity.length + addToOffset, "`");
                        stringBuilder.insert(entity.offset + addToOffset, "`");
                        addToOffset += 2;
                    } else if (entity instanceof TLRPC.TL_messageEntityPre) {
                        stringBuilder.insert(entity.offset + entity.length + addToOffset, "```");
                        stringBuilder.insert(entity.offset + addToOffset, "```");
                        addToOffset += 6;
                    }
                }
                message = stringBuilder;
            } else {
                message = draftMessage.message;
            }
            chatActivityEnterView.setFieldText(message);
            if (getArguments().getBoolean("hasUrl", false)) {
                chatActivityEnterView.setSelection(draftMessage.message.indexOf('\n') + 1);
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        if (chatActivityEnterView != null) {
                            chatActivityEnterView.setFieldFocused(true);
                            chatActivityEnterView.openKeyboard();
                        }
                    }
                }, 700);
            }
        }
    } else if (canClear && draftMessage == null) {
        chatActivityEnterView.setFieldText("");
        showReplyPanel(false, null, null, null, false, true);
    }
    if (replyingMessageObject == null && draftReplyMessage != null) {
        replyingMessageObject = new MessageObject(draftReplyMessage,
                MessagesController.getInstance().getUsers(), false);
        showReplyPanel(true, replyingMessageObject, null, null, false, false);
    }
}