android.text.style.LocaleSpan Java Examples
The following examples show how to use
android.text.style.LocaleSpan.
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 File: SpanEZTest.java From SpanEZ with Apache License 2.0 | 5 votes |
@Test public void locale_should_add_only_one_span() { spanBuilder.locale(range, Locale.US) .apply(); verify((SpanEZ) spanBuilder, times(1)) .addSpan(isA(TargetRange.class), isA(LocaleSpan.class)); }
Example #2
Source File: Spans.java From spanner with Apache License 2.0 | 5 votes |
/** * @see android.text.style.LocaleSpan#LocaleSpan(LocaleList) */ @RequiresApi(api = Build.VERSION_CODES.N) public static Span locale(@NonNull final LocaleList localeList) { return new Span(new SpanBuilder() { @Override public Object build() { return new LocaleSpan(localeList); } }); }
Example #3
Source File: LocaleUtils.java From talkback with Apache License 2.0 | 5 votes |
/** * Wraps the {@link text} with {@link preferredLocale}. If a LocaleSpan is already attached to the * {@link text}, {@link SpannableString#setSpan} will add a second LocaleSpan. */ public static @Nullable CharSequence wrapWithLocaleSpan( @Nullable CharSequence text, @Nullable Locale preferredLocale) { if (text != null && preferredLocale != null) { SpannableString textToBeWrapped = new SpannableString(text); textToBeWrapped.setSpan(new LocaleSpan(preferredLocale), 0, textToBeWrapped.length(), 0); return textToBeWrapped; } return text; }
Example #4
Source File: SearchScreenOverlay.java From talkback with Apache License 2.0 | 5 votes |
/** Checks if {@code span} is a formatting span or not. */ private static boolean isFormattingSpan(Object span) { // We only keep LocaleSpan for now, could add more types if needed. if (span instanceof LocaleSpan) { return false; } else { return true; } }
Example #5
Source File: AndroidSpan.java From AndroidSpan with Apache License 2.0 | 5 votes |
/** * @param text * @param locale Locale.CHINESE * @return */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public AndroidSpan drawLocaleSpan(String text, Locale locale) { LocaleSpan span = new LocaleSpan(locale); drawSpan(text, span); return this; }
Example #6
Source File: SpanOptions.java From AndroidSpan with Apache License 2.0 | 5 votes |
/** * @param locale Locale.CHINESE * @return */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public SpanOptions addLocaleSpan(Locale locale) { LocaleSpan span = new LocaleSpan(locale); listSpan.add(span); return this; }
Example #7
Source File: FeedbackProcessingUtils.java From talkback with Apache License 2.0 | 4 votes |
/** * Splits and adds feedback to {@link FeedbackItem}s for spannable text contained within this * {@link FeedbackItem} * * @param item The item to process for formatted text. */ public static void addFormattingCharacteristics(FeedbackItem item) { for (int i = 0; i < item.getFragments().size(); ++i) { final FeedbackFragment fragment = item.getFragments().get(i); final CharSequence fragmentText = fragment.getText(); if (TextUtils.isEmpty(fragmentText) || !(fragmentText instanceof Spannable)) { continue; } Spannable spannable = (Spannable) fragmentText; int len = spannable.length(); int next; boolean isFirstFragment = true; for (int begin = 0; begin < len; begin = next) { next = nextSpanTransition(spannable, begin, len, LocaleSpan.class, TARGET_SPAN_CLASS); // CharacterStyle is a superclass of both ClickableSpan(including URLSpan) and LocaleSpan; // we want to split by only ClickableSpan and LocaleSpan, but it is OK if we request any // CharacterStyle in the list of spans since we ignore the ones that are not // ClickableSpan/LocaleSpan. CharacterStyle[] spans = spannable.getSpans(begin, next, CharacterStyle.class); CharacterStyle chosenSpan = null; for (CharacterStyle span : spans) { if (span instanceof LocaleSpan) { // Prioritize LocaleSpan, quit the loop when a LocaleSpan is detected. Note: If multiple // LocaleSpans are attached to the text, first LocaleSpan is given preference. chosenSpan = span; break; } else if ((span instanceof ClickableSpan) || (span instanceof URLSpan)) { chosenSpan = span; } // Ignore other CharacterStyle. } final FeedbackFragment newFragment; CharSequence subString = spannable.subSequence(begin, next); boolean isIdentifier = SpannableUtils.isWrappedWithTargetSpan( subString, SpannableUtils.IdentifierSpan.class, /* shouldTrim= */ true); if (isIdentifier) { continue; } if (isFirstFragment) { // This is the first new fragment, so we should reuse the old fragment. // That way, we'll keep the existing haptic/earcon feedback at the beginning! isFirstFragment = false; newFragment = fragment; newFragment.setText(subString); } else { // Otherwise, add after the last fragment processed/added. newFragment = new FeedbackFragment(subString, /* speechParams= */ null); ++i; newFragment.setStartIndexInFeedbackItem(begin); item.addFragmentAtPosition(newFragment, i); } if (chosenSpan instanceof LocaleSpan) { // LocaleSpan newFragment.setLocale(((LocaleSpan) chosenSpan).getLocale()); } else if (chosenSpan != null) { // ClickableSpan (including UrlSpan) handleClickableSpan(newFragment); } } } }
Example #8
Source File: SpannableUtils.java From talkback with Apache License 2.0 | 4 votes |
/** * Logs the type, position and args of spans which attach to given text, but only if log priority * is equal to Log.VERBOSE. Format is {type 'spanned text' extra-data} {type 'other text' * extra-data} ..." * * @param text Text to be logged */ public static String spansToStringForLogging(CharSequence text) { if (!LogUtils.shouldLog(Log.VERBOSE)) { return null; } if (isEmptyOrNotSpannableStringType(text)) { return null; } Spanned spanned = (Spanned) text; ParcelableSpan[] spans = spanned.getSpans(0, text.length(), ParcelableSpan.class); if (spans.length == 0) { return null; } StringBuilder stringBuilder = new StringBuilder(); for (ParcelableSpan span : spans) { stringBuilder.append("{"); // Span type. stringBuilder.append(span.getClass().getSimpleName()); // Span text. int start = spanned.getSpanStart(span); int end = spanned.getSpanEnd(span); if (start < 0 || end < 0 || start == end) { stringBuilder.append(" invalid index:["); stringBuilder.append(start); stringBuilder.append(","); stringBuilder.append(end); stringBuilder.append("]}"); continue; } else { stringBuilder.append(" '"); stringBuilder.append(spanned, start, end); stringBuilder.append("'"); } // Extra data. if (span instanceof LocaleSpan) { LocaleSpan localeSpan = (LocaleSpan) span; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { Locale locale = localeSpan.getLocale(); if (locale != null) { stringBuilder.append(" locale="); stringBuilder.append(locale); } } else { LocaleList localeList = localeSpan.getLocales(); int size = localeList.size(); if (size > 0) { stringBuilder.append(" locale=["); for (int i = 0; i < size - 1; i++) { stringBuilder.append(localeList.get(i)); stringBuilder.append(","); } stringBuilder.append(localeList.get(size - 1)); stringBuilder.append("]"); } } } else if (span instanceof TtsSpan) { TtsSpan ttsSpan = (TtsSpan) span; stringBuilder.append(" ttsType="); stringBuilder.append(ttsSpan.getType()); PersistableBundle bundle = ttsSpan.getArgs(); Set<String> keys = bundle.keySet(); if (!keys.isEmpty()) { for (String key : keys) { stringBuilder.append(" "); stringBuilder.append(key); stringBuilder.append("="); stringBuilder.append(bundle.get(key)); } } } else if (span instanceof URLSpan) { URLSpan urlSpan = (URLSpan) span; stringBuilder.append(" url="); stringBuilder.append(urlSpan.getURL()); } stringBuilder.append("}"); } return stringBuilder.toString(); }
Example #9
Source File: ProcessorPhoneticLetters.java From talkback with Apache License 2.0 | 4 votes |
/** Handle an event that indicates a key is held on the soft keyboard. */ private void processKeyboardKeyEvent(AccessibilityEvent event, EventId eventId) { final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event); if (TextUtils.isEmpty(text)) { return; } String localeString = null; // For new version of Gboard, contentDescription of the node is wrapped in the locale of the // IME. if (text instanceof Spannable) { Spannable spannable = (Spannable) text; LocaleSpan[] spans = spannable.getSpans(0, text.length(), LocaleSpan.class); for (LocaleSpan span : spans) { // Quit the loop when a LocaleSpan is detected. We expect just one LocaleSpan. localeString = span.getLocale().toString(); break; } } // Old version of Gboard does not provide content description wrapped in the locale of the IME // so we try using InputMethodManager. if (localeString == null) { InputMethodManager inputMethodManager = (InputMethodManager) service.getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodSubtype inputMethod = inputMethodManager.getCurrentInputMethodSubtype(); if (inputMethod != null) { String localeStringFromIme = inputMethod.getLocale(); if (!localeStringFromIme.isEmpty()) { localeString = localeStringFromIme; } } } // Use system locale as the fallback option. if (localeString == null) { localeString = Locale.getDefault().toString(); } CharSequence phoneticLetter = getPhoneticLetter(localeString, text.toString()); if (phoneticLetter != null) { postPhoneticLetterRunnable(phoneticLetter, eventId); } }