1+ package org.newpipe.externalplayer
2+
3+ import android.app.Notification
4+ import android.app.PendingIntent
5+ import android.content.Context
6+ import android.content.Intent
7+ import android.graphics.BitmapFactory
8+ import androidx.core.app.NotificationCompat
9+ import androidx.media.app.NotificationCompat.MediaStyle
10+
11+ class MediaNotificationManager (private val context : Context ) {
12+
13+ fun buildNotification (player : com.google.android.exoplayer2.ExoPlayer ? , token : android.os.Parcelable ? ): Notification {
14+ val playIntent = Intent (context, PlayerService ::class .java).apply { action = PlayerService .ACTION_PLAY }
15+ val pauseIntent = Intent (context, PlayerService ::class .java).apply { action = PlayerService .ACTION_PAUSE }
16+ val stopIntent = Intent (context, PlayerService ::class .java).apply { action = PlayerService .ACTION_STOP }
17+
18+ val playPending = PendingIntent .getService(context, 0 , playIntent, PendingIntent .FLAG_IMMUTABLE )
19+ val pausePending = PendingIntent .getService(context, 1 , pauseIntent, PendingIntent .FLAG_IMMUTABLE )
20+ val stopPending = PendingIntent .getService(context, 2 , stopIntent, PendingIntent .FLAG_IMMUTABLE )
21+
22+ val isPlaying = player?.isPlaying == true
23+ val action = if (isPlaying) {
24+ NotificationCompat .Action (android.R .drawable.ic_media_pause, " Pause" , pausePending)
25+ } else {
26+ NotificationCompat .Action (android.R .drawable.ic_media_play, " Play" , playPending)
27+ }
28+
29+ val mediaStyle = MediaStyle ()
30+ token?.let { mediaStyle.setMediaSession(it as android.media.session.MediaSession .Token ) }
31+
32+ val builder = NotificationCompat .Builder (context, PlayerService .CHANNEL_ID )
33+ .setContentTitle(" NewPipe External Player" )
34+ .setContentText(" Playing" )
35+ .setSmallIcon(android.R .drawable.ic_media_play)
36+ .setLargeIcon(BitmapFactory .decodeResource(context.resources, android.R .mipmap.sym_def_app_icon))
37+ .addAction(action)
38+ .addAction(NotificationCompat .Action (android.R .drawable.ic_media_previous, " Stop" , stopPending))
39+ .setStyle(mediaStyle)
40+ .setVisibility(NotificationCompat .VISIBILITY_PUBLIC )
41+ .setOnlyAlertOnce(true )
42+ .setOngoing(isPlaying)
43+
44+ return builder.build()
45+ }
46+ }
0 commit comments