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.

Label helper: print whole lines only, fall back icon → banner → PM icon

jesse-agent-3622ce0d14d22c2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fe2085a..2257af4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ until this file has a section for its version.
- **New:** Uninstall apps you installed yourself; apps that came with the TV can only be disabled. [#33](../../pull/33)
- **Improved:** Apps that can't be changed fold into their own section below the list. [#33](../../pull/33)
+- **Fixed:** App icons that failed to load once now come back, with a fallback to the banner. [#34](../../pull/34)
## 0.1.0 — 2026-10-23
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/LabelHelperTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/LabelHelperTest.kt
index 9bbc30f..f441dc7 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/LabelHelperTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/LabelHelperTest.kt
@@ -44,6 +44,16 @@ class LabelHelperTest {
assertNull(apps[1].iconPng)
}
+ @Test fun aHalfPrintedLineCostsTheNextAppToo() {
+ // What the helper printed when loadIcon threw after the label: no closing brace, no newline,
+ // so the next app's object landed on the same line. The helper now prints whole lines only.
+ val out = """
+ {"p":"com.apple.atve.androidtv.appletv","l":"Apple TV","v":7{"p":"org.jellyfin.androidtv","l":"Jellyfin","v":2,"i":"$b64"}
+ {"p":"com.netflix.ninja","l":"Netflix","v":1,"i":"$b64"}
+ """.trimIndent()
+ assertEquals(listOf("com.netflix.ninja"), LabelHelper.parse(out).map { it.packageName })
+ }
+
@Test fun pushesRunsAndDeletes() = runTest {
val sh = FakeShell(mapOf("CLASSPATH=" to ok("""{"p":"com.a","l":"A","v":7}""" + "\n")))
val apps = LabelHelper(sh, dex).describe(listOf("com.a", "com.b"))
diff --git a/app/src/helper/java/app/tellyscope/helper/Labels.java b/app/src/helper/java/app/tellyscope/helper/Labels.java
index c3a7edc..b1cd458 100644
--- a/app/src/helper/java/app/tellyscope/helper/Labels.java
+++ b/app/src/helper/java/app/tellyscope/helper/Labels.java
@@ -40,27 +40,24 @@ public final class Labels {
for (String a : args) names.add(a);
}
for (String name : names) {
+ // The whole line is built before any of it is printed: a throw halfway used to leave an
+ // unclosed object that glued itself to the next app's line, and the app lost both.
try {
PackageInfo p = pm.getPackageInfo(name, 0);
ApplicationInfo ai = p.applicationInfo;
if (ai == null) continue;
- out.print("{\"p\":");
- string(out, name);
- out.print(",\"l\":");
- string(out, ai.loadLabel(pm).toString());
- out.print(",\"v\":");
- out.print(versionCode(p));
- // TV-only apps often ship a banner and no icon, which would draw as the generic robot.
- Drawable art = ai.icon == 0 && ai.banner != 0 ? ai.loadBanner(pm) : ai.loadIcon(pm);
- String icon = icon(art);
- if (icon != null) {
- out.print(",\"i\":\"");
- out.print(icon);
- out.print('"');
- }
- out.print("}\n");
- } catch (Exception e) {
- // One unreadable app (gone since the list, broken resources) doesn't stop the rest.
+ StringBuilder line = new StringBuilder("{\"p\":");
+ string(line, name);
+ line.append(",\"l\":");
+ string(line, ai.loadLabel(pm).toString());
+ line.append(",\"v\":").append(versionCode(p));
+ String icon = icon(pm, ai);
+ if (icon != null) line.append(",\"i\":\"").append(icon).append('"');
+ line.append("}\n");
+ out.print(line);
+ } catch (Throwable t) {
+ // One unreadable app (gone since the list, broken resources, a drawable that runs the
+ // helper out of memory) doesn't stop the rest.
}
}
out.flush();
@@ -84,10 +81,38 @@ public final class Labels {
return Build.VERSION.SDK_INT >= 28 ? p.getLongVersionCode() : p.versionCode;
}
- private static String icon(Drawable d) {
+ /**
+ * The first artwork that loads and draws: the icon, the banner, then PackageManager's own pick.
+ * TV-only apps often ship a banner and no icon, which would draw as the generic robot, so for
+ * them the banner goes first.
+ */
+ private static String icon(PackageManager pm, ApplicationInfo ai) {
+ int first = ai.icon == 0 && ai.banner != 0 ? BANNER : ICON;
+ for (int kind : new int[] {first, first == ICON ? BANNER : ICON, APPLICATION_ICON}) {
+ try {
+ String png = render(art(pm, ai, kind));
+ if (png != null) return png;
+ } catch (Throwable t) {
+ // Try the next one; the label is still worth sending without any.
+ }
+ }
+ return null;
+ }
+
+ private static final int ICON = 0, BANNER = 1, APPLICATION_ICON = 2;
+
+ private static Drawable art(PackageManager pm, ApplicationInfo ai, int kind) {
+ switch (kind) {
+ case ICON: return ai.loadIcon(pm);
+ case BANNER: return ai.loadBanner(pm); // null when the app has none
+ default: return pm.getApplicationIcon(ai);
+ }
+ }
+
+ private static String render(Drawable d) {
if (d == null) return null;
+ Bitmap b = Bitmap.createBitmap(ICON_PX, ICON_PX, Bitmap.Config.ARGB_8888);
try {
- Bitmap b = Bitmap.createBitmap(ICON_PX, ICON_PX, Bitmap.Config.ARGB_8888);
// Fit, centred: a 16:9 banner keeps its shape instead of being squashed square.
int w = d.getIntrinsicWidth(), h = d.getIntrinsicHeight();
if (w > 0 && h > 0 && w != h) {
@@ -100,27 +125,26 @@ public final class Labels {
d.draw(new Canvas(b));
ByteArrayOutputStream png = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, png);
- b.recycle();
return Base64.encodeToString(png.toByteArray(), Base64.NO_WRAP);
- } catch (Exception e) {
- return null; // the label is still worth sending
+ } finally {
+ b.recycle();
}
}
/** JSON string, ASCII only: anything outside printable ASCII is a \\u escape. */
- private static void string(PrintStream out, String s) {
- out.print('"');
+ private static void string(StringBuilder out, String s) {
+ out.append('"');
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '"' || c == '\\') {
- out.print('\\');
- out.print(c);
+ out.append('\\');
+ out.append(c);
} else if (c < 0x20 || c > 0x7e) {
- out.print(String.format("\\u%04x", (int) c));
+ out.append(String.format("\\u%04x", (int) c));
} else {
- out.print(c);
+ out.append(c);
}
}
- out.print('"');
+ out.append('"');
}
}
diff --git a/app/src/main/java/app/tellyscope/data/AppNames.kt b/app/src/main/java/app/tellyscope/data/AppNames.kt
index 511fbae..0c6814a 100644
--- a/app/src/main/java/app/tellyscope/data/AppNames.kt
+++ b/app/src/main/java/app/tellyscope/data/AppNames.kt
@@ -24,6 +24,8 @@ class AppNames(private val dir: File) {
private val refused = mutableSetOf<String>()
/** Asked for and not described (no readable app info): don't run the helper again just for these. */
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>>()
/**
* [tv] identifies the TV (its [historyKey]); [packages] is `pm list packages --show-versioncode`.
@@ -39,10 +41,13 @@ class AppNames(private val dir: File) {
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.
- (hit == null || (p.versionCode != null && p.versionCode != hit.versionCode)) && (tv to p.name) !in unanswered
+ val outdated = hit == null || (p.versionCode != null && p.versionCode != hit.versionCode)
+ // A run where the icon failed once mustn't cost the app its icon until its next update.
+ (outdated || (hit?.iconPng == null && (tv to p.name) !in asked)) && (tv to p.name) !in unanswered
}
if (stale.isNotEmpty() && tv !in refused) {
val fresh = describe(stale.map { it.name })
+ stale.forEach { asked += tv to it.name }
if (fresh == null) {
refused += tv
} else {
diff --git a/app/src/test/java/app/tellyscope/AppNamesTest.kt b/app/src/test/java/app/tellyscope/AppNamesTest.kt
index 2939042..90cb863 100644
--- a/app/src/test/java/app/tellyscope/AppNamesTest.kt
+++ b/app/src/test/java/app/tellyscope/AppNamesTest.kt
@@ -71,6 +71,19 @@ class AppNamesTest {
assertEquals(listOf("com.netflix.ninja", "com.google.android.youtube.tv"), helper.runs.last())
}
+ @Test fun anIconlessEntryIsAskedForOnceMorePerLaunch() = runBlocking {
+ // One run lost Netflix's icon; the cache kept the label without it.
+ AppNames(tmp.root).names("SN1", listOf(netflix, odd), { listOf(AppLabel(netflix.name, "Netflix", 10, null), AppLabel(odd.name, "Weather", 3, icon)) })
+ val helper = Helper().apply { versions = mutableMapOf(netflix.name to 10) }
+ val later = AppNames(tmp.root)
+ val names = later.names("SN1", listOf(netflix, odd), helper::describe)
+ assertEquals(listOf(listOf(netflix.name)), helper.runs)
+ assertArrayEquals(icon, names.getValue(netflix.name).icon)
+ // Asked once this launch: not again, even if that run had also come back without one.
+ later.names("SN1", listOf(netflix, odd), helper::describe)
+ assertEquals(1, helper.runs.size)
+ }
+
@Test fun cacheIsPerTv() = runBlocking {
val helper = Helper()
val appNames = AppNames(tmp.root)
@@ -89,7 +102,7 @@ class AppNamesTest {
}
@Test fun damagedCacheLinesCostOnlyThatApp() = runBlocking {
- File(tmp.root, "SN1.tsv").writeText("com.netflix.ninja\t10\tNetflix\t\ngarbage line\ncom.example.weatherbox\tx\tW\t\n")
+ File(tmp.root, "SN1.tsv").writeText("com.netflix.ninja\t10\tNetflix\tAQID\ngarbage line\ncom.example.weatherbox\tx\tW\t\n")
val helper = Helper()
val names = AppNames(tmp.root).names("SN1", listOf(netflix, odd), helper::describe)
assertEquals("Netflix", names.getValue("com.netflix.ninja").label)