Check user dictionary before inserting new word
Bug: 3264920 Change-Id: I9fe45a61b2ad2b1ed69d3a0cbc6eebecb4038accmain
parent
9ecad8c2e8
commit
c5f1368090
|
@ -21,6 +21,7 @@ import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.net.Uri;
|
||||||
import android.provider.UserDictionary.Words;
|
import android.provider.UserDictionary.Words;
|
||||||
|
|
||||||
public class UserDictionary extends ExpandableDictionary {
|
public class UserDictionary extends ExpandableDictionary {
|
||||||
|
@ -80,7 +81,7 @@ public class UserDictionary extends ExpandableDictionary {
|
||||||
* @TODO use a higher or float range for frequency
|
* @TODO use a higher or float range for frequency
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized void addWord(String word, int frequency) {
|
public synchronized void addWord(final String word, final int frequency) {
|
||||||
// Force load the dictionary here synchronously
|
// Force load the dictionary here synchronously
|
||||||
if (getRequiresReload()) loadDictionaryAsync();
|
if (getRequiresReload()) loadDictionaryAsync();
|
||||||
// Safeguard against adding long words. Can cause stack overflow.
|
// Safeguard against adding long words. Can cause stack overflow.
|
||||||
|
@ -99,8 +100,23 @@ public class UserDictionary extends ExpandableDictionary {
|
||||||
new Thread("addWord") {
|
new Thread("addWord") {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
Cursor cursor = contentResolver.query(Words.CONTENT_URI, PROJECTION,
|
||||||
|
"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);
|
contentResolver.insert(Words.CONTENT_URI, values);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}.start();
|
}.start();
|
||||||
|
|
||||||
// In case the above does a synchronous callback of the change observer
|
// In case the above does a synchronous callback of the change observer
|
||||||
|
|
Loading…
Reference in New Issue