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 
19 import android.annotation.IntDef;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import com.android.internal.util.ArrayUtils;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * This class contains a metadata of suggestions from the text service
30  */
31 public final class SuggestionsInfo implements Parcelable {
32     private static final String[] EMPTY = ArrayUtils.emptyArray(String.class);
33 
34     /**
35      * An internal annotation to indicate that one ore more combinations of
36      * <code>RESULT_ATTR_</code> flags are expected.
37      *
38      * @hide
39      */
40     @Retention(RetentionPolicy.SOURCE)
41     @IntDef(flag = true, prefix = { "RESULT_ATTR_" }, value = {
42             RESULT_ATTR_IN_THE_DICTIONARY,
43             RESULT_ATTR_LOOKS_LIKE_TYPO,
44             RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS,
45             RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR,
46             RESULT_ATTR_DONT_SHOW_UI_FOR_SUGGESTIONS,
47     })
48     public @interface ResultAttrs {}
49 
50     /**
51      * Flag of the attributes of the suggestions that can be obtained by
52      * {@link #getSuggestionsAttributes}: this tells that the requested word was found
53      * in the dictionary in the text service.
54      */
55     public static final int RESULT_ATTR_IN_THE_DICTIONARY = 0x0001;
56     /**
57      * Flag of the attributes of the suggestions that can be obtained by
58      * {@link #getSuggestionsAttributes}: this tells that the text service thinks the requested
59      * word looks like a typo.
60      */
61     public static final int RESULT_ATTR_LOOKS_LIKE_TYPO = 0x0002;
62     /**
63      * Flag of the attributes of the suggestions that can be obtained by
64      * {@link #getSuggestionsAttributes}: this tells that the text service thinks
65      * the result suggestions include highly recommended ones.
66      */
67     public static final int RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS = 0x0004;
68 
69     /**
70      * Flag of the attributes of the suggestions that can be obtained by
71      * {@link #getSuggestionsAttributes}: this tells that the text service thinks the requested
72      * sentence contains a grammar error.
73      */
74     public static final int RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR = 0x0008;
75 
76     /**
77      * Flag of the attributes of the suggestions that can be obtained by
78      * {@link #getSuggestionsAttributes}: this tells that the text service has an alternative way to
79      * show UI for the list of correction suggestions to the user. When this flag is set, the
80      * receiver of the result suggestions should mark the erroneous part of the text with a text
81      * signifier (for example, underline), but should not show any UI for the list of correction
82      * suggestions to the user (for example, in a popup window).
83      */
84     public static final int RESULT_ATTR_DONT_SHOW_UI_FOR_SUGGESTIONS = 0x0010;
85 
86     private final @ResultAttrs int mSuggestionsAttributes;
87     private final String[] mSuggestions;
88     private final boolean mSuggestionsAvailable;
89     private int mCookie;
90     private int mSequence;
91 
92     /**
93      * Constructor.
94      * @param suggestionsAttributes from the text service
95      * @param suggestions from the text service
96      */
SuggestionsInfo(int suggestionsAttributes, String[] suggestions)97     public SuggestionsInfo(int suggestionsAttributes, String[] suggestions) {
98         this(suggestionsAttributes, suggestions, 0, 0);
99     }
100 
101     /**
102      * Constructor.
103      * @param suggestionsAttributes from the text service
104      * @param suggestions from the text service
105      * @param cookie the cookie of the input TextInfo
106      * @param sequence the cookie of the input TextInfo
107      */
SuggestionsInfo(@esultAttrs int suggestionsAttributes, String[] suggestions, int cookie, int sequence)108     public SuggestionsInfo(@ResultAttrs int suggestionsAttributes, String[] suggestions, int cookie,
109             int sequence) {
110         if (suggestions == null) {
111             mSuggestions = EMPTY;
112             mSuggestionsAvailable = false;
113         } else {
114             mSuggestions = suggestions;
115             mSuggestionsAvailable = true;
116         }
117         mSuggestionsAttributes = suggestionsAttributes;
118         mCookie = cookie;
119         mSequence = sequence;
120     }
121 
SuggestionsInfo(Parcel source)122     public SuggestionsInfo(Parcel source) {
123         mSuggestionsAttributes = source.readInt();
124         mSuggestions = source.readStringArray();
125         mCookie = source.readInt();
126         mSequence = source.readInt();
127         mSuggestionsAvailable = source.readInt() == 1;
128     }
129 
130     /**
131      * Used to package this object into a {@link Parcel}.
132      *
133      * @param dest The {@link Parcel} to be written.
134      * @param flags The flags used for parceling.
135      */
136     @Override
writeToParcel(Parcel dest, int flags)137     public void writeToParcel(Parcel dest, int flags) {
138         dest.writeInt(mSuggestionsAttributes);
139         dest.writeStringArray(mSuggestions);
140         dest.writeInt(mCookie);
141         dest.writeInt(mSequence);
142         dest.writeInt(mSuggestionsAvailable ? 1 : 0);
143     }
144 
145     /**
146      * Set the cookie and the sequence of SuggestionsInfo which are set to TextInfo from a client
147      * application
148      * @param cookie the cookie of an input TextInfo
149      * @param sequence the cookie of an input TextInfo
150      */
setCookieAndSequence(int cookie, int sequence)151     public void setCookieAndSequence(int cookie, int sequence) {
152         mCookie = cookie;
153         mSequence = sequence;
154     }
155 
156     /**
157      * @return the cookie which may be set by a client application
158      */
getCookie()159     public int getCookie() {
160         return mCookie;
161     }
162 
163     /**
164      * @return the sequence which may be set by a client application
165      */
getSequence()166     public int getSequence() {
167         return mSequence;
168     }
169 
170     /**
171      * @return the attributes of suggestions. This includes whether the spell checker has the word
172      * in its dictionary or not and whether the spell checker has confident suggestions for the
173      * word or not.
174      */
getSuggestionsAttributes()175     public @ResultAttrs int getSuggestionsAttributes() {
176         return mSuggestionsAttributes;
177     }
178 
179     /**
180      * @return the count of the suggestions. If there's no suggestions at all, this method returns
181      * -1. Even if this method returns 0, it doesn't necessarily mean that there are no suggestions
182      * for the requested word. For instance, the caller could have been asked to limit the maximum
183      * number of suggestions returned.
184      */
getSuggestionsCount()185     public int getSuggestionsCount() {
186         if (!mSuggestionsAvailable) {
187             return -1;
188         }
189         return mSuggestions.length;
190     }
191 
192     /**
193      * @param i the id of suggestions
194      * @return the suggestion at the specified id
195      */
getSuggestionAt(int i)196     public String getSuggestionAt(int i) {
197         return mSuggestions[i];
198     }
199 
200     /**
201      * Used to make this class parcelable.
202      */
203     public static final @android.annotation.NonNull Parcelable.Creator<SuggestionsInfo> CREATOR
204             = new Parcelable.Creator<SuggestionsInfo>() {
205         @Override
206         public SuggestionsInfo createFromParcel(Parcel source) {
207             return new SuggestionsInfo(source);
208         }
209 
210         @Override
211         public SuggestionsInfo[] newArray(int size) {
212             return new SuggestionsInfo[size];
213         }
214     };
215 
216     /**
217      * Used to make this class parcelable.
218      */
219     @Override
describeContents()220     public int describeContents() {
221         return 0;
222     }
223 }
224