blob: 0d37632beb7e11c5edfde80f4172b369cd47485c [file] [log] [blame]
satokd404fe12012-02-22 06:38:18 +09001/*
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
17package android.view.textservice;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Arrays;
23
24/**
satok6183cd62012-03-27 12:08:29 +090025 * 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.
satokd404fe12012-02-22 06:38:18 +090032 */
33public 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 */
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
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
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
85 public int describeContents() {
86 return 0;
87 }
88
89 /**
satok6183cd62012-03-27 12:08:29 +090090 * @return the count of {@link SuggestionsInfo}s this instance holds.
satokd404fe12012-02-22 06:38:18 +090091 */
satok88983582011-11-30 15:38:30 +090092 public int getSuggestionsCount() {
93 return mSuggestionsInfos.length;
94 }
95
96 /**
satok6183cd62012-03-27 12:08:29 +090097 * @param i the id of {@link SuggestionsInfo}s this instance holds.
98 * @return a {@link SuggestionsInfo} at the specified id
satok88983582011-11-30 15:38:30 +090099 */
satokd404fe12012-02-22 06:38:18 +0900100 public SuggestionsInfo getSuggestionsInfoAt(int i) {
101 if (i >= 0 && i < mSuggestionsInfos.length) {
102 return mSuggestionsInfos[i];
103 }
104 return null;
105 }
106
107 /**
satok6183cd62012-03-27 12:08:29 +0900108 * @param i the id of {@link SuggestionsInfo}s this instance holds
109 * @return the offset of the specified {@link SuggestionsInfo}
satokd404fe12012-02-22 06:38:18 +0900110 */
111 public int getOffsetAt(int i) {
112 if (i >= 0 && i < mOffsets.length) {
113 return mOffsets[i];
114 }
115 return -1;
116 }
117
118 /**
satok6183cd62012-03-27 12:08:29 +0900119 * @param i the id of {@link SuggestionsInfo}s this instance holds
120 * @return the length of the specified {@link SuggestionsInfo}
satokd404fe12012-02-22 06:38:18 +0900121 */
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 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700132 public static final @android.annotation.NonNull Parcelable.Creator<SentenceSuggestionsInfo> CREATOR
satokd404fe12012-02-22 06:38:18 +0900133 = 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}