feat: PC steuern im neuen Stil, Warteschlange, Playlists, dauerhafte Verbindung (1.3.0)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-06 16:20:44 +01:00
co-authored by Claude Sonnet 5.5
parent 968920e7f3
commit 29d38ef2e6
30 changed files with 1584 additions and 342 deletions
@@ -20,6 +20,12 @@ import java.net.SocketTimeoutException
data class PcDevice(val name: String, val host: String, val port: Int = PcRemote.DEFAULT_PORT)
/** Was auf dem PC gerade laeuft (aus der "status"-Antwort, Zeiten in Sekunden). */
/** Ein Titel der Warteschlange am PC. */
data class QueueItem(val title: String, val subtitle: String, val thumbnail: String, val current: Boolean)
/** Eine Playlist der YouTube-Music-Bibliothek am PC. */
data class PcPlaylist(val id: String, val title: String)
data class PcState(
val activeTab: String,
val visibleTab: String,
@@ -31,6 +37,8 @@ data class PcState(
val duration: Double,
val volume: Int,
val liked: Boolean,
/** "NONE", "ALL" oder "ONE" (leer = unbekannt). */
val repeatMode: String = "",
) {
companion object {
val EMPTY = PcState("music", "music", "", "", "", false, 0.0, 0.0, 100, false)
@@ -73,9 +81,45 @@ object PcRemote {
duration = tab?.optDouble("duration")?.takeIf { it.isFinite() } ?: 0.0,
volume = (tab?.optDouble("volume")?.takeIf { it.isFinite() } ?: 100.0).toInt().coerceIn(0, 100),
liked = tab?.optString("likeStatus").equals("LIKE", ignoreCase = true),
repeatMode = tab.text("repeatMode").uppercase(),
)
}
/** "queue"-Antwort -> Titel (Cover in Listengroesse); Eintraege ohne JSON-Objekt werden uebersprungen. */
fun parseQueue(reply: JSONObject): List<QueueItem> {
val array = reply.optJSONArray("queue") ?: return emptyList()
val items = ArrayList<QueueItem>()
for (i in 0 until array.length()) {
val item = array.optJSONObject(i) ?: continue
// Nichts weglassen: der Index muss zur Reihenfolge am PC passen (play_queue_item).
items += QueueItem(
title = item.text("title").trim(),
subtitle = item.text("subtitle").trim(),
thumbnail = sizedCover(item.text("thumbnail").takeIf { it.startsWith("https://") }.orEmpty(), 226),
current = item.optBoolean("current"),
)
}
return items
}
/** "list_playlists"-Antwort -> Playlists. */
fun parsePlaylists(reply: JSONObject): List<PcPlaylist> {
val array = reply.optJSONArray("playlists") ?: return emptyList()
val items = ArrayList<PcPlaylist>()
for (i in 0 until array.length()) {
val item = array.optJSONObject(i) ?: continue
val id = item.text("id").trim()
if (id.isEmpty()) continue
items += PcPlaylist(id, item.text("title").trim().ifEmpty { id })
}
return items
}
/** Ein PC mit Playtube 2.0.x kennt "queue" noch nicht und antwortet mit "unbekannter Befehl". */
fun isUnknownCommand(message: String?): Boolean =
message != null && (message.contains("unbekannter Befehl", ignoreCase = true) ||
message.contains("unknown command", ignoreCase = true))
private val GOOGLE_IMAGE_SIZE = Regex("=(w\\d+-h\\d+|s\\d+)[^/=]*$")
private val YTIMG_SMALL = Regex("/(default|mqdefault)\\.jpg$")
@@ -83,9 +127,12 @@ object PcRemote {
* Das Cover aus der YouTube-Music-Player-Leiste ist nur 60x60 Pixel gross ("=w60-h60-l90-rj") -
* fuer die Fernbedienung in 544x544 anfordern; kleine YouTube-Vorschaubilder durch hqdefault ersetzen.
*/
fun largeCover(url: String): String = when {
fun largeCover(url: String): String = sizedCover(url, 544)
/** Wie [largeCover], aber in [size] x [size] Pixeln (fuer kleine Listenbilder). */
fun sizedCover(url: String, size: Int): String = when {
url.contains("googleusercontent.com") || url.contains("ggpht.com") ->
url.replace(GOOGLE_IMAGE_SIZE, "=w544-h544-l90-rj")
url.replace(GOOGLE_IMAGE_SIZE, "=w$size-h$size-l90-rj")
url.contains("ytimg.com") -> url.replace(YTIMG_SMALL, "/hqdefault.jpg")
else -> url
}