From 38026b4f03fb4d846b8613d889d68c439f6e30cb Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 20 Apr 2012 18:25:05 +0900 Subject: [PATCH] Predefined keyboard layouts are configurable via XML resource Bug: 4460018 Change-Id: Ia03ec826fb3853ea814d9764686903dbe31a6507 --- java/res/values/donottranslate.xml | 21 ++++++-- .../inputmethod/latin/AdditionalSubtype.java | 25 +-------- .../latin/AdditionalSubtypeSettings.java | 2 +- .../inputmethod/latin/SubtypeLocale.java | 52 +++++++++++++++---- .../inputmethod/latin/SubtypeSwitcher.java | 2 +- .../inputmethod/latin/SubtypeLocaleTests.java | 20 +++---- 6 files changed, 73 insertions(+), 49 deletions(-) diff --git a/java/res/values/donottranslate.xml b/java/res/values/donottranslate.xml index b97d4d7d4..99b80d0b6 100644 --- a/java/res/values/donottranslate.xml +++ b/java/res/values/donottranslate.xml @@ -146,13 +146,26 @@ %s - + + + + qwerty + qwertz + azerty + dvorak + + + + QWERTY + QWERTZ + AZERTY + Dvorak + + %s (QWERTY) - %s (QWERTZ) - %s (AZERTY) - %s (Dvorak) diff --git a/java/src/com/android/inputmethod/latin/AdditionalSubtype.java b/java/src/com/android/inputmethod/latin/AdditionalSubtype.java index 1e405f9a2..458d9ee14 100644 --- a/java/src/com/android/inputmethod/latin/AdditionalSubtype.java +++ b/java/src/com/android/inputmethod/latin/AdditionalSubtype.java @@ -22,30 +22,8 @@ import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.KEYBOAR import android.view.inputmethod.InputMethodSubtype; -import java.util.HashMap; public class AdditionalSubtype { - public static final String QWERTY = "qwerty"; - public static final String QWERTZ = "qwertz"; - public static final String AZERTY = "azerty"; - public static final String DVORAK = "dvorak"; - public static final String[] PREDEFINED_KEYBOARD_LAYOUT_SET = { - QWERTY, - QWERTZ, - AZERTY, - DVORAK - }; - - // Keyboard layout to subtype name resource id map. - private static final HashMap sKeyboardLayoutToNameIdsMap = - new HashMap(); - - static { - sKeyboardLayoutToNameIdsMap.put(QWERTY, R.string.subtype_generic_qwerty); - sKeyboardLayoutToNameIdsMap.put(QWERTZ, R.string.subtype_generic_qwertz); - sKeyboardLayoutToNameIdsMap.put(AZERTY, R.string.subtype_generic_azerty); - sKeyboardLayoutToNameIdsMap.put(DVORAK, R.string.subtype_generic_dvorak); - } private AdditionalSubtype() { // This utility class is not publicly instantiable. @@ -63,7 +41,8 @@ public class AdditionalSubtype { final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName; final String filteredExtraValue = StringUtils.appendToCsvIfNotExists( IS_ADDITIONAL_SUBTYPE, extraValue); - Integer nameId = sKeyboardLayoutToNameIdsMap.get(keyboardLayoutSetName); + Integer nameId = SubtypeLocale.getSubtypeNameIdFromKeyboardLayoutName( + keyboardLayoutSetName); if (nameId == null) nameId = R.string.subtype_generic; return new InputMethodSubtype(nameId, R.drawable.ic_subtype_keyboard, localeString, KEYBOARD_MODE, diff --git a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java index 7a22c9742..b67f327d7 100644 --- a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java +++ b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java @@ -129,7 +129,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // TODO: Should filter out already existing combinations of locale and layout. - for (final String layout : AdditionalSubtype.PREDEFINED_KEYBOARD_LAYOUT_SET) { + for (final String layout : SubtypeLocale.getPredefinedKeyboardLayoutSet()) { add(new KeyboardLayoutSetItem(layout)); } } diff --git a/java/src/com/android/inputmethod/latin/SubtypeLocale.java b/java/src/com/android/inputmethod/latin/SubtypeLocale.java index d291d1a2e..33ad23a60 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeLocale.java +++ b/java/src/com/android/inputmethod/latin/SubtypeLocale.java @@ -27,10 +27,23 @@ import java.util.Locale; public class SubtypeLocale { private static final String TAG = SubtypeLocale.class.getSimpleName(); + // This class must be located in the same package as LatinIME.java. + private static final String RESOURCE_PACKAGE_NAME = + DictionaryFactory.class.getPackage().getName(); // Special language code to represent "no language". public static final String NO_LANGUAGE = "zz"; + public static final String QWERTY = "qwerty"; + + private static String[] sPredefinedKeyboardLayoutSet; + // Keyboard layout to its display name map. + private static final HashMap sKeyboardKayoutToDisplayNameMap = + new HashMap(); + // Keyboard layout to subtype name resource id map. + private static final HashMap sKeyboardLayoutToNameIdsMap = + new HashMap(); + private static final String SUBTYPE_RESOURCE_GENERIC_NAME_PREFIX = "string/subtype_generic_"; // Exceptional locales to display name map. private static final HashMap sExceptionalDisplayNamesMap = new HashMap(); @@ -41,11 +54,34 @@ public class SubtypeLocale { public static void init(Context context) { final Resources res = context.getResources(); - final String[] locales = res.getStringArray(R.array.subtype_locale_exception_keys); - final String[] displayNames = res.getStringArray(R.array.subtype_locale_exception_values); - for (int i = 0; i < locales.length; i++) { - sExceptionalDisplayNamesMap.put(locales[i], displayNames[i]); + + final String[] predefinedLayoutSet = res.getStringArray(R.array.predefined_layouts); + sPredefinedKeyboardLayoutSet = predefinedLayoutSet; + final String[] layoutDisplayNames = res.getStringArray( + R.array.predefined_layout_display_names); + for (int i = 0; i < predefinedLayoutSet.length; i++) { + final String layoutName = predefinedLayoutSet[i]; + sKeyboardKayoutToDisplayNameMap.put(layoutName, layoutDisplayNames[i]); + final String resourceName = SUBTYPE_RESOURCE_GENERIC_NAME_PREFIX + layoutName; + final int resId = res.getIdentifier(resourceName, null, RESOURCE_PACKAGE_NAME); + sKeyboardLayoutToNameIdsMap.put(layoutName, resId); } + + 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++) { + sExceptionalDisplayNamesMap.put(exceptionalLocales[i], exceptionalDisplayNames[i]); + } + } + + public static String[] getPredefinedKeyboardLayoutSet() { + return sPredefinedKeyboardLayoutSet; + } + + public static int getSubtypeNameIdFromKeyboardLayoutName(String keyboardLayoutName) { + return sKeyboardLayoutToNameIdsMap.get(keyboardLayoutName); } // Get InputMethodSubtype's display name in its locale. @@ -116,11 +152,7 @@ public class SubtypeLocale { public static String getKeyboardLayoutSetDisplayName(InputMethodSubtype subtype) { final String layoutName = getKeyboardLayoutSetName(subtype); - // TODO: This hack should be removed. - if (layoutName.equals(AdditionalSubtype.DVORAK)) { - return StringUtils.toTitleCase(layoutName, Locale.US); - } - return layoutName.toUpperCase(); + return sKeyboardKayoutToDisplayNameMap.get(layoutName); } public static String getKeyboardLayoutSetName(InputMethodSubtype subtype) { @@ -130,7 +162,7 @@ public class SubtypeLocale { if (keyboardLayoutSet == null) { android.util.Log.w(TAG, "KeyboardLayoutSet not found, use QWERTY: " + "locale=" + subtype.getLocale() + " extraValue=" + subtype.getExtraValue()); - return AdditionalSubtype.QWERTY; + return QWERTY; } return keyboardLayoutSet; } diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index 3b9a4069d..804287309 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -100,7 +100,7 @@ public class SubtypeSwitcher { mCurrentSystemLocale = mResources.getConfiguration().locale; mCurrentSubtype = mImm.getCurrentInputMethodSubtype(); mNoLanguageSubtype = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - service, SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.QWERTY); + service, SubtypeLocale.NO_LANGUAGE, SubtypeLocale.QWERTY); final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); mIsNetworkConnected = (info != null && info.isConnected()); diff --git a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java index b29477057..16b544169 100644 --- a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java +++ b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java @@ -116,17 +116,17 @@ public class SubtypeLocaleTests extends AndroidTestCase { public void testSampleSubtypes() { final Context context = getContext(); final InputMethodSubtype EN_US = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - context, Locale.US.toString(), AdditionalSubtype.QWERTY); + context, Locale.US.toString(), "qwerty"); final InputMethodSubtype EN_GB = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - context, Locale.UK.toString(), AdditionalSubtype.QWERTY); + context, Locale.UK.toString(), "qwerty"); final InputMethodSubtype FR = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - context, Locale.FRENCH.toString(), AdditionalSubtype.AZERTY); + context, Locale.FRENCH.toString(), "azerty"); final InputMethodSubtype FR_CA = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - context, Locale.CANADA_FRENCH.toString(), AdditionalSubtype.QWERTY); + context, Locale.CANADA_FRENCH.toString(), "qwerty"); final InputMethodSubtype DE = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - context, Locale.GERMAN.toString(), AdditionalSubtype.QWERTZ); + context, Locale.GERMAN.toString(), "qwertz"); final InputMethodSubtype ZZ = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( - context, SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.QWERTY); + context, SubtypeLocale.NO_LANGUAGE, "qwerty"); assertFalse(AdditionalSubtype.isAdditionalSubtype(EN_US)); assertFalse(AdditionalSubtype.isAdditionalSubtype(EN_GB)); @@ -166,13 +166,13 @@ public class SubtypeLocaleTests extends AndroidTestCase { public void testAdditionalSubtype() { final InputMethodSubtype DE_QWERTY = AdditionalSubtype.createAdditionalSubtype( - Locale.GERMAN.toString(), AdditionalSubtype.QWERTY, null); + Locale.GERMAN.toString(), "qwerty", null); final InputMethodSubtype FR_QWERTZ = AdditionalSubtype.createAdditionalSubtype( - Locale.FRENCH.toString(), AdditionalSubtype.QWERTZ, null); + Locale.FRENCH.toString(), "qwertz", null); final InputMethodSubtype US_AZERTY = AdditionalSubtype.createAdditionalSubtype( - Locale.US.toString(), AdditionalSubtype.AZERTY, null); + Locale.US.toString(), "azerty", null); final InputMethodSubtype ZZ_AZERTY = AdditionalSubtype.createAdditionalSubtype( - SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.AZERTY, null); + SubtypeLocale.NO_LANGUAGE, "azerty", null); assertTrue(AdditionalSubtype.isAdditionalSubtype(FR_QWERTZ)); assertTrue(AdditionalSubtype.isAdditionalSubtype(DE_QWERTY));