diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/UserBinaryDictionary.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/UserBinaryDictionary.java | 81 |
1 files changed, 41 insertions, 40 deletions
diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java index 15b3d8d02..b21f30087 100644 --- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java @@ -18,7 +18,6 @@ package com.android.inputmethod.latin; import android.content.ContentProviderClient; import android.content.ContentResolver; -import android.content.ContentUris; import android.content.Context; import android.database.ContentObserver; import android.database.Cursor; @@ -30,9 +29,9 @@ import android.text.TextUtils; import android.util.Log; import com.android.inputmethod.compat.UserDictionaryCompatUtils; -import com.android.inputmethod.latin.utils.LocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; +import java.io.File; import java.util.Arrays; import java.util.Locale; @@ -75,24 +74,31 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { final private String mLocale; final private boolean mAlsoUseMoreRestrictiveLocales; - public UserBinaryDictionary(final Context context, final String locale) { - this(context, locale, false); + public UserBinaryDictionary(final Context context, final Locale locale) { + this(context, locale, false /* alsoUseMoreRestrictiveLocales */, null /* dictFile */); } - public UserBinaryDictionary(final Context context, final String locale, - final boolean alsoUseMoreRestrictiveLocales) { - super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_USER, - false /* isUpdatable */); + public UserBinaryDictionary(final Context context, final Locale locale, final File dictFile) { + this(context, locale, false /* alsoUseMoreRestrictiveLocales */, dictFile); + } + + public UserBinaryDictionary(final Context context, final Locale locale, + final boolean alsoUseMoreRestrictiveLocales, final File dictFile) { + this(context, locale, alsoUseMoreRestrictiveLocales, dictFile, NAME); + } + + protected UserBinaryDictionary(final Context context, final Locale locale, + final boolean alsoUseMoreRestrictiveLocales, final File dictFile, final String name) { + super(context, getDictName(name, locale, dictFile), locale, Dictionary.TYPE_USER, dictFile); if (null == locale) throw new NullPointerException(); // Catch the error earlier - if (SubtypeLocaleUtils.NO_LANGUAGE.equals(locale)) { + final String localeStr = locale.toString(); + if (SubtypeLocaleUtils.NO_LANGUAGE.equals(localeStr)) { // If we don't have a locale, insert into the "all locales" user dictionary. mLocale = USER_DICTIONARY_ALL_LANGUAGES; } else { - mLocale = locale; + mLocale = localeStr; } mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales; - // Perform a managed query. The Activity will handle closing and re-querying the cursor - // when needed. ContentResolver cres = context.getContentResolver(); mObserver = new ContentObserver(null) { @@ -108,12 +114,11 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { // devices. On older versions of the platform, the hook above will be called instead. @Override public void onChange(final boolean self, final Uri uri) { - setRequiresReload(true); + setNeedsToReload(); } }; cres.registerContentObserver(Words.CONTENT_URI, true, mObserver); - - loadDictionary(); + reloadDictionaryIfRequired(); } @Override @@ -126,7 +131,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { } @Override - public void loadDictionaryAsync() { + public void loadInitialContentsLocked() { // Split the locale. For example "en" => ["en"], "de_DE" => ["de", "DE"], // "en_US_foo_bar_qux" => ["en", "US", "foo_bar_qux"] because of the limit of 3. // This is correct for locale processing. @@ -178,7 +183,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { try { cursor = mContext.getContentResolver().query( Words.CONTENT_URI, PROJECTION_QUERY, request.toString(), requestArguments, null); - addWords(cursor); + addWordsLocked(cursor); } catch (final SQLiteException e) { Log.e(TAG, "SQLiteException in the remote User dictionary process.", e); } finally { @@ -190,8 +195,8 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { } } - public boolean isEnabled() { - final ContentResolver cr = mContext.getContentResolver(); + public static boolean isEnabled(final Context context) { + final ContentResolver cr = context.getContentResolver(); final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI); if (client != null) { client.release(); @@ -204,18 +209,15 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { /** * Adds a word to the user dictionary and makes it persistent. * + * @param context the context + * @param locale the locale * @param word the word to add. If the word is capitalized, then the dictionary will * recognize it as a capitalized word when searched. */ - public synchronized void addWordToUserDictionary(final String word) { + public static void addWordToUserDictionary(final Context context, final Locale locale, + final String word) { // Update the user dictionary provider - final Locale locale; - if (USER_DICTIONARY_ALL_LANGUAGES == mLocale) { - locale = null; - } else { - locale = LocaleUtils.constructLocaleFromString(mLocale); - } - UserDictionaryCompatUtils.addWord(mContext, word, + UserDictionaryCompatUtils.addWord(context, word, HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale); } @@ -232,7 +234,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { } } - private void addWords(final Cursor cursor) { + private void addWordsLocked(final Cursor cursor) { final boolean hasShortcutColumn = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; if (cursor == null) return; if (cursor.moveToFirst()) { @@ -246,12 +248,16 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency); // Safeguard against adding really long words. if (word.length() < MAX_WORD_LENGTH) { - super.addWord(word, null, adjustedFrequency, 0 /* shortcutFreq */, - false /* isNotAWord */); - } - if (null != shortcut && shortcut.length() < MAX_WORD_LENGTH) { - super.addWord(shortcut, word, adjustedFrequency, USER_DICT_SHORTCUT_FREQUENCY, - true /* isNotAWord */); + runGCIfRequiredLocked(true /* mindsBlockByGC */); + addWordDynamicallyLocked(word, adjustedFrequency, null /* shortcutTarget */, + 0 /* shortcutFreq */, false /* isNotAWord */, + false /* isBlacklisted */, BinaryDictionary.NOT_A_VALID_TIMESTAMP); + if (null != shortcut && shortcut.length() < MAX_WORD_LENGTH) { + runGCIfRequiredLocked(true /* mindsBlockByGC */); + addWordDynamicallyLocked(shortcut, adjustedFrequency, word, + USER_DICT_SHORTCUT_FREQUENCY, true /* isNotAWord */, + false /* isBlacklisted */, BinaryDictionary.NOT_A_VALID_TIMESTAMP); + } } cursor.moveToNext(); } @@ -259,12 +265,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { } @Override - protected boolean hasContentChanged() { - return true; - } - - @Override - protected boolean needsToReloadBeforeWriting() { + protected boolean haveContentsChanged() { return true; } } |