[Android] Remote access: implement the add/delete/rename bookmark API

Nicolas Pomepuy git at videolan.org
Mon Sep 9 15:14:15 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Aug 14 12:10:18 2024 +0200| [bf02bfe196c1abebe74da7e5842f3c12f1539873] | committer: Duncan McNamara

Remote access: implement the add/delete/rename bookmark API

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

 .../videolan/vlc/webserver/RemoteAccessServer.kt   |  7 ++--
 .../videolan/vlc/webserver/TranslationMapping.kt   |  3 ++
 .../webserver/websockets/RemoteAccessWebSockets.kt | 40 ++++++++++++++++++++++
 .../vlc/webserver/websockets/WSIncomingMessage.kt  |  1 +
 4 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
index 748780d786..8ab2ac40db 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
@@ -590,7 +590,7 @@ class RemoteAccessServer(private val context: Context) : PlaybackService.Callbac
                 val sleepTimer = playerSleepTime.value?.time?.time ?: 0L
                 val nowPlaying = NowPlaying(media.title ?: "", media.artist
                         ?: "", service.isPlaying, service.getTime(), service.length, media.id, media.artworkURL
-                        ?: "", media.uri.toString(), getVolume(), speed, sleepTimer, service.isShuffling, service.repeatType, bookmarks = bookmarks.map { WSBookmark(it.title, it.time) }, chapters = chapters.map { WSBookmark(it.name, it.duration) })
+                        ?: "", media.uri.toString(), getVolume(), speed, sleepTimer, service.isShuffling, service.repeatType, bookmarks = bookmarks.map { WSBookmark(it.id, it.title, it.time) }, chapters = chapters.map { WSChapter(it.name, it.duration) })
                 return nowPlaying
 
             }
@@ -743,9 +743,10 @@ class RemoteAccessServer(private val context: Context) : PlaybackService.Callbac
 
     abstract class WSMessage(val type: String)
     data class NowPlaying(val title: String, val artist: String, val playing: Boolean, val progress: Long, val duration: Long, val id: Long, val artworkURL: String, val uri: String, val volume: Int, val speed: Float, val sleepTimer: Long, val shuffle: Boolean, val repeat: Int, val shouldShow: Boolean = PlaylistManager.playingState.value
-            ?: false, val bookmarks:List<WSBookmark> = listOf(), val chapters:List<WSBookmark> = listOf()) : WSMessage("now-playing")
+            ?: false, val bookmarks:List<WSBookmark> = listOf(), val chapters:List<WSChapter> = listOf()) : WSMessage("now-playing")
 
-    data class WSBookmark(val title: String, val time: Long)
+    data class WSBookmark(val id:Long, val title: String, val time: Long)
+    data class WSChapter(val title: String, val time: Long)
 
     data class PlayQueue(val medias: List<PlayQueueItem>) : WSMessage("play-queue")
     data class PlayQueueItem(val id: Long, val title: String, val artist: String, val duration: Long, val artworkURL: String, val playing: Boolean, val resolution: String = "", val path: String = "", val isFolder: Boolean = false, val progress: Long = 0L, val played: Boolean = false, var fileType: String = "", val favorite: Boolean = false)
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 82e5f1d9d9..54c8e5dc18 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
@@ -129,6 +129,9 @@ object TranslationMapping {
         PLAYBACK_SPEED(R.string.playback_speed),
         CHAPTERS(R.string.chapters),
         BOOKMARKS(R.string.bookmarks),
+        NO_BOOKMARK(R.string.no_bookmark),
+        OK(R.string.ok),
+        CANCEL(R.string.cancel),
         NOT_SET(R.string.notset),
     }
 }
\ No newline at end of file
diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt
index 4278ef958b..a20b635f3c 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt
@@ -36,11 +36,15 @@ import io.ktor.server.websocket.webSocket
 import io.ktor.websocket.Frame
 import io.ktor.websocket.close
 import io.ktor.websocket.readText
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.channels.Channel
 import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import org.videolan.medialibrary.Tools
 import org.videolan.tools.AppScope
 import org.videolan.tools.REMOTE_ACCESS_PLAYBACK_CONTROL
 import org.videolan.vlc.PlaybackService
+import org.videolan.vlc.R
 import org.videolan.vlc.webserver.BuildConfig
 import org.videolan.vlc.webserver.RemoteAccessServer
 import org.videolan.vlc.webserver.ssl.SecretGenerator
@@ -189,6 +193,42 @@ object RemoteAccessWebSockets {
                                 if (playbackControlAllowedOrSend(settings)) service?.sleepTimerInterval = sleepTimerEnd
                             }
                         }
+                        "add-bookmark" -> {
+                            incomingMessage.longValue?.let { time ->
+                                service?.currentMediaWrapper?.let {
+                                    AppScope.launch {
+                                        withContext(Dispatchers.IO) {
+                                            val bookmark = it.addBookmark(time)
+                                            bookmark?.setName(context.getString(R.string.bookmark_default_name, Tools.millisToString(service!!.getTime())))
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                        "delete-bookmark" -> {
+                            incomingMessage.longValue?.let { bookmarkTime ->
+                                service?.currentMediaWrapper?.let { media ->
+                                    AppScope.launch {
+                                        withContext(Dispatchers.IO) {
+                                            media.removeBookmark(bookmarkTime)
+                                        }
+                                    }
+                                }
+                            }
+                        }
+            "rename-bookmark" -> {
+                incomingMessage.longValue?.let { bookmarkTime ->
+                    incomingMessage.stringValue?.let { bookmarkName ->
+                        service?.currentMediaWrapper?.let { media ->
+                            AppScope.launch {
+                                withContext(Dispatchers.IO) {
+                                    media.bookmarks.firstOrNull { it.time == bookmarkTime }?.setName(bookmarkName)
+                                }
+                            }
+                        }
+                    }
+                }
+            }
             "play-media" -> {
                 if (playbackControlAllowedOrSend(settings)) service?.playIndex(incomingMessage.id!!) else return false
 
diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/WSIncomingMessage.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/WSIncomingMessage.kt
index 0bb73d67da..198d7aad18 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/WSIncomingMessage.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/WSIncomingMessage.kt
@@ -29,5 +29,6 @@ data class WSIncomingMessage(
             val id: Int?,
             val floatValue: Float? = null,
             val longValue: Long? = null,
+            val stringValue: String? = null,
             val authTicket: String? = null
 )
\ No newline at end of file



More information about the Android mailing list