Example usage for android.support.v4.media.session PlaybackStateCompat getCustomActions

List of usage examples for android.support.v4.media.session PlaybackStateCompat getCustomActions

Introduction

In this page you can find the example usage for android.support.v4.media.session PlaybackStateCompat getCustomActions.

Prototype

public List<PlaybackStateCompat.CustomAction> getCustomActions() 

Source Link

Document

Get the list of custom actions.

Usage

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

private void setupRows() {
    PlaybackControlsRowPresenter playbackControlsRowPresenter;
    playbackControlsRowPresenter = new PlaybackControlsRowPresenter(new DescriptionPresenter());

    playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() {
        public void onActionClicked(Action action) {
            if (action.getId() == playPauseAction.getId()) {
                if (lastPlaybackState != null) {
                    if (lastPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                        mediaController.getTransportControls().pause();
                    } else {
                        mediaController.getTransportControls().play();
                    }// ww  w  .  ja  v a2s .c o  m
                }
            } else if (action.getId() == skipNextAction.getId()) {
                mediaController.getTransportControls().skipToNext();
            } else if (action.getId() == skipPreviousAction.getId()) {
                mediaController.getTransportControls().skipToPrevious();
            } else if (action.getId() == stopAction.getId()) {
                mediaController.getTransportControls().stop();
            } else if (action.getId() == clearPlaylistAction.getId()) {
                Log.d(TAG, "onActionClicked -> Clear Playlist");
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();
                controls.sendCustomAction(MediaService.ACTION_CLEAR_PLAYLIST, null);
            } else if (action.getId() == shuffleAction.getId()) {
                Log.d(TAG, "onActionClicked -> Shuffle");

                PlaybackStateCompat state = mediaController.getPlaybackState();
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

                if (state == null) {
                    return;
                }

                for (PlaybackStateCompat.CustomAction cAction : state.getCustomActions()) {
                    switch (cAction.getAction()) {
                    case MediaService.STATE_SHUFFLE_ON:
                        controls.sendCustomAction(MediaService.STATE_SHUFFLE_ON, null);
                        break;

                    case MediaService.STATE_SHUFFLE_OFF:
                        controls.sendCustomAction(MediaService.STATE_SHUFFLE_OFF, null);
                        break;
                    }
                }

                updatePlaybackState(state);
            } else if (action.getId() == repeatAction.getId()) {
                Log.d(TAG, "onActionClicked -> Repeat");

                PlaybackStateCompat state = mediaController.getPlaybackState();
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

                if (state == null) {
                    return;
                }

                for (PlaybackStateCompat.CustomAction cAction : state.getCustomActions()) {
                    switch (cAction.getAction()) {
                    case MediaService.STATE_REPEAT_NONE:
                    case MediaService.STATE_REPEAT_ALL:
                    case MediaService.STATE_REPEAT_ONE:
                        controls.sendCustomAction(cAction.getAction(), null);
                        break;
                    }
                }

                updatePlaybackState(state);
            }
        }
    });

    presenterSelector.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter);
    presenterSelector.addClassPresenter(String.class, new HeaderPresenter());

    playbackControlsRow = new PlaybackControlsRow(new MediaDescriptionHolder());
    ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector();
    primaryActionsAdapter = new ArrayObjectAdapter(presenterSelector);
    secondaryActionsAdapter = new ArrayObjectAdapter(presenterSelector);
    playbackControlsRow.setPrimaryActionsAdapter(primaryActionsAdapter);
    playbackControlsRow.setSecondaryActionsAdapter(secondaryActionsAdapter);

    playPauseAction = new PlayPauseAction(getActivity());
    stopAction = new StopAction((getActivity()));
    skipNextAction = new PlaybackControlsRow.SkipNextAction(getActivity());
    skipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(getActivity());
    clearPlaylistAction = new ClearPlaylistAction(getActivity());
    shuffleAction = new PlaybackControlsRow.ShuffleAction(getActivity());
    repeatAction = new PlaybackControlsRow.RepeatAction(getActivity());

    primaryActionsAdapter.add(skipPreviousAction);
    primaryActionsAdapter.add(stopAction);
    primaryActionsAdapter.add(playPauseAction);
    primaryActionsAdapter.add(skipNextAction);

    secondaryActionsAdapter.add(shuffleAction);
    secondaryActionsAdapter.add(repeatAction);
    secondaryActionsAdapter.add(clearPlaylistAction);

    rowsAdapter.add(playbackControlsRow);

    // Add heading
    rowsAdapter.add(getString(R.string.heading_queue));

    setAdapter(rowsAdapter);
    setOnItemViewClickedListener(new ItemViewClickedListener());
}

From source file:com.scooter1556.sms.android.activity.FullScreenPlayerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen_player);
    initialiseToolbar();// w  ww  . j  a v  a2  s  .  c om
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
    }

    backgroundImage = (ImageView) findViewById(R.id.background_image);
    pauseDrawable = ContextCompat.getDrawable(this, R.drawable.ic_pause_white_48dp);
    playDrawable = ContextCompat.getDrawable(this, R.drawable.ic_play_arrow_white_48dp);
    playPause = (ImageView) findViewById(R.id.play_pause);
    skipNext = (ImageView) findViewById(R.id.next);
    skipPrev = (ImageView) findViewById(R.id.prev);
    shuffle = (ImageView) findViewById(R.id.shuffle);
    repeat = (ImageView) findViewById(R.id.repeat);
    start = (TextView) findViewById(R.id.startText);
    end = (TextView) findViewById(R.id.endText);
    seekbar = (SeekBar) findViewById(R.id.seekBar);
    title = (TextView) findViewById(R.id.title);
    subtitle = (TextView) findViewById(R.id.subtitle);
    extra = (TextView) findViewById(R.id.extra);
    loading = (ProgressBar) findViewById(R.id.progressBar);
    controllers = findViewById(R.id.controllers);

    skipNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();
            controls.skipToNext();
        }
    });

    skipPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();
            controls.skipToPrevious();
        }
    });

    playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = mediaController.getPlaybackState();
            if (state != null) {
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();
                switch (state.getState()) {
                case PlaybackStateCompat.STATE_PLAYING: // fall through
                case PlaybackStateCompat.STATE_BUFFERING:
                    controls.pause();
                    stopSeekbarUpdate();
                    break;
                case PlaybackStateCompat.STATE_PAUSED:
                case PlaybackStateCompat.STATE_STOPPED:
                    controls.play();
                    scheduleSeekbarUpdate();
                    break;
                }
            }
        }
    });

    shuffle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = mediaController.getPlaybackState();
            MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

            if (state == null) {
                return;
            }

            for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
                switch (action.getAction()) {
                case MediaService.STATE_SHUFFLE_ON:
                    controls.sendCustomAction(MediaService.STATE_SHUFFLE_ON, null);
                    break;

                case MediaService.STATE_SHUFFLE_OFF:
                    controls.sendCustomAction(MediaService.STATE_SHUFFLE_OFF, null);
                    break;
                }
            }

            updatePlaybackState(state);
        }
    });

    repeat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = mediaController.getPlaybackState();
            MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

            if (state == null) {
                return;
            }

            for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
                switch (action.getAction()) {
                case MediaService.STATE_REPEAT_NONE:
                case MediaService.STATE_REPEAT_ALL:
                case MediaService.STATE_REPEAT_ONE:
                    controls.sendCustomAction(action.getAction(), null);
                    break;
                }
            }

            updatePlaybackState(state);
        }
    });

    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            start.setText(DateUtils.formatElapsedTime(progress / 1000));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            stopSeekbarUpdate();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mediaController.getTransportControls().seekTo(seekBar.getProgress());
            scheduleSeekbarUpdate();
        }
    });

    // Only update from the intent if we are not recreating from a config change:
    if (savedInstanceState == null) {
        updateFromParams(getIntent());
    }

    mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MediaService.class), connectionCallback,
            null);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null || playbackControlsRow == null) {
        return;// w  w w  .j a va2s . co m
    }

    lastPlaybackState = state;

    switch (state.getState()) {

    case PlaybackStateCompat.STATE_PLAYING:
        scheduleSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PAUSE);
        break;

    case PlaybackStateCompat.STATE_PAUSED:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        break;

    case PlaybackStateCompat.STATE_NONE:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        resetPlaybackRow();
        updatePlayListRow();
        break;

    case PlaybackStateCompat.STATE_STOPPED:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        playbackControlsRow.setCurrentTime(0);
        break;

    case PlaybackStateCompat.STATE_BUFFERING:
        stopSeekbarUpdate();
        break;

    default:
        Log.d(TAG, "Unhandled state: " + state.getState());
    }

    // Custom Actions
    for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
        switch (action.getAction()) {
        case MediaService.STATE_SHUFFLE_ON:
            shuffleAction.setIcon(
                    ContextCompat.getDrawable(getActivity(), R.drawable.ic_shuffle_enabled_white_48dp));

            // Update interface if necessary
            if (shuffleMode == null || !shuffleMode.equals(MediaService.STATE_SHUFFLE_ON)) {
                shuffleMode = MediaService.STATE_SHUFFLE_ON;
                updatePlayListRow();
            }

            break;

        case MediaService.STATE_SHUFFLE_OFF:
            shuffleAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_shuffle_white_48dp));

            // Update interface if necessary
            if (shuffleMode == null || !shuffleMode.equals(MediaService.STATE_SHUFFLE_OFF)) {
                shuffleMode = MediaService.STATE_SHUFFLE_OFF;
                updatePlayListRow();
            }

            break;

        case MediaService.STATE_REPEAT_NONE:
            repeatAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_white_48dp));
            break;

        case MediaService.STATE_REPEAT_ALL:
            repeatAction
                    .setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_enable_white_48dp));
            break;

        case MediaService.STATE_REPEAT_ONE:
            repeatAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_one_white_48dp));
            break;
        }
    }

    rowsAdapter.notifyArrayItemRangeChanged(rowsAdapter.indexOf(playbackControlsRow), 1);
}

From source file:com.scooter1556.sms.android.activity.FullScreenPlayerActivity.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;/* w w w. j  a  va 2s. co  m*/
    }

    lastPlaybackState = state;
    if (mediaController != null && mediaController.getExtras() != null) {
        String castName = mediaController.getExtras().getString(MediaService.EXTRA_CONNECTED_CAST);
        String extraInfo = castName == null ? "" : getResources().getString(R.string.cast_to_device, castName);
        extra.setText(extraInfo);
    }

    switch (state.getState()) {

    case PlaybackStateCompat.STATE_PLAYING:
        loading.setVisibility(INVISIBLE);
        playPause.setVisibility(VISIBLE);
        playPause.setImageDrawable(pauseDrawable);
        controllers.setVisibility(VISIBLE);
        scheduleSeekbarUpdate();
        break;

    case PlaybackStateCompat.STATE_PAUSED:
        controllers.setVisibility(VISIBLE);
        loading.setVisibility(INVISIBLE);
        playPause.setVisibility(VISIBLE);
        playPause.setImageDrawable(playDrawable);
        stopSeekbarUpdate();
        break;

    case PlaybackStateCompat.STATE_NONE:

    case PlaybackStateCompat.STATE_STOPPED:
        loading.setVisibility(INVISIBLE);
        playPause.setVisibility(VISIBLE);
        playPause.setImageDrawable(playDrawable);
        stopSeekbarUpdate();
        break;

    case PlaybackStateCompat.STATE_BUFFERING:
        playPause.setVisibility(INVISIBLE);
        loading.setVisibility(VISIBLE);
        extra.setText(R.string.state_loading);
        stopSeekbarUpdate();
        break;

    default:
        Log.d(TAG, "Unhandled state: " + state.getState());
    }

    skipNext.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
    skipPrev.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
    shuffle.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE) == 0 ? INVISIBLE : VISIBLE);
    repeat.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SET_REPEAT_MODE) == 0 ? INVISIBLE : VISIBLE);

    // Custom Actions
    for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
        switch (action.getAction()) {
        case MediaService.STATE_SHUFFLE_ON:
            shuffle.setColorFilter(ContextCompat.getColor(getBaseContext(), R.color.accent));
            break;

        case MediaService.STATE_SHUFFLE_OFF:
            shuffle.clearColorFilter();
            break;

        case MediaService.STATE_REPEAT_NONE:
            repeat.setImageResource(R.drawable.ic_repeat_white_24dp);
            repeat.clearColorFilter();
            break;

        case MediaService.STATE_REPEAT_ALL:
            repeat.setImageResource(R.drawable.ic_repeat_white_24dp);
            repeat.setColorFilter(ContextCompat.getColor(getBaseContext(), R.color.accent));
            break;

        case MediaService.STATE_REPEAT_ONE:
            repeat.setImageResource(R.drawable.ic_repeat_one_white_24dp);
            repeat.setColorFilter(ContextCompat.getColor(getBaseContext(), R.color.accent));
            break;
        }
    }
}