diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-12-25 12:58:41 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-12-25 12:58:41 -0800 |
commit | a33ac5f4e1bb59c2b2a394e22925f22fa4de40e3 (patch) | |
tree | 7568572ce360864a9d885b2f7a00d8b29f791a28 /src/github | |
parent | adb44cd78d63d4ee18e2d963e601e413d777bf5a (diff) | |
download | dsub-a33ac5f4e1bb59c2b2a394e22925f22fa4de40e3.tar.gz dsub-a33ac5f4e1bb59c2b2a394e22925f22fa4de40e3.tar.bz2 dsub-a33ac5f4e1bb59c2b2a394e22925f22fa4de40e3.zip |
#224 Unpin songs removed from synced playlist
Diffstat (limited to 'src/github')
-rw-r--r-- | src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java | 61 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/SyncUtil.java | 45 |
2 files changed, 84 insertions, 22 deletions
diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index fddfcc01..b3f605e5 100644 --- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java @@ -23,6 +23,7 @@ import android.annotation.TargetApi; import android.content.Context;
import android.util.Log;
+import java.io.File;
import java.util.ArrayList;
import java.util.List;
@@ -30,7 +31,9 @@ import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.service.DownloadFile;
import github.daneren2005.dsub.service.parser.SubsonicRESTException;
+import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.SyncUtil;
+import github.daneren2005.dsub.util.SyncUtil.SyncSet;
import github.daneren2005.dsub.util.Util;
/**
@@ -59,21 +62,60 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { Log.e(TAG, "Failed to refresh playlist list for " + serverName);
}
- List<String> playlistList = SyncUtil.getSyncedPlaylists(context, instance);
+ ArrayList<SyncSet> playlistList = SyncUtil.getSyncedPlaylists(context, instance);
List<String> updated = new ArrayList<String>();
+ boolean removed = false;
for(int i = 0; i < playlistList.size(); i++) {
- String id = playlistList.get(i);
+ SyncSet cachedPlaylist = playlistList.get(i);
+ String id = cachedPlaylist.id;
try {
- MusicDirectory playlist = musicService.getPlaylist(true, id, serverName, context, null);
+ MusicDirectory playlist = musicService.getPlaylist(false, id, serverName, context, null);
+
+ // Get list of original paths
+ List<String> origPathList = new ArrayList<String>();
+ if(cachedPlaylist.synced != null) {
+ origPathList.addAll(cachedPlaylist.synced);
+ } else {
+ cachedPlaylist.synced = new ArrayList<String>();
+ }
for(MusicDirectory.Entry entry: playlist.getChildren()) {
DownloadFile file = new DownloadFile(context, entry, true);
- while(!file.isSaved() && !file.isFailedMax()) {
- file.downloadNow(musicService);
- if(!updated.contains(playlist.getName())) {
- updated.add(playlist.getName());
+ String path = file.getCompleteFile().getPath();
+ if(!cachedPlaylist.synced.contains(path)) {
+ while(!file.isSaved() && !file.isFailedMax()) {
+ file.downloadNow(musicService);
+ if(!updated.contains(playlist.getName())) {
+ updated.add(playlist.getName());
+ }
+ }
+
+ // Add to cached path set if saved
+ if(file.isSaved()) {
+ cachedPlaylist.synced.add(path);
}
}
+
+ origPathList.remove(path);
+ }
+
+ // Check to unpin all paths which are no longer in playlist
+ if(origPathList.size() > 0) {
+ for(String path: origPathList) {
+ File saveFile = new File(path);
+
+ // Unpin file, rename to .complete
+ File completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
+ ".complete." + FileUtil.getExtension(saveFile.getName()));
+
+ if(!saveFile.renameTo(completeFile)) {
+ Log.w(TAG, "Failed to rename " + path + " to " + completeFile.getPath());
+ }
+
+ cachedPlaylist.synced.remove(path);
+ }
+
+ removed = true;
}
} catch(SubsonicRESTException e) {
if(e.getCode() == 70) {
@@ -81,12 +123,15 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { Log.i(TAG, "Unsync deleted playlist " + id + " for " + serverName);
}
} catch(Exception e) {
- Log.e(TAG, "Failed to get playlist " + id + " for " + serverName);
+ Log.e(TAG, "Failed to get playlist " + id + " for " + serverName, e);
}
if(updated.size() > 0) {
SyncUtil.showSyncNotification(context, R.string.sync_new_playlists, SyncUtil.joinNames(updated));
}
+ if(updated.size() > 0 || removed) {
+ SyncUtil.setSyncedPlaylists(context, instance, playlistList);
+ }
}
}
}
diff --git a/src/github/daneren2005/dsub/util/SyncUtil.java b/src/github/daneren2005/dsub/util/SyncUtil.java index 21aaaccc..17ec4b5c 100644 --- a/src/github/daneren2005/dsub/util/SyncUtil.java +++ b/src/github/daneren2005/dsub/util/SyncUtil.java @@ -19,7 +19,7 @@ import github.daneren2005.dsub.activity.SubsonicFragmentActivity; */
public final class SyncUtil {
private static String TAG = SyncUtil.class.getSimpleName();
- private static ArrayList<String> syncedPlaylists;
+ private static ArrayList<SyncSet> syncedPlaylists;
private static ArrayList<SyncSet> syncedPodcasts;
// Playlist sync
@@ -27,25 +27,41 @@ public final class SyncUtil { if(syncedPlaylists == null) {
syncedPlaylists = getSyncedPlaylists(context);
}
- return syncedPlaylists.contains(playlistId);
+ return syncedPlaylists.contains(new SyncSet(playlistId));
}
- public static ArrayList<String> getSyncedPlaylists(Context context) {
+ public static ArrayList<SyncSet> getSyncedPlaylists(Context context) {
return getSyncedPlaylists(context, Util.getActiveServer(context));
}
- public static ArrayList<String> getSyncedPlaylists(Context context, int instance) {
- ArrayList<String> playlists = FileUtil.deserialize(context, getPlaylistSyncFile(context, instance), ArrayList.class);
+ public static ArrayList<SyncSet> getSyncedPlaylists(Context context, int instance) {
+ String syncFile = getPlaylistSyncFile(context, instance);
+ ArrayList<SyncSet> playlists = FileUtil.deserializeCompressed(context, syncFile, ArrayList.class);
if(playlists == null) {
- playlists = new ArrayList<String>();
+ playlists = new ArrayList<SyncSet>();
+
+ // Try to convert old style into new style
+ ArrayList<String> oldPlaylists = FileUtil.deserialize(context, syncFile, ArrayList.class);
+ // If exists, time to convert!
+ if(oldPlaylists != null) {
+ for(String id: oldPlaylists) {
+ playlists.add(new SyncSet(id));
+ }
+
+ FileUtil.serializeCompressed(context, playlists, syncFile);
+ }
}
return playlists;
}
+ public static void setSyncedPlaylists(Context context, int instance, ArrayList<SyncSet> playlists) {
+ FileUtil.serializeCompressed(context, playlists, getPlaylistSyncFile(context, instance));
+ }
public static void addSyncedPlaylist(Context context, String playlistId) {
String playlistFile = getPlaylistSyncFile(context);
- ArrayList<String> playlists = getSyncedPlaylists(context);
- if(!playlists.contains(playlistId)) {
- playlists.add(playlistId);
+ ArrayList<SyncSet> playlists = getSyncedPlaylists(context);
+ SyncSet set = new SyncSet(playlistId);
+ if(!playlists.contains(set)) {
+ playlists.add(set);
}
- FileUtil.serialize(context, playlists, playlistFile);
+ FileUtil.serializeCompressed(context, playlists, playlistFile);
syncedPlaylists = playlists;
}
public static void removeSyncedPlaylist(Context context, String playlistId) {
@@ -54,10 +70,11 @@ public final class SyncUtil { }
public static void removeSyncedPlaylist(Context context, String playlistId, int instance) {
String playlistFile = getPlaylistSyncFile(context, instance);
- ArrayList<String> playlists = getSyncedPlaylists(context, instance);
- if(playlists.contains(playlistId)) {
- playlists.remove(playlistId);
- FileUtil.serialize(context, playlists, playlistFile);
+ ArrayList<SyncSet> playlists = getSyncedPlaylists(context, instance);
+ SyncSet set = new SyncSet(playlistId);
+ if(playlists.contains(set)) {
+ playlists.remove(set);
+ FileUtil.serializeCompressed(context, playlists, playlistFile);
syncedPlaylists = playlists;
}
}
|