Search in sources :

Example 1 with SuggestedWords

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

the class SpacingAndPunctuationsTests method testingStandardPunctuationSuggestions.

private static void testingStandardPunctuationSuggestions(final SpacingAndPunctuations sp, final String[] punctuationLabels, final String[] punctuationWords) {
    final SuggestedWords suggestedWords = sp.mSuggestPuncList;
    assertFalse("typedWordValid", suggestedWords.mTypedWordValid);
    assertFalse("willAutoCorrect", suggestedWords.mWillAutoCorrect);
    assertTrue("isPunctuationSuggestions", suggestedWords.isPunctuationSuggestions());
    assertFalse("isObsoleteSuggestions", suggestedWords.mIsObsoleteSuggestions);
    assertFalse("isPrediction", suggestedWords.isPrediction());
    assertEquals("size", punctuationLabels.length, suggestedWords.size());
    for (int index = 0; index < suggestedWords.size(); index++) {
        assertEquals("punctuation label at " + index, punctuationLabels[index], suggestedWords.getLabel(index));
        assertEquals("punctuation word at " + index, punctuationWords[index], suggestedWords.getWord(index));
    }
}
Also used : SuggestedWords(com.android.inputmethod.latin.SuggestedWords)

Example 2 with SuggestedWords

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

the class InputLogic method retrieveOlderSuggestions.

/**
     * Make a {@link com.android.inputmethod.latin.SuggestedWords} object containing a typed word
     * and obsolete suggestions.
     * See {@link com.android.inputmethod.latin.SuggestedWords#getTypedWordAndPreviousSuggestions(
     *      SuggestedWordInfo, com.android.inputmethod.latin.SuggestedWords)}.
     * @param typedWordInfo The typed word as a SuggestedWordInfo.
     * @param previousSuggestedWords The previously suggested words.
     * @return Obsolete suggestions with the newly typed word.
     */
static SuggestedWords retrieveOlderSuggestions(final SuggestedWordInfo typedWordInfo, final SuggestedWords previousSuggestedWords) {
    final SuggestedWords oldSuggestedWords = previousSuggestedWords.isPunctuationSuggestions() ? SuggestedWords.getEmptyInstance() : previousSuggestedWords;
    final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions = SuggestedWords.getTypedWordAndPreviousSuggestions(typedWordInfo, oldSuggestedWords);
    return new SuggestedWords(typedWordAndPreviousSuggestions, null, /* rawSuggestions */
    typedWordInfo, false, /* typedWordValid */
    false, /* hasAutoCorrectionCandidate */
    true, /* isObsoleteSuggestions */
    oldSuggestedWords.mInputStyle, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) SuggestedWords(com.android.inputmethod.latin.SuggestedWords)

Example 3 with SuggestedWords

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

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

the class SuggestionSpanUtilsTest method testGetTextWithSuggestionSpan.

public void testGetTextWithSuggestionSpan() {
    final SuggestedWordInfo prediction1 = createWordInfo("Quality", SuggestedWordInfo.KIND_PREDICTION);
    final SuggestedWordInfo prediction2 = createWordInfo("Speed", SuggestedWordInfo.KIND_PREDICTION);
    final SuggestedWordInfo prediction3 = createWordInfo("Price", SuggestedWordInfo.KIND_PREDICTION);
    final SuggestedWordInfo typed = createWordInfo("Hey", SuggestedWordInfo.KIND_TYPED);
    final SuggestedWordInfo[] corrections = new SuggestedWordInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE * 2];
    for (int i = 0; i < corrections.length; ++i) {
        corrections[i] = createWordInfo("correction" + i, SuggestedWordInfo.KIND_CORRECTION);
    }
    final Locale NONNULL_LOCALE = new Locale("en", "GB");
    // SuggestionSpan will not be attached when {@link SuggestedWords#INPUT_STYLE_PREDICTION}
    // is specified.
    {
        final SuggestedWords predictedWords = new SuggestedWords(new ArrayList<>(Arrays.asList(prediction1, prediction2, prediction3)), null, /* rawSuggestions */
        null, /* typedWord */
        false, /* typedWordValid */
        false, /* willAutoCorrect */
        false, /* isObsoleteSuggestions */
        SuggestedWords.INPUT_STYLE_PREDICTION, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
        final String PICKED_WORD = prediction2.mWord;
        // Note that the framework uses the context locale as a fallback locale.
        assertNotSuggestionSpan(PICKED_WORD, SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD, predictedWords, NONNULL_LOCALE));
    }
    final ArrayList<SuggestedWordInfo> suggestedWordList = new ArrayList<>();
    suggestedWordList.add(typed);
    suggestedWordList.add(prediction1);
    suggestedWordList.add(prediction2);
    suggestedWordList.add(prediction3);
    suggestedWordList.addAll(Arrays.asList(corrections));
    final SuggestedWords typedAndCollectedWords = new SuggestedWords(suggestedWordList, null, /* rawSuggestions */
    null, /* typedWord */
    false, /* typedWordValid */
    false, /* willAutoCorrect */
    false, /* isObsoleteSuggestions */
    SuggestedWords.INPUT_STYLE_TYPING, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
    for (final SuggestedWordInfo pickedWord : suggestedWordList) {
        final String PICKED_WORD = pickedWord.mWord;
        final ArrayList<String> expectedSuggestions = new ArrayList<>();
        for (SuggestedWordInfo suggestedWordInfo : suggestedWordList) {
            if (expectedSuggestions.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
                break;
            }
            if (suggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_PREDICTION)) {
                // Currently predictions are not filled into SuggestionSpan.
                continue;
            }
            final String suggestedWord = suggestedWordInfo.mWord;
            if (TextUtils.equals(PICKED_WORD, suggestedWord)) {
                // Typed word itself is not added to SuggestionSpan.
                continue;
            }
            expectedSuggestions.add(suggestedWord);
        }
        // non-null locale
        assertSuggestionSpan(PICKED_WORD, 0, /* reuiredSuggestionSpanFlags */
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, /* requiredSpanFlags */
        expectedSuggestions.toArray(new String[expectedSuggestions.size()]), NONNULL_LOCALE, SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD, typedAndCollectedWords, NONNULL_LOCALE));
        // root locale
        assertSuggestionSpan(PICKED_WORD, 0, /* reuiredSuggestionSpanFlags */
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, /* requiredSpanFlags */
        expectedSuggestions.toArray(new String[expectedSuggestions.size()]), Locale.ROOT, SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD, typedAndCollectedWords, Locale.ROOT));
    }
}
Also used : Locale(java.util.Locale) SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) ArrayList(java.util.ArrayList) SuggestedWords(com.android.inputmethod.latin.SuggestedWords)

Example 5 with SuggestedWords

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

the class MoreSuggestionsView method onKeyInput.

@Override
protected void onKeyInput(final Key key, final int x, final int y) {
    if (!(key instanceof MoreSuggestionKey)) {
        Log.e(TAG, "Expected key is MoreSuggestionKey, but found " + key.getClass().getName());
        return;
    }
    final Keyboard keyboard = getKeyboard();
    if (!(keyboard instanceof MoreSuggestions)) {
        Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " + keyboard.getClass().getName());
        return;
    }
    final SuggestedWords suggestedWords = ((MoreSuggestions) keyboard).mSuggestedWords;
    final int index = ((MoreSuggestionKey) key).mSuggestedWordIndex;
    if (index < 0 || index >= suggestedWords.size()) {
        Log.e(TAG, "Selected suggestion has an illegal index: " + index);
        return;
    }
    if (!(mListener instanceof MoreSuggestionsListener)) {
        Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " + mListener.getClass().getName());
        return;
    }
    ((MoreSuggestionsListener) mListener).onSuggestionSelected(suggestedWords.getInfo(index));
}
Also used : Keyboard(com.android.inputmethod.keyboard.Keyboard) MoreSuggestionKey(com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionKey) SuggestedWords(com.android.inputmethod.latin.SuggestedWords)

Aggregations

SuggestedWords (com.android.inputmethod.latin.SuggestedWords)10 SuggestedWordInfo (com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)4 SpannableString (android.text.SpannableString)3 OnGetSuggestedWordsCallback (com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 SuggestionSpan (android.text.style.SuggestionSpan)1 KeyEvent (android.view.KeyEvent)1 Event (com.android.inputmethod.event.Event)1 InputTransaction (com.android.inputmethod.event.InputTransaction)1 Keyboard (com.android.inputmethod.keyboard.Keyboard)1 NgramContext (com.android.inputmethod.latin.NgramContext)1 MoreSuggestionKey (com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionKey)1 AsyncResultHolder (com.android.inputmethod.latin.utils.AsyncResultHolder)1 TextRange (com.android.inputmethod.latin.utils.TextRange)1