diff options
author | Yohei Yukawa <yukawa@google.com> | 2014-06-25 19:46:47 +0900 |
---|---|---|
committer | Yohei Yukawa <yukawa@google.com> | 2014-06-30 09:42:18 +0900 |
commit | 5d6b6f2892c90e95ef3bc650a245a5f2ca021d38 (patch) | |
tree | 50b7c124d3424ab13c203ba7a470fde7d46fa92c /core/java/android/view/textservice | |
parent | 9c5ac7aef16d26bfa9d8f4b9bd1fec4c5b93b69f (diff) | |
download | frameworks_base-5d6b6f2892c90e95ef3bc650a245a5f2ca021d38.zip frameworks_base-5d6b6f2892c90e95ef3bc650a245a5f2ca021d38.tar.gz frameworks_base-5d6b6f2892c90e95ef3bc650a245a5f2ca021d38.tar.bz2 |
Allow TextInfo to store ParcelableSpans for TextService
This CL adds a new constructor of TextService so that spell
parcelable spans can be preserved in TextInfo. The primary usage
scenario is to provide locale information for spell checker
service.
BUG: 15869548
Change-Id: Ib58ece68dee4c6187d469049fca8c35a307c7a01
Diffstat (limited to 'core/java/android/view/textservice')
-rw-r--r-- | core/java/android/view/textservice/TextInfo.java | 67 |
1 files changed, 52 insertions, 15 deletions
diff --git a/core/java/android/view/textservice/TextInfo.java b/core/java/android/view/textservice/TextInfo.java index b534eb0..55fc048 100644 --- a/core/java/android/view/textservice/TextInfo.java +++ b/core/java/android/view/textservice/TextInfo.java @@ -18,43 +18,69 @@ package android.view.textservice; import android.os.Parcel; import android.os.Parcelable; +import android.text.ParcelableSpan; +import android.text.SpannableStringBuilder; import android.text.TextUtils; +import android.text.style.SpellCheckSpan; /** * This class contains a metadata of the input of TextService */ public final class TextInfo implements Parcelable { - private final String mText; + private final CharSequence mCharSequence; private final int mCookie; - private final int mSequence; + private final int mSequenceNumber; /** * Constructor. * @param text the text which will be input to TextService */ public TextInfo(String text) { - this(text, 0, 0); + this(text, 0, 0, 0, 0); } /** * Constructor. * @param text the text which will be input to TextService * @param cookie the cookie for this TextInfo - * @param sequence the sequence number for this TextInfo + * @param sequenceNumber the sequence number for this TextInfo */ - public TextInfo(String text, int cookie, int sequence) { - if (TextUtils.isEmpty(text)) { - throw new IllegalArgumentException(text); + public TextInfo(String text, int cookie, int sequenceNumber) { + this(text, 0, 0, 0, 0); + } + + /** + * Constructor. + * @param charSequence the text which will be input to TextService. Attached spans that + * implement {@link ParcelableSpan} will also be marshaled alongside with the text. + * @param start the beginning of the range of text (inclusive). + * @param end the end of the range of text (exclusive). + * @param cookie the cookie for this TextInfo + * @param sequenceNumber the sequence number for this TextInfo + */ + public TextInfo(CharSequence charSequence, int start, int end, int cookie, int sequenceNumber) { + if (TextUtils.isEmpty(charSequence)) { + throw new IllegalArgumentException("charSequence is empty"); + } + // Create a snapshot of the text including spans in case they are updated outside later. + final SpannableStringBuilder spannableString = + new SpannableStringBuilder(charSequence, start, end); + // SpellCheckSpan is for internal use. We do not want to marshal this for TextService. + final SpellCheckSpan[] spans = spannableString.getSpans(0, spannableString.length(), + SpellCheckSpan.class); + for (int i = 0; i < spans.length; ++i) { + spannableString.removeSpan(spans[i]); } - mText = text; + + mCharSequence = spannableString; mCookie = cookie; - mSequence = sequence; + mSequenceNumber = sequenceNumber; } public TextInfo(Parcel source) { - mText = source.readString(); + mCharSequence = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); mCookie = source.readInt(); - mSequence = source.readInt(); + mSequenceNumber = source.readInt(); } /** @@ -65,16 +91,27 @@ public final class TextInfo implements Parcelable { */ @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeString(mText); + TextUtils.writeToParcel(mCharSequence, dest, flags); dest.writeInt(mCookie); - dest.writeInt(mSequence); + dest.writeInt(mSequenceNumber); } /** * @return the text which is an input of a text service */ public String getText() { - return mText; + if (mCharSequence == null) { + return null; + } + return mCharSequence.toString(); + } + + /** + * @return the charSequence which is an input of a text service. This may have some parcelable + * spans. + */ + public CharSequence getCharSequence() { + return mCharSequence; } /** @@ -88,7 +125,7 @@ public final class TextInfo implements Parcelable { * @return the sequence of TextInfo */ public int getSequence() { - return mSequence; + return mSequenceNumber; } /** |