Display custom input style name in system locale

Bug: 8032166
Change-Id: Ib9a6b63c4d540ce377892fb865e53abdd8adec16
main
Tadashi G. Takaoka 2013-01-23 15:41:43 +09:00
parent 9d514af466
commit 8203f7afe7
7 changed files with 240 additions and 48 deletions

View File

@ -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>

View File

@ -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>

View File

@ -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"

View File

@ -72,7 +72,8 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment {
}
public SubtypeLocaleItem(final String localeString) {
this(localeString, SubtypeLocale.getSubtypeLocaleDisplayName(localeString));
this(localeString,
SubtypeLocale.getSubtypeLocaleDisplayNameInSystemLocale(localeString));
}
@Override
@ -103,7 +104,7 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment {
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 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment {
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 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment {
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();
}

View File

@ -237,7 +237,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment
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);
}

View File

@ -51,20 +51,22 @@ public final class SubtypeLocale {
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 @@ public final class SubtypeLocale {
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 final class SubtypeLocale {
}
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 @@ public final class SubtypeLocale {
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 @@ public final class SubtypeLocale {
// 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 @@ public final class SubtypeLocale {
}
};
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) {

View File

@ -42,8 +42,10 @@ public class SubtypeLocaleTests extends AndroidTestCase {
InputMethodSubtype ZZ;
InputMethodSubtype DE_QWERTY;
InputMethodSubtype FR_QWERTZ;
InputMethodSubtype US_AZERTY;
InputMethodSubtype ZZ_AZERTY;
InputMethodSubtype EN_US_AZERTY;
InputMethodSubtype EN_UK_DVORAK;
InputMethodSubtype ES_US_COLEMAK;
InputMethodSubtype ZZ_PC;
@Override
protected void setUp() throws Exception {
@ -72,10 +74,14 @@ public class SubtypeLocaleTests extends AndroidTestCase {
Locale.GERMAN.toString(), "qwerty", null);
FR_QWERTZ = AdditionalSubtype.createAdditionalSubtype(
Locale.FRENCH.toString(), "qwertz", null);
US_AZERTY = AdditionalSubtype.createAdditionalSubtype(
EN_US_AZERTY = AdditionalSubtype.createAdditionalSubtype(
Locale.US.toString(), "azerty", null);
ZZ_AZERTY = AdditionalSubtype.createAdditionalSubtype(
SubtypeLocale.NO_LANGUAGE, "azerty", null);
EN_UK_DVORAK = AdditionalSubtype.createAdditionalSubtype(
Locale.UK.toString(), "dvorak", null);
ES_US_COLEMAK = AdditionalSubtype.createAdditionalSubtype(
"es_US", "colemak", null);
ZZ_PC = AdditionalSubtype.createAdditionalSubtype(
SubtypeLocale.NO_LANGUAGE, "pcqwerty", null);
}
@ -106,8 +112,10 @@ public class SubtypeLocaleTests extends AndroidTestCase {
// 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)
// zz azerty T No language (AZERTY) in system locale
// en_US azerty T English (US) (AZERTY) exception
// en_UK dvorak T English (UK) (Dvorak) exception
// es_US colemak T Español (EE.UU.) (Colemak) exception
// zz pc T No language (PC) in system locale
public void testPredefinedSubtypesInEnglish() {
assertEquals("en_US", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(EN_US));
@ -150,9 +158,13 @@ public class SubtypeLocaleTests extends AndroidTestCase {
assertEquals("de qwerty", "Deutsch (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(DE_QWERTY));
assertEquals("en_US azerty", "English (US) (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(US_AZERTY));
assertEquals("zz azerty", "No language (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ_AZERTY));
SubtypeLocale.getSubtypeDisplayName(EN_US_AZERTY));
assertEquals("en_UK dvorak", "English (UK) (Dvorak)",
SubtypeLocale.getSubtypeDisplayName(EN_UK_DVORAK));
assertEquals("es_US colemak","Español (EE.UU.) (Colemak)",
SubtypeLocale.getSubtypeDisplayName(ES_US_COLEMAK));
assertEquals("zz pc", "No language (PC)",
SubtypeLocale.getSubtypeDisplayName(ZZ_PC));
return null;
}
};
@ -192,9 +204,149 @@ public class SubtypeLocaleTests extends AndroidTestCase {
assertEquals("de qwerty", "Deutsch (QWERTY)",
SubtypeLocale.getSubtypeDisplayName(DE_QWERTY));
assertEquals("en_US azerty", "English (US) (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(US_AZERTY));
assertEquals("zz azerty", "Aucune langue (AZERTY)",
SubtypeLocale.getSubtypeDisplayName(ZZ_AZERTY));
SubtypeLocale.getSubtypeDisplayName(EN_US_AZERTY));
assertEquals("en_UK dvorak", "English (UK) (Dvorak)",
SubtypeLocale.getSubtypeDisplayName(EN_UK_DVORAK));
assertEquals("es_US colemak","Español (EE.UU.) (Colemak)",
SubtypeLocale.getSubtypeDisplayName(ES_US_COLEMAK));
assertEquals("zz azerty", "Aucune langue (PC)",
SubtypeLocale.getSubtypeDisplayName(ZZ_PC));
return null;
}
};
tests.runInLocale(mRes, Locale.FRENCH);
}
// InputMethodSubtype's display name in system locale (en_US).
// isAdditionalSubtype (T=true, F=false)
// locale layout | display name
// ------ ------- - ----------------------
// en_US qwerty F English (US) exception
// en_GB qwerty F English (UK) exception
// es_US spanish F Spanish (US) exception
// fr azerty F French
// fr_CA qwerty F French (Canada)
// de qwertz F German
// zz qwerty F No language (QWERTY)
// fr qwertz T French (QWERTZ)
// de qwerty T German (QWERTY)
// en_US azerty T English (US) (AZERTY) exception
// en_UK dvorak T English (UK) (Dvorak) exception
// es_US colemak T Spanish (US) (Colemak) exception
// zz pc T No language (PC)
public void testPredefinedSubtypesInEnglishSystemLocale() {
assertEquals("en_US", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(EN_US));
assertEquals("en_GB", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(EN_GB));
assertEquals("es_US", "spanish", SubtypeLocale.getKeyboardLayoutSetName(ES_US));
assertEquals("fr ", "azerty", SubtypeLocale.getKeyboardLayoutSetName(FR));
assertEquals("fr_CA", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(FR_CA));
assertEquals("de ", "qwertz", SubtypeLocale.getKeyboardLayoutSetName(DE));
assertEquals("zz ", "qwerty", SubtypeLocale.getKeyboardLayoutSetName(ZZ));
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("en_US", "English (US)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_US));
assertEquals("en_GB", "English (UK)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_GB));
assertEquals("es_US", "Spanish (US)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ES_US));
assertEquals("fr ", "French",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(FR));
assertEquals("fr_CA", "French (Canada)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(FR_CA));
assertEquals("de ", "German",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(DE));
assertEquals("zz ", "No language (QWERTY)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ZZ));
return null;
}
};
tests.runInLocale(mRes, Locale.ENGLISH);
}
public void testAdditionalSubtypesInEnglishSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("fr qwertz", "French (QWERTZ)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(FR_QWERTZ));
assertEquals("de qwerty", "German (QWERTY)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(DE_QWERTY));
assertEquals("en_US azerty", "English (US) (AZERTY)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_US_AZERTY));
assertEquals("en_UK dvorak", "English (UK) (Dvorak)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_UK_DVORAK));
assertEquals("es_US colemak","Spanish (US) (Colemak)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ES_US_COLEMAK));
assertEquals("zz azerty", "No language (PC)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ZZ_PC));
return null;
}
};
tests.runInLocale(mRes, Locale.ENGLISH);
}
// InputMethodSubtype's display name in system locale (fr).
// isAdditionalSubtype (T=true, F=false)
// locale layout | display name
// ------ ------- - ----------------------
// en_US qwerty F Anglais (États-Unis) exception
// en_GB qwerty F Anglais (Royaume-Uni) exception
// es_US spanish F Espagnol (États-Unis) exception
// fr azerty F Français
// fr_CA qwerty F Français (Canada)
// de qwertz F Allemand
// zz qwerty F Pas de langue (QWERTY)
// fr qwertz T Français (QWERTZ)
// de qwerty T Allemand (QWERTY)
// en_US azerty T Anglais (États-Unis) (AZERTY) exception
// en_UK dvorak T Anglais (Royaume-Uni) (Dvorak) exception
// es_US colemak T Espagnol (États-Unis) (Colemak) exception
// zz pc T Aucune langue (PC)
public void testPredefinedSubtypesInFrenchSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("en_US", "Anglais (États-Unis)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_US));
assertEquals("en_GB", "Anglais (Royaume-Uni)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_GB));
assertEquals("es_US", "Espagnol (États-Unis)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ES_US));
assertEquals("fr ", "Français",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(FR));
assertEquals("fr_CA", "Français (Canada)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(FR_CA));
assertEquals("de ", "Allemand",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(DE));
assertEquals("zz ", "Pas de langue (QWERTY)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ZZ));
return null;
}
};
tests.runInLocale(mRes, Locale.FRENCH);
}
public void testAdditionalSubtypesInFrenchSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
assertEquals("fr qwertz", "Français (QWERTZ)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(FR_QWERTZ));
assertEquals("de qwerty", "Allemand (QWERTY)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(DE_QWERTY));
assertEquals("en_US azerty", "Anglais (États-Unis) (AZERTY)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_US_AZERTY));
assertEquals("en_UK dvorak", "Anglais (Royaume-Uni) (Dvorak)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(EN_UK_DVORAK));
assertEquals("es_US colemak","Espagnol (États-Unis) (Colemak)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ES_US_COLEMAK));
assertEquals("zz azerty", "Aucune langue (PC)",
SubtypeLocale.getSubtypeDisplayNameInSystemLocale(ZZ_PC));
return null;
}
};