From 016c3f2d226ce6b9b8223ebdc26e15aa6743f84e Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Tue, 17 Aug 2021 18:54:42 +0200 Subject: [PATCH] Add PlaybackForwardingActivity for bridging to new playback module --- app/src/main/AndroidManifest.xml | 3 + .../androidtv/ui/playback/PlaybackLauncher.kt | 4 +- .../rewrite/PlaybackForwardingActivity.kt | 69 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackForwardingActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 96263d6952..249e377a76 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -167,5 +167,8 @@ android:name=".ui.playback.nextup.NextUpActivity" android:noHistory="true" android:screenOrientation="landscape" /> + + + 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 43ead5e2c6..a42abf206a 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 @@ -3,6 +3,7 @@ package org.jellyfin.androidtv.ui.playback import android.app.Activity import org.jellyfin.androidtv.preference.UserPreferences import org.jellyfin.androidtv.preference.constant.PreferredVideoPlayer +import org.jellyfin.androidtv.ui.playback.rewrite.PlaybackForwardingActivity import org.jellyfin.apiclient.model.dto.BaseItemType interface PlaybackLauncher { @@ -32,8 +33,7 @@ class GarbagePlaybackLauncher( } } -// TODO: Move to playback module class RewritePlaybackLauncher : PlaybackLauncher { override fun useExternalPlayer(itemType: BaseItemType?) = false - override fun getPlaybackActivityClass(itemType: BaseItemType?) = TODO("Not yet implemented") + override fun getPlaybackActivityClass(itemType: BaseItemType?) = PlaybackForwardingActivity::class.java } 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/PlaybackForwardingActivity.kt new file mode 100644 index 0000000000..6ac6caac49 --- /dev/null +++ b/app/src/main/java/org/jellyfin/androidtv/ui/playback/rewrite/PlaybackForwardingActivity.kt @@ -0,0 +1,69 @@ +package org.jellyfin.androidtv.ui.playback.rewrite + +import android.os.Bundle +import android.widget.Toast +import androidx.fragment.app.FragmentActivity +import androidx.lifecycle.lifecycleScope +import org.jellyfin.androidtv.di.userApiClient +import org.jellyfin.androidtv.ui.playback.MediaManager +import org.jellyfin.apiclient.model.dto.BaseItemDto +import org.jellyfin.apiclient.model.dto.BaseItemType +import org.jellyfin.sdk.api.operations.UserLibraryApi +import org.jellyfin.sdk.model.serializer.toUUIDOrNull +import org.koin.android.ext.android.get +import org.koin.android.ext.android.inject +import timber.log.Timber + +class PlaybackForwardingActivity : FragmentActivity() { + private val mediaManager by inject() + private val userLibraryApi by lazy { + UserLibraryApi(get(userApiClient)) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // Try find the item id + val itemId = findItem()?.id?.toUUIDOrNull() + + if (itemId == null) { + Toast.makeText( + this, + "Could not find item to play (itemId=null)", + Toast.LENGTH_LONG + ).show() + finishAfterTransition() + return + } + + lifecycleScope.launchWhenCreated { + // Get fresh info from API in SDK format + val item by userLibraryApi.getItem(itemId = itemId) + + // Log info + Toast.makeText( + this@PlaybackForwardingActivity, + "Found item of type ${item.type} - ${item.name} (${item.id}", + Toast.LENGTH_LONG + ).show() + Timber.i(item.toString()) + + // TODO: Create queue, send to new playback manager, start new player UI + finishAfterTransition() + } + } + + private fun findItem(): BaseItemDto? { + var first: BaseItemDto? = null + var best: BaseItemDto? = null + + for (item in mediaManager.currentVideoQueue) { + if (first == null) first = item + if (best == null && item.baseItemType !== BaseItemType.Trailer) best = item + + if (first != null && best != null) break + } + + return best ?: first + } +}