Search in sources :

Example 1 with MPDException

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

the class MPDCommandHandler method handleMessage.

/**
 * This is the main entry point of messages.
 * Here all possible messages types need to be handled with the MPDConnection.
 * Have a look into the baseclass MPDGenericHandler for more information about the handling.
 *
 * @param msg Message to process.
 */
@Override
public void handleMessage(Message msg) {
    // Call the baseclass handleMessage method here to ensure that the messages handled
    // by the baseclass are handled in subclasses as well.
    super.handleMessage(msg);
    // Type checking
    if (!(msg.obj instanceof MPDHandlerAction)) {
        /* Check if the message object is of correct type. Otherwise just abort here. */
        return;
    }
    MPDHandlerAction mpdAction = (MPDHandlerAction) msg.obj;
    /* Catch MPD exceptions here for now. */
    MPDHandlerAction.NET_HANDLER_ACTION action = mpdAction.getAction();
    try {
        // None of the actions should result in a returned result like a track list.
        if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_NEXT_SONG) {
            MPDInterface.mInstance.nextSong();
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_PREVIOUS_SONG) {
            MPDInterface.mInstance.previousSong();
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_STOP) {
            MPDInterface.mInstance.stopPlayback();
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_PAUSE) {
            MPDInterface.mInstance.pause(true);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_PLAY) {
            MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
            MPDCurrentStatus.MPD_PLAYBACK_STATE state = status.getPlaybackState();
            if (state == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PAUSING) {
                MPDInterface.mInstance.pause(false);
            } else {
                MPDInterface.mInstance.playSongIndex(status.getCurrentSongIndex());
            }
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_TOGGLE_PAUSE) {
            MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
            MPDCurrentStatus.MPD_PLAYBACK_STATE state = status.getPlaybackState();
            if (state == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PLAYING) {
                MPDInterface.mInstance.pause(true);
            } else if (state == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PAUSING) {
                MPDInterface.mInstance.pause(false);
            } else {
                MPDInterface.mInstance.playSongIndex(status.getCurrentSongIndex());
            }
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SET_RANDOM) {
            boolean random = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_RANDOM) == 1;
            MPDInterface.mInstance.setRandom(random);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SET_REPEAT) {
            boolean repeat = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_REPEAT) == 1;
            MPDInterface.mInstance.setRepeat(repeat);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SET_SINGLE) {
            boolean single = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SINGLE) == 1;
            MPDInterface.mInstance.setSingle(single);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SET_CONSUME) {
            boolean consume = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_CONSUME) == 1;
            MPDInterface.mInstance.setConsume(consume);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_JUMP_INDEX) {
            int index = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SONG_INDEX);
            MPDInterface.mInstance.playSongIndex(index);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_COMMAND_SEEK_SECONDS) {
            int seekTo = -1;
            // seek operations.
            while (seekTo != mRequestedSeekVal) {
                seekTo = mRequestedSeekVal;
                MPDInterface.mInstance.seekSeconds(seekTo);
            }
            mSeekActive = false;
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SET_VOLUME) {
            int volume = -1;
            while (volume != mRequestVolume) {
                volume = mRequestVolume;
                MPDInterface.mInstance.setVolume(volume);
            }
            mSetVolumeActive = false;
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_TOGGLE_OUTPUT) {
            int outputID = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_OUTPUT_ID);
            MPDInterface.mInstance.toggleOutput(outputID);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ENABLE_OUTPUT) {
            int outputID = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_OUTPUT_ID);
            MPDInterface.mInstance.enableOutput(outputID);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_DISABLE_OUTPUT) {
            int outputID = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_OUTPUT_ID);
            MPDInterface.mInstance.disableOutput(outputID);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_UP_VOLUME) {
            MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
            // Get step size from message
            int stepSize = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_VOLUME);
            // Limit the volume value to 100(%)
            int targetVolume = status.getVolume() + stepSize;
            if (targetVolume > 100) {
                targetVolume = 100;
            }
            MPDInterface.mInstance.setVolume(targetVolume);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_DOWN_VOLUME) {
            MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
            // Get step size from message
            int stepSize = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_VOLUME);
            // Limit the volume value to 0(%)
            int targetVolume = status.getVolume() - stepSize;
            if (targetVolume < 0) {
                targetVolume = 0;
            }
            MPDInterface.mInstance.setVolume(targetVolume);
        }
    } catch (MPDException e) {
        handleMPDError(e);
    }
}
Also used : MPDCurrentStatus(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus) MPDException(org.gateshipone.malp.mpdservice.mpdprotocol.MPDException)

Example 2 with MPDException

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

the class MPDQueryHandler method handleMessage.

/**
 * This is the main entry point of messages.
 * Here all possible messages types need to be handled with the MPDConnection.
 * Have a look into the baseclass MPDGenericHandler for more information about the handling.
 *
 * @param msg Message to process.
 */
@Override
public void handleMessage(Message msg) {
    // Call the baseclass handleMessage method here to ensure that the messages handled
    // by the baseclass are handled in subclasses as well.
    super.handleMessage(msg);
    // Type checking
    if (!(msg.obj instanceof MPDHandlerAction)) {
        /* Check if the message object is of correct type. Otherwise just abort here. */
        return;
    }
    MPDHandlerAction mpdAction = (MPDHandlerAction) msg.obj;
    // ResponseHandler used to return the requested items to the caller
    MPDResponseHandler responseHandler;
    /*
         * All messages are handled the same way:
         *  * Check which action was requested
         *  * Check if a ResponseHandler is necessary and also provided. (If not just abort here)
         *  * Request the list of data objects from the MPDConnection (and therefor from the server)
         *  * Pack the response in a Message requested from the given ResponseHandler.
         *  * Send the message to the ResponseHandler
         */
    MPDHandlerAction.NET_HANDLER_ACTION action = mpdAction.getAction();
    try {
        if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ALBUMS) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseAlbumList)) {
                return;
            }
            List<MPDAlbum> albumList = MPDInterface.mInstance.getAlbums();
            ((MPDResponseAlbumList) responseHandler).sendAlbums(albumList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ALBUMS_IN_PATH) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseAlbumList)) {
                return;
            }
            String path = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PATH);
            List<MPDAlbum> albumList = MPDInterface.mInstance.getAlbumsInPath(path);
            ((MPDResponseAlbumList) responseHandler).sendAlbums(albumList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_ALBUMS_IN_PATH) {
            String path = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PATH);
            List<MPDAlbum> albumList = MPDInterface.mInstance.getAlbumsInPath(path);
            MPDInterface.mInstance.clearPlaylist();
            for (MPDAlbum album : albumList) {
                MPDInterface.mInstance.addAlbumTracks(album.getName(), album.getArtistName(), album.getMBID());
            }
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ARTIST_ALBUMS) {
            String artistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseAlbumList) || (null == artistName)) {
                return;
            }
            List<MPDAlbum> albumList = MPDInterface.mInstance.getArtistAlbums(artistName);
            ((MPDResponseAlbumList) responseHandler).sendAlbums(albumList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ARTISTSORT_ALBUMS) {
            String artistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseAlbumList) || (null == artistName)) {
                return;
            }
            List<MPDAlbum> albumList = MPDInterface.mInstance.getArtistSortAlbums(artistName);
            ((MPDResponseAlbumList) responseHandler).sendAlbums(albumList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ARTISTS) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseArtistList)) {
                return;
            }
            List<MPDArtist> artistList = MPDInterface.mInstance.getArtists();
            ((MPDResponseArtistList) responseHandler).sendArtists(artistList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ARTISTSORT) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseArtistList)) {
                return;
            }
            List<MPDArtist> artistList = MPDInterface.mInstance.getArtistsSort();
            ((MPDResponseArtistList) responseHandler).sendArtists(artistList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ALBUMARTISTS) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseArtistList)) {
                return;
            }
            MPDCapabilities caps = MPDInterface.mInstance.getServerCapabilities();
            List<MPDArtist> artistList;
            // Check if server supports the right tag, otherwise use fallback list
            if (null != caps && caps.hasTagAlbumArtist()) {
                artistList = MPDInterface.mInstance.getAlbumArtists();
            } else {
                // If server does not support the albumartist tag
                artistList = MPDInterface.mInstance.getArtists();
            }
            ((MPDResponseArtistList) responseHandler).sendArtists(artistList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ALBUMARTISTSORT) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseArtistList)) {
                return;
            }
            MPDCapabilities caps = MPDInterface.mInstance.getServerCapabilities();
            List<MPDArtist> artistList;
            // Check if server supports the right tag, otherwise use fallback list
            if (null != caps && caps.hasTagAlbumArtist()) {
                artistList = MPDInterface.mInstance.getAlbumArtistsSort();
            } else {
                // If server does not support the albumartist tag
                artistList = MPDInterface.mInstance.getArtistsSort();
            }
            ((MPDResponseArtistList) responseHandler).sendArtists(artistList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ALBUM_TRACKS) {
            String albumName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList) || (null == albumName)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getAlbumTracks(albumName, albumMBID);
            ((MPDResponseFileList) responseHandler).sendFileList(trackList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_TRACKS) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getAllTracks();
            ((MPDResponseFileList) responseHandler).sendFileList(trackList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ARTIST_ALBUM_TRACKS) {
            String artistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            String albumName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList) || (null == albumName) || (null == artistName)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getArtistAlbumTracks(albumName, artistName, albumMBID);
            ((MPDResponseFileList) responseHandler).sendFileList(trackList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_ARTIST_SORT_ALBUM_TRACKS) {
            String artistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            String albumName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList) || (null == albumName) || (null == artistName)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getArtistSortAlbumTracks(albumName, artistName, albumMBID);
            ((MPDResponseFileList) responseHandler).sendFileList(trackList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_CURRENT_PLAYLIST) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getCurrentPlaylist();
            ((MPDResponseFileList) responseHandler).sendFileList(trackList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_CURRENT_PLAYLIST_WINDOW) {
            int start = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_WINDOW_START);
            int end = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_WINDOW_END);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getCurrentPlaylistWindow(start, end);
            ((MPDResponseFileList) responseHandler).sendFileList(trackList, start, end);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_SAVED_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> trackList = MPDInterface.mInstance.getSavedPlaylist(playlistName);
            ((MPDResponseFileList) responseHandler).sendFileList(trackList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_SAVED_PLAYLISTS) {
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> playlistList = MPDInterface.mInstance.getPlaylists();
            ((MPDResponseFileList) responseHandler).sendFileList(playlistList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SAVE_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            MPDInterface.mInstance.savePlaylist(playlistName);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ADD_SONG_TO_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            String path = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PATH);
            MPDInterface.mInstance.addSongToPlaylist(playlistName, path);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_REMOVE_SONG_FROM_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            int position = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SONG_INDEX);
            MPDInterface.mInstance.removeSongFromPlaylist(playlistName, position);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_REMOVE_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            MPDInterface.mInstance.removePlaylist(playlistName);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_LOAD_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            MPDInterface.mInstance.loadPlaylist(playlistName);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_PLAYLIST) {
            String playlistName = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PLAYLIST_NAME);
            MPDInterface.mInstance.clearPlaylist();
            MPDInterface.mInstance.loadPlaylist(playlistName);
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ADD_ARTIST_ALBUM) {
            String albumname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            MPDInterface.mInstance.addAlbumTracks(albumname, artistname, albumMBID);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ADD_ARTIST_SORT_ALBUM) {
            String albumname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            MPDInterface.mInstance.addArtistSortAlbumTracks(albumname, artistname, albumMBID);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_ARTIST_ALBUM) {
            String albumname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            MPDInterface.mInstance.clearPlaylist();
            MPDInterface.mInstance.addAlbumTracks(albumname, artistname, albumMBID);
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_ARTIST_SORT_ALBUM) {
            String albumname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_NAME);
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            String albumMBID = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ALBUM_MBID);
            MPDInterface.mInstance.clearPlaylist();
            MPDInterface.mInstance.addArtistSortAlbumTracks(albumname, artistname, albumMBID);
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ADD_ARTIST) {
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            MPDAlbum.MPD_ALBUM_SORT_ORDER sortOrder = MPDAlbum.MPD_ALBUM_SORT_ORDER.values()[mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SORT_ORDER)];
            MPDInterface.mInstance.addArtist(artistname, sortOrder);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_ARTIST) {
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            MPDAlbum.MPD_ALBUM_SORT_ORDER sortOrder = MPDAlbum.MPD_ALBUM_SORT_ORDER.values()[mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SORT_ORDER)];
            MPDInterface.mInstance.clearPlaylist();
            MPDInterface.mInstance.addArtist(artistname, sortOrder);
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_ARTIST_SORT) {
            String artistname = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_ARTIST_NAME);
            MPDAlbum.MPD_ALBUM_SORT_ORDER sortOrder = MPDAlbum.MPD_ALBUM_SORT_ORDER.values()[mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SORT_ORDER)];
            MPDInterface.mInstance.clearPlaylist();
            MPDInterface.mInstance.addArtistSort(artistname, sortOrder);
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ADD_PATH) {
            String url = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_SONG_URL);
            MPDInterface.mInstance.addSong(url);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_SONG_NEXT) {
            String url = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_SONG_URL);
            MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
            MPDInterface.mInstance.addSongatIndex(url, status.getCurrentSongIndex() + 1);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_SONG) {
            String url = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_SONG_URL);
            MPDCapabilities caps = MPDInterface.mInstance.getServerCapabilities();
            /*
                 * Check if song is already enqueued in the current playlist. If it is get the position
                 * and just jump to the song position.
                 *
                 * Otherwise add it to the last playlist position and jump there.
                 */
            List<MPDFileEntry> playlistFindTracks = null;
            if (caps != null && caps.hasPlaylistFind()) {
                playlistFindTracks = MPDInterface.mInstance.getPlaylistFindTrack(url);
            }
            if (playlistFindTracks != null && playlistFindTracks.size() > 0) {
                // Song already found in the playlist. Jump there.
                MPDInterface.mInstance.playSongIndex(((MPDTrack) playlistFindTracks.get(0)).getSongPosition());
            } else {
                // Not part of the current playlist. Add it at the end of the playlist and play it from there.
                MPDInterface.mInstance.addSong(url);
                MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
                MPDInterface.mInstance.playSongIndex(status.getPlaylistLength() - 1);
            }
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_CLEAR_CURRENT_PLAYLIST) {
            MPDInterface.mInstance.clearPlaylist();
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SHUFFLE_CURRENT_PLAYLIST) {
            MPDInterface.mInstance.shufflePlaylist();
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_MOVE_SONG_AFTER_CURRENT) {
            MPDCurrentStatus status = MPDInterface.mInstance.getCurrentServerStatus();
            int index = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SONG_INDEX);
            if (index < status.getCurrentSongIndex()) {
                MPDInterface.mInstance.moveSongFromTo(index, status.getCurrentSongIndex());
            } else if (index > status.getCurrentSongIndex()) {
                MPDInterface.mInstance.moveSongFromTo(index, status.getCurrentSongIndex() + 1);
            }
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_REMOVE_SONG_FROM_CURRENT_PLAYLIST) {
            int index = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SONG_INDEX);
            MPDInterface.mInstance.removeIndex(index);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_REMOVE_RANGE_FROM_CURRENT_PLAYLIST) {
            int start = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_WINDOW_START);
            int end = mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_WINDOW_END);
            MPDInterface.mInstance.removeRange(start, end);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_FILES) {
            String path = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PATH);
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> fileList = MPDInterface.mInstance.getFiles(path);
            ((MPDResponseFileList) responseHandler).sendFileList(fileList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_DIRECTORY) {
            String path = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PATH);
            MPDInterface.mInstance.clearPlaylist();
            MPDInterface.mInstance.addSong(path);
            MPDInterface.mInstance.playSongIndex(0);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_OUTPUTS) {
            responseHandler = mpdAction.getResponseHandler();
            List<MPDOutput> outputList = MPDInterface.mInstance.getOutputs();
            ((MPDResponseOutputList) responseHandler).sendOutputs(outputList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_GET_SERVER_STATISTICS) {
            responseHandler = mpdAction.getResponseHandler();
            MPDStatistics stats;
            stats = MPDInterface.mInstance.getServerStatistics();
            ((MPDResponseServerStatistics) responseHandler).sendServerStatistics(stats);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_UPDATE_DATABASE) {
            String updatePath = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_PATH);
            MPDInterface.mInstance.updateDatabase(updatePath);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_SEARCH_FILES) {
            String term = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_SEARCH_TERM);
            MPDCommands.MPD_SEARCH_TYPE type = MPDCommands.MPD_SEARCH_TYPE.values()[mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SEARCH_TYPE)];
            responseHandler = mpdAction.getResponseHandler();
            if (!(responseHandler instanceof MPDResponseFileList)) {
                return;
            }
            List<MPDFileEntry> fileList = MPDInterface.mInstance.getSearchedFiles(term, type);
            ((MPDResponseFileList) responseHandler).sendFileList(fileList);
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_ADD_SEARCH_FILES) {
            String term = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_SEARCH_TERM);
            MPDCommands.MPD_SEARCH_TYPE type = MPDCommands.MPD_SEARCH_TYPE.values()[mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SEARCH_TYPE)];
            MPDCapabilities caps = MPDInterface.mInstance.getServerCapabilities();
            // Check if server has the add search result capability
            if (null != caps && caps.hasSearchAdd()) {
                MPDInterface.mInstance.addSearchedFiles(term, type);
            } else {
                // Fetch search results and add them
                List<MPDFileEntry> searchResults = MPDInterface.mInstance.getSearchedFiles(term, type);
                MPDInterface.mInstance.addTrackList(searchResults);
            }
        } else if (action == MPDHandlerAction.NET_HANDLER_ACTION.ACTION_PLAY_SEARCH_FILES) {
            String term = mpdAction.getStringExtra(MPDHandlerAction.NET_HANDLER_EXTRA_STRING.EXTRA_SEARCH_TERM);
            MPDCommands.MPD_SEARCH_TYPE type = MPDCommands.MPD_SEARCH_TYPE.values()[mpdAction.getIntExtra(MPDHandlerAction.NET_HANDLER_EXTRA_INT.EXTRA_SEARCH_TYPE)];
            MPDInterface.mInstance.clearPlaylist();
            // Check if server has the add search result capability
            if (MPDInterface.mInstance.getServerCapabilities().hasSearchAdd()) {
                MPDInterface.mInstance.addSearchedFiles(term, type);
            } else {
                // Fetch search results and add them
                List<MPDFileEntry> searchResults = MPDInterface.mInstance.getSearchedFiles(term, type);
                MPDInterface.mInstance.addTrackList(searchResults);
            }
            MPDInterface.mInstance.playSongIndex(0);
        }
    } catch (MPDException e) {
        handleMPDError(e);
    }
}
Also used : MPDResponseFileList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseFileList) MPDResponseAlbumList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseAlbumList) MPDAlbum(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDAlbum) MPDResponseArtistList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseArtistList) MPDResponseFileList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseFileList) List(java.util.List) MPDResponseAlbumList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseAlbumList) MPDResponseOutputList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseOutputList) MPDStatistics(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDStatistics) MPDFileEntry(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry) MPDCurrentStatus(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus) MPDException(org.gateshipone.malp.mpdservice.mpdprotocol.MPDException) MPDResponseHandler(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseHandler) MPDArtist(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDArtist) MPDCapabilities(org.gateshipone.malp.mpdservice.mpdprotocol.MPDCapabilities) MPDCommands(org.gateshipone.malp.mpdservice.mpdprotocol.MPDCommands) MPDOutput(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDOutput) MPDResponseArtistList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseArtistList) MPDResponseOutputList(org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseOutputList)

Example 3 with MPDException

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

the class MPDStateMonitoringHandler method resynchronizeState.

private void resynchronizeState() {
    // Stop the interpolation
    stopInterpolation();
    // If a resync timer is running kill it also. It will be restarted when idling again
    stopResynchronization();
    mLastTimeBase = System.nanoTime();
    MPDCurrentStatus status = null;
    try {
        status = MPDInterface.mInstance.getCurrentServerStatus();
    } catch (MPDException e) {
        handleMPDError(e);
        return;
    }
    if (status.getCurrentSongIndex() != mLastStatus.getCurrentSongIndex() || status.getPlaylistVersion() != mLastStatus.getPlaylistVersion()) {
        // New track started playing. Get it and inform the listener.
        try {
            mLastFile = MPDInterface.mInstance.getCurrentSong();
        } catch (MPDException e) {
            handleMPDError(e);
        }
        distributeNewTrack(mLastFile);
    }
    mLastStatus = status;
    distributeNewStatus(status);
    startInterpolation();
}
Also used : MPDCurrentStatus(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus) MPDException(org.gateshipone.malp.mpdservice.mpdprotocol.MPDException)

Aggregations

MPDException (org.gateshipone.malp.mpdservice.mpdprotocol.MPDException)3 MPDCurrentStatus (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus)3 List (java.util.List)1 MPDResponseAlbumList (org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseAlbumList)1 MPDResponseArtistList (org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseArtistList)1 MPDResponseFileList (org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseFileList)1 MPDResponseHandler (org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseHandler)1 MPDResponseOutputList (org.gateshipone.malp.mpdservice.handlers.responsehandler.MPDResponseOutputList)1 MPDCapabilities (org.gateshipone.malp.mpdservice.mpdprotocol.MPDCapabilities)1 MPDCommands (org.gateshipone.malp.mpdservice.mpdprotocol.MPDCommands)1 MPDAlbum (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDAlbum)1 MPDArtist (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDArtist)1 MPDFileEntry (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry)1 MPDOutput (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDOutput)1 MPDStatistics (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDStatistics)1