diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java index 2766cc3f1..164d2b748 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -16,6 +16,7 @@ package com.android.inputmethod.compat; +import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; @@ -27,6 +28,7 @@ import android.text.TextUtils; import android.util.Log; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Locale; @@ -38,9 +40,6 @@ public class SuggestionSpanUtils { public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; - // TODO: Use the API constant after it gets public. - public static final int FLAG_AUTO_CORRECTION = 0x0004; - public static final int SUGGESTION_MAX_SIZE = 5; public static final boolean SUGGESTION_SPAN_IS_SUPPORTED; private static final Class CLASS_SuggestionSpan = CompatUtils @@ -49,20 +48,36 @@ public class SuggestionSpanUtils { 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_SUGGESTION_MAX_SIZE + = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTION_MAX_SIZE"); + public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);; + public static final Integer OBJ_SUGGESTION_MAX_SIZE = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_SUGGESTION_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_SUGGESTION_MAX_SIZE == null)) { + Log.e(TAG, "Field is accidentially null."); + } + } } public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( Context context, CharSequence text) { - if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null) { + if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null + || OBJ_FLAG_AUTO_CORRECTION == null) { return text; } final Spannable spannable = text instanceof Spannable ? (Spannable) text : new SpannableString(text); final Object[] args = - { context, null, new String[] {}, FLAG_AUTO_CORRECTION, + { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION, (Class) SuggestionSpanPickedNotificationReceiver.class }; final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); if (ss == null) { @@ -78,7 +93,8 @@ public class SuggestionSpanUtils { CharSequence pickedWord, SuggestedWords suggestedWords) { if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null || suggestedWords == null || suggestedWords.size() == 0 - || suggestedWords.getInfo(0).isObsoleteSuggestedWord()) { + || suggestedWords.getInfo(0).isObsoleteSuggestedWord() + || OBJ_SUGGESTION_MAX_SIZE == null) { return pickedWord; } @@ -90,7 +106,7 @@ public class SuggestionSpanUtils { } final ArrayList suggestionsList = new ArrayList(); for (int i = 0; i < suggestedWords.size(); ++i) { - if (suggestionsList.size() >= SUGGESTION_MAX_SIZE) { + if (suggestionsList.size() >= OBJ_SUGGESTION_MAX_SIZE) { break; } final CharSequence word = suggestedWords.getWord(i);