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 External Torrent Streaming #1093

Closed
wants to merge 2 commits into from
Closed
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
Expand Up @@ -24,6 +24,7 @@ import eu.kanade.domain.base.BasePreferences
import eu.kanade.presentation.more.settings.Preference
import eu.kanade.presentation.util.collectAsState
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.player.AMNIS
import eu.kanade.tachiyomi.ui.player.JUST_PLAYER
import eu.kanade.tachiyomi.ui.player.MPV_PLAYER
import eu.kanade.tachiyomi.ui.player.MPV_REMOTE
Expand Down Expand Up @@ -395,4 +396,5 @@ val externalPlayers = listOf(
JUST_PLAYER,
NEXT_PLAYER,
X_PLAYER,
AMNIS,
)
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ class AnimeScreen(

private suspend fun openEpisode(context: Context, episode: Episode, useExternalPlayer: Boolean) {
withIOContext {
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, useExternalPlayer)
if (episode.url.startsWith("magnet:")) {
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, episode.url, true)
} else {
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, episode.url, useExternalPlayer)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun Screen.animeHistoryTab(
val playerPreferences: PlayerPreferences by injectLazy()
val extPlayer = playerPreferences.alwaysUseExternalPlayer().get()
if (episode != null) {
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, extPlayer)
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, episode.url, extPlayer)
} else {
snackbarHostState.showSnackbar(context.getString(R.string.no_next_episode))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object AnimeLibraryTab : Tab {
suspend fun openEpisode(episode: Episode) {
val playerPreferences: PlayerPreferences by injectLazy()
val extPlayer = playerPreferences.alwaysUseExternalPlayer().get()
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, extPlayer)
MainActivity.startPlayerActivity(context, episode.animeId, episode.id, episode.url, extPlayer)
}

val defaultTitle = if (fromMore) stringResource(R.string.label_library) else stringResource(R.string.label_anime_library)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ class MainActivity : BaseActivity() {

var externalPlayerResult: ActivityResultLauncher<Intent>? = null

suspend fun startPlayerActivity(context: Context, animeId: Long, episodeId: Long, extPlayer: Boolean) {
if (extPlayer) {
suspend fun startPlayerActivity(context: Context, animeId: Long, episodeId: Long, episodeUrl: String?, extPlayer: Boolean) {
if (extPlayer || (episodeUrl?.startsWith("magnet:") == true)) {
externalPlayerResult?.launch(ExternalIntents.newIntent(context, animeId, episodeId)) ?: return
} else {
context.startActivity(PlayerActivity.newIntent(context, animeId, episodeId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import eu.kanade.domain.track.anime.model.toDbTrack
import eu.kanade.domain.track.anime.service.DelayedAnimeTrackingUpdateJob
import eu.kanade.domain.track.anime.store.DelayedAnimeTrackingStore
import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.animesource.AnimeSource
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.animesource.online.AnimeHttpSource
Expand Down Expand Up @@ -81,7 +82,9 @@ class ExternalIntents {

val pkgName = playerPreferences.externalPlayerPreference().get()

return if (pkgName.isEmpty()) {
return if (videoUrl.toString().startsWith("magnet:")) {
torrentIntentForPackage(context, videoUrl, video)
} else if (pkgName.isEmpty()) {
Intent(Intent.ACTION_VIEW).apply {
setDataAndTypeAndNormalize(videoUrl, getMime(videoUrl))
addExtrasAndFlags(false, this)
Expand Down Expand Up @@ -199,6 +202,30 @@ class ExternalIntents {
}
}

/**
* Returns the [Intent] with added data to send to the given torrent external player.
*
* @param context the application context.
* @param uri the path data of the video.
* @param video the video being sent to the external player.
*/
private suspend fun torrentIntentForPackage(context: Context, uri: Uri, video: Video): Intent {
return Intent(Intent.ACTION_VIEW).apply {
if (isPackageInstalled(AMNIS, context.packageManager)) {
if (uri.toString().startsWith("magnet:")) {
component = getComponent(AMNIS)
}
} else {
withUIContext {
context.toast(R.string.install_amnis, 8)
}
}
data = uri
addExtrasAndFlags(true, this)
addVideoHeaders(true, video, this)
}
}

/**
* Adds extras and flags to the given [Intent].
*
Expand Down Expand Up @@ -266,6 +293,7 @@ class ExternalIntents {
JUST_PLAYER -> ComponentName(packageName, "$packageName.PlayerActivity")
NEXT_PLAYER -> ComponentName(packageName, "$packageName.feature.player.PlayerActivity")
X_PLAYER -> ComponentName(packageName, "com.inshot.xplayer.activities.PlayerActivity")
AMNIS -> ComponentName(packageName, "$packageName.gui.player.PlayerActivity")
else -> null
}
}
Expand Down Expand Up @@ -507,3 +535,4 @@ const val MPV_REMOTE = "com.husudosu.mpvremote"
const val JUST_PLAYER = "com.brouken.player"
const val NEXT_PLAYER = "dev.anilbeesetti.nextplayer"
const val X_PLAYER = "video.player.videoplayer"
const val AMNIS = "com.amnis"
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.player.settings.PlayerPreferences
import kotlinx.coroutines.flow.collectLatest
import tachiyomi.core.util.lang.launchIO
import tachiyomi.domain.items.episode.interactor.GetEpisodeByAnimeId
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy

@Composable
Expand All @@ -44,11 +47,14 @@ fun Screen.animeUpdatesTab(

val navigateUp: (() -> Unit)? = if (fromMore) navigator::pop else null

val getEpisodeByAnimeId: GetEpisodeByAnimeId = Injekt.get()

suspend fun openEpisode(updateItem: AnimeUpdatesItem, altPlayer: Boolean = false) {
val playerPreferences: PlayerPreferences by injectLazy()
val update = updateItem.update
val extPlayer = playerPreferences.alwaysUseExternalPlayer().get() != altPlayer
MainActivity.startPlayerActivity(context, update.animeId, update.episodeId, extPlayer)
val episode = getEpisodeByAnimeId.await(update.animeId).find { it.id == update.episodeId }
MainActivity.startPlayerActivity(context, update.animeId, update.episodeId, episode?.url, extPlayer)
}

return TabContent(
Expand Down
3 changes: 3 additions & 0 deletions i18n/src/main/res/values/strings-aniyomi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,7 @@
<string name="resmush">resmush.it</string>
<string name="bandwidth_data_saver_server">Bandwidth Hero Proxy Server</string>
<string name="data_saver_server_summary">Put Bandwidth Hero Proxy server url here</string>

<!--Torrent -->
<string name="install_amnis">Amnis Player needs to be installed</string>
</resources>