From eacc73eebff1acbe19060e7161154dcd1a7fb947 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 26 May 2017 18:58:51 +0200 Subject: [PATCH 1/3] Setting File_url when starting download This way, downloads get resumed even if the service crashes --- .../service/download/DownloadService.java | 46 ++++++++++++------- .../core/storage/DownloadRequester.java | 6 ++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index 6eefc296f..a0605c79f 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -424,6 +424,8 @@ public class DownloadService extends Service { "ACTION_ENQUEUE_DOWNLOAD intent needs request extra"); } + writeFileUrl(request); + Downloader downloader = getDownloader(request); if (downloader != null) { numberOfDownloads.incrementAndGet(); @@ -905,6 +907,33 @@ public class DownloadService extends Service { } + private void writeFileUrl(DownloadRequest request) { + File dest = new File(request.getDestination()); + if (!dest.exists()) { + try { + dest.createNewFile(); + } catch (IOException e) { + Log.e(TAG, "Unable to create file"); + } + } + if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { + Log.d(TAG, "File has been partially downloaded. Writing file url"); + FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId()); + if (media == null) { + Log.d(TAG, "No media"); + return; + } + media.setFile_url(request.getDestination()); + try { + DBWriter.setFeedMedia(media).get(); + } catch (InterruptedException e) { + Log.e(TAG, "FailedDownloadHandler was interrupted"); + } catch (ExecutionException e) { + Log.e(TAG, "ExecutionException in FailedDownloadHandler: " + e.getMessage()); + } + } + } + /** * Handles failed downloads. *

@@ -929,23 +958,6 @@ public class DownloadService extends Service { DBWriter.setFeedLastUpdateFailed(request.getFeedfileId(), true); } else if (request.isDeleteOnFailure()) { Log.d(TAG, "Ignoring failed download, deleteOnFailure=true"); - } else { - File dest = new File(request.getDestination()); - if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { - Log.d(TAG, "File has been partially downloaded. Writing file url"); - FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId()); - if (media == null) { - return; - } - media.setFile_url(request.getDestination()); - try { - DBWriter.setFeedMedia(media).get(); - } catch (InterruptedException e) { - Log.e(TAG, "FailedDownloadHandler was interrupted"); - } catch (ExecutionException e) { - Log.e(TAG, "ExecutionException in FailedDownloadHandler: " + e.getMessage()); - } - } } } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java index 7051d7f4d..71880e609 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java @@ -11,8 +11,10 @@ import android.webkit.URLUtil; import org.apache.commons.io.FilenameUtils; import java.io.File; +import java.io.IOException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; import de.danoeh.antennapod.core.BuildConfig; import de.danoeh.antennapod.core.feed.Feed; @@ -89,7 +91,9 @@ public class DownloadRequester { private void download(Context context, FeedFile item, FeedFile container, File dest, boolean overwriteIfExists, String username, String password, String lastModified, boolean deleteOnFailure, Bundle arguments) { - final boolean partiallyDownloadedFileExists = item.getFile_url() != null; + final boolean partiallyDownloadedFileExists = item.getFile_url() != null && new File(item.getFile_url()).exists(); + + Log.d(TAG, "partiallyDownloadedFileExists: " + partiallyDownloadedFileExists); if (isDownloadingFile(item)) { Log.e(TAG, "URL " + item.getDownload_url() + " is already being downloaded"); From 115268475dc808fd14693a1cad41f8d3d5880796 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 15 Oct 2017 16:06:39 +0200 Subject: [PATCH 2/3] Improved log messages --- .../antennapod/core/service/download/DownloadService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index a0605c79f..c33f39f92 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -917,7 +917,7 @@ public class DownloadService extends Service { } } if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { - Log.d(TAG, "File has been partially downloaded. Writing file url"); + Log.d(TAG, "Writing file url"); FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId()); if (media == null) { Log.d(TAG, "No media"); @@ -927,9 +927,9 @@ public class DownloadService extends Service { try { DBWriter.setFeedMedia(media).get(); } catch (InterruptedException e) { - Log.e(TAG, "FailedDownloadHandler was interrupted"); + Log.e(TAG, "writeFileUrl was interrupted"); } catch (ExecutionException e) { - Log.e(TAG, "ExecutionException in FailedDownloadHandler: " + e.getMessage()); + Log.e(TAG, "ExecutionException in writeFileUrl: " + e.getMessage()); } } } From 70f9a9f3986a9a353d6b779f53ab1b33250b3e76 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 15 Oct 2017 16:20:33 +0200 Subject: [PATCH 3/3] Only write url of media files when starting download --- .../core/service/download/DownloadService.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index c33f39f92..0ea862ce8 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -907,7 +907,15 @@ public class DownloadService extends Service { } + /** + * Creates the destination file and writes FeedMedia File_url directly after starting download + * to make it possible to resume download after the service was killed by the system. + */ private void writeFileUrl(DownloadRequest request) { + if (request.getFeedfileType() != FeedMedia.FEEDFILETYPE_FEEDMEDIA) { + return; + } + File dest = new File(request.getDestination()); if (!dest.exists()) { try { @@ -916,7 +924,8 @@ public class DownloadService extends Service { Log.e(TAG, "Unable to create file"); } } - if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { + + if (dest.exists()) { Log.d(TAG, "Writing file url"); FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId()); if (media == null) {