fix player being closed when out of view

This commit is contained in:
jmir1 2021-05-29 23:47:36 +02:00
parent 12a4bfaf8a
commit 00cb52ec7c
5 changed files with 56 additions and 96 deletions

View file

@ -9,7 +9,6 @@ import android.net.Uri
import android.os.Build
import android.os.Handler
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.animesource.AnimeSourceManager
import eu.kanade.tachiyomi.data.animelib.AnimelibUpdateService
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.database.DatabaseHelper
@ -25,11 +24,9 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.anime.AnimeController
import eu.kanade.tachiyomi.ui.anime.episode.EpisodeItem
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaController
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.ui.watcher.EpisodeLoader
import eu.kanade.tachiyomi.ui.watcher.WatcherActivity
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.storage.DiskUtil
@ -40,7 +37,6 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.util.Collections.emptyList
import eu.kanade.tachiyomi.BuildConfig.APPLICATION_ID as ID
/**
@ -490,10 +486,8 @@ class NotificationReceiver : BroadcastReceiver() {
* @param episode episode that needs to be opened
*/
internal fun openEpisodePendingActivity(context: Context, anime: Anime, episode: Episode): PendingIntent {
val source = Injekt.get<AnimeSourceManager>().getOrStub(anime.source)
val link = EpisodeLoader.getUri(episode, anime, source)
val episodeList: List<EpisodeItem> = emptyList()
val newIntent = WatcherActivity.newIntent(context, anime, episode, episodeList, link)
val episodeList = ArrayList<Episode>()
val newIntent = WatcherActivity.newIntent(context, anime, episode, episodeList)
return PendingIntent.getActivity(context, AnimeController.REQUEST_INTERNAL, newIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}

View file

@ -89,6 +89,7 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.min
class AnimeController :
@ -701,22 +702,6 @@ class AnimeController :
val activity = activity ?: return
presenter.editCover(anime!!, activity, dataUri)
}
if (requestCode == REQUEST_INTERNAL) {
val episode: Episode = data!!.getSerializableExtra("episode") as Episode
// if next or previous episode was pressed
if (data.getBooleanExtra("nextResult", false)) {
val episodeList = presenter.filteredAndSortedEpisodes
val idx = episodeList.indexOfFirst { it.episode_number == episode.episode_number }
val nextEpisode = episodeList[idx - 1].episode
openEpisode(nextEpisode)
}
if (data.getBooleanExtra("previousResult", false)) {
val episodeList = presenter.filteredAndSortedEpisodes
val idx = episodeList.indexOfLast { it.episode_number == episode.episode_number }
val previousEpisode = episodeList[idx + 1].episode
openEpisode(previousEpisode)
}
}
if (requestCode == REQUEST_EXTERNAL && resultCode == Activity.RESULT_OK) {
val currentPosition: Long
val duration: Long
@ -903,12 +888,15 @@ class AnimeController :
fun openEpisode(episode: Episode) {
val activity = activity ?: return
launchIO {
val url = EpisodeLoader.getUri(episode, anime!!, source!!)
val episodeList = presenter.filteredAndSortedEpisodes
val intent = WatcherActivity.newIntent(activity, presenter.anime, episode, episodeList, url)
val episodeList = ArrayList<Episode>()
for (episodeItem in presenter.filteredAndSortedEpisodes.reversed()) {
episodeList.add(episodeItem.episode)
}
val intent = WatcherActivity.newIntent(activity, presenter.anime, episode, episodeList)
if (!preferences.alwaysUseExternalPlayer()) {
startActivityForResult(intent, REQUEST_INTERNAL)
startActivity(intent)
} else {
val url = EpisodeLoader.getUri(episode, anime!!, source!!)
currentExtEpisode = episode
val extIntent = Intent(Intent.ACTION_VIEW)
extIntent.setDataAndTypeAndNormalize(Uri.parse(url), "video/*")

View file

@ -13,30 +13,26 @@ import com.afollestad.materialdialogs.MaterialDialog
import dev.chrisbanes.insetter.applyInsetter
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.animesource.AnimeSourceManager
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.database.AnimeDatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Anime
import eu.kanade.tachiyomi.data.database.models.AnimeHistory
import eu.kanade.tachiyomi.data.database.models.Episode
import eu.kanade.tachiyomi.databinding.AnimeHistoryControllerBinding
import eu.kanade.tachiyomi.ui.anime.AnimeController
import eu.kanade.tachiyomi.ui.anime.episode.EpisodeItem
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.base.controller.RootController
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.ui.browse.source.browse.ProgressItem
import eu.kanade.tachiyomi.ui.watcher.EpisodeLoader
import eu.kanade.tachiyomi.ui.watcher.WatcherActivity
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.appcompat.queryTextChanges
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.util.Collections
/**
* Fragment that shows recently read anime.
@ -160,10 +156,8 @@ class AnimeHistoryController :
val nextEpisode = presenter.getNextEpisode(chapter, anime)
if (nextEpisode != null) {
val source = Injekt.get<AnimeSourceManager>().getOrStub(anime.source)
val link = EpisodeLoader.getUri(nextEpisode, anime, source)
val episodeList: List<EpisodeItem> = Collections.emptyList()
val newIntent = WatcherActivity.newIntent(activity, anime, nextEpisode, episodeList, link)
val episodeList = ArrayList<Episode>()
val newIntent = WatcherActivity.newIntent(activity, anime, nextEpisode, episodeList)
startActivity(newIntent)
} else {
activity.toast(R.string.no_next_chapter)

View file

@ -13,20 +13,18 @@ import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.SelectableAdapter
import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.animesource.AnimeSourceManager
import eu.kanade.tachiyomi.data.animelib.AnimelibUpdateService
import eu.kanade.tachiyomi.data.database.models.Episode
import eu.kanade.tachiyomi.data.download.AnimeDownloadService
import eu.kanade.tachiyomi.data.download.model.AnimeDownload
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.databinding.AnimeUpdatesControllerBinding
import eu.kanade.tachiyomi.ui.anime.AnimeController
import eu.kanade.tachiyomi.ui.anime.episode.EpisodeItem
import eu.kanade.tachiyomi.ui.anime.episode.base.BaseEpisodesAdapter
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.base.controller.RootController
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.watcher.EpisodeLoader
import eu.kanade.tachiyomi.ui.watcher.WatcherActivity
import eu.kanade.tachiyomi.util.system.notificationManager
import eu.kanade.tachiyomi.util.system.toast
@ -35,7 +33,6 @@ import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.recyclerview.scrollStateChanges
import reactivecircus.flowbinding.swiperefreshlayout.refreshes
import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.*
@ -210,10 +207,8 @@ class AnimeUpdatesController :
*/
private fun openEpisode(item: AnimeUpdatesItem) {
val activity = activity ?: return
val source = Injekt.get<AnimeSourceManager>().getOrStub(item.anime.source)
val link = EpisodeLoader.getUri(item.episode, item.anime, source)
val episodeList: List<EpisodeItem> = Collections.emptyList()
val intent = WatcherActivity.newIntent(activity, item.anime, item.episode, episodeList, link)
val episodeList = ArrayList<Episode>()
val intent = WatcherActivity.newIntent(activity, item.anime, item.episode, episodeList)
startActivity(intent)
}

View file

@ -60,6 +60,12 @@ class WatcherActivity : AppCompatActivity() {
private lateinit var nextBtn: ImageButton
private lateinit var prevBtn: ImageButton
private lateinit var episode: Episode
private lateinit var anime: Anime
private lateinit var source: AnimeSource
private lateinit var uri: String
private lateinit var episodeList: ArrayList<Episode>
private var duration: Long = 0
private var currentWindow = 0
private var playbackPosition = 0L
@ -95,11 +101,16 @@ class WatcherActivity : AppCompatActivity() {
prevBtn = findViewById(R.id.watcher_controls_prev)
dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "xyz.jmir.tachiyomi.mi"))
anime = intent.getSerializableExtra("anime") as Anime
episode = intent.getSerializableExtra("episode") as Episode
source = Injekt.get<AnimeSourceManager>().getOrStub(anime.source)
episodeList = intent.getSerializableExtra("episodeList") as ArrayList<Episode>
uri = EpisodeLoader.getUri(episode, anime, source)
mediaItem = MediaItem.Builder()
.setUri(intent.getStringExtra("uri"))
.setUri(uri)
.setMimeType(MimeTypes.VIDEO_MP4)
.build()
playbackPosition = intent.extras!!.getLong("second")
playbackPosition = episode.last_second_seen
if (savedInstanceState != null) {
currentWindow = savedInstanceState.getInt(STATE_RESUME_WINDOW)
@ -131,12 +142,12 @@ class WatcherActivity : AppCompatActivity() {
}
exoPlayer.addListener(PlayerEventListener())
skipBtn.setOnClickListener { exoPlayer.seekTo(exoPlayer.currentPosition + 85000) }
if (intent.getBooleanExtra("hasNextEpisode", false)) {
if (episodeList.indexOf(episode) != episodeList.lastIndex && episodeList.isNotEmpty()) {
nextBtn.setOnClickListener {
nextEpisode()
}
}
if (intent.getBooleanExtra("hasPreviousEpisode", false)) {
if (episodeList.indexOf(episode) != 0 && episodeList.isNotEmpty()) {
prevBtn.setOnClickListener {
previousEpisode()
}
@ -156,49 +167,37 @@ class WatcherActivity : AppCompatActivity() {
isPlayerPlaying = exoPlayer.playWhenReady
playbackPosition = exoPlayer.currentPosition
currentWindow = exoPlayer.currentWindowIndex
val episode = intent.getSerializableExtra("episode") as Episode
val anime = intent.getSerializableExtra("anime_anime") as Anime
saveEpisodeHistory(EpisodeItem(episode, anime))
setEpisodeProgress(episode, anime, exoPlayer.currentPosition, exoPlayer.duration)
val returnIntent = intent
returnIntent.putExtra("seconds_result", playbackPosition)
returnIntent.putExtra("total_seconds_result", exoPlayer.duration)
returnIntent.putExtra("episode", episode)
setResult(RESULT_OK, returnIntent)
exoPlayer.release()
super.onBackPressed()
}
private fun nextEpisode() {
youTubeDoubleTap.player(exoPlayer)
isPlayerPlaying = exoPlayer.playWhenReady
playbackPosition = exoPlayer.currentPosition
currentWindow = exoPlayer.currentWindowIndex
val episode = intent.getSerializableExtra("episode") as Episode
val returnIntent = intent
returnIntent.putExtra("seconds_result", playbackPosition)
returnIntent.putExtra("total_seconds_result", exoPlayer.duration)
returnIntent.putExtra("episode", episode)
returnIntent.putExtra("nextResult", true)
setResult(RESULT_OK, returnIntent)
exoPlayer.release()
super.onBackPressed()
saveEpisodeHistory(EpisodeItem(episode, anime))
setEpisodeProgress(episode, anime, exoPlayer.currentPosition, exoPlayer.duration)
episode = episodeList[episodeList.indexOf(episode) + 1]
uri = EpisodeLoader.getUri(episode, anime, source)
mediaItem = MediaItem.Builder()
.setUri(uri)
.setMimeType(MimeTypes.VIDEO_MP4)
.build()
exoPlayer.setMediaItem(mediaItem, episode.last_second_seen)
exoPlayer.prepare()
exoPlayer.playWhenReady = true
}
private fun previousEpisode() {
youTubeDoubleTap.player(exoPlayer)
isPlayerPlaying = exoPlayer.playWhenReady
playbackPosition = exoPlayer.currentPosition
currentWindow = exoPlayer.currentWindowIndex
val episode = intent.getSerializableExtra("episode") as Episode
val returnIntent = intent
returnIntent.putExtra("seconds_result", playbackPosition)
returnIntent.putExtra("total_seconds_result", exoPlayer.duration)
returnIntent.putExtra("episode", episode)
returnIntent.putExtra("previousResult", true)
setResult(RESULT_OK, returnIntent)
exoPlayer.release()
super.onBackPressed()
saveEpisodeHistory(EpisodeItem(episode, anime))
setEpisodeProgress(episode, anime, exoPlayer.currentPosition, exoPlayer.duration)
episode = episodeList[episodeList.indexOf(episode) - 1]
uri = EpisodeLoader.getUri(episode, anime, source)
mediaItem = MediaItem.Builder()
.setUri(uri)
.setMimeType(MimeTypes.VIDEO_MP4)
.build()
exoPlayer.setMediaItem(mediaItem, episode.last_second_seen)
exoPlayer.prepare()
exoPlayer.playWhenReady = true
}
override fun onSaveInstanceState(outState: Bundle) {
@ -285,8 +284,6 @@ class WatcherActivity : AppCompatActivity() {
}
private fun updateTrackEpisodeSeen(episode: Episode) {
val anime = intent.getSerializableExtra("anime_anime") as? Anime ?: return
val episodeSeen = episode.episode_number.toInt()
val trackManager = Injekt.get<TrackManager>()
@ -317,20 +314,12 @@ class WatcherActivity : AppCompatActivity() {
}
companion object {
fun newIntent(context: Context, anime: Anime, episode: Episode, episodeList: List<EpisodeItem>, url: String): Intent {
fun newIntent(context: Context, anime: Anime, episode: Episode, episodeList: ArrayList<Episode>): Intent {
return Intent(context, WatcherActivity::class.java).apply {
putExtra("anime", anime.id)
putExtra("anime_anime", anime)
putExtra("anime", anime)
putExtra("episode", episode)
putExtra("second", episode.last_second_seen)
putExtra("uri", url)
if (episodeList.isNotEmpty()) {
putExtra("hasNextEpisode", episode.episode_number < episodeList[0].episode_number)
putExtra("hasPreviousEpisode", episode.episode_number > episodeList[episodeList.size - 1].episode_number)
} else {
putExtra("hasNextEpisode", false)
putExtra("hasPreviousEpisode", false)
}
putExtra("episodeList", episodeList)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
}