Reset DateFormat.is24Hour after each test

Bug: 20899571
Bug: 20937589
Bug: 20939139
Bug: 20378566
Bug: 21585934
Bug: https://code.google.com/p/android/issues/detail?id=162384
Change-Id: I3edb30eb5228e5d36b50867675fea9a8cdb1b2d2
diff --git a/src/vogar/target/TestEnvironment.java b/src/vogar/target/TestEnvironment.java
index 40e4d2f..c5ad9d3 100644
--- a/src/vogar/target/TestEnvironment.java
+++ b/src/vogar/target/TestEnvironment.java
@@ -22,6 +22,7 @@
 import java.net.Authenticator;
 import java.net.CookieHandler;
 import java.net.ResponseCache;
+import java.text.DateFormat;
 import java.util.Locale;
 import java.util.HashMap;
 import java.util.TimeZone;
@@ -44,6 +45,20 @@
     private final HostnameVerifier defaultHostnameVerifier;
     private final SSLSocketFactory defaultSSLSocketFactory;
 
+    /** The DateFormat.is24Hour field. Not present on older versions of Android or the RI. */
+    private static final Field dateFormatIs24HourField;
+    static {
+        Field f;
+        try {
+            Class<?> dateFormatClass = Class.forName("java.text.DateFormat");
+            f = dateFormatClass.getDeclaredField("is24Hour");
+        } catch (ClassNotFoundException | NoSuchFieldException e) {
+            f = null;
+        }
+        dateFormatIs24HourField = f;
+    }
+    private final Boolean defaultDateFormatIs24Hour;
+
     private static final String JAVA_RUNTIME_VERSION = System.getProperty("java.runtime.version"); 
     private static final String JAVA_VM_INFO = System.getProperty("java.vm.info"); 
     private static final String JAVA_VM_VERSION = System.getProperty("java.vm.version"); 
@@ -71,6 +86,7 @@
 
         defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
         defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
+        defaultDateFormatIs24Hour = hasDateFormatIs24Hour() ? getDateFormatIs24Hour() : null;
 
         disableSecurity();
     }
@@ -115,6 +131,9 @@
         // Localization
         Locale.setDefault(Locale.US);
         TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
+        if (hasDateFormatIs24Hour()) {
+            setDateFormatIs24Hour(defaultDateFormatIs24Hour);
+        }
 
         // Preferences
         // Temporarily silence the java.util.prefs logger, which otherwise emits
@@ -196,4 +215,28 @@
         } catch (Exception ignored) {
         }
     }
+
+    private static boolean hasDateFormatIs24Hour() {
+        return dateFormatIs24HourField != null;
+    }
+
+    private static Boolean getDateFormatIs24Hour() {
+        try {
+            return (Boolean) dateFormatIs24HourField.get(null);
+        } catch (IllegalAccessException e) {
+            Error e2 = new AssertionError("Unable to get java.text.DateFormat.is24Hour");
+            e2.initCause(e);
+            throw e2;
+        }
+    }
+
+    private static void setDateFormatIs24Hour(Boolean value) {
+        try {
+            dateFormatIs24HourField.set(null, value);
+        } catch (IllegalAccessException e) {
+            Error e2 = new AssertionError("Unable to set java.text.DateFormat.is24Hour");
+            e2.initCause(e);
+            throw e2;
+        }
+    }
 }