Merge "Optimisations and bugs in SpellChecker"
This commit is contained in:
committed by
Android (Google) Code Review
commit
a13a322e09
@@ -165,6 +165,7 @@ public class SpellChecker implements SpellCheckerSessionListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGetSuggestions(SuggestionsInfo[] results) {
|
public void onGetSuggestions(SuggestionsInfo[] results) {
|
||||||
|
final Editable editable = (Editable) mTextView.getText();
|
||||||
for (int i = 0; i < results.length; i++) {
|
for (int i = 0; i < results.length; i++) {
|
||||||
SuggestionsInfo suggestionsInfo = results[i];
|
SuggestionsInfo suggestionsInfo = results[i];
|
||||||
if (suggestionsInfo.getCookie() != mCookie) continue;
|
if (suggestionsInfo.getCookie() != mCookie) continue;
|
||||||
@@ -178,18 +179,19 @@ public class SpellChecker implements SpellCheckerSessionListener {
|
|||||||
boolean looksLikeTypo =
|
boolean looksLikeTypo =
|
||||||
((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
|
((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
|
||||||
|
|
||||||
|
SpellCheckSpan spellCheckSpan = mSpellCheckSpans[j];
|
||||||
if (!isInDictionary && looksLikeTypo) {
|
if (!isInDictionary && looksLikeTypo) {
|
||||||
createMisspelledSuggestionSpan(suggestionsInfo, mSpellCheckSpans[j]);
|
createMisspelledSuggestionSpan(editable, suggestionsInfo, spellCheckSpan);
|
||||||
}
|
}
|
||||||
|
editable.removeSpan(spellCheckSpan);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createMisspelledSuggestionSpan(SuggestionsInfo suggestionsInfo,
|
private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo,
|
||||||
SpellCheckSpan spellCheckSpan) {
|
SpellCheckSpan spellCheckSpan) {
|
||||||
final Editable editable = (Editable) mTextView.getText();
|
|
||||||
final int start = editable.getSpanStart(spellCheckSpan);
|
final int start = editable.getSpanStart(spellCheckSpan);
|
||||||
final int end = editable.getSpanEnd(spellCheckSpan);
|
final int end = editable.getSpanEnd(spellCheckSpan);
|
||||||
|
|
||||||
@@ -251,6 +253,5 @@ public class SpellChecker implements SpellCheckerSessionListener {
|
|||||||
|
|
||||||
// TODO limit to the word rectangle region
|
// TODO limit to the word rectangle region
|
||||||
mTextView.invalidate();
|
mTextView.invalidate();
|
||||||
editable.removeSpan(spellCheckSpan);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7780,7 +7780,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
|
|
||||||
// Iterate over the newly added text and schedule new SpellCheckSpans
|
// Iterate over the newly added text and schedule new SpellCheckSpans
|
||||||
while (wordStart <= shiftedEnd) {
|
while (wordStart <= shiftedEnd) {
|
||||||
if (wordEnd >= shiftedStart) {
|
if (wordEnd >= shiftedStart && wordEnd > wordStart) {
|
||||||
// A new word has been created across the interval boundaries. Remove previous spans
|
// A new word has been created across the interval boundaries. Remove previous spans
|
||||||
if (wordStart < shiftedStart && wordEnd > shiftedStart) {
|
if (wordStart < shiftedStart && wordEnd > shiftedStart) {
|
||||||
removeSpansAt(start, spellCheckSpans, text);
|
removeSpansAt(start, spellCheckSpans, text);
|
||||||
@@ -9946,8 +9946,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
suggestionInfo.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
suggestionInfo.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
|
||||||
// Add the text before and after the span.
|
// Add the text before and after the span.
|
||||||
suggestionInfo.text.insert(0, mText.subSequence(unionStart, spanStart).toString());
|
suggestionInfo.text.insert(0, mText.toString().substring(unionStart, spanStart));
|
||||||
suggestionInfo.text.append(mText.subSequence(spanEnd, unionEnd).toString());
|
suggestionInfo.text.append(mText.toString().substring(spanEnd, unionEnd));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -9979,7 +9979,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
hide();
|
hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final String originalText = mText.subSequence(spanStart, spanEnd).toString();
|
final String originalText = mText.toString().substring(spanStart, spanEnd);
|
||||||
|
|
||||||
if (suggestionInfo.suggestionIndex == ADD_TO_DICTIONARY) {
|
if (suggestionInfo.suggestionIndex == ADD_TO_DICTIONARY) {
|
||||||
Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT);
|
Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT);
|
||||||
@@ -10016,8 +10016,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
if (!TextUtils.isEmpty(
|
if (!TextUtils.isEmpty(
|
||||||
suggestionInfo.suggestionSpan.getNotificationTargetClassName())) {
|
suggestionInfo.suggestionSpan.getNotificationTargetClassName())) {
|
||||||
InputMethodManager imm = InputMethodManager.peekInstance();
|
InputMethodManager imm = InputMethodManager.peekInstance();
|
||||||
imm.notifySuggestionPicked(suggestionInfo.suggestionSpan, originalText,
|
if (imm != null) {
|
||||||
suggestionInfo.suggestionIndex);
|
imm.notifySuggestionPicked(suggestionInfo.suggestionSpan, originalText,
|
||||||
|
suggestionInfo.suggestionIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap text content between actual text and Suggestion span
|
// Swap text content between actual text and Suggestion span
|
||||||
@@ -10037,7 +10039,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move cursor at the end of the replacement word
|
// Move cursor at the end of the replaced word
|
||||||
Selection.setSelection(editable, spanEnd + lengthDifference);
|
Selection.setSelection(editable, spanEnd + lengthDifference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10166,8 +10168,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
|
|
||||||
if (!hasSelection()) {
|
if (!hasSelection()) {
|
||||||
// There may already be a selection on device rotation
|
// There may already be a selection on device rotation
|
||||||
boolean currentWordSelected = selectCurrentWord();
|
if (!selectCurrentWord()) {
|
||||||
if (!currentWordSelected) {
|
|
||||||
// No word found under cursor or text selection not permitted.
|
// No word found under cursor or text selection not permitted.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user