Skip to content

Instantly share code, notes, and snippets.

@saik0
Created November 15, 2012 15:14
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save saik0/4079112 to your computer and use it in GitHub Desktop.
Sample ContentProvider that performs all operations transactionally and notifies once in batch operations.
public class MyContentProvider extends ContentProvider {
private final ThreadLocal<Boolean> mApplyingBatch;
private final ThreadLocal<Set<Uri>> mChangedUris;
private boolean applyingBatch() {
return mApplyingBatch.get() != null && mApplyingBatch.get();
}
private Uri insert(final Uri uri, final ContentValues values, final SQLiteDatabase db) {
// do the uri matching and insert
return resultUri;
}
@Override
public Uri insert(final Uri uri, final ContentValues values) {
/*
* there are no observers on content that does not exist
* so we add the base uri to the set for batch operations
* for delete/update we notify with the id instead.
*/
final SQLiteDatabase db = getDatabase(getContext());
if (!applyingBatch()) {
mChangedUris.set(new HashSet<Uri>());
db.beginTransaction();
try {
Uri resultUri = insert(uri, values, db);
db.setTransactionSuccessful();
mChangedUris.get().add(uri);
return resultUri;
} finally {
db.endTransaction();
onOperationComplete();
}
} else {
Uri resultUri = insert(uri, values, db);
mChangedUris.get().add(uri);
return resultUri;
}
}
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
mChangedUris.set(new HashSet<Uri>());
db.beginTransaction();
try {
mApplyingBatch.set(true);
final int numOperations = operations.size();
final ContentProviderResult[] results = new ContentProviderResult[numOperations];
for (int i = 0; i < numOperations; i++) {
results[i] = operations.get(i).apply(this, results, i);
}
db.setTransactionSuccessful();
return results;
} finally {
db.endTransaction();
mApplyingBatch.set(false);
onOperationComplete();
}
}
private void onOperationComplete() {
for (Uri uri : mChangedUris.get()) {
getContext().getContentResolver().notifyChange(uri, null);
}
}
@Override
public int bulkInsert(final Uri uri, final ContentValues[] values) {
// for performance we dont want to call this.insert, better only match Uri once here
final SQLiteDatabase db = getDatabase(getContext());
int numberInserted = 0;
db.beginTransaction();
try {
// iterate through values and do your db.insert satements
} finally {
db.endTransaction();
}
// now just notify the base url
getContext().getContentResolver().notifyChange(uri, null);
return numberInserted;
}
}
@sagarbbpim
Copy link

Hey i have a qn about Content provider..
Can i know how to get all the files and folders from the SD card and Cloud file services? etc

@pankajgangwar
Copy link

Thanks dude. Your code really helped me and saved my time..!!

@adhigadu
Copy link

this is beautiful.. thanks ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment