From ef35cb631c45c8b106fe7ed9e0d1178c3e5fb963 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 26 Aug 2011 20:22:47 +0900 Subject: [PATCH] Move locale-related utility methods to LocaleUtils. Change-Id: I7e9e6e5bc4486d8618d0213b112308c3d305c15e --- .../InputLanguageSelection.java | 5 +- .../languageswitcher/LanguageSwitcher.java | 4 +- .../keyboard/KeyboardSwitcher.java | 5 +- .../latin/BinaryDictionaryGetter.java | 4 +- .../inputmethod/latin/DictionaryFactory.java | 14 ++--- .../android/inputmethod/latin/LatinIME.java | 8 +-- .../inputmethod/latin/LocaleUtils.java | 51 +++++++++++++++++++ .../android/inputmethod/latin/Settings.java | 6 +-- .../inputmethod/latin/SubtypeSwitcher.java | 2 +- .../com/android/inputmethod/latin/Utils.java | 34 +------------ .../AndroidSpellCheckerService.java | 5 +- .../inputmethod/latin/SubtypeLocaleTests.java | 4 +- 12 files changed, 82 insertions(+), 60 deletions(-) diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java index 7eb5acda8..b6e0ec9cf 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java @@ -18,6 +18,7 @@ package com.android.inputmethod.deprecated.languageswitcher; import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.latin.DictionaryFactory; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SharedPreferencesCompat; @@ -155,7 +156,7 @@ public class InputLanguageSelection extends PreferenceActivity { private Pair hasDictionaryOrLayout(Locale locale) { if (locale == null) return new Pair(null, false); final Resources res = getResources(); - final Locale saveLocale = Utils.setSystemLocale(res, locale); + final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); final Long dictionaryId = DictionaryFactory.getDictionaryId(this, locale); boolean hasLayout = false; @@ -174,7 +175,7 @@ public class InputLanguageSelection extends PreferenceActivity { } catch (XmlPullParserException e) { } catch (IOException e) { } - Utils.setSystemLocale(res, saveLocale); + LocaleUtils.setSystemLocale(res, saveLocale); return new Pair(dictionaryId, hasLayout); } diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java index 1eedb5ee1..8070942d0 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java @@ -18,9 +18,9 @@ package com.android.inputmethod.deprecated.languageswitcher; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SharedPreferencesCompat; -import com.android.inputmethod.latin.Utils; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @@ -126,7 +126,7 @@ public class LanguageSwitcher { private void constructLocales() { mLocales.clear(); for (final String lang : mSelectedLanguageArray) { - final Locale locale = Utils.constructLocaleFromString(lang); + final Locale locale = LocaleUtils.constructLocaleFromString(lang); mLocales.add(locale); } } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index b1212f424..e43ae55a8 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -33,6 +33,7 @@ import com.android.inputmethod.keyboard.internal.ModifierKeyState; import com.android.inputmethod.keyboard.internal.ShiftKeyState; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SubtypeSwitcher; @@ -258,11 +259,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha final SoftReference ref = mKeyboardCache.get(id); LatinKeyboard keyboard = (ref == null) ? null : ref.get(); if (keyboard == null) { - final Locale savedLocale = Utils.setSystemLocale(mResources, id.mLocale); + final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, id.mLocale); try { keyboard = new LatinKeyboard.Builder(mThemeContext).load(id).build(); } finally { - Utils.setSystemLocale(mResources, savedLocale); + LocaleUtils.setSystemLocale(mResources, savedLocale); } mKeyboardCache.put(id, new SoftReference(keyboard)); diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 360c944d2..5546d48c0 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -157,9 +157,9 @@ class BinaryDictionaryGetter { private static AssetFileAddress loadFallbackResource(final Context context, final int fallbackResId, final Locale locale) { final Resources res = context.getResources(); - final Locale savedLocale = Utils.setSystemLocale(res, locale); + final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale); final AssetFileDescriptor afd = res.openRawResourceFd(fallbackResId); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); if (afd == null) { Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index 9642151d7..dfaad26bc 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -83,9 +83,9 @@ public class DictionaryFactory { try { final Resources res = context.getResources(); if (null != locale) { - final Locale savedLocale = Utils.setSystemLocale(res, locale); + final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale); afd = res.openRawResourceFd(resId); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); } else { afd = res.openRawResourceFd(resId); } @@ -145,7 +145,7 @@ public class DictionaryFactory { */ public static boolean isDictionaryAvailable(Context context, Locale locale) { final Resources res = context.getResources(); - final Locale saveLocale = Utils.setSystemLocale(res, locale); + final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); final int resourceId = Utils.getMainDictionaryResourceId(res); final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); @@ -156,14 +156,14 @@ public class DictionaryFactory { /* Um, what can we do here exactly? */ } - Utils.setSystemLocale(res, saveLocale); + LocaleUtils.setSystemLocale(res, saveLocale); return hasDictionary; } // TODO: Do not use the size of the dictionary as an unique dictionary ID. - public static Long getDictionaryId(Context context, Locale locale) { + public static Long getDictionaryId(final Context context, final Locale locale) { final Resources res = context.getResources(); - final Locale saveLocale = Utils.setSystemLocale(res, locale); + final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale); final int resourceId = Utils.getMainDictionaryResourceId(res); final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); @@ -175,7 +175,7 @@ public class DictionaryFactory { } catch (java.io.IOException e) { } - Utils.setSystemLocale(res, saveLocale); + LocaleUtils.setSystemLocale(res, saveLocale); return size; } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 552517bc8..229bf0f4c 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -479,10 +479,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private void initSuggest() { final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); - final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); + final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); final Resources res = mResources; - final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale); + final Locale savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale); final ContactsDictionary oldContactsDictionary; if (mSuggest != null) { oldContactsDictionary = mSuggest.getContactsDictionary(); @@ -514,7 +514,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar updateCorrectionMode(); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); } /** @@ -551,7 +551,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar /* package private */ void resetSuggestMainDict() { final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); - final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); + final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); int mainDicResId = Utils.getMainDictionaryResourceId(mResources); mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); } diff --git a/java/src/com/android/inputmethod/latin/LocaleUtils.java b/java/src/com/android/inputmethod/latin/LocaleUtils.java index 054f1f9b8..efa9bfee3 100644 --- a/java/src/com/android/inputmethod/latin/LocaleUtils.java +++ b/java/src/com/android/inputmethod/latin/LocaleUtils.java @@ -16,8 +16,13 @@ package com.android.inputmethod.latin; +import android.content.res.Configuration; +import android.content.res.Resources; import android.text.TextUtils; +import java.util.HashMap; +import java.util.Locale; + /** * A class to help with handling Locales in string form. * @@ -30,6 +35,10 @@ public class LocaleUtils { private final static String TAG = LocaleUtils.class.getSimpleName(); + private LocaleUtils() { + // Intentional empty constructor for utility class. + } + // Locale match level constants. // A higher level of match is guaranteed to have a higher numerical value. // Some room is left within constants to add match cases that may arise necessary @@ -154,4 +163,46 @@ public class LocaleUtils { public static boolean isMatch(int level) { return LOCALE_MATCH <= level; } + + /** + * Sets the system locale for this process. + * + * @param res the resources to use. Pass current resources. + * @param newLocale the locale to change to. + * @return the old locale. + */ + public static Locale setSystemLocale(final Resources res, final Locale newLocale) { + final Configuration conf = res.getConfiguration(); + final Locale saveLocale = conf.locale; + conf.locale = newLocale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + return saveLocale; + } + + private static final HashMap sLocaleCache = new HashMap(); + + /** + * Creates a locale from a string specification. + */ + public static Locale constructLocaleFromString(final String localeStr) { + if (localeStr == null) + return null; + synchronized (sLocaleCache) { + if (sLocaleCache.containsKey(localeStr)) + return sLocaleCache.get(localeStr); + Locale retval = null; + String[] localeParams = localeStr.split("_", 3); + if (localeParams.length == 1) { + retval = new Locale(localeParams[0]); + } else if (localeParams.length == 2) { + retval = new Locale(localeParams[0], localeParams[1]); + } else if (localeParams.length == 3) { + retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); + } + if (retval != null) { + sLocaleCache.put(localeStr, retval); + } + return retval; + } + } } diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 87a713f5c..a5eed9015 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -128,8 +128,8 @@ public class Settings extends InputMethodSettingsActivity final Resources res = context.getResources(); final Locale savedLocale; if (null != localeStr) { - final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); - savedLocale = Utils.setSystemLocale(res, keyboardLocale); + final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr); + savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale); } else { savedLocale = null; } @@ -191,7 +191,7 @@ public class Settings extends InputMethodSettingsActivity mVoiceKeyEnabled = voiceMode != null && !voiceMode.equals(voiceModeOff); mVoiceKeyOnMain = voiceMode != null && voiceMode.equals(voiceModeMain); - Utils.setSystemLocale(res, savedLocale); + LocaleUtils.setSystemLocale(res, savedLocale); } public boolean isSuggestedPunctuation(int code) { diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index d969e39eb..87d854940 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -267,7 +267,7 @@ public class SubtypeSwitcher { // "en" --> language: en // "" --> the system locale if (!TextUtils.isEmpty(inputLocaleStr)) { - mInputLocale = Utils.constructLocaleFromString(inputLocaleStr); + mInputLocale = LocaleUtils.constructLocaleFromString(inputLocaleStr); mInputLocaleStr = inputLocaleStr; } else { mInputLocale = mSystemLocale; diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index ff051dcbb..60a4cfb38 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -705,38 +705,6 @@ public class Utils { return (int) (dip * scale + 0.5); } - public static Locale setSystemLocale(Resources res, Locale newLocale) { - final Configuration conf = res.getConfiguration(); - final Locale saveLocale = conf.locale; - conf.locale = newLocale; - res.updateConfiguration(conf, res.getDisplayMetrics()); - return saveLocale; - } - - private static final HashMap sLocaleCache = new HashMap(); - - public static Locale constructLocaleFromString(String localeStr) { - if (localeStr == null) - return null; - synchronized (sLocaleCache) { - if (sLocaleCache.containsKey(localeStr)) - return sLocaleCache.get(localeStr); - Locale retval = null; - String[] localeParams = localeStr.split("_", 3); - if (localeParams.length == 1) { - retval = new Locale(localeParams[0]); - } else if (localeParams.length == 2) { - retval = new Locale(localeParams[0], localeParams[1]); - } else if (localeParams.length == 3) { - retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); - } - if (retval != null) { - sLocaleCache.put(localeStr, retval); - } - return retval; - } - } - /** * Remove duplicates from an array of strings. * @@ -783,7 +751,7 @@ public class Utils { } public static String getMiddleDisplayLanguage(Locale locale) { - return toTitleCase((constructLocaleFromString( + return toTitleCase((LocaleUtils.constructLocaleFromString( locale.getLanguage()).getDisplayLanguage(locale)), locale); } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 502ebb52a..3244bcc65 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -33,6 +33,7 @@ import com.android.inputmethod.latin.Dictionary.DataType; import com.android.inputmethod.latin.Dictionary.WordCallback; import com.android.inputmethod.latin.DictionaryCollection; import com.android.inputmethod.latin.DictionaryFactory; +import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.UserDictionary; import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.WordComposer; @@ -139,7 +140,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private DictionaryPool getDictionaryPool(final String locale) { DictionaryPool pool = mDictionaryPools.get(locale); if (null == pool) { - final Locale localeObject = Utils.constructLocaleFromString(locale); + final Locale localeObject = LocaleUtils.constructLocaleFromString(locale); pool = new DictionaryPool(POOL_SIZE, this, localeObject); mDictionaryPools.put(locale, pool); } @@ -172,7 +173,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService { public void onCreate() { final String localeString = getLocale(); mDictionaryPool = getDictionaryPool(localeString); - mLocale = Utils.constructLocaleFromString(localeString); + mLocale = LocaleUtils.constructLocaleFromString(localeString); } // Note : this must be reentrant diff --git a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java index d102aa4d1..fec3e8ee1 100644 --- a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java +++ b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java @@ -16,7 +16,7 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.latin.Utils; +import com.android.inputmethod.latin.LocaleUtils; import android.content.Context; import android.content.res.Resources; @@ -77,7 +77,7 @@ public class SubtypeLocaleTests extends AndroidTestCase { int failedCount = 0; for (final InputMethodSubtype subtype : mKeyboardSubtypes) { final String localeCode = subtype.getLocale(); - final Locale locale = Utils.constructLocaleFromString(localeCode); + final Locale locale = LocaleUtils.constructLocaleFromString(localeCode); // The locale name which will be displayed on spacebar. For example 'English (US)' or // 'Francais (Canada)'. (c=\u008d) final String displayName = SubtypeLocale.getFullDisplayName(locale);