SuggestionSpanUtils.java revision c61cd79229b1871d0f603a23389695d7f7751e66
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 public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( 81 Context context, CharSequence text) { 82 if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null 83 || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTIONS_MAX_SIZE == null 84 || OBJ_FLAG_MISSPELLED == null || OBJ_FLAG_EASY_CORRECT == null) { 85 return text; 86 } 87 final Spannable spannable = text instanceof Spannable 88 ? (Spannable) text : new SpannableString(text); 89 final Object[] args = 90 { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION, 91 (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; 92 final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); 93 if (ss == null) { 94 Log.w(TAG, "Suggestion span was not created."); 95 return text; 96 } 97 spannable.setSpan(ss, 0, text.length(), 98 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 99 return spannable; 100 } 101 102 public static CharSequence getTextWithSuggestionSpan(Context context, 103 CharSequence pickedWord, SuggestedWords suggestedWords) { 104 if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null 105 || suggestedWords == null || suggestedWords.size() == 0 106 || OBJ_SUGGESTIONS_MAX_SIZE == null) { 107 return pickedWord; 108 } 109 110 final Spannable spannable; 111 if (pickedWord instanceof Spannable) { 112 spannable = (Spannable) pickedWord; 113 } else { 114 spannable = new SpannableString(pickedWord); 115 } 116 final ArrayList<String> suggestionsList = new ArrayList<String>(); 117 boolean sameAsTyped = false; 118 for (int i = 0; i < suggestedWords.size(); ++i) { 119 if (suggestionsList.size() >= OBJ_SUGGESTIONS_MAX_SIZE) { 120 break; 121 } 122 final CharSequence word = suggestedWords.getWord(i); 123 if (!TextUtils.equals(pickedWord, word)) { 124 suggestionsList.add(word.toString()); 125 } else if (i == 0) { 126 sameAsTyped = true; 127 } 128 } 129 // TODO: Share the implementation for checking typed word validity between the IME 130 // and the spell checker. 131 final int flag = (sameAsTyped && !suggestedWords.mTypedWordValid) 132 ? (OBJ_FLAG_EASY_CORRECT | OBJ_FLAG_MISSPELLED) 133 : 0; 134 135 final Object[] args = 136 { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), flag, 137 (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; 138 final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); 139 if (ss == null) { 140 return pickedWord; 141 } 142 spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 143 return spannable; 144 } 145} 146