use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.
the class Editor method isCursorInsideEasyCorrectionSpan.
/**
* @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with
* {@link SuggestionSpan#FLAG_EASY_CORRECT} set.
*/
private boolean isCursorInsideEasyCorrectionSpan() {
Spannable spannable = (Spannable) mTextView.getText();
SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(), mTextView.getSelectionEnd(), SuggestionSpan.class);
for (int i = 0; i < suggestionSpans.length; i++) {
if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) {
return true;
}
}
return false;
}
use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.
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);
}
}
}
}
use of android.text.style.SuggestionSpan in project platform_frameworks_base by android.
the class ViewPropertyAlphaActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_properties);
getWindow().getDecorView().postDelayed(new Runnable() {
@Override
public void run() {
startAnim(R.id.button);
startAnim(R.id.textview);
startAnim(R.id.spantext);
startAnim(R.id.edittext);
startAnim(R.id.selectedtext);
startAnim(R.id.textviewbackground);
startAnim(R.id.layout);
startAnim(R.id.imageview);
startAnim(myViewAlphaDefault);
startAnim(myViewAlphaHandled);
EditText selectedText = (EditText) findViewById(R.id.selectedtext);
selectedText.setSelection(3, 8);
}
}, 2000);
Button invalidator = (Button) findViewById(R.id.invalidateButton);
invalidator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findViewById(R.id.textview).invalidate();
findViewById(R.id.spantext).invalidate();
}
});
TextView textView = (TextView) findViewById(R.id.spantext);
if (textView != null) {
SpannableStringBuilder text = new SpannableStringBuilder("Now this is a short text message with spans");
text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new SuggestionSpan(this, new String[] { "longer" }, 3), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new UnderlineSpan(), 17, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(text);
}
LinearLayout container = (LinearLayout) findViewById(R.id.container);
myViewAlphaDefault = new MyView(this, false);
myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
container.addView(myViewAlphaDefault);
myViewAlphaHandled = new MyView(this, true);
myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
container.addView(myViewAlphaHandled);
}
use of android.text.style.SuggestionSpan in project android_frameworks_base by crdroidandroid.
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());
}
}
use of android.text.style.SuggestionSpan in project android_frameworks_base by crdroidandroid.
the class SuggestionsPopupWindowTest method testOnTextContextMenuItem.
@SmallTest
public void testOnTextContextMenuItem() {
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);
final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
textView.post(() -> textView.onTextContextMenuItem(TextView.ID_REPLACE));
getInstrumentation().waitForIdleSync();
assertSuggestionsPopupIsDisplayed();
}
Aggregations