am 782c9fee: Merge "Don\'t resume suggestion on links."

* commit '782c9fee6bcb32d67c8ee9faa7e5c9dfb091afd3':
  Don't resume suggestion on links.
This commit is contained in:
Jean Chalard 2014-02-17 03:48:50 -08:00 committed by Android Git Automerger
commit 5828d1f3fa
4 changed files with 26 additions and 3 deletions

View file

@ -668,12 +668,16 @@ public final class RichInputConnection {
} }
} }
final boolean hasUrlSpans =
SpannableStringUtils.hasUrlSpans(before, startIndexInBefore, before.length())
|| SpannableStringUtils.hasUrlSpans(after, 0, endIndexInAfter);
// We don't use TextUtils#concat because it copies all spans without respect to their // We don't use TextUtils#concat because it copies all spans without respect to their
// nature. If the text includes a PARAGRAPH span and it has been split, then // nature. If the text includes a PARAGRAPH span and it has been split, then
// TextUtils#concat will crash when it tries to concat both sides of it. // TextUtils#concat will crash when it tries to concat both sides of it.
return new TextRange( return new TextRange(
SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(before, after), SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(before, after),
startIndexInBefore, before.length() + endIndexInAfter, before.length()); startIndexInBefore, before.length() + endIndexInAfter, before.length(),
hasUrlSpans);
} }
public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations) { public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations) {

View file

@ -1270,6 +1270,10 @@ public final class InputLogic {
if (range.length() <= 0) return; // Race condition. No text to resume on, so bail out. if (range.length() <= 0) return; // Race condition. No text to resume on, so bail out.
// If for some strange reason (editor bug or so) we measure the text before the cursor as // If for some strange reason (editor bug or so) we measure the text before the cursor as
// longer than what the entire text is supposed to be, the safe thing to do is bail out. // longer than what the entire text is supposed to be, the safe thing to do is bail out.
if (range.mHasUrlSpans) return; // If there are links, we don't resume suggestions. Making
// edits to a linkified text through batch commands would ruin the URL spans, and unless
// we take very complicated steps to preserve the whole link, we can't do things right so
// we just do not resume because it's safer.
final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor(); final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor();
if (numberOfCharsInWordBeforeCursor > expectedCursorPosition) return; if (numberOfCharsInWordBeforeCursor > expectedCursorPosition) return;
final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList(); final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();

View file

@ -22,6 +22,7 @@ import android.text.Spanned;
import android.text.SpannedString; import android.text.SpannedString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.SuggestionSpan; import android.text.style.SuggestionSpan;
import android.text.style.URLSpan;
public final class SpannableStringUtils { public final class SpannableStringUtils {
/** /**
@ -112,4 +113,16 @@ public final class SpannableStringUtils {
return new SpannedString(ss); return new SpannedString(ss);
} }
public static boolean hasUrlSpans(final CharSequence text,
final int startIndex, final int endIndex) {
if (!(text instanceof Spanned)) {
return false; // Not spanned, so no link
}
final Spanned spanned = (Spanned)text;
// getSpans(x, y) does not return spans that start on x or end on y. x-1, y+1 does the
// trick, and works in all cases even if startIndex <= 0 or endIndex >= text.length().
final URLSpan[] spans = spanned.getSpans(startIndex - 1, endIndex + 1, URLSpan.class);
return null != spans && spans.length > 0;
}
} }

View file

@ -31,6 +31,7 @@ public final class TextRange {
private final int mCursorIndex; private final int mCursorIndex;
public final CharSequence mWord; public final CharSequence mWord;
public final boolean mHasUrlSpans;
public int getNumberOfCharsInWordBeforeCursor() { public int getNumberOfCharsInWordBeforeCursor() {
return mCursorIndex - mWordAtCursorStartIndex; return mCursorIndex - mWordAtCursorStartIndex;
@ -95,7 +96,7 @@ public final class TextRange {
} }
} }
if (spanStart == mWordAtCursorStartIndex && spanEnd == mWordAtCursorEndIndex) { if (spanStart == mWordAtCursorStartIndex && spanEnd == mWordAtCursorEndIndex) {
// If the span does not start and stop here, we ignore it. It probably extends // If the span does not start and stop here, ignore it. It probably extends
// past the start or end of the word, as happens in missing space correction // past the start or end of the word, as happens in missing space correction
// or EasyEditSpans put by voice input. // or EasyEditSpans put by voice input.
spans[writeIndex++] = spans[readIndex]; spans[writeIndex++] = spans[readIndex];
@ -105,7 +106,7 @@ public final class TextRange {
} }
public TextRange(final CharSequence textAtCursor, final int wordAtCursorStartIndex, public TextRange(final CharSequence textAtCursor, final int wordAtCursorStartIndex,
final int wordAtCursorEndIndex, final int cursorIndex) { final int wordAtCursorEndIndex, final int cursorIndex, final boolean hasUrlSpans) {
if (wordAtCursorStartIndex < 0 || cursorIndex < wordAtCursorStartIndex if (wordAtCursorStartIndex < 0 || cursorIndex < wordAtCursorStartIndex
|| cursorIndex > wordAtCursorEndIndex || cursorIndex > wordAtCursorEndIndex
|| wordAtCursorEndIndex > textAtCursor.length()) { || wordAtCursorEndIndex > textAtCursor.length()) {
@ -115,6 +116,7 @@ public final class TextRange {
mWordAtCursorStartIndex = wordAtCursorStartIndex; mWordAtCursorStartIndex = wordAtCursorStartIndex;
mWordAtCursorEndIndex = wordAtCursorEndIndex; mWordAtCursorEndIndex = wordAtCursorEndIndex;
mCursorIndex = cursorIndex; mCursorIndex = cursorIndex;
mHasUrlSpans = hasUrlSpans;
mWord = mTextAtCursor.subSequence(mWordAtCursorStartIndex, mWordAtCursorEndIndex); mWord = mTextAtCursor.subSequence(mWordAtCursorStartIndex, mWordAtCursorEndIndex);
} }
} }