/** * 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 createThumbnail(ContentValues map) throws IOException { File videoFile = fileFromResourceMap(map); Log.d("Creating thumbnail from video file " + videoFile.toString()); Bitmap bitmap = ThumbnailUtils.createVideoThumbnail( videoFile.toString(), MediaStore.Video.Thumbnails.MINI_KIND); if (bitmap == null) { Log.w("Error creating thumbnail"); return; } String filename = (String) map.get(Resources.FILENAME); if (TextUtils.isEmpty(filename)) { throw new IOException("Must specify FILENAME when inserting Resource"); } Uri thumbnailUri = Resources.buildThumbnailUri(filename + THUMBNAIL_EXT); OutputStream ostream; try { ostream = getContext().getContentResolver().openOutputStream(thumbnailUri); } catch (FileNotFoundException e) { Log.d("Could not open output stream for thumbnail storage: " + e.getLocalizedMessage()); return; } bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream); ostream.flush(); IOUtilities.closeStream(ostream); map.put(Resources.THUMBNAIL, thumbnailUri.toString()); }
/** {@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); } } }