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 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.text.style.SuggestionSpan;
     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.Field;
     32 import java.util.ArrayList;
     33 
     34 public final class SuggestionSpanUtils {
     35     private static final String TAG = SuggestionSpanUtils.class.getSimpleName();
     36 
     37     // Note that SuggestionSpan.FLAG_AUTO_CORRECTION has been introduced
     38     // in API level 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1).
     39     public static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
     40             SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
     41     public static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
     42             null /* receiver */, null /* defaultValue */, FIELD_FLAG_AUTO_CORRECTION);
     43 
     44     static {
     45         if (LatinImeLogger.sDBG) {
     46             if (OBJ_FLAG_AUTO_CORRECTION == null) {
     47                 throw new RuntimeException("Field is accidentially null.");
     48             }
     49         }
     50     }
     51 
     52     private SuggestionSpanUtils() {
     53         // This utility class is not publicly instantiable.
     54     }
     55 
     56     public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
     57             final Context context, final String text) {
     58         if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
     59             return text;
     60         }
     61         final Spannable spannable = new SpannableString(text);
     62         final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */,
     63                 new String[] {} /* suggestions */, (int)OBJ_FLAG_AUTO_CORRECTION,
     64                 SuggestionSpanPickedNotificationReceiver.class);
     65         spannable.setSpan(suggestionSpan, 0, text.length(),
     66                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
     67         return spannable;
     68     }
     69 
     70     public static CharSequence getTextWithSuggestionSpan(final Context context,
     71             final String pickedWord, final SuggestedWords suggestedWords,
     72             final boolean dictionaryAvailable) {
     73         if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
     74                 || suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions) {
     75             return pickedWord;
     76         }
     77 
     78         final Spannable spannable = new SpannableString(pickedWord);
     79         final ArrayList<String> suggestionsList = CollectionUtils.newArrayList();
     80         for (int i = 0; i < suggestedWords.size(); ++i) {
     81             if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
     82                 break;
     83             }
     84             final String word = suggestedWords.getWord(i);
     85             if (!TextUtils.equals(pickedWord, word)) {
     86                 suggestionsList.add(word.toString());
     87             }
     88         }
     89 
     90         // TODO: We should avoid adding suggestion span candidates that came from the bigram
     91         // prediction.
     92         final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */,
     93                 suggestionsList.toArray(new String[suggestionsList.size()]), 0 /* flags */,
     94                 SuggestionSpanPickedNotificationReceiver.class);
     95         spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
     96         return spannable;
     97     }
     98 }
     99