Merge pull request #8011 from XiangRongLin/extract_view_listeners

Extract view click listeners from Player
This commit is contained in:
Stypox 2022-03-16 22:28:01 +01:00 committed by GitHub
commit 102204e293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 173 additions and 109 deletions

View File

@ -164,9 +164,10 @@ import org.schabi.newpipe.player.event.PlayerServiceEventListener;
import org.schabi.newpipe.player.helper.AudioReactor; import org.schabi.newpipe.player.helper.AudioReactor;
import org.schabi.newpipe.player.helper.LoadController; import org.schabi.newpipe.player.helper.LoadController;
import org.schabi.newpipe.player.helper.MediaSessionManager; import org.schabi.newpipe.player.helper.MediaSessionManager;
import org.schabi.newpipe.player.helper.PlaybackParameterDialog;
import org.schabi.newpipe.player.helper.PlayerDataSource; import org.schabi.newpipe.player.helper.PlayerDataSource;
import org.schabi.newpipe.player.helper.PlayerHelper; import org.schabi.newpipe.player.helper.PlayerHelper;
import org.schabi.newpipe.player.listeners.view.PlaybackSpeedClickListener;
import org.schabi.newpipe.player.listeners.view.QualityClickListener;
import org.schabi.newpipe.player.playback.CustomTrackSelector; import org.schabi.newpipe.player.playback.CustomTrackSelector;
import org.schabi.newpipe.player.playback.MediaSourceManager; import org.schabi.newpipe.player.playback.MediaSourceManager;
import org.schabi.newpipe.player.playback.PlaybackListener; import org.schabi.newpipe.player.playback.PlaybackListener;
@ -530,9 +531,12 @@ public final class Player implements
} }
private void initListeners() { private void initListeners() {
binding.qualityTextView.setOnClickListener(
new QualityClickListener(this, qualityPopupMenu));
binding.playbackSpeed.setOnClickListener(
new PlaybackSpeedClickListener(this, playbackSpeedPopupMenu));
binding.playbackSeekBar.setOnSeekBarChangeListener(this); binding.playbackSeekBar.setOnSeekBarChangeListener(this);
binding.playbackSpeed.setOnClickListener(this);
binding.qualityTextView.setOnClickListener(this);
binding.captionTextView.setOnClickListener(this); binding.captionTextView.setOnClickListener(this);
binding.resizeTextView.setOnClickListener(this); binding.resizeTextView.setOnClickListener(this);
binding.playbackLiveSync.setOnClickListener(this); binding.playbackLiveSync.setOnClickListener(this);
@ -541,11 +545,15 @@ public final class Player implements
gestureDetector = new GestureDetectorCompat(context, playerGestureListener); gestureDetector = new GestureDetectorCompat(context, playerGestureListener);
binding.getRoot().setOnTouchListener(playerGestureListener); binding.getRoot().setOnTouchListener(playerGestureListener);
binding.queueButton.setOnClickListener(this); binding.queueButton.setOnClickListener(v -> onQueueClicked());
binding.segmentsButton.setOnClickListener(this); binding.segmentsButton.setOnClickListener(v -> onSegmentsClicked());
binding.repeatButton.setOnClickListener(this); binding.repeatButton.setOnClickListener(v -> onRepeatClicked());
binding.shuffleButton.setOnClickListener(this); binding.shuffleButton.setOnClickListener(v -> onShuffleClicked());
binding.addToPlaylistButton.setOnClickListener(this); binding.addToPlaylistButton.setOnClickListener(v -> {
if (getParentActivity() != null) {
onAddToPlaylistClicked(getParentActivity().getSupportFragmentManager());
}
});
binding.playPauseButton.setOnClickListener(this); binding.playPauseButton.setOnClickListener(this);
binding.playPreviousButton.setOnClickListener(this); binding.playPreviousButton.setOnClickListener(this);
@ -1926,7 +1934,7 @@ public final class Player implements
}, delay); }, delay);
} }
private void showHideShadow(final boolean show, final long duration) { public void showHideShadow(final boolean show, final long duration) {
animate(binding.playbackControlsShadow, show, duration, AnimationType.ALPHA, 0, null); animate(binding.playbackControlsShadow, show, duration, AnimationType.ALPHA, 0, null);
animate(binding.playerTopShadow, show, duration, AnimationType.ALPHA, 0, null); animate(binding.playerTopShadow, show, duration, AnimationType.ALPHA, 0, null);
animate(binding.playerBottomShadow, show, duration, AnimationType.ALPHA, 0, null); animate(binding.playerBottomShadow, show, duration, AnimationType.ALPHA, 0, null);
@ -3607,37 +3615,6 @@ public final class Player implements
} }
} }
private void onQualitySelectorClicked() {
if (DEBUG) {
Log.d(TAG, "onQualitySelectorClicked() called");
}
qualityPopupMenu.show();
isSomePopupMenuVisible = true;
final VideoStream videoStream = getSelectedVideoStream();
if (videoStream != null) {
final String qualityText = MediaFormat.getNameById(videoStream.getFormatId()) + " "
+ videoStream.resolution;
binding.qualityTextView.setText(qualityText);
}
saveWasPlaying();
}
private void onPlaybackSpeedClicked() {
if (DEBUG) {
Log.d(TAG, "onPlaybackSpeedClicked() called");
}
if (videoPlayerSelected()) {
PlaybackParameterDialog.newInstance(getPlaybackSpeed(), getPlaybackPitch(),
getPlaybackSkipSilence(), this::setPlaybackParameters)
.show(getParentActivity().getSupportFragmentManager(), null);
} else {
playbackSpeedPopupMenu.show();
isSomePopupMenuVisible = true;
}
}
private void onCaptionClicked() { private void onCaptionClicked() {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onCaptionClicked() called"); Log.d(TAG, "onCaptionClicked() called");
@ -3742,11 +3719,7 @@ public final class Player implements
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onClick() called with: v = [" + v + "]"); Log.d(TAG, "onClick() called with: v = [" + v + "]");
} }
if (v.getId() == binding.qualityTextView.getId()) { if (v.getId() == binding.resizeTextView.getId()) {
onQualitySelectorClicked();
} else if (v.getId() == binding.playbackSpeed.getId()) {
onPlaybackSpeedClicked();
} else if (v.getId() == binding.resizeTextView.getId()) {
onResizeClicked(); onResizeClicked();
} else if (v.getId() == binding.captionTextView.getId()) { } else if (v.getId() == binding.captionTextView.getId()) {
onCaptionClicked(); onCaptionClicked();
@ -3758,23 +3731,6 @@ public final class Player implements
playPrevious(); playPrevious();
} else if (v.getId() == binding.playNextButton.getId()) { } else if (v.getId() == binding.playNextButton.getId()) {
playNext(); playNext();
} else if (v.getId() == binding.queueButton.getId()) {
onQueueClicked();
return;
} else if (v.getId() == binding.segmentsButton.getId()) {
onSegmentsClicked();
return;
} else if (v.getId() == binding.repeatButton.getId()) {
onRepeatClicked();
return;
} else if (v.getId() == binding.shuffleButton.getId()) {
onShuffleClicked();
return;
} else if (v.getId() == binding.addToPlaylistButton.getId()) {
if (getParentActivity() != null) {
onAddToPlaylistClicked(getParentActivity().getSupportFragmentManager());
}
return;
} else if (v.getId() == binding.moreOptionsButton.getId()) { } else if (v.getId() == binding.moreOptionsButton.getId()) {
onMoreOptionsClicked(); onMoreOptionsClicked();
} else if (v.getId() == binding.share.getId()) { } else if (v.getId() == binding.share.getId()) {
@ -3803,23 +3759,33 @@ public final class Player implements
context.sendBroadcast(new Intent(VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER)); context.sendBroadcast(new Intent(VideoDetailFragment.ACTION_HIDE_MAIN_PLAYER));
} }
if (currentState != STATE_COMPLETED) { manageControlsAfterOnClick(v);
controlsVisibilityHandler.removeCallbacksAndMessages(null); }
showHideShadow(true, DEFAULT_CONTROLS_DURATION);
animate(binding.playbackControlRoot, true, DEFAULT_CONTROLS_DURATION, /**
AnimationType.ALPHA, 0, () -> { * Manages the controls after a click occurred on the player UI.
if (currentState == STATE_PLAYING && !isSomePopupMenuVisible) { * @param v The view that was clicked
if (v.getId() == binding.playPauseButton.getId() */
// Hide controls in fullscreen immediately public void manageControlsAfterOnClick(@NonNull final View v) {
|| (v.getId() == binding.screenRotationButton.getId() if (currentState == STATE_COMPLETED) {
&& isFullscreen)) { return;
hideControls(0, 0);
} else {
hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
}
}
});
} }
controlsVisibilityHandler.removeCallbacksAndMessages(null);
showHideShadow(true, DEFAULT_CONTROLS_DURATION);
animate(binding.playbackControlRoot, true, DEFAULT_CONTROLS_DURATION,
AnimationType.ALPHA, 0, () -> {
if (currentState == STATE_PLAYING && !isSomePopupMenuVisible) {
if (v.getId() == binding.playPauseButton.getId()
// Hide controls in fullscreen immediately
|| (v.getId() == binding.screenRotationButton.getId()
&& isFullscreen)) {
hideControls(0, 0);
} else {
hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
}
}
});
} }
@Override @Override
@ -4446,6 +4412,10 @@ public final class Player implements
return isSomePopupMenuVisible; return isSomePopupMenuVisible;
} }
public void setSomePopupMenuVisible(final boolean somePopupMenuVisible) {
isSomePopupMenuVisible = somePopupMenuVisible;
}
public ImageButton getPlayPauseButton() { public ImageButton getPlayPauseButton() {
return binding.playPauseButton; return binding.playPauseButton;
} }
@ -4527,6 +4497,11 @@ public final class Player implements
public PlayQueueAdapter getPlayQueueAdapter() { public PlayQueueAdapter getPlayQueueAdapter() {
return playQueueAdapter; return playQueueAdapter;
} }
public PlayerBinding getBinding() {
return binding;
}
//endregion //endregion

View File

@ -0,0 +1,47 @@
package org.schabi.newpipe.player.listeners.view
import android.util.Log
import android.view.View
import androidx.appcompat.widget.PopupMenu
import org.schabi.newpipe.MainActivity
import org.schabi.newpipe.player.Player
import org.schabi.newpipe.player.helper.PlaybackParameterDialog
/**
* Click listener for the playbackSpeed textview of the player
*/
class PlaybackSpeedClickListener(
private val player: Player,
private val playbackSpeedPopupMenu: PopupMenu
) : View.OnClickListener {
companion object {
private const val TAG: String = "PlaybSpeedClickListener"
}
override fun onClick(v: View) {
if (MainActivity.DEBUG) {
Log.d(TAG, "onPlaybackSpeedClicked() called")
}
if (player.videoPlayerSelected()) {
PlaybackParameterDialog.newInstance(
player.playbackSpeed.toDouble(),
player.playbackPitch.toDouble(),
player.playbackSkipSilence
) { speed: Float, pitch: Float, skipSilence: Boolean ->
player.setPlaybackParameters(
speed,
pitch,
skipSilence
)
}
.show(player.parentActivity!!.supportFragmentManager, null)
} else {
playbackSpeedPopupMenu.show()
player.isSomePopupMenuVisible = true
}
player.manageControlsAfterOnClick(v)
}
}

View File

@ -0,0 +1,41 @@
package org.schabi.newpipe.player.listeners.view
import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import androidx.appcompat.widget.PopupMenu
import org.schabi.newpipe.MainActivity
import org.schabi.newpipe.extractor.MediaFormat
import org.schabi.newpipe.player.Player
/**
* Click listener for the qualityTextView of the player
*/
class QualityClickListener(
private val player: Player,
private val qualityPopupMenu: PopupMenu
) : View.OnClickListener {
companion object {
private const val TAG: String = "QualityClickListener"
}
@SuppressLint("SetTextI18n") // we don't need I18N because of a " "
override fun onClick(v: View) {
if (MainActivity.DEBUG) {
Log.d(TAG, "onQualitySelectorClicked() called")
}
qualityPopupMenu.show()
player.isSomePopupMenuVisible = true
val videoStream = player.selectedVideoStream
if (videoStream != null) {
player.binding.qualityTextView.text =
MediaFormat.getNameById(videoStream.formatId) + " " + videoStream.resolution
}
player.saveWasPlaying()
player.manageControlsAfterOnClick(v)
}
}

View File

@ -563,37 +563,6 @@
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:visibility="gone" /> android:visibility="gone" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/itemsListClose"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="40dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/close"
android:focusable="true"
android:padding="10dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_close"
app:tint="@color/white" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/addToPlaylistButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/itemsListClose"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="10dp"
android:scaleType="fitXY"
android:tint="?attr/colorAccent"
app:srcCompat="@drawable/ic_playlist_add"
tools:ignore="ContentDescription,RtlHardcoded" />
<androidx.appcompat.widget.AppCompatImageButton <androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/repeatButton" android:id="@+id/repeatButton"
android:layout_width="50dp" android:layout_width="50dp"
@ -637,6 +606,38 @@
android:layout_toEndOf="@id/shuffleButton" android:layout_toEndOf="@id/shuffleButton"
android:gravity="center" android:gravity="center"
android:textColor="@android:color/white" /> android:textColor="@android:color/white" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/addToPlaylistButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/itemsListClose"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="10dp"
android:scaleType="fitXY"
android:tint="?attr/colorAccent"
app:srcCompat="@drawable/ic_playlist_add"
tools:ignore="ContentDescription,RtlHardcoded" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/itemsListClose"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="40dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/close"
android:focusable="true"
android:padding="10dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_close"
app:tint="@color/white" />
</RelativeLayout> </RelativeLayout>
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView