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.os.Parcel;
20 import android.os.Parcelable;
21 
22 import com.android.internal.util.ArrayUtils;
23 
24 /**
25  * This class contains a metadata of suggestions from the text service
26  */
27 public 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     /**
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;
48     private final int mSuggestionsAttributes;
49     private final String[] mSuggestions;
50     private final boolean mSuggestionsAvailable;
51     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      */
SuggestionsInfo(int suggestionsAttributes, String[] suggestions)59     public SuggestionsInfo(int suggestionsAttributes, String[] suggestions) {
60         this(suggestionsAttributes, suggestions, 0, 0);
61     }
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      */
SuggestionsInfo( int suggestionsAttributes, String[] suggestions, int cookie, int sequence)70     public SuggestionsInfo(
71             int suggestionsAttributes, String[] suggestions, int cookie, int sequence) {
72         if (suggestions == null) {
73             mSuggestions = EMPTY;
74             mSuggestionsAvailable = false;
75         } else {
76             mSuggestions = suggestions;
77             mSuggestionsAvailable = true;
78         }
79         mSuggestionsAttributes = suggestionsAttributes;
80         mCookie = cookie;
81         mSequence = sequence;
82     }
83 
SuggestionsInfo(Parcel source)84     public SuggestionsInfo(Parcel source) {
85         mSuggestionsAttributes = source.readInt();
86         mSuggestions = source.readStringArray();
87         mCookie = source.readInt();
88         mSequence = source.readInt();
89         mSuggestionsAvailable = source.readInt() == 1;
90     }
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
writeToParcel(Parcel dest, int flags)99     public void writeToParcel(Parcel dest, int flags) {
100         dest.writeInt(mSuggestionsAttributes);
101         dest.writeStringArray(mSuggestions);
102         dest.writeInt(mCookie);
103         dest.writeInt(mSequence);
104         dest.writeInt(mSuggestionsAvailable ? 1 : 0);
105     }
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      */
setCookieAndSequence(int cookie, int sequence)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      */
getCookie()121     public int getCookie() {
122         return mCookie;
123     }
124 
125     /**
126      * @return the sequence which may be set by a client application
127      */
getSequence()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      */
getSuggestionsAttributes()137     public int getSuggestionsAttributes() {
138         return mSuggestionsAttributes;
139     }
140 
141     /**
142      * @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.
146      */
getSuggestionsCount()147     public int getSuggestionsCount() {
148         if (!mSuggestionsAvailable) {
149             return -1;
150         }
151         return mSuggestions.length;
152     }
153 
154     /**
155      * @param i the id of suggestions
156      * @return the suggestion at the specified id
157      */
getSuggestionAt(int i)158     public String getSuggestionAt(int i) {
159         return mSuggestions[i];
160     }
161 
162     /**
163      * Used to make this class parcelable.
164      */
165     public static final @android.annotation.NonNull Parcelable.Creator<SuggestionsInfo> CREATOR
166             = 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
describeContents()182     public int describeContents() {
183         return 0;
184     }
185 }
186