Fix crash on adding a word in the user dictionary.

When the User dictionary service is off, LatinIME still allows to add
words to it and it crashed if the user tries to do so. This change
fixes the crash; it does not however fix the interface.

Bug: 5004408
Change-Id: I5bd2684ab3010963c1f737fc764e70e79aadbe8d
This commit is contained in:
Jean Chalard 2011-07-13 17:29:06 +09:00
parent 3e41c071e6
commit 092826c8cd

View file

@ -16,12 +16,14 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.ContentProviderClient;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentValues; 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.net.Uri;
import android.os.RemoteException;
import android.provider.UserDictionary.Words; import android.provider.UserDictionary.Words;
public class UserDictionary extends ExpandableDictionary { public class UserDictionary extends ExpandableDictionary {
@ -99,24 +101,34 @@ public class UserDictionary extends ExpandableDictionary {
values.put(Words.APP_ID, 0); values.put(Words.APP_ID, 0);
final ContentResolver contentResolver = getContext().getContentResolver(); final ContentResolver contentResolver = getContext().getContentResolver();
final ContentProviderClient client =
contentResolver.acquireContentProviderClient(Words.CONTENT_URI);
if (null == client) return;
new Thread("addWord") { new Thread("addWord") {
@Override @Override
public void run() { public void run() {
Cursor cursor = contentResolver.query(Words.CONTENT_URI, PROJECTION_ADD, try {
"word=? and ((locale IS NULL) or (locale=?))", final Cursor cursor = client.query(Words.CONTENT_URI, PROJECTION_ADD,
new String[] { word, mLocale }, null); "word=? and ((locale IS NULL) or (locale=?))",
if (cursor != null && cursor.moveToFirst()) { new String[] { word, mLocale }, null);
String locale = cursor.getString(cursor.getColumnIndex(Words.LOCALE)); if (cursor != null && cursor.moveToFirst()) {
// If locale is null, we will not override the entry. final String locale = cursor.getString(cursor.getColumnIndex(Words.LOCALE));
if (locale != null && locale.equals(mLocale.toString())) { // If locale is null, we will not override the entry.
long id = cursor.getLong(cursor.getColumnIndex(Words._ID)); if (locale != null && locale.equals(mLocale.toString())) {
Uri uri = Uri.withAppendedPath(Words.CONTENT_URI, Long.toString(id)); final long id = cursor.getLong(cursor.getColumnIndex(Words._ID));
// Update the entry with new frequency value. final Uri uri =
contentResolver.update(uri, values, null, null); Uri.withAppendedPath(Words.CONTENT_URI, Long.toString(id));
// Update the entry with new frequency value.
client.update(uri, values, null, null);
}
} else {
// Insert new entry.
client.insert(Words.CONTENT_URI, values);
} }
} else { } catch (RemoteException e) {
// Insert new entry. // If we come here, the activity is already about to be killed, and we
contentResolver.insert(Words.CONTENT_URI, values); // have no means of contacting the content provider any more.
// See ContentResolver#insert, inside the catch(){}
} }
} }
}.start(); }.start();