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

View File

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

View File

@ -1,8 +1,11 @@
package net.newpipe.newplayer.service
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import androidx.annotation.OptIn
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_CHANNEL_NAME = "Player"
const val NEW_PLAYER_REQUEST_CODE_OPEN_ACTIVITY = 0
@OptIn(UnstableApi::class)
fun createNewPlayerNotification(
service: NewPlayerService,
session: MediaSession,
notificationManager: NotificationManager,
notificationIcon: IconCompat
notificationIcon: IconCompat,
playerActivity: Class<Activity>
): Notification {
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 =
NotificationCompat.Builder(service, NEW_PLAYER_MEDIA_NOTIFICATION_CHANNEL_NAME)
.setContentTitle(service.resources.getString(R.string.new_player_name))
.setContentText(service.resources.getString(R.string.playing_in_background))
.setStyle(MediaStyleNotificationHelper.MediaStyle(session))
.setContentIntent(onNotificationClickPendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationBuilder.setSmallIcon(notificationIcon)

View File

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

View File

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