use of android.text.style.SuggestionSpan in project android_frameworks_base by AOSPA.
the class SpellChecker method createMisspelledSuggestionSpan.
private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo, SpellCheckSpan spellCheckSpan, int offset, int length) {
final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
if (spellCheckSpanStart < 0 || spellCheckSpanEnd <= spellCheckSpanStart)
// span was removed in the meantime
return;
final int start;
final int end;
if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
start = spellCheckSpanStart + offset;
end = start + length;
} else {
start = spellCheckSpanStart;
end = spellCheckSpanEnd;
}
final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
String[] suggestions;
if (suggestionsCount > 0) {
suggestions = new String[suggestionsCount];
for (int i = 0; i < suggestionsCount; i++) {
suggestions[i] = suggestionsInfo.getSuggestionAt(i);
}
} else {
suggestions = ArrayUtils.emptyArray(String.class);
}
SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions, SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
// to share the logic of word level spell checker and sentence level spell checker
if (mIsSentenceSpellCheckSupported) {
final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
if (tempSuggestionSpan != null) {
if (DBG) {
Log.i(TAG, "Cached span on the same position is cleard. " + editable.subSequence(start, end));
}
editable.removeSpan(tempSuggestionSpan);
}
mSuggestionSpanCache.put(key, suggestionSpan);
}
editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.invalidateRegion(start, end, false);
}
use of android.text.style.SuggestionSpan in project android_frameworks_base by DirtyUnicorns.
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 android_frameworks_base by DirtyUnicorns.
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_packages_inputmethods_LatinIME by CyanogenMod.
the class SuggestionSpanUtils method getTextWithAutoCorrectionIndicatorUnderline.
@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;
}
use of android.text.style.SuggestionSpan in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SuggestionSpanUtilsTest method testFindFirstLocaleFromSuggestionSpans.
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 }));
}
Aggregations