Improve notification-icon display handling

This commit is contained in:
James Babcock committed 2026-10-25 00:36:51 -07:00
1 parent ae48313b81
commit ce8b7656f4
6 files changed
+843 -186

No files matched your search

@@ -5,7 +5,6 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
@@ -30,17 +29,8 @@ import org.json.JSONObject
class FaceclawMediaNotificationListenerService : NotificationListenerService() {
companion object {
private const val TAG = "FaceclawNotify"
private const val NOTIFICATION_ICON_GAMMA = 1.6
/** Alpha at or below this is treated as cut out when classifying an icon. */
private const val TEMPLATE_ALPHA_FLOOR = 32
/** Spread between an icon pixel's brightest and darkest channel that reads as hue. */
private const val TEMPLATE_CHROMA_THRESHOLD = 24
private const val EXTRA_SUBSTITUTE_APP_NAME = "android.substName"
/** Set once the debug-icon directory has been cleaned for this process. */
@Volatile
private var debugIconsPruned = false
@Volatile
private var activeService: FaceclawMediaNotificationListenerService? = null
private val mainHandler = Handler(Looper.getMainLooper())
@@ -136,15 +126,16 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) {
continue
}
val drawable = loadNotificationIcon(service, statusBarNotification.notification)
if (drawable == null) {
Log.i(TAG, "icon skipped (no drawable): " + statusBarNotification.packageName)
val icon = NotificationIconLoader.iconFor(service, statusBarNotification, size) {
getNotificationAppName(service, statusBarNotification)
}
if (icon == null) {
Log.i(TAG, "icon skipped (nothing to draw): " + statusBarNotification.packageName)
continue
}
Log.i(TAG, "icon[" + emitted + "] pkg=" + statusBarNotification.packageName
+ " drawable=" + drawable.javaClass.simpleName
+ " intrinsic=" + drawable.intrinsicWidth + "x" + drawable.intrinsicHeight)
appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.packageName)
+ " source=" + icon.source + " kind=" + icon.art.kind)
out.write(icon.art.gray)
keysOut?.add(statusBarNotification.key ?: "")
if (dedupeGroupKey != null) {
emittedGroupKeys.add(dedupeGroupKey)
@@ -174,13 +165,10 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
if (statusBarNotification == null || statusBarNotification.notification == null) {
return ByteArray(0)
}
val drawable = loadNotificationIcon(service, statusBarNotification.notification)
if (drawable == null) {
return ByteArray(0)
val icon = NotificationIconLoader.iconFor(service, statusBarNotification, size) {
getNotificationAppName(service, statusBarNotification)
}
val out = ByteArrayOutputStream(size * size)
appendIconGrayBytes(drawable, size, out, service, -1, statusBarNotification.packageName)
return out.toByteArray()
return icon?.art?.gray ?: ByteArray(0)
}
/**
@@ -442,133 +430,6 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
return groupKey
}
private fun loadNotificationIcon(service: FaceclawMediaNotificationListenerService, notification: Notification): Drawable? {
try {
val smallIcon: Icon? = notification.smallIcon
if (smallIcon != null) {
val drawable: Drawable? = smallIcon.loadDrawable(service)
if (drawable != null) {
// Template or not is decided from the rendered pixels
// (see iconIsAlphaTemplate): apps also ship filled color
// bitmaps as their small icon, and tinting one of those
// white keeps the alpha while discarding the shape that
// the colors carried, leaving a solid block.
drawable.mutate()
if (drawable is BitmapDrawable) {
drawable.setFilterBitmap(true)
}
return drawable
}
}
} catch (t: Throwable) {
Log.w(TAG, "failed to load small notification icon", t)
}
try {
// Large icons are real color images (avatars, album art); keep color.
val largeIcon: Icon? = notification.getLargeIcon()
if (largeIcon != null) {
return largeIcon.loadDrawable(service)
}
} catch (t: Throwable) {
Log.w(TAG, "failed to load large notification icon", t)
}
return null
}
private fun appendIconGrayBytes(drawable: Drawable, size: Int, out: ByteArrayOutputStream,
service: FaceclawMediaNotificationListenerService, index: Int, packageName: String?) {
val bitmap = renderIconScaled(drawable, size)
val template = iconIsAlphaTemplate(bitmap)
dumpIconDebugPng(service, index, packageName, bitmap)
for (y in 0 until size) {
for (x in 0 until size) {
val color = bitmap.getPixel(x, y)
val alpha = Color.alpha(color)
val grayLinear = if (template) {
alpha / 255.0
} else {
(0.2126 * Color.red(color) + 0.7152 * Color.green(color) + 0.0722 * Color.blue(color)) * alpha / (255.0 * 255.0)
}
val gray = Math.round(255.0 * Math.pow(Math.max(0.0, Math.min(1.0, grayLinear)), NOTIFICATION_ICON_GAMMA)).toInt()
out.write(gray and 0xff)
}
}
bitmap.recycle()
}
/**
* Whether a rendered icon is an alpha template: a shape the platform
* draws by tinting, where alpha alone carries the outline.
*
* Small icons are meant to be templates, but plenty of apps put a filled
* color bitmap there instead. Tinting one of those white keeps its alpha
* and throws away the only thing that carried the shape, so the glasses
* got a blank block where the icon should be.
*
* Hue settles it: an icon with color is color artwork. A colorless icon is
* a template only when its alpha is actually cut out, and a colorless
* icon with solid alpha is a flat image its own brightness has to carry.
*/
private fun iconIsAlphaTemplate(bitmap: Bitmap): Boolean {
var opaque = 0
var transparent = 0
var colored = 0
for (y in 0 until bitmap.height) {
for (x in 0 until bitmap.width) {
val color = bitmap.getPixel(x, y)
val alpha = Color.alpha(color)
if (alpha < TEMPLATE_ALPHA_FLOOR) {
transparent += 1
continue
}
opaque += 1
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
val high = Math.max(red, Math.max(green, blue))
val low = Math.min(red, Math.min(green, blue))
if (high - low >= TEMPLATE_CHROMA_THRESHOLD) colored += 1
}
}
if (opaque == 0) return false
// A quarter of the icon carrying hue is color artwork, whatever its alpha does.
if (colored * 4 >= opaque) return false
// Otherwise it is a template only if there is something cut out of it.
return transparent * 20 >= opaque + transparent
}
/**
* Render a drawable at the target size with proper downscaling. Detailed
* sources (e.g. avatar bitmaps used as notification icons) are rendered at
* native resolution and reduced by repeated halving: a single filtered pass
* from, say, 126px to 24px samples too sparsely and turns fine detail into
* speckle that reads as a garbled icon.
*/
private fun renderIconScaled(drawable: Drawable, size: Int): Bitmap {
val renderW = Math.max(size, drawable.intrinsicWidth)
val renderH = Math.max(size, drawable.intrinsicHeight)
var bitmap = Bitmap.createBitmap(renderW, renderH, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, renderW, renderH)
drawable.draw(canvas)
while (bitmap.width >= size * 2 && bitmap.height >= size * 2) {
val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true)
bitmap.recycle()
bitmap = halved
}
if (bitmap.width != size || bitmap.height != size) {
val scaled = Bitmap.createScaledBitmap(bitmap, size, size, true)
bitmap.recycle()
bitmap = scaled
}
return bitmap
}
private fun isDebuggable(service: FaceclawMediaNotificationListenerService): Boolean {
val flags = service.applicationInfo.flags
return (flags and android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE) != 0
}
/** Whether a notification carries a picture for loadNotificationPicture, without loading it. */
@Suppress("DEPRECATION")
private fun hasNotificationPicture(extras: Bundle): Boolean {
@@ -627,7 +488,7 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
* Halve a picture while it is still at least twice the size it will
* be shown at, leaving the last (under 2x) step of fitting it into
* maxWidth x maxHeight to bitmapToGrayPacket. As with icons
* (renderIconScaled), one filtered pass from a much larger source
* (NotificationIconLoader.renderArgb), one filtered pass from a much larger source
* samples too sparsely: a graph's thin lines break up into dots.
*/
private fun halveToward(source: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap {
@@ -642,40 +503,6 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
return bitmap
}
/**
* Debug aid for garbled-icon reports: saves each rendered icon to
* <externalFilesDir>/debug-icons/ (adb-pullable) so extraction problems can
* be told apart from downstream compositing/transmission problems. Cheap:
* runs at most once per icon-cache refresh on tiny bitmaps.
*/
private fun dumpIconDebugPng(service: FaceclawMediaNotificationListenerService, index: Int, packageName: String?, bitmap: Bitmap) {
if (index < 0) {
return
}
// Debug aid only. Nobody can pull these files out of a release build,
// and the directory kept one file per icon per index forever.
if (!isDebuggable(service)) {
return
}
try {
val dir = java.io.File(service.getExternalFilesDir(null), "debug-icons")
if (!dir.exists() && !dir.mkdirs()) {
return
}
if (!debugIconsPruned) {
debugIconsPruned = true
dir.listFiles()?.forEach { stale -> stale.delete() }
}
val safeName = if (packageName == null) "unknown" else packageName.replace(Regex("[^A-Za-z0-9._-]"), "_")
val file = java.io.File(dir, "icon-$index-$safeName.png")
java.io.FileOutputStream(file).use { stream ->
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
}
} catch (t: Throwable) {
Log.w(TAG, "failed to dump debug icon", t)
}
}
private fun findActiveNotificationByKey(service: FaceclawMediaNotificationListenerService?, key: String?): StatusBarNotification? {
if (service == null || key == null || key.isEmpty()) {
return null
@@ -0,0 +1,290 @@
package com.faceclaw.app
import android.app.Notification
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.AdaptiveIconDrawable
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
import android.os.Build
import android.os.SystemClock
import android.service.notification.StatusBarNotification
import android.util.Log
import com.faceclaw.app.NotificationIconArt.Kind
import java.io.File
import java.io.FileOutputStream
import kotlin.math.max
import kotlin.math.roundToInt
/**
* Picks and renders the glasses' status-bar icon for a notification (see iconFor).
* NotificationIconArt does the pixel analysis; this object owns the drawables, package lookups,
* caching and the debug dump.
*/
internal object NotificationIconLoader {
private const val TAG = "FaceclawNotifyIcon"
/** AdaptiveIconDrawable draws its layers this share of the icon's size past each edge. */
private const val ADAPTIVE_LAYER_OUTSET = 0.25f
private const val CACHE_MAX_ENTRIES = 128
/** Cached art is redone after this long, so an updated app's new icon shows up. */
private const val CACHE_MAX_AGE_MS = 30 * 60 * 1000L
/** The debug dump keeps at most this many files, deleting the oldest. */
private const val DEBUG_DUMP_MAX_FILES = 96
class Rendered(
/** The drawable it came from: small, monochrome, launcher, foreground, large or letter. */
val source: String,
val art: NotificationIconArt.Art,
)
/**
* An app's launcher icon art: its themed monochrome layer, else a glyph or shape read from
* the icon (or its adaptive foreground layer), else the icon read by brightness.
*/
private class LauncherArt(val monochrome: Rendered?, val shape: Rendered?, val picture: Rendered?)
private val smallIconCache = ArtCache<Rendered?>(CACHE_MAX_ENTRIES, CACHE_MAX_AGE_MS)
private val launcherCache = ArtCache<LauncherArt>(CACHE_MAX_ENTRIES, CACHE_MAX_AGE_MS)
/**
* The icon for a notification, size x size gray, from the first of these that works:
* 1. the small icon, when its alpha carries a shape (nearly every app);
* 2. the launcher icon's themed monochrome layer (Android 13+), which is made to be tinted;
* 3. the small icon's glyph, when it is a color tile with one background color;
* 4. the launcher icon's glyph or shape, or its adaptive foreground layer's shape;
* 5. the small icon, then the launcher icon, read by brightness;
* 6. the large icon (usually the sender's avatar: the notification's content, not its app);
* 7. the app name's initial in a rounded box.
* Null only when even the initial can't be drawn.
*/
fun iconFor(context: Context, statusBarNotification: StatusBarNotification, size: Int, appName: () -> String): Rendered? {
val packageName = statusBarNotification.packageName ?: ""
val notification = statusBarNotification.notification
val small = smallIconArt(context, packageName, notification.smallIcon, size)
val smallKind = small?.art?.kind
if (smallKind == Kind.TEMPLATE || smallKind == Kind.SOLID) return small
val launcher = launcherArt(context, packageName, size)
launcher.monochrome?.let { return it }
if (smallKind == Kind.GLYPH) return small
launcher.shape?.let { return it }
if (smallKind == Kind.PICTURE) return small
launcher.picture?.let { return it }
largeIconArt(context, packageName, notification, size)?.let { return it }
return letterArt(appName(), size)
}
private fun smallIconArt(context: Context, packageName: String, icon: Icon?, size: Int): Rendered? {
if (icon == null) return null
val key = resourceKey(icon) ?: return loadSmallIcon(context, packageName, icon, size)
return smallIconCache.getOrPut("$packageName|$key|$size") { loadSmallIcon(context, packageName, icon, size) }
}
/** Resource icons are cached by resource; bitmap and URI icons are converted every time. */
private fun resourceKey(icon: Icon): String? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P || icon.type != Icon.TYPE_RESOURCE) return null
return icon.resPackage + "/" + icon.resId
}
private fun loadSmallIcon(context: Context, packageName: String, icon: Icon, size: Int): Rendered? =
tryRender("small", packageName) {
val drawable = icon.loadDrawable(context) ?: return@tryRender null
// A tint (the Icon's own, or a vector's theme tint) recolors the very pixels that tell
// a template from a color tile, and can fade a template's alpha. Templates are read
// by alpha alone, so dropping it costs nothing.
drawable.mutate()
drawable.setTintList(null)
renderSource(context, packageName, "small", drawable, size)
}
private fun launcherArt(context: Context, packageName: String, size: Int): LauncherArt =
launcherCache.getOrPut("$packageName|$size") { loadLauncherArt(context, packageName, size) }
private fun loadLauncherArt(context: Context, packageName: String, size: Int): LauncherArt {
val icon: Drawable = try {
context.packageManager.getApplicationIcon(packageName)
} catch (t: Throwable) {
Log.w(TAG, "no launcher icon for $packageName", t)
return LauncherArt(null, null, null)
}
val adaptive = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) icon as? AdaptiveIconDrawable else null
if (adaptive != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val layer = adaptive.monochrome
val monochrome = if (layer == null) null else tryRender("monochrome", packageName) {
renderSource(context, packageName, "monochrome", layer, size, ADAPTIVE_LAYER_OUTSET, template = true)
}
if (monochrome != null) return LauncherArt(monochrome, null, null)
}
val whole = tryRender("launcher", packageName) { renderSource(context, packageName, "launcher", icon, size) }
var shape = whole?.takeIf { it.art.kind == Kind.TEMPLATE || it.art.kind == Kind.GLYPH }
val foreground = adaptive?.foreground
if (shape == null && foreground != null) {
// A gradient or picture background hides the glyph; the foreground layer alone may carry it as alpha.
shape = tryRender("foreground", packageName) {
renderSource(context, packageName, "foreground", foreground, size, ADAPTIVE_LAYER_OUTSET)
}?.takeIf { it.art.kind == Kind.TEMPLATE }
}
return LauncherArt(null, shape, whole?.takeIf { it.art.kind == Kind.PICTURE })
}
private fun largeIconArt(context: Context, packageName: String, notification: Notification, size: Int): Rendered? =
tryRender("large", packageName) {
val drawable = notification.getLargeIcon()?.loadDrawable(context) ?: return@tryRender null
renderSource(context, packageName, "large", drawable, size)
}
/** The app name's first letter or digit in a rounded box, like the shell's letter window icons. */
private fun letterArt(appName: String, size: Int): Rendered? = tryRender("letter", appName) {
val letter = appName.firstOrNull { it.isLetterOrDigit() }?.uppercaseChar()?.toString() ?: "?"
val canvasSize = size * NotificationIconArt.ANALYSIS_SCALE
val bitmap = Bitmap.createBitmap(canvasSize, canvasSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val stroke = NotificationIconArt.ANALYSIS_SCALE.toFloat()
val box = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = stroke
color = Color.argb(160, 255, 255, 255)
}
val radius = canvasSize / 4f
canvas.drawRoundRect(stroke / 2, stroke / 2, canvasSize - stroke / 2, canvasSize - stroke / 2, radius, radius, box)
val text = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
typeface = Typeface.DEFAULT_BOLD
textSize = canvasSize * 0.6f
textAlign = Paint.Align.CENTER
}
val bounds = Rect()
text.getTextBounds(letter, 0, letter.length, bounds)
canvas.drawText(letter, canvasSize / 2f, canvasSize / 2f - bounds.exactCenterY(), text)
val argb = IntArray(canvasSize * canvasSize)
bitmap.getPixels(argb, 0, canvasSize, 0, 0, canvasSize, canvasSize)
bitmap.recycle()
val art = NotificationIconArt.template(argb, size)
if (art.kind == Kind.EMPTY) null else Rendered("letter", art)
}
private inline fun tryRender(source: String, owner: String, render: () -> Rendered?): Rendered? {
return try {
render()
} catch (t: Throwable) {
Log.w(TAG, "failed to render $source icon for $owner", t)
null
}
}
/** Render, dump and convert one source drawable; null when nothing visible came out. */
private fun renderSource(
context: Context, packageName: String, source: String, drawable: Drawable, size: Int,
outset: Float = 0f, template: Boolean = false,
): Rendered? {
val analysisSize = size * NotificationIconArt.ANALYSIS_SCALE
val argb = renderArgb(drawable, analysisSize, outset)
dumpDebugPng(context, packageName, source, argb, analysisSize)
val art = if (template) NotificationIconArt.template(argb, size) else NotificationIconArt.convert(argb, size)
return if (art.kind == Kind.EMPTY) null else Rendered(source, art)
}
/**
* Render a drawable to target x target ARGB with proper downscaling. Detailed sources (e.g.
* avatar bitmaps used as notification icons) are rendered at native resolution and reduced
* by repeated halving: a single filtered pass from, say, 126px to 24px samples too sparsely
* and turns fine detail into speckle that reads as a garbled icon. An outset draws the
* drawable that share of the canvas past each edge, as AdaptiveIconDrawable draws its layers.
*/
private fun renderArgb(drawable: Drawable, target: Int, outset: Float): IntArray {
drawable.mutate()
if (drawable is BitmapDrawable) {
drawable.setFilterBitmap(true)
}
val scale = 1f + 2f * outset
val renderW = max(target, (drawable.intrinsicWidth / scale).roundToInt())
val renderH = max(target, (drawable.intrinsicHeight / scale).roundToInt())
var bitmap = Bitmap.createBitmap(renderW, renderH, Bitmap.Config.ARGB_8888)
val dx = (renderW * outset).roundToInt()
val dy = (renderH * outset).roundToInt()
drawable.setBounds(-dx, -dy, renderW + dx, renderH + dy)
drawable.draw(Canvas(bitmap))
while (bitmap.width >= target * 2 && bitmap.height >= target * 2) {
val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true)
bitmap.recycle()
bitmap = halved
}
if (bitmap.width != target || bitmap.height != target) {
val scaled = Bitmap.createScaledBitmap(bitmap, target, target, true)
bitmap.recycle()
bitmap = scaled
}
val pixels = IntArray(target * target)
bitmap.getPixels(pixels, 0, target, 0, 0, target, target)
bitmap.recycle()
return pixels
}
/**
* Debug aid for icon reports: saves each source as rendered for analysis, before any
* conversion, to <externalFilesDir>/debug-icons/<package>-<source>.png (adb-pullable), so a
* bad icon can be rerun through NotificationIconArt off-device. Runs only when a source is
* converted, not on cached icons; the oldest files past DEBUG_DUMP_MAX_FILES are deleted.
*/
private fun dumpDebugPng(context: Context, packageName: String, source: String, argb: IntArray, size: Int) {
try {
val base = context.getExternalFilesDir(null) ?: return
val dir = File(base, "debug-icons")
if (!dir.exists() && !dir.mkdirs()) {
return
}
val safeName = packageName.replace(Regex("[^A-Za-z0-9._-]"), "_")
val bitmap = Bitmap.createBitmap(argb, size, size, Bitmap.Config.ARGB_8888)
try {
FileOutputStream(File(dir, "$safeName-$source.png")).use { stream ->
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
}
} finally {
bitmap.recycle()
}
val files = dir.listFiles() ?: return
if (files.size > DEBUG_DUMP_MAX_FILES) {
files.sortBy { it.lastModified() }
for (index in 0 until files.size - DEBUG_DUMP_MAX_FILES) {
files[index].delete()
}
}
} catch (t: Throwable) {
Log.w(TAG, "failed to dump debug icon", t)
}
}
/** A small LRU of converted art with an age limit. Conversions run under its lock. */
private class ArtCache<V>(private val maxEntries: Int, private val maxAgeMs: Long) {
private class Entry<T>(val value: T, val atMs: Long)
private val entries = object : LinkedHashMap<String, Entry<V>>(16, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, Entry<V>>): Boolean {
return size > maxEntries
}
}
@Synchronized
fun getOrPut(key: String, compute: () -> V): V {
val now = SystemClock.elapsedRealtime()
val cached = entries[key]
if (cached != null && now - cached.atMs < maxAgeMs) {
return cached.value
}
val value = compute()
entries[key] = Entry(value, now)
return value
}
}
}
+1
View File
@@ -11,6 +11,7 @@
### Bug fixes
* Inverting Android notification-icons to indicate selectionis now gamma-correct
* Android notification icons that are full-color pictures rather than shapes no longer show as blank blocks
## 0.8.4 - 2026-10-17
+2 -2
View File
@@ -12,8 +12,8 @@ local `@faceclaw/kotlin` plugin. Generated binaries and Gradle caches are ignore
the Android adapters: `util/` (Json codec, FrameTimingsCore, concurrency
primitives, sha256/clock/log/file expects), `g2protocol/FirmwareImage`,
`audio/` (Lc3PacketFramer, AudioSegmentation, VoicePrint, WavPcmReader),
`graphics/` (GrayPacket, PreviewPalette, StatusIconArt, GlyphPacket,
OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream,
`graphics/` (GrayPacket, PreviewPalette, StatusIconArt, NotificationIconArt,
GlyphPacket, OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream,
ResumableDownload, RemoteInputSession), `evenhub/EvenHubAssetServer`,
`alarms/` (AlarmSchedule, AlarmRingingPolicy), `settings/SettingsChangeHub`,
the stock-firmware flows over a `StockLink` GATT port (`g2protocol/StockLinkSession`,
@@ -0,0 +1,378 @@
package com.faceclaw.app
import kotlin.jvm.JvmStatic
import kotlin.math.abs
import kotlin.math.cbrt
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.math.sqrt
/**
* Turns a rendered notification icon into the lit-on-black gray silhouette the glasses' status
* bar shows: outputSize x outputSize bytes, 0 = off. Platform code picks the source drawables
* (small icon, launcher icon layers, large icon) and renders them to ARGB at [ANALYSIS_SCALE]
* times the output size; this object owns the pixel analysis, so the classification can be
* tested off-device.
*
* Small icons are supposed to be alpha templates: the platform tints them and ignores their
* color channels, which apps fill with anything (a brand color, Discord's noise, a theme tint).
* Some apps instead ship a filled color tile whose alpha is just a square or circle, with the
* shape carried by the colors; tinted, those become a blank block. The alpha's own shape tells
* the two apart, never the colors: alpha with holes or concavities is a template and is read by
* alpha alone, while a plain convex tile has its glyph pulled out of the tile's background color.
*/
object NotificationIconArt {
/** Sources are rendered at this multiple of the output size, analyzed, then box-filtered down. */
const val ANALYSIS_SCALE = 2
/** Output gray = lit^GAMMA, for lit (coverage or glyph strength) in 0..1. */
const val GAMMA = 1.6
/** Below this peak alpha an icon counts as invisible. */
private const val MIN_VISIBLE_ALPHA = 16
/**
* A plain tile fills at least this share of the pixel centers inside its own convex hull; a
* digitized convex shape fills all of them. Templates have notches, gaps and separate parts.
*/
private const val MIN_TILE_SOLIDITY = 0.92
/** A plain tile has alpha cut-outs (holes) covering at most this share of its area. */
private const val MAX_TILE_HOLE_SHARE = 0.005
/** OKLab distance from the tile's background color that still counts as background. */
private const val BACKGROUND_TOLERANCE = 0.1
/** Share of the tile's rim that must be background-colored for the tile to have one background. */
private const val MIN_BACKGROUND_RIM_SHARE = 0.8
/** Weakest OKLab contrast between glyph and background worth extracting; below it the tile is solid. */
private const val MIN_GLYPH_CONTRAST = 0.12
/** The quantile of a tile's distances from its background taken as the glyph's color contrast. */
private const val GLYPH_CONTRAST_QUANTILE = 0.98
/** A picture whose middle 90% of brightness spans less than this is left unstretched. */
private const val MIN_PICTURE_RANGE = 0.1
enum class Kind {
/** Nothing visible. */
EMPTY,
/** Alpha carries the shape: lit by alpha alone, colors ignored. */
TEMPLATE,
/** A plain tile with no glyph on it (a filled shape): lit by alpha like a template. */
SOLID,
/** A plain tile with a glyph on a one-color background: lit by color distance from the background. */
GLYPH,
/** A plain tile with no one background color (a photo, a gradient): lit by brightness. */
PICTURE,
}
class Art(val kind: Kind, val gray: ByteArray)
/** sRGB byte value to linear light. */
private val SRGB_TO_LINEAR = DoubleArray(256) { value ->
val c = value / 255.0
if (c <= 0.04045) c / 12.92 else ((c + 0.055) / 1.055).pow(2.4)
}
/**
* Classify and convert an icon rendered at (outputSize * ANALYSIS_SCALE) square, row-major
* ARGB (not premultiplied).
*/
@JvmStatic
fun convert(argb: IntArray, outputSize: Int): Art {
val size = analysisSize(argb, outputSize)
val maxAlpha = peakAlpha(argb)
if (maxAlpha < MIN_VISIBLE_ALPHA) {
return Art(Kind.EMPTY, ByteArray(outputSize * outputSize))
}
val mask = BooleanArray(argb.size) { alpha(argb[it]) * 2 >= maxAlpha }
if (!isPlainTile(mask, size)) {
return Art(Kind.TEMPLATE, toGray(alphaLit(argb, maxAlpha), size, outputSize))
}
return readTile(argb, mask, size, maxAlpha, outputSize)
}
/**
* Convert an icon known to be an alpha template (a themed icon's monochrome layer, a drawn
* initial) without classifying it: lit by alpha alone.
*/
@JvmStatic
fun template(argb: IntArray, outputSize: Int): Art {
val size = analysisSize(argb, outputSize)
val maxAlpha = peakAlpha(argb)
if (maxAlpha < MIN_VISIBLE_ALPHA) {
return Art(Kind.EMPTY, ByteArray(outputSize * outputSize))
}
return Art(Kind.TEMPLATE, toGray(alphaLit(argb, maxAlpha), size, outputSize))
}
private fun analysisSize(argb: IntArray, outputSize: Int): Int {
val size = outputSize * ANALYSIS_SCALE
require(outputSize > 0 && argb.size == size * size) { "expected ${size}x$size pixels, got ${argb.size}" }
return size
}
/**
* Whether the alpha is a plain tile (a square, circle or squircle, at any size) rather than
* a shape: no holes worth the name, and nearly convex.
*/
private fun isPlainTile(mask: BooleanArray, size: Int): Boolean {
val opaque = mask.count { it }
if (holeCount(mask, size) > opaque * MAX_TILE_HOLE_SHARE) {
return false
}
return solidity(mask, size, opaque) >= MIN_TILE_SOLIDITY
}
/** Transparent pixels the outside can't reach through other transparent pixels (4-connected). */
private fun holeCount(mask: BooleanArray, size: Int): Int {
val outside = BooleanArray(mask.size)
val stack = IntArray(mask.size)
var top = 0
fun visit(i: Int) {
if (!mask[i] && !outside[i]) {
outside[i] = true
stack[top++] = i
}
}
for (k in 0 until size) {
visit(k)
visit((size - 1) * size + k)
visit(k * size)
visit(k * size + size - 1)
}
while (top > 0) {
val i = stack[--top]
val x = i % size
val y = i / size
if (x > 0) visit(i - 1)
if (x < size - 1) visit(i + 1)
if (y > 0) visit(i - size)
if (y < size - 1) visit(i + size)
}
var holes = 0
for (i in mask.indices) {
if (!mask[i] && !outside[i]) holes += 1
}
return holes
}
/**
* The mask's pixel count over the number of pixel centers inside its convex hull, which
* Pick's theorem gives from the hull's area and the lattice points on its edges.
*/
private fun solidity(mask: BooleanArray, size: Int, opaque: Int): Double {
// Each row's leftmost and rightmost pixels are the only hull candidates. Generated row by
// row, they are already sorted by (y, x), the order the monotone chain below works in.
val xs = IntArray(size * 2)
val ys = IntArray(size * 2)
var count = 0
for (y in 0 until size) {
var left = -1
var right = -1
for (x in 0 until size) {
if (mask[y * size + x]) {
if (left < 0) left = x
right = x
}
}
if (left < 0) continue
xs[count] = left
ys[count] = y
count += 1
if (right != left) {
xs[count] = right
ys[count] = y
count += 1
}
}
// Cross product in (y, x) coordinates, matching the sort order.
fun cross(o: Int, a: Int, b: Int): Long =
(ys[a] - ys[o]).toLong() * (xs[b] - xs[o]) - (xs[a] - xs[o]).toLong() * (ys[b] - ys[o])
val hull = IntArray(count * 2 + 1)
var k = 0
for (i in 0 until count) {
while (k >= 2 && cross(hull[k - 2], hull[k - 1], i) <= 0) k -= 1
hull[k++] = i
}
val lowerSize = k + 1
for (i in count - 2 downTo 0) {
while (k >= lowerSize && cross(hull[k - 2], hull[k - 1], i) <= 0) k -= 1
hull[k++] = i
}
// The chain ends back at its first point; with one point there is no closing repeat.
val vertices = if (count == 1) 1 else k - 1
var twiceArea = 0L
var boundary = 0L
for (v in 0 until vertices) {
val a = hull[v]
val b = hull[(v + 1) % vertices]
twiceArea += xs[a].toLong() * ys[b] - xs[b].toLong() * ys[a]
boundary += gcd(abs(xs[b] - xs[a]), abs(ys[b] - ys[a]))
}
val latticePoints = abs(twiceArea) / 2.0 + boundary / 2.0 + 1.0
return opaque / latticePoints
}
private fun gcd(a: Int, b: Int): Int {
var x = a
var y = b
while (y != 0) {
val t = x % y
x = y
y = t
}
return x
}
private fun readTile(argb: IntArray, mask: BooleanArray, size: Int, maxAlpha: Int, outputSize: Int): Art {
val lab = DoubleArray(argb.size * 3)
for (i in argb.indices) {
if (mask[i]) toOklab(argb[i], lab, i * 3)
}
// The background is the color of the tile's rim, about one output pixel deep.
val rim = rimIndices(mask, size)
val background = DoubleArray(3) { channel ->
val values = DoubleArray(rim.size) { lab[rim[it] * 3 + channel] }
values.sort()
values[values.size / 2]
}
val rimDistances = DoubleArray(rim.size) { distance(lab, rim[it], background) }
rimDistances.sort()
val uniformRim = rimDistances.count { it <= BACKGROUND_TOLERANCE }
if (uniformRim < rim.size * MIN_BACKGROUND_RIM_SHARE) {
return Art(Kind.PICTURE, toGray(pictureLit(argb, mask, maxAlpha), size, outputSize))
}
// How far the background itself wanders, and how far the glyph stands from it.
val noise = quantile(rimDistances, MIN_BACKGROUND_RIM_SHARE)
val distances = DoubleArray(argb.size)
var tileCount = 0
for (i in argb.indices) {
if (mask[i]) {
distances[i] = distance(lab, i, background)
tileCount += 1
}
}
val tileDistances = DoubleArray(tileCount)
var next = 0
for (i in argb.indices) {
if (mask[i]) tileDistances[next++] = distances[i]
}
tileDistances.sort()
val contrast = quantile(tileDistances, GLYPH_CONTRAST_QUANTILE)
if (contrast < max(MIN_GLYPH_CONTRAST, 2 * noise)) {
return Art(Kind.SOLID, toGray(alphaLit(argb, maxAlpha), size, outputSize))
}
// Ramp from background to glyph; the glyph is lit whether it is lighter or darker than the tile.
val low = max(noise, 0.25 * contrast)
val high = 0.75 * contrast
val lit = DoubleArray(argb.size) { i ->
if (!mask[i]) {
0.0
} else {
val strength = ((distances[i] - low) / (high - low)).coerceIn(0.0, 1.0)
strength * min(1.0, alpha(argb[i]).toDouble() / maxAlpha)
}
}
return Art(Kind.GLYPH, toGray(lit, size, outputSize))
}
/** Tile pixels within one output pixel of its edge (or the image's edge). */
private fun rimIndices(mask: BooleanArray, size: Int): IntArray {
var inner = mask
repeat(ANALYSIS_SCALE) { inner = erode(inner, size) }
val rim = ArrayList<Int>()
for (i in mask.indices) {
if (mask[i] && !inner[i]) rim.add(i)
}
return rim.toIntArray()
}
private fun erode(mask: BooleanArray, size: Int): BooleanArray = BooleanArray(mask.size) { i ->
val x = i % size
val y = i / size
mask[i] && x > 0 && mask[i - 1] && x < size - 1 && mask[i + 1] &&
y > 0 && mask[i - size] && y < size - 1 && mask[i + size]
}
/** Lit by brightness, stretched so the picture's middle 90% spans the full range. */
private fun pictureLit(argb: IntArray, mask: BooleanArray, maxAlpha: Int): DoubleArray {
val brightness = DoubleArray(argb.size) { i ->
val p = argb[i]
(0.2126 * ((p shr 16) and 0xff) + 0.7152 * ((p shr 8) and 0xff) + 0.0722 * (p and 0xff)) / 255.0
}
val tile = ArrayList<Double>()
for (i in argb.indices) {
if (mask[i]) tile.add(brightness[i])
}
val sorted = tile.toDoubleArray()
sorted.sort()
var low = quantile(sorted, 0.05)
var high = quantile(sorted, 0.95)
if (high - low < MIN_PICTURE_RANGE) {
low = 0.0
high = 1.0
}
return DoubleArray(argb.size) { i ->
((brightness[i] - low) / (high - low)).coerceIn(0.0, 1.0) * min(1.0, alpha(argb[i]).toDouble() / maxAlpha)
}
}
/** Lit by alpha alone, normalized so a faint template (a theme's 54% tint) still lights fully. */
private fun alphaLit(argb: IntArray, maxAlpha: Int): DoubleArray =
DoubleArray(argb.size) { min(1.0, alpha(argb[it]).toDouble() / maxAlpha) }
/** Box-filter lit values down to the output size, then apply the tone curve. */
private fun toGray(lit: DoubleArray, size: Int, outputSize: Int): ByteArray {
val scale = size / outputSize
val gray = ByteArray(outputSize * outputSize)
for (oy in 0 until outputSize) {
for (ox in 0 until outputSize) {
var sum = 0.0
for (dy in 0 until scale) {
val row = (oy * scale + dy) * size + ox * scale
for (dx in 0 until scale) sum += lit[row + dx]
}
val mean = sum / (scale * scale)
gray[oy * outputSize + ox] = (255.0 * mean.coerceIn(0.0, 1.0).pow(GAMMA)).roundToInt().toByte()
}
}
return gray
}
private fun toOklab(pixel: Int, out: DoubleArray, offset: Int) {
val r = SRGB_TO_LINEAR[(pixel shr 16) and 0xff]
val g = SRGB_TO_LINEAR[(pixel shr 8) and 0xff]
val b = SRGB_TO_LINEAR[pixel and 0xff]
val l = cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
val m = cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
val s = cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
out[offset] = 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s
out[offset + 1] = 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s
out[offset + 2] = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
}
private fun distance(lab: DoubleArray, index: Int, background: DoubleArray): Double {
val dl = lab[index * 3] - background[0]
val da = lab[index * 3 + 1] - background[1]
val db = lab[index * 3 + 2] - background[2]
return sqrt(dl * dl + da * da + db * db)
}
private fun quantile(sorted: DoubleArray, q: Double): Double = sorted[((sorted.size - 1) * q).roundToInt()]
private fun peakAlpha(argb: IntArray): Int {
var peak = 0
for (p in argb) peak = max(peak, alpha(p))
return peak
}
private fun alpha(p: Int): Int = (p ushr 24) and 0xff
}
@@ -0,0 +1,161 @@
package com.faceclaw.app
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.test.*
class NotificationIconArtTest {
private val outputSize = 12
private val size = outputSize * NotificationIconArt.ANALYSIS_SCALE
private val clear = 0
private val white = argb(255, 255, 255, 255)
private val black = argb(255, 0, 0, 0)
private val blue = argb(255, 0, 0, 255)
private val green = argb(255, 20, 160, 70)
private fun argb(a: Int, r: Int, g: Int, b: Int): Int = (a shl 24) or (r shl 16) or (g shl 8) or b
private fun icon(pixel: (x: Int, y: Int) -> Int): IntArray = IntArray(size * size) { pixel(it % size, it / size) }
private fun inCircle(x: Int, y: Int, cx: Double, cy: Double, r: Double): Boolean {
val dx = x + 0.5 - cx
val dy = y + 0.5 - cy
return dx * dx + dy * dy <= r * r
}
/** A rounded square inset from the image edge. */
private fun inRoundedSquare(x: Int, y: Int, inset: Int, radius: Int): Boolean {
val lo = inset
val hi = size - 1 - inset
if (x < lo || x > hi || y < lo || y > hi) return false
val cx = x.coerceIn(lo + radius, hi - radius)
val cy = y.coerceIn(lo + radius, hi - radius)
return inCircle(x, y, cx + 0.5, cy + 0.5, radius + 0.5)
}
/** A horizontal bar across the middle, in analysis pixels aligned to output pixels. */
private fun inBar(x: Int, y: Int): Boolean = y in 10..13 && x in 6..17
private fun gray(art: NotificationIconArt.Art, ox: Int, oy: Int): Int = art.gray[oy * outputSize + ox].toInt() and 0xff
@Test fun emptyIconIsEmpty() {
val art = NotificationIconArt.convert(IntArray(size * size), outputSize)
assertEquals(NotificationIconArt.Kind.EMPTY, art.kind)
assertTrue(art.gray.all { it.toInt() == 0 })
}
@Test fun ringWithNoisyColorsIsATemplate() {
// Discord-style: the alpha is a clean shape with a hole, the colors are garbage.
var seed = 12345
val pixels = icon { x, y ->
seed = seed * 1103515245 + 12345
val inRing = inCircle(x, y, 12.0, 12.0, 11.0) && !inCircle(x, y, 12.0, 12.0, 5.0)
if (inRing) argb(255, (seed ushr 8) and 0xff, (seed ushr 16) and 0xff, (seed ushr 4) and 0xff) else clear
}
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertContentEquals(NotificationIconArt.template(pixels, outputSize).gray, art.gray)
assertEquals(255, gray(art, 1, 6))
assertEquals(0, gray(art, 6, 6))
}
@Test fun darkColoredTemplateStillLightsFully() {
// A plus sign filled pure blue: by brightness it would all but vanish.
val pixels = icon { x, y -> if ((x in 8..15 && y in 2..21) || (y in 8..15 && x in 2..21)) blue else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertEquals(255, gray(art, 6, 2))
assertEquals(0, gray(art, 1, 1))
}
@Test fun faintTemplateIsNormalizedToFullBrightness() {
// A theme tint like #8A000000 leaves the whole shape at 54% alpha.
val pixels = icon { x, y -> if (x in 4..19 && y in 4..19 && !(x in 8..15 && y in 8..15)) argb(138, 0, 0, 0) else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertEquals(255, gray(art, 2, 2))
}
@Test fun squareWithCutOutIsATemplate() {
val pixels = icon { x, y -> if (inBar(x, y)) clear else white }
assertEquals(NotificationIconArt.Kind.TEMPLATE, NotificationIconArt.convert(pixels, outputSize).kind)
}
@Test fun oneColorDiscIsSolid() {
val pixels = icon { x, y -> if (inCircle(x, y, 12.0, 12.0, 11.0)) blue else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.SOLID, art.kind)
assertEquals(255, gray(art, 6, 6))
assertEquals(0, gray(art, 0, 0))
}
@Test fun lightGlyphOnColoredTileIsExtracted() {
val pixels = icon { x, y ->
when {
!inRoundedSquare(x, y, 0, 5) -> clear
inBar(x, y) -> white
else -> green
}
}
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.GLYPH, art.kind)
assertEquals(255, gray(art, 6, 5))
assertEquals(0, gray(art, 6, 2))
assertEquals(0, gray(art, 1, 6))
}
@Test fun darkGlyphOnWhiteCircleIsLitNotTheCircle() {
val pixels = icon { x, y ->
when {
!inCircle(x, y, 12.0, 12.0, 11.5) -> clear
inBar(x, y) -> black
else -> white
}
}
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.GLYPH, art.kind)
assertEquals(255, gray(art, 6, 5))
assertEquals(0, gray(art, 6, 2))
}
@Test fun tileWithTransparentMarginIsStillATile() {
val pixels = icon { x, y ->
when {
!inRoundedSquare(x, y, 2, 3) -> clear
inBar(x, y) -> white
else -> green
}
}
assertEquals(NotificationIconArt.Kind.GLYPH, NotificationIconArt.convert(pixels, outputSize).kind)
}
@Test fun gradientTileIsReadAsAPicture() {
val pixels = icon { x, _ -> val v = x * 10; argb(255, v, v, v) }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.PICTURE, art.kind)
assertTrue(gray(art, 1, 6) < gray(art, 6, 6))
assertTrue(gray(art, 6, 6) < gray(art, 10, 6))
assertEquals(255, gray(art, 11, 6))
}
@Test fun templateConversionIgnoresColors() {
val pixels = icon { x, y -> if (inBar(x, y)) white else green }
val art = NotificationIconArt.template(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
assertTrue(art.gray.all { (it.toInt() and 0xff) == 255 })
}
@Test fun downscaleAveragesCoverageThenAppliesTheToneCurve() {
// Alternate opaque and clear columns: every output pixel is half covered.
val pixels = icon { x, _ -> if (x % 2 == 0) white else clear }
val art = NotificationIconArt.convert(pixels, outputSize)
assertEquals(NotificationIconArt.Kind.TEMPLATE, art.kind)
val expected = (255 * 0.5.pow(NotificationIconArt.GAMMA)).roundToInt()
assertTrue(art.gray.all { (it.toInt() and 0xff) == expected })
}
@Test fun rejectsTheWrongPixelCount() {
assertFailsWith<IllegalArgumentException> { NotificationIconArt.convert(IntArray(10), outputSize) }
}
}