InputConnection.java revision 3dec7d563a2f3e1eb967ce2054a00b6620e3558c
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.Bundle;
20import android.text.Spanned;
21import android.view.KeyCharacterMap;
22import android.view.KeyEvent;
23
24/**
25 * The InputConnection interface is the communication channel from an
26 * {@link InputMethod} back to the application that is receiving its input. It
27 * is used to perform such things as reading text around the cursor,
28 * committing text to the text box, and sending raw key events to the application.
29 *
30 * <p>Implementations of this interface should generally be done by
31 * subclassing {@link BaseInputConnection}.
32 */
33public interface InputConnection {
34    /**
35     * Flag for use with {@link #getTextAfterCursor} and
36     * {@link #getTextBeforeCursor} to have style information returned along
37     * with the text.  If not set, you will receive only the raw text.  If
38     * set, you may receive a complex CharSequence of both text and style
39     * spans.
40     */
41    static final int GET_TEXT_WITH_STYLES = 0x0001;
42
43    /**
44     * Flag for use with {@link #getExtractedText} to indicate you would
45     * like to receive updates when the extracted text changes.
46     */
47    public static final int GET_EXTRACTED_TEXT_MONITOR = 0x0001;
48
49    /**
50     * Get <var>n</var> characters of text before the current cursor position.
51     *
52     * <p>This method may fail either if the input connection has become invalid
53     * (such as its process crashing) or the client is taking too long to
54     * respond with the text (it is given a couple seconds to return).
55     * In either case, a null is returned.
56     *
57     * @param n The expected length of the text.
58     * @param flags Supplies additional options controlling how the text is
59     * returned.  May be either 0 or {@link #GET_TEXT_WITH_STYLES}.
60     *
61     * @return Returns the text before the cursor position; the length of the
62     * returned text might be less than <var>n</var>.
63     */
64    public CharSequence getTextBeforeCursor(int n, int flags);
65
66    /**
67     * Get <var>n</var> characters of text after the current cursor position.
68     *
69     * <p>This method may fail either if the input connection has become invalid
70     * (such as its process crashing) or the client is taking too long to
71     * respond with the text (it is given a couple seconds to return).
72     * In either case, a null is returned.
73     *
74     * @param n The expected length of the text.
75     * @param flags Supplies additional options controlling how the text is
76     * returned.  May be either 0 or {@link #GET_TEXT_WITH_STYLES}.
77     *
78     * @return Returns the text after the cursor position; the length of the
79     * returned text might be less than <var>n</var>.
80     */
81    public CharSequence getTextAfterCursor(int n, int flags);
82
83    /**
84     * Retrieve the current capitalization mode in effect at the current
85     * cursor position in the text.  See
86     * {@link android.text.TextUtils#getCapsMode TextUtils.getCapsMode} for
87     * more information.
88     *
89     * <p>This method may fail either if the input connection has become invalid
90     * (such as its process crashing) or the client is taking too long to
91     * respond with the text (it is given a couple seconds to return).
92     * In either case, a 0 is returned.
93     *
94     * @param reqModes The desired modes to retrieve, as defined by
95     * {@link android.text.TextUtils#getCapsMode TextUtils.getCapsMode}.  These
96     * constants are defined so that you can simply pass the current
97     * {@link EditorInfo#inputType TextBoxAttribute.contentType} value
98     * directly in to here.
99     *
100     * @return Returns the caps mode flags that are in effect.
101     */
102    public int getCursorCapsMode(int reqModes);
103
104    /**
105     * Retrieve the current text in the input connection's editor, and monitor
106     * for any changes to it.  This function returns with the current text,
107     * and optionally the input connection can send updates to the
108     * input method when its text changes.
109     *
110     * <p>This method may fail either if the input connection has become invalid
111     * (such as its process crashing) or the client is taking too long to
112     * respond with the text (it is given a couple seconds to return).
113     * In either case, a null is returned.
114     *
115     * @param request Description of how the text should be returned.
116     * @param flags Additional options to control the client, either 0 or
117     * {@link #GET_EXTRACTED_TEXT_MONITOR}.
118     *
119     * @return Returns an ExtractedText object describing the state of the
120     * text view and containing the extracted text itself.
121     */
122    public ExtractedText getExtractedText(ExtractedTextRequest request,
123            int flags);
124
125    /**
126     * Delete <var>leftLength</var> characters of text before the current cursor
127     * position, and delete <var>rightLength</var> characters of text after the
128     * current cursor position, excluding composing text.
129     *
130     * @param leftLength The number of characters to be deleted before the
131     *        current cursor position.
132     * @param rightLength The number of characters to be deleted after the
133     *        current cursor position.
134     *
135     * @return Returns true on success, false if the input connection is no longer
136     * valid.
137     */
138    boolean deleteSurroundingText(int leftLength, int rightLength);
139
140    /**
141     * Set composing text around the current cursor position with the given text,
142     * and set the new cursor position.  Any composing text set previously will
143     * be removed automatically.
144     *
145     * @param text The composing text with styles if necessary. If no style
146     *        object attached to the text, the default style for composing text
147     *        is used. See {#link android.text.Spanned} for how to attach style
148     *        object to the text. {#link android.text.SpannableString} and
149     *        {#link android.text.SpannableStringBuilder} are two
150     *        implementations of the interface {#link android.text.Spanned}.
151     * @param newCursorPosition The new cursor position around the text.  If
152     *        > 0, this is relative to the end of the text - 1; if <= 0, this
153     *        is relative to the start of the text.  So a value of 1 will
154     *        always advance you to the position after the full text being
155     *        inserted.  Note that this means you can't position the cursor
156     *        within the text, because the editor can make modifications to
157     *        the text you are providing so it is not possible to correctly
158     *        specify locations there.
159     *
160     * @return Returns true on success, false if the input connection is no longer
161     * valid.
162     */
163    public boolean setComposingText(CharSequence text, int newCursorPosition);
164
165    /**
166     * Have the text editor finish whatever composing text is currently
167     * active.  This simply leaves the text as-is, removing any special
168     * composing styling or other state that was around it.  The cursor
169     * position remains unchanged.
170     */
171    public boolean finishComposingText();
172
173    /**
174     * Commit text to the text box and set the new cursor position.
175     * Any composing text set previously will be removed
176     * automatically.
177     *
178     * @param text The committed text.
179     * @param newCursorPosition The new cursor position around the text.  If
180     *        > 0, this is relative to the end of the text - 1; if <= 0, this
181     *        is relative to the start of the text.  So a value of 1 will
182     *        always advance you to the position after the full text being
183     *        inserted.  Note that this means you can't position the cursor
184     *        within the text, because the editor can make modifications to
185     *        the text you are providing so it is not possible to correctly
186     *        specify locations there.
187     *
188     *
189     * @return Returns true on success, false if the input connection is no longer
190     * valid.
191     */
192    public boolean commitText(CharSequence text, int newCursorPosition);
193
194    /**
195     * Commit a completion the user has selected from the possible ones
196     * previously reported to {@link InputMethodSession#displayCompletions
197     * InputMethodSession.displayCompletions()}.  This will result in the
198     * same behavior as if the user had selected the completion from the
199     * actual UI.
200     *
201     * @param text The committed completion.
202     *
203     * @return Returns true on success, false if the input connection is no longer
204     * valid.
205     */
206    public boolean commitCompletion(CompletionInfo text);
207
208    /**
209     * Set the selection of the text editor.  To set the cursor position,
210     * start and end should have the same value.
211     * @return Returns true on success, false if the input connection is no longer
212     * valid.
213     */
214    public boolean setSelection(int start, int end);
215
216    /**
217     * Have the editor perform an action it has said it can do.
218     *
219     * @param editorAction This must be one of the action constants for
220     * {@link EditorInfo#imeOptions EditorInfo.editorType}, such as
221     * {@link EditorInfo#IME_ACTION_GO EditorInfo.EDITOR_ACTION_GO}.
222     *
223     * @return Returns true on success, false if the input connection is no longer
224     * valid.
225     */
226    public boolean performEditorAction(int editorAction);
227
228    /**
229     * Perform a context menu action on the field.  The given id may be one of:
230     * {@link android.R.id#selectAll},
231     * {@link android.R.id#startSelectingText}, {@link android.R.id#stopSelectingText},
232     * {@link android.R.id#cut}, {@link android.R.id#copy},
233     * {@link android.R.id#paste}, {@link android.R.id#copyUrl},
234     * or {@link android.R.id#switchInputMethod}
235     */
236    public boolean performContextMenuAction(int id);
237
238    /**
239     * Tell the editor that you are starting a batch of editor operations.
240     * The editor will try to avoid sending you updates about its state
241     * until {@link #endBatchEdit} is called.
242     */
243    public boolean beginBatchEdit();
244
245    /**
246     * Tell the editor that you are done with a batch edit previously
247     * initiated with {@link #endBatchEdit}.
248     */
249    public boolean endBatchEdit();
250
251    /**
252     * Send a key event to the process that is currently attached through
253     * this input connection.  The event will be dispatched like a normal
254     * key event, to the currently focused; this generally is the view that
255     * is providing this InputConnection, but due to the asynchronous nature
256     * of this protocol that can not be guaranteed and the focus may have
257     * changed by the time the event is received.
258     *
259     * <p>
260     * This method can be used to send key events to the application. For
261     * example, an on-screen keyboard may use this method to simulate a hardware
262     * keyboard. There are three types of standard keyboards, numeric (12-key),
263     * predictive (20-key) and ALPHA (QWERTY). You can specify the keyboard type
264     * by specify the device id of the key event.
265     *
266     * <p>
267     * You will usually want to set the flag
268     * {@link KeyEvent#FLAG_SOFT_KEYBOARD KeyEvent.FLAG_SOFT_KEYBOARD} on all
269     * key event objects you give to this API; the flag will not be set
270     * for you.
271     *
272     * @param event The key event.
273     *
274     * @return Returns true on success, false if the input connection is no longer
275     * valid.
276     *
277     * @see KeyEvent
278     * @see KeyCharacterMap#NUMERIC
279     * @see KeyCharacterMap#PREDICTIVE
280     * @see KeyCharacterMap#ALPHA
281     */
282    public boolean sendKeyEvent(KeyEvent event);
283
284    /**
285     * Clear the given meta key pressed states in the given input connection.
286     *
287     * @param states The states to be cleared, may be one or more bits as
288     * per {@link KeyEvent#getMetaState() KeyEvent.getMetaState()}.
289     *
290     * @return Returns true on success, false if the input connection is no longer
291     * valid.
292     */
293    public boolean clearMetaKeyStates(int states);
294
295    /**
296     * Called by the IME to tell the client when it switches between fullscreen
297     * and normal modes.  This will normally be called for you by the standard
298     * implementation of {@link android.inputmethodservice.InputMethodService}.
299     */
300    public boolean reportFullscreenMode(boolean enabled);
301
302    /**
303     * API to send private commands from an input method to its connected
304     * editor.  This can be used to provide domain-specific features that are
305     * only known between certain input methods and their clients.  Note that
306     * because the InputConnection protocol is asynchronous, you have no way
307     * to get a result back or know if the client understood the command; you
308     * can use the information in {@link EditorInfo} to determine if
309     * a client supports a particular command.
310     *
311     * @param action Name of the command to be performed.  This <em>must</em>
312     * be a scoped name, i.e. prefixed with a package name you own, so that
313     * different developers will not create conflicting commands.
314     * @param data Any data to include with the command.
315     * @return Returns true if the command was sent (whether or not the
316     * associated editor understood it), false if the input connection is no longer
317     * valid.
318     */
319    public boolean performPrivateCommand(String action, Bundle data);
320}
321