diff options
author | Elliott Hughes <enh@google.com> | 2014-10-21 19:27:20 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2014-10-23 09:26:21 -0700 |
commit | 0836eb6118ddedbfe420b5d6ff98f07adf5a5d23 (patch) | |
tree | 0d494d62e149204d0e98cd5991877b69fb3d0b3d | |
parent | 5be6ff73065daac6bb53508df5191ebac4f96aea (diff) | |
download | libcore-0836eb6118ddedbfe420b5d6ff98f07adf5a5d23.zip libcore-0836eb6118ddedbfe420b5d6ff98f07adf5a5d23.tar.gz libcore-0836eb6118ddedbfe420b5d6ff98f07adf5a5d23.tar.bz2 |
Wire up frameworks notifications of 12/24 hour preferences.
Bug: 10361358
(cherry picked from commit cafe1a748caf89091d2f1c3956a43daf23b9403b)
Change-Id: I2629bbad0737bc5836afa7cfe4a9667095df0357
-rw-r--r-- | luni/src/main/java/java/text/DateFormat.java | 8 | ||||
-rw-r--r-- | luni/src/main/java/libcore/icu/LocaleData.java | 34 |
2 files changed, 37 insertions, 5 deletions
diff --git a/luni/src/main/java/java/text/DateFormat.java b/luni/src/main/java/java/text/DateFormat.java index 3055843..ebdf73a 100644 --- a/luni/src/main/java/java/text/DateFormat.java +++ b/luni/src/main/java/java/text/DateFormat.java @@ -66,6 +66,13 @@ public abstract class DateFormat extends Format { private static final long serialVersionUID = 7218322306649953788L; /** + * A tri-state boolean. If we're running stand-alone this will be null. + * If we're running in an app, the frameworks will have told us the user preference. + * @hide + */ + public static Boolean is24Hour; + + /** * The calendar that this {@code DateFormat} uses to format a number * representing a date. */ @@ -466,6 +473,7 @@ public abstract class DateFormat extends Format { * @hide for internal use only. */ public static final void set24HourTimePref(boolean is24Hour) { + DateFormat.is24Hour = is24Hour; } /** diff --git a/luni/src/main/java/libcore/icu/LocaleData.java b/luni/src/main/java/libcore/icu/LocaleData.java index 5ce6837..cca38e1 100644 --- a/luni/src/main/java/libcore/icu/LocaleData.java +++ b/luni/src/main/java/libcore/icu/LocaleData.java @@ -87,6 +87,12 @@ public final class LocaleData { // Used by android.text.format.DateFormat.getDateFormatStringForSetting. public String shortDateFormat4; + // Used by DateFormat to implement 12- and 24-hour SHORT and MEDIUM. + public String timeFormat_hm; + public String timeFormat_Hm; + public String timeFormat_hms; + public String timeFormat_Hms; + // Used by android.text.format.DateFormat.getTimeFormat. public String timeFormat12; // "hh:mm a" public String timeFormat24; // "HH:mm" @@ -175,12 +181,22 @@ public final class LocaleData { public String getTimeFormat(int style) { switch (style) { case DateFormat.SHORT: - return shortTimeFormat; + if (DateFormat.is24Hour == null) { + return shortTimeFormat; + } else { + return DateFormat.is24Hour ? timeFormat_Hm : timeFormat_hm; + } case DateFormat.MEDIUM: - return mediumTimeFormat; + if (DateFormat.is24Hour == null) { + return mediumTimeFormat; + } else { + return DateFormat.is24Hour ? timeFormat_Hms : timeFormat_hms; + } case DateFormat.LONG: + // CLDR doesn't really have anything we can use to obey the 12-/24-hour preference. return longTimeFormat; case DateFormat.FULL: + // CLDR doesn't really have anything we can use to obey the 12-/24-hour preference. return fullTimeFormat; } throw new AssertionError(); @@ -192,9 +208,17 @@ public final class LocaleData { throw new AssertionError("couldn't initialize LocaleData for locale " + locale); } - // Get the "h:mm a" and "HH:mm" 12- and 24-hour time format strings. - localeData.timeFormat12 = ICU.getBestDateTimePattern("hm", locale); - localeData.timeFormat24 = ICU.getBestDateTimePattern("Hm", locale); + // Get the SHORT and MEDIUM 12- and 24-hour time format strings. + localeData.timeFormat_hm = ICU.getBestDateTimePattern("hm", locale); + localeData.timeFormat_Hm = ICU.getBestDateTimePattern("Hm", locale); + localeData.timeFormat_hms = ICU.getBestDateTimePattern("hms", locale); + localeData.timeFormat_Hms = ICU.getBestDateTimePattern("Hms", locale); + // We could move callers over to the other fields, but these seem simpler and discourage + // people from shooting themselves in the foot by learning about patterns and skeletons. + // TODO: the right fix here is probably to move callers over to java.text.DateFormat, + // so nothing outside libcore references these any more. + localeData.timeFormat12 = localeData.timeFormat_hm; + localeData.timeFormat24 = localeData.timeFormat_Hm; // Fix up a couple of patterns. if (localeData.fullTimeFormat != null) { |