Search in sources :

Example 61 with LocaleData

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

the class Clock method getSmallTime.

private final CharSequence getSmallTime() {
    Context context = getContext();
    boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser());
    LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
    final char MAGIC1 = '';
    final char MAGIC2 = '';
    SimpleDateFormat sdf;
    String format = mShowSeconds ? is24 ? d.timeFormat_Hms : d.timeFormat_hms : is24 ? d.timeFormat_Hm : d.timeFormat_hm;
    if (!format.equals(mClockFormatString)) {
        mContentDescriptionFormat = new SimpleDateFormat(format);
        /*
             * 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 (mAmPmStyle != 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;
    }
    CharSequence dateString = null;
    String result = "";
    String timeResult = sdf.format(mCalendar.getTime());
    String dateResult = "";
    int clockDatePosition = Settings.System.getInt(getContext().getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_POSITION, 0);
    if (mClockDateDisplay != CLOCK_DATE_DISPLAY_GONE) {
        Date now = new Date();
        String clockDateFormat = Settings.System.getString(getContext().getContentResolver(), Settings.System.STATUS_BAR_DATE_FORMAT);
        if (clockDateFormat == null || clockDateFormat.isEmpty()) {
            // Set dateString to short uppercase Weekday (Default for AOKP) if empty
            dateString = DateFormat.format("EEE", now);
        } else {
            dateString = DateFormat.format(clockDateFormat, now);
        }
        if (mClockDateStyle == CLOCK_DATE_STYLE_LOWERCASE) {
            // When Date style is small, convert date to lowercase
            dateResult = dateString.toString().toLowerCase();
        } else if (mClockDateStyle == CLOCK_DATE_STYLE_UPPERCASE) {
            dateResult = dateString.toString().toUpperCase();
        } else {
            dateResult = dateString.toString();
        }
        result = (clockDatePosition == STYLE_DATE_LEFT) ? dateResult + " " + timeResult : timeResult + " " + dateResult;
    } else {
        // No date, just show time
        result = timeResult;
    }
    SpannableStringBuilder formatted = new SpannableStringBuilder(result);
    if (mClockDateDisplay != CLOCK_DATE_DISPLAY_NORMAL) {
        if (dateString != null) {
            int dateStringLen = dateString.length();
            int timeStringOffset = (clockDatePosition == STYLE_DATE_RIGHT) ? timeResult.length() + 1 : 0;
            if (mClockDateDisplay == CLOCK_DATE_DISPLAY_GONE) {
                formatted.delete(0, dateStringLen);
            } else {
                if (mClockDateDisplay == CLOCK_DATE_DISPLAY_SMALL) {
                    CharacterStyle style = new RelativeSizeSpan(0.7f);
                    formatted.setSpan(style, timeStringOffset, timeStringOffset + dateStringLen, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }
            }
        }
    }
    if (mAmPmStyle != AM_PM_STYLE_NORMAL) {
        int magic1 = result.indexOf(MAGIC1);
        int magic2 = result.indexOf(MAGIC2);
        if (magic1 >= 0 && magic2 > magic1) {
            if (mAmPmStyle == AM_PM_STYLE_GONE) {
                formatted.delete(magic1, magic2 + 1);
            } else {
                if (mAmPmStyle == 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;
}
Also used : Context(android.content.Context) LocaleData(libcore.icu.LocaleData) RelativeSizeSpan(android.text.style.RelativeSizeSpan) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 62 with LocaleData

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

the class DateUtils method getMonthString.

/**
     * Return a localized string for the month of the year.
     * @param month One of {@link Calendar#JANUARY Calendar.JANUARY},
     *               {@link Calendar#FEBRUARY Calendar.FEBRUARY}, etc.
     * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_MEDIUM},
     *               or {@link #LENGTH_SHORTEST}.
     *               Undefined lengths will return {@link #LENGTH_MEDIUM}
     *               but may return something different in the future.
     * @return Localized month of the year.
     * @deprecated Use {@link java.text.SimpleDateFormat} instead.
     */
@Deprecated
public static String getMonthString(int month, int abbrev) {
    LocaleData d = LocaleData.get(Locale.getDefault());
    String[] names;
    switch(abbrev) {
        case LENGTH_LONG:
            names = d.longMonthNames;
            break;
        case LENGTH_MEDIUM:
            names = d.shortMonthNames;
            break;
        case LENGTH_SHORT:
            names = d.shortMonthNames;
            break;
        case LENGTH_SHORTER:
            names = d.shortMonthNames;
            break;
        case LENGTH_SHORTEST:
            names = d.tinyMonthNames;
            break;
        default:
            names = d.shortMonthNames;
            break;
    }
    return names[month];
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 63 with LocaleData

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

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

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

the class TextClock method init.

private void init() {
    if (mFormat12 == null || mFormat24 == null) {
        LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale);
        if (mFormat12 == null) {
            mFormat12 = ld.timeFormat_hm;
        }
        if (mFormat24 == null) {
            mFormat24 = ld.timeFormat_Hm;
        }
    }
    createTime(mTimeZone);
    // Wait until registering for events to handle the ticker
    chooseFormat(false);
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 65 with LocaleData

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

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)

Aggregations

LocaleData (libcore.icu.LocaleData)70 SpannableStringBuilder (android.text.SpannableStringBuilder)12 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 Locale (java.util.Locale)6 Test (org.junit.Test)6 Config (org.robolectric.annotation.Config)4 Date (java.util.Date)3 Resources (android.content.res.Resources)1 Implementation (org.robolectric.annotation.Implementation)1