From 22703eb8084fb00c20400881053dc42674b58d3a Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 18 Feb 2014 15:37:59 -0800 Subject: #167 CacheCleaner runs off of BackgroundTask --- src/github/daneren2005/dsub/util/CacheCleaner.java | 49 ++++++++++++++-------- .../dsub/util/SilentBackgroundTask.java | 5 +++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/github/daneren2005/dsub/util/CacheCleaner.java b/src/github/daneren2005/dsub/util/CacheCleaner.java index 102c1dc9..273ed05f 100644 --- a/src/github/daneren2005/dsub/util/CacheCleaner.java +++ b/src/github/daneren2005/dsub/util/CacheCleaner.java @@ -9,7 +9,6 @@ import java.util.List; import java.util.Set; import android.content.Context; -import android.os.AsyncTask; import android.util.Log; import android.os.StatFs; import github.daneren2005.dsub.domain.Playlist; @@ -35,13 +34,13 @@ public class CacheCleaner { } public void clean() { - new BackgroundCleanup().execute(); + new BackgroundCleanup(context).execute(); } public void cleanSpace() { - new BackgroundSpaceCleanup().execute(); + new BackgroundSpaceCleanup(context).execute(); } public void cleanPlaylists(List playlists) { - new BackgroundPlaylistsCleanup().execute(playlists); + new BackgroundPlaylistsCleanup(context, playlists).execute(); } private void deleteEmptyDirs(List dirs, Set undeletable) { @@ -166,9 +165,13 @@ public class CacheCleaner { return undeletable; } - private class BackgroundCleanup extends AsyncTask { + private class BackgroundCleanup extends SilentBackgroundTask { + public BackgroundCleanup(Context context) { + super(context); + } + @Override - protected Void doInBackground(Void... params) { + protected Void doInBackground() { if (downloadService == null) { Log.e(TAG, "DownloadService not set. Aborting cache cleaning."); return null; @@ -189,14 +192,18 @@ public class CacheCleaner { } catch (RuntimeException x) { Log.e(TAG, "Error in cache cleaning.", x); } - + return null; } } - - private class BackgroundSpaceCleanup extends AsyncTask { + + private class BackgroundSpaceCleanup extends SilentBackgroundTask { + public BackgroundSpaceCleanup(Context context) { + super(context); + } + @Override - protected Void doInBackground(Void... params) { + protected Void doInBackground() { if (downloadService == null) { Log.e(TAG, "DownloadService not set. Aborting cache cleaning."); return null; @@ -207,7 +214,7 @@ public class CacheCleaner { List pinned = new ArrayList(); List dirs = new ArrayList(); findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs); - + long bytesToDelete = getMinimumDelete(files, pinned); if(bytesToDelete > 0L) { sortByAscendingModificationTime(files); @@ -217,29 +224,35 @@ public class CacheCleaner { } catch (RuntimeException x) { Log.e(TAG, "Error in cache cleaning.", x); } - + return null; } } - - private class BackgroundPlaylistsCleanup extends AsyncTask, Void, Void> { + + private class BackgroundPlaylistsCleanup extends SilentBackgroundTask { + private final List playlists; + + public BackgroundPlaylistCleanup(Context context, List playlists) { + super(context); + this.playlists = playlists; + } + @Override - protected Void doInBackground(List... params) { + protected Void doInBackground() { try { String server = Util.getServerName(context); SortedSet playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(server)); - List playlists = params[0]; for (Playlist playlist : playlists) { playlistFiles.remove(FileUtil.getPlaylistFile(server, playlist.getName())); } - + for(File playlist : playlistFiles) { playlist.delete(); } } catch (RuntimeException x) { Log.e(TAG, "Error in playlist cache cleaning.", x); } - + return null; } } diff --git a/src/github/daneren2005/dsub/util/SilentBackgroundTask.java b/src/github/daneren2005/dsub/util/SilentBackgroundTask.java index a0db7ca4..b99b7e0e 100644 --- a/src/github/daneren2005/dsub/util/SilentBackgroundTask.java +++ b/src/github/daneren2005/dsub/util/SilentBackgroundTask.java @@ -33,6 +33,11 @@ public abstract class SilentBackgroundTask extends BackgroundTask { queue.offer(task = new Task()); } + @Override + protected void done(T result) { + // Don't do anything unless overriden + } + @Override public void updateProgress(int messageId) { } -- cgit v1.2.3