FileDocCategorySizeDatePackage
NotePadProvider.javaAPI DocGoogle Android v1.5 Example7975Sun Nov 11 13:01:04 GMT 2007com.google.android.notepad

NotePadProvider

public class NotePadProvider extends android.content.ContentProvider
Provides access to a database of notes. Each note has a title, the note itself, a creation date and a modified data.

Fields Summary
private android.database.sqlite.SQLiteDatabase
mDB
private static final String
TAG
private static final String
DATABASE_NAME
private static final int
DATABASE_VERSION
private static HashMap
NOTES_LIST_PROJECTION_MAP
private static final int
NOTES
private static final int
NOTE_ID
private static final android.content.ContentURIParser
URL_MATCHER
Constructors Summary
Methods Summary
public intdelete(android.net.ContentURI url, java.lang.String where, java.lang.String[] whereArgs)

        int count;
        long rowId = 0;
        switch (URL_MATCHER.match(url)) {
        case NOTES:
            count = mDB.delete("note_pad", where, whereArgs);
            break;

        case NOTE_ID:
            String segment = url.getPathSegment(1);
            rowId = Long.parseLong(segment);
            count = mDB
                    .delete("notes", "_id="
                            + segment
                            + (!TextUtils.isEmpty(where) ? " AND (" + where
                                    + ')" : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }

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

        switch (URL_MATCHER.match(url)) {
        case NOTES:
            return "vnd.android.cursor.dir/vnd.google.note";

        case NOTE_ID:
            return "vnd.android.cursor.item/vnd.google.note";

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }
    
public android.net.ContentURIinsert(android.net.ContentURI url, android.content.ContentValues initialValues)

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

        if (URL_MATCHER.match(url) != NOTES) {
            throw new IllegalArgumentException("Unknown URL " + url);
        }

        Long now = Long.valueOf(System.currentTimeMillis());
        Resources r = Resources.getSystem();

        // Make sure that the fields are all set
        if (values.containsKey(NotePad.Notes.CREATED_DATE) == false) {
            values.put(NotePad.Notes.CREATED_DATE, now);
        }

        if (values.containsKey(NotePad.Notes.MODIFIED_DATE) == false) {
            values.put(NotePad.Notes.MODIFIED_DATE, now);
        }

        if (values.containsKey(NotePad.Notes.TITLE) == false) {
            values.put(NotePad.Notes.TITLE, r.getString(android.R.string.untitled));
        }

        if (values.containsKey(NotePad.Notes.NOTE) == false) {
            values.put(NotePad.Notes.NOTE, "");
        }

        rowID = mDB.insert("notes", "note", values);
        if (rowID > 0) {
            ContentURI uri = NotePad.Notes.CONTENT_URI.addId(rowID);
            getContext().getContentResolver().notifyChange(uri, null);
            return uri;
        }

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

        DatabaseHelper dbHelper = new DatabaseHelper();
        mDB = dbHelper.openDatabase(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
        return (mDB == null) ? false : true;
    
public android.database.Cursorquery(android.net.ContentURI url, java.lang.String[] projection, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String groupBy, java.lang.String having, java.lang.String sort)

        QueryBuilder qb = new QueryBuilder();

        switch (URL_MATCHER.match(url)) {
        case NOTES:
            qb.setTables("notes");
            qb.setProjectionMap(NOTES_LIST_PROJECTION_MAP);
            break;

        case NOTE_ID:
            qb.setTables("notes");
            qb.appendWhere("_id=" + url.getPathSegment(1));
            break;

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }

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

        Cursor c = qb.query(mDB, projection, selection, selectionArgs, groupBy,
                having, orderBy);
        c.setNotificationUri(getContext().getContentResolver(), url);
        return c;
    
public intupdate(android.net.ContentURI url, android.content.ContentValues values, java.lang.String where, java.lang.String[] whereArgs)

        int count;
        switch (URL_MATCHER.match(url)) {
        case NOTES:
            count = mDB.update("notes", values, where, whereArgs);
            break;

        case NOTE_ID:
            String segment = url.getPathSegment(1);
            count = mDB
                    .update("notes", values, "_id="
                            + segment
                            + (!TextUtils.isEmpty(where) ? " AND (" + where
                                    + ')" : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }

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