diff options
author | Tom Hennen <tom.hennen@gmail.com> | 2015-09-30 06:33:35 -0400 |
---|---|---|
committer | Tom Hennen <tom.hennen@gmail.com> | 2015-09-30 06:33:35 -0400 |
commit | 60e341cf787eb7fef9c8cf4ce0347117e34771ee (patch) | |
tree | 53250add9639a44583a78b1358cbc7e42ed72b1c | |
parent | 2339fb99e98bb7d5a078aea47095c94df1f88f10 (diff) | |
download | AntennaPod-60e341cf787eb7fef9c8cf4ce0347117e34771ee.zip |
resolved compile time issues. refactored some code
8 files changed, 58 insertions, 27 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/config/DBTasksCallbacksImpl.java b/app/src/main/java/de/danoeh/antennapod/config/DBTasksCallbacksImpl.java index 75dcb2ef1..9f8af1142 100644 --- a/app/src/main/java/de/danoeh/antennapod/config/DBTasksCallbacksImpl.java +++ b/app/src/main/java/de/danoeh/antennapod/config/DBTasksCallbacksImpl.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.config; import de.danoeh.antennapod.core.DBTasksCallbacks; +import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.APCleanupAlgorithm; import de.danoeh.antennapod.core.storage.APDownloadAlgorithm; import de.danoeh.antennapod.core.storage.AutomaticDownloadAlgorithm; @@ -15,6 +16,6 @@ public class DBTasksCallbacksImpl implements DBTasksCallbacks { @Override public EpisodeCleanupAlgorithm getEpisodeCacheCleanupAlgorithm() { - return new APCleanupAlgorithm(); + return UserPreferences.getEpisodeCleanupAlgorithm(); } } 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 1474180e2..1d5bd5031 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 @@ -492,7 +492,7 @@ public class UserPreferences { } - public static EpisodeCleanupAlgorithm<Integer> getEpisodeCleanupAlgorithm() { + public static EpisodeCleanupAlgorithm getEpisodeCleanupAlgorithm() { int cleanupValue = prefs.getInt(PREF_EPISODE_CLEANUP, -1); if (cleanupValue == -1) { return new APQueueCleanupAlgorithm(); 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 e28c902dd..ef834921a 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 @@ -18,7 +18,7 @@ import de.danoeh.antennapod.core.util.LongList; /** * Implementation of the EpisodeCleanupAlgorithm interface used by AntennaPod. */ -public class APCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> { +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 */ @@ -29,7 +29,7 @@ public class APCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> { } @Override - public int performCleanup(Context context, Integer numberOfEpisodesToDelete) { + public int performCleanup(Context context, int numberOfEpisodesToDelete) { List<FeedItem> candidates = new ArrayList<>(); List<FeedItem> downloadedItems = DBReader.getDownloadedItems(); LongList queue = DBReader.getQueueIDList(); @@ -89,7 +89,7 @@ public class APCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> { } @Override - public Integer getDefaultCleanupParameter() { + public int getDefaultCleanupParameter() { return 0; } } 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 efc742be2..9e21a55f2 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 @@ -70,8 +70,8 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { int autoDownloadableEpisodes = candidates.size(); int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes(); - int deletedEpisodes = UserPreferences.getEpisodeCleanupAlgorithm().performCleanup(context, - getPerformAutoCleanupArgs(autoDownloadableEpisodes)); + int deletedEpisodes = UserPreferences.getEpisodeCleanupAlgorithm() + .makeRoomForEpisodes(context, autoDownloadableEpisodes); boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences .getEpisodeCacheSizeUnlimited(); int episodeCacheSize = UserPreferences.getEpisodeCacheSize(); 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 index b17f9fdee..132b61853 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java @@ -6,19 +6,19 @@ import android.util.Log; /** * A cleanup algorithm that never removes anything */ -public class APNullCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> { +public class APNullCleanupAlgorithm extends EpisodeCleanupAlgorithm { private static final String TAG = "APNullCleanupAlgorithm"; @Override - public int performCleanup(Context context, Integer parameter) { + public int performCleanup(Context context, int parameter) { // never clean anything up Log.i(TAG, "performCleanup: Not removing anything"); return 0; } @Override - public Integer getDefaultCleanupParameter() { + public int getDefaultCleanupParameter() { 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 index 18dd46c7a..ebf8ca4cf 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java @@ -18,12 +18,12 @@ import de.danoeh.antennapod.core.util.LongList; * 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 implements EpisodeCleanupAlgorithm<Integer> { +public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm { private static final String TAG = "APQueueCleanupAlgorithm"; @Override - public int performCleanup(Context context, Integer numberOfEpisodesToDelete) { + public int performCleanup(Context context, int numberOfEpisodesToDelete) { List<FeedItem> candidates = new ArrayList<>(); List<FeedItem> downloadedItems = DBReader.getDownloadedItems(); LongList queue = DBReader.getQueueIDList(); @@ -73,7 +73,7 @@ public class APQueueCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> } @Override - public Integer getDefaultCleanupParameter() { + public int getDefaultCleanupParameter() { return 0; } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 2fc7e52bd..f54e13471 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -332,9 +332,7 @@ public final class DBTasks { @Override public void run() { ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm() - .performCleanup(context, - ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm() - .getPerformCleanupParameter(items.length)); + .makeRoomForEpisodes(context, items.length); } }.start(); @@ -390,8 +388,7 @@ public final class DBTasks { * @param context Used for accessing the DB. */ public static void performAutoCleanup(final Context context) { - ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm().performCleanup(context, - ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm().getDefaultCleanupParameter()); + ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm().performCleanup(context); } /** 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 index 484263f97..0f402745c 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java @@ -2,27 +2,60 @@ package de.danoeh.antennapod.core.storage; import android.content.Context; -import java.util.List; +import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.feed.FeedItem; - -public interface EpisodeCleanupAlgorithm<T> { +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 parameter An additional parameter. This parameter is either returned by getDefaultCleanupParameter - * or getPerformCleanupParameter. + * @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. */ - int performCleanup(Context context, T parameter); + public 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. */ - T getDefaultCleanupParameter(); + public 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)); + } + + /** + * @param amountOfRoomNeeded the number of episodes we want to download + * @return the number of episodes to delete in order to make room + */ + protected int getNumEpisodesToCleanup(final int amountOfRoomNeeded) { + if (amountOfRoomNeeded >= 0 + && UserPreferences.getEpisodeCacheSize() != UserPreferences + .getEpisodeCacheSizeUnlimited()) { + int downloadedEpisodes = DBReader + .getNumberOfDownloadedEpisodes(); + if (downloadedEpisodes + amountOfRoomNeeded >= UserPreferences + .getEpisodeCacheSize()) { + + return downloadedEpisodes + amountOfRoomNeeded + - UserPreferences.getEpisodeCacheSize(); + } + } + return 0; + } } |