Example usage for android.view.inputmethod InputConnection deleteSurroundingText

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

Introduction

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

Prototype

boolean deleteSurroundingText(int beforeLength, int afterLength);

Source Link

Document

Delete beforeLength characters of text before the current cursor position, and delete afterLength characters of text after the current cursor position, excluding the selection.

Usage

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

private void handleBackWord(InputConnection ic) {
    if (ic == null) {
        return;//from   w w w  .  j  a  v  a  2s  . com
    }

    if (TextEntryState.isPredicting()) {
        mWord.reset();
        mSuggest.resetNextWordSentence();
        TextEntryState.newSession(mPredictionOn);
        ic.setComposingText("", 1);
        postUpdateSuggestions();
        return;
    }
    // I will not delete more than 128 characters. Just a safe-guard.
    // this will also allow me do just one call to getTextBeforeCursor!
    // Which is always good. This is a part of issue 951.
    CharSequence cs = ic.getTextBeforeCursor(128, 0);
    if (TextUtils.isEmpty(cs)) {
        return;// nothing to delete
    }
    // TWO OPTIONS
    // 1) Either we do like Linux and Windows (and probably ALL desktop
    // OSes):
    // Delete all the characters till a complete word was deleted:
    /*
     * What to do: We delete until we find a separator (the function
     * isBackWordStopChar). Note that we MUST delete a delete a whole word!
     * So if the back-word starts at separators, we'll delete those, and then
     * the word before: "test this,       ," -> "test "
     */
    // Pro: same as desktop
    // Con: when auto-caps is on (the default), this will delete the
    // previous word, which can be annoying..
    // E.g., Writing a sentence, then a period, then ASK will auto-caps,
    // then when the user press backspace (for some reason),
    // the entire previous word deletes.

    // 2) Or we delete all the characters till we encounter a separator, but
    // delete at least one character.
    /*
     * What to do: We delete until we find a separator (the function
     * isBackWordStopChar). Note that we MUST delete a delete at least one
     * character "test this, " -> "test this," -> "test this" -> "test "
     */
    // Pro: Supports auto-caps, and mostly similar to desktop OSes
    // Con: Not all desktop use-cases are here.

    // For now, I go with option 2, but I'm open for discussion.

    // 2b) "test this, " -> "test this"

    final int inputLength = cs.length();
    int idx = inputLength - 1;// it's OK since we checked whether cs is
    // empty after retrieving it.
    while (idx > 0 && !isBackWordStopChar((int) cs.charAt(idx))) {
        idx--;
    }
    ic.deleteSurroundingText(inputLength - idx, 0);// it is always > 0 !
}

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 w  w  w.  j  av a  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:com.anysoftkeyboard.AnySoftKeyboard.java

private void handleBackWord(InputConnection ic) {
    if (ic == null) {
        return;//from  w  w  w.  j av a2  s.  co m
    }

    if (TextEntryState.isPredicting() && mWord.cursorPosition() > 0 && mWord.length() > 0) {
        //sp#ace -> ace
        //cursor == 2
        //length == 5
        //textLeft = word.substring(2, 3) -> word.substring(cursor, length - cursor)
        final CharSequence textLeft = mWord.getTypedWord().subSequence(mWord.cursorPosition(), mWord.length());
        mWord.reset();
        mSuggest.resetNextWordSentence();
        TextEntryState.newSession(mPredictionOn);
        ic.setComposingText(textLeft, 0);
        postUpdateSuggestions();
        return;
    }
    // I will not delete more than 128 characters. Just a safe-guard.
    // this will also allow me do just one call to getTextBeforeCursor!
    // Which is always good. This is a part of issue 951.
    CharSequence cs = ic.getTextBeforeCursor(128, 0);
    if (TextUtils.isEmpty(cs)) {
        return;// nothing to delete
    }
    // TWO OPTIONS
    // 1) Either we do like Linux and Windows (and probably ALL desktop
    // OSes):
    // Delete all the characters till a complete word was deleted:
    /*
     * What to do: We delete until we find a separator (the function
     * isBackWordStopChar). Note that we MUST delete a delete a whole word!
     * So if the back-word starts at separators, we'll delete those, and then
     * the word before: "test this,       ," -> "test "
     */
    // Pro: same as desktop
    // Con: when auto-caps is on (the default), this will delete the
    // previous word, which can be annoying..
    // E.g., Writing a sentence, then a period, then ASK will auto-caps,
    // then when the user press backspace (for some reason),
    // the entire previous word deletes.

    // 2) Or we delete all the characters till we encounter a separator, but
    // delete at least one character.
    /*
     * What to do: We delete until we find a separator (the function
     * isBackWordStopChar). Note that we MUST delete a delete at least one
     * character "test this, " -> "test this," -> "test this" -> "test "
     */
    // Pro: Supports auto-caps, and mostly similar to desktop OSes
    // Con: Not all desktop use-cases are here.

    // For now, I go with option 2, but I'm open for discussion.

    // 2b) "test this, " -> "test this"

    final int inputLength = cs.length();
    int idx = inputLength - 1;// it's OK since we checked whether cs is empty after retrieving it.
    if (isBackWordDeleteChar((int) cs.charAt(idx))) {
        while (idx > 0 && isBackWordDeleteChar((int) cs.charAt(idx - 1))) {
            idx--;
        }
    }
    ic.deleteSurroundingText(inputLength - idx, 0);// it is always > 0 !
}

From source file:keyboard.ecloga.com.eclogakeyboard.EclogaKeyboard.java

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    final InputConnection ic = getCurrentInputConnection();

    playClick();//from  www  . java 2  s.com
    printChar = true;

    if (primaryCode != 126 && primaryCode != -5 && primaryCode != -1
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_1
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_2
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_3
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_4
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_5) {

        keyPressCounter++;

        if (keyPressCounter > 100 && keyPressCounter <= 1000 && keypresscounter1.equals("false")) {
            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(R.drawable.notify, "Ecloga Keyboard",
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);

            notify.setLatestEventInfo(getApplicationContext(), "Warming up!", "Type more than 100 characters",
                    pending);
            notif.notify(0, notify);

            Preferences.setDefaults("keypresscounter1", "true", getApplicationContext());
        } else if (keyPressCounter > 10000 && keyPressCounter <= 10000 && keypresscounter2.equals("false")) {
            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(R.drawable.notify, "Ecloga Keyboard",
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);

            notify.setLatestEventInfo(getApplicationContext(), "Keep it up!", "Type more than 1000 characters",
                    pending);
            notif.notify(0, notify);

            Preferences.setDefaults("keypresscounter2", "true", getApplicationContext());
        } else if (keyPressCounter > 10000 && keypresscounter3.equals("false")) {
            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(R.drawable.notify, "Ecloga Keyboard",
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);

            notify.setLatestEventInfo(getApplicationContext(), "Typing master!",
                    "Type more than 10000 characters", pending);
            notif.notify(0, notify);

            Preferences.setDefaults("keypresscounter3", "true", getApplicationContext());
        }

        Preferences.setDefaults("keypresses", String.valueOf(keyPressCounter), getApplicationContext());
    }

    switch (primaryCode) {
    case 58:
        ic.commitText(":", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }
        break;
    case 59:
        ic.commitText(";", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }
        break;
    case 33:
        ic.commitText("!", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
        break;
    case 63:
        ic.commitText("?", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
        break;
    case -5:
        if (lang.equals("seclang")) {
            if (orient.equals("portrait")) {
                keyboard = prilang;
            } else if (orient.equals("landscape")) {
                keyboard = prilang_landscape;
            }
            kv.setKeyboard(keyboard);
            kv.invalidateAllKeys();
            lang = "prilang";
        } else if (lang.equals("prilang")) {
            if (orient.equals("portrait")) {
                keyboard = seclang;
            } else if (orient.equals("landscape")) {
                keyboard = seclang_landscape;
            }
            kv.setKeyboard(keyboard);
            kv.invalidateAllKeys();
            lang = "seclang";
        }
        break;
    case 32:
        ic.commitText(" ", 1);

        if (autoCapitalize) {
            if (String.valueOf(ic.getTextBeforeCursor(2, 0)).equals(". ")) {
                caps = true;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                printChar = false;
            }
        }
        break;
    case 126:
        ic.deleteSurroundingText(1, 0);

        if (autoCapitalize) {
            if (String.valueOf(ic.getTextBeforeCursor(1, 0)).equals(".")
                    || String.valueOf(ic.getTextBeforeCursor(2, 0)).equals(". ")
                    || String.valueOf(ic.getTextBeforeCursor(2, 0)).equals("")) {
                caps = true;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                printChar = false;
            }
        }
        break;
    case 44:
        ic.commitText(",", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }
        break;
    case 46:
        ic.commitText(".", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
        break;
    case -1:
        ic.performEditorAction(EditorInfo.IME_ACTION_GO);
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_1:
        keyboard = new Keyboard(this, R.xml.emoji_a1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 1;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_2:
        keyboard = new Keyboard(this, R.xml.emoji_b1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 2;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_3:
        keyboard = new Keyboard(this, R.xml.emoji_c1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 3;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_4:
        keyboard = new Keyboard(this, R.xml.emoji_d1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 4;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_5:
        keyboard = new Keyboard(this, R.xml.emoji_e1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 5;
        eScreen = 1;
        break;
    default:
        char code = (char) primaryCode;

        if (allCaps) {
            if (Character.isLetter(code) && capsLock) {
                code = Character.toLowerCase(code);
            } else if (Character.isLetter(code) && !capsLock) {
                code = Character.toUpperCase(code);
            }
        } else {
            if (Character.isLetter(code) && caps) {
                code = Character.toUpperCase(code);
            }
        }

        String character = String.valueOf(code);
        ic.commitText(character, 1);
    }

    if (printedDifferent) {
        ic.deleteSurroundingText(1, 0);
    }

    if (printedDot) {
        ic.deleteSurroundingText(1, 0);

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
    }

    if (doubleUp == 2) {
        capsChange();
    } else {
        changeCaps();
    }
}