Search in sources :

Example 31 with SpannedString

use of android.text.SpannedString in project android_frameworks_base by crdroidandroid.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 32 with SpannedString

use of android.text.SpannedString in project ExoPlayer by google.

the class Tx3gDecoderTest method initializationAllDefaultsDecodeWithStyl.

@Test
public void initializationAllDefaultsDecodeWithStyl() throws Exception {
    byte[] initBytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION_ALL_DEFAULTS);
    Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes));
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
    assertThat(text.toString()).isEqualTo("CC Test");
    assertThat(text).hasBoldItalicSpanBetween(0, 6);
    assertThat(text).hasUnderlineSpanBetween(0, 6);
    assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
    assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) SpannedString(android.text.SpannedString) Test(org.junit.Test)

Example 33 with SpannedString

use of android.text.SpannedString in project ExoPlayer by google.

the class Tx3gDecoderTest method decodeWithStylAllDefaults.

@Test
public void decodeWithStylAllDefaults() throws Exception {
    Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL_ALL_DEFAULTS);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
    assertThat(text.toString()).isEqualTo("CC Test");
    assertThat(text).hasNoSpans();
    assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) SpannedString(android.text.SpannedString) Test(org.junit.Test)

Example 34 with SpannedString

use of android.text.SpannedString in project ExoPlayer by google.

the class Tx3gDecoderTest method decodeWithOtherExtension.

@Test
public void decodeWithOtherExtension() throws Exception {
    Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_OTHER_EXTENSION);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
    assertThat(text.toString()).isEqualTo("CC Test");
    assertThat(text).hasBoldSpanBetween(0, 6);
    assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
    assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) SpannedString(android.text.SpannedString) Test(org.junit.Test)

Example 35 with SpannedString

use of android.text.SpannedString in project ExoPlayer by google.

the class Tx3gDecoderTest method decodeWithStyl.

@Test
public void decodeWithStyl() throws Exception {
    Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
    assertThat(text.toString()).isEqualTo("CC Test");
    assertThat(text).hasBoldItalicSpanBetween(0, 6);
    assertThat(text).hasUnderlineSpanBetween(0, 6);
    assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
    assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) SpannedString(android.text.SpannedString) Test(org.junit.Test)

Aggregations

SpannedString (android.text.SpannedString)41 Test (org.junit.Test)15 Spanned (android.text.Spanned)12 Subtitle (com.google.android.exoplayer2.text.Subtitle)12 SpannableStringBuilder (android.text.SpannableStringBuilder)10 SpannableString (android.text.SpannableString)9 LocaleData (libcore.icu.LocaleData)6 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)4 TextPaint (android.text.TextPaint)2 ForegroundColorSpan (android.text.style.ForegroundColorSpan)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 NotificationChannel (android.app.NotificationChannel)1 ConversationChannel (android.app.people.ConversationChannel)1 ShortcutInfo (android.content.pm.ShortcutInfo)1 NonNull (android.support.annotation.NonNull)1 Spannable (android.text.Spannable)1 StaticLayout (android.text.StaticLayout)1 ImageSpan (android.text.style.ImageSpan)1 Pair (android.util.Pair)1