Source:
void download(Callback callback) { downloadFileStatus.markAsDownloading(); callback.onUpdate(downloadFileStatus); fileSize = requestTotalFileSizeIfNecessary(fileSize); if (fileSize.isTotalSizeUnknown()) { updateAndFeedbackWithStatus(Error.FILE_TOTAL_SIZE_REQUEST_FAILED, callback); return; } fileSize.setCurrentSize(filePersistence.getCurrentSize(filePath)); if (downloadFileStatus.isMarkedAsDeleted()) { return; } Log.v("persist file " + downloadFileId.rawId() + ", with status: " + downloadFileStatus.status()); if (persist()) { return; } if (fileSize.currentSize() == fileSize.totalSize()) { downloadFileStatus.update(fileSize, filePath); callback.onUpdate(downloadFileStatus); return; } FilePersistenceResult result = filePersistence.create(filePath, fileSize); if (result != FilePersistenceResult.SUCCESS) { Error error = convertError(result); updateAndFeedbackWithStatus(error, callback); return; } fileDownloader.startDownloading(url, fileSize, new FileDownloader.Callback() { @Override public void onBytesRead(byte[] buffer, int bytesRead) { boolean success = filePersistence.write(buffer, 0, bytesRead); if (!success) { updateAndFeedbackWithStatus(Error.FILE_CANNOT_BE_WRITTEN, callback); } if (downloadFileStatus.isMarkedAsDownloading()) { fileSize.addToCurrentSize(bytesRead); downloadFileStatus.update(fileSize, filePath); callback.onUpdate(downloadFileStatus); } } @Override public void onError() { updateAndFeedbackWithStatus(Error.NETWORK_ERROR_CANNOT_DOWNLOAD_FILE, callback); } @Override public void onDownloadFinished() { filePersistence.close(); if (downloadFileStatus.isMarkedAsDeleted()) { filePersistence.delete(filePath); } if (downloadFileStatus.isMarkedAsWaitingForNetwork()) { callback.onUpdate(downloadFileStatus); } } }); }

Target:
void download(Callback callback) { downloadFileStatus.markAsDownloading(); callback.onUpdate(downloadFileStatus); fileSize = requestTotalFileSizeIfNecessary(fileSize); if (fileSize.isTotalSizeUnknown()) { updateAndFeedbackWithStatus(Error.FILE_TOTAL_SIZE_REQUEST_FAILED, callback); return; } fileSize.setCurrentSize(filePersistence.getCurrentSize(filePath)); if (downloadFileStatus.isMarkedAsDeleted()) { return; } Log.v("persist file " + downloadFileId.rawId() + ", with status: " + downloadFileStatus.status()); if (!persist()) { Log.e("persisting file " + downloadFileId.rawId() + " with status " + downloadFileStatus.status() + " failed"); return; } if (fileSize.currentSize() == fileSize.totalSize()) { downloadFileStatus.update(fileSize, filePath); callback.onUpdate(downloadFileStatus); return; } FilePersistenceResult result = filePersistence.create(filePath, fileSize); if (result != FilePersistenceResult.SUCCESS) { Error error = convertError(result); updateAndFeedbackWithStatus(error, callback); return; } fileDownloader.startDownloading(url, fileSize, new FileDownloader.Callback() { @Override public void onBytesRead(byte[] buffer, int bytesRead) { boolean success = filePersistence.write(buffer, 0, bytesRead); if (!success) { updateAndFeedbackWithStatus(Error.FILE_CANNOT_BE_WRITTEN, callback); } if (downloadFileStatus.isMarkedAsDownloading()) { fileSize.addToCurrentSize(bytesRead); downloadFileStatus.update(fileSize, filePath); callback.onUpdate(downloadFileStatus); } } @Override public void onError() { updateAndFeedbackWithStatus(Error.NETWORK_ERROR_CANNOT_DOWNLOAD_FILE, callback); } @Override public void onDownloadFinished() { filePersistence.close(); if (downloadFileStatus.isMarkedAsDeleted()) { filePersistence.delete(filePath); } if (downloadFileStatus.isMarkedAsWaitingForNetwork()) { callback.onUpdate(downloadFileStatus); } } }); }