Example usage for android.view.inputmethod InputConnection commitCompletion

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

Introduction

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

Prototype

boolean commitCompletion(CompletionInfo text);

Source Link

Document

Commit a completion the user has selected from the possible ones previously reported to InputMethodSession#displayCompletions InputMethodSession#displayCompletions(CompletionInfo[]) or InputMethodManager#displayCompletions InputMethodManager#displayCompletions(View, CompletionInfo[]) .

Usage

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

public void pickSuggestionManually(int index, CharSequence suggestion) {
    final String typedWord = mWord.getTypedWord().toString();

    if (mWord.isAtTagsSearchState()) {
        if (index == 0) {
            //this is a special case for tags-searcher
            //since we append a magnifying glass to the suggestions, the "suggestion"
            //value is not a valid output suggestion
            suggestion = typedWord;/*  ww w.  j ava  2 s . com*/
        } else {
            //regular emoji. Storing in history.
            List<QuickKeyHistoryRecords.HistoryKey> keys = QuickKeyHistoryRecords.load(getSharedPrefs());
            QuickKeyHistoryRecords.store(getSharedPrefs(), keys,
                    new QuickKeyHistoryRecords.HistoryKey(suggestion.toString(), suggestion.toString()));
        }
    }

    final InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.beginBatchEdit();
    }

    TextEntryState.acceptedSuggestion(typedWord, suggestion);

    try {
        if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
            CompletionInfo ci = mCompletions[index];
            if (ic != null) {
                ic.commitCompletion(ci);
            }
            mCommittedWord = suggestion;
            if (mCandidateView != null) {
                mCandidateView.clear();
            }
            return;
        }
        commitWordToInput(suggestion,
                false/*user physically picked a word from the suggestions strip. this is not a fix*/);

        TextEntryState.acceptedSuggestion(mWord.getTypedWord(), suggestion);
        // Follow it with a space
        if (mAutoSpace && (index == 0 || !mWord.isAtTagsSearchState())) {
            sendKeyChar((char) KeyCodes.SPACE);
            mJustAddedAutoSpace = true;
            setSpaceTimeStamp(true);
            TextEntryState.typedCharacter(' ', true);
        }
        // Add the word to the auto dictionary if it's not a known word
        mJustAutoAddedWord = false;

        if (!mWord.isAtTagsSearchState()) {
            if (index == 0) {
                mJustAutoAddedWord = checkAddToDictionaryWithAutoDictionary(mWord,
                        AutoDictionary.AdditionType.Picked);
                if (mJustAutoAddedWord)
                    TextEntryState.acceptedSuggestionAddedToDictionary();
            }

            final boolean showingAddToDictionaryHint = (!mJustAutoAddedWord) && index == 0
                    && (mQuickFixes || mShowSuggestions) && (!mSuggest.isValidWord(suggestion))// this is for the case that the word was auto-added upon picking
                    && (!mSuggest.isValidWord(
                            suggestion.toString().toLowerCase(getCurrentAlphabetKeyboard().getLocale())));

            if (showingAddToDictionaryHint) {
                if (mCandidateView != null)
                    mCandidateView.showAddToDictionaryHint(suggestion);
            } else if (!TextUtils.isEmpty(mCommittedWord) && !mJustAutoAddedWord) {
                //showing next-words if:
                //showingAddToDictionaryHint == false, we most likely do not have a next-word suggestion! The committed word is not in the dictionary
                //mJustAutoAddedWord == false, we most likely do not have a next-word suggestion for a newly added word.
                setSuggestions(mSuggest.getNextSuggestions(mCommittedWord, mWord.isAllUpperCase()), false,
                        false, false);
                mWord.setFirstCharCapitalized(false);
            }
        }
    } finally {
        if (ic != null) {
            ic.endBatchEdit();
        }
    }
}

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();//from   w w  w . jav a2  s. com
    }
    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:org.distantshoresmedia.keyboard.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();//from  ww w  .  j av a2 s .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)))) {
        // Word separators are suggested before the user inputs something.
        // So, TKIMELogger logs "" as a user's input.
        TKIMELogger.logOnManualSuggestion("", suggestion.toString(), index, suggestions);
        final char primaryCode = suggestion.charAt(0);
        onKey(primaryCode, new int[] { primaryCode }, BaseKeyboardView.NOT_A_TOUCH_COORDINATE,
                BaseKeyboardView.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);
    }
    TKIMELogger.logOnManualSuggestion(mComposing.toString(), suggestion.toString(), index, suggestions);
    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();
    }
}