[Android] Voice actions: add playlists support

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


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

Voice actions: add playlists support

(cherry picked from commit 813db590769e4e089cbdc17e0632352e8467db2b)

> https://code.videolan.org/videolan/vlc-android/commit/6c9819920de26b89d7877323d1ffaeca6cea1112
---

 .../src/org/videolan/vlc/MediaSessionCallback.kt   | 30 +++++++++++-----------
 .../org/videolan/vlc/util/VoiceSearchParams.java   |  8 ++++++
 2 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index f46e204c5..941d2f1af 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) {
-            val vsp = VoiceSearchParams(query, extras)
+            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.java b/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.java
index eeab26817..df270ece3 100644
--- a/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.java
+++ b/vlc-android/src/org/videolan/vlc/util/VoiceSearchParams.java
@@ -37,10 +37,12 @@ public class VoiceSearchParams {
     public boolean isGenreFocus;
     public boolean isArtistFocus;
     public boolean isAlbumFocus;
+    public boolean isPlaylistFocus;
     public boolean isSongFocus;
     public String genre;
     public String artist;
     public String album;
+    public String playlist;
     public String song;
 
     public VoiceSearchParams(String query, Bundle extras) {
@@ -70,6 +72,10 @@ public class VoiceSearchParams {
                     genre = extras.getString(genreKey);
                     artist = extras.getString(MediaStore.EXTRA_MEDIA_ARTIST);
                     break;
+                case MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE:
+                    isPlaylistFocus = true;
+                    playlist = extras.getString(MediaStore.EXTRA_MEDIA_PLAYLIST);
+                    break;
                 case MediaStore.Audio.Media.ENTRY_CONTENT_TYPE:
                     isSongFocus = true;
                     song = extras.getString(MediaStore.EXTRA_MEDIA_TITLE);
@@ -90,10 +96,12 @@ public class VoiceSearchParams {
             + " isGenreFocus=" + isGenreFocus
             + " isArtistFocus=" + isArtistFocus
             + " isAlbumFocus=" + isAlbumFocus
+            + " isPlaylistFocus=" + isPlaylistFocus
             + " isSongFocus=" + isSongFocus
             + " genre=" + genre
             + " artist=" + artist
             + " album=" + album
+            + " playlist=" + playlist
             + " song=" + song;
     }
 }



More information about the Android mailing list