diff --git a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java index 4bf6ef9ae7b..41d66dc90df 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.player; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; @@ -11,15 +12,10 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageButton; -import android.widget.LinearLayout; import android.widget.PopupMenu; -import android.widget.ProgressBar; import android.widget.SeekBar; -import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; import androidx.core.app.ActivityCompat; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; @@ -29,6 +25,7 @@ import com.google.android.exoplayer2.Player; import org.schabi.newpipe.R; +import org.schabi.newpipe.databinding.ActivityPlayerQueueControlBinding; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.fragments.OnScrollBelowItemsListener; import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; @@ -70,30 +67,10 @@ public abstract class ServicePlayerActivity extends AppCompatActivity // Views //////////////////////////////////////////////////////////////////////////// - private View rootView; + private ActivityPlayerQueueControlBinding queueControlBinding; - private RecyclerView itemsList; private ItemTouchHelper itemTouchHelper; - private LinearLayout metadata; - private TextView metadataTitle; - private TextView metadataArtist; - - private SeekBar progressSeekBar; - private TextView progressCurrentTime; - private TextView progressEndTime; - private TextView progressLiveSync; - private TextView seekDisplay; - - private ImageButton repeatButton; - private ImageButton backwardButton; - private ImageButton fastRewindButton; - private ImageButton playPauseButton; - private ImageButton fastForwardButton; - private ImageButton forwardButton; - private ImageButton shuffleButton; - private ProgressBar progressBar; - private Menu menu; //////////////////////////////////////////////////////////////////////////// @@ -123,11 +100,11 @@ protected void onCreate(final Bundle savedInstanceState) { assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); ThemeHelper.setTheme(this); - setContentView(R.layout.activity_player_queue_control); - rootView = findViewById(R.id.main_content); - final Toolbar toolbar = rootView.findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + queueControlBinding = ActivityPlayerQueueControlBinding.inflate(getLayoutInflater()); + setContentView(queueControlBinding.getRoot()); + + setSupportActionBar(queueControlBinding.toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(getSupportActionTitle()); @@ -230,14 +207,11 @@ private void unbind() { if (player != null && player.getPlayQueueAdapter() != null) { player.getPlayQueueAdapter().unsetSelectedListener(); } - if (itemsList != null) { - itemsList.setAdapter(null); - } + queueControlBinding.playQueue.setAdapter(null); if (itemTouchHelper != null) { itemTouchHelper.attachToRecyclerView(null); } - itemsList = null; itemTouchHelper = null; player = null; } @@ -284,58 +258,38 @@ private void buildComponents() { } private void buildQueue() { - itemsList = findViewById(R.id.play_queue); - itemsList.setLayoutManager(new LinearLayoutManager(this)); - itemsList.setAdapter(player.getPlayQueueAdapter()); - itemsList.setClickable(true); - itemsList.setLongClickable(true); - itemsList.clearOnScrollListeners(); - itemsList.addOnScrollListener(getQueueScrollListener()); + queueControlBinding.playQueue.setLayoutManager(new LinearLayoutManager(this)); + queueControlBinding.playQueue.setAdapter(player.getPlayQueueAdapter()); + queueControlBinding.playQueue.setClickable(true); + queueControlBinding.playQueue.setLongClickable(true); + queueControlBinding.playQueue.clearOnScrollListeners(); + queueControlBinding.playQueue.addOnScrollListener(getQueueScrollListener()); itemTouchHelper = new ItemTouchHelper(getItemTouchCallback()); - itemTouchHelper.attachToRecyclerView(itemsList); + itemTouchHelper.attachToRecyclerView(queueControlBinding.playQueue); player.getPlayQueueAdapter().setSelectedListener(getOnSelectedListener()); } private void buildMetadata() { - metadata = rootView.findViewById(R.id.metadata); - metadataTitle = rootView.findViewById(R.id.song_name); - metadataArtist = rootView.findViewById(R.id.artist_name); - - metadata.setOnClickListener(this); - metadataTitle.setSelected(true); - metadataArtist.setSelected(true); + queueControlBinding.metadata.setOnClickListener(this); + queueControlBinding.songName.setSelected(true); + queueControlBinding.artistName.setSelected(true); } private void buildSeekBar() { - progressCurrentTime = rootView.findViewById(R.id.current_time); - progressSeekBar = rootView.findViewById(R.id.seek_bar); - progressEndTime = rootView.findViewById(R.id.end_time); - progressLiveSync = rootView.findViewById(R.id.live_sync); - seekDisplay = rootView.findViewById(R.id.seek_display); - - progressSeekBar.setOnSeekBarChangeListener(this); - progressLiveSync.setOnClickListener(this); + queueControlBinding.seekBar.setOnSeekBarChangeListener(this); + queueControlBinding.liveSync.setOnClickListener(this); } private void buildControls() { - repeatButton = rootView.findViewById(R.id.control_repeat); - backwardButton = rootView.findViewById(R.id.control_backward); - fastRewindButton = rootView.findViewById(R.id.control_fast_rewind); - playPauseButton = rootView.findViewById(R.id.control_play_pause); - fastForwardButton = rootView.findViewById(R.id.control_fast_forward); - forwardButton = rootView.findViewById(R.id.control_forward); - shuffleButton = rootView.findViewById(R.id.control_shuffle); - progressBar = rootView.findViewById(R.id.control_progress_bar); - - repeatButton.setOnClickListener(this); - backwardButton.setOnClickListener(this); - fastRewindButton.setOnClickListener(this); - playPauseButton.setOnClickListener(this); - fastForwardButton.setOnClickListener(this); - forwardButton.setOnClickListener(this); - shuffleButton.setOnClickListener(this); + queueControlBinding.controlRepeat.setOnClickListener(this); + queueControlBinding.controlBackward.setOnClickListener(this); + queueControlBinding.controlFastRewind.setOnClickListener(this); + queueControlBinding.controlPlayPause.setOnClickListener(this); + queueControlBinding.controlFastForward.setOnClickListener(this); + queueControlBinding.controlForward.setOnClickListener(this); + queueControlBinding.controlShuffle.setOnClickListener(this); } private void buildItemPopupMenu(final PlayQueueItem item, final View view) { @@ -391,8 +345,8 @@ public void onScrolledDown(final RecyclerView recyclerView) { if (player != null && player.getPlayQueue() != null && !player.getPlayQueue().isComplete()) { player.getPlayQueue().fetch(); - } else if (itemsList != null) { - itemsList.clearOnScrollListeners(); + } else { + queueControlBinding.playQueue.clearOnScrollListeners(); } } }; @@ -453,8 +407,9 @@ private void scrollToSelected() { final int currentPlayingIndex = player.getPlayQueue().getIndex(); final int currentVisibleIndex; - if (itemsList.getLayoutManager() instanceof LinearLayoutManager) { - final LinearLayoutManager layout = ((LinearLayoutManager) itemsList.getLayoutManager()); + if (queueControlBinding.playQueue.getLayoutManager() instanceof LinearLayoutManager) { + final LinearLayoutManager layout = + (LinearLayoutManager) queueControlBinding.playQueue.getLayoutManager(); currentVisibleIndex = layout.findFirstVisibleItemPosition(); } else { currentVisibleIndex = 0; @@ -462,9 +417,9 @@ private void scrollToSelected() { final int distance = Math.abs(currentPlayingIndex - currentVisibleIndex); if (distance < SMOOTH_SCROLL_MAXIMUM_DISTANCE) { - itemsList.smoothScrollToPosition(currentPlayingIndex); + queueControlBinding.playQueue.smoothScrollToPosition(currentPlayingIndex); } else { - itemsList.scrollToPosition(currentPlayingIndex); + queueControlBinding.playQueue.scrollToPosition(currentPlayingIndex); } } @@ -478,23 +433,23 @@ public void onClick(final View view) { return; } - if (view.getId() == repeatButton.getId()) { + if (view.getId() == queueControlBinding.controlRepeat.getId()) { player.onRepeatClicked(); - } else if (view.getId() == backwardButton.getId()) { + } else if (view.getId() == queueControlBinding.controlBackward.getId()) { player.onPlayPrevious(); - } else if (view.getId() == fastRewindButton.getId()) { + } else if (view.getId() == queueControlBinding.controlFastRewind.getId()) { player.onFastRewind(); - } else if (view.getId() == playPauseButton.getId()) { + } else if (view.getId() == queueControlBinding.controlPlayPause.getId()) { player.onPlayPause(); - } else if (view.getId() == fastForwardButton.getId()) { + } else if (view.getId() == queueControlBinding.controlFastForward.getId()) { player.onFastForward(); - } else if (view.getId() == forwardButton.getId()) { + } else if (view.getId() == queueControlBinding.controlForward.getId()) { player.onPlayNext(); - } else if (view.getId() == shuffleButton.getId()) { + } else if (view.getId() == queueControlBinding.controlShuffle.getId()) { player.onShuffleClicked(); - } else if (view.getId() == metadata.getId()) { + } else if (view.getId() == queueControlBinding.metadata.getId()) { scrollToSelected(); - } else if (view.getId() == progressLiveSync.getId()) { + } else if (view.getId() == queueControlBinding.liveSync.getId()) { player.seekToDefault(); } } @@ -528,15 +483,15 @@ public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { if (fromUser) { final String seekTime = Localization.getDurationString(progress / 1000); - progressCurrentTime.setText(seekTime); - seekDisplay.setText(seekTime); + queueControlBinding.currentTime.setText(seekTime); + queueControlBinding.seekDisplay.setText(seekTime); } } @Override public void onStartTrackingTouch(final SeekBar seekBar) { seeking = true; - seekDisplay.setVisibility(View.VISIBLE); + queueControlBinding.seekDisplay.setVisibility(View.VISIBLE); } @Override @@ -544,7 +499,7 @@ public void onStopTrackingTouch(final SeekBar seekBar) { if (player != null) { player.seekTo(seekBar.getProgress()); } - seekDisplay.setVisibility(View.GONE); + queueControlBinding.seekDisplay.setVisibility(View.GONE); seeking = false; } @@ -602,45 +557,46 @@ public void onPlaybackUpdate(final int state, final int repeatMode, final boolea public void onProgressUpdate(final int currentProgress, final int duration, final int bufferPercent) { // Set buffer progress - progressSeekBar.setSecondaryProgress((int) (progressSeekBar.getMax() + queueControlBinding.seekBar.setSecondaryProgress((int) (queueControlBinding.seekBar.getMax() * ((float) bufferPercent / 100))); // Set Duration - progressSeekBar.setMax(duration); - progressEndTime.setText(Localization.getDurationString(duration / 1000)); + queueControlBinding.seekBar.setMax(duration); + queueControlBinding.endTime.setText(Localization.getDurationString(duration / 1000)); // Set current time if not seeking if (!seeking) { - progressSeekBar.setProgress(currentProgress); - progressCurrentTime.setText(Localization.getDurationString(currentProgress / 1000)); + queueControlBinding.seekBar.setProgress(currentProgress); + queueControlBinding.currentTime.setText(Localization + .getDurationString(currentProgress / 1000)); } if (player != null) { - progressLiveSync.setClickable(!player.isLiveEdge()); + queueControlBinding.liveSync.setClickable(!player.isLiveEdge()); } // this will make sure progressCurrentTime has the same width as progressEndTime - final ViewGroup.LayoutParams endTimeParams = progressEndTime.getLayoutParams(); - final ViewGroup.LayoutParams currentTimeParams = progressCurrentTime.getLayoutParams(); - currentTimeParams.width = progressEndTime.getWidth(); - progressCurrentTime.setLayoutParams(currentTimeParams); + final ViewGroup.LayoutParams currentTimeParams = + queueControlBinding.currentTime.getLayoutParams(); + currentTimeParams.width = queueControlBinding.endTime.getWidth(); + queueControlBinding.currentTime.setLayoutParams(currentTimeParams); } @Override public void onMetadataUpdate(final StreamInfo info, final PlayQueue queue) { if (info != null) { - metadataTitle.setText(info.getName()); - metadataArtist.setText(info.getUploaderName()); + queueControlBinding.songName.setText(info.getName()); + queueControlBinding.artistName.setText(info.getUploaderName()); - progressEndTime.setVisibility(View.GONE); - progressLiveSync.setVisibility(View.GONE); + queueControlBinding.endTime.setVisibility(View.GONE); + queueControlBinding.liveSync.setVisibility(View.GONE); switch (info.getStreamType()) { case LIVE_STREAM: case AUDIO_LIVE_STREAM: - progressLiveSync.setVisibility(View.VISIBLE); + queueControlBinding.liveSync.setVisibility(View.VISIBLE); break; default: - progressEndTime.setVisibility(View.VISIBLE); + queueControlBinding.endTime.setVisibility(View.VISIBLE); break; } @@ -661,13 +617,16 @@ public void onServiceStopped() { private void onStateChanged(final int state) { switch (state) { case BasePlayer.STATE_PAUSED: - playPauseButton.setImageResource(R.drawable.ic_play_arrow_white_24dp); + queueControlBinding.controlPlayPause + .setImageResource(R.drawable.ic_play_arrow_white_24dp); break; case BasePlayer.STATE_PLAYING: - playPauseButton.setImageResource(R.drawable.ic_pause_white_24dp); + queueControlBinding.controlPlayPause + .setImageResource(R.drawable.ic_pause_white_24dp); break; case BasePlayer.STATE_COMPLETED: - playPauseButton.setImageResource(R.drawable.ic_replay_white_24dp); + queueControlBinding.controlPlayPause + .setImageResource(R.drawable.ic_replay_white_24dp); break; default: break; @@ -677,14 +636,14 @@ private void onStateChanged(final int state) { case BasePlayer.STATE_PAUSED: case BasePlayer.STATE_PLAYING: case BasePlayer.STATE_COMPLETED: - playPauseButton.setClickable(true); - playPauseButton.setVisibility(View.VISIBLE); - progressBar.setVisibility(View.GONE); + queueControlBinding.controlPlayPause.setClickable(true); + queueControlBinding.controlPlayPause.setVisibility(View.VISIBLE); + queueControlBinding.progressBar.setVisibility(View.GONE); break; default: - playPauseButton.setClickable(false); - playPauseButton.setVisibility(View.INVISIBLE); - progressBar.setVisibility(View.VISIBLE); + queueControlBinding.controlPlayPause.setClickable(false); + queueControlBinding.controlPlayPause.setVisibility(View.INVISIBLE); + queueControlBinding.progressBar.setVisibility(View.VISIBLE); break; } } @@ -692,18 +651,21 @@ private void onStateChanged(final int state) { private void onPlayModeChanged(final int repeatMode, final boolean shuffled) { switch (repeatMode) { case Player.REPEAT_MODE_OFF: - repeatButton.setImageResource(R.drawable.exo_controls_repeat_off); + queueControlBinding.controlRepeat + .setImageResource(R.drawable.exo_controls_repeat_off); break; case Player.REPEAT_MODE_ONE: - repeatButton.setImageResource(R.drawable.exo_controls_repeat_one); + queueControlBinding.controlRepeat + .setImageResource(R.drawable.exo_controls_repeat_one); break; case Player.REPEAT_MODE_ALL: - repeatButton.setImageResource(R.drawable.exo_controls_repeat_all); + queueControlBinding.controlRepeat + .setImageResource(R.drawable.exo_controls_repeat_all); break; } final int shuffleAlpha = shuffled ? 255 : 77; - shuffleButton.setImageAlpha(shuffleAlpha); + queueControlBinding.controlShuffle.setImageAlpha(shuffleAlpha); } private void onPlaybackParameterChanged(final PlaybackParameters parameters) { @@ -716,12 +678,13 @@ private void onPlaybackParameterChanged(final PlaybackParameters parameters) { } private void onMaybePlaybackAdapterChanged() { - if (itemsList == null || player == null) { + if (player == null) { return; } final PlayQueueAdapter maybeNewAdapter = player.getPlayQueueAdapter(); - if (maybeNewAdapter != null && itemsList.getAdapter() != maybeNewAdapter) { - itemsList.setAdapter(maybeNewAdapter); + if (maybeNewAdapter != null + && queueControlBinding.playQueue.getAdapter() != maybeNewAdapter) { + queueControlBinding.playQueue.setAdapter(maybeNewAdapter); } } @@ -735,7 +698,8 @@ private void onMaybeMuteChanged() { //2) Icon change accordingly to current App Theme // using rootView.getContext() because getApplicationContext() didn't work - item.setIcon(ThemeHelper.resolveResourceIdFromAttr(rootView.getContext(), + final Context context = queueControlBinding.getRoot().getContext(); + item.setIcon(ThemeHelper.resolveResourceIdFromAttr(context, player.isMuted() ? R.attr.ic_volume_off : R.attr.ic_volume_up));