Merge "Stop reloading contacts when not appropriate."

main
Jean Chalard 2011-08-08 23:44:24 -07:00 committed by Android (Google) Code Review
commit 741c683d5e
3 changed files with 55 additions and 12 deletions

View File

@ -49,20 +49,28 @@ public class ContactsDictionary extends ExpandableDictionary {
private long mLastLoadedContacts; private long mLastLoadedContacts;
public ContactsDictionary(Context context, int dicTypeId) { public ContactsDictionary(final Context context, final int dicTypeId) {
super(context, 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 // Perform a managed query. The Activity will handle closing and requerying the cursor
// when needed. // when needed.
if (mObserver != null) return;
ContentResolver cres = context.getContentResolver(); ContentResolver cres = context.getContentResolver();
cres.registerContentObserver( cres.registerContentObserver(
Contacts.CONTENT_URI, true,mObserver = new ContentObserver(null) { Contacts.CONTENT_URI, true, mObserver = new ContentObserver(null) {
@Override @Override
public void onChange(boolean self) { public void onChange(boolean self) {
setRequiresReload(true); setRequiresReload(true);
} }
}); });
loadDictionary(); }
public void reopen(final Context context) {
registerObserver(context);
} }
@Override @Override

View File

@ -509,7 +509,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if (null == mSubtypeSwitcher) mSubtypeSwitcher = SubtypeSwitcher.getInstance(); if (null == mSubtypeSwitcher) mSubtypeSwitcher = SubtypeSwitcher.getInstance();
mSettingsValues = new Settings.Values(mPrefs, this, mSubtypeSwitcher.getInputLocaleStr()); mSettingsValues = new Settings.Values(mPrefs, this, mSubtypeSwitcher.getInputLocaleStr());
resetContactsDictionary(); resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary());
} }
private void initSuggest() { private void initSuggest() {
@ -518,8 +518,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final Resources res = mResources; final Resources res = mResources;
final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale); final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale);
final ContactsDictionary oldContactsDictionary;
if (mSuggest != null) { if (mSuggest != null) {
oldContactsDictionary = mSuggest.getContactsDictionary();
mSuggest.close(); mSuggest.close();
} else {
oldContactsDictionary = null;
} }
int mainDicResId = Utils.getMainDictionaryResourceId(res); int mainDicResId = Utils.getMainDictionaryResourceId(res);
@ -533,7 +537,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mSuggest.setUserDictionary(mUserDictionary); mSuggest.setUserDictionary(mUserDictionary);
mIsUserDictionaryAvaliable = mUserDictionary.isEnabled(); mIsUserDictionaryAvaliable = mUserDictionary.isEnabled();
resetContactsDictionary(); resetContactsDictionary(oldContactsDictionary);
mUserUnigramDictionary mUserUnigramDictionary
= new UserUnigramDictionary(this, this, localeStr, Suggest.DIC_USER_UNIGRAM); = new UserUnigramDictionary(this, this, localeStr, Suggest.DIC_USER_UNIGRAM);
@ -548,11 +552,36 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
Utils.setSystemLocale(res, savedLocale); Utils.setSystemLocale(res, savedLocale);
} }
private void resetContactsDictionary() { /**
if (null == mSuggest) return; * Resets the contacts dictionary in mSuggest according to the user settings.
ContactsDictionary contactsDictionary = mSettingsValues.mUseContactsDict *
? new ContactsDictionary(this, Suggest.DIC_CONTACTS) : null; * This method takes an optional contacts dictionary to use. Since the contacts dictionary
mSuggest.setContactsDictionary(contactsDictionary); * 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() { /* package private */ void resetSuggestMainDict() {

View File

@ -88,6 +88,7 @@ public class Suggest implements Dictionary.WordCallback {
private AutoCorrection mAutoCorrection; private AutoCorrection mAutoCorrection;
private Dictionary mMainDict; private Dictionary mMainDict;
private ContactsDictionary mContactsDict;
private WhitelistDictionary mWhiteListDictionary; private WhitelistDictionary mWhiteListDictionary;
private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>(); private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>();
private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>(); private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>();
@ -197,6 +198,10 @@ public class Suggest implements Dictionary.WordCallback {
return mMainDict != null; return mMainDict != null;
} }
public ContactsDictionary getContactsDictionary() {
return mContactsDict;
}
public Map<String, Dictionary> getUnigramDictionaries() { public Map<String, Dictionary> getUnigramDictionaries() {
return mUnigramDictionaries; 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 * the contacts dictionary by passing null to this method. In this case no contacts dictionary
* won't be used. * won't be used.
*/ */
public void setContactsDictionary(Dictionary contactsDictionary) { public void setContactsDictionary(ContactsDictionary contactsDictionary) {
mContactsDict = contactsDictionary;
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
} }