Merge "Add a method to set the locale to Utils."

main
Jean Chalard 2011-04-27 00:42:24 -07:00 committed by Android (Google) Code Review
commit fd78781ced
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

@ -169,7 +169,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);
@ -183,7 +184,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();
@ -652,4 +654,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;
}
} }