[Android] PlaybackService: Replace old callbacks
Geoffrey Métais
git at videolan.org
Wed Dec 18 14:19:48 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Dec 18 14:18:33 2019 +0100| [0357ec73403b15a9882e7021cf997169c7883ded] | committer: Geoffrey Métais
PlaybackService: Replace old callbacks
Fix #1129
> https://code.videolan.org/videolan/vlc-android/commit/0357ec73403b15a9882e7021cf997169c7883ded
---
.../src/org/videolan/vlc/MediaSessionCallback.kt | 2 +-
.../src/org/videolan/vlc/PlaybackService.kt | 15 +++--
.../src/org/videolan/vlc/util/Kextensions.kt | 2 +
.../videolan/vlc/util/PBSMedialibraryReceiver.kt | 64 ----------------------
4 files changed, 12 insertions(+), 71 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index 66e484d60..1ae84bed9 100644
--- a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -91,7 +91,7 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
playbackService.mediaSession.setPlaybackState(PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_CONNECTING, playbackService.time, 1.0f).build())
playbackService.scope.launch(Dispatchers.IO) {
if (!isActive) return at launch
- playbackService.getFromMl { isStarted }
+ playbackService.awaitMedialibraryStarted()
val vsp = VoiceSearchParams(query ?: "", extras)
var items: Array<out MediaLibraryItem>? = null
var tracks: Array<AbstractMediaWrapper>? = null
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 10bca2faf..a51624510 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -108,8 +108,6 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
private var widgetPositionTimestamp = System.currentTimeMillis()
private var popupManager: PopupManager? = null
- internal var libraryReceiver: PBSMedialibraryReceiver? = null
-
private val receiver = object : BroadcastReceiver() {
private var wasPlaying = false
override fun onReceive(context: Context, intent: Intent) {
@@ -466,7 +464,6 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
Util.checkCpuCompatibility(this)
medialibrary = AbstractMedialibrary.getInstance()
- if (!medialibrary.isInitiated) registerMedialibrary(null)
detectHeadset = settings.getBoolean("enable_headset_detection", true)
@@ -990,11 +987,17 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
private fun loadLastAudioPlaylist() {
if (AndroidDevices.isAndroidTv) return
- runOnceReady(Runnable { if (!playlistManager.loadLastPlaylist()) stopSelf() })
+ scope.launch {
+ awaitMedialibraryStarted()
+ if (!playlistManager.loadLastPlaylist()) stopSelf()
+ }
}
fun loadLastPlaylist(type: Int) {
- runOnceReady(Runnable { playlistManager.loadLastPlaylist(type) })
+ scope.launch {
+ awaitMedialibraryStarted()
+ playlistManager.loadLastPlaylist(type)
+ }
}
fun showToast(text: String, duration: Int, isError: Boolean = false) {
@@ -1294,7 +1297,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
override fun onLoadChildren(parentId: String, result: Result<List<MediaBrowserCompat.MediaItem>>) {
result.detach()
scope.launch(start = CoroutineStart.UNDISPATCHED) {
- getFromMl { isStarted }
+ awaitMedialibraryStarted()
sendResults(result, parentId)
}
}
diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index da164fe1e..f9cc40c2f 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -122,6 +122,8 @@ suspend inline fun <reified T> Context.getFromMl(crossinline block: AbstractMedi
}
}
+suspend fun Context.awaitMedialibraryStarted() = getFromMl { isStarted }
+
@WorkerThread
fun List<AbstractMediaWrapper>.updateWithMLMeta() : MutableList<AbstractMediaWrapper> {
val ml = AbstractMedialibrary.getInstance()
diff --git a/vlc-android/src/org/videolan/vlc/util/PBSMedialibraryReceiver.kt b/vlc-android/src/org/videolan/vlc/util/PBSMedialibraryReceiver.kt
deleted file mode 100644
index defc8c40d..000000000
--- a/vlc-android/src/org/videolan/vlc/util/PBSMedialibraryReceiver.kt
+++ /dev/null
@@ -1,64 +0,0 @@
-/*****************************************************************************
- * PBSMedialibraryReceiver.kt
- *****************************************************************************
- * Copyright © 2018 VLC authors and VideoLAN
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
-
-package org.videolan.vlc.util
-
-import android.content.BroadcastReceiver
-import android.content.Context
-import android.content.Intent
-import android.content.IntentFilter
-import androidx.localbroadcastmanager.content.LocalBroadcastManager
-import org.videolan.vlc.PlaybackService
-import org.videolan.vlc.VLCApplication
-import org.videolan.vlc.startMedialibrary
-import java.util.*
-
-internal class PBSMedialibraryReceiver(private val service: PlaybackService) : BroadcastReceiver() {
- private val pendingActions by lazy(LazyThreadSafetyMode.NONE) { LinkedList<Runnable>() }
-
- init {
- LocalBroadcastManager.getInstance(service).registerReceiver(this, IntentFilter(VLCApplication.ACTION_MEDIALIBRARY_READY))
- }
-
- override fun onReceive(context: Context, intent: Intent) {
- service.libraryReceiver = null
- LocalBroadcastManager.getInstance(service).unregisterReceiver(this)
- for (r in pendingActions) r.run()
- }
-
- fun addAction(r: Runnable) = pendingActions.add(r)
-}
-
-internal fun PlaybackService.registerMedialibrary(action: Runnable?) {
- if (!Permissions.canReadStorage(this)) return
- if (libraryReceiver == null) {
- libraryReceiver = PBSMedialibraryReceiver(this)
- startMedialibrary(parse = false)
- }
- if (action != null) libraryReceiver?.addAction(action)
-}
-
-internal fun PlaybackService.runOnceReady(action: Runnable) {
- when {
- medialibrary.isInitiated -> action.run()
- libraryReceiver == null -> registerMedialibrary(action)
- else -> libraryReceiver?.addAction(action)
- }
-}
\ No newline at end of file
More information about the Android
mailing list