diff --git a/android/app/src/main/java/dev/ultra/launcher/MainActivity.java b/android/app/src/main/java/dev/ultra/launcher/MainActivity.java index 838bf9d..624b82e 100644 --- a/android/app/src/main/java/dev/ultra/launcher/MainActivity.java +++ b/android/app/src/main/java/dev/ultra/launcher/MainActivity.java @@ -25,6 +25,7 @@ import android.os.Process; import android.os.UserHandle; import android.provider.Settings; import android.text.format.DateFormat; +import android.util.Log; import android.view.HapticFeedbackConstants; import android.view.MotionEvent; import android.view.View; @@ -240,21 +241,41 @@ public class MainActivity extends Activity { 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) { byte[] png = icons.get(c); 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); if (cn == null) return null; LauncherActivityInfo info = launcher.resolveActivity( new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(cn), user); if (info == null) return null; int dpi = getResources().getDisplayMetrics().densityDpi; - png = toPng(info.getIcon(dpi), Math.round(ICON_DP * dpi / 160f)); - icons.put(c, png); - return png; + Drawable d = info.getIcon(dpi); + if (d == null) d = getPackageManager().getDefaultActivityIcon(); + return toPng(d, Math.round(ICON_DP * dpi / 160f)); } 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); d.setBounds(0, 0, size, size); d.draw(new Canvas(bmp)); @@ -299,14 +320,33 @@ public class MainActivity extends Activity { String k = "w:" + p; byte[] png = icons.get(k); if (png != null) return png; - AppWidgetProviderInfo w = provider(p); - if (w == null) return null; - int dpi = getResources().getDisplayMetrics().densityDpi; - Drawable d = w.loadIcon(this, dpi); - if (d == null) return null; - png = toPng(d, Math.round(ICON_DP * dpi / 160f)); - icons.put(k, png); - return png; + synchronized (iconLock) { + AppWidgetProviderInfo w = provider(p); + if (w == null) return null; + int dpi = getResources().getDisplayMetrics().densityDpi; + Drawable d = null; + try { + d = w.loadIcon(this, dpi); + } 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. */ @@ -556,6 +596,7 @@ public class MainActivity extends Activity { return respond(mime(name), getAssets().open(name)); } } catch (IOException | JSONException | RuntimeException e) { + Log.w("ultra", "serving " + path, e); return notFound(); } } diff --git a/web/index.html b/web/index.html index 16dd191..c1348b5 100644 --- a/web/index.html +++ b/web/index.html @@ -81,6 +81,21 @@ widgetMenu: (id) => app.dispatch(WIDGET_MENU, id), 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(); }); addEventListener("scroll", () => requestAnimationFrame(place), { passive: true }); addEventListener("resize", () => requestAnimationFrame(place)); diff --git a/web/style.css b/web/style.css index 5a4a32a..c0c1a54 100644 --- a/web/style.css +++ b/web/style.css @@ -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); } .icon { width: 3.25rem; height: 3.25rem; object-fit: contain; pointer-events: none; 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; text-shadow: var(--shadow-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }