/** {@inheritDoc} */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.v("delete(uri=" + uri + ")"); final int match = getUriMatcher().match(uri); switch (match) { case RESOURCES_ID: deleteResourceSideEffect(uri); break; case DOWNLOADS_ID: deleteDownloadSideEffect(uri); break; default: break; } final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final SelectionBuilder builder = buildSimpleSelection(uri); int retVal; synchronized (mutex) { retVal = builder.where(selection, selectionArgs).delete(db); } getContext().getContentResolver().notifyChange(uri, null); return retVal; }
/** {@inheritDoc} */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.v("update(uri=" + uri + ")"); final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final SelectionBuilder builder = buildSimpleSelection(uri); int rowsChanged; synchronized (mutex) { rowsChanged = builder.where(selection, selectionArgs).update(db, values); } getContext().getContentResolver().notifyChange(uri, null); return rowsChanged; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.v("query(uri=" + uri + ", proj=" + Arrays.toString(projection) + ")"); final SQLiteDatabase db = mOpenHelper.getReadableDatabase(); final int match = getUriMatcher().match(uri); final SelectionBuilder builder = buildExpandedSelection(uri, match); Cursor query; synchronized (mutex) { query = builder.where(selection, selectionArgs).query(db, projection, sortOrder); } query.setNotificationUri(getContext().getContentResolver(), uri); return query; }
/** * Apply the given set of {@link ContentProviderOperation}, executing inside a {@link * SQLiteDatabase} transaction. All changes will be rolled back if any single one fails. */ @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try { 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(); } }
/** {@inheritDoc} */ @Override public Uri insert(Uri uri, ContentValues values) { Log.v("insert(uri=" + uri + ", values=[" + values.toString() + "])"); final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = getUriMatcher().match(uri); long id = 0; switch (match) { case FAVORITES: { synchronized (mutex) { id = db.insertOrThrow(Tables.FAVORITES, null, values); } getContext().getContentResolver().notifyChange(uri, null); return Favorites.buildFavoriteUri(String.valueOf(id)); } case RESOURCES: case RESOURCES_ID: { try { addDefaultThumbnail(values); calculateDuration(values); } catch (Exception e) { Log.w("Exception thrown creating thumbnail"); e.printStackTrace(); } try { informMediaScanner(values); } catch (IOException e) { } values.put(Resources.TIMESTAMP, String.valueOf(System.currentTimeMillis())); synchronized (mutex) { id = db.insertOrThrow(Tables.RESOURCES, null, values); } Uri resourceUri = Resources.buildResourceUri(String.valueOf(id)); try { createThumbnail(values); getContext().getContentResolver().update(resourceUri, values, null, null); } catch (IOException e) { Log.w("Error creating thumbnail"); } getContext().getContentResolver().notifyChange(uri, null); return resourceUri; } case DOWNLOADS: { values.put(Downloads.STATUS, Downloads.Status.INPROGRESS.valueOf()); values.put(Downloads.TIMESTAMP, String.valueOf(System.currentTimeMillis())); values.put(Downloads.MAX_PROGRESS, Downloads.PROGRESS_MAX_NUM); synchronized (mutex) { id = db.insertOrThrow(Tables.DOWNLOADS, null, values); } getContext().getContentResolver().notifyChange(uri, null); return Downloads.buildDownloadUri(String.valueOf(id)); } default: { throw new UnsupportedOperationException("Unknown uri(" + match + "): " + uri); } } }