Skip to content

Commit e5458bc

Browse files
committed
Properly handle item errors during media browser loading
Non-item errors, i.e. critical parsing errors of the page, are still handled properly.
1 parent dc62d21 commit e5458bc

2 files changed

Lines changed: 19 additions & 37 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserImpl.kt

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.media.MediaBrowserServiceCompat.Result
1313
import androidx.media.utils.MediaConstants
1414
import io.reactivex.rxjava3.core.Flowable
1515
import io.reactivex.rxjava3.core.Single
16-
import io.reactivex.rxjava3.core.SingleSource
1716
import io.reactivex.rxjava3.disposables.CompositeDisposable
1817
import io.reactivex.rxjava3.schedulers.Schedulers
1918
import org.schabi.newpipe.MainActivity.DEBUG
@@ -25,10 +24,8 @@ import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
2524
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity
2625
import org.schabi.newpipe.extractor.InfoItem
2726
import org.schabi.newpipe.extractor.InfoItem.InfoType
28-
import org.schabi.newpipe.extractor.ListInfo
2927
import org.schabi.newpipe.extractor.channel.ChannelInfoItem
3028
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException
31-
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException
3229
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
3330
import org.schabi.newpipe.extractor.search.SearchInfo
3431
import org.schabi.newpipe.extractor.stream.StreamInfoItem
@@ -86,6 +83,10 @@ class MediaBrowserImpl(
8683

8784
//region onLoadChildren
8885
fun onLoadChildren(parentId: String, result: Result<List<MediaBrowserCompat.MediaItem>>) {
86+
if (DEBUG) {
87+
Log.d(TAG, "onLoadChildren($parentId)")
88+
}
89+
8990
result.detach() // allows sendResult() to happen later
9091
disposables.add(
9192
onLoadChildren(parentId)
@@ -101,10 +102,6 @@ class MediaBrowserImpl(
101102
}
102103

103104
private fun onLoadChildren(parentId: String): Single<List<MediaBrowserCompat.MediaItem>> {
104-
if (DEBUG) {
105-
Log.d(TAG, "onLoadChildren($parentId)")
106-
}
107-
108105
try {
109106
val parentIdUri = Uri.parse(parentId)
110107
val path = ArrayList(parentIdUri.pathSegments)
@@ -353,15 +350,12 @@ class MediaBrowserImpl(
353350
private fun populateRemotePlaylist(playlistId: Long): Single<List<MediaBrowserCompat.MediaItem>> {
354351
return RemotePlaylistManager(database).getPlaylist(playlistId).firstOrError()
355352
.flatMap { ExtractorHelper.getPlaylistInfo(it.serviceId, it.url, false) }
356-
.flatMap { info ->
357-
info.errors.firstOrNull { it !is ContentNotSupportedException }?.let {
358-
return@flatMap Single.error(it)
353+
.map {
354+
// ignore it.errors, i.e. ignore errors about specific items, since there would
355+
// be no way to show the error properly in Android Auto anyway
356+
it.relatedItems.mapIndexed { index, item ->
357+
createRemotePlaylistStreamMediaItem(playlistId, item, index)
359358
}
360-
Single.just(
361-
info.relatedItems.mapIndexed { index, item ->
362-
createRemotePlaylistStreamMediaItem(playlistId, item, index)
363-
}
364-
)
365359
}
366360
}
367361
//endregion
@@ -371,10 +365,16 @@ class MediaBrowserImpl(
371365
query: String,
372366
result: Result<List<MediaBrowserCompat.MediaItem>>
373367
) {
368+
if (DEBUG) {
369+
Log.d(TAG, "onSearch($query)")
370+
}
371+
374372
result.detach() // allows sendResult() to happen later
375373
disposables.add(
376374
searchMusicBySongTitle(query)
377-
.flatMap { this.mediaItemsFromInfoItemList(it) }
375+
// ignore it.errors, i.e. ignore errors about specific items, since there would
376+
// be no way to show the error properly in Android Auto anyway
377+
.map { it.relatedItems.mapNotNull(this::createInfoItemMediaItem) }
378378
.subscribeOn(Schedulers.io())
379379
.subscribe(
380380
{ result.sendResult(it) },
@@ -391,20 +391,6 @@ class MediaBrowserImpl(
391391
val serviceId = ServiceHelper.getSelectedServiceId(context)
392392
return ExtractorHelper.searchFor(serviceId, query, listOf(), "")
393393
}
394-
395-
private fun mediaItemsFromInfoItemList(
396-
result: ListInfo<InfoItem>
397-
): SingleSource<List<MediaBrowserCompat.MediaItem>> {
398-
result.errors.firstOrNull()?.let { return@mediaItemsFromInfoItemList Single.error(it) }
399-
400-
return try {
401-
Single.just(
402-
result.relatedItems.mapNotNull { item -> this.createInfoItemMediaItem(item) }
403-
)
404-
} catch (e: Exception) {
405-
Single.error(e)
406-
}
407-
}
408394
//endregion
409395

410396
companion object {

app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserPlaybackPreparer.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import org.schabi.newpipe.NewPipeDatabase
1717
import org.schabi.newpipe.R
1818
import org.schabi.newpipe.extractor.InfoItem.InfoType
1919
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException
20-
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException
2120
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler
2221
import org.schabi.newpipe.local.playlist.LocalPlaylistManager
2322
import org.schabi.newpipe.local.playlist.RemotePlaylistManager
@@ -131,12 +130,9 @@ class MediaBrowserPlaybackPreparer(
131130
private fun extractRemotePlayQueue(playlistId: Long, index: Int): Single<PlayQueue> {
132131
return RemotePlaylistManager(database).getPlaylist(playlistId).firstOrError()
133132
.flatMap { ExtractorHelper.getPlaylistInfo(it.serviceId, it.url, false) }
134-
.flatMap { info ->
135-
info.errors.firstOrNull { it !is ContentNotSupportedException }?.let {
136-
return@flatMap Single.error(it)
137-
}
138-
Single.just(PlaylistPlayQueue(info, index))
139-
}
133+
// ignore info.errors, i.e. ignore errors about specific items, since there would
134+
// be no way to show the error properly in Android Auto anyway
135+
.map { info -> PlaylistPlayQueue(info, index) }
140136
}
141137

142138
private fun extractPlayQueueFromMediaId(mediaId: String): Single<PlayQueue> {

0 commit comments

Comments
 (0)