[Android] PlaybackService: force foreground sooner

Geoffrey Métais git at videolan.org
Wed Apr 29 12:57:03 CEST 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Apr 28 15:25:46 2020 +0200| [63fd93c278f3de4a717eeafd4ba15ef4c7a519d5] | committer: Nicolas Pomepuy

PlaybackService: force foreground sooner

> https://code.videolan.org/videolan/vlc-android/commit/63fd93c278f3de4a717eeafd4ba15ef4c7a519d5
---

 .../vlc-android/src/org/videolan/vlc/PlaybackService.kt      | 11 +++++++----
 .../src/org/videolan/vlc/gui/helpers/NotificationHelper.kt   | 12 ++++++------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 6120892d8..e1d9505a6 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -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 at 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) }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
index 7eddacc95..094a94566 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
@@ -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()
     }



More information about the Android mailing list