Home | History | Annotate | Download | only in compat
      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 com.android.inputmethod.latin.LatinImeLogger;
     20 import com.android.inputmethod.latin.SuggestedWords;
     21 import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
     22 
     23 import android.content.Context;
     24 import android.text.Spannable;
     25 import android.text.SpannableString;
     26 import android.text.Spanned;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 
     30 import java.lang.reflect.Constructor;
     31 import java.lang.reflect.Field;
     32 import java.util.ArrayList;
     33 import java.util.Locale;
     34 
     35 public 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, boolean dictionaryAvailable) {
    108         if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord)
    109                 || CONSTRUCTOR_SuggestionSpan == null
    110                 || suggestedWords == null || suggestedWords.size() == 0
    111                 || suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions
    112                 || OBJ_SUGGESTIONS_MAX_SIZE == null) {
    113             return pickedWord;
    114         }
    115 
    116         final Spannable spannable;
    117         if (pickedWord instanceof Spannable) {
    118             spannable = (Spannable) pickedWord;
    119         } else {
    120             spannable = new SpannableString(pickedWord);
    121         }
    122         final ArrayList<String> suggestionsList = new ArrayList<String>();
    123         boolean sameAsTyped = false;
    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             } else if (i == 0) {
    132                 sameAsTyped = true;
    133             }
    134         }
    135 
    136         // TODO: We should avoid adding suggestion span candidates that came from the bigram
    137         // prediction.
    138         final Object[] args =
    139                 { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0,
    140                         (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
    141         final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
    142         if (ss == null) {
    143             return pickedWord;
    144         }
    145         spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    146         return spannable;
    147     }
    148 }
    149