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;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
2010-02-24 03:01:43 +00:00
|
|
|
import android.content.ContentValues;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.ContentObserver;
|
|
|
|
import android.database.Cursor;
|
2010-12-13 02:37:28 +00:00
|
|
|
import android.net.Uri;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.provider.UserDictionary.Words;
|
|
|
|
|
2009-07-17 19:51:45 +00:00
|
|
|
public class UserDictionary extends ExpandableDictionary {
|
2009-03-13 22:11:42 +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
|
|
|
};
|
|
|
|
|
2010-12-16 03:19:09 +00:00
|
|
|
private static final String[] PROJECTION_ADD = {
|
|
|
|
Words._ID,
|
|
|
|
Words.FREQUENCY,
|
|
|
|
Words.LOCALE,
|
|
|
|
};
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
private ContentObserver mObserver;
|
2010-02-24 03:01:43 +00:00
|
|
|
private String mLocale;
|
|
|
|
|
|
|
|
public UserDictionary(Context context, String locale) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super(context, Suggest.DIC_USER);
|
2010-02-24 03:01:43 +00:00
|
|
|
mLocale = locale;
|
2009-03-13 22:11:42 +00:00
|
|
|
// Perform a managed query. The Activity will handle closing and requerying the cursor
|
|
|
|
// when needed.
|
|
|
|
ContentResolver cres = context.getContentResolver();
|
|
|
|
|
|
|
|
cres.registerContentObserver(Words.CONTENT_URI, true, mObserver = new ContentObserver(null) {
|
|
|
|
@Override
|
|
|
|
public void onChange(boolean self) {
|
2010-03-10 19:39:06 +00:00
|
|
|
setRequiresReload(true);
|
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() {
|
2009-07-17 19:51:45 +00:00
|
|
|
Cursor cursor = getContext().getContentResolver()
|
2010-12-16 03:19:09 +00:00
|
|
|
.query(Words.CONTENT_URI, PROJECTION_QUERY, "(locale IS NULL) or (locale=?)",
|
2010-02-24 03:01:43 +00:00
|
|
|
new String[] { mLocale }, null);
|
2009-03-13 22:11:42 +00:00
|
|
|
addWords(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
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;
|
|
|
|
|
|
|
|
super.addWord(word, frequency);
|
|
|
|
|
2010-02-24 03:01:43 +00:00
|
|
|
// Update the user dictionary provider
|
2010-09-16 10:14:13 +00:00
|
|
|
final ContentValues values = new ContentValues(5);
|
2010-02-24 03:01:43 +00:00
|
|
|
values.put(Words.WORD, word);
|
|
|
|
values.put(Words.FREQUENCY, frequency);
|
|
|
|
values.put(Words.LOCALE, mLocale);
|
|
|
|
values.put(Words.APP_ID, 0);
|
|
|
|
|
2010-09-16 10:14:13 +00:00
|
|
|
final ContentResolver contentResolver = getContext().getContentResolver();
|
|
|
|
new Thread("addWord") {
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-09-16 10:14:13 +00:00
|
|
|
public void run() {
|
2010-12-16 03:19:09 +00:00
|
|
|
Cursor cursor = contentResolver.query(Words.CONTENT_URI, PROJECTION_ADD,
|
2010-12-13 02:37:28 +00:00
|
|
|
"word=? and ((locale IS NULL) or (locale=?))",
|
|
|
|
new String[] { word, mLocale }, null);
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
String locale = cursor.getString(cursor.getColumnIndex(Words.LOCALE));
|
|
|
|
// If locale is null, we will not override the entry.
|
|
|
|
if (locale != null && locale.equals(mLocale.toString())) {
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndex(Words._ID));
|
|
|
|
Uri uri = Uri.withAppendedPath(Words.CONTENT_URI, Long.toString(id));
|
|
|
|
// Update the entry with new frequency value.
|
|
|
|
contentResolver.update(uri, values, null, null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Insert new entry.
|
|
|
|
contentResolver.insert(Words.CONTENT_URI, values);
|
|
|
|
}
|
2010-09-16 10:14:13 +00:00
|
|
|
}
|
|
|
|
}.start();
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
// In case the above does a synchronous callback of the change observer
|
2010-03-10 19:39:06 +00:00
|
|
|
setRequiresReload(false);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-02-05 22:07:04 +00:00
|
|
|
public synchronized void getWords(final WordComposer codes, final WordCallback callback,
|
|
|
|
int[] nextLettersFrequencies) {
|
|
|
|
super.getWords(codes, callback, nextLettersFrequencies);
|
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();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|