[Android] Delay MediaParsingService stop

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


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Feb 26 15:21:11 2020 +0100| [06895c92e9311feab5208484a13d3ad9d0997b38] | committer: Nicolas Pomepuy

Delay MediaParsingService stop

Ensure setForeground() is effective before cancelling it.

Fix #1197

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

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

diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 344159f5e..c817bf775 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -366,7 +366,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
     override fun onDiscoveryProgress(entryPoint: String) {
         if (BuildConfig.DEBUG) Log.v(TAG, "onDiscoveryProgress: $entryPoint")
         currentDiscovery = entryPoint
-        notificationActor.safeOffer(Show)
+        if (::notificationActor.isInitialized) notificationActor.safeOffer(Show)
     }
 
     override fun onDiscoveryCompleted(entryPoint: String) {
@@ -376,7 +376,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
     override fun onParsingStatsUpdated(percent: Int) {
         if (BuildConfig.DEBUG) Log.v(TAG, "onParsingStatsUpdated: $percent")
         parsing = percent
-        if (parsing != 100) notificationActor.safeOffer(Show)
+        if (parsing != 100 && ::notificationActor.isInitialized) notificationActor.safeOffer(Show)
     }
 
     override fun onReloadStarted(entryPoint: String) {
@@ -400,9 +400,13 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
             } catch (e: Exception) {
                 if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "${e.cause}")
             }
-            notificationActor.safeOffer(Hide)
-            stopForeground(true)
-            stopService(Intent(applicationContext, MediaParsingService::class.java))
+            if (::notificationActor.isInitialized) notificationActor.safeOffer(Hide)
+            //Delay service stop to ensure service goes foreground.
+            // Otherwise, we get some RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
+            lifecycleScope.launch {
+                delay(100L)
+                stopService(Intent(applicationContext, MediaParsingService::class.java))
+            }
         }
     }
 



More information about the Android mailing list