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

Robert Stone git at videolan.org
Mon Jan 9 07:39:31 UTC 2023


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

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

Fixes #2248

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

 .../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 d8a989c71c..b1e8410aab 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
@@ -90,11 +90,12 @@ 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()
     }
 }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 35cad20530..95fb8c0017 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -897,13 +897,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