Add a method to set the locale to Utils.

Add a setLocale method to Utils that returns the previous locale.
Also unify all calls through the code.

Change-Id: Ic850dc5df19fba00ed3601835652859b4321b544
This commit is contained in:
Jean Chalard 2011-04-27 15:43:03 +09:00
parent 663d4f5cc1
commit 309bff562f
5 changed files with 18 additions and 25 deletions

View file

@ -29,7 +29,6 @@ import org.xmlpull.v1.XmlPullParserException;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
@ -123,10 +122,7 @@ public class InputLanguageSelection extends PreferenceActivity {
private Pair<Boolean, Boolean> hasDictionaryOrLayout(Locale locale) { private Pair<Boolean, Boolean> hasDictionaryOrLayout(Locale locale) {
if (locale == null) return new Pair<Boolean, Boolean>(false, false); if (locale == null) return new Pair<Boolean, Boolean>(false, false);
final Resources res = getResources(); final Resources res = getResources();
final Configuration conf = res.getConfiguration(); final Locale saveLocale = Utils.setSystemLocale(res, locale);
final Locale saveLocale = conf.locale;
conf.locale = locale;
res.updateConfiguration(conf, res.getDisplayMetrics());
boolean hasDictionary = false; boolean hasDictionary = false;
boolean hasLayout = false; boolean hasLayout = false;
@ -155,8 +151,7 @@ public class InputLanguageSelection extends PreferenceActivity {
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
} catch (IOException e) { } catch (IOException e) {
} }
conf.locale = saveLocale; Utils.setSystemLocale(res, saveLocale);
res.updateConfiguration(conf, res.getDisplayMetrics());
return new Pair<Boolean, Boolean>(hasDictionary, hasLayout); return new Pair<Boolean, Boolean>(hasDictionary, hasLayout);
} }

View file

@ -164,7 +164,8 @@ 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 = mSubtypeSwitcher.changeSystemLocale( final Resources res = mInputMethodService.getResources();
final Locale savedLocale = Utils.setSystemLocale(res,
mSubtypeSwitcher.getInputLocale()); mSubtypeSwitcher.getInputLocale());
keyboard = new LatinKeyboard(mInputMethodService, id); keyboard = new LatinKeyboard(mInputMethodService, id);
@ -178,7 +179,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": " Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": "
+ ((ref == null) ? "LOAD" : "GCed") + " id=" + id); + ((ref == null) ? "LOAD" : "GCed") + " id=" + id);
mSubtypeSwitcher.changeSystemLocale(savedLocale); Utils.setSystemLocale(res, savedLocale);
} else if (DEBUG) { } else if (DEBUG) {
Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id); Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id);
} }

View file

@ -470,14 +470,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); final String localeStr = mSubtypeSwitcher.getInputLocaleStr();
final Locale keyboardLocale = new Locale(localeStr); final Locale keyboardLocale = new Locale(localeStr);
final Locale savedLocale = mSubtypeSwitcher.changeSystemLocale(keyboardLocale); final Resources res = mResources;
final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale);
if (mSuggest != null) { if (mSuggest != null) {
mSuggest.close(); mSuggest.close();
} }
final SharedPreferences prefs = mPrefs; final SharedPreferences prefs = mPrefs;
mQuickFixes = isQuickFixesEnabled(prefs); mQuickFixes = isQuickFixesEnabled(prefs);
final Resources res = mResources;
int mainDicResId = Utils.getMainDictionaryResourceId(res); int mainDicResId = Utils.getMainDictionaryResourceId(res);
mSuggest = new Suggest(this, mainDicResId, keyboardLocale); mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
loadAndSetAutoCorrectionThreshold(prefs); loadAndSetAutoCorrectionThreshold(prefs);
@ -499,7 +499,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mWordSeparators = res.getString(R.string.word_separators); mWordSeparators = res.getString(R.string.word_separators);
mSentenceSeparators = res.getString(R.string.sentence_separators); mSentenceSeparators = res.getString(R.string.sentence_separators);
mSubtypeSwitcher.changeSystemLocale(savedLocale); Utils.setSystemLocale(res, savedLocale);
} }
/* package private */ void resetSuggestMainDict() { /* package private */ void resetSuggestMainDict() {

View file

@ -475,19 +475,6 @@ public class SubtypeSwitcher {
} }
} }
/**
* Change system locale for this application
* @param newLocale
* @return oldLocale
*/
public Locale changeSystemLocale(Locale newLocale) {
Configuration conf = mResources.getConfiguration();
Locale oldLocale = conf.locale;
conf.locale = newLocale;
mResources.updateConfiguration(conf, mResources.getDisplayMetrics());
return oldLocale;
}
public boolean isKeyboardMode() { public boolean isKeyboardMode() {
return KEYBOARD_MODE.equals(getCurrentSubtypeMode()); return KEYBOARD_MODE.equals(getCurrentSubtypeMode());
} }

View file

@ -23,6 +23,7 @@ import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.keyboard.KeyboardId;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -43,6 +44,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale;
public class Utils { public class Utils {
private static final String TAG = Utils.class.getSimpleName(); private static final String TAG = Utils.class.getSimpleName();
@ -650,4 +652,12 @@ public class Utils {
public static int dipToPixel(float scale, int dip) { public static int dipToPixel(float scale, int dip) {
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;
}
} }