Search in sources :

Example 21 with MPDTrack

use of org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack in project malp by gateship-one.

the class FileAdapter method getItemViewType.

/**
 * Returns the type (section track or normal track) of the item at the given position.
 * If preceding {@link MPDFileEntry} is not a {@link MPDTrack} it will generate a secion entry.
 * Else it will check if the preceding {@link MPDTrack} is another album.
 *
 * @param position Position of the item in question
 * @return the int value of the enum {@link CurrentPlaylistAdapter.VIEW_TYPES}
 */
@Override
public int getItemViewType(int position) {
    // Get MPDTrack at the given index used for this item.
    MPDFileEntry file = (MPDFileEntry) getItem(position);
    if (file instanceof MPDTrack) {
        boolean newAlbum = false;
        MPDTrack track = (MPDTrack) file;
        MPDFileEntry previousFile;
        if (position > 0) {
            previousFile = (MPDFileEntry) getItem(position - 1);
            if (previousFile != null) {
                if (previousFile instanceof MPDTrack) {
                    MPDTrack previousTrack = (MPDTrack) previousFile;
                    newAlbum = !previousTrack.getTrackAlbum().equals(track.getTrackAlbum());
                }
            }
        } else {
            return VIEW_TYPES.TYPE_SECTION_FILE_ITEM.ordinal();
        }
        return newAlbum ? VIEW_TYPES.TYPE_SECTION_FILE_ITEM.ordinal() : VIEW_TYPES.TYPE_FILE_ITEM.ordinal();
    } else {
        return VIEW_TYPES.TYPE_FILE_ITEM.ordinal();
    }
}
Also used : MPDFileEntry(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry) MPDTrack(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack)

Example 22 with MPDTrack

use of org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack in project malp by gateship-one.

the class BackgroundService method checkMPDConnection.

/**
 * Ensures an MPD server is connected before performing an action.
 */
private void checkMPDConnection() {
    if (!MPDInterface.mInstance.isConnected() && !mConnecting) {
        mNotificationManager.showNotification();
        mLastTrack = new MPDTrack("");
        mLastStatus = new MPDCurrentStatus();
        connectMPDServer();
    }
}
Also used : MPDTrack(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack) MPDCurrentStatus(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus)

Example 23 with MPDTrack

use of org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack in project malp by gateship-one.

the class MPDInterface method addTrackList.

/*
     ***********************
     *    Queue commands   *
     ***********************
     */
/**
 * This method adds songs in a bulk command list. Should be reasonably in performance this way.
 *
 * @param tracks List of MPDFileEntry objects to add to the current playlist.
 */
public synchronized void addTrackList(List<MPDFileEntry> tracks) throws MPDException {
    if (null == tracks) {
        return;
    }
    mConnection.startCommandList();
    for (MPDFileEntry track : tracks) {
        if (track instanceof MPDTrack) {
            mConnection.sendMPDRAWCommand(MPDCommands.MPD_COMMAND_ADD_FILE(track.getPath()));
        }
    }
    mConnection.endCommandList();
}
Also used : MPDFileEntry(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry) MPDTrack(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack)

Example 24 with MPDTrack

use of org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack in project malp by gateship-one.

the class MPDResponseParser method parseMPDTracks.

/**
 * Parses the response of mpd on requests that return track items. This is also used
 * for MPD file, directory and playlist responses. This allows the GUI to develop
 * one adapter for all three types. Also MPD mixes them when requesting directory listings.
 * <p/>
 * It will return a list of MPDFileEntry objects which is a parent class for (MPDTrack, MPDPlaylist,
 * MPDDirectory) you can use instanceof to check which type you got.
 *
 * @return List of MPDFileEntry objects
 * @throws MPDException if an error from MPD was received during reading
 */
static ArrayList<MPDFileEntry> parseMPDTracks(final MPDConnection connection) throws MPDException {
    ArrayList<MPDFileEntry> trackList = new ArrayList<>();
    if (!connection.isConnected()) {
        return trackList;
    }
    /* Temporary file entry (added to list later) */
    MPDFileEntry tempFileEntry = null;
    /* Response line from MPD */
    String response = connection.readLine();
    while (response != null && !response.startsWith("OK")) {
        /* This if block will just check all the different response possible by MPDs file/dir/playlist response */
        if (response.startsWith(MPDResponses.MPD_RESPONSE_FILE)) {
            if (null != tempFileEntry) {
                trackList.add(tempFileEntry);
            }
            tempFileEntry = new MPDTrack(response.substring(MPDResponses.MPD_RESPONSE_FILE.length()));
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYLIST)) {
            if (null != tempFileEntry) {
                trackList.add(tempFileEntry);
            }
            tempFileEntry = new MPDPlaylist(response.substring(MPDResponses.MPD_RESPONSE_PLAYLIST.length()));
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_DIRECTORY)) {
            if (null != tempFileEntry) {
                trackList.add(tempFileEntry);
            }
            tempFileEntry = new MPDDirectory(response.substring(MPDResponses.MPD_RESPONSE_DIRECTORY.length()));
        }
        // Currently parsing a file (check its properties)
        if (tempFileEntry instanceof MPDTrack) {
            if (response.startsWith(MPDResponses.MPD_RESPONSE_TRACK_TITLE)) {
                ((MPDTrack) tempFileEntry).setTrackTitle(response.substring(MPDResponses.MPD_RESPONSE_TRACK_TITLE.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ARTIST_NAME)) {
                ((MPDTrack) tempFileEntry).setTrackArtist(response.substring(MPDResponses.MPD_RESPONSE_ARTIST_NAME.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ARTIST_SORT_NAME)) {
                ((MPDTrack) tempFileEntry).setTrackArtistSort(response.substring(MPDResponses.MPD_RESPONSE_ARTIST_SORT_NAME.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_TRACK_NAME)) {
                ((MPDTrack) tempFileEntry).setTrackName(response.substring(MPDResponses.MPD_RESPONSE_TRACK_NAME.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ALBUMARTIST_NAME)) {
                ((MPDTrack) tempFileEntry).setTrackAlbumArtist(response.substring(MPDResponses.MPD_RESPONSE_ALBUMARTIST_NAME.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ALBUMARTIST_SORT_NAME)) {
                ((MPDTrack) tempFileEntry).setTrackAlbumArtistSort(response.substring(MPDResponses.MPD_RESPONSE_ALBUMARTIST_SORT_NAME.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ALBUM_NAME)) {
                ((MPDTrack) tempFileEntry).setTrackAlbum(response.substring(MPDResponses.MPD_RESPONSE_ALBUM_NAME.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_DATE)) {
                ((MPDTrack) tempFileEntry).setDate(response.substring(MPDResponses.MPD_RESPONSE_DATE.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ALBUM_MBID)) {
                ((MPDTrack) tempFileEntry).setTrackAlbumMBID(response.substring(MPDResponses.MPD_RESPONSE_ALBUM_MBID.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ARTIST_MBID)) {
                ((MPDTrack) tempFileEntry).setTrackArtistMBID(response.substring(MPDResponses.MPD_RESPONSE_ARTIST_MBID.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ALBUM_ARTIST_MBID)) {
                ((MPDTrack) tempFileEntry).setTrackAlbumArtistMBID(response.substring(MPDResponses.MPD_RESPONSE_ALBUM_ARTIST_MBID.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_TRACK_MBID)) {
                ((MPDTrack) tempFileEntry).setTrackMBID(response.substring(MPDResponses.MPD_RESPONSE_TRACK_MBID.length()));
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_TRACK_TIME)) {
                try {
                    ((MPDTrack) tempFileEntry).setLength(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_TRACK_TIME.length())));
                } catch (NumberFormatException ignored) {
                }
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_SONG_ID)) {
                try {
                    ((MPDTrack) tempFileEntry).setSongID(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_SONG_ID.length())));
                } catch (NumberFormatException ignored) {
                }
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_SONG_POS)) {
                try {
                    ((MPDTrack) tempFileEntry).setSongPosition(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_SONG_POS.length())));
                } catch (NumberFormatException ignored) {
                }
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_DISC_NUMBER)) {
                /*
                * Check if MPD returned a discnumber like: "1" or "1/3" and set disc count accordingly.
                */
                String discNumber = response.substring(MPDResponses.MPD_RESPONSE_DISC_NUMBER.length());
                discNumber = discNumber.replaceAll(" ", "");
                String[] discNumberSep = discNumber.split("/");
                if (discNumberSep.length > 0) {
                    try {
                        ((MPDTrack) tempFileEntry).setDiscNumber(Integer.valueOf(discNumberSep[0]));
                    } catch (NumberFormatException ignored) {
                    }
                    if (discNumberSep.length > 1) {
                        try {
                            ((MPDTrack) tempFileEntry).psetAlbumDiscCount(Integer.valueOf(discNumberSep[1]));
                        } catch (NumberFormatException ignored) {
                        }
                    }
                } else {
                    try {
                        ((MPDTrack) tempFileEntry).setDiscNumber(Integer.valueOf(discNumber));
                    } catch (NumberFormatException ignored) {
                    }
                }
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_TRACK_NUMBER)) {
                /*
                 * Check if MPD returned a tracknumber like: "12" or "12/42" and set albumtrack count accordingly.
                 */
                String trackNumber = response.substring(MPDResponses.MPD_RESPONSE_TRACK_NUMBER.length());
                trackNumber = trackNumber.replaceAll(" ", "");
                String[] trackNumbersSep = trackNumber.split("/");
                if (trackNumbersSep.length > 0) {
                    try {
                        ((MPDTrack) tempFileEntry).setTrackNumber(Integer.valueOf(trackNumbersSep[0]));
                    } catch (NumberFormatException ignored) {
                    }
                    if (trackNumbersSep.length > 1) {
                        try {
                            ((MPDTrack) tempFileEntry).setAlbumTrackCount(Integer.valueOf(trackNumbersSep[1]));
                        } catch (NumberFormatException ignored) {
                        }
                    }
                } else {
                    try {
                        ((MPDTrack) tempFileEntry).setTrackNumber(Integer.valueOf(trackNumber));
                    } catch (NumberFormatException ignored) {
                    }
                }
            } else if (response.startsWith(MPDResponses.MPD_RESPONSE_LAST_MODIFIED)) {
                tempFileEntry.setLastModified(response.substring(MPDResponses.MPD_RESPONSE_LAST_MODIFIED.length()));
            }
        } else if (tempFileEntry != null) {
            // Other case tempFileEntry is a playlist or a directory (properties of generic files)
            if (response.startsWith(MPDResponses.MPD_RESPONSE_LAST_MODIFIED)) {
                tempFileEntry.setLastModified(response.substring(MPDResponses.MPD_RESPONSE_LAST_MODIFIED.length()));
            }
        }
        // Move to the next line.
        response = connection.readLine();
    }
    /* Add last remaining track to list. */
    if (null != tempFileEntry) {
        trackList.add(tempFileEntry);
    }
    return trackList;
}
Also used : MPDFileEntry(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry) MPDTrack(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack) MPDDirectory(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDDirectory) ArrayList(java.util.ArrayList) MPDPlaylist(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDPlaylist)

Example 25 with MPDTrack

use of org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack in project malp by gateship-one.

the class AsyncLoader method doInBackground.

/**
 * Asynchronous task in parallel to the GUI thread.
 * @param params Input parameter containing all the necessary informaton to fetch the image.
 * @return Bitmap loaded from the database.
 */
@Override
protected Bitmap doInBackground(CoverViewHolder... params) {
    // Save the time when loading started for later duration calculation
    mStartTime = System.currentTimeMillis();
    mCover = params[0];
    Bitmap image = null;
    // Check if model item is artist or album
    if (mCover.modelItem instanceof MPDArtist) {
        MPDArtist artist = (MPDArtist) mCover.modelItem;
        try {
            // Check if image is available. If it is not yet fetched it will throw an exception
            // If it was already searched for and not found, this will be null.
            image = mCover.artworkManager.getArtistImage(artist, mCover.imageDimension.first, mCover.imageDimension.second, false);
        } catch (ImageNotFoundException e) {
            // Check if fetching for this item is already ongoing
            if (!artist.getFetching()) {
                // If not set it as ongoing and request the image fetch.
                mCover.artworkManager.fetchArtistImage(artist);
                artist.setFetching(true);
            }
        }
    } else if (mCover.modelItem instanceof MPDAlbum) {
        MPDAlbum album = (MPDAlbum) mCover.modelItem;
        try {
            // Check if image is available. If it is not yet fetched it will throw an exception.
            // If it was already searched for and not found, this will be null.
            image = mCover.artworkManager.getAlbumImage(album, mCover.imageDimension.first, mCover.imageDimension.second, false);
        } catch (ImageNotFoundException e) {
            // Check if fetching for this item is already ongoing
            if (!album.getFetching()) {
                // If not set it as ongoing and request the image fetch.
                mCover.artworkManager.fetchAlbumImage(album);
                album.setFetching(true);
            }
        }
    } else if (mCover.modelItem instanceof MPDTrack) {
        MPDTrack track = (MPDTrack) mCover.modelItem;
        try {
            // Check if image is available. If it is not yet fetched it will throw an exception.
            // If it was already searched for and not found, this will be null.
            image = mCover.artworkManager.getAlbumImageForTrack(track, mCover.imageDimension.first, mCover.imageDimension.second, false);
        } catch (ImageNotFoundException e) {
            // Check if fetching for this item is already ongoing
            if (!track.getFetching()) {
                // If not set it as ongoing and request the image fetch.
                mCover.artworkManager.fetchAlbumImage(track);
                track.setFetching(true);
            }
        }
    }
    return image;
}
Also used : MPDTrack(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack) Bitmap(android.graphics.Bitmap) MPDAlbum(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDAlbum) MPDArtist(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDArtist) ImageNotFoundException(org.gateshipone.malp.application.artworkdatabase.ImageNotFoundException)

Aggregations

MPDTrack (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack)25 MPDFileEntry (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry)7 Bundle (android.os.Bundle)4 MPDAlbum (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDAlbum)4 MPDDirectory (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDDirectory)4 MPDPlaylist (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDPlaylist)4 AppCompatActivity (android.support.v7.app.AppCompatActivity)3 AdapterView (android.widget.AdapterView)3 AddPathToPlaylist (org.gateshipone.malp.application.callbacks.AddPathToPlaylist)3 MPDCurrentStatus (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus)3 FileListItem (org.gateshipone.malp.application.listviewitems.FileListItem)2 MPDArtist (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDArtist)2 Bitmap (android.graphics.Bitmap)1 MenuInflater (android.view.MenuInflater)1 VolleyError (com.android.volley.VolleyError)1 ArrayList (java.util.ArrayList)1 ImageNotFoundException (org.gateshipone.malp.application.artworkdatabase.ImageNotFoundException)1 FanartFetchError (org.gateshipone.malp.application.artworkdatabase.network.responses.FanartFetchError)1 TrackAlbumFetchError (org.gateshipone.malp.application.artworkdatabase.network.responses.TrackAlbumFetchError)1 ChoosePlaylistDialog (org.gateshipone.malp.application.fragments.serverfragments.ChoosePlaylistDialog)1