Java Code Examples for android.view.inputmethod.InputConnection#deleteSurroundingText()

The following examples show how to use android.view.inputmethod.InputConnection#deleteSurroundingText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    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;
    }
}
 
Example 2
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
private void reswapPeriodAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    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());
    }
}
 
Example 3
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
private void doubleSpace() {
    // if (!mAutoPunctuate) return;
    if (mCorrectionMode == Suggest.CORRECTION_NONE)
        return;
    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;
    }
}
 
Example 4
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
public void revertLastWord(boolean deleteChar) {
    final int length = mComposing.length();
    if (!mPredicting && length > 0) {
        final InputConnection ic = getCurrentInputConnection();
        mPredicting = true;
        mJustRevertedSeparator = ic.getTextBeforeCursor(1, 0);
        if (deleteChar)
            ic.deleteSurroundingText(1, 0);
        int toDelete = mCommittedLength;
        CharSequence toTheLeft = ic
                .getTextBeforeCursor(mCommittedLength, 0);
        if (toTheLeft != null && toTheLeft.length() > 0
                && isWordSeparator(toTheLeft.charAt(0))) {
            toDelete--;
        }
        ic.deleteSurroundingText(toDelete, 0);
        ic.setComposingText(mComposing, 1);
        TextEntryState.backspace();
        postUpdateSuggestions();
    } else {
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
        mJustRevertedSeparator = null;
    }
}
 
Example 5
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @hide
 */
public void onExtractedDeleteText(int start, int end) {
    InputConnection conn = getCurrentInputConnection();
    if (conn != null) {
        conn.finishComposingText();
        conn.setSelection(start, start);
        conn.deleteSurroundingText(0, end - start);
    }
}
 
Example 6
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
private void maybeRemovePreviousPeriod(CharSequence text) {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null || text.length() == 0)
        return;

    // When the text's first character is '.', remove the previous period
    // if there is one.
    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1
            && lastOne.charAt(0) == ASCII_PERIOD
            && text.charAt(0) == ASCII_PERIOD) {
        ic.deleteSurroundingText(1, 0);
    }
}
 
Example 7
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
private void removeTrailingSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;

    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1
            && lastOne.charAt(0) == ASCII_SPACE) {
        ic.deleteSurroundingText(1, 0);
    }
}
 
Example 8
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the word surrounding the cursor. Parameters are identical to
 * getWordAtCursor.
 */
public static void deleteWordAtCursor(
    InputConnection connection, String separators) {

    Range range = getWordRangeAtCursor(connection, separators, null);
    if (range == null) return;

    connection.finishComposingText();
    // Move cursor to beginning of word, to avoid crash when cursor is outside
    // of valid range after deleting text.
    int newCursor = getCursorPosition(connection) - range.charsBefore;
    connection.setSelection(newCursor, newCursor);
    connection.deleteSurroundingText(0, range.charsBefore + range.charsAfter);
}
 
Example 9
Source File: IMEService.java    From Puff-Android with MIT License 4 votes vote down vote up
@Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
        switch(primaryCode){
            case PuffKeyboardView.KEYCODE_EDIT :
                Intent intent = new Intent(this, DialogAcctList.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                getApplicationContext().startActivity(intent);
                stopSelf();
                break;
            case -11 :
                ic.commitText(account, 1);
                account = "";
                break;
            case -12:
                ic.commitText(password, 1);
                password = "";
                break;
            case -13:
                ic.commitText(additional, 1);
                additional = "";
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            case Keyboard.KEYCODE_DELETE:
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_MODE_CHANGE:
                if(kv.getKeyboard() == symbolKeyboard || kv.getKeyboard() == symsftKeyboard) {
                    currentKeyboard = normalKeyboard;
                } else {
                    currentKeyboard = symbolKeyboard;
                }
                kv.setKeyboard(currentKeyboard);
                break;
            case Keyboard.KEYCODE_SHIFT:
//                if (kv.getKeyboard() == normalKeyboard) {
//                    caps = !caps;
//                    normalKeyboard.setShifted(caps);
//                    kv.invalidateAllKeys();
//                }
                if(currentKeyboard == shiftKeyboard) {
                    currentKeyboard = normalKeyboard;
                } else {
                    currentKeyboard = shiftKeyboard;
                }
                kv.setKeyboard(currentKeyboard);
                break;
            case Keyboard.KEYCODE_ALT:
                if (kv.getKeyboard() == symbolKeyboard) {
                    currentKeyboard = symsftKeyboard;
                } else {
                    currentKeyboard = symbolKeyboard;
                }
                kv.setKeyboard(currentKeyboard);
                break;
            case -886 :
                Toast.makeText(IMEService.this, "close", Toast.LENGTH_SHORT).show();
                break ;
            default:
                char code = (char)primaryCode;
                if (caps) {
                    code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code),1);
                break;
        }

        VibrateUtil.getStaticInstance(this).tick();
    }
 
Example 10
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 4 votes vote down vote up
private void handleBackspace() {
    boolean deleteChar = false;
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;

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