Skip to content

Commit

Permalink
#429 Add ability to play/shuffle by album lists. Just plays/shuffles …
Browse files Browse the repository at this point in the history
…whatever is loaded so far.
  • Loading branch information
daneren2005 committed Dec 16, 2014
1 parent 89b42b0 commit c6d492a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 39 deletions.
19 changes: 19 additions & 0 deletions res/menu/select_album_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_play_now"
android:icon="?media_button_start"
android:title="@string/menu.play"
compat:showAsAction="always|withText"/>

<item
android:id="@+id/menu_shuffle"
android:icon="?attr/shuffle"
android:title="@string/menu.shuffle"
compat:showAsAction="ifRoom|withText"/>

<item
android:id="@+id/menu_exit"
android:title="@string/menu.exit"/>
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
}
else if(hideButtons && !showAll) {
if(albumListType != null) {
menuInflater.inflate(R.menu.empty, menu);
menuInflater.inflate(R.menu.select_album_list, menu);
} else {
menuInflater.inflate(R.menu.select_album, menu);

Expand Down Expand Up @@ -802,6 +802,8 @@ private void playAll(final boolean shuffle, final boolean append) {

if (hasSubFolders && (id != null || share != null || "starred".equals(albumListType))) {
downloadRecursively(id, false, append, !append, shuffle, false);
} else if(hasSubFolders && albumListType != null) {
downloadRecursively(albums, shuffle, append);
} else {
selectAll(true, false);
download(append, false, !append, false, shuffle);
Expand Down
120 changes: 82 additions & 38 deletions src/github/daneren2005/dsub/fragments/SubsonicFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,7 @@ protected void downloadRecursively(final String id, final String name, final boo
downloadRecursively(id, name, isDirectory, save, append, autoplay, shuffle, background, false);
}
protected void downloadRecursively(final String id, final String name, final boolean isDirectory, final boolean save, final boolean append, final boolean autoplay, final boolean shuffle, final boolean background, final boolean playNext) {
LoadingTask<Boolean> task = new LoadingTask<Boolean>(context) {
private MusicService musicService;
private static final int MAX_SONGS = 500;
private boolean playNowOverride = false;
private List<Entry> songs;

new RecursiveLoader(context) {
@Override
protected Boolean doInBackground() throws Throwable {
musicService = MusicServiceFactory.getMusicService(context);
Expand Down Expand Up @@ -904,48 +899,47 @@ else if(isDirectory) {

return transition;
}
}.execute();
}

private void getSongsRecursively(MusicDirectory parent, List<Entry> songs) throws Exception {
if (songs.size() > MAX_SONGS) {
return;
}
protected void downloadRecursively(final List<Entry> albums, final boolean shuffle, final boolean append) {
new RecursiveLoader(context) {
@Override
protected Boolean doInBackground() throws Throwable {
musicService = MusicServiceFactory.getMusicService(context);

for (Entry song : parent.getChildren(false, true)) {
if (!song.isVideo() && song.getRating() != 1) {
songs.add(song);
}
if(shuffle) {
Collections.shuffle(albums);
}
for (Entry dir : parent.getChildren(true, false)) {
if(dir.getRating() == 1) {
continue;
}

MusicDirectory musicDirectory;
if(Util.isTagBrowsing(context) && !Util.isOffline(context)) {
musicDirectory = musicService.getAlbum(dir.getId(), dir.getTitle(), false, context, this);
} else {
musicDirectory = musicService.getMusicDirectory(dir.getId(), dir.getTitle(), false, context, this);
songs = new LinkedList<Entry>();
MusicDirectory root = new MusicDirectory();
root.addChildren(albums);
getSongsRecursively(root, songs);

DownloadService downloadService = getDownloadService();
boolean transition = false;
if (!songs.isEmpty() && downloadService != null) {
// Conditions for a standard play now operation
if(!append && !shuffle) {
playNowOverride = true;
return false;
}
getSongsRecursively(musicDirectory, songs);
}
}

@Override
protected void done(Boolean result) {
warnIfStorageUnavailable();
if (!append) {
downloadService.clear();
}

if(playNowOverride) {
playNow(songs);
return;
downloadService.download(songs, false, true, false, false);
if(!append) {
transition = true;
}
}
artistOverride = false;

if(result) {
Util.startActivityWithoutTransition(context, DownloadActivity.class);
}
return transition;
}
};

task.execute();
}.execute();
}

protected MusicDirectory getMusicDirectory(String id, String name, boolean refresh, MusicService service, ProgressListener listener) throws Exception {
Expand Down Expand Up @@ -1732,4 +1726,54 @@ public abstract class OnRatingChange {
public abstract class OnStarChange {
abstract void starChange(boolean starred);
}

public abstract class RecursiveLoader extends LoadingTask<Boolean> {
protected MusicService musicService;
protected static final int MAX_SONGS = 500;
protected boolean playNowOverride = false;
protected List<Entry> songs;

public RecursiveLoader(Activity context) {
super(context);
}

protected void getSongsRecursively(MusicDirectory parent, List<Entry> songs) throws Exception {
if (songs.size() > MAX_SONGS) {
return;
}

for (Entry song : parent.getChildren(false, true)) {
if (!song.isVideo() && song.getRating() != 1) {
songs.add(song);
}
}
for (Entry dir : parent.getChildren(true, false)) {
if(dir.getRating() == 1) {
continue;
}

MusicDirectory musicDirectory;
if(Util.isTagBrowsing(context) && !Util.isOffline(context)) {
musicDirectory = musicService.getAlbum(dir.getId(), dir.getTitle(), false, context, this);
} else {
musicDirectory = musicService.getMusicDirectory(dir.getId(), dir.getTitle(), false, context, this);
}
getSongsRecursively(musicDirectory, songs);
}
}

@Override
protected void done(Boolean result) {
warnIfStorageUnavailable();

if(playNowOverride) {
playNow(songs);
return;
}

if(result) {
Util.startActivityWithoutTransition(context, DownloadActivity.class);
}
}
}
}

0 comments on commit c6d492a

Please sign in to comment.