Search in sources :

Example 26 with SuggestionSpan

use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.

the class Editor method downgradeEasyCorrectionSpans.

/**
     * Downgrades to simple suggestions all the easy correction spans that are not a spell check
     * span.
     */
private void downgradeEasyCorrectionSpans() {
    CharSequence text = mTextView.getText();
    if (text instanceof Spannable) {
        Spannable spannable = (Spannable) text;
        SuggestionSpan[] suggestionSpans = spannable.getSpans(0, spannable.length(), SuggestionSpan.class);
        for (int i = 0; i < suggestionSpans.length; i++) {
            int flags = suggestionSpans[i].getFlags();
            if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0 && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
                flags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
                suggestionSpans[i].setFlags(flags);
            }
        }
    }
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) Spannable(android.text.Spannable) Paint(android.graphics.Paint)

Example 27 with SuggestionSpan

use of android.text.style.SuggestionSpan in project WordPress-Android by wordpress-mobile.

the class EditPostActivity method updatePostContent.

// TODO: Replace with contents of the updatePostContentNewEditor() method when legacy editor is dropped
/**
     * Updates post object with content of this fragment
     */
public void updatePostContent(boolean isAutoSave) throws IllegalEditorStateException {
    if (mPost == null) {
        return;
    }
    String title = StringUtils.notNullStr((String) mEditorFragment.getTitle());
    SpannableStringBuilder postContent;
    if (mEditorFragment.getSpannedContent() != null) {
        // needed by the legacy editor to save local drafts
        try {
            postContent = new SpannableStringBuilder(mEditorFragment.getSpannedContent());
        } catch (IndexOutOfBoundsException e) {
            // A core android bug might cause an out of bounds exception, if so we'll just use the current editable
            // See https://code.google.com/p/android/issues/detail?id=5164
            postContent = new SpannableStringBuilder(StringUtils.notNullStr((String) mEditorFragment.getContent()));
        }
    } else {
        postContent = new SpannableStringBuilder(StringUtils.notNullStr((String) mEditorFragment.getContent()));
    }
    String content;
    if (mPost.isLocalDraft()) {
        // remove suggestion spans, they cause craziness in WPHtml.toHTML().
        CharacterStyle[] characterStyles = postContent.getSpans(0, postContent.length(), CharacterStyle.class);
        for (CharacterStyle characterStyle : characterStyles) {
            if (characterStyle instanceof SuggestionSpan) {
                postContent.removeSpan(characterStyle);
            }
        }
        content = WPHtml.toHtml(postContent);
        // replace duplicate <p> tags so there's not duplicates, trac #86
        content = content.replace("<p><p>", "<p>");
        content = content.replace("</p></p>", "</p>");
        content = content.replace("<br><br>", "<br>");
        // sometimes the editor creates extra tags
        content = content.replace("</strong><strong>", "").replace("</em><em>", "").replace("</u><u>", "").replace("</strike><strike>", "").replace("</blockquote><blockquote>", "");
    } else {
        if (!isAutoSave) {
            // Add gallery shortcode
            MediaGalleryImageSpan[] gallerySpans = postContent.getSpans(0, postContent.length(), MediaGalleryImageSpan.class);
            for (MediaGalleryImageSpan gallerySpan : gallerySpans) {
                int start = postContent.getSpanStart(gallerySpan);
                postContent.removeSpan(gallerySpan);
                postContent.insert(start, WPHtml.getGalleryShortcode(gallerySpan));
            }
        }
        WPImageSpan[] imageSpans = postContent.getSpans(0, postContent.length(), WPImageSpan.class);
        if (imageSpans.length != 0) {
            for (WPImageSpan wpIS : imageSpans) {
                MediaFile mediaFile = wpIS.getMediaFile();
                if (mediaFile == null) {
                    continue;
                }
                if (mediaFile.getMediaId() != null) {
                    updateMediaFileOnServer(mediaFile);
                } else {
                    mediaFile.setFileName(wpIS.getImageSource().toString());
                    mediaFile.setFilePath(wpIS.getImageSource().toString());
                }
                int tagStart = postContent.getSpanStart(wpIS);
                if (!isAutoSave) {
                    postContent.removeSpan(wpIS);
                    // network image has a mediaId
                    if (mediaFile.getMediaId() != null && mediaFile.getMediaId().length() > 0) {
                        postContent.insert(tagStart, WPHtml.getContent(wpIS));
                    } else {
                        // local image for upload
                        postContent.insert(tagStart, "<img android-uri=\"" + wpIS.getImageSource().toString() + "\" />");
                    }
                }
            }
        }
        content = postContent.toString();
    }
    mPost.setTitle(title);
    mPost.setContent(content);
    if (!mPost.isLocalDraft()) {
        mPost.setIsLocallyChanged(true);
    }
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) SuggestionSpan(android.text.style.SuggestionSpan) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 28 with SuggestionSpan

use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.

the class SuggestionsPopupWindowTest method testTextAppearanceInSuggestionsPopup.

@SmallTest
public void testTextAppearanceInSuggestionsPopup() {
    final String text = "abc def ghi";
    final String[] singleWordCandidates = { "DEF", "Def" };
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final String[] multiWordCandidates = { "ABC DEF GHI", "Abc Def Ghi" };
    final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(), multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final TypedArray array = getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
    final int id = array.getResourceId(com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
    array.recycle();
    final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
    final TextPaint tmpTp = new TextPaint();
    expectedSpan.updateDrawState(tmpTp);
    final int expectedHighlightTextColor = tmpTp.getColor();
    final float expectedHighlightTextSize = tmpTp.getTextSize();
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    // *XX* means that XX is highlighted.
    for (int i = 0; i < 2; i++) {
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(replaceText(text));
        setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
        setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
        showSuggestionsPopup();
        assertSuggestionsPopupIsDisplayed();
        assertSuggestionsPopupContainsItem("abc DEF ghi");
        assertSuggestionsPopupContainsItem("abc Def ghi");
        assertSuggestionsPopupContainsItem("ABC DEF GHI");
        assertSuggestionsPopupContainsItem("Abc Def Ghi");
        assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
        onSuggestionsPopup().check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException e) {
                final ListView listView = (ListView) view.findViewById(com.android.internal.R.id.suggestionContainer);
                assertNotNull(listView);
                final int childNum = listView.getChildCount();
                assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum);
                for (int j = 0; j < childNum; j++) {
                    final TextView suggestion = (TextView) listView.getChildAt(j);
                    assertNotNull(suggestion);
                    final Spanned spanned = (Spanned) suggestion.getText();
                    assertNotNull(spanned);
                    // Check that the suggestion item order is kept.
                    final String expectedText;
                    if (j < singleWordCandidates.length) {
                        expectedText = "abc " + singleWordCandidates[j] + " ghi";
                    } else {
                        expectedText = multiWordCandidates[j - singleWordCandidates.length];
                    }
                    assertEquals(expectedText, spanned.toString());
                    // Check that the text is highlighted with correct color and text size.
                    final TextAppearanceSpan[] taSpan = spanned.getSpans(text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
                    assertEquals(1, taSpan.length);
                    TextPaint tp = new TextPaint();
                    taSpan[0].updateDrawState(tp);
                    assertEquals(expectedHighlightTextColor, tp.getColor());
                    assertEquals(expectedHighlightTextSize, tp.getTextSize());
                    // Check the correct part of the text is highlighted.
                    final int expectedStart;
                    final int expectedEnd;
                    if (j < singleWordCandidates.length) {
                        expectedStart = text.indexOf('d');
                        expectedEnd = text.indexOf('f') + 1;
                    } else {
                        expectedStart = 0;
                        expectedEnd = text.length();
                    }
                    assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
                    assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
                }
            }
        });
        pressBack();
        onView(withId(R.id.textview)).inRoot(withDecorView(is(getActivity().getWindow().getDecorView()))).perform(clearText());
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) RootMatchers.withDecorView(android.support.test.espresso.matcher.RootMatchers.withDecorView) DragHandleUtils.onHandleView(android.widget.espresso.DragHandleUtils.onHandleView) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) ViewAssertion(android.support.test.espresso.ViewAssertion) TypedArray(android.content.res.TypedArray) SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 29 with SuggestionSpan

use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.

the class SuggestionsPopupWindowTest method testMisspelled.

@SmallTest
public void testMisspelled() {
    final String text = "abc def ghi";
    onView(withId(R.id.textview)).perform(click());
    onView(withId(R.id.textview)).perform(replaceText(text));
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), new String[] { "DEF", "Def" }, SuggestionSpan.FLAG_MISSPELLED);
    setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("DEF");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.addToDictionary));
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
    // Click "Add to dictionary".
    clickSuggestionsPopupItem(getActivity().getString(com.android.internal.R.string.addToDictionary));
// TODO: Check if add to dictionary dialog is displayed.
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 30 with SuggestionSpan

use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.

the class SuggestionsPopupWindowTest method testSuggestionItems.

@SmallTest
public void testSuggestionItems() {
    final String text = "abc def ghi";
    onView(withId(R.id.textview)).perform(click());
    onView(withId(R.id.textview)).perform(replaceText(text));
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), new String[] { "DEF", "Def" }, SuggestionSpan.FLAG_AUTO_CORRECTION);
    setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("DEF");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
    // Select an item.
    clickSuggestionsPopupItem("DEF");
    assertSuggestionsPopupIsNotDisplayed();
    onView(withId(R.id.textview)).check(matches(withText("abc DEF ghi")));
    showSuggestionsPopup();
    assertSuggestionsPopupIsDisplayed();
    assertSuggestionsPopupContainsItem("def");
    assertSuggestionsPopupContainsItem("Def");
    assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
    // Delete
    clickSuggestionsPopupItem(getActivity().getString(com.android.internal.R.string.delete));
    assertSuggestionsPopupIsNotDisplayed();
    onView(withId(R.id.textview)).check(matches(withText("abc ghi")));
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

SuggestionSpan (android.text.style.SuggestionSpan)114 Paint (android.graphics.Paint)43 SmallTest (android.test.suitebuilder.annotation.SmallTest)35 Spannable (android.text.Spannable)29 Spanned (android.text.Spanned)24 Editable (android.text.Editable)21 TextPaint (android.text.TextPaint)21 SpannableString (android.text.SpannableString)14 View (android.view.View)10 SpannableStringBuilder (android.text.SpannableStringBuilder)7 SpellCheckSpan (android.text.style.SpellCheckSpan)7 Nullable (android.annotation.Nullable)5 TypedArray (android.content.res.TypedArray)5 Espresso.onView (android.support.test.espresso.Espresso.onView)5 NoMatchingViewException (android.support.test.espresso.NoMatchingViewException)5 ViewAssertion (android.support.test.espresso.ViewAssertion)5 RootMatchers.withDecorView (android.support.test.espresso.matcher.RootMatchers.withDecorView)5 BackgroundColorSpan (android.text.style.BackgroundColorSpan)5 ForegroundColorSpan (android.text.style.ForegroundColorSpan)5 ImageSpan (android.text.style.ImageSpan)5