diff options
author | 2010-03-10 12:59:16 -0800 | |
---|---|---|
committer | 2010-03-10 12:59:16 -0800 | |
commit | 21daa53245318296adaecb88ab548628540b151c (patch) | |
tree | 3d08c9c02f95977ef52768022b2e2f8bd521eb87 /java/src/com/android/inputmethod/latin/ExpandableDictionary.java | |
parent | 918f1b1de60ff56181e34f8591ea0aff9773d812 (diff) | |
parent | 283a77f633e92ed7dbe96b083c921fc244bbe880 (diff) | |
download | latinime-21daa53245318296adaecb88ab548628540b151c.tar.gz latinime-21daa53245318296adaecb88ab548628540b151c.tar.xz latinime-21daa53245318296adaecb88ab548628540b151c.zip |
Merge "Load UserDictionary and AutoDictionary in a background thread."
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]; |