Clean up SuggestionSpanUtils for API level 14+

Change-Id: Iadc235524341b48e7618e9ce05907c786409e004
main
Ken Wakasa 2012-12-17 19:35:09 +09:00
parent 96b22200be
commit fbe83245c9
2 changed files with 21 additions and 67 deletions

View File

@ -21,58 +21,28 @@ import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.text.style.SuggestionSpan;
import com.android.inputmethod.latin.CollectionUtils; import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.LatinImeLogger; 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;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
public final class SuggestionSpanUtils { public final class SuggestionSpanUtils {
private static final String TAG = SuggestionSpanUtils.class.getSimpleName(); private static final String TAG = SuggestionSpanUtils.class.getSimpleName();
// 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 boolean SUGGESTION_SPAN_IS_SUPPORTED;
private static final Class<?> CLASS_SuggestionSpan = CompatUtils // Note that SuggestionSpan.FLAG_AUTO_CORRECTION was added in API level 15.
.getClass("android.text.style.SuggestionSpan");
private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] {
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_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 = public static final Field FIELD_FLAG_AUTO_CORRECTION =
CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION"); CompatUtils.getField(SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
public static final Field FIELD_SUGGESTIONS_MAX_SIZE public static final Integer OBJ_FLAG_AUTO_CORRECTION =
= CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE"); (Integer) CompatUtils.getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);
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);
public static final Integer OBJ_SUGGESTIONS_MAX_SIZE = (Integer) CompatUtils
.getFieldValue(null, null, FIELD_SUGGESTIONS_MAX_SIZE);
static { static {
SUGGESTION_SPAN_IS_SUPPORTED =
CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
if (LatinImeLogger.sDBG) { if (LatinImeLogger.sDBG) {
if (SUGGESTION_SPAN_IS_SUPPORTED if (OBJ_FLAG_AUTO_CORRECTION == 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."); throw new RuntimeException("Field is accidentially null.");
} }
} }
@ -84,21 +54,13 @@ public final class SuggestionSpanUtils {
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
final Context context, final String text) { final Context context, final String text) {
if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
|| OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null
|| OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) {
return text; return text;
} }
final Spannable spannable = new SpannableString(text); final Spannable spannable = new SpannableString(text);
final Object[] args = final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null, new String[] {},
{ context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION, (int)OBJ_FLAG_AUTO_CORRECTION, SuggestionSpanPickedNotificationReceiver.class);
(Class<?>) SuggestionSpanPickedNotificationReceiver.class }; spannable.setSpan(suggestionSpan, 0, text.length(),
final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
if (ss == null) {
Log.w(TAG, "Suggestion span was not created.");
return text;
}
spannable.setSpan(ss, 0, text.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
return spannable; return spannable;
} }
@ -106,18 +68,15 @@ public final class SuggestionSpanUtils {
public static CharSequence getTextWithSuggestionSpan(final Context context, public static CharSequence getTextWithSuggestionSpan(final Context context,
final String pickedWord, final SuggestedWords suggestedWords, final String pickedWord, final SuggestedWords suggestedWords,
final boolean dictionaryAvailable) { final boolean dictionaryAvailable) {
if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord) if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
|| CONSTRUCTOR_SuggestionSpan == null || suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions) {
|| suggestedWords.isEmpty() || suggestedWords.mIsPrediction
|| suggestedWords.mIsPunctuationSuggestions
|| OBJ_SUGGESTIONS_MAX_SIZE == null) {
return pickedWord; return pickedWord;
} }
final Spannable spannable = new SpannableString(pickedWord); final Spannable spannable = new SpannableString(pickedWord);
final ArrayList<String> suggestionsList = CollectionUtils.newArrayList(); final ArrayList<String> suggestionsList = CollectionUtils.newArrayList();
for (int i = 0; i < suggestedWords.size(); ++i) { for (int i = 0; i < suggestedWords.size(); ++i) {
if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) { if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
break; break;
} }
final String word = suggestedWords.getWord(i); final String word = suggestedWords.getWord(i);
@ -128,14 +87,10 @@ public final class SuggestionSpanUtils {
// TODO: We should avoid adding suggestion span candidates that came from the bigram // TODO: We should avoid adding suggestion span candidates that came from the bigram
// prediction. // prediction.
final Object[] args = final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null,
{ context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0, suggestionsList.toArray(new String[suggestionsList.size()]), 0,
(Class<?>) SuggestionSpanPickedNotificationReceiver.class }; SuggestionSpanPickedNotificationReceiver.class);
final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (ss == null) {
return pickedWord;
}
spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable; return spannable;
} }
} }

View File

@ -16,11 +16,10 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.compat.SuggestionSpanUtils;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.style.SuggestionSpan;
import android.util.Log; import android.util.Log;
public final class SuggestionSpanPickedNotificationReceiver extends BroadcastReceiver { public final class SuggestionSpanPickedNotificationReceiver extends BroadcastReceiver {
@ -30,12 +29,12 @@ public final class SuggestionSpanPickedNotificationReceiver extends BroadcastRec
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (SuggestionSpanUtils.ACTION_SUGGESTION_PICKED.equals(intent.getAction())) { if (SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(intent.getAction())) {
if (DBG) { if (DBG) {
final String before = intent.getStringExtra( final String before = intent.getStringExtra(
SuggestionSpanUtils.SUGGESTION_SPAN_PICKED_BEFORE); SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE);
final String after = intent.getStringExtra( final String after = intent.getStringExtra(
SuggestionSpanUtils.SUGGESTION_SPAN_PICKED_AFTER); SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER);
Log.d(TAG, "Received notification picked: " + before + "," + after); Log.d(TAG, "Received notification picked: " + before + "," + after);
} }
} }