Move locale-related utility methods to LocaleUtils.

Change-Id: I7e9e6e5bc4486d8618d0213b112308c3d305c15e
main
Jean Chalard 2011-08-26 20:22:47 +09:00
parent 0ada663f3d
commit ef35cb631c
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.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<Long, Boolean> hasDictionaryOrLayout(Locale locale) {
if (locale == null) return new Pair<Long, Boolean>(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<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.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);
}
}

View File

@ -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<LatinKeyboard> 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<LatinKeyboard>(keyboard));

View File

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

View File

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

View File

@ -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);
}

View File

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

View File

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

View File

@ -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<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.
*
@ -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);
}

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

View File

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