Merge "Move locale-related utility methods to LocaleUtils."

This commit is contained in:
Jean Chalard 2011-08-28 20:34:02 -07:00 committed by Android (Google) Code Review
commit 940514989a
12 changed files with 82 additions and 60 deletions

View file

@ -18,6 +18,7 @@ package com.android.inputmethod.deprecated.languageswitcher;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.latin.DictionaryFactory; import com.android.inputmethod.latin.DictionaryFactory;
import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.Settings;
import com.android.inputmethod.latin.SharedPreferencesCompat; import com.android.inputmethod.latin.SharedPreferencesCompat;
@ -155,7 +156,7 @@ public class InputLanguageSelection extends PreferenceActivity {
private Pair<Long, Boolean> hasDictionaryOrLayout(Locale locale) { private Pair<Long, Boolean> hasDictionaryOrLayout(Locale locale) {
if (locale == null) return new Pair<Long, Boolean>(null, false); if (locale == null) return new Pair<Long, Boolean>(null, false);
final Resources res = getResources(); 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); final Long dictionaryId = DictionaryFactory.getDictionaryId(this, locale);
boolean hasLayout = false; boolean hasLayout = false;
@ -174,7 +175,7 @@ public class InputLanguageSelection extends PreferenceActivity {
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
} catch (IOException e) { } catch (IOException e) {
} }
Utils.setSystemLocale(res, saveLocale); LocaleUtils.setSystemLocale(res, saveLocale);
return new Pair<Long, Boolean>(dictionaryId, hasLayout); return new Pair<Long, Boolean>(dictionaryId, hasLayout);
} }

View file

@ -18,9 +18,9 @@ package com.android.inputmethod.deprecated.languageswitcher;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.Settings;
import com.android.inputmethod.latin.SharedPreferencesCompat; import com.android.inputmethod.latin.SharedPreferencesCompat;
import com.android.inputmethod.latin.Utils;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
@ -126,7 +126,7 @@ public class LanguageSwitcher {
private void constructLocales() { private void constructLocales() {
mLocales.clear(); mLocales.clear();
for (final String lang : mSelectedLanguageArray) { for (final String lang : mSelectedLanguageArray) {
final Locale locale = Utils.constructLocaleFromString(lang); final Locale locale = LocaleUtils.constructLocaleFromString(lang);
mLocales.add(locale); mLocales.add(locale);
} }
} }

View file

@ -33,6 +33,7 @@ import com.android.inputmethod.keyboard.internal.ModifierKeyState;
import com.android.inputmethod.keyboard.internal.ShiftKeyState; import com.android.inputmethod.keyboard.internal.ShiftKeyState;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.Settings;
import com.android.inputmethod.latin.SubtypeSwitcher; import com.android.inputmethod.latin.SubtypeSwitcher;
@ -258,11 +259,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id); final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id);
LatinKeyboard keyboard = (ref == null) ? null : ref.get(); LatinKeyboard keyboard = (ref == null) ? null : ref.get();
if (keyboard == null) { if (keyboard == null) {
final Locale savedLocale = Utils.setSystemLocale(mResources, id.mLocale); final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, id.mLocale);
try { try {
keyboard = new LatinKeyboard.Builder(mThemeContext).load(id).build(); keyboard = new LatinKeyboard.Builder(mThemeContext).load(id).build();
} finally { } finally {
Utils.setSystemLocale(mResources, savedLocale); LocaleUtils.setSystemLocale(mResources, savedLocale);
} }
mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard)); mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard));

View file

@ -157,9 +157,9 @@ class BinaryDictionaryGetter {
private static AssetFileAddress loadFallbackResource(final Context context, private static AssetFileAddress loadFallbackResource(final Context context,
final int fallbackResId, final Locale locale) { final int fallbackResId, final Locale locale) {
final Resources res = context.getResources(); 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); final AssetFileDescriptor afd = res.openRawResourceFd(fallbackResId);
Utils.setSystemLocale(res, savedLocale); LocaleUtils.setSystemLocale(res, savedLocale);
if (afd == null) { if (afd == null) {
Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId="

View file

@ -83,9 +83,9 @@ public class DictionaryFactory {
try { try {
final Resources res = context.getResources(); final Resources res = context.getResources();
if (null != locale) { if (null != locale) {
final Locale savedLocale = Utils.setSystemLocale(res, locale); final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale);
afd = res.openRawResourceFd(resId); afd = res.openRawResourceFd(resId);
Utils.setSystemLocale(res, savedLocale); LocaleUtils.setSystemLocale(res, savedLocale);
} else { } else {
afd = res.openRawResourceFd(resId); afd = res.openRawResourceFd(resId);
} }
@ -145,7 +145,7 @@ public class DictionaryFactory {
*/ */
public static boolean isDictionaryAvailable(Context context, Locale locale) { public static boolean isDictionaryAvailable(Context context, Locale locale) {
final Resources res = context.getResources(); 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 int resourceId = Utils.getMainDictionaryResourceId(res);
final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
@ -156,14 +156,14 @@ public class DictionaryFactory {
/* Um, what can we do here exactly? */ /* Um, what can we do here exactly? */
} }
Utils.setSystemLocale(res, saveLocale); LocaleUtils.setSystemLocale(res, saveLocale);
return hasDictionary; return hasDictionary;
} }
// TODO: Do not use the size of the dictionary as an unique dictionary ID. // 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 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 int resourceId = Utils.getMainDictionaryResourceId(res);
final AssetFileDescriptor afd = res.openRawResourceFd(resourceId); final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
@ -175,7 +175,7 @@ public class DictionaryFactory {
} catch (java.io.IOException e) { } catch (java.io.IOException e) {
} }
Utils.setSystemLocale(res, saveLocale); LocaleUtils.setSystemLocale(res, saveLocale);
return size; return size;
} }

View file

@ -479,10 +479,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private void initSuggest() { private void initSuggest() {
final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); final String localeStr = mSubtypeSwitcher.getInputLocaleStr();
final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr);
final Resources res = mResources; final Resources res = mResources;
final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale); final Locale savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale);
final ContactsDictionary oldContactsDictionary; final ContactsDictionary oldContactsDictionary;
if (mSuggest != null) { if (mSuggest != null) {
oldContactsDictionary = mSuggest.getContactsDictionary(); oldContactsDictionary = mSuggest.getContactsDictionary();
@ -514,7 +514,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
updateCorrectionMode(); updateCorrectionMode();
Utils.setSystemLocale(res, savedLocale); LocaleUtils.setSystemLocale(res, savedLocale);
} }
/** /**
@ -551,7 +551,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
/* package private */ void resetSuggestMainDict() { /* package private */ void resetSuggestMainDict() {
final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); final String localeStr = mSubtypeSwitcher.getInputLocaleStr();
final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr);
int mainDicResId = Utils.getMainDictionaryResourceId(mResources); int mainDicResId = Utils.getMainDictionaryResourceId(mResources);
mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); mSuggest.resetMainDict(this, mainDicResId, keyboardLocale);
} }

View file

@ -16,8 +16,13 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils; import android.text.TextUtils;
import java.util.HashMap;
import java.util.Locale;
/** /**
* A class to help with handling Locales in string form. * 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 final static String TAG = LocaleUtils.class.getSimpleName();
private LocaleUtils() {
// Intentional empty constructor for utility class.
}
// Locale match level constants. // Locale match level constants.
// A higher level of match is guaranteed to have a higher numerical value. // 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 // 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) { public static boolean isMatch(int level) {
return LOCALE_MATCH <= 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<String, Locale> sLocaleCache = new HashMap<String, Locale>();
/**
* 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;
}
}
} }

View file

@ -128,8 +128,8 @@ public class Settings extends InputMethodSettingsActivity
final Resources res = context.getResources(); final Resources res = context.getResources();
final Locale savedLocale; final Locale savedLocale;
if (null != localeStr) { if (null != localeStr) {
final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); final Locale keyboardLocale = LocaleUtils.constructLocaleFromString(localeStr);
savedLocale = Utils.setSystemLocale(res, keyboardLocale); savedLocale = LocaleUtils.setSystemLocale(res, keyboardLocale);
} else { } else {
savedLocale = null; savedLocale = null;
} }
@ -191,7 +191,7 @@ public class Settings extends InputMethodSettingsActivity
mVoiceKeyEnabled = voiceMode != null && !voiceMode.equals(voiceModeOff); mVoiceKeyEnabled = voiceMode != null && !voiceMode.equals(voiceModeOff);
mVoiceKeyOnMain = voiceMode != null && voiceMode.equals(voiceModeMain); mVoiceKeyOnMain = voiceMode != null && voiceMode.equals(voiceModeMain);
Utils.setSystemLocale(res, savedLocale); LocaleUtils.setSystemLocale(res, savedLocale);
} }
public boolean isSuggestedPunctuation(int code) { public boolean isSuggestedPunctuation(int code) {

View file

@ -267,7 +267,7 @@ public class SubtypeSwitcher {
// "en" --> language: en // "en" --> language: en
// "" --> the system locale // "" --> the system locale
if (!TextUtils.isEmpty(inputLocaleStr)) { if (!TextUtils.isEmpty(inputLocaleStr)) {
mInputLocale = Utils.constructLocaleFromString(inputLocaleStr); mInputLocale = LocaleUtils.constructLocaleFromString(inputLocaleStr);
mInputLocaleStr = inputLocaleStr; mInputLocaleStr = inputLocaleStr;
} else { } else {
mInputLocale = mSystemLocale; mInputLocale = mSystemLocale;

View file

@ -705,38 +705,6 @@ public class Utils {
return (int) (dip * scale + 0.5); 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<String, Locale> sLocaleCache = new HashMap<String, Locale>();
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. * Remove duplicates from an array of strings.
* *
@ -783,7 +751,7 @@ public class Utils {
} }
public static String getMiddleDisplayLanguage(Locale locale) { public static String getMiddleDisplayLanguage(Locale locale) {
return toTitleCase((constructLocaleFromString( return toTitleCase((LocaleUtils.constructLocaleFromString(
locale.getLanguage()).getDisplayLanguage(locale)), locale); locale.getLanguage()).getDisplayLanguage(locale)), locale);
} }

View file

@ -33,6 +33,7 @@ import com.android.inputmethod.latin.Dictionary.DataType;
import com.android.inputmethod.latin.Dictionary.WordCallback; import com.android.inputmethod.latin.Dictionary.WordCallback;
import com.android.inputmethod.latin.DictionaryCollection; import com.android.inputmethod.latin.DictionaryCollection;
import com.android.inputmethod.latin.DictionaryFactory; import com.android.inputmethod.latin.DictionaryFactory;
import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.UserDictionary; import com.android.inputmethod.latin.UserDictionary;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.WordComposer;
@ -139,7 +140,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
private DictionaryPool getDictionaryPool(final String locale) { private DictionaryPool getDictionaryPool(final String locale) {
DictionaryPool pool = mDictionaryPools.get(locale); DictionaryPool pool = mDictionaryPools.get(locale);
if (null == pool) { if (null == pool) {
final Locale localeObject = Utils.constructLocaleFromString(locale); final Locale localeObject = LocaleUtils.constructLocaleFromString(locale);
pool = new DictionaryPool(POOL_SIZE, this, localeObject); pool = new DictionaryPool(POOL_SIZE, this, localeObject);
mDictionaryPools.put(locale, pool); mDictionaryPools.put(locale, pool);
} }
@ -172,7 +173,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
public void onCreate() { public void onCreate() {
final String localeString = getLocale(); final String localeString = getLocale();
mDictionaryPool = getDictionaryPool(localeString); mDictionaryPool = getDictionaryPool(localeString);
mLocale = Utils.constructLocaleFromString(localeString); mLocale = LocaleUtils.constructLocaleFromString(localeString);
} }
// Note : this must be reentrant // Note : this must be reentrant

View file

@ -16,7 +16,7 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.LocaleUtils;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
@ -77,7 +77,7 @@ public class SubtypeLocaleTests extends AndroidTestCase {
int failedCount = 0; int failedCount = 0;
for (final InputMethodSubtype subtype : mKeyboardSubtypes) { for (final InputMethodSubtype subtype : mKeyboardSubtypes) {
final String localeCode = subtype.getLocale(); 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 // The locale name which will be displayed on spacebar. For example 'English (US)' or
// 'Francais (Canada)'. (c=\u008d) // 'Francais (Canada)'. (c=\u008d)
final String displayName = SubtypeLocale.getFullDisplayName(locale); final String displayName = SubtypeLocale.getFullDisplayName(locale);