1 /*
2  * Copyright (C) 2012 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 java.util.Arrays;
23 
24 /**
25  * This class contains a metadata of suggestions returned from a text service
26  * (e.g. {@link android.service.textservice.SpellCheckerService}).
27  * The text service uses this class to return the suggestions
28  * for a sentence. See {@link SuggestionsInfo} which is used for suggestions for a word.
29  * This class extends the functionality of {@link SuggestionsInfo} as far as this class enables
30  * you to put multiple {@link SuggestionsInfo}s on a sentence with the offsets and the lengths
31  * of all {@link SuggestionsInfo}s.
32  */
33 public final class SentenceSuggestionsInfo implements Parcelable {
34 
35     private final SuggestionsInfo[] mSuggestionsInfos;
36     private final int[] mOffsets;
37     private final int[] mLengths;
38 
39     /**
40      * Constructor.
41      * @param suggestionsInfos from the text service
42      * @param offsets the array of offsets of suggestions
43      * @param lengths the array of lengths of suggestions
44      */
SentenceSuggestionsInfo( SuggestionsInfo[] suggestionsInfos, int[] offsets, int[] lengths)45     public SentenceSuggestionsInfo(
46             SuggestionsInfo[] suggestionsInfos, int[] offsets, int[] lengths) {
47         if (suggestionsInfos == null || offsets == null || lengths == null) {
48             throw new NullPointerException();
49         }
50         if (suggestionsInfos.length != offsets.length || offsets.length != lengths.length) {
51             throw new IllegalArgumentException();
52         }
53         final int infoSize = suggestionsInfos.length;
54         mSuggestionsInfos = Arrays.copyOf(suggestionsInfos, infoSize);
55         mOffsets = Arrays.copyOf(offsets, infoSize);
56         mLengths = Arrays.copyOf(lengths, infoSize);
57     }
58 
SentenceSuggestionsInfo(Parcel source)59     public SentenceSuggestionsInfo(Parcel source) {
60         final int infoSize = source.readInt();
61         mSuggestionsInfos = new SuggestionsInfo[infoSize];
62         source.readTypedArray(mSuggestionsInfos, SuggestionsInfo.CREATOR);
63         mOffsets = new int[mSuggestionsInfos.length];
64         source.readIntArray(mOffsets);
65         mLengths = new int[mSuggestionsInfos.length];
66         source.readIntArray(mLengths);
67     }
68 
69     /**
70      * Used to package this object into a {@link Parcel}.
71      *
72      * @param dest The {@link Parcel} to be written.
73      * @param flags The flags used for parceling.
74      */
75     @Override
writeToParcel(Parcel dest, int flags)76     public void writeToParcel(Parcel dest, int flags) {
77         final int infoSize = mSuggestionsInfos.length;
78         dest.writeInt(infoSize);
79         dest.writeTypedArray(mSuggestionsInfos, 0);
80         dest.writeIntArray(mOffsets);
81         dest.writeIntArray(mLengths);
82     }
83 
84     @Override
describeContents()85     public int describeContents() {
86         return 0;
87     }
88 
89     /**
90      * @return the count of {@link SuggestionsInfo}s this instance holds.
91      */
getSuggestionsCount()92     public int getSuggestionsCount() {
93         return mSuggestionsInfos.length;
94     }
95 
96     /**
97      * @param i the id of {@link SuggestionsInfo}s this instance holds.
98      * @return a {@link SuggestionsInfo} at the specified id
99      */
getSuggestionsInfoAt(int i)100     public SuggestionsInfo getSuggestionsInfoAt(int i) {
101         if (i >= 0 && i < mSuggestionsInfos.length) {
102             return mSuggestionsInfos[i];
103         }
104         return null;
105     }
106 
107     /**
108      * @param i the id of {@link SuggestionsInfo}s this instance holds
109      * @return the offset of the specified {@link SuggestionsInfo}
110      */
getOffsetAt(int i)111     public int getOffsetAt(int i) {
112         if (i >= 0 && i < mOffsets.length) {
113             return mOffsets[i];
114         }
115         return -1;
116     }
117 
118     /**
119      * @param i the id of {@link SuggestionsInfo}s this instance holds
120      * @return the length of the specified {@link SuggestionsInfo}
121      */
getLengthAt(int i)122     public int getLengthAt(int i) {
123         if (i >= 0 && i < mLengths.length) {
124             return mLengths[i];
125         }
126         return -1;
127     }
128 
129     /**
130      * Used to make this class parcelable.
131      */
132     public static final @android.annotation.NonNull Parcelable.Creator<SentenceSuggestionsInfo> CREATOR
133             = new Parcelable.Creator<SentenceSuggestionsInfo>() {
134         @Override
135         public SentenceSuggestionsInfo createFromParcel(Parcel source) {
136             return new SentenceSuggestionsInfo(source);
137         }
138 
139         @Override
140         public SentenceSuggestionsInfo[] newArray(int size) {
141             return new SentenceSuggestionsInfo[size];
142         }
143     };
144 }
145