From 14a4bb34889ceb43de6701ecc5f4e2d98cae8a4f Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Tue, 7 May 2024 20:20:02 +0200 Subject: [PATCH] Replace PlaybackForwardingActivity with PlaybackRewriteFragment --- app/src/main/AndroidManifest.xml | 3 --- .../jellyfin/androidtv/di/PlaybackModule.kt | 2 +- .../androidtv/ui/playback/PlaybackLauncher.kt | 20 +++++++++------ ...Activity.kt => PlaybackRewriteFragment.kt} | 25 ++++++++++++------- 4 files changed, 29 insertions(+), 21 deletions(-) rename app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/{PlaybackForwardingActivity.kt => PlaybackRewriteFragment.kt} (80%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1b24cb6028..348afc5792 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -140,8 +140,5 @@ android:theme="@style/Theme.Jellyfin.Preferences" /> - - - diff --git a/app/src/main/java/org/jellyfin/androidtv/di/PlaybackModule.kt b/app/src/main/java/org/jellyfin/androidtv/di/PlaybackModule.kt index 53e12bf368..c00303e685 100644 --- a/app/src/main/java/org/jellyfin/androidtv/di/PlaybackModule.kt +++ b/app/src/main/java/org/jellyfin/androidtv/di/PlaybackModule.kt @@ -36,7 +36,7 @@ val playbackModule = module { val preferences = get() val useRewrite = preferences[UserPreferences.playbackRewriteVideoEnabled] && BuildConfig.DEVELOPMENT - if (useRewrite) RewritePlaybackLauncher() + if (useRewrite) RewritePlaybackLauncher(get()) else GarbagePlaybackLauncher(get()) } diff --git a/app/src/main/java/org/jellyfin/androidtv/ui/playback/PlaybackLauncher.kt b/app/src/main/java/org/jellyfin/androidtv/ui/playback/PlaybackLauncher.kt index 78c6c42dcc..89e4c93163 100644 --- a/app/src/main/java/org/jellyfin/androidtv/ui/playback/PlaybackLauncher.kt +++ b/app/src/main/java/org/jellyfin/androidtv/ui/playback/PlaybackLauncher.kt @@ -1,15 +1,16 @@ package org.jellyfin.androidtv.ui.playback import android.content.Context -import android.content.Intent import org.jellyfin.androidtv.preference.UserPreferences import org.jellyfin.androidtv.preference.constant.PreferredVideoPlayer import org.jellyfin.androidtv.ui.navigation.Destination import org.jellyfin.androidtv.ui.navigation.Destinations -import org.jellyfin.androidtv.ui.navigation.activityDestination -import org.jellyfin.androidtv.ui.playback.rewrite.PlaybackForwardingActivity +import org.jellyfin.androidtv.ui.navigation.NavigationRepository +import org.jellyfin.androidtv.ui.navigation.fragmentDestination +import org.jellyfin.androidtv.ui.playback.rewrite.PlaybackRewriteFragment import org.jellyfin.sdk.model.api.BaseItemDto import org.jellyfin.sdk.model.api.BaseItemKind +import org.jellyfin.sdk.model.constant.MediaType interface PlaybackLauncher { fun useExternalPlayer(itemType: BaseItemKind?): Boolean @@ -42,17 +43,20 @@ class GarbagePlaybackLauncher( override fun interceptPlayRequest(context: Context, item: BaseItemDto?): Boolean = false } -class RewritePlaybackLauncher : PlaybackLauncher { +class RewritePlaybackLauncher( + private val navigationRepository: NavigationRepository, +) : PlaybackLauncher { override fun useExternalPlayer(itemType: BaseItemKind?) = false override fun getPlaybackDestination(itemType: BaseItemKind?, position: Int) = - activityDestination() + fragmentDestination() override fun interceptPlayRequest(context: Context, item: BaseItemDto?): Boolean { if (item == null) return false + if (item.mediaType == MediaType.Audio) return false - val intent = Intent(context, PlaybackForwardingActivity::class.java) - intent.putExtra(PlaybackForwardingActivity.EXTRA_ITEM_ID, item.id.toString()) - context.startActivity(intent) + navigationRepository.navigate(fragmentDestination( + PlaybackRewriteFragment.EXTRA_ITEM_ID to item.id.toString() + )) return true } diff --git a/app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackForwardingActivity.kt b/app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackRewriteFragment.kt similarity index 80% rename from app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackForwardingActivity.kt rename to app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackRewriteFragment.kt index ec2cad712f..4b29248635 100644 --- a/app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackForwardingActivity.kt +++ b/app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackRewriteFragment.kt @@ -1,8 +1,11 @@ package org.jellyfin.androidtv.ui.playback.rewrite import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup import android.widget.Toast -import androidx.activity.ComponentActivity +import androidx.fragment.app.Fragment import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle @@ -22,7 +25,11 @@ import org.koin.android.ext.android.inject import timber.log.Timber import java.util.UUID -class PlaybackForwardingActivity : ComponentActivity() { +/** + * Temporary fragment used for testing the playback rewrite. This will eventually be replaced with a + * proper player user interface. + */ +class PlaybackRewriteFragment : Fragment() { companion object { const val EXTRA_ITEM_ID: String = "item_id" } @@ -39,11 +46,10 @@ class PlaybackForwardingActivity : ComponentActivity() { if (itemId == null) { Toast.makeText( - this, + requireContext(), "Could not find item to play (itemId=null)", Toast.LENGTH_LONG ).show() - finishAfterTransition() return } @@ -54,7 +60,7 @@ class PlaybackForwardingActivity : ComponentActivity() { // Log info Toast.makeText( - this@PlaybackForwardingActivity, + requireContext(), "Found item of type ${item.type} - ${item.name} (${item.id}", Toast.LENGTH_LONG ).show() @@ -72,15 +78,16 @@ class PlaybackForwardingActivity : ComponentActivity() { }) } } + } - // TODO: Dirty hack to display surface - val view = PlayerSurfaceView(this) + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + val view = PlayerSurfaceView(requireContext()) view.playbackManager = playbackManager - setContentView(view) + return view } private fun findItemId(): UUID? { - val extra = intent.getStringExtra(EXTRA_ITEM_ID)?.toUUIDOrNull() + val extra = requireArguments().getString(EXTRA_ITEM_ID)?.toUUIDOrNull() var first: BaseItemDto? = null var best: BaseItemDto? = null