From 080c4ba68051e37930c512c40c644866dd253a35 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Thu, 10 Mar 2022 20:20:34 +0100 Subject: [PATCH] Extract 2 view click listeners from Player --- .../org/schabi/newpipe/player/Player.java | 26 +++++++---- .../listeners/view/PlaybackSpeedListener.kt | 46 +++++++++++++++++++ .../listeners/view/QualityTextListener.kt | 41 +++++++++++++++++ 3 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/player/listeners/view/PlaybackSpeedListener.kt create mode 100644 app/src/main/java/org/schabi/newpipe/player/listeners/view/QualityTextListener.kt diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index 481f9a6d8..a32ae247d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -167,6 +167,8 @@ 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.PlayerHelper; +import org.schabi.newpipe.player.listeners.view.PlaybackSpeedListener; +import org.schabi.newpipe.player.listeners.view.QualityTextListener; import org.schabi.newpipe.player.playback.CustomTrackSelector; import org.schabi.newpipe.player.playback.MediaSourceManager; import org.schabi.newpipe.player.playback.PlaybackListener; @@ -530,9 +532,12 @@ public final class Player implements } private void initListeners() { + binding.qualityTextView.setOnClickListener( + new QualityTextListener(this, qualityPopupMenu)); + binding.playbackSpeed.setOnClickListener( + new PlaybackSpeedListener(this, playbackSpeedPopupMenu)); + binding.playbackSeekBar.setOnSeekBarChangeListener(this); - binding.playbackSpeed.setOnClickListener(this); - binding.qualityTextView.setOnClickListener(this); binding.captionTextView.setOnClickListener(this); binding.resizeTextView.setOnClickListener(this); binding.playbackLiveSync.setOnClickListener(this); @@ -1926,7 +1931,7 @@ public final class Player implements }, 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.playerTopShadow, show, duration, AnimationType.ALPHA, 0, null); animate(binding.playerBottomShadow, show, duration, AnimationType.ALPHA, 0, null); @@ -3742,11 +3747,7 @@ public final class Player implements if (DEBUG) { Log.d(TAG, "onClick() called with: v = [" + v + "]"); } - if (v.getId() == binding.qualityTextView.getId()) { - onQualitySelectorClicked(); - } else if (v.getId() == binding.playbackSpeed.getId()) { - onPlaybackSpeedClicked(); - } else if (v.getId() == binding.resizeTextView.getId()) { + if (v.getId() == binding.resizeTextView.getId()) { onResizeClicked(); } else if (v.getId() == binding.captionTextView.getId()) { onCaptionClicked(); @@ -4446,6 +4447,10 @@ public final class Player implements return isSomePopupMenuVisible; } + public void setSomePopupMenuVisible(final boolean somePopupMenuVisible) { + isSomePopupMenuVisible = somePopupMenuVisible; + } + public ImageButton getPlayPauseButton() { return binding.playPauseButton; } @@ -4527,6 +4532,11 @@ public final class Player implements public PlayQueueAdapter getPlayQueueAdapter() { return playQueueAdapter; } + + public PlayerBinding getBinding() { + return binding; + } + //endregion diff --git a/app/src/main/java/org/schabi/newpipe/player/listeners/view/PlaybackSpeedListener.kt b/app/src/main/java/org/schabi/newpipe/player/listeners/view/PlaybackSpeedListener.kt new file mode 100644 index 000000000..a616e3c7e --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/listeners/view/PlaybackSpeedListener.kt @@ -0,0 +1,46 @@ +package org.schabi.newpipe.player.listeners.view + +import android.util.Log +import android.view.View +import android.widget.PopupMenu +import org.schabi.newpipe.MainActivity +import org.schabi.newpipe.player.Player +import org.schabi.newpipe.player.helper.PlaybackParameterDialog + +class PlaybackSpeedListener( + private val player: Player, + private val playbackSpeedPopupMenu: PopupMenu + +) : View.OnClickListener { + + companion object { + private val DEBUG = MainActivity.DEBUG + private val TAG: String = PlaybackSpeedListener::class.java.simpleName + } + + override fun onClick(v: View) { + if (player.binding.qualityTextView.id == v.id) { + if (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 + } + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/listeners/view/QualityTextListener.kt b/app/src/main/java/org/schabi/newpipe/player/listeners/view/QualityTextListener.kt new file mode 100644 index 000000000..58d4db9ea --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/listeners/view/QualityTextListener.kt @@ -0,0 +1,41 @@ +package org.schabi.newpipe.player.listeners.view + +import android.util.Log +import android.view.View +import android.widget.PopupMenu +import org.schabi.newpipe.MainActivity +import org.schabi.newpipe.extractor.MediaFormat +import org.schabi.newpipe.player.Player + +class QualityTextListener( + private val player: Player, + private val qualityPopupMenu: PopupMenu +) : View.OnClickListener { + + companion object { + private val DEBUG = MainActivity.DEBUG + private val TAG: String = QualityTextListener::class.java.simpleName + } + + override fun onClick(v: View) { + if (player.binding.qualityTextView.id == v.id) { + if (DEBUG) { + Log.d(TAG, "onQualitySelectorClicked() called") + } + + qualityPopupMenu.show() + player.isSomePopupMenuVisible = true + + val videoStream = player.selectedVideoStream + if (videoStream != null) { + val qualityText = ( + MediaFormat.getNameById(videoStream.formatId) + " " + + videoStream.resolution + ) + player.binding.qualityTextView.text = qualityText + } + + player.saveWasPlaying() + } + } +}