Display custom input style name in system locale

Bug: 8032166
Change-Id: Ib9a6b63c4d540ce377892fb865e53abdd8adec16
diff --git a/java/res/values/donottranslate.xml b/java/res/values/donottranslate.xml
index 36412b4..edf615a 100644
--- a/java/res/values/donottranslate.xml
+++ b/java/res/values/donottranslate.xml
@@ -120,18 +120,14 @@
     </string-array>
 
     <!-- Subtype locale display name exceptions.
-         For each exception, there should be related string resource for display name that has
-         explicit keyboard layout. The string resource name must be "subtype_with_layout_<locale>. -->
+         For each exception, there should be related string resources for display name that may have
+         explicit keyboard layout. The string resource name must be "subtype_<locale>" or
+         "subtype_with_layout_<locale>. Please refer to strings.xml for these resources. -->
     <string-array name="subtype_locale_exception_keys">
         <item>en_US</item>
         <item>en_GB</item>
         <item>es_US</item>
     </string-array>
-    <string-array name="subtype_locale_exception_values">
-        <item>English (US)</item>
-        <item>English (UK)</item>
-        <item>Español (EE.UU.)</item>
-    </string-array>
 
     <!-- Generic subtype label -->
     <string name="subtype_generic">%s</string>
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index e39cafc..ff15256 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -319,12 +319,17 @@
     <string name="subtype_en_GB">English (UK)</string>
     <!-- Description for English (United States) keyboard subtype [CHAR LIMIT=25] -->
     <string name="subtype_en_US">English (US)</string>
+    <!-- Description for Spanish (United States) keyboard subtype [CHAR LIMIT=25] -->
+    <string name="subtype_es_US">Spanish (US)</string>
     <!-- Description for English (United Kingdom) keyboard subtype with explicit keyboard layout [CHAR LIMIT=25]
          This should be identical to subtype_en_GB aside from the trailing (%s). -->
     <string name="subtype_with_layout_en_GB">English (UK) (<xliff:g id="layout">%s</xliff:g>)</string>
     <!-- Description for English (United States) keyboard subtype with explicit keyboard layout [CHAR LIMIT=25]
          This should be identical to subtype_en_US aside from the trailing (%s). -->
     <string name="subtype_with_layout_en_US">English (US) (<xliff:g id="layout">%s</xliff:g>)</string>
+    <!-- Description for Spanish (United States) keyboard subtype with explicit keyboard layout [CHAR LIMIT=25]
+         This should be identical to subtype_es_US aside from the trailing (%s). -->
+    <string name="subtype_with_layout_es_US">Spanish (US) (<xliff:g id="layout">%s</xliff:g>)</string>
     <!-- TODO: Uncomment once we can handle IETF language tag with script name specified.
          Description for Serbian Cyrillic keyboard subtype [CHAR LIMIT=25]
     <string name="subtype_serbian_cyrillic">Serbian (Cyrillic)</string>
diff --git a/java/res/xml/method.xml b/java/res/xml/method.xml
index aa59c57..f30ef23 100644
--- a/java/res/xml/method.xml
+++ b/java/res/xml/method.xml
@@ -186,7 +186,7 @@
             android:imeSubtypeExtraValue="AsciiCapable,SupportTouchPositionCorrection"
     />
     <subtype android:icon="@drawable/ic_subtype_keyboard"
-            android:label="@string/subtype_generic"
+            android:label="@string/subtype_es_US"
             android:subtypeId="0x84d2efc6"
             android:imeSubtypeLocale="es_US"
             android:imeSubtypeMode="keyboard"
diff --git a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java
index a56c78b..f787722 100644
--- a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java
+++ b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java
@@ -72,7 +72,8 @@
         }
 
         public SubtypeLocaleItem(final String localeString) {
-            this(localeString, SubtypeLocale.getSubtypeLocaleDisplayName(localeString));
+            this(localeString,
+                    SubtypeLocale.getSubtypeLocaleDisplayNameInSystemLocale(localeString));
         }
 
         @Override
@@ -103,7 +104,7 @@
                 if (DEBUG_SUBTYPE_ID) {
                     android.util.Log.d(TAG, String.format("%-6s 0x%08x %11d %s",
                             subtype.getLocale(), subtype.hashCode(), subtype.hashCode(),
-                            SubtypeLocale.getSubtypeDisplayName(subtype)));
+                            SubtypeLocale.getSubtypeDisplayNameInSystemLocale(subtype)));
                 }
                 if (subtype.containsExtraValueKey(ASCII_CAPABLE)) {
                     items.add(createItem(context, subtype.getLocale()));
@@ -205,7 +206,8 @@
                 setDialogTitle(R.string.add_style);
                 setKey(KEY_NEW_SUBTYPE);
             } else {
-                final String displayName = SubtypeLocale.getSubtypeDisplayName(subtype);
+                final String displayName =
+                        SubtypeLocale.getSubtypeDisplayNameInSystemLocale(subtype);
                 setTitle(displayName);
                 setDialogTitle(displayName);
                 setKey(KEY_PREFIX + subtype.getLocale() + "_"
@@ -497,7 +499,7 @@
         final Context context = getActivity();
         final Resources res = context.getResources();
         final String message = res.getString(R.string.custom_input_style_already_exists,
-                SubtypeLocale.getSubtypeDisplayName(subtype));
+                SubtypeLocale.getSubtypeDisplayNameInSystemLocale(subtype));
         Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     }
 
diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java
index 3ba24fb..7f3d1ae 100644
--- a/java/src/com/android/inputmethod/latin/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java
@@ -237,7 +237,7 @@
         final StringBuilder styles = new StringBuilder();
         for (final InputMethodSubtype subtype : subtypes) {
             if (styles.length() > 0) styles.append(", ");
-            styles.append(SubtypeLocale.getSubtypeDisplayName(subtype));
+            styles.append(SubtypeLocale.getSubtypeDisplayNameInSystemLocale(subtype));
         }
         customInputStyles.setSummary(styles);
     }
diff --git a/java/src/com/android/inputmethod/latin/SubtypeLocale.java b/java/src/com/android/inputmethod/latin/SubtypeLocale.java
index 068c34e..2f26f92 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeLocale.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeLocale.java
@@ -51,20 +51,22 @@
     private static final HashMap<String, Integer> sKeyboardLayoutToNameIdsMap =
             CollectionUtils.newHashMap();
     // Exceptional locale to subtype name resource id map.
+    private static final HashMap<String, Integer> sExceptionalLocaleToNameIdsMap =
+            CollectionUtils.newHashMap();
+    // Exceptional locale to subtype name with layout resource id map.
     private static final HashMap<String, Integer> sExceptionalLocaleToWithLayoutNameIdsMap =
             CollectionUtils.newHashMap();
+    private static final String SUBTYPE_NAME_RESOURCE_PREFIX =
+            "string/subtype_";
     private static final String SUBTYPE_NAME_RESOURCE_GENERIC_PREFIX =
             "string/subtype_generic_";
     private static final String SUBTYPE_NAME_RESOURCE_WITH_LAYOUT_PREFIX =
             "string/subtype_with_layout_";
     private static final String SUBTYPE_NAME_RESOURCE_NO_LANGUAGE_PREFIX =
             "string/subtype_no_language_";
-    // Exceptional locales to display name map.
-    private static final HashMap<String, String> sExceptionalDisplayNamesMap =
-            CollectionUtils.newHashMap();
     // Keyboard layout set name for the subtypes that don't have a keyboardLayoutSet extra value.
     // This is for compatibility to keep the same subtype ids as pre-JellyBean.
-    private static final HashMap<String,String> sLocaleAndExtraValueToKeyboardLayoutSetMap =
+    private static final HashMap<String, String> sLocaleAndExtraValueToKeyboardLayoutSetMap =
             CollectionUtils.newHashMap();
 
     private SubtypeLocale() {
@@ -98,14 +100,16 @@
 
         final String[] exceptionalLocales = res.getStringArray(
                 R.array.subtype_locale_exception_keys);
-        final String[] exceptionalDisplayNames = res.getStringArray(
-                R.array.subtype_locale_exception_values);
         for (int i = 0; i < exceptionalLocales.length; i++) {
             final String localeString = exceptionalLocales[i];
-            sExceptionalDisplayNamesMap.put(localeString, exceptionalDisplayNames[i]);
-            final String resourceName = SUBTYPE_NAME_RESOURCE_WITH_LAYOUT_PREFIX + localeString;
+            final String resourceName = SUBTYPE_NAME_RESOURCE_PREFIX + localeString;
             final int resId = res.getIdentifier(resourceName, null, RESOURCE_PACKAGE_NAME);
-            sExceptionalLocaleToWithLayoutNameIdsMap.put(localeString, resId);
+            sExceptionalLocaleToNameIdsMap.put(localeString, resId);
+            final String resourceNameWithLayout =
+                    SUBTYPE_NAME_RESOURCE_WITH_LAYOUT_PREFIX + localeString;
+            final int resIdWithLayout = res.getIdentifier(
+                    resourceNameWithLayout, null, RESOURCE_PACKAGE_NAME);
+            sExceptionalLocaleToWithLayoutNameIdsMap.put(localeString, resIdWithLayout);
         }
 
         final String[] keyboardLayoutSetMap = res.getStringArray(
@@ -124,7 +128,7 @@
     }
 
     public static boolean isExceptionalLocale(final String localeString) {
-        return sExceptionalLocaleToWithLayoutNameIdsMap.containsKey(localeString);
+        return sExceptionalLocaleToNameIdsMap.containsKey(localeString);
     }
 
     private static final String getNoLanguageLayoutKey(final String keyboardLayoutName) {
@@ -143,13 +147,33 @@
         return nameId == null ? UNKNOWN_KEYBOARD_LAYOUT : nameId;
     }
 
+    public static String getSubtypeLocaleDisplayNameInSystemLocale(final String localeString) {
+        final Locale displayLocale = sResources.getConfiguration().locale;
+        return getSubtypeLocaleDisplayNameInternal(localeString, displayLocale);
+    }
+
     public static String getSubtypeLocaleDisplayName(final String localeString) {
-        final String exceptionalValue = sExceptionalDisplayNamesMap.get(localeString);
-        if (exceptionalValue != null) {
-            return exceptionalValue;
-        }
+        final Locale displayLocale = LocaleUtils.constructLocaleFromString(localeString);
+        return getSubtypeLocaleDisplayNameInternal(localeString, displayLocale);
+    }
+
+    private static String getSubtypeLocaleDisplayNameInternal(final String localeString,
+            final Locale displayLocale) {
         final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
-        return StringUtils.toTitleCase(locale.getDisplayName(locale), locale);
+        final Integer exceptionalNameResId = sExceptionalLocaleToNameIdsMap.get(localeString);
+        final String displayName;
+        if (exceptionalNameResId != null) {
+            final RunInLocale<String> getExceptionalName = new RunInLocale<String>() {
+                @Override
+                protected String job(final Resources res) {
+                    return res.getString(exceptionalNameResId);
+                }
+            };
+            displayName = getExceptionalName.runInLocale(sResources, displayLocale);
+        } else {
+            displayName = locale.getDisplayName(displayLocale);
+        }
+        return StringUtils.toTitleCase(displayName, displayLocale);
     }
 
     // InputMethodSubtype's display name in its locale.
@@ -165,24 +189,36 @@
     //  zz    qwerty  F  No language (QWERTY)    in system locale
     //  fr    qwertz  T  Français (QWERTZ)
     //  de    qwerty  T  Deutsch (QWERTY)
-    //  en_US azerty  T  English (US) (AZERTY)
+    //  en_US azerty  T  English (US) (AZERTY)   exception
     //  zz    azerty  T  No language (AZERTY)    in system locale
 
-    private static String getReplacementString(final InputMethodSubtype subtype) {
+    private static String getReplacementString(final InputMethodSubtype subtype,
+            final Locale displayLocale) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                 && subtype.containsExtraValueKey(UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)) {
             return subtype.getExtraValueOf(UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME);
         } else {
-            return getSubtypeLocaleDisplayName(subtype.getLocale());
+            return getSubtypeLocaleDisplayNameInternal(subtype.getLocale(), displayLocale);
         }
     }
 
+    public static String getSubtypeDisplayNameInSystemLocale(final InputMethodSubtype subtype) {
+        final Locale subtypeLocale = sResources.getConfiguration().locale;
+        return getSubtypeDisplayNameInternal(subtype, subtypeLocale);
+    }
+
     public static String getSubtypeDisplayName(final InputMethodSubtype subtype) {
-        final String replacementString = getReplacementString(subtype);
+        final Locale subtypeLocale = LocaleUtils.constructLocaleFromString(subtype.getLocale());
+        return getSubtypeDisplayNameInternal(subtype, subtypeLocale);
+    }
+
+    private static String getSubtypeDisplayNameInternal(final InputMethodSubtype subtype,
+            final Locale displayLocale) {
+        final String replacementString = getReplacementString(subtype, displayLocale);
         final int nameResId = subtype.getNameResId();
         final RunInLocale<String> getSubtypeName = new RunInLocale<String>() {
             @Override
-            protected String job(Resources res) {
+            protected String job(final Resources res) {
                 try {
                     return res.getString(nameResId, replacementString);
                 } catch (Resources.NotFoundException e) {
@@ -197,8 +233,9 @@
             }
         };
         final Locale locale = isNoLanguage(subtype)
-                ? sResources.getConfiguration().locale : getSubtypeLocale(subtype);
-        return getSubtypeName.runInLocale(sResources, locale);
+                ? sResources.getConfiguration().locale : displayLocale;
+        return StringUtils.toTitleCase(
+                getSubtypeName.runInLocale(sResources, locale), locale);
     }
 
     public static boolean isNoLanguage(final InputMethodSubtype subtype) {