diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/UserBinaryDictionary.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/UserBinaryDictionary.java | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java index ddae5ac48..a16784985 100644 --- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java @@ -24,27 +24,28 @@ import android.content.Intent; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; +import android.os.Build; import android.provider.UserDictionary.Words; import android.text.TextUtils; import java.util.Arrays; /** - * An expandable dictionary that stores the words in the user unigram dictionary. - * - * Largely a copy of UserDictionary, will replace that class in the future. + * An expandable dictionary that stores the words in the user dictionary provider into a binary + * dictionary file to use it from native code. */ public class UserBinaryDictionary extends ExpandableBinaryDictionary { // The user dictionary provider uses an empty string to mean "all languages". private static final String USER_DICTIONARY_ALL_LANGUAGES = ""; + private static final int HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY = 250; + private static final int LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY = 160; // TODO: use Words.SHORTCUT when we target JellyBean or above final static String SHORTCUT = "shortcut"; private static final String[] PROJECTION_QUERY; static { - // 16 is JellyBean, but we want this to compile against ICS. - if (android.os.Build.VERSION.SDK_INT >= 16) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { PROJECTION_QUERY = new String[] { Words.WORD, SHORTCUT, @@ -90,13 +91,14 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { mObserver = new ContentObserver(null) { @Override public void onChange(final boolean self) { - // This hook is deprecated as of API level 16, but should still be supported for - // cases where the IME is running on an older version of the platform. + // This hook is deprecated as of API level 16 (Build.VERSION_CODES.JELLY_BEAN), + // but should still be supported for cases where the IME is running on an older + // version of the platform. onChange(self, null); } - // The following hook is only available as of API level 16, and as such it will only - // work on JellyBean+ devices. On older versions of the platform, the hook - // above will be called instead. + // The following hook is only available as of API level 16 + // (Build.VERSION_CODES.JELLY_BEAN), and as such it will only work on JellyBean+ + // 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); @@ -232,9 +234,21 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { mContext.startActivity(intent); } + private int scaleFrequencyFromDefaultToLatinIme(final int defaultFrequency) { + // The default frequency for the user dictionary is 250 for historical reasons. + // Latin IME considers a good value for the default user dictionary frequency + // is about 160 considering the scale we use. So we are scaling down the values. + if (defaultFrequency > Integer.MAX_VALUE / LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY) { + return (defaultFrequency / HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY) + * LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY; + } else { + return (defaultFrequency * LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY) + / HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY; + } + } + private void addWords(final Cursor cursor) { - // 16 is JellyBean, but we want this to compile against ICS. - final boolean hasShortcutColumn = android.os.Build.VERSION.SDK_INT >= 16; + final boolean hasShortcutColumn = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; clearFusionDictionary(); if (cursor == null) return; if (cursor.moveToFirst()) { @@ -245,12 +259,13 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { final String word = cursor.getString(indexWord); final String shortcut = hasShortcutColumn ? cursor.getString(indexShortcut) : null; final int frequency = cursor.getInt(indexFrequency); + final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency); // Safeguard against adding really long words. if (word.length() < MAX_WORD_LENGTH) { - super.addWord(word, null, frequency); + super.addWord(word, null, adjustedFrequency); } if (null != shortcut && shortcut.length() < MAX_WORD_LENGTH) { - super.addWord(shortcut, word, frequency); + super.addWord(shortcut, word, adjustedFrequency); } cursor.moveToNext(); } |