From 27d13713bbb291d25a910f97d88a81fdbabddc0e Mon Sep 17 00:00:00 2001
From: Ken Wakasa <kwakasa@google.com>
Date: Fri, 26 Nov 2010 13:08:36 +0900
Subject: [PATCH] Preload SharedPreferences at start.

bug: 3207554
Change-Id: I84072461aee9675cc67897de03f89e9c0036bc1f
---
 .../latin/InputLanguageSelection.java         |  8 ++--
 .../inputmethod/latin/KeyboardSwitcher.java   |  9 ++--
 .../inputmethod/latin/LanguageSwitcher.java   |  5 +--
 .../android/inputmethod/latin/LatinIME.java   | 42 ++++++++++---------
 .../inputmethod/latin/LatinImeLogger.java     |  2 +-
 .../inputmethod/latin/SubtypeSwitcher.java    | 16 +++----
 .../inputmethod/{latin => voice}/Hints.java   | 36 ++++++++--------
 .../inputmethod/voice/VoiceIMEConnector.java  |  9 ++--
 8 files changed, 64 insertions(+), 63 deletions(-)
 rename java/src/com/android/inputmethod/{latin => voice}/Hints.java (84%)

diff --git a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
index 7f64bcb433..2c9a162f72 100644
--- a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
+++ b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
@@ -34,6 +34,7 @@ import android.text.TextUtils;
 
 public class InputLanguageSelection extends PreferenceActivity {
 
+    private SharedPreferences mPrefs;
     private String mSelectedLanguages;
     private ArrayList<Loc> mAvailableLanguages = new ArrayList<Loc>();
     private static final String[] BLACKLIST_LANGUAGES = {
@@ -66,8 +67,8 @@ public class InputLanguageSelection extends PreferenceActivity {
         super.onCreate(icicle);
         addPreferencesFromResource(R.xml.language_prefs);
         // Get the settings preferences
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
-        mSelectedLanguages = sp.getString(LatinIME.PREF_SELECTED_LANGUAGES, "");
+        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
+        mSelectedLanguages = mPrefs.getString(LatinIME.PREF_SELECTED_LANGUAGES, "");
         String[] languageList = mSelectedLanguages.split(",");
         mAvailableLanguages = getUniqueLocales();
         PreferenceGroup parent = getPreferenceScreen();
@@ -140,8 +141,7 @@ public class InputLanguageSelection extends PreferenceActivity {
             }
         }
         if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
-        Editor editor = sp.edit();
+        Editor editor = mPrefs.edit();
         editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, checkedLanguages);
         SharedPreferencesCompat.apply(editor);
     }
diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
index b08b97a810..3f77153298 100644
--- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
@@ -60,6 +60,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
     private static final int SYMBOLS_MODE_STATE_SYMBOL = 2;
 
     private SubtypeSwitcher mSubtypeSwitcher;
+    private SharedPreferences mPrefs;
 
     private LatinKeyboardView mInputView;
     private LatinIME mInputMethodService;
@@ -105,11 +106,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
     private KeyboardSwitcher() {
     }
 
-    public static void init(LatinIME ims) {
+    public static void init(LatinIME ims, SharedPreferences prefs) {
         sInstance.mInputMethodService = ims;
+        sInstance.mPrefs = prefs;
         sInstance.mSubtypeSwitcher = SubtypeSwitcher.getInstance();
 
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ims);
         sInstance.mLayoutId = Integer.valueOf(
                 prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID));
         prefs.registerOnSharedPreferenceChangeListener(sInstance);
@@ -282,9 +283,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
         mVoiceButtonOnPrimary = voiceButtonOnPrimary;
         mIsSymbols = isSymbols;
         // Update the settings key state because number of enabled IMEs could have been changed
-        mHasSettingsKey = getSettingsKeyMode(
-                PreferenceManager.getDefaultSharedPreferences(mInputMethodService),
-                mInputMethodService);
+        mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService);
         makeSymbolsKeyboardIds();
 
         KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols);
diff --git a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java
index c2805f506b..05d5164c4d 100644
--- a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java
@@ -186,9 +186,8 @@ public class LanguageSwitcher {
         mCurrentIndex = prevLocaleIndex();
     }
 
-    public void persist() {
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mIme);
-        Editor editor = sp.edit();
+    public void persist(SharedPreferences prefs) {
+        Editor editor = prefs.edit();
         editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage());
         SharedPreferencesCompat.apply(editor);
     }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 8a0d1461af..3e12bf017e 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -145,6 +145,7 @@ public class LatinIME extends InputMethodService
     private AutoDictionary mAutoDictionary;
 
     private Resources mResources;
+    private SharedPreferences mPrefs;
 
     private final StringBuilder mComposing = new StringBuilder();
     private WordComposer mWord = new WordComposer();
@@ -332,15 +333,16 @@ public class LatinIME extends InputMethodService
 
     @Override
     public void onCreate() {
-        LatinImeLogger.init(this);
-        SubtypeSwitcher.init(this);
-        KeyboardSwitcher.init(this);
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+        mPrefs = prefs;
+        LatinImeLogger.init(this, prefs);
+        SubtypeSwitcher.init(this, prefs);
+        KeyboardSwitcher.init(this, prefs);
         super.onCreate();
         //setStatusIcon(R.drawable.ime_qwerty);
         mResources = getResources();
         mImm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
         final Configuration conf = mResources.getConfiguration();
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
         mKeyboardSwitcher = KeyboardSwitcher.getInstance();
         mReCorrectionEnabled = prefs.getBoolean(PREF_RECORRECTION_ENABLED,
@@ -363,7 +365,7 @@ public class LatinIME extends InputMethodService
         // register to receive ringer mode changes for silent mode
         IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
         registerReceiver(mReceiver, filter);
-        mVoiceConnector = VoiceIMEConnector.init(this, mHandler);
+        mVoiceConnector = VoiceIMEConnector.init(this, prefs, mHandler);
         prefs.registerOnSharedPreferenceChangeListener(this);
     }
 
@@ -415,12 +417,12 @@ public class LatinIME extends InputMethodService
         if (mSuggest != null) {
             mSuggest.close();
         }
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
-        mQuickFixes = sp.getBoolean(PREF_QUICK_FIXES, true);
+        final SharedPreferences prefs = mPrefs;
+        mQuickFixes = prefs.getBoolean(PREF_QUICK_FIXES, true);
 
         int[] dictionaries = getDictionary(orig);
         mSuggest = new Suggest(this, dictionaries);
-        loadAndSetAutoCompletionThreshold(sp);
+        loadAndSetAutoCompletionThreshold(prefs);
         if (mUserDictionary != null) mUserDictionary.close();
         mUserDictionary = new UserDictionary(this, locale);
         if (mContactsDictionary == null) {
@@ -2070,26 +2072,26 @@ public class LatinIME extends InputMethodService
 
     private void loadSettings(EditorInfo attribute) {
         // Get the settings preferences
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
+        final SharedPreferences prefs = mPrefs;
         Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
         mVibrateOn = vibrator != null && vibrator.hasVibrator()
-                && sp.getBoolean(LatinIMESettings.PREF_VIBRATE_ON, false);
-        mSoundOn = sp.getBoolean(PREF_SOUND_ON, false);
-        mPopupOn = sp.getBoolean(PREF_POPUP_ON,
+                && prefs.getBoolean(LatinIMESettings.PREF_VIBRATE_ON, false);
+        mSoundOn = prefs.getBoolean(PREF_SOUND_ON, false);
+        mPopupOn = prefs.getBoolean(PREF_POPUP_ON,
                 mResources.getBoolean(R.bool.default_popup_preview));
-        mAutoCap = sp.getBoolean(PREF_AUTO_CAP, true);
-        mQuickFixes = sp.getBoolean(PREF_QUICK_FIXES, true);
+        mAutoCap = prefs.getBoolean(PREF_AUTO_CAP, true);
+        mQuickFixes = prefs.getBoolean(PREF_QUICK_FIXES, true);
 
-        mAutoCorrectEnabled = isAutoCorrectEnabled(sp);
-        mBigramSuggestionEnabled = mAutoCorrectEnabled && isBigramSuggestionEnabled(sp);
-        loadAndSetAutoCompletionThreshold(sp);
+        mAutoCorrectEnabled = isAutoCorrectEnabled(prefs);
+        mBigramSuggestionEnabled = mAutoCorrectEnabled && isBigramSuggestionEnabled(prefs);
+        loadAndSetAutoCompletionThreshold(prefs);
 
-        mVoiceConnector.loadSettings(attribute, sp);
+        mVoiceConnector.loadSettings(attribute, prefs);
 
         updateCorrectionMode();
         updateAutoTextEnabled();
-        updateSuggestionVisibility(sp);
-        SubtypeSwitcher.getInstance().loadSettings(sp);
+        updateSuggestionVisibility(prefs);
+        SubtypeSwitcher.getInstance().loadSettings();
     }
 
     /**
diff --git a/java/src/com/android/inputmethod/latin/LatinImeLogger.java b/java/src/com/android/inputmethod/latin/LatinImeLogger.java
index a3e368f2d8..bb74a7a6f7 100644
--- a/java/src/com/android/inputmethod/latin/LatinImeLogger.java
+++ b/java/src/com/android/inputmethod/latin/LatinImeLogger.java
@@ -29,7 +29,7 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
     }
 
-    public static void init(Context context) {
+    public static void init(Context context, SharedPreferences prefs) {
     }
 
     public static void commit() {
diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
index 7cf055672d..bb9c0eb90b 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
@@ -50,6 +50,7 @@ public class SubtypeSwitcher {
 
     private static final SubtypeSwitcher sInstance = new SubtypeSwitcher();
     private /* final */ LatinIME mService;
+    private /* final */ SharedPreferences mPrefs;
     private /* final */ InputMethodManager mImm;
     private /* final */ Resources mResources;
     private final ArrayList<InputMethodSubtype> mEnabledKeyboardSubtypesOfCurrentInputMethod =
@@ -72,7 +73,8 @@ public class SubtypeSwitcher {
         return sInstance;
     }
 
-    public static void init(LatinIME service) {
+    public static void init(LatinIME service, SharedPreferences prefs) {
+        sInstance.mPrefs = prefs;
         sInstance.resetParams(service);
         if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             sInstance.initLanguageSwitcher(service);
@@ -304,8 +306,7 @@ public class SubtypeSwitcher {
                 // locale (mSystemLocale), then reload the input locale list from the
                 // latin ime settings (shared prefs) and reset the input locale
                 // to the first one.
-                mLanguageSwitcher.loadLocales(PreferenceManager
-                        .getDefaultSharedPreferences(mService));
+                mLanguageSwitcher.loadLocales(mPrefs);
                 mLanguageSwitcher.setSystemLocale(systemLocale);
             } else {
                 updateAllParameters();
@@ -450,9 +451,9 @@ public class SubtypeSwitcher {
         return voiceInputSupportedLocales.contains(locale);
     }
 
-    public void loadSettings(SharedPreferences prefs) {
+    public void loadSettings() {
         if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
-            mLanguageSwitcher.loadLocales(prefs);
+            mLanguageSwitcher.loadLocales(mPrefs);
         }
     }
 
@@ -467,15 +468,14 @@ public class SubtypeSwitcher {
                     mLanguageSwitcher.prev();
                 }
             }
-            mLanguageSwitcher.persist();
+            mLanguageSwitcher.persist(mPrefs);
         }
     }
 
     private void initLanguageSwitcher(LatinIME service) {
         final Configuration conf = service.getResources().getConfiguration();
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
         mLanguageSwitcher = new LanguageSwitcher(service);
-        mLanguageSwitcher.loadLocales(prefs);
+        mLanguageSwitcher.loadLocales(mPrefs);
         mLanguageSwitcher.setSystemLocale(conf.locale);
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/Hints.java b/java/src/com/android/inputmethod/voice/Hints.java
similarity index 84%
rename from java/src/com/android/inputmethod/latin/Hints.java
rename to java/src/com/android/inputmethod/voice/Hints.java
index c467365e7b..3707dc3664 100644
--- a/java/src/com/android/inputmethod/latin/Hints.java
+++ b/java/src/com/android/inputmethod/voice/Hints.java
@@ -14,9 +14,10 @@
  * the License.
  */
 
-package com.android.inputmethod.latin;
+package com.android.inputmethod.voice;
 
-import com.android.inputmethod.voice.SettingsUtil;
+import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.SharedPreferencesCompat;
 
 import android.content.ContentResolver;
 import android.content.Context;
@@ -47,8 +48,9 @@ public class Hints {
     private static final int DEFAULT_SWIPE_HINT_MAX_DAYS_TO_SHOW = 7;
     private static final int DEFAULT_PUNCTUATION_HINT_MAX_DISPLAYS = 7;
 
-    private Context mContext;
-    private Display mDisplay;
+    private final Context mContext;
+    private final SharedPreferences mPrefs;
+    private final Display mDisplay;
     private boolean mVoiceResultContainedPunctuation;
     private int mSwipeHintMaxDaysToShow;
     private int mPunctuationHintMaxDisplays;
@@ -62,8 +64,9 @@ public class Hints {
         SPEAKABLE_PUNCTUATION.put("?", "question mark");
     }
 
-    public Hints(Context context, Display display) {
+    public Hints(Context context, SharedPreferences prefs, Display display) {
         mContext = context;
+        mPrefs = prefs;
         mDisplay = display;
 
         ContentResolver cr = mContext.getContentResolver();
@@ -103,8 +106,7 @@ public class Hints {
 
     public void registerVoiceResult(String text) {
         // Update the current time as the last time voice input was used.
-        SharedPreferences.Editor editor =
-                PreferenceManager.getDefaultSharedPreferences(mContext).edit();
+        SharedPreferences.Editor editor = mPrefs.edit();
         editor.putLong(PREF_VOICE_INPUT_LAST_TIME_USED, System.currentTimeMillis());
         SharedPreferencesCompat.apply(editor);
 
@@ -118,14 +120,14 @@ public class Hints {
     }
 
     private boolean shouldShowSwipeHint() {
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
+        final SharedPreferences prefs = mPrefs;
 
-        int numUniqueDaysShown = sp.getInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, 0);
+        int numUniqueDaysShown = prefs.getInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, 0);
 
         // If we've already shown the hint for enough days, we'll return false.
         if (numUniqueDaysShown < mSwipeHintMaxDaysToShow) {
 
-            long lastTimeVoiceWasUsed = sp.getLong(PREF_VOICE_INPUT_LAST_TIME_USED, 0);
+            long lastTimeVoiceWasUsed = prefs.getLong(PREF_VOICE_INPUT_LAST_TIME_USED, 0);
 
             // If the user has used voice today, we'll return false. (We don't show the hint on
             // any day that the user has already used voice.)
@@ -156,16 +158,16 @@ public class Hints {
     }
 
     private void showHint(int hintViewResource) {
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
+        final SharedPreferences prefs = mPrefs;
 
-        int numUniqueDaysShown = sp.getInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, 0);
-        long lastTimeHintWasShown = sp.getLong(PREF_VOICE_HINT_LAST_TIME_SHOWN, 0);
+        int numUniqueDaysShown = prefs.getInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, 0);
+        long lastTimeHintWasShown = prefs.getLong(PREF_VOICE_HINT_LAST_TIME_SHOWN, 0);
 
         // If this is the first time the hint is being shown today, increase the saved values
         // to represent that. We don't need to increase the last time the hint was shown unless
         // it is a different day from the current value.
         if (!isFromToday(lastTimeHintWasShown)) {
-            SharedPreferences.Editor editor = sp.edit();
+            SharedPreferences.Editor editor = prefs.edit();
             editor.putInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, numUniqueDaysShown + 1);
             editor.putLong(PREF_VOICE_HINT_LAST_TIME_SHOWN, System.currentTimeMillis());
             SharedPreferencesCompat.apply(editor);
@@ -177,9 +179,9 @@ public class Hints {
     }
 
     private int getAndIncrementPref(String pref) {
-        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
-        int value = sp.getInt(pref, 0);
-        SharedPreferences.Editor editor = sp.edit();
+        final SharedPreferences prefs = mPrefs;
+        int value = prefs.getInt(pref, 0);
+        SharedPreferences.Editor editor = prefs.edit();
         editor.putInt(pref, value + 1);
         SharedPreferencesCompat.apply(editor);
         return value;
diff --git a/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java b/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java
index 359760d3ce..498ef728c1 100644
--- a/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java
+++ b/java/src/com/android/inputmethod/voice/VoiceIMEConnector.java
@@ -17,7 +17,6 @@
 package com.android.inputmethod.voice;
 
 import com.android.inputmethod.latin.EditingUtil;
-import com.android.inputmethod.latin.Hints;
 import com.android.inputmethod.latin.KeyboardSwitcher;
 import com.android.inputmethod.latin.LatinIME;
 import com.android.inputmethod.latin.R;
@@ -91,8 +90,8 @@ public class VoiceIMEConnector implements VoiceInput.UiListener {
     private final Map<String, List<CharSequence>> mWordToSuggestions =
             new HashMap<String, List<CharSequence>>();
 
-    public static VoiceIMEConnector init(LatinIME context, UIHandler h) {
-        sInstance.initInternal(context, h);
+    public static VoiceIMEConnector init(LatinIME context, SharedPreferences prefs, UIHandler h) {
+        sInstance.initInternal(context, prefs, h);
         return sInstance;
     }
 
@@ -100,14 +99,14 @@ public class VoiceIMEConnector implements VoiceInput.UiListener {
         return sInstance;
     }
 
-    private void initInternal(LatinIME context, UIHandler h) {
+    private void initInternal(LatinIME context, SharedPreferences prefs, UIHandler h) {
         mContext = context;
         mHandler = h;
         mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
         if (VOICE_INSTALLED) {
             mVoiceInput = new VoiceInput(context, this);
-            mHints = new Hints(context, new Hints.Display() {
+            mHints = new Hints(context, prefs, new Hints.Display() {
                 public void showHint(int viewResource) {
                     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                             Context.LAYOUT_INFLATER_SERVICE);
-- 
GitLab