Option for background and skip controls in PiP

This commit is contained in:
Bnyro 2023-01-09 17:38:17 +01:00
parent 07dc1a80b8
commit 3dd58d47fa
6 changed files with 78 additions and 22 deletions

View File

@ -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

View File

@ -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 }

View File

@ -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)

View File

@ -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<RemoteAction> {
fun getPiPModeActions(activity: Activity, isPlaying: Boolean, isOfflinePlayer: Boolean = false): ArrayList<RemoteAction> {
val actions: ArrayList<RemoteAction> = 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
}

View File

@ -428,6 +428,8 @@
<string name="rewind">Rewind</string>
<string name="forward">Forward</string>
<string name="pause">Pause</string>
<string name="alternative_pip_controls">Alternative PiP controls</string>
<string name="alternative_pip_controls_summary">Show audio only and skip controls in PiP instead of forward and rewind</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>

View File

@ -38,6 +38,13 @@
app:key="picture_in_picture"
app:title="@string/picture_in_picture" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_headphones"
app:key="alternative_pip_controls"
app:title="@string/alternative_pip_controls"
android:summary="@string/alternative_pip_controls_summary"/>
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_pause_filled"