blob: e69b6e7277a86c5bc7c06458ce4948d81e7b2c47 [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
19import android.os.Parcel;
20import android.os.Parcelable;
21
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070022import com.android.internal.util.ArrayUtils;
23
satok988323c2011-06-22 16:38:13 +090024/**
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;
satoka17b3502011-10-28 19:11:00 +090042 /**
43 * Flag of the attributes of the suggestions that can be obtained by
44 * {@link #getSuggestionsAttributes}: this tells that the text service thinks
45 * the result suggestions include highly recommended ones.
46 */
47 public static final int RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS = 0x0004;
satok988323c2011-06-22 16:38:13 +090048 private final int mSuggestionsAttributes;
49 private final String[] mSuggestions;
satok1bedd992011-07-23 11:39:55 +090050 private final boolean mSuggestionsAvailable;
satok988323c2011-06-22 16:38:13 +090051 private int mCookie;
52 private int mSequence;
53
54 /**
55 * Constructor.
56 * @param suggestionsAttributes from the text service
57 * @param suggestions from the text service
58 */
59 public SuggestionsInfo(int suggestionsAttributes, String[] suggestions) {
satok0dc1f642011-11-18 11:27:10 +090060 this(suggestionsAttributes, suggestions, 0, 0);
satok988323c2011-06-22 16:38:13 +090061 }
62
63 /**
64 * Constructor.
65 * @param suggestionsAttributes from the text service
66 * @param suggestions from the text service
67 * @param cookie the cookie of the input TextInfo
68 * @param sequence the cookie of the input TextInfo
69 */
70 public SuggestionsInfo(
71 int suggestionsAttributes, String[] suggestions, int cookie, int sequence) {
72 if (suggestions == null) {
satok1bedd992011-07-23 11:39:55 +090073 mSuggestions = EMPTY;
74 mSuggestionsAvailable = false;
75 } else {
76 mSuggestions = suggestions;
77 mSuggestionsAvailable = true;
satok988323c2011-06-22 16:38:13 +090078 }
79 mSuggestionsAttributes = suggestionsAttributes;
satok988323c2011-06-22 16:38:13 +090080 mCookie = cookie;
81 mSequence = sequence;
82 }
83
84 public SuggestionsInfo(Parcel source) {
85 mSuggestionsAttributes = source.readInt();
86 mSuggestions = source.readStringArray();
87 mCookie = source.readInt();
88 mSequence = source.readInt();
satok1bedd992011-07-23 11:39:55 +090089 mSuggestionsAvailable = source.readInt() == 1;
satok988323c2011-06-22 16:38:13 +090090 }
91
92 /**
93 * Used to package this object into a {@link Parcel}.
94 *
95 * @param dest The {@link Parcel} to be written.
96 * @param flags The flags used for parceling.
97 */
98 @Override
99 public void writeToParcel(Parcel dest, int flags) {
100 dest.writeInt(mSuggestionsAttributes);
101 dest.writeStringArray(mSuggestions);
102 dest.writeInt(mCookie);
103 dest.writeInt(mSequence);
satok1bedd992011-07-23 11:39:55 +0900104 dest.writeInt(mSuggestionsAvailable ? 1 : 0);
satok988323c2011-06-22 16:38:13 +0900105 }
106
107 /**
108 * Set the cookie and the sequence of SuggestionsInfo which are set to TextInfo from a client
109 * application
110 * @param cookie the cookie of an input TextInfo
111 * @param sequence the cookie of an input TextInfo
112 */
113 public void setCookieAndSequence(int cookie, int sequence) {
114 mCookie = cookie;
115 mSequence = sequence;
116 }
117
118 /**
119 * @return the cookie which may be set by a client application
120 */
121 public int getCookie() {
122 return mCookie;
123 }
124
125 /**
126 * @return the sequence which may be set by a client application
127 */
128 public int getSequence() {
129 return mSequence;
130 }
131
132 /**
133 * @return the attributes of suggestions. This includes whether the spell checker has the word
134 * in its dictionary or not and whether the spell checker has confident suggestions for the
135 * word or not.
136 */
137 public int getSuggestionsAttributes() {
138 return mSuggestionsAttributes;
139 }
140
141 /**
satok1bedd992011-07-23 11:39:55 +0900142 * @return the count of the suggestions. If there's no suggestions at all, this method returns
143 * -1. Even if this method returns 0, it doesn't necessarily mean that there are no suggestions
144 * for the requested word. For instance, the caller could have been asked to limit the maximum
145 * number of suggestions returned.
satok988323c2011-06-22 16:38:13 +0900146 */
147 public int getSuggestionsCount() {
satok1bedd992011-07-23 11:39:55 +0900148 if (!mSuggestionsAvailable) {
149 return -1;
150 }
satok988323c2011-06-22 16:38:13 +0900151 return mSuggestions.length;
152 }
153
154 /**
155 * @param i the id of suggestions
156 * @return the suggestion at the specified id
157 */
158 public String getSuggestionAt(int i) {
159 return mSuggestions[i];
160 }
161
162 /**
163 * Used to make this class parcelable.
164 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700165 public static final @android.annotation.NonNull Parcelable.Creator<SuggestionsInfo> CREATOR
satok988323c2011-06-22 16:38:13 +0900166 = new Parcelable.Creator<SuggestionsInfo>() {
167 @Override
168 public SuggestionsInfo createFromParcel(Parcel source) {
169 return new SuggestionsInfo(source);
170 }
171
172 @Override
173 public SuggestionsInfo[] newArray(int size) {
174 return new SuggestionsInfo[size];
175 }
176 };
177
178 /**
179 * Used to make this class parcelable.
180 */
181 @Override
182 public int describeContents() {
183 return 0;
184 }
satok988323c2011-06-22 16:38:13 +0900185}