From 51fb70a1b382e322d446f0734e1338da3e531f83 Mon Sep 17 00:00:00 2001 From: Haggai Eran Date: Sun, 25 Dec 2022 09:16:43 +0200 Subject: [PATCH] Simple playback status and controls in Android Auto Expose a MediaBrowserService from within the existing PlayerService, and use the existing MediaSession for Auto. Empty media browser for now. To test, one needs to enable "Unknown sources" in Android Auto's developer settings. Issue: #1758 --- app/src/main/AndroidManifest.xml | 3 ++ .../schabi/newpipe/player/PlayerService.java | 36 ++++++++++++++++- .../mediabrowser/MediaBrowserConnector.java | 39 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d11de9f478d..1950031cd09 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,6 +64,9 @@ + + + > result) { + result.detach(); + final var disposable = mediaBrowserConnector.onLoadChildren(parentId) + .subscribe(result::sendResult); + compositeDisposableLoadChildren.add(disposable); + } } 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 6fc61c3d748..3c05b211983 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 @@ -1,13 +1,26 @@ package org.schabi.newpipe.player.mediabrowser; +import static org.schabi.newpipe.MainActivity.DEBUG; + +import android.os.Bundle; +import android.support.v4.media.MediaBrowserCompat; +import android.support.v4.media.MediaBrowserCompat.MediaItem; import android.support.v4.media.session.MediaSessionCompat; +import android.util.Log; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.media.MediaBrowserServiceCompat; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; import org.schabi.newpipe.player.PlayerService; +import java.util.ArrayList; +import java.util.List; + +import io.reactivex.rxjava3.core.Single; + public class MediaBrowserConnector { private static final String TAG = MediaBrowserConnector.class.getSimpleName(); @@ -20,6 +33,7 @@ public MediaBrowserConnector(@NonNull final PlayerService playerService) { mediaSession = new MediaSessionCompat(playerService, TAG); sessionConnector = new MediaSessionConnector(mediaSession); sessionConnector.setMetadataDeduplicationEnabled(true); + playerService.setSessionToken(mediaSession.getSessionToken()); } public @NonNull MediaSessionConnector getSessionConnector() { @@ -29,4 +43,29 @@ public MediaBrowserConnector(@NonNull final PlayerService playerService) { public void release() { mediaSession.release(); } + + @NonNull + private static final String MY_MEDIA_ROOT_ID = "media_root_id"; + + @Nullable + public MediaBrowserServiceCompat.BrowserRoot onGetRoot(@NonNull final String clientPackageName, + final int clientUid, + @Nullable final Bundle rootHints) { + if (DEBUG) { + Log.d(TAG, String.format("MediaBrowserService.onGetRoot(%s, %s, %s)", + clientPackageName, clientUid, rootHints)); + } + + return new MediaBrowserServiceCompat.BrowserRoot(MY_MEDIA_ROOT_ID, null); + } + + public Single> onLoadChildren(@NonNull final String parentId) { + if (DEBUG) { + Log.d(TAG, String.format("MediaBrowserService.onLoadChildren(%s)", parentId)); + } + + final List mediaItems = new ArrayList<>(); + + return Single.just(mediaItems); + } }