SuggestionSpanUtils.java revision fde7efd87710dcc9e8376e3ef6db287e254c65fc
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 17package com.android.inputmethod.compat; 18 19import com.android.inputmethod.latin.LatinImeLogger; 20import com.android.inputmethod.latin.SuggestedWords; 21import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; 22 23import android.content.Context; 24import android.text.Spannable; 25import android.text.SpannableString; 26import android.text.Spanned; 27import android.text.TextUtils; 28import android.util.Log; 29 30import java.lang.reflect.Constructor; 31import java.lang.reflect.Field; 32import java.util.ArrayList; 33import java.util.Locale; 34 35public class SuggestionSpanUtils { 36 private static final String TAG = SuggestionSpanUtils.class.getSimpleName(); 37 // TODO: Use reflection to get field values 38 public static final String ACTION_SUGGESTION_PICKED = 39 "android.text.style.SUGGESTION_PICKED"; 40 public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; 41 public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; 42 public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; 43 public static final boolean SUGGESTION_SPAN_IS_SUPPORTED; 44 45 private static final Class<?> CLASS_SuggestionSpan = CompatUtils 46 .getClass("android.text.style.SuggestionSpan"); 47 private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] { 48 Context.class, Locale.class, String[].class, int.class, Class.class }; 49 private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils 50 .getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); 51 public static final Field FIELD_FLAG_EASY_CORRECT = 52 CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_EASY_CORRECT"); 53 public static final Field FIELD_FLAG_MISSPELLED = 54 CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_MISSPELLED"); 55 public static final Field FIELD_FLAG_AUTO_CORRECTION = 56 CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION"); 57 public static final Field FIELD_SUGGESTIONS_MAX_SIZE 58 = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE"); 59 public static final Integer OBJ_FLAG_EASY_CORRECT = (Integer) CompatUtils 60 .getFieldValue(null, null, FIELD_FLAG_EASY_CORRECT); 61 public static final Integer OBJ_FLAG_MISSPELLED = (Integer) CompatUtils 62 .getFieldValue(null, null, FIELD_FLAG_MISSPELLED); 63 public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils 64 .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION); 65 public static final Integer OBJ_SUGGESTIONS_MAX_SIZE = (Integer) CompatUtils 66 .getFieldValue(null, null, FIELD_SUGGESTIONS_MAX_SIZE); 67 68 static { 69 SUGGESTION_SPAN_IS_SUPPORTED = 70 CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; 71 if (LatinImeLogger.sDBG) { 72 if (SUGGESTION_SPAN_IS_SUPPORTED 73 && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null 74 || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null)) { 75 throw new RuntimeException("Field is accidentially null."); 76 } 77 } 78 } 79 80 private SuggestionSpanUtils() { 81 // This utility class is not publicly instantiable. 82 } 83 84 public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( 85 Context context, CharSequence text) { 86 if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null 87 || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null 88 || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) { 89 return text; 90 } 91 final Spannable spannable = text instanceof Spannable 92 ? (Spannable) text : new SpannableString(text); 93 final Object[] args = 94 { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION, 95 (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; 96 final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); 97 if (ss == null) { 98 Log.w(TAG, "Suggestion span was not created."); 99 return text; 100 } 101 spannable.setSpan(ss, 0, text.length(), 102 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 103 return spannable; 104 } 105 106 public static CharSequence getTextWithSuggestionSpan(Context context, 107 CharSequence pickedWord, SuggestedWords suggestedWords) { 108 if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null 109 || suggestedWords == null || suggestedWords.size() == 0 110 || OBJ_SUGGESTIONS_MAX_SIZE == null) { 111 return pickedWord; 112 } 113 114 final Spannable spannable; 115 if (pickedWord instanceof Spannable) { 116 spannable = (Spannable) pickedWord; 117 } else { 118 spannable = new SpannableString(pickedWord); 119 } 120 final ArrayList<String> suggestionsList = new ArrayList<String>(); 121 boolean sameAsTyped = false; 122 for (int i = 0; i < suggestedWords.size(); ++i) { 123 if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) { 124 break; 125 } 126 final CharSequence word = suggestedWords.getWord(i); 127 if (!TextUtils.equals(pickedWord, word)) { 128 suggestionsList.add(word.toString()); 129 } else if (i == 0) { 130 sameAsTyped = true; 131 } 132 } 133 // TODO: Share the implementation for checking typed word validity between the IME 134 // and the spell checker. 135 final int flag = (sameAsTyped && !suggestedWords.mTypedWordValid) 136 ? (OBJ_FLAG_EASY_CORRECT | OBJ_FLAG_MISSPELLED) 137 : 0; 138 139 final Object[] args = 140 { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), flag, 141 (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; 142 final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); 143 if (ss == null) { 144 return pickedWord; 145 } 146 spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 147 return spannable; 148 } 149} 150