resolved compile time issues. refactored some code

This commit is contained in:
Tom Hennen 2015-09-30 06:33:35 -04:00
parent 2339fb99e9
commit 60e341cf78
8 changed files with 58 additions and 27 deletions

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
/**

View File

@ -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;
}
}