diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-10-05 14:47:49 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-10-05 14:47:49 +0200 |
commit | f7bd65d9c17dacdfe83d9f4664feaff1acf8d884 (patch) | |
tree | 45717409b250f0d219ca7215e4cdbb6b3802e2ab /src | |
parent | abb3aaf5d96cb301ad29e6ad401edb3ee271d46e (diff) | |
download | AntennaPod-f7bd65d9c17dacdfe83d9f4664feaff1acf8d884.zip |
Execute autodownload method on SingleThreadExecutor
Diffstat (limited to 'src')
-rw-r--r-- | src/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java | 8 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/storage/DBTasks.java | 155 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/storage/DBWriter.java | 28 |
3 files changed, 92 insertions, 99 deletions
diff --git a/src/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java b/src/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java index aebe5a681..c1dd30be7 100644 --- a/src/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java +++ b/src/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java @@ -24,13 +24,7 @@ public class ConnectivityActionReceiver extends BroadcastReceiver { if (AppConfig.DEBUG) Log.d(TAG, "auto-dl network available, starting auto-download"); - new Thread() { - @Override - public void run() { - DBTasks - .autodownloadUndownloadedItems(context); - } - }.start(); + DBTasks.autodownloadUndownloadedItems(context); } else { // if new network is Wi-Fi, finish ongoing downloads, // otherwise cancel all downloads ConnectivityManager cm = (ConnectivityManager) context diff --git a/src/de/danoeh/antennapod/storage/DBTasks.java b/src/de/danoeh/antennapod/storage/DBTasks.java index cd0e65e8f..7813a6f6d 100644 --- a/src/de/danoeh/antennapod/storage/DBTasks.java +++ b/src/de/danoeh/antennapod/storage/DBTasks.java @@ -17,9 +17,7 @@ import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator; import de.danoeh.antennapod.util.exception.MediaFileNotFoundException; import java.util.*; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.FutureTask; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -28,6 +26,22 @@ import java.util.concurrent.atomic.AtomicBoolean; public final class DBTasks { private static final String TAG = "DBTasks"; + /** + * Executor service used by the autodownloadUndownloadedEpisodes method. + */ + private static ExecutorService autodownloadExec; + + static { + autodownloadExec = Executors.newSingleThreadExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setPriority(Thread.MIN_PRIORITY); + return t; + } + }); + } + private DBTasks() { } @@ -138,6 +152,7 @@ public final class DBTasks { isRefreshing.set(false); GpodnetSyncService.sendSyncIntent(context); + autodownloadUndownloadedItems(context); } }.start(); } else { @@ -151,9 +166,10 @@ public final class DBTasks { * Used by refreshExpiredFeeds to determine which feeds should be refreshed. * This method will use the value specified in the UserPreferences as the * expiration time. + * * @param context Used for DB access. * @return A list of expired feeds. An empty list will be returned if there - * are no expired feeds. + * are no expired feeds. */ public static List<Feed> getExpiredFeeds(final Context context) { long millis = UserPreferences.getUpdateInterval(); @@ -343,76 +359,83 @@ public final class DBTasks { * Looks for undownloaded episodes in the queue or list of unread items and request a download if * 1. Network is available * 2. There is free space in the episode cache - * This method should NOT be executed on the GUI thread. + * This method is executed on an internal single thread executor. * * @param context Used for accessing the DB. + * @return A Future that can be used for waiting for the methods completion. */ - public static void autodownloadUndownloadedItems(final Context context) { - if (AppConfig.DEBUG) - Log.d(TAG, "Performing auto-dl of undownloaded episodes"); - if (NetworkUtils.autodownloadNetworkAvailable(context) - && UserPreferences.isEnableAutodownload()) { - final List<FeedItem> queue = DBReader.getQueue(context); - final List<FeedItem> unreadItems = DBReader - .getUnreadItemsList(context); - - int undownloadedEpisodes = getNumberOfUndownloadedEpisodes(queue, - unreadItems); - int downloadedEpisodes = DBReader - .getNumberOfDownloadedEpisodes(context); - int deletedEpisodes = performAutoCleanup(context, - getPerformAutoCleanupArgs(context, undownloadedEpisodes)); - int episodeSpaceLeft = undownloadedEpisodes; - boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences - .getEpisodeCacheSizeUnlimited(); - - if (!cacheIsUnlimited - && UserPreferences.getEpisodeCacheSize() < downloadedEpisodes - + undownloadedEpisodes) { - episodeSpaceLeft = UserPreferences.getEpisodeCacheSize() - - (downloadedEpisodes - deletedEpisodes); - } + public static Future<?> autodownloadUndownloadedItems(final Context context) { + return autodownloadExec.submit(new Runnable() { + @Override + public void run() { + if (AppConfig.DEBUG) + Log.d(TAG, "Performing auto-dl of undownloaded episodes"); + if (NetworkUtils.autodownloadNetworkAvailable(context) + && UserPreferences.isEnableAutodownload()) { + final List<FeedItem> queue = DBReader.getQueue(context); + final List<FeedItem> unreadItems = DBReader + .getUnreadItemsList(context); + + int undownloadedEpisodes = getNumberOfUndownloadedEpisodes(queue, + unreadItems); + int downloadedEpisodes = DBReader + .getNumberOfDownloadedEpisodes(context); + int deletedEpisodes = performAutoCleanup(context, + getPerformAutoCleanupArgs(context, undownloadedEpisodes)); + int episodeSpaceLeft = undownloadedEpisodes; + boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences + .getEpisodeCacheSizeUnlimited(); + + if (!cacheIsUnlimited + && UserPreferences.getEpisodeCacheSize() < downloadedEpisodes + + undownloadedEpisodes) { + episodeSpaceLeft = UserPreferences.getEpisodeCacheSize() + - (downloadedEpisodes - deletedEpisodes); + } - List<FeedItem> itemsToDownload = new ArrayList<FeedItem>(); - if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) { - for (int i = 0; i < queue.size(); i++) { // ignore playing item - FeedItem item = queue.get(i); - if (item.hasMedia() && !item.getMedia().isDownloaded() - && !item.getMedia().isPlaying()) { - itemsToDownload.add(item); - episodeSpaceLeft--; - undownloadedEpisodes--; - if (episodeSpaceLeft == 0 || undownloadedEpisodes == 0) { - break; + List<FeedItem> itemsToDownload = new ArrayList<FeedItem>(); + if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) { + for (int i = 0; i < queue.size(); i++) { // ignore playing item + FeedItem item = queue.get(i); + if (item.hasMedia() && !item.getMedia().isDownloaded() + && !item.getMedia().isPlaying()) { + itemsToDownload.add(item); + episodeSpaceLeft--; + undownloadedEpisodes--; + if (episodeSpaceLeft == 0 || undownloadedEpisodes == 0) { + break; + } + } } } - } - } - if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) { - for (FeedItem item : unreadItems) { - if (item.hasMedia() && !item.getMedia().isDownloaded()) { - itemsToDownload.add(item); - episodeSpaceLeft--; - undownloadedEpisodes--; - if (episodeSpaceLeft == 0 || undownloadedEpisodes == 0) { - break; + if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) { + for (FeedItem item : unreadItems) { + if (item.hasMedia() && !item.getMedia().isDownloaded()) { + itemsToDownload.add(item); + episodeSpaceLeft--; + undownloadedEpisodes--; + if (episodeSpaceLeft == 0 || undownloadedEpisodes == 0) { + break; + } + } } } - } - } - if (AppConfig.DEBUG) - Log.d(TAG, "Enqueueing " + itemsToDownload.size() - + " items for download"); + if (AppConfig.DEBUG) + Log.d(TAG, "Enqueueing " + itemsToDownload.size() + + " items for download"); - try { - downloadFeedItems(false, context, - itemsToDownload.toArray(new FeedItem[itemsToDownload - .size()])); - } catch (DownloadRequestException e) { - e.printStackTrace(); + try { + downloadFeedItems(false, context, + itemsToDownload.toArray(new FeedItem[itemsToDownload + .size()])); + } catch (DownloadRequestException e) { + e.printStackTrace(); + } + + } } + }); - } } private static int getPerformAutoCleanupArgs(Context context, @@ -641,12 +664,6 @@ public final class DBTasks { } catch (ExecutionException e) { e.printStackTrace(); } - new Thread() { - @Override - public void run() { - autodownloadUndownloadedItems(context); - } - }.start(); return savedFeed; } } diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index 490629b27..26c09c68a 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -67,6 +67,8 @@ public class DBWriter { final FeedMedia media = DBReader.getFeedMedia(context, mediaId); if (media != null) { + Log.i(TAG, String.format("Requested to delete FeedMedia [id=%d, title=%s, downloaded=%s", + media.getId(), media.getEpisodeTitle(), String.valueOf(media.isDownloaded()))); boolean result = false; if (media.isDownloaded()) { // delete downloaded media file @@ -312,14 +314,7 @@ public class DBWriter { } adapter.close(); if (performAutoDownload) { - - new Thread() { - @Override - public void run() { - DBTasks.autodownloadUndownloadedItems(context); - - } - }.start(); + DBTasks.autodownloadUndownloadedItems(context); } } @@ -378,13 +373,7 @@ public class DBWriter { } } adapter.close(); - new Thread() { - @Override - public void run() { - DBTasks.autodownloadUndownloadedItems(context); - - } - }.start(); + DBTasks.autodownloadUndownloadedItems(context); } } }); @@ -451,14 +440,7 @@ public class DBWriter { } adapter.close(); if (performAutoDownload) { - - new Thread() { - @Override - public void run() { - DBTasks.autodownloadUndownloadedItems(context); - - } - }.start(); + DBTasks.autodownloadUndownloadedItems(context); } } }); |