Skip to content

Commit

Permalink
feat. Added on click to the notification for opening the app
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed May 5, 2024
1 parent 6972f64 commit ad5a26d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ad5a26d

Please sign in to comment.