Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c71c832a authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use the Context's locale in DateFormat"

parents 51bd66d5 3150434f
Loading
Loading
Loading
Loading
+54 −81
Original line number Original line Diff line number Diff line
@@ -177,11 +177,13 @@ public class DateFormat {
     * @hide
     * @hide
     */
     */
    public static boolean is24HourFormat(Context context, int userHandle) {
    public static boolean is24HourFormat(Context context, int userHandle) {
        String value = Settings.System.getStringForUser(context.getContentResolver(),
        final String value = Settings.System.getStringForUser(context.getContentResolver(),
                Settings.System.TIME_12_24, userHandle);
                Settings.System.TIME_12_24, userHandle);
        if (value != null) {
            return value.equals("24");
        }


        if (value == null) {
        final Locale locale = context.getResources().getConfiguration().locale;
            Locale locale = context.getResources().getConfiguration().locale;


        synchronized (sLocaleLock) {
        synchronized (sLocaleLock) {
            if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
            if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
@@ -189,31 +191,24 @@ public class DateFormat {
            }
            }
        }
        }


            java.text.DateFormat natural =
        final java.text.DateFormat natural =
                java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);
                java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);


        final boolean is24Hour;
        if (natural instanceof SimpleDateFormat) {
        if (natural instanceof SimpleDateFormat) {
                SimpleDateFormat sdf = (SimpleDateFormat) natural;
            final SimpleDateFormat sdf = (SimpleDateFormat) natural;
                String pattern = sdf.toPattern();
            final String pattern = sdf.toPattern();

            is24Hour = hasDesignator(pattern, 'H');
                if (pattern.indexOf('H') >= 0) {
                    value = "24";
                } else {
                    value = "12";
                }
        } else {
        } else {
                value = "12";
            is24Hour = false;
        }
        }


        synchronized (sLocaleLock) {
        synchronized (sLocaleLock) {
            sIs24HourLocale = locale;
            sIs24HourLocale = locale;
                sIs24Hour = value.equals("24");
            sIs24Hour = is24Hour;
        }
        }


            return sIs24Hour;
        return is24Hour;
        }

        return value.equals("24");
    }
    }


    /**
    /**
@@ -249,17 +244,18 @@ public class DateFormat {


    /**
    /**
     * Returns a {@link java.text.DateFormat} object that can format the time according
     * Returns a {@link java.text.DateFormat} object that can format the time according
     * to the current locale and the user's 12-/24-hour clock preference.
     * to the context's locale and the user's 12-/24-hour clock preference.
     * @param context the application context
     * @param context the application context
     * @return the {@link java.text.DateFormat} object that properly formats the time.
     * @return the {@link java.text.DateFormat} object that properly formats the time.
     */
     */
    public static java.text.DateFormat getTimeFormat(Context context) {
    public static java.text.DateFormat getTimeFormat(Context context) {
        return new java.text.SimpleDateFormat(getTimeFormatString(context));
        final Locale locale = context.getResources().getConfiguration().locale;
        return new java.text.SimpleDateFormat(getTimeFormatString(context), locale);
    }
    }


    /**
    /**
     * Returns a String pattern that can be used to format the time according
     * Returns a String pattern that can be used to format the time according
     * to the current locale and the user's 12-/24-hour clock preference.
     * to the context's locale and the user's 12-/24-hour clock preference.
     * @param context the application context
     * @param context the application context
     * @hide
     * @hide
     */
     */
@@ -269,45 +265,48 @@ public class DateFormat {


    /**
    /**
     * Returns a String pattern that can be used to format the time according
     * Returns a String pattern that can be used to format the time according
     * to the current locale and the user's 12-/24-hour clock preference.
     * to the context's locale and the user's 12-/24-hour clock preference.
     * @param context the application context
     * @param context the application context
     * @param userHandle the user handle of the user to query the format for
     * @param userHandle the user handle of the user to query the format for
     * @hide
     * @hide
     */
     */
    public static String getTimeFormatString(Context context, int userHandle) {
    public static String getTimeFormatString(Context context, int userHandle) {
        LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
        final LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale);
        return is24HourFormat(context, userHandle) ? d.timeFormat_Hm : d.timeFormat_hm;
        return is24HourFormat(context, userHandle) ? d.timeFormat_Hm : d.timeFormat_hm;
    }
    }


    /**
    /**
     * Returns a {@link java.text.DateFormat} object that can format the date
     * Returns a {@link java.text.DateFormat} object that can format the date
     * in short form according to the current locale.
     * in short form according to the context's locale.
     *
     *
     * @param context the application context
     * @param context the application context
     * @return the {@link java.text.DateFormat} object that properly formats the date.
     * @return the {@link java.text.DateFormat} object that properly formats the date.
     */
     */
    public static java.text.DateFormat getDateFormat(Context context) {
    public static java.text.DateFormat getDateFormat(Context context) {
        return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
        final Locale locale = context.getResources().getConfiguration().locale;
        return java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale);
    }
    }


    /**
    /**
     * Returns a {@link java.text.DateFormat} object that can format the date
     * Returns a {@link java.text.DateFormat} object that can format the date
     * in long form (such as {@code Monday, January 3, 2000}) for the current locale.
     * in long form (such as {@code Monday, January 3, 2000}) for the context's locale.
     * @param context the application context
     * @param context the application context
     * @return the {@link java.text.DateFormat} object that formats the date in long form.
     * @return the {@link java.text.DateFormat} object that formats the date in long form.
     */
     */
    public static java.text.DateFormat getLongDateFormat(Context context) {
    public static java.text.DateFormat getLongDateFormat(Context context) {
        return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG);
        final Locale locale = context.getResources().getConfiguration().locale;
        return java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG, locale);
    }
    }


    /**
    /**
     * Returns a {@link java.text.DateFormat} object that can format the date
     * Returns a {@link java.text.DateFormat} object that can format the date
     * in medium form (such as {@code Jan 3, 2000}) for the current locale.
     * in medium form (such as {@code Jan 3, 2000}) for the context's locale.
     * @param context the application context
     * @param context the application context
     * @return the {@link java.text.DateFormat} object that formats the date in long form.
     * @return the {@link java.text.DateFormat} object that formats the date in long form.
     */
     */
    public static java.text.DateFormat getMediumDateFormat(Context context) {
    public static java.text.DateFormat getMediumDateFormat(Context context) {
        return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM);
        final Locale locale = context.getResources().getConfiguration().locale;
        return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, locale);
    }
    }


    /**
    /**
@@ -320,11 +319,13 @@ public class DateFormat {
     * order returned here.
     * order returned here.
     */
     */
    public static char[] getDateFormatOrder(Context context) {
    public static char[] getDateFormatOrder(Context context) {
        return ICU.getDateFormatOrder(getDateFormatString());
        return ICU.getDateFormatOrder(getDateFormatString(context));
    }
    }


    private static String getDateFormatString() {
    private static String getDateFormatString(Context context) {
        java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
        final Locale locale = context.getResources().getConfiguration().locale;
        java.text.DateFormat df = java.text.DateFormat.getDateInstance(
                java.text.DateFormat.SHORT, locale);
        if (df instanceof SimpleDateFormat) {
        if (df instanceof SimpleDateFormat) {
            return ((SimpleDateFormat) df).toPattern();
            return ((SimpleDateFormat) df).toPattern();
        }
        }
@@ -375,6 +376,9 @@ public class DateFormat {
     * Test if a format string contains the given designator. Always returns
     * Test if a format string contains the given designator. Always returns
     * {@code false} if the input format is {@code null}.
     * {@code false} if the input format is {@code null}.
     *
     *
     * Note that this is intended for searching for designators, not arbitrary
     * characters. So searching for a literal single quote would not work correctly.
     *
     * @hide
     * @hide
     */
     */
    public static boolean hasDesignator(CharSequence inFormat, char designator) {
    public static boolean hasDesignator(CharSequence inFormat, char designator) {
@@ -382,50 +386,19 @@ public class DateFormat {


        final int length = inFormat.length();
        final int length = inFormat.length();


        int c;
        boolean insideQuote = false;
        int count;
        for (int i = 0; i < length; i++) {

            final char c = inFormat.charAt(i);
        for (int i = 0; i < length; i += count) {
            count = 1;
            c = inFormat.charAt(i);

            if (c == QUOTE) {
            if (c == QUOTE) {
                count = skipQuotedText(inFormat, i, length);
                insideQuote = !insideQuote;
            } else if (c == designator) {
            } else if (!insideQuote) {
                if (c == designator) {
                    return true;
                    return true;
                }
                }
            }
            }

        return false;
    }

    private static int skipQuotedText(CharSequence s, int i, int len) {
        if (i + 1 < len && s.charAt(i + 1) == QUOTE) {
            return 2;
        }

        int count = 1;
        // skip leading quote
        i++;

        while (i < len) {
            char c = s.charAt(i);

            if (c == QUOTE) {
                count++;
                //  QUOTEQUOTE -> QUOTE
                if (i + 1 < len && s.charAt(i + 1) == QUOTE) {
                    i++;
                } else {
                    break;
                }
            } else {
                i++;
                count++;
            }
        }
        }


        return count;
        return false;
    }
    }


    /**
    /**