diff options
Diffstat (limited to 'src/github')
-rw-r--r-- | src/github/daneren2005/dsub/service/CachedMusicService.java | 8 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/service/RESTMusicService.java | 27 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/FileUtil.java | 21 |
3 files changed, 30 insertions, 26 deletions
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 0826e967..34a0118d 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -188,12 +188,18 @@ public class CachedMusicService implements MusicService { @Override public MusicDirectory getPlaylist(boolean refresh, String id, String name, Context context, ProgressListener progressListener) throws Exception { MusicDirectory dir = null; + MusicDirectory cachedPlaylist = FileUtil.deserialize(context, getCacheName(context, "playlist", id), MusicDirectory.class); if(!refresh) { - dir = FileUtil.deserialize(context, getCacheName(context, "playlist", id), MusicDirectory.class); + dir = cachedPlaylist; } if(dir == null) { dir = musicService.getPlaylist(refresh, id, name, context, progressListener); FileUtil.serialize(context, dir, getCacheName(context, "playlist", id)); + + if(cachedPlaylist == null || !cachedPlaylist.equals(dir)) { + File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context, musicService.getInstance(context)), name); + FileUtil.writePlaylistFile(playlistFile, dir); + } } return dir; } diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 512a446a..8ea29a41 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -341,30 +341,7 @@ public class RESTMusicService implements MusicService { Reader reader = getReader(context, progressListener, "getPlaylist", params, "id", id); try { - MusicDirectory playlist = new PlaylistParser(context).parse(reader, progressListener); - - File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context), name); - FileWriter fw = new FileWriter(playlistFile); - BufferedWriter bw = new BufferedWriter(fw); - try { - fw.write("#EXTM3U\n"); - for (MusicDirectory.Entry e : playlist.getChildren()) { - String filePath = FileUtil.getSongFile(context, e).getAbsolutePath(); - if(! new File(filePath).exists()){ - String ext = FileUtil.getExtension(filePath); - String base = FileUtil.getBaseName(filePath); - filePath = base + ".complete." + ext; - } - fw.write(filePath + "\n"); - } - } catch(Exception e) { - Log.w(TAG, "Failed to save playlist: " + name); - } finally { - bw.close(); - fw.close(); - } - - return playlist; + return new PlaylistParser(context).parse(reader, progressListener); } finally { Util.close(reader); } @@ -1505,7 +1482,7 @@ public class RESTMusicService implements MusicService { return networkInfo == null ? -1 : networkInfo.getType(); } - private int getInstance(Context context) { + public int getInstance(Context context) { if(instance == null) { return Util.getActiveServer(context); } else { diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index 3c24143c..7d106305 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -126,6 +126,27 @@ public class FileUtil { File playlistDir = getPlaylistDirectory(server); return new File(playlistDir, fileSystemSafe(name) + ".m3u"); } + public static void writePlaylistFile(File file, MusicDirectory playlist) { + FileWriter fw = new FileWriter(file); + BufferedWriter bw = new BufferedWriter(fw); + try { + fw.write("#EXTM3U\n"); + for (MusicDirectory.Entry e : playlist.getChildren()) { + String filePath = FileUtil.getSongFile(context, e).getAbsolutePath(); + if(! new File(filePath).exists()){ + String ext = FileUtil.getExtension(filePath); + String base = FileUtil.getBaseName(filePath); + filePath = base + ".complete." + ext; + } + fw.write(filePath + "\n"); + } + } catch(Exception e) { + Log.w(TAG, "Failed to save playlist: " + playlist.getName()); + } finally { + bw.close(); + fw.close(); + } + } public static File getPlaylistDirectory() { File playlistDir = new File(getSubsonicDirectory(), "playlists"); ensureDirectoryExistsAndIsReadWritable(playlistDir); |