1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| public class PeopleProvider extends ContentProvider {
private SQLiteDatabase db; private DBOpenHelper dbOpenHelper;
private static final int MULTIPLE_PEOPLE = 1; private static final int SINGLE_PEOPLE = 2; private static final UriMatcher uriMatcher;
static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(People.AUTHORITY, People.PATH_MULTIPLE, MULTIPLE_PEOPLE); uriMatcher.addURI(People.AUTHORITY, People.PATH_SINGLE, SINGLE_PEOPLE); }
@Override public boolean onCreate() { Context context = getContext(); dbOpenHelper = new DBOpenHelper(context, DBOpenHelper.DB_NAME, null, DBOpenHelper.DB_VERSION); db = dbOpenHelper.getWritableDatabase(); if (db == null) { return false; } else { return true; } }
@Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(DBOpenHelper.DB_TABLE); switch (uriMatcher.match(uri)) { case SINGLE_PEOPLE: qb.appendWhere(People.KEY_ID + "=" + uri.getPathSegments().get(1)); break; default: break; } Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
@Nullable @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)) { case MULTIPLE_PEOPLE: return People.MIME_TYPE_MULTIPLE; case SINGLE_PEOPLE: return People.MIME_TYPE_SINGLE; default: throw new IllegalArgumentException("Unkown uro:" + uri); } }
@Nullable @Override public Uri insert(Uri uri, ContentValues values) { long id = db.insert(DBOpenHelper.DB_TABLE, null, values); if (id > 0) { Uri newUri = ContentUris.withAppendedId(People.CONTENT_URI, id); getContext().getContentResolver().notifyChange(newUri, null); return newUri; } throw new SQLException("failed to insert row into " + uri); }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; switch (uriMatcher.match(uri)) { case MULTIPLE_PEOPLE: count = db.delete(DBOpenHelper.DB_TABLE, selection, selectionArgs); break; case SINGLE_PEOPLE: String segment = uri.getPathSegments().get(1); count = db.delete(DBOpenHelper.DB_TABLE, People.KEY_ID + "=" + segment, selectionArgs); break; default: throw new IllegalArgumentException("Unsupported URI:" + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count; switch (uriMatcher.match(uri)) { case MULTIPLE_PEOPLE: count = db.update(DBOpenHelper.DB_TABLE, values, selection, selectionArgs); break; case SINGLE_PEOPLE: String segment = uri.getPathSegments().get(1); count = db.update(DBOpenHelper.DB_TABLE, values, People.KEY_ID + "=" + segment, selectionArgs); break; default: throw new IllegalArgumentException("Unknow URI: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; }
}
|