Skip to content

Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.

Keep each TV's app icons in memory, read and write them on IO

jesse-agent-8210da7374fb27f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2a26d60..51a22ca 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ until this file has a section for its version.
## Unreleased
- **Improved:** Opening a TV reads its apps once, in one go, so Apps fills in sooner. [#173](../../pull/173)
+- **Improved:** App icons load once per TV and no longer slow the screen down. [#PR](../../pull/PR)
- **Improved:** Tellyscope stops polling your TV while it's in the background. [#172](../../pull/172)
- **Improved:** The TV's page no longer redraws every app each second. [#174](../../pull/174)
diff --git a/app/src/main/java/app/tellyscope/data/AppNames.kt b/app/src/main/java/app/tellyscope/data/AppNames.kt
index 1c70d08..2c5d415 100644
--- a/app/src/main/java/app/tellyscope/data/AppNames.kt
+++ b/app/src/main/java/app/tellyscope/data/AppNames.kt
@@ -4,8 +4,10 @@ package app.tellyscope.data
import app.tellyscope.adb.AppLabel
import app.tellyscope.adb.Parsers
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
+import kotlinx.coroutines.withContext
import java.io.File
import java.util.Base64
@@ -19,7 +21,9 @@ class AppName(val label: String, val icon: ByteArray?, val tileColor: Long? = nu
* Real app names and icons per TV. The TV's own labels come from `describe` (the label helper) and
* are cached in [dir], one file per TV, so the helper only runs again for new or updated apps. When
* the helper can't run, [KnownLabels] and then a humanised package id stand in, with no icon, and
- * that TV isn't asked again until the app restarts.
+ * that TV isn't asked again until the app restarts. Each TV's file is read once per launch, off the
+ * main thread, and kept: every list after that gets the same icon arrays, so the screen doesn't
+ * decode them again.
*/
class AppNames(private val dir: File) {
/** One helper run at a time; a second caller then finds the first one's results cached. */
@@ -29,6 +33,8 @@ class AppNames(private val dir: File) {
private val unanswered = mutableSetOf<Pair<String, String>>()
/** Asked for in this launch. A cached entry without an icon is asked for once more per launch. */
private val asked = mutableSetOf<Pair<String, String>>()
+ /** [names]' `tv` → its cache, as read from disk once and updated since. */
+ private val loaded = mutableMapOf<String, MutableMap<String, AppLabel>>()
/**
* [tv] identifies the TV (its [historyKey]); [packages] is `pm list packages --show-versioncode`.
@@ -40,8 +46,12 @@ class AppNames(private val dir: File) {
describe: suspend (List<String>) -> List<AppLabel>?,
): Map<String, AppName> = lock.withLock {
val file = File(dir, fileName(tv))
- File(dir, fileName(tv, OLD_CACHE)).delete()
- val cache = load(file).toMutableMap()
+ val cache = loaded.getOrPut(tv) {
+ withContext(Dispatchers.IO) {
+ File(dir, fileName(tv, OLD_CACHE)).delete()
+ load(file).toMutableMap()
+ }
+ }
val stale = packages.filter { p ->
val hit = cache[p.name]
// Without a versionCode (an older pm) a cached label is as good as it gets.
@@ -57,7 +67,8 @@ class AppNames(private val dir: File) {
} else {
fresh.forEach { cache[it.packageName] = it }
stale.filter { s -> fresh.none { it.packageName == s.name } }.forEach { unanswered += tv to it.name }
- save(file, cache.values)
+ val labels = cache.values.toList()
+ withContext(Dispatchers.IO) { save(file, labels) }
}
}
packages.associate { p ->
diff --git a/app/src/test/java/app/tellyscope/AppNamesTest.kt b/app/src/test/java/app/tellyscope/AppNamesTest.kt
index bd21a6f..3ac16ba 100644
--- a/app/src/test/java/app/tellyscope/AppNamesTest.kt
+++ b/app/src/test/java/app/tellyscope/AppNamesTest.kt
@@ -11,6 +11,7 @@ import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
+import org.junit.Assert.assertSame
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
@@ -40,6 +41,17 @@ class AppNamesTest {
assertArrayEquals(icon, names.getValue("com.netflix.ninja").icon)
}
+ @Test fun aTvsCacheIsReadOnceAndKept() = runBlocking {
+ val helper = Helper().apply { versions = mutableMapOf(netflix.name to 10, odd.name to 3) }
+ val appNames = AppNames(tmp.root)
+ val first = appNames.names("SN1", listOf(netflix, odd), helper::describe)
+ // Gone from disk: the next list still comes from memory, with the very same icon.
+ tmp.root.listFiles()!!.forEach { it.delete() }
+ val again = appNames.names("SN1", listOf(netflix, odd), helper::describe)
+ assertEquals(1, helper.runs.size)
+ assertSame(first.getValue(netflix.name).icon, again.getValue(netflix.name).icon)
+ }
+
@Test fun fallbackKeepsTheMaker() {
assertEquals("XGIMI Assistant", AppNames.fallback("com.xgimi.assistant"))
assertEquals("Youtube", AppNames.fallback("com.google.android.youtube"))