Browse Source

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

Fixes #2248


(cherry picked from commit a372c900b4)
3.5.x
Robert Stone 4 years ago
committed by Nicolas Pomepuy
parent
commit
0e2476eb07
  1. 5
      application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
  2. 8
      application/vlc-android/src/org/videolan/vlc/PlaybackService.kt

5
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@launchForeground
AppScope.launch(Dispatchers.Main) {
intent.putExtra("foreground", true)
ContextCompat.startForegroundService(ctx, intent)
block()
}
}

8
application/vlc-android/src/org/videolan/vlc/PlaybackService.kt

@ -901,13 +901,15 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
if (isPlayingPopup) return@launch
if (!AndroidUtil.isLolliPopOrLater || playing || audioFocusHelper.lossTransient) {
if (!isForeground) {
this@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@PlaybackService, ServiceCompat.STOP_FOREGROUND_DETACH)
ServiceCompat.stopForeground(ctx, ServiceCompat.STOP_FOREGROUND_DETACH)
isForeground = false
}
NotificationManagerCompat.from(ctx).notify(3, notification)

Loading…
Cancel
Save