Search in sources :

Example 11 with MPDCurrentStatus

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

the class MPDResponseParser method parseMPDCurrentStatus.

static MPDCurrentStatus parseMPDCurrentStatus(final MPDConnection connection) throws MPDException {
    MPDCurrentStatus status = new MPDCurrentStatus();
    if (!connection.isConnected()) {
        return status;
    }
    /* Response line from MPD */
    String response = connection.readLine();
    while (response != null && !response.startsWith("OK")) {
        if (response.startsWith(MPDResponses.MPD_RESPONSE_VOLUME)) {
            try {
                status.setVolume(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_VOLUME.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_REPEAT)) {
            try {
                status.setRepeat(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_REPEAT.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_RANDOM)) {
            try {
                status.setRandom(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_RANDOM.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_SINGLE)) {
            try {
                status.setSinglePlayback(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_SINGLE.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_CONSUME)) {
            try {
                status.setConsume(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_CONSUME.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYLIST_VERSION)) {
            try {
                status.setPlaylistVersion(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_PLAYLIST_VERSION.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYLIST_LENGTH)) {
            try {
                status.setPlaylistLength(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_PLAYLIST_LENGTH.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYBACK_STATE)) {
            String state = response.substring(MPDResponses.MPD_RESPONSE_PLAYBACK_STATE.length());
            switch(state) {
                case MPDResponses.MPD_PLAYBACK_STATE_RESPONSE_PLAY:
                    status.setPlaybackState(MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PLAYING);
                    break;
                case MPDResponses.MPD_PLAYBACK_STATE_RESPONSE_PAUSE:
                    status.setPlaybackState(MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PAUSING);
                    break;
                case MPDResponses.MPD_PLAYBACK_STATE_RESPONSE_STOP:
                    status.setPlaybackState(MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_STOPPED);
                    break;
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_CURRENT_SONG_INDEX)) {
            try {
                status.setCurrentSongIndex(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_CURRENT_SONG_INDEX.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_NEXT_SONG_INDEX)) {
            try {
                status.setNextSongIndex(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_NEXT_SONG_INDEX.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_TIME_INFORMATION_OLD)) {
            String timeInfo = response.substring(MPDResponses.MPD_RESPONSE_TIME_INFORMATION_OLD.length());
            String[] timeInfoSep = timeInfo.split(":");
            if (timeInfoSep.length == 2) {
                try {
                    status.setElapsedTime(Integer.valueOf(timeInfoSep[0]));
                    status.setTrackLength(Integer.valueOf(timeInfoSep[1]));
                } catch (NumberFormatException ignored) {
                }
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_ELAPSED_TIME)) {
            try {
                status.setElapsedTime(Math.round(Float.valueOf(response.substring(MPDResponses.MPD_RESPONSE_ELAPSED_TIME.length()))));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_DURATION)) {
            try {
                status.setTrackLength(Math.round(Float.valueOf(response.substring(MPDResponses.MPD_RESPONSE_DURATION.length()))));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_BITRATE)) {
            try {
                status.setBitrate(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_BITRATE.length())));
            } catch (NumberFormatException ignored) {
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_AUDIO_INFORMATION)) {
            String audioInfo = response.substring(MPDResponses.MPD_RESPONSE_AUDIO_INFORMATION.length());
            String[] audioInfoSep = audioInfo.split(":");
            if (audioInfoSep.length == 3) {
                /* Extract the separate pieces */
                try {
                    /* First is sampleRate */
                    status.setSamplerate(Integer.valueOf(audioInfoSep[0]));
                    /* Second is bitresolution */
                    status.setBitDepth(audioInfoSep[1]);
                    /* Third is channel count */
                    status.setChannelCount(Integer.valueOf(audioInfoSep[2]));
                } catch (NumberFormatException ignored) {
                }
            }
        } else if (response.startsWith(MPDResponses.MPD_RESPONSE_UPDATING_DB)) {
            try {
                status.setUpdateDBJob(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_UPDATING_DB.length())));
            } catch (NumberFormatException ignored) {
            }
        }
        response = connection.readLine();
    }
    return status;
}
Also used : MPDCurrentStatus(org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus)

Aggregations

MPDCurrentStatus (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus)11 MPDException (org.gateshipone.malp.mpdservice.mpdprotocol.MPDException)3 MPDTrack (org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack)3 IntentFilter (android.content.IntentFilter)1 MenuInflater (android.view.MenuInflater)1 List (java.util.List)1 CoverBitmapLoader (org.gateshipone.malp.application.utils.CoverBitmapLoader)1 CurrentPlaylistView (org.gateshipone.malp.application.views.CurrentPlaylistView)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