Example usage for android.view.inputmethod InputConnection endBatchEdit

List of usage examples for android.view.inputmethod InputConnection endBatchEdit

Introduction

In this page you can find the example usage for android.view.inputmethod InputConnection endBatchEdit.

Prototype

boolean endBatchEdit();

Source Link

Document

Tell the editor that you are done with a batch edit previously initiated with #beginBatchEdit .

Usage

From source file:com.volosyukivan.WiFiInputMethod.java

boolean setText(String text) {
    // FIXME: need feedback if the input was lost
    InputConnection conn = getCurrentInputConnection();
    if (conn == null) {
        //      Debug.d("connection closed");
        return false;
    }/*from w  w  w  . ja  v  a 2 s .c o  m*/
    conn.beginBatchEdit();
    // FIXME: hack
    conn.deleteSurroundingText(100000, 100000);
    conn.commitText(text, text.length());
    conn.endBatchEdit();
    return true;
}

From source file:net.zhdev.ctrlvkeyboard.CtrlVKeyboard.java

/**
 * Inserts text in the current focused text editor if possible and if it is a valid one.
 *
 * @param text          the text to be inserted
 * @param clearBefore   whether the text editor should be cleared before inserting the text
 * @param endWithAction whether the IME action should be performed at the end
 * @return {@code true} if the text was inserted and the actions performed, {@code false}
 * otherwise/*from  w  w  w  .ja  v  a  2s  . c  om*/
 */
private boolean type(String text, boolean clearBefore, boolean endWithAction) {
    boolean result = false;

    if (text == null) {
        text = "";
    }

    EditorInfo info = getCurrentInputEditorInfo();

    if (info != null) {
        // If the view accepts text as input
        if ((info.inputType & EditorInfo.TYPE_MASK_CLASS) != EditorInfo.TYPE_NULL) {
            InputConnection ic = getCurrentInputConnection();

            if (ic != null) {
                boolean enterShouldHaveAction = true;
                ic.beginBatchEdit();
                if (endWithAction) {
                    enterShouldHaveAction = (info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0;
                    if (!enterShouldHaveAction) {
                        text += "\n";
                    }
                }
                if (clearBefore) {
                    ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
                }
                ic.commitText(text, 1);
                result = ic.endBatchEdit();

                if (result && endWithAction && enterShouldHaveAction) {
                    result = ic.performEditorAction(info.imeOptions & EditorInfo.IME_MASK_ACTION);
                }
            }
        }
    }
    return result;
}

From source file:research.sg.edu.edapp.kb.KbSoftKeyboard.java

public void onText(CharSequence text) {
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//www  .  j  a  va2s .c  o m
    ic.beginBatchEdit();
    if (mComposing.length() > 0) {
        commitTyped(ic);
    }
    ic.commitText(text, 0);
    ic.endBatchEdit();
    updateShiftKeyState(getCurrentInputEditorInfo());
}

From source file:android.support.text.emoji.EmojiProcessor.java

/**
 * Handles deleteSurroundingText commands from {@link InputConnection} and tries to delete an
 * {@link EmojiSpan} from an {@link Editable}. Returns {@code true} if an {@link EmojiSpan} is
 * deleted./*from w  ww. j av a 2  s .  co  m*/
 * <p/>
 * If there is a selection where selection start is not equal to selection end, does not
 * delete.
 *
 * @param inputConnection InputConnection instance
 * @param editable TextView.Editable instance
 * @param beforeLength the number of characters before the cursor to be deleted
 * @param afterLength the number of characters after the cursor to be deleted
 * @param inCodePoints {@code true} if length parameters are in codepoints
 *
 * @return {@code true} if an {@link EmojiSpan} is deleted
 */
static boolean handleDeleteSurroundingText(@NonNull final InputConnection inputConnection,
        @NonNull final Editable editable, @IntRange(from = 0) final int beforeLength,
        @IntRange(from = 0) final int afterLength, final boolean inCodePoints) {
    //noinspection ConstantConditions
    if (editable == null || inputConnection == null) {
        return false;
    }

    if (beforeLength < 0 || afterLength < 0) {
        return false;
    }

    final int selectionStart = Selection.getSelectionStart(editable);
    final int selectionEnd = Selection.getSelectionEnd(editable);

    if (hasInvalidSelection(selectionStart, selectionEnd)) {
        return false;
    }

    int start;
    int end;
    if (inCodePoints) {
        // go backwards in terms of codepoints
        start = CodepointIndexFinder.findIndexBackward(editable, selectionStart, Math.max(beforeLength, 0));
        end = CodepointIndexFinder.findIndexForward(editable, selectionEnd, Math.max(afterLength, 0));

        if (start == CodepointIndexFinder.INVALID_INDEX || end == CodepointIndexFinder.INVALID_INDEX) {
            return false;
        }
    } else {
        start = Math.max(selectionStart - beforeLength, 0);
        end = Math.min(selectionEnd + afterLength, editable.length());
    }

    final EmojiSpan[] spans = editable.getSpans(start, end, EmojiSpan.class);
    if (spans != null && spans.length > 0) {
        final int length = spans.length;
        for (int index = 0; index < length; index++) {
            final EmojiSpan span = spans[index];
            int spanStart = editable.getSpanStart(span);
            int spanEnd = editable.getSpanEnd(span);
            start = Math.min(spanStart, start);
            end = Math.max(spanEnd, end);
        }

        start = Math.max(start, 0);
        end = Math.min(end, editable.length());

        inputConnection.beginBatchEdit();
        editable.delete(start, end);
        inputConnection.endBatchEdit();
        return true;
    }

    return false;
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

public void onMultiTapEnded() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic != null)
        ic.endBatchEdit();
    updateShiftStateNow();//from w  w  w.  j a v a 2s .  c  om
}

From source file:com.yek.keyboard.anysoftkeyboard.AnySoftKeyboard.java

public void onText(Keyboard.Key key, CharSequence text) {
    Logger.d(TAG, "onText: '%s'", text);
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//w w  w.  j a v a 2  s . co m
    ic.beginBatchEdit();

    abortCorrectionAndResetPredictionState(false);
    ic.commitText(text, 1);

    mJustAddedAutoSpace = false;
    mCommittedWord = text;
    mUndoCommitCursorPosition = UNDO_COMMIT_WAITING_TO_RECORD_POSITION;

    TextEntryState.acceptedDefault(text);
    ic.endBatchEdit();

    setSuggestions(mSuggest.getNextSuggestions(mCommittedWord, false), false, false, false);
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

public void revertLastWord() {
    final int length = mCommittedWord.length() + (mJustAddedAutoSpace ? 1 : 0);
    if (length > 0) {
        mAutoCorrectOn = false;/*from   w  ww  .j  a va2  s .  c  om*/
        //note: typedWord may be empty
        final InputConnection ic = getCurrentInputConnection();
        mUndoCommitCursorPosition = UNDO_COMMIT_NONE;
        ic.beginBatchEdit();
        ic.deleteSurroundingText(length, 0);
        final CharSequence typedWord = mWord.getTypedWord();
        ic.setComposingText(typedWord/* mComposing */, 1);
        TextEntryState.backspace();
        ic.endBatchEdit();
        performUpdateSuggestions();
        if (mJustAutoAddedWord) {
            removeFromUserDictionary(typedWord.toString());
        }
        getInputView().revertPopTextOutOfKey();
    } else {
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

public void onText(Key key, CharSequence text) {
    Logger.d(TAG, "onText: '%s'", text);
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from  w  w  w.j  a v a 2  s  .  co m*/
    ic.beginBatchEdit();

    abortCorrectionAndResetPredictionState(false);
    ic.commitText(text, 1);

    mJustAddedAutoSpace = false;
    mCommittedWord = text;
    mUndoCommitCursorPosition = UNDO_COMMIT_WAITING_TO_RECORD_POSITION;

    TextEntryState.acceptedDefault(text);
    ic.endBatchEdit();

    setSuggestions(mSuggest.getNextSuggestions(mCommittedWord, false), false, false, false);
}

From source file:com.yek.keyboard.anysoftkeyboard.AnySoftKeyboard.java

private void handleCharacter(final int primaryCode, final Keyboard.Key key, final int multiTapIndex,
        int[] nearByKeyCodes) {
    if (BuildConfig.DEBUG)
        Logger.d(TAG, "handleCharacter: %d, isPredictionOn: %s, mPredicting: %s", primaryCode, isPredictionOn(),
                TextEntryState.isPredicting());

    mExpectingSelectionUpdateBy = SystemClock.uptimeMillis() + MAX_TIME_TO_EXPECT_SELECTION_UPDATE;
    if (TextEntryState.isReadyToPredict() && isAlphabet(primaryCode) && !isCursorTouchingWord()) {
        TextEntryState.newSession(mPredictionOn);
        mUndoCommitCursorPosition = UNDO_COMMIT_NONE;
        mWord.reset();/*from www.j  a  v  a  2s .c  o m*/
        mAutoCorrectOn = mAutoComplete;
        TextEntryState.typedCharacter((char) primaryCode, false);
        if (mShiftKeyState.isActive()) {
            mWord.setFirstCharCapitalized(true);
        }
    } else if (TextEntryState.isPredicting()) {
        TextEntryState.typedCharacter((char) primaryCode, false);
    }

    mLastCharacterWasShifted = (getInputView() != null) && getInputView().isShifted();

    if (TextEntryState.isPredicting()) {
        final InputConnection ic = getCurrentInputConnection();
        mWord.add(primaryCode, nearByKeyCodes);
        ChewbaccaOnTheDrums.onKeyTyped(mWord, getApplicationContext());

        if (ic != null) {
            final int cursorPosition;
            if (mWord.cursorPosition() != mWord.length()) {
                //Cursor is not at the end of the word. I'll need to reposition
                cursorPosition = mGlobalCursorPosition + 1/*adding the new character*/;
                ic.beginBatchEdit();
            } else {
                cursorPosition = -1;
            }

            ic.setComposingText(mWord.getTypedWord(), 1);
            if (cursorPosition > 0) {
                ic.setSelection(cursorPosition, cursorPosition);
                ic.endBatchEdit();
            }
        }
        // this should be done ONLY if the key is a letter, and not a inner
        // character (like ').
        if (isSuggestionAffectingCharacter(primaryCode)) {
            postUpdateSuggestions();
        } else {
            // just replace the typed word in the candidates view
            if (mCandidateView != null)
                mCandidateView.replaceTypedWord(mWord.getTypedWord());
        }
    } else {
        sendKeyChar((char) primaryCode);
    }
    mJustAutoAddedWord = false;
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void handleCharacter(final int primaryCode, final Key key, final int multiTapIndex,
        int[] nearByKeyCodes) {
    if (BuildConfig.DEBUG)
        Logger.d(TAG, "handleCharacter: %d, isPredictionOn: %s, mPredicting: %s", primaryCode, isPredictionOn(),
                TextEntryState.isPredicting());

    mExpectingSelectionUpdateBy = SystemClock.uptimeMillis() + MAX_TIME_TO_EXPECT_SELECTION_UPDATE;
    if (TextEntryState.isReadyToPredict() && isAlphabet(primaryCode) && !isCursorTouchingWord()) {
        TextEntryState.newSession(mPredictionOn);
        mUndoCommitCursorPosition = UNDO_COMMIT_NONE;
        mWord.reset();/*from  w  ww .ja v a 2  s.  co m*/
        mAutoCorrectOn = mAutoComplete;
        TextEntryState.typedCharacter((char) primaryCode, false);
        if (mShiftKeyState.isActive()) {
            mWord.setFirstCharCapitalized(true);
        }
    } else if (TextEntryState.isPredicting()) {
        TextEntryState.typedCharacter((char) primaryCode, false);
    }

    mLastCharacterWasShifted = (getInputView() != null) && getInputView().isShifted();

    if (TextEntryState.isPredicting()) {
        final InputConnection ic = getCurrentInputConnection();
        mWord.add(primaryCode, nearByKeyCodes);
        ChewbaccaOnTheDrums.onKeyTyped(mWord, getApplicationContext());

        if (ic != null) {
            final int cursorPosition;
            if (mWord.cursorPosition() != mWord.length()) {
                //Cursor is not at the end of the word. I'll need to reposition
                cursorPosition = mGlobalCursorPosition + 1/*adding the new character*/;
                ic.beginBatchEdit();
            } else {
                cursorPosition = -1;
            }

            ic.setComposingText(mWord.getTypedWord(), 1);
            if (cursorPosition > 0) {
                ic.setSelection(cursorPosition, cursorPosition);
                ic.endBatchEdit();
            }
        }
        // this should be done ONLY if the key is a letter, and not a inner
        // character (like ').
        if (isSuggestionAffectingCharacter(primaryCode)) {
            postUpdateSuggestions();
        } else {
            // just replace the typed word in the candidates view
            if (mCandidateView != null)
                mCandidateView.replaceTypedWord(mWord.getTypedWord());
        }
    } else {
        sendKeyChar((char) primaryCode);
    }
    mJustAutoAddedWord = false;
}