# InputConnectionCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/view/inputmethod/InputConnectionCompat.java+class:androidx.core.view.inputmethod.InputConnectionCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public final class InputConnectionCompat
```

<br />

*** ** * ** ***

Helper for accessing features in `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` introduced after API level 13 in a backwards compatible fashion.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat.OnCommitContentListener` Listener for commitContent method call, in a backwards compatible fashion. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#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 `https://developer.android.com/reference/androidx/core/view/inputmethod/InputContentInfoCompat` object, in a similar manner that has been recommended in [Sharing Files](https://developer.android.com/training/secure-file-sharing). |

| ### Public constructors |
|---|
| `[InputConnectionCompat](https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#InputConnectionCompat())()` **This method is deprecated.** This type should not be instantiated as it contains only static methods. <br /> |

| ### Public methods |
|---|---|
| `static boolean` | `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#commitContent(android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo,androidx.core.view.inputmethod.InputContentInfoCompat,int,android.os.Bundle)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html editorInfo, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/inputmethod/InputContentInfoCompat inputContentInfo, int flags, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html opts )` Calls commitContent API, in a backwards compatible fashion. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` | `[createWrapper](https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#createWrapper(android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo,androidx.core.view.inputmethod.InputConnectionCompat.OnCommitContentListener))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html editorInfo, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat.OnCommitContentListener onCommitContentListener )` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#createWrapper(android.view.View,android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo)` <br /> |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` | `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#createWrapper(android.view.View,android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/View.html view, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html editorInfo )` Creates a wrapper `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` that implements `InputConnection`'s features on past versions of Android. |

## Constants

### INPUT_CONTENT_GRANT_READ_URI_PERMISSION

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/androidx/core/view/inputmethod/InputContentInfoCompat` object, in a similar manner that has been recommended in [Sharing Files](https://developer.android.com/training/secure-file-sharing).

Make sure that the content provider owning the Uri sets the `https://developer.android.com/reference/android/R.attr.html#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 `https://developer.android.com/reference/android/content/ContentProvider.html`.

- On API \>= 23 `https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#packageName` is guaranteed to not be spoofed, which can later be compared with `https://developer.android.com/reference/android/content/ContentProvider.html#getCallingPackage()` in the `https://developer.android.com/reference/android/content/ContentProvider.html`.
- `https://developer.android.com/reference/android/view/inputmethod/InputBinding.html#getUid()` is guaranteed to not be spoofed, which can later be compared with `https://developer.android.com/reference/android/os/Binder.html#getCallingUid()` in the `https://developer.android.com/reference/android/content/ContentProvider.html`.

## Public constructors

### InputConnectionCompat

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public [InputConnectionCompat](https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#InputConnectionCompat())()
```

> [!CAUTION]
> **This method is deprecated.**   
>
> This type should not be instantiated as it contains only static methods.

## Public methods

### commitContent

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection` | `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` with which commitContent API will be called |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html editorInfo` | `https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html` associated with the given `inputConnection` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/inputmethod/InputContentInfoCompat inputContentInfo` | content information to be passed to the editor |
| `int flags` | `0` or `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#INPUT_CONTENT_GRANT_READ_URI_PERMISSION()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html 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](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)

```
public static @NonNull InputConnection [createWrapper](https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#createWrapper(android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo,androidx.core.view.inputmethod.InputConnectionCompat.OnCommitContentListener))(
    @NonNull InputConnection inputConnection,
    @NonNull EditorInfo editorInfo,
    @NonNull InputConnectionCompat.OnCommitContentListener onCommitContentListener
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#createWrapper(android.view.View,android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo)`

Creates a wrapper `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` object from an existing `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` and `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat.OnCommitContentListener` that can be returned to the system.

By returning the wrapper object to the IME, the editor can be notified by `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat.OnCommitContentListener#onCommitContent(androidx.core.view.inputmethod.InputContentInfoCompat,int,android.os.Bundle)` when the IME calls `https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat#commitContent(android.view.inputmethod.InputConnection,android.view.inputmethod.EditorInfo,androidx.core.view.inputmethod.InputContentInfoCompat,int,android.os.Bundle)` and the corresponding Framework API that is available on API \>= 25.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection` | `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` to be wrapped |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html editorInfo` | `https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html` associated with the given `inputConnection` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/inputmethod/InputConnectionCompat.OnCommitContentListener onCommitContentListener` | the listener that the wrapper object will call |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` | a wrapper `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` object that can be returned to the IME |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | when `inputConnection`, `editorInfo`, or `onCommitContentListener` is `null` |

### createWrapper

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)

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

Creates a wrapper `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` that implements `InputConnection`'s features on past versions of Android.

Currently, handles `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html#commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle)` by dispatching to `https://developer.android.com/reference/androidx/core/view/ViewCompat#performReceiveContent(android.view.View,androidx.core.view.ContentInfoCompat)`, enabling apps to use `https://developer.android.com/reference/androidx/core/view/ViewCompat#setOnReceiveContentListener(android.view.View,java.lang.String[],androidx.core.view.OnReceiveContentListener)` 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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/View.html view` | The view that the given input connection is associated with. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html inputConnection` | The input connection to be wrapped. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html editorInfo` | The editor metadata associated with the given input connection. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` | A wrapper `https://developer.android.com/reference/android/view/inputmethod/InputConnection.html` object that can be returned to the IME. |