Search in sources :

Example 6 with SuggestedWordInfo

use of com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class InputLogic method setSuggestedWords.

// TODO: on the long term, this method should become private, but it will be difficult.
// Especially, how do we deal with InputMethodService.onDisplayCompletions?
public void setSuggestedWords(final SuggestedWords suggestedWords) {
    if (!suggestedWords.isEmpty()) {
        final SuggestedWordInfo suggestedWordInfo;
        if (suggestedWords.mWillAutoCorrect) {
            suggestedWordInfo = suggestedWords.getInfo(SuggestedWords.INDEX_OF_AUTO_CORRECTION);
        } else {
            // We can't use suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD)
            // because it may differ from mWordComposer.mTypedWord.
            suggestedWordInfo = suggestedWords.mTypedWordInfo;
        }
        mWordComposer.setAutoCorrection(suggestedWordInfo);
    }
    mSuggestedWords = suggestedWords;
    final boolean newAutoCorrectionIndicator = suggestedWords.mWillAutoCorrect;
    // Put a blue underline to a word in TextView which will be auto-corrected.
    if (mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator && mWordComposer.isComposingWord()) {
        mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator;
        final CharSequence textWithUnderline = getTextWithUnderline(mWordComposer.getTypedWord());
        // TODO: when called from an updateSuggestionStrip() call that results from a posted
        // message, this is called outside any batch edit. Potentially, this may result in some
        // janky flickering of the screen, although the display speed makes it unlikely in
        // the practice.
        setComposingTextInternal(textWithUnderline, 1);
    }
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)

Example 7 with SuggestedWordInfo

use of com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo 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 8 with SuggestedWordInfo

use of com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SuggestionStripView method onClick.

@Override
public void onClick(final View view) {
    AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback(Constants.CODE_UNSPECIFIED, this);
    if (view == mImportantNoticeStrip) {
        mListener.showImportantNoticeContents();
        return;
    }
    if (view == mVoiceKey) {
        mListener.onCodeInput(Constants.CODE_SHORTCUT, Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, false);
        return;
    }
    final Object tag = view.getTag();
    // {@link SuggestionStripLayoutHelper#layoutPunctuationSuggestions(SuggestedWords,ViewGroup}
    if (tag instanceof Integer) {
        final int index = (Integer) tag;
        if (index >= mSuggestedWords.size()) {
            return;
        }
        final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index);
        mListener.pickSuggestionManually(wordInfo);
    }
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)

Example 9 with SuggestedWordInfo

use of com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class AndroidWordLevelSpellCheckerSession method getResult.

private static Result getResult(final int capitalizeType, final Locale locale, final int suggestionsLimit, final float recommendedThreshold, final String originalText, final SuggestionResults suggestionResults) {
    if (suggestionResults.isEmpty() || suggestionsLimit <= 0) {
        return new Result(null, /* gatheredSuggestions */
        false);
    }
    final ArrayList<String> suggestions = new ArrayList<>();
    for (final SuggestedWordInfo suggestedWordInfo : suggestionResults) {
        final String suggestion;
        if (StringUtils.CAPITALIZE_ALL == capitalizeType) {
            suggestion = suggestedWordInfo.mWord.toUpperCase(locale);
        } else if (StringUtils.CAPITALIZE_FIRST == capitalizeType) {
            suggestion = StringUtils.capitalizeFirstCodePoint(suggestedWordInfo.mWord, locale);
        } else {
            suggestion = suggestedWordInfo.mWord;
        }
        suggestions.add(suggestion);
    }
    StringUtils.removeDupes(suggestions);
    // This returns a String[], while toArray() returns an Object[] which cannot be cast
    // into a String[].
    final List<String> gatheredSuggestionsList = suggestions.subList(0, Math.min(suggestions.size(), suggestionsLimit));
    final String[] gatheredSuggestions = gatheredSuggestionsList.toArray(new String[gatheredSuggestionsList.size()]);
    final int bestScore = suggestionResults.first().mScore;
    final String bestSuggestion = suggestions.get(0);
    final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore(originalText, bestSuggestion, bestScore);
    final boolean hasRecommendedSuggestions = (normalizedScore > recommendedThreshold);
    return new Result(gatheredSuggestions, hasRecommendedSuggestions);
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) ArrayList(java.util.ArrayList)

Example 10 with SuggestedWordInfo

use of com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SuggestionSpanUtils method getTextWithSuggestionSpan.

@UsedForTesting
public static CharSequence getTextWithSuggestionSpan(final Context context, final String pickedWord, final SuggestedWords suggestedWords, final Locale locale) {
    if (TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty() || suggestedWords.isPrediction() || suggestedWords.isPunctuationSuggestions()) {
        return pickedWord;
    }
    final ArrayList<String> suggestionsList = new ArrayList<>();
    for (int i = 0; i < suggestedWords.size(); ++i) {
        if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
            break;
        }
        final SuggestedWordInfo info = suggestedWords.getInfo(i);
        if (info.isKindOf(SuggestedWordInfo.KIND_PREDICTION)) {
            continue;
        }
        final String word = suggestedWords.getWord(i);
        if (!TextUtils.equals(pickedWord, word)) {
            suggestionsList.add(word.toString());
        }
    }
    final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale, suggestionsList.toArray(new String[suggestionsList.size()]), 0, /* flags */
    null);
    final Spannable spannable = new SpannableString(pickedWord);
    spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannable;
}
Also used : SpannableString(android.text.SpannableString) SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan) Spannable(android.text.Spannable) UsedForTesting(com.android.inputmethod.annotations.UsedForTesting)

Aggregations

SuggestedWordInfo (com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)18 ArrayList (java.util.ArrayList)9 SpannableString (android.text.SpannableString)4 SuggestedWords (com.android.inputmethod.latin.SuggestedWords)4 Locale (java.util.Locale)4 SuggestionResults (com.android.inputmethod.latin.utils.SuggestionResults)3 SuggestionSpan (android.text.style.SuggestionSpan)2 OnGetSuggestedWordsCallback (com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback)2 Spannable (android.text.Spannable)1 CorrectionInfo (android.view.inputmethod.CorrectionInfo)1 UsedForTesting (com.android.inputmethod.annotations.UsedForTesting)1 InputPointers (com.android.inputmethod.latin.common.InputPointers)1 UserHistoryDictionary (com.android.inputmethod.latin.personalization.UserHistoryDictionary)1 AsyncResultHolder (com.android.inputmethod.latin.utils.AsyncResultHolder)1 TextRange (com.android.inputmethod.latin.utils.TextRange)1 Nonnull (javax.annotation.Nonnull)1