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/ExpandableDictionary.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/ExpandableDictionary.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/ExpandableDictionary.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index 006593700..46bc41c42 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -16,7 +16,11 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.latin.Dictionary.WordCallback; + import android.content.Context; +import android.os.AsyncTask; +import android.os.SystemClock; /** * Base class for an in-memory dictionary that can grow dynamically and can @@ -32,6 +36,13 @@ public class ExpandableDictionary extends Dictionary { public static final int MAX_WORD_LENGTH = 32; private static final char QUOTE = '\''; + private boolean mRequiresReload; + + private boolean mUpdatingDictionary; + + // Use this lock before touching mUpdatingDictionary & mRequiresDownload + private Object mUpdatingLock = new Object(); + static class Node { char code; int frequency; @@ -70,6 +81,34 @@ public class ExpandableDictionary extends Dictionary { mCodes = new int[MAX_WORD_LENGTH][]; } + public void loadDictionary() { + synchronized (mUpdatingLock) { + startDictionaryLoadingTaskLocked(); + } + } + + public void startDictionaryLoadingTaskLocked() { + if (!mUpdatingDictionary) { + mUpdatingDictionary = true; + mRequiresReload = false; + new LoadDictionaryTask().execute(); + } + } + + public void setRequiresReload(boolean reload) { + synchronized (mUpdatingLock) { + mRequiresReload = reload; + } + } + + public boolean getRequiresReload() { + return mRequiresReload; + } + + /** Override to load your dictionary here, on a background thread. */ + public void loadDictionaryAsync() { + } + Context getContext() { return mContext; } @@ -119,6 +158,13 @@ public class ExpandableDictionary extends Dictionary { @Override public void getWords(final WordComposer codes, final WordCallback callback, int[] nextLettersFrequencies) { + synchronized (mUpdatingLock) { + // If we need to update, start off a background task + if (mRequiresReload) startDictionaryLoadingTaskLocked(); + // Currently updating contacts, don't return any results. + if (mUpdatingDictionary) return; + } + mInputLength = codes.size(); mNextLettersFrequencies = nextLettersFrequencies; if (mCodes.length < mInputLength) mCodes = new int[mInputLength][]; @@ -135,6 +181,11 @@ public class ExpandableDictionary extends Dictionary { @Override public synchronized boolean isValidWord(CharSequence word) { + synchronized (mUpdatingLock) { + // If we need to update, start off a background task + if (mRequiresReload) startDictionaryLoadingTaskLocked(); + if (mUpdatingDictionary) return false; + } final int freq = getWordFrequencyRec(mRoots, word, 0, word.length()); return freq > -1; } @@ -277,6 +328,24 @@ public class ExpandableDictionary extends Dictionary { mRoots = new NodeArray(); } + private class LoadDictionaryTask extends AsyncTask<Void, Void, Void> { + @Override + protected Void doInBackground(Void... v) { + loadDictionaryAsync(); + return null; + } + + @Override + protected void onPostExecute(Void result) { + // TODO Auto-generated method stub + synchronized (mUpdatingLock) { + mUpdatingDictionary = false; + } + super.onPostExecute(result); + } + + } + static char toLowerCase(char c) { if (c < BASE_CHARS.length) { c = BASE_CHARS[c]; |