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.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();/*w  w w  . j  a  va2 s .c om*/
            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:org.distantshoresmedia.keyboard.LatinIME.java

private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//from   w  ww.j a  va 2 s .  c o 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;/*from w  ww.j av a2 s .co  m*/
    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());
    }
}

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

private void doubleSpace() {
    // if (!mAutoPunctuate) return;
    if (mCorrectionMode == Suggest.CORRECTION_NONE)
        return;//from   w  ww  .ja v  a  2s.  com
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
    if (lastThree != null && lastThree.length() == 3 && Character.isLetterOrDigit(lastThree.charAt(0))
            && lastThree.charAt(1) == ASCII_SPACE && lastThree.charAt(2) == ASCII_SPACE) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(2, 0);
        ic.commitText(". ", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
        mJustAddedAutoSpace = true;
    }
}

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();//from w w w  .j av a2  s.com
    }
    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

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();/*from   w ww.j  a  va2  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)))) {
        // 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

public void onText(CharSequence text) {
    //mDeadAccentBuffer.clear();  // FIXME
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//from  ww w.  j  a  v  a 2s .com
    if (mPredicting && text.length() == 1) {
        // If adding a single letter, treat it as a regular keystroke so
        // that completion works as expected.
        int c = text.charAt(0);
        if (!isWordSeparator(c)) {
            int[] codes = { c };
            handleCharacter(c, codes);
            return;
        }
    }
    abortCorrection(false);
    ic.beginBatchEdit();
    if (mPredicting) {
        commitTyped(ic, true);
    }
    maybeRemovePreviousPeriod(text);
    ic.commitText(text, 1);
    ic.endBatchEdit();
    updateShiftKeyState(getCurrentInputEditorInfo());
    mKeyboardSwitcher.onKey(0); // dummy key code.
    mJustRevertedSeparator = null;
    mJustAddedAutoSpace = false;
    mEnteredText = text;
}

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

private void onFunctionKey(final int primaryCode, final Keyboard.Key key, final int multiTapIndex,
        final int[] nearByKeyCodes, final boolean fromUI) {
    if (BuildConfig.DEBUG)
        Logger.d(TAG, "onFunctionKey %d", primaryCode);

    final InputConnection ic = getCurrentInputConnection();

    switch (primaryCode) {
    case KeyCodes.DELETE:
        if (ic == null)// if we don't want to do anything, lets check null first.
            break;
        // we do backword if the shift is pressed while pressing
        // backspace (like in a PC)
        if (mAskPrefs.useBackword() && mShiftKeyState.isPressed() && !mShiftKeyState.isLocked()) {
            handleBackWord(ic);// w  ww .j  a  v a  2  s . c  o  m
        } else {
            handleDeleteLastCharacter(false);
        }
        break;
    case KeyCodes.SHIFT:
        if (fromUI) {
            handleShift();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.SHIFT_LOCK:
        mShiftKeyState.toggleLocked();
        handleShift();
        break;
    case KeyCodes.DELETE_WORD:
        if (ic == null)// if we don't want to do anything, lets check
            // null first.
            break;
        handleBackWord(ic);
        break;
    case KeyCodes.CLEAR_INPUT:
        if (ic != null) {
            ic.beginBatchEdit();
            abortCorrectionAndResetPredictionState(false);
            ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
            ic.endBatchEdit();
        }
        break;
    case KeyCodes.CTRL:
        if (fromUI) {
            handleControl();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.CTRL_LOCK:
        mControlKeyState.toggleLocked();
        handleControl();
        break;
    case KeyCodes.ARROW_LEFT:
    case KeyCodes.ARROW_RIGHT:
        final int keyEventKeyCode = primaryCode == KeyCodes.ARROW_LEFT ? KeyEvent.KEYCODE_DPAD_LEFT
                : KeyEvent.KEYCODE_DPAD_RIGHT;
        if (!handleSelectionExpending(keyEventKeyCode, ic, mGlobalSelectionStartPosition,
                mGlobalCursorPosition)) {
            sendDownUpKeyEvents(keyEventKeyCode);
        }
        break;
    case KeyCodes.ARROW_UP:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP);
        break;
    case KeyCodes.ARROW_DOWN:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_DOWN);
        break;
    case KeyCodes.MOVE_HOME:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sendDownUpKeyEvents(0x0000007a/*API 11:KeyEvent.KEYCODE_MOVE_HOME*/);
        } else {
            if (ic != null) {
                CharSequence textBefore = ic.getTextBeforeCursor(1024, 0);
                if (!TextUtils.isEmpty(textBefore)) {
                    int newPosition = textBefore.length() - 1;
                    while (newPosition > 0) {
                        char chatAt = textBefore.charAt(newPosition - 1);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition--;
                    }
                    if (newPosition < 0)
                        newPosition = 0;
                    ic.setSelection(newPosition, newPosition);
                }
            }
        }
        break;
    case KeyCodes.MOVE_END:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //API 11: KeyEvent.KEYCODE_MOVE_END
            sendDownUpKeyEvents(0x0000007b);
        } else {
            if (ic != null) {
                CharSequence textAfter = ic.getTextAfterCursor(1024, 0);
                if (!TextUtils.isEmpty(textAfter)) {
                    int newPosition = 1;
                    while (newPosition < textAfter.length()) {
                        char chatAt = textAfter.charAt(newPosition);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition++;
                    }
                    if (newPosition > textAfter.length())
                        newPosition = textAfter.length();
                    try {
                        CharSequence textBefore = ic.getTextBeforeCursor(Integer.MAX_VALUE, 0);
                        if (!TextUtils.isEmpty(textBefore)) {
                            newPosition = newPosition + textBefore.length();
                        }
                        ic.setSelection(newPosition, newPosition);
                    } catch (Throwable e/*I'm using Integer.MAX_VALUE, it's scary.*/) {
                        Logger.w(TAG, "Failed to getTextBeforeCursor.", e);
                    }
                }
            }
        }
        break;
    case KeyCodes.VOICE_INPUT:
        if (mVoiceRecognitionTrigger.isInstalled()) {
            mVoiceRecognitionTrigger
                    .startVoiceRecognition(getCurrentAlphabetKeyboard().getDefaultDictionaryLocale());
        } else {
            Intent voiceInputNotInstalledIntent = new Intent(getApplicationContext(),
                    VoiceInputNotInstalledActivity.class);
            voiceInputNotInstalledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(voiceInputNotInstalledIntent);
        }
        break;
    case KeyCodes.CANCEL:
        handleClose();
        break;
    case KeyCodes.SETTINGS:
        showOptionsMenu();
        break;
    case KeyCodes.SPLIT_LAYOUT:
    case KeyCodes.MERGE_LAYOUT:
    case KeyCodes.COMPACT_LAYOUT_TO_RIGHT:
    case KeyCodes.COMPACT_LAYOUT_TO_LEFT:
        if (getInputView() != null) {
            mKeyboardInCondensedMode = CondenseType.fromKeyCode(primaryCode);
            setKeyboardForView(getCurrentKeyboard());
        }
        break;
    case KeyCodes.DOMAIN:
        onText(key, mAskPrefs.getDomainText());
        break;
    case KeyCodes.QUICK_TEXT:
        onQuickTextRequested(key);
        break;
    case KeyCodes.QUICK_TEXT_POPUP:
        onQuickTextKeyboardRequested(key);
        break;
    case KeyCodes.MODE_SYMOBLS:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Symbols);
        break;
    case KeyCodes.MODE_ALPHABET:
        if (getKeyboardSwitcher().shouldPopupForLanguageSwitch()) {
            showLanguageSelectionDialog();
        } else {
            nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Alphabet);
        }
        break;
    case KeyCodes.UTILITY_KEYBOARD:
        getInputView().openUtilityKeyboard();
        break;
    case KeyCodes.MODE_ALPHABET_POPUP:
        showLanguageSelectionDialog();
        break;
    case KeyCodes.ALT:
        nextAlterKeyboard(getCurrentInputEditorInfo());
        break;
    case KeyCodes.KEYBOARD_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Any);
        break;
    case KeyCodes.KEYBOARD_REVERSE_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.PreviousAny);
        break;
    case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.AnyInsideMode);
        break;
    case KeyCodes.KEYBOARD_MODE_CHANGE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.OtherMode);
        break;
    case KeyCodes.CLIPBOARD_COPY:
    case KeyCodes.CLIPBOARD_PASTE:
    case KeyCodes.CLIPBOARD_CUT:
    case KeyCodes.CLIPBOARD_SELECT_ALL:
    case KeyCodes.CLIPBOARD_PASTE_POPUP:
    case KeyCodes.CLIPBOARD_SELECT:
        handleClipboardOperation(key, primaryCode, ic);
        //not allowing undo on-text in clipboard paste operations.
        if (primaryCode == KeyCodes.CLIPBOARD_PASTE)
            mCommittedWord = "";
        break;
    default:
        if (BuildConfig.DEBUG) {
            //this should not happen! We should handle ALL function keys.
            throw new RuntimeException("UNHANDLED FUNCTION KEY! primary code " + primaryCode);
        } else {
            Logger.w(TAG, "UNHANDLED FUNCTION KEY! primary code %d. Ignoring.", primaryCode);
        }
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void onFunctionKey(final int primaryCode, final Key key, final int multiTapIndex,
        final int[] nearByKeyCodes, final boolean fromUI) {
    if (BuildConfig.DEBUG)
        Logger.d(TAG, "onFunctionKey %d", primaryCode);

    final InputConnection ic = getCurrentInputConnection();

    switch (primaryCode) {
    case KeyCodes.DELETE:
        if (ic == null)// if we don't want to do anything, lets check null first.
            break;
        // we do backword if the shift is pressed while pressing
        // backspace (like in a PC)
        if (mAskPrefs.useBackword() && mShiftKeyState.isPressed() && !mShiftKeyState.isLocked()) {
            handleBackWord(ic);//from w ww. ja va 2  s . c  o  m
        } else {
            handleDeleteLastCharacter(false);
        }
        break;
    case KeyCodes.SHIFT:
        if (fromUI) {
            handleShift();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.SHIFT_LOCK:
        mShiftKeyState.toggleLocked();
        handleShift();
        break;
    case KeyCodes.DELETE_WORD:
        if (ic == null)// if we don't want to do anything, lets check
            // null first.
            break;
        handleBackWord(ic);
        break;
    case KeyCodes.CLEAR_INPUT:
        if (ic != null) {
            ic.beginBatchEdit();
            abortCorrectionAndResetPredictionState(false);
            ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
            ic.endBatchEdit();
        }
        break;
    case KeyCodes.CTRL:
        if (fromUI) {
            handleControl();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.CTRL_LOCK:
        mControlKeyState.toggleLocked();
        handleControl();
        break;
    case KeyCodes.ARROW_LEFT:
    case KeyCodes.ARROW_RIGHT:
        final int keyEventKeyCode = primaryCode == KeyCodes.ARROW_LEFT ? KeyEvent.KEYCODE_DPAD_LEFT
                : KeyEvent.KEYCODE_DPAD_RIGHT;
        if (!handleSelectionExpending(keyEventKeyCode, ic, mGlobalSelectionStartPosition,
                mGlobalCursorPosition)) {
            sendDownUpKeyEvents(keyEventKeyCode);
        }
        break;
    case KeyCodes.ARROW_UP:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP);
        break;
    case KeyCodes.ARROW_DOWN:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_DOWN);
        break;
    case KeyCodes.MOVE_HOME:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sendDownUpKeyEvents(0x0000007a/*API 11:KeyEvent.KEYCODE_MOVE_HOME*/);
        } else {
            if (ic != null) {
                CharSequence textBefore = ic.getTextBeforeCursor(1024, 0);
                if (!TextUtils.isEmpty(textBefore)) {
                    int newPosition = textBefore.length() - 1;
                    while (newPosition > 0) {
                        char chatAt = textBefore.charAt(newPosition - 1);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition--;
                    }
                    if (newPosition < 0)
                        newPosition = 0;
                    ic.setSelection(newPosition, newPosition);
                }
            }
        }
        break;
    case KeyCodes.MOVE_END:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //API 11: KeyEvent.KEYCODE_MOVE_END
            sendDownUpKeyEvents(0x0000007b);
        } else {
            if (ic != null) {
                CharSequence textAfter = ic.getTextAfterCursor(1024, 0);
                if (!TextUtils.isEmpty(textAfter)) {
                    int newPosition = 1;
                    while (newPosition < textAfter.length()) {
                        char chatAt = textAfter.charAt(newPosition);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition++;
                    }
                    if (newPosition > textAfter.length())
                        newPosition = textAfter.length();
                    try {
                        CharSequence textBefore = ic.getTextBeforeCursor(Integer.MAX_VALUE, 0);
                        if (!TextUtils.isEmpty(textBefore)) {
                            newPosition = newPosition + textBefore.length();
                        }
                        ic.setSelection(newPosition, newPosition);
                    } catch (Throwable e/*I'm using Integer.MAX_VALUE, it's scary.*/) {
                        Logger.w(TAG, "Failed to getTextBeforeCursor.", e);
                    }
                }
            }
        }
        break;
    case KeyCodes.VOICE_INPUT:
        if (mVoiceRecognitionTrigger.isInstalled()) {
            mVoiceRecognitionTrigger
                    .startVoiceRecognition(getCurrentAlphabetKeyboard().getDefaultDictionaryLocale());
        } else {
            Intent voiceInputNotInstalledIntent = new Intent(getApplicationContext(),
                    VoiceInputNotInstalledActivity.class);
            voiceInputNotInstalledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(voiceInputNotInstalledIntent);
        }
        break;
    case KeyCodes.CANCEL:
        hideWindow();
        break;
    case KeyCodes.SETTINGS:
        showOptionsMenu();
        break;
    case KeyCodes.SPLIT_LAYOUT:
    case KeyCodes.MERGE_LAYOUT:
    case KeyCodes.COMPACT_LAYOUT_TO_RIGHT:
    case KeyCodes.COMPACT_LAYOUT_TO_LEFT:
        if (getInputView() != null) {
            mKeyboardInCondensedMode = CondenseType.fromKeyCode(primaryCode);
            setKeyboardForView(getCurrentKeyboard());
        }
        break;
    case KeyCodes.DOMAIN:
        onText(key, mAskPrefs.getDomainText());
        break;
    case KeyCodes.QUICK_TEXT:
        onQuickTextRequested(key);
        break;
    case KeyCodes.QUICK_TEXT_POPUP:
        onQuickTextKeyboardRequested(key);
        break;
    case KeyCodes.MODE_SYMOBLS:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Symbols);
        break;
    case KeyCodes.MODE_ALPHABET:
        if (getKeyboardSwitcher().shouldPopupForLanguageSwitch()) {
            showLanguageSelectionDialog();
        } else {
            nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Alphabet);
        }
        break;
    case KeyCodes.UTILITY_KEYBOARD:
        getInputView().openUtilityKeyboard();
        break;
    case KeyCodes.MODE_ALPHABET_POPUP:
        showLanguageSelectionDialog();
        break;
    case KeyCodes.ALT:
        nextAlterKeyboard(getCurrentInputEditorInfo());
        break;
    case KeyCodes.KEYBOARD_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Any);
        break;
    case KeyCodes.KEYBOARD_REVERSE_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.PreviousAny);
        break;
    case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.AnyInsideMode);
        break;
    case KeyCodes.KEYBOARD_MODE_CHANGE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.OtherMode);
        break;
    case KeyCodes.CLIPBOARD_COPY:
    case KeyCodes.CLIPBOARD_PASTE:
    case KeyCodes.CLIPBOARD_CUT:
    case KeyCodes.CLIPBOARD_SELECT_ALL:
    case KeyCodes.CLIPBOARD_PASTE_POPUP:
    case KeyCodes.CLIPBOARD_SELECT:
        handleClipboardOperation(key, primaryCode, ic);
        //not allowing undo on-text in clipboard paste operations.
        if (primaryCode == KeyCodes.CLIPBOARD_PASTE)
            mCommittedWord = "";
        break;
    default:
        if (BuildConfig.DEBUG) {
            //this should not happen! We should handle ALL function keys.
            throw new RuntimeException("UNHANDLED FUNCTION KEY! primary code " + primaryCode);
        } else {
            Logger.w(TAG, "UNHANDLED FUNCTION KEY! primary code %d. Ignoring.", primaryCode);
        }
    }
}

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

private void handleBackspace() {
    boolean deleteChar = false;
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from   w w w . jav  a2 s . com*/

    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();
}