Search in sources :

Example 1 with ExtractedText

use of android.view.inputmethod.ExtractedText in project platform_frameworks_base by android.

the class InputConnectionWrapper method getExtractedText.

public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
    ExtractedText value = null;
    try {
        InputContextCallback callback = InputContextCallback.getInstance();
        mIInputContext.getExtractedText(request, flags, callback.mSeq, callback);
        synchronized (callback) {
            callback.waitForResultLocked();
            if (callback.mHaveValue) {
                value = callback.mExtractedText;
            }
        }
        callback.dispose();
    } catch (RemoteException e) {
        return null;
    }
    return value;
}
Also used : RemoteException(android.os.RemoteException) ExtractedText(android.view.inputmethod.ExtractedText)

Example 2 with ExtractedText

use of android.view.inputmethod.ExtractedText in project XobotOS by xamarin.

the class InputConnectionWrapper method getExtractedText.

public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
    ExtractedText value = null;
    try {
        InputContextCallback callback = InputContextCallback.getInstance();
        mIInputContext.getExtractedText(request, flags, callback.mSeq, callback);
        synchronized (callback) {
            callback.waitForResultLocked();
            if (callback.mHaveValue) {
                value = callback.mExtractedText;
            }
        }
        callback.dispose();
    } catch (RemoteException e) {
        return null;
    }
    return value;
}
Also used : RemoteException(android.os.RemoteException) ExtractedText(android.view.inputmethod.ExtractedText)

Example 3 with ExtractedText

use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.

the class InputConnectionCommandEditor method getCommitTextOp.

/**
     * Op that commits a text at the cursor. If successful then an undo is returned which deletes
     * the text and restores the old selection.
     */
private Op getCommitTextOp(final CharSequence oldText, final CharSequence newText) {
    return new Op("commitText") {

        @Override
        public Op run() {
            Op undo = null;
            // TODO: use getSelection
            final ExtractedText et = getExtractedText();
            if (mInputConnection.commitText(newText, 1)) {
                undo = new Op("deleteSurroundingText+commitText") {

                    @Override
                    public Op run() {
                        mInputConnection.beginBatchEdit();
                        boolean success = mInputConnection.deleteSurroundingText(newText.length(), 0);
                        if (success && oldText != null) {
                            success = mInputConnection.commitText(oldText, 1);
                        }
                        if (et != null && success) {
                            success = mInputConnection.setSelection(et.selectionStart, et.selectionEnd);
                        }
                        mInputConnection.endBatchEdit();
                        if (success) {
                            return NO_OP;
                        }
                        return null;
                    }
                };
            }
            return undo;
        }
    };
}
Also used : ExtractedText(android.view.inputmethod.ExtractedText)

Example 4 with ExtractedText

use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.

the class InputConnectionCommandEditor method getCommitWithOverwriteOp.

/**
     * TODO: review
     * we should be able to review the last N ops and undo then if they can be interpreted as
     * a combined op.
     */
private Op getCommitWithOverwriteOp(final String text) {
    return new Op("add " + text) {

        @Override
        public Op run() {
            // Calculate the length of the text that has changed
            String commonPrefix = greatestCommonPrefix(mPrevText, text);
            int commonPrefixLength = commonPrefix.length();
            mInputConnection.beginBatchEdit();
            final ExtractedText et = getExtractedText();
            final String selectedText = getSelectedText();
            // Delete the part that changed compared to the partial text added earlier.
            int deletableLength = mPrevText.length() - commonPrefixLength;
            if (deletableLength > 0) {
                mInputConnection.deleteSurroundingText(deletableLength, 0);
            }
            // Finish if there is nothing to add
            if (text.isEmpty() || commonPrefixLength == text.length()) {
                mAddedLength -= deletableLength;
            } else {
                CharSequence leftContext = "";
                String glue = "";
                String text1 = text;
                // If the prev text and the current text share no prefix then recalculate the glue.
                if (commonPrefixLength == 0) {
                    // We look at the left context of the cursor
                    // to decide which glue symbol to use and whether to capitalize the text.
                    CharSequence textBeforeCursor = mInputConnection.getTextBeforeCursor(MAX_DELETABLE_CONTEXT, 0);
                    // In some error situations, null is returned
                    if (textBeforeCursor != null) {
                        leftContext = textBeforeCursor;
                    }
                    glue = getGlue(text, leftContext);
                    mAddedLength = glue.length() + text.length();
                } else {
                    text1 = text.substring(commonPrefixLength);
                    leftContext = commonPrefix;
                    mAddedLength = mAddedLength - deletableLength + text1.length();
                }
                text1 = capitalizeIfNeeded(text1, leftContext);
                mInputConnection.commitText(glue + text1, 1);
            }
            mInputConnection.endBatchEdit();
            return new Op("delete " + mAddedLength) {

                @Override
                public Op run() {
                    mInputConnection.beginBatchEdit();
                    boolean success = mInputConnection.deleteSurroundingText(mAddedLength, 0);
                    if (et != null && selectedText.length() > 0) {
                        success = mInputConnection.commitText(selectedText, 1) && mInputConnection.setSelection(et.selectionStart, et.selectionEnd);
                    }
                    mInputConnection.endBatchEdit();
                    if (success) {
                        return NO_OP;
                    }
                    return null;
                }
            };
        }
    };
}
Also used : ExtractedText(android.view.inputmethod.ExtractedText)

Example 5 with ExtractedText

use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.

the class InputConnectionCommandEditor method selectReAfter.

@Override
public Op selectReAfter(final String regex, final int n) {
    return new Op("selectReAfter") {

        @Override
        public Op run() {
            Op undo = null;
            mInputConnection.beginBatchEdit();
            final ExtractedText et = getExtractedText();
            if (et != null) {
                CharSequence input = et.text.subSequence(et.selectionEnd, et.text.length());
                Pair<Integer, Integer> pos = matchNth(Pattern.compile(regex), input, n);
                if (pos != null) {
                    undo = getOpSetSelection(et.selectionEnd + pos.first, et.selectionEnd + pos.second, et.selectionStart, et.selectionEnd).run();
                }
            }
            mInputConnection.endBatchEdit();
            return undo;
        }
    };
}
Also used : ExtractedText(android.view.inputmethod.ExtractedText)

Aggregations

ExtractedText (android.view.inputmethod.ExtractedText)27 RemoteException (android.os.RemoteException)7 ExtractedTextRequest (android.view.inputmethod.ExtractedTextRequest)2 Pair (android.support.v4.util.Pair)1 Editable (android.text.Editable)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 KeyEvent (android.view.KeyEvent)1 EditorInfo (android.view.inputmethod.EditorInfo)1 InputConnection (android.view.inputmethod.InputConnection)1 SpacingAndPunctuations (com.android.inputmethod.latin.settings.SpacingAndPunctuations)1 TextRange (com.android.inputmethod.latin.utils.TextRange)1