SuggestedWords.java revision 7e181fe1010c8eac7814cc67a0c4b3864a10b151
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 Object[] mDebugInfo;
33
34    private SuggestedWords(List<CharSequence> words, boolean isApplicationSpecifiedCompletions,
35            boolean typedWordValid, boolean hasMinamlSuggestion, Object[] debugInfo) {
36        if (words != null) {
37            mWords = words;
38        } else {
39            mWords = Collections.emptyList();
40        }
41        mIsApplicationSpecifiedCompletions = isApplicationSpecifiedCompletions;
42        mTypedWordValid = typedWordValid;
43        mHasMinimalSuggestion = hasMinamlSuggestion;
44        mDebugInfo = debugInfo;
45    }
46
47    public int size() {
48        return mWords.size();
49    }
50
51    public CharSequence getWord(int pos) {
52        return mWords.get(pos);
53    }
54
55    public boolean hasAutoCorrectionWord() {
56        return mHasMinimalSuggestion && ((size() >= 1 && !mTypedWordValid) || mTypedWordValid);
57    }
58
59    public static class Builder {
60        private List<CharSequence> mWords;
61        private boolean mIsCompletions;
62        private boolean mTypedWordVallid;
63        private boolean mHasMinimalSuggestion;
64        private Object[] mDebugInfo;
65
66        public Builder() {
67            // Nothing to do here.
68        }
69
70        public Builder setWords(List<CharSequence> words) {
71            mWords = words;
72            return this;
73        }
74
75        public Builder setDebugInfo(Object[] debuginfo) {
76            mDebugInfo = debuginfo;
77            return this;
78        }
79
80        public Builder addWord(int pos, CharSequence word) {
81            if (mWords == null)
82                mWords = new ArrayList<CharSequence>();
83            mWords.add(pos, word);
84            return this;
85        }
86
87        public Builder addWord(CharSequence word) {
88            if (mWords == null)
89                mWords = new ArrayList<CharSequence>();
90            mWords.add(word);
91            return this;
92        }
93
94        public Builder setApplicationSpecifiedCompletions(CompletionInfo[] infos) {
95            for (CompletionInfo info : infos)
96                addWord(info.getText());
97            mIsCompletions = true;
98            return this;
99        }
100
101        public Builder setTypedWordValid(boolean typedWordValid) {
102            mTypedWordVallid = typedWordValid;
103            return this;
104        }
105
106        public Builder setHasMinimalSuggestion(boolean hasMinamlSuggestion) {
107            mHasMinimalSuggestion = hasMinamlSuggestion;
108            return this;
109        }
110
111        public CharSequence getWord(int pos) {
112            return mWords.get(pos);
113        }
114
115        public SuggestedWords build() {
116            return new SuggestedWords(mWords, mIsCompletions, mTypedWordVallid,
117                    mHasMinimalSuggestion, mDebugInfo);
118        }
119    }
120}
121