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