Revert "Make the space part of the replacement image span."
This reverts commit a1d52e9203ea3cdfc0d1c69a8a4f99445fdd5ca1
Also reverts later change:
https://android-git.corp.google.com/g/#/c/138160/3
That was made to remove code that took into account having a space not
covered by the span character.
Fixes tests cases to respect the space character again.
Change-Id: I96d52a5df4d01e757f72949cbcf3b1f582679a46
diff --git a/src/com/android/ex/chips/RecipientChip.java b/src/com/android/ex/chips/RecipientChip.java
index 29ee4d0..41d950f 100644
--- a/src/com/android/ex/chips/RecipientChip.java
+++ b/src/com/android/ex/chips/RecipientChip.java
@@ -16,7 +16,6 @@
package com.android.ex.chips;
-import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.style.DynamicDrawableSpan;
@@ -42,7 +41,7 @@
private CharSequence mOriginalText;
public RecipientChip(Drawable drawable, RecipientEntry entry, int offset) {
- super(drawable, DynamicDrawableSpan.ALIGN_BOTTOM);
+ super(drawable, DynamicDrawableSpan.ALIGN_BASELINE);
mDisplay = entry.getDisplayName();
mValue = entry.getDestination().trim();
mContactId = entry.getContactId();
@@ -101,16 +100,13 @@
}
public void setOriginalText(String text) {
+ if (!TextUtils.isEmpty(text)) {
+ text = text.trim();
+ }
mOriginalText = text;
}
public CharSequence getOriginalText() {
return !TextUtils.isEmpty(mOriginalText) ? mOriginalText : mEntry.getDestination();
}
-
- @Override
- public int getSize(Paint paint, CharSequence text, int start, int end,
- Paint.FontMetricsInt fm) {
- return super.getSize(paint, text, start, end, fm) + (int) paint.measureText(" ");
- }
}
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index f4573c6..37e882a 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -434,9 +434,8 @@
* Get the background drawable for a RecipientChip.
*/
public Drawable getChipBackground(RecipientEntry contact) {
- String destination = contact.getDestination();
- return (mValidator != null && !TextUtils.isEmpty(destination) && mValidator
- .isValid(destination)) ? mChipBackground : mInvalidChipBackground;
+ return (mValidator != null && mValidator.isValid(contact.getDestination())) ?
+ mChipBackground : mInvalidChipBackground;
}
private Bitmap createUnselectedChip(RecipientEntry contact, TextPaint paint, Layout layout) {
@@ -774,7 +773,7 @@
if (entry != null) {
String destText = createDisplayText(entry);
// Always leave a blank space at the end of a chip.
- int textLength = destText.length();
+ int textLength = destText.length() - 1;
SpannableString chipText = new SpannableString(destText);
int end = getSelectionEnd();
int start = mTokenizer.findTokenStart(getText(), end);
@@ -983,7 +982,7 @@
if (editable.length() > tokenEnd && editable.charAt(tokenEnd) == ',') {
tokenEnd++;
}
- String text = editable.toString().substring(start, tokenEnd);
+ String text = editable.toString().substring(start, tokenEnd).trim();
clearComposingText();
if (text != null && text.length() > 0 && !text.equals(" ")) {
RecipientEntry entry = createTokenizedEntry(text);
@@ -1313,7 +1312,7 @@
private CharSequence createChip(RecipientEntry entry, boolean pressed) {
String displayText = createDisplayText(entry);
// Always leave a blank space at the end of a chip.
- int textLength = displayText.length();
+ int textLength = displayText.length()-1;
SpannableString chipText = new SpannableString(displayText);
int end = getSelectionEnd();
int start = mTokenizer.findTokenStart(getText(), end);
@@ -1749,13 +1748,18 @@
int spanStart = spannable.getSpanStart(chip);
int spanEnd = spannable.getSpanEnd(chip);
Editable text = getText();
+ int toDelete = spanEnd;
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);
- text.delete(spanStart, spanEnd);
+ text.delete(spanStart, toDelete);
if (wasSelected) {
clearSelectedChip();
}
@@ -1780,7 +1784,15 @@
editable.insert(0, chipText);
} else {
if (!TextUtils.isEmpty(chipText)) {
- editable.replace(start, end, 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);
diff --git a/tests/src/com/android/ex/chips/ChipsTest.java b/tests/src/com/android/ex/chips/ChipsTest.java
index 26a9631..eeeed0b 100644
--- a/tests/src/com/android/ex/chips/ChipsTest.java
+++ b/tests/src/com/android/ex/chips/ChipsTest.java
@@ -118,9 +118,9 @@
mEditable = new SpannableStringBuilder();
mEditable.append(first + extra + second);
int firstStart = mEditable.toString().indexOf(first);
- int firstEnd = firstStart + first.length();
+ int firstEnd = firstStart + first.trim().length();
int secondStart = mEditable.toString().indexOf(second);
- int secondEnd = secondStart + second.length();
+ int secondEnd = secondStart + second.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 1], secondStart, secondEnd, 0);
view.sanitizeBetween();
@@ -158,11 +158,11 @@
mEditable.append(first+second+third);
int firstStart = mEditable.toString().indexOf(first);
- int firstEnd = firstStart + first.length();
+ int firstEnd = firstStart + first.trim().length();
int secondStart = mEditable.toString().indexOf(second);
- int secondEnd = secondStart + second.length();
+ int secondEnd = secondStart + second.trim().length();
int thirdStart = mEditable.toString().indexOf(third);
- int thirdEnd = thirdStart + third.length();
+ int thirdEnd = thirdStart + third.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0);
@@ -226,25 +226,25 @@
mEditable.append(first+second+third+fourth+fifth+sixth+seventh+eigth+ninth+tenth);
int firstStart = mEditable.toString().indexOf(first);
- int firstEnd = firstStart + first.length();
+ int firstEnd = firstStart + first.trim().length();
int secondStart = mEditable.toString().indexOf(second);
- int secondEnd = secondStart + second.length();
+ int secondEnd = secondStart + second.trim().length();
int thirdStart = mEditable.toString().indexOf(third);
- int thirdEnd = thirdStart + third.length();
+ int thirdEnd = thirdStart + third.trim().length();
int fourthStart = mEditable.toString().indexOf(fourth);
- int fourthEnd = fourthStart + fourth.length();
+ int fourthEnd = fourthStart + fourth.trim().length();
int fifthStart = mEditable.toString().indexOf(fifth);
- int fifthEnd = fifthStart + fifth.length();
+ int fifthEnd = fifthStart + fifth.trim().length();
int sixthStart = mEditable.toString().indexOf(sixth);
- int sixthEnd = sixthStart + sixth.length();
+ int sixthEnd = sixthStart + sixth.trim().length();
int seventhStart = mEditable.toString().indexOf(seventh);
- int seventhEnd = seventhStart + seventh.length();
+ int seventhEnd = seventhStart + seventh.trim().length();
int eighthStart = mEditable.toString().indexOf(eigth);
- int eighthEnd = eighthStart + eigth.length();
+ int eighthEnd = eighthStart + eigth.trim().length();
int ninthStart = mEditable.toString().indexOf(ninth);
- int ninthEnd = ninthStart + ninth.length();
+ int ninthEnd = ninthStart + ninth.trim().length();
int tenthStart = mEditable.toString().indexOf(tenth);
- int tenthEnd = tenthStart + tenth.length();
+ int tenthEnd = tenthStart + tenth.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 10], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 9], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 8], thirdStart, thirdEnd, 0);
@@ -298,11 +298,11 @@
mEditable.append(first+second+third);
int firstStart = mEditable.toString().indexOf(first);
- int firstEnd = firstStart + first.length();
+ int firstEnd = firstStart + first.trim().length();
int secondStart = mEditable.toString().indexOf(second);
- int secondEnd = secondStart + second.length();
+ int secondEnd = secondStart + second.trim().length();
int thirdStart = mEditable.toString().indexOf(third);
- int thirdEnd = thirdStart + third.length();
+ int thirdEnd = thirdStart + third.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0);
@@ -339,13 +339,13 @@
mEditable.append(first+second+third+third);
int firstStart = mEditable.toString().indexOf(first);
- int firstEnd = firstStart + first.length();
+ int firstEnd = firstStart + first.trim().length();
int secondStart = mEditable.toString().indexOf(second);
- int secondEnd = secondStart + second.length();
+ int secondEnd = secondStart + second.trim().length();
int thirdStart = mEditable.toString().indexOf(third);
- int thirdEnd = thirdStart + third.length();
+ int thirdEnd = thirdStart + third.trim().length();
int thirdNextStart = mEditable.toString().indexOf(third, thirdEnd);
- int thirdNextEnd = thirdNextStart + third.length();
+ int thirdNextEnd = thirdNextStart + third.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 4], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 3], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], thirdStart, thirdEnd, 0);
@@ -503,11 +503,11 @@
// Test replacing the first chip with a new chip.
int firstStart = mEditable.toString().indexOf(first);
- int firstEnd = firstStart + first.length();
+ int firstEnd = firstStart + first.trim().length();
int secondStart = mEditable.toString().indexOf(second);
- int secondEnd = secondStart + second.length();
+ int secondEnd = secondStart + second.trim().length();
int thirdStart = mEditable.toString().indexOf(third);
- int thirdEnd = thirdStart + third.length();
+ int thirdEnd = thirdStart + third.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0);
@@ -523,12 +523,12 @@
.toString().indexOf(second));
assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 2]), mEditable
.toString().indexOf(second)
- + second.length());
+ + second.trim().length());
assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 1]), mEditable
.toString().indexOf(third));
assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 1]), mEditable
.toString().indexOf(third)
- + third.length());
+ + third.trim().length());
RecipientChip[] spans = mEditable.getSpans(0, mEditable.length(), RecipientChip.class);
assertEquals(spans.length, 3);
spans = mEditable
@@ -540,20 +540,19 @@
mEditable = new SpannableStringBuilder();
mEditable.append(first + second + third);
firstStart = mEditable.toString().indexOf(first);
- firstEnd = firstStart + first.length();
+ firstEnd = firstStart + first.trim().length();
secondStart = mEditable.toString().indexOf(second);
- secondEnd = secondStart + second.length();
+ secondEnd = secondStart + second.trim().length();
thirdStart = mEditable.toString().indexOf(third);
- thirdEnd = thirdStart + third.length();
+ thirdEnd = thirdStart + third.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0);
assertEquals(mEditable.toString(), first + second + third);
view.replaceChip(mMockRecips[mMockRecips.length - 2], RecipientEntry
.constructGeneratedEntry("replacement", "replacement@replacement.com"));
- assertEquals(mEditable.toString(), mTokenizer
- .terminateToken("replacement <replacement@replacement.com>")
- + second + third);
+ assertEquals(mEditable.toString(), first + mTokenizer
+ .terminateToken("replacement <replacement@replacement.com>") + third);
assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 3]), firstStart);
assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 3]), firstEnd);
assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 2]), -1);
@@ -562,7 +561,7 @@
.toString().indexOf(third));
assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 1]), mEditable
.toString().indexOf(third)
- + third.length());
+ + third.trim().length());
spans = mEditable.getSpans(0, mEditable.length(), RecipientChip.class);
assertEquals(spans.length, 3);
spans = mEditable
@@ -574,20 +573,19 @@
mEditable = new SpannableStringBuilder();
mEditable.append(first + second + third);
firstStart = mEditable.toString().indexOf(first);
- firstEnd = firstStart + first.length();
+ firstEnd = firstStart + first.trim().length();
secondStart = mEditable.toString().indexOf(second);
- secondEnd = secondStart + second.length();
+ secondEnd = secondStart + second.trim().length();
thirdStart = mEditable.toString().indexOf(third);
- thirdEnd = thirdStart + third.length();
+ thirdEnd = thirdStart + third.trim().length();
mEditable.setSpan(mMockRecips[mMockRecips.length - 3], firstStart, firstEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 2], secondStart, secondEnd, 0);
mEditable.setSpan(mMockRecips[mMockRecips.length - 1], thirdStart, thirdEnd, 0);
assertEquals(mEditable.toString(), first + second + third);
view.replaceChip(mMockRecips[mMockRecips.length - 1], RecipientEntry
.constructGeneratedEntry("replacement", "replacement@replacement.com"));
- assertEquals(mEditable.toString(), mTokenizer
- .terminateToken("replacement <replacement@replacement.com>")
- + second + third);
+ assertEquals(mEditable.toString(), first + second + mTokenizer
+ .terminateToken("replacement <replacement@replacement.com>"));
assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 3]), firstStart);
assertEquals(mEditable.getSpanEnd(mMockRecips[mMockRecips.length - 3]), firstEnd);
assertEquals(mEditable.getSpanStart(mMockRecips[mMockRecips.length - 2]), secondStart);