SuggestedWords.java revision 9ecad8c2e8571ece6f3f7fbb19ceda5be7866cf0
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; 57 } 58 59 public boolean hasWordAboveAutoCorrectionScoreThreshold() { 60 return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid); 61 } 62 63 public static class Builder { 64 private List<CharSequence> mWords; 65 private boolean mIsCompletions; 66 private boolean mTypedWordVallid; 67 private boolean mHasMinimalSuggestion; 68 private Object[] mDebugInfo; 69 70 public Builder() { 71 // Nothing to do here. 72 } 73 74 public Builder setWords(List<CharSequence> words) { 75 mWords = words; 76 return this; 77 } 78 79 public Builder setDebugInfo(Object[] debuginfo) { 80 mDebugInfo = debuginfo; 81 return this; 82 } 83 84 public Builder addWord(int pos, CharSequence word) { 85 if (mWords == null) 86 mWords = new ArrayList<CharSequence>(); 87 mWords.add(pos, word); 88 return this; 89 } 90 91 public Builder addWord(CharSequence word) { 92 if (mWords == null) 93 mWords = new ArrayList<CharSequence>(); 94 mWords.add(word); 95 return this; 96 } 97 98 public Builder setApplicationSpecifiedCompletions(CompletionInfo[] infos) { 99 for (CompletionInfo info : infos) 100 addWord(info.getText()); 101 mIsCompletions = true; 102 return this; 103 } 104 105 public Builder setTypedWordValid(boolean typedWordValid) { 106 mTypedWordVallid = typedWordValid; 107 return this; 108 } 109 110 public Builder setHasMinimalSuggestion(boolean hasMinamlSuggestion) { 111 mHasMinimalSuggestion = hasMinamlSuggestion; 112 return this; 113 } 114 115 public CharSequence getWord(int pos) { 116 return mWords.get(pos); 117 } 118 119 public SuggestedWords build() { 120 return new SuggestedWords(mWords, mIsCompletions, mTypedWordVallid, 121 mHasMinimalSuggestion, mDebugInfo); 122 } 123 } 124} 125