Skip to content

Commit 572527f

Browse files
Gilles DebunneAndroid (Google) Code Review
authored andcommitted
Merge "Remove the suggestion underline when the TextView loses focus."
2 parents 2d05d4b + fe5e983 commit 572527f

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

core/java/android/text/style/SuggestionSpan.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan {
7676
private final String mNotificationTargetClassName;
7777
private final int mHashCode;
7878

79-
private float mUnderlineThickness;
80-
private int mUnderlineColor;
79+
private float mEasyCorrectUnderlineThickness;
80+
private int mEasyCorrectUnderlineColor;
81+
82+
private float mMisspelledUnderlineThickness;
83+
private int mMisspelledUnderlineColor;
8184

8285
/*
8386
* TODO: If switching IME is required, needs to add parameters for ids of InputMethodInfo
@@ -132,25 +135,22 @@ public SuggestionSpan(Context context, Locale locale, String[] suggestions, int
132135
}
133136

134137
private void initStyle(Context context) {
135-
int defStyle = 0;
136-
if ((getFlags() & FLAG_MISSPELLED) != 0) {
137-
defStyle = com.android.internal.R.attr.textAppearanceMisspelledSuggestion;
138-
} else if ((getFlags() & FLAG_EASY_CORRECT) != 0) {
139-
defStyle = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion;
140-
} else {
141-
// No style is applied.
142-
mUnderlineThickness = 0;
143-
mUnderlineColor = 0;
144-
return;
145-
}
138+
int defStyle = com.android.internal.R.attr.textAppearanceMisspelledSuggestion;
139+
TypedArray typedArray = context.obtainStyledAttributes(
140+
null, com.android.internal.R.styleable.SuggestionSpan, defStyle, 0);
141+
mMisspelledUnderlineThickness = typedArray.getDimension(
142+
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
143+
mMisspelledUnderlineColor = typedArray.getColor(
144+
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
145+
146+
defStyle = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion;
146147

147-
TypedArray typedArray = context.obtainStyledAttributes(null,
148-
com.android.internal.R.styleable.SuggestionSpan,
149-
defStyle, 0);
148+
typedArray = context.obtainStyledAttributes(
149+
null, com.android.internal.R.styleable.SuggestionSpan, defStyle, 0);
150150

151-
mUnderlineThickness = typedArray.getDimension(
151+
mEasyCorrectUnderlineThickness = typedArray.getDimension(
152152
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
153-
mUnderlineColor = typedArray.getColor(
153+
mEasyCorrectUnderlineColor = typedArray.getColor(
154154
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
155155
}
156156

@@ -160,8 +160,10 @@ public SuggestionSpan(Parcel src) {
160160
mLocaleString = src.readString();
161161
mNotificationTargetClassName = src.readString();
162162
mHashCode = src.readInt();
163-
mUnderlineColor = src.readInt();
164-
mUnderlineThickness = src.readFloat();
163+
mEasyCorrectUnderlineColor = src.readInt();
164+
mEasyCorrectUnderlineThickness = src.readFloat();
165+
mMisspelledUnderlineColor = src.readInt();
166+
mMisspelledUnderlineThickness = src.readFloat();
165167
}
166168

167169
/**
@@ -211,8 +213,10 @@ public void writeToParcel(Parcel dest, int flags) {
211213
dest.writeString(mLocaleString);
212214
dest.writeString(mNotificationTargetClassName);
213215
dest.writeInt(mHashCode);
214-
dest.writeInt(mUnderlineColor);
215-
dest.writeFloat(mUnderlineThickness);
216+
dest.writeInt(mEasyCorrectUnderlineColor);
217+
dest.writeFloat(mEasyCorrectUnderlineThickness);
218+
dest.writeInt(mMisspelledUnderlineColor);
219+
dest.writeFloat(mMisspelledUnderlineThickness);
216220
}
217221

218222
@Override
@@ -254,6 +258,10 @@ public SuggestionSpan[] newArray(int size) {
254258

255259
@Override
256260
public void updateDrawState(TextPaint tp) {
257-
tp.setUnderlineText(mUnderlineColor, mUnderlineThickness);
261+
if ((mFlags & FLAG_MISSPELLED) != 0) {
262+
tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness);
263+
} else if ((mFlags & FLAG_EASY_CORRECT) != 0) {
264+
tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness);
265+
}
258266
}
259267
}

core/java/android/widget/TextView.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8121,6 +8121,7 @@ protected void onFocusChanged(boolean focused, int direction, Rect previouslyFoc
81218121
Selection.setSelection((Spannable) mText, selStart, selEnd);
81228122
} else {
81238123
hideControllers();
8124+
downgradeEasyCorrectionSpans();
81248125
}
81258126

81268127
// No need to create the controller
@@ -8327,6 +8328,26 @@ private boolean isCursorInsideEasyCorrectionSpan() {
83278328
return false;
83288329
}
83298330

8331+
/**
8332+
* Downgrades to simple suggestions all the easy correction spans that are not a spell check
8333+
* span.
8334+
*/
8335+
private void downgradeEasyCorrectionSpans() {
8336+
if (mText instanceof Spannable) {
8337+
Spannable spannable = (Spannable) mText;
8338+
SuggestionSpan[] suggestionSpans = spannable.getSpans(0,
8339+
spannable.length(), SuggestionSpan.class);
8340+
for (int i = 0; i < suggestionSpans.length; i++) {
8341+
int flags = suggestionSpans[i].getFlags();
8342+
if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
8343+
&& (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
8344+
flags = flags & ~SuggestionSpan.FLAG_EASY_CORRECT;
8345+
suggestionSpans[i].setFlags(flags);
8346+
}
8347+
}
8348+
}
8349+
}
8350+
83308351
@Override
83318352
public boolean onGenericMotionEvent(MotionEvent event) {
83328353
if (mMovement != null && mText instanceof Spannable && mLayout != null) {

0 commit comments

Comments
 (0)