Search in sources :

Example 1 with OnGetSuggestedWordsCallback

use of com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class InputLogic method performUpdateSuggestionStripSync.

public void performUpdateSuggestionStripSync(final SettingsValues settingsValues, final int inputStyle) {
    long startTimeMillis = 0;
    if (DebugFlags.DEBUG_ENABLED) {
        startTimeMillis = System.currentTimeMillis();
        Log.d(TAG, "performUpdateSuggestionStripSync()");
    }
    // Check if we have a suggestion engine attached.
    if (!settingsValues.needsToLookupSuggestions()) {
        if (mWordComposer.isComposingWord()) {
            Log.w(TAG, "Called updateSuggestionsOrPredictions but suggestions were not " + "requested!");
        }
        // Clear the suggestions strip.
        mSuggestionStripViewAccessor.showSuggestionStrip(SuggestedWords.getEmptyInstance());
        return;
    }
    if (!mWordComposer.isComposingWord() && !settingsValues.mBigramPredictionEnabled) {
        mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
        return;
    }
    final AsyncResultHolder<SuggestedWords> holder = new AsyncResultHolder<>("Suggest");
    mInputLogicHandler.getSuggestedWords(inputStyle, SuggestedWords.NOT_A_SEQUENCE_NUMBER, new OnGetSuggestedWordsCallback() {

        @Override
        public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
            final String typedWordString = mWordComposer.getTypedWord();
            final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(typedWordString, "", /* prevWordsContext */
            SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED, SuggestedWordInfo.NOT_AN_INDEX, /* indexOfTouchPointOfSecondWord */
            SuggestedWordInfo.NOT_A_CONFIDENCE);
            // typed word is <= 1 (after a deletion typically) we clear old suggestions.
            if (suggestedWords.size() > 1 || typedWordString.length() <= 1) {
                holder.set(suggestedWords);
            } else {
                holder.set(retrieveOlderSuggestions(typedWordInfo, mSuggestedWords));
            }
        }
    });
    // This line may cause the current thread to wait.
    final SuggestedWords suggestedWords = holder.get(null, Constants.GET_SUGGESTED_WORDS_TIMEOUT);
    if (suggestedWords != null) {
        mSuggestionStripViewAccessor.showSuggestionStrip(suggestedWords);
    }
    if (DebugFlags.DEBUG_ENABLED) {
        long runTimeMillis = System.currentTimeMillis() - startTimeMillis;
        Log.d(TAG, "performUpdateSuggestionStripSync() : " + runTimeMillis + " ms to finish");
    }
}
Also used : OnGetSuggestedWordsCallback(com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback) SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) SuggestedWords(com.android.inputmethod.latin.SuggestedWords) SpannableString(android.text.SpannableString) AsyncResultHolder(com.android.inputmethod.latin.utils.AsyncResultHolder)

Example 2 with OnGetSuggestedWordsCallback

use of com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class InputLogic method restartSuggestionsOnWordTouchedByCursor.

/**
     * Check if the cursor is touching a word. If so, restart suggestions on this word, else
     * do nothing.
     *
     * @param settingsValues the current values of the settings.
     * @param forStartInput whether we're doing this in answer to starting the input (as opposed
     *   to a cursor move, for example). In ICS, there is a platform bug that we need to work
     *   around only when we come here at input start time.
     */
public void restartSuggestionsOnWordTouchedByCursor(final SettingsValues settingsValues, final boolean forStartInput, // TODO: remove this argument, put it into settingsValues
final int currentKeyboardScriptId) {
    // TODO: remove this.
    if (settingsValues.isBrokenByRecorrection() || // how to segment them yet.
    !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces || // If no suggestions are requested, don't try restarting suggestions.
    !settingsValues.needsToLookupSuggestions() || // that the app moves the cursor on its own accord during a batch input.
    mInputLogicHandler.isInBatchInput() || // If the cursor is not touching a word, or if there is a selection, return right away.
    mConnection.hasSelection() || // If we don't know the cursor location, return.
    mConnection.getExpectedSelectionStart() < 0) {
        mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
        return;
    }
    final int expectedCursorPosition = mConnection.getExpectedSelectionStart();
    if (!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations, true)) {
        // Show predictions.
        mWordComposer.setCapitalizedModeAtStartComposingTime(WordComposer.CAPS_MODE_OFF);
        mLatinIME.mHandler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_RECORRECTION);
        return;
    }
    final TextRange range = mConnection.getWordRangeAtCursor(settingsValues.mSpacingAndPunctuations, currentKeyboardScriptId);
    // Happens if we don't have an input connection at all
    if (null == range)
        return;
    if (range.length() <= 0) {
        // Race condition, or touching a word in a non-supported script.
        mLatinIME.setNeutralSuggestionStrip();
        return;
    }
    // If there are links, we don't resume suggestions. Making
    if (range.mHasUrlSpans)
        return;
    // edits to a linkified text through batch commands would ruin the URL spans, and unless
    // we take very complicated steps to preserve the whole link, we can't do things right so
    // we just do not resume because it's safer.
    final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor();
    if (numberOfCharsInWordBeforeCursor > expectedCursorPosition)
        return;
    final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>();
    final String typedWordString = range.mWord.toString();
    final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(typedWordString, "", /* prevWordsContext */
    SuggestedWords.MAX_SUGGESTIONS + 1, SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED, SuggestedWordInfo.NOT_AN_INDEX, /* indexOfTouchPointOfSecondWord */
    SuggestedWordInfo.NOT_A_CONFIDENCE);
    suggestions.add(typedWordInfo);
    if (!isResumableWord(settingsValues, typedWordString)) {
        mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
        return;
    }
    int i = 0;
    for (final SuggestionSpan span : range.getSuggestionSpansAtWord()) {
        for (final String s : span.getSuggestions()) {
            ++i;
            if (!TextUtils.equals(s, typedWordString)) {
                suggestions.add(new SuggestedWordInfo(s, "", /* prevWordsContext */
                SuggestedWords.MAX_SUGGESTIONS - i, SuggestedWordInfo.KIND_RESUMED, Dictionary.DICTIONARY_RESUMED, SuggestedWordInfo.NOT_AN_INDEX, /* indexOfTouchPointOfSecondWord */
                SuggestedWordInfo.NOT_A_CONFIDENCE));
            }
        }
    }
    final int[] codePoints = StringUtils.toCodePointArray(typedWordString);
    mWordComposer.setComposingWord(codePoints, mLatinIME.getCoordinatesForCurrentKeyboard(codePoints));
    mWordComposer.setCursorPositionWithinWord(typedWordString.codePointCount(0, numberOfCharsInWordBeforeCursor));
    if (forStartInput) {
        mConnection.maybeMoveTheCursorAroundAndRestoreToWorkaroundABug();
    }
    mConnection.setComposingRegion(expectedCursorPosition - numberOfCharsInWordBeforeCursor, expectedCursorPosition + range.getNumberOfCharsInWordAfterCursor());
    if (suggestions.size() <= 1) {
        // If there weren't any suggestion spans on this word, suggestions#size() will be 1
        // if shouldIncludeResumedWordInSuggestions is true, 0 otherwise. In this case, we
        // have no useful suggestions, so we will try to compute some for it instead.
        mInputLogicHandler.getSuggestedWords(Suggest.SESSION_ID_TYPING, SuggestedWords.NOT_A_SEQUENCE_NUMBER, new OnGetSuggestedWordsCallback() {

            @Override
            public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
                doShowSuggestionsAndClearAutoCorrectionIndicator(suggestedWords);
            }
        });
    } else {
        // We found suggestion spans in the word. We'll create the SuggestedWords out of
        // them, and make willAutoCorrect false. We make typedWordValid false, because the
        // color of the word in the suggestion strip changes according to this parameter,
        // and false gives the correct color.
        final SuggestedWords suggestedWords = new SuggestedWords(suggestions, null, /* rawSuggestions */
        typedWordInfo, false, /* typedWordValid */
        false, /* willAutoCorrect */
        false, /* isObsoleteSuggestions */
        SuggestedWords.INPUT_STYLE_RECORRECTION, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
        doShowSuggestionsAndClearAutoCorrectionIndicator(suggestedWords);
    }
}
Also used : OnGetSuggestedWordsCallback(com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback) SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) ArrayList(java.util.ArrayList) SuggestedWords(com.android.inputmethod.latin.SuggestedWords) TextRange(com.android.inputmethod.latin.utils.TextRange) SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan)

Example 3 with OnGetSuggestedWordsCallback

use of com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class InputLogicHandler method updateBatchInput.

/**
     * Fetch suggestions corresponding to an update of a batch input.
     * @param batchPointers the updated pointers, including the part that was passed last time.
     * @param sequenceNumber the sequence number associated with this batch input.
     * @param isTailBatchInput true if this is the end of a batch input, false if it's an update.
     */
// This method can be called from any thread and will see to it that the correct threads
// are used for parts that require it. This method will send a message to the Non-UI handler
// thread to pull suggestions, and get the inlined callback to get called on the Non-UI
// handler thread. If this is the end of a batch input, the callback will then proceed to
// send a message to the UI handler in LatinIME so that showing suggestions can be done on
// the UI thread.
private void updateBatchInput(final InputPointers batchPointers, final int sequenceNumber, final boolean isTailBatchInput) {
    synchronized (mLock) {
        if (!mInBatchInput) {
            // Batch input has ended or canceled while the message was being delivered.
            return;
        }
        mInputLogic.mWordComposer.setBatchInputPointers(batchPointers);
        final OnGetSuggestedWordsCallback callback = new OnGetSuggestedWordsCallback() {

            @Override
            public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
                showGestureSuggestionsWithPreviewVisuals(suggestedWords, isTailBatchInput);
            }
        };
        getSuggestedWords(isTailBatchInput ? SuggestedWords.INPUT_STYLE_TAIL_BATCH : SuggestedWords.INPUT_STYLE_UPDATE_BATCH, sequenceNumber, callback);
    }
}
Also used : OnGetSuggestedWordsCallback(com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback) SuggestedWords(com.android.inputmethod.latin.SuggestedWords)

Aggregations

OnGetSuggestedWordsCallback (com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback)3 SuggestedWords (com.android.inputmethod.latin.SuggestedWords)3 SpannableString (android.text.SpannableString)2 SuggestedWordInfo (com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)2 SuggestionSpan (android.text.style.SuggestionSpan)1 AsyncResultHolder (com.android.inputmethod.latin.utils.AsyncResultHolder)1 TextRange (com.android.inputmethod.latin.utils.TextRange)1 ArrayList (java.util.ArrayList)1