1 /* 2 * Copyright (C) 2007-2008 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 completion that an editor has reported to 25 * an input method. 26 */ 27 public final class CompletionInfo implements Parcelable { 28 static final String TAG = "CompletionInfo"; 29 30 final long mId; 31 final int mPosition; 32 final CharSequence mText; 33 final CharSequence mLabel; 34 35 /** 36 * Create a simple completion with just text, no label. 37 */ 38 public CompletionInfo(long id, int index, CharSequence text) { 39 mId = id; 40 mPosition = index; 41 mText = text; 42 mLabel = null; 43 } 44 45 /** 46 * Create a full completion with both text and label. 47 */ 48 public CompletionInfo(long id, int index, CharSequence text, CharSequence label) { 49 mId = id; 50 mPosition = index; 51 mText = text; 52 mLabel = label; 53 } 54 55 CompletionInfo(Parcel source) { 56 mId = source.readLong(); 57 mPosition = source.readInt(); 58 mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 59 mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 60 } 61 62 /** 63 * Return the abstract identifier for this completion, typically 64 * corresponding to the id associated with it in the original adapter. 65 */ 66 public long getId() { 67 return mId; 68 } 69 70 /** 71 * Return the original position of this completion, typically 72 * corresponding to its position in the original adapter. 73 */ 74 public int getPosition() { 75 return mPosition; 76 } 77 78 /** 79 * Return the actual text associated with this completion. This is the 80 * real text that will be inserted into the editor if the user selects it. 81 */ 82 public CharSequence getText() { 83 return mText; 84 } 85 86 /** 87 * Return the user-visible label for the completion, or null if the plain 88 * text should be shown. If non-null, this will be what the user sees as 89 * the completion option instead of the actual text. 90 */ 91 public CharSequence getLabel() { 92 return mLabel; 93 } 94 95 @Override 96 public String toString() { 97 return "CompletionInfo{#" + mPosition + " \"" + mText 98 + "\" id=" + mId + " label=" + mLabel + "}"; 99 } 100 101 /** 102 * Used to package this object into a {@link Parcel}. 103 * 104 * @param dest The {@link Parcel} to be written. 105 * @param flags The flags used for parceling. 106 */ 107 public void writeToParcel(Parcel dest, int flags) { 108 dest.writeLong(mId); 109 dest.writeInt(mPosition); 110 TextUtils.writeToParcel(mText, dest, flags); 111 TextUtils.writeToParcel(mLabel, dest, flags); 112 } 113 114 /** 115 * Used to make this class parcelable. 116 */ 117 public static final Parcelable.Creator<CompletionInfo> CREATOR 118 = new Parcelable.Creator<CompletionInfo>() { 119 public CompletionInfo createFromParcel(Parcel source) { 120 return new CompletionInfo(source); 121 } 122 123 public CompletionInfo[] newArray(int size) { 124 return new CompletionInfo[size]; 125 } 126 }; 127 128 public int describeContents() { 129 return 0; 130 } 131 } 132