diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionary.java b/java/src/com/android/inputmethod/latin/ContactsDictionary.java index 66a041508..8a7dfb839 100644 --- a/java/src/com/android/inputmethod/latin/ContactsDictionary.java +++ b/java/src/com/android/inputmethod/latin/ContactsDictionary.java @@ -49,20 +49,28 @@ public class ContactsDictionary extends ExpandableDictionary { private long mLastLoadedContacts; - public ContactsDictionary(Context context, int dicTypeId) { + public ContactsDictionary(final Context context, final int dicTypeId) { super(context, dicTypeId); + registerObserver(context); + loadDictionary(); + } + + private synchronized void registerObserver(final Context context) { // Perform a managed query. The Activity will handle closing and requerying the cursor // when needed. + if (mObserver != null) return; ContentResolver cres = context.getContentResolver(); - cres.registerContentObserver( - Contacts.CONTENT_URI, true,mObserver = new ContentObserver(null) { + Contacts.CONTENT_URI, true, mObserver = new ContentObserver(null) { @Override public void onChange(boolean self) { setRequiresReload(true); } }); - loadDictionary(); + } + + public void reopen(final Context context) { + registerObserver(context); } @Override diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index a58815892..d74babf4f 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -509,7 +509,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (null == mSubtypeSwitcher) mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mSettingsValues = new Settings.Values(mPrefs, this, mSubtypeSwitcher.getInputLocaleStr()); - resetContactsDictionary(); + resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary()); } private void initSuggest() { @@ -518,8 +518,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final Resources res = mResources; final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale); + final ContactsDictionary oldContactsDictionary; if (mSuggest != null) { + oldContactsDictionary = mSuggest.getContactsDictionary(); mSuggest.close(); + } else { + oldContactsDictionary = null; } int mainDicResId = Utils.getMainDictionaryResourceId(res); @@ -533,7 +537,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mSuggest.setUserDictionary(mUserDictionary); mIsUserDictionaryAvaliable = mUserDictionary.isEnabled(); - resetContactsDictionary(); + resetContactsDictionary(oldContactsDictionary); mUserUnigramDictionary = new UserUnigramDictionary(this, this, localeStr, Suggest.DIC_USER_UNIGRAM); @@ -548,11 +552,36 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar Utils.setSystemLocale(res, savedLocale); } - private void resetContactsDictionary() { - if (null == mSuggest) return; - ContactsDictionary contactsDictionary = mSettingsValues.mUseContactsDict - ? new ContactsDictionary(this, Suggest.DIC_CONTACTS) : null; - mSuggest.setContactsDictionary(contactsDictionary); + /** + * Resets the contacts dictionary in mSuggest according to the user settings. + * + * This method takes an optional contacts dictionary to use. Since the contacts dictionary + * does not depend on the locale, it can be reused across different instances of Suggest. + * The dictionary will also be opened or closed as necessary depending on the settings. + * + * @param oldContactsDictionary an optional dictionary to use, or null + */ + private void resetContactsDictionary(final ContactsDictionary oldContactsDictionary) { + final boolean shouldSetDictionary = (null != mSuggest && mSettingsValues.mUseContactsDict); + + final ContactsDictionary dictionaryToUse; + if (!shouldSetDictionary) { + // Make sure the dictionary is closed. If it is already closed, this is a no-op, + // so it's safe to call it anyways. + if (null != oldContactsDictionary) oldContactsDictionary.close(); + dictionaryToUse = null; + } else if (null != oldContactsDictionary) { + // Make sure the old contacts dictionary is opened. If it is already open, this is a + // no-op, so it's safe to call it anyways. + oldContactsDictionary.reopen(this); + dictionaryToUse = oldContactsDictionary; + } else { + dictionaryToUse = new ContactsDictionary(this, Suggest.DIC_CONTACTS); + } + + if (null != mSuggest) { + mSuggest.setContactsDictionary(dictionaryToUse); + } } /* package private */ void resetSuggestMainDict() { diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 36a29e896..a2d66f398 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -88,6 +88,7 @@ public class Suggest implements Dictionary.WordCallback { private AutoCorrection mAutoCorrection; private Dictionary mMainDict; + private ContactsDictionary mContactsDict; private WhitelistDictionary mWhiteListDictionary; private final Map mUnigramDictionaries = new HashMap(); private final Map mBigramDictionaries = new HashMap(); @@ -197,6 +198,10 @@ public class Suggest implements Dictionary.WordCallback { return mMainDict != null; } + public ContactsDictionary getContactsDictionary() { + return mContactsDict; + } + public Map getUnigramDictionaries() { return mUnigramDictionaries; } @@ -218,7 +223,8 @@ public class Suggest implements Dictionary.WordCallback { * the contacts dictionary by passing null to this method. In this case no contacts dictionary * won't be used. */ - public void setContactsDictionary(Dictionary contactsDictionary) { + public void setContactsDictionary(ContactsDictionary contactsDictionary) { + mContactsDict = contactsDictionary; addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); }