Skip to content

Commit

Permalink
show error messages when playing anime
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed Nov 26, 2021
1 parent c039e53 commit bc4fd7b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
20 changes: 16 additions & 4 deletions app/src/main/java/eu/kanade/tachiyomi/ui/anime/AnimeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1043,10 +1043,14 @@ class AnimeController :
}

if (!useInternal) launchIO {
val video = EpisodeLoader.getLink(episode, anime!!, source!!).awaitSingle()
val video = try {
EpisodeLoader.getLink(episode, anime!!, source!!).awaitSingle()
} catch (e: Exception) {
return@launchIO makeErrorToast(context, e)
}
if (video != null) {
val videoUri = video.uri
val videoUrl = Uri.parse(video.videoUrl)
val videoUrl = Uri.parse(video.videoUrl ?: return@launchIO makeErrorToast(context, Exception("video URL is null.")))
currentExtEpisode = episode
val pkgName = preferences.externalPlayerPreference()

Expand All @@ -1055,15 +1059,23 @@ class AnimeController :
} else videoUri ?: videoUrl

val extIntent = getExternalIntent(pkgName, uri, episode, video, context)
try { startActivityForResult(extIntent, REQUEST_EXTERNAL) } catch (t: Throwable) { launchUI { context.toast("Cannot open episode") } }
try {
startActivityForResult(extIntent, REQUEST_EXTERNAL)
} catch (e: Exception) {
makeErrorToast(context, e)
}
} else {
launchUI { context.toast("Cannot open episode") }
makeErrorToast(context, Exception("Couldn't find any video links."))
}
} else {
startActivity(intent)
}
}

private fun makeErrorToast(context: Context, e: Exception?) {
launchUI { context.toast(e?.message ?: "Cannot open episode") }
}

private fun getExternalIntent(pkgName: String?, uri: Uri, episode: Episode, video: Video, context: Context): Intent {
return if (pkgName.isNullOrEmpty()) {
Intent(Intent.ACTION_VIEW).apply {
Expand Down
13 changes: 3 additions & 10 deletions app/src/main/java/eu/kanade/tachiyomi/ui/player/EpisodeLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,20 @@ class EpisodeLoader {
val isDownloaded = downloadManager.isEpisodeDownloaded(episode, anime, true)
return when {
isDownloaded -> isDownloaded(episode, anime, source, downloadManager).map {
if (it.isEmpty()) null
else it.first()
it.firstOrNull()
}
source is AnimeHttpSource -> isHttp(episode, source).map {
if (it.isEmpty()) null
else it.first()
it.firstOrNull()
}
source is LocalAnimeSource -> isLocal(episode).map {
if (it.isEmpty()) null
else it.first()
it.firstOrNull()
}
else -> error("source not supported")
}
}

private fun isHttp(episode: Episode, source: AnimeHttpSource): Observable<List<Video>> {
return source.fetchVideoList(episode)
.onErrorReturn {
errorMessage = it.message ?: "error getting links"
emptyList()
}
.flatMapIterable { it }
.flatMap {
source.fetchUrlFromVideo(it)
Expand Down
66 changes: 27 additions & 39 deletions app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.listener.SimpleSeekBarListener
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import logcat.LogPriority
import rx.Observable
import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
Expand All @@ -101,7 +99,6 @@ class PlayerActivity : AppCompatActivity() {
private val cacheSize = 100L * 1024L * 1024L // 100 MB
private lateinit var simpleCache: SimpleCache
private lateinit var cacheFactory: CacheDataSource.Factory
private var message: String? = null
private lateinit var mediaSourceFactory: MediaSourceFactory
private lateinit var playerView: DoubleTapPlayerView
private lateinit var youTubeDoubleTap: YouTubeOverlay
Expand All @@ -120,7 +117,6 @@ class PlayerActivity : AppCompatActivity() {
private val defaultUserAgentString = WebSettings.getDefaultUserAgent(this)
private lateinit var uri: String
private var videos = emptyList<Video>()
private lateinit var videoListObservable: Observable<Observable<List<Video>>>
private var isBuffering = true
private var isLocal = false
private var currentQuality = 0
Expand Down Expand Up @@ -191,42 +187,41 @@ class PlayerActivity : AppCompatActivity() {
mediaSourceFactory = DefaultMediaSourceFactory(DefaultDataSource.Factory(this))
exoPlayer = newPlayer()
dbProvider = StandaloneDatabaseProvider(baseContext)
if (SimpleCache.isCacheFolderLocked(baseContext.filesDir)) {
SimpleCache.delete(baseContext.filesDir, dbProvider)
val cacheFile = File(baseContext.filesDir, "media")
if (SimpleCache.isCacheFolderLocked(cacheFile)) {
SimpleCache.delete(cacheFile, dbProvider)
}
simpleCache = SimpleCache(
File(baseContext.filesDir, "media"),
cacheFile,
LeastRecentlyUsedCacheEvictor(cacheSize),
dbProvider
)

videoListObservable = Observable.fromCallable {
awaitVideoList()
}
initPlayer()
}

private fun onGetLinksError(e: Throwable? = null) {
baseContext.toast(e?.message ?: "error getting links")
finish()
return
launchUI {
baseContext.toast(e?.message ?: "error getting links", Toast.LENGTH_LONG)
finish()
}
}

private fun onGetLinks() {
val context = this
launchUI {
isBuffering(false)
if (videos.isEmpty()) {
onGetLinksError()
onGetLinksError(Exception("Couldn't find any video links."))
return@launchUI
}
dbProvider = StandaloneDatabaseProvider(baseContext)
isLocal = EpisodeLoader.isDownloaded(episode, anime) || source is LocalAnimeSource
if (isLocal) {
uri = videos.firstOrNull()?.uri?.toString() ?: return@launchUI onGetLinksError()
uri = videos.firstOrNull()?.uri?.toString() ?: return@launchUI onGetLinksError(Exception("URI is null."))
dataSourceFactory = DefaultDataSource.Factory(context)
} else {
uri = videos.firstOrNull()?.videoUrl ?: return@launchUI onGetLinksError()
uri = videos.firstOrNull()?.videoUrl ?: return@launchUI onGetLinksError(Exception("video URL is null."))
dataSourceFactory = newDataSourceFactory()
}
logcat(LogPriority.INFO) { "playing $uri" }
Expand Down Expand Up @@ -298,7 +293,7 @@ class PlayerActivity : AppCompatActivity() {
youTubeDoubleTap.player(exoPlayer)
playerView.player = exoPlayer
duration = exoPlayer.duration
getVideoList()
awaitVideoList()
}

private fun isBuffering(param: Boolean) {
Expand All @@ -310,19 +305,6 @@ class PlayerActivity : AppCompatActivity() {
}
}

private fun getVideoList() {
launchUI {
isBuffering(true)
}
videoListObservable
.subscribeOn(Schedulers.io())
.doOnCompleted { onGetLinks() }
.doOnError { onGetLinksError(it) }
.subscribe {
videos = runBlocking { it.awaitSingle() }
}
}

private fun onClickFitScreen() {
playerView.resizeMode = when (playerView.resizeMode) {
AspectRatioFrameLayout.RESIZE_MODE_FILL -> AspectRatioFrameLayout.RESIZE_MODE_FIT
Expand Down Expand Up @@ -458,13 +440,19 @@ class PlayerActivity : AppCompatActivity() {
exoPlayer.release()
}

private fun awaitVideoList(): Observable<List<Video>> {
return try {
EpisodeLoader.getLinks(episode, anime, source)
} catch (e: Exception) {
message = e.message ?: "error getting links"
logcat(LogPriority.WARN, e) { e.message ?: "error getting links" }
Observable.just(emptyList())
private fun awaitVideoList() {
isBuffering(true)
launchIO {
try {
EpisodeLoader.getLinks(episode, anime, source)
.doOnNext {
videos = it
onGetLinks()
}.awaitSingle()
} catch (e: Exception) {
logcat(LogPriority.ERROR, e) { e.message ?: "error getting links" }
onGetLinksError(e)
}
}
}

Expand All @@ -477,7 +465,7 @@ class PlayerActivity : AppCompatActivity() {
if (oldEpisode == episode) return
title.text = baseContext.getString(R.string.playertitle, anime.title, episode.name)
currentQuality = 0
getVideoList()
awaitVideoList()
}

private fun previousEpisode() {
Expand All @@ -489,7 +477,7 @@ class PlayerActivity : AppCompatActivity() {
if (oldEpisode == episode) return
title.text = baseContext.getString(R.string.playertitle, anime.title, episode.name)
currentQuality = 0
getVideoList()
awaitVideoList()
}

private fun changeQuality(quality: Int) {
Expand Down

0 comments on commit bc4fd7b

Please sign in to comment.