blob: 62a06b9afa873e292f42121aa448df8f15b8f717 [file] [log] [blame]
satok988323c2011-06-22 16:38:13 +09001/*
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
Gilles Debunne6435a562011-08-04 21:22:30 -070019import com.android.internal.util.ArrayUtils;
20
satok988323c2011-06-22 16:38:13 +090021import 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 {
Gilles Debunne6435a562011-08-04 21:22:30 -070028 private static final String[] EMPTY = ArrayUtils.emptyArray(String.class);
satok1bedd992011-07-23 11:39:55 +090029
satok988323c2011-06-22 16:38:13 +090030 /**
31 * Flag of the attributes of the suggestions that can be obtained by
satok3de3d6a2011-07-22 15:35:31 +090032 * {@link #getSuggestionsAttributes}: this tells that the requested word was found
satok988323c2011-06-22 16:38:13 +090033 * in the dictionary in the text service.
34 */
35 public static final int RESULT_ATTR_IN_THE_DICTIONARY = 0x0001;
satok988323c2011-06-22 16:38:13 +090036 /**
37 * Flag of the attributes of the suggestions that can be obtained by
satok3de3d6a2011-07-22 15:35:31 +090038 * {@link #getSuggestionsAttributes}: this tells that the text service thinks the requested
satokc6600912011-08-03 18:44:54 +090039 * word looks like a typo.
satok988323c2011-06-22 16:38:13 +090040 */
satokc6600912011-08-03 18:44:54 +090041 public static final int RESULT_ATTR_LOOKS_LIKE_TYPO = 0x0002;
satok988323c2011-06-22 16:38:13 +090042 private final int mSuggestionsAttributes;
43 private final String[] mSuggestions;
satok1bedd992011-07-23 11:39:55 +090044 private final boolean mSuggestionsAvailable;
satok988323c2011-06-22 16:38:13 +090045 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) {
satok988323c2011-06-22 16:38:13 +090054 mSuggestionsAttributes = suggestionsAttributes;
satok1bedd992011-07-23 11:39:55 +090055 if (suggestions == null) {
56 mSuggestions = EMPTY;
57 mSuggestionsAvailable = false;
58 } else {
59 mSuggestions = suggestions;
60 mSuggestionsAvailable = true;
61 }
satok988323c2011-06-22 16:38:13 +090062 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) {
satok1bedd992011-07-23 11:39:55 +090076 mSuggestions = EMPTY;
77 mSuggestionsAvailable = false;
78 } else {
79 mSuggestions = suggestions;
80 mSuggestionsAvailable = true;
satok988323c2011-06-22 16:38:13 +090081 }
82 mSuggestionsAttributes = suggestionsAttributes;
satok988323c2011-06-22 16:38:13 +090083 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();
satok1bedd992011-07-23 11:39:55 +090092 mSuggestionsAvailable = source.readInt() == 1;
satok988323c2011-06-22 16:38:13 +090093 }
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);
satok1bedd992011-07-23 11:39:55 +0900107 dest.writeInt(mSuggestionsAvailable ? 1 : 0);
satok988323c2011-06-22 16:38:13 +0900108 }
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 /**
satok1bedd992011-07-23 11:39:55 +0900145 * @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.
satok988323c2011-06-22 16:38:13 +0900149 */
150 public int getSuggestionsCount() {
satok1bedd992011-07-23 11:39:55 +0900151 if (!mSuggestionsAvailable) {
152 return -1;
153 }
satok988323c2011-06-22 16:38:13 +0900154 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}