Created
May 15, 2012 20:41
-
-
Save codebutler/2704981 to your computer and use it in GitHub Desktop.
The missing ContentProvider base class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.*; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.database.sqlite.SQLiteQueryBuilder; | |
import android.net.Uri; | |
import android.provider.BaseColumns; | |
import android.text.TextUtils; | |
public abstract class BetterContentProvider extends ContentProvider { | |
private SQLiteOpenHelper mHelper; | |
private Class<SQLiteOpenHelper> mHelperClass; | |
private String mItemType; | |
private Uri mContentUri; | |
private String mDirType; | |
private String mTableName; | |
private UriMatcher mUriMatcher; | |
private static final int CODE_COLLECTION = 100; | |
private static final int CODE_SINGLE = 101; | |
public BetterContentProvider(Class helperClass, String dirType, String itemType, String tableName, Uri contentUri) { | |
mHelperClass = helperClass; | |
mDirType = dirType; | |
mItemType = itemType; | |
mTableName = tableName; | |
mContentUri = contentUri; | |
String basePath = contentUri.getPath().substring(1); | |
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); | |
mUriMatcher.addURI(contentUri.getAuthority(), basePath, CODE_COLLECTION); | |
mUriMatcher.addURI(contentUri.getAuthority(), basePath + "/#", CODE_SINGLE); | |
} | |
@Override | |
public boolean onCreate() { | |
try { | |
mHelper = mHelperClass.getConstructor(Context.class).newInstance(getContext()); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
return true; | |
} | |
@Override | |
public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | |
SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); | |
builder.setTables(mTableName); | |
switch (mUriMatcher.match(uri)) { | |
case CODE_COLLECTION: | |
// Nothing needed here | |
break; | |
case CODE_SINGLE: | |
builder.appendWhere(BaseColumns._ID + "=" + uri.getPathSegments().get(1)); | |
break; | |
default: | |
throw new IllegalArgumentException("Unknown URI: " + uri); | |
} | |
SQLiteDatabase db = mHelper.getReadableDatabase(); | |
Cursor cursor = builder.query(db, null, selection, selectionArgs, null, null, sortOrder); | |
cursor.setNotificationUri(getContext().getContentResolver(), uri); | |
return cursor; | |
} | |
@Override | |
public String getType(Uri uri) { | |
switch (mUriMatcher.match(uri)) { | |
case CODE_COLLECTION: | |
return mDirType; | |
case CODE_SINGLE: | |
return mItemType; | |
default: | |
throw new IllegalArgumentException("Unknown URI: " + uri); | |
} | |
} | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
if (mUriMatcher.match(uri) != CODE_COLLECTION) { | |
throw new IllegalArgumentException("Incorrect URI: " + uri); | |
} | |
SQLiteDatabase db = mHelper.getWritableDatabase(); | |
long rowId = db.insertOrThrow(mTableName, null, values); | |
Uri itemUri = ContentUris.withAppendedId(mContentUri, rowId); | |
getContext().getContentResolver().notifyChange(itemUri, null); | |
return itemUri; | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
SQLiteDatabase db = mHelper.getWritableDatabase(); | |
int count = 0; | |
switch (mUriMatcher.match(uri)) { | |
case CODE_SINGLE: | |
String rowId = uri.getPathSegments().get(1); | |
if (TextUtils.isEmpty(selection)) { | |
count = db.delete(mTableName, BaseColumns._ID + "=" + rowId, null); | |
} else { | |
count = db.delete(mTableName, | |
selection + " AND " + BaseColumns._ID + "=" + rowId, | |
selectionArgs); | |
} | |
break; | |
case CODE_COLLECTION: | |
count = db.delete(mTableName, selection, selectionArgs); | |
break; | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return count; | |
} | |
@Override | |
public int update (Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
SQLiteDatabase db = mHelper.getWritableDatabase(); | |
int count = 0; | |
switch (mUriMatcher.match(uri)) { | |
case CODE_COLLECTION: | |
count = db.update(mTableName, values, selection, selectionArgs); | |
break; | |
case CODE_SINGLE: | |
String rowId = uri.getPathSegments().get(1); | |
if (TextUtils.isEmpty(selection)) { | |
count = db.update(mTableName, values, BaseColumns._ID + "=" + rowId, null); | |
} else { | |
count = db.update(mTableName, values, | |
selection + " AND " + BaseColumns._ID + "=" + rowId, | |
selectionArgs); | |
} | |
break; | |
default: | |
throw new IllegalArgumentException("Unknown URI: " + uri); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment