mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-27 17:07:40 +01:00
Prevent leaks by making cleanup tasks static
This commit is contained in:
parent
b25a118148
commit
7922d6caad
@ -13,7 +13,6 @@ import java.io.File;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -30,9 +29,6 @@ public class CacheCleaner
|
|||||||
{
|
{
|
||||||
private static final long MIN_FREE_SPACE = 500 * 1024L * 1024L;
|
private static final long MIN_FREE_SPACE = 500 * 1024L * 1024L;
|
||||||
|
|
||||||
private Lazy<Downloader> downloader = inject(Downloader.class);
|
|
||||||
private Lazy<ActiveServerProvider> activeServerProvider = inject(ActiveServerProvider.class);
|
|
||||||
|
|
||||||
public CacheCleaner()
|
public CacheCleaner()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -76,7 +72,7 @@ public class CacheCleaner
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteEmptyDirs(Iterable<File> dirs, Collection<File> doNotDelete)
|
private static void deleteEmptyDirs(Iterable<File> dirs, Collection<File> doNotDelete)
|
||||||
{
|
{
|
||||||
for (File dir : dirs)
|
for (File dir : dirs)
|
||||||
{
|
{
|
||||||
@ -105,7 +101,7 @@ public class CacheCleaner
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getMinimumDelete(List<File> files)
|
private static long getMinimumDelete(List<File> files)
|
||||||
{
|
{
|
||||||
if (files.isEmpty())
|
if (files.isEmpty())
|
||||||
{
|
{
|
||||||
@ -194,20 +190,14 @@ public class CacheCleaner
|
|||||||
|
|
||||||
private static void sortByAscendingModificationTime(List<File> files)
|
private static void sortByAscendingModificationTime(List<File> files)
|
||||||
{
|
{
|
||||||
Collections.sort(files, new Comparator<File>()
|
Collections.sort(files, (a, b) -> Long.compare(a.lastModified(), b.lastModified()));
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public int compare(File a, File b)
|
|
||||||
{
|
|
||||||
return Long.compare(a.lastModified(), b.lastModified());
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<File> findFilesToNotDelete()
|
private static Set<File> findFilesToNotDelete()
|
||||||
{
|
{
|
||||||
Set<File> filesToNotDelete = new HashSet<File>(5);
|
Set<File> filesToNotDelete = new HashSet<>(5);
|
||||||
|
|
||||||
|
Lazy<Downloader> downloader = inject(Downloader.class);
|
||||||
|
|
||||||
for (DownloadFile downloadFile : downloader.getValue().getDownloads())
|
for (DownloadFile downloadFile : downloader.getValue().getDownloads())
|
||||||
{
|
{
|
||||||
@ -219,7 +209,7 @@ public class CacheCleaner
|
|||||||
return filesToNotDelete;
|
return filesToNotDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BackgroundCleanup extends AsyncTask<Void, Void, Void>
|
private static class BackgroundCleanup extends AsyncTask<Void, Void, Void>
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params)
|
protected Void doInBackground(Void... params)
|
||||||
@ -227,8 +217,8 @@ public class CacheCleaner
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Thread.currentThread().setName("BackgroundCleanup");
|
Thread.currentThread().setName("BackgroundCleanup");
|
||||||
List<File> files = new ArrayList<File>();
|
List<File> files = new ArrayList<>();
|
||||||
List<File> dirs = new ArrayList<File>();
|
List<File> dirs = new ArrayList<>();
|
||||||
|
|
||||||
findCandidatesForDeletion(FileUtil.getMusicDirectory(), files, dirs);
|
findCandidatesForDeletion(FileUtil.getMusicDirectory(), files, dirs);
|
||||||
sortByAscendingModificationTime(files);
|
sortByAscendingModificationTime(files);
|
||||||
@ -247,7 +237,7 @@ public class CacheCleaner
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BackgroundSpaceCleanup extends AsyncTask<Void, Void, Void>
|
private static class BackgroundSpaceCleanup extends AsyncTask<Void, Void, Void>
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params)
|
protected Void doInBackground(Void... params)
|
||||||
@ -255,8 +245,8 @@ public class CacheCleaner
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Thread.currentThread().setName("BackgroundSpaceCleanup");
|
Thread.currentThread().setName("BackgroundSpaceCleanup");
|
||||||
List<File> files = new ArrayList<File>();
|
List<File> files = new ArrayList<>();
|
||||||
List<File> dirs = new ArrayList<File>();
|
List<File> dirs = new ArrayList<>();
|
||||||
findCandidatesForDeletion(FileUtil.getMusicDirectory(), files, dirs);
|
findCandidatesForDeletion(FileUtil.getMusicDirectory(), files, dirs);
|
||||||
|
|
||||||
long bytesToDelete = getMinimumDelete(files);
|
long bytesToDelete = getMinimumDelete(files);
|
||||||
@ -276,13 +266,14 @@ public class CacheCleaner
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BackgroundPlaylistsCleanup extends AsyncTask<List<Playlist>, Void, Void>
|
private static class BackgroundPlaylistsCleanup extends AsyncTask<List<Playlist>, Void, Void>
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(List<Playlist>... params)
|
protected Void doInBackground(List<Playlist>... params)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
Lazy<ActiveServerProvider> activeServerProvider = inject(ActiveServerProvider.class);
|
||||||
Thread.currentThread().setName("BackgroundPlaylistsCleanup");
|
Thread.currentThread().setName("BackgroundPlaylistsCleanup");
|
||||||
String server = activeServerProvider.getValue().getActiveServer().getName();
|
String server = activeServerProvider.getValue().getActiveServer().getName();
|
||||||
SortedSet<File> playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(server));
|
SortedSet<File> playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(server));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user