aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2011-08-08 23:44:24 -0700
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-08-08 23:44:24 -0700
commit741c683d5e73b9a1357457b7f45794ec5e0debf1 (patch)
tree23741f73a6dae914762fde33444eb48a29ee7b2f /java/src
parent33f3b60cf0660ad5893c1463077a47a03ac6bcf9 (diff)
parent14051e2b5343db4b0531b7b4b806da0c09d6e251 (diff)
downloadlatinime-741c683d5e73b9a1357457b7f45794ec5e0debf1.tar.gz
latinime-741c683d5e73b9a1357457b7f45794ec5e0debf1.tar.xz
latinime-741c683d5e73b9a1357457b7f45794ec5e0debf1.zip
Merge "Stop reloading contacts when not appropriate."
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/latin/ContactsDictionary.java16
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java43
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java8
3 files changed, 55 insertions, 12 deletions
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<String, Dictionary> mUnigramDictionaries = 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;
}
+ public ContactsDictionary getContactsDictionary() {
+ return mContactsDict;
+ }
+
public Map<String, Dictionary> 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);
}