1 /*
2  * Copyright (C) 2007-2010 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.inputmethod;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.text.TextUtils;
22 
23 /**
24  * Information about a single text correction that an editor has reported to
25  * an input method.
26  */
27 public final class CorrectionInfo implements Parcelable {
28     private final int mOffset;
29     private final CharSequence mOldText;
30     private final CharSequence mNewText;
31 
32     /**
33      * @param offset The offset in the edited text where the old and new text start.
34      * @param oldText The old text that has been replaced.
35      * @param newText The replacement text.
36      */
CorrectionInfo(int offset, CharSequence oldText, CharSequence newText)37     public CorrectionInfo(int offset, CharSequence oldText, CharSequence newText) {
38         mOffset = offset;
39         mOldText = oldText;
40         mNewText = newText;
41     }
42 
CorrectionInfo(Parcel source)43     private CorrectionInfo(Parcel source) {
44         mOffset = source.readInt();
45         mOldText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
46         mNewText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
47     }
48 
49     /**
50      * Return the offset position of this correction in the text. Both the {@link #getOldText()} and
51      * {@link #getNewText()} start at this offset.
52      */
getOffset()53     public int getOffset() {
54         return mOffset;
55     }
56 
57     /**
58      * Return the text that has actually been typed by the user, and which has been corrected.
59      */
getOldText()60     public CharSequence getOldText() {
61         return mOldText;
62     }
63 
64     /**
65      * Return the new text that corrects what was typed by the user.
66      */
getNewText()67     public CharSequence getNewText() {
68         return mNewText;
69     }
70 
71     @Override
toString()72     public String toString() {
73         return "CorrectionInfo{#" + mOffset + " \"" + mOldText + "\" -> \"" + mNewText + "\"}";
74     }
75 
76     /**
77      * Used to package this object into a {@link Parcel}.
78      *
79      * @param dest The {@link Parcel} to be written.
80      * @param flags The flags used for parceling.
81      */
writeToParcel(Parcel dest, int flags)82     public void writeToParcel(Parcel dest, int flags) {
83         dest.writeInt(mOffset);
84         TextUtils.writeToParcel(mOldText, dest, flags);
85         TextUtils.writeToParcel(mNewText, dest, flags);
86     }
87 
88     /**
89      * Used to make this class parcelable.
90      */
91     public static final @android.annotation.NonNull Parcelable.Creator<CorrectionInfo> CREATOR =
92             new Parcelable.Creator<CorrectionInfo>() {
93                 public CorrectionInfo createFromParcel(Parcel source) {
94                     return new CorrectionInfo(source);
95                 }
96                 public CorrectionInfo[] newArray(int size) {
97                     return new CorrectionInfo[size];
98                 }
99             };
100 
describeContents()101     public int describeContents() {
102         return 0;
103     }
104 }
105