[Android] Voice actions: add playlists support

Geoffrey Métais git at videolan.org
Wed May 22 18:07:39 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed May 22 18:07:16 2019 +0200| [813db590769e4e089cbdc17e0632352e8467db2b] | committer: Geoffrey Métais

Voice actions: add playlists support

> https://code.videolan.org/videolan/vlc-android/commit/813db590769e4e089cbdc17e0632352e8467db2b
---

 .../src/org/videolan/vlc/MediaSessionCallback.kt   | 28 +++++++++----------
 .../src/org/videolan/vlc/util/VoiceSearchParams.kt | 31 ++++++++++++++--------
 2 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index 45c096334..c96aaec5b 100644
--- a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -6,8 +6,10 @@ import android.os.Bundle
 import android.support.v4.media.session.MediaSessionCompat
 import android.support.v4.media.session.PlaybackStateCompat
 import android.view.KeyEvent
-import kotlinx.coroutines.*
-import org.videolan.medialibrary.Tools
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.launch
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.extensions.ExtensionsManager
@@ -89,33 +91,31 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
     override fun onPlayFromUri(uri: Uri?, extras: Bundle?) = playbackService.loadUri(uri)
 
     override fun onPlayFromSearch(query: String?, extras: Bundle?) {
-        if (!playbackService.medialibrary.isInitiated || playbackService.libraryReceiver != null) {
-            playbackService.registerMedialibrary(Runnable { onPlayFromSearch(query, extras) })
-            return
-        }
         playbackService.mediaSession.setPlaybackState(PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_CONNECTING, playbackService.time, 1.0f).build())
         AppScope.launch(Dispatchers.IO) {
+            playbackService.getFromMl { isStarted }
             val vsp = VoiceSearchParams(query ?: "", extras)
             var items: Array<out MediaLibraryItem>? = null
             var tracks: Array<MediaWrapper>? = null
             when {
                 vsp.isAny -> {
-                    items = playbackService.medialibrary.audio
-                    if (!playbackService.isShuffling) playbackService.shuffle()
+                    items = playbackService.medialibrary.audio.also { if (!playbackService.isShuffling) playbackService.shuffle() }
                 }
                 vsp.isArtistFocus -> items = playbackService.medialibrary.searchArtist(vsp.artist)
                 vsp.isAlbumFocus -> items = playbackService.medialibrary.searchAlbum(vsp.album)
                 vsp.isGenreFocus -> items = playbackService.medialibrary.searchGenre(vsp.genre)
-                vsp.isSongFocus -> tracks = playbackService.medialibrary.searchMedia(vsp.song)!!
+                vsp.isPlaylistFocus -> items = playbackService.medialibrary.searchPlaylist(vsp.playlist)
+                vsp.isSongFocus -> tracks = playbackService.medialibrary.searchMedia(vsp.song)
             }
-            if (Tools.isArrayEmpty(tracks) && query !== null && query.length > 2) playbackService.medialibrary.search(query)?.run {
+            if (tracks.isNullOrEmpty() && items.isNullOrEmpty() && query?.length ?: 0 > 2) playbackService.medialibrary.search(query)?.run {
                 when {
-                    !Tools.isArrayEmpty(albums) -> tracks = albums[0].tracks
-                    !Tools.isArrayEmpty(artists) -> tracks = artists[0].tracks
-                    !Tools.isArrayEmpty(genres) -> tracks = genres[0].tracks
+                    !albums.isNullOrEmpty() -> tracks = albums[0].tracks
+                    !artists.isNullOrEmpty() -> tracks = artists[0].tracks
+                    !playlists.isNullOrEmpty() -> tracks = playlists[0].tracks
+                    !genres.isNullOrEmpty() -> tracks = genres[0].tracks
                 }
             }
-            if (tracks == null && !items.isNullOrEmpty()) tracks = items[0].tracks
+            if (tracks.isNullOrEmpty() && !items.isNullOrEmpty()) tracks = items[0].tracks
             if (!tracks.isNullOrEmpty()) playbackService.load(tracks, 0)
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.kt b/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.kt
index 853e5e503..7c4b7e5b7 100644
--- a/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.kt
+++ b/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.kt
@@ -35,10 +35,12 @@ class VoiceSearchParams(val query: String, extras: Bundle?) {
     var isGenreFocus: Boolean = false
     var isArtistFocus: Boolean = false
     var isAlbumFocus: Boolean = false
+    var isPlaylistFocus: Boolean = false
     var isSongFocus: Boolean = false
     var genre: String? = null
     var artist: String? = null
     var album: String? = null
+    var playlist: String? = null
     var song: String? = null
 
     init {
@@ -69,6 +71,10 @@ class VoiceSearchParams(val query: String, extras: Bundle?) {
                     genre = extras.getString(genreKey)
                     artist = extras.getString(MediaStore.EXTRA_MEDIA_ARTIST)
                 }
+                MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE -> {
+                    isPlaylistFocus = true
+                    playlist = extras.getString(MediaStore.EXTRA_MEDIA_PLAYLIST)
+                }
                 MediaStore.Audio.Media.ENTRY_CONTENT_TYPE -> {
                     isSongFocus = true
                     song = extras.getString(MediaStore.EXTRA_MEDIA_TITLE)
@@ -83,16 +89,19 @@ class VoiceSearchParams(val query: String, extras: Bundle?) {
     }
 
     override fun toString(): String {
-        return ("query=" + query
-                + " isAny=" + isAny
-                + " isUnstructured=" + isUnstructured
-                + " isGenreFocus=" + isGenreFocus
-                + " isArtistFocus=" + isArtistFocus
-                + " isAlbumFocus=" + isAlbumFocus
-                + " isSongFocus=" + isSongFocus
-                + " genre=" + genre
-                + " artist=" + artist
-                + " album=" + album
-                + " song=" + song)
+        return ("""
+            query=$query
+            isAny=$isAny
+            isUnstructured=$isUnstructured
+            isGenreFocus=$isGenreFocus
+            isArtistFocus=$isArtistFocus
+            isAlbumFocus=$isAlbumFocus
+            isPlaylistFocus=$isPlaylistFocus
+            isSongFocus=$isSongFocus
+            genre=$genre
+            artist=$artist
+            album=$album
+            playlist=$playlist
+            song=$song""".trimIndent())
     }
 }



More information about the Android mailing list