Preload SharedPreferences at start.

bug: 3207554
Change-Id: I84072461aee9675cc67897de03f89e9c0036bc1f
main
Ken Wakasa 2010-11-26 13:08:36 +09:00
parent 7f0befe1f0
commit 27d13713bb
8 changed files with 64 additions and 63 deletions

View File

@ -34,6 +34,7 @@ import android.text.TextUtils;
public class InputLanguageSelection extends PreferenceActivity { public class InputLanguageSelection extends PreferenceActivity {
private SharedPreferences mPrefs;
private String mSelectedLanguages; private String mSelectedLanguages;
private ArrayList<Loc> mAvailableLanguages = new ArrayList<Loc>(); private ArrayList<Loc> mAvailableLanguages = new ArrayList<Loc>();
private static final String[] BLACKLIST_LANGUAGES = { private static final String[] BLACKLIST_LANGUAGES = {
@ -66,8 +67,8 @@ public class InputLanguageSelection extends PreferenceActivity {
super.onCreate(icicle); super.onCreate(icicle);
addPreferencesFromResource(R.xml.language_prefs); addPreferencesFromResource(R.xml.language_prefs);
// Get the settings preferences // Get the settings preferences
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mSelectedLanguages = sp.getString(LatinIME.PREF_SELECTED_LANGUAGES, ""); mSelectedLanguages = mPrefs.getString(LatinIME.PREF_SELECTED_LANGUAGES, "");
String[] languageList = mSelectedLanguages.split(","); String[] languageList = mSelectedLanguages.split(",");
mAvailableLanguages = getUniqueLocales(); mAvailableLanguages = getUniqueLocales();
PreferenceGroup parent = getPreferenceScreen(); PreferenceGroup parent = getPreferenceScreen();
@ -140,8 +141,7 @@ public class InputLanguageSelection extends PreferenceActivity {
} }
} }
if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); Editor editor = mPrefs.edit();
Editor editor = sp.edit();
editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, checkedLanguages); editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, checkedLanguages);
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
} }

View File

@ -60,6 +60,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
private static final int SYMBOLS_MODE_STATE_SYMBOL = 2; private static final int SYMBOLS_MODE_STATE_SYMBOL = 2;
private SubtypeSwitcher mSubtypeSwitcher; private SubtypeSwitcher mSubtypeSwitcher;
private SharedPreferences mPrefs;
private LatinKeyboardView mInputView; private LatinKeyboardView mInputView;
private LatinIME mInputMethodService; private LatinIME mInputMethodService;
@ -105,11 +106,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
private KeyboardSwitcher() { private KeyboardSwitcher() {
} }
public static void init(LatinIME ims) { public static void init(LatinIME ims, SharedPreferences prefs) {
sInstance.mInputMethodService = ims; sInstance.mInputMethodService = ims;
sInstance.mPrefs = prefs;
sInstance.mSubtypeSwitcher = SubtypeSwitcher.getInstance(); sInstance.mSubtypeSwitcher = SubtypeSwitcher.getInstance();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ims);
sInstance.mLayoutId = Integer.valueOf( sInstance.mLayoutId = Integer.valueOf(
prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID)); prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID));
prefs.registerOnSharedPreferenceChangeListener(sInstance); prefs.registerOnSharedPreferenceChangeListener(sInstance);
@ -282,9 +283,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
mVoiceButtonOnPrimary = voiceButtonOnPrimary; mVoiceButtonOnPrimary = voiceButtonOnPrimary;
mIsSymbols = isSymbols; mIsSymbols = isSymbols;
// Update the settings key state because number of enabled IMEs could have been changed // Update the settings key state because number of enabled IMEs could have been changed
mHasSettingsKey = getSettingsKeyMode( mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService);
PreferenceManager.getDefaultSharedPreferences(mInputMethodService),
mInputMethodService);
makeSymbolsKeyboardIds(); makeSymbolsKeyboardIds();
KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols); KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols);

View File

@ -186,9 +186,8 @@ public class LanguageSwitcher {
mCurrentIndex = prevLocaleIndex(); mCurrentIndex = prevLocaleIndex();
} }
public void persist() { public void persist(SharedPreferences prefs) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mIme); Editor editor = prefs.edit();
Editor editor = sp.edit();
editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage()); editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage());
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
} }

View File

@ -145,6 +145,7 @@ public class LatinIME extends InputMethodService
private AutoDictionary mAutoDictionary; private AutoDictionary mAutoDictionary;
private Resources mResources; private Resources mResources;
private SharedPreferences mPrefs;
private final StringBuilder mComposing = new StringBuilder(); private final StringBuilder mComposing = new StringBuilder();
private WordComposer mWord = new WordComposer(); private WordComposer mWord = new WordComposer();
@ -332,15 +333,16 @@ public class LatinIME extends InputMethodService
@Override @Override
public void onCreate() { public void onCreate() {
LatinImeLogger.init(this); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SubtypeSwitcher.init(this); mPrefs = prefs;
KeyboardSwitcher.init(this); LatinImeLogger.init(this, prefs);
SubtypeSwitcher.init(this, prefs);
KeyboardSwitcher.init(this, prefs);
super.onCreate(); super.onCreate();
//setStatusIcon(R.drawable.ime_qwerty); //setStatusIcon(R.drawable.ime_qwerty);
mResources = getResources(); mResources = getResources();
mImm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)); mImm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
final Configuration conf = mResources.getConfiguration(); final Configuration conf = mResources.getConfiguration();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mSubtypeSwitcher = SubtypeSwitcher.getInstance();
mKeyboardSwitcher = KeyboardSwitcher.getInstance(); mKeyboardSwitcher = KeyboardSwitcher.getInstance();
mReCorrectionEnabled = prefs.getBoolean(PREF_RECORRECTION_ENABLED, mReCorrectionEnabled = prefs.getBoolean(PREF_RECORRECTION_ENABLED,
@ -363,7 +365,7 @@ public class LatinIME extends InputMethodService
// register to receive ringer mode changes for silent mode // register to receive ringer mode changes for silent mode
IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(mReceiver, filter); registerReceiver(mReceiver, filter);
mVoiceConnector = VoiceIMEConnector.init(this, mHandler); mVoiceConnector = VoiceIMEConnector.init(this, prefs, mHandler);
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
} }
@ -415,12 +417,12 @@ public class LatinIME extends InputMethodService
if (mSuggest != null) { if (mSuggest != null) {
mSuggest.close(); mSuggest.close();
} }
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences prefs = mPrefs;
mQuickFixes = sp.getBoolean(PREF_QUICK_FIXES, true); mQuickFixes = prefs.getBoolean(PREF_QUICK_FIXES, true);
int[] dictionaries = getDictionary(orig); int[] dictionaries = getDictionary(orig);
mSuggest = new Suggest(this, dictionaries); mSuggest = new Suggest(this, dictionaries);
loadAndSetAutoCompletionThreshold(sp); loadAndSetAutoCompletionThreshold(prefs);
if (mUserDictionary != null) mUserDictionary.close(); if (mUserDictionary != null) mUserDictionary.close();
mUserDictionary = new UserDictionary(this, locale); mUserDictionary = new UserDictionary(this, locale);
if (mContactsDictionary == null) { if (mContactsDictionary == null) {
@ -2070,26 +2072,26 @@ public class LatinIME extends InputMethodService
private void loadSettings(EditorInfo attribute) { private void loadSettings(EditorInfo attribute) {
// Get the settings preferences // Get the settings preferences
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences prefs = mPrefs;
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
mVibrateOn = vibrator != null && vibrator.hasVibrator() mVibrateOn = vibrator != null && vibrator.hasVibrator()
&& sp.getBoolean(LatinIMESettings.PREF_VIBRATE_ON, false); && prefs.getBoolean(LatinIMESettings.PREF_VIBRATE_ON, false);
mSoundOn = sp.getBoolean(PREF_SOUND_ON, false); mSoundOn = prefs.getBoolean(PREF_SOUND_ON, false);
mPopupOn = sp.getBoolean(PREF_POPUP_ON, mPopupOn = prefs.getBoolean(PREF_POPUP_ON,
mResources.getBoolean(R.bool.default_popup_preview)); mResources.getBoolean(R.bool.default_popup_preview));
mAutoCap = sp.getBoolean(PREF_AUTO_CAP, true); mAutoCap = prefs.getBoolean(PREF_AUTO_CAP, true);
mQuickFixes = sp.getBoolean(PREF_QUICK_FIXES, true); mQuickFixes = prefs.getBoolean(PREF_QUICK_FIXES, true);
mAutoCorrectEnabled = isAutoCorrectEnabled(sp); mAutoCorrectEnabled = isAutoCorrectEnabled(prefs);
mBigramSuggestionEnabled = mAutoCorrectEnabled && isBigramSuggestionEnabled(sp); mBigramSuggestionEnabled = mAutoCorrectEnabled && isBigramSuggestionEnabled(prefs);
loadAndSetAutoCompletionThreshold(sp); loadAndSetAutoCompletionThreshold(prefs);
mVoiceConnector.loadSettings(attribute, sp); mVoiceConnector.loadSettings(attribute, prefs);
updateCorrectionMode(); updateCorrectionMode();
updateAutoTextEnabled(); updateAutoTextEnabled();
updateSuggestionVisibility(sp); updateSuggestionVisibility(prefs);
SubtypeSwitcher.getInstance().loadSettings(sp); SubtypeSwitcher.getInstance().loadSettings();
} }
/** /**

View File

@ -29,7 +29,7 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 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() { public static void commit() {

View File

@ -50,6 +50,7 @@ public class SubtypeSwitcher {
private static final SubtypeSwitcher sInstance = new SubtypeSwitcher(); private static final SubtypeSwitcher sInstance = new SubtypeSwitcher();
private /* final */ LatinIME mService; private /* final */ LatinIME mService;
private /* final */ SharedPreferences mPrefs;
private /* final */ InputMethodManager mImm; private /* final */ InputMethodManager mImm;
private /* final */ Resources mResources; private /* final */ Resources mResources;
private final ArrayList<InputMethodSubtype> mEnabledKeyboardSubtypesOfCurrentInputMethod = private final ArrayList<InputMethodSubtype> mEnabledKeyboardSubtypesOfCurrentInputMethod =
@ -72,7 +73,8 @@ public class SubtypeSwitcher {
return sInstance; return sInstance;
} }
public static void init(LatinIME service) { public static void init(LatinIME service, SharedPreferences prefs) {
sInstance.mPrefs = prefs;
sInstance.resetParams(service); sInstance.resetParams(service);
if (USE_SPACEBAR_LANGUAGE_SWITCHER) { if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
sInstance.initLanguageSwitcher(service); sInstance.initLanguageSwitcher(service);
@ -304,8 +306,7 @@ public class SubtypeSwitcher {
// locale (mSystemLocale), then reload the input locale list from the // locale (mSystemLocale), then reload the input locale list from the
// latin ime settings (shared prefs) and reset the input locale // latin ime settings (shared prefs) and reset the input locale
// to the first one. // to the first one.
mLanguageSwitcher.loadLocales(PreferenceManager mLanguageSwitcher.loadLocales(mPrefs);
.getDefaultSharedPreferences(mService));
mLanguageSwitcher.setSystemLocale(systemLocale); mLanguageSwitcher.setSystemLocale(systemLocale);
} else { } else {
updateAllParameters(); updateAllParameters();
@ -450,9 +451,9 @@ public class SubtypeSwitcher {
return voiceInputSupportedLocales.contains(locale); return voiceInputSupportedLocales.contains(locale);
} }
public void loadSettings(SharedPreferences prefs) { public void loadSettings() {
if (USE_SPACEBAR_LANGUAGE_SWITCHER) { if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
mLanguageSwitcher.loadLocales(prefs); mLanguageSwitcher.loadLocales(mPrefs);
} }
} }
@ -467,15 +468,14 @@ public class SubtypeSwitcher {
mLanguageSwitcher.prev(); mLanguageSwitcher.prev();
} }
} }
mLanguageSwitcher.persist(); mLanguageSwitcher.persist(mPrefs);
} }
} }
private void initLanguageSwitcher(LatinIME service) { private void initLanguageSwitcher(LatinIME service) {
final Configuration conf = service.getResources().getConfiguration(); final Configuration conf = service.getResources().getConfiguration();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
mLanguageSwitcher = new LanguageSwitcher(service); mLanguageSwitcher = new LanguageSwitcher(service);
mLanguageSwitcher.loadLocales(prefs); mLanguageSwitcher.loadLocales(mPrefs);
mLanguageSwitcher.setSystemLocale(conf.locale); mLanguageSwitcher.setSystemLocale(conf.locale);
} }
} }

View File

@ -14,9 +14,10 @@
* the License. * 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.ContentResolver;
import android.content.Context; 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_SWIPE_HINT_MAX_DAYS_TO_SHOW = 7;
private static final int DEFAULT_PUNCTUATION_HINT_MAX_DISPLAYS = 7; private static final int DEFAULT_PUNCTUATION_HINT_MAX_DISPLAYS = 7;
private Context mContext; private final Context mContext;
private Display mDisplay; private final SharedPreferences mPrefs;
private final Display mDisplay;
private boolean mVoiceResultContainedPunctuation; private boolean mVoiceResultContainedPunctuation;
private int mSwipeHintMaxDaysToShow; private int mSwipeHintMaxDaysToShow;
private int mPunctuationHintMaxDisplays; private int mPunctuationHintMaxDisplays;
@ -62,8 +64,9 @@ public class Hints {
SPEAKABLE_PUNCTUATION.put("?", "question mark"); SPEAKABLE_PUNCTUATION.put("?", "question mark");
} }
public Hints(Context context, Display display) { public Hints(Context context, SharedPreferences prefs, Display display) {
mContext = context; mContext = context;
mPrefs = prefs;
mDisplay = display; mDisplay = display;
ContentResolver cr = mContext.getContentResolver(); ContentResolver cr = mContext.getContentResolver();
@ -103,8 +106,7 @@ public class Hints {
public void registerVoiceResult(String text) { public void registerVoiceResult(String text) {
// Update the current time as the last time voice input was used. // Update the current time as the last time voice input was used.
SharedPreferences.Editor editor = SharedPreferences.Editor editor = mPrefs.edit();
PreferenceManager.getDefaultSharedPreferences(mContext).edit();
editor.putLong(PREF_VOICE_INPUT_LAST_TIME_USED, System.currentTimeMillis()); editor.putLong(PREF_VOICE_INPUT_LAST_TIME_USED, System.currentTimeMillis());
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
@ -118,14 +120,14 @@ public class Hints {
} }
private boolean shouldShowSwipeHint() { 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 we've already shown the hint for enough days, we'll return false.
if (numUniqueDaysShown < mSwipeHintMaxDaysToShow) { 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 // 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.) // any day that the user has already used voice.)
@ -156,16 +158,16 @@ public class Hints {
} }
private void showHint(int hintViewResource) { 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); int numUniqueDaysShown = prefs.getInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, 0);
long lastTimeHintWasShown = sp.getLong(PREF_VOICE_HINT_LAST_TIME_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 // 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 // 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. // it is a different day from the current value.
if (!isFromToday(lastTimeHintWasShown)) { 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.putInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, numUniqueDaysShown + 1);
editor.putLong(PREF_VOICE_HINT_LAST_TIME_SHOWN, System.currentTimeMillis()); editor.putLong(PREF_VOICE_HINT_LAST_TIME_SHOWN, System.currentTimeMillis());
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
@ -177,9 +179,9 @@ public class Hints {
} }
private int getAndIncrementPref(String pref) { private int getAndIncrementPref(String pref) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); final SharedPreferences prefs = mPrefs;
int value = sp.getInt(pref, 0); int value = prefs.getInt(pref, 0);
SharedPreferences.Editor editor = sp.edit(); SharedPreferences.Editor editor = prefs.edit();
editor.putInt(pref, value + 1); editor.putInt(pref, value + 1);
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
return value; return value;

View File

@ -17,7 +17,6 @@
package com.android.inputmethod.voice; package com.android.inputmethod.voice;
import com.android.inputmethod.latin.EditingUtil; import com.android.inputmethod.latin.EditingUtil;
import com.android.inputmethod.latin.Hints;
import com.android.inputmethod.latin.KeyboardSwitcher; import com.android.inputmethod.latin.KeyboardSwitcher;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
@ -91,8 +90,8 @@ public class VoiceIMEConnector implements VoiceInput.UiListener {
private final Map<String, List<CharSequence>> mWordToSuggestions = private final Map<String, List<CharSequence>> mWordToSuggestions =
new HashMap<String, List<CharSequence>>(); new HashMap<String, List<CharSequence>>();
public static VoiceIMEConnector init(LatinIME context, UIHandler h) { public static VoiceIMEConnector init(LatinIME context, SharedPreferences prefs, UIHandler h) {
sInstance.initInternal(context, h); sInstance.initInternal(context, prefs, h);
return sInstance; return sInstance;
} }
@ -100,14 +99,14 @@ public class VoiceIMEConnector implements VoiceInput.UiListener {
return sInstance; return sInstance;
} }
private void initInternal(LatinIME context, UIHandler h) { private void initInternal(LatinIME context, SharedPreferences prefs, UIHandler h) {
mContext = context; mContext = context;
mHandler = h; mHandler = h;
mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mSubtypeSwitcher = SubtypeSwitcher.getInstance();
if (VOICE_INSTALLED) { if (VOICE_INSTALLED) {
mVoiceInput = new VoiceInput(context, this); 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) { public void showHint(int viewResource) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE); Context.LAYOUT_INFLATER_SERVICE);