diff options
author | orionlee <orionlee@yahoo.com> | 2018-05-12 13:13:38 -0700 |
---|---|---|
committer | orionlee <orionlee@yahoo.com> | 2019-01-04 10:32:14 -0800 |
commit | 5bc497009aded82d2767470abea24a79179f1837 (patch) | |
tree | 3a334d6e85a2bd17d645148f80201794ff772a94 /core/src/main/java/de | |
parent | 8252f6a41d76e5ed9899d5a1d6ac926d77a4bc9b (diff) | |
download | AntennaPod-5bc497009aded82d2767470abea24a79179f1837.zip |
#2685: add "12 hours after finishing" option for auto download episodes cleanup
Diffstat (limited to 'core/src/main/java/de')
3 files changed, 28 insertions, 8 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index c44999c88..a8d0b6ba1 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -674,7 +674,7 @@ public class UserPreferences { } public static EpisodeCleanupAlgorithm getEpisodeCleanupAlgorithm() { - int cleanupValue = Integer.parseInt(prefs.getString(PREF_EPISODE_CLEANUP, "-1")); + float cleanupValue = Float.parseFloat(prefs.getString(PREF_EPISODE_CLEANUP, "-1")); if (cleanupValue == EPISODE_CLEANUP_QUEUE) { return new APQueueCleanupAlgorithm(); } else if (cleanupValue == EPISODE_CLEANUP_NULL) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java index e94874453..c9dd8f374 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java @@ -13,6 +13,7 @@ import java.util.concurrent.ExecutionException; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.util.Converter; /** * Implementation of the EpisodeCleanupAlgorithm interface used by AntennaPod. @@ -20,10 +21,11 @@ import de.danoeh.antennapod.core.feed.FeedMedia; public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm { private static final String TAG = "APCleanupAlgorithm"; - /** the number of days after playback to wait before an item is eligible to be cleaned up */ - private final int numberOfDaysAfterPlayback; + /** the number of days after playback to wait before an item is eligible to be cleaned up. + Fractional for number of hours, e.g., 0.5 = 12 hours, 0.0416 = 1 hour. */ + private final float numberOfDaysAfterPlayback; - public APCleanupAlgorithm(int numberOfDaysAfterPlayback) { + public APCleanupAlgorithm(float numberOfDaysAfterPlayback) { this.numberOfDaysAfterPlayback = numberOfDaysAfterPlayback; } @@ -81,9 +83,8 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm { private List<FeedItem> getCandidates() { List<FeedItem> candidates = new ArrayList<>(); List<FeedItem> downloadedItems = DBReader.getDownloadedItems(); - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_MONTH, -1 * numberOfDaysAfterPlayback); - Date mostRecentDateForDeletion = cal.getTime(); + + Date mostRecentDateForDeletion = minusDays(new Date(), numberOfDaysAfterPlayback); for (FeedItem item : downloadedItems) { if (item.hasMedia() && item.getMedia().isDownloaded() @@ -108,5 +109,19 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm { return getNumEpisodesToCleanup(0); } - public int getNumberOfDaysAfterPlayback() { return numberOfDaysAfterPlayback; } + public float getNumberOfDaysAfterPlayback() { return numberOfDaysAfterPlayback; } + + private static Date minusDays(Date baseDate, float numberOfDays) { + Calendar cal = Calendar.getInstance(); + cal.setTime(baseDate); + + int integral = (int)numberOfDays; + float fraction = numberOfDays - integral; + + cal.add(Calendar.DAY_OF_MONTH, -1 * integral); + cal.add(Calendar.HOUR_OF_DAY, -1 * Converter.numberOfDaysFloatToNumberOfHours(fraction)); + + return cal.getTime(); + } + } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java b/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java index 3cb226add..5915d216c 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/Converter.java @@ -130,6 +130,11 @@ public final class Converter { return String.format(Locale.getDefault(), "%.1f ", hours) + context.getString(R.string.time_hours); } + + public static int numberOfDaysFloatToNumberOfHours(float numberOfDays) { + return (int)Math.ceil(numberOfDays * 24); + } + /** * Converts the volume as read as the progress from a SeekBar scaled to 100 and as saved in * UserPreferences to the format taken by setVolume methods. |