From 33baa6e2493183f8669ccf6bf5490147f7dd5f64 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 17 Dec 2013 07:13:06 -0800 Subject: #214 Give a notification when new media is synced --- .../dsub/service/parser/PlaylistParser.java | 5 +++- .../dsub/service/sync/MostRecentSyncAdapter.java | 14 ++++++--- .../dsub/service/sync/PlaylistSyncAdapter.java | 10 +++++++ .../dsub/service/sync/PodcastSyncAdapter.java | 10 +++++-- .../dsub/service/sync/StarredSyncAdapter.java | 6 +++- .../dsub/service/sync/SubsonicSyncAdapter.java | 10 +++++-- src/github/daneren2005/dsub/util/SyncUtil.java | 35 ++++++++++++++++++++++ 7 files changed, 79 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/parser/PlaylistParser.java b/src/github/daneren2005/dsub/service/parser/PlaylistParser.java index 8c6cfc6f..c630eb44 100644 --- a/src/github/daneren2005/dsub/service/parser/PlaylistParser.java +++ b/src/github/daneren2005/dsub/service/parser/PlaylistParser.java @@ -49,7 +49,10 @@ public class PlaylistParser extends MusicDirectoryEntryParser { dir.addChild(parseEntry("")); } else if ("error".equals(name)) { handleError(); - } + } else if ("playlist".equals(name)) { + dir.setName(get("name")); + dir.setId(get("id")); + } } } while (eventType != XmlPullParser.END_DOCUMENT); diff --git a/src/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java index 7bead10b..3ac1ca23 100644 --- a/src/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/MostRecentSyncAdapter.java @@ -27,6 +27,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.PodcastEpisode; import github.daneren2005.dsub.service.DownloadFile; @@ -55,20 +56,21 @@ public class MostRecentSyncAdapter extends SubsonicSyncAdapter { try { ArrayList syncedList = SyncUtil.getSyncedMostRecent(context, instance); MusicDirectory albumList = musicService.getAlbumList("newest", 20, 0, context, null); - boolean updated = false; + List updated = new ArrayList(); + boolean firstRun = false; if(syncedList.size() == 0) { // Get the initial set of albums on first run, don't sync any of these! for(MusicDirectory.Entry album: albumList.getChildren()) { syncedList.add(album.getId()); } - updated = true; + firstRun = true; } else { for(MusicDirectory.Entry album: albumList.getChildren()) { if(!syncedList.contains(album.getId())) { try { downloadRecursively(null, musicService.getMusicDirectory(album.getId(), album.getTitle(), true, context, null), context, false); syncedList.add(album.getId()); - updated = true; + updated.add(album.getTitle()); } catch(Exception e) { Log.w(TAG, "Failed to get songs for " + album.getId() + " on " + Util.getServerName(context, instance)); } @@ -76,13 +78,17 @@ public class MostRecentSyncAdapter extends SubsonicSyncAdapter { } } - if(updated) { + if(updated.size() > 0) { FileUtil.serialize(context, syncedList, SyncUtil.getMostRecentSyncFile(context, instance)); // If there is a new album on the active server, chances are artists need to be refreshed if(Util.getActiveServer(context) == instance) { musicService.getIndexes(Util.getSelectedMusicFolderId(context), true, context, null); } + + SyncUtil.showSyncNotification(context, R.string.sync_new_albums, SyncUtil.joinNames(updated)); + } else if(firstRun) { + FileUtil.serialize(context, syncedList, SyncUtil.getMostRecentSyncFile(context, instance)); } } catch(Exception e) { Log.e(TAG, "Failed to get most recent list for " + Util.getServerName(context, instance)); diff --git a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java index 6724e561..fddfcc01 100644 --- a/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PlaylistSyncAdapter.java @@ -23,8 +23,10 @@ import android.annotation.TargetApi; import android.content.Context; import android.util.Log; +import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.service.parser.SubsonicRESTException; @@ -58,6 +60,7 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { } List playlistList = SyncUtil.getSyncedPlaylists(context, instance); + List updated = new ArrayList(); for(int i = 0; i < playlistList.size(); i++) { String id = playlistList.get(i); try { @@ -67,6 +70,9 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { DownloadFile file = new DownloadFile(context, entry, true); while(!file.isSaved() && !file.isFailedMax()) { file.downloadNow(musicService); + if(!updated.contains(playlist.getName())) { + updated.add(playlist.getName()); + } } } } catch(SubsonicRESTException e) { @@ -77,6 +83,10 @@ public class PlaylistSyncAdapter extends SubsonicSyncAdapter { } catch(Exception e) { Log.e(TAG, "Failed to get playlist " + id + " for " + serverName); } + + if(updated.size() > 0) { + SyncUtil.showSyncNotification(context, R.string.sync_new_playlists, SyncUtil.joinNames(updated)); + } } } } diff --git a/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java index 4826c214..d22a11b9 100644 --- a/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/PodcastSyncAdapter.java @@ -27,6 +27,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.PodcastEpisode; import github.daneren2005.dsub.service.DownloadFile; @@ -66,7 +67,7 @@ public class PodcastSyncAdapter extends SubsonicSyncAdapter { musicService.getPodcastChannels(true, context, null); } - boolean updated = false; + List updated = new ArrayList(); for(int i = 0; i < podcastList.size(); i++) { SyncSet set = podcastList.get(i); String id = set.id; @@ -84,7 +85,9 @@ public class PodcastSyncAdapter extends SubsonicSyncAdapter { // Only add if actualy downloaded correctly if(file.isSaved()) { existingEpisodes.add(entry.getId()); - updated = true; + if(!updated.contains(entry.getAlbum())) { + updated.add(entry.getAlbum()); + } } } } @@ -99,8 +102,9 @@ public class PodcastSyncAdapter extends SubsonicSyncAdapter { } // Make sure there are is at least one change before re-syncing - if(updated) { + if(updated.size() > 0) { FileUtil.serialize(context, podcastList, SyncUtil.getPodcastSyncFile(context, instance)); + SyncUtil.showSyncNotification(context, R.string.sync_new_podcasts, SyncUtil.joinNames(updated)); } } catch(Exception e) { Log.w(TAG, "Failed to get podcasts for " + Util.getServerName(context, instance)); diff --git a/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java index 10ab58b6..ff153e33 100644 --- a/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java @@ -27,6 +27,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.SyncUtil; @@ -54,7 +55,7 @@ public class StarredSyncAdapter extends SubsonicSyncAdapter { MusicDirectory starredList = musicService.getStarredList(context, null); // Pin all the starred stuff - downloadRecursively(syncedList, starredList, context, true); + boolean updated = downloadRecursively(syncedList, starredList, context, true); // Get old starred list ArrayList oldSyncedList = SyncUtil.getSyncedStarred(context, instance); @@ -75,6 +76,9 @@ public class StarredSyncAdapter extends SubsonicSyncAdapter { } FileUtil.serialize(context, syncedList, SyncUtil.getStarredSyncFile(context, instance)); + if(updated) { + SyncUtil.showSyncNotification(context, R.string.sync_new_starred, null); + } } catch(Exception e) { Log.e(TAG, "Failed to get starred list for " + Util.getServerName(context, instance)); } diff --git a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java index 1c45d8e1..97a68835 100644 --- a/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/SubsonicSyncAdapter.java @@ -119,12 +119,14 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter { } - protected void downloadRecursively(List paths, MusicDirectory parent, Context context, boolean save) throws Exception { + protected boolean downloadRecursively(List paths, MusicDirectory parent, Context context, boolean save) throws Exception { + boolean downloaded = false; for (MusicDirectory.Entry song: parent.getChildren(false, true)) { if (!song.isVideo()) { DownloadFile file = new DownloadFile(context, song, save); while(!(save && file.isSaved() || !save && file.isCompleteFileAvailable()) && !file.isFailedMax()) { file.downloadNow(musicService); + downloaded = true; } if(paths != null && file.isCompleteFileAvailable()) { @@ -134,7 +136,11 @@ public class SubsonicSyncAdapter extends AbstractThreadedSyncAdapter { } for (MusicDirectory.Entry dir: parent.getChildren(true, false)) { - downloadRecursively(paths, musicService.getMusicDirectory(dir.getId(), dir.getTitle(), true, context, null), context, save); + if(downloadRecursively(paths, musicService.getMusicDirectory(dir.getId(), dir.getTitle(), true, context, null), context, save)) { + downloaded = true; + } } + + return downloaded; } } diff --git a/src/github/daneren2005/dsub/util/SyncUtil.java b/src/github/daneren2005/dsub/util/SyncUtil.java index 5a8b79f4..4fb639a5 100644 --- a/src/github/daneren2005/dsub/util/SyncUtil.java +++ b/src/github/daneren2005/dsub/util/SyncUtil.java @@ -1,11 +1,19 @@ package github.daneren2005.dsub.util; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; +import android.util.Log; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.activity.SubsonicFragmentActivity; + /** * Created by Scott on 11/24/13. */ @@ -133,6 +141,33 @@ public final class SyncUtil { return "sync-most_recent-" + (Util.getRestUrl(context, null, instance)).hashCode() + ".ser"; } + public static void showSyncNotification(final Context context, int stringId, String extra) { + String content = (extra != null) ? context.getResources().getString(stringId, extra) : context.getResources().getString(stringId); + + NotificationCompat.Builder builder; + builder = new NotificationCompat.Builder(context) + .setSmallIcon(R.drawable.stat_notify_download) + .setContentTitle(context.getResources().getString(R.string.sync_title)) + .setContentText(content) + .setOngoing(false); + + Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class); + notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + builder.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0)); + + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.notify(stringId, builder.build()); + } + + public static String joinNames(List names) { + StringBuilder builder = new StringBuilder(); + for (String val : names) { + builder.append(val).append(", "); + } + builder.setLength(builder.length() - 2); + return builder.toString(); + } + public static class SyncSet implements Serializable { public String id; public List synced; -- cgit v1.2.3