Commit 63fd93c2 authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Nicolas Pomepuy
Browse files

PlaybackService: force foreground sooner

parent 04d8b8d1
Pipeline #16652 passed with stage
in 3 minutes and 41 seconds
......@@ -459,6 +459,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate() {
dispatcher.onServicePreSuperOnCreate()
forceForeground()
super.onCreate()
setupScope()
NotificationHelper.createNotificationChannels(applicationContext)
......@@ -477,7 +478,6 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
updateHasWidget()
if (!this::mediaSession.isInitialized) initMediaSession()
forceForeground()
val filter = IntentFilter().apply {
priority = Integer.MAX_VALUE
......@@ -589,12 +589,15 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
@TargetApi(Build.VERSION_CODES.O)
private fun forceForeground() {
if (!AndroidUtil.isOOrLater || isForeground) return
val ctx = this@PlaybackService
val ctx = applicationContext
val stopped = PlayerController.playbackState == PlaybackStateCompat.STATE_STOPPED
val notification = if (this::notification.isInitialized && !stopped) notification
else NotificationHelper.createPlaybackNotification(ctx, false,
else {
val pi = if (::playlistManager.isInitialized) sessionPendingIntent else null
NotificationHelper.createPlaybackNotification(ctx, false,
ctx.resources.getString(R.string.loading), "", "", null,
false, true, mediaSession.sessionToken, sessionPendingIntent)
false, true, null, pi)
}
startForeground(3, notification)
isForeground = true
if (stopped) lifecycleScope.launch { hideNotification(true) }
......
......@@ -54,8 +54,8 @@ object NotificationHelper {
fun createPlaybackNotification(ctx: Context, video: Boolean, title: String, artist: String,
album: String, cover: Bitmap?, playing: Boolean, pausable: Boolean,
sessionToken: MediaSessionCompat.Token,
spi: PendingIntent): Notification {
sessionToken: MediaSessionCompat.Token?,
spi: PendingIntent?): Notification {
val piStop = MediaButtonReceiver.buildMediaButtonPendingIntent(ctx, PlaybackStateCompat.ACTION_STOP)
val builder = NotificationCompat.Builder(ctx, PLAYBACK_SERVICE_CHANNEL_ID)
......@@ -71,11 +71,11 @@ object NotificationHelper {
.setOngoing(playing)
.setCategory(NotificationCompat.CATEGORY_TRANSPORT)
.setDeleteIntent(piStop)
.setContentIntent(spi)
.addAction(NotificationCompat.Action(
R.drawable.ic_widget_previous_w, ctx.getString(R.string.previous),
MediaButtonReceiver.buildMediaButtonPendingIntent(ctx,
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
spi?.let { builder.setContentIntent(it) }
if (pausable) {
if (playing) builder.addAction(NotificationCompat.Action(
R.drawable.ic_widget_pause_w, ctx.getString(R.string.pause),
......@@ -94,12 +94,12 @@ object NotificationHelper {
R.drawable.ic_widget_close_w, ctx.getString(R.string.stop), piStop))
if (AndroidDevices.showMediaStyle) {
builder.setStyle(androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(sessionToken)
val mediaStyle = androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setShowCancelButton(true)
.setCancelButtonIntent(piStop)
)
sessionToken?.let { mediaStyle.setMediaSession(it) }
builder.setStyle(mediaStyle)
}
return builder.build()
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment