Search in sources :

Example 31 with LocaleData

use of libcore.icu.LocaleData in project android_frameworks_base by crdroidandroid.

the class TimePicker method getAmPmStrings.

static String[] getAmPmStrings(Context context) {
    final Locale locale = context.getResources().getConfiguration().locale;
    final LocaleData d = LocaleData.get(locale);
    final String[] result = new String[2];
    result[0] = d.amPm[0].length() > 4 ? d.narrowAm : d.amPm[0];
    result[1] = d.amPm[1].length() > 4 ? d.narrowPm : d.amPm[1];
    return result;
}
Also used : Locale(java.util.Locale) LocaleData(libcore.icu.LocaleData)

Example 32 with LocaleData

use of libcore.icu.LocaleData in project android_frameworks_base by crdroidandroid.

the class TimePickerSpinnerDelegate method getAmPmStrings.

public static String[] getAmPmStrings(Context context) {
    String[] result = new String[2];
    LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
    result[0] = d.amPm[0].length() > 4 ? d.narrowAm : d.amPm[0];
    result[1] = d.amPm[1].length() > 4 ? d.narrowPm : d.amPm[1];
    return result;
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 33 with LocaleData

use of libcore.icu.LocaleData in project android_frameworks_base by AOSPA.

the class TextClock method chooseFormat.

/**
     * Selects either one of {@link #getFormat12Hour()} or {@link #getFormat24Hour()}
     * depending on whether the user has selected 24-hour format.
     *
     * @param handleTicker true if calling this method should schedule/unschedule the
     *                     time ticker, false otherwise
     */
private void chooseFormat(boolean handleTicker) {
    final boolean format24Requested = is24HourModeEnabled();
    LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale);
    if (format24Requested) {
        mFormat = abc(mFormat24, mFormat12, ld.timeFormat_Hm);
        mDescFormat = abc(mDescFormat24, mDescFormat12, mFormat);
    } else {
        mFormat = abc(mFormat12, mFormat24, ld.timeFormat_hm);
        mDescFormat = abc(mDescFormat12, mDescFormat24, mFormat);
    }
    boolean hadSeconds = mHasSeconds;
    mHasSeconds = DateFormat.hasSeconds(mFormat);
    if (handleTicker && mRegistered && hadSeconds != mHasSeconds) {
        if (hadSeconds)
            getHandler().removeCallbacks(mTicker);
        else
            mTicker.run();
    }
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 34 with LocaleData

use of libcore.icu.LocaleData in project android_frameworks_base by DirtyUnicorns.

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 35 with LocaleData

use of libcore.icu.LocaleData in project robolectric by robolectric.

the class ShadowLocaleDataTest method shouldSupportLocaleEn_US_since_m.

@Test
@Config(minSdk = M)
public void shouldSupportLocaleEn_US_since_m() {
    LocaleData localeData = LocaleData.get(Locale.US);
    assertThat(localeData.timeFormat_Hm).isEqualTo("HH:mm");
    assertThat(localeData.timeFormat_hm).isEqualTo("h:mm a");
}
Also used : LocaleData(libcore.icu.LocaleData) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

LocaleData (libcore.icu.LocaleData)78 SpannableStringBuilder (android.text.SpannableStringBuilder)12 Locale (java.util.Locale)11 Test (org.junit.Test)8 Context (android.content.Context)6 Spanned (android.text.Spanned)6 SpannedString (android.text.SpannedString)6 CharacterStyle (android.text.style.CharacterStyle)6 RelativeSizeSpan (android.text.style.RelativeSizeSpan)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Config (org.robolectric.annotation.Config)6 Date (java.util.Date)3 Resources (android.content.res.Resources)1 Implementation (org.robolectric.annotation.Implementation)1