2009-03-13 22:11:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 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;
|
|
|
|
|
2011-07-13 08:29:06 +00:00
|
|
|
import android.content.ContentProviderClient;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
2011-12-07 06:53:37 +00:00
|
|
|
import android.content.Intent;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.database.ContentObserver;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.provider.UserDictionary.Words;
|
2011-09-13 12:42:16 +00:00
|
|
|
import android.text.TextUtils;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2011-08-04 03:08:22 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
|
|
|
|
2011-09-15 05:31:24 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2009-07-17 19:51:45 +00:00
|
|
|
public class UserDictionary extends ExpandableDictionary {
|
2011-09-13 12:42:16 +00:00
|
|
|
|
2010-12-16 03:19:09 +00:00
|
|
|
private static final String[] PROJECTION_QUERY = {
|
2009-03-13 22:11:42 +00:00
|
|
|
Words.WORD,
|
2010-12-16 03:19:09 +00:00
|
|
|
Words.FREQUENCY,
|
2009-03-13 22:11:42 +00:00
|
|
|
};
|
2011-09-13 12:42:16 +00:00
|
|
|
|
2011-12-07 06:53:37 +00:00
|
|
|
// This is not exported by the framework so we pretty much have to write it here verbatim
|
|
|
|
private static final String ACTION_USER_DICTIONARY_INSERT =
|
|
|
|
"com.android.settings.USER_DICTIONARY_INSERT";
|
2011-07-22 05:32:36 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
private ContentObserver mObserver;
|
2011-09-13 12:42:16 +00:00
|
|
|
final private String mLocale;
|
2011-09-15 05:31:24 +00:00
|
|
|
final private boolean mAlsoUseMoreRestrictiveLocales;
|
|
|
|
|
|
|
|
public UserDictionary(final Context context, final String locale) {
|
|
|
|
this(context, locale, false);
|
|
|
|
}
|
2010-02-24 03:01:43 +00:00
|
|
|
|
2011-09-15 05:31:24 +00:00
|
|
|
public UserDictionary(final Context context, final String locale,
|
|
|
|
final boolean alsoUseMoreRestrictiveLocales) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super(context, Suggest.DIC_USER);
|
2011-09-13 12:42:16 +00:00
|
|
|
if (null == locale) throw new NullPointerException(); // Catch the error earlier
|
2010-02-24 03:01:43 +00:00
|
|
|
mLocale = locale;
|
2011-09-15 05:31:24 +00:00
|
|
|
mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales;
|
2011-07-22 05:32:36 +00:00
|
|
|
// Perform a managed query. The Activity will handle closing and re-querying the cursor
|
2009-03-13 22:11:42 +00:00
|
|
|
// when needed.
|
|
|
|
ContentResolver cres = context.getContentResolver();
|
2011-07-22 05:32:36 +00:00
|
|
|
|
|
|
|
mObserver = new ContentObserver(null) {
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
|
|
|
public void onChange(boolean self) {
|
2010-03-10 19:39:06 +00:00
|
|
|
setRequiresReload(true);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2011-07-22 05:32:36 +00:00
|
|
|
};
|
|
|
|
cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
loadDictionary();
|
|
|
|
}
|
2010-03-10 19:39:06 +00:00
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
2009-03-13 22:11:42 +00:00
|
|
|
public synchronized void close() {
|
|
|
|
if (mObserver != null) {
|
2009-07-17 19:51:45 +00:00
|
|
|
getContext().getContentResolver().unregisterContentObserver(mObserver);
|
2009-03-13 22:11:42 +00:00
|
|
|
mObserver = null;
|
|
|
|
}
|
2010-03-10 19:39:06 +00:00
|
|
|
super.close();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-03-10 19:39:06 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadDictionaryAsync() {
|
2011-09-13 12:42:16 +00:00
|
|
|
// 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);
|
2011-09-15 05:31:24 +00:00
|
|
|
final int length = localeElements.length;
|
2011-09-13 12:42:16 +00:00
|
|
|
|
|
|
|
final StringBuilder request = new StringBuilder("(locale is NULL)");
|
|
|
|
String localeSoFar = "";
|
|
|
|
// At start, localeElements = ["en", "US", "POSIX"] ; localeSoFar = "" ;
|
|
|
|
// and request = "(locale is NULL)"
|
2011-09-15 05:31:24 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2011-09-13 12:42:16 +00:00
|
|
|
// 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=?)"
|
2011-09-15 05:31:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
final Cursor cursor = getContext().getContentResolver()
|
2011-09-13 12:42:16 +00:00
|
|
|
.query(Words.CONTENT_URI, PROJECTION_QUERY, request.toString(),
|
2011-09-15 05:31:24 +00:00
|
|
|
requestArguments, null);
|
2011-11-18 06:24:49 +00:00
|
|
|
try {
|
|
|
|
addWords(cursor);
|
|
|
|
} finally {
|
|
|
|
if (null != cursor) cursor.close();
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 05:32:36 +00:00
|
|
|
public boolean isEnabled() {
|
|
|
|
final ContentResolver cr = getContext().getContentResolver();
|
|
|
|
final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI);
|
|
|
|
if (client != null) {
|
|
|
|
client.release();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Adds a word to the 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.
|
|
|
|
* @param frequency the frequency of occurrence of the word. A frequency of 255 is considered
|
|
|
|
* the highest.
|
|
|
|
* @TODO use a higher or float range for frequency
|
|
|
|
*/
|
2009-07-17 19:51:45 +00:00
|
|
|
@Override
|
2010-12-13 02:37:28 +00:00
|
|
|
public synchronized void addWord(final String word, final int frequency) {
|
2010-03-10 19:39:06 +00:00
|
|
|
// Force load the dictionary here synchronously
|
|
|
|
if (getRequiresReload()) loadDictionaryAsync();
|
2011-12-07 06:53:37 +00:00
|
|
|
// TODO: do something for the UI. With the following, any sufficiently long word will
|
|
|
|
// look like it will go to the user dictionary but it won't.
|
2009-03-13 22:11:42 +00:00
|
|
|
// Safeguard against adding long words. Can cause stack overflow.
|
2009-07-17 19:51:45 +00:00
|
|
|
if (word.length() >= getMaxWordLength()) return;
|
|
|
|
|
2011-12-07 06:53:37 +00:00
|
|
|
// TODO: Add an argument to the intent to specify the frequency.
|
|
|
|
Intent intent = new Intent(ACTION_USER_DICTIONARY_INSERT);
|
|
|
|
intent.putExtra(Words.WORD, word);
|
|
|
|
intent.putExtra(Words.LOCALE, mLocale);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
getContext().startActivity(intent);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-04-17 03:43:53 +00:00
|
|
|
public synchronized void getWords(final WordComposer codes,
|
|
|
|
final CharSequence prevWordForBigrams, final WordCallback callback,
|
2011-08-04 03:08:22 +00:00
|
|
|
final ProximityInfo proximityInfo) {
|
2012-04-17 03:43:53 +00:00
|
|
|
super.getWords(codes, prevWordForBigrams, callback, proximityInfo);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized boolean isValidWord(CharSequence word) {
|
2009-07-17 19:51:45 +00:00
|
|
|
return super.isValidWord(word);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addWords(Cursor cursor) {
|
2009-07-17 19:51:45 +00:00
|
|
|
clearDictionary();
|
2011-02-14 14:55:16 +00:00
|
|
|
if (cursor == null) return;
|
2009-07-17 19:51:45 +00:00
|
|
|
final int maxWordLength = getMaxWordLength();
|
2009-03-13 22:11:42 +00:00
|
|
|
if (cursor.moveToFirst()) {
|
2010-12-16 03:19:09 +00:00
|
|
|
final int indexWord = cursor.getColumnIndex(Words.WORD);
|
|
|
|
final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
|
2009-03-13 22:11:42 +00:00
|
|
|
while (!cursor.isAfterLast()) {
|
2010-12-16 03:19:09 +00:00
|
|
|
String word = cursor.getString(indexWord);
|
|
|
|
int frequency = cursor.getInt(indexFrequency);
|
2009-03-13 22:11:42 +00:00
|
|
|
// Safeguard against adding really long words. Stack may overflow due
|
|
|
|
// to recursion
|
2009-07-17 19:51:45 +00:00
|
|
|
if (word.length() < maxWordLength) {
|
|
|
|
super.addWord(word, frequency);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
cursor.moveToNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|