SentenceSuggestionsInfo.java revision d404fe110558bd2e1960b428db6a2ee8bfd040cd
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
17package android.view.textservice;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Arrays;
23
24/**
25 * @hide
26 * This class contains a metadata of sentence level suggestions from the text service
27 */
28public final class SentenceSuggestionsInfo implements Parcelable {
29
30    private final SuggestionsInfo[] mSuggestionsInfos;
31    private final int[] mOffsets;
32    private final int[] mLengths;
33
34    /**
35     * Constructor.
36     * @param suggestionsInfos from the text service
37     * @param offsets the array of offsets of suggestions
38     * @param lengths the array of lengths of suggestions
39     */
40    public SentenceSuggestionsInfo(
41            SuggestionsInfo[] suggestionsInfos, int[] offsets, int[] lengths) {
42        if (suggestionsInfos == null || offsets == null || lengths == null) {
43            throw new NullPointerException();
44        }
45        if (suggestionsInfos.length != offsets.length || offsets.length != lengths.length) {
46            throw new IllegalArgumentException();
47        }
48        final int infoSize = suggestionsInfos.length;
49        mSuggestionsInfos = Arrays.copyOf(suggestionsInfos, infoSize);
50        mOffsets = Arrays.copyOf(offsets, infoSize);
51        mLengths = Arrays.copyOf(lengths, infoSize);
52    }
53
54    public SentenceSuggestionsInfo(Parcel source) {
55        final int infoSize = source.readInt();
56        mSuggestionsInfos = new SuggestionsInfo[infoSize];
57        source.readTypedArray(mSuggestionsInfos, SuggestionsInfo.CREATOR);
58        mOffsets = new int[mSuggestionsInfos.length];
59        source.readIntArray(mOffsets);
60        mLengths = new int[mSuggestionsInfos.length];
61        source.readIntArray(mLengths);
62    }
63
64    /**
65     * Used to package this object into a {@link Parcel}.
66     *
67     * @param dest The {@link Parcel} to be written.
68     * @param flags The flags used for parceling.
69     */
70    @Override
71    public void writeToParcel(Parcel dest, int flags) {
72        final int infoSize = mSuggestionsInfos.length;
73        dest.writeInt(infoSize);
74        dest.writeTypedArray(mSuggestionsInfos, 0);
75        dest.writeIntArray(mOffsets);
76        dest.writeIntArray(mLengths);
77    }
78
79    @Override
80    public int describeContents() {
81        return 0;
82    }
83
84    /**
85     * @hide
86     */
87    public SuggestionsInfo getSuggestionsInfoAt(int i) {
88        if (i >= 0 && i < mSuggestionsInfos.length) {
89            return mSuggestionsInfos[i];
90        }
91        return null;
92    }
93
94    /**
95     * @hide
96     */
97    public int getOffsetAt(int i) {
98        if (i >= 0 && i < mOffsets.length) {
99            return mOffsets[i];
100        }
101        return -1;
102    }
103
104    /**
105     * @hide
106     */
107    public int getLengthAt(int i) {
108        if (i >= 0 && i < mLengths.length) {
109            return mLengths[i];
110        }
111        return -1;
112    }
113
114    /**
115     * Used to make this class parcelable.
116     */
117    public static final Parcelable.Creator<SentenceSuggestionsInfo> CREATOR
118            = new Parcelable.Creator<SentenceSuggestionsInfo>() {
119        @Override
120        public SentenceSuggestionsInfo createFromParcel(Parcel source) {
121            return new SentenceSuggestionsInfo(source);
122        }
123
124        @Override
125        public SentenceSuggestionsInfo[] newArray(int size) {
126            return new SentenceSuggestionsInfo[size];
127        }
128    };
129}
130