diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-09-16 14:24:36 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-09-16 14:24:36 -0700 |
commit | ca0d8048bb62400780dbe814a18628ce5f86344d (patch) | |
tree | dea1cc12ff06356524a2b5b14f1cbd08deb3150d /src | |
parent | a08edbf859fbbbc4d2686a26fb182d5332bb65dd (diff) | |
download | dsub-ca0d8048bb62400780dbe814a18628ce5f86344d.tar.gz dsub-ca0d8048bb62400780dbe814a18628ce5f86344d.tar.bz2 dsub-ca0d8048bb62400780dbe814a18628ce5f86344d.zip |
#403 Make sure artwork folder never grows beyond limit, delete LRU
Diffstat (limited to 'src')
-rw-r--r-- | src/github/daneren2005/dsub/util/CacheCleaner.java | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/github/daneren2005/dsub/util/CacheCleaner.java b/src/github/daneren2005/dsub/util/CacheCleaner.java index cbc2389a..89e2d460 100644 --- a/src/github/daneren2005/dsub/util/CacheCleaner.java +++ b/src/github/daneren2005/dsub/util/CacheCleaner.java @@ -26,6 +26,7 @@ public class CacheCleaner { private static final String TAG = CacheCleaner.class.getSimpleName(); private static final long MIN_FREE_SPACE = 500 * 1024L * 1024L; + private static final long MAX_COVER_ART_SPACE = 100 * 1024L * 1024L; private final Context context; private final DownloadService downloadService; @@ -169,6 +170,41 @@ public class CacheCleaner { undeletable.add(FileUtil.getMusicDirectory(context)); return undeletable; } + + private void cleanupCoverArt(Context context) { + File dir = FileUtil.getAlbumArtDirectory(context); + + List<File> files = new ArrayList<File>(); + long bytesUsed = 0L; + for(File file: dir.listFiles()) { + if(file.isFile()) { + files.add(file); + bytesUsed += file.length(); + } + } + + // Don't waste time sorting if under limit already + if(bytesUsed < MAX_COVER_ART_SPACE) { + return; + } + + sortByAscendingModificationTime(files); + long bytesDeleted = 0L; + for(File file: files) { + // End as soon as the space used is below the threshold + if(bytesUsed < MAX_COVER_ART_SPACE) { + break; + } + + long bytes = file.length(); + if(file.delete()) { + bytesUsed -= bytes; + bytesDeleted += bytes; + } + } + + Log.i(TAG, "Deleted " + Util.formatBytes(bytesDeleted) + " worth of cover art"); + } private class BackgroundCleanup extends SilentBackgroundTask<Void> { public BackgroundCleanup(Context context) { @@ -194,6 +230,9 @@ public class CacheCleaner { deleteFiles(files, undeletable, getMinimumDelete(files, pinned), true); deleteEmptyDirs(dirs, undeletable); + + // Make sure cover art directory does not grow too large + cleanupCoverArt(); } catch (RuntimeException x) { Log.e(TAG, "Error in cache cleaning.", x); } |