Embedded country names in the app for ones not in framework

 SX    Sint Maarten
 BQ    Caribbean Netherlands
 CW    CuraƧao
 SS    South Sudan

Bug: 8558228
Change-Id: Id555c91b5ad14b562ae5e69d2a0f0774b8c9ab51
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 56e6564..affcce1 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -112,4 +112,29 @@
         <item>"Kaliningrad Time"</item>
     </string-array>
 
+    <!--
+        Country codes
+        The order should match the entries in backup_country_names
+        ALWAYS ADD NEW ENTRIES AT THE END. Othewise the entries after the insertion point will
+        not match for languages that didn't get the translations.
+    -->
+    <string-array name="backup_country_codes" translatable="false">
+        <item>"SX"</item>
+        <item>"BQ"</item>
+        <item>"CW"</item>
+        <item>"SS"</item>
+    </string-array>
+
+    <!--
+        Display strings for country names. Used only if framework doesn't have translated
+        country names. The order should match the entries in backup_country_codes
+        ALWAYS ADD NEW ENTRIES AT THE END. Othewise the array will not be the same size until
+        all the translations are available. [CHAR LIMIT=32]
+    -->
+    <string-array name="backup_country_names">
+        <item>"Sint Maarten"</item>
+        <item>"Caribbean Netherlands"</item>
+        <item>"Curaçao"</item>
+        <item>"South Sudan"</item>
+    </string-array>
 </resources>
\ No newline at end of file
diff --git a/src/com/android/timezonepicker/TimeZoneData.java b/src/com/android/timezonepicker/TimeZoneData.java
index a0cfa4b..1105169 100644
--- a/src/com/android/timezonepicker/TimeZoneData.java
+++ b/src/com/android/timezonepicker/TimeZoneData.java
@@ -385,8 +385,7 @@
                     // name
                     String country = mCountryCodeToNameMap.get(countryCode);
                     if (country == null) {
-                        country = new Locale(lang, countryCode)
-                                .getDisplayCountry(Locale.getDefault());
+                        country = getCountryNames(lang, countryCode);
                         mCountryCodeToNameMap.put(countryCode, country);
                     }
 
@@ -456,6 +455,38 @@
         return processedTimeZones;
     }
 
+    @SuppressWarnings("unused")
+    private static Locale mBackupCountryLocale;
+    private static String[] mBackupCountryCodes;
+    private static String[] mBackupCountryNames;
+
+    private String getCountryNames(String lang, String countryCode) {
+        final Locale defaultLocale = Locale.getDefault();
+        String countryDisplayName = new Locale(lang, countryCode).getDisplayCountry(defaultLocale);
+
+        if (!countryCode.equals(countryDisplayName)) {
+            return countryDisplayName;
+        }
+
+        if (mBackupCountryCodes == null || !defaultLocale.equals(mBackupCountryLocale)) {
+            mBackupCountryLocale = defaultLocale;
+            mBackupCountryCodes = mContext.getResources().getStringArray(
+                    R.array.backup_country_codes);
+            mBackupCountryNames = mContext.getResources().getStringArray(
+                    R.array.backup_country_names);
+        }
+
+        int length = Math.min(mBackupCountryCodes.length, mBackupCountryNames.length);
+
+        for (int i = 0; i < length; i++) {
+            if (mBackupCountryCodes[i].equals(countryCode)) {
+                return mBackupCountryNames[i];
+            }
+        }
+
+        return countryCode;
+    }
+
     private int getIdenticalTimeZoneInTheCountry(TimeZoneInfo timeZoneInfo) {
         int idx = 0;
         for (TimeZoneInfo tzi : mTimeZones) {