[Android] Stop service if loadLastPlaylist fails

Geoffrey Métais git at videolan.org
Thu May 3 14:17:14 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu May  3 14:14:50 2018 +0200| [7340ca334e1a531e5aacc138f21c4a45de3557ad] | committer: Geoffrey Métais

Stop service if loadLastPlaylist fails

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

 .../src/org/videolan/vlc/PlaybackService.kt        |  4 ++--
 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 26 ++++++++++++++--------
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 3d0a2ab1e..ecd511ada 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -959,9 +959,9 @@ class PlaybackService : MediaBrowserServiceCompat() {
     private fun loadLastAudioPlaylist() {
         if (AndroidDevices.isAndroidTv) return
         if (medialibrary.isInitiated && libraryReceiver == null)
-            playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO)
+            if (!playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO)) stopSelf()
         else
-            registerMedialibrary(Runnable { playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO) })
+            registerMedialibrary(Runnable { if (!playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO)) stopSelf() })
     }
 
     fun loadLastPlaylist(type: Int) {
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 5f0020ac0..096e1a6ac 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -20,6 +20,7 @@ import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
+import org.videolan.vlc.R.string.audio
 import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.gui.preferences.PreferencesActivity
 import org.videolan.vlc.gui.preferences.PreferencesFragment
@@ -118,18 +119,24 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
     @Volatile
     private var loadingLastPlaylist = false
-    fun loadLastPlaylist(type: Int) {
-        if (loadingLastPlaylist) return
+    fun loadLastPlaylist(type: Int) : Boolean {
+        if (loadingLastPlaylist) return true
         loadingLastPlaylist = true
+        val audio = type == Constants.PLAYLIST_TYPE_AUDIO
+        val currentMedia = settings.getString(if (audio) "current_song" else "current_media", "")
+        if (currentMedia.isEmpty()) {
+            loadingLastPlaylist = false
+            return false
+        }
+        val locations = settings.getString(if (audio) "audio_list" else "media_list", "").split(" ".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
+        if (Util.isArrayEmpty(locations)) {
+            loadingLastPlaylist = false
+            return false
+        }
         launch(UI, CoroutineStart.UNDISPATCHED) {
-            val audio = type == Constants.PLAYLIST_TYPE_AUDIO
-            val currentMedia = settings.getString(if (audio) "current_song" else "current_media", "")
-            if ("" == currentMedia) return at launch
-            val locations = settings.getString(if (audio) "audio_list" else "media_list", "").split(" ".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
-            if (Util.isArrayEmpty(locations)) return at launch
-            val playList = async {
+            val playList = withContext(CommonPool) {
                 locations.map { Uri.decode(it) }.mapTo(ArrayList(locations.size)) { MediaWrapper(Uri.parse(it)) }
-            }.await()
+            }
             // load playlist
             shuffling = settings.getBoolean(if (audio) "audio_shuffling" else "media_shuffling", false)
             repeating = settings.getInt(if (audio) "audio_repeating" else "media_repeating", Constants.REPEAT_NONE)
@@ -145,6 +152,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
             load(playList, position)
             loadingLastPlaylist = false
         }
+        return true
     }
 
     private fun onPlaylistLoaded() {



More information about the Android mailing list