From 978d811470f3cb3a1746af3d3774fb6110f63d0b Mon Sep 17 00:00:00 2001 From: Haggai Eran Date: Sat, 4 Feb 2023 18:09:52 +0200 Subject: [PATCH] MediaBrowser: expose search history --- .../mediabrowser/MediaBrowserConnector.java | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java index 6a30e413c53..08f8b6c05a6 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java @@ -22,6 +22,7 @@ import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.R; import org.schabi.newpipe.database.AppDatabase; +import org.schabi.newpipe.database.history.model.StreamHistoryEntry; import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry; import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; import org.schabi.newpipe.local.playlist.LocalPlaylistManager; @@ -64,6 +65,10 @@ public void release() { private static final String ID_ROOT = "//${BuildConfig.APPLICATION_ID}/r"; @NonNull private static final String ID_BOOKMARKS = ID_ROOT + "/playlists"; + @NonNull + private static final String ID_HISTORY = ID_ROOT + "/history"; + @NonNull + private static final String ID_STREAM = ID_ROOT + "/stream"; private MediaItem createRootMediaItem(final String mediaId, final String folderName) { final var builder = new MediaDescriptionCompat.Builder(); @@ -130,7 +135,9 @@ public void onLoadChildren(@NonNull final String parentId, mediaItems.add( createRootMediaItem(ID_BOOKMARKS, playerService.getResources().getString(R.string.tab_bookmarks))); - + mediaItems.add( + createRootMediaItem(ID_HISTORY, + playerService.getResources().getString(R.string.action_history))); } else if (parentId.startsWith(ID_BOOKMARKS)) { final var path = parentIdUri.getPathSegments(); if (path.size() == 2) { @@ -141,16 +148,38 @@ public void onLoadChildren(@NonNull final String parentId, } else { Log.w(TAG, "Unknown playlist uri " + parentId); } + } else if (parentId.equals(ID_HISTORY)) { + populateHistory(mediaItems); } result.sendResult(mediaItems); } - private LocalPlaylistManager getPlaylistManager() { + private void populateHistory(final List mediaItems) { + final var streamHistory = getDatabase().streamHistoryDAO(); + final var history = streamHistory.getHistory().blockingFirst(); + mediaItems.addAll(history.stream().map(this::createHistoryMediaItem) + .collect(Collectors.toList())); + } + + private MediaItem createHistoryMediaItem(@NonNull final StreamHistoryEntry streamHistoryEntry) { + final var builder = new MediaDescriptionCompat.Builder(); + builder.setMediaId(ID_STREAM + '/' + streamHistoryEntry.getStreamId()) + .setTitle(streamHistoryEntry.getStreamEntity().getTitle()) + .setIconUri(Uri.parse(streamHistoryEntry.getStreamEntity().getThumbnailUrl())); + + return new MediaItem(builder.build(), MediaItem.FLAG_PLAYABLE); + } + + private AppDatabase getDatabase() { if (database == null) { database = NewPipeDatabase.getInstance(playerService); } + return database; + } + + private LocalPlaylistManager getPlaylistManager() { if (localPlaylistManager == null) { - localPlaylistManager = new LocalPlaylistManager(database); + localPlaylistManager = new LocalPlaylistManager(getDatabase()); } return localPlaylistManager; } @@ -202,6 +231,15 @@ private PlayQueue extractPlayQueueFromMediaId(final String mediaId) { return new SinglePlayQueue(items, index); } + } else if (mediaId.startsWith(ID_STREAM)) { + final var path = mediaIdUri.getPathSegments(); + if (path.size() == 3) { + final long streamId = Long.parseLong(path.get(2)); + final var items = getDatabase().streamHistoryDAO().getHistory() + .blockingFirst().stream().filter(it -> it.getStreamId() == streamId) + .map(StreamHistoryEntry::toStreamInfoItem).collect(Collectors.toList()); + return new SinglePlayQueue(items, 0); + } } playbackError(R.string.error_http_not_found,