Java 类android.text.style.TtsSpan 实例源码

项目:talkback    文件:TTSSpanTest.java   
@Override
public View getView(final LayoutInflater inflater, ViewGroup container, Context context) {
    View view = inflater.inflate(R.layout.test_tts_span, container, false);
    ListView list = (ListView) view.findViewById(R.id.test_tts_span_list);

    TtsSpan[] ttsSpans = initTtsSpans();
    String[] spanTypes = container.getResources()
            .getStringArray(R.array.test_tts_span_type_array);
    String[] texts = context.getResources().getStringArray(R.array.test_tts_span_text_array);

    TTSSpanTestAdapter adapter = new TTSSpanTestAdapter(context,
            ttsSpans, spanTypes, texts);

    list.setAdapter(adapter);

    return view;
}
项目:talkback    文件:TTSSpanTest.java   
private TtsSpan[] initTtsSpans() {
    TtsSpan[] ttsSpans = new TtsSpan[7];
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ttsSpans[0] = new TtsSpan.CardinalBuilder("1234").build();
        ttsSpans[1] = new TtsSpan.DateBuilder().setYear(1998).setMonth(9).setDay(4).build();
        ttsSpans[2] = new TtsSpan.DecimalBuilder().setArgumentsFromDouble(Math.PI, 0, 5)
                .build();
        ttsSpans[3] = new TtsSpan.DigitsBuilder().setDigits("2048").build();
        ttsSpans[4] = new TtsSpan.MoneyBuilder().setCurrency("USD").setIntegerPart(3057000)
                .build();
        ttsSpans[5] = new TtsSpan.OrdinalBuilder().setNumber(21).build();
        ttsSpans[6] = new TtsSpan.TextBuilder().setText("Spoken Text").build();
    }
    //TODO add more TtsSpans

    return ttsSpans;
}
项目:LaunchEnr    文件:Utilities.java   
/**
 * 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;
}
项目:mobile-store    文件:CategoryTextWatcher.java   
/**
 * Ensures that a {@link CategorySpan} is in {@param textToSpannify} if required.
 * Will firstly remove all existing category spans, and then add back one if neccesary.
 * 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);
        }
    }
}
项目:FlickLauncher    文件:Utilities.java   
/**
 * 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
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static CharSequence wrapForTts(CharSequence msg, String ttsMsg) {
    if (Utilities.ATLEAST_LOLLIPOP) {
        SpannableString spanned = new SpannableString(msg);
        spanned.setSpan(new TtsSpan.TextBuilder(ttsMsg).build(),
                0, spanned.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanned;
    } else {
        return msg;
    }
}
项目:DateTimePicker    文件:TimePickerClockDelegate.java   
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;
    }
}
项目:SimpleUILauncher    文件:Utilities.java   
/**
 * 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
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static CharSequence wrapForTts(CharSequence msg, String ttsMsg) {
    if (Utilities.ATLEAST_LOLLIPOP) {
        SpannableString spanned = new SpannableString(msg);
        spanned.setSpan(new TtsSpan.TextBuilder(ttsMsg).build(),
                0, spanned.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanned;
    } else {
        return msg;
    }
}
项目:CheckSmarter    文件:SublimeTimePicker.java   
@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;
}
项目:SublimePicker    文件:SublimeTimePicker.java   
@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;
}
项目:FLauncher    文件:Utilities.java   
/**
 * 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
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static CharSequence wrapForTts(CharSequence msg, String ttsMsg) {
    if (Utilities.ATLEAST_LOLLIPOP) {
        SpannableString spanned = new SpannableString(msg);
        spanned.setSpan(new TtsSpan.TextBuilder(ttsMsg).build(),
                0, spanned.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanned;
    } else {
        return msg;
    }
}
项目:talkback    文件:TTSSpanTestAdapter.java   
public TTSSpanTestAdapter(Context context, TtsSpan[] ttsSpans,
                          String[] spanTypes, String[] texts) {
    mContext = context;
    mTtsSpans = ttsSpans;
    mSpanTypes = spanTypes;
    mTexts = texts;
}
项目:talkback    文件:TalkBackDumpAccessibilityEventActivity.java   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set preferences to use device-protected storage.
    if (BuildCompat.isAtLeastN()) {
        getPreferenceManager().setStorageDeviceProtected();
    }

    mService = TalkBackService.getInstance();

    // Compose preference keys from a key prefix and accessibility event types.
    for (int id : DUMP_A11Y_EVENT_IDS) {
        mDumpEventPrefKeys.add(getString(R.string.pref_dump_event_key_prefix, id));
    }

    addPreferencesFromResource(R.xml.dump_events_preferences);
    PreferenceScreen screen = getPreferenceScreen();

    for (int i = 0; i < DUMP_A11Y_EVENT_IDS.length; i++) {
        CheckBoxPreference preference = new CheckBoxPreference(
                getActivity().getApplicationContext());
        preference.setKey(mDumpEventPrefKeys.get(i));

        String title = AccessibilityEvent.eventTypeToString(DUMP_A11Y_EVENT_IDS[i]);
        // Add TtsSpan to the titles to improve readability.
        SpannableString spannableString = new SpannableString(title);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            TtsSpan ttsSpan = new TtsSpan.TextBuilder(title.replaceAll("_", " ")).build();
            spannableString.setSpan(ttsSpan, 0, title.length(), 0 /* no flag */);
        }
        preference.setTitle(spannableString);

        screen.addPreference(preference);
        preference.setOnPreferenceChangeListener(this);
    }
}
项目:AppCompat-Extension-Library    文件:AppCompatTimePickerDelegate.java   
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;
    }
}
项目:fdroidclient    文件:CategoryTextWatcher.java   
/**
 * Ensures that a {@link CategorySpan} is in {@param textToSpannify} if required.
 * Will firstly remove all existing category spans, and then add back one if neccesary.
 * 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);
        }
    }
}