Modern Apps git

← Modern-Apps.git

sdk/cast: now-playing omits empty resource ids instead of naming a phantom one

Commit
60fe00d06b1a0886e196106d00374447991797fa
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:25 UTC (5 days ago)
Parents
9402fcb
CastContract defines absent vs present KEY_RESOURCE_ID as
distinct meanings: present names the playing media item and
the receiver renders the snapshot only while that resource
plays (discarding it otherwise); absent means no resource to
name and the TV renders at once. setNowPlaying always wrote
the key โ€” with the default empty string for a Surface
session โ€” so a now-playing of 'Song / Singer' named a
resource that is never playing and the receiver discarded
it per contract. Artwork had the same flaw.

The client now puts each id only when non-empty, restoring
the documented wire semantics (receivers see null, the
documented absent case โ€” IPC stays backward-compatible, no
contract change). Regression test pins a Surface session
omitting both keys while named ids still carry. The games
SDK was audited and cleared. Verified: :sdk:cast:
testDebugUnitTest and lint BUILD SUCCESSFUL.

Diff

sdk/cast/src/main/java/com/vayunmathur/sdk/cast/CastClient.kt
diff --git a/sdk/cast/src/main/java/com/vayunmathur/sdk/cast/CastClient.kt b/sdk/cast/src/main/java/com/vayunmathur/sdk/cast/CastClient.ktindex 80e489be06..0ac72cd803 100644--- a/sdk/cast/src/main/java/com/vayunmathur/sdk/cast/CastClient.kt+++ b/sdk/cast/src/main/java/com/vayunmathur/sdk/cast/CastClient.kt@@ -448,11 +448,18 @@ class CastClient(context: Context) {         val remote = service ?: return         val message = Message.obtain(null, CastContract.MSG_SET_NOW_PLAYING).apply {             data = Bundle().apply {-                putString(CastContract.KEY_RESOURCE_ID, resourceId)+                // Absent, not empty, is what means "no resource to name" (see+                // CastContract's KEY_RESOURCE_ID notes): a present-but-empty id+                // names a resource that is never playing, so the snapshot would+                // be discarded instead of rendered. Same for the artwork id,+                // where absent means "no cover".+                if (resourceId.isNotEmpty()) putString(CastContract.KEY_RESOURCE_ID, resourceId)                 putString(CastContract.KEY_TITLE, title)                 putString(CastContract.KEY_AUTHOR, author)                 putString(CastContract.KEY_ALBUM, album)-                putString(CastContract.KEY_ARTWORK_RESOURCE_ID, artworkResourceId)+                if (artworkResourceId.isNotEmpty()) {+                    putString(CastContract.KEY_ARTWORK_RESOURCE_ID, artworkResourceId)+                }                 // Primitive arrays rather than a list of a small class, for the reason                 // `PlaybackAction` is an int on the wire: a `Serializable` in a `Bundle` is a                 // class-loading problem waiting for a client built against a different SDK version.
sdk/cast/src/test/java/com/vayunmathur/sdk/cast/CastClientSessionTest.kt
diff --git a/sdk/cast/src/test/java/com/vayunmathur/sdk/cast/CastClientSessionTest.kt b/sdk/cast/src/test/java/com/vayunmathur/sdk/cast/CastClientSessionTest.ktindex 7e88a603ba..719c83204d 100644--- a/sdk/cast/src/test/java/com/vayunmathur/sdk/cast/CastClientSessionTest.kt+++ b/sdk/cast/src/test/java/com/vayunmathur/sdk/cast/CastClientSessionTest.kt@@ -292,6 +292,44 @@ class CastClientSessionTest {         deleteOnExit()     } +    @Test fun nowPlayingWithoutResourceOmitsResourceAndArtworkIds() = runBlocking {+        installCast(CastContract.MIN_CAST_VERSION_CODE)+        bindTo(fakeService { replyTo ->+            val ready = Message.obtain(null, CastContract.MSG_SESSION_READY).apply {+                data = readyBundle(withSurface = true)+            }+            replyTo!!.send(ready)+        })+        val client = CastClient(context)+        val deferred = async(Dispatchers.Default) { client.openSession(1280, 720, wantAudio = true) }+        var guard = 0+        var session: CastClient.Session? = null+        while (session == null && guard++ < 200) {+            idleMain()+            session = withTimeoutOrNull(50) { deferred.await() }+            if (session == null) Thread.sleep(10)+        }+        assertNotNull(session)+        // A Surface session has no resource to name: the ids must be absent,+        // not empty, or the receiver discards the snapshot as naming a+        // resource that is not playing (CastContract KEY_RESOURCE_ID notes).+        client.setNowPlaying(title = "Song", author = "Singer")+        idleMain()+        val surfaceNowPlaying = received.first { it.what == CastContract.MSG_SET_NOW_PLAYING }+        assertEquals("Song", surfaceNowPlaying.data.getString(CastContract.KEY_TITLE))+        assertFalse(surfaceNowPlaying.data.containsKey(CastContract.KEY_RESOURCE_ID))+        assertFalse(surfaceNowPlaying.data.containsKey(CastContract.KEY_ARTWORK_RESOURCE_ID))+        // A named resource/artwork still travels, for a content session.+        received.clear()+        client.setNowPlaying(title = "Song", resourceId = "track-1", artworkResourceId = "cover-1")+        idleMain()+        val namedNowPlaying = received.first { it.what == CastContract.MSG_SET_NOW_PLAYING }+        assertEquals("track-1", namedNowPlaying.data.getString(CastContract.KEY_RESOURCE_ID))+        assertEquals("cover-1", namedNowPlaying.data.getString(CastContract.KEY_ARTWORK_RESOURCE_ID))+        client.close()+        Unit+    }+     @Test fun contentSessionSendsFullOutgoingBundles() = runBlocking {         val fixture = openContent(tempResourceFile())         val client = fixture.client