android.text.style.SuggestionSpan Java Examples

The following examples show how to use android.text.style.SuggestionSpan. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: openboard   Author: dslul   File: SpannableStringUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
 *
 * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
 * kind of span that is copied.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
        Spannable dest, int destoff) {
    Object[] spans = source.getSpans(start, end, SuggestionSpan.class);

    for (int i = 0; i < spans.length; i++) {
        int fl = source.getSpanFlags(spans[i]);
        // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
        // is set, Spannable#setSpan will throw an exception unless the span is on the edge
        // of a word. But the spans have been split into two by the getText{Before,After}Cursor
        // methods, so after concatenation they may end in the middle of a word.
        // Since we don't use them, we can just remove them and avoid crashing.
        fl &= ~Spanned.SPAN_PARAGRAPH;

        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example #2
Source Project: AOSP-Kayboard-7.1.2   Author: sergchil   File: SpannableStringUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
 *
 * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
 * kind of span that is copied.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
        Spannable dest, int destoff) {
    Object[] spans = source.getSpans(start, end, SuggestionSpan.class);

    for (int i = 0; i < spans.length; i++) {
        int fl = source.getSpanFlags(spans[i]);
        // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
        // is set, Spannable#setSpan will throw an exception unless the span is on the edge
        // of a word. But the spans have been split into two by the getText{Before,After}Cursor
        // methods, so after concatenation they may end in the middle of a word.
        // Since we don't use them, we can just remove them and avoid crashing.
        fl &= ~Spanned.SPAN_PARAGRAPH;

        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example #3
Source Project: Spyglass   Author: linkedin   File: MentionsEditableTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@TargetApi(14)
public void testSetSpan() {
    // Should be able to add a MentionSpan
    final int spanStart = 3;
    final int spanEnd = 4 + NAME.length();
    mEditable.setSpan(mMentionSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    assertEquals(1, mEditable.getSpans(0, mEditable.length(), MentionSpan.class).length);

    // Can only add a SuggestionSpan (i.e. a subclass of CharacterStyle) if it does not overlap
    // at all with a MentionSpan (note: SuggestionSpan requires API 14)
    SuggestionSpan suggestionSpan = new SuggestionSpan(ApplicationProvider.getApplicationContext(), new String[0], 0);
    mEditable.setSpan(suggestionSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    assertEquals(0, mEditable.getSpans(0, mEditable.length(), SuggestionSpan.class).length);
    mEditable.setSpan(suggestionSpan, 0, spanEnd - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    assertEquals(0, mEditable.getSpans(0, mEditable.length(), SuggestionSpan.class).length);
    mEditable.setSpan(suggestionSpan, spanStart, spanEnd + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    assertEquals(0, mEditable.getSpans(0, mEditable.length(), SuggestionSpan.class).length);
    mEditable.setSpan(suggestionSpan, spanEnd, spanEnd + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    assertEquals(1, mEditable.getSpans(0, mEditable.length(), SuggestionSpan.class).length);
}
 
Example #4
Source Project: Indic-Keyboard   Author: smc   File: SpannableStringUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
 *
 * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
 * kind of span that is copied.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
        Spannable dest, int destoff) {
    Object[] spans = source.getSpans(start, end, SuggestionSpan.class);

    for (int i = 0; i < spans.length; i++) {
        int fl = source.getSpanFlags(spans[i]);
        // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
        // is set, Spannable#setSpan will throw an exception unless the span is on the edge
        // of a word. But the spans have been split into two by the getText{Before,After}Cursor
        // methods, so after concatenation they may end in the middle of a word.
        // Since we don't use them, we can just remove them and avoid crashing.
        fl &= ~Spanned.SPAN_PARAGRAPH;

        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example #5
Source Project: Indic-Keyboard   Author: smc   File: AndroidSpellCheckerServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testSpellchecker() {
    changeLanguage("en_US");
    mEditText.setText("tgis ");
    mEditText.setSelection(mEditText.getText().length());
    mEditText.onAttachedToWindow();
    sleep(1000);
    runMessages();
    sleep(1000);

    final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    // If no span, the following will crash
    final String[] suggestions = span.getSuggestions();
    // For this test we consider "tgis" should yield at least 2 suggestions (at this moment
    // it yields 5).
    assertTrue(suggestions.length >= 2);
    // We also assume the top suggestion should be "this".
    assertEquals("Test basic spell checking", "this", suggestions[0]);
}
 
Example #6
Source Project: Indic-Keyboard   Author: smc   File: AndroidSpellCheckerServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testSpellcheckWithPeriods() {
    changeLanguage("en_US");
    mEditText.setText("I'm.sure ");
    mEditText.setSelection(mEditText.getText().length());
    mEditText.onAttachedToWindow();
    sleep(1000);
    runMessages();
    sleep(1000);

    final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    // If no span, the following will crash
    final String[] suggestions = span.getSuggestions();
    // The first suggestion should be "I'm sure".
    assertEquals("Test spell checking of mistyped period for space", "I'm sure",
            suggestions[0]);
}
 
Example #7
Source Project: Indic-Keyboard   Author: smc   File: BlueUnderlineTests.java    License: Apache License 2.0 6 votes vote down vote up
public void testBlueUnderlineDisappears() {
    final String STRING_1_TO_TYPE = "tqis";
    final String STRING_2_TO_TYPE = "g";
    final int EXPECTED_SPAN_START = 0;
    final int EXPECTED_SPAN_END = 5;
    type(STRING_1_TO_TYPE);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    type(STRING_2_TO_TYPE);
    // We haven't have time to look into the dictionary yet, so the line should still be
    // blue to avoid any flicker.
    final SpanGetter spanBefore = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    assertEquals("extend blue underline, span start", EXPECTED_SPAN_START, spanBefore.mStart);
    assertEquals("extend blue underline, span end", EXPECTED_SPAN_END, spanBefore.mEnd);
    assertTrue("extend blue underline, span color", spanBefore.isAutoCorrectionIndicator());
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    // Now we have been able to re-evaluate the word, there shouldn't be an auto-correction span
    final SpanGetter spanAfter = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    assertNull("hide blue underline", spanAfter.mSpan);
}
 
Example #8
Source Project: Indic-Keyboard   Author: smc   File: BlueUnderlineTests.java    License: Apache License 2.0 6 votes vote down vote up
public void testBlueUnderlineDisappearsWhenCursorMoved() {
    final String STRING_TO_TYPE = "tgis";
    final int typedLength = STRING_TO_TYPE.length();
    final int NEW_CURSOR_POSITION = 0;
    type(STRING_TO_TYPE);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    // Simulate the onUpdateSelection() event
    mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
    runMessages();
    // Here the blue underline has been set. testBlueUnderline() is testing for this already,
    // so let's not test it here again.
    // Now simulate the user moving the cursor.
    mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
    mLatinIME.onUpdateSelection(typedLength, typedLength,
            NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    assertFalse("blue underline removed when cursor is moved",
            span.isAutoCorrectionIndicator());
}
 
Example #9
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testFindFirstLocaleFromSuggestionSpans() {
    final String[] suggestions = new String[] {"Quality", "Speed", "Price"};
    final SuggestionSpan nullLocaleSpan = new SuggestionSpan((Locale)null, suggestions, 0);
    final SuggestionSpan emptyLocaleSpan = new SuggestionSpan(new Locale(""), suggestions, 0);
    final SuggestionSpan enUsLocaleSpan = new SuggestionSpan(Locale.US, suggestions, 0);
    final SuggestionSpan jaJpLocaleSpan = new SuggestionSpan(Locale.JAPAN, suggestions, 0);

    assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
            new SuggestionSpan[] {}));

    assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
            new SuggestionSpan[] {emptyLocaleSpan}));

    assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
            new SuggestionSpan[] {enUsLocaleSpan}));

    assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
            new SuggestionSpan[] {nullLocaleSpan, enUsLocaleSpan}));

    assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
            new SuggestionSpan[] {nullLocaleSpan, emptyLocaleSpan, enUsLocaleSpan}));

    assertEquals(Locale.JAPAN, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
            new SuggestionSpan[] {nullLocaleSpan, jaJpLocaleSpan, enUsLocaleSpan}));
}
 
Example #10
Source Project: android_9.0.0_r45   Author: lulululbj   File: InputMethodManager.java    License: Apache License 2.0 5 votes vote down vote up
/** @hide */
public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
    try {
        mService.registerSuggestionSpansForNotification(spans);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #11
Source Project: android_9.0.0_r45   Author: lulululbj   File: InputMethodManager.java    License: Apache License 2.0 5 votes vote down vote up
/** @hide */
public void notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
    try {
        mService.notifySuggestionPicked(span, originalString, index);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #12
Source Project: AOSP-Kayboard-7.1.2   Author: sergchil   File: SuggestionSpanUtils.java    License: Apache License 2.0 5 votes vote down vote up
@UsedForTesting
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
        final Context context, final String text, @Nonnull final Locale locale) {
    if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
        return text;
    }
    final Spannable spannable = new SpannableString(text);
    final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale,
            new String[] {} /* suggestions */, OBJ_FLAG_AUTO_CORRECTION, null);
    spannable.setSpan(suggestionSpan, 0, text.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
    return spannable;
}
 
Example #13
Source Project: AOSP-Kayboard-7.1.2   Author: sergchil   File: SuggestionSpanUtils.java    License: Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #14
Source Project: AOSP-Kayboard-7.1.2   Author: sergchil   File: SuggestionSpanUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns first {@link Locale} found in the given array of {@link SuggestionSpan}.
 * @param suggestionSpans the array of {@link SuggestionSpan} to be examined.
 * @return the first {@link Locale} found in {@code suggestionSpans}. {@code null} when not
 * found.
 */
@UsedForTesting
@Nullable
public static Locale findFirstLocaleFromSuggestionSpans(
        final SuggestionSpan[] suggestionSpans) {
    for (final SuggestionSpan suggestionSpan : suggestionSpans) {
        final String localeString = suggestionSpan.getLocale();
        if (TextUtils.isEmpty(localeString)) {
            continue;
        }
        return LocaleUtils.constructLocaleFromString(localeString);
    }
    return null;
}
 
Example #15
Source Project: spanner   Author: neworld   File: Spans.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see android.text.style.SuggestionSpan
 */
public static Span suggestion(@NonNull final Context context, @NonNull final String[] suggestions, final int flags) {
    return new Span(new SpanBuilder() {
        @Override
        public Object build() {
            return new SuggestionSpan(context, suggestions, flags);
        }
    });
}
 
Example #16
Source Project: spanner   Author: neworld   File: Spans.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see android.text.style.SuggestionSpan
 */
public static Span suggestion(@NonNull final Locale locale, @NonNull final String[] suggestions, final int flags) {
    return new Span(new SpanBuilder() {
        @Override
        public Object build() {
            return new SuggestionSpan(locale, suggestions, flags);
        }
    });
}
 
Example #17
Source Project: spanner   Author: neworld   File: Spans.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see android.text.style.SuggestionSpan
 */
public static Span suggestion(@NonNull final Context context, @NonNull final Locale locale, @NonNull final String[] suggestions, final int flags, @NonNull final Class<?> notificationTargetClass) {
    return new Span(new SpanBuilder() {
        @Override
        public Object build() {
            return new SuggestionSpan(context, locale, suggestions, flags, notificationTargetClass);
        }
    });
}
 
Example #18
Source Project: TvRemoteControl   Author: haotie1990   File: InputMethodManagerService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
    if (!calledFromValidUser()) {
        return;
    }
    synchronized (mMethodMap) {
        final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
        for (int i = 0; i < spans.length; ++i) {
            SuggestionSpan ss = spans[i];
            if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
                mSecureSuggestionSpans.put(ss, currentImi);
            }
        }
    }
}
 
Example #19
Source Project: TvRemoteControl   Author: haotie1990   File: InputMethodManagerService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
    if (!calledFromValidUser()) {
        return false;
    }
    synchronized (mMethodMap) {
        final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
        // TODO: Do not send the intent if the process of the targetImi is already dead.
        if (targetImi != null) {
            final String[] suggestions = span.getSuggestions();
            if (index < 0 || index >= suggestions.length) return false;
            final String className = span.getNotificationTargetClassName();
            final Intent intent = new Intent();
            // Ensures that only a class in the original IME package will receive the
            // notification.
            intent.setClassName(targetImi.getPackageName(), className);
            intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
            intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
            intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
            intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
            final long ident = Binder.clearCallingIdentity();
            try {
                mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
            return true;
        }
    }
    return false;
}
 
Example #20
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtils.java    License: Apache License 2.0 5 votes vote down vote up
@UsedForTesting
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
        final Context context, final String text, @Nonnull final Locale locale) {
    if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
        return text;
    }
    final Spannable spannable = new SpannableString(text);
    final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale,
            new String[] {} /* suggestions */, OBJ_FLAG_AUTO_CORRECTION, null);
    spannable.setSpan(suggestionSpan, 0, text.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
    return spannable;
}
 
Example #21
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtils.java    License: Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #22
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns first {@link Locale} found in the given array of {@link SuggestionSpan}.
 * @param suggestionSpans the array of {@link SuggestionSpan} to be examined.
 * @return the first {@link Locale} found in {@code suggestionSpans}. {@code null} when not
 * found.
 */
@UsedForTesting
@Nullable
public static Locale findFirstLocaleFromSuggestionSpans(
        final SuggestionSpan[] suggestionSpans) {
    for (final SuggestionSpan suggestionSpan : suggestionSpans) {
        final String localeString = suggestionSpan.getLocale();
        if (TextUtils.isEmpty(localeString)) {
            continue;
        }
        return LocaleUtils.constructLocaleFromString(localeString);
    }
    return null;
}
 
Example #23
Source Project: Indic-Keyboard   Author: smc   File: SpannableStringUtilsTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testConcatWithSuggestionSpansOnly() {
    SpannableStringBuilder s = new SpannableStringBuilder("test string\ntest string\n"
            + "test string\ntest string\ntest string\ntest string\ntest string\ntest string\n"
            + "test string\ntest string\n");
    final int N = 10;
    for (int i = 0; i < N; ++i) {
        // Put a PARAGRAPH-flagged span that should not be found in the result.
        s.setSpan(new SuggestionSpan(getContext(),
                new String[] {"" + i}, Spanned.SPAN_PARAGRAPH),
                i * 12, i * 12 + 12, Spanned.SPAN_PARAGRAPH);
        // Put a normal suggestion span that should be found in the result.
        s.setSpan(new SuggestionSpan(getContext(), new String[] {"" + i}, 0), i, i * 2, 0);
        // Put a URL span than should not be found in the result.
        s.setSpan(new URLSpan("http://a"), i, i * 2, 0);
    }

    final CharSequence a = s.subSequence(0, 15);
    final CharSequence b = s.subSequence(15, s.length());
    final Spanned result =
            (Spanned)SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(a, b);

    Object[] spans = result.getSpans(0, result.length(), SuggestionSpan.class);
    for (int i = 0; i < spans.length; i++) {
        final int flags = result.getSpanFlags(spans[i]);
        assertEquals("Should not find a span with PARAGRAPH flag",
                flags & Spanned.SPAN_PARAGRAPH, 0);
        assertTrue("Should be a SuggestionSpan", spans[i] instanceof SuggestionSpan);
    }
}
 
Example #24
Source Project: Indic-Keyboard   Author: smc   File: AndroidSpellCheckerServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testRussianSpellchecker() {
    changeLanguage("ru");
    mEditText.onAttachedToWindow();
    mEditText.setText("годп ");
    mEditText.setSelection(mEditText.getText().length());
    mEditText.onAttachedToWindow();
    sleep(1000);
    runMessages();
    sleep(1000);

    final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    // We don't ship with Russian LM
    assertNull(span.getSpan());
}
 
Example #25
Source Project: Indic-Keyboard   Author: smc   File: BlueUnderlineTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testBlueUnderline() {
    final String STRING_TO_TYPE = "tgis";
    final int EXPECTED_SPAN_START = 0;
    final int EXPECTED_SPAN_END = 4;
    type(STRING_TO_TYPE);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    final SpanGetter span = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    assertEquals("show blue underline, span start", EXPECTED_SPAN_START, span.mStart);
    assertEquals("show blue underline, span end", EXPECTED_SPAN_END, span.mEnd);
    assertEquals("show blue underline, span color", true, span.isAutoCorrectionIndicator());
}
 
Example #26
Source Project: Indic-Keyboard   Author: smc   File: BlueUnderlineTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testBlueUnderlineOnBackspace() {
    final String STRING_TO_TYPE = "tgis";
    final int typedLength = STRING_TO_TYPE.length();
    final int EXPECTED_UNDERLINE_SPAN_START = 0;
    final int EXPECTED_UNDERLINE_SPAN_END = 3;
    type(STRING_TO_TYPE);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    type(Constants.CODE_SPACE);
    // typedLength + 1 because we also typed a space
    mLatinIME.onUpdateSelection(0, 0, typedLength + 1, typedLength + 1, -1, -1);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    type(Constants.CODE_DELETE);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    type(Constants.CODE_DELETE);
    sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
    runMessages();
    final SpanGetter suggestionSpan = new SpanGetter(mEditText.getText(), SuggestionSpan.class);
    assertFalse("show no blue underline after backspace, span should not be the auto-"
            + "correction indicator", suggestionSpan.isAutoCorrectionIndicator());
    final SpanGetter underlineSpan = new SpanGetter(mEditText.getText(), UnderlineSpan.class);
    assertEquals("should be composing, so should have an underline span",
            EXPECTED_UNDERLINE_SPAN_START, underlineSpan.mStart);
    assertEquals("should be composing, so should have an underline span",
            EXPECTED_UNDERLINE_SPAN_END, underlineSpan.mEnd);
}
 
Example #27
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void assertNotSuggestionSpan(final String expectedText,
        final CharSequence actualText) {
    assertTrue(TextUtils.equals(expectedText, actualText));
    if (!(actualText instanceof Spanned)) {
        return;
    }
    final Spanned spanned = (Spanned)actualText;
    final SuggestionSpan[] suggestionSpans = spanned.getSpans(0, spanned.length(),
            SuggestionSpan.class);
    assertEquals(0, suggestionSpans.length);
}
 
Example #28
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void assertSuggestionSpan(final String expectedText,
        final int reuiredSuggestionSpanFlags, final int requiredSpanFlags,
        final String[] expectedSuggestions, @Nullable final Locale expectedLocale,
        final CharSequence actualText) {
    assertTrue(TextUtils.equals(expectedText, actualText));
    assertTrue(actualText instanceof Spanned);
    final Spanned spanned = (Spanned)actualText;
    final SuggestionSpan[] suggestionSpans = spanned.getSpans(0, spanned.length(),
            SuggestionSpan.class);
    assertEquals(1, suggestionSpans.length);
    final SuggestionSpan suggestionSpan = suggestionSpans[0];
    if (reuiredSuggestionSpanFlags != 0) {
        assertTrue((suggestionSpan.getFlags() & reuiredSuggestionSpanFlags) != 0);
    }
    if (requiredSpanFlags != 0) {
        assertTrue((spanned.getSpanFlags(suggestionSpan) & requiredSpanFlags) != 0);
    }
    if (expectedSuggestions != null) {
        final String[] actualSuggestions = suggestionSpan.getSuggestions();
        assertEquals(expectedSuggestions.length, actualSuggestions.length);
        for (int i = 0; i < expectedSuggestions.length; ++i) {
            assertEquals(expectedSuggestions[i], actualSuggestions[i]);
        }
    }
    // CAVEAT: SuggestionSpan#getLocale() returns String rather than Locale object.
    assertEquals(expectedLocale.toString(), suggestionSpan.getLocale());
}
 
Example #29
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
public void testGetTextWithAutoCorrectionIndicatorUnderline() {
    final String ORIGINAL_TEXT = "Hey!";
    final Locale NONNULL_LOCALE = new Locale("en", "GB");
    final CharSequence text = SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
            getContext(), ORIGINAL_TEXT, NONNULL_LOCALE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        assertNotSuggestionSpan(ORIGINAL_TEXT, text);
        return;
    }
    assertSuggestionSpan(ORIGINAL_TEXT,
            SuggestionSpan.FLAG_AUTO_CORRECTION /* reuiredSuggestionSpanFlags */,
            Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */,
            new String[]{}, NONNULL_LOCALE, text);
}
 
Example #30
Source Project: Indic-Keyboard   Author: smc   File: SuggestionSpanUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
public void testGetTextWithAutoCorrectionIndicatorUnderlineRootLocale() {
    final String ORIGINAL_TEXT = "Hey!";
    final CharSequence text = SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
            getContext(), ORIGINAL_TEXT, Locale.ROOT);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        assertNotSuggestionSpan(ORIGINAL_TEXT, text);
        return;
    }
    assertSuggestionSpan(ORIGINAL_TEXT,
            SuggestionSpan.FLAG_AUTO_CORRECTION /* reuiredSuggestionSpanFlags */,
            Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */,
            new String[]{}, Locale.ROOT, text);
}