SuggestedWords.java revision 6f7218627eda110a8454053f8ecb7b80edfdc8ce
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.view.inputmethod.CompletionInfo;
20
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.List;
24
25public class SuggestedWords {
26    public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, null);
27
28    public final List<CharSequence> mWords;
29    public final boolean mIsApplicationSpecifiedCompletions;
30    public final boolean mTypedWordValid;
31    public final boolean mHasMinimalSuggestion;
32    public final List<SuggestedWordInfo> mSuggestedWordInfoList;
33
34    private SuggestedWords(List<CharSequence> words, boolean isApplicationSpecifiedCompletions,
35            boolean typedWordValid, boolean hasMinamlSuggestion,
36            List<SuggestedWordInfo> suggestedWordInfoList) {
37        if (words != null) {
38            mWords = words;
39        } else {
40            mWords = Collections.emptyList();
41        }
42        mIsApplicationSpecifiedCompletions = isApplicationSpecifiedCompletions;
43        mTypedWordValid = typedWordValid;
44        mHasMinimalSuggestion = hasMinamlSuggestion;
45        mSuggestedWordInfoList = suggestedWordInfoList;
46    }
47
48    public int size() {
49        return mWords.size();
50    }
51
52    public CharSequence getWord(int pos) {
53        return mWords.get(pos);
54    }
55
56    public boolean hasAutoCorrectionWord() {
57        return mHasMinimalSuggestion && size() > 1 && !mTypedWordValid;
58    }
59
60    public boolean hasWordAboveAutoCorrectionScoreThreshold() {
61        return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid);
62    }
63
64    public static class Builder {
65        private List<CharSequence> mWords = new ArrayList<CharSequence>();
66        private boolean mIsCompletions;
67        private boolean mTypedWordValid;
68        private boolean mHasMinimalSuggestion;
69        private List<SuggestedWordInfo> mSuggestedWordInfoList =
70                new ArrayList<SuggestedWordInfo>();
71
72        public Builder() {
73            // Nothing to do here.
74        }
75
76        public Builder addWords(List<CharSequence> words,
77                List<SuggestedWordInfo> suggestedWordInfoList) {
78            final int N = words.size();
79            for (int i = 0; i < N; ++i) {
80                SuggestedWordInfo suggestedWordInfo = null;
81                if (suggestedWordInfoList != null) {
82                    suggestedWordInfo = suggestedWordInfoList.get(i);
83                }
84                if (suggestedWordInfo == null) {
85                    suggestedWordInfo = new SuggestedWordInfo();
86                }
87                addWord(words.get(i), suggestedWordInfo);
88            }
89            return this;
90        }
91
92        public Builder addWord(CharSequence word) {
93            return addWord(word, null, false);
94        }
95
96        public Builder addWord(CharSequence word, CharSequence debugString,
97                boolean isPreviousSuggestedWord) {
98            SuggestedWordInfo info = new SuggestedWordInfo(debugString, isPreviousSuggestedWord);
99            return addWord(word, info);
100        }
101
102        private Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
103            mWords.add(word);
104            mSuggestedWordInfoList.add(suggestedWordInfo);
105            return this;
106        }
107
108        public Builder setApplicationSpecifiedCompletions(CompletionInfo[] infos) {
109            for (CompletionInfo info : infos)
110                addWord(info.getText());
111            mIsCompletions = true;
112            return this;
113        }
114
115        public Builder setTypedWordValid(boolean typedWordValid) {
116            mTypedWordValid = typedWordValid;
117            return this;
118        }
119
120        public Builder setHasMinimalSuggestion(boolean hasMinamlSuggestion) {
121            mHasMinimalSuggestion = hasMinamlSuggestion;
122            return this;
123        }
124
125        // Should get rid of the first one (what the user typed previously) from suggestions
126        // and replace it with what the user currently typed.
127        public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
128                SuggestedWords previousSuggestions) {
129            mWords.clear();
130            mSuggestedWordInfoList.clear();
131            addWord(typedWord, null, false);
132            final int previousSize = previousSuggestions.size();
133            for (int pos = 1; pos < previousSize; pos++)
134                addWord(previousSuggestions.getWord(pos), null, true);
135            mIsCompletions = false;
136            mTypedWordValid = false;
137            mHasMinimalSuggestion = false;
138            return this;
139        }
140
141        public SuggestedWords build() {
142            return new SuggestedWords(mWords, mIsCompletions, mTypedWordValid,
143                    mHasMinimalSuggestion, mSuggestedWordInfoList);
144        }
145
146        public int size() {
147            return mWords.size();
148        }
149
150        public CharSequence getWord(int pos) {
151            return mWords.get(pos);
152        }
153    }
154
155    public static class SuggestedWordInfo {
156        private final CharSequence mDebugString;
157        private final boolean mPreviousSuggestedWord;
158
159        public SuggestedWordInfo() {
160            mDebugString = "";
161            mPreviousSuggestedWord = false;
162        }
163
164        public SuggestedWordInfo(CharSequence debugString, boolean previousSuggestedWord) {
165            mDebugString = debugString;
166            mPreviousSuggestedWord = previousSuggestedWord;
167        }
168
169        public String getDebugString() {
170            if (mDebugString == null) {
171                return "";
172            } else {
173                return mDebugString.toString();
174            }
175        }
176
177        public boolean isPreviousSuggestedWord () {
178            return mPreviousSuggestedWord;
179        }
180    }
181}
182