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/UserDictionary.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/UserDictionary.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/UserDictionary.java | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/java/src/com/android/inputmethod/latin/UserDictionary.java b/java/src/com/android/inputmethod/latin/UserDictionary.java index 4b98eacce..e8ca33af3 100644 --- a/java/src/com/android/inputmethod/latin/UserDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserDictionary.java @@ -16,16 +16,11 @@ package com.android.inputmethod.latin; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.ContentObserver; import android.database.Cursor; -import android.net.Uri; import android.provider.UserDictionary.Words; public class UserDictionary extends ExpandableDictionary { @@ -40,8 +35,6 @@ public class UserDictionary extends ExpandableDictionary { private static final int INDEX_FREQUENCY = 2; private ContentObserver mObserver; - - private boolean mRequiresReload; private String mLocale; public UserDictionary(Context context, String locale) { @@ -54,26 +47,27 @@ public class UserDictionary extends ExpandableDictionary { cres.registerContentObserver(Words.CONTENT_URI, true, mObserver = new ContentObserver(null) { @Override public void onChange(boolean self) { - mRequiresReload = true; + setRequiresReload(true); } }); loadDictionary(); } - + public synchronized void close() { if (mObserver != null) { getContext().getContentResolver().unregisterContentObserver(mObserver); mObserver = null; } + super.close(); } - - private synchronized void loadDictionary() { + + @Override + public void loadDictionaryAsync() { Cursor cursor = getContext().getContentResolver() .query(Words.CONTENT_URI, PROJECTION, "(locale IS NULL) or (locale=?)", new String[] { mLocale }, null); addWords(cursor); - mRequiresReload = false; } /** @@ -86,7 +80,8 @@ public class UserDictionary extends ExpandableDictionary { */ @Override public synchronized void addWord(String word, int frequency) { - if (mRequiresReload) loadDictionary(); + // Force load the dictionary here synchronously + if (getRequiresReload()) loadDictionaryAsync(); // Safeguard against adding long words. Can cause stack overflow. if (word.length() >= getMaxWordLength()) return; @@ -101,19 +96,17 @@ public class UserDictionary extends ExpandableDictionary { getContext().getContentResolver().insert(Words.CONTENT_URI, values); // In case the above does a synchronous callback of the change observer - mRequiresReload = false; + setRequiresReload(false); } @Override public synchronized void getWords(final WordComposer codes, final WordCallback callback, int[] nextLettersFrequencies) { - if (mRequiresReload) loadDictionary(); super.getWords(codes, callback, nextLettersFrequencies); } @Override public synchronized boolean isValidWord(CharSequence word) { - if (mRequiresReload) loadDictionary(); return super.isValidWord(word); } |