satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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; |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 21 | import android.text.ParcelableSpan; |
| 22 | import android.text.SpannableStringBuilder; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 23 | import android.text.TextUtils; |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 24 | import android.text.style.SpellCheckSpan; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * This class contains a metadata of the input of TextService |
| 28 | */ |
| 29 | public final class TextInfo implements Parcelable { |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 30 | private final CharSequence mCharSequence; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 31 | private final int mCookie; |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 32 | private final int mSequenceNumber; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 33 | |
Yohei Yukawa | bf65598 | 2014-07-09 22:15:19 +0900 | [diff] [blame] | 34 | private static final int DEFAULT_COOKIE = 0; |
| 35 | private static final int DEFAULT_SEQUENCE_NUMBER = 0; |
| 36 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 37 | /** |
| 38 | * Constructor. |
| 39 | * @param text the text which will be input to TextService |
| 40 | */ |
| 41 | public TextInfo(String text) { |
Yohei Yukawa | bf65598 | 2014-07-09 22:15:19 +0900 | [diff] [blame] | 42 | this(text, 0, getStringLengthOrZero(text), DEFAULT_COOKIE, DEFAULT_SEQUENCE_NUMBER); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Constructor. |
| 47 | * @param text the text which will be input to TextService |
| 48 | * @param cookie the cookie for this TextInfo |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 49 | * @param sequenceNumber the sequence number for this TextInfo |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 50 | */ |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 51 | public TextInfo(String text, int cookie, int sequenceNumber) { |
Yohei Yukawa | bf65598 | 2014-07-09 22:15:19 +0900 | [diff] [blame] | 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(); |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 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"); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 71 | } |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 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; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 83 | mCookie = cookie; |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 84 | mSequenceNumber = sequenceNumber; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | public TextInfo(Parcel source) { |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 88 | mCharSequence = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 89 | mCookie = source.readInt(); |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 90 | mSequenceNumber = source.readInt(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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) { |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 101 | TextUtils.writeToParcel(mCharSequence, dest, flags); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 102 | dest.writeInt(mCookie); |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 103 | dest.writeInt(mSequenceNumber); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return the text which is an input of a text service |
| 108 | */ |
| 109 | public String getText() { |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 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; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 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() { |
Yohei Yukawa | 5d6b6f2 | 2014-06-25 19:46:47 +0900 | [diff] [blame] | 135 | return mSequenceNumber; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 136 | } |
| 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 | } |