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 java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.ContentObserver;
|
|
|
|
import android.database.Cursor;
|
|
|
|
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
|
|
|
|
|
|
|
private static final String[] PROJECTION = {
|
|
|
|
Words._ID,
|
|
|
|
Words.WORD,
|
|
|
|
Words.FREQUENCY
|
|
|
|
};
|
|
|
|
|
|
|
|
private static final int INDEX_WORD = 1;
|
|
|
|
private static final int INDEX_FREQUENCY = 2;
|
|
|
|
|
|
|
|
private ContentObserver mObserver;
|
|
|
|
|
|
|
|
private boolean mRequiresReload;
|
|
|
|
|
|
|
|
public UserDictionary(Context context) {
|
2009-07-17 19:51:45 +00:00
|
|
|
super(context);
|
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) {
|
|
|
|
mRequiresReload = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
loadDictionary();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void loadDictionary() {
|
2009-07-17 19:51:45 +00:00
|
|
|
Cursor cursor = getContext().getContentResolver()
|
2009-03-13 22:11:42 +00:00
|
|
|
.query(Words.CONTENT_URI, PROJECTION, "(locale IS NULL) or (locale=?)",
|
|
|
|
new String[] { Locale.getDefault().toString() }, null);
|
|
|
|
addWords(cursor);
|
|
|
|
mRequiresReload = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2009-03-13 22:11:42 +00:00
|
|
|
public synchronized void addWord(String word, int frequency) {
|
|
|
|
if (mRequiresReload) loadDictionary();
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
Words.addWord(getContext(), word, frequency, Words.LOCALE_TYPE_CURRENT);
|
2009-03-13 22:11:42 +00:00
|
|
|
// In case the above does a synchronous callback of the change observer
|
|
|
|
mRequiresReload = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-02-05 22:07:04 +00:00
|
|
|
public synchronized void getWords(final WordComposer codes, final WordCallback callback,
|
|
|
|
int[] nextLettersFrequencies) {
|
2009-03-13 22:11:42 +00:00
|
|
|
if (mRequiresReload) loadDictionary();
|
2010-02-05 22:07:04 +00:00
|
|
|
super.getWords(codes, callback, nextLettersFrequencies);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized boolean isValidWord(CharSequence word) {
|
|
|
|
if (mRequiresReload) loadDictionary();
|
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()) {
|
|
|
|
while (!cursor.isAfterLast()) {
|
|
|
|
String word = cursor.getString(INDEX_WORD);
|
|
|
|
int frequency = cursor.getInt(INDEX_FREQUENCY);
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|