Merge "Use the Context's locale in DateFormat"

This commit is contained in:
TreeHugger Robot
2017-07-21 01:01:41 +00:00
committed by Android (Google) Code Review

View File

@@ -177,43 +177,38 @@ public class DateFormat {
* @hide
*/
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);
if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;
synchronized (sLocaleLock) {
if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
return sIs24Hour;
}
}
java.text.DateFormat natural =
java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);
if (natural instanceof SimpleDateFormat) {
SimpleDateFormat sdf = (SimpleDateFormat) natural;
String pattern = sdf.toPattern();
if (pattern.indexOf('H') >= 0) {
value = "24";
} else {
value = "12";
}
} else {
value = "12";
}
synchronized (sLocaleLock) {
sIs24HourLocale = locale;
sIs24Hour = value.equals("24");
}
return sIs24Hour;
if (value != null) {
return value.equals("24");
}
return value.equals("24");
final Locale locale = context.getResources().getConfiguration().locale;
synchronized (sLocaleLock) {
if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
return sIs24Hour;
}
}
final java.text.DateFormat natural =
java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);
final boolean is24Hour;
if (natural instanceof SimpleDateFormat) {
final SimpleDateFormat sdf = (SimpleDateFormat) natural;
final String pattern = sdf.toPattern();
is24Hour = hasDesignator(pattern, 'H');
} else {
is24Hour = false;
}
synchronized (sLocaleLock) {
sIs24HourLocale = locale;
sIs24Hour = is24Hour;
}
return is24Hour;
}
/**
@@ -249,17 +244,18 @@ public class DateFormat {
/**
* 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
* @return the {@link java.text.DateFormat} object that properly formats the time.
*/
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
* 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
* @hide
*/
@@ -269,45 +265,48 @@ public class DateFormat {
/**
* 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 userHandle the user handle of the user to query the format for
* @hide
*/
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;
}
/**
* 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
* @return the {@link java.text.DateFormat} object that properly formats the date.
*/
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
* 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
* @return the {@link java.text.DateFormat} object that formats the date in long form.
*/
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
* 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
* @return the {@link java.text.DateFormat} object that formats the date in long form.
*/
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.
*/
public static char[] getDateFormatOrder(Context context) {
return ICU.getDateFormatOrder(getDateFormatString());
return ICU.getDateFormatOrder(getDateFormatString(context));
}
private static String getDateFormatString() {
java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
private static String getDateFormatString(Context context) {
final Locale locale = context.getResources().getConfiguration().locale;
java.text.DateFormat df = java.text.DateFormat.getDateInstance(
java.text.DateFormat.SHORT, locale);
if (df instanceof SimpleDateFormat) {
return ((SimpleDateFormat) df).toPattern();
}
@@ -375,6 +376,9 @@ public class DateFormat {
* Test if a format string contains the given designator. Always returns
* {@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
*/
public static boolean hasDesignator(CharSequence inFormat, char designator) {
@@ -382,52 +386,21 @@ public class DateFormat {
final int length = inFormat.length();
int c;
int count;
for (int i = 0; i < length; i += count) {
count = 1;
c = inFormat.charAt(i);
boolean insideQuote = false;
for (int i = 0; i < length; i++) {
final char c = inFormat.charAt(i);
if (c == QUOTE) {
count = skipQuotedText(inFormat, i, length);
} else if (c == designator) {
return true;
insideQuote = !insideQuote;
} else if (!insideQuote) {
if (c == designator) {
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;
}
/**
* Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
* containing the requested date.