diff --git a/android/app/src/main/java/tv/orbit/classic/MainActivity.kt b/android/app/src/main/java/tv/orbit/classic/MainActivity.kt index b8142e0c..39a6aa3f 100644 --- a/android/app/src/main/java/tv/orbit/classic/MainActivity.kt +++ b/android/app/src/main/java/tv/orbit/classic/MainActivity.kt @@ -48,7 +48,7 @@ internal fun clock(time: Long) = SimpleDateFormat("HH:mm", Locale.UK).format(Dat class MainActivity : ComponentActivity() { override fun onStart() { - super.onStart(); OrbitCompanion.onTv = true; if (PhoneLink.phones.isNotEmpty()) PhoneLink.start() + super.onStart(); WatchCheck.started(); OrbitCompanion.onTv = true; if (PhoneLink.phones.isNotEmpty()) PhoneLink.start() // Multiroom auto-scan: a set-up Orbit can be found by a new TV while it's open. if (Onboarding.done(this)) { RoomBeacon.on(this) @@ -59,7 +59,7 @@ class MainActivity : ComponentActivity() { // Phones stay connected while Orbit is in the background (Paul, 8 November 2026: the web app couldn't connect while the TV // was in another app), and paired rooms keep syncing; the phone says Orbit isn't on screen instead of failing to load. override fun onDestroy() { InstallSnapshot.screenOpen = false; super.onDestroy() } - override fun onStop() { OrbitCompanion.onTv = false; PhoneLink.offerRoom(null); if (PhoneLink.phones.isEmpty() && RoomLinks.links.none { it.on }) { RoomBeacon.off(this); PhoneLink.stop() }; super.onStop() } + override fun onStop() { WatchCheck.stopped(); OrbitCompanion.onTv = false; PhoneLink.offerRoom(null); if (PhoneLink.phones.isEmpty() && RoomLinks.links.none { it.on }) { RoomBeacon.off(this); PhoneLink.stop() }; super.onStop() } /** * The adb-only requests (inspection, evidence, the provider switch and shadow comparison). Read at start and, since Orbit * opens as one copy (singleTask, 8 November 2026), also when adb sends them to an Orbit already open. @@ -183,7 +183,8 @@ private fun OrbitApp() { LaunchedEffect(realChannelAvailable, profile, Profiles.asks, onboarding) { if (realChannelAvailable && profile == null && !Profiles.asks && !onboarding) Profiles.current = Profiles.all.first { it.kind == ProfileKind.Grown } } - val picking = realChannelAvailable && restoreChecked && !chosen && Profiles.asks && !onboarding + val asking = realChannelAvailable && restoreChecked && Profiles.asks && !onboarding + val picking = asking && (!chosen || WatchCheck.pending) val kidsDriveIn = profile?.kind != ProfileKind.Kids || profile.driveIn // Kids sees the Arcade only when Arcade for Kids leaves it some games (Family settings). val kidsArcade = arcadeOpenFor(profile) @@ -195,6 +196,9 @@ private fun OrbitApp() { val activity = androidx.activity.compose.LocalActivity.current LaunchedEffect(Unit) { while (true) { delay(1000); if (SleepTimer.tick(System.currentTimeMillis())) { SleepTimer.clear(); activity?.finish() } } } Box(Modifier.fillMaxSize().background(Navy).onPreviewKeyEvent { event -> + // The first press after two hours without one asks who's watching, and does nothing else (WatchCheck). + if (event.nativeKeyEvent.action == android.view.KeyEvent.ACTION_DOWN && WatchCheck.key()) { WatchCheck.swallowUp = true; return@onPreviewKeyEvent true } + if (event.nativeKeyEvent.action == android.view.KeyEvent.ACTION_UP && WatchCheck.swallowUp) { WatchCheck.swallowUp = false; return@onPreviewKeyEvent true } // During the timer's last minute any button keeps you watching, and does nothing else. if (!SleepTimer.warning && !SleepTimer.goodnight) return@onPreviewKeyEvent false if (event.nativeKeyEvent.action == android.view.KeyEvent.ACTION_UP) SleepTimer.keepWatching(System.currentTimeMillis()) @@ -225,7 +229,8 @@ private fun OrbitApp() { LaunchedEffect(Unit) { delay(400); slowStart = true } if (!restoreChecked && slowStart) MomentClip(Moment.Tuning, loop = true) // The sections sit centred on the screen, over the centred page below; the logo keeps the left, the clock the right. - if (picking) WhosWatching(onClose = { activity?.finish() }) + if (picking && !chosen) WhosWatching(onClose = { activity?.finish() }) + else if (picking) WhosWatching(onClose = { WatchCheck.pending = false }, initial = profile, onChosen = { WatchCheck.pending = false }) // At the end of the flow the one who set Orbit up starts watching as Adult; the tour goes back to Settings. if (onboarding) OnboardingScreen(signedIn = realChannelAvailable) { tour -> if (!tour) { section = if (realChannelAvailable) "Home" else "Connect Emby"; Profiles.current = Profiles.all.first { it.kind == ProfileKind.Grown } } diff --git a/android/app/src/main/java/tv/orbit/classic/Profiles.kt b/android/app/src/main/java/tv/orbit/classic/Profiles.kt index 9b3eea89..3a3e3328 100644 --- a/android/app/src/main/java/tv/orbit/classic/Profiles.kt +++ b/android/app/src/main/java/tv/orbit/classic/Profiles.kt @@ -1,5 +1,6 @@ package tv.orbit.classic +import androidx.compose.ui.input.key.onPreviewKeyEvent import android.content.Context import androidx.activity.compose.BackHandler import androidx.compose.foundation.Canvas @@ -223,20 +224,50 @@ internal fun StarrySky(modifier: Modifier = Modifier) { } } -/** "Who's watching?" Adult asks for the PIN, when one's set; Kids goes straight in. */ +/** + * Asking again (Paul, 9 November 2026), so Taste learns from the right person: when Orbit comes back after 10 minutes or + * more away, and on the first button press after 2 hours without one (whoever picks up the remote). Whoever was watching + * is already highlighted: OK, Back, or 5 seconds with no button carries on as them. Only while Orbit asks at all + * ([Profiles.asks]). A full-screen programme isn't interrupted: the press is seen when the guide or Home has it. + */ +internal object WatchCheck { + var pending by mutableStateOf(false) + /** The release of the press that only asked, so it does nothing either. */ + var swallowUp = false + private var lastKeyMs = android.os.SystemClock.elapsedRealtime() + private var stoppedMs: Long? = null + const val IDLE_MS = 2 * 60 * 60_000L + const val AWAY_MS = 10 * 60_000L + const val CARRY_ON_MS = 5_000L + fun started(nowMs: Long = android.os.SystemClock.elapsedRealtime()) { if (stoppedMs.let { it == null || nowMs - it >= AWAY_MS }) pending = true; lastKeyMs = nowMs } + fun stopped(nowMs: Long = android.os.SystemClock.elapsedRealtime()) { stoppedMs = nowMs } + /** A button press; true when it comes after the idle gap, so it only brings up the question. */ + fun key(nowMs: Long = android.os.SystemClock.elapsedRealtime()): Boolean { + val idle = nowMs - lastKeyMs >= IDLE_MS; lastKeyMs = nowMs + if (idle && Profiles.asks && Profiles.current != null) { pending = true; return true } + return false + } +} + +/** "Who's watching?" Adult asks for the PIN, when one's set; Kids goes straight in. [initial]: asking again, see [WatchCheck]. */ @Composable -internal fun WhosWatching(onClose: () -> Unit) { +internal fun WhosWatching(onClose: () -> Unit, initial: Profile? = null, onChosen: () -> Unit = {}) { val context = LocalContext.current var asking by remember { mutableStateOf(null) } var problem by remember { mutableStateOf(null) } val first = remember { FocusRequester() } + val ordered = Profiles.all.sortedBy { if (it.kind == ProfileKind.Kids) 0 else 1 } + val firstIndex = ordered.indexOfFirst { it.id == initial?.id }.coerceAtLeast(0) + var touched by remember { mutableStateOf(false) } LaunchedEffect(asking) { if (asking == null) runCatching { first.requestFocus() } } + // Asking again carries on as whoever was watching when nobody answers. + LaunchedEffect(touched, asking) { if (initial != null && !touched && asking == null) { kotlinx.coroutines.delay(WatchCheck.CARRY_ON_MS); onClose() } } BackHandler(asking == null, onBack = onClose) fun choose(p: Profile) { - if (p.kind == ProfileKind.Grown && AfterDarkLock.pinSet && !AfterDarkLock.unlocked) { asking = p; problem = null } - else { if (p.kind == ProfileKind.Kids) AfterDarkLock.lock(); Profiles.current = p } + if (p.kind == ProfileKind.Grown && p.id != initial?.id && AfterDarkLock.pinSet && !AfterDarkLock.unlocked) { asking = p; problem = null } + else { if (p.kind == ProfileKind.Kids) AfterDarkLock.lock(); Profiles.current = p; onChosen() } } - Box(Modifier.fillMaxSize()) { + Box(Modifier.fillMaxSize().onPreviewKeyEvent { touched = true; false }) { Column(Modifier.align(Alignment.Center), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(28.dp)) { Image(cachedPainter(R.drawable.orbit_logo), "Orbit", Modifier.logoPlate().width(150.dp).height(38.dp)) Text("Who's watching?", color = Ink, fontFamily = Anton, fontSize = 40.sp) @@ -244,9 +275,9 @@ internal fun WhosWatching(onClose: () -> Unit) { val many = Profiles.all.size > 4 Row(horizontalArrangement = Arrangement.spacedBy(if (many) 28.dp else 56.dp)) { // Kids first, so a child's OK goes straight to their own. - Profiles.all.sortedBy { if (it.kind == ProfileKind.Kids) 0 else 1 }.forEachIndexed { i, p -> + ordered.forEachIndexed { i, p -> var focused by remember { mutableStateOf(false) } - Column(Modifier.then(if (i == 0) Modifier.focusRequester(first) else Modifier).onFocusChanged { focused = it.isFocused } + Column(Modifier.then(if (i == firstIndex) Modifier.focusRequester(first) else Modifier).onFocusChanged { focused = it.isFocused } // No grey focus box behind the picture (Paul, 6 November 2026): the gold ring and name say it's chosen. .clickable(remember { androidx.compose.foundation.interaction.MutableInteractionSource() }, indication = null) { choose(p) }, horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) { @@ -263,7 +294,8 @@ internal fun WhosWatching(onClose: () -> Unit) { } } } - Text("Kids sees only the channels and games chosen for it." + if (AfterDarkLock.pinSet) " Adult needs the PIN." else " Set a PIN in Settings › Family to keep Adult for grown-ups.", + if (initial != null && !touched) Text("Carrying on as ${initial.name} in a moment. OK to confirm, or choose someone else.", color = Muted, fontSize = 15.sp) + else Text("Kids sees only the channels and games chosen for it." + if (AfterDarkLock.pinSet) " Adult needs the PIN." else " Set a PIN in Settings › Family to keep Adult for grown-ups.", color = Muted, fontSize = 15.sp) } asking?.let { p -> @@ -272,7 +304,7 @@ internal fun WhosWatching(onClose: () -> Unit) { ProfileAvatar(p.picture, 90.dp) Text(p.name, color = Ink, fontSize = 20.sp, fontWeight = FontWeight.SemiBold) PinEntry("Enter the PIN", problem, onDone = { pin -> - if (AfterDarkLock.unlock(context, pin)) { asking = null; Profiles.current = p } else problem = AfterDarkLock.refusal(context) + if (AfterDarkLock.unlock(context, pin)) { asking = null; Profiles.current = p; onChosen() } else problem = AfterDarkLock.refusal(context) }, onCancel = { asking = null }) } } diff --git a/android/app/src/test/java/tv/orbit/classic/WatchCheckTest.kt b/android/app/src/test/java/tv/orbit/classic/WatchCheckTest.kt new file mode 100644 index 00000000..54e09a9c --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/WatchCheckTest.kt @@ -0,0 +1,32 @@ +package tv.orbit.classic + +import org.junit.Assert.* +import org.junit.Test + +/** Asking again who's watching (9 November 2026): back after 10 minutes away, or the first press after 2 hours. */ +class WatchCheckTest { + private val hour = 60 * 60_000L + + @Test fun asksWhenOrbitComesBackAfterAWhile() { + WatchCheck.pending = false + WatchCheck.stopped(1_000_000L); WatchCheck.started(1_000_000L + 2 * 60_000L) + assertFalse(WatchCheck.pending) // a quick look at another app: no question + WatchCheck.stopped(2_000_000L); WatchCheck.started(2_000_000L + 11 * 60_000L) + assertTrue(WatchCheck.pending) + } + + @Test fun theFirstPressAfterTwoHoursOnlyAsks() { + val kids = Profiles.kidsOn + Profiles.current = Profiles.all.first { it.kind == ProfileKind.Grown } + WatchCheck.pending = false + val t = 10 * hour + WatchCheck.started(t); WatchCheck.pending = false + assertFalse(WatchCheck.key(t + hour)) // pressed within the two hours: carries on + if (Profiles.asks) { + assertTrue(WatchCheck.key(t + hour + 2 * hour)) // two hours later: the press only asks + assertTrue(WatchCheck.pending) + assertFalse(WatchCheck.key(t + 3 * hour + 1_000)) // and the next press works as usual + } + assertEquals(kids, Profiles.kidsOn) + } +}