SuggestionSpanUtils.java revision 8380f921f7edaeea2033a1e967a14941400fe246
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
17package com.android.inputmethod.compat;
18
19import android.content.Context;
20import android.text.Spannable;
21import android.text.SpannableString;
22import android.text.Spanned;
23import android.text.TextUtils;
24import android.text.style.SuggestionSpan;
25
26import com.android.inputmethod.latin.SuggestedWords;
27import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
28import com.android.inputmethod.latin.define.DebugFlags;
29import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
30
31import java.lang.reflect.Field;
32import java.util.ArrayList;
33
34public 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    private static final Field FIELD_FLAG_AUTO_CORRECTION = CompatUtils.getField(
38            SuggestionSpan.class, "FLAG_AUTO_CORRECTION");
39    private static final Integer OBJ_FLAG_AUTO_CORRECTION = (Integer) CompatUtils.getFieldValue(
40            null /* receiver */, null /* defaultValue */, FIELD_FLAG_AUTO_CORRECTION);
41
42    static {
43        if (DebugFlags.DEBUG_ENABLED) {
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        if (TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
71                || suggestedWords.isPrediction() || suggestedWords.isPunctuationSuggestions()) {
72            return pickedWord;
73        }
74
75        final ArrayList<String> suggestionsList = new ArrayList<>();
76        for (int i = 0; i < suggestedWords.size(); ++i) {
77            if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
78                break;
79            }
80            final SuggestedWordInfo info = suggestedWords.getInfo(i);
81            if (info.isKindOf(SuggestedWordInfo.KIND_PREDICTION)) {
82                continue;
83            }
84            final String word = suggestedWords.getWord(i);
85            if (!TextUtils.equals(pickedWord, word)) {
86                suggestionsList.add(word.toString());
87            }
88        }
89        final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */,
90                suggestionsList.toArray(new String[suggestionsList.size()]), 0 /* flags */,
91                SuggestionSpanPickedNotificationReceiver.class);
92        final Spannable spannable = new SpannableString(pickedWord);
93        spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
94        return spannable;
95    }
96}
97