From bbb7cfe7ed7e477cd3d5627e71e6522dd1e30b1a Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Tue, 26 Nov 2019 20:54:26 +0100 Subject: [PATCH] Do not start download service if there is nothing to download --- .../core/storage/APDownloadAlgorithm.java | 28 +++++++++---------- .../core/storage/DownloadRequester.java | 9 +++--- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java index 218320c68..d029e7bfb 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java @@ -53,19 +53,19 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { final List newItems = DBReader.getNewItemsList(); candidates = new ArrayList<>(queue.size() + newItems.size()); candidates.addAll(queue); - for(FeedItem newItem : newItems) { + for (FeedItem newItem : newItems) { FeedPreferences feedPrefs = newItem.getFeed().getPreferences(); FeedFilter feedFilter = feedPrefs.getFilter(); - if(!candidates.contains(newItem) && feedFilter.shouldAutoDownload(newItem)) { + if (!candidates.contains(newItem) && feedFilter.shouldAutoDownload(newItem)) { candidates.add(newItem); } } // filter items that are not auto downloadable Iterator it = candidates.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { FeedItem item = it.next(); - if(!item.isAutoDownloadable()) { + if (!item.isAutoDownloadable()) { it.remove(); } } @@ -74,13 +74,12 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes(); int deletedEpisodes = UserPreferences.getEpisodeCleanupAlgorithm() .makeRoomForEpisodes(context, autoDownloadableEpisodes); - boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences - .getEpisodeCacheSizeUnlimited(); + boolean cacheIsUnlimited = + UserPreferences.getEpisodeCacheSize() == UserPreferences.getEpisodeCacheSizeUnlimited(); int episodeCacheSize = UserPreferences.getEpisodeCacheSize(); int episodeSpaceLeft; - if (cacheIsUnlimited || - episodeCacheSize >= downloadedEpisodes + autoDownloadableEpisodes) { + if (cacheIsUnlimited || episodeCacheSize >= downloadedEpisodes + autoDownloadableEpisodes) { episodeSpaceLeft = autoDownloadableEpisodes; } else { episodeSpaceLeft = episodeCacheSize - (downloadedEpisodes - deletedEpisodes); @@ -89,14 +88,15 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { FeedItem[] itemsToDownload = candidates.subList(0, episodeSpaceLeft) .toArray(new FeedItem[episodeSpaceLeft]); - Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download"); + if (itemsToDownload.length > 0) { + Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download"); - try { - DownloadRequester.getInstance().downloadMedia(false, context, itemsToDownload); - } catch (DownloadRequestException e) { - e.printStackTrace(); + try { + DownloadRequester.getInstance().downloadMedia(false, context, itemsToDownload); + } catch (DownloadRequestException e) { + e.printStackTrace(); + } } - } }; } 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 ea3724adc..21f90a70c 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 @@ -80,13 +80,14 @@ public class DownloadRequester implements DownloadStateProvider { * with the same source URL is already stored, this one will be skipped. * @return True if any of the download request was accepted, false otherwise. */ - public synchronized boolean download(@NonNull Context context, - DownloadRequest... requests) { + public synchronized boolean download(@NonNull Context context, DownloadRequest... requests) { return download(context, false, requests); } - private boolean download(@NonNull Context context, boolean cleanupMedia, - DownloadRequest... requests) { + private boolean download(@NonNull Context context, boolean cleanupMedia, DownloadRequest... requests) { + if (requests.length <= 0) { + return false; + } boolean result = false; ArrayList requestsToSend = new ArrayList<>(requests.length);