Launcher: fix broken app and widget icons

The WebView requests a screenful of icons at once on several threads,
and loading and drawing icon drawables concurrently failed for that first
batch (and for every widget icon). Icons are now drawn one at a time
under a lock from a mutated drawable, widget icons fall back to the app
icon, and failures are logged under the "ultra" tag. The page retries a
failed icon twice and then shows a blank disc instead of a broken image.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019EEjkJVV72Lu8ndDVWg5BT
This commit is contained in:
Claude committed 2026-10-21 18:43:08 +00:00
1 parent 3ed5afbafc
commit 70fc005e90
3 files changed
+68 -11

No files matched your search

@@ -25,6 +25,7 @@ import android.os.Process;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@@ -240,21 +241,41 @@ public class MainActivity extends Activity {
return new JSONObject().put("apps", apps).toString(); return new JSONObject().put("apps", apps).toString();
} }
// Icons are drawn one at a time: the WebView asks for a screenful at once on several
// threads, and some icon drawables don't survive being loaded and drawn concurrently.
private final Object iconLock = new Object();
private byte[] iconPng(String c) { private byte[] iconPng(String c) {
byte[] png = icons.get(c); byte[] png = icons.get(c);
if (png != null) return png; if (png != null) return png;
synchronized (iconLock) {
png = icons.get(c);
if (png != null) return png;
try {
png = drawIcon(c);
} catch (RuntimeException | OutOfMemoryError e) {
Log.w("ultra", "icon for " + c, e);
return null;
}
if (png != null) icons.put(c, png);
return png;
}
}
private byte[] drawIcon(String c) {
ComponentName cn = c == null ? null : ComponentName.unflattenFromString(c); ComponentName cn = c == null ? null : ComponentName.unflattenFromString(c);
if (cn == null) return null; if (cn == null) return null;
LauncherActivityInfo info = launcher.resolveActivity( LauncherActivityInfo info = launcher.resolveActivity(
new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(cn), user); new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(cn), user);
if (info == null) return null; if (info == null) return null;
int dpi = getResources().getDisplayMetrics().densityDpi; int dpi = getResources().getDisplayMetrics().densityDpi;
png = toPng(info.getIcon(dpi), Math.round(ICON_DP * dpi / 160f)); Drawable d = info.getIcon(dpi);
icons.put(c, png); if (d == null) d = getPackageManager().getDefaultActivityIcon();
return png; return toPng(d, Math.round(ICON_DP * dpi / 160f));
} }
private static byte[] toPng(Drawable d, int size) { private static byte[] toPng(Drawable d, int size) {
d = d.mutate(); // setBounds on a drawable shared with other callers would move theirs
Bitmap bmp = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Bitmap bmp = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
d.setBounds(0, 0, size, size); d.setBounds(0, 0, size, size);
d.draw(new Canvas(bmp)); d.draw(new Canvas(bmp));
@@ -299,14 +320,33 @@ public class MainActivity extends Activity {
String k = "w:" + p; String k = "w:" + p;
byte[] png = icons.get(k); byte[] png = icons.get(k);
if (png != null) return png; if (png != null) return png;
AppWidgetProviderInfo w = provider(p); synchronized (iconLock) {
if (w == null) return null; AppWidgetProviderInfo w = provider(p);
int dpi = getResources().getDisplayMetrics().densityDpi; if (w == null) return null;
Drawable d = w.loadIcon(this, dpi); int dpi = getResources().getDisplayMetrics().densityDpi;
if (d == null) return null; Drawable d = null;
png = toPng(d, Math.round(ICON_DP * dpi / 160f)); try {
icons.put(k, png); d = w.loadIcon(this, dpi);
return png; } catch (RuntimeException e) {
Log.w("ultra", "widget icon for " + p, e);
}
if (d == null) {
// Fall back to the app's own icon, then to the system default.
try {
d = getPackageManager().getApplicationIcon(w.provider.getPackageName());
} catch (android.content.pm.PackageManager.NameNotFoundException | RuntimeException e) {
d = getPackageManager().getDefaultActivityIcon();
}
}
try {
png = toPng(d, Math.round(ICON_DP * dpi / 160f));
} catch (RuntimeException | OutOfMemoryError e) {
Log.w("ultra", "drawing widget icon for " + p, e);
return null;
}
icons.put(k, png);
return png;
}
} }
/** Bind a widget for provider p, then configure it if it asks to be; see onActivityResult. */ /** Bind a widget for provider p, then configure it if it asks to be; see onActivityResult. */
@@ -556,6 +596,7 @@ public class MainActivity extends Activity {
return respond(mime(name), getAssets().open(name)); return respond(mime(name), getAssets().open(name));
} }
} catch (IOException | JSONException | RuntimeException e) { } catch (IOException | JSONException | RuntimeException e) {
Log.w("ultra", "serving " + path, e);
return notFound(); return notFound();
} }
} }
+15
View File
@@ -81,6 +81,21 @@
widgetMenu: (id) => app.dispatch(WIDGET_MENU, id), widgetMenu: (id) => app.dispatch(WIDGET_MENU, id),
place: () => { placed = ""; place(); }, place: () => { placed = ""; place(); },
}; };
// An icon that fails to load is retried twice with backoff, then drawn as a blank
// disc instead of the browser's broken-image box.
document.addEventListener("error", (e) => {
const img = e.target;
if (!(img instanceof HTMLImageElement) || !img.classList.contains("icon")) return;
const n = (img.__retries = (img.__retries || 0) + 1);
if (n > 2) { img.classList.add("missing"); return; }
setTimeout(() => {
const base = img.getAttribute("src").replace(/[?&]retry=\d+$/, "");
img.src = base + (base.includes("?") ? "&" : "?") + "retry=" + n;
}, 300 * n);
}, true);
document.addEventListener("load", (e) => {
if (e.target instanceof HTMLImageElement) e.target.classList.remove("missing");
}, true);
document.addEventListener("visibilitychange", () => { if (!document.hidden) window.ultraHome.tick(); }); document.addEventListener("visibilitychange", () => { if (!document.hidden) window.ultraHome.tick(); });
addEventListener("scroll", () => requestAnimationFrame(place), { passive: true }); addEventListener("scroll", () => requestAnimationFrame(place), { passive: true });
addEventListener("resize", () => requestAnimationFrame(place)); addEventListener("resize", () => requestAnimationFrame(place));
+1
View File
@@ -34,6 +34,7 @@ body { margin: 0; min-height: 100vh; color: #fff; -webkit-tap-highlight-color: t
.tile:active { background: rgb(255 255 255 / .12); } .tile:active { background: rgb(255 255 255 / .12); }
.icon { width: 3.25rem; height: 3.25rem; object-fit: contain; pointer-events: none; .icon { width: 3.25rem; height: 3.25rem; object-fit: contain; pointer-events: none;
filter: drop-shadow(0 2px 4px rgb(0 0 0 / .3)); } filter: drop-shadow(0 2px 4px rgb(0 0 0 / .3)); }
.icon.missing { border-radius: 999px; background: rgb(255 255 255 / .18); object-position: -999px 0; }
.name { max-width: 100%; font-size: .75rem; line-height: 1.2; text-align: center; .name { max-width: 100%; font-size: .75rem; line-height: 1.2; text-align: center;
text-shadow: var(--shadow-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } text-shadow: var(--shadow-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }