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

The following examples show how to use android.view.inputmethod.InputConnection#getTextBeforeCursor() . 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 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 2
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
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;
    }
    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;
    }
}
 
Example 3
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
/**
 * Append newText to the text field represented by connection.
 * The new text becomes selected.
 */
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);
}
 
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: LatinIME.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
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;
    }
    if (!TextUtils.isEmpty(toRight) && !isWordSeparator(toRight.charAt(0))
            && !isSuggestedPunctuation(toRight.charAt(0))) {
        return true;
    }
    return false;
}
 
Example 6
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 7
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 8
Source File: ImeContainer.java    From mongol-library with MIT License 6 votes vote down vote up
private boolean isSingleZwjThatSplitsWord(InputConnection ic, String text) {
    if (text.length() != 1
            || text.charAt(0) != MongolCode.Uni.ZWJ)
        return false;
    CharSequence previous = ic.getTextBeforeCursor(1, 0);
    if (TextUtils.isEmpty(previous))
        return false;
    char previousChar = previous.charAt(0);
    if (!MongolCode.isMongolian(previousChar)
            || previousChar == MongolCode.Uni.ZWJ)
        return false;
    CharSequence next = ic.getTextAfterCursor(1, 0);
    if (TextUtils.isEmpty(next))
        return false;
    char nextChar = next.charAt(0);
    return (MongolCode.isMongolian(nextChar)
            && nextChar != MongolCode.Uni.ZWJ);
}
 
Example 9
Source File: ImeContainer.java    From mongol-library with MIT License 6 votes vote down vote up
/**
 * Returns the previous words before the cursor. If the cursor is touching a word then the text
 * from the cursor to the beginning of the word is counted as the first previous word.
 *
 * @param numberOfWords                the number of words to return
 * @param allowSingleSpaceBeforeCursor if true then a single space is ignored before the cursor
 *                                     and the word before that is counted as the first word
 * @return a array of words of length numberOfWords where index 0 is closest to the cursor
 */
public List<String> getPreviousMongolWords(int numberOfWords, boolean allowSingleSpaceBeforeCursor) {
    InputConnection ic = getInputConnection();
    List<String> words = new ArrayList<>();
    if (ic == null) return words;
    CharSequence previous = ic.getTextBeforeCursor(MAX_CHARS_BEFORE_CURSOR, 0);
    if (TextUtils.isEmpty(previous)) return words;

    int endIndex = previous.length();
    if (allowSingleSpaceBeforeCursor && isQualifiedSpaceAt(previous, endIndex - 1)) {
        endIndex--;
    }

    for (int i = 0; i < numberOfWords; i++) {
        int startIndex = getStartIndex(endIndex, previous);
        String word = previous.subSequence(startIndex, endIndex).toString();
        words.add(word);
        endIndex = startIndex;
        if (isQualifiedSpaceAt(previous, endIndex - 1)) {
            endIndex--;
        }
    }
    return words;
}
 
Example 10
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
private char getPreviousChar() {
    InputConnection ic = getInputConnection();
    if (ic == null) return 0;
    CharSequence previous = ic.getTextBeforeCursor(1, 0);
    if (TextUtils.isEmpty(previous)) return 0;
    return previous.charAt(0);
}
 
Example 11
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 12
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 13
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
private boolean shouldDoMvsShortcut(InputConnection ic, String text) {
    if (!text.equals(String.valueOf(MongolCode.Uni.A))
            && !text.equals(String.valueOf(MongolCode.Uni.E)))
        return false;
    CharSequence previousTwo = ic.getTextBeforeCursor(2, 0);
    return previousTwo != null
            && previousTwo.length() >= 2
            && MongolCode.isMvsPrecedingChar(previousTwo.charAt(0))
            && previousTwo.charAt(1) == text.charAt(0);
}
 
Example 14
Source File: ImeContainer.java    From mongol-library with MIT License 5 votes vote down vote up
private boolean needsHookedYForVowelYI(InputConnection ic, String text) {
    // XXX: Note that this makes it practically impossible for users to enter
    //      spellings like BAYINA for the double long tooth rendering
    // XXX: Developers can override this by subclassing ImeContainer
    //      and overriding commitText()
    if (text.length() != 1
            || text.charAt(0) != MongolCode.Uni.I)
        return false;
    CharSequence previous = ic.getTextBeforeCursor(2, 0);
    if (TextUtils.isEmpty(previous) || previous.length() != 2)
        return false;
    return (MongolCode.isVowel(previous.charAt(0))
            && previous.charAt(1) == MongolCode.Uni.YA);
}
 
Example 15
Source File: EditingUtil.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
private static Range getWordRangeAtCursor(
        InputConnection connection, String sep, Range range) {
    if (connection == null || sep == null) {
        return null;
    }
    CharSequence before = connection.getTextBeforeCursor(1000, 0);
    CharSequence after = connection.getTextAfterCursor(1000, 0);
    if (before == null || after == null) {
        return null;
    }

    // Find first word separator before the cursor
    int start = before.length();
    while (start > 0 && !isWhitespace(before.charAt(start - 1), sep)) start--;

    // Find last word separator after the cursor
    int end = -1;
    while (++end < after.length() && !isWhitespace(after.charAt(end), sep));

    int cursor = getCursorPosition(connection);
    if (start >= 0 && cursor + end <= after.length() + before.length()) {
        String word = before.toString().substring(start, before.length())
                + after.toString().substring(0, end);

        Range returnRange = range != null? range : new Range();
        returnRange.charsBefore = before.length() - start;
        returnRange.charsAfter = end;
        returnRange.word = word;
        return returnRange;
    }

    return null;
}
 
Example 16
Source File: LatinIME.java    From hackerskeyboard with Apache License 2.0 4 votes vote down vote up
private boolean sameAsTextBeforeCursor(InputConnection ic, CharSequence text) {
    CharSequence beforeText = ic.getTextBeforeCursor(text.length(), 0);
    return TextUtils.equals(text, beforeText);
}
 
Example 17
Source File: ImeContainer.java    From mongol-library with MIT License 3 votes vote down vote up
/**
 * Keyboard.OnKeyboardListener method
 *
 * @param numberOfChars the number of characters located before the cursor
 *                      to request from the editor
 * @return the text before the cursor. The length may be shorter than requested.
 */
@Override
public CharSequence getTextBeforeCursor(int numberOfChars) {
    InputConnection ic = getInputConnection();
    if (ic == null) return "";
    return ic.getTextBeforeCursor(numberOfChars, 0);
}