summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author vichang <vichang@google.com> 2020-10-15 20:20:06 +0000
committer Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> 2020-10-15 20:20:06 +0000
commitc9ba946b80cd67c58f7ca52bfec4c3d10127deaf (patch)
treee36c21ac6b0f43184327b25a4860f084ae95df55
parent7307875a4f228a3a44f02ec9df81b9e57052ce3a (diff)
parent4d95bacaa8711e6db7ef8e4c5cc2c130eefc9b28 (diff)
Merge "Allow the skeleton of "jmma" in DateFormat.getBestDateTimePattern()" am: be4e63522f am: aef91d9f66 am: a222a9b750 am: 4d95bacaa8
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1458400 Change-Id: I44f974bceabcee75250ccd491153ef3ac14f5762
-rwxr-xr-xcore/java/android/text/format/DateFormat.java18
-rw-r--r--core/tests/coretests/src/android/text/format/DateFormatTest.java34
2 files changed, 51 insertions, 1 deletions
diff --git a/core/java/android/text/format/DateFormat.java b/core/java/android/text/format/DateFormat.java
index 38e3b39f8cfc..4a0bec1300b7 100755
--- a/core/java/android/text/format/DateFormat.java
+++ b/core/java/android/text/format/DateFormat.java
@@ -17,10 +17,14 @@
package android.text.format;
import android.annotation.NonNull;
+import android.app.compat.CompatChanges;
+import android.compat.annotation.ChangeId;
+import android.compat.annotation.EnabledAfter;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.icu.text.DateFormatSymbols;
import android.icu.text.DateTimePatternGenerator;
+import android.os.Build;
import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
@@ -159,6 +163,16 @@ public class DateFormat {
private static boolean sIs24Hour;
/**
+ * {@link #getBestDateTimePattern(Locale, String)} does not allow non-consecutive repeated
+ * symbol in the skeleton. For example, please use a skeleton of {@code "jmm"} or
+ * {@code "hmma"} instead of {@code "ahmma"} or {@code "jmma"}, because the field 'j' could
+ * mean using 12-hour in some locales and, in this case, is duplicated as the 'a' field.
+ */
+ @ChangeId
+ @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.R)
+ static final long DISALLOW_DUPLICATE_FIELD_IN_SKELETON = 170233598L;
+
+ /**
* Returns true if times should be formatted as 24 hour times, false if times should be
* formatted as 12 hour (AM/PM) times. Based on the user's chosen locale and other preferences.
* @param context the context to use for the content resolver
@@ -251,7 +265,9 @@ public class DateFormat {
*/
public static String getBestDateTimePattern(Locale locale, String skeleton) {
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(locale);
- return dtpg.getBestPattern(skeleton);
+ boolean allowDuplicateFields = !CompatChanges.isChangeEnabled(
+ DISALLOW_DUPLICATE_FIELD_IN_SKELETON);
+ return dtpg.getBestPattern(skeleton, allowDuplicateFields);
}
/**
diff --git a/core/tests/coretests/src/android/text/format/DateFormatTest.java b/core/tests/coretests/src/android/text/format/DateFormatTest.java
index a3434e885012..212cc44eefab 100644
--- a/core/tests/coretests/src/android/text/format/DateFormatTest.java
+++ b/core/tests/coretests/src/android/text/format/DateFormatTest.java
@@ -21,13 +21,19 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import android.compat.testing.PlatformCompatChangeRule;
import android.icu.text.DateFormatSymbols;
import android.platform.test.annotations.Presubmit;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
+import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
+import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
+
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import java.util.Arrays;
@@ -38,6 +44,9 @@ import java.util.Locale;
@RunWith(AndroidJUnit4.class)
public class DateFormatTest {
+ @Rule
+ public TestRule compatChangeRule = new PlatformCompatChangeRule();
+
@Test
public void testHasDesignator() {
assertTrue(DateFormat.hasDesignator("hh:mm:ss", DateFormat.MINUTE));
@@ -135,4 +144,29 @@ public class DateFormatTest {
private static String best(Locale l, String skeleton) {
return DateFormat.getBestDateTimePattern(l, skeleton);
}
+
+ @Test
+ @EnableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON})
+ public void testGetBestDateTimePattern_disableDuplicateField() {
+ assertIllegalArgumentException(Locale.US, "jmma");
+ assertIllegalArgumentException(Locale.US, "ahmma");
+ }
+
+ @Test
+ @DisableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON})
+ public void testGetBestDateTimePattern_enableDuplicateField() {
+ // en-US uses 12-hour format by default.
+ assertEquals("h:mm a", DateFormat.getBestDateTimePattern(Locale.US, "jmma"));
+ assertEquals("h:mm a", DateFormat.getBestDateTimePattern(Locale.US, "ahmma"));
+ }
+
+ private static void assertIllegalArgumentException(Locale l, String skeleton) {
+ try {
+ DateFormat.getBestDateTimePattern(l, skeleton);
+ fail("getBestDateTimePattern() does not fail with Locale: " + l
+ + " skeleton: " + skeleton);
+ } catch (IllegalArgumentException expected) {
+ // ignored
+ }
+ }
}