summaryrefslogtreecommitdiff
path: root/core/java/android/text/SpannableStringBuilder.java
diff options
context:
space:
mode:
authorGilles Debunne <debunne@google.com>2010-07-02 17:02:34 -0700
committerGilles Debunne <debunne@google.com>2010-07-02 22:01:44 -0700
commit312cd580391d70140eeb4080a4e195039172a6a6 (patch)
tree59f85d1528ee89dfcd45919cce29b514d4d84aac /core/java/android/text/SpannableStringBuilder.java
parent216108c492fef05261c70f1018d94cef0c3b23fd (diff)
Fixed bug in getTextRunCursor.
When auto-correction is used, the text is modified to underline the corrected word, which modifies mGapLength and mGapStart. The mGapLength shift was not properly handled in getTextRunCursor. Bug 2807595 Change-Id: I43ee216f6780270e452aa9199ffd892fe7ab6c3f
Diffstat (limited to 'core/java/android/text/SpannableStringBuilder.java')
-rw-r--r--core/java/android/text/SpannableStringBuilder.java47
1 files changed, 29 insertions, 18 deletions
diff --git a/core/java/android/text/SpannableStringBuilder.java b/core/java/android/text/SpannableStringBuilder.java
index 56f130284464..fc01ef2ba52f 100644
--- a/core/java/android/text/SpannableStringBuilder.java
+++ b/core/java/android/text/SpannableStringBuilder.java
@@ -313,12 +313,15 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
moveGapTo(end);
- if (tbend - tbstart >= mGapLength + (end - start))
- resizeFor(mText.length - mGapLength +
- tbend - tbstart - (end - start));
+ // Can be negative
+ final int nbNewChars = (tbend - tbstart) - (end - start);
- mGapStart += tbend - tbstart - (end - start);
- mGapLength -= tbend - tbstart - (end - start);
+ if (nbNewChars >= mGapLength) {
+ resizeFor(mText.length + nbNewChars - mGapLength);
+ }
+
+ mGapStart += nbNewChars;
+ mGapLength -= nbNewChars;
if (mGapLength < 1)
new Exception("mGapLength < 1").printStackTrace();
@@ -708,6 +711,7 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
* the specified range of the buffer. The kind may be Object.class to get
* a list of all the spans regardless of type.
*/
+ @SuppressWarnings("unchecked")
public <T> T[] getSpans(int queryStart, int queryEnd, Class<T> kind) {
int spanCount = mSpanCount;
Object[] spans = mSpans;
@@ -718,8 +722,8 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
int gaplen = mGapLength;
int count = 0;
- Object[] ret = null;
- Object ret1 = null;
+ T[] ret = null;
+ T ret1 = null;
for (int i = 0; i < spanCount; i++) {
int spanStart = starts[i];
@@ -751,11 +755,13 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
}
if (count == 0) {
- ret1 = spans[i];
+ // Safe conversion thanks to the isInstance test above
+ ret1 = (T) spans[i];
count++;
} else {
if (count == 1) {
- ret = (Object[]) Array.newInstance(kind, spanCount - i + 1);
+ // Safe conversion, but requires a suppressWarning
+ ret = (T[]) Array.newInstance(kind, spanCount - i + 1);
ret[0] = ret1;
}
@@ -772,10 +778,12 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
}
System.arraycopy(ret, j, ret, j + 1, count - j);
- ret[j] = spans[i];
+ // Safe conversion thanks to the isInstance test above
+ ret[j] = (T) spans[i];
count++;
} else {
- ret[count++] = spans[i];
+ // Safe conversion thanks to the isInstance test above
+ ret[count++] = (T) spans[i];
}
}
}
@@ -784,17 +792,19 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
return ArrayUtils.emptyArray(kind);
}
if (count == 1) {
- ret = (Object[]) Array.newInstance(kind, 1);
+ // Safe conversion, but requires a suppressWarning
+ ret = (T[]) Array.newInstance(kind, 1);
ret[0] = ret1;
- return (T[]) ret;
+ return ret;
}
if (count == ret.length) {
- return (T[]) ret;
+ return ret;
}
- Object[] nret = (Object[]) Array.newInstance(kind, count);
+ // Safe conversion, but requires a suppressWarning
+ T[] nret = (T[]) Array.newInstance(kind, count);
System.arraycopy(ret, 0, nret, 0, count);
- return (T[]) nret;
+ return nret;
}
/**
@@ -863,6 +873,7 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
/**
* Return a String containing a copy of the chars in this buffer.
*/
+ @Override
public String toString() {
int len = length();
char[] buf = new char[len];
@@ -953,6 +964,7 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
}
}
+/*
private boolean isprint(char c) { // XXX
if (c >= ' ' && c <= '~')
return true;
@@ -960,7 +972,6 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
return false;
}
-/*
private static final int startFlag(int flag) {
return (flag >> 4) & 0x0F;
}
@@ -1169,7 +1180,7 @@ implements CharSequence, GetChars, Spannable, Editable, Appendable,
flags, offset, cursorOpt);
} else if (contextStart >= mGapStart) {
ret = p.getTextRunCursor(mText, contextStart + mGapLength, contextLen,
- flags, offset + mGapLength, cursorOpt);
+ flags, offset + mGapLength, cursorOpt) - mGapLength;
} else {
char[] buf = TextUtils.obtain(contextLen);
getChars(contextStart, contextEnd, buf, 0);