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
diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java
index ce576d0..6678082 100644
--- a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java
+++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java
@@ -29,7 +29,6 @@
 
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
-import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
@@ -123,10 +122,7 @@
     private Pair<Boolean, Boolean> hasDictionaryOrLayout(Locale locale) {
         if (locale == null) return new Pair<Boolean, Boolean>(false, false);
         final Resources res = getResources();
-        final Configuration conf = res.getConfiguration();
-        final Locale saveLocale = conf.locale;
-        conf.locale = locale;
-        res.updateConfiguration(conf, res.getDisplayMetrics());
+        final Locale saveLocale = Utils.setSystemLocale(res, locale);
         boolean hasDictionary = false;
         boolean hasLayout = false;
 
@@ -155,8 +151,7 @@
         } catch (XmlPullParserException e) {
         } catch (IOException e) {
         }
-        conf.locale = saveLocale;
-        res.updateConfiguration(conf, res.getDisplayMetrics());
+        Utils.setSystemLocale(res, saveLocale);
         return new Pair<Boolean, Boolean>(hasDictionary, hasLayout);
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index e163457..8627508 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -164,7 +164,8 @@
         final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id);
         LatinKeyboard keyboard = (ref == null) ? null : ref.get();
         if (keyboard == null) {
-            final Locale savedLocale =  mSubtypeSwitcher.changeSystemLocale(
+            final Resources res = mInputMethodService.getResources();
+            final Locale savedLocale = Utils.setSystemLocale(res,
                     mSubtypeSwitcher.getInputLocale());
 
             keyboard = new LatinKeyboard(mInputMethodService, id);
@@ -178,7 +179,7 @@
                 Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": "
                         + ((ref == null) ? "LOAD" : "GCed") + " id=" + id);
 
-            mSubtypeSwitcher.changeSystemLocale(savedLocale);
+            Utils.setSystemLocale(res, savedLocale);
         } else if (DEBUG) {
             Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT  id=" + id);
         }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 28fd6aa..175467f 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -470,14 +470,14 @@
         final String localeStr = mSubtypeSwitcher.getInputLocaleStr();
         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) {
             mSuggest.close();
         }
         final SharedPreferences prefs = mPrefs;
         mQuickFixes = isQuickFixesEnabled(prefs);
 
-        final Resources res = mResources;
         int mainDicResId = Utils.getMainDictionaryResourceId(res);
         mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
         loadAndSetAutoCorrectionThreshold(prefs);
@@ -499,7 +499,7 @@
         mWordSeparators = res.getString(R.string.word_separators);
         mSentenceSeparators = res.getString(R.string.sentence_separators);
 
-        mSubtypeSwitcher.changeSystemLocale(savedLocale);
+        Utils.setSystemLocale(res, savedLocale);
     }
 
     /* package private */ void resetSuggestMainDict() {
diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
index 1925e83..d801208 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
@@ -475,19 +475,6 @@
         }
     }
 
-    /**
-     * 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() {
         return KEYBOARD_MODE.equals(getCurrentSubtypeMode());
     }
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index b5afb50..9d7b984 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -23,6 +23,7 @@
 import com.android.inputmethod.keyboard.KeyboardId;
 
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.inputmethodservice.InputMethodService;
 import android.os.AsyncTask;
@@ -43,6 +44,7 @@
 import java.io.PrintWriter;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
 
 public class Utils {
     private static final String TAG = Utils.class.getSimpleName();
@@ -650,4 +652,12 @@
     public static int dipToPixel(float scale, int dip) {
         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;
+    }
 }