From 69865bd6860a97793a06523a48dfe6472e9b7562 Mon Sep 17 00:00:00 2001 From: Gilles Debunne Date: Wed, 9 May 2012 11:12:03 -0700 Subject: [PATCH] Minor changes in SpellCheckSpan pool management in SpellChecker Bug 6464190 The 'inProgress' flag is set to false when the SpellCheckSpan starts to get used (instead of a less intuitive when it is removed). Pool recycling in handled by onSpellCheckSpanRemoved, called from the TextView's SpanWatcher, when a SpellCheckSpan is removed for any reason (from the SC code or due to text editing). The other change is that Sentence SC now correctly removes the span from the text (and hence recycles it in the pool). Change-Id: If8b433fd5e41d4dc0304a127ebcc088ea1eecaa7 --- core/java/android/widget/SpellChecker.java | 18 ++++++++++-------- core/java/android/widget/TextView.java | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/core/java/android/widget/SpellChecker.java b/core/java/android/widget/SpellChecker.java index e1103dd2f0355..96edddb91e5e3 100644 --- a/core/java/android/widget/SpellChecker.java +++ b/core/java/android/widget/SpellChecker.java @@ -132,8 +132,6 @@ public class SpellChecker implements SpellCheckerSessionListener { // Restore SpellCheckSpans in pool for (int i = 0; i < mLength; i++) { - // Resets id and progress to invalidate spell check span - mSpellCheckSpans[i].setSpellCheckInProgress(false); mIds[i] = -1; } mLength = 0; @@ -200,15 +198,16 @@ public class SpellChecker implements SpellCheckerSessionListener { private void addSpellCheckSpan(Editable editable, int start, int end) { final int index = nextSpellCheckSpanIndex(); - editable.setSpan(mSpellCheckSpans[index], start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + SpellCheckSpan spellCheckSpan = mSpellCheckSpans[index]; + editable.setSpan(spellCheckSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + spellCheckSpan.setSpellCheckInProgress(false); mIds[index] = mSpanSequenceCounter++; } - public void removeSpellCheckSpan(SpellCheckSpan spellCheckSpan) { + public void onSpellCheckSpanRemoved(SpellCheckSpan spellCheckSpan) { + // Recycle any removed SpellCheckSpan (from this code or during text edition) for (int i = 0; i < mLength; i++) { if (mSpellCheckSpans[i] == spellCheckSpan) { - // Resets id and progress to invalidate spell check span - mSpellCheckSpans[i].setSpellCheckInProgress(false); mIds[i] = -1; return; } @@ -357,6 +356,7 @@ public class SpellChecker implements SpellCheckerSessionListener { final SpellCheckSpan spellCheckSpan = onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE); if (spellCheckSpan != null) { + // onSpellCheckSpanRemoved will recycle this span in the pool editable.removeSpan(spellCheckSpan); } } @@ -384,11 +384,12 @@ public class SpellChecker implements SpellCheckerSessionListener { suggestionsInfo, offset, length); if (spellCheckSpan == null && scs != null) { // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same - // SentenceSuggestionsInfo + // SentenceSuggestionsInfo. Removal is deferred after this loop. spellCheckSpan = scs; } } if (spellCheckSpan != null) { + // onSpellCheckSpanRemoved will recycle this span in the pool editable.removeSpan(spellCheckSpan); } } @@ -595,7 +596,8 @@ public class SpellChecker implements SpellCheckerSessionListener { } break; } - removeSpellCheckSpan(spellCheckSpan); + // This spellCheckSpan is replaced by the one we are creating + editable.removeSpan(spellCheckSpan); spellCheckStart = Math.min(spanStart, spellCheckStart); spellCheckEnd = Math.max(spanEnd, spellCheckEnd); } diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 555c974360fcf..277b26e7e89da 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -7214,7 +7214,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (mEditor != null && mEditor.mSpellChecker != null && newStart < 0 && what instanceof SpellCheckSpan) { - mEditor.mSpellChecker.removeSpellCheckSpan((SpellCheckSpan) what); + mEditor.mSpellChecker.onSpellCheckSpanRemoved((SpellCheckSpan) what); } }