don't show space warning if we can autodelete stuff.

This commit is contained in:
Tom Hennen 2016-01-31 09:51:35 -05:00
parent 0288767d06
commit 79b5328ecf
5 changed files with 75 additions and 32 deletions

View File

@ -228,7 +228,10 @@ public class NavListAdapter extends BaseAdapter
}
} else if(tag.equals(DownloadsFragment.TAG) && UserPreferences.isEnableAutodownload()) {
int epCacheSize = UserPreferences.getEpisodeCacheSize();
if(itemAccess.getNumberOfDownloadedItems() >= epCacheSize) {
// don't count episodes that we could delete when autodownloading
int spaceUsed = itemAccess.getNumberOfDownloadedItems() -
UserPreferences.getEpisodeCleanupAlgorithm().getAvailableSpace();
if (spaceUsed >= epCacheSize) {
holder.count.setText("{md-disc-full 150%}");
Iconify.addIcons(holder.count);
holder.count.setVisibility(View.VISIBLE);

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.storage;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.ArrayList;
@ -28,30 +29,18 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm {
this.numberOfDaysAfterPlayback = numberOfDaysAfterPlayback;
}
/**
* @return the number of episodes that *could* be cleaned up, if needed
*/
public int getAvailableSpace()
{
return getCandidates().size();
}
@Override
public int performCleanup(Context context, int numberOfEpisodesToDelete) {
List<FeedItem> candidates = new ArrayList<>();
List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
List<FeedItem> candidates = getCandidates();
List<FeedItem> delete;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1 * numberOfDaysAfterPlayback);
Date mostRecentDateForDeletion = cal.getTime();
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);
}
}
}
Collections.sort(candidates, (lhs, rhs) -> {
Date l = lhs.getMedia().getPlaybackCompletionDate();
@ -90,6 +79,32 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm {
return counter;
}
@NonNull
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();
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);

View File

@ -21,4 +21,9 @@ public class APNullCleanupAlgorithm extends EpisodeCleanupAlgorithm {
public int getDefaultCleanupParameter() {
return 0;
}
@Override
public int getAvailableSpace() {
return 0;
}
}

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.storage;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.ArrayList;
@ -22,19 +23,18 @@ 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 getAvailableSpace()
{
return getCandidates().size();
}
@Override
public int performCleanup(Context context, int numberOfEpisodesToDelete) {
List<FeedItem> candidates = new ArrayList<>();
List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
List<FeedItem> candidates = getCandidates();
List<FeedItem> delete;
for (FeedItem item : downloadedItems) {
if (item.hasMedia()
&& item.getMedia().isDownloaded()
&& !item.isTagged(FeedItem.TAG_QUEUE)
&& !item.isTagged(FeedItem.TAG_FAVORITE)) {
candidates.add(item);
}
}
// in the absence of better data, we'll sort by item publication date
Collections.sort(candidates, (lhs, rhs) -> {
@ -74,6 +74,21 @@ public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm {
return counter;
}
@NonNull
private List<FeedItem> getCandidates() {
List<FeedItem> candidates = new ArrayList<>();
List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
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);

View File

@ -39,6 +39,11 @@ public abstract class EpisodeCleanupAlgorithm {
return performCleanup(context, getNumEpisodesToCleanup(amountOfRoomNeeded));
}
/**
* @return the number of episodes that *could* be cleaned up, if needed
*/
public abstract int getAvailableSpace();
/**
* @param amountOfRoomNeeded the number of episodes we want to download
* @return the number of episodes to delete in order to make room