am 352b9310: Merge "Use reflection to get field values of SuggestionSpan" into ics-mr1

* commit '352b93104afa73fd53a04c4414bf382fcf477f04':
  Use reflection to get field values of SuggestionSpan
main
satok 2011-10-31 12:26:22 +00:00 committed by Android Git Automerger
commit cbd8ece513
1 changed files with 23 additions and 7 deletions

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.compat; package com.android.inputmethod.compat;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
@ -27,6 +28,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; 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_AFTER = "after";
public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before";
public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; 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; public static final boolean SUGGESTION_SPAN_IS_SUPPORTED;
private static final Class<?> CLASS_SuggestionSpan = CompatUtils private static final Class<?> CLASS_SuggestionSpan = CompatUtils
@ -49,20 +48,36 @@ public class SuggestionSpanUtils {
Context.class, Locale.class, String[].class, int.class, Class.class }; Context.class, Locale.class, String[].class, int.class, Class.class };
private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils
.getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); .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 { static {
SUGGESTION_SPAN_IS_SUPPORTED = SUGGESTION_SPAN_IS_SUPPORTED =
CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; 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( public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
Context context, CharSequence text) { 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; return text;
} }
final Spannable spannable = text instanceof Spannable final Spannable spannable = text instanceof Spannable
? (Spannable) text : new SpannableString(text); ? (Spannable) text : new SpannableString(text);
final Object[] args = final Object[] args =
{ context, null, new String[] {}, FLAG_AUTO_CORRECTION, { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION,
(Class<?>) SuggestionSpanPickedNotificationReceiver.class }; (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
if (ss == null) { if (ss == null) {
@ -78,7 +93,8 @@ public class SuggestionSpanUtils {
CharSequence pickedWord, SuggestedWords suggestedWords) { CharSequence pickedWord, SuggestedWords suggestedWords) {
if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null
|| suggestedWords == null || suggestedWords.size() == 0 || suggestedWords == null || suggestedWords.size() == 0
|| suggestedWords.getInfo(0).isObsoleteSuggestedWord()) { || suggestedWords.getInfo(0).isObsoleteSuggestedWord()
|| OBJ_SUGGESTION_MAX_SIZE == null) {
return pickedWord; return pickedWord;
} }
@ -90,7 +106,7 @@ public class SuggestionSpanUtils {
} }
final ArrayList<String> suggestionsList = new ArrayList<String>(); final ArrayList<String> suggestionsList = new ArrayList<String>();
for (int i = 0; i < suggestedWords.size(); ++i) { for (int i = 0; i < suggestedWords.size(); ++i) {
if (suggestionsList.size() >= SUGGESTION_MAX_SIZE) { if (suggestionsList.size() >= OBJ_SUGGESTION_MAX_SIZE) {
break; break;
} }
final CharSequence word = suggestedWords.getWord(i); final CharSequence word = suggestedWords.getWord(i);