Debian Codegraph @

core/java/android/view/textservice/TextInfo.java

raw symbols SCIP
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 android.os.Parcel;
20 import android.os.Parcelable;
21 import android.text.ParcelableSpan;
22 import android.text.SpannableStringBuilder;
23 import android.text.TextUtils;
24 import android.text.style.SpellCheckSpan;
25
26 /**
27 * This class contains a metadata of the input of TextService
28 */
29 public final class TextInfo implements Parcelable {
30 private final CharSequence mCharSequence;
31 private final int mCookie;
32 private final int mSequenceNumber;
33
34 private static final int DEFAULT_COOKIE = 0;
35 private static final int DEFAULT_SEQUENCE_NUMBER = 0;
36
37 /**
38 * Constructor.
39 * @param text the text which will be input to TextService
40 */
41 public TextInfo(String text) {
42 this(text, 0, getStringLengthOrZero(text), DEFAULT_COOKIE, DEFAULT_SEQUENCE_NUMBER);
43 }
44
45 /**
46 * Constructor.
47 * @param text the text which will be input to TextService
48 * @param cookie the cookie for this TextInfo
49 * @param sequenceNumber the sequence number for this TextInfo
50 */
51 public TextInfo(String text, int cookie, int sequenceNumber) {
52 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();
57 }
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");
71 }
72 // 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;
83 mCookie = cookie;
84 mSequenceNumber = sequenceNumber;
85 }
86
87 public TextInfo(Parcel source) {
88 mCharSequence = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
89 mCookie = source.readInt();
90 mSequenceNumber = source.readInt();
91 }
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) {
101 TextUtils.writeToParcel(mCharSequence, dest, flags);
102 dest.writeInt(mCookie);
103 dest.writeInt(mSequenceNumber);
104 }
105
106 /**
107 * @return the text which is an input of a text service
108 */
109 public String getText() {
110 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;
122 }
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() {
135 return mSequenceNumber;
136 }
137
138 /**
139 * Used to make this class parcelable.
140 */
141 public static final @android.annotation.NonNull 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 }