Java源码示例:android.text.style.TtsSpan

示例1
@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;
}
 
示例2
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);
}
 
示例3
/**
 * 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;
}
 
示例4
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;
    }
}
 
示例5
@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;
}
 
示例6
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;
    }
}
 
示例7
/**
 * 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);
        }
    }
}
 
示例8
static final CharSequence obtainVerbatim(String text) {
    return new SpannableStringBuilder().append(text,
            new TtsSpan.VerbatimBuilder(text).build(), 0);
}
 
示例9
/**
 * 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();
}