Merge "Refactor of SuggestionSpanUtils"

This commit is contained in:
satok 2011-06-17 03:29:08 -07:00 committed by Android (Google) Code Review
commit 144ba95813

View file

@ -30,12 +30,14 @@ import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
public class SuggestionSpanUtils { public class SuggestionSpanUtils {
// TODO: Use reflection to get field values
public static final String ACTION_SUGGESTION_PICKED = public static final String ACTION_SUGGESTION_PICKED =
"android.text.style.SUGGESTION_PICKED"; "android.text.style.SUGGESTION_PICKED";
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";
public static final int SUGGESTION_MAX_SIZE = 5; public static final int SUGGESTION_MAX_SIZE = 5;
public static final boolean SUGGESTION_SPAN_IS_SUPPORTED;
private static final Class<?> CLASS_SuggestionSpan = CompatUtils private static final Class<?> CLASS_SuggestionSpan = CompatUtils
.getClass("android.text.style.SuggestionSpan"); .getClass("android.text.style.SuggestionSpan");
@ -43,24 +45,23 @@ 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 boolean SUGGESTION_SPAN_IS_SUPPORTED;
static { static {
SUGGESTION_SPAN_IS_SUPPORTED = SUGGESTION_SPAN_IS_SUPPORTED =
CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
} }
public static CharSequence getTextWithSuggestionSpan(Context context, public static CharSequence getTextWithSuggestionSpan(Context context,
CharSequence suggestion, SuggestedWords suggestedWords) { CharSequence pickedWord, SuggestedWords suggestedWords) {
if (TextUtils.isEmpty(suggestion) || CONSTRUCTOR_SuggestionSpan == null if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null
|| suggestedWords == null || suggestedWords.size() == 0) { || suggestedWords == null || suggestedWords.size() == 0) {
return suggestion; return pickedWord;
} }
final Spannable spannable; final Spannable spannable;
if (suggestion instanceof Spannable) { if (pickedWord instanceof Spannable) {
spannable = (Spannable) suggestion; spannable = (Spannable) pickedWord;
} else { } else {
spannable = new SpannableString(suggestion); spannable = new SpannableString(pickedWord);
} }
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) {
@ -68,7 +69,7 @@ public class SuggestionSpanUtils {
break; break;
} }
final CharSequence word = suggestedWords.getWord(i); final CharSequence word = suggestedWords.getWord(i);
if (!TextUtils.equals(suggestion, word)) { if (!TextUtils.equals(pickedWord, word)) {
suggestionsList.add(word.toString()); suggestionsList.add(word.toString());
} }
} }
@ -78,9 +79,9 @@ public class SuggestionSpanUtils {
(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) {
return suggestion; return pickedWord;
} }
spannable.setSpan(ss, 0, suggestion.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable; return spannable;
} }
} }