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