From 28af3222ac09cf1b4118fa1a9f646d6ad9622e9d Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 6 Jul 2012 20:36:08 -0700 Subject: Offline playlists --- .../androidapp/activity/SelectAlbumActivity.java | 4 +-- .../androidapp/activity/SubsonicTabActivity.java | 1 - .../androidapp/service/CachedMusicService.java | 4 +-- .../subsonic/androidapp/service/MusicService.java | 2 +- .../androidapp/service/OfflineMusicService.java | 35 ++++++++++++++++++++-- .../androidapp/service/RESTMusicService.java | 21 +++++++++++-- .../subsonic/androidapp/util/FileUtil.java | 10 +++++++ 7 files changed, 66 insertions(+), 11 deletions(-) (limited to 'subsonic-android/src/net') diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SelectAlbumActivity.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SelectAlbumActivity.java index b354599f..070bed0b 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SelectAlbumActivity.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SelectAlbumActivity.java @@ -279,13 +279,13 @@ public class SelectAlbumActivity extends SubsonicTabActivity { }.execute(); } - private void getPlaylist(final String playlistId, String playlistName) { + private void getPlaylist(final String playlistId, final String playlistName) { setTitle(playlistName); new LoadTask() { @Override protected MusicDirectory load(MusicService service) throws Exception { - return service.getPlaylist(playlistId, SelectAlbumActivity.this, this); + return service.getPlaylist(playlistId, playlistName, SelectAlbumActivity.this, this); } }.execute(); } diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SubsonicTabActivity.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SubsonicTabActivity.java index 8c9c0687..f75e39fc 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SubsonicTabActivity.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/activity/SubsonicTabActivity.java @@ -240,7 +240,6 @@ public class SubsonicTabActivity extends Activity { private void updateButtonVisibility() { int visibility = Util.isOffline(this) ? View.GONE : View.VISIBLE; searchButton.setVisibility(visibility); - playlistButton.setVisibility(visibility); } public void setProgressVisible(boolean visible) { diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/CachedMusicService.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/CachedMusicService.java index a06f3995..14aed954 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/CachedMusicService.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/CachedMusicService.java @@ -128,8 +128,8 @@ public class CachedMusicService implements MusicService { } @Override - public MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception { - return musicService.getPlaylist(id, context, progressListener); + public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception { + return musicService.getPlaylist(id, name, context, progressListener); } @Override diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/MusicService.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/MusicService.java index 2acb4c65..cb0c5709 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/MusicService.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/MusicService.java @@ -53,7 +53,7 @@ public interface MusicService { SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception; - MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception; + MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception; List getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception; diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/OfflineMusicService.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/OfflineMusicService.java index 6a8ad6d0..79fee6d2 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/OfflineMusicService.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/OfflineMusicService.java @@ -21,6 +21,8 @@ package net.sourceforge.subsonic.androidapp.service; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.io.Reader; +import java.io.FileReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -41,6 +43,7 @@ import net.sourceforge.subsonic.androidapp.domain.MusicFolder; import net.sourceforge.subsonic.androidapp.domain.Playlist; import net.sourceforge.subsonic.androidapp.domain.SearchCritera; import net.sourceforge.subsonic.androidapp.domain.SearchResult; +import net.sourceforge.subsonic.androidapp.service.parser.PlaylistParser; import net.sourceforge.subsonic.androidapp.util.Constants; import net.sourceforge.subsonic.androidapp.util.FileUtil; import net.sourceforge.subsonic.androidapp.util.ProgressListener; @@ -150,12 +153,38 @@ public class OfflineMusicService extends RESTMusicService { @Override public List getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception { - throw new OfflineException("Playlists not available in offline mode"); + List playlists = new ArrayList(); + File root = FileUtil.getPlaylistDirectory(); + for (File file : FileUtil.listFiles(root)) { + Playlist playlist = new Playlist(file.getName(), file.getName()); + playlists.add(playlist); + } + return playlists; } @Override - public MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception { - throw new OfflineException("Playlists not available in offline mode"); + public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception { + DownloadService downloadService = DownloadServiceImpl.getInstance(); + if (downloadService == null) { + return new MusicDirectory(); + } + + Reader reader = null; + try { + reader = new FileReader(FileUtil.getPlaylistFile(name)); + MusicDirectory fullList = new PlaylistParser(context).parse(reader, progressListener); + MusicDirectory playlist = new MusicDirectory(); + for(MusicDirectory.Entry song: fullList.getChildren()) { + DownloadFile downloadFile = downloadService.forSong(song); + File completeFile = downloadFile.getCompleteFile(); + if(completeFile.exists()) { + playlist.addChild(song); + } + } + return playlist; + } finally { + Util.close(reader); + } } @Override diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/RESTMusicService.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/RESTMusicService.java index 1d99f2d9..a939feef 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/RESTMusicService.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/service/RESTMusicService.java @@ -22,7 +22,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.FileReader; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.Reader; import java.net.URLEncoder; import java.util.ArrayList; @@ -317,13 +319,28 @@ public class RESTMusicService implements MusicService { } @Override - public MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception { + public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setSoTimeout(params, SOCKET_READ_TIMEOUT_GET_PLAYLIST); Reader reader = getReader(context, progressListener, "getPlaylist", params, "id", id); + OutputStreamWriter out = null; + try { + out = new OutputStreamWriter(new FileOutputStream(FileUtil.getPlaylistFile(name))); + + char[] buff = new char[256]; + int n; + while((n = reader.read(buff)) >= 0) { + out.write(buff, 0, n); + } + } finally { + Util.close(out); + Util.close(reader); + } + try { - return new PlaylistParser(context).parse(reader, progressListener); + reader = new FileReader(FileUtil.getPlaylistFile(name)); + return new PlaylistParser(context).parse(reader, progressListener); } finally { Util.close(reader); } diff --git a/subsonic-android/src/net/sourceforge/subsonic/androidapp/util/FileUtil.java b/subsonic-android/src/net/sourceforge/subsonic/androidapp/util/FileUtil.java index 6cdd6fb1..88d10d3e 100644 --- a/subsonic-android/src/net/sourceforge/subsonic/androidapp/util/FileUtil.java +++ b/subsonic-android/src/net/sourceforge/subsonic/androidapp/util/FileUtil.java @@ -70,6 +70,16 @@ public class FileUtil { return new File(dir, fileName.toString()); } + + public static File getPlaylistFile(String id) { + File playlistDir = getPlaylistDirectory(); + return new File(playlistDir, id); + } + public static File getPlaylistDirectory() { + File playlistDir = new File(getSubsonicDirectory(), "playlists"); + ensureDirectoryExistsAndIsReadWritable(playlistDir); + return playlistDir; + } public static File getAlbumArtFile(Context context, MusicDirectory.Entry entry) { File albumDir = getAlbumDirectory(context, entry); -- cgit v1.2.3