FileDocCategorySizeDatePackage
UserDictionaryProvider.javaAPI DocAndroid 1.5 API8337Wed May 06 22:42:48 BST 2009com.android.providers.userdictionary

UserDictionaryProvider

public class UserDictionaryProvider extends android.content.ContentProvider
Provides access to a database of user defined words. Each item has a word and a frequency.

Fields Summary
private static final String
AUTHORITY
private static final String
TAG
private static final android.net.Uri
CONTENT_URI
private static final String
DATABASE_NAME
private static final int
DATABASE_VERSION
private static final String
USERDICT_TABLE_NAME
private static HashMap
sDictProjectionMap
private static final android.content.UriMatcher
sUriMatcher
private static final int
WORDS
private static final int
WORD_ID
private DatabaseHelper
mOpenHelper
Constructors Summary
Methods Summary
public intdelete(android.net.Uri uri, java.lang.String where, java.lang.String[] whereArgs)

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case WORDS:
            count = db.delete(USERDICT_TABLE_NAME, where, whereArgs);
            break;

        case WORD_ID:
            String wordId = uri.getPathSegments().get(1);
            count = db.delete(USERDICT_TABLE_NAME, Words._ID + "=" + wordId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')" : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    
public java.lang.StringgetType(android.net.Uri uri)

        switch (sUriMatcher.match(uri)) {
        case WORDS:
            return Words.CONTENT_TYPE;

        case WORD_ID:
            return Words.CONTENT_ITEM_TYPE;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    
public android.net.Uriinsert(android.net.Uri uri, android.content.ContentValues initialValues)

        // Validate the requested uri
        if (sUriMatcher.match(uri) != WORDS) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        if (values.containsKey(Words.WORD) == false) {
            throw new SQLException("Word must be specified");
        }

        if (values.containsKey(Words.FREQUENCY) == false) {
            values.put(Words.FREQUENCY, "1");
        }

        if (values.containsKey(Words.LOCALE) == false) {
            values.put(Words.LOCALE, (String) null);
        }
        
        values.put(Words.APP_ID, 0);

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(USERDICT_TABLE_NAME, Words.WORD, values);
        if (rowId > 0) {
            Uri wordUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(wordUri, null);
            return wordUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    
public booleanonCreate()

        mOpenHelper = new DatabaseHelper(getContext());
        return true;
    
public android.database.Cursorquery(android.net.Uri uri, java.lang.String[] projection, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sortOrder)

        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        switch (sUriMatcher.match(uri)) {
        case WORDS:
            qb.setTables(USERDICT_TABLE_NAME);
            qb.setProjectionMap(sDictProjectionMap);
            break;

        case WORD_ID:
            qb.setTables(USERDICT_TABLE_NAME);
            qb.setProjectionMap(sDictProjectionMap);
            qb.appendWhere("_id" + "=" + uri.getPathSegments().get(1));
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // If no sort order is specified use the default
        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = Words.DEFAULT_SORT_ORDER;
        } else {
            orderBy = sortOrder;
        }

        // Get the database and run the query
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    
public intupdate(android.net.Uri uri, android.content.ContentValues values, java.lang.String where, java.lang.String[] whereArgs)

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case WORDS:
            count = db.update(USERDICT_TABLE_NAME, values, where, whereArgs);
            break;

        case WORD_ID:
            String wordId = uri.getPathSegments().get(1);
            count = db.update(USERDICT_TABLE_NAME, values, Words._ID + "=" + wordId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')" : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;