private File fileFromResourceMap(ContentValues map) throws IOException { String videoDir = (String) map.get(Resources.DIRECTORY); if (TextUtils.isEmpty(videoDir)) { throw new IOException("Must specify DIRECTORY when inserting Resource"); } String filename = (String) map.get(Resources.FILENAME); if (TextUtils.isEmpty(filename)) { throw new IOException("Must specify FILENAME when inserting Resource"); } // Deprecated if (VizContract.PATH_VIDEO_UNLOCKED.equals(videoDir)) { return VizUtils.getPublicVideoFile(filename); } else if (VizContract.PATH_VIDEO_LOCKED.equals(videoDir)) { return VizUtils.getPrivateVideoFile(filename); } else { // All new files will have a literal path return new File(videoDir, filename); } }
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(); }
/** Return true if download should continue, otherwise false; */ private boolean errorCheckPathAndFile(String filename) { if (TextUtils.isEmpty(filename)) { Toast.makeText( VizApp.getContext(), VizApp.getResString(R.string.invalid_filename), Toast.LENGTH_LONG) .show(); return false; } File downloadDir = VizUtils.getDownloadDir(); if (downloadDir == null || !downloadDir.exists()) { Toast.makeText( VizApp.getContext(), VizApp.getResString(R.string.directory_access_error), Toast.LENGTH_LONG) .show(); return false; } if (!downloadDir.canWrite() || !downloadDir.canRead()) { Toast.makeText( VizApp.getContext(), VizApp.getResString(R.string.directory_access_error), Toast.LENGTH_LONG) .show(); return false; } File file = VizUtils.getVideoFile(filename); if (file != null && file.exists()) { Toast.makeText( VizApp.getContext(), VizApp.getResString(R.string.file_exists_error), Toast.LENGTH_LONG) .show(); return false; } return true; }
private void informMediaScanner(ContentValues map) throws IOException { final File videoFile = fileFromResourceMap(map); String dir = (String) map.get(Resources.DIRECTORY); if (VizUtils.isPublicDir(dir)) { Thread t = new Thread("AddVideoToGallery") { @Override public void run() { VizUtils.informMediaScanner(videoFile.toString()); } }; Utils.threadStart(t, "Failed to add video to gallery"); } }
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 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); }