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.annotations.UsedForTesting;
27 import com.android.inputmethod.latin.SuggestedWords;
28 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
29 import com.android.inputmethod.latin.common.LocaleUtils;
30 import com.android.inputmethod.latin.define.DebugFlags;
31 
32 import java.lang.reflect.Field;
33 import java.util.ArrayList;
34 import java.util.Locale;
35 
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
38 
39 public final class SuggestionSpanUtils {
40     // Note that SuggestionSpan.FLAG_AUTO_CORRECTION has been introduced
41     // in API level 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1).
42     private static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
43             SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
44     private static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
45             null /* receiver */, null /* defaultValue */, FIELD_FLAG_AUTO_CORRECTION);
46 
47     static {
48         if (DebugFlags.DEBUG_ENABLED) {
49             if (OBJ_FLAG_AUTO_CORRECTION == null) {
50                 throw new RuntimeException("Field is accidentially null.");
51             }
52         }
53     }
54 
SuggestionSpanUtils()55     private SuggestionSpanUtils() {
56         // This utility class is not publicly instantiable.
57     }
58 
59     @UsedForTesting
getTextWithAutoCorrectionIndicatorUnderline( final Context context, final String text, @Nonnull final Locale locale)60     public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
61             final Context context, final String text, @Nonnull final Locale locale) {
62         if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
63             return text;
64         }
65         final Spannable spannable = new SpannableString(text);
66         final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale,
67                 new String[] {} /* suggestions */, OBJ_FLAG_AUTO_CORRECTION, null);
68         spannable.setSpan(suggestionSpan, 0, text.length(),
69                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
70         return spannable;
71     }
72 
73     @UsedForTesting
getTextWithSuggestionSpan(final Context context, final String pickedWord, final SuggestedWords suggestedWords, final Locale locale)74     public static CharSequence getTextWithSuggestionSpan(final Context context,
75             final String pickedWord, final SuggestedWords suggestedWords, final Locale locale) {
76         if (TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
77                 || suggestedWords.isPrediction() || suggestedWords.isPunctuationSuggestions()) {
78             return pickedWord;
79         }
80 
81         final ArrayList<String> suggestionsList = new ArrayList<>();
82         for (int i = 0; i < suggestedWords.size(); ++i) {
83             if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
84                 break;
85             }
86             final SuggestedWordInfo info = suggestedWords.getInfo(i);
87             if (info.isKindOf(SuggestedWordInfo.KIND_PREDICTION)) {
88                 continue;
89             }
90             final String word = suggestedWords.getWord(i);
91             if (!TextUtils.equals(pickedWord, word)) {
92                 suggestionsList.add(word.toString());
93             }
94         }
95         final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale,
96                 suggestionsList.toArray(new String[suggestionsList.size()]), 0 /* flags */, null);
97         final Spannable spannable = new SpannableString(pickedWord);
98         spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
99         return spannable;
100     }
101 
102     /**
103      * Returns first {@link Locale} found in the given array of {@link SuggestionSpan}.
104      * @param suggestionSpans the array of {@link SuggestionSpan} to be examined.
105      * @return the first {@link Locale} found in {@code suggestionSpans}. {@code null} when not
106      * found.
107      */
108     @UsedForTesting
109     @Nullable
findFirstLocaleFromSuggestionSpans( final SuggestionSpan[] suggestionSpans)110     public static Locale findFirstLocaleFromSuggestionSpans(
111             final SuggestionSpan[] suggestionSpans) {
112         for (final SuggestionSpan suggestionSpan : suggestionSpans) {
113             final String localeString = suggestionSpan.getLocale();
114             if (TextUtils.isEmpty(localeString)) {
115                 continue;
116             }
117             return LocaleUtils.constructLocaleFromString(localeString);
118         }
119         return null;
120     }
121 }
122