Skip to content

Commit ea1c7f3

Browse files
Gilles DebunneAndroid (Google) Code Review
authored andcommitted
Merge "Bug 5248215: Even though I turned off the Spelling correction, it still shows up"
2 parents 9722442 + 186aaf9 commit ea1c7f3

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

core/java/android/widget/SpellChecker.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ public SpellChecker(TextView textView) {
7575
mLength = 0;
7676
}
7777

78+
/**
79+
* @return true if a spell checker session has successfully been created. Returns false if not,
80+
* for instance when spell checking has been disabled in settings.
81+
*/
82+
public boolean isSessionActive() {
83+
return mSpellCheckerSession != null;
84+
}
85+
86+
public void closeSession() {
87+
if (mSpellCheckerSession != null) {
88+
mSpellCheckerSession.close();
89+
}
90+
}
91+
7892
public void addSpellCheckSpan(SpellCheckSpan spellCheckSpan) {
7993
int length = mIds.length;
8094
if (mLength >= length) {

core/java/android/widget/TextView.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,8 @@ private void setText(CharSequence text, BufferType type,
32503250
sendOnTextChanged(text, 0, oldlen, textLength);
32513251
onTextChanged(text, 0, oldlen, textLength);
32523252

3253-
if (startSpellCheck) {
3253+
if (startSpellCheck && mSpellChecker != null) {
3254+
// This view has to have been previously attached for mSpellChecker to exist
32543255
updateSpellCheckSpans(0, textLength);
32553256
}
32563257

@@ -4412,6 +4413,8 @@ protected void onAttachedToWindow() {
44124413

44134414
// Resolve drawables as the layout direction has been resolved
44144415
resolveDrawables();
4416+
4417+
updateSpellCheckSpans(0, mText.length());
44154418
}
44164419

44174420
@Override
@@ -4443,6 +4446,14 @@ protected void onDetachedFromWindow() {
44434446
hideControllers();
44444447

44454448
resetResolvedDrawables();
4449+
4450+
if (mSpellChecker != null) {
4451+
mSpellChecker.closeSession();
4452+
removeMisspelledSpans();
4453+
// Forces the creation of a new SpellChecker next time this window is created.
4454+
// Will handle the cases where the settings has been changed in the meantime.
4455+
mSpellChecker = null;
4456+
}
44464457
}
44474458

44484459
@Override
@@ -7595,7 +7606,7 @@ void handleTextChanged(CharSequence buffer, int start, int before, int after) {
75957606
}
75967607
ims.mChangedDelta += after-before;
75977608
}
7598-
7609+
75997610
sendOnTextChanged(buffer, start, before, after);
76007611
onTextChanged(buffer, start, before, after);
76017612

@@ -7737,7 +7748,8 @@ void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd
77377748
* Create new SpellCheckSpans on the modified region.
77387749
*/
77397750
private void updateSpellCheckSpans(int start, int end) {
7740-
if (!(mText instanceof Editable) || !isSuggestionsEnabled()) return;
7751+
if (!isTextEditable() || !isSuggestionsEnabled() || !getSpellChecker().isSessionActive())
7752+
return;
77417753
Editable text = (Editable) mText;
77427754

77437755
WordIterator wordIterator = getWordIterator();
@@ -8427,13 +8439,31 @@ private void downgradeEasyCorrectionSpans() {
84278439
int flags = suggestionSpans[i].getFlags();
84288440
if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
84298441
&& (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
8430-
flags = flags & ~SuggestionSpan.FLAG_EASY_CORRECT;
8442+
flags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
84318443
suggestionSpans[i].setFlags(flags);
84328444
}
84338445
}
84348446
}
84358447
}
84368448

8449+
/**
8450+
* Removes the suggestion spans for misspelled words.
8451+
*/
8452+
private void removeMisspelledSpans() {
8453+
if (mText instanceof Spannable) {
8454+
Spannable spannable = (Spannable) mText;
8455+
SuggestionSpan[] suggestionSpans = spannable.getSpans(0,
8456+
spannable.length(), SuggestionSpan.class);
8457+
for (int i = 0; i < suggestionSpans.length; i++) {
8458+
int flags = suggestionSpans[i].getFlags();
8459+
if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
8460+
&& (flags & SuggestionSpan.FLAG_MISSPELLED) != 0) {
8461+
spannable.removeSpan(suggestionSpans[i]);
8462+
}
8463+
}
8464+
}
8465+
}
8466+
84378467
@Override
84388468
public boolean onGenericMotionEvent(MotionEvent event) {
84398469
if (mMovement != null && mText instanceof Spannable && mLayout != null) {
@@ -9573,7 +9603,6 @@ private class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItem
95739603
private final Comparator<SuggestionSpan> mSuggestionSpanComparator;
95749604
private final HashMap<SuggestionSpan, Integer> mSpansLengths;
95759605

9576-
95779606
private class CustomPopupWindow extends PopupWindow {
95789607
public CustomPopupWindow(Context context, int defStyle) {
95799608
super(context, null, defStyle);
@@ -9585,9 +9614,8 @@ public void dismiss() {
95859614

95869615
TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this);
95879616

9588-
if ((mText instanceof Spannable)) {
9589-
((Spannable) mText).removeSpan(mSuggestionRangeSpan);
9590-
}
9617+
// Safe cast since show() checks that mText is an Editable
9618+
((Spannable) mText).removeSpan(mSuggestionRangeSpan);
95919619

95929620
setCursorVisible(mCursorWasVisibleBeforeSuggestions);
95939621
if (hasInsertionController()) {
@@ -9637,8 +9665,8 @@ private class SuggestionInfo {
96379665
void removeMisspelledFlag() {
96389666
int suggestionSpanFlags = suggestionSpan.getFlags();
96399667
if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) > 0) {
9640-
suggestionSpanFlags &= ~(SuggestionSpan.FLAG_MISSPELLED);
9641-
suggestionSpanFlags &= ~(SuggestionSpan.FLAG_EASY_CORRECT);
9668+
suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED;
9669+
suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
96429670
suggestionSpan.setFlags(suggestionSpanFlags);
96439671
}
96449672
}
@@ -10520,9 +10548,7 @@ private boolean isVisible() {
1052010548

1052110549
public abstract int getCurrentCursorOffset();
1052210550

10523-
protected void updateSelection(int offset) {
10524-
updateDrawable();
10525-
}
10551+
protected abstract void updateSelection(int offset);
1052610552

1052710553
public abstract void updatePosition(float x, float y);
1052810554

@@ -10796,8 +10822,8 @@ public int getCurrentCursorOffset() {
1079610822

1079710823
@Override
1079810824
public void updateSelection(int offset) {
10799-
super.updateSelection(offset);
1080010825
Selection.setSelection((Spannable) mText, offset, getSelectionEnd());
10826+
updateDrawable();
1080110827
}
1080210828

1080310829
@Override
@@ -10838,8 +10864,8 @@ public int getCurrentCursorOffset() {
1083810864

1083910865
@Override
1084010866
public void updateSelection(int offset) {
10841-
super.updateSelection(offset);
1084210867
Selection.setSelection((Spannable) mText, getSelectionStart(), offset);
10868+
updateDrawable();
1084310869
}
1084410870

1084510871
@Override

0 commit comments

Comments
 (0)