blob: 5499918a8c05d239622f9c791e5f2bf9a272b22c [file] [log] [blame]
satok988323c2011-06-22 16:38:13 +09001/*
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
17package android.view.textservice;
18
19import android.os.Parcel;
20import android.os.Parcelable;
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090021import android.text.ParcelableSpan;
22import android.text.SpannableStringBuilder;
satok988323c2011-06-22 16:38:13 +090023import android.text.TextUtils;
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090024import android.text.style.SpellCheckSpan;
satok988323c2011-06-22 16:38:13 +090025
26/**
27 * This class contains a metadata of the input of TextService
28 */
29public final class TextInfo implements Parcelable {
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090030 private final CharSequence mCharSequence;
satok988323c2011-06-22 16:38:13 +090031 private final int mCookie;
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090032 private final int mSequenceNumber;
satok988323c2011-06-22 16:38:13 +090033
Yohei Yukawabf655982014-07-09 22:15:19 +090034 private static final int DEFAULT_COOKIE = 0;
35 private static final int DEFAULT_SEQUENCE_NUMBER = 0;
36
satok988323c2011-06-22 16:38:13 +090037 /**
38 * Constructor.
39 * @param text the text which will be input to TextService
40 */
41 public TextInfo(String text) {
Yohei Yukawabf655982014-07-09 22:15:19 +090042 this(text, 0, getStringLengthOrZero(text), DEFAULT_COOKIE, DEFAULT_SEQUENCE_NUMBER);
satok988323c2011-06-22 16:38:13 +090043 }
44
45 /**
46 * Constructor.
47 * @param text the text which will be input to TextService
48 * @param cookie the cookie for this TextInfo
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090049 * @param sequenceNumber the sequence number for this TextInfo
satok988323c2011-06-22 16:38:13 +090050 */
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090051 public TextInfo(String text, int cookie, int sequenceNumber) {
Yohei Yukawabf655982014-07-09 22:15:19 +090052 this(text, 0, getStringLengthOrZero(text), cookie, sequenceNumber);
53 }
54
55 private static int getStringLengthOrZero(final String text) {
56 return TextUtils.isEmpty(text) ? 0 : text.length();
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090057 }
58
59 /**
60 * Constructor.
61 * @param charSequence the text which will be input to TextService. Attached spans that
62 * implement {@link ParcelableSpan} will also be marshaled alongside with the text.
63 * @param start the beginning of the range of text (inclusive).
64 * @param end the end of the range of text (exclusive).
65 * @param cookie the cookie for this TextInfo
66 * @param sequenceNumber the sequence number for this TextInfo
67 */
68 public TextInfo(CharSequence charSequence, int start, int end, int cookie, int sequenceNumber) {
69 if (TextUtils.isEmpty(charSequence)) {
70 throw new IllegalArgumentException("charSequence is empty");
satok988323c2011-06-22 16:38:13 +090071 }
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090072 // Create a snapshot of the text including spans in case they are updated outside later.
73 final SpannableStringBuilder spannableString =
74 new SpannableStringBuilder(charSequence, start, end);
75 // SpellCheckSpan is for internal use. We do not want to marshal this for TextService.
76 final SpellCheckSpan[] spans = spannableString.getSpans(0, spannableString.length(),
77 SpellCheckSpan.class);
78 for (int i = 0; i < spans.length; ++i) {
79 spannableString.removeSpan(spans[i]);
80 }
81
82 mCharSequence = spannableString;
satok988323c2011-06-22 16:38:13 +090083 mCookie = cookie;
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090084 mSequenceNumber = sequenceNumber;
satok988323c2011-06-22 16:38:13 +090085 }
86
87 public TextInfo(Parcel source) {
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090088 mCharSequence = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
satok988323c2011-06-22 16:38:13 +090089 mCookie = source.readInt();
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +090090 mSequenceNumber = source.readInt();
satok988323c2011-06-22 16:38:13 +090091 }
92
93 /**
94 * Used to package this object into a {@link Parcel}.
95 *
96 * @param dest The {@link Parcel} to be written.
97 * @param flags The flags used for parceling.
98 */
99 @Override
100 public void writeToParcel(Parcel dest, int flags) {
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +0900101 TextUtils.writeToParcel(mCharSequence, dest, flags);
satok988323c2011-06-22 16:38:13 +0900102 dest.writeInt(mCookie);
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +0900103 dest.writeInt(mSequenceNumber);
satok988323c2011-06-22 16:38:13 +0900104 }
105
106 /**
107 * @return the text which is an input of a text service
108 */
109 public String getText() {
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +0900110 if (mCharSequence == null) {
111 return null;
112 }
113 return mCharSequence.toString();
114 }
115
116 /**
117 * @return the charSequence which is an input of a text service. This may have some parcelable
118 * spans.
119 */
120 public CharSequence getCharSequence() {
121 return mCharSequence;
satok988323c2011-06-22 16:38:13 +0900122 }
123
124 /**
125 * @return the cookie of TextInfo
126 */
127 public int getCookie() {
128 return mCookie;
129 }
130
131 /**
132 * @return the sequence of TextInfo
133 */
134 public int getSequence() {
Yohei Yukawa5d6b6f22014-06-25 19:46:47 +0900135 return mSequenceNumber;
satok988323c2011-06-22 16:38:13 +0900136 }
137
138 /**
139 * Used to make this class parcelable.
140 */
141 public static final Parcelable.Creator<TextInfo> CREATOR
142 = new Parcelable.Creator<TextInfo>() {
143 @Override
144 public TextInfo createFromParcel(Parcel source) {
145 return new TextInfo(source);
146 }
147
148 @Override
149 public TextInfo[] newArray(int size) {
150 return new TextInfo[size];
151 }
152 };
153
154 /**
155 * Used to make this class parcelable.
156 */
157 @Override
158 public int describeContents() {
159 return 0;
160 }
161}