Skip to content

Commit

Permalink
MPD: Fix album list with either empty list or invalid list response.
Browse files Browse the repository at this point in the history
We've been getting automated crash reports where the album key is not included
in a list response line, this is probably due to an empty list or possibly an
invalid server response line. During repair, I noticed we're doing a lot of
unnecessary work with regard to album listing, so this patch fixes that at the
same time.
  • Loading branch information
avuton committed Dec 6, 2014
1 parent 5492bbc commit d4816d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 53 deletions.
59 changes: 10 additions & 49 deletions JMPDComm/src/main/java/org/a0z/mpd/MPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,71 +1216,32 @@ public List<String[]> listAlbumArtists(final List<Album> albums)
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums() throws IOException, MPDException {
return listAlbums(null, false, true);
}

/**
* List all albums from database.
*
* @param useAlbumArtist use AlbumArtist instead of Artist
* @return {@code Collection} with all album names from database.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums(final boolean useAlbumArtist) throws IOException, MPDException {
return listAlbums(null, useAlbumArtist, true);
}

/**
* List all albums from a given artist, including an entry for songs with no
* album tag.
*
* @param artist artist to list albums
* @param useAlbumArtist use AlbumArtist instead of Artist
* @return {@code Collection} with all album names from database.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums(final String artist, final boolean useAlbumArtist)
throws IOException, MPDException {
return listAlbums(artist, useAlbumArtist, true);
return listAlbums(null, false);
}

/**
* List all albums from a given artist.
*
* @param artist artist to list albums
* @param useAlbumArtist use AlbumArtist instead of Artist
* @param includeUnknownAlbum include an entry for songs with no album tag
* @return {@code Collection} with all album names from the given
* artist present in database.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
public List<String> listAlbums(final String artist, final boolean useAlbumArtist,
final boolean includeUnknownAlbum) throws IOException, MPDException {
boolean foundSongWithoutAlbum = false;

public List<String> listAlbums(final String artist, final boolean useAlbumArtist)
throws IOException, MPDException {
final List<String> response =
mConnection.sendCommand
(listAlbumsCommand(artist, useAlbumArtist));

final List<String> result = new ArrayList<>(response.size());
for (final String line : response) {
final String name = line.substring("Album: ".length());
if (name.isEmpty()) {
foundSongWithoutAlbum = true;
} else {
result.add(name);
}
}
mConnection.sendCommand(listAlbumsCommand(artist, useAlbumArtist));
final List<String> result;

// add a single blank entry to host all songs without an album set
if (includeUnknownAlbum && foundSongWithoutAlbum) {
result.add("");
if (response.isEmpty()) {
result = Collections.emptyList();
} else {
result = Tools.parseResponse(response, "Album");
Collections.sort(result);
}

Collections.sort(result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,19 @@ public List<String[]> listAlbumArtists(final List<Album> albums)
*
* @param artist artist to list albums
* @param useAlbumArtist use AlbumArtist instead of Artist
* @param includeUnknownAlbum include an entry for songs with no album tag
* @return List of albums.
* @throws IOException Thrown upon a communication error with the server.
* @throws MPDException Thrown if an error occurs as a result of command execution.
*/
@Override
public List<String> listAlbums(final String artist, final boolean useAlbumArtist,
final boolean includeUnknownAlbum) throws IOException, MPDException {
public List<String> listAlbums(final String artist, final boolean useAlbumArtist)
throws IOException, MPDException {
final List<String> albums;

if (isCached()) {
albums = new ArrayList(mCache.getAlbums(artist, useAlbumArtist));
} else {
albums = super.listAlbums(artist, useAlbumArtist, includeUnknownAlbum);
albums = super.listAlbums(artist, useAlbumArtist);
}

return albums;
Expand Down

0 comments on commit d4816d7

Please sign in to comment.