private void startDownload(Resource resource) { mConfirmationInProgress = false; ActivityDelegate ad = getActivityDelegate(); if (ad == null) { return; } resource.setDownloadDirectory(VizUtils.getDownloadDir()); // check download directory in case sd card or whatever has been // unmounted and we can no longer write to it. File directory = resource.getDownloadDirectory(); if (!Utils.directoryCreate(directory)) { new AlertDialog.Builder(ad) .setIcon(R.drawable.ic_launcher) .setTitle(VizApp.getResString(R.string.download_failed)) .setMessage(VizApp.getResString(R.string.storage_error)) .setNeutralButton(R.string.ok, null) .create() .show(); return; } Uri uri = VizApp.getResolver().insert(VizContract.Downloads.CONTENT_URI, resource.toContentValues()); if (uri == null) { Log.e("Could not add download to database error"); Toast.makeText( VizApp.getContext(), VizApp.getResString(R.string.database_access_error), Toast.LENGTH_LONG) .show(); return; } Log.d("(uri=" + uri + ")"); resource.setDownloadUri(uri); Downloads downloads = ad.getDownloadsFragment(); downloads.queue(resource); }
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(); }
private File getFileFromUri(Uri uri) throws FileNotFoundException { Cursor cursor = null; String filename = null; File videoDir = null; int match = getUriMatcher().match(uri); Log.d("getFileFromUri(uri=" + uri + ", match=" + match + ")"); switch (match) { case RESOURCES_ID: cursor = query( uri, new String[] {Resources._ID, Resources.DIRECTORY, Resources.FILENAME}, null, null, null); if (!cursor.moveToFirst()) { cursor.close(); throw new FileNotFoundException("Could not find Resource for uri"); } filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME)); videoDir = directorySwitch(cursor.getString(cursor.getColumnIndex(Resources.DIRECTORY))); Log.d("Got filename: " + filename + " directory: " + videoDir); break; case DOWNLOADS_ID: cursor = query( uri, new String[] {Downloads._ID, Downloads.DIRECTORY, Downloads.FILENAME}, null, null, null); if (!cursor.moveToFirst()) { cursor.close(); throw new FileNotFoundException("Could not find Download for uri"); } filename = cursor.getString(cursor.getColumnIndex(Downloads.FILENAME)); videoDir = directorySwitch(cursor.getString(cursor.getColumnIndex(Downloads.DIRECTORY))); Log.d("Got filename: " + filename + " directory: " + videoDir); break; case RESOURCES_THUMBNAILS: Log.d("Got thumbnail: " + uri); filename = uri.getLastPathSegment(); File thumbnailDirectory = VizUtils.getVideosThumbnailDir(); Log.d("Full thumbnail directory path: " + VizUtils.getVideosThumbnailPath()); if (thumbnailDirectory == null || !thumbnailDirectory.exists()) { Log.e("Could not create directory error: " + VizUtils.getVideosThumbnailPath()); throw new FileNotFoundException("Media not mounted error: thumbnail could not be found"); } Log.d("Got thumbnail filename: " + filename); return new File(thumbnailDirectory, filename); default: throw new FileNotFoundException("No case for " + match); } cursor.close(); if (videoDir == null) { throw new FileNotFoundException("no video directory specified for " + match); } if (filename == null) { throw new FileNotFoundException("no filename specified for " + match); } return new File(videoDir, filename); }