[Android] Delay MediaParsingService stop

Geoffrey Métais git at videolan.org
Thu Feb 27 10:32:46 CET 2020


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Feb 27 10:31:30 2020 +0100| [5ad8a104c045ea48d7223a4cb5016b0d880d8f0a] | committer: Geoffrey Métais

Delay MediaParsingService stop

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

 vlc-android/src/org/videolan/vlc/MediaParsingService.kt | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index e43b46c83..e776597cb 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -355,7 +355,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
     override fun onDiscoveryProgress(entryPoint: String) {
         if (BuildConfig.DEBUG) Log.v(TAG, "onDiscoveryProgress: $entryPoint")
         currentDiscovery = entryPoint
-        notificationActor.offer(Show)
+        if (!notificationActor.isClosedForSend) notificationActor.offer(Show)
     }
 
     override fun onDiscoveryCompleted(entryPoint: String) {
@@ -365,7 +365,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
     override fun onParsingStatsUpdated(percent: Int) {
         if (BuildConfig.DEBUG) Log.v(TAG, "onParsingStatsUpdated: $percent")
         parsing = percent
-        if (parsing != 100) notificationActor.offer(Show)
+        if (parsing != 100 && !notificationActor.isClosedForSend) notificationActor.offer(Show)
     }
 
     override fun onReloadStarted(entryPoint: String) {
@@ -383,9 +383,13 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
         if (!medialibrary.isWorking && !serviceLock && !discoverTriggered) {
             lastNotificationTime = 0L
             if (wakeLock.isHeld) wakeLock.release()
-            stopForeground(true)
-            notificationActor.offer(Hide)
-            stopService(Intent(applicationContext, MediaParsingService::class.java))
+            if (!notificationActor.isClosedForSend) notificationActor.offer(Hide)
+            // Delay service stop to ensure service goes foreground.
+            // Otherwise, we get some RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
+            launch {
+                delay(100L)
+                stopService(Intent(applicationContext, MediaParsingService::class.java))
+            }
         }
     }
 



More information about the Android mailing list