[Android] Invoke launchForeground prior to startForeground to keep PlaybackService alive in driving mode

Robert Stone git at videolan.org
Mon Jan 9 07:40:06 UTC 2023


vlc-android | branch: 3.5.x | Robert Stone <rhstone at gmail.com> | Fri Jan  6 09:38:07 2023 -0800| [0e2476eb079612c75ff476b267510c09be5c9b6b] | committer: Nicolas Pomepuy

Invoke launchForeground prior to startForeground to keep PlaybackService alive in driving mode

Fixes #2248


(cherry picked from commit a372c900b4bd9f531f1589941836894ab875d40b)

> https://code.videolan.org/videolan/vlc-android/commit/0e2476eb079612c75ff476b267510c09be5c9b6b
---

 .../src/main/java/org/videolan/resources/util/Extensions.kt       | 5 +++--
 application/vlc-android/src/org/videolan/vlc/PlaybackService.kt   | 8 +++++---
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
index ebbb18e913..d47164b08e 100644
--- a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
+++ b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
@@ -86,10 +86,11 @@ suspend fun Context.dbExists(coroutineContextProvider: CoroutineContextProvider
     File(getDir("db", Context.MODE_PRIVATE).toString() + Medialibrary.VLC_MEDIA_DB_NAME).exists()
 }
 
-fun Context.launchForeground(intent: Intent) {
-    val ctx = this
+fun Context.launchForeground(intent: Intent, block: () -> Unit = {}) {
+    val ctx = this at launchForeground
     AppScope.launch(Dispatchers.Main) {
         intent.putExtra("foreground", true)
         ContextCompat.startForegroundService(ctx, intent)
+        block()
     }
 }
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 807408e516..0d6d96b0b0 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -901,13 +901,15 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
                     if (isPlayingPopup) return at launch
                     if (!AndroidUtil.isLolliPopOrLater || playing || audioFocusHelper.lossTransient) {
                         if (!isForeground) {
-                            this at PlaybackService.startForeground(3, notification)
-                            isForeground = true
+                            ctx.launchForeground(Intent(ctx, PlaybackService::class.java)) {
+                                ctx.startForeground(3, notification)
+                                isForeground = true
+                            }
                         } else
                             NotificationManagerCompat.from(ctx).notify(3, notification)
                     } else {
                         if (isForeground) {
-                            ServiceCompat.stopForeground(this at PlaybackService, ServiceCompat.STOP_FOREGROUND_DETACH)
+                            ServiceCompat.stopForeground(ctx, ServiceCompat.STOP_FOREGROUND_DETACH)
                             isForeground = false
                         }
                         NotificationManagerCompat.from(ctx).notify(3, notification)



More information about the Android mailing list