SuggestedWords.java revision b26af7eae7747853c1818b3b97f5d1d94b3105ee
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.text.TextUtils;
20import android.view.inputmethod.CompletionInfo;
21
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.HashSet;
25import java.util.List;
26
27public class SuggestedWords {
28    public static final SuggestedWords EMPTY = new SuggestedWords(false, false, false, false,
29            Collections.<SuggestedWordInfo>emptyList());
30
31    public final boolean mTypedWordValid;
32    public final boolean mHasAutoCorrectionCandidate;
33    public final boolean mIsPunctuationSuggestions;
34    private final List<SuggestedWordInfo> mSuggestedWordInfoList;
35
36    SuggestedWords(boolean typedWordValid,
37            boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
38            boolean shouldBlockAutoCorrectionBySafetyNet,
39            List<SuggestedWordInfo> suggestedWordInfoList) {
40        mTypedWordValid = typedWordValid;
41        mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate
42                && !shouldBlockAutoCorrectionBySafetyNet;
43        mIsPunctuationSuggestions = isPunctuationSuggestions;
44        mSuggestedWordInfoList = suggestedWordInfoList;
45    }
46
47    public int size() {
48        return mSuggestedWordInfoList.size();
49    }
50
51    public CharSequence getWord(int pos) {
52        return mSuggestedWordInfoList.get(pos).mWord;
53    }
54
55    public SuggestedWordInfo getInfo(int pos) {
56        return mSuggestedWordInfoList != null ? mSuggestedWordInfoList.get(pos) : null;
57    }
58
59    public boolean hasAutoCorrectionWord() {
60        return mHasAutoCorrectionCandidate && size() > 1 && !mTypedWordValid;
61    }
62
63    public boolean willAutoCorrect() {
64        return !mTypedWordValid && mHasAutoCorrectionCandidate;
65    }
66
67    @Override
68    public String toString() {
69        // Pretty-print method to help debug
70        return "SuggestedWords:"
71                + " mTypedWordValid=" + mTypedWordValid
72                + " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate
73                + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions;
74    }
75
76    public static class Builder {
77        private boolean mTypedWordValid;
78        private boolean mHasMinimalSuggestion;
79        private boolean mIsPunctuationSuggestions;
80        private boolean mShouldBlockAutoCorrectionBySafetyNet;
81        private boolean mAllowsToBeAutoCorrected;
82        private boolean mHasAutoCorrection;
83        private List<SuggestedWordInfo> mSuggestedWordInfoList =
84                new ArrayList<SuggestedWordInfo>();
85
86        public Builder() {
87            // Nothing to do here.
88        }
89
90        public Builder addWords(List<CharSequence> words,
91                List<SuggestedWordInfo> suggestedWordInfoList) {
92            final int N = words.size();
93            for (int i = 0; i < N; ++i) {
94                final CharSequence word = words.get(i);
95                SuggestedWordInfo suggestedWordInfo = null;
96                if (suggestedWordInfoList != null) {
97                    suggestedWordInfo = suggestedWordInfoList.get(i);
98                }
99                if (suggestedWordInfo == null) {
100                    suggestedWordInfo = new SuggestedWordInfo(word);
101                }
102                addWord(word, suggestedWordInfo);
103            }
104            return this;
105        }
106
107        public Builder addWord(CharSequence word) {
108            return addWord(word, null, false);
109        }
110
111        public Builder addWord(CharSequence word, CharSequence debugString,
112                boolean isPreviousSuggestedWord) {
113            SuggestedWordInfo info = new SuggestedWordInfo(word, debugString,
114                    isPreviousSuggestedWord);
115            return addWord(word, info);
116        }
117
118        /* package for tests */
119        Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
120            if (!TextUtils.isEmpty(suggestedWordInfo.mWord)) {
121                // It's okay if suggestedWordInfo is null since it's checked where it's used.
122                mSuggestedWordInfoList.add(suggestedWordInfo);
123            }
124            return this;
125        }
126
127        public static List<CharSequence> getFromApplicationSpecifiedCompletions(
128                final CompletionInfo[] infos) {
129            final ArrayList<CharSequence> result = new ArrayList<CharSequence>();
130            for (CompletionInfo info : infos) {
131                if (null != info) result.add(info.getText());
132            }
133            return result;
134        }
135
136        public Builder setTypedWordValid(boolean typedWordValid) {
137            mTypedWordValid = typedWordValid;
138            return this;
139        }
140
141        public Builder setHasMinimalSuggestion(boolean hasMinimalSuggestion) {
142            mHasMinimalSuggestion = hasMinimalSuggestion;
143            return this;
144        }
145
146        public Builder setIsPunctuationSuggestions() {
147            mIsPunctuationSuggestions = true;
148            return this;
149        }
150
151        public Builder setShouldBlockAutoCorrectionBySafetyNet() {
152            mShouldBlockAutoCorrectionBySafetyNet = true;
153            return this;
154        }
155
156        public Builder setAllowsToBeAutoCorrected(final boolean allowsToBeAutoCorrected) {
157            mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
158            return this;
159        }
160
161        public Builder setHasAutoCorrection(final boolean hasAutoCorrection) {
162            mHasAutoCorrection = hasAutoCorrection;
163            return this;
164        }
165
166        // Should get rid of the first one (what the user typed previously) from suggestions
167        // and replace it with what the user currently typed.
168        public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
169                SuggestedWords previousSuggestions) {
170            mSuggestedWordInfoList.clear();
171            final HashSet<String> alreadySeen = new HashSet<String>();
172            addWord(typedWord, null, false);
173            alreadySeen.add(typedWord.toString());
174            final int previousSize = previousSuggestions.size();
175            for (int pos = 1; pos < previousSize; pos++) {
176                final String prevWord = previousSuggestions.getWord(pos).toString();
177                // Filter out duplicate suggestion.
178                if (!alreadySeen.contains(prevWord)) {
179                    addWord(prevWord, null, true);
180                    alreadySeen.add(prevWord);
181                }
182            }
183            mTypedWordValid = false;
184            mHasMinimalSuggestion = false;
185            return this;
186        }
187
188        public SuggestedWords build() {
189            return new SuggestedWords(mTypedWordValid, mHasMinimalSuggestion,
190                    mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet,
191                    mSuggestedWordInfoList);
192        }
193
194        public int size() {
195            return mSuggestedWordInfoList.size();
196        }
197
198        public CharSequence getWord(int pos) {
199            return mSuggestedWordInfoList.get(pos).mWord;
200        }
201
202        public boolean allowsToBeAutoCorrected() {
203            return mAllowsToBeAutoCorrected;
204        }
205
206        public boolean hasAutoCorrection() {
207            return mHasAutoCorrection;
208        }
209
210        @Override
211        public String toString() {
212            // Pretty-print method to help debug
213            return "SuggestedWords.Builder:"
214                    + " mTypedWordValid=" + mTypedWordValid
215                    + " mHasMinimalSuggestion=" + mHasMinimalSuggestion
216                    + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
217                    + " mShouldBlockAutoCorrectionBySafetyNet="
218                    + mShouldBlockAutoCorrectionBySafetyNet;
219        }
220    }
221
222    public static class SuggestedWordInfo {
223        private final CharSequence mWord;
224        private final CharSequence mDebugString;
225        private final boolean mPreviousSuggestedWord;
226
227        public SuggestedWordInfo(final CharSequence word) {
228            mWord = word;
229            mDebugString = "";
230            mPreviousSuggestedWord = false;
231        }
232
233        public SuggestedWordInfo(final CharSequence word, final CharSequence debugString,
234                final boolean previousSuggestedWord) {
235            mWord = word;
236            mDebugString = debugString;
237            mPreviousSuggestedWord = previousSuggestedWord;
238        }
239
240        public String getDebugString() {
241            if (mDebugString == null) {
242                return "";
243            } else {
244                return mDebugString.toString();
245            }
246        }
247
248        public boolean isObsoleteSuggestedWord () {
249            return mPreviousSuggestedWord;
250        }
251    }
252}
253