2012-04-23 17:45:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.ContentProviderClient;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.ContentObserver;
|
|
|
|
import android.database.Cursor;
|
2013-09-20 10:31:39 +00:00
|
|
|
import android.database.sqlite.SQLiteException;
|
2012-12-13 12:59:13 +00:00
|
|
|
import android.net.Uri;
|
2013-01-06 02:10:27 +00:00
|
|
|
import android.os.Build;
|
2012-04-23 17:45:48 +00:00
|
|
|
import android.provider.UserDictionary.Words;
|
|
|
|
import android.text.TextUtils;
|
2013-09-20 10:31:39 +00:00
|
|
|
import android.util.Log;
|
2012-04-23 17:45:48 +00:00
|
|
|
|
2013-04-12 06:58:18 +00:00
|
|
|
import com.android.inputmethod.compat.UserDictionaryCompatUtils;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.LocaleUtils;
|
2013-07-24 01:37:07 +00:00
|
|
|
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
|
2013-04-12 06:58:18 +00:00
|
|
|
|
2013-12-13 05:48:43 +00:00
|
|
|
import java.io.File;
|
2012-04-23 17:45:48 +00:00
|
|
|
import java.util.Arrays;
|
2013-04-12 06:58:18 +00:00
|
|
|
import java.util.Locale;
|
2012-04-23 17:45:48 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-07 10:15:33 +00:00
|
|
|
* An expandable dictionary that stores the words in the user dictionary provider into a binary
|
|
|
|
* dictionary file to use it from native code.
|
2012-04-23 17:45:48 +00:00
|
|
|
*/
|
|
|
|
public class UserBinaryDictionary extends ExpandableBinaryDictionary {
|
2013-09-20 10:31:39 +00:00
|
|
|
private static final String TAG = ExpandableBinaryDictionary.class.getSimpleName();
|
2012-04-23 17:45:48 +00:00
|
|
|
|
2012-07-02 08:12:56 +00:00
|
|
|
// The user dictionary provider uses an empty string to mean "all languages".
|
|
|
|
private static final String USER_DICTIONARY_ALL_LANGUAGES = "";
|
2013-01-07 10:15:33 +00:00
|
|
|
private static final int HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY = 250;
|
|
|
|
private static final int LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY = 160;
|
2013-10-04 14:26:18 +00:00
|
|
|
// Shortcut frequency is 0~15, with 15 = whitelist. We don't want user dictionary entries
|
|
|
|
// to auto-correct, so we set this to the highest frequency that won't, i.e. 14.
|
|
|
|
private static final int USER_DICT_SHORTCUT_FREQUENCY = 14;
|
2012-07-02 08:12:56 +00:00
|
|
|
|
|
|
|
// TODO: use Words.SHORTCUT when we target JellyBean or above
|
2012-04-23 17:45:48 +00:00
|
|
|
final static String SHORTCUT = "shortcut";
|
2012-06-20 23:49:04 +00:00
|
|
|
private static final String[] PROJECTION_QUERY;
|
|
|
|
static {
|
2013-01-06 02:10:27 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
2012-06-20 23:49:04 +00:00
|
|
|
PROJECTION_QUERY = new String[] {
|
|
|
|
Words.WORD,
|
|
|
|
SHORTCUT,
|
|
|
|
Words.FREQUENCY,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
PROJECTION_QUERY = new String[] {
|
|
|
|
Words.WORD,
|
|
|
|
Words.FREQUENCY,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 17:45:48 +00:00
|
|
|
|
|
|
|
private static final String NAME = "userunigram";
|
|
|
|
|
|
|
|
private ContentObserver mObserver;
|
|
|
|
final private String mLocale;
|
|
|
|
final private boolean mAlsoUseMoreRestrictiveLocales;
|
2013-12-20 10:05:33 +00:00
|
|
|
final public boolean mEnabled;
|
2012-04-23 17:45:48 +00:00
|
|
|
|
2013-12-03 10:22:17 +00:00
|
|
|
public UserBinaryDictionary(final Context context, final Locale locale) {
|
2014-02-13 03:12:13 +00:00
|
|
|
this(context, locale, false /* alsoUseMoreRestrictiveLocales */, null /* dictFile */);
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 03:12:13 +00:00
|
|
|
public UserBinaryDictionary(final Context context, final Locale locale, final File dictFile) {
|
|
|
|
this(context, locale, false /* alsoUseMoreRestrictiveLocales */, dictFile);
|
2013-12-13 05:48:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 10:22:17 +00:00
|
|
|
public UserBinaryDictionary(final Context context, final Locale locale,
|
2014-02-13 03:12:13 +00:00
|
|
|
final boolean alsoUseMoreRestrictiveLocales, final File dictFile) {
|
|
|
|
super(context, getDictName(NAME, locale, dictFile), locale, Dictionary.TYPE_USER,
|
|
|
|
false /* isUpdatable */, dictFile);
|
2012-04-23 17:45:48 +00:00
|
|
|
if (null == locale) throw new NullPointerException(); // Catch the error earlier
|
2013-12-03 10:22:17 +00:00
|
|
|
final String localeStr = locale.toString();
|
|
|
|
if (SubtypeLocaleUtils.NO_LANGUAGE.equals(localeStr)) {
|
2012-07-02 08:12:56 +00:00
|
|
|
// If we don't have a locale, insert into the "all locales" user dictionary.
|
|
|
|
mLocale = USER_DICTIONARY_ALL_LANGUAGES;
|
|
|
|
} else {
|
2013-12-03 10:22:17 +00:00
|
|
|
mLocale = localeStr;
|
2012-07-02 08:12:56 +00:00
|
|
|
}
|
2012-04-23 17:45:48 +00:00
|
|
|
mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales;
|
|
|
|
ContentResolver cres = context.getContentResolver();
|
|
|
|
|
|
|
|
mObserver = new ContentObserver(null) {
|
|
|
|
@Override
|
2012-12-13 12:59:13 +00:00
|
|
|
public void onChange(final boolean self) {
|
2013-01-06 02:10:27 +00:00
|
|
|
// 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.
|
2012-12-13 12:59:13 +00:00
|
|
|
onChange(self, null);
|
|
|
|
}
|
2013-01-06 02:10:27 +00:00
|
|
|
// 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.
|
2012-12-13 12:59:13 +00:00
|
|
|
@Override
|
|
|
|
public void onChange(final boolean self, final Uri uri) {
|
2012-04-23 17:45:48 +00:00
|
|
|
setRequiresReload(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
|
2013-12-20 10:05:33 +00:00
|
|
|
mEnabled = readIsEnabled();
|
2012-04-23 17:45:48 +00:00
|
|
|
loadDictionary();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void close() {
|
|
|
|
if (mObserver != null) {
|
|
|
|
mContext.getContentResolver().unregisterContentObserver(mObserver);
|
|
|
|
mObserver = null;
|
|
|
|
}
|
|
|
|
super.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadDictionaryAsync() {
|
|
|
|
// 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.
|
|
|
|
// For this example, we'll look at the "en_US_POSIX" case.
|
|
|
|
final String[] localeElements =
|
|
|
|
TextUtils.isEmpty(mLocale) ? new String[] {} : mLocale.split("_", 3);
|
|
|
|
final int length = localeElements.length;
|
|
|
|
|
|
|
|
final StringBuilder request = new StringBuilder("(locale is NULL)");
|
|
|
|
String localeSoFar = "";
|
|
|
|
// At start, localeElements = ["en", "US", "POSIX"] ; localeSoFar = "" ;
|
|
|
|
// and request = "(locale is NULL)"
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
// i | localeSoFar | localeElements
|
|
|
|
// 0 | "" | ["en", "US", "POSIX"]
|
|
|
|
// 1 | "en_" | ["en", "US", "POSIX"]
|
|
|
|
// 2 | "en_US_" | ["en", "en_US", "POSIX"]
|
|
|
|
localeElements[i] = localeSoFar + localeElements[i];
|
|
|
|
localeSoFar = localeElements[i] + "_";
|
|
|
|
// i | request
|
|
|
|
// 0 | "(locale is NULL)"
|
|
|
|
// 1 | "(locale is NULL) or (locale=?)"
|
|
|
|
// 2 | "(locale is NULL) or (locale=?) or (locale=?)"
|
|
|
|
request.append(" or (locale=?)");
|
|
|
|
}
|
|
|
|
// At the end, localeElements = ["en", "en_US", "en_US_POSIX"]; localeSoFar = en_US_POSIX_"
|
|
|
|
// and request = "(locale is NULL) or (locale=?) or (locale=?) or (locale=?)"
|
|
|
|
|
|
|
|
final String[] requestArguments;
|
|
|
|
// If length == 3, we already have all the arguments we need (common prefix is meaningless
|
|
|
|
// inside variants
|
|
|
|
if (mAlsoUseMoreRestrictiveLocales && length < 3) {
|
|
|
|
request.append(" or (locale like ?)");
|
|
|
|
// The following creates an array with one more (null) position
|
|
|
|
final String[] localeElementsWithMoreRestrictiveLocalesIncluded =
|
|
|
|
Arrays.copyOf(localeElements, length + 1);
|
|
|
|
localeElementsWithMoreRestrictiveLocalesIncluded[length] =
|
|
|
|
localeElements[length - 1] + "_%";
|
|
|
|
requestArguments = localeElementsWithMoreRestrictiveLocalesIncluded;
|
|
|
|
// If for example localeElements = ["en"]
|
|
|
|
// then requestArguments = ["en", "en_%"]
|
|
|
|
// and request = (locale is NULL) or (locale=?) or (locale like ?)
|
|
|
|
// If localeElements = ["en", "en_US"]
|
|
|
|
// then requestArguments = ["en", "en_US", "en_US_%"]
|
|
|
|
} else {
|
|
|
|
requestArguments = localeElements;
|
|
|
|
}
|
2013-09-20 10:31:39 +00:00
|
|
|
Cursor cursor = null;
|
2012-04-23 17:45:48 +00:00
|
|
|
try {
|
2013-09-20 10:31:39 +00:00
|
|
|
cursor = mContext.getContentResolver().query(
|
|
|
|
Words.CONTENT_URI, PROJECTION_QUERY, request.toString(), requestArguments, null);
|
2012-04-23 17:45:48 +00:00
|
|
|
addWords(cursor);
|
2013-09-20 10:31:39 +00:00
|
|
|
} catch (final SQLiteException e) {
|
|
|
|
Log.e(TAG, "SQLiteException in the remote User dictionary process.", e);
|
2012-04-23 17:45:48 +00:00
|
|
|
} finally {
|
2013-09-20 10:31:39 +00:00
|
|
|
try {
|
|
|
|
if (null != cursor) cursor.close();
|
|
|
|
} catch (final SQLiteException e) {
|
|
|
|
Log.e(TAG, "SQLiteException in the remote User dictionary process.", e);
|
|
|
|
}
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 10:05:33 +00:00
|
|
|
private boolean readIsEnabled() {
|
2012-04-23 17:45:48 +00:00
|
|
|
final ContentResolver cr = mContext.getContentResolver();
|
|
|
|
final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI);
|
|
|
|
if (client != null) {
|
|
|
|
client.release();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a word to the user dictionary and makes it persistent.
|
|
|
|
*
|
|
|
|
* @param word the word to add. If the word is capitalized, then the dictionary will
|
|
|
|
* recognize it as a capitalized word when searched.
|
|
|
|
*/
|
2013-01-16 13:07:43 +00:00
|
|
|
public synchronized void addWordToUserDictionary(final String word) {
|
2013-04-12 06:58:18 +00:00
|
|
|
// 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,
|
|
|
|
HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale);
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 10:15:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
private void addWords(final Cursor cursor) {
|
2013-01-06 02:10:27 +00:00
|
|
|
final boolean hasShortcutColumn = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
|
2012-04-23 17:45:48 +00:00
|
|
|
if (cursor == null) return;
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
final int indexWord = cursor.getColumnIndex(Words.WORD);
|
2012-06-20 23:49:04 +00:00
|
|
|
final int indexShortcut = hasShortcutColumn ? cursor.getColumnIndex(SHORTCUT) : 0;
|
2012-04-23 17:45:48 +00:00
|
|
|
final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
|
|
|
|
while (!cursor.isAfterLast()) {
|
2012-06-20 23:49:04 +00:00
|
|
|
final String word = cursor.getString(indexWord);
|
|
|
|
final String shortcut = hasShortcutColumn ? cursor.getString(indexShortcut) : null;
|
|
|
|
final int frequency = cursor.getInt(indexFrequency);
|
2013-01-07 10:15:33 +00:00
|
|
|
final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency);
|
2012-04-23 17:45:48 +00:00
|
|
|
// Safeguard against adding really long words.
|
|
|
|
if (word.length() < MAX_WORD_LENGTH) {
|
2013-10-04 14:26:18 +00:00
|
|
|
super.addWord(word, null, adjustedFrequency, 0 /* shortcutFreq */,
|
|
|
|
false /* isNotAWord */);
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
if (null != shortcut && shortcut.length() < MAX_WORD_LENGTH) {
|
2013-10-04 14:26:18 +00:00
|
|
|
super.addWord(shortcut, word, adjustedFrequency, USER_DICT_SHORTCUT_FREQUENCY,
|
|
|
|
true /* isNotAWord */);
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
cursor.moveToNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean hasContentChanged() {
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-04 12:06:04 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean needsToReloadBeforeWriting() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|