aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorKen Wakasa <kwakasa@google.com>2012-12-17 19:35:09 +0900
committerKen Wakasa <kwakasa@google.com>2012-12-17 19:41:44 +0900
commitfbe83245c9392aadbda6f47b514b047094e2182f (patch)
tree20dde769c6d73ceeb0dbb82bae8e6ec9d260e38e /java/src
parent96b22200beb98fd1a6288f4cf53e38611a09cdd0 (diff)
downloadlatinime-fbe83245c9392aadbda6f47b514b047094e2182f.tar.gz
latinime-fbe83245c9392aadbda6f47b514b047094e2182f.tar.xz
latinime-fbe83245c9392aadbda6f47b514b047094e2182f.zip
Clean up SuggestionSpanUtils for API level 14+
Change-Id: Iadc235524341b48e7618e9ce05907c786409e004
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java79
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java9
2 files changed, 21 insertions, 67 deletions
diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
index c1609ea28..0e3634d52 100644
--- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
+++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
@@ -21,58 +21,28 @@ import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
-import android.util.Log;
+import android.text.style.SuggestionSpan;
import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
-import java.util.Locale;
public final class SuggestionSpanUtils {
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
- .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");
+ // Note that SuggestionSpan.FLAG_AUTO_CORRECTION was added in API level 15.
public static final Field FIELD_FLAG_AUTO_CORRECTION =
- CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION");
- public static final Field FIELD_SUGGESTIONS_MAX_SIZE
- = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE");
- 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);
+ CompatUtils.getField(SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
+ public static final Integer OBJ_FLAG_AUTO_CORRECTION =
+ (Integer) CompatUtils.getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);
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_SUGGESTIONS_MAX_SIZE == null
- || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null)) {
+ if (OBJ_FLAG_AUTO_CORRECTION == null) {
throw new RuntimeException("Field is accidentially null.");
}
}
@@ -84,21 +54,13 @@ public final class SuggestionSpanUtils {
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
final Context context, final String text) {
- if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null
- || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null
- || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) {
+ if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
return text;
}
final Spannable spannable = new SpannableString(text);
- final Object[] args =
- { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION,
- (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
- 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(),
+ final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null, new String[] {},
+ (int)OBJ_FLAG_AUTO_CORRECTION, SuggestionSpanPickedNotificationReceiver.class);
+ spannable.setSpan(suggestionSpan, 0, text.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
return spannable;
}
@@ -106,18 +68,15 @@ public final class SuggestionSpanUtils {
public static CharSequence getTextWithSuggestionSpan(final Context context,
final String pickedWord, final SuggestedWords suggestedWords,
final boolean dictionaryAvailable) {
- if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord)
- || CONSTRUCTOR_SuggestionSpan == null
- || suggestedWords.isEmpty() || suggestedWords.mIsPrediction
- || suggestedWords.mIsPunctuationSuggestions
- || OBJ_SUGGESTIONS_MAX_SIZE == null) {
+ if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
+ || suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions) {
return pickedWord;
}
final Spannable spannable = new SpannableString(pickedWord);
final ArrayList<String> suggestionsList = CollectionUtils.newArrayList();
for (int i = 0; i < suggestedWords.size(); ++i) {
- if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) {
+ if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
break;
}
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
// prediction.
- final Object[] args =
- { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0,
- (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
- final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
- if (ss == null) {
- return pickedWord;
- }
- spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+ final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null,
+ suggestionsList.toArray(new String[suggestionsList.size()]), 0,
+ SuggestionSpanPickedNotificationReceiver.class);
+ spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}
}
diff --git a/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java b/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java
index d188fc5ef..ed6fc03f9 100644
--- a/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java
+++ b/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java
@@ -16,11 +16,10 @@
package com.android.inputmethod.latin;
-import com.android.inputmethod.compat.SuggestionSpanUtils;
-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
+import android.text.style.SuggestionSpan;
import android.util.Log;
public final class SuggestionSpanPickedNotificationReceiver extends BroadcastReceiver {
@@ -30,12 +29,12 @@ public final class SuggestionSpanPickedNotificationReceiver extends BroadcastRec
@Override
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) {
final String before = intent.getStringExtra(
- SuggestionSpanUtils.SUGGESTION_SPAN_PICKED_BEFORE);
+ SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE);
final String after = intent.getStringExtra(
- SuggestionSpanUtils.SUGGESTION_SPAN_PICKED_AFTER);
+ SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER);
Log.d(TAG, "Received notification picked: " + before + "," + after);
}
}