From 3dd58d47fad84c5fe7ed1b6461860cb932ffda8c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 9 Jan 2023 17:38:17 +0100 Subject: [PATCH] Option for background and skip controls in PiP --- .../libretube/constants/PreferenceKeys.kt | 1 + .../com/github/libretube/enums/PlayerEvent.kt | 3 +- .../libretube/ui/fragments/PlayerFragment.kt | 34 +++++++++--- .../com/github/libretube/util/PlayerHelper.kt | 53 ++++++++++++++----- app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/player_settings.xml | 7 +++ 6 files changed, 78 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index c5d40686a..ef5b472fc 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -87,6 +87,7 @@ object PreferenceKeys { const val CAPTIONS_SIZE = "captions_size" const val DOUBLE_TAP_TO_SEEK = "double_tap_seek" const val PAUSE_ON_QUIT = "pause_on_quit" + const val ALTERNATIVE_PIP_CONTROLS = "alternative_pip_controls" /** * Background mode diff --git a/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt b/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt index 9479e309c..9a9e680e8 100644 --- a/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt +++ b/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt @@ -6,7 +6,8 @@ enum class PlayerEvent(val value: Int) { Forward(2), Rewind(3), Next(5), - Prev(6); + Prev(6), + Background(7); companion object { fun fromInt(value: Int) = PlayerEvent.values().first { it.value == value } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index c11f5ba59..a79cac126 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -28,6 +28,7 @@ import androidx.annotation.RequiresApi import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.net.toUri import androidx.core.os.bundleOf +import androidx.core.os.postDelayed import androidx.core.view.isVisible import androidx.fragment.app.activityViewModels import androidx.lifecycle.Lifecycle @@ -181,6 +182,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { val handler = Handler(Looper.getMainLooper()) + /** + * Receiver for all actions in the PiP mode + */ private val broadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val action = intent?.getIntExtra(PlayerHelper.CONTROL_TYPE, 0) ?: return @@ -197,6 +201,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { PlayerEvent.Rewind -> { exoPlayer.seekTo(exoPlayer.currentPosition - PlayerHelper.seekIncrement) } + PlayerEvent.Next -> { + playNextVideo() + } + PlayerEvent.Background -> { + playOnBackground() + // wait some time in order for the service to get started properly + handler.postDelayed({ + activity?.finish() + }, 500) + } else -> { } } @@ -447,13 +461,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { exoPlayer.pause() // start the background mode - BackgroundHelper.playOnBackground( - requireContext(), - videoId!!, - exoPlayer.currentPosition, - playlistId, - channelId - ) + playOnBackground() } binding.relatedRecView.layoutManager = VideosAdapter.getLayout(requireContext()) @@ -465,6 +473,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { ) } + private fun playOnBackground() { + BackgroundHelper.playOnBackground( + requireContext(), + videoId!!, + exoPlayer.currentPosition, + playlistId, + channelId + ) + } + private fun setFullscreen() { with(binding.playerMotionLayout) { getConstraintSet(R.id.start).constrainHeight(R.id.player, -1) @@ -1497,7 +1515,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { @RequiresApi(Build.VERSION_CODES.O) fun getPipParams(): PictureInPictureParams = PictureInPictureParams.Builder() - .setActions(PlayerHelper.getPIPModeActions(requireActivity(), exoPlayer.isPlaying)) + .setActions(PlayerHelper.getPiPModeActions(requireActivity(), exoPlayer.isPlaying)) .apply { if (SDK_INT >= Build.VERSION_CODES.S) { setAutoEnterEnabled(true) diff --git a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt index 202720a9f..3687d7aed 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -332,6 +332,12 @@ object PlayerHelper { false ) + val alternativePiPControls: Boolean + get() = PreferenceHelper.getBoolean( + PreferenceKeys.ALTERNATIVE_PIP_CONTROLS, + false + ) + fun getDefaultResolution(context: Context): String { return if (NetworkHelper.isNetworkMobile(context)) { PreferenceHelper.getString( @@ -384,16 +390,28 @@ object PlayerHelper { ) } + /** + * Create controls to use in the PiP window + */ @RequiresApi(Build.VERSION_CODES.O) - fun getPIPModeActions(activity: Activity, isPlaying: Boolean): ArrayList { + fun getPiPModeActions(activity: Activity, isPlaying: Boolean, isOfflinePlayer: Boolean = false): ArrayList { val actions: ArrayList = ArrayList() actions.add( - getRemoteAction( - activity, - R.drawable.ic_rewind, - R.string.rewind, - PlayerEvent.Rewind - ) + if (!isOfflinePlayer && alternativePiPControls) { + getRemoteAction( + activity, + R.drawable.ic_headphones, + R.string.background_mode, + PlayerEvent.Background + ) + } else { + getRemoteAction( + activity, + R.drawable.ic_rewind, + R.string.rewind, + PlayerEvent.Rewind + ) + } ) actions.add( @@ -406,12 +424,21 @@ object PlayerHelper { ) actions.add( - getRemoteAction( - activity, - R.drawable.ic_forward, - R.string.forward, - PlayerEvent.Forward - ) + if (!isOfflinePlayer && alternativePiPControls) { + getRemoteAction( + activity, + R.drawable.ic_next, + R.string.play_next, + PlayerEvent.Next + ) + } else { + getRemoteAction( + activity, + R.drawable.ic_forward, + R.string.forward, + PlayerEvent.Forward + ) + } ) return actions } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b290994e4..58a016faf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -428,6 +428,8 @@ Rewind Forward Pause + Alternative PiP controls + Show audio only and skip controls in PiP instead of forward and rewind Download Service diff --git a/app/src/main/res/xml/player_settings.xml b/app/src/main/res/xml/player_settings.xml index af04d498a..1b7ac7bbb 100644 --- a/app/src/main/res/xml/player_settings.xml +++ b/app/src/main/res/xml/player_settings.xml @@ -38,6 +38,13 @@ app:key="picture_in_picture" app:title="@string/picture_in_picture" /> + +