From ad5a26d8ed2c77df332f483fd77d16bb25b2e8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Font=C3=A1n?= Date: Sun, 5 May 2024 10:57:15 +0200 Subject: [PATCH] feat. Added on click to the notification for opening the app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gabriel Fontán --- .../mediaplayer/di/MediaPlayerModule.kt | 11 +++++++++- .../service/MediaLibrarySessionCallback.kt | 22 +++++++++++++------ .../mediaplayer/service/MediaplayerService.kt | 3 +++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/di/MediaPlayerModule.kt b/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/di/MediaPlayerModule.kt index 5a3942a..85d1376 100644 --- a/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/di/MediaPlayerModule.kt +++ b/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/di/MediaPlayerModule.kt @@ -1,5 +1,6 @@ package com.bobbyesp.mediaplayer.di +import android.app.PendingIntent import android.content.Context import android.os.Build import androidx.annotation.OptIn @@ -63,9 +64,17 @@ object MediaPlayerModule { fun provideMediaSession( @ApplicationContext context: Context, player: ExoPlayer, - mediaLibrarySessionCallback: MediaLibrarySessionCallback + mediaLibrarySessionCallback: MediaLibrarySessionCallback, ): MediaSession = MediaLibrarySession.Builder(context, player, mediaLibrarySessionCallback) + .setSessionActivity( + PendingIntent.getActivity( + context, + 0, + context.packageManager.getLaunchIntentForPackage(context.packageName), + PendingIntent.FLAG_IMMUTABLE + ) + ) .build() @Provides diff --git a/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaLibrarySessionCallback.kt b/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaLibrarySessionCallback.kt index 21c0e23..63e7e30 100644 --- a/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaLibrarySessionCallback.kt +++ b/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaLibrarySessionCallback.kt @@ -25,21 +25,29 @@ import dagger.hilt.android.qualifiers.ApplicationContext import javax.inject.Inject class MediaLibrarySessionCallback @Inject constructor( - @ApplicationContext val context: Context + @ApplicationContext val context: Context, ) : MediaLibraryService.MediaLibrarySession.Callback { + private val availableCommands = listOf( + CommandToggleLibrary, + CommandToggleLike, + CommandToggleShuffle, + CommandToggleRepeatMode + ) + override fun onConnect( session: MediaSession, controller: MediaSession.ControllerInfo ): MediaSession.ConnectionResult { val connectionResult = super.onConnect(session, controller) + val availableSessionCommands = connectionResult.availableSessionCommands.buildUpon() + + availableCommands.forEach { + availableSessionCommands.add(it) + } + return MediaSession.ConnectionResult.accept( - connectionResult.availableSessionCommands.buildUpon() - .add(CommandToggleLibrary) - .add(CommandToggleLike) - .add(CommandToggleShuffle) - .add(CommandToggleRepeatMode) - .build(), + availableSessionCommands.build(), connectionResult.availablePlayerCommands ) } diff --git a/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaplayerService.kt b/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaplayerService.kt index 37ea6cd..0a21f2e 100644 --- a/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaplayerService.kt +++ b/app/mediaplayer/src/main/java/com/bobbyesp/mediaplayer/service/MediaplayerService.kt @@ -36,6 +36,9 @@ class MediaplayerService : MediaSessionService(), MediaSessionLayoutHandler { override fun updateNotificationLayout() { Log.i("MediaplayerService", "Updating notification layout") + Log.i("MediaplayerService", "Shuffle mode: ${mediaSession.player.shuffleModeEnabled}") + Log.i("MediaplayerService", "Repeat mode: ${mediaSession.player.repeatMode}") + mediaSession.setCustomLayout( listOf( CommandButton.Builder()