Skip to content

Commit 9ca4b43

Browse files
committed
Add a hidden flag for the auto correction indicator in SuggestionSpan
Bug: 5245468 Change-Id: Ic111554da86fefd47dd9a26c1bd41e4ee57dc552
1 parent b89ebaa commit 9ca4b43

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan {
5656
*/
5757
public static final int FLAG_MISSPELLED = 0x0002;
5858

59+
/**
60+
* Sets this flag if the auto correction is about to be applied to a word/text
61+
* that the user is typing/composing. This type of suggestion is rendered differently
62+
* to indicate the auto correction is happening.
63+
* @hide
64+
*/
65+
public static final int FLAG_AUTO_CORRECTION = 0x0004;
66+
5967
public static final String ACTION_SUGGESTION_PICKED = "android.text.style.SUGGESTION_PICKED";
6068
public static final String SUGGESTION_SPAN_PICKED_AFTER = "after";
6169
public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before";
@@ -82,6 +90,9 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan {
8290
private float mMisspelledUnderlineThickness;
8391
private int mMisspelledUnderlineColor;
8492

93+
private float mAutoCorrectionUnderlineThickness;
94+
private int mAutoCorrectionUnderlineColor;
95+
8596
/*
8697
* TODO: If switching IME is required, needs to add parameters for ids of InputMethodInfo
8798
* and InputMethodSubtype.
@@ -145,14 +156,21 @@ private void initStyle(Context context) {
145156
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
146157

147158
defStyle = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion;
148-
149159
typedArray = context.obtainStyledAttributes(
150160
null, com.android.internal.R.styleable.SuggestionSpan, defStyle, 0);
151-
152161
mEasyCorrectUnderlineThickness = typedArray.getDimension(
153162
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
154163
mEasyCorrectUnderlineColor = typedArray.getColor(
155164
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
165+
166+
defStyle = com.android.internal.R.attr.textAppearanceAutoCorrectionSuggestion;
167+
typedArray = context.obtainStyledAttributes(
168+
null, com.android.internal.R.styleable.SuggestionSpan, defStyle, 0);
169+
mAutoCorrectionUnderlineThickness = typedArray.getDimension(
170+
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
171+
mAutoCorrectionUnderlineColor = typedArray.getColor(
172+
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
173+
156174
}
157175

158176
public SuggestionSpan(Parcel src) {
@@ -165,6 +183,8 @@ public SuggestionSpan(Parcel src) {
165183
mEasyCorrectUnderlineThickness = src.readFloat();
166184
mMisspelledUnderlineColor = src.readInt();
167185
mMisspelledUnderlineThickness = src.readFloat();
186+
mAutoCorrectionUnderlineColor = src.readInt();
187+
mAutoCorrectionUnderlineThickness = src.readFloat();
168188
}
169189

170190
/**
@@ -218,6 +238,8 @@ public void writeToParcel(Parcel dest, int flags) {
218238
dest.writeFloat(mEasyCorrectUnderlineThickness);
219239
dest.writeInt(mMisspelledUnderlineColor);
220240
dest.writeFloat(mMisspelledUnderlineThickness);
241+
dest.writeInt(mAutoCorrectionUnderlineColor);
242+
dest.writeFloat(mAutoCorrectionUnderlineThickness);
221243
}
222244

223245
@Override
@@ -261,6 +283,7 @@ public SuggestionSpan[] newArray(int size) {
261283
public void updateDrawState(TextPaint tp) {
262284
final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0;
263285
final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0;
286+
final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0;
264287
if (easy) {
265288
if (!misspelled) {
266289
tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness);
@@ -269,6 +292,8 @@ public void updateDrawState(TextPaint tp) {
269292
// than just easy, do not apply misspelled if an easy (or a mispelled) has been set
270293
tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness);
271294
}
295+
} else if (autoCorrection) {
296+
tp.setUnderlineText(mAutoCorrectionUnderlineColor, mAutoCorrectionUnderlineThickness);
272297
}
273298
}
274299

@@ -281,12 +306,15 @@ public int getUnderlineColor() {
281306
// The order here should match what is used in updateDrawState
282307
final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0;
283308
final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0;
309+
final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0;
284310
if (easy) {
285311
if (!misspelled) {
286312
return mEasyCorrectUnderlineColor;
287313
} else {
288314
return mMisspelledUnderlineColor;
289315
}
316+
} else if (autoCorrection) {
317+
return mAutoCorrectionUnderlineColor;
290318
}
291319
return 0;
292320
}

core/res/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
<!-- The underline color and thickness for misspelled suggestion -->
159159
<attr name="textAppearanceMisspelledSuggestion" format="reference" />
160160

161+
<!-- The underline color and thickness for auto correction suggestion -->
162+
<attr name="textAppearanceAutoCorrectionSuggestion" format="reference" />
163+
161164
<!-- The underline color -->
162165
<attr name="textUnderlineColor" format="reference|color" />
163166
<!-- The underline thickness -->

core/res/res/values/styles.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ please see styles_device_defaults.xml.
257257
<item name="android:textUnderlineColor">@color/holo_red_light</item>
258258
</style>
259259

260+
<style name="TextAppearance.AutoCorrectionSuggestion" parent="TextAppearance.Suggestion">
261+
<item name="android:textUnderlineColor">@color/holo_blue_light</item>
262+
</style>
263+
260264
<!-- Widget Styles -->
261265

262266
<style name="Widget">

core/res/res/values/themes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ please see themes_device_defaults.xml.
9191

9292
<item name="textAppearanceEasyCorrectSuggestion">@android:style/TextAppearance.EasyCorrectSuggestion</item>
9393
<item name="textAppearanceMisspelledSuggestion">@android:style/TextAppearance.MisspelledSuggestion</item>
94+
<item name="textAppearanceAutoCorrectionSuggestion">@android:style/TextAppearance.AutoCorrectionSuggestion</item>
9495

9596
<item name="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item>
9697

0 commit comments

Comments
 (0)