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