Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "ask to skip" media segment action #4068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.jellyfin.androidtv.ui.composable

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

/**
* Apply a [padding] with the default overscan values of 48 horizontal and 27 vertical display pixels.
* Default overscan values of 48 horizontal and 27 vertical display pixels.
*/
fun Modifier.overscan(): Modifier = then(padding(48.dp, 27.dp))
val overscanPaddingValues = PaddingValues(48.dp, 27.dp)

/**
* Apply a [padding] with [overscanPaddingValues].
*/
fun Modifier.overscan(): Modifier = padding(overscanPaddingValues)
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
import timber.log.Timber;

public class CustomPlaybackOverlayFragment extends Fragment implements LiveTvGuide, View.OnKeyListener {
private VlcPlayerInterfaceBinding binding;
protected VlcPlayerInterfaceBinding binding;

Check notice

Code scanning / Android Lint

Unknown nullness Note

Unknown nullability; explicitly declare as @Nullable or @NonNull to improve Kotlin interoperability; see https://developer.android.com/kotlin/interop#nullability_annotations
private OverlayTvGuideBinding tvGuideBinding;

private RowsSupportFragment mPopupRowsFragment;
Expand Down Expand Up @@ -475,6 +475,22 @@
leanbackOverlayFragment.hideOverlay();
}

if (binding.skipOverlay.getVisible()) {
// Hide without doing anything
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_BUTTON_B || keyCode == KeyEvent.KEYCODE_ESCAPE) {
binding.skipOverlay.setTargetPositionMs(null);
return true;
}

// Hide with seek
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
playbackControllerContainer.getValue().getPlaybackController().seek(binding.skipOverlay.getTargetPositionMs());
leanbackOverlayFragment.setShouldShowOverlay(false);
binding.skipOverlay.setTargetPositionMs(null);
return true;
}
}

if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP) {
closePlayer();
return true;
Expand Down Expand Up @@ -693,22 +709,26 @@
public void show() {
binding.topPanel.startAnimation(slideDown);
mIsVisible = true;
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

public void hide() {
mIsVisible = false;
binding.topPanel.startAnimation(fadeOut);
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

private void showChapterPanel() {
setFadingEnabled(false);
binding.popupArea.startAnimation(showPopup);
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

private void hidePopupPanel() {
startFadeTimer();
binding.popupArea.startAnimation(hidePopup);
mPopupPanelVisible = false;
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

public void showGuide() {
Expand All @@ -729,12 +749,14 @@
if (needLoad) {
loadGuide();
}
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

private void hideGuide() {
tvGuideBinding.getRoot().setVisibility(View.GONE);
playbackControllerContainer.getValue().getPlaybackController().mVideoManager.setVideoFullSize(true);
mGuideVisible = false;
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

private void loadGuide() {
Expand Down Expand Up @@ -784,6 +806,7 @@
mDisplayProgramsTask.execute(mCurrentDisplayChannelStartNdx, mCurrentDisplayChannelEndNdx);
}
});
binding.skipOverlay.setSkipUiEnabled(!mIsVisible && !mGuideVisible && !mPopupPanelVisible);
}

DisplayProgramsTask mDisplayProgramsTask;
Expand Down Expand Up @@ -1181,6 +1204,7 @@
}

public void setCurrentTime(long time) {
binding.skipOverlay.setCurrentPositionMs(time);
if (leanbackOverlayFragment != null)
leanbackOverlayFragment.updateCurrentPosition();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.koin.android.ext.android.inject
import timber.log.Timber
import java.time.Instant
import java.util.UUID
import kotlin.time.Duration

fun CustomPlaybackOverlayFragment.toggleFavorite() {
val header = mSelectedProgramView as? GuideChannelHeader
Expand Down Expand Up @@ -174,3 +175,7 @@ fun CustomPlaybackOverlayFragment.recordProgram(program: BaseItemDto, isSeries:
)
}
}

fun CustomPlaybackOverlayFragment.askToSkip(position: Duration) {
binding.skipOverlay.targetPosition = position
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fun PlaybackController.applyMediaSegments(

when (action) {
MediaSegmentAction.SKIP -> addSkipAction(mediaSegment)
MediaSegmentAction.ASK_TO_SKIP -> addAskToSkipAction(mediaSegment)
MediaSegmentAction.NOTHING -> Unit
}
}
Expand All @@ -58,7 +59,7 @@ fun PlaybackController.applyMediaSegments(
@OptIn(UnstableApi::class)
private fun PlaybackController.addSkipAction(mediaSegment: MediaSegmentDto) {
mVideoManager.mExoPlayer
.createMessage { messageType: Int, payload: Any? ->
.createMessage { _, _ ->
// We can't seek directly on the ExoPlayer instance as not all media is seekable
// the seek function in the PlaybackController checks this and optionally starts a transcode
// at the requested position
Expand All @@ -68,7 +69,18 @@ private fun PlaybackController.addSkipAction(mediaSegment: MediaSegmentDto) {
}
// Segments at position 0 will never be hit by ExoPlayer so we need to add a minimum value
.setPosition(mediaSegment.start.inWholeMilliseconds.coerceAtLeast(1))
.setPayload(mediaSegment)
.setDeleteAfterDelivery(false)
.send()
}

@OptIn(UnstableApi::class)
private fun PlaybackController.addAskToSkipAction(mediaSegment: MediaSegmentDto) {
mVideoManager.mExoPlayer
.createMessage { _, _ ->
fragment?.askToSkip(mediaSegment.end)
}
// Segments at position 0 will never be hit by ExoPlayer so we need to add a minimum value
.setPosition(mediaSegment.start.inWholeMilliseconds.coerceAtLeast(1))
.setDeleteAfterDelivery(false)
.send()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.jellyfin.androidtv.ui.playback.overlay

import android.content.Context
import android.util.AttributeSet
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.tv.material3.Text
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.ui.playback.segment.MediaSegmentRepository
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

@Composable
fun SkipOverlayComposable(
visible: Boolean,
) {
Box(
contentAlignment = Alignment.BottomEnd,
modifier = Modifier
.padding(48.dp, 48.dp)
) {
AnimatedVisibility(visible, enter = fadeIn(), exit = fadeOut()) {
Row(
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
.background(colorResource(R.color.popup_menu_background).copy(alpha = 0.6f))
.padding(10.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Image(
painter = painterResource(R.drawable.ic_control_select),
contentDescription = null,
)

Text(
text = stringResource(R.string.segment_action_skip),
color = colorResource(R.color.button_default_normal_text),
fontSize = 18.sp,
)
}
}
}
}

class SkipOverlayView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : AbstractComposeView(context, attrs, defStyle) {
private val _currentPosition = MutableStateFlow(Duration.ZERO)
private val _targetPosition = MutableStateFlow<Duration?>(null)
private val _skipUiEnabled = MutableStateFlow(true)

var currentPosition: Duration
get() = _currentPosition.value
set(value) {
_currentPosition.value = value
}

var currentPositionMs: Long
get() = _currentPosition.value.inWholeMilliseconds
set(value) {
_currentPosition.value = value.milliseconds
}

var targetPosition: Duration?
get() = _targetPosition.value
set(value) {
_targetPosition.value = value
}

var targetPositionMs: Long?
get() = _targetPosition.value?.inWholeMilliseconds
set(value) {
_targetPosition.value = value?.milliseconds
}

var skipUiEnabled: Boolean
get() = _skipUiEnabled.value
set(value) {
_skipUiEnabled.value = value
}

val visible: Boolean
get() {
val enabled = _skipUiEnabled.value
val targetPosition = _targetPosition.value
val currentPosition = _currentPosition.value

return enabled && targetPosition != null && currentPosition <= (targetPosition - MediaSegmentRepository.SkipMinDuration)
}

@Composable
override fun Content() {
val skipUiEnabled by _skipUiEnabled.collectAsState()
val currentPosition by _currentPosition.collectAsState()
val targetPosition by _targetPosition.collectAsState()

val visible by remember(skipUiEnabled, currentPosition, targetPosition) {
derivedStateOf { visible }
}

// Auto hide
LaunchedEffect(skipUiEnabled, targetPosition) {
delay(MediaSegmentRepository.AskToSkipAutoHideDuration)
_targetPosition.value = null
}

SkipOverlayComposable(visible)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ enum class MediaSegmentAction(
* lagg. The skip action will only execute when playing over the segment start, not when seeking into the segment block.
*/
SKIP(R.string.segment_action_skip),

/**
* Ask the user if they want to skip this segment. When the user agrees this behaves like [SKIP]. Confirmation should only be asked for
* segments with a duration of at least 3 seconds to avoid UI flickering.
*/
ASK_TO_SKIP(R.string.segment_action_ask_to_skip),
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
* The minimum duration for a media segment to allow the [MediaSegmentAction.SKIP] action.
*/
val SkipMinDuration = 1.seconds

/**
* The minimum duration for a media segment to allow the [MediaSegmentAction.ASK_TO_SKIP] action.
*/
val AskToSkipMinDuration = 3.seconds

/**
* The duration to wait before automatically hiding the "ask to skip" UI.
*/
val AskToSkipAutoHideDuration = 8.seconds
}

fun getDefaultSegmentTypeAction(type: MediaSegmentType): MediaSegmentAction
Expand Down Expand Up @@ -84,6 +94,8 @@
val action = getDefaultSegmentTypeAction(segment.type)
// Skip the skip action if timespan is too short
if (action == MediaSegmentAction.SKIP && segment.duration < MediaSegmentRepository.SkipMinDuration) return MediaSegmentAction.NOTHING
// Skip the ask to skip action if timespan is too short
if (action == MediaSegmentAction.ASK_TO_SKIP && segment.duration < MediaSegmentRepository.AskToSkipMinDuration) return MediaSegmentAction.NOTHING

Check warning

Code scanning / detekt

Line detected, which is longer than the defined maximum line length in the code style. Warning

Line detected, which is longer than the defined maximum line length in the code style.
return action
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/vlc_player_interface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<org.jellyfin.androidtv.ui.playback.overlay.SkipOverlayView
android:id="@+id/skip_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@
<string name="prefer_exoplayer_ffmpeg_content">Use FFmpeg to decode audio, even if platform codecs are available.</string>
<string name="video_start_delay">Video start delay</string>
<string name="pref_mediasegment_actions">Media segment actions</string>
<string name="segment_action_ask_to_skip">Ask to skip</string>
<string name="segment_action_nothing">Do nothing</string>
<string name="segment_action_skip">Skip</string>
<string name="segment_type_commercial">Commercials</string>
Expand Down
Loading