From bf7acf155e730b22ae17f0bb302b14c7f1076009 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 5 Jun 2013 20:36:51 -0700 Subject: Added option to overwrite existing playlists for 4.7+ --- .../dsub/fragments/SelectDirectoryFragment.java | 4 +- .../dsub/fragments/SubsonicFragment.java | 49 ++++++++++++++++++++-- .../dsub/service/CachedMusicService.java | 5 +++ .../daneren2005/dsub/service/DownloadService.java | 4 +- .../dsub/service/DownloadServiceImpl.java | 9 +++- .../daneren2005/dsub/service/MusicService.java | 2 + .../dsub/service/OfflineMusicService.java | 5 +++ .../daneren2005/dsub/service/RESTMusicService.java | 25 +++++++++++ 8 files changed, 97 insertions(+), 6 deletions(-) (limited to 'subsonic-android/src/github') diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index 13f61625..2bb04fa3 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -492,7 +492,9 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter warnIfNetworkOrStorageUnavailable(); getDownloadService().download(songs, save, autoplay, playNext, shuffle); if (playlistName != null) { - getDownloadService().setSuggestedPlaylistName(playlistName); + getDownloadService().setSuggestedPlaylistName(playlistName, playlistId); + } else { + getDownloadService().setSuggestedPlaylistName(null, null); } if (autoplay) { Util.startActivityWithoutTransition(context, DownloadActivity.class); diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicFragment.java index c6a0e89d..35a97e7c 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -34,8 +34,8 @@ import android.view.ContextMenu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; -import android.widget.AdapterView; import android.widget.Button; +import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import com.actionbarsherlock.app.SherlockFragment; @@ -69,7 +69,6 @@ import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -696,10 +695,21 @@ public class SubsonicFragment extends SherlockFragment { protected void createNewPlaylist(final List songs, boolean getSuggestion) { View layout = context.getLayoutInflater().inflate(R.layout.save_playlist, null); final EditText playlistNameView = (EditText) layout.findViewById(R.id.save_playlist_name); + final CheckBox overwriteCheckBox = (CheckBox) layout.findViewById(R.id.save_playlist_overwrite); if(getSuggestion) { String playlistName = (getDownloadService() != null) ? getDownloadService().getSuggestedPlaylistName() : null; if (playlistName != null) { playlistNameView.setText(playlistName); + Version version = Util.getServerRestVersion(context); + Version updatePlaylistVersion = new Version("1.8.0"); + try { + if(version.compareTo(updatePlaylistVersion) >= 0 && Integer.parseInt(getDownloadService().getSuggestedPlaylistId()) != -1) { + overwriteCheckBox.setChecked(true); + overwriteCheckBox.setVisibility(View.VISIBLE); + } + } catch(Exception e) { + Log.d(TAG, "Playlist id isn't a integer, probably MusicCabinet"); + } } else { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); playlistNameView.setText(dateFormat.format(new Date())); @@ -716,7 +726,11 @@ public class SubsonicFragment extends SherlockFragment { .setPositiveButton(R.string.common_save, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { - createNewPlaylist(songs, String.valueOf(playlistNameView.getText())); + if(overwriteCheckBox.isChecked()) { + overwritePlaylist(songs, String.valueOf(playlistNameView.getText()), getDownloadService().getSuggestedPlaylistId()); + } else { + createNewPlaylist(songs, String.valueOf(playlistNameView.getText())); + } } }) .setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() { @@ -751,6 +765,35 @@ public class SubsonicFragment extends SherlockFragment { } }.execute(); } + private void overwritePlaylist(final List songs, final String name, final String id) { + new SilentBackgroundTask(context) { + @Override + protected Void doInBackground() throws Throwable { + MusicService musicService = MusicServiceFactory.getMusicService(context); + MusicDirectory playlist = musicService.getPlaylist(id, name, context, null); + List toDelete = playlist.getChildren(); + musicService.overwritePlaylist(id, name, toDelete.size(), songs, context, null); + return null; + } + + @Override + protected void done(Void result) { + Util.toast(context, R.string.download_playlist_done); + } + + @Override + protected void error(Throwable error) { + String msg; + if (error instanceof OfflineException || error instanceof ServerTooOldException) { + msg = getErrorMessage(error); + } else { + msg = context.getResources().getString(R.string.download_playlist_error) + " " + getErrorMessage(error); + } + + Util.toast(context, msg, false); + } + }.execute(); + } public void displaySongInfo(final MusicDirectory.Entry song) { Integer bitrate = null; diff --git a/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java index 314f065e..3802ab3e 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -168,6 +168,11 @@ public class CachedMusicService implements MusicService { musicService.removeFromPlaylist(id, toRemove, context, progressListener); } + @Override + public void overwritePlaylist(String id, String name, int toRemove, List toAdd, Context context, ProgressListener progressListener) throws Exception { + musicService.overwritePlaylist(id, name, toRemove, toAdd, context, progressListener); + } + @Override public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception { musicService.updatePlaylist(id, name, comment, pub, context, progressListener); diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadService.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadService.java index 43e24096..328cc962 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadService.java @@ -107,10 +107,12 @@ public interface DownloadService { long getDownloadListUpdateRevision(); - void setSuggestedPlaylistName(String name); + void setSuggestedPlaylistName(String name, String id); String getSuggestedPlaylistName(); + String getSuggestedPlaylistId(); + boolean getEqualizerAvailable(); boolean getVisualizerAvailable(); diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java index f659a434..7858ac3a 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java @@ -110,6 +110,7 @@ public class DownloadServiceImpl extends Service implements DownloadService { private long revision; private static DownloadService instance; private String suggestedPlaylistName; + private String suggestedPlaylistId; private PowerManager.WakeLock wakeLock; private boolean keepScreenOn; private int cachedPosition = 0; @@ -931,8 +932,9 @@ public class DownloadServiceImpl extends Service implements DownloadService { } @Override - public void setSuggestedPlaylistName(String name) { + public void setSuggestedPlaylistName(String name, String id) { this.suggestedPlaylistName = name; + this.suggestedPlaylistId = id; } @Override @@ -940,6 +942,11 @@ public class DownloadServiceImpl extends Service implements DownloadService { return suggestedPlaylistName; } + @Override + public String getSuggestedPlaylistId() { + return suggestedPlaylistId; + } + @Override public boolean getEqualizerAvailable() { return equalizerAvailable; diff --git a/subsonic-android/src/github/daneren2005/dsub/service/MusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/MusicService.java index f69ebfb1..1b60cc37 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/MusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/MusicService.java @@ -70,6 +70,8 @@ public interface MusicService { void removeFromPlaylist(String id, List toRemove, Context context, ProgressListener progressListener) throws Exception; + void overwritePlaylist(String id, String name, int toRemove, List toAdd, Context context, ProgressListener progressListener) throws Exception; + void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception; Lyrics getLyrics(String artist, String title, Context context, ProgressListener progressListener) throws Exception; diff --git a/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java index 9af58b16..e20a64de 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -417,6 +417,11 @@ public class OfflineMusicService extends RESTMusicService { throw new OfflineException("Removing from playlist not available in offline mode"); } + @Override + public void overwritePlaylist(String id, String name, int toRemove, List toAdd, Context context, ProgressListener progressListener) throws Exception { + throw new OfflineException("Overwriting playlist not available in offline mode"); + } + @Override public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception { throw new OfflineException("Updating playlist not available in offline mode"); diff --git a/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java index f4f64046..dae4944c 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -441,6 +441,31 @@ public class RESTMusicService implements MusicService { } } + @Override + public void overwritePlaylist(String id, String name, int toRemove, List toAdd, Context context, ProgressListener progressListener) throws Exception { + checkServerVersion(context, "1.8", "Updating playlists is not supported."); + List names = new ArrayList(); + List values = new ArrayList(); + names.add("playlistId"); + values.add(id); + names.add("name"); + values.add(name); + for(MusicDirectory.Entry song: toAdd) { + names.add("songIdToAdd"); + values.add(song.getId()); + } + for(int i = 0; i < toRemove; i++) { + names.add("songIndexToRemove"); + values.add(i); + } + Reader reader = getReader(context, progressListener, "updatePlaylist", null, names, values); + try { + new ErrorParser(context).parse(reader); + } finally { + Util.close(reader); + } + } + @Override public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception { checkServerVersion(context, "1.8", "Updating playlists is not supported."); -- cgit v1.2.3