List of usage examples for android.text Editable length
int length();
From source file:com.android.ex.chips.RecipientEditTextView.java
ArrayList<DrawableRecipientChip> handlePaste() { final String text = getText().toString(); final int originalTokenStart = mTokenizer.findTokenStart(text, getSelectionEnd()); final String lastAddress = text.substring(originalTokenStart); int tokenStart = originalTokenStart; int prevTokenStart = 0; DrawableRecipientChip findChip = null; final ArrayList<DrawableRecipientChip> created = new ArrayList<DrawableRecipientChip>(); if (tokenStart != 0) { // There are things before this! while (tokenStart != 0 && findChip == null && tokenStart != prevTokenStart) { prevTokenStart = tokenStart; tokenStart = mTokenizer.findTokenStart(text, tokenStart); findChip = findChip(tokenStart); if (tokenStart == originalTokenStart && findChip == null) break; }/*from www. j av a2 s . c o m*/ if (tokenStart != originalTokenStart) { if (findChip != null) tokenStart = prevTokenStart; int tokenEnd; DrawableRecipientChip createdChip; while (tokenStart < originalTokenStart) { tokenEnd = movePastTerminators(mTokenizer.findTokenEnd(getText().toString(), tokenStart)); commitChip(tokenStart, tokenEnd, getText()); createdChip = findChip(tokenStart); if (createdChip == null) break; // +1 for the space at the end. tokenStart = getSpannable().getSpanEnd(createdChip) + 1; created.add(createdChip); } } } // Take a look at the last token. If the token has been completed with a // commit character, create a chip. if (isCompletedToken(lastAddress)) { final Editable editable = getText(); tokenStart = editable.toString().indexOf(lastAddress, originalTokenStart); commitChip(tokenStart, editable.length(), editable); created.add(findChip(tokenStart)); } return created; }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Remove selection from this chip. Unselecting a RecipientChip will render the chip without a delete icon and with * an unfocused background. This is called when the RecipientChip no longer has focus. */// w w w . j a v a 2 s . c o m private void unselectChip(final DrawableRecipientChip chip) { final int start = getChipStart(chip); final int end = getChipEnd(chip); final Editable editable = getText(); mSelectedChip = null; if (start == -1 || end == -1) { Log.w(TAG, "The chip doesn't exist or may be a chip a user was editing"); setSelection(editable.length()); commitDefault(); } else { getSpannable().removeSpan(chip); QwertyKeyListener.markAsReplaced(editable, start, end, ""); editable.removeSpan(chip); try { if (!mNoChips) editable.setSpan(constructChipSpan(chip.getEntry(), false, false), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (final NullPointerException e) { Log.e(TAG, e.getMessage(), e); } } setCursorVisible(true); setSelection(editable.length()); /*if(mAlternatesPopup!=null&&mAlternatesPopup.isShowing()) mAlternatesPopup.dismiss();*/ }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Remove the chip and any text associated with it from the RecipientEditTextView. * * @param alsoNotifyAboutDataChanges//w w w . ja v a 2 s .com */ // Visible for testing. /* package */void removeChip(final DrawableRecipientChip chip, final boolean alsoNotifyAboutDataChanges) { if (!alsoNotifyAboutDataChanges) --mPreviousChipsCount; final Spannable spannable = getSpannable(); final int spanStart = spannable.getSpanStart(chip); final int spanEnd = spannable.getSpanEnd(chip); final Editable text = getText(); int toDelete = spanEnd; final boolean wasSelected = chip == mSelectedChip; // Clear that there is a selected chip before updating any text. if (wasSelected) mSelectedChip = null; // Always remove trailing spaces when removing a chip. while (toDelete >= 0 && toDelete < text.length() && text.charAt(toDelete) == ' ') toDelete++; spannable.removeSpan(chip); if (spanStart >= 0 && toDelete > 0) text.delete(spanStart, toDelete); if (wasSelected) clearSelectedChip(); }
From source file:com.android.ex.chips.RecipientEditTextView.java
private void shrink() { if (mTokenizer == null) return;/*w w w . j av a2s. c o m*/ final long contactId = mSelectedChip != null ? mSelectedChip.getEntry().getContactId() : -1; if (mSelectedChip != null && contactId != RecipientEntry.INVALID_CONTACT && !isPhoneQuery() && contactId != RecipientEntry.GENERATED_CONTACT) clearSelectedChip(); else { if (getWidth() <= 0) { // We don't have the width yet which means the view hasn't been drawn yet // and there is no reason to attempt to commit chips yet. // This focus lost must be the result of an orientation change // or an initial rendering. // Re-post the shrink for later. mHandler.removeCallbacks(mDelayedShrink); mHandler.post(mDelayedShrink); return; } // Reset any pending chips as they would have been handled // when the field lost focus. if (mPendingChipsCount > 0) postHandlePendingChips(); else { final Editable editable = getText(); final int end = getSelectionEnd(); final int start = mTokenizer.findTokenStart(editable, end); final DrawableRecipientChip[] chips = getSpannable().getSpans(start, end, DrawableRecipientChip.class); if (chips == null || chips.length == 0 && start >= 0) { final Editable text = getText(); // TODO check why this code can crash (index out of bounds). Currently fixed by checking that // start>=0 int whatEnd = mTokenizer.findTokenEnd(text, start); // This token was already tokenized, so skip past the ending token. if (whatEnd < text.length() && text.charAt(whatEnd) == ',') whatEnd = movePastTerminators(whatEnd); // In the middle of chip; treat this as an edit // and commit the whole token. final int selEnd = getSelectionEnd(); if (whatEnd != selEnd) handleEdit(start, whatEnd); else commitChip(start, end, editable); } } mHandler.post(mAddTextWatcher); } createMoreChip(); }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Show specified chip as selected. If the RecipientChip is just an email address, selecting the chip will take the * contents of the chip and place it at the end of the RecipientEditTextView for inline editing. If the * RecipientChip is a complete contact, then selecting the chip will change the background color of the chip, show * the delete icon, and a popup window with the address in use highlighted and any other alternate addresses for the * contact./*from ww w. j a v a 2 s . c o m*/ * * @param currentChip * Chip to select. * @return A RecipientChip in the selected state or null if the chip just contained an email address. */ private DrawableRecipientChip selectChip(final DrawableRecipientChip currentChip) { if (shouldShowEditableText(currentChip)) { final CharSequence text = currentChip.getValue(); final Editable editable = getText(); final Spannable spannable = getSpannable(); final int spanStart = spannable.getSpanStart(currentChip); final int spanEnd = spannable.getSpanEnd(currentChip); spannable.removeSpan(currentChip); editable.delete(spanStart, spanEnd); setCursorVisible(true); setSelection(editable.length()); editable.append(text); return constructChipSpan(RecipientEntry.constructFakeEntry((String) text, isValid(text.toString())), true, false); } else if (currentChip.getContactId() == RecipientEntry.GENERATED_CONTACT || currentChip.isGalContact()) { final int start = getChipStart(currentChip); final int end = getChipEnd(currentChip); getSpannable().removeSpan(currentChip); DrawableRecipientChip newChip; try { if (mNoChips) return null; newChip = constructChipSpan(currentChip.getEntry(), true, false); } catch (final NullPointerException e) { Log.e(TAG, e.getMessage(), e); return null; } final Editable editable = getText(); QwertyKeyListener.markAsReplaced(editable, start, end, ""); if (start == -1 || end == -1) Log.d(TAG, "The chip being selected no longer exists but should."); else editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); newChip.setSelected(true); if (shouldShowEditableText(newChip)) scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip))); //showAddress(newChip,mAddressPopup,getWidth()); setCursorVisible(false); return newChip; } else { final int start = getChipStart(currentChip); final int end = getChipEnd(currentChip); getSpannable().removeSpan(currentChip); DrawableRecipientChip newChip; try { newChip = constructChipSpan(currentChip.getEntry(), true, false); } catch (final NullPointerException e) { Log.e(TAG, e.getMessage(), e); return null; } final Editable editable = getText(); QwertyKeyListener.markAsReplaced(editable, start, end, ""); if (start == -1 || end == -1) Log.d(TAG, "The chip being selected no longer exists but should."); else editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); newChip.setSelected(true); if (shouldShowEditableText(newChip)) scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip))); //showAlternates(newChip,mAlternatesPopup,getWidth()); setCursorVisible(false); return newChip; } }
From source file:com.android.ex.chips.RecipientEditTextView.java
private boolean commitChip(final int start, final int end, final Editable editable) { final ListAdapter adapter = getAdapter(); if (adapter != null && adapter.getCount() > 0 && enoughToFilter() && end == getSelectionEnd() && !isPhoneQuery()) { // choose the first entry. submitItemAtPosition(0);//w w w .ja va 2 s.co m dismissDropDown(); return true; } else { int tokenEnd = mTokenizer.findTokenEnd(editable, start); if (editable.length() > tokenEnd + 1) { final char charAt = editable.charAt(tokenEnd + 1); if (charAt == COMMIT_CHAR_COMMA || charAt == COMMIT_CHAR_SEMICOLON || charAt == COMMIT_CHAR_SPACE) //// ---- Added by shreyash tokenEnd++; } //----------------------- final String text = editable.toString().substring(start, tokenEnd).trim(); clearComposingText(); if (text != null && text.length() > 0 && !text.equals(" ")) { final RecipientEntry entry = createTokenizedEntry(text); if (entry != null) { QwertyKeyListener.markAsReplaced(editable, start, end, ""); final CharSequence chipText = createChip(entry, false); if (chipText != null && start > -1 && end > -1) editable.replace(start, end, chipText); } // Only dismiss the dropdown if it is related to the text we // just committed. // For paste, it may not be as there are possibly multiple // tokens being added. if (end == getSelectionEnd()) dismissDropDown(); sanitizeBetween(); return true; } } return false; }
From source file:com.android.ex.chips.RecipientEditTextView.java
void createMoreChipPlainText() { // Take the first <= CHIP_LIMIT addresses and get to the end of the second one. final Editable text = getText(); int start = 0; int end = start; for (int i = 0; i < CHIP_LIMIT; i++) { end = movePastTerminators(mTokenizer.findTokenEnd(text, start)); start = end; // move to the next token and get its end. }//from ww w . j a v a2 s .co m // Now, count total addresses. start = 0; final int tokenCount = countTokens(text); final MoreImageSpan moreSpan = createMoreSpan(tokenCount - CHIP_LIMIT); final SpannableString chipText = new SpannableString(text.subSequence(end, text.length())); chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.replace(end, text.length(), chipText); mMoreChip = moreSpan; }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Replace this currently selected chip with a new chip that uses the contact data provided. *///from w ww. j ava 2s . co m // Visible for testing. /* package */void replaceChip(final DrawableRecipientChip chip, final RecipientEntry entry) { final boolean wasSelected = chip == mSelectedChip; if (wasSelected) mSelectedChip = null; final int start = getChipStart(chip); final int end = getChipEnd(chip); getSpannable().removeSpan(chip); final Editable editable = getText(); final CharSequence chipText = createChip(entry, false); if (chipText != null) if (start == -1 || end == -1) { Log.e(TAG, "The chip to replace does not exist but should."); editable.insert(0, chipText); } else if (!TextUtils.isEmpty(chipText)) { // There may be a space to replace with this chip's new // associated space. Check for it int toReplace = end; while (toReplace >= 0 && toReplace < editable.length() && editable.charAt(toReplace) == ' ') toReplace++; editable.replace(start, toReplace, chipText); } setCursorVisible(true); if (wasSelected) clearSelectedChip(); if (mChipListener != null) mChipListener.onDataChanged(); }
From source file:com.android.ex.chips.RecipientEditTextView.java
void handlePendingChips() { if (getViewWidth() <= 0) // The widget has not been sized yet. // This will be called as a result of onSizeChanged // at a later point. return;/*w w w . j a va 2s . c o m*/ if (mPendingChipsCount <= 0) return; synchronized (mPendingChips) { final Editable editable = getText(); // Tokenize! if (mPendingChipsCount <= MAX_CHIPS_PARSED) { for (int i = 0; i < mPendingChips.size(); i++) { final String current = mPendingChips.get(i); final int tokenStart = editable.toString().indexOf(current); // Always leave a space at the end between tokens. int tokenEnd = tokenStart + current.length() - 1; if (tokenStart >= 0) { // When we have a valid token, include it with the token // to the left. if (tokenEnd < editable.length() - 2 && editable.charAt(tokenEnd) == COMMIT_CHAR_COMMA) tokenEnd++; createReplacementChip(tokenStart, tokenEnd, editable, i < CHIP_LIMIT || !mShouldShrink); } mPendingChipsCount--; } sanitizeEnd(); } else mNoChips = true; if (mTemporaryRecipients != null && mTemporaryRecipients.size() > 0 && mTemporaryRecipients.size() <= RecipientAlternatesAdapter.MAX_LOOKUPS) { if (hasFocus() || mTemporaryRecipients.size() < CHIP_LIMIT) { new RecipientReplacementTask().execute(); mTemporaryRecipients = null; } else { // Create the "more" chip mIndividualReplacements = new IndividualReplacementTask(); mIndividualReplacements.execute(new ArrayList<>(mTemporaryRecipients.subList(0, CHIP_LIMIT))); if (mTemporaryRecipients.size() > CHIP_LIMIT) mTemporaryRecipients = new ArrayList<>( mTemporaryRecipients.subList(CHIP_LIMIT, mTemporaryRecipients.size())); else mTemporaryRecipients = null; createMoreChip(); } } else { // There are too many recipients to look up, so just fall back // to showing addresses for all of them. mTemporaryRecipients = null; createMoreChip(); } mPendingChipsCount = 0; mPendingChips.clear(); } }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Replace the more chip, if it exists, with all of the recipient chips it had replaced when the * RecipientEditTextView gains focus./*from w w w. j a va 2 s . c om*/ */ // Visible for testing. /* package */void removeMoreChip() { if (mMoreChip != null) { final Spannable span = getSpannable(); span.removeSpan(mMoreChip); mMoreChip = null; // Re-add the spans that were removed. if (mRemovedSpans != null && mRemovedSpans.size() > 0) { // Recreate each removed span. final DrawableRecipientChip[] recipients = getSortedRecipients(); // Start the search for tokens after the last currently visible // chip. if (recipients == null || recipients.length == 0) return; int end = span.getSpanEnd(recipients[recipients.length - 1]); final Editable editable = getText(); for (final DrawableRecipientChip chip : mRemovedSpans) { int chipStart; int chipEnd; String token; // Need to find the location of the chip, again. token = (String) chip.getOriginalText(); // As we find the matching recipient for the remove spans, // reduce the size of the string we need to search. // That way, if there are duplicates, we always find the correct // recipient. chipStart = editable.toString().indexOf(token, end); end = chipEnd = Math.min(editable.length(), chipStart + token.length()); // Only set the span if we found a matching token. if (chipStart != -1) editable.setSpan(chip, chipStart, chipEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } mRemovedSpans.clear(); } } }