From 9deb868578f32847402566f83667f24597633e5b Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 19 Aug 2014 21:45:56 -0700 Subject: More work on synchronized bookmarks --- .../dsub/fragments/NowPlayingFragment.java | 13 ++++-- .../dsub/fragments/SelectBookmarkFragment.java | 2 +- .../dsub/fragments/SubsonicFragment.java | 4 +- .../dsub/service/CachedMusicService.java | 48 +++++++++++++--------- .../daneren2005/dsub/service/MusicService.java | 4 +- .../dsub/service/OfflineMusicService.java | 4 +- .../daneren2005/dsub/service/RESTMusicService.java | 4 +- src/github/daneren2005/dsub/util/Util.java | 14 +++++++ 8 files changed, 61 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java b/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java index 3486f154..4565b192 100644 --- a/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java +++ b/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java @@ -30,7 +30,6 @@ import android.os.Bundle; import android.os.Handler; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.MediaRouteButton; -import android.util.Log; import android.view.ContextMenu; import android.view.Display; import android.view.GestureDetector; @@ -46,7 +45,6 @@ import android.view.ViewGroup; import android.view.WindowManager; import android.view.animation.AnimationUtils; import android.widget.AdapterView; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; @@ -57,6 +55,7 @@ import android.widget.TextView; import android.widget.ViewFlipper; import github.daneren2005.dsub.R; import github.daneren2005.dsub.activity.SubsonicFragmentActivity; +import github.daneren2005.dsub.domain.Bookmark; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.PlayerState; import github.daneren2005.dsub.domain.RepeatMode; @@ -68,7 +67,6 @@ import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.view.DownloadFileAdapter; import github.daneren2005.dsub.view.FadeOutAnimation; -import github.daneren2005.dsub.view.SongView; import github.daneren2005.dsub.view.UpdateView; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.view.VisualizerView; @@ -1321,7 +1319,14 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis protected Void doInBackground() throws Throwable { MusicDirectory.Entry currentSong = currentDownload.getSong(); MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.createBookmark(currentSong.getId(), getDownloadService().getPlayerPosition(), comment, context, null); + int position = getDownloadService().getPlayerPosition(); + musicService.createBookmark(currentSong.getId(), Util.getParentFromEntry(context, currentSong), getDownloadService().getPlayerPosition(), comment, context, null); + + currentSong.setBookmark(new Bookmark(position)); + MusicDirectory.Entry find = UpdateView.findEntry(currentSong); + if(find != null && find != currentSong) { + find.setBookmark(new Bookmark(position)); + } return null; } diff --git a/src/github/daneren2005/dsub/fragments/SelectBookmarkFragment.java b/src/github/daneren2005/dsub/fragments/SelectBookmarkFragment.java index 16350f69..dfaf2d4d 100644 --- a/src/github/daneren2005/dsub/fragments/SelectBookmarkFragment.java +++ b/src/github/daneren2005/dsub/fragments/SelectBookmarkFragment.java @@ -140,7 +140,7 @@ public class SelectBookmarkFragment extends SelectListFragment newList = new ArrayList(); + final List newList = new ArrayList(); newList.addAll(dir.getChildren()); - List oldList = oldDir.getChildren(); + final List oldList = oldDir.getChildren(); removeDuplicates(oldList, newList); @@ -466,7 +467,7 @@ public class CachedMusicService implements MusicService { new MusicDirectoryUpdater(context, cacheName, parent) { @Override public boolean checkResult(Entry check) { - if (entry.getId().equals(check.getId()) && entry.isStarred() != starred) { + if (entry.getId().equals(check.getId()) && check.isStarred() != starred) { return true; } @@ -799,10 +800,10 @@ public class CachedMusicService implements MusicService { public MusicDirectory getBookmarks(boolean refresh, Context context, ProgressListener progressListener) throws Exception { MusicDirectory bookmarks = musicService.getBookmarks(refresh, context, progressListener); - MusicDirectory oldBookmarks = FileUtil.deserailize(context, "bookmarks", MusicDirectory.class); + MusicDirectory oldBookmarks = FileUtil.deserialize(context, "bookmarks", MusicDirectory.class); if(oldBookmarks != null) { - List oldList = oldBookmarks.getChildren(); - List newList = new ArrayList<>(); + final List oldList = oldBookmarks.getChildren(); + final List newList = new ArrayList(); newList.addAll(bookmarks.getChildren()); removeDuplicates(oldList, newList); @@ -812,7 +813,7 @@ public class CachedMusicService implements MusicService { // Add new bookmarks for things in new list setBookmarkCache(context, newList, false); - if(oldList.size() > 0 || nextList.size() > 0) { + if(oldList.size() > 0 || newList.size() > 0) { new PlaylistDirectoryUpdater(context) { @Override public boolean checkResult(Entry check) { @@ -849,7 +850,7 @@ public class CachedMusicService implements MusicService { @Override public void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception { - musicService.createBookmark(id, position, comment, context, progressListener); + musicService.createBookmark(id, null, position, comment, context, progressListener); // Add to directory cache setBookmarkCache(context, id, parent, position); // Add to playlist cache @@ -858,7 +859,7 @@ public class CachedMusicService implements MusicService { @Override public void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception { - musicService.deleteBookmark(id, context, progressListener); + musicService.deleteBookmark(id, null, context, progressListener); // Delete from directory cache setBookmarkCache(context, id, parent, -1); // Delete from playlist cache @@ -868,15 +869,22 @@ public class CachedMusicService implements MusicService { private void setBookmarkCache(Context context, List entries, boolean remove) { for(final Entry entry: entries) { if(remove) { - setBookmarkCache(context, entry.getId(), entry.getParent(), -1); + setBookmarkCache(context, entry.getId(), Util.getParentFromEntry(context, entry), -1); } else { - setBookmarkCache(context, entry.getId(), entry.getParent(), entry.getBookmark().getPosition()); + setBookmarkCache(context, entry.getId(), Util.getParentFromEntry(context, entry), entry.getBookmark().getPosition()); } } } private void setBookmarkCache(Context context, final String id, final String parent, final int position) { + String cacheName; + if(isTagBrowsing) { + cacheName = "album"; + } else { + cacheName = "directory"; + } + // Update the parent directory with bookmark data - new MusicDirectoryUpdater(context, "directory", parent) { + new MusicDirectoryUpdater(context, cacheName, parent) { @Override public boolean checkResult(Entry check) { return shouldBookmarkUpdate(check, id, position); @@ -904,7 +912,7 @@ public class CachedMusicService implements MusicService { } private boolean shouldBookmarkUpdate(Entry check, String id, int position) { - if(id.equals(check.getId()) { + if(id.equals(check.getId())) { if(position == -1 && check.getBookmark() != null) { return true; } else if(position >= 0 && (check.getBookmark() == null || check.getBookmark().getPosition() != position)) { @@ -1054,7 +1062,7 @@ public class CachedMusicService implements MusicService { private void removeDuplicates(List oldList, List newList) { for(Iterator it = oldList.iterator(); it.hasNext(); ) { // Remove entries from newList - if(newList.remove(it.next()) { + if(newList.remove(it.next())) { // If it was removed, then remove it from old list as well it.remove(); } @@ -1097,10 +1105,10 @@ public class CachedMusicService implements MusicService { // Only execute if something to check against if(objects != null) { - List objects = new ArrayList(); + List results = new ArrayList(); for(T check: objects) { if(checkResult(check)) { - objects.push(check); + results.add(check); if(singleUpdate) { break; } @@ -1108,12 +1116,12 @@ public class CachedMusicService implements MusicService { } // Iterate through and update each object matched - for(T object: objects) { - updateResult(objects, object); + for(T result: results) { + updateResult(objects, result); } // Only reserialize if at least one match was found - if(objects.size() > 0) { + if(results.size() > 0) { save(objects); } } @@ -1177,7 +1185,7 @@ public class CachedMusicService implements MusicService { } public abstract boolean checkResult(Entry check); - public abstract boolean updateResult(Entry result); + public abstract void updateResult(Entry result); public void execute() { List playlists = FileUtil.deserialize(context, getCacheName(context, "playlist"), ArrayList.class); diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 58df8904..d70f1c46 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -156,9 +156,9 @@ public interface MusicService { MusicDirectory getBookmarks(boolean refresh, Context context, ProgressListener progressListener) throws Exception; - void createBookmark(String id, int position, String comment, Context context, ProgressListener progressListener) throws Exception; + void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception; - void deleteBookmark(String id, Context context, ProgressListener progressListener) throws Exception; + void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception; User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception; diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index 82d3de10..8aeba2b4 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -747,12 +747,12 @@ public class OfflineMusicService implements MusicService { } @Override - public void createBookmark(String id, int position, String comment, Context context, ProgressListener progressListener) throws Exception { + public void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception { throw new OfflineException(ERRORMSG); } @Override - public void deleteBookmark(String id, Context context, ProgressListener progressListener) throws Exception { + public void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception { throw new OfflineException(ERRORMSG); } diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 13d1555c..025d07c5 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -1127,7 +1127,7 @@ public class RESTMusicService implements MusicService { } @Override - public void createBookmark(String id, int position, String comment, Context context, ProgressListener progressListener) throws Exception { + public void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception { checkServerVersion(context, "1.9", "Creating bookmarks not supported."); Reader reader = getReader(context, progressListener, "createBookmark", null, Arrays.asList("id", "position", "comment"), Arrays.asList(id, position, comment)); @@ -1139,7 +1139,7 @@ public class RESTMusicService implements MusicService { } @Override - public void deleteBookmark(String id, Context context, ProgressListener progressListener) throws Exception { + public void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception { checkServerVersion(context, "1.9", "Deleting bookmarks not supported."); Reader reader = getReader(context, progressListener, "deleteBookmark", null, Arrays.asList("id"), Arrays.asList(id)); diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java index 1ea1a3be..258e4eb8 100644 --- a/src/github/daneren2005/dsub/util/Util.java +++ b/src/github/daneren2005/dsub/util/Util.java @@ -385,6 +385,20 @@ public final class Util { return prefs.getBoolean(Constants.PREFERENCES_KEY_BROWSE_TAGS + instance, false); } + public static String getParentFromEntry(Context context, MusicDirectory.Entry entry) { + if(Util.isTagBrowsing(context)) { + if(!entry.isDirectory()) { + return entry.getAlbumId(); + } else if(entry.isAlbum()) { + return entry.getArtistId(); + } else { + return null; + } + } else { + return entry.getParent(); + } + } + public static boolean isOpenToLibrary(Context context) { SharedPreferences prefs = getPreferences(context); return prefs.getBoolean(Constants.PREFERENCES_KEY_OPEN_TO_LIBRARY, false); -- cgit v1.2.3