satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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 | |
| 17 | package android.view.textservice; |
| 18 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 19 | import com.android.internal.util.ArrayUtils; |
| 20 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| 24 | /** |
| 25 | * This class contains a metadata of suggestions from the text service |
| 26 | */ |
| 27 | public final class SuggestionsInfo implements Parcelable { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 28 | private static final String[] EMPTY = ArrayUtils.emptyArray(String.class); |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 29 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 30 | /** |
| 31 | * Flag of the attributes of the suggestions that can be obtained by |
satok | 3de3d6a | 2011-07-22 15:35:31 +0900 | [diff] [blame] | 32 | * {@link #getSuggestionsAttributes}: this tells that the requested word was found |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 33 | * in the dictionary in the text service. |
| 34 | */ |
| 35 | public static final int RESULT_ATTR_IN_THE_DICTIONARY = 0x0001; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 36 | /** |
| 37 | * Flag of the attributes of the suggestions that can be obtained by |
satok | 3de3d6a | 2011-07-22 15:35:31 +0900 | [diff] [blame] | 38 | * {@link #getSuggestionsAttributes}: this tells that the text service thinks the requested |
satok | c660091 | 2011-08-03 18:44:54 +0900 | [diff] [blame] | 39 | * word looks like a typo. |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 40 | */ |
satok | c660091 | 2011-08-03 18:44:54 +0900 | [diff] [blame] | 41 | public static final int RESULT_ATTR_LOOKS_LIKE_TYPO = 0x0002; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 42 | private final int mSuggestionsAttributes; |
| 43 | private final String[] mSuggestions; |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 44 | private final boolean mSuggestionsAvailable; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 54 | mSuggestionsAttributes = suggestionsAttributes; |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 55 | if (suggestions == null) { |
| 56 | mSuggestions = EMPTY; |
| 57 | mSuggestionsAvailable = false; |
| 58 | } else { |
| 59 | mSuggestions = suggestions; |
| 60 | mSuggestionsAvailable = true; |
| 61 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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) { |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 76 | mSuggestions = EMPTY; |
| 77 | mSuggestionsAvailable = false; |
| 78 | } else { |
| 79 | mSuggestions = suggestions; |
| 80 | mSuggestionsAvailable = true; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 81 | } |
| 82 | mSuggestionsAttributes = suggestionsAttributes; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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(); |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 92 | mSuggestionsAvailable = source.readInt() == 1; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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); |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 107 | dest.writeInt(mSuggestionsAvailable ? 1 : 0); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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 | /** |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 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. |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 149 | */ |
| 150 | public int getSuggestionsCount() { |
satok | 1bedd99 | 2011-07-23 11:39:55 +0900 | [diff] [blame] | 151 | if (!mSuggestionsAvailable) { |
| 152 | return -1; |
| 153 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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 | } |