SuggestionsInfo.java revision 6435a56a8c02de98befcc8cd743b2b638cffb327
1/* 2 * Copyright (C) 2011 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 android.view.textservice; 18 19import com.android.internal.util.ArrayUtils; 20 21import android.os.Parcel; 22import android.os.Parcelable; 23 24/** 25 * This class contains a metadata of suggestions from the text service 26 */ 27public final class SuggestionsInfo implements Parcelable { 28 private static final String[] EMPTY = ArrayUtils.emptyArray(String.class); 29 30 /** 31 * Flag of the attributes of the suggestions that can be obtained by 32 * {@link #getSuggestionsAttributes}: this tells that the requested word was found 33 * in the dictionary in the text service. 34 */ 35 public static final int RESULT_ATTR_IN_THE_DICTIONARY = 0x0001; 36 /** 37 * Flag of the attributes of the suggestions that can be obtained by 38 * {@link #getSuggestionsAttributes}: this tells that the text service thinks the requested 39 * word looks like a typo. 40 */ 41 public static final int RESULT_ATTR_LOOKS_LIKE_TYPO = 0x0002; 42 private final int mSuggestionsAttributes; 43 private final String[] mSuggestions; 44 private final boolean mSuggestionsAvailable; 45 private int mCookie; 46 private int mSequence; 47 48 /** 49 * Constructor. 50 * @param suggestionsAttributes from the text service 51 * @param suggestions from the text service 52 */ 53 public SuggestionsInfo(int suggestionsAttributes, String[] suggestions) { 54 mSuggestionsAttributes = suggestionsAttributes; 55 if (suggestions == null) { 56 mSuggestions = EMPTY; 57 mSuggestionsAvailable = false; 58 } else { 59 mSuggestions = suggestions; 60 mSuggestionsAvailable = true; 61 } 62 mCookie = 0; 63 mSequence = 0; 64 } 65 66 /** 67 * Constructor. 68 * @param suggestionsAttributes from the text service 69 * @param suggestions from the text service 70 * @param cookie the cookie of the input TextInfo 71 * @param sequence the cookie of the input TextInfo 72 */ 73 public SuggestionsInfo( 74 int suggestionsAttributes, String[] suggestions, int cookie, int sequence) { 75 if (suggestions == null) { 76 mSuggestions = EMPTY; 77 mSuggestionsAvailable = false; 78 } else { 79 mSuggestions = suggestions; 80 mSuggestionsAvailable = true; 81 } 82 mSuggestionsAttributes = suggestionsAttributes; 83 mCookie = cookie; 84 mSequence = sequence; 85 } 86 87 public SuggestionsInfo(Parcel source) { 88 mSuggestionsAttributes = source.readInt(); 89 mSuggestions = source.readStringArray(); 90 mCookie = source.readInt(); 91 mSequence = source.readInt(); 92 mSuggestionsAvailable = source.readInt() == 1; 93 } 94 95 /** 96 * Used to package this object into a {@link Parcel}. 97 * 98 * @param dest The {@link Parcel} to be written. 99 * @param flags The flags used for parceling. 100 */ 101 @Override 102 public void writeToParcel(Parcel dest, int flags) { 103 dest.writeInt(mSuggestionsAttributes); 104 dest.writeStringArray(mSuggestions); 105 dest.writeInt(mCookie); 106 dest.writeInt(mSequence); 107 dest.writeInt(mSuggestionsAvailable ? 1 : 0); 108 } 109 110 /** 111 * Set the cookie and the sequence of SuggestionsInfo which are set to TextInfo from a client 112 * application 113 * @param cookie the cookie of an input TextInfo 114 * @param sequence the cookie of an input TextInfo 115 */ 116 public void setCookieAndSequence(int cookie, int sequence) { 117 mCookie = cookie; 118 mSequence = sequence; 119 } 120 121 /** 122 * @return the cookie which may be set by a client application 123 */ 124 public int getCookie() { 125 return mCookie; 126 } 127 128 /** 129 * @return the sequence which may be set by a client application 130 */ 131 public int getSequence() { 132 return mSequence; 133 } 134 135 /** 136 * @return the attributes of suggestions. This includes whether the spell checker has the word 137 * in its dictionary or not and whether the spell checker has confident suggestions for the 138 * word or not. 139 */ 140 public int getSuggestionsAttributes() { 141 return mSuggestionsAttributes; 142 } 143 144 /** 145 * @return the count of the suggestions. If there's no suggestions at all, this method returns 146 * -1. Even if this method returns 0, it doesn't necessarily mean that there are no suggestions 147 * for the requested word. For instance, the caller could have been asked to limit the maximum 148 * number of suggestions returned. 149 */ 150 public int getSuggestionsCount() { 151 if (!mSuggestionsAvailable) { 152 return -1; 153 } 154 return mSuggestions.length; 155 } 156 157 /** 158 * @param i the id of suggestions 159 * @return the suggestion at the specified id 160 */ 161 public String getSuggestionAt(int i) { 162 return mSuggestions[i]; 163 } 164 165 /** 166 * Used to make this class parcelable. 167 */ 168 public static final Parcelable.Creator<SuggestionsInfo> CREATOR 169 = new Parcelable.Creator<SuggestionsInfo>() { 170 @Override 171 public SuggestionsInfo createFromParcel(Parcel source) { 172 return new SuggestionsInfo(source); 173 } 174 175 @Override 176 public SuggestionsInfo[] newArray(int size) { 177 return new SuggestionsInfo[size]; 178 } 179 }; 180 181 /** 182 * Used to make this class parcelable. 183 */ 184 @Override 185 public int describeContents() { 186 return 0; 187 } 188} 189