Skip to content

Native Android client for Spotify, built with Jetpack Compose

TV: its means measured at start, what it has no screen for turned off; the welcome button shows its focus

kota-zo76e0eb2230bf
diff --git a/base/app/src/main/java/app/myspoti/MyspotiApplication.kt b/base/app/src/main/java/app/myspoti/MyspotiApplication.kt
index 6089f30c..010e4d5b 100644
--- a/base/app/src/main/java/app/myspoti/MyspotiApplication.kt
+++ b/base/app/src/main/java/app/myspoti/MyspotiApplication.kt
@@ -49,6 +49,8 @@ class MyspotiApplication : Application() {
com.music.spotui.data.NewReleaseNotifier.start(this)
// Another account kept on the phone takes over: its own quality and extension settings.
followAccount()
+ // A TV: what it has no screen for, off; the energy saver on when it is short of means.
+ com.music.spotui.ui.tv.TvCaps.apply(this)
}
/**
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/WelcomeScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/WelcomeScreen.kt
index 6bda4e02..6b909339 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/WelcomeScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/WelcomeScreen.kt
@@ -57,6 +57,10 @@ import com.music.spotui.ui.components.pressable
import com.music.spotui.ui.theme.AppAccent
import com.music.spotui.ui.theme.AppBackground
import com.music.spotui.ui.theme.AppSans
+import androidx.compose.ui.focus.focusRequester
+import androidx.compose.ui.focus.onFocusChanged
+import androidx.compose.foundation.border
+import androidx.compose.runtime.mutableStateOf
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.PI
@@ -112,13 +116,22 @@ fun WelcomeScreen(onLogin: () -> Unit) {
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxWidth().align(Alignment.BottomCenter).rise(3),
) {
+ // A TV remote: the button takes the focus at once and shows it (a white ring); a
+ // focus with no look left nothing on screen to say what OK would do.
+ val ctx = androidx.compose.ui.platform.LocalContext.current
+ val tv = remember { com.music.spotui.ui.tv.Tv.isTv(ctx) }
+ val focus = remember { androidx.compose.ui.focus.FocusRequester() }
+ var focused by remember { mutableStateOf(false) }
+ if (tv) LaunchedEffect(Unit) { delay(300); runCatching { focus.requestFocus() } }
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxWidth()
.height(54.dp)
+ .then(if (tv) Modifier.focusRequester(focus).onFocusChanged { focused = it.isFocused } else Modifier)
.pressable(0.96f, onClick = onLogin)
.clip(RoundedCornerShape(50))
+ .then(if (focused) Modifier.border(3.dp, Color.White, RoundedCornerShape(50)) else Modifier)
.background(AppAccent),
) {
Text(stringResource(R.string.welcome_login), color = Color.Black, fontFamily = AppSans,
diff --git a/base/app/src/main/java/com/music/spotui/ui/tv/TvCaps.kt b/base/app/src/main/java/com/music/spotui/ui/tv/TvCaps.kt
new file mode 100644
index 00000000..642dda1b
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/ui/tv/TvCaps.kt
@@ -0,0 +1,42 @@
+package com.music.spotui.ui.tv
+
+import android.app.ActivityManager
+import android.content.Context
+import android.os.Build
+import app.myspoti.host.android.Logbook
+
+/**
+ * What a TV can carry, measured once at start: its memory, its cores and whether it runs 64-bit
+ * apps. Many TVs run 32-bit apps with 1.5 to 2 GB on four slow cores, and the TV screens show
+ * neither clips nor Canvas nor AutoMix's settings: those stay off there, and the energy saver goes on
+ * when the TV is short of means. Nothing changes on a phone.
+ */
+object TvCaps {
+ data class Caps(val ramMb: Long, val lowRam: Boolean, val abi64: Boolean, val cores: Int, val sdk: Int) {
+ /** Too little for the extras: under 2.5 GB, a "low RAM" device, 32-bit only, or under 4 cores. */
+ val weak: Boolean get() = lowRam || ramMb < 2_500 || !abi64 || cores < 4
+ }
+
+ fun probe(context: Context): Caps {
+ val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
+ val mem = ActivityManager.MemoryInfo().also(am::getMemoryInfo)
+ return Caps(mem.totalMem / (1024 * 1024), am.isLowRamDevice, Build.SUPPORTED_64_BIT_ABIS.isNotEmpty(),
+ Runtime.getRuntime().availableProcessors(), Build.VERSION.SDK_INT)
+ }
+
+ /** On a TV: what has no screen there is turned off, and the energy saver on for a weak one. */
+ fun apply(context: Context) {
+ if (!Tv.isTv(context)) return
+ val c = runCatching { probe(context) }.getOrNull() ?: return
+ // Analysing every song (Beat This on ONNX) for a mix whose settings the TV does not show.
+ if (com.music.spotui.automix.AutoMix.on) com.music.spotui.automix.AutoMix.set(false)
+ // Looked up for each song, shown by no TV screen.
+ if (com.music.spotui.data.preferences.MusicVideoPref.on) com.music.spotui.data.preferences.MusicVideoPref.set(context, false)
+ if (app.myspoti.power.EnergySettings.state.value.canvasEnabled) app.myspoti.power.EnergySettings.setCanvasEnabled(false)
+ // Notifications are not read on a TV.
+ if (com.music.spotui.data.NewReleaseNotifier.on) com.music.spotui.data.NewReleaseNotifier.setOn(context, false)
+ if (c.weak && !app.myspoti.power.EnergySettings.state.value.manualSaver) app.myspoti.power.EnergySettings.setSaver(true)
+ Logbook.log("tv: ${c.ramMb} MB${if (c.lowRam) " (low RAM)" else ""}, ${c.cores} cores, ${if (c.abi64) "64" else "32"}-bit, Android ${c.sdk}: " +
+ "AutoMix, clips, Canvas and release notices off${if (c.weak) ", energy saver on (weak)" else ""}")
+ }
+}