Example usage for android.view.inputmethod InputConnection getTextBeforeCursor

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

Introduction

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

Prototype

CharSequence getTextBeforeCursor(int n, int flags);

Source Link

Document

Get n characters of text before the current cursor position.

Usage

From source file:Main.java

public static CharSequence getPreviousWord(InputConnection connection, String sentenceSeperators) {
    //TODO: Should fix this. This could be slow!
    CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
    if (prev == null) {
        return null;
    }/*from   ww w  . ja va2  s  .c  o  m*/
    String[] w = spaceRegex.split(prev);
    if (w.length >= 2 && w[w.length - 2].length() > 0) {
        char lastChar = w[w.length - 2].charAt(w[w.length - 2].length() - 1);
        if (sentenceSeperators.contains(String.valueOf(lastChar))) {
            return null;
        }
        return w[w.length - 2];
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Append newText to the text field represented by connection.
 * The new text becomes selected./*from   ww w  . j  a v  a 2s. c om*/
 */
public static void appendText(InputConnection connection, String newText) {
    if (connection == null) {
        return;
    }

    // Commit the composing text
    connection.finishComposingText();

    // Add a space if the field already has text.
    CharSequence charBeforeCursor = connection.getTextBeforeCursor(1, 0);
    if (charBeforeCursor != null && !charBeforeCursor.equals(" ") && (charBeforeCursor.length() > 0)) {
        newText = " " + newText;
    }

    connection.setComposingText(newText, 1);
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void removeTrailingSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//from w  ww. j a v  a2s  .  com

    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == KeyCodes.SPACE) {
        ic.deleteSurroundingText(1, 0);
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private boolean isCursorTouchingWord() {
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return false;

    CharSequence toLeft = ic.getTextBeforeCursor(1, 0);
    // It is not exactly clear to me why, but sometimes, although I request
    // 1 character, I get
    // the entire text. This causes me to incorrectly detect restart
    // suggestions...
    if (!TextUtils.isEmpty(toLeft) && toLeft.length() == 1 && !isWordSeparator(toLeft.charAt(0))) {
        return true;
    }//from   w  w  w. jav  a  2  s  . co m

    CharSequence toRight = ic.getTextAfterCursor(1, 0);
    return (!TextUtils.isEmpty(toRight)) && (toRight.length() == 1) && (!isWordSeparator(toRight.charAt(0)));
}

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

private void handleBackWord(InputConnection ic) {
    if (ic == null) {
        return;//w  w w. j av a 2 s  . c o  m
    }

    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:org.distantshoresmedia.keyboard.LatinIME.java

private boolean sameAsTextBeforeCursor(InputConnection ic, CharSequence text) {
    CharSequence beforeText = ic.getTextBeforeCursor(text.length(), 0);
    return TextUtils.equals(text, beforeText);
}

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

private void removeTrailingSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*  w  w w .  j a  v  a  2  s  .  c o m*/

    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == ASCII_SPACE) {
        ic.deleteSurroundingText(1, 0);
    }
}

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

private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from   www.  j  a v a2s  . c  om*/
    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 boolean isCursorTouchingWord() {
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return false;
    CharSequence toLeft = ic.getTextBeforeCursor(1, 0);
    CharSequence toRight = ic.getTextAfterCursor(1, 0);
    if (!TextUtils.isEmpty(toLeft) && !isWordSeparator(toLeft.charAt(0))
            && !isSuggestedPunctuation(toLeft.charAt(0))) {
        return true;
    }//from w  w  w.  j av a2 s.c om
    if (!TextUtils.isEmpty(toRight) && !isWordSeparator(toRight.charAt(0))
            && !isSuggestedPunctuation(toRight.charAt(0))) {
        return true;
    }
    return false;
}

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

private void reswapPeriodAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from w w w.jav  a  2  s.  c  o 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());
    }
}