make it possible to reopen the player containing activity from the player notification

This commit is contained in:
Christian Schabesberger 2024-09-26 01:15:32 +02:00
parent 6954fc0990
commit 9959aea783
5 changed files with 28 additions and 6 deletions

View File

@ -20,6 +20,7 @@
package net.newpipe.newplayer package net.newpipe.newplayer
import android.app.Activity
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.Player import androidx.media3.common.Player
@ -56,6 +57,7 @@ interface NewPlayer {
val preferredAudioVariants: List<String> val preferredAudioVariants: List<String>
val preferredStreamLanguage: List<String> val preferredStreamLanguage: List<String>
val notificationIcon: IconCompat val notificationIcon: IconCompat
val playerActivityClass: Class<Activity>
val exoPlayer: StateFlow<Player?> val exoPlayer: StateFlow<Player?>
var playWhenReady: Boolean var playWhenReady: Boolean

View File

@ -20,6 +20,7 @@
package net.newpipe.newplayer package net.newpipe.newplayer
import android.app.Activity
import android.app.Application import android.app.Application
import android.content.ComponentName import android.content.ComponentName
import android.util.Log import android.util.Log
@ -59,6 +60,7 @@ private const val TAG = "NewPlayerImpl"
class NewPlayerImpl( class NewPlayerImpl(
val app: Application, val app: Application,
override val playerActivityClass: Class<Activity>,
private val repository: MediaRepository, private val repository: MediaRepository,
override val preferredVideoVariants: List<String> = emptyList(), override val preferredVideoVariants: List<String> = emptyList(),
override val preferredStreamLanguage: List<String> = emptyList(), override val preferredStreamLanguage: List<String> = emptyList(),
@ -69,7 +71,6 @@ class NewPlayerImpl(
R.drawable.new_player_tiny_icon R.drawable.new_player_tiny_icon
), ),
) : NewPlayer { ) : NewPlayer {
private val mutableExoPlayer = MutableStateFlow<ExoPlayer?>(null) private val mutableExoPlayer = MutableStateFlow<ExoPlayer?>(null)
override val exoPlayer = mutableExoPlayer.asStateFlow() override val exoPlayer = mutableExoPlayer.asStateFlow()

View File

@ -1,8 +1,11 @@
package net.newpipe.newplayer.service package net.newpipe.newplayer.service
import android.app.Activity
import android.app.Notification import android.app.Notification
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build import android.os.Build
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
@ -15,12 +18,15 @@ import net.newpipe.newplayer.R
const val NEW_PLAYER_MEDIA_NOTIFICATION_ID = 17480 const val NEW_PLAYER_MEDIA_NOTIFICATION_ID = 17480
const val NEW_PLAYER_MEDIA_NOTIFICATION_CHANNEL_NAME = "Player" const val NEW_PLAYER_MEDIA_NOTIFICATION_CHANNEL_NAME = "Player"
const val NEW_PLAYER_REQUEST_CODE_OPEN_ACTIVITY = 0
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
fun createNewPlayerNotification( fun createNewPlayerNotification(
service: NewPlayerService, service: NewPlayerService,
session: MediaSession, session: MediaSession,
notificationManager: NotificationManager, notificationManager: NotificationManager,
notificationIcon: IconCompat notificationIcon: IconCompat,
playerActivity: Class<Activity>
): Notification { ): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -34,11 +40,21 @@ fun createNewPlayerNotification(
} }
val onNotificationClickIntent = Intent(service, playerActivity)
val onNotificationClickPendingIntent =
PendingIntent.getActivity(
service,
NEW_PLAYER_REQUEST_CODE_OPEN_ACTIVITY,
onNotificationClickIntent,
PendingIntent.FLAG_IMMUTABLE
)
val notificationBuilder = val notificationBuilder =
NotificationCompat.Builder(service, NEW_PLAYER_MEDIA_NOTIFICATION_CHANNEL_NAME) NotificationCompat.Builder(service, NEW_PLAYER_MEDIA_NOTIFICATION_CHANNEL_NAME)
.setContentTitle(service.resources.getString(R.string.new_player_name)) .setContentTitle(service.resources.getString(R.string.new_player_name))
.setContentText(service.resources.getString(R.string.playing_in_background)) .setContentText(service.resources.getString(R.string.playing_in_background))
.setStyle(MediaStyleNotificationHelper.MediaStyle(session)) .setStyle(MediaStyleNotificationHelper.MediaStyle(session))
.setContentIntent(onNotificationClickPendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationBuilder.setSmallIcon(notificationIcon) notificationBuilder.setSmallIcon(notificationIcon)

View File

@ -78,7 +78,8 @@ class NewPlayerService : MediaSessionService() {
notificationManager = getSystemService( notificationManager = getSystemService(
Context.NOTIFICATION_SERVICE Context.NOTIFICATION_SERVICE
) as NotificationManager, ) as NotificationManager,
notificationIcon = newPlayer.notificationIcon notificationIcon = newPlayer.notificationIcon,
playerActivity = newPlayer.playerActivityClass
) )
return MediaNotification(NEW_PLAYER_MEDIA_NOTIFICATION_ID, notification) return MediaNotification(NEW_PLAYER_MEDIA_NOTIFICATION_ID, notification)

View File

@ -20,6 +20,7 @@
package net.newpipe.newplayer.testapp package net.newpipe.newplayer.testapp
import android.app.Activity
import android.app.Application import android.app.Application
import android.util.Log import android.util.Log
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
@ -40,9 +41,10 @@ object NewPlayerComponent {
@Singleton @Singleton
fun provideNewPlayer(app: Application): NewPlayer { fun provideNewPlayer(app: Application): NewPlayer {
val player = NewPlayerImpl( val player = NewPlayerImpl(
app, app = app,
TestMediaRepository(app), repository = TestMediaRepository(app),
notificationIcon = IconCompat.createWithResource(app, R.drawable.tinny_cools) notificationIcon = IconCompat.createWithResource(app, R.drawable.tinny_cools),
playerActivityClass = MainActivity::class.java as Class<Activity>
) )
if (app is NewPlayerApp) { if (app is NewPlayerApp) {
app.appScope.launch { app.appScope.launch {