Example usage for android.view.inputmethod BaseInputConnection getComposingSpanEnd

List of usage examples for android.view.inputmethod BaseInputConnection getComposingSpanEnd

Introduction

In this page you can find the example usage for android.view.inputmethod BaseInputConnection getComposingSpanEnd.

Prototype

public static int getComposingSpanEnd(Spannable text) 

Source Link

Usage

From source file:org.mozilla.focus.widget.InlineAutocompleteEditText.java

/**
 * Code to handle deleting autocomplete first when backspacing.
 * If there is no autocomplete text, both removeAutocomplete() and commitAutocomplete()
 * are no-ops and return false. Therefore we can use them here without checking explicitly
 * if we have autocomplete text or not./*from   w w  w. j av  a  2s. co  m*/
 *
 * Also turns off text prediction for private mode tabs.
 */
@Override
public InputConnection onCreateInputConnection(final EditorInfo outAttrs) {
    final InputConnection ic = super.onCreateInputConnection(outAttrs);
    if (ic == null) {
        return null;
    }

    return new InputConnectionWrapper(ic, false) {
        @Override
        public boolean deleteSurroundingText(final int beforeLength, final int afterLength) {
            if (removeAutocomplete(getText())) {
                // If we have autocomplete text, the cursor is at the boundary between
                // regular and autocomplete text. So regardless of which direction we
                // are deleting, we should delete the autocomplete text first.
                // Make the IME aware that we interrupted the deleteSurroundingText call,
                // by restarting the IME.
                final InputMethodManager imm = (InputMethodManager) mContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.restartInput(InlineAutocompleteEditText.this);
                }
                return false;
            }
            return super.deleteSurroundingText(beforeLength, afterLength);
        }

        private boolean removeAutocompleteOnComposing(final CharSequence text) {
            final Editable editable = getText();
            final int composingStart = BaseInputConnection.getComposingSpanStart(editable);
            final int composingEnd = BaseInputConnection.getComposingSpanEnd(editable);
            // We only delete the autocomplete text when the user is backspacing,
            // i.e. when the composing text is getting shorter.
            if (composingStart >= 0 && composingEnd >= 0 && (composingEnd - composingStart) > text.length()
                    && removeAutocomplete(editable)) {
                // Make the IME aware that we interrupted the setComposingText call,
                // by having finishComposingText() send change notifications to the IME.
                finishComposingText();
                setComposingRegion(composingStart, composingEnd);
                return true;
            }
            return false;
        }

        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {
            if (removeAutocompleteOnComposing(text)) {
                return false;
            }
            return super.commitText(text, newCursorPosition);
        }

        @Override
        public boolean setComposingText(final CharSequence text, final int newCursorPosition) {
            if (removeAutocompleteOnComposing(text)) {
                return false;
            }
            return super.setComposingText(text, newCursorPosition);
        }
    };
}