blob: eee8a62fe11c2ee65f6250f3dce4eb04fd9482d5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007-2008 The Android Open Source Project
Jean Chalarde811de22013-05-24 08:06:28 +09003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * 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
Jean Chalarde811de22013-05-24 08:06:28 +09007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Jean Chalarde811de22013-05-24 08:06:28 +09009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * 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.
Jean Chalarde811de22013-05-24 08:06:28 +090026 *
27 * <p>This class encapsulates a completion offered by an application
28 * that wants it to be presented to the user by the IME. Usually, apps
29 * present their completions directly in a scrolling list for example
30 * (UI developers will usually use or extend
31 * {@see android.widget.AutoCompleteTextView} to implement this).
32 * However, in some cases, the editor may not be visible, as in the
33 * case in extract mode where the IME has taken over the full
34 * screen. In this case, the editor can choose to send their
35 * completions to the IME for display.
36 *
37 * <p>Most applications who want to send completions to an IME should use
38 * {@link android.widget.AutoCompleteTextView} as this class makes this
39 * process easy. In this case, the application would not have to deal directly
40 * with this class.
41 *
42 * <p>An application who implements its own editor and wants direct control
43 * over this would create an array of CompletionInfo objects, and send it to the IME using
44 * {@link InputMethodManager#displayCompletions(View, CompletionInfo[])}.
45 * The IME would present the completions however they see fit, and
46 * call back to the editor through
47 * {@link InputConnection#commitCompletion(CompletionInfo)}.
48 * The application can then pick up the commit event by overriding
49 * {@link android.widget.TextView#onCommitCompletion(CompletionInfo)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 */
51public final class CompletionInfo implements Parcelable {
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080052 private final long mId;
53 private final int mPosition;
54 private final CharSequence mText;
55 private final CharSequence mLabel;
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 /**
58 * Create a simple completion with just text, no label.
Jean Chalarde811de22013-05-24 08:06:28 +090059 *
60 * @param id An id that get passed as is (to the editor's discretion)
61 * @param index An index that get passed as is. Typically this is the
62 * index in the list of completions inside the editor.
63 * @param text The text that should be inserted into the editor when
64 * this completion is chosen.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
66 public CompletionInfo(long id, int index, CharSequence text) {
67 mId = id;
68 mPosition = index;
69 mText = text;
70 mLabel = null;
71 }
72
73 /**
Jean Chalarde811de22013-05-24 08:06:28 +090074 * Create a full completion with both text and label. The text is
75 * what will get inserted into the editor, while the label is what
76 * the IME should display. If they are the same, use the version
77 * of the constructor without a `label' argument.
78 *
79 * @param id An id that get passed as is (to the editor's discretion)
80 * @param index An index that get passed as is. Typically this is the
81 * index in the list of completions inside the editor.
82 * @param text The text that should be inserted into the editor when
83 * this completion is chosen.
84 * @param label The text that the IME should be showing among the
85 * completions list.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 */
87 public CompletionInfo(long id, int index, CharSequence text, CharSequence label) {
88 mId = id;
89 mPosition = index;
90 mText = text;
91 mLabel = label;
92 }
93
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -080094 private CompletionInfo(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 mId = source.readLong();
96 mPosition = source.readInt();
97 mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
98 mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
99 }
Jean Chalarde811de22013-05-24 08:06:28 +0900100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
102 * Return the abstract identifier for this completion, typically
103 * corresponding to the id associated with it in the original adapter.
104 */
105 public long getId() {
106 return mId;
107 }
Jean Chalarde811de22013-05-24 08:06:28 +0900108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
110 * Return the original position of this completion, typically
111 * corresponding to its position in the original adapter.
112 */
113 public int getPosition() {
114 return mPosition;
115 }
Jean Chalarde811de22013-05-24 08:06:28 +0900116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
118 * Return the actual text associated with this completion. This is the
119 * real text that will be inserted into the editor if the user selects it.
120 */
121 public CharSequence getText() {
122 return mText;
123 }
Jean Chalarde811de22013-05-24 08:06:28 +0900124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
126 * Return the user-visible label for the completion, or null if the plain
127 * text should be shown. If non-null, this will be what the user sees as
128 * the completion option instead of the actual text.
129 */
130 public CharSequence getLabel() {
131 return mLabel;
132 }
Jean Chalarde811de22013-05-24 08:06:28 +0900133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 @Override
135 public String toString() {
136 return "CompletionInfo{#" + mPosition + " \"" + mText
137 + "\" id=" + mId + " label=" + mLabel + "}";
138 }
139
140 /**
141 * Used to package this object into a {@link Parcel}.
Gilles Debunnecf9cf2f2010-12-08 17:43:58 -0800142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * @param dest The {@link Parcel} to be written.
144 * @param flags The flags used for parceling.
145 */
146 public void writeToParcel(Parcel dest, int flags) {
147 dest.writeLong(mId);
148 dest.writeInt(mPosition);
149 TextUtils.writeToParcel(mText, dest, flags);
150 TextUtils.writeToParcel(mLabel, dest, flags);
151 }
152
153 /**
154 * Used to make this class parcelable.
155 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700156 public static final @android.annotation.NonNull Parcelable.Creator<CompletionInfo> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 = new Parcelable.Creator<CompletionInfo>() {
158 public CompletionInfo createFromParcel(Parcel source) {
159 return new CompletionInfo(source);
160 }
161
162 public CompletionInfo[] newArray(int size) {
163 return new CompletionInfo[size];
164 }
165 };
166
167 public int describeContents() {
168 return 0;
169 }
170}