1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.compat; 18 19 import android.content.Context; 20 import android.text.Spannable; 21 import android.text.SpannableString; 22 import android.text.Spanned; 23 import android.text.TextUtils; 24 import android.util.Log; 25 26 import com.android.inputmethod.latin.CollectionUtils; 27 import com.android.inputmethod.latin.LatinImeLogger; 28 import com.android.inputmethod.latin.SuggestedWords; 29 import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; 30 31 import java.lang.reflect.Constructor; 32 import java.lang.reflect.Field; 33 import java.util.ArrayList; 34 import java.util.Locale; 35 36 public final class SuggestionSpanUtils { 37 private static final String TAG = SuggestionSpanUtils.class.getSimpleName(); 38 // TODO: Use reflection to get field values 39 public static final String ACTION_SUGGESTION_PICKED = 40 "android.text.style.SUGGESTION_PICKED"; 41 public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; 42 public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; 43 public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; 44 public static final boolean SUGGESTION_SPAN_IS_SUPPORTED; 45 46 private static final Class<?> CLASS_SuggestionSpan = CompatUtils 47 .getClass("android.text.style.SuggestionSpan"); 48 private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] { 49 Context.class, Locale.class, String[].class, int.class, Class.class }; 50 private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils 51 .getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); 52 public static final Field FIELD_FLAG_EASY_CORRECT = 53 CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_EASY_CORRECT"); 54 public static final Field FIELD_FLAG_MISSPELLED = 55 CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_MISSPELLED"); 56 public static final Field FIELD_FLAG_AUTO_CORRECTION = 57 CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION"); 58 public static final Field FIELD_SUGGESTIONS_MAX_SIZE 59 = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE"); 60 public static final Integer OBJ_FLAG_EASY_CORRECT = (Integer) CompatUtils 61 .getFieldValue(null, null, FIELD_FLAG_EASY_CORRECT); 62 public static final Integer OBJ_FLAG_MISSPELLED = (Integer) CompatUtils 63 .getFieldValue(null, null, FIELD_FLAG_MISSPELLED); 64 public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils 65 .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION); 66 public static final Integer OBJ_SUGGESTIONS_MAX_SIZE = (Integer) CompatUtils 67 .getFieldValue(null, null, FIELD_SUGGESTIONS_MAX_SIZE); 68 69 static { 70 SUGGESTION_SPAN_IS_SUPPORTED = 71 CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; 72 if (LatinImeLogger.sDBG) { 73 if (SUGGESTION_SPAN_IS_SUPPORTED 74 && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null 75 || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null)) { 76 throw new RuntimeException("Field is accidentially null."); 77 } 78 } 79 } 80 81 private SuggestionSpanUtils() { 82 // This utility class is not publicly instantiable. 83 } 84 85 public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( 86 Context context, CharSequence text) { 87 if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null 88 || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null 89 || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) { 90 return text; 91 } 92 final Spannable spannable = text instanceof Spannable 93 ? (Spannable) text : new SpannableString(text); 94 final Object[] args = 95 { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION, 96 (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; 97 final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); 98 if (ss == null) { 99 Log.w(TAG, "Suggestion span was not created."); 100 return text; 101 } 102 spannable.setSpan(ss, 0, text.length(), 103 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 104 return spannable; 105 } 106 107 public static CharSequence getTextWithSuggestionSpan(Context context, 108 CharSequence pickedWord, SuggestedWords suggestedWords, boolean dictionaryAvailable) { 109 if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord) 110 || CONSTRUCTOR_SuggestionSpan == null 111 || suggestedWords == null || suggestedWords.size() == 0 112 || suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions 113 || OBJ_SUGGESTIONS_MAX_SIZE == null) { 114 return pickedWord; 115 } 116 117 final Spannable spannable; 118 if (pickedWord instanceof Spannable) { 119 spannable = (Spannable) pickedWord; 120 } else { 121 spannable = new SpannableString(pickedWord); 122 } 123 final ArrayList<String> suggestionsList = CollectionUtils.newArrayList(); 124 for (int i = 0; i < suggestedWords.size(); ++i) { 125 if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) { 126 break; 127 } 128 final CharSequence word = suggestedWords.getWord(i); 129 if (!TextUtils.equals(pickedWord, word)) { 130 suggestionsList.add(word.toString()); 131 } 132 } 133 134 // TODO: We should avoid adding suggestion span candidates that came from the bigram 135 // prediction. 136 final Object[] args = 137 { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0, 138 (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; 139 final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); 140 if (ss == null) { 141 return pickedWord; 142 } 143 spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 144 return spannable; 145 } 146 } 147