1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

Use view binding in ServicePlayerActivity.

This commit is contained in:
Isira Seneviratne 2020-10-31 15:33:00 +05:30
parent 22ec70e94d
commit e6021465f6

View File

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