diff options
author | Scott Jackson <daneren2005@gmail.com> | 2012-12-08 11:47:49 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2012-12-08 11:47:49 -0800 |
commit | a6e46790aae2d2cda2cef6f9f3c27fb755c64faa (patch) | |
tree | d54e229d83af3e8856d97271d25213a8ac25f866 /subsonic-android | |
parent | aca5b2838dc622ced1af6c0903d50cc208c83c2f (diff) | |
download | dsub-a6e46790aae2d2cda2cef6f9f3c27fb755c64faa.tar.gz dsub-a6e46790aae2d2cda2cef6f9f3c27fb755c64faa.tar.bz2 dsub-a6e46790aae2d2cda2cef6f9f3c27fb755c64faa.zip |
Added a shorter cache cleaner at end of downloading file
Diffstat (limited to 'subsonic-android')
-rw-r--r-- | subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java | 3 | ||||
-rw-r--r-- | subsonic-android/src/github/daneren2005/dsub/util/CacheCleaner.java | 60 |
2 files changed, 47 insertions, 16 deletions
diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java index d91395c0..9369847e 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadFile.java @@ -304,8 +304,7 @@ public class DownloadFile { if (wifiLock != null) { wifiLock.release(); } - // TODO: Separate into cleanup space and full cleanup - // new CacheCleaner(context, DownloadServiceImpl.getInstance()).clean(); + new CacheCleaner(context, DownloadServiceImpl.getInstance()).cleanSpace(); } } diff --git a/subsonic-android/src/github/daneren2005/dsub/util/CacheCleaner.java b/subsonic-android/src/github/daneren2005/dsub/util/CacheCleaner.java index 732d6a57..eee014b6 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/CacheCleaner.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/CacheCleaner.java @@ -37,6 +37,9 @@ public class CacheCleaner { public void clean() { new BackgroundCleanup().execute(); } + public void cleanSpace() { + new BackgroundSpaceCleanup().execute(); + } public void cleanPlaylists(List<Playlist> playlists) { new BackgroundPlaylistsCleanup().execute(playlists); } @@ -56,21 +59,16 @@ public class CacheCleaner { } } } - - private void deleteFiles(List<File> files, Set<File> undeletable) { - - if (files.isEmpty()) { - return; - } - - long cacheSizeBytes = Util.getCacheSizeMB(context) * 1024L * 1024L; - + + private long getMinimumDelete(List<File> files) { + long cacheSizeBytes = Util.getCacheSizeMB(context) * 1024L * 1024L; + long bytesUsedBySubsonic = 0L; for (File file : files) { bytesUsedBySubsonic += file.length(); } - - // Ensure that file system is not more than 95% full. + + // Ensure that file system is not more than 95% full. StatFs stat = new StatFs(files.get(0).getPath()); long bytesTotalFs = (long) stat.getBlockCount() * (long) stat.getBlockSize(); long bytesAvailableFs = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); @@ -85,6 +83,14 @@ public class CacheCleaner { Log.i(TAG, "Cache limit : " + Util.formatBytes(cacheSizeBytes)); Log.i(TAG, "Cache size before : " + Util.formatBytes(bytesUsedBySubsonic)); Log.i(TAG, "Minimum to delete : " + Util.formatBytes(bytesToDelete)); + + return bytesToDelete; + } + + private void deleteFiles(List<File> files, Set<File> undeletable, long bytesToDelete, boolean deletePartials) { + if (files.isEmpty()) { + return; + } long bytesDeleted = 0L; for (File file : files) { @@ -93,7 +99,7 @@ public class CacheCleaner { // Move artwork to new folder. file.renameTo(FileUtil.getAlbumArtFile(file.getParentFile())); - } else if (bytesToDelete > bytesDeleted || file.getName().endsWith(".partial") || file.getName().contains(".partial.")) { + } else if (bytesToDelete > bytesDeleted || (deletePartials && (file.getName().endsWith(".partial") || file.getName().contains(".partial.")))) { if (!undeletable.contains(file)) { long size = file.length(); if (Util.delete(file)) { @@ -104,7 +110,6 @@ public class CacheCleaner { } Log.i(TAG, "Deleted : " + Util.formatBytes(bytesDeleted)); - Log.i(TAG, "Cache size after : " + Util.formatBytes(bytesUsedBySubsonic - bytesDeleted)); } private void findCandidatesForDeletion(File file, List<File> files, List<File> dirs) { @@ -168,7 +173,7 @@ public class CacheCleaner { Set<File> undeletable = findUndeletableFiles(); - deleteFiles(files, undeletable); + deleteFiles(files, undeletable, getMinimumDelete(files), true); deleteEmptyDirs(dirs, undeletable); } catch (RuntimeException x) { Log.e(TAG, "Error in cache cleaning.", x); @@ -178,6 +183,33 @@ public class CacheCleaner { } } + private class BackgroundSpaceCleanup extends AsyncTask<Void, Void, Void> { + @Override + protected Void doInBackground(Void... params) { + if (downloadService == null) { + Log.e(TAG, "DownloadService not set. Aborting cache cleaning."); + return null; + } + + try { + List<File> files = new ArrayList<File>(); + List<File> dirs = new ArrayList<File>(); + findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, dirs); + + long bytesToDelete = getMinimumDelete(files); + if(bytesToDelete > 0L) { + sortByAscendingModificationTime(files); + Set<File> undeletable = findUndeletableFiles(); + deleteFiles(files, undeletable, bytesToDelete, false); + } + } catch (RuntimeException x) { + Log.e(TAG, "Error in cache cleaning.", x); + } + + return null; + } + } + private class BackgroundPlaylistsCleanup extends AsyncTask<List<Playlist>, Void, Void> { @Override protected Void doInBackground(List<Playlist>... params) { |