use of org.mozilla.focus.download.DownloadInfoManager in project Rocket by mozilla-tw.
the class RelocateService method moveFile.
/**
* If removable storage exists, to move a file to it. Once moving completed, also update
* database for latest file path.
*
* @param rowId id of downloaded file in our database
* @param downloadId downloadId of downloaded file, need this to update system database
* @param srcFile file to be moved
* @param type MIME type of the file, to decide sub directory
*/
private void moveFile(final long rowId, final long downloadId, final File srcFile, final String type) {
final Settings settings = Settings.getInstance(getApplicationContext());
File destFile = null;
try {
final File outputDir = StorageUtils.getTargetDirOnRemovableStorageForDownloads(this, type);
if (outputDir != null) {
FileUtils.ensureDir(outputDir);
destFile = FileUtils.getFileSlot(outputDir, srcFile.getName());
if (outputDir.getUsableSpace() < srcFile.length()) {
final CharSequence msg = getString(R.string.message_removable_storage_space_not_enough);
broadcastUi(msg);
Log.w(TAG, msg.toString());
broadcastRelocateFinished(rowId);
return;
}
// instead of rename, to use copy + remove for safety
boolean copied = FileUtils.copy(srcFile, destFile);
if (!copied) {
Log.w(TAG, String.format("cannot copy file from %s to %s", srcFile.getPath(), destFile.getPath()));
broadcastRelocateFinished(rowId);
return;
}
boolean deleted = srcFile.delete();
if (!deleted) {
throw new RuntimeException("Cannot delete original file: " + srcFile.getAbsolutePath());
}
// downloaded file is moved, update database to reflect this changing
final DownloadInfoManager mgr = DownloadInfoManager.getInstance();
mgr.replacePath(downloadId, destFile.getAbsolutePath(), type);
// we moved download file to removable-storage, now we should inform user
if (!settings.getRemovableStorageStateOnCreate()) {
// avoid sending same message continuously
if (settings.getShowedStorageMessage() != Settings.STORAGE_MSG_TYPE_REMOVABLE_AVAILABLE) {
settings.setShowedStorageMessage(Settings.STORAGE_MSG_TYPE_REMOVABLE_AVAILABLE);
final CharSequence msg = getString(R.string.message_start_to_save_to_removable_storage);
broadcastUi(msg);
Log.w(TAG, msg.toString());
}
}
}
} catch (NoRemovableStorageException e) {
// removable-storage existed on app creation, but now it is gone
// we keep download file in original path, now we should inform user
broadcastRelocateFinished(rowId);
if (settings.getRemovableStorageStateOnCreate()) {
// avoid sending same message continuously
if (settings.getShowedStorageMessage() != Settings.STORAGE_MSG_TYPE_REMOVABLE_UNAVAILABLE) {
settings.setShowedStorageMessage(Settings.STORAGE_MSG_TYPE_REMOVABLE_UNAVAILABLE);
final CharSequence msg = getString(R.string.message_fallback_save_to_primary_external);
broadcastUi(msg);
Log.w(TAG, msg.toString());
}
}
e.printStackTrace();
} catch (Exception e) {
// if anything wrong, try to keep original file
broadcastRelocateFinished(rowId);
try {
if ((destFile != null) && destFile.exists() && destFile.canWrite() && srcFile.exists()) {
if (destFile.delete()) {
Log.w(TAG, "cannot delete copied file: " + destFile.getAbsolutePath());
}
}
} catch (Exception e2) {
e2.printStackTrace();
}
e.printStackTrace();
}
}
use of org.mozilla.focus.download.DownloadInfoManager in project Rocket by mozilla-tw.
the class RelocateService method handleActionMove.
/**
* Handle action Move in the provided background thread with the provided
* parameters.
*/
private void handleActionMove(final long rowId, final long downloadId, @NonNull final File srcFile, @Nullable final String mediaType) {
final Settings settings = Settings.getInstance(getApplicationContext());
// Do nothing, if user turned off the option
if (!settings.shouldSaveToRemovableStorage()) {
broadcastRelocateFinished(rowId);
return;
}
// if the download id is not in our database, ignore this operation
final DownloadInfoManager mgr = DownloadInfoManager.getInstance();
if (!mgr.recordExists(downloadId)) {
return;
}
// To move file if we have correct permission
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
moveFile(rowId, downloadId, srcFile, mediaType);
} else {
// if no permission, send broadcast to UI.
// If get permission from UI, it should startService again
broadcastNoPermission(downloadId, srcFile, mediaType);
}
}
Aggregations