diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 9dbad671..a64dc45a 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -9,8 +9,8 @@ android { applicationId = "tv.orbit.classic" minSdk = 26 targetSdk = 36 - versionCode = 225 - versionName = "0.108.65-tune-ahead" + versionCode = 226 + versionName = "0.108.66-cameo-rules" } buildFeatures { compose = true } // The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so diff --git a/android/app/src/main/java/tv/orbit/classic/Cameos.kt b/android/app/src/main/java/tv/orbit/classic/Cameos.kt new file mode 100644 index 00000000..1a8bc523 --- /dev/null +++ b/android/app/src/main/java/tv/orbit/classic/Cameos.kt @@ -0,0 +1,89 @@ +package tv.orbit.classic + +import java.time.Instant +import java.time.ZoneId + +/** + * Character cameos, phase 2 (Paul, 6 November 2026, docs/character-continuity.md): the choice of when the cast may appear + * between programmes, with no artwork yet. Paul's answers: Riot, ToonZ's evening and ToonZ Late, and Nite Owl go first; + * cameos are off until he turns them on; Luna's fourth-wall glance and eye-roll end about one gag in three. + * + * The rules, all editorial, none touching the schedule: + * - Only at a changeover Orbit owns: one programme running straight into the next, the ending one at least ten minutes. + * - Never next to a film (its credits or its start), never on the Drive-In, Tots or for a Kids profile. + * - At most one per channel every 60 to 90 minutes, at least 20 minutes between any two, and no gag twice in a session. + * - The same changeover always gets the same answer, so changing channel and coming back can't set one off again. + * Mini shorts need gaps the schedule already has and their own artwork; they're chosen in a later phase. + */ +internal enum class Gag(val title: String, val overnightOnly: Boolean = false) { + Chase("The Chase"), WindUp("The Wind-Up"), Sign("The Sign"), RunningLate("Running Late"), NiteOwlShift("Nite Owl's Shift", overnightOnly = true) +} + +internal data class Cameo(val gag: Gag, val lunaEnds: Boolean) + +/** One changeover: the programme ending and the one starting straight after it. */ +internal data class Changeover(val channel: Int, val endingStartMs: Long, val endingType: String, val nextStartMs: Long, val nextType: String) + +internal const val CAMEO_MIN_GAP_MS = 60 * 60_000L +internal const val CAMEO_GAP_SPREAD_MS = 30 * 60_000L +internal const val CAMEO_HOUSEHOLD_MS = 20 * 60_000L +private const val CAMEO_SHORTEST_MS = 10 * 60_000L +private const val RIOT = 105 +private const val TOONZ = 605 + +/** Where cameos may appear first: Riot, ToonZ from 18:00 through ToonZ Late, and Junior during Nite Owl. */ +internal fun cameoWindow(channel: Int, atMs: Long, zone: ZoneId = ZoneId.systemDefault()): Boolean { + val hour = Instant.ofEpochMilli(atMs).atZone(zone).hour + return when (channel) { + RIOT -> true + TOONZ -> hour >= 18 || hour < TOONZ_LATE.toHour + NITE_OWL.channel -> hour >= NITE_OWL.fromHour || hour < NITE_OWL.toHour + else -> false + } +} + +/** The household's record for this time Orbit is open: kept in memory only, gone when Orbit closes. */ +internal class CameoDiary { + val lastByChannel = mutableMapOf() + var lastAny: Long? = null + val seen = mutableSetOf() + val decided = mutableMapOf, Cameo?>() +} + +internal object CameoSession { val diary = CameoDiary() } + +/** A steady number from a changeover, so its answer is the same every time it's asked. */ +private fun mix(channel: Int, atMs: Long): Int { + var h = channel.toLong() * 1_000_003L xor (atMs / 60_000) + h = (h xor (h ushr 33)) * -0x00ae502812aa7333L; h = h xor (h ushr 29) + return (h and 0x7fffffff).toInt() +} + +/** How long this channel rests after a cameo: 60 to 90 minutes, steady for the changeover. */ +internal fun cameoGap(channel: Int, atMs: Long) = CAMEO_MIN_GAP_MS + (mix(channel, atMs) % (CAMEO_GAP_SPREAD_MS / 60_000 + 1)) * 60_000L + +/** + * Whether this changeover gets a cameo, and which. [on] is Character cameos with the cast switched on; [kids] a Kids + * profile watching. Asked again for the same changeover, it gives the same answer and records nothing more. + */ +internal fun chooseCameo(c: Changeover, diary: CameoDiary, on: Boolean, kids: Boolean, zone: ZoneId = ZoneId.systemDefault()): Cameo? { + if (!on || kids) return null + val key = c.channel to c.nextStartMs + if (key in diary.decided) return diary.decided[key] + val answer = run { + if (c.channel == TOTS_NUMBER || c.channel == DRIVE_IN_NUMBER) return@run null + if (c.endingType == "Movie" || c.nextType == "Movie") return@run null + if (c.nextStartMs - c.endingStartMs < CAMEO_SHORTEST_MS) return@run null + if (!cameoWindow(c.channel, c.nextStartMs, zone)) return@run null + diary.lastByChannel[c.channel]?.let { if (c.nextStartMs - it < cameoGap(c.channel, it)) return@run null } + diary.lastAny?.let { if (c.nextStartMs - it < CAMEO_HOUSEHOLD_MS) return@run null } + val overnight = c.channel == NITE_OWL.channel + val left = Gag.entries.filter { it !in diary.seen && (!it.overnightOnly || overnight) } + if (left.isEmpty()) return@run null + val m = mix(c.channel, c.nextStartMs) + Cameo(left[m % left.size], lunaEnds = (m / 7) % 3 == 0) + } + diary.decided[key] = answer + if (answer != null) { diary.lastByChannel[c.channel] = c.nextStartMs; diary.lastAny = c.nextStartMs; diary.seen += answer.gag } + return answer +} diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt b/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt index 8cdc2cc0..0af4a9a8 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyClient.kt @@ -62,9 +62,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI } /** The sign-in headers for this kind of server. */ internal fun authHeaders(token: String?): Map = when (kind) { - ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.65\"") + + ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.66\"") + (token?.let { mapOf("X-Emby-Token" to it) } ?: emptyMap()) - ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.65\"" + + ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.66\"" + (token?.let { ", Token=\"$it\"" } ?: "")) } /** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */ diff --git a/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt b/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt index e2bf14ec..ab961430 100644 --- a/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt +++ b/android/app/src/main/java/tv/orbit/classic/SecureMediaDataSource.kt @@ -39,7 +39,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B next.instanceFollowRedirects = false next.connectTimeout = 10_000 next.readTimeout = 20_000 - next.setRequestProperty("User-Agent", "OrbitTV/0.108.65") + next.setRequestProperty("User-Agent", "OrbitTV/0.108.66") next.setRequestProperty("Accept-Encoding", "identity") session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) } if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) { diff --git a/android/app/src/main/java/tv/orbit/classic/Settings.kt b/android/app/src/main/java/tv/orbit/classic/Settings.kt index 5df3481d..6c56b9b4 100644 --- a/android/app/src/main/java/tv/orbit/classic/Settings.kt +++ b/android/app/src/main/java/tv/orbit/classic/Settings.kt @@ -116,6 +116,14 @@ internal class OrbitSettings(context: Context) { get() = saved.getString("look", null)?.let { name -> OrbitLook.entries.firstOrNull { it.name == name } } ?: OrbitLook.Night set(value) { saved.edit().putString("look", value.name).apply() } /** Turns off moving presentation: the Drive-In's curtains, and every slide and rise becomes a short fade (design/motion). */ + /** The cast between programmes, now and then (docs/character-continuity.md); off until Paul turns it on. */ + var characterCameos: Boolean + get() = saved.getBoolean("character_cameos", false) + set(value) { saved.edit().putBoolean("character_cameos", value).apply() } + /** The cast's mini shorts, rarer still; off until Paul turns them on. */ + var miniShorts: Boolean + get() = saved.getBoolean("mini_shorts", false) + set(value) { saved.edit().putBoolean("mini_shorts", value).apply() } /** Brick Break's power-up sounds (Paul, 6 November 2026). */ var arcadeSounds: Boolean get() = saved.getBoolean("arcade_sounds", true) @@ -254,6 +262,8 @@ private fun ViewingDetail(firstSwitch: FocusRequester, modifier: Modifier, top: var reduceMotion by remember { mutableStateOf(settings.reduceMotion) } var squeezeBack by remember { mutableStateOf(settings.squeezeBack) } var castOn by remember { mutableStateOf(settings.castOn) } + var cameos by remember { mutableStateOf(settings.characterCameos) } + var miniShorts by remember { mutableStateOf(settings.miniShorts) } var specialIdents by remember { mutableStateOf(settings.specialIdents) } var blockIdentSound by remember { mutableStateOf(settings.blockIdentSound) } Column(modifier.verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(8.dp)) { @@ -284,6 +294,13 @@ private fun ViewingDetail(firstSwitch: FocusRequester, modifier: Modifier, top: SettingLine("The cast", "Show Ollie, Zip, Blaze, Drift and Luna in the squeeze-back and promos.") { OnOff(castOn, Modifier, onClick = { castOn = !castOn; settings.castOn = castOn }) } + // Character Shorts (Paul, 6 November 2026): both follow The cast, which turns everything off. + SettingLine("Character cameos", if (castOn) "Now and then the cast pop up between programmes on Riot, ToonZ in the evening and Nite Owl. Never over a programme or a film." else "Turn on The cast first.") { + OnOff(cameos, Modifier, enabled = castOn, onClick = { cameos = !cameos; settings.characterCameos = cameos }) + } + SettingLine("Mini shorts", if (castOn) "Rarer still: a short cartoon with the cast in a gap the schedule already has. Coming in a later update." else "Turn on The cast first.") { + OnOff(miniShorts, Modifier, enabled = castOn, onClick = { miniShorts = !miniShorts; settings.miniShorts = miniShorts }) + } var promoNote by remember { mutableStateOf(null) } SettingLine("Promos", promoNote ?: "Now and then a promo for tonight's highlights plays in place of a channel's ident. See one now; it closes by itself.") { OrbitButton("Show one", onClick = { diff --git a/android/app/src/test/java/tv/orbit/classic/CameoReplayTest.kt b/android/app/src/test/java/tv/orbit/classic/CameoReplayTest.kt new file mode 100644 index 00000000..ec723b25 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/CameoReplayTest.kt @@ -0,0 +1,51 @@ +package tv.orbit.classic + +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File +import java.time.ZoneId + +/** + * Character cameos over the box's real schedules (Paul, 6 November 2026): every channel's booked programmes for the week, + * one session an evening (18:00 to 02:00), through the chooser. Run by hand: + * ORBIT_REPLAY_DIR= ./gradlew :app:testDebugUnitTest --tests '*CameoReplayTest*' + * Writes cameo-replay.txt there (counts and times only), and fails on any broken rule. Reads the copies; changes nothing. + */ +class CameoReplayTest { + private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File) + private val zone = ZoneId.of("Europe/London") + private val hour = 60 * 60_000L; private val day = 24 * hour + + private fun changeovers(file: File): List { + val channel = Regex("""schedule-(\d+)\.json""").find(file.name)!!.groupValues[1].toInt() + val rows = JSONObject(file.readText()).getJSONArray("entries") + val shows = (0 until rows.length()).map { rows.getJSONObject(it) }.map { Triple(it.getLong("a"), it.getLong("d"), it.optString("k", "Episode")) }.sortedBy { it.first } + return shows.zipWithNext().filter { (a, b) -> a.first + a.second == b.first }.map { (a, b) -> Changeover(channel, a.first, a.third, b.first, b.third) } + } + + @Test fun aWeekOfRealSchedules() { + assumeTrue("set ORBIT_REPLAY_DIR to run", dir != null && dir.isDirectory) + val all = dir!!.listFiles { f -> f.name.matches(Regex("""schedule-\d+\.json""")) }!!.flatMap(::changeovers).sortedBy { it.nextStartMs } + assertTrue(all.isNotEmpty()) + val start = java.time.Instant.ofEpochMilli(all.first().nextStartMs).atZone(zone).toLocalDate().atStartOfDay(zone).toInstant().toEpochMilli() + val report = StringBuilder("Cameo replay: ${all.size} changeovers on ${all.map { it.channel }.distinct().size} channels\n") + var total = 0; var luna = 0 + for (d in 0 until 7) { + val from = start + d * day + 18 * hour; val to = from + 8 * hour + val diary = CameoDiary() + val evening = all.filter { it.nextStartMs in from until to }.mapNotNull { c -> chooseCameo(c, diary, true, false, zone)?.let { c to it } } + evening.forEach { (c, cameo) -> + assertTrue(c.channel in setOf(105, 605, NITE_OWL.channel)); assertFalse(c.endingType == "Movie" || c.nextType == "Movie") + report.append(" day ${d + 1} ${java.time.Instant.ofEpochMilli(c.nextStartMs).atZone(zone).toLocalTime()} channel ${c.channel} ${cameo.gag.title}${if (cameo.lunaEnds) " + Luna" else ""}\n") + } + evening.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_HOUSEHOLD_MS) } + evening.groupBy { it.first.channel }.values.forEach { one -> one.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_MIN_GAP_MS) } } + assertEquals(evening.size, evening.map { it.second.gag }.distinct().size) + total += evening.size; luna += evening.count { it.second.lunaEnds } + } + report.append("Total $total cameos over 7 evenings, $luna ending with Luna\n") + File(dir, "cameo-replay.txt").writeText(report.toString()) + } +} diff --git a/android/app/src/test/java/tv/orbit/classic/CameoTest.kt b/android/app/src/test/java/tv/orbit/classic/CameoTest.kt new file mode 100644 index 00000000..9a3a1033 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/CameoTest.kt @@ -0,0 +1,70 @@ +package tv.orbit.classic + +import org.junit.Assert.* +import org.junit.Test +import java.time.ZoneId + +/** Character cameos, phase 2 (Paul, 6 November 2026): a week of changeovers through the chooser, checking every rule. */ +class CameoTest { + private val zone = ZoneId.of("Europe/London") + private val minute = 60_000L; private val hour = 60 * minute; private val day = 24 * hour + // Monday 3 November 2025, 00:00 in London. + private val monday = java.time.LocalDate.of(2025, 11, 3).atStartOfDay(zone).toInstant().toEpochMilli() + + /** Half-hour episodes on each channel, with a two-hour film on Riot every six hours. */ + private fun week(channels: List): List = channels.flatMap { channel -> + val out = mutableListOf(); var t = monday; var prevStart = monday - 30 * minute; var prevType = "Episode" + while (t < monday + 7 * day) { + val film = channel == 105 && ((t - monday) / hour) % 6 == 0L && (t - monday) % hour == 0L + val type = if (film) "Movie" else "Episode" + out += Changeover(channel, prevStart, prevType, t, type) + prevStart = t; prevType = type; t += if (film) 2 * hour else 30 * minute + } + out + }.sortedBy { it.nextStartMs } + + /** One session an evening, 18:00 to 02:00, as the household might watch. */ + private fun sessions(all: List, on: Boolean = true, kids: Boolean = false): List> = (0 until 7).flatMap { d -> + val from = monday + d * day + 18 * hour; val to = from + 8 * hour + val diary = CameoDiary() + all.filter { it.nextStartMs in from until to }.mapNotNull { c -> chooseCameo(c, diary, on, kids, zone)?.let { c to it } } + } + + private val all = week(listOf(101, 105, 605, NITE_OWL.channel, TOTS_NUMBER, DRIVE_IN_NUMBER)) + + @Test fun cameosKeepToEveryRule() { + val chosen = sessions(all) + assertTrue("some cameos over a week", chosen.size >= 7) + for ((c, _) in chosen) { + assertTrue("first channels only: ${c.channel}", c.channel in setOf(105, 605, NITE_OWL.channel)) + assertFalse("never next to a film", c.endingType == "Movie" || c.nextType == "Movie") + assertTrue(cameoWindow(c.channel, c.nextStartMs, zone)) + } + // Gaps: per channel 60 minutes or more, between any two 20 or more, no gag twice in an evening. + chosen.groupBy { (c, _) -> (c.nextStartMs - monday - 18 * hour) / day }.values.forEach { evening -> + evening.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_HOUSEHOLD_MS) } + evening.groupBy { it.first.channel }.values.forEach { one -> one.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_MIN_GAP_MS) } } + assertEquals(evening.size, evening.map { it.second.gag }.distinct().size) + evening.filter { it.second.gag.overnightOnly }.forEach { assertEquals(NITE_OWL.channel, it.first.channel) } + } + } + + @Test fun theSameChangeoverAlwaysGetsTheSameAnswer() { + assertEquals(sessions(all), sessions(all)) + val diary = CameoDiary(); val c = all.first { it.channel == 105 && it.nextType == "Episode" && it.endingType == "Episode" && cameoWindow(105, it.nextStartMs, zone) } + val first = chooseCameo(c, diary, true, false, zone) + assertEquals(first, chooseCameo(c, diary, true, false, zone)) + assertEquals(if (first != null) 1 else 0, diary.seen.size) + } + + @Test fun offOrForKidsNothingAppears() { + assertTrue(sessions(all, on = false).isEmpty()) + assertTrue(sessions(all, kids = true).isEmpty()) + } + + @Test fun lunaEndsAboutOneGagInThree() { + val many = (0 until 400).mapNotNull { i -> chooseCameo(Changeover(105, monday + i * day, "Episode", monday + i * day + 30 * minute, "Episode"), CameoDiary(), true, false, zone) } + val share = many.count { it.lunaEnds }.toDouble() / many.size + assertTrue("Luna ends $share of gags", share in .2..0.45) + } +}