Home | History | Annotate | Download | only in textservice
      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 com.android.internal.util.ArrayUtils;
     20 
     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 {
     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      */
     59     public SuggestionsInfo(int suggestionsAttributes, String[] suggestions) {
     60         mSuggestionsAttributes = suggestionsAttributes;
     61         if (suggestions == null) {
     62             mSuggestions = EMPTY;
     63             mSuggestionsAvailable = false;
     64         } else {
     65             mSuggestions = suggestions;
     66             mSuggestionsAvailable = true;
     67         }
     68         mCookie = 0;
     69         mSequence = 0;
     70     }
     71 
     72     /**
     73      * Constructor.
     74      * @param suggestionsAttributes from the text service
     75      * @param suggestions from the text service
     76      * @param cookie the cookie of the input TextInfo
     77      * @param sequence the cookie of the input TextInfo
     78      */
     79     public SuggestionsInfo(
     80             int suggestionsAttributes, String[] suggestions, int cookie, int sequence) {
     81         if (suggestions == null) {
     82             mSuggestions = EMPTY;
     83             mSuggestionsAvailable = false;
     84         } else {
     85             mSuggestions = suggestions;
     86             mSuggestionsAvailable = true;
     87         }
     88         mSuggestionsAttributes = suggestionsAttributes;
     89         mCookie = cookie;
     90         mSequence = sequence;
     91     }
     92 
     93     public SuggestionsInfo(Parcel source) {
     94         mSuggestionsAttributes = source.readInt();
     95         mSuggestions = source.readStringArray();
     96         mCookie = source.readInt();
     97         mSequence = source.readInt();
     98         mSuggestionsAvailable = source.readInt() == 1;
     99     }
    100 
    101     /**
    102      * Used to package this object into a {@link Parcel}.
    103      *
    104      * @param dest The {@link Parcel} to be written.
    105      * @param flags The flags used for parceling.
    106      */
    107     @Override
    108     public void writeToParcel(Parcel dest, int flags) {
    109         dest.writeInt(mSuggestionsAttributes);
    110         dest.writeStringArray(mSuggestions);
    111         dest.writeInt(mCookie);
    112         dest.writeInt(mSequence);
    113         dest.writeInt(mSuggestionsAvailable ? 1 : 0);
    114     }
    115 
    116     /**
    117      * Set the cookie and the sequence of SuggestionsInfo which are set to TextInfo from a client
    118      * application
    119      * @param cookie the cookie of an input TextInfo
    120      * @param sequence the cookie of an input TextInfo
    121      */
    122     public void setCookieAndSequence(int cookie, int sequence) {
    123         mCookie = cookie;
    124         mSequence = sequence;
    125     }
    126 
    127     /**
    128      * @return the cookie which may be set by a client application
    129      */
    130     public int getCookie() {
    131         return mCookie;
    132     }
    133 
    134     /**
    135      * @return the sequence which may be set by a client application
    136      */
    137     public int getSequence() {
    138         return mSequence;
    139     }
    140 
    141     /**
    142      * @return the attributes of suggestions. This includes whether the spell checker has the word
    143      * in its dictionary or not and whether the spell checker has confident suggestions for the
    144      * word or not.
    145      */
    146     public int getSuggestionsAttributes() {
    147         return mSuggestionsAttributes;
    148     }
    149 
    150     /**
    151      * @return the count of the suggestions. If there's no suggestions at all, this method returns
    152      * -1. Even if this method returns 0, it doesn't necessarily mean that there are no suggestions
    153      * for the requested word. For instance, the caller could have been asked to limit the maximum
    154      * number of suggestions returned.
    155      */
    156     public int getSuggestionsCount() {
    157         if (!mSuggestionsAvailable) {
    158             return -1;
    159         }
    160         return mSuggestions.length;
    161     }
    162 
    163     /**
    164      * @param i the id of suggestions
    165      * @return the suggestion at the specified id
    166      */
    167     public String getSuggestionAt(int i) {
    168         return mSuggestions[i];
    169     }
    170 
    171     /**
    172      * Used to make this class parcelable.
    173      */
    174     public static final Parcelable.Creator<SuggestionsInfo> CREATOR
    175             = new Parcelable.Creator<SuggestionsInfo>() {
    176         @Override
    177         public SuggestionsInfo createFromParcel(Parcel source) {
    178             return new SuggestionsInfo(source);
    179         }
    180 
    181         @Override
    182         public SuggestionsInfo[] newArray(int size) {
    183             return new SuggestionsInfo[size];
    184         }
    185     };
    186 
    187     /**
    188      * Used to make this class parcelable.
    189      */
    190     @Override
    191     public int describeContents() {
    192         return 0;
    193     }
    194 }
    195