Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

In this page you can find the example usage for java.lang CharSequence charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

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

private void maybeRemovePreviousPeriod(CharSequence text) {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null || text.length() == 0)
        return;/*from  w  w  w. j  a  v  a  2 s  .  c  o m*/

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

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

private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/*from   w w w  . ja  v  a2  s  .com*/
    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 www.j a v  a2s .  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 . j ava2s  . c  o  m*/
    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.distantshoresmedia.keyboard.LatinIME.java

public void revertLastWord(boolean deleteChar) {
    final int length = mComposing.length();
    if (!mPredicting && length > 0) {
        final InputConnection ic = getCurrentInputConnection();
        mPredicting = true;/*from   ww  w. ja va2s .c  o  m*/
        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;
    }
}

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  ww .  j a v  a 2s .  c  o  m
    if (!TextUtils.isEmpty(toRight) && !isWordSeparator(toRight.charAt(0))
            && !isSuggestedPunctuation(toRight.charAt(0))) {
        return true;
    }
    return false;
}

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 va 2 s . c om
    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:org.openintents.shopping.ui.ShoppingActivity.java

private boolean QuickEditFieldPopupMenu(final Cursor c, final int pos, final FieldType field, View v) {
    QuickSelectMenu popup = new QuickSelectMenu(this, v);

    Menu menu = popup.getMenu();/*  w w  w . jav a  2s  .  co  m*/
    if (menu == null) {
        return false;
    }
    menu.add("1");
    menu.add("2");
    menu.add("3");
    menu.add("4");
    menu.add("5");
    if (field == FieldType.QUANTITY) {
        menu.add(R.string.otherqty);
    }
    if (field == FieldType.PRIORITY) {
        menu.add(R.string.otherpri);
    }

    popup.setOnItemSelectedListener(new QuickSelectMenu.OnItemSelectedListener() {
        public void onItemSelected(CharSequence name, int id) {
            // TODO: use a flavor of menu.add which takes id,
            // then identifying the selection becomes easier here.

            if (name.length() > 1) {
                // Other ... use edit dialog
                editItem(pos, field);
            } else {
                long number = name.charAt(0) - '0';
                ContentValues values = new ContentValues();
                switch (field) {
                case PRIORITY:
                    values.put(Contains.PRIORITY, number);
                    break;
                case QUANTITY:
                    values.put(Contains.QUANTITY, number);
                    break;
                }
                mItemsView.mCursorItems.moveToPosition(pos);
                String containsId = mItemsView.mCursorItems.getString(mStringItemsCONTAINSID);
                Uri uri = Uri.withAppendedPath(ShoppingContract.Contains.CONTENT_URI, containsId);
                getApplicationContext().getContentResolver().update(uri, values, null, null);
                onItemChanged(); // probably overkill
                mItemsView.updateTotal();
            }
        }
    });

    popup.show();
    return 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();// w  w  w .  j a va  2s  .  c  o  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)))) {
        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:com.yek.keyboard.anysoftkeyboard.AnySoftKeyboard.java

private void handleBackWord(InputConnection ic) {
    if (ic == null) {
        return;/*from  w  w  w. j  a  va 2s . c  om*/
    }

    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 !
}