Search in sources :

Example 21 with ExtractedText

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

the class InputConnectionCommandEditor method moveAbs.

@Override
public Op moveAbs(final int pos) {
    return new Op("moveAbs " + pos) {

        @Override
        public Op run() {
            Op undo = null;
            mInputConnection.beginBatchEdit();
            ExtractedText et = getExtractedText();
            if (et != null) {
                int charPos = pos;
                if (pos < 0) {
                    //-1 == end of text
                    charPos = et.text.length() + pos + 1;
                }
                undo = getOpSetSelection(charPos, charPos, et.selectionStart, et.selectionEnd).run();
            }
            mInputConnection.endBatchEdit();
            return undo;
        }
    };
}
Also used : ExtractedText(android.view.inputmethod.ExtractedText)

Example 22 with ExtractedText

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

the class InputConnectionCommandEditor method replace.

@Override
public Op replace(final String query, final String replacement) {
    return new Op("replace") {

        @Override
        public Op run() {
            Op undo = null;
            mInputConnection.beginBatchEdit();
            final ExtractedText et = getExtractedText();
            if (et != null) {
                Pair<Integer, CharSequence> queryResult = lastIndexOf(query, et);
                final CharSequence match = queryResult.second;
                if (queryResult.first >= 0) {
                    boolean success = mInputConnection.setSelection(queryResult.first, queryResult.first);
                    if (success) {
                        // Delete existing text
                        success = mInputConnection.deleteSurroundingText(0, match.length());
                        if (replacement.isEmpty()) {
                            if (success) {
                                undo = new Op("undo replace1") {

                                    @Override
                                    public Op run() {
                                        mInputConnection.beginBatchEdit();
                                        boolean success2 = mInputConnection.commitText(match, 1) && mInputConnection.setSelection(et.selectionStart, et.selectionEnd);
                                        mInputConnection.endBatchEdit();
                                        if (success2) {
                                            return NO_OP;
                                        }
                                        return null;
                                    }
                                };
                            }
                        } else {
                            success = mInputConnection.commitText(replacement, 1);
                            if (success) {
                                undo = new Op("undo replace2") {

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

Example 23 with ExtractedText

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

the class InputConnectionCommandEditor method commitFinalResult.

@Override
public CommandEditorResult commitFinalResult(final String text) {
    CommandEditorResult result = null;
    if (mRewriters == null || mRewriters.isEmpty()) {
        // If rewrites/commands are not defined (default), then selection can be dictated over.
        commitWithOverwrite(text);
    } else {
        final ExtractedText et = getExtractedText();
        final String selectedText = getSelectedText();
        // Try to interpret the text as a command and if it is, then apply it.
        // Otherwise write out the text as usual.
        UtteranceRewriter.Rewrite rewrite = applyCommand(text);
        String textRewritten = rewrite.mStr;
        final int len = commitWithOverwrite(textRewritten);
        // TODO: add undo for setSelection even if len==0
        if (len > 0) {
            pushOpUndo(new Op("delete " + len) {

                @Override
                public Op run() {
                    mInputConnection.beginBatchEdit();
                    boolean success = mInputConnection.deleteSurroundingText(len, 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;
                }
            });
        }
        boolean success = false;
        if (rewrite.isCommand()) {
            CommandEditorManager.EditorCommand ec = CommandEditorManager.get(rewrite.mId);
            if (ec != null) {
                // TODO: dont call runOp from here
                success = runOp(ec.getOp(this, rewrite.mArgs));
            }
        } else {
            mCommandPrefix.add(textRewritten);
        }
        result = new CommandEditorResult(success, rewrite);
    }
    mPrevText = "";
    mAddedLength = 0;
    return result;
}
Also used : ExtractedText(android.view.inputmethod.ExtractedText)

Example 24 with ExtractedText

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

the class InputConnectionCommandEditor method selectReBefore.

@Override
public Op selectReBefore(final String regex) {
    return new Op("selectReBefore") {

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

Example 25 with ExtractedText

use of android.view.inputmethod.ExtractedText in project android_frameworks_base by DirtyUnicorns.

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)

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