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_AUTO_CORRECTION
     52             = CompatUtils.getField(CLASS_SuggestionSpan, "FLAG_AUTO_CORRECTION");
     53     public static final Field FIELD_SUGGESTION_MAX_SIZE
     54             = CompatUtils.getField(CLASS_SuggestionSpan, "SUGGESTIONS_MAX_SIZE");
     55     public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils
     56             .getFieldValue(null, null, FIELD_FLAG_AUTO_CORRECTION);;
     57     public static final Integer OBJ_SUGGESTION_MAX_SIZE = (Integer) CompatUtils
     58             .getFieldValue(null, null, FIELD_SUGGESTION_MAX_SIZE);;
     59 
     60     static {
     61         SUGGESTION_SPAN_IS_SUPPORTED =
     62                 CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
     63         if (LatinImeLogger.sDBG) {
     64             if (SUGGESTION_SPAN_IS_SUPPORTED
     65                     && (OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTION_MAX_SIZE == null)) {
     66                 throw new RuntimeException("Field is accidentially null.");
     67             }
     68         }
     69     }
     70 
     71     public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
     72             Context context, CharSequence text) {
     73         if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null
     74                 || OBJ_FLAG_AUTO_CORRECTION == null || OBJ_SUGGESTION_MAX_SIZE == null) {
     75             return text;
     76         }
     77         final Spannable spannable = text instanceof Spannable
     78                 ? (Spannable) text : new SpannableString(text);
     79         final Object[] args =
     80                 { context, null, new String[] {}, (int)OBJ_FLAG_AUTO_CORRECTION,
     81                         (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
     82         final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
     83         if (ss == null) {
     84             Log.w(TAG, "Suggestion span was not created.");
     85             return text;
     86         }
     87         spannable.setSpan(ss, 0, text.length(),
     88                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
     89         return spannable;
     90     }
     91 
     92     public static CharSequence getTextWithSuggestionSpan(Context context,
     93             CharSequence pickedWord, SuggestedWords suggestedWords) {
     94         if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null
     95                 || suggestedWords == null || suggestedWords.size() == 0
     96                 || suggestedWords.getInfo(0).isObsoleteSuggestedWord()
     97                 || OBJ_SUGGESTION_MAX_SIZE == null) {
     98             return pickedWord;
     99         }
    100 
    101         final Spannable spannable;
    102         if (pickedWord instanceof Spannable) {
    103             spannable = (Spannable) pickedWord;
    104         } else {
    105             spannable = new SpannableString(pickedWord);
    106         }
    107         final ArrayList<String> suggestionsList = new ArrayList<String>();
    108         for (int i = 0; i < suggestedWords.size(); ++i) {
    109             if (suggestionsList.size() >= OBJ_SUGGESTION_MAX_SIZE) {
    110                 break;
    111             }
    112             final CharSequence word = suggestedWords.getWord(i);
    113             if (!TextUtils.equals(pickedWord, word)) {
    114                 suggestionsList.add(word.toString());
    115             }
    116         }
    117 
    118         final Object[] args =
    119                 { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0,
    120                         (Class<?>) SuggestionSpanPickedNotificationReceiver.class };
    121         final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
    122         if (ss == null) {
    123             return pickedWord;
    124         }
    125         spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    126         return spannable;
    127     }
    128 }
    129