use of android.text.style.SuggestionSpan in project android_frameworks_base by ResurrectionRemix.
the class Editor method shouldOfferToShowSuggestions.
/**
* @return <code>true</code> if it's reasonable to offer to show suggestions depending on
* the current cursor position or selection range. This method is consistent with the
* method to show suggestions {@link SuggestionsPopupWindow#updateSuggestions}.
*/
private boolean shouldOfferToShowSuggestions() {
CharSequence text = mTextView.getText();
if (!(text instanceof Spannable))
return false;
final Spannable spannable = (Spannable) text;
final int selectionStart = mTextView.getSelectionStart();
final int selectionEnd = mTextView.getSelectionEnd();
final SuggestionSpan[] suggestionSpans = spannable.getSpans(selectionStart, selectionEnd, SuggestionSpan.class);
if (suggestionSpans.length == 0) {
return false;
}
if (selectionStart == selectionEnd) {
// Spans overlap the cursor.
for (int i = 0; i < suggestionSpans.length; i++) {
if (suggestionSpans[i].getSuggestions().length > 0) {
return true;
}
}
return false;
}
int minSpanStart = mTextView.getText().length();
int maxSpanEnd = 0;
int unionOfSpansCoveringSelectionStartStart = mTextView.getText().length();
int unionOfSpansCoveringSelectionStartEnd = 0;
boolean hasValidSuggestions = false;
for (int i = 0; i < suggestionSpans.length; i++) {
final int spanStart = spannable.getSpanStart(suggestionSpans[i]);
final int spanEnd = spannable.getSpanEnd(suggestionSpans[i]);
minSpanStart = Math.min(minSpanStart, spanStart);
maxSpanEnd = Math.max(maxSpanEnd, spanEnd);
if (selectionStart < spanStart || selectionStart > spanEnd) {
// The span doesn't cover the current selection start point.
continue;
}
hasValidSuggestions = hasValidSuggestions || suggestionSpans[i].getSuggestions().length > 0;
unionOfSpansCoveringSelectionStartStart = Math.min(unionOfSpansCoveringSelectionStartStart, spanStart);
unionOfSpansCoveringSelectionStartEnd = Math.max(unionOfSpansCoveringSelectionStartEnd, spanEnd);
}
if (!hasValidSuggestions) {
return false;
}
if (unionOfSpansCoveringSelectionStartStart >= unionOfSpansCoveringSelectionStartEnd) {
// No spans cover the selection start point.
return false;
}
if (minSpanStart < unionOfSpansCoveringSelectionStartStart || maxSpanEnd > unionOfSpansCoveringSelectionStartEnd) {
// to show suggestions as it's confusing.
return false;
}
return true;
}
use of android.text.style.SuggestionSpan in project android_frameworks_base by ResurrectionRemix.
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 ResurrectionRemix.
the class TextView method removeAdjacentSuggestionSpans.
void removeAdjacentSuggestionSpans(final int pos) {
if (!(mText instanceof Editable))
return;
final Editable text = (Editable) mText;
final SuggestionSpan[] spans = text.getSpans(pos, pos, SuggestionSpan.class);
final int length = spans.length;
for (int i = 0; i < length; i++) {
final int spanStart = text.getSpanStart(spans[i]);
final int spanEnd = text.getSpanEnd(spans[i]);
if (spanEnd == pos || spanStart == pos) {
if (SpellChecker.haveWordBoundariesChanged(text, pos, pos, spanStart, spanEnd)) {
text.removeSpan(spans[i]);
}
}
}
}
use of android.text.style.SuggestionSpan in project android_frameworks_base by ResurrectionRemix.
the class TextView method removeSuggestionSpans.
/**
* Removes the suggestion spans.
*/
CharSequence removeSuggestionSpans(CharSequence text) {
if (text instanceof Spanned) {
Spannable spannable;
if (text instanceof Spannable) {
spannable = (Spannable) text;
} else {
spannable = new SpannableString(text);
text = spannable;
}
SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
for (int i = 0; i < spans.length; i++) {
spannable.removeSpan(spans[i]);
}
}
return text;
}
use of android.text.style.SuggestionSpan in project android_frameworks_base by ResurrectionRemix.
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