3737import com .google .android .exoplayer2 .ext .mediasession .MediaSessionConnector ;
3838
3939import org .schabi .newpipe .ktx .BundleKt ;
40+ import org .schabi .newpipe .player .mediabrowser .MediaBrowserImpl ;
41+ import org .schabi .newpipe .player .mediabrowser .MediaBrowserPlaybackPreparer ;
4042import org .schabi .newpipe .player .mediasession .MediaSessionPlayerUi ;
4143import org .schabi .newpipe .player .notification .NotificationPlayerUi ;
4244import org .schabi .newpipe .util .ThemeHelper ;
@@ -55,6 +57,12 @@ public final class PlayerService extends MediaBrowserServiceCompat {
5557 public static final String SHOULD_START_FOREGROUND_EXTRA = "should_start_foreground_extra" ;
5658 public static final String BIND_PLAYER_HOLDER_ACTION = "bind_player_holder_action" ;
5759
60+ // These objects are used to cleanly separate the Service implementation (in this file) and the
61+ // media browser and playback preparer implementations. At the moment the playback preparer is
62+ // only used in conjunction with the media browser.
63+ private MediaBrowserImpl mediaBrowserImpl ;
64+ private MediaBrowserPlaybackPreparer mediaBrowserPlaybackPreparer ;
65+
5866 // these are instantiated in onCreate() as per
5967 // https://developer.android.com/training/cars/media#browser_workflow
6068 private MediaSessionCompat mediaSession ;
@@ -66,10 +74,7 @@ public final class PlayerService extends MediaBrowserServiceCompat {
6674 private final IBinder mBinder = new PlayerService .LocalBinder (this );
6775
6876
69- /*//////////////////////////////////////////////////////////////////////////
70- // Service's LifeCycle
71- //////////////////////////////////////////////////////////////////////////*/
72-
77+ //region Service lifecycle
7378 @ Override
7479 public void onCreate () {
7580 super .onCreate ();
@@ -80,12 +85,21 @@ public void onCreate() {
8085 assureCorrectAppLanguage (this );
8186 ThemeHelper .setTheme (this );
8287
88+ mediaBrowserImpl = new MediaBrowserImpl (this , this ::notifyChildrenChanged );
89+
8390 // see https://developer.android.com/training/cars/media#browser_workflow
8491 mediaSession = new MediaSessionCompat (this , "MediaSessionPlayerServ" );
8592 setSessionToken (mediaSession .getSessionToken ());
8693 sessionConnector = new MediaSessionConnector (mediaSession );
8794 sessionConnector .setMetadataDeduplicationEnabled (true );
8895
96+ mediaBrowserPlaybackPreparer = new MediaBrowserPlaybackPreparer (
97+ this ,
98+ sessionConnector ::setCustomErrorMessage ,
99+ () -> sessionConnector .setCustomErrorMessage (null )
100+ );
101+ sessionConnector .setPlaybackPreparer (mediaBrowserPlaybackPreparer );
102+
89103 // Note: you might be tempted to create the player instance and call startForeground here,
90104 // but be aware that the Android system might start the service just to perform media
91105 // queries. In those cases creating a player instance is a waste of resources, and calling
@@ -177,8 +191,10 @@ public void onDestroy() {
177191
178192 cleanup ();
179193
194+ mediaBrowserPlaybackPreparer .dispose ();
180195 mediaSession .setActive (false );
181196 mediaSession .release ();
197+ mediaBrowserImpl .dispose ();
182198 }
183199
184200 private void cleanup () {
@@ -197,7 +213,9 @@ public void stopService() {
197213 protected void attachBaseContext (final Context base ) {
198214 super .attachBaseContext (AudioServiceLeakFix .preventLeakOf (base ));
199215 }
216+ //endregion
200217
218+ //region Bind
201219 @ Override
202220 public IBinder onBind (final Intent intent ) {
203221 if (DEBUG ) {
@@ -236,18 +254,28 @@ public Player getPlayer() {
236254 return playerService .get ().player ;
237255 }
238256 }
257+ //endregion
239258
240- @ Nullable
259+ //region Media browser
241260 @ Override
242261 public BrowserRoot onGetRoot (@ NonNull final String clientPackageName ,
243262 final int clientUid ,
244263 @ Nullable final Bundle rootHints ) {
245- return null ;
264+ // TODO check if the accessing package has permission to view data
265+ return mediaBrowserImpl .onGetRoot (clientPackageName , clientUid , rootHints );
246266 }
247267
248268 @ Override
249269 public void onLoadChildren (@ NonNull final String parentId ,
250270 @ NonNull final Result <List <MediaBrowserCompat .MediaItem >> result ) {
271+ mediaBrowserImpl .onLoadChildren (parentId , result );
272+ }
251273
274+ @ Override
275+ public void onSearch (@ NonNull final String query ,
276+ final Bundle extras ,
277+ @ NonNull final Result <List <MediaBrowserCompat .MediaItem >> result ) {
278+ mediaBrowserImpl .onSearch (query , result );
252279 }
280+ //endregion
253281}
0 commit comments