/** * Build a simple {@link SelectionBuilder} to match the requested {@link Uri}. This is usually * enough to support {@link #insert}, {@link #update}, and {@link #delete} operations. */ private SelectionBuilder buildSimpleSelection(Uri uri) { final SelectionBuilder builder = new SelectionBuilder(); final int match = getUriMatcher().match(uri); switch (match) { case FAVORITES_ID: { final String favoriteId = Favorites.getFavoriteId(uri); return builder.table(Tables.FAVORITES).where(Favorites._ID + "=?", favoriteId); } case RESOURCES_ID: { final String resourceId = Resources.getResourceId(uri); return builder.table(Tables.RESOURCES).where(Resources._ID + "=?", resourceId); } case DOWNLOADS_ID: { final String downloadId = Downloads.getDownloadId(uri); return builder.table(Tables.DOWNLOADS).where(Downloads._ID + "=?", downloadId); } case DOWNLOADS: { return builder.table(Tables.DOWNLOADS); } case RESOURCES: { return builder.table(Tables.RESOURCES); } default: { throw new UnsupportedOperationException("Unknown uri( " + match + "): " + uri); } } }
private void deleteResourceSideEffect(Uri resourceUri) { Log.v("deleteResourceSideEffect(uri=" + resourceUri + ")"); // delete download row correspondong to this content Cursor cursor = query( resourceUri, new String[] { Resources._ID, Resources.DOWNLOAD_ID, Resources.DIRECTORY, Resources.FILENAME, Resources.CONTENT }, null, null, null); if (!cursor.moveToFirst()) { Log.e("deleteResourceSideEffect: cursor empty error"); return; } String downloadId = cursor.getString(cursor.getColumnIndex(Resources.DOWNLOAD_ID)); Uri downloadUri = Downloads.buildDownloadUri(downloadId); delete(downloadUri, null, null); getContext().getContentResolver().notifyChange(downloadUri, null); // delete video file associated with resource String filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME)); String dir = cursor.getString(cursor.getColumnIndex(Resources.DIRECTORY)); deleteVideo(directorySwitch(dir), filename); if (VizUtils.isPublicDir(dir)) { removeFromMediaStore(VizUtils.getPublicVideoFilename(filename)); } deleteThumbnail(VizContract.PATH_THUMBNAILS, filename + THUMBNAIL_EXT); cursor.close(); }
/** {@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); } } }