[Android] Remote access: add media to playlists

Nicolas Pomepuy git at videolan.org
Tue Jun 25 12:16:39 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Jun 24 08:14:27 2024 +0200| [55c52e3242351ef73038d20b8166619741f98305] | committer: Duncan McNamara

Remote access: add media to playlists

> https://code.videolan.org/videolan/vlc-android/commit/55c52e3242351ef73038d20b8166619741f98305
---

 .../src/main/res/drawable/ic_play_all.xml          |  2 +-
 .../resources/src/main/res/values/strings.xml      |  2 +
 .../videolan/vlc/webserver/RemoteAccessRouting.kt  | 78 ++++++++++++++++++++++
 .../videolan/vlc/webserver/TranslationMapping.kt   |  6 ++
 4 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/application/resources/src/main/res/drawable/ic_play_all.xml b/application/resources/src/main/res/drawable/ic_play_all.xml
index 9a60f5bd91..efafe04cfa 100644
--- a/application/resources/src/main/res/drawable/ic_play_all.xml
+++ b/application/resources/src/main/res/drawable/ic_play_all.xml
@@ -1,4 +1,4 @@
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="48dp"
         android:height="48dp"
         android:viewportWidth="48"
diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index 243a5b811d..a2003f17c9 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -1252,4 +1252,6 @@
     <string name="metered_warn">Warn me (the warning may be missed for audio playback)</string>
     <string name="metered_connection_stopped">Media stopped because the connection is metered</string>
     <string name="metered_connection_warning">Your connection is metered. Fees may apply.</string>
+    <string name="adding">Adding</string>
+    <string name="to">to</string>
 </resources>
diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt
index e9623ba118..8dd167f926 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessRouting.kt
@@ -99,6 +99,7 @@ import org.videolan.tools.livedata.LiveDataset
 import org.videolan.tools.resIdByName
 import org.videolan.vlc.ArtworkProvider
 import org.videolan.vlc.BuildConfig
+import org.videolan.vlc.gui.dialogs.getPlaylistByName
 import org.videolan.vlc.gui.helpers.AudioUtil
 import org.videolan.vlc.gui.helpers.BitmapUtil
 import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
@@ -558,6 +559,83 @@ fun Route.setupRouting(appContext: Context, scope: CoroutineScope) {
             val gson = Gson()
             call.respondText(gson.toJson(result))
         }
+        // Create a new playlist
+        post("/playlist-create") {
+            verifyLogin(settings)
+            if (!settings.servePlaylists(appContext)) {
+                call.respond(HttpStatusCode.Forbidden)
+                return at post
+            }
+
+            val formParameters = try {
+                call.receiveParameters()
+            } catch (e: Exception) {
+                null
+            }
+
+            val name = formParameters?.get("name") ?: call.respond(HttpStatusCode.NoContent)
+            appContext.getFromMl {
+                if (getPlaylistByName(name as String) == null) {
+                    createPlaylist(name, true, false)
+                }
+            }
+
+            call.respondText("")
+        }
+        // Add a media to playlists
+        post("/playlist-add") {
+            verifyLogin(settings)
+            if (!settings.servePlaylists(appContext)) {
+                call.respond(HttpStatusCode.Forbidden)
+                return at post
+            }
+
+            val formParameters = try {
+                call.receiveParameters()
+            } catch (e: Exception) {
+                null
+            }
+
+            val mediaId = formParameters?.get("mediaId")?.toLong()
+            val mediaType = formParameters?.get("mediaType")
+            val playlists = formParameters?.getAll("playlists[]") as List<String>
+            if (mediaId == null || mediaType == null) {
+                call.respond(HttpStatusCode.NoContent)
+                return at post
+            }
+            if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "mediaId: $mediaId, mediaType: $mediaType, playlists: $playlists")
+
+            val medias = appContext.getFromMl {
+               when (mediaType) {
+                    "album" -> getAlbum(mediaId).tracks
+                    "artist" -> getArtist(mediaId).tracks
+                    "genre" -> getGenre(mediaId).tracks
+                    "video-group" -> {
+                        val group = getVideoGroup(mediaId)
+                        group.media(Medialibrary.SORT_DEFAULT, false, false, false, group.mediaCount(), 0)
+                    }
+                    "video-folder" -> {
+                        val folder = getFolder(Folder.TYPE_FOLDER_VIDEO, mediaId)
+                        folder.media(Folder.TYPE_FOLDER_VIDEO, Medialibrary.SORT_DEFAULT, false, false, false, folder.mediaCount(Folder.TYPE_FOLDER_VIDEO), 0)
+                    }
+                    else -> arrayOf(getMedia(mediaId))
+                }
+            } ?: run {
+                call.respond(HttpStatusCode.NoContent)
+                return at post
+            }
+            appContext.getFromMl {
+                playlists.forEach {
+                    val playlist = getPlaylist(it.toLong(), true, false)
+                    medias.forEach {
+                        playlist.append(it.id)
+                    }
+                }
+
+            }
+
+            call.respondText("")
+        }
         // Get an artist details
         get("/artist") {
             verifyLogin(settings)
diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/TranslationMapping.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/TranslationMapping.kt
index 99ef92f9d4..b27df34e15 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/TranslationMapping.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/TranslationMapping.kt
@@ -103,5 +103,11 @@ object TranslationMapping {
         LOCAL_NETWORK(R.string.network_browsing),
         STREAMS(R.string.streams),
         LOADING(R.string.loading),
+        ADD_PLAYLIST(R.string.add_to_playlist),
+        ADDING(R.string.adding),
+        TO(R.string.to),
+        NEW_PLAYLIST(R.string.create_new_playlist),
+        CREATE(R.string.create),
+        ADD(R.string.add),
     }
 }
\ No newline at end of file



More information about the Android mailing list