android.text.style.TtsSpan Java Examples

The following examples show how to use android.text.style.TtsSpan. 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: TextEventInterpreter.java    From talkback with Apache License 2.0 6 votes vote down vote up
@Nullable
public static CharSequence getSubsequenceWithSpans(
    @Nullable CharSequence text, int from, int to) {
  if (text == null) {
    return null;
  }
  if (from < 0 || text.length() < to || to < from) {
    return null;
  }

  SpannableString textWithSpans = SpannableString.valueOf(text);
  CharSequence subsequence = text.subSequence(from, to);
  SpannableString subsequenceWithSpans = SpannableString.valueOf(subsequence);
  TtsSpan[] spans = subsequenceWithSpans.getSpans(0, subsequence.length(), TtsSpan.class);

  for (TtsSpan span : spans) {
    if (textWithSpans.getSpanStart(span) < from || to < textWithSpans.getSpanEnd(span)) {
      subsequenceWithSpans.removeSpan(span);
    }
  }
  return subsequence;
}
 
Example #2
Source File: TalkBackDumpAccessibilityEventActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
EventDumperSwitchPreference(Context context, int eventType) {
  super(context);
  this.eventType = eventType;
  setOnPreferenceChangeListener(this);

  String title = AccessibilityEvent.eventTypeToString(eventType);
  // Add TtsSpan to the titles to improve readability.
  SpannableString spannableString = new SpannableString(title);
  TtsSpan ttsSpan = new TtsSpan.TextBuilder(title.replaceAll("_", " ")).build();
  spannableString.setSpan(ttsSpan, 0, title.length(), 0 /* no flag */);
  setTitle(spannableString);

  // Set initial value.
  SharedPreferences prefs = SharedPreferencesUtils.getSharedPreferences(getContext());
  int value = prefs.getInt(getContext().getString(R.string.pref_dump_event_mask_key), 0);
  setChecked((value & eventType) != 0);
}
 
Example #3
Source File: Utilities.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Wraps a message with a TTS span, so that a different message is spoken than
 * what is getting displayed.
 * @param msg original message
 * @param ttsMsg message to be spoken
 */
public static CharSequence wrapForTts(CharSequence msg, String ttsMsg) {
    SpannableString spanned = new SpannableString(msg);
    spanned.setSpan(new TtsSpan.TextBuilder(ttsMsg).build(),
            0, spanned.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return spanned;
}
 
Example #4
Source File: TimePickerClockDelegate.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
static final CharSequence obtainVerbatim(String text) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return new SpannableStringBuilder().append(text,
                new TtsSpan.VerbatimBuilder(text).build(), 0);
    } else {
        return text;
    }
}
 
Example #5
Source File: SublimeTimePicker.java    From SublimePicker with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private CharSequence obtainVerbatim(String text) {
    return (SUtils.isApi_21_OrHigher()) ?
            new SpannableStringBuilder().append(text,
                    new TtsSpan.VerbatimBuilder(text).build(), 0)
            : text;
}
 
Example #6
Source File: AppCompatTimePickerDelegate.java    From AppCompat-Extension-Library with Apache License 2.0 5 votes vote down vote up
private static final CharSequence obtainVerbatim(String text) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return new SpannableStringBuilder().append(text,
                new TtsSpan.VerbatimBuilder(text).build(), 0);
    } else {
        return text;
    }
}
 
Example #7
Source File: CategoryTextWatcher.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Ensures that a {@link CategorySpan} is in {@param textToSpannify} if required.
 * Will firstly remove all existing category spans, and then add back one if necessary.
 * In addition, also adds a {@link TtsSpan} to indicate to screen readers that the category
 * span has semantic meaning representing a category.
 */
@TargetApi(21)
private void prepareSpans(Editable textToSpannify) {
    if (textToSpannify == null) {
        return;
    }

    removeSpans(textToSpannify, CategorySpan.class);
    if (Build.VERSION.SDK_INT >= 21) {
        removeSpans(textToSpannify, TtsSpan.class);
    }

    int colonIndex = textToSpannify.toString().indexOf(':');
    if (colonIndex > 0) {
        CategorySpan span = new CategorySpan(context);
        textToSpannify.setSpan(span, 0, colonIndex + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (Build.VERSION.SDK_INT >= 21) {
            // For accessibility reasons, make this more clear to screen readers that the
            // span we just added semantically represents a category.
            CharSequence categoryName = textToSpannify.subSequence(0, colonIndex);
            TtsSpan ttsSpan = new TtsSpan.TextBuilder(context.getString(R.string.tts_category_name,
                    categoryName)).build();
            textToSpannify.setSpan(ttsSpan, 0, 0, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example #8
Source File: TimePickerClockDelegate.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
static final CharSequence obtainVerbatim(String text) {
    return new SpannableStringBuilder().append(text,
            new TtsSpan.VerbatimBuilder(text).build(), 0);
}
 
Example #9
Source File: SpannableUtils.java    From talkback with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}