SuggestedWords.java revision add3e053797d7d2355c61160ab746f7dfeb92aef
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(null, false, false, false, null);
29
30    public final List<CharSequence> mWords;
31    public final boolean mTypedWordValid;
32    public final boolean mHasMinimalSuggestion;
33    public final boolean mIsPunctuationSuggestions;
34    private final List<SuggestedWordInfo> mSuggestedWordInfoList;
35
36    private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
37            boolean hasMinimalSuggestion, boolean isPunctuationSuggestions,
38            List<SuggestedWordInfo> suggestedWordInfoList) {
39        if (words != null) {
40            mWords = words;
41        } else {
42            mWords = Collections.emptyList();
43        }
44        mTypedWordValid = typedWordValid;
45        mHasMinimalSuggestion = hasMinimalSuggestion;
46        mIsPunctuationSuggestions = isPunctuationSuggestions;
47        mSuggestedWordInfoList = suggestedWordInfoList;
48    }
49
50    public int size() {
51        return mWords.size();
52    }
53
54    public CharSequence getWord(int pos) {
55        return mWords.get(pos);
56    }
57
58    public SuggestedWordInfo getInfo(int pos) {
59        return mSuggestedWordInfoList != null ? mSuggestedWordInfoList.get(pos) : null;
60    }
61
62    public boolean hasAutoCorrectionWord() {
63        return mHasMinimalSuggestion && size() > 1 && !mTypedWordValid;
64    }
65
66    public boolean hasWordAboveAutoCorrectionScoreThreshold() {
67        return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid);
68    }
69
70    public boolean isPunctuationSuggestions() {
71        return mIsPunctuationSuggestions;
72    }
73
74    public static class Builder {
75        private List<CharSequence> mWords = new ArrayList<CharSequence>();
76        private boolean mTypedWordValid;
77        private boolean mHasMinimalSuggestion;
78        private boolean mIsPunctuationSuggestions;
79        private List<SuggestedWordInfo> mSuggestedWordInfoList =
80                new ArrayList<SuggestedWordInfo>();
81
82        public Builder() {
83            // Nothing to do here.
84        }
85
86        public Builder addWords(List<CharSequence> words,
87                List<SuggestedWordInfo> suggestedWordInfoList) {
88            final int N = words.size();
89            for (int i = 0; i < N; ++i) {
90                SuggestedWordInfo suggestedWordInfo = null;
91                if (suggestedWordInfoList != null) {
92                    suggestedWordInfo = suggestedWordInfoList.get(i);
93                }
94                if (suggestedWordInfo == null) {
95                    suggestedWordInfo = new SuggestedWordInfo();
96                }
97                addWord(words.get(i), suggestedWordInfo);
98            }
99            return this;
100        }
101
102        public Builder addWord(CharSequence word) {
103            return addWord(word, null, false);
104        }
105
106        public Builder addWord(CharSequence word, CharSequence debugString,
107                boolean isPreviousSuggestedWord) {
108            SuggestedWordInfo info = new SuggestedWordInfo(debugString, isPreviousSuggestedWord);
109            return addWord(word, info);
110        }
111
112        private Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
113            if (!TextUtils.isEmpty(word)) {
114                mWords.add(word);
115                // It's okay if suggestedWordInfo is null since it's checked where it's used.
116                mSuggestedWordInfoList.add(suggestedWordInfo);
117            }
118            return this;
119        }
120
121        public Builder setApplicationSpecifiedCompletions(CompletionInfo[] infos) {
122            for (CompletionInfo info : infos) {
123                if (null != info) addWord(info.getText());
124            }
125            return this;
126        }
127
128        public Builder setTypedWordValid(boolean typedWordValid) {
129            mTypedWordValid = typedWordValid;
130            return this;
131        }
132
133        public Builder setHasMinimalSuggestion(boolean hasMinimalSuggestion) {
134            mHasMinimalSuggestion = hasMinimalSuggestion;
135            return this;
136        }
137
138        public Builder setIsPunctuationSuggestions() {
139            mIsPunctuationSuggestions = true;
140            return this;
141        }
142
143        // Should get rid of the first one (what the user typed previously) from suggestions
144        // and replace it with what the user currently typed.
145        public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
146                SuggestedWords previousSuggestions) {
147            mWords.clear();
148            mSuggestedWordInfoList.clear();
149            final HashSet<String> alreadySeen = new HashSet<String>();
150            addWord(typedWord, null, false);
151            alreadySeen.add(typedWord.toString());
152            final int previousSize = previousSuggestions.size();
153            for (int pos = 1; pos < previousSize; pos++) {
154                final String prevWord = previousSuggestions.getWord(pos).toString();
155                // Filter out duplicate suggestion.
156                if (!alreadySeen.contains(prevWord)) {
157                    addWord(prevWord, null, true);
158                    alreadySeen.add(prevWord);
159                }
160            }
161            mTypedWordValid = false;
162            mHasMinimalSuggestion = false;
163            return this;
164        }
165
166        public SuggestedWords build() {
167            return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion,
168                    mIsPunctuationSuggestions, mSuggestedWordInfoList);
169        }
170
171        public int size() {
172            return mWords.size();
173        }
174
175        public CharSequence getWord(int pos) {
176            return mWords.get(pos);
177        }
178    }
179
180    public static class SuggestedWordInfo {
181        private final CharSequence mDebugString;
182        private final boolean mPreviousSuggestedWord;
183
184        public SuggestedWordInfo() {
185            mDebugString = "";
186            mPreviousSuggestedWord = false;
187        }
188
189        public SuggestedWordInfo(CharSequence debugString, boolean previousSuggestedWord) {
190            mDebugString = debugString;
191            mPreviousSuggestedWord = previousSuggestedWord;
192        }
193
194        public String getDebugString() {
195            if (mDebugString == null) {
196                return "";
197            } else {
198                return mDebugString.toString();
199            }
200        }
201
202        public boolean isObsoleteSuggestedWord () {
203            return mPreviousSuggestedWord;
204        }
205    }
206}
207