Search in sources :

Example 1 with LocaleData

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

the class Clock method getSmallTime.

public final CharSequence getSmallTime() {
    Context context = getContext();
    boolean is24 = DateFormat.is24HourFormat(context);
    LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
    final char MAGIC1 = '';
    final char MAGIC2 = '';
    SimpleDateFormat sdf;
    String format = is24 ? d.timeFormat24 : d.timeFormat12;
    if (!format.equals(mClockFormatString)) {
        /*
             * Search for an unquoted "a" in the format string, so we can
             * add dummy characters around it to let us find it again after
             * formatting and change its size.
             */
        if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
            int a = -1;
            boolean quoted = false;
            for (int i = 0; i < format.length(); i++) {
                char c = format.charAt(i);
                if (c == '\'') {
                    quoted = !quoted;
                }
                if (!quoted && c == 'a') {
                    a = i;
                    break;
                }
            }
            if (a >= 0) {
                // Move a back so any whitespace before AM/PM is also in the alternate size.
                final int b = a;
                while (a > 0 && Character.isWhitespace(format.charAt(a - 1))) {
                    a--;
                }
                format = format.substring(0, a) + MAGIC1 + format.substring(a, b) + "a" + MAGIC2 + format.substring(b + 1);
            }
        }
        mClockFormat = sdf = new SimpleDateFormat(format);
        mClockFormatString = format;
    } else {
        sdf = mClockFormat;
    }
    String result = sdf.format(mCalendar.getTime());
    if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
        int magic1 = result.indexOf(MAGIC1);
        int magic2 = result.indexOf(MAGIC2);
        if (magic1 >= 0 && magic2 > magic1) {
            SpannableStringBuilder formatted = new SpannableStringBuilder(result);
            if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
                formatted.delete(magic1, magic2 + 1);
            } else {
                if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
                    CharacterStyle style = new RelativeSizeSpan(0.7f);
                    formatted.setSpan(style, magic1, magic2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }
                formatted.delete(magic2, magic2 + 1);
                formatted.delete(magic1, magic1 + 1);
            }
            return formatted;
        }
    }
    return result;
}
Also used : Context(android.content.Context) LocaleData(libcore.icu.LocaleData) RelativeSizeSpan(android.text.style.RelativeSizeSpan) SimpleDateFormat(java.text.SimpleDateFormat) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 2 with LocaleData

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

the class DateUtils method getDayOfWeekString.

/**
     * Return a string for the day of the week.
     * @param dayOfWeek One of {@link Calendar#SUNDAY Calendar.SUNDAY},
     *               {@link Calendar#MONDAY Calendar.MONDAY}, etc.
     * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_SHORT},
     *               {@link #LENGTH_MEDIUM}, or {@link #LENGTH_SHORTEST}.
     *               Note that in most languages, {@link #LENGTH_SHORT}
     *               will return the same as {@link #LENGTH_MEDIUM}.
     *               Undefined lengths will return {@link #LENGTH_MEDIUM}
     *               but may return something different in the future.
     * @throws IndexOutOfBoundsException if the dayOfWeek is out of bounds.
     * @deprecated Use {@link java.text.SimpleDateFormat} instead.
     */
@Deprecated
public static String getDayOfWeekString(int dayOfWeek, int abbrev) {
    LocaleData d = LocaleData.get(Locale.getDefault());
    String[] names;
    switch(abbrev) {
        case LENGTH_LONG:
            names = d.longWeekdayNames;
            break;
        case LENGTH_MEDIUM:
            names = d.shortWeekdayNames;
            break;
        // TODO
        case LENGTH_SHORT:
            names = d.shortWeekdayNames;
            break;
        // TODO
        case LENGTH_SHORTER:
            names = d.shortWeekdayNames;
            break;
        case LENGTH_SHORTEST:
            names = d.tinyWeekdayNames;
            break;
        default:
            names = d.shortWeekdayNames;
            break;
    }
    return names[dayOfWeek];
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 3 with LocaleData

use of libcore.icu.LocaleData in project XobotOS by xamarin.

the class Date method toString.

/**
     * Returns a string representation of this {@code Date}.
     * The formatting is equivalent to using a {@code SimpleDateFormat} with
     * the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something
     * like "Tue Jun 22 13:07:00 PDT 1999". The current default time zone and
     * locale are used. If you need control over the time zone or locale,
     * use {@code SimpleDateFormat} instead.
     */
@Override
public String toString() {
    // TODO: equivalent to the following one-liner, though that's slower on stingray
    // at 476us versus 69us...
    //   return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(d);
    LocaleData localeData = LocaleData.get(Locale.US);
    Calendar cal = new GregorianCalendar(milliseconds);
    TimeZone tz = cal.getTimeZone();
    StringBuilder result = new StringBuilder();
    result.append(localeData.shortWeekdayNames[cal.get(Calendar.DAY_OF_WEEK)]);
    result.append(' ');
    result.append(localeData.shortMonthNames[cal.get(Calendar.MONTH)]);
    result.append(' ');
    appendTwoDigits(result, cal.get(Calendar.DAY_OF_MONTH));
    result.append(' ');
    appendTwoDigits(result, cal.get(Calendar.HOUR_OF_DAY));
    result.append(':');
    appendTwoDigits(result, cal.get(Calendar.MINUTE));
    result.append(':');
    appendTwoDigits(result, cal.get(Calendar.SECOND));
    result.append(' ');
    result.append(tz.getDisplayName(tz.inDaylightTime(this), TimeZone.SHORT));
    result.append(' ');
    result.append(cal.get(Calendar.YEAR));
    return result.toString();
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 4 with LocaleData

use of libcore.icu.LocaleData in project XobotOS by xamarin.

the class Currency method getSymbol.

/**
     * Returns the localized currency symbol for this currency in {@code locale}.
     * That is, given "USD" and Locale.US, you'd get "$", but given "USD" and a non-US locale,
     * you'd get "US$".
     *
     * <p>If the locale only specifies a language rather than a language and a country (such as
     * {@code Locale.JAPANESE} or {new Locale("en", "")} rather than {@code Locale.JAPAN} or
     * {new Locale("en", "US")}), the ISO 4217 currency code is returned.
     *
     * <p>If there is no locale-specific currency symbol, the ISO 4217 currency code is returned.
     */
public String getSymbol(Locale locale) {
    if (locale.getCountry().length() == 0) {
        return currencyCode;
    }
    // Check the locale first, in case the locale has the same currency.
    LocaleData localeData = LocaleData.get(locale);
    if (localeData.internationalCurrencySymbol.equals(currencyCode)) {
        return localeData.currencySymbol;
    }
    // Try ICU, and fall back to the currency code if ICU has nothing.
    String symbol = ICU.getCurrencySymbol(locale.toString(), currencyCode);
    return symbol != null ? symbol : currencyCode;
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 5 with LocaleData

use of libcore.icu.LocaleData in project XobotOS by xamarin.

the class DateFormat method getDateTimeInstance.

/**
     * Returns a {@code DateFormat} instance for formatting and parsing dates
     * and time values in the specified styles for the specified locale.
     *
     * @param dateStyle
     *            one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
     * @param timeStyle
     *            one of SHORT, MEDIUM, LONG, FULL, or DEFAULT.
     * @param locale
     *            the locale.
     * @return the {@code DateFormat} instance for {@code dateStyle},
     *         {@code timeStyle} and {@code locale}.
     * @throws IllegalArgumentException
     *             if {@code dateStyle} or {@code timeStyle} is not one of
     *             SHORT, MEDIUM, LONG, FULL, or DEFAULT.
     */
public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) {
    checkTimeStyle(timeStyle);
    checkDateStyle(dateStyle);
    LocaleData localeData = LocaleData.get(locale);
    String pattern = localeData.getDateFormat(dateStyle) + " " + localeData.getTimeFormat(timeStyle);
    return new SimpleDateFormat(pattern, locale);
}
Also used : LocaleData(libcore.icu.LocaleData)

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