Put a misspelled flag if the word is not valid by the same logic of Android spell checker.

Bug: 6222722

Currently, the flags of the suggestion span from the Latin IME is different from the flags from Android spell checker.

Change-Id: I2f7a54ae0d63235a0b94e039109ab8b2f1311055
diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
index e82d914..a9e4840 100644
--- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
+++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
@@ -48,21 +48,30 @@
             Context.class, Locale.class, String[].class, int.class, Class.class };
     private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils
             .getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan);
-    public static final Field FIELD_FLAG_AUTO_CORRECTION
-            = CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION");
+    public static final Field FIELD_FLAG_EASY_CORRECT =
+            CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_EASY_CORRECT");
+    public static final Field FIELD_FLAG_MISSPELLED =
+            CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_MISSPELLED");
+    public static final Field FIELD_FLAG_AUTO_CORRECTION =
+            CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION");
     public static final Field FIELD_SUGGESTIONS_MAX_SIZE
             = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE");
+    public static final Integer OBJ_FLAG_EASY_CORRECT = (Integer) CompatUtils
+            .getFieldValue(null, null, FIELD_FLAG_EASY_CORRECT);
+    public static final Integer OBJ_FLAG_MISSPELLED = (Integer) CompatUtils
+            .getFieldValue(null, null, FIELD_FLAG_MISSPELLED);
     public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils
-            .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);;
+            .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);
     public static final Integer OBJ_SUGGESTIONS_MAX_SIZE = (Integer) CompatUtils
-            .getFieldValue(null, null, FIELD_SUGGESTIONS_MAX_SIZE);;
+            .getFieldValue(null, null, FIELD_SUGGESTIONS_MAX_SIZE);
 
     static {
         SUGGESTION_SPAN_IS_SUPPORTED =
                 CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
         if (LatinImeLogger.sDBG) {
             if (SUGGESTION_SPAN_IS_SUPPORTED
-                    && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null)) {
+                    && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null
+                            || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null)) {
                 throw new RuntimeException("Field is accidentially null.");
             }
         }
@@ -71,7 +80,8 @@
     public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
             Context context, CharSequence text) {
         if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null
-                || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null) {
+                || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null
+                || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) {
             return text;
         }
         final Spannable spannable = text instanceof Spannable
@@ -104,6 +114,7 @@
             spannable = new SpannableString(pickedWord);
         }
         final ArrayList<String> suggestionsList = new ArrayList<String>();
+        boolean sameAsTyped = false;
         for (int i = 0; i < suggestedWords.size(); ++i) {
             if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) {
                 break;
@@ -111,11 +122,18 @@
             final CharSequence word = suggestedWords.getWord(i);
             if (!TextUtils.equals(pickedWord, word)) {
                 suggestionsList.add(word.toString());
+            } else if (i == 0) {
+                sameAsTyped = true;
             }
         }
+        // TODO: Share the implementation for checking typed word validity between the IME
+        // and the spell checker.
+        final int flag = (sameAsTyped && !suggestedWords.mTypedWordValid)
+                ? ((int)OBJ_FLAG_EASY_CORRECT | (int)OBJ_FLAG_MISSPELLED)
+                : 0;
 
         final Object[] args =
-                { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0,
+                { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), flag,
                         (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
         final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
         if (ss == null) {
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index be64c2f..0485c88 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -31,9 +31,7 @@
 import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.util.Log;
-import android.view.MotionEvent;
 
-import com.android.inputmethod.keyboard.Keyboard;
 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
 
 import java.io.BufferedReader;
@@ -112,7 +110,6 @@
         /* package */ static final int BUFSIZE = 20;
         private InputMethodService mContext;
         private boolean mEnabled = false;
-        private boolean mUsabilityStudy = false;
         private int mEnd = 0;
         /* package */ int mLength = 0;
         private char[] mCharBuf = new char[BUFSIZE];
@@ -129,7 +126,6 @@
                 boolean usabilityStudy) {
             sRingCharBuffer.mContext = context;
             sRingCharBuffer.mEnabled = enabled || usabilityStudy;
-            sRingCharBuffer.mUsabilityStudy = usabilityStudy;
             UsabilityStudyLogUtils.getInstance().init(context);
             return sRingCharBuffer;
         }