Modern Apps git

← Modern-Apps.git

music: updated automated screenshot generation

Commit
81479089b0faeddb7180d86555f191958e62d67f
Author
Vayun Mathur <[email protected]>
When
2026-07-07 05:32 UTC (5 months ago)
Parents
17ab519

Diff

metadata_data/photos/music/1.png
diff --git a/metadata_data/photos/music/1.png b/metadata_data/photos/music/1.pngindex 83aa2304a0..dcc3bdcd37 100644Binary files a/metadata_data/photos/music/1.png and b/metadata_data/photos/music/1.png differ
metadata_data/photos/music/2.png
diff --git a/metadata_data/photos/music/2.png b/metadata_data/photos/music/2.pngnew file mode 100644index 0000000000..3cb133c4e9Binary files /dev/null and b/metadata_data/photos/music/2.png differ
metadata_data/photos/music/3.png
diff --git a/metadata_data/photos/music/3.png b/metadata_data/photos/music/3.pngnew file mode 100644index 0000000000..3919182af4Binary files /dev/null and b/metadata_data/photos/music/3.png differ
music/src/androidTest/java/com/vayunmathur/music/MetadataScreenshots.kt
diff --git a/music/src/androidTest/java/com/vayunmathur/music/MetadataScreenshots.kt b/music/src/androidTest/java/com/vayunmathur/music/MetadataScreenshots.ktindex 1f1c4479f1..ae839b64ef 100644--- a/music/src/androidTest/java/com/vayunmathur/music/MetadataScreenshots.kt+++ b/music/src/androidTest/java/com/vayunmathur/music/MetadataScreenshots.kt@@ -1,11 +1,15 @@ package com.vayunmathur.music  import android.content.ContentValues+import android.media.MediaScannerConnection+import android.os.Environment import android.provider.MediaStore import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.junit4.createEmptyComposeRule+import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.onRoot+import androidx.compose.ui.test.performClick import androidx.test.core.app.ActivityScenario import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry@@ -16,13 +20,15 @@ import kotlinx.coroutines.runBlocking import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith+import java.io.ByteArrayOutputStream import java.io.File-import java.nio.ByteBuffer-import java.nio.ByteOrder+import java.util.concurrent.CountDownLatch+import java.util.concurrent.TimeUnit  /**- * Screenshot generator driven by `:music:metadata`. Seeds a few short WAV tracks (with- * title/artist/album metadata) into MediaStore, indexes them, then captures the library.+ * Screenshot generator driven by `:music:metadata`. Seeds a few short, TAGGED MP3s (ID3v2+ * title/artist/album) into MediaStore so the scanner surfaces real metadata, indexes them,+ * then captures the Songs / Albums / Artists tabs.  */ @RunWith(AndroidJUnit4::class) class MetadataScreenshots {@@ -46,18 +52,53 @@ class MetadataScreenshots {         }     } -    /** A valid 1-second silent mono 16-bit PCM WAV so MediaStore treats it as playable audio. */-    private fun silentWav(): ByteArray {-        val sampleRate = 8000-        val numSamples = sampleRate // 1 second-        val dataSize = numSamples * 2-        val buf = ByteBuffer.allocate(44 + dataSize).order(ByteOrder.LITTLE_ENDIAN)-        buf.put("RIFF".toByteArray()); buf.putInt(36 + dataSize); buf.put("WAVE".toByteArray())-        buf.put("fmt ".toByteArray()); buf.putInt(16); buf.putShort(1); buf.putShort(1)-        buf.putInt(sampleRate); buf.putInt(sampleRate * 2); buf.putShort(2); buf.putShort(16)-        buf.put("data".toByteArray()); buf.putInt(dataSize)-        // data left as zeros (silence)-        return buf.array()+    // --- Tagged MP3 synthesis -------------------------------------------------++    private fun syncsafe(n: Int) = byteArrayOf(+        ((n ushr 21) and 0x7F).toByte(),+        ((n ushr 14) and 0x7F).toByte(),+        ((n ushr 7) and 0x7F).toByte(),+        (n and 0x7F).toByte(),+    )++    private fun be32(n: Int) = byteArrayOf(+        (n ushr 24).toByte(), (n ushr 16).toByte(), (n ushr 8).toByte(), n.toByte(),+    )++    /** An ID3v2.3 text frame (ISO-8859-1 encoding byte + ASCII text). */+    private fun textFrame(id: String, text: String): ByteArray {+        val data = byteArrayOf(0x00) + text.toByteArray(Charsets.ISO_8859_1)+        return id.toByteArray(Charsets.US_ASCII) + be32(data.size) + byteArrayOf(0, 0) + data+    }++    /**+     * A minimal valid MP3: an ID3v2.3 tag (so MediaStore's scanner reads real+     * title/artist/album) followed by ~1s of silent MPEG-1 Layer III frames+     * (128 kbps, 44.1 kHz, stereo). All-zero frame bodies decode to silence.+     */+    private fun taggedMp3(title: String, artist: String, album: String): ByteArray {+        val tagBody = textFrame("TIT2", title) + textFrame("TPE1", artist) + textFrame("TALB", album)+        val id3 = "ID3".toByteArray(Charsets.US_ASCII) ++            byteArrayOf(0x03, 0x00, 0x00) + syncsafe(tagBody.size) + tagBody++        val frameHeader = byteArrayOf(0xFF.toByte(), 0xFB.toByte(), 0x90.toByte(), 0x00)+        val frame = frameHeader + ByteArray(FRAME_SIZE - frameHeader.size)+        val out = ByteArrayOutputStream()+        out.write(id3)+        repeat(NUM_FRAMES) { out.write(frame) }+        return out.toByteArray()+    }++    // --- MediaStore seeding ---------------------------------------------------++    /** Remove tracks seeded by a previous run so reruns don't accumulate duplicates. */+    private fun deleteSeeded() {+        val collection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)+        ctx.contentResolver.delete(+            collection,+            "${MediaStore.Audio.Media.RELATIVE_PATH} LIKE ?",+            arrayOf("$REL_PATH%"),+        )     }      private fun seedMusic() {@@ -73,31 +114,41 @@ class MetadataScreenshots {         )         val resolver = ctx.contentResolver         val collection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)-        val wav = silentWav()+        val scanPaths = mutableListOf<String>()         tracks.forEachIndexed { i, t ->+            val fileName = "metadata_${i + 1}.mp3"             val values = ContentValues().apply {-                put(MediaStore.Audio.Media.DISPLAY_NAME, "${t.title}.wav")-                put(MediaStore.Audio.Media.MIME_TYPE, "audio/x-wav")-                put(MediaStore.Audio.Media.RELATIVE_PATH, "Music/Metadata")-                put(MediaStore.Audio.Media.TITLE, t.title)-                put(MediaStore.Audio.Media.ARTIST, t.artist)-                put(MediaStore.Audio.Media.ALBUM, t.album)-                put(MediaStore.Audio.Media.TRACK, i + 1)+                put(MediaStore.Audio.Media.DISPLAY_NAME, fileName)+                put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg")+                put(MediaStore.Audio.Media.RELATIVE_PATH, REL_PATH)                 put(MediaStore.Audio.Media.IS_MUSIC, 1)                 put(MediaStore.Audio.Media.IS_PENDING, 1)             }             val uri = resolver.insert(collection, values) ?: return@forEachIndexed-            resolver.openOutputStream(uri)!!.use { it.write(wav) }+            resolver.openOutputStream(uri)!!.use { it.write(taggedMp3(t.title, t.artist, t.album)) }             values.clear()             values.put(MediaStore.Audio.Media.IS_PENDING, 0)             resolver.update(uri, values, null, null)+            scanPaths += File(+                Environment.getExternalStorageDirectory(),+                "$REL_PATH$fileName",+            ).absolutePath+        }++        // Force the media scanner to parse the ID3 tags before we index, so+        // TITLE/ARTIST/ALBUM are populated (not "<unknown>") when syncMusic runs.+        val latch = CountDownLatch(scanPaths.size)+        MediaScannerConnection.scanFile(ctx, scanPaths.toTypedArray(), null) { _, _ ->+            latch.countDown()         }+        latch.await(20, TimeUnit.SECONDS)     }      private data class Track(val title: String, val artist: String, val album: String)      @Test     fun generateStoreScreenshots() {+        deleteSeeded()         seedMusic()         // Index MediaStore into the app's Room DB directly (shares the cached instance         // the app uses), so the library populates deterministically.@@ -106,7 +157,25 @@ class MetadataScreenshots {          ActivityScenario.launch(MainActivity::class.java).use {             Thread.sleep(4000)-            snap(1)+            snap(1) // Songs++            composeRule.onNodeWithText("Albums").performClick()+            composeRule.waitForIdle()+            Thread.sleep(1500)+            snap(2) // Albums++            composeRule.onNodeWithText("Artists").performClick()+            composeRule.waitForIdle()+            Thread.sleep(1500)+            snap(3) // Artists         }     }++    companion object {+        private const val REL_PATH = "Music/Metadata/"+        // MPEG-1 Layer III, 128 kbps, 44.1 kHz => floor(144*128000/44100) = 417 bytes/frame.+        private const val FRAME_SIZE = 417+        // ~1s of audio (1152 samples/frame at 44.1 kHz => ~26 ms/frame).+        private const val NUM_FRAMES = 40+    } }
music/src/main/ic_launcher-playstore.png
diff --git a/music/src/main/ic_launcher-playstore.png b/music/src/main/ic_launcher-playstore.pngindex a7ad662e90..4083519d48 100644Binary files a/music/src/main/ic_launcher-playstore.png and b/music/src/main/ic_launcher-playstore.png differ