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