Commit fee78710 authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais
Browse files

PlaybackService: force foreground sooner

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