21
21
import org .schabi .newpipe .NewPipeDatabase ;
22
22
import org .schabi .newpipe .R ;
23
23
import org .schabi .newpipe .database .AppDatabase ;
24
+ import org .schabi .newpipe .database .history .model .StreamHistoryEntry ;
24
25
import org .schabi .newpipe .database .playlist .PlaylistMetadataEntry ;
25
26
import org .schabi .newpipe .database .playlist .PlaylistStreamEntry ;
26
27
import org .schabi .newpipe .local .playlist .LocalPlaylistManager ;
@@ -70,6 +71,10 @@ public void release() {
70
71
private static final String ID_ROOT = "//${BuildConfig.APPLICATION_ID}/r" ;
71
72
@ NonNull
72
73
private static final String ID_BOOKMARKS = ID_ROOT + "/playlists" ;
74
+ @ NonNull
75
+ private static final String ID_HISTORY = ID_ROOT + "/history" ;
76
+ @ NonNull
77
+ private static final String ID_STREAM = ID_ROOT + "/stream" ;
73
78
74
79
private MediaItem createRootMediaItem (final String mediaId , final String folderName ) {
75
80
final var builder = new MediaDescriptionCompat .Builder ();
@@ -135,7 +140,9 @@ public Single<List<MediaItem>> onLoadChildren(@NonNull final String parentId) {
135
140
mediaItems .add (
136
141
createRootMediaItem (ID_BOOKMARKS ,
137
142
playerService .getResources ().getString (R .string .tab_bookmarks )));
138
-
143
+ mediaItems .add (
144
+ createRootMediaItem (ID_HISTORY ,
145
+ playerService .getResources ().getString (R .string .action_history )));
139
146
} else if (parentId .startsWith (ID_BOOKMARKS )) {
140
147
final var path = parentIdUri .getPathSegments ();
141
148
if (path .size () == 2 ) {
@@ -146,16 +153,38 @@ public Single<List<MediaItem>> onLoadChildren(@NonNull final String parentId) {
146
153
} else {
147
154
Log .w (TAG , "Unknown playlist uri " + parentId );
148
155
}
156
+ } else if (parentId .equals (ID_HISTORY )) {
157
+ return populateHistory ();
149
158
}
150
159
return Single .just (mediaItems );
151
160
}
152
161
153
- private LocalPlaylistManager getPlaylistManager () {
162
+ private Single <List <MediaItem >> populateHistory () {
163
+ final var streamHistory = getDatabase ().streamHistoryDAO ();
164
+ final var history = streamHistory .getHistory ().firstOrError ();
165
+ return history .map (items ->
166
+ items .stream ().map (this ::createHistoryMediaItem ).collect (Collectors .toList ()));
167
+ }
168
+
169
+ private MediaItem createHistoryMediaItem (@ NonNull final StreamHistoryEntry streamHistoryEntry ) {
170
+ final var builder = new MediaDescriptionCompat .Builder ();
171
+ builder .setMediaId (ID_STREAM + '/' + streamHistoryEntry .getStreamId ())
172
+ .setTitle (streamHistoryEntry .getStreamEntity ().getTitle ())
173
+ .setIconUri (Uri .parse (streamHistoryEntry .getStreamEntity ().getThumbnailUrl ()));
174
+
175
+ return new MediaItem (builder .build (), MediaItem .FLAG_PLAYABLE );
176
+ }
177
+
178
+ private AppDatabase getDatabase () {
154
179
if (database == null ) {
155
180
database = NewPipeDatabase .getInstance (playerService );
156
181
}
182
+ return database ;
183
+ }
184
+
185
+ private LocalPlaylistManager getPlaylistManager () {
157
186
if (localPlaylistManager == null ) {
158
- localPlaylistManager = new LocalPlaylistManager (database );
187
+ localPlaylistManager = new LocalPlaylistManager (getDatabase () );
159
188
}
160
189
return localPlaylistManager ;
161
190
}
@@ -205,6 +234,20 @@ private Single<PlayQueue> extractPlayQueueFromMediaId(final String mediaId) {
205
234
return new SinglePlayQueue (infoItems , index );
206
235
});
207
236
}
237
+ } else if (mediaId .startsWith (ID_STREAM )) {
238
+ final var path = mediaIdUri .getPathSegments ();
239
+ if (path .size () == 3 ) {
240
+ final long streamId = Long .parseLong (path .get (2 ));
241
+ return getDatabase ().streamHistoryDAO ().getHistory ()
242
+ .firstOrError ()
243
+ .map (items -> {
244
+ final var infoItems = items .stream ()
245
+ .filter (it -> it .getStreamId () == streamId )
246
+ .map (StreamHistoryEntry ::toStreamInfoItem )
247
+ .collect (Collectors .toList ());
248
+ return new SinglePlayQueue (infoItems , 0 );
249
+ });
250
+ }
208
251
}
209
252
210
253
return Single .error (new NullPointerException ());
0 commit comments