summaryrefslogtreecommitdiff
path: root/core/src/main/java/de
diff options
context:
space:
mode:
authorByteHamster <ByteHamster@users.noreply.github.com>2024-04-04 22:26:53 +0200
committerGitHub <noreply@github.com>2024-04-04 22:26:53 +0200
commit2143ab135182434911d4554a8ef08115eaa0d2d0 (patch)
treeb380fdf38b34a032c5ea0ed5b5fa22dce7433723 /core/src/main/java/de
parent0288d4e51eb7eef565be8d814fb8c152383e5031 (diff)
downloadAntennaPod-2143ab135182434911d4554a8ef08115eaa0d2d0.zip
Move some tests from core module to their respective module (#7059)
Diffstat (limited to 'core/src/main/java/de')
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java135
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java29
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java99
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/AutoDownloadManagerImpl.java55
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java121
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java66
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithmFactory.java22
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithm.java104
8 files changed, 0 insertions, 631 deletions
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
deleted file mode 100644
index d1de1f616..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java
+++ /dev/null
@@ -1,135 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-import androidx.annotation.NonNull;
-import androidx.annotation.VisibleForTesting;
-import android.util.Log;
-
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-import java.util.Locale;
-import java.util.concurrent.ExecutionException;
-
-import de.danoeh.antennapod.model.feed.FeedItem;
-import de.danoeh.antennapod.model.feed.FeedItemFilter;
-import de.danoeh.antennapod.model.feed.FeedMedia;
-import de.danoeh.antennapod.model.feed.SortOrder;
-import de.danoeh.antennapod.storage.database.DBReader;
-import de.danoeh.antennapod.storage.database.DBWriter;
-
-/**
- * Implementation of the EpisodeCleanupAlgorithm interface used by AntennaPod.
- */
-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.
- Fractional for number of hours, e.g., 0.5 = 12 hours, 0.0416 = 1 hour. */
- private final int numberOfHoursAfterPlayback;
-
- public APCleanupAlgorithm(int numberOfHoursAfterPlayback) {
- this.numberOfHoursAfterPlayback = numberOfHoursAfterPlayback;
- }
-
- /**
- * @return the number of episodes that *could* be cleaned up, if needed
- */
- public int getReclaimableItems()
- {
- return getCandidates().size();
- }
-
- @Override
- public int performCleanup(Context context, int numberOfEpisodesToDelete) {
- List<FeedItem> candidates = getCandidates();
- List<FeedItem> delete;
-
- Collections.sort(candidates, (lhs, rhs) -> {
- Date l = lhs.getMedia().getPlaybackCompletionDate();
- Date r = rhs.getMedia().getPlaybackCompletionDate();
-
- if (l == null) {
- l = new Date();
- }
- if (r == null) {
- r = new Date();
- }
- return l.compareTo(r);
- });
-
- if (candidates.size() > numberOfEpisodesToDelete) {
- delete = candidates.subList(0, numberOfEpisodesToDelete);
- } else {
- delete = candidates;
- }
-
- for (FeedItem item : delete) {
- try {
- DBWriter.deleteFeedMediaOfItem(context, item.getMedia()).get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
-
- int counter = delete.size();
-
-
- Log.i(TAG, String.format(Locale.US,
- "Auto-delete deleted %d episodes (%d requested)", counter,
- numberOfEpisodesToDelete));
-
- return counter;
- }
-
- @VisibleForTesting
- Date calcMostRecentDateForDeletion(@NonNull Date currentDate) {
- return minusHours(currentDate, numberOfHoursAfterPlayback);
- }
-
- @NonNull
- private List<FeedItem> getCandidates() {
- List<FeedItem> candidates = new ArrayList<>();
- List<FeedItem> downloadedItems = DBReader.getEpisodes(0, Integer.MAX_VALUE,
- new FeedItemFilter(FeedItemFilter.DOWNLOADED), SortOrder.DATE_NEW_OLD);
-
- Date mostRecentDateForDeletion = calcMostRecentDateForDeletion(new Date());
- for (FeedItem item : downloadedItems) {
- if (item.hasMedia()
- && item.getMedia().isDownloaded()
- && !item.isTagged(FeedItem.TAG_QUEUE)
- && item.isPlayed()
- && !item.isTagged(FeedItem.TAG_FAVORITE)) {
- FeedMedia media = item.getMedia();
- // make sure this candidate was played at least the proper amount of days prior
- // to now
- if (media != null
- && media.getPlaybackCompletionDate() != null
- && media.getPlaybackCompletionDate().before(mostRecentDateForDeletion)) {
- candidates.add(item);
- }
- }
- }
- return candidates;
- }
-
- @Override
- public int getDefaultCleanupParameter() {
- return getNumEpisodesToCleanup(0);
- }
-
- @VisibleForTesting
- public int getNumberOfHoursAfterPlayback() { return numberOfHoursAfterPlayback; }
-
- private static Date minusHours(Date baseDate, int numberOfHours) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(baseDate);
-
- cal.add(Calendar.HOUR_OF_DAY, -1 * numberOfHours);
-
- return cal.getTime();
- }
-
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java
deleted file mode 100644
index 9cec62d83..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-import android.util.Log;
-
-/**
- * A cleanup algorithm that never removes anything
- */
-public class APNullCleanupAlgorithm extends EpisodeCleanupAlgorithm {
-
- private static final String TAG = "APNullCleanupAlgorithm";
-
- @Override
- public int performCleanup(Context context, int parameter) {
- // never clean anything up
- Log.i(TAG, "performCleanup: Not removing anything");
- return 0;
- }
-
- @Override
- public int getDefaultCleanupParameter() {
- return 0;
- }
-
- @Override
- public int getReclaimableItems() {
- return 0;
- }
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java
deleted file mode 100644
index 74ee43cf0..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java
+++ /dev/null
@@ -1,99 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-import androidx.annotation.NonNull;
-import android.util.Log;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-import java.util.Locale;
-import java.util.concurrent.ExecutionException;
-
-import de.danoeh.antennapod.model.feed.FeedItem;
-import de.danoeh.antennapod.model.feed.FeedItemFilter;
-import de.danoeh.antennapod.model.feed.SortOrder;
-import de.danoeh.antennapod.storage.database.DBReader;
-import de.danoeh.antennapod.storage.database.DBWriter;
-
-/**
- * A cleanup algorithm that removes any item that isn't in the queue and isn't a favorite
- * but only if space is needed.
- */
-public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm {
-
- private static final String TAG = "APQueueCleanupAlgorithm";
-
- /**
- * @return the number of episodes that *could* be cleaned up, if needed
- */
- public int getReclaimableItems()
- {
- return getCandidates().size();
- }
-
- @Override
- public int performCleanup(Context context, int numberOfEpisodesToDelete) {
- List<FeedItem> candidates = getCandidates();
- List<FeedItem> delete;
-
- // in the absence of better data, we'll sort by item publication date
- Collections.sort(candidates, (lhs, rhs) -> {
- Date l = lhs.getPubDate();
- Date r = rhs.getPubDate();
-
- if (l == null) {
- l = new Date();
- }
- if (r == null) {
- r = new Date();
- }
- return l.compareTo(r);
- });
-
- if (candidates.size() > numberOfEpisodesToDelete) {
- delete = candidates.subList(0, numberOfEpisodesToDelete);
- } else {
- delete = candidates;
- }
-
- for (FeedItem item : delete) {
- try {
- DBWriter.deleteFeedMediaOfItem(context, item.getMedia()).get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
-
- int counter = delete.size();
-
-
- Log.i(TAG, String.format(Locale.US,
- "Auto-delete deleted %d episodes (%d requested)", counter,
- numberOfEpisodesToDelete));
-
- return counter;
- }
-
- @NonNull
- private List<FeedItem> getCandidates() {
- List<FeedItem> candidates = new ArrayList<>();
- List<FeedItem> downloadedItems = DBReader.getEpisodes(0, Integer.MAX_VALUE,
- new FeedItemFilter(FeedItemFilter.DOWNLOADED), SortOrder.DATE_NEW_OLD);
- for (FeedItem item : downloadedItems) {
- if (item.hasMedia()
- && item.getMedia().isDownloaded()
- && !item.isTagged(FeedItem.TAG_QUEUE)
- && !item.isTagged(FeedItem.TAG_FAVORITE)) {
- candidates.add(item);
- }
- }
- return candidates;
- }
-
- @Override
- public int getDefaultCleanupParameter() {
- return getNumEpisodesToCleanup(0);
- }
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/AutoDownloadManagerImpl.java b/core/src/main/java/de/danoeh/antennapod/core/storage/AutoDownloadManagerImpl.java
deleted file mode 100644
index b00375ffe..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/AutoDownloadManagerImpl.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-import android.util.Log;
-import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager;
-
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-
-public class AutoDownloadManagerImpl extends AutoDownloadManager {
- private static final String TAG = "AutoDownloadManager";
-
- /**
- * Executor service used by the autodownloadUndownloadedEpisodes method.
- */
- private static final ExecutorService autodownloadExec;
-
- private static AutomaticDownloadAlgorithm downloadAlgorithm = new AutomaticDownloadAlgorithm();
-
- static {
- autodownloadExec = Executors.newSingleThreadExecutor(r -> {
- Thread t = new Thread(r);
- t.setPriority(Thread.MIN_PRIORITY);
- return t;
- });
- }
-
- /**
- * Looks for non-downloaded episodes in the queue or list of unread items and request a download if
- * 1. Network is available
- * 2. The device is charging or the user allows auto download on battery
- * 3. There is free space in the episode cache
- * 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 Future<?> autodownloadUndownloadedItems(final Context context) {
- Log.d(TAG, "autodownloadUndownloadedItems");
- return autodownloadExec.submit(downloadAlgorithm.autoDownloadUndownloadedItems(context));
- }
-
- /**
- * Removed downloaded episodes outside of the queue if the episode cache is full. Episodes with a smaller
- * 'playbackCompletionDate'-value will be deleted first.
- * <p/>
- * This method should NOT be executed on the GUI thread.
- *
- * @param context Used for accessing the DB.
- */
- public void performAutoCleanup(final Context context) {
- EpisodeCleanupAlgorithmFactory.build().performCleanup(context);
- }
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java
deleted file mode 100644
index dbcc899ba..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java
+++ /dev/null
@@ -1,121 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.os.BatteryManager;
-import android.util.Log;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import de.danoeh.antennapod.model.feed.FeedItemFilter;
-import de.danoeh.antennapod.model.feed.SortOrder;
-import de.danoeh.antennapod.model.feed.FeedItem;
-import de.danoeh.antennapod.model.feed.FeedPreferences;
-import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface;
-import de.danoeh.antennapod.storage.database.DBReader;
-import de.danoeh.antennapod.storage.preferences.UserPreferences;
-import de.danoeh.antennapod.net.common.NetworkUtils;
-
-/**
- * Implements the automatic download algorithm used by AntennaPod. This class assumes that
- * the client uses the {@link EpisodeCleanupAlgorithm}.
- */
-public class AutomaticDownloadAlgorithm {
- private static final String TAG = "DownloadAlgorithm";
-
- /**
- * Looks for undownloaded episodes in the queue or list of new items and request a download if
- * 1. Network is available
- * 2. The device is charging or the user allows auto download on battery
- * 3. There is free space in the episode cache
- * This method is executed on an internal single thread executor.
- *
- * @param context Used for accessing the DB.
- * @return A Runnable that will be submitted to an ExecutorService.
- */
- public Runnable autoDownloadUndownloadedItems(final Context context) {
- return () -> {
-
- // true if we should auto download based on network status
- boolean networkShouldAutoDl = NetworkUtils.isAutoDownloadAllowed()
- && UserPreferences.isEnableAutodownload();
-
- // true if we should auto download based on power status
- boolean powerShouldAutoDl = deviceCharging(context) || UserPreferences.isEnableAutodownloadOnBattery();
-
- // we should only auto download if both network AND power are happy
- if (networkShouldAutoDl && powerShouldAutoDl) {
-
- Log.d(TAG, "Performing auto-dl of undownloaded episodes");
-
- List<FeedItem> candidates;
- final List<FeedItem> queue = DBReader.getQueue();
- final List<FeedItem> newItems = DBReader.getEpisodes(0, Integer.MAX_VALUE,
- new FeedItemFilter(FeedItemFilter.NEW), SortOrder.DATE_NEW_OLD);
- candidates = new ArrayList<>(queue.size() + newItems.size());
- candidates.addAll(queue);
- for (FeedItem newItem : newItems) {
- FeedPreferences feedPrefs = newItem.getFeed().getPreferences();
- if (feedPrefs.getAutoDownload()
- && !candidates.contains(newItem)
- && feedPrefs.getFilter().shouldAutoDownload(newItem)) {
- candidates.add(newItem);
- }
- }
-
- // filter items that are not auto downloadable
- Iterator<FeedItem> it = candidates.iterator();
- while (it.hasNext()) {
- FeedItem item = it.next();
- if (!item.isAutoDownloadEnabled()
- || item.isDownloaded()
- || !item.hasMedia()
- || item.getFeed().isLocalFeed()) {
- it.remove();
- }
- }
-
- int autoDownloadableEpisodes = candidates.size();
- int downloadedEpisodes = DBReader.getTotalEpisodeCount(new FeedItemFilter(FeedItemFilter.DOWNLOADED));
- int deletedEpisodes = EpisodeCleanupAlgorithmFactory.build()
- .makeRoomForEpisodes(context, autoDownloadableEpisodes);
- boolean cacheIsUnlimited =
- UserPreferences.getEpisodeCacheSize() == UserPreferences.EPISODE_CACHE_SIZE_UNLIMITED;
- int episodeCacheSize = UserPreferences.getEpisodeCacheSize();
-
- int episodeSpaceLeft;
- if (cacheIsUnlimited || episodeCacheSize >= downloadedEpisodes + autoDownloadableEpisodes) {
- episodeSpaceLeft = autoDownloadableEpisodes;
- } else {
- episodeSpaceLeft = episodeCacheSize - (downloadedEpisodes - deletedEpisodes);
- }
-
- List<FeedItem> itemsToDownload = candidates.subList(0, episodeSpaceLeft);
- if (itemsToDownload.size() > 0) {
- Log.d(TAG, "Enqueueing " + itemsToDownload.size() + " items for download");
-
- for (FeedItem episode : itemsToDownload) {
- DownloadServiceInterface.get().download(context, episode);
- }
- }
- }
- };
- }
-
- /**
- * @return true if the device is charging
- */
- public static boolean deviceCharging(Context context) {
- // from http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
- IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- Intent batteryStatus = context.registerReceiver(null, intentFilter);
-
- int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
- return (status == BatteryManager.BATTERY_STATUS_CHARGING
- || status == BatteryManager.BATTERY_STATUS_FULL);
-
- }
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java
deleted file mode 100644
index 782abb4e7..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-
-import de.danoeh.antennapod.model.feed.FeedItemFilter;
-import de.danoeh.antennapod.storage.database.DBReader;
-import de.danoeh.antennapod.storage.preferences.UserPreferences;
-
-public abstract class EpisodeCleanupAlgorithm {
-
- /**
- * Deletes downloaded episodes that are no longer needed. What episodes are deleted and how many
- * of them depends on the implementation.
- *
- * @param context Can be used for accessing the database
- * @param numToRemove An additional parameter. This parameter is either returned by getDefaultCleanupParameter
- * or getPerformCleanupParameter.
- * @return The number of episodes that were deleted.
- */
- protected abstract int performCleanup(Context context, int numToRemove);
-
- public int performCleanup(Context context) {
- return performCleanup(context, getDefaultCleanupParameter());
- }
-
- /**
- * Returns a parameter for performCleanup. The implementation of this interface should decide how much
- * space to free to satisfy the episode cache conditions. If the conditions are already satisfied, this
- * method should not have any effects.
- */
- protected abstract int getDefaultCleanupParameter();
-
- /**
- * Cleans up just enough episodes to make room for the requested number
- *
- * @param context Can be used for accessing the database
- * @param amountOfRoomNeeded the number of episodes we need space for
- * @return The number of epiosdes that were deleted
- */
- public int makeRoomForEpisodes(Context context, int amountOfRoomNeeded) {
- return performCleanup(context, getNumEpisodesToCleanup(amountOfRoomNeeded));
- }
-
- /**
- * @return the number of episodes/items that *could* be cleaned up, if needed
- */
- public abstract int getReclaimableItems();
-
- /**
- * @param amountOfRoomNeeded the number of episodes we want to download
- * @return the number of episodes to delete in order to make room
- */
- int getNumEpisodesToCleanup(final int amountOfRoomNeeded) {
- if (amountOfRoomNeeded >= 0
- && UserPreferences.getEpisodeCacheSize() != UserPreferences.EPISODE_CACHE_SIZE_UNLIMITED) {
- int downloadedEpisodes = DBReader.getTotalEpisodeCount(new FeedItemFilter(FeedItemFilter.DOWNLOADED));
- if (downloadedEpisodes + amountOfRoomNeeded >= UserPreferences
- .getEpisodeCacheSize()) {
-
- return downloadedEpisodes + amountOfRoomNeeded
- - UserPreferences.getEpisodeCacheSize();
- }
- }
- return 0;
- }
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithmFactory.java b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithmFactory.java
deleted file mode 100644
index f4d23e04d..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithmFactory.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import de.danoeh.antennapod.storage.preferences.UserPreferences;
-
-public abstract class EpisodeCleanupAlgorithmFactory {
- public static EpisodeCleanupAlgorithm build() {
- if (!UserPreferences.isEnableAutodownload()) {
- return new APNullCleanupAlgorithm();
- }
- int cleanupValue = UserPreferences.getEpisodeCleanupValue();
- switch (cleanupValue) {
- case UserPreferences.EPISODE_CLEANUP_EXCEPT_FAVORITE:
- return new ExceptFavoriteCleanupAlgorithm();
- case UserPreferences.EPISODE_CLEANUP_QUEUE:
- return new APQueueCleanupAlgorithm();
- case UserPreferences.EPISODE_CLEANUP_NULL:
- return new APNullCleanupAlgorithm();
- default:
- return new APCleanupAlgorithm(cleanupValue);
- }
- }
-}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithm.java
deleted file mode 100644
index 2fc2ef902..000000000
--- a/core/src/main/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithm.java
+++ /dev/null
@@ -1,104 +0,0 @@
-package de.danoeh.antennapod.core.storage;
-
-import android.content.Context;
-import android.util.Log;
-
-import androidx.annotation.NonNull;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-import java.util.Locale;
-import java.util.concurrent.ExecutionException;
-
-import de.danoeh.antennapod.model.feed.FeedItem;
-import de.danoeh.antennapod.model.feed.FeedItemFilter;
-import de.danoeh.antennapod.model.feed.SortOrder;
-import de.danoeh.antennapod.storage.database.DBReader;
-import de.danoeh.antennapod.storage.database.DBWriter;
-import de.danoeh.antennapod.storage.preferences.UserPreferences;
-
-/**
- * A cleanup algorithm that removes any item that isn't a favorite but only if space is needed.
- */
-public class ExceptFavoriteCleanupAlgorithm extends EpisodeCleanupAlgorithm {
-
- private static final String TAG = "ExceptFavCleanupAlgo";
-
- /**
- * The maximum number of episodes that could be cleaned up.
- *
- * @return the number of episodes that *could* be cleaned up, if needed
- */
- public int getReclaimableItems() {
- return getCandidates().size();
- }
-
- @Override
- public int performCleanup(Context context, int numberOfEpisodesToDelete) {
- List<FeedItem> candidates = getCandidates();
- List<FeedItem> delete;
-
- // in the absence of better data, we'll sort by item publication date
- Collections.sort(candidates, (lhs, rhs) -> {
- Date l = lhs.getPubDate();
- Date r = rhs.getPubDate();
-
- if (l != null && r != null) {
- return l.compareTo(r);
- } else {
- // No date - compare by id which should be always incremented
- return Long.compare(lhs.getId(), rhs.getId());
- }
- });
-
- if (candidates.size() > numberOfEpisodesToDelete) {
- delete = candidates.subList(0, numberOfEpisodesToDelete);
- } else {
- delete = candidates;
- }
-
- for (FeedItem item : delete) {
- try {
- DBWriter.deleteFeedMediaOfItem(context, item.getMedia()).get();
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
-
- int counter = delete.size();
- Log.i(TAG, String.format(Locale.US,
- "Auto-delete deleted %d episodes (%d requested)", counter,
- numberOfEpisodesToDelete));
-
- return counter;
- }
-
- @NonNull
- private List<FeedItem> getCandidates() {
- List<FeedItem> candidates = new ArrayList<>();
- List<FeedItem> downloadedItems = DBReader.getEpisodes(0, Integer.MAX_VALUE,
- new FeedItemFilter(FeedItemFilter.DOWNLOADED), SortOrder.DATE_NEW_OLD);
- for (FeedItem item : downloadedItems) {
- if (item.hasMedia()
- && item.getMedia().isDownloaded()
- && !item.isTagged(FeedItem.TAG_FAVORITE)) {
- candidates.add(item);
- }
- }
- return candidates;
- }
-
- @Override
- public int getDefaultCleanupParameter() {
- int cacheSize = UserPreferences.getEpisodeCacheSize();
- if (cacheSize != UserPreferences.EPISODE_CACHE_SIZE_UNLIMITED) {
- int downloadedEpisodes = DBReader.getTotalEpisodeCount(new FeedItemFilter(FeedItemFilter.DOWNLOADED));
- if (downloadedEpisodes > cacheSize) {
- return downloadedEpisodes - cacheSize;
- }
- }
- return 0;
- }
-}