Merge "Remove the suggestion underline when the TextView loses focus."
diff --git a/core/java/android/text/style/SuggestionSpan.java b/core/java/android/text/style/SuggestionSpan.java
index 8625257..693a7a9 100644
--- a/core/java/android/text/style/SuggestionSpan.java
+++ b/core/java/android/text/style/SuggestionSpan.java
@@ -76,8 +76,11 @@
     private final String mNotificationTargetClassName;
     private final int mHashCode;
 
-    private float mUnderlineThickness;
-    private int mUnderlineColor;
+    private float mEasyCorrectUnderlineThickness;
+    private int mEasyCorrectUnderlineColor;
+
+    private float mMisspelledUnderlineThickness;
+    private int mMisspelledUnderlineColor;
 
     /*
      * TODO: If switching IME is required, needs to add parameters for ids of InputMethodInfo
@@ -132,25 +135,22 @@
     }
 
     private void initStyle(Context context) {
-        int defStyle = 0;
-        if ((getFlags() & FLAG_MISSPELLED) != 0) {
-            defStyle = com.android.internal.R.attr.textAppearanceMisspelledSuggestion;
-        } else if ((getFlags() & FLAG_EASY_CORRECT) != 0) {
-            defStyle = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion;
-        } else {
-            // No style is applied.
-            mUnderlineThickness = 0;
-            mUnderlineColor = 0;
-            return;
-        }
-
-        TypedArray typedArray = context.obtainStyledAttributes(null,
-                com.android.internal.R.styleable.SuggestionSpan,
-                defStyle, 0);
-
-        mUnderlineThickness = typedArray.getDimension(
+        int defStyle = com.android.internal.R.attr.textAppearanceMisspelledSuggestion;
+        TypedArray typedArray = context.obtainStyledAttributes(
+                null, com.android.internal.R.styleable.SuggestionSpan, defStyle, 0);
+        mMisspelledUnderlineThickness = typedArray.getDimension(
                 com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
-        mUnderlineColor = typedArray.getColor(
+        mMisspelledUnderlineColor = typedArray.getColor(
+                com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
+
+        defStyle = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion;
+
+        typedArray = context.obtainStyledAttributes(
+                null, com.android.internal.R.styleable.SuggestionSpan, defStyle, 0);
+
+        mEasyCorrectUnderlineThickness = typedArray.getDimension(
+                com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
+        mEasyCorrectUnderlineColor = typedArray.getColor(
                 com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
     }
 
@@ -160,8 +160,10 @@
         mLocaleString = src.readString();
         mNotificationTargetClassName = src.readString();
         mHashCode = src.readInt();
-        mUnderlineColor = src.readInt();
-        mUnderlineThickness = src.readFloat();
+        mEasyCorrectUnderlineColor = src.readInt();
+        mEasyCorrectUnderlineThickness = src.readFloat();
+        mMisspelledUnderlineColor = src.readInt();
+        mMisspelledUnderlineThickness = src.readFloat();
     }
 
     /**
@@ -211,8 +213,10 @@
         dest.writeString(mLocaleString);
         dest.writeString(mNotificationTargetClassName);
         dest.writeInt(mHashCode);
-        dest.writeInt(mUnderlineColor);
-        dest.writeFloat(mUnderlineThickness);
+        dest.writeInt(mEasyCorrectUnderlineColor);
+        dest.writeFloat(mEasyCorrectUnderlineThickness);
+        dest.writeInt(mMisspelledUnderlineColor);
+        dest.writeFloat(mMisspelledUnderlineThickness);
     }
 
     @Override
@@ -254,6 +258,10 @@
 
     @Override
     public void updateDrawState(TextPaint tp) {
-        tp.setUnderlineText(mUnderlineColor, mUnderlineThickness);
+        if ((mFlags & FLAG_MISSPELLED) != 0) {
+            tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness);
+        } else if ((mFlags & FLAG_EASY_CORRECT) != 0) {
+            tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness);
+        }
     }
 }
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 5dc2a60..abf1281 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -8121,6 +8121,7 @@
                 Selection.setSelection((Spannable) mText, selStart, selEnd);
             } else {
                 hideControllers();
+                downgradeEasyCorrectionSpans();
             }
 
             // No need to create the controller
@@ -8327,6 +8328,26 @@
         return false;
     }
 
+    /**
+     * Downgrades to simple suggestions all the easy correction spans that are not a spell check
+     * span.
+     */
+    private void downgradeEasyCorrectionSpans() {
+        if (mText instanceof Spannable) {
+            Spannable spannable = (Spannable) mText;
+            SuggestionSpan[] suggestionSpans = spannable.getSpans(0,
+                    spannable.length(), SuggestionSpan.class);
+            for (int i = 0; i < suggestionSpans.length; i++) {
+                int flags = suggestionSpans[i].getFlags();
+                if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
+                        && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
+                    flags = flags & ~SuggestionSpan.FLAG_EASY_CORRECT;
+                    suggestionSpans[i].setFlags(flags);
+                }
+            }
+        }
+    }
+
     @Override
     public boolean onGenericMotionEvent(MotionEvent event) {
         if (mMovement != null && mText instanceof Spannable && mLayout != null) {