aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/domain/Indexes.java6
-rw-r--r--src/github/daneren2005/dsub/domain/MusicDirectory.java4
-rw-r--r--src/github/daneren2005/dsub/domain/User.java4
-rw-r--r--src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java15
-rw-r--r--src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java12
-rw-r--r--src/github/daneren2005/dsub/fragments/SubsonicFragment.java32
-rw-r--r--src/github/daneren2005/dsub/service/CachedMusicService.java456
-rw-r--r--src/github/daneren2005/dsub/service/MusicService.java2
-rw-r--r--src/github/daneren2005/dsub/service/OfflineMusicService.java2
-rw-r--r--src/github/daneren2005/dsub/service/RESTMusicService.java8
-rw-r--r--src/github/daneren2005/dsub/util/FileUtil.java4
-rw-r--r--src/github/daneren2005/dsub/util/UserUtil.java2
-rw-r--r--src/github/daneren2005/dsub/util/Util.java8
-rw-r--r--src/github/daneren2005/dsub/view/EntryAdapter.java2
14 files changed, 488 insertions, 69 deletions
diff --git a/src/github/daneren2005/dsub/domain/Indexes.java b/src/github/daneren2005/dsub/domain/Indexes.java
index 67eb33bc..63c3a8d4 100644
--- a/src/github/daneren2005/dsub/domain/Indexes.java
+++ b/src/github/daneren2005/dsub/domain/Indexes.java
@@ -67,6 +67,12 @@ public class Indexes implements Serializable {
return artists;
}
+ public void setArtists(List<Artist> artists) {
+ this.shortcuts = new ArrayList<Artist>();
+ this.artists.clear();
+ this.artists.addAll(artists);
+ }
+
public List<MusicDirectory.Entry> getEntries() {
return entries;
}
diff --git a/src/github/daneren2005/dsub/domain/MusicDirectory.java b/src/github/daneren2005/dsub/domain/MusicDirectory.java
index 492726f9..1b3342bd 100644
--- a/src/github/daneren2005/dsub/domain/MusicDirectory.java
+++ b/src/github/daneren2005/dsub/domain/MusicDirectory.java
@@ -249,6 +249,10 @@ public class MusicDirectory implements Serializable {
return album;
}
+ public boolean isAlbum() {
+ return getParent() != null || getArtist() != null;
+ }
+
public String getAlbumDisplay() {
if(album != null && title.startsWith("Disc ")) {
return album;
diff --git a/src/github/daneren2005/dsub/domain/User.java b/src/github/daneren2005/dsub/domain/User.java
index f61f98df..179ac033 100644
--- a/src/github/daneren2005/dsub/domain/User.java
+++ b/src/github/daneren2005/dsub/domain/User.java
@@ -84,6 +84,10 @@ public class User implements Serializable {
public List<Setting> getSettings() {
return settings;
}
+ public void setSettings(List<Setting> settings) {
+ this.settings.clear();
+ this.settings.addAll(settings);
+ }
public void addSetting(String name, Boolean value) {
settings.add(new Setting(name, value));
}
diff --git a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java
index c26d7085..bf9e5531 100644
--- a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java
+++ b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java
@@ -274,6 +274,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter
addToPlaylist(getSelectedSongs());
return true;
case R.id.menu_remove_playlist:
+ // TODO: getSelected is returning offset
removeFromPlaylist(playlistId, playlistName, getSelectedIndexes());
return true;
case R.id.menu_download_all:
@@ -826,9 +827,10 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter
List<Integer> indexes = new ArrayList<Integer>();
int count = entryList.getCount();
+ int headers = entryList.getHeaderViewsCount();
for (int i = 0; i < count; i++) {
if (entryList.isItemChecked(i)) {
- indexes.add(i - 1);
+ indexes.add(i - headers);
}
}
@@ -1046,18 +1048,21 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter
List<String> ids = new ArrayList<String>();
List<String> artists = new ArrayList<String>();
List<String> albums = new ArrayList<String>();
+ List<String> parents = new ArrayList<String>();
for(MusicDirectory.Entry entry: unstar) {
if(entry.isDirectory()) {
- if(entry.getArtist() == null || entry.getParent() == null) {
- artists.add(entry.getId());
- } else {
+ if(entry.isAlbum()) {
albums.add(entry.getId());
+ parents.add(entry.getArtistId());
+ } else {
+ artists.add(entry.getId());
}
} else {
ids.add(entry.getId());
+ parents.add(entry.getParent());
}
}
- musicService.setStarred(ids, artists, albums, false, context, this);
+ musicService.setStarred(ids, artists, albums, parents, false, this, context);
return null;
}
diff --git a/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java b/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java
index 694c6d19..caf9079f 100644
--- a/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java
+++ b/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java
@@ -234,14 +234,22 @@ public class SelectPlaylistFragment extends SelectListFragment<Playlist> {
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
+ String name = nameBox.getText().toString();
+ String comment = commentBox.getText().toString();
+ boolean isPublic = publicBox.isChecked();
+
MusicService musicService = MusicServiceFactory.getMusicService(context);
- musicService.updatePlaylist(playlist.getId(), nameBox.getText().toString(), commentBox.getText().toString(), publicBox.isChecked(), context, null);
+ musicService.updatePlaylist(playlist.getId(), name, comment, isPublic, context, null);
+
+ playlist.setName(name);
+ playlist.setComment(comment);
+ playlist.setPublic(isPublic);
+
return null;
}
@Override
protected void done(Void result) {
- refresh();
Util.toast(context, context.getResources().getString(R.string.playlist_updated_info, playlist.getName()));
}
diff --git a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java
index a1a69f0a..516c4c59 100644
--- a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java
+++ b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java
@@ -645,20 +645,26 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
if(entry.isDirectory() && Util.isTagBrowsing(context) && !Util.isOffline(context)) {
- if(entry.getParent() != null || entry.getArtist() != null) {
- musicService.setStarred(null, null, Arrays.asList(entry.getId()), starred, context, null);
+ if(entry.isAlbum()) {
+ musicService.setStarred(null, null, Arrays.asList(entry.getId()), Arrays.asList(entry.getArtistId()), starred, null, context);
} else {
- musicService.setStarred(null, Arrays.asList(entry.getId()), null, starred, context, null);
+ musicService.setStarred(null, Arrays.asList(entry.getId()), null, null, starred, null, context);
}
} else {
- musicService.setStarred(Arrays.asList(entry.getId()), null, null, starred, context, null);
+ List<String> parents = null;
+ if(Util.isTagBrowsing(context)) {
+ if(entry.getAlbumId() != null) {
+ parents = Arrays.asList(entry.getAlbumId());
+ }
+ } else {
+ if(entry.getParent() != null) {
+ parents = Arrays.asList(entry.getParent());
+ }
+ }
+
+ musicService.setStarred(Arrays.asList(entry.getId()), null, null, parents, starred, null, context);
}
-
- // Make sure to clear parent cache
- String s = Util.getRestUrl(context, null) + entry.getParent();
- String parentCache = "directory-" + s.hashCode() + ".ser";
- File file = new File(context.getCacheDir(), parentCache);
- file.delete();
+
return null;
}
@@ -670,6 +676,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR
@Override
protected void error(Throwable error) {
+ Log.w(TAG, "Failed to star", error);
entry.setStarred(!starred);
String msg;
@@ -692,9 +699,9 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
if(Util.isTagBrowsing(context) && !Util.isOffline(context)) {
- musicService.setStarred(null, Arrays.asList(entry.getId()), null, starred, context, null);
+ musicService.setStarred(null, Arrays.asList(entry.getId()), null, null, starred, null, context);
} else {
- musicService.setStarred(Arrays.asList(entry.getId()), null, null, starred, context, null);
+ musicService.setStarred(Arrays.asList(entry.getId()), null, null, null, starred, null, context);
}
return null;
}
@@ -707,6 +714,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR
@Override
protected void error(Throwable error) {
+ Log.w(TAG, "Failed to star", error);
entry.setStarred(!starred);
String msg;
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java
index 611e438e..b8266913 100644
--- a/src/github/daneren2005/dsub/service/CachedMusicService.java
+++ b/src/github/daneren2005/dsub/service/CachedMusicService.java
@@ -20,18 +20,23 @@ package github.daneren2005.dsub.service;
import java.io.File;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import java.util.ListIterator;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpResponse;
import android.content.Context;
import android.graphics.Bitmap;
+import android.util.Log;
+import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.Bookmark;
import github.daneren2005.dsub.domain.ChatMessage;
import github.daneren2005.dsub.domain.Genre;
import github.daneren2005.dsub.domain.Indexes;
+import github.daneren2005.dsub.domain.PodcastEpisode;
import github.daneren2005.dsub.domain.RemoteStatus;
import github.daneren2005.dsub.domain.Lyrics;
import github.daneren2005.dsub.domain.MusicDirectory;
@@ -48,6 +53,8 @@ import github.daneren2005.dsub.util.TimeLimitedCache;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.Util;
+import static github.daneren2005.dsub.domain.MusicDirectory.Entry;
+
/**
* @author Sindre Mehus
*/
@@ -237,7 +244,7 @@ public class CachedMusicService implements MusicService {
}
@Override
- public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
+ public void createPlaylist(String id, String name, List<Entry> entries, Context context, ProgressListener progressListener) throws Exception {
cachedPlaylists.clear();
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist")));
musicService.createPlaylist(id, name, entries, context, progressListener);
@@ -245,32 +252,86 @@ public class CachedMusicService implements MusicService {
@Override
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist")));
musicService.deletePlaylist(id, context, progressListener);
+
+ new PlaylistUpdater(context, id) {
+ @Override
+ public void updateResult(List<Playlist> objects, Playlist result) {
+ objects.remove(result);
+ cachedPlaylists.set(objects);
+ }
+ }.execute();
}
@Override
- public void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
+ public void addToPlaylist(String id, final List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
musicService.addToPlaylist(id, toAdd, context, progressListener);
+
+ new MusicDirectoryUpdater(context, "playlist", id) {
+ @Override
+ public boolean checkResult(Entry check) {
+ return true;
+ }
+
+ @Override
+ public void updateResult(List<Entry> objects, Entry result) {
+ objects.addAll(toAdd);
+ }
+ }.execute();
}
@Override
- public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
+ public void removeFromPlaylist(String id, final List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
musicService.removeFromPlaylist(id, toRemove, context, progressListener);
+
+ new MusicDirectoryUpdater(context, "playlist", id) {
+ @Override
+ public boolean checkResult(Entry check) {
+ return true;
+ }
+
+ @Override
+ public void updateResult(List<Entry> objects, Entry result) {
+ // Remove in reverse order so indexes are still correct as we iterate through
+ for(ListIterator<Integer> iterator = toRemove.listIterator(toRemove.size()); iterator.hasPrevious(); ) {
+ objects.remove((int) iterator.previous());
+ }
+ }
+ }.execute();
}
@Override
- public void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
+ public void overwritePlaylist(String id, String name, int toRemove, final List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
musicService.overwritePlaylist(id, name, toRemove, toAdd, context, progressListener);
+
+ new MusicDirectoryUpdater(context, "playlist", id) {
+ @Override
+ public boolean checkResult(Entry check) {
+ return true;
+ }
+
+ @Override
+ public void updateResult(List<Entry> objects, Entry result) {
+ objects.clear();
+ objects.addAll(toAdd);
+ }
+ }.execute();
}
@Override
- public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
+ public void updatePlaylist(String id, final String name, final String comment, final boolean pub, Context context, ProgressListener progressListener) throws Exception {
musicService.updatePlaylist(id, name, comment, pub, context, progressListener);
+
+ new PlaylistUpdater(context, id) {
+ @Override
+ public void updateResult(List<Playlist> objects, Playlist result) {
+ result.setName(name);
+ result.setComment(comment);
+ result.setPublic(pub);
+
+ cachedPlaylists.set(objects);
+ }
+ }.execute();
}
@Override
@@ -295,31 +356,119 @@ public class CachedMusicService implements MusicService {
@Override
public MusicDirectory getStarredList(Context context, ProgressListener progressListener) throws Exception {
- return musicService.getStarredList(context, progressListener);
+ MusicDirectory dir = musicService.getStarredList(context, progressListener);
+
+ MusicDirectory oldDir = FileUtil.deserialize(context, "starred", MusicDirectory.class);
+ if(oldDir != null) {
+ List<Entry> newList = new ArrayList<Entry>();
+ newList.addAll(dir.getChildren());
+ List<Entry> oldList = oldDir.getChildren();
+
+ for(Iterator<Entry> it = oldList.iterator(); it.hasNext(); ) {
+ // Remove any entries from newList
+ if(newList.remove(it.next())) {
+ // If it was removed, then remove from oldList as well
+ it.remove();
+ }
+ }
+
+ // Left overs in newList need to be starred
+ boolean isTagBrowsing = Util.isTagBrowsing(context, musicService.getInstance(context));
+ updateStarredList(context, newList, true, isTagBrowsing);
+
+ // Left overs in oldList need to be unstarred
+ updateStarredList(context, oldList, false, isTagBrowsing);
+ }
+ FileUtil.serialize(context, dir, "starred");
+
+ return dir;
}
+ private void updateStarredList(Context context, List<Entry> list, final boolean starred, final boolean isTagBrowsing) {
+ for(final Entry entry: list) {
+ String cacheName, parent = null;
+ boolean isArtist = false;
+ if(isTagBrowsing) {
+ if(entry.isDirectory()) {
+ if(entry.isAlbum()) {
+ cacheName = "artist";
+ parent = entry.getArtistId();
+ } else {
+ isArtist = true;
+ cacheName = "artists";
+ }
+ } else {
+ cacheName = "album";
+ parent = entry.getAlbumId();
+ }
+ } else {
+ if(entry.isDirectory() && !entry.isAlbum()) {
+ isArtist = true;
+ cacheName = "indexes";
+ } else {
+ cacheName = "directory";
+ parent = entry.getParent();
+ }
+ }
+
+ if(isArtist) {
+ new IndexesUpdater(context, isTagBrowsing ? "artists" : "indexes") {
+ @Override
+ public boolean checkResult(Artist check) {
+ if(entry.getId().equals(check.getId())) {
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public void updateResult(List<Artist> objects, Artist result) {
+ result.setStarred(starred);
+ }
+ }.execute();
+ } else {
+ new MusicDirectoryUpdater(context, cacheName, parent) {
+ @Override
+ public boolean checkResult(Entry check) {
+ if (entry.getId().equals(check.getId())) {
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public void updateResult(List<Entry> objects, Entry result) {
+ result.setStarred(true);
+ }
+ }.execute();
+ }
+ }
+ }
+
@Override
public MusicDirectory getRandomSongs(int size, String folder, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
return musicService.getRandomSongs(size, folder, genre, startYear, endYear, context, progressListener);
}
@Override
- public String getCoverArtUrl(Context context, MusicDirectory.Entry entry) throws Exception {
+ public String getCoverArtUrl(Context context, Entry entry) throws Exception {
return musicService.getCoverArtUrl(context, entry);
}
@Override
- public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, ProgressListener progressListener) throws Exception {
+ public Bitmap getCoverArt(Context context, Entry entry, int size, ProgressListener progressListener) throws Exception {
return musicService.getCoverArt(context, entry, size, progressListener);
}
@Override
- public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
+ public HttpResponse getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
return musicService.getDownloadInputStream(context, song, offset, maxBitrate, task);
}
@Override
- public String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate) throws Exception {
+ public String getMusicUrl(Context context, Entry song, int maxBitrate) throws Exception {
return musicService.getMusicUrl(context, song, maxBitrate);
}
@@ -369,8 +518,83 @@ public class CachedMusicService implements MusicService {
}
@Override
- public void setStarred(List<String> id, List<String> artistId, List<String> albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception {
- musicService.setStarred(id, artistId, albumId, starred, context, progressListener);
+ public void setStarred(List<String> id, List<String> artistId, List<String> albumId, List<String> parents, final boolean starred, ProgressListener progressListener, Context context) throws Exception {
+ musicService.setStarred(id, artistId, albumId, parents, starred, progressListener, context);
+
+ // Fuzzy logic to update parents serialization
+ List<String> ids;
+ if(artistId != null && artistId.size() > 0) {
+ ids = artistId;
+ } else if(albumId != null && albumId.size() > 0) {
+ ids = albumId;
+ } else {
+ ids = id;
+ }
+
+ // Make sure list is not somehow null here
+ if(ids == null) {
+ Log.w(TAG, "There should never be no ids in setStarred");
+ return;
+ }
+
+ // Define another variable final because Java is retarded
+ final List<String> checkIds = ids;
+
+ // If parents is null, or artist id's are set, then we are looking at artists
+ if(parents != null && (artistId == null || artistId.size() == 0)) {
+ String cacheName;
+
+ // If using tag browsing, need to do lookup off of different criteria
+ if(Util.isTagBrowsing(context, musicService.getInstance(context))) {
+ // If using id's, we are starring songs and need to use album listings
+ if(id != null && id.size() > 0) {
+ cacheName = "album";
+ } else {
+ cacheName = "artist";
+ }
+ } else {
+ cacheName = "directory";
+ }
+
+ for (String parent : parents) {
+ new MusicDirectoryUpdater(context, cacheName, parent) {
+ @Override
+ public boolean checkResult(Entry check) {
+ for (String id : checkIds) {
+ if(id.equals(check.getId())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public void updateResult(List<Entry> objects, Entry result) {
+ result.setStarred(starred);
+ }
+ }.execute();
+ }
+ } else {
+ String name = Util.isTagBrowsing(context, musicService.getInstance(context)) ? "artists" : "indexes";
+ new IndexesUpdater(context, name) {
+ @Override
+ public boolean checkResult(Artist check) {
+ for (String id : checkIds) {
+ if(id.equals(check.getId())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public void updateResult(List<Artist> objects, Artist result) {
+ result.setStarred(starred);
+ }
+ }.execute();
+ }
}
@Override
@@ -468,15 +692,23 @@ public class CachedMusicService implements MusicService {
@Override
public void createPodcastChannel(String url, Context context, ProgressListener progressListener) throws Exception{
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "podcast")));
- cachedPodcastChannels.clear();
musicService.createPodcastChannel(url, context, progressListener);
}
@Override
- public void deletePodcastChannel(String id, Context context, ProgressListener progressListener) throws Exception{
- Util.delete(new File(context.getCacheDir(), getCacheName(context, "podcast")));
- cachedPodcastChannels.clear();
+ public void deletePodcastChannel(final String id, Context context, ProgressListener progressListener) throws Exception{
+ new SerializeUpdater<PodcastChannel>(context, "podcast") {
+ @Override
+ public boolean checkResult(PodcastChannel check) {
+ return id.equals(check.getId());
+ }
+
+ @Override
+ public void updateResult(List<PodcastChannel> objects, PodcastChannel result) {
+ objects.remove(result);
+ cachedPodcastChannels.set(objects);
+ }
+ }.execute();
musicService.deletePodcastChannel(id, context, progressListener);
}
@@ -486,8 +718,20 @@ public class CachedMusicService implements MusicService {
}
@Override
- public void deletePodcastEpisode(String id, Context context, ProgressListener progressListener) throws Exception{
+ public void deletePodcastEpisode(final String id, Context context, ProgressListener progressListener) throws Exception{
musicService.deletePodcastEpisode(id, context, progressListener);
+
+ new SerializeUpdater<PodcastEpisode>(context, "directory", "p-" + id) {
+ @Override
+ public boolean checkResult(PodcastEpisode check) {
+ return id.equals(check.getId());
+ }
+
+ @Override
+ public void updateResult(List<PodcastEpisode> objects, PodcastEpisode result) {
+ objects.remove(result);
+ }
+ }.execute();
}
@Override
@@ -545,35 +789,58 @@ public class CachedMusicService implements MusicService {
}
@Override
- public void createUser(User user, Context context, ProgressListener progressListener) throws Exception {
+ public void createUser(final User user, Context context, ProgressListener progressListener) throws Exception {
musicService.createUser(user, context, progressListener);
+
+ new UserUpdater(context, "") {
+ @Override
+ public boolean checkResult(User check) {
+ return true;
+ }
+
+ @Override
+ public void updateResult(List<User> users, User result) {
+ users.add(user);
+ }
+ }.execute();
}
@Override
- public void updateUser(User user, Context context, ProgressListener progressListener) throws Exception {
+ public void updateUser(final User user, Context context, ProgressListener progressListener) throws Exception {
musicService.updateUser(user, context, progressListener);
- // Delete cached users if anything updated
- File file = new File(context.getCacheDir(), getCacheName(context, "users"));
- file.delete();
+ new UserUpdater(context, user.getUsername()) {
+ @Override
+ public void updateResult(List<User> users, User result) {
+ result.setEmail(user.getEmail());
+ result.setSettings(user.getSettings());
+ }
+ }.execute();
}
@Override
public void deleteUser(String username, Context context, ProgressListener progressListener) throws Exception {
musicService.deleteUser(username, context, progressListener);
- // Delete cached users if any have been removed from list
- File file = new File(context.getCacheDir(), getCacheName(context, "users"));
- file.delete();
+ new UserUpdater(context, username) {
+ @Override
+ public void updateResult(List<User> users, User result) {
+ users.remove(result);
+ }
+ }.execute();
}
@Override
- public void changeEmail(String username, String email, Context context, ProgressListener progressListener) throws Exception {
+ public void changeEmail(String username, final String email, Context context, ProgressListener progressListener) throws Exception {
musicService.changeEmail(username, email, context, progressListener);
-
- // Delete cached users if any have been removed from list
- File file = new File(context.getCacheDir(), getCacheName(context, "users"));
- file.delete();
+
+ // Update cached email for user
+ new UserUpdater(context, username) {
+ @Override
+ public void updateResult(List<User> users, User result) {
+ result.setEmail(email);
+ }
+ }.execute();
}
@Override
@@ -604,6 +871,123 @@ public class CachedMusicService implements MusicService {
String s = musicService.getRestUrl(context, null, false);
return name + "-" + s.hashCode() + ".ser";
}
+
+ private abstract class SerializeUpdater<T> {
+ final Context context;
+ final String cacheName;
+
+ public SerializeUpdater(Context context, String cacheName) {
+ this.context = context;
+ this.cacheName = getCacheName(context, cacheName);
+ }
+ public SerializeUpdater(Context context, String cacheName, String id) {
+ this.context = context;
+ this.cacheName = getCacheName(context, cacheName, id);
+ }
+
+ public ArrayList<T> getArrayList() {
+ return FileUtil.deserialize(context, cacheName, ArrayList.class);
+ }
+ public abstract boolean checkResult(T check);
+ public abstract void updateResult(List<T> objects, T result);
+ public void save(ArrayList<T> objects) {
+ FileUtil.serialize(context, objects, cacheName);
+ }
+
+ public void execute() {
+ ArrayList<T> objects = getArrayList();
+
+ // Only execute if something to check against
+ if(objects != null) {
+ T object = null;
+ for(T check: objects) {
+ if(checkResult(check)) {
+ object = check;
+ break;
+ }
+ }
+
+ // Only reserialize if a match was found
+ if(object != null) {
+ updateResult(objects, object);
+ save(objects);
+ }
+ }
+ }
+ }
+ private abstract class UserUpdater extends SerializeUpdater<User> {
+ String username;
+
+ public UserUpdater(Context context, String username) {
+ super(context, "users");
+ this.username = username;
+ }
+
+ @Override
+ public boolean checkResult(User check) {
+ return username.equals(check.getUsername());
+ }
+ }
+ private abstract class PlaylistUpdater extends SerializeUpdater<Playlist> {
+ String id;
+
+ public PlaylistUpdater(Context context, String id) {
+ super(context, "playlist");
+ this.id = id;
+ }
+
+ @Override
+ public boolean checkResult(Playlist check) {
+ return id.equals(check.getId());
+ }
+ }
+ private abstract class MusicDirectoryUpdater extends SerializeUpdater<Entry> {
+ private MusicDirectory musicDirectory;
+
+ public MusicDirectoryUpdater(Context context, String cacheName, String id) {
+ super(context, cacheName, id);
+ }
+
+ @Override
+ public ArrayList<Entry> getArrayList() {
+ musicDirectory = FileUtil.deserialize(context, cacheName, MusicDirectory.class);
+ if(musicDirectory != null) {
+ return new ArrayList<Entry>(musicDirectory.getChildren());
+ } else {
+ return null;
+ }
+ }
+ public void save(ArrayList<Entry> objects) {
+ musicDirectory.replaceChildren(objects);
+ FileUtil.serialize(context, musicDirectory, cacheName);
+ }
+ }
+ private abstract class IndexesUpdater extends SerializeUpdater<Artist> {
+ Indexes indexes;
+
+ IndexesUpdater(Context context, String name) {
+ super(context, name, Util.getSelectedMusicFolderId(context, musicService.getInstance(context)));
+ }
+
+ @Override
+ public ArrayList<Artist> getArrayList() {
+ indexes = FileUtil.deserialize(context, cacheName, Indexes.class);
+ if(indexes == null) {
+ return null;
+ }
+
+ ArrayList<Artist> artists = new ArrayList<Artist>();
+ artists.addAll(indexes.getArtists());
+ artists.addAll(indexes.getShortcuts());
+ return artists;
+ }
+
+ public void save(ArrayList<Artist> objects) {
+ indexes.setArtists(objects);
+ FileUtil.serialize(context, indexes, cacheName);
+ cachedIndexes.set(indexes);
+ }
+ }
private void checkSettingsChanged(Context context) {
String newUrl = musicService.getRestUrl(context, null, false);
diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java
index 9f87065c..78c99b73 100644
--- a/src/github/daneren2005/dsub/service/MusicService.java
+++ b/src/github/daneren2005/dsub/service/MusicService.java
@@ -120,7 +120,7 @@ public interface MusicService {
RemoteStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception;
- void setStarred(List<String> id, List<String> artistId, List<String> albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception;
+ void setStarred(List<String> id, List<String> artistId, List<String> albumId, List<String> parents, boolean starred, ProgressListener progressListener, Context context) throws Exception;
List<Share> getShares(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 3dacaee7..68906fd7 100644
--- a/src/github/daneren2005/dsub/service/OfflineMusicService.java
+++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java
@@ -595,7 +595,7 @@ public class OfflineMusicService implements MusicService {
}
@Override
- public void setStarred(List<String> ids, List<String> artistId, List<String> albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception {
+ public void setStarred(List<String> ids, List<String> artistId, List<String> albumId, List<String> parents, boolean starred, ProgressListener progressListener, Context context) throws Exception {
SharedPreferences prefs = Util.getPreferences(context);
String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java
index 51a8a2a6..fe135dc6 100644
--- a/src/github/daneren2005/dsub/service/RESTMusicService.java
+++ b/src/github/daneren2005/dsub/service/RESTMusicService.java
@@ -794,7 +794,7 @@ public class RESTMusicService implements MusicService {
}
@Override
- public void setStarred(List<String> ids, List<String> artistId, List<String> albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception {
+ public void setStarred(List<String> ids, List<String> artistId, List<String> albumId, List<String> parents, boolean starred, ProgressListener progressListener, Context context) throws Exception {
checkServerVersion(context, "1.8", "Starring is not supported.");
List<String> names = new ArrayList<String>();
@@ -1363,7 +1363,7 @@ public class RESTMusicService implements MusicService {
String id = offline.getString(Constants.OFFLINE_STAR_ID + i, null);
boolean starred = offline.getBoolean(Constants.OFFLINE_STAR_SETTING + i, false);
if(id != null) {
- setStarred(Arrays.asList(id), null, null, starred, context, progressListener);
+ setStarred(Arrays.asList(id), null, null, null, starred, progressListener, context);
} else {
String search = offline.getString(Constants.OFFLINE_STAR_SEARCH + i, "");
try{
@@ -1371,10 +1371,10 @@ public class RESTMusicService implements MusicService {
SearchResult result = searchNew(critera, context, progressListener);
if(result.getSongs().size() == 1){
Log.i(TAG, "Query '" + search + "' returned song " + result.getSongs().get(0).getTitle() + " by " + result.getSongs().get(0).getArtist() + " with id " + result.getSongs().get(0).getId());
- setStarred(Arrays.asList(result.getSongs().get(0).getId()), null, null, starred, context, progressListener);
+ setStarred(Arrays.asList(result.getSongs().get(0).getId()), null, null, null, starred, progressListener, context);
} else if(result.getAlbums().size() == 1){
Log.i(TAG, "Query '" + search + "' returned song " + result.getAlbums().get(0).getTitle() + " by " + result.getAlbums().get(0).getArtist() + " with id " + result.getAlbums().get(0).getId());
- setStarred(Arrays.asList(result.getAlbums().get(0).getId()), null, null, starred, context, progressListener);
+ setStarred(Arrays.asList(result.getAlbums().get(0).getId()), null, null, null, starred, progressListener, context);
}
else{
throw new Exception("Song not found on server");
diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java
index 205b3e15..304541b6 100644
--- a/src/github/daneren2005/dsub/util/FileUtil.java
+++ b/src/github/daneren2005/dsub/util/FileUtil.java
@@ -577,7 +577,6 @@ public class FileUtil {
RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
out = new Output(new FileOutputStream(file.getFD()));
kryo.writeObject(out, obj);
- Log.i(TAG, "Serialized object to " + fileName);
return true;
} catch (Throwable x) {
Log.w(TAG, "Failed to serialize object to " + fileName);
@@ -614,7 +613,6 @@ public class FileUtil {
in = new Input(new FileInputStream(randomFile.getFD()));
T result = (T) kryo.readObject(in, tClass);
- Log.i(TAG, "Deserialized object from " + fileName);
return result;
} catch(FileNotFoundException e) {
// Different error message
@@ -636,7 +634,6 @@ public class FileUtil {
RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
out = new Output(new DeflaterOutputStream(new FileOutputStream(file.getFD())));
kryo.writeObject(out, obj);
- Log.i(TAG, "Serialized compressed object to " + fileName);
return true;
} catch (Throwable x) {
Log.w(TAG, "Failed to serialize compressed object to " + fileName);
@@ -656,7 +653,6 @@ public class FileUtil {
in = new Input(new InflaterInputStream(new FileInputStream(file.getFD())));
T result = (T) kryo.readObject(in, tClass);
- Log.i(TAG, "Deserialized compressed object from " + fileName);
return result;
} catch(FileNotFoundException e) {
// Different error message
diff --git a/src/github/daneren2005/dsub/util/UserUtil.java b/src/github/daneren2005/dsub/util/UserUtil.java
index 5c57b422..5821a9dc 100644
--- a/src/github/daneren2005/dsub/util/UserUtil.java
+++ b/src/github/daneren2005/dsub/util/UserUtil.java
@@ -245,6 +245,7 @@ public final class UserUtil {
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.updateUser(user, context, null);
+ user.setSettings(user.getSettings());
return null;
}
@@ -296,6 +297,7 @@ public final class UserUtil {
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.changeEmail(user.getUsername(), email, context, null);
+ user.setEmail(email);
return null;
}
diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java
index 844face3..1ea1a3be 100644
--- a/src/github/daneren2005/dsub/util/Util.java
+++ b/src/github/daneren2005/dsub/util/Util.java
@@ -239,10 +239,12 @@ public final class Util {
}
public static String getSelectedMusicFolderId(Context context) {
- SharedPreferences prefs = getPreferences(context);
- int instance = getActiveServer(context);
- return prefs.getString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null);
+ return getSelectedMusicFolderId(context, getActiveServer(context));
}
+ public static String getSelectedMusicFolderId(Context context, int instance) {
+ SharedPreferences prefs = getPreferences(context);
+ return prefs.getString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null);
+ }
public static String getTheme(Context context) {
SharedPreferences prefs = getPreferences(context);
diff --git a/src/github/daneren2005/dsub/view/EntryAdapter.java b/src/github/daneren2005/dsub/view/EntryAdapter.java
index 2dc14e1b..e9611692 100644
--- a/src/github/daneren2005/dsub/view/EntryAdapter.java
+++ b/src/github/daneren2005/dsub/view/EntryAdapter.java
@@ -55,7 +55,7 @@ public class EntryAdapter extends ArrayAdapter<MusicDirectory.Entry> {
MusicDirectory.Entry entry = getItem(position);
if (entry.isDirectory()) {
- if(entry.getArtist() != null || entry.getParent() != null) {
+ if(entry.isAlbum()) {
AlbumView view;
view = new AlbumView(activity);
view.setObject(entry, imageLoader);