Need to override [onCreateInputConnection] function for TextView

1.7k views Asked by At

I made a little chat and I want to use my Google keyboard (GBoard) to send Gifs. When I clicked on a Gif I had this message: “App doesn't support image insertion here”.

I looked online and I saw that I need to override onCreateInputConnection() function for TextView like it says in this link : https://developer.android.com/guide/topics/text/image-keyboard#java

If I understand well, EditText is the native component of TextView. But I really don't know how I can override it.

I transposed the java code in typescript but I can't test and I don't know how to test it right now.

export class CustomTextView extends android.widget.TextView {    

    public onCreateInputConnection(editorInfo: android.view.inputmethod.EditorInfo): android.view.inputmethod.InputConnection {
        var inputConnection = super.onCreateInputConnection(editorInfo);
        androidx.core.view.inputmethod.EditorInfoCompat.setContentMimeTypes(editorInfo, Array("image/gif", "image/png"));

        var callback = new androidx.core.view.inputmethod.InputConnectionCompat.OnCommitContentListener();
        callback.onCommitContent = (inputContentInfo: androidx.core.view.inputmethod.InputContentInfoCompat, flags: number, opts: globalAndroid.os.Bundle): boolean => {
            if (androidx.core.os.BuildCompat.isAtLeastNMR1() && (flags &
                androidx.core.view.inputmethod.InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                try {
                    inputContentInfo.requestPermission();
                }
                catch (e) {
                    return false;
                }
            }
            return true;
        }
        return androidx.core.view.inputmethod.InputConnectionCompat.createWrapper(inputConnection, editorInfo, callback);
    }
}

I don't found any help online...

Thanks !

1

There are 1 answers

0
AudioBubble On

Thank you very much for your answer @manoj. So i tried to create a component like you said and override onCreateInputConnection.

It quite works. I don't have this message: “App doesn't support image insertion here” anymore but nothing happen next and i don't know what i have to do...

Here is my code:

import { CustomTextEditBase } from "./custom-text-edit.common";

class CustomAndroidWidgetEditText extends android.widget.EditText {

    constructor(context: globalAndroid.content.Context) {
        super(context);
    }

    public onCreateInputConnection(editorInfo: android.view.inputmethod.EditorInfo): android.view.inputmethod.InputConnection {
        var inputConnection = super.onCreateInputConnection(editorInfo);
        let mimeTypesArray : native.Array<string> = ["image/gif", "image/png", "image/jpg"];
        androidx.core.view.inputmethod.EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypesArray);

        var callback = new androidx.core.view.inputmethod.InputConnectionCompat.OnCommitContentListener({
            onCommitContent: (inputContentInfo: androidx.core.view.inputmethod.InputContentInfoCompat, flags: number, opts: globalAndroid.os.Bundle): boolean => {
                if (androidx.core.os.BuildCompat.isAtLeastNMR1() && (flags &
                    androidx.core.view.inputmethod.InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                    try {
                        inputContentInfo.requestPermission();
                    }
                    catch (e) {
                        return false;
                    }
                }

                var gifSupported: boolean = false;
                for (let mimeType in mimeTypesArray) {
                    if (inputContentInfo.getDescription().hasMimeType(mimeTypesArray[mimeType])) {
                        gifSupported = true;
                    }
                }
                if(!gifSupported) return false;

                androidx.core.view.inputmethod.InputConnectionCompat.commitContent(inputConnection, editorInfo, inputContentInfo, flags, opts);
                return true;
            }
        });
        return androidx.core.view.inputmethod.InputConnectionCompat.createWrapper(inputConnection, editorInfo, callback);
    }
}

export class CustomTextEdit extends CustomTextEditBase {

    nativeView: CustomAndroidWidgetEditText;

    public createNativeView(): Object {
        const editText = new CustomAndroidWidgetEditText(this._context);
        return editText;
    }

    initNativeView(): void {
        (<any>this.nativeView).owner = this;
        super.initNativeView();
    }

    disposeNativeView(): void {
        (<any>this.nativeView).owner = null;
        super.disposeNativeView();
    }
}