InputConnectionCompat

Added in 1.1.0

public final class InputConnectionCompat


Helper for accessing features in InputConnection introduced after API level 13 in a backwards compatible fashion.

Summary

Nested types

Listener for commitContent method call, in a backwards compatible fashion.

Constants

static final int

When this flag is used, the editor will be able to request temporary access permissions to the content URI contained in the InputContentInfoCompat object, in a similar manner that has been recommended in Sharing Files.

Public constructors

This method is deprecated.

This type should not be instantiated as it contains only static methods.

Public methods

static boolean
commitContent(
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo,
    @NonNull InputContentInfoCompat inputContentInfo,
    int flags,
    @Nullable Bundle opts
)

Calls commitContent API, in a backwards compatible fashion.

static @NonNull InputConnection
createWrapper(
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo,
    @NonNull InputConnectionCompat.OnCommitContentListener onCommitContentListener
)

This method is deprecated.

Use and { } instead.

static @NonNull InputConnection
createWrapper(
    @NonNull View view,
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo
)

Creates a wrapper InputConnection that implements InputConnection's features on past versions of Android.

Constants

INPUT_CONTENT_GRANT_READ_URI_PERMISSION

Added in 1.1.0
public static final int INPUT_CONTENT_GRANT_READ_URI_PERMISSION = 1

When this flag is used, the editor will be able to request temporary access permissions to the content URI contained in the InputContentInfoCompat object, in a similar manner that has been recommended in Sharing Files.

Make sure that the content provider owning the Uri sets the grantUriPermissions attribute in its manifest or included the <grant-uri-permissions> tag.

Supported only on API >= 25.

On API <= 24 devices, IME developers need to ensure that the content URI is accessible only from the target application, for example, by generating a URL with a unique name that others cannot guess. IME developers can also rely on the following information of the target application to do additional access checks in their ContentProvider.

Public constructors

InputConnectionCompat

Added in 1.1.0
Deprecated in 1.1.0
public InputConnectionCompat()

Public methods

commitContent

Added in 1.1.0
public static boolean commitContent(
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo,
    @NonNull InputContentInfoCompat inputContentInfo,
    int flags,
    @Nullable Bundle opts
)

Calls commitContent API, in a backwards compatible fashion.

Parameters
@NonNull InputConnection inputConnection

InputConnection with which commitContent API will be called

@NonNull EditorInfo editorInfo

EditorInfo associated with the given inputConnection

@NonNull InputContentInfoCompat inputContentInfo

content information to be passed to the editor

int flags

0 or INPUT_CONTENT_GRANT_READ_URI_PERMISSION

@Nullable Bundle opts

optional bundle data. This can be null

Returns
boolean

true if this request is accepted by the application, no matter if the request is already handled or still being handled in background

createWrapper

Added in 1.1.0
Deprecated in 1.7.0
public static @NonNull InputConnection createWrapper(
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo,
    @NonNull InputConnectionCompat.OnCommitContentListener onCommitContentListener
)

Creates a wrapper InputConnection object from an existing InputConnection and OnCommitContentListener that can be returned to the system.

By returning the wrapper object to the IME, the editor can be notified by onCommitContent when the IME calls commitContent and the corresponding Framework API that is available on API >= 25.

Parameters
@NonNull InputConnection inputConnection

InputConnection to be wrapped

@NonNull EditorInfo editorInfo

EditorInfo associated with the given inputConnection

@NonNull InputConnectionCompat.OnCommitContentListener onCommitContentListener

the listener that the wrapper object will call

Returns
@NonNull InputConnection

a wrapper InputConnection object that can be returned to the IME

Throws
java.lang.IllegalArgumentException

when inputConnection, editorInfo, or onCommitContentListener is null

createWrapper

Added in 1.7.0
public static @NonNull InputConnection createWrapper(
    @NonNull View view,
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo
)

Creates a wrapper InputConnection that implements InputConnection's features on past versions of Android.

Currently, handles commitContent by dispatching to performReceiveContent, enabling apps to use setOnReceiveContentListener to specify handling for content insertion from the IME.

Usage:

public class MyWidget extends View {
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        InputConnection ic = super.onCreateInputConnection(outAttrs);
        if (ic == null) {
            return ic;
        }
        String[] mimeTypes = ViewCompat.getOnReceiveContentMimeTypes(this);
        if (mimeTypes != null) {
            EditorInfoCompat.setContentMimeTypes(outAttrs, mimeTypes);
            ic = InputConnectionCompat.createWrapper(this, ic, outAttrs);
        }
        return ic;
    }
}
Parameters
@NonNull View view

The view that the given input connection is associated with.

@NonNull InputConnection inputConnection

The input connection to be wrapped.

@NonNull EditorInfo editorInfo

The editor metadata associated with the given input connection.

Returns
@NonNull InputConnection

A wrapper InputConnection object that can be returned to the IME.