diff options
author | 2010-03-10 11:39:06 -0800 | |
---|---|---|
committer | 2010-03-10 11:39:06 -0800 | |
commit | 283a77f633e92ed7dbe96b083c921fc244bbe880 (patch) | |
tree | b7cebd95da3d926f14eec5cf9513d87a86f83087 /java/src/com/android/inputmethod/latin/ContactsDictionary.java | |
parent | 97e2d11039137b2f82ff300d742ecb433f1a56e0 (diff) | |
download | latinime-283a77f633e92ed7dbe96b083c921fc244bbe880.tar.gz latinime-283a77f633e92ed7dbe96b083c921fc244bbe880.tar.xz latinime-283a77f633e92ed7dbe96b083c921fc244bbe880.zip |
Load UserDictionary and AutoDictionary in a background thread.
This is to avoid ANRs during bootup, as some of the providers
may not have been initialized yet.
Refactored the ContactsDictionary and moved the async loading
code to ExpandableDictionary to share with the other dicts.
Bug: 2501133
Change-Id: I20393edb6fdf5df2f54ebac8dd04419a592177a2
Diffstat (limited to 'java/src/com/android/inputmethod/latin/ContactsDictionary.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/ContactsDictionary.java | 73 |
1 files changed, 12 insertions, 61 deletions
diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionary.java b/java/src/com/android/inputmethod/latin/ContactsDictionary.java index f53ebf3f5..15edb706a 100644 --- a/java/src/com/android/inputmethod/latin/ContactsDictionary.java +++ b/java/src/com/android/inputmethod/latin/ContactsDictionary.java @@ -35,15 +35,8 @@ public class ContactsDictionary extends ExpandableDictionary { private ContentObserver mObserver; - private boolean mRequiresReload; - private long mLastLoadedContacts; - private boolean mUpdatingContacts; - - // Use this lock before touching mUpdatingContacts & mRequiresDownload - private Object mUpdatingLock = new Object(); - public ContactsDictionary(Context context) { super(context); // Perform a managed query. The Activity will handle closing and requerying the cursor @@ -53,15 +46,10 @@ public class ContactsDictionary extends ExpandableDictionary { cres.registerContentObserver(Contacts.CONTENT_URI, true, mObserver = new ContentObserver(null) { @Override public void onChange(boolean self) { - synchronized (mUpdatingLock) { - mRequiresReload = true; - } + setRequiresReload(true); } }); - - synchronized (mUpdatingLock) { - loadDictionaryAsyncLocked(); - } + loadDictionary(); } public synchronized void close() { @@ -69,41 +57,26 @@ public class ContactsDictionary extends ExpandableDictionary { getContext().getContentResolver().unregisterContentObserver(mObserver); mObserver = null; } + super.close(); } - private synchronized void loadDictionaryAsyncLocked() { + @Override + public void startDictionaryLoadingTaskLocked() { long now = SystemClock.uptimeMillis(); if (mLastLoadedContacts == 0 || now - mLastLoadedContacts > 30 * 60 * 1000 /* 30 minutes */) { - if (!mUpdatingContacts) { - mUpdatingContacts = true; - mRequiresReload = false; - new LoadContactsTask().execute(); - } - } - } - - @Override - public synchronized void getWords(final WordComposer codes, final WordCallback callback, - int[] nextLettersFrequencies) { - synchronized (mUpdatingLock) { - // If we need to update, start off a background task - if (mRequiresReload) loadDictionaryAsyncLocked(); - // Currently updating contacts, don't return any results. - if (mUpdatingContacts) return; + super.startDictionaryLoadingTaskLocked(); } - super.getWords(codes, callback, nextLettersFrequencies); } @Override - public synchronized boolean isValidWord(CharSequence word) { - synchronized (mUpdatingLock) { - // If we need to update, start off a background task - if (mRequiresReload) loadDictionaryAsyncLocked(); - if (mUpdatingContacts) return false; + public void loadDictionaryAsync() { + Cursor cursor = getContext().getContentResolver() + .query(Contacts.CONTENT_URI, PROJECTION, null, null, null); + if (cursor != null) { + addWords(cursor); } - - return super.isValidWord(word); + mLastLoadedContacts = SystemClock.uptimeMillis(); } private void addWords(Cursor cursor) { @@ -150,27 +123,5 @@ public class ContactsDictionary extends ExpandableDictionary { } cursor.close(); } - - private class LoadContactsTask extends AsyncTask<Void, Void, Void> { - @Override - protected Void doInBackground(Void... v) { - Cursor cursor = getContext().getContentResolver() - .query(Contacts.CONTENT_URI, PROJECTION, null, null, null); - if (cursor != null) { - addWords(cursor); - } - mLastLoadedContacts = SystemClock.uptimeMillis(); - return null; - } - @Override - protected void onPostExecute(Void result) { - // TODO Auto-generated method stub - synchronized (mUpdatingLock) { - mUpdatingContacts = false; - } - super.onPostExecute(result); - } - - } } |