Android APIs
public class

BaseInputConnection

extends Object
implements InputConnection
java.lang.Object
   ↳ android.view.inputmethod.BaseInputConnection

Class Overview

Base class for implementors of the InputConnection interface, taking care of most of the common behavior for providing a connection to an Editable. Implementors of this class will want to be sure to implement getEditable() to provide access to their own editable object, and to refer to the documentation in InputConnection.

Summary

[Expand]
Inherited Constants
From interface android.view.inputmethod.InputConnection
Public Constructors
BaseInputConnection(View targetView, boolean fullEditor)
Public Methods
boolean beginBatchEdit()
Default implementation does nothing.
boolean clearMetaKeyStates(int states)
Default implementation uses MetaKeyKeyListener.clearMetaKeyState(long, int) to clear the state.
boolean commitCompletion(CompletionInfo text)
Default implementation does nothing and returns false.
boolean commitCorrection(CorrectionInfo correctionInfo)
Default implementation does nothing and returns false.
boolean commitText(CharSequence text, int newCursorPosition)
Default implementation replaces any existing composing text with the given text.
boolean deleteSurroundingText(int beforeLength, int afterLength)
The default implementation performs the deletion around the current selection position of the editable text.
boolean endBatchEdit()
Default implementation does nothing.
boolean finishComposingText()
The default implementation removes the composing state from the current editable text.
static int getComposingSpanEnd(Spannable text)
static int getComposingSpanStart(Spannable text)
int getCursorCapsMode(int reqModes)
The default implementation uses TextUtils.getCapsMode to get the cursor caps mode for the current selection position in the editable text, unless in dummy mode in which case 0 is always returned.
Editable getEditable()
Return the target of edit operations.
ExtractedText getExtractedText(ExtractedTextRequest request, int flags)
The default implementation always returns null.
CharSequence getSelectedText(int flags)
The default implementation returns the text currently selected, or null if none is selected.
CharSequence getTextAfterCursor(int length, int flags)
The default implementation returns the given amount of text from the current cursor position in the buffer.
CharSequence getTextBeforeCursor(int length, int flags)
The default implementation returns the given amount of text from the current cursor position in the buffer.
boolean performContextMenuAction(int id)
The default implementation does nothing.
boolean performEditorAction(int actionCode)
The default implementation turns this into the enter key.
boolean performPrivateCommand(String action, Bundle data)
The default implementation does nothing.
final static void removeComposingSpans(Spannable text)
boolean reportFullscreenMode(boolean enabled)
Updates InputMethodManager with the current fullscreen mode.
boolean requestCursorUpdates(int cursorUpdateMode)
The default implementation does nothing.
boolean sendKeyEvent(KeyEvent event)
Provides standard implementation for sending a key event to the window attached to the input connection's view.
boolean setComposingRegion(int start, int end)
Mark a certain region of text as composing text.
static void setComposingSpans(Spannable text)
boolean setComposingText(CharSequence text, int newCursorPosition)
The default implementation places the given text into the editable, replacing any existing composing text.
boolean setSelection(int start, int end)
The default implementation changes the selection position in the current editable text.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.view.inputmethod.InputConnection

Public Constructors

public BaseInputConnection (View targetView, boolean fullEditor)

Added in API level 3

Parameters
targetView View
fullEditor boolean

Public Methods

public boolean beginBatchEdit ()

Added in API level 3

Default implementation does nothing.

Returns
boolean true if a batch edit is now in progress, false otherwise. Since this method starts a batch edit, that means it will always return true unless the input connection is no longer valid.

public boolean clearMetaKeyStates (int states)

Added in API level 3

Default implementation uses MetaKeyKeyListener.clearMetaKeyState(long, int) to clear the state.

Parameters
states int: The states to be cleared, may be one or more bits as per KeyEvent.getMetaState().
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean commitCompletion (CompletionInfo text)

Added in API level 3

Default implementation does nothing and returns false.

Parameters
text CompletionInfo: The committed completion.
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean commitCorrection (CorrectionInfo correctionInfo)

Added in API level 11

Default implementation does nothing and returns false.

Parameters
correctionInfo CorrectionInfo: Detailed information about the correction.
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean commitText (CharSequence text, int newCursorPosition)

Added in API level 3

Default implementation replaces any existing composing text with the given text. In addition, only if dummy mode, a key event is sent for the new text and the current editable buffer cleared.

Parameters
text CharSequence: The text to commit. This may include styles.
newCursorPosition int: The new cursor position around the text, in Java characters. If > 0, this is relative to the end of the text - 1; if <= 0, this is relative to the start of the text. So a value of 1 will always advance the cursor to the position after the full text being inserted. Note that this means you can't position the cursor within the text, because the editor can make modifications to the text you are providing so it is not possible to correctly specify locations there.
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean deleteSurroundingText (int beforeLength, int afterLength)

Added in API level 3

The default implementation performs the deletion around the current selection position of the editable text.

Returns
boolean true on success, false if the input connection is no longer valid.

public boolean endBatchEdit ()

Added in API level 3

Default implementation does nothing.

Returns
boolean true if there is still a batch edit in progress after closing the latest one (in other words, if the nesting count is > 0), false otherwise or if the input connection is no longer valid.

public boolean finishComposingText ()

Added in API level 3

The default implementation removes the composing state from the current editable text. In addition, only if dummy mode, a key event is sent for the new text and the current editable buffer cleared.

Returns
boolean true on success, false if the input connection is no longer valid.

public static int getComposingSpanEnd (Spannable text)

Added in API level 3

Parameters
text Spannable
Returns
int

public static int getComposingSpanStart (Spannable text)

Added in API level 3

Parameters
text Spannable
Returns
int

public int getCursorCapsMode (int reqModes)

Added in API level 3

The default implementation uses TextUtils.getCapsMode to get the cursor caps mode for the current selection position in the editable text, unless in dummy mode in which case 0 is always returned.

Parameters
reqModes int: The desired modes to retrieve, as defined by TextUtils.getCapsMode. These constants are defined so that you can simply pass the current TextBoxAttribute.contentType value directly in to here.
Returns
int the caps mode flags that are in effect at the current cursor position. See TYPE_TEXT_FLAG_CAPS_* in InputType.

public Editable getEditable ()

Added in API level 3

Return the target of edit operations. The default implementation returns its own fake editable that is just used for composing text; subclasses that are real text editors should override this and supply their own.

Returns
Editable

public ExtractedText getExtractedText (ExtractedTextRequest request, int flags)

Added in API level 3

The default implementation always returns null.

Parameters
request ExtractedTextRequest: Description of how the text should be returned. ExtractedTextRequest
flags int: Additional options to control the client, either 0 or GET_EXTRACTED_TEXT_MONITOR.
Returns
ExtractedText an ExtractedText object describing the state of the text view and containing the extracted text itself, or null if the input connection is no longer valid of the editor can't comply with the request for some reason.

public CharSequence getSelectedText (int flags)

Added in API level 9

The default implementation returns the text currently selected, or null if none is selected.

Parameters
flags int: Supplies additional options controlling how the text is returned. May be either 0 or GET_TEXT_WITH_STYLES.
Returns
CharSequence the text that is currently selected, if any, or null if no text is selected.

public CharSequence getTextAfterCursor (int length, int flags)

Added in API level 3

The default implementation returns the given amount of text from the current cursor position in the buffer.

Parameters
length int: The expected length of the text.
flags int: Supplies additional options controlling how the text is returned. May be either 0 or GET_TEXT_WITH_STYLES.
Returns
CharSequence the text after the cursor position; the length of the returned text might be less than n.

public CharSequence getTextBeforeCursor (int length, int flags)

Added in API level 3

The default implementation returns the given amount of text from the current cursor position in the buffer.

Parameters
length int: The expected length of the text.
flags int: Supplies additional options controlling how the text is returned. May be either 0 or GET_TEXT_WITH_STYLES.
Returns
CharSequence the text before the cursor position; the length of the returned text might be less than n.

public boolean performContextMenuAction (int id)

Added in API level 3

The default implementation does nothing.

Parameters
id int
Returns
boolean

public boolean performEditorAction (int actionCode)

Added in API level 3

The default implementation turns this into the enter key.

Parameters
actionCode int: This must be one of the action constants for EditorInfo.editorType, such as EditorInfo.EDITOR_ACTION_GO.
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean performPrivateCommand (String action, Bundle data)

Added in API level 3

The default implementation does nothing.

Parameters
action String: Name of the command to be performed. This must be a scoped name, i.e. prefixed with a package name you own, so that different developers will not create conflicting commands.
data Bundle: Any data to include with the command.
Returns
boolean true if the command was sent (whether or not the associated editor understood it), false if the input connection is no longer valid.

public static final void removeComposingSpans (Spannable text)

Added in API level 3

Parameters
text Spannable

public boolean reportFullscreenMode (boolean enabled)

Added in API level 3

Updates InputMethodManager with the current fullscreen mode.

Parameters
enabled boolean
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean requestCursorUpdates (int cursorUpdateMode)

Added in API level 21

The default implementation does nothing.

Parameters
cursorUpdateMode int: CURSOR_UPDATE_IMMEDIATE and/or CURSOR_UPDATE_MONITOR. Pass 0 to disable the effect of CURSOR_UPDATE_MONITOR.
Returns
boolean true if the request is scheduled. false to indicate that when the application will not call updateCursorAnchorInfo(android.view.View, CursorAnchorInfo).

public boolean sendKeyEvent (KeyEvent event)

Added in API level 3

Provides standard implementation for sending a key event to the window attached to the input connection's view.

Parameters
event KeyEvent: The key event.
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean setComposingRegion (int start, int end)

Added in API level 9

Mark a certain region of text as composing text. If there was a composing region, the characters are left as they were and the composing span removed, as if finishComposingText() has been called. The default style for composing text is used.

The passed indices are clipped to the contents bounds. If the resulting region is zero-sized, no region is marked and the effect is the same as that of calling finishComposingText(). The order of start and end is not important. In effect, the region from start to end and the region from end to start is the same. Editor authors, be ready to accept a start that is greater than end.

Since this does not change the contents of the text, editors should not call updateSelection(View, int, int, int, int) and IMEs should not receive onUpdateSelection(int, int, int, int, int, int).

This has no impact on the cursor/selection position. It may result in the cursor being anywhere inside or outside the composing region, including cases where the selection and the composing region overlap partially or entirely.

Parameters
start int: the position in the text at which the composing region begins
end int: the position in the text at which the composing region ends
Returns
boolean true on success, false if the input connection is no longer valid.

public static void setComposingSpans (Spannable text)

Added in API level 3

Parameters
text Spannable

public boolean setComposingText (CharSequence text, int newCursorPosition)

Added in API level 3

The default implementation places the given text into the editable, replacing any existing composing text. The new text is marked as in a composing state with the composing style.

Parameters
text CharSequence: The composing text with styles if necessary. If no style object attached to the text, the default style for composing text is used. See Spanned for how to attach style object to the text. SpannableString and SpannableStringBuilder are two implementations of the interface Spanned.
newCursorPosition int: The new cursor position around the text. If > 0, this is relative to the end of the text - 1; if <= 0, this is relative to the start of the text. So a value of 1 will always advance you to the position after the full text being inserted. Note that this means you can't position the cursor within the text, because the editor can make modifications to the text you are providing so it is not possible to correctly specify locations there.
Returns
boolean true on success, false if the input connection is no longer valid.

public boolean setSelection (int start, int end)

Added in API level 3

The default implementation changes the selection position in the current editable text.

Parameters
start int: the character index where the selection should start.
end int: the character index where the selection should end.
Returns
boolean true on success, false if the input connection is no longer valid.