aboutsummaryrefslogtreecommitdiff
path: root/src/github/daneren2005/dsub/service/CachedMusicService.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/github/daneren2005/dsub/service/CachedMusicService.java')
-rw-r--r--src/github/daneren2005/dsub/service/CachedMusicService.java47
1 files changed, 40 insertions, 7 deletions
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java
index 59ed550c..8e8e120d 100644
--- a/src/github/daneren2005/dsub/service/CachedMusicService.java
+++ b/src/github/daneren2005/dsub/service/CachedMusicService.java
@@ -156,14 +156,17 @@ public class CachedMusicService implements MusicService {
@Override
public MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
-
+ MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class);
if(!refresh) {
- dir = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class);
+ dir = cached;
}
if(dir == null) {
dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "directory", id));
+
+ // If a cached copy exists to check against, look for removes
+ deleteRemovedEntries(context, dir, cached);
}
return dir;
@@ -172,14 +175,17 @@ public class CachedMusicService implements MusicService {
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
-
+ MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "artist", id), MusicDirectory.class);
if(!refresh) {
- dir = FileUtil.deserialize(context, getCacheName(context, "artist", id), MusicDirectory.class);
+ dir = cached;
}
if(dir == null) {
dir = musicService.getArtist(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "artist", id));
+
+ // If a cached copy exists to check against, look for removes
+ deleteRemovedEntries(context, dir, cached);
}
return dir;
@@ -188,14 +194,17 @@ public class CachedMusicService implements MusicService {
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
-
+ MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "album", id), MusicDirectory.class);
if(!refresh) {
- dir = FileUtil.deserialize(context, getCacheName(context, "album", id), MusicDirectory.class);
+ dir = cached;
}
if(dir == null) {
dir = musicService.getAlbum(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "album", id));
+
+ // If a cached copy exists to check against, look for removes
+ deleteRemovedEntries(context, dir, cached);
}
return dir;
@@ -626,7 +635,12 @@ public class CachedMusicService implements MusicService {
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
return musicService.getSongsByGenre(genre, count, offset, context, progressListener);
}
-
+
+ @Override
+ public MusicDirectory getTopTrackSongs(String artist, int size, Context context, ProgressListener progressListener) throws Exception {
+ return musicService.getTopTrackSongs(artist, size, context, progressListener);
+ }
+
@Override
public List<PodcastChannel> getPodcastChannels(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
checkSettingsChanged(context);
@@ -894,6 +908,25 @@ public class CachedMusicService implements MusicService {
String s = musicService.getRestUrl(context, null, false);
return name + "-" + s.hashCode() + ".ser";
}
+
+ private void deleteRemovedEntries(Context context, MusicDirectory dir, MusicDirectory cached) {
+ if(cached != null) {
+ List<Entry> oldList = new ArrayList<Entry>();
+ oldList.addAll(cached.getChildren());
+
+ // Remove all current items from old list
+ for(Entry entry: dir.getChildren()) {
+ oldList.remove(entry);
+ }
+
+ // Anything remaining has been removed from server
+ MediaStoreService store = new MediaStoreService(context);
+ for(Entry entry: oldList) {
+ File file = FileUtil.getEntryFile(context, entry);
+ Util.recursiveDelete(file, store);
+ }
+ }
+ }
private abstract class SerializeUpdater<T> {
final Context context;