Example usage for android.view.inputmethod InputConnection beginBatchEdit

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

Introduction

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

Prototype

boolean beginBatchEdit();

Source Link

Document

Tell the editor that you are starting a batch of editor operations.

Usage

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 .ja  v  a  2  s. 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();//  w w  w.j a  v a 2s . c  om
        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 handleDeleteLastCharacter(boolean forMultiTap) {
    InputConnection ic = getCurrentInputConnection();

    boolean deleteChar = false;
    if (TextEntryState.isPredicting()) {
        final boolean wordManipulation = mWord.length() > 0 && mWord.cursorPosition() > 0;
        if (wordManipulation) {
            mWord.deleteLast();//from   w  w w.  j  av a2s .c o  m
            final int cursorPosition;
            if (mWord.cursorPosition() != mWord.length())
                cursorPosition = getCursorPosition(ic);
            else
                cursorPosition = -1;

            if (cursorPosition >= 0)
                ic.beginBatchEdit();

            ic.setComposingText(mWord.getTypedWord(), 1);
            if (mWord.length() == 0) {
                TextEntryState.newSession(mPredictionOn);
            } else if (cursorPosition >= 0) {
                ic.setSelection(cursorPosition - 1, cursorPosition - 1);
            }

            if (cursorPosition >= 0)
                ic.endBatchEdit();

            postUpdateSuggestions();
        } else {
            ic.deleteSurroundingText(1, 0);
        }
    } else {
        deleteChar = true;
    }

    TextEntryState.backspace();
    if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) {
        revertLastWord();
    } else if (deleteChar) {
        //just making sure that
        if (mCandidateView != null)
            mCandidateView.dismissAddToDictionaryHint();

        if (!forMultiTap) {
            sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
        } else {
            // this code tries to delete the text in a different way,
            // because of multi-tap stuff
            // using "deleteSurroundingText" will actually get the input
            // updated faster!
            // but will not handle "delete all selected text" feature,
            // hence the "if (!forMultiTap)" above
            final CharSequence beforeText = ic == null ? null : ic.getTextBeforeCursor(1, 0);
            final int textLengthBeforeDelete = (TextUtils.isEmpty(beforeText)) ? 0 : beforeText.length();
            if (textLengthBeforeDelete > 0)
                ic.deleteSurroundingText(1, 0);
            else
                sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
        }
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

public void performRestartWordSuggestion(final InputConnection ic) {
    // I assume ASK DOES NOT predict at this moment!

    // 2) predicting and moved outside the word - abort predicting, update
    // shift state
    // 2.1) to a new word - restart predicting on the new word
    // 2.2) to no word land - nothing else

    // this means that the new cursor position is outside the candidates
    // underline/*from www  .j  a va  2  s  .  c o m*/
    // this can be either because the cursor is really outside the
    // previously underlined (suggested)
    // or nothing was suggested.
    // in this case, we would like to reset the prediction and restart
    // if the user clicked inside a different word
    // restart required?
    if (canRestartWordSuggestion()) {// 2.1
        ic.beginBatchEdit();// don't want any events till I finish handling
        // this touch
        abortCorrectionAndResetPredictionState(false);

        // locating the word
        CharSequence toLeft = "";
        CharSequence toRight = "";
        while (true) {
            CharSequence newToLeft = ic.getTextBeforeCursor(toLeft.length() + 1, 0);
            if (TextUtils.isEmpty(newToLeft) || isWordSeparator(newToLeft.charAt(0))
                    || newToLeft.length() == toLeft.length()) {
                break;
            }
            toLeft = newToLeft;
        }
        while (true) {
            CharSequence newToRight = ic.getTextAfterCursor(toRight.length() + 1, 0);
            if (TextUtils.isEmpty(newToRight) || isWordSeparator(newToRight.charAt(newToRight.length() - 1))
                    || newToRight.length() == toRight.length()) {
                break;
            }
            toRight = newToRight;
        }
        CharSequence word = toLeft.toString() + toRight.toString();
        Logger.d(TAG, "Starting new prediction on word '%s'.", word);
        mUndoCommitCursorPosition = UNDO_COMMIT_NONE;
        mWord.reset();

        final int[] tempNearByKeys = new int[1];

        for (int index = 0; index < word.length(); index++) {
            final char c = word.charAt(index);
            if (index == 0)
                mWord.setFirstCharCapitalized(Character.isUpperCase(c));

            tempNearByKeys[0] = c;
            mWord.add(c, tempNearByKeys);

            TextEntryState.typedCharacter(c, false);
        }
        ic.deleteSurroundingText(toLeft.length(), toRight.length());
        ic.setComposingText(word, 1);
        // repositioning the cursor
        if (toRight.length() > 0) {
            final int cursorPosition = getCursorPosition(ic) - toRight.length();
            Logger.d(TAG, "Repositioning the cursor inside the word to position %d", cursorPosition);
            ic.setSelection(cursorPosition, cursorPosition);
        }

        mWord.setCursorPosition(toLeft.length());
        ic.endBatchEdit();
        postUpdateSuggestions();
    } else {
        Logger.d(TAG, "performRestartWordSuggestion canRestartWordSuggestion == false");
    }
}

From source file:org.pocketworkstation.pckeyboard.LatinIME.java

public void pickSuggestionManually(int index, CharSequence suggestion) {
    List<CharSequence> suggestions = mCandidateView.getSuggestions();

    final boolean correcting = TextEntryState.isCorrecting();
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.beginBatchEdit();
    }/* w w w  .  j  a v  a2 s .co m*/
    if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
        CompletionInfo ci = mCompletions[index];
        if (ic != null) {
            ic.commitCompletion(ci);
        }
        mCommittedLength = suggestion.length();
        if (mCandidateView != null) {
            mCandidateView.clear();
        }
        updateShiftKeyState(getCurrentInputEditorInfo());
        if (ic != null) {
            ic.endBatchEdit();
        }
        return;
    }

    // If this is a punctuation, apply it through the normal key press
    if (suggestion.length() == 1
            && (isWordSeparator(suggestion.charAt(0)) || isSuggestedPunctuation(suggestion.charAt(0)))) {
        final char primaryCode = suggestion.charAt(0);
        onKey(primaryCode, new int[] { primaryCode }, LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
                LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
        if (ic != null) {
            ic.endBatchEdit();
        }
        return;
    }
    mJustAccepted = true;
    pickSuggestion(suggestion, correcting);
    // Add the word to the auto dictionary if it's not a known word
    if (index == 0) {
        addToDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED);
    } else {
        addToBigramDictionary(suggestion, 1);
    }
    TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion);
    // Follow it with a space
    if (mAutoSpace && !correcting) {
        sendSpace();
        mJustAddedAutoSpace = true;
    }

    final boolean showingAddToDictionaryHint = index == 0 && mCorrectionMode > 0
            && !mSuggest.isValidWord(suggestion) && !mSuggest.isValidWord(suggestion.toString().toLowerCase());

    if (!correcting) {
        // Fool the state watcher so that a subsequent backspace will not do
        // a revert, unless
        // we just did a correction, in which case we need to stay in
        // TextEntryState.State.PICKED_SUGGESTION state.
        TextEntryState.typedCharacter((char) ASCII_SPACE, true);
        setNextSuggestions();
    } else if (!showingAddToDictionaryHint) {
        // If we're not showing the "Touch again to save", then show
        // corrections again.
        // In case the cursor position doesn't change, make sure we show the
        // suggestions again.
        clearSuggestions();
        postUpdateOldSuggestions();
    }
    if (showingAddToDictionaryHint) {
        mCandidateView.showAddToDictionaryHint(suggestion);
    }
    if (ic != null) {
        ic.endBatchEdit();
    }
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private void handleSeparator(int primaryCode) {

    // Should dismiss the "Touch again to save" message when handling
    // separator/*from w w w. j  a v  a  2 s  .  c  om*/
    if (mCandidateView != null && mCandidateView.dismissAddToDictionaryHint()) {
        postUpdateSuggestions();
    }

    boolean pickedDefault = false;
    // Handle separator
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.beginBatchEdit();
        abortCorrection(false);
    }
    if (mPredicting) {
        // In certain languages where single quote is a separator, it's
        // better
        // not to auto correct, but accept the typed word. For instance,
        // in Italian dov' should not be expanded to dove' because the
        // elision
        // requires the last vowel to be removed.
        if (mAutoCorrectOn && primaryCode != '\'' && (mJustRevertedSeparator == null
                || mJustRevertedSeparator.length() == 0 || mJustRevertedSeparator.charAt(0) != primaryCode)) {
            pickedDefault = pickDefaultSuggestion();
            // Picked the suggestion by the space key. We consider this
            // as "added an auto space" in autocomplete mode, but as manually
            // typed space in "quick fixes" mode.
            if (primaryCode == ASCII_SPACE) {
                if (mAutoCorrectEnabled) {
                    mJustAddedAutoSpace = true;
                } else {
                    TextEntryState.manualTyped("");
                }
            }
        } else {
            commitTyped(ic, true);
        }
    }
    if (mJustAddedAutoSpace && primaryCode == ASCII_ENTER) {
        removeTrailingSpace();
        mJustAddedAutoSpace = false;
    }
    sendModifiableKeyChar((char) primaryCode);

    // Handle the case of ". ." -> " .." with auto-space if necessary
    // before changing the TextEntryState.
    if (TextEntryState.getState() == TextEntryState.State.PUNCTUATION_AFTER_ACCEPTED
            && primaryCode == ASCII_PERIOD) {
        reswapPeriodAndSpace();
    }

    TextEntryState.typedCharacter((char) primaryCode, true);
    if (TextEntryState.getState() == TextEntryState.State.PUNCTUATION_AFTER_ACCEPTED
            && primaryCode != ASCII_ENTER) {
        swapPunctuationAndSpace();
    } else if (isPredictionOn() && primaryCode == ASCII_SPACE) {
        doubleSpace();
    }
    if (pickedDefault) {
        TextEntryState.backToAcceptedDefault(mWord.getTypedWord());
    }
    updateShiftKeyState(getCurrentInputEditorInfo());
    if (ic != null) {
        ic.endBatchEdit();
    }
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private void handleBackspace() {
    boolean deleteChar = false;
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from ww w  .ja  v  a  2  s  . c  o m*/

    ic.beginBatchEdit();

    if (mPredicting) {
        final int length = mComposing.length();
        if (length > 0) {
            mComposing.delete(length - 1, length);
            mWord.deleteLast();
            ic.setComposingText(mComposing, 1);
            if (mComposing.length() == 0) {
                mPredicting = false;
            }
            postUpdateSuggestions();
        } else {
            ic.deleteSurroundingText(1, 0);
        }
    } else {
        deleteChar = true;
    }
    postUpdateShiftKeyState();
    TextEntryState.backspace();
    if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) {
        revertLastWord(deleteChar);
        ic.endBatchEdit();
        return;
    } else if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) {
        ic.deleteSurroundingText(mEnteredText.length(), 0);
    } else if (deleteChar) {
        if (mCandidateView != null && mCandidateView.dismissAddToDictionaryHint()) {
            // Go back to the suggestion mode if the user canceled the
            // "Touch again to save".
            // NOTE: In gerenal, we don't revert the word when backspacing
            // from a manual suggestion pick. We deliberately chose a
            // different behavior only in the case of picking the first
            // suggestion (typed word). It's intentional to have made this
            // inconsistent with backspacing after selecting other
            // suggestions.
            revertLastWord(deleteChar);
        } else {
            sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
            if (mDeleteCount > DELETE_ACCELERATE_AT) {
                sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
            }
        }
    }
    mJustRevertedSeparator = null;
    ic.endBatchEdit();
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

public void pickSuggestionManually(int index, CharSequence suggestion) {
    List<CharSequence> suggestions = mCandidateView.getSuggestions();

    final boolean correcting = TextEntryState.isCorrecting();
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.beginBatchEdit();
    }//www .j av  a 2 s . c o  m
    if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
        CompletionInfo ci = mCompletions[index];
        if (ic != null) {
            ic.commitCompletion(ci);
        }
        mCommittedLength = suggestion.length();
        if (mCandidateView != null) {
            mCandidateView.clear();
        }
        updateShiftKeyState(getCurrentInputEditorInfo());
        if (ic != null) {
            ic.endBatchEdit();
        }
        return;
    }

    // If this is a punctuation, apply it through the normal key press
    if (suggestion.length() == 1
            && (isWordSeparator(suggestion.charAt(0)) || isSuggestedPunctuation(suggestion.charAt(0)))) {
        // Word separators are suggested before the user inputs something.
        // So, TKIMELogger logs "" as a user's input.
        TKIMELogger.logOnManualSuggestion("", suggestion.toString(), index, suggestions);
        final char primaryCode = suggestion.charAt(0);
        onKey(primaryCode, new int[] { primaryCode }, BaseKeyboardView.NOT_A_TOUCH_COORDINATE,
                BaseKeyboardView.NOT_A_TOUCH_COORDINATE);
        if (ic != null) {
            ic.endBatchEdit();
        }
        return;
    }
    mJustAccepted = true;
    pickSuggestion(suggestion, correcting);
    // Add the word to the auto dictionary if it's not a known word
    if (index == 0) {
        addToDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED);
    } else {
        addToBigramDictionary(suggestion, 1);
    }
    TKIMELogger.logOnManualSuggestion(mComposing.toString(), suggestion.toString(), index, suggestions);
    TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion);
    // Follow it with a space
    if (mAutoSpace && !correcting) {
        sendSpace();
        mJustAddedAutoSpace = true;
    }

    final boolean showingAddToDictionaryHint = index == 0 && mCorrectionMode > 0
            && !mSuggest.isValidWord(suggestion) && !mSuggest.isValidWord(suggestion.toString().toLowerCase());

    if (!correcting) {
        // Fool the state watcher so that a subsequent backspace will not do
        // a revert, unless
        // we just did a correction, in which case we need to stay in
        // TextEntryState.State.PICKED_SUGGESTION state.
        TextEntryState.typedCharacter((char) ASCII_SPACE, true);
        setNextSuggestions();
    } else if (!showingAddToDictionaryHint) {
        // If we're not showing the "Touch again to save", then show
        // corrections again.
        // In case the cursor position doesn't change, make sure we show the
        // suggestions again.
        clearSuggestions();
        postUpdateOldSuggestions();
    }
    if (showingAddToDictionaryHint) {
        mCandidateView.showAddToDictionaryHint(suggestion);
    }
    if (ic != null) {
        ic.endBatchEdit();
    }
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from   w ww . ja va2s . co  m*/
    CharSequence lastTwo = ic.getTextBeforeCursor(2, 0);
    if (lastTwo != null && lastTwo.length() == 2 && lastTwo.charAt(0) == ASCII_SPACE
            && isSentenceSeparator(lastTwo.charAt(1))) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(2, 0);
        ic.commitText(lastTwo.charAt(1) + " ", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
        mJustAddedAutoSpace = true;
    }
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private void reswapPeriodAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*  www .  ja  v a  2  s . com*/
    CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
    if (lastThree != null && lastThree.length() == 3 && lastThree.charAt(0) == ASCII_PERIOD
            && lastThree.charAt(1) == ASCII_SPACE && lastThree.charAt(2) == ASCII_PERIOD) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(3, 0);
        ic.commitText(" ..", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
    }
}