Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-11-01 11:00:38 -08:00
commit fcea0b369a
218 files changed
+14438 -2259

No files matched your search

@@ -17,6 +17,10 @@ class AndroidSessionLink(private val bleManager: FaceclawBleManager) : SessionLi
bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH)
}
override fun requestLe2mPhy(address: String) = bleManager.requestLe2mPhy(address)
override fun describePhy(address: String): String? = bleManager.describePhy(address)
override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean =
bleManager.requestMtu(address, mtu, timeoutMs)
@@ -18,6 +18,8 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
@@ -27,6 +29,10 @@ class FaceclawBleManager(context: Context) {
private const val TAG = "FaceclawBle"
private val WRITE_RETRY_DELAYS_MS = intArrayOf(1, 1, 1, 2, 4, 8, 12, 20, 35, 100, 200)
// Gap after each LE 2M PHY request before checking whether it landed and asking
// again; the request gives up once these run out (see requestLe2mPhy).
private val PHY_RETRY_DELAYS_MS = longArrayOf(1_000, 2_000, 4_000, 8_000, 15_000)
// Handed to an operation waiter whose link dropped before its completion
// callback arrived; never a real GATT status.
private const val STATUS_LINK_LOST = -1
@@ -47,6 +53,19 @@ class FaceclawBleManager(context: Context) {
Thread(runnable, "FaceclawBleCallbacks").apply { isDaemon = true }
}
// Runs the LE 2M PHY request loops. Each step takes the global lock, so it must not
// run on the binder thread or the callback executor.
private val phyScheduler: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor { runnable ->
Thread(runnable, "FaceclawBlePhy").apply { isDaemon = true }
}
private fun phyName(phy: Int): String = when (phy) {
BluetoothDevice.PHY_LE_1M -> "1M"
BluetoothDevice.PHY_LE_2M -> "2M"
BluetoothDevice.PHY_LE_CODED -> "Coded"
else -> "?"
}
// Process-wide outbound-traffic totals, sampled by the phone UI's BLE
// bandwidth indicator. Static so counts from every manager instance and
// isolate land in the same totals.
@@ -76,6 +95,16 @@ class FaceclawBleManager(context: Context) {
@Volatile var gatt: BluetoothGatt? = null
@Volatile var connected = false
val connectLatch = CountDownLatch(1)
// The PHY pair Android last reported for this connection (BluetoothDevice.PHY_LE_*),
// 0 until a report arrives.
@Volatile var txPhy = 0
@Volatile var rxPhy = 0
// The 2M request loop's next step; only touched on phyScheduler.
var phyStep: ScheduledFuture<*>? = null
fun on2mPhy(): Boolean = txPhy == BluetoothDevice.PHY_LE_2M && rxPhy == BluetoothDevice.PHY_LE_2M
}
private enum class OpKind { MTU, SERVICES, DESCRIPTOR, WRITE }
@@ -252,13 +281,72 @@ class FaceclawBleManager(context: Context) {
val accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)
Log.i(TAG, "benchmark HIGH address=$address submitted=$accepted")
}
if ((mode and 2) != 0) {
Log.i(TAG, "benchmark request 2M address=$address")
gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK,
BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED)
}
gatt.readPhy()
}
if ((mode and 2) != 0) {
Log.i(TAG, "benchmark request 2M address=$address")
requestLe2mPhy(address)
}
}
/**
* Ask for the LE 2M PHY in both directions, then keep checking and re-asking until it
* lands or [PHY_RETRY_DELAYS_MS] runs out. A single request is unreliable: right after
* connecting, Android can reject it as unsupported before it has read the peer's feature
* bits, and an on-air PHY update can lose a collision with the peer's own
* connection-parameter request. Our preference stays 2M-only, so a granted 2M link
* shouldn't drift back; the loop ends with the connection.
*/
fun requestLe2mPhy(address: String) {
val link = links[address] ?: return
phyScheduler.execute {
link.phyStep?.cancel(false)
runPhyStep(address, link, 0)
}
}
private fun runPhyStep(address: String, link: GattLink, attempt: Int) {
link.phyStep = null
if (links[address] !== link || !link.connected) {
return
}
if (link.on2mPhy()) {
Log.i(TAG, "LE 2M PHY active: address=$address requests=$attempt")
return
}
if (attempt >= PHY_RETRY_DELAYS_MS.size) {
Log.w(TAG, "LE 2M PHY not granted after $attempt requests: address=$address"
+ " tx=${phyName(link.txPhy)} rx=${phyName(link.rxPhy)}")
return
}
synchronized(bluetoothApiLock) {
val gatt = link.gatt
if (links[address] !== link || !link.connected || gatt == null) {
return
}
Log.i(TAG, "requesting LE 2M PHY: address=$address attempt=${attempt + 1}")
gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK,
BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED)
// The update-complete event can be skipped when nothing changes; a read always
// reports, so the next step sees the PHY even then.
gatt.readPhy()
}
link.phyStep = phyScheduler.schedule(
{ runPhyStep(address, link, attempt + 1) },
PHY_RETRY_DELAYS_MS[attempt],
TimeUnit.MILLISECONDS
)
}
/** The link's PHY for diagnostics: "2M", "1M", "tx/rx" when they differ, "?" before a report; null when not connected. */
fun describePhy(address: String): String? {
val link = links[address]
if (link == null || !link.connected) {
return null
}
val tx = link.txPhy
val rx = link.rxPhy
return if (tx == rx) phyName(tx) else phyName(tx) + "/" + phyName(rx)
}
fun getNegotiatedMtu(address: String): Int {
@@ -600,11 +688,23 @@ class FaceclawBleManager(context: Context) {
override fun onPhyRead(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) {
Log.i(TAG, "onPhyRead: address=" + gatt.device.address
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status)
recordPhy(gatt, txPhy, rxPhy, status)
}
override fun onPhyUpdate(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) {
Log.i(TAG, "onPhyUpdate: address=" + gatt.device.address
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status)
recordPhy(gatt, txPhy, rxPhy, status)
}
private fun recordPhy(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) {
// A failed request reports the PHYs it asked for, not the ones in use.
if (status != BluetoothGatt.GATT_SUCCESS) return
val link = links[gatt.device.address] ?: return
val current = link.gatt
if (current != null && current !== gatt) return
link.txPhy = txPhy
link.rxPhy = rxPhy
}
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
@@ -35,6 +35,8 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
companion object {
private const val TAG = "FaceclawNotify"
private const val EXTRA_SUBSTITUTE_APP_NAME = "android.substName"
/** MessagingStyle's conversation icon (Notification.EXTRA_CONVERSATION_ICON, hidden). */
private const val EXTRA_CONVERSATION_ICON = "android.conversationIcon"
/**
* How long notification access can be granted with no listener
* connected before the watchdog forces a rebind: longer than a
@@ -47,6 +49,8 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
private const val LISTENER_STUCK_MS = 30_000L
/** Notifications posted while disconnected alert on reconnect only if this recent. */
private const val RECONNECT_ALERT_WINDOW_MS = 5 * 60_000L
/** Large icons are drawn at no more than this many pixels a side before scaling down. */
private const val LARGE_ICON_MAX_SOURCE = 512
/** The connected listener; null until onListenerConnected and after onListenerDisconnected. */
@Volatile
@@ -366,6 +370,48 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
halveToward(picture, maxWidth, maxHeight), maxWidth, maxHeight, gamma, dither, invertLightBackground)
}
/**
* One active notification's large icon (largeIconOf: a sender's avatar, a site's logo)
* scaled to fit size x size as a gray packet, a light background shown as its negative
* like a picture's; empty when the notification or its icon is unavailable.
*/
@JvmStatic
fun getNotificationLargeIconGray(key: String?, size: Int, gamma: Float, dither: Boolean): ByteArray {
val service = activeService
if (service == null || size <= 0) {
return ByteArray(0)
}
val statusBarNotification = findActiveNotificationByKey(service, key)
val icon = statusBarNotification?.notification?.let { largeIconOf(it) } ?: return ByteArray(0)
try {
val drawable = icon.loadDrawable(service) ?: return ByteArray(0)
val width = if (drawable.intrinsicWidth > 0) Math.min(drawable.intrinsicWidth, LARGE_ICON_MAX_SOURCE) else size * 4
val height = if (drawable.intrinsicHeight > 0) Math.min(drawable.intrinsicHeight, LARGE_ICON_MAX_SOURCE) else size * 4
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
drawable.setBounds(0, 0, width, height)
drawable.draw(Canvas(bitmap))
return ImageFileLoader.bitmapToGrayPacket(halveToward(bitmap, size, size), size, size, gamma, dither, true)
} catch (t: Throwable) {
Log.w(TAG, "failed to load large icon for " + statusBarNotification.packageName, t)
return ByteArray(0)
}
}
/**
* One active notification's custom layout (hasCustomView in its JSON) drawn to fit
* maxWidth x maxHeight as a gray packet; see NotificationCustomViews.renderGray. Empty
* when the notification or its layout is unavailable.
*/
@JvmStatic
fun getNotificationCustomViewGray(key: String?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray {
val service = activeService ?: return ByteArray(0)
val statusBarNotification = findActiveNotificationByKey(service, key)
val notification = statusBarNotification?.notification ?: return ByteArray(0)
val hasPicture = notification.extras?.let { hasNotificationPicture(it) } ?: false
return NotificationCustomViews.renderGray(
service, statusBarNotification, !hasPicture, maxWidth, maxHeight, gamma, dither)
}
@JvmStatic
fun getActiveNotificationsJson(maxNotifications: Int): String {
val service = activeService
@@ -672,6 +718,32 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
return null
}
/**
* The picture a notification shows beside its text: its large icon (Gmail's sender
* avatar or logo), else from Android 11 its conversation's icon, else the icon of its
* latest message's sender.
*/
@Suppress("DEPRECATION")
private fun largeIconOf(notification: Notification): Icon? {
notification.getLargeIcon()?.let { return it }
val extras = notification.extras
if (extras == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
return null
}
try {
val conversationIcon: Icon? = extras.getParcelable(EXTRA_CONVERSATION_ICON)
if (conversationIcon != null) {
return conversationIcon
}
val messages = extras.getParcelableArray(Notification.EXTRA_MESSAGES) ?: return null
return Notification.MessagingStyle.Message.getMessagesFromBundleArray(messages)
.lastOrNull()?.senderPerson?.icon
} catch (t: Throwable) {
Log.w(TAG, "failed to read conversation icons", t)
return null
}
}
/** Hardware bitmaps can't be read back pixel by pixel or drawn on a software canvas; copy those. */
private fun softwareBitmap(bitmap: Bitmap): Bitmap {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && bitmap.config == Bitmap.Config.HARDWARE) {
@@ -687,7 +759,7 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
* (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 {
internal fun halveToward(source: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap {
var bitmap = source
while ((bitmap.width >= maxWidth * 2 || bitmap.height >= maxHeight * 2) && bitmap.width >= 2 && bitmap.height >= 2) {
val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true)
@@ -759,6 +831,17 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
out.put("lines", JSONArray())
out.put("hasPicture", false)
}
out.put("hasLargeIcon", largeIconOf(notification) != null)
val hasCustomView = NotificationCustomViews.hasCustomView(notification)
out.put("hasCustomView", hasCustomView)
val customText = JSONArray()
// Only needed (and only worth inflating the layout for) when there's no title or text to show instead.
if (hasCustomView && out.optString("title").isEmpty() && out.optString("text").isEmpty()) {
for (text in NotificationCustomViews.texts(service, statusBarNotification)) {
customText.put(text)
}
}
out.put("customText", customText)
val actionsJson = JSONArray()
val actions: Array<Notification.Action?>? = notification.actions
@@ -13,7 +13,7 @@ class FaceclawResourceUsage private constructor() {
companion object {
/**
* Returns elapsedMs, processCpuMs, rssKb, nativeHeapKb, javaHeapKb,
* javaHeapCommittedKb, and threadCount. Reading /proc/self/status avoids
* javaHeapCommittedKb, threadCount, and peakRssKb. Reading /proc/self/status avoids
* the considerably heavier full Debug.MemoryInfo/PSS collection once per
* second while still reporting the RSS that Android records at exit.
*/
@@ -30,30 +30,34 @@ class FaceclawResourceUsage private constructor() {
javaUsedBytes / 1024L,
runtime.totalMemory() / 1024L,
status[1],
status[2],
)
}
/** Returns {VmRSS in KiB, thread count}; unavailable values are zero. */
/** Returns {VmRSS in KiB, thread count, VmHWM in KiB}; unavailable values are zero. */
private fun readProcessStatus(): LongArray {
var rssKb = 0L
var threadCount = 0L
var peakRssKb = 0L
try {
BufferedReader(FileReader("/proc/self/status")).use { reader ->
var line: String?
while (reader.readLine().also { line = it } != null) {
val current = line!!
if (current.startsWith("VmRSS:")) {
if (current.startsWith("VmHWM:")) {
peakRssKb = firstNumber(current)
} else if (current.startsWith("VmRSS:")) {
rssKb = firstNumber(current)
} else if (current.startsWith("Threads:")) {
threadCount = firstNumber(current)
}
if (rssKb > 0 && threadCount > 0) break
if (rssKb > 0 && threadCount > 0 && peakRssKb > 0) break
}
}
} catch (ignored: IOException) {
// The graph remains useful with the runtime heap counters alone.
}
return longArrayOf(rssKb, threadCount)
return longArrayOf(rssKb, threadCount, peakRssKb)
}
private fun firstNumber(line: String): Long {
@@ -25,13 +25,23 @@ class FaceclawSettings private constructor(context: Context) {
private val prefs: SharedPreferences = context.applicationContext
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
private val hub = SettingsChangeHub(object : SettingsStorage {
override fun getString(key: String?, defaultValue: String?): String? = prefs.getString(key, defaultValue)
// An imported or hand-edited config can store a setting as a different type than the
// app reads it as (iOS is lenient about this); read it as text rather than crash.
override fun getString(key: String?, defaultValue: String?): String? = try {
prefs.getString(key, defaultValue)
} catch (_: ClassCastException) {
prefs.all[key]?.toString() ?: defaultValue
}
override fun putString(key: String?, value: String?) {
prefs.edit().putString(key, value).apply()
}
override fun getBoolean(key: String?, defaultValue: Boolean): Boolean = prefs.getBoolean(key, defaultValue)
override fun getBoolean(key: String?, defaultValue: Boolean): Boolean = try {
prefs.getBoolean(key, defaultValue)
} catch (_: ClassCastException) {
prefs.all[key]?.toString()?.toBooleanStrictOrNull() ?: defaultValue
}
override fun putBoolean(key: String?, value: Boolean) {
prefs.edit().putBoolean(key, value).apply()
@@ -3,52 +3,68 @@ package com.faceclaw.app
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Handler
import android.os.Looper
import android.util.AtomicFile
import android.util.Log
import android.util.Xml
import org.xmlpull.v1.XmlPullParser
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
/**
* adb-triggered export/import of the faceclaw_settings SharedPreferences
* file, so configuration (API keys, device addresses, ...) can be moved off a
* release build, where run-as is unavailable. See scripts/pull_config.sh and
* scripts/push_config.sh, which use run-as on debuggable builds and fall back
* to this receiver otherwise.
* adb-triggered export/import of Faceclaw's settings as the JSON document shared with iOS
* (SettingsTransfer; see scripts/config-transfer.md). scripts/pull_config.sh and
* scripts/push_config.sh drive it; it works on release builds, where run-as is unavailable.
*
* adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \
* -a com.faceclaw.app.SETTINGS_EXPORT
* adb pull /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-export.xml
* adb shell am broadcast --include-stopped-packages \
* -n com.faceclaw.app/.FaceclawSettingsPortReceiver -a com.faceclaw.app.SETTINGS_EXPORT
* adb pull /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-export.json
*
* adb push settings.xml /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-import.xml
* adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \
* -a com.faceclaw.app.SETTINGS_IMPORT
* adb push settings.json /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-import.json
* adb shell am broadcast --include-stopped-packages \
* -n com.faceclaw.app/.FaceclawSettingsPortReceiver -a com.faceclaw.app.SETTINGS_IMPORT
*
* Security: the settings file contains API tokens, so the manifest guards
* this receiver with android.permission.DUMP — a development permission the
* adb shell holds but ordinary apps cannot obtain without adb's help, keeping
* the trigger reachable from adb only. The transfer file lives in
* getExternalFilesDir(), which other apps cannot read on Android 11+ (and
* which the scripts delete as soon as the transfer completes); on the
* Android 7-10 devices minSdk still admits, apps holding READ_EXTERNAL_STORAGE
* could read it during that window.
* Settings live in two SharedPreferences files: FaceclawSettings' own, and NativeScript's
* ApplicationSettings file (prefs.db), which still holds the pairing, onboarding and EvenHub
* localStorage keys. Both are exported, and each imported key is written to the file the app
* reads it from. An import merges: settings missing from the document are left alone.
* It writes through the process's live SharedPreferences, so a running app cannot flush stale
* values over it, then exits so the next launch starts from the imported settings.
*
* Security: the settings contain API tokens, so the manifest guards this receiver with
* android.permission.DUMP — a development permission the adb shell holds but ordinary apps
* cannot obtain without adb's help, keeping the trigger reachable from adb only. The transfer
* file lives in getExternalFilesDir(), which other apps cannot read on Android 11+ (and which
* the scripts delete as soon as the transfer completes); on the Android 7-10 devices minSdk
* still admits, apps holding READ_EXTERNAL_STORAGE could read it during that window.
*/
class FaceclawSettingsPortReceiver : BroadcastReceiver() {
companion object {
private const val TAG = "FaceclawSettingsPort"
const val ACTION_EXPORT = "com.faceclaw.app.SETTINGS_EXPORT"
const val ACTION_IMPORT = "com.faceclaw.app.SETTINGS_IMPORT"
private const val PREFS_FILE = "faceclaw_settings.xml"
private const val EXPORT_NAME = "faceclaw-settings-export.xml"
private const val IMPORT_NAME = "faceclaw-settings-import.xml"
private const val PLATFORM = "android"
private const val SETTINGS_PREFS = "faceclaw_settings" // FaceclawSettings
private const val NATIVESCRIPT_PREFS = "prefs.db" // NativeScript ApplicationSettings
private const val EXPORT_NAME = "faceclaw-settings-export.json"
private const val IMPORT_NAME = "faceclaw-settings-import.json"
private const val BACKUP_NAME = "faceclaw-settings-previous.json"
private enum class StoredAs { STRING, BOOLEAN, NUMBER }
/**
* Keys the app reads through NativeScript's ApplicationSettings rather than FaceclawSettings,
* and the getter it uses (device-addresses.ts, onboarding-state.ts, evenhub/session.ts;
* tests/settings-transfer.test.cjs keeps this list in step with them). prefs.db also holds
* settings abandoned when FaceclawSettings took over, so nothing else is read from it.
*/
private fun nativeScriptKey(key: String): StoredAs? = when {
key == "onboarding.complete" || key == "onboarding.previewOnly" -> StoredAs.BOOLEAN
key == "deviceIdentity.pairedAtMs" -> StoredAs.NUMBER
key.startsWith("deviceAddress.") || key.startsWith("deviceIdentity.") || key.startsWith("evenhub:") -> StoredAs.STRING
else -> null
}
}
override fun onReceive(context: Context, intent: Intent?) {
@@ -57,11 +73,10 @@ class FaceclawSettingsPortReceiver : BroadcastReceiver() {
if (ACTION_EXPORT == action) {
resultData = exportSettings(context)
} else if (ACTION_IMPORT == action) {
resultData = importSettings(context)
// Exit once the broadcast result has been delivered: this
// process may hold stale in-memory SharedPreferences that a
// later apply() would flush over the imported file, and the
// next launch must re-read from disk.
val result = importSettings(context)
resultData = result
if (!result.startsWith("imported:")) return
// Restart cleanly: running code may hold settings it read before the import.
Handler(Looper.getMainLooper()).postDelayed({
Log.i(TAG, "Exiting after settings import")
System.exit(0)
@@ -69,13 +84,15 @@ class FaceclawSettingsPortReceiver : BroadcastReceiver() {
}
} catch (e: Exception) {
Log.e(TAG, "Settings $action failed", e)
resultData = "error: $e"
resultData = "error: ${e.message ?: e}"
}
}
private fun prefsFile(context: Context): File {
return File(context.applicationInfo.dataDir, "shared_prefs/$PREFS_FILE")
}
private fun settingsPrefs(context: Context): SharedPreferences =
context.getSharedPreferences(SETTINGS_PREFS, Context.MODE_PRIVATE)
private fun nativeScriptPrefs(context: Context): SharedPreferences =
context.getSharedPreferences(NATIVESCRIPT_PREFS, Context.MODE_PRIVATE)
@Throws(IOException::class)
private fun externalFile(context: Context, name: String): File {
@@ -84,63 +101,111 @@ class FaceclawSettingsPortReceiver : BroadcastReceiver() {
return File(dir, name)
}
@Throws(IOException::class)
private fun exportSettings(context: Context): String {
val prefs = prefsFile(context)
if (!prefs.exists()) {
return "error: no settings file yet ($prefs)"
private fun appVersion(context: Context): String? =
runCatching { context.packageManager.getPackageInfo(context.packageName, 0).versionName }.getOrNull()
/** Every setting, from the file the app reads it from, as SettingsTransfer values. */
private fun currentSettings(context: Context): Map<String, Any?> {
val values = linkedMapOf<String, Any?>()
for ((key, value) in settingsPrefs(context).all) {
if (key == null || nativeScriptKey(key) != null) continue
val exported = exportable(value)
if (exported != null) values[key] = exported else Log.w(TAG, "Not exporting $key: unsupported type")
}
for ((key, value) in nativeScriptPrefs(context).all) {
val storedAs = nativeScriptKey(key ?: continue) ?: continue
// NativeScript stores numbers as the raw bits of a double in a long.
val exported = if (storedAs == StoredAs.NUMBER && value is Long) {
Double.fromBits(value).takeIf { it.isFinite() }?.let { JsonNumber.of(it) }
} else {
exportable(value)
}
if (exported != null) values[key] = exported else Log.w(TAG, "Not exporting $key: unsupported value")
}
return values
}
private fun exportable(value: Any?): Any? = when (value) {
is String, is Boolean -> value
is Int, is Long -> JsonNumber(value.toString())
is Float -> value.toDouble().takeIf { it.isFinite() }?.let { JsonNumber.of(it) }
else -> null // string sets are unused
}
private fun exportSettings(context: Context): String {
val document = SettingsTransfer.export(currentSettings(context), PLATFORM, appVersion(context))
val out = externalFile(context, EXPORT_NAME)
copy(prefs, out)
writeAtomically(out, document)
return "exported: $out"
}
@Throws(Exception::class)
private fun importSettings(context: Context): String {
val staged = externalFile(context, IMPORT_NAME)
if (!staged.exists()) {
return "error: nothing staged at $staged"
}
validatePrefsXml(staged)
val prefs = prefsFile(context)
prefs.parentFile?.mkdirs()
if (prefs.exists()) {
copy(prefs, File(prefs.path + ".bak"))
val plan = try {
SettingsTransfer.planImport(staged.readText(), PLATFORM)
} finally {
staged.delete() // holds API tokens
}
copy(staged, prefs)
staged.delete()
return "imported: $prefs (previous settings in $PREFS_FILE.bak)"
}
/** Reject files that are not a SharedPreferences <map> document. */
@Throws(Exception::class)
private fun validatePrefsXml(file: File) {
FileInputStream(file).use { input ->
val parser = Xml.newPullParser()
parser.setInput(input, null)
var event = parser.next()
while (event != XmlPullParser.START_TAG && event != XmlPullParser.END_DOCUMENT) {
event = parser.next()
}
if (event != XmlPullParser.START_TAG || "map" != parser.name) {
throw IOException("not a SharedPreferences <map> document")
}
// Walk the rest so malformed XML is caught before it replaces
// the live settings file.
while (parser.next() != XmlPullParser.END_DOCUMENT) { }
}
}
@Throws(IOException::class)
private fun copy(from: File, to: File) {
FileInputStream(from).use { input: InputStream ->
FileOutputStream(to).use { out: OutputStream ->
val buffer = ByteArray(8192)
var n: Int
while (input.read(buffer).also { n = it } > 0) {
out.write(buffer, 0, n)
// Convert everything before writing anything, so a bad value leaves settings untouched.
val settingsWrites = ArrayList<(SharedPreferences.Editor) -> Unit>()
val nativeScriptWrites = ArrayList<(SharedPreferences.Editor) -> Unit>()
for ((key, value) in plan.values) {
when (nativeScriptKey(key)) {
// FaceclawSettings only has string and boolean getters.
null -> if (value is Boolean) {
settingsWrites.add { e -> e.putBoolean(key, value) }
} else {
settingsWrites.add { e -> e.putString(key, text(value)) }
}
StoredAs.STRING -> nativeScriptWrites.add { e -> e.putString(key, text(value)) }
StoredAs.BOOLEAN -> {
val flag = value as? Boolean ?: (value as? String)?.toBooleanStrictOrNull()
?: throw IllegalArgumentException("Setting $key must be a boolean")
nativeScriptWrites.add { e -> e.putBoolean(key, flag) }
}
StoredAs.NUMBER -> {
val number = text(value).toDoubleOrNull()?.takeIf { it.isFinite() }
?: throw IllegalArgumentException("Setting $key must be a number")
nativeScriptWrites.add { e -> e.putLong(key, number.toRawBits()) }
}
}
}
val backup = File(context.filesDir, BACKUP_NAME)
writeAtomically(backup, SettingsTransfer.export(currentSettings(context), PLATFORM, appVersion(context)))
commit(settingsPrefs(context), settingsWrites)
commit(nativeScriptPrefs(context), nativeScriptWrites)
val skipped = if (plan.skipped.isEmpty()) "" else
" (left out ${plan.skipped.size} ${plan.sourcePlatform ?: "other-platform"}-only settings)"
return "imported: ${plan.values.size} settings$skipped; previous settings saved in $backup"
}
private fun text(value: Any): String = when (value) {
is JsonNumber -> value.text
else -> value.toString()
}
private fun commit(prefs: SharedPreferences, writes: List<(SharedPreferences.Editor) -> Unit>) {
if (writes.isEmpty()) return
val editor = prefs.edit()
for (write in writes) write(editor)
if (!editor.commit()) throw IOException("Could not save settings")
}
private fun writeAtomically(file: File, text: String) {
val atomic = AtomicFile(file)
val stream = atomic.startWrite()
try {
stream.write(text.toByteArray(Charsets.UTF_8))
atomic.finishWrite(stream)
} catch (e: IOException) {
atomic.failWrite(stream)
throw e
}
}
}
@@ -0,0 +1,194 @@
package com.faceclaw.app
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.util.Base64
import android.util.Log
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.net.BindException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.ServerSocket
import java.net.Socket
import java.security.MessageDigest
import java.security.SecureRandom
/**
* OAuth sign-in in the phone's browser with a loopback redirect
* (http://127.0.0.1:<port><path>, the redirect providers such as Spotify
* accept from native apps): Android's counterpart of the iOS
* FaceclawWebSignIn / FaceclawLoopbackRedirect. A receiver on the loopback
* interface answers the browser's request for the path with a short "signed
* in" page and hands back the full redirect URL, query included; any other
* path (a favicon) gets a 404.
*
* Android blocks activity starts from the background, so the TypeScript side
* (app/native/spotify.ts) calls start(), then openBrowser() from Faceclaw's
* activity once it is in front. The code exchange needs no foreground, so the
* user can stay in the browser afterwards.
*/
class FaceclawWebSignIn private constructor() {
companion object {
private const val TAG = "FaceclawWebSignIn"
/** Request heads beyond this are not a browser's redirect; stop reading. */
private const val MAX_REQUEST_BYTES = 16384
private const val CLIENT_TIMEOUT_MS = 3000
private const val DONE_PAGE =
"<!doctype html><html><head><meta charset=\"utf-8\">" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Faceclaw</title></head>" +
"<body style=\"font-family: sans-serif; text-align: center; padding: 64px 24px\">" +
"<h2>Signed in</h2><p>You can close this page and return to Faceclaw.</p></body></html>"
private val INSTANCE = FaceclawWebSignIn()
private val random = SecureRandom()
@JvmStatic fun shared(): FaceclawWebSignIn = INSTANCE
/** byteCount random bytes, base64url without padding (PKCE verifiers, OAuth state). */
@JvmStatic
fun randomBase64Url(byteCount: Int): String {
val bytes = ByteArray(byteCount.coerceIn(1, 256))
random.nextBytes(bytes)
return base64Url(bytes)
}
/** SHA-256 of the string's UTF-8 bytes, base64url without padding (a PKCE S256 challenge). */
@JvmStatic
fun sha256Base64Url(value: String): String =
base64Url(MessageDigest.getInstance("SHA-256").digest(value.toByteArray(Charsets.UTF_8)))
/** The target ("/path?query") of a GET request head; null for anything else. */
@JvmStatic
fun requestTarget(head: String): String? {
val parts = head.substringBefore("\r\n").split(' ')
return if (parts.size >= 3 && parts[0] == "GET" && parts[1].startsWith("/")) parts[1] else null
}
private fun base64Url(bytes: ByteArray): String =
Base64.encodeToString(bytes, Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP)
}
private val mainHandler = Handler(Looper.getMainLooper())
private val lock = Any()
/** The receiver of the sign-in in progress; null when none is. */
private var server: ServerSocket? = null
private var listener: FaceclawWebSignInListener? = null
/**
* Listen for the redirect to http://127.0.0.1:<port><path>, ending any
* sign-in in progress. Returns null, or why the receiver couldn't start
* (and then the listener never hears). Otherwise the listener hears
* exactly once, on the main thread.
*/
fun start(port: Int, path: String, listener: FaceclawWebSignInListener): String? {
cancel()
if (port <= 0 || port > 65535 || !path.startsWith("/")) return "Invalid redirect address"
val socket = ServerSocket()
try {
socket.reuseAddress = true
socket.bind(InetSocketAddress(InetAddress.getByName("127.0.0.1"), port), 8)
} catch (e: IOException) {
try { socket.close() } catch (ignored: IOException) {}
return if (e is BindException) "Another app is using port $port; close it and try again"
else "Could not listen on port $port: ${e.message}"
}
synchronized(lock) {
server = socket
this.listener = listener
}
val thread = Thread({ acceptLoop(socket, port, path) }, "FaceclawWebSignIn")
thread.isDaemon = true
thread.start()
return null
}
/** Open the sign-in page in the phone's browser; null, or why not. Call while Faceclaw is in front. */
fun openBrowser(context: Context, url: String): String? {
val uri = Uri.parse(url)
if (uri.scheme != "https") return "Invalid sign-in address"
val intent = Intent(Intent.ACTION_VIEW, uri)
.addCategory(Intent.CATEGORY_BROWSABLE)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
return try {
context.startActivity(intent)
null
} catch (e: ActivityNotFoundException) {
"No browser on this phone can open the sign-in page"
} catch (e: Exception) {
Log.w(TAG, "could not open the sign-in page", e)
"Could not open the sign-in page: ${e.message}"
}
}
/** End the sign-in in progress, if any; its listener hears "cancelled". */
fun cancel() = fail("cancelled")
/** End the sign-in in progress, if any, with this error. */
fun fail(error: String) = finish(null, null, error)
private fun acceptLoop(socket: ServerSocket, port: Int, path: String) {
while (true) {
// finish() closes the socket, which ends accept() with an exception.
val client = try { socket.accept() } catch (e: IOException) { return }
val target = try {
serve(client, path)
} catch (e: IOException) {
null
} finally {
try { client.close() } catch (ignored: IOException) {}
}
if (target != null) {
finish(socket, "http://127.0.0.1:$port$target", null)
return
}
}
}
/** Read one request head and answer it; returns its target when it is the redirect. */
private fun serve(client: Socket, path: String): String? {
client.soTimeout = CLIENT_TIMEOUT_MS
val input = client.getInputStream()
val head = ByteArrayOutputStream()
val buffer = ByteArray(2048)
while (head.size() < MAX_REQUEST_BYTES) {
val count = input.read(buffer)
if (count <= 0) break
head.write(buffer, 0, count)
if (head.toString("ISO-8859-1").contains("\r\n\r\n")) break
}
val target = requestTarget(head.toString("UTF-8"))
val matched = target != null && target.substringBefore('?') == path
val body = (if (matched) DONE_PAGE else "Not found").toByteArray(Charsets.UTF_8)
val headers = "HTTP/1.1 ${if (matched) "200 OK" else "404 Not Found"}\r\n" +
"Content-Type: text/html; charset=utf-8\r\nContent-Length: ${body.size}\r\n" +
"Cache-Control: no-store\r\nConnection: close\r\n\r\n"
val output = client.getOutputStream()
output.write(headers.toByteArray(Charsets.US_ASCII))
output.write(body)
output.flush()
return if (matched) target else null
}
/**
* Report the outcome once and stop listening. `socket` limits it to that
* receiver's sign-in (a late redirect after a newer start() is dropped);
* null ends whichever is in progress.
*/
private fun finish(socket: ServerSocket?, url: String?, error: String?) {
val ended = synchronized(lock) {
val current = server
if (current == null || (socket != null && socket !== current)) return
val pending = listener
server = null
listener = null
try { current.close() } catch (ignored: IOException) {}
pending
} ?: return
mainHandler.post { ended.onFinished(url, if (url != null) null else error) }
}
}
@@ -0,0 +1,200 @@
package com.faceclaw.app
import android.app.Notification
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Picture
import android.os.Build
import android.service.notification.StatusBarNotification
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.RemoteViews
import android.widget.TextView
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
/**
* A notification's custom layout (RemoteViews), which can carry what its standard fields leave
* out: Dexcom's G7 notification has no title or text at all, just a layout with the glucose
* reading and trend arrow. Inflated here the way the shade does it, for its text (a stand-in
* title in lists) and to draw it as a picture.
*/
internal object NotificationCustomViews {
private const val TAG = "FaceclawNotifyView"
/** Set by Notification.Builder when the app gave it a custom layout (Notification.EXTRA_CONTAINS_CUSTOM_VIEW, hidden). */
private const val EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView"
private const val VIEW_CACHE_MAX_ENTRIES = 8
private const val TEXT_CACHE_MAX_ENTRIES = 64
/** Layouts are cut off below this height, in dp; the shade caps an expanded notification at 256. */
private const val MAX_HEIGHT_DP = 320
/** Inflated layouts by notification post and layout; null when inflating failed, so it isn't retried every paint. */
private val views = Lru<View?>(VIEW_CACHE_MAX_ENTRIES)
private val texts = Lru<List<String>>(TEXT_CACHE_MAX_ENTRIES)
/**
* Whether the app gave the notification a custom layout. The flag tells one apart from the
* standard template that Notification.Builder stores in contentView for apps targeting
* Android 6 and older, which only repeats the title and text.
*/
@Suppress("DEPRECATION")
fun hasCustomView(notification: Notification): Boolean {
val extras = notification.extras ?: return false
return extras.getBoolean(EXTRA_CONTAINS_CUSTOM_VIEW)
&& (notification.contentView != null || notification.bigContentView != null || notification.headsUpContentView != null)
}
/**
* The text of the custom layout as collapsed in the shade, one entry per visible TextView in
* layout order; empty without a custom layout. Cached per post of the notification.
*/
@Synchronized
fun texts(context: Context, statusBarNotification: StatusBarNotification): List<String> {
val notification = statusBarNotification.notification ?: return emptyList()
val (name, layout) = layoutFor(notification, expanded = false) ?: return emptyList()
return texts.getOrPut(postKey(statusBarNotification, name)) {
inflate(context, statusBarNotification, name, layout)?.let { collectTexts(it, ArrayList()) } ?: emptyList()
}
}
/**
* The custom layout drawn as a gray packet fitting maxWidth x maxHeight; empty without one
* or when it can't be drawn. It is laid out maxWidth dp wide and shown at no more than a
* glasses pixel per dp, where the shade's text sizes land near the glasses' UI font, then
* trimmed to what it draws. The collapsed layout is used when the notification has a
* picture to show under it (an expanded layout tends to repeat the picture, as Dexcom's
* does), else the expanded one. Ink drawn for a light background comes out light on dark.
*/
@Synchronized
fun renderGray(
context: Context, statusBarNotification: StatusBarNotification, expanded: Boolean,
maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean,
): ByteArray {
val notification = statusBarNotification.notification ?: return ByteArray(0)
if (maxWidth <= 0 || maxHeight <= 0) return ByteArray(0)
val (name, layout) = layoutFor(notification, expanded) ?: return ByteArray(0)
val view = inflate(context, statusBarNotification, name, layout) ?: return ByteArray(0)
try {
val density = context.resources.displayMetrics.density
val widthPx = max(1, (maxWidth * density).roundToInt())
val maxHeightPx = max(1, (MAX_HEIGHT_DP * density).roundToInt())
view.measure(
View.MeasureSpec.makeMeasureSpec(widthPx, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
if (view.measuredHeight > maxHeightPx) {
view.measure(
View.MeasureSpec.makeMeasureSpec(widthPx, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(maxHeightPx, View.MeasureSpec.AT_MOST))
}
val heightPx = min(view.measuredHeight, maxHeightPx)
if (heightPx <= 0) return ByteArray(0)
view.layout(0, 0, widthPx, heightPx)
val drawn = drawView(view, widthPx, heightPx)
val pixels = IntArray(widthPx * heightPx)
drawn.getPixels(pixels, 0, widthPx, 0, 0, widthPx, heightPx)
drawn.recycle()
val bounds = GrayPacket.inkBounds(pixels, widthPx, heightPx) ?: return ByteArray(0)
val cropWidth = bounds[2] - bounds[0]
val cropHeight = bounds[3] - bounds[1]
val cropped = IntArray(cropWidth * cropHeight)
for (y in 0 until cropHeight) {
System.arraycopy(pixels, (bounds[1] + y) * widthPx + bounds[0], cropped, y * cropWidth, cropWidth)
}
GrayPacket.lightenDarkInk(cropped, cropWidth, cropHeight)
val art = Bitmap.createBitmap(cropped, cropWidth, cropHeight, Bitmap.Config.ARGB_8888)
// A dp per glasses pixel at most: bitmapToGrayPacket otherwise blows a narrow layout up to the box.
val fitWidth = min(maxWidth, ceil(cropWidth / density).toInt())
val fitHeight = min(maxHeight, ceil(cropHeight / density).toInt())
Log.i(TAG, "rendered $name layout of ${statusBarNotification.packageName}: ${cropWidth}x$cropHeight px"
+ " into ${fitWidth}x$fitHeight")
return ImageFileLoader.bitmapToGrayPacket(
FaceclawMediaNotificationListenerService.halveToward(art, fitWidth, fitHeight),
fitWidth, fitHeight, gamma, dither, true)
} catch (t: Throwable) {
Log.w(TAG, "failed to draw the $name layout of ${statusBarNotification.packageName}", t)
return ByteArray(0)
}
}
/**
* The custom layout to use, with the field it came from: the collapsed one (contentView)
* or the expanded one (bigContentView), falling back on the other, then on the heads-up one.
*/
@Suppress("DEPRECATION")
private fun layoutFor(notification: Notification, expanded: Boolean): Pair<String, RemoteViews>? {
if (!hasCustomView(notification)) return null
val collapsed = notification.contentView?.let { "content" to it }
val big = notification.bigContentView?.let { "big" to it }
val first = if (expanded) big ?: collapsed else collapsed ?: big
return first ?: notification.headsUpContentView?.let { "headsUp" to it }
}
/** A notification is reposted (with a new postTime) whenever its app updates it. */
private fun postKey(statusBarNotification: StatusBarNotification, layoutName: String): String =
"${statusBarNotification.key}|${statusBarNotification.postTime}|$layoutName"
private fun inflate(context: Context, statusBarNotification: StatusBarNotification, name: String, layout: RemoteViews): View? =
views.getOrPut(postKey(statusBarNotification, name)) {
try {
// RemoteViews loads the layout and its resources from the posting app's package.
layout.apply(context, FrameLayout(context))
} catch (t: Throwable) {
Log.w(TAG, "failed to inflate the $name layout of ${statusBarNotification.packageName}", t)
null
}
}
private fun collectTexts(view: View, out: MutableList<String>): List<String> {
if (view.visibility != View.VISIBLE) return out
if (view is TextView) {
val text = view.text?.toString()?.replace(Regex("\\s+"), " ")?.trim() ?: ""
if (text.isNotEmpty()) out.add(text)
} else if (view is ViewGroup) {
for (index in 0 until view.childCount) {
collectTexts(view.getChildAt(index), out)
}
}
return out
}
/**
* Draws a laid-out view to a software bitmap. From Android 9 this goes through a Picture,
* which (unlike a bitmap's Canvas) accepts the hardware bitmaps a layout's images can
* arrive as; Bitmap.createBitmap then renders it on the GPU and reads it back if it has to.
*/
private fun drawView(view: View, width: Int, height: Int): Bitmap {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val picture = Picture()
view.draw(picture.beginRecording(width, height))
picture.endRecording()
return Bitmap.createBitmap(picture, width, height, Bitmap.Config.ARGB_8888)
}
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
view.draw(Canvas(bitmap))
return bitmap
}
/** A small LRU map; values may be null. Callers hold the object's lock. */
private class Lru<V>(private val maxEntries: Int) {
private val entries = object : LinkedHashMap<String, V>(16, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, V>): Boolean = size > maxEntries
}
fun getOrPut(key: String, compute: () -> V): V {
if (entries.containsKey(key)) {
@Suppress("UNCHECKED_CAST")
return entries[key] as V
}
val value = compute()
entries[key] = value
return value
}
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- History icon for the keyboard-input panel's recent-inputs button.
Material Design "history" glyph (Apache 2.0); base fill is overridden by
tint-color in app.css. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF333333"
android:pathData="M13 3c-4.97 0-9 4.03-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42C8.27 19.99 10.51 21 13 21c4.97 0 9-4.03 9-9s-4.03-9-9-9zm-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8H12z" />
</vector>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Glasses half of the keyboard-input panel's visibility button (dimmed
while the dialog is hidden on the glasses): two lens frames, bridge and
temple stubs. Base fill is overridden by tint-color in app.css. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF333333"
android:fillType="evenOdd"
android:pathData="M4.5 8h3a3 3 0 0 1 3 3v1.5a3 3 0 0 1-3 3h-3a3 3 0 0 1-3-3v-1.5a3 3 0 0 1 3-3zM4.75 9.75h2.5a1.5 1.5 0 0 1 1.5 1.5v1a1.5 1.5 0 0 1-1.5 1.5h-2.5a1.5 1.5 0 0 1-1.5-1.5v-1a1.5 1.5 0 0 1 1.5-1.5zM16.5 8h3a3 3 0 0 1 3 3v1.5a3 3 0 0 1-3 3h-3a3 3 0 0 1-3-3v-1.5a3 3 0 0 1 3-3zM16.75 9.75h2.5a1.5 1.5 0 0 1 1.5 1.5v1a1.5 1.5 0 0 1-1.5 1.5h-2.5a1.5 1.5 0 0 1-1.5-1.5v-1a1.5 1.5 0 0 1 1.5-1.5z" />
<path
android:fillColor="#FF333333"
android:pathData="M10 10.25Q12 8.25 14 10.25V12Q12 10 10 12zM0 8h3v1.75H0zM21 8h3v1.75h-3z" />
</vector>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Phone half of the keyboard-input panel's visibility button (dimmed while
the dialog is hidden on the phone). Material Design "smartphone" glyph
(Apache 2.0); base fill is overridden by tint-color in app.css. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF333333"
android:pathData="M17 1.01L7 1c-1.1 0-2 .9-2 2v18c0 1.1.9 2 2 2h10c1.1 0 2-.9 2-2V3c0-1.1-.9-1.99-2-1.99zM17 19H7V5h10v14z" />
</vector>
@@ -0,0 +1,13 @@
{
"images": [
{
"idiom": "universal",
"filename": "ic_input_history@3x.png",
"scale": "3x"
}
],
"info": {
"author": "xcode",
"version": 1
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

@@ -0,0 +1,13 @@
{
"images": [
{
"idiom": "universal",
"filename": "ic_show_glasses@3x.png",
"scale": "3x"
}
],
"info": {
"author": "xcode",
"version": 1
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

@@ -0,0 +1,13 @@
{
"images": [
{
"idiom": "universal",
"filename": "ic_show_phone@3x.png",
"scale": "3x"
}
],
"info": {
"author": "xcode",
"version": 1
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 715 B

+1 -1
View File
@@ -13,7 +13,7 @@
<key>CFBundleDisplayName</key>
<string>Faceclaw</string>
<key>CFBundleShortVersionString</key>
<string>0.8.4</string>
<string>0.8.5</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
+3 -1
View File
@@ -3,7 +3,9 @@ TARGETED_DEVICE_FAMILY = 1;
CODE_SIGN_STYLE = Automatic;
// Set IOS_TEAM_ID in build_paths.sh (or pass --teamId) to choose the signing team.
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework SafariServices -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit -framework MediaPlayer
// FoundationModels (the on-phone LLM, FaceclawSystemLanguageModel.swift) only exists on iOS 26+,
// so it is weak-linked: the app still launches on older iOS and reports the model unavailable.
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework SafariServices -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit -framework MediaPlayer -framework AuthenticationServices -framework Security -weak_framework FoundationModels
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
+2 -24
View File
@@ -1,4 +1,5 @@
#import "FaceclawAppleMusic.h"
#import "FaceclawGraphics.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <UIKit/UIKit.h>
@@ -219,30 +220,7 @@ static NSDictionary *BrowseRow(NSString *mediaId, NSString *title, NSString *sub
- (NSData *)albumArt:(NSInteger)maxSize {
if (maxSize <= 0 || maxSize > 1024 || ![self hasPermission]) return nil;
MPMediaItemArtwork *artwork = MPMusicPlayerController.systemMusicPlayer.nowPlayingItem.artwork;
UIImage *image = [artwork imageWithSize:CGSizeMake(maxSize, maxSize)];
CGImageRef source = image.CGImage;
if (!source) return nil;
double sourceWidth = CGImageGetWidth(source), sourceHeight = CGImageGetHeight(source);
if (sourceWidth <= 0 || sourceHeight <= 0) return nil;
double scale = MIN(1.0, MIN(maxSize / sourceWidth, maxSize / sourceHeight));
NSInteger width = MAX(1, lround(sourceWidth * scale)), height = MAX(1, lround(sourceHeight * scale));
NSMutableData *packet = [NSMutableData dataWithLength:4 + width * height * 4];
uint8_t *bytes = packet.mutableBytes;
bytes[0] = width & 255; bytes[1] = (width >> 8) & 255;
bytes[2] = height & 255; bytes[3] = (height >> 8) & 255;
uint8_t *pixels = bytes + 4;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
// Memory order B, G, R, A: one little-endian ARGB word per pixel.
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4, space,
kCGBitmapByteOrder32Little | (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(space);
if (!context) return nil;
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), source);
CGContextRelease(context);
// Composited over black (the display's see-through background); now opaque.
for (NSInteger i = 0; i < width * height; i++) pixels[i * 4 + 3] = 255;
return packet;
return [FaceclawGraphics bgraPacket:[artwork imageWithSize:CGSizeMake(maxSize, maxSize)].CGImage maxSize:maxSize];
}
#pragma mark - Queue
+13 -2
View File
@@ -1,6 +1,17 @@
#import <Foundation/Foundation.h>
/** Developer config transfer through the app container, before workers start. */
NS_ASSUME_NONNULL_BEGIN
/** Converts between this phone's settings and the transfer document (Kotlin SettingsTransfer). */
@protocol FaceclawConfigCodec <NSObject>
/** valuesJson is a JSON object of the settings; nil when they cannot be exported. */
- (nullable NSString *)exportValuesJson:(NSString *)valuesJson platform:(NSString *)platform appVersion:(nullable NSString *)appVersion;
/** A JSON object: {"ok":true,"settings":{...},"skipped":[...],"sourcePlatform":...} or {"ok":false,"message":...}. */
- (NSString *)importPlanDocument:(NSString *)document platform:(NSString *)platform;
@end
/** Developer config transfer through the app container, before workers start (scripts/config-transfer.md). */
@interface FaceclawConfigPort : NSObject
+ (void)processPendingRequest;
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain;
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain
codec:(id<FaceclawConfigCodec>)codec appVersion:(nullable NSString *)appVersion;
@end
NS_ASSUME_NONNULL_END
+85 -30
View File
@@ -1,57 +1,112 @@
#import "FaceclawConfigPort.h"
#include <math.h>
// The app links FaceclawKit; tests/ios-config-native.cjs builds this file for macOS without it
// and supplies its own codec.
#if __has_include(<FaceclawKit/FaceclawKit.h>)
#import <FaceclawKit/FaceclawKit.h>
@interface FaceclawKitConfigCodec : NSObject <FaceclawConfigCodec>
@end
@implementation FaceclawKitConfigCodec
- (NSString *)exportValuesJson:(NSString *)valuesJson platform:(NSString *)platform appVersion:(NSString *)appVersion {
return [FaceclawKitSettingsTransfer.shared exportValuesJson:valuesJson platform:platform appVersion:appVersion];
}
- (NSString *)importPlanDocument:(NSString *)document platform:(NSString *)platform {
return [FaceclawKitSettingsTransfer.shared importPlanDocument:document platform:platform];
}
@end
#define FACECLAW_CONFIG_CODEC [FaceclawKitConfigCodec new]
#endif
static NSString *const kPlatform = @"ios";
static NSString *const kChangeToken = @"ios.settings.changeToken";
@implementation FaceclawConfigPort
+ (void)processPendingRequest {
#ifdef FACECLAW_CONFIG_CODEC
NSString *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
[self processDirectory:[library stringByAppendingPathComponent:@"FaceclawConfigPort"]
defaults:NSUserDefaults.standardUserDefaults domain:NSBundle.mainBundle.bundleIdentifier];
defaults:NSUserDefaults.standardUserDefaults domain:NSBundle.mainBundle.bundleIdentifier
codec:FACECLAW_CONFIG_CODEC
appVersion:NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"]];
#endif
}
+ (BOOL)validSettings:(id)settings {
if (![settings isKindOfClass:NSDictionary.class] || [settings count] > 4096) return NO;
for (id key in settings) {
// Typed locals, not messages to id: FaceclawKit declares other `count`/`length` properties.
+ (BOOL)validSettings:(id)candidate {
if (![candidate isKindOfClass:NSDictionary.class]) return NO;
NSDictionary *settings = candidate;
if (settings.count > 4096) return NO;
for (id candidateKey in settings) {
if (![candidateKey isKindOfClass:NSString.class]) return NO;
NSString *key = candidateKey;
id value = settings[key];
if (![key isKindOfClass:NSString.class] || ![key length] || [key length] > 512 ||
if (!key.length || key.length > 512 ||
!([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class])) return NO;
}
return YES;
}
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain {
/** The app's settings: strings and finite numbers (booleans included) in its persistent domain. */
+ (NSDictionary *)exportableSettings:(NSDictionary *)domain {
NSMutableDictionary *settings = [NSMutableDictionary new];
for (NSString *key in domain) {
id value = domain[key];
if ([value isKindOfClass:NSString.class] ||
([value isKindOfClass:NSNumber.class] && isfinite([value doubleValue]))) settings[key] = value;
}
[settings removeObjectForKey:kChangeToken];
return settings;
}
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain
codec:(id<FaceclawConfigCodec>)codec appVersion:(NSString *)appVersion {
NSString *requestPath = [directory stringByAppendingPathComponent:@"request.json"];
NSData *data = [NSData dataWithContentsOfFile:requestPath];
if (!data) return;
NSFileManager *fm = NSFileManager.defaultManager;
// Consume once. A failed import must never be replayed at a later launch.
[fm removeItemAtPath:requestPath error:nil];
id request = data.length <= 2 * 1024 * 1024 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
// A push embeds a document of up to 2 MiB as a JSON string, which escaping can enlarge.
id request = data.length <= 8 * 1024 * 1024 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
BOOL valid = [request isKindOfClass:NSDictionary.class];
NSString *identifier = valid && [request[@"id"] isKindOfClass:NSString.class] ? request[@"id"] : @"";
NSMutableDictionary *reply = [@{@"id":identifier, @"ok":@NO, @"message":@"Invalid config request"} mutableCopy];
[defaults synchronize];
NSDictionary *before = [defaults persistentDomainForName:domain] ?: @{};
if (valid && [request[@"operation"] isEqual:@"pull"]) {
NSMutableDictionary *settings = [NSMutableDictionary new];
for (NSString *key in before) {
id value = before[key];
if ([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class]) settings[key] = value;
NSDictionary *settings = [self exportableSettings:before];
NSData *values = [NSJSONSerialization dataWithJSONObject:settings options:0 error:nil];
NSString *document = values ? [codec exportValuesJson:[[NSString alloc] initWithData:values encoding:NSUTF8StringEncoding]
platform:kPlatform appVersion:appVersion] : nil;
if (document) {
reply[@"ok"] = @YES; reply[@"message"] = @"Config exported";
reply[@"document"] = document; reply[@"count"] = @(settings.count);
} else reply[@"message"] = @"Could not encode settings";
} else if (valid && [request[@"operation"] isEqual:@"push"] && [request[@"document"] isKindOfClass:NSString.class]) {
NSData *planData = [[codec importPlanDocument:request[@"document"] platform:kPlatform] dataUsingEncoding:NSUTF8StringEncoding];
id plan = planData ? [NSJSONSerialization JSONObjectWithData:planData options:0 error:nil] : nil;
NSDictionary *settings = [plan isKindOfClass:NSDictionary.class] ? plan[@"settings"] : nil;
if (![plan isKindOfClass:NSDictionary.class] || ![plan[@"ok"] isEqual:@YES]) {
id message = [plan isKindOfClass:NSDictionary.class] ? plan[@"message"] : nil;
reply[@"message"] = [message isKindOfClass:NSString.class] ? message : @"Invalid config";
} else if (![self validSettings:settings]) {
reply[@"message"] = @"Invalid config";
} else {
NSError *error = nil;
NSData *backup = [NSPropertyListSerialization dataWithPropertyList:before format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
if (backup && [backup writeToFile:[directory stringByAppendingPathComponent:@"previous.plist"] options:NSDataWritingAtomic error:&error]) {
NSMutableDictionary *next = [before mutableCopy];
[next addEntriesFromDictionary:settings];
next[kChangeToken] = NSUUID.UUID.UUIDString;
[defaults setPersistentDomain:next forName:domain];
if ([defaults synchronize]) {
reply[@"ok"] = @YES; reply[@"message"] = @"Config imported";
reply[@"count"] = @(settings.count);
if ([plan[@"skipped"] isKindOfClass:NSArray.class]) reply[@"skipped"] = plan[@"skipped"];
if ([plan[@"sourcePlatform"] isKindOfClass:NSString.class]) reply[@"sourcePlatform"] = plan[@"sourcePlatform"];
} else {
[defaults setPersistentDomain:before forName:domain]; [defaults synchronize];
reply[@"message"] = @"Could not persist config; restored previous settings";
}
} else reply[@"message"] = @"Could not back up existing settings; import skipped";
}
[settings removeObjectForKey:@"ios.settings.changeToken"];
reply[@"config"] = @{@"schema":@1, @"settings":settings}; reply[@"ok"] = @YES;
reply[@"message"] = @"Config exported";
} else if (valid && [request[@"operation"] isEqual:@"push"] && [self validSettings:request[@"settings"]]) {
NSError *error = nil;
NSData *backup = [NSPropertyListSerialization dataWithPropertyList:before format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
if (backup && [backup writeToFile:[directory stringByAppendingPathComponent:@"previous.plist"] options:NSDataWritingAtomic error:&error]) {
NSMutableDictionary *next = [before mutableCopy];
[next addEntriesFromDictionary:request[@"settings"]];
next[@"ios.settings.changeToken"] = NSUUID.UUID.UUIDString;
[defaults setPersistentDomain:next forName:domain];
if ([defaults synchronize]) {
reply[@"ok"] = @YES; reply[@"message"] = @"Config imported";
reply[@"count"] = @([request[@"settings"] count]);
} else {
[defaults setPersistentDomain:before forName:domain]; [defaults synchronize];
reply[@"message"] = @"Could not persist config; restored previous settings";
}
} else reply[@"message"] = @"Could not back up existing settings; import skipped";
}
NSData *response = [NSJSONSerialization dataWithJSONObject:reply options:NSJSONWritingPrettyPrinted error:nil];
[response writeToFile:[directory stringByAppendingPathComponent:@"response.json"] options:NSDataWritingAtomic error:nil];
+4
View File
@@ -2,4 +2,8 @@
@interface FaceclawCrypto : NSObject
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message;
+ (NSString *)sha256:(NSData *)data;
/** byteCount bytes from the system CSPRNG, base64url without padding (an OAuth state or PKCE verifier). */
+ (NSString *)randomBase64Url:(NSInteger)byteCount;
/** SHA-256 of the text's UTF-8, base64url without padding (a PKCE S256 code challenge). */
+ (NSString *)sha256Base64Url:(NSString *)text;
@end
+21
View File
@@ -1,6 +1,15 @@
#import "FaceclawCrypto.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonDigest.h>
#import <Security/Security.h>
static NSString *Base64Url(NSData *data) {
NSString *base64 = [data base64EncodedStringWithOptions:0];
base64 = [base64 stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
base64 = [base64 stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
return [base64 stringByReplacingOccurrencesOfString:@"=" withString:@""];
}
@implementation FaceclawCrypto
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message {
NSData *key = [secret dataUsingEncoding:NSUTF8StringEncoding];
@@ -16,4 +25,16 @@
for (NSUInteger i = 0; i < sizeof(digest); i++) [hex appendFormat:@"%02x", digest[i]];
return hex;
}
+ (NSString *)randomBase64Url:(NSInteger)byteCount {
if (byteCount <= 0 || byteCount > 1024) return nil;
NSMutableData *bytes = [NSMutableData dataWithLength:(NSUInteger)byteCount];
if (SecRandomCopyBytes(kSecRandomDefault, bytes.length, bytes.mutableBytes) != errSecSuccess) return nil;
return Base64Url(bytes);
}
+ (NSString *)sha256Base64Url:(NSString *)text {
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(data.bytes, (CC_LONG)data.length, digest);
return Base64Url([NSData dataWithBytes:digest length:sizeof(digest)]);
}
@end
+7
View File
@@ -7,4 +7,11 @@
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size;
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height;
+ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight;
/**
* [widthLo, widthHi, heightLo, heightHi, BGRA...] fitted within maxSize (never upscaled),
* composited over black; the input of the shared Kotlin gray tone curve and dither.
*/
+ (NSData *)bgraPacket:(CGImageRef)image maxSize:(NSInteger)maxSize;
/** bgraPacket of encoded image bytes (JPEG, PNG, ...); nil when undecodable. */
+ (NSData *)bgraPacketFromImageData:(NSData *)data maxSize:(NSInteger)maxSize;
@end
+38
View File
@@ -120,4 +120,42 @@
CGColorSpaceRelease(space);
return gray;
}
+ (NSData *)bgraPacket:(CGImageRef)image maxSize:(NSInteger)maxSize {
if (!image || maxSize <= 0 || maxSize > 1024) return nil;
double sourceWidth = CGImageGetWidth(image), sourceHeight = CGImageGetHeight(image);
if (sourceWidth <= 0 || sourceHeight <= 0) return nil;
double scale = MIN(1.0, MIN(maxSize / sourceWidth, maxSize / sourceHeight));
NSInteger width = MAX(1, lround(sourceWidth * scale)), height = MAX(1, lround(sourceHeight * scale));
NSMutableData *packet = [NSMutableData dataWithLength:4 + width * height * 4];
uint8_t *bytes = packet.mutableBytes;
bytes[0] = width & 255; bytes[1] = (width >> 8) & 255;
bytes[2] = height & 255; bytes[3] = (height >> 8) & 255;
uint8_t *pixels = bytes + 4;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
// Memory order B, G, R, A: one little-endian ARGB word per pixel.
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4, space,
kCGBitmapByteOrder32Little | (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(space);
if (!context) return nil;
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
// Composited over black (the display's see-through background); now opaque.
for (NSInteger i = 0; i < width * height; i++) pixels[i * 4 + 3] = 255;
return packet;
}
+ (NSData *)bgraPacketFromImageData:(NSData *)data maxSize:(NSInteger)maxSize {
if (!data.length || maxSize <= 0 || maxSize > 1024) return nil;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
if (!source) return nil;
NSDictionary *options = @{(id)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
(id)kCGImageSourceCreateThumbnailWithTransform: @YES,
(id)kCGImageSourceThumbnailMaxPixelSize: @(maxSize)};
CGImageRef image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options);
CFRelease(source);
if (!image) return nil;
NSData *packet = [self bgraPacket:image maxSize:maxSize];
CGImageRelease(image);
return packet;
}
@end
@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
/**
* Catches an OAuth redirect to http://127.0.0.1:<port><path> (the loopback redirect that
* providers such as Spotify accept from native apps): listens on the loopback interface
* only, answers the browser's request for the path with a short "return to Faceclaw"
* page and hands back the full redirect URL, query included. Any other path (a favicon)
* gets a 404. Foundation-only, so the macOS test builds it as is.
*/
@interface FaceclawLoopbackRedirect : NSObject
/** Called on the main queue with each redirect to the path. */
@property(nonatomic, copy) void (^redirectHandler)(NSString *url);
/** Start listening; nil on success, otherwise why not (e.g. another app holds the port). */
- (NSString *)startOnPort:(NSInteger)port path:(NSString *)path;
- (void)stop;
@end
@@ -0,0 +1,128 @@
#import "FaceclawLoopbackRedirect.h"
#import <arpa/inet.h>
#import <errno.h>
#import <fcntl.h>
#import <netinet/in.h>
#import <sys/socket.h>
#import <unistd.h>
/** Request heads beyond this are not a browser's redirect; stop reading. */
static const NSUInteger kMaxRequestBytes = 16384;
static NSString *const kDonePage =
@"<!doctype html><html><head><meta charset=\"utf-8\">"
@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Faceclaw</title></head>"
@"<body style=\"font-family: -apple-system, sans-serif; text-align: center; padding: 64px 24px\">"
@"<h2>Signed in</h2><p>You can return to Faceclaw.</p></body></html>";
@interface FaceclawLoopbackRedirect ()
@property(nonatomic, strong) dispatch_queue_t queue;
@property(nonatomic, strong) dispatch_source_t acceptSource;
@property(nonatomic) NSInteger port;
@property(nonatomic, copy) NSString *path;
@end
@implementation FaceclawLoopbackRedirect
- (instancetype)init {
if ((self = [super init])) {
_queue = dispatch_queue_create("com.faceclaw.loopback-redirect", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)dealloc {
[self stop];
}
- (NSString *)startOnPort:(NSInteger)port path:(NSString *)path {
[self stop];
if (port <= 0 || port > 65535 || ![path hasPrefix:@"/"]) return @"Invalid redirect address";
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return [NSString stringWithFormat:@"Could not open a socket (errno %d)", errno];
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
struct sockaddr_in address = {0};
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons((uint16_t)port);
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(fd, (struct sockaddr *)&address, sizeof(address)) != 0 || listen(fd, 8) != 0) {
int error = errno;
close(fd);
return error == EADDRINUSE
? [NSString stringWithFormat:@"Another app is using port %ld; close it and try again", (long)port]
: [NSString stringWithFormat:@"Could not listen on port %ld (errno %d)", (long)port, error];
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
self.port = port;
self.path = path;
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)fd, 0, self.queue);
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(source, ^{
int client = accept(fd, NULL, NULL);
if (client < 0) return;
FaceclawLoopbackRedirect *strongSelf = weakSelf;
if (strongSelf) [strongSelf serve:client];
else close(client);
});
dispatch_source_set_cancel_handler(source, ^{ close(fd); });
self.acceptSource = source;
dispatch_resume(source);
return nil;
}
- (void)stop {
if (self.acceptSource) dispatch_source_cancel(self.acceptSource);
self.acceptSource = nil;
}
/** Read one request head and answer it; runs on the receiver's queue (blocking, with timeouts). */
- (void)serve:(int)client {
// BSD sockets inherit O_NONBLOCK from the listener.
fcntl(client, F_SETFL, fcntl(client, F_GETFL) & ~O_NONBLOCK);
int yes = 1;
setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes));
struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
NSMutableData *head = [NSMutableData data];
NSData *terminator = [@"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding];
char buffer[2048];
while (head.length < kMaxRequestBytes) {
ssize_t count = recv(client, buffer, sizeof(buffer), 0);
if (count <= 0) break;
[head appendBytes:buffer length:(NSUInteger)count];
if ([head rangeOfData:terminator options:0 range:NSMakeRange(0, head.length)].location != NSNotFound) break;
}
NSString *text = [[NSString alloc] initWithData:head encoding:NSUTF8StringEncoding] ?: @"";
NSArray<NSString *> *requestLine = [[text componentsSeparatedByString:@"\r\n"].firstObject componentsSeparatedByString:@" "];
NSString *target = requestLine.count >= 3 && [requestLine[0] isEqualToString:@"GET"] ? requestLine[1] : nil;
BOOL matched = target && [[target componentsSeparatedByString:@"?"].firstObject isEqualToString:self.path];
[self respond:client status:matched ? @"200 OK" : @"404 Not Found" body:matched ? kDonePage : @"Not found"];
close(client);
if (!matched) return;
NSString *url = [NSString stringWithFormat:@"http://127.0.0.1:%ld%@", (long)self.port, target];
void (^handler)(NSString *) = self.redirectHandler;
dispatch_async(dispatch_get_main_queue(), ^{
if (handler) handler(url);
});
}
- (void)respond:(int)client status:(NSString *)status body:(NSString *)body {
NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSString *headers = [NSString stringWithFormat:
@"HTTP/1.1 %@\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: %lu\r\n"
@"Cache-Control: no-store\r\nConnection: close\r\n\r\n", status, (unsigned long)bodyData.length];
NSMutableData *response = [[headers dataUsingEncoding:NSASCIIStringEncoding] mutableCopy];
[response appendData:bodyData];
const uint8_t *bytes = response.bytes;
NSUInteger sent = 0;
while (sent < response.length) {
ssize_t count = send(client, bytes + sent, response.length - sent, 0);
if (count <= 0) break;
sent += (NSUInteger)count;
}
}
@end
@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
/** Lightweight process counters for the on-glasses resource-usage monitor. */
@interface FaceclawResourceUsage : NSObject
/**
* One sample as a JSON object: atMs (monotonic clock, counting sleep), cpuMs
* (user+system time of every thread, alive or exited), footprintKb (the
* phys_footprint that jetsam limits and Xcode's memory gauge shows),
* footprintPeakKb (lifetime peak), residentKb, mallocKb (bytes in use across
* malloc zones), availableKb (headroom before the jetsam limit; 0 when there
* is none, as on the simulator), threads, thermalState
* (NSProcessInfoThermalState) and lowPower. Fields whose query fails are
* omitted.
*/
+ (NSString *)sample;
@end
@@ -0,0 +1,60 @@
#import "FaceclawResourceUsage.h"
#import <TargetConditionals.h>
#import <mach/mach.h>
#import <malloc/malloc.h>
#import <sys/resource.h>
#import <time.h>
#if TARGET_OS_IPHONE
#import <os/proc.h>
#endif
static NSNumber *kib(uint64_t bytes) { return @(bytes / 1024); }
@implementation FaceclawResourceUsage
+ (NSString *)sample {
NSMutableDictionary *out = [NSMutableDictionary new];
out[@"atMs"] = @(clock_gettime_nsec_np(CLOCK_MONOTONIC) / 1000000ULL);
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage) == 0) {
double seconds = (double)usage.ru_utime.tv_sec + (double)usage.ru_stime.tv_sec;
double micros = (double)usage.ru_utime.tv_usec + (double)usage.ru_stime.tv_usec;
out[@"cpuMs"] = @(seconds * 1000.0 + micros / 1000.0);
}
// The kernel fills as much of the struct as it knows and shrinks count to
// match, so later-revision fields are read only when count covers them.
task_vm_info_data_t vm;
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm, &count) == KERN_SUCCESS) {
out[@"residentKb"] = kib(vm.resident_size);
if (count >= TASK_VM_INFO_REV1_COUNT) out[@"footprintKb"] = kib(vm.phys_footprint);
if (count >= TASK_VM_INFO_REV3_COUNT && vm.ledger_phys_footprint_peak > 0) {
out[@"footprintPeakKb"] = kib((uint64_t)vm.ledger_phys_footprint_peak);
}
}
malloc_statistics_t heap = {0};
malloc_zone_statistics(NULL, &heap);
out[@"mallocKb"] = kib(heap.size_in_use);
#if TARGET_OS_IPHONE
out[@"availableKb"] = kib(os_proc_available_memory());
#endif
thread_act_array_t threads = NULL;
mach_msg_type_number_t threadCount = 0;
if (task_threads(mach_task_self(), &threads, &threadCount) == KERN_SUCCESS) {
for (mach_msg_type_number_t i = 0; i < threadCount; i++) mach_port_deallocate(mach_task_self(), threads[i]);
vm_deallocate(mach_task_self(), (vm_address_t)threads, threadCount * sizeof(thread_act_t));
out[@"threads"] = @(threadCount);
}
NSProcessInfo *process = NSProcessInfo.processInfo;
out[@"thermalState"] = @(process.thermalState);
out[@"lowPower"] = @(process.lowPowerModeEnabled);
NSData *json = [NSJSONSerialization dataWithJSONObject:out options:0 error:nil];
return json ? [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding] : @"{}";
}
@end
@@ -0,0 +1,273 @@
@preconcurrency import AVFoundation
import CoreMedia
import Foundation
import Speech
/// Long-form on-device transcription with SpeechAnalyzer and SpeechTranscriber
/// (iOS 26+), for continuous capture (Transcribe, Teleprompter, Calculator):
/// FaceclawSpeech's SFSpeechRecognizer only handles short dictations. It takes
/// the same 16 kHz mono PCM16LE the cloud providers get (see
/// app/native/speech-analyzer-stt.ios.ts) and reports JSON events on the main
/// queue through eventHandler:
///
/// {"kind":"status","message":String}
/// {"kind":"ready","locale":String} audio is accepted from here on
/// {"kind":"transcript","text":String,"final":Bool,
/// "start":Double?,"end":Double?, the result's audio range
/// "speechEnd":Double?} when its last word ends
/// {"kind":"finished"} after finish(), once the last result is in
/// {"kind":"error","message":String}
///
/// Times are seconds of audio since start. Volatile results (final: false)
/// cover all audio not finalized yet, replacing the previous volatile one;
/// their word times are only as fine as the result's range. A final's first
/// word reaches back into the silence before it, so only word ends are
/// reported.
///
/// There is no way to settle text at a pause: on macOS 26.4 any mid-stream
/// SpeechAnalyzer.finalize(through:) left the next phrase's final as dots,
/// while the model splits its finals at pauses on its own.
/// Called from JavaScript, so on the main thread.
@MainActor
@objc(FaceclawSpeechAnalyzer)
@objcMembers
public class FaceclawSpeechAnalyzer: NSObject {
public var eventHandler: ((String) -> Void)?
// An AnalyzerEngine, typed loosely so the class itself loads before iOS 26.
private var engine: AnyObject?
/// Whether this device can transcribe with SpeechTranscriber (iOS 26 and supported hardware).
public static func isSupported() -> Bool {
if #available(iOS 26.0, *) { return SpeechTranscriber.isAvailable }
return false
}
/// `language` is a locale identifier ("en", "en-US"), or "" for the device's preferred language.
public func start(language: String) {
cancel()
guard #available(iOS 26.0, *) else {
emit(["kind": "error", "message": "On-device transcription needs iOS 26 or later."])
return
}
let engine = AnalyzerEngine { [weak self] event in self?.emit(event) }
self.engine = engine
engine.start(language: language)
}
/// 16 kHz mono PCM16LE; ignored until "ready".
public func acceptPcm(_ pcm: Data) {
if #available(iOS 26.0, *) { (engine as? AnalyzerEngine)?.accept(pcm) }
}
/// End of audio: finalize the rest, then report "finished".
public func finish() {
if #available(iOS 26.0, *) { (engine as? AnalyzerEngine)?.finish() }
}
/// Abandon the session without further events.
public func cancel() {
if #available(iOS 26.0, *) { (engine as? AnalyzerEngine)?.cancel() }
engine = nil
}
private func emit(_ event: [String: Any]) {
// JSONSerialization raises (not throws) on NaN and other invalid values.
guard JSONSerialization.isValidJSONObject(event),
let data = try? JSONSerialization.data(withJSONObject: event),
let json = String(data: data, encoding: .utf8) else { return }
eventHandler?(json)
}
}
@available(iOS 26.0, *)
@MainActor
private final class AnalyzerEngine {
private static let sampleRate = 16000.0
private let emit: ([String: Any]) -> Void
private let pcmFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: AnalyzerEngine.sampleRate, channels: 1, interleaved: true)!
private var analyzer: SpeechAnalyzer?
private var input: AsyncStream<AnalyzerInput>.Continuation?
private var analyzerFormat: AVAudioFormat?
// Nil when the analyzer takes the input format as it is.
private var converter: AVAudioConverter?
private var ready = false
private var finishing = false
private var closed = false
private var setup: Task<Void, Never>?
private var results: Task<Void, Never>?
init(emit: @escaping ([String: Any]) -> Void) { self.emit = emit }
func start(language: String) {
setup = Task { [weak self] in await self?.prepare(language: language) }
}
/// Events stop once the session is cancelled, even from work still winding down.
private func send(_ event: [String: Any]) {
if !closed { emit(event) }
}
private func prepare(language: String) async {
send(["kind": "status", "message": "Preparing on-device transcription…"])
let identifier = language.isEmpty ? (Locale.preferredLanguages.first ?? "en-US") : language
guard let locale = await SpeechTranscriber.supportedLocale(equivalentTo: Locale(identifier: identifier)) else {
fail("On-device transcription doesn't support \(identifier). Choose a cloud or Whisper server transcription provider (Settings > Voice).")
return
}
if closed { return }
let transcriber = SpeechTranscriber(locale: locale, transcriptionOptions: [],
reportingOptions: [.volatileResults], attributeOptions: [.audioTimeRange])
do {
// Keep the model installed between sessions (best effort).
if !(await AssetInventory.reservedLocales).contains(locale) {
_ = try? await AssetInventory.reserve(locale: locale)
}
if closed { return }
if let request = try await AssetInventory.assetInstallationRequest(supporting: [transcriber]) {
if closed { return }
try await download(request)
}
if closed { return }
guard let format = await SpeechAnalyzer.bestAvailableAudioFormat(compatibleWith: [transcriber]) else {
fail("On-device transcription has no usable audio format.")
return
}
if closed { return }
if format != pcmFormat {
guard let converter = AVAudioConverter(from: pcmFormat, to: format) else {
fail("On-device transcription can't convert the microphone audio.")
return
}
self.converter = converter
}
analyzerFormat = format
let analyzer = SpeechAnalyzer(modules: [transcriber])
let (stream, input) = AsyncStream<AnalyzerInput>.makeStream()
self.analyzer = analyzer
self.input = input
results = Task { [weak self] in await self?.collect(transcriber) }
try await analyzer.prepareToAnalyze(in: format)
try await analyzer.start(inputSequence: stream)
if closed { return }
ready = true
send(["kind": "ready", "locale": locale.identifier])
if finishing { await finishAnalysis() }
} catch {
fail("On-device transcription: \(error.localizedDescription)")
}
}
/// Fetch the locale's model, reporting progress while it downloads.
private func download(_ request: AssetInstallationRequest) async throws {
let progress = request.progress
let reporter = Task { [weak self] in
while !Task.isCancelled {
let percent = Int((progress.fractionCompleted * 100).rounded())
self?.send(["kind": "status", "message": "Downloading on-device speech model… \(percent)%"])
try? await Task.sleep(nanoseconds: 1_000_000_000)
}
}
defer { reporter.cancel() }
try await request.downloadAndInstall()
}
func accept(_ pcm: Data) {
guard ready, !finishing, !closed, let input, let analyzerFormat else { return }
let frames = AVAudioFrameCount(pcm.count / 2)
guard frames > 0, let buffer = AVAudioPCMBuffer(pcmFormat: pcmFormat, frameCapacity: frames),
let channel = buffer.int16ChannelData else { return }
buffer.frameLength = frames
pcm.withUnsafeBytes { raw in
if let base = raw.baseAddress { memcpy(channel[0], base, Int(frames) * 2) }
}
guard let converter else {
input.yield(AnalyzerInput(buffer: buffer))
return
}
let capacity = AVAudioFrameCount((Double(frames) * analyzerFormat.sampleRate / AnalyzerEngine.sampleRate).rounded(.up)) + 32
guard let converted = AVAudioPCMBuffer(pcmFormat: analyzerFormat, frameCapacity: capacity) else { return }
var consumed = false
var error: NSError?
// noDataNow, not endOfStream: the converter keeps its resampling state for the next chunk.
converter.convert(to: converted, error: &error) { _, status in
if consumed {
status.pointee = .noDataNow
return nil
}
consumed = true
status.pointee = .haveData
return buffer
}
if error == nil && converted.frameLength > 0 { input.yield(AnalyzerInput(buffer: converted)) }
}
func finish() {
guard !finishing, !closed else { return }
finishing = true
// Before "ready", prepare() finishes once the analyzer is running.
if ready { Task { await finishAnalysis() } }
}
private func finishAnalysis() async {
input?.finish()
input = nil
do {
try await analyzer?.finalizeAndFinishThroughEndOfInput()
} catch {
fail("On-device transcription: \(error.localizedDescription)")
}
// collect() reports "finished" once the results sequence ends.
}
func cancel() {
guard !closed else { return }
closed = true
input?.finish()
input = nil
setup?.cancel()
results?.cancel()
if let analyzer { Task { await analyzer.cancelAndFinishNow() } }
analyzer = nil
}
private func collect(_ transcriber: SpeechTranscriber) async {
do {
for try await result in transcriber.results {
if closed { return }
send(event(for: result))
}
send(["kind": "finished"])
} catch {
fail("On-device transcription: \(error.localizedDescription)")
}
}
private func event(for result: SpeechTranscriber.Result) -> [String: Any] {
let text = result.text
var speechEnd: Double?
for run in text.runs {
guard let range = run.audioTimeRange,
!String(text[run.range].characters).trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue }
speechEnd = finite(range.end) ?? speechEnd
}
return [
"kind": "transcript",
"text": String(text.characters),
"final": result.isFinal,
"start": finite(result.range.start) ?? NSNull(),
"end": finite(result.range.end) ?? NSNull(),
"speechEnd": speechEnd ?? NSNull(),
]
}
private func finite(_ time: CMTime) -> Double? {
let seconds = time.seconds
return seconds.isFinite ? seconds : nil
}
private func fail(_ message: String) {
guard !closed else { return }
emit(["kind": "error", "message": message])
cancel()
}
}
@@ -0,0 +1,694 @@
import Foundation
#if canImport(FoundationModels)
import FoundationModels
#endif
/// The on-phone LLM on iOS: Apple's Foundation Models framework (the Apple
/// Intelligence system model, iOS 26+, eligible hardware). It runs out of
/// process on the Neural Engine, so it costs the app no model download or
/// memory, and unlike Metal work it is allowed (within a system budget) while
/// Faceclaw is in the background driving the glasses.
///
/// JavaScript (app/native/local-llm.ios.ts) drives the same provider-neutral
/// agent loop as the cloud models: each start() is one loop iteration. The
/// request is JSON:
///
/// {"system":String,
/// "tools":[{"name","description","parameters":<JSON Schema>}],
/// "history":[{"type":"prompt"|"response","text"} |
/// {"type":"toolCalls","calls":[{"id","name","arguments":<JSON string>}]} |
/// {"type":"toolOutput","id","name","text"}],
/// "prompt":String, "maxTokens":Int, "permissive":Bool}
///
/// and events arrive on the main queue through eventHandler:
///
/// {"kind":"delta","text":String} streamed reply text
/// {"kind":"toolCalls","calls":[{"id","name","arguments":<JSON string>}]}
/// {"kind":"done"}
/// {"kind":"error","code":String,"message":String}
/// {"kind":"log","message":String}
///
/// The framework runs tools itself, mid-response. When the model calls one,
/// the call is reported as "toolCalls" and the generation stays suspended
/// inside the tool until resolveToolCalls() hands back the results that
/// JavaScript's tool loop produced; the reply then continues on the same
/// instance.
///
/// The model's context window is small (4096 tokens on iOS 26, shared by input
/// and reply) and the framework spends ~50 tokens on each tool before its
/// parameters, so the assistant's full tool list (30+ tools, ~5000 tokens) can't
/// be offered at once. When the tools don't fit their share of the window, a
/// quick schema-constrained routing request first asks the model which few
/// tools the request needs. History is then trimmed to fit before each start().
/// Called from JavaScript, so on the main thread.
@MainActor
@objc(FaceclawSystemLanguageModel)
@objcMembers
public class FaceclawSystemLanguageModel: NSObject {
public var eventHandler: ((String) -> Void)?
// A LanguageModelEngine, typed loosely so the class itself loads before iOS 26.
private var engine: AnyObject?
/// "available", or why not: "unsupportedOS", "deviceNotEligible",
/// "appleIntelligenceNotEnabled", "modelNotReady" (still downloading), "unavailable".
/// Nonisolated: worker isolates load the model-choice list too, off the main thread.
nonisolated public static func availability() -> String {
#if canImport(FoundationModels)
if #available(iOS 26.0, *) {
switch SystemLanguageModel.default.availability {
case .available:
return "available"
case .unavailable(let reason):
switch reason {
case .deviceNotEligible: return "deviceNotEligible"
case .appleIntelligenceNotEnabled: return "appleIntelligenceNotEnabled"
case .modelNotReady: return "modelNotReady"
@unknown default: return "unavailable"
}
}
}
#endif
return "unsupportedOS"
}
/// The model's context window in tokens (input and output share it), or 0 when unavailable.
nonisolated public static func contextSize() -> Int {
#if canImport(FoundationModels)
if #available(iOS 26.0, *) { return SystemLanguageModel.default.contextSize }
#endif
return 0
}
public func start(_ requestJson: String) {
cancel()
#if canImport(FoundationModels)
if #available(iOS 26.0, *) {
guard let data = requestJson.data(using: .utf8),
let request = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
emit(["kind": "error", "code": "other", "message": "Invalid on-device model request"])
return
}
let engine = LanguageModelEngine { [weak self] event in self?.emit(event) }
self.engine = engine
engine.start(request)
return
}
#endif
emit(["kind": "error", "code": "unavailable", "message": "The on-device model needs iOS 26 or later."])
}
/// `[{"id","content","isError"}]` for the calls of the last "toolCalls" event.
public func resolveToolCalls(_ resultsJson: String) {
#if canImport(FoundationModels)
if #available(iOS 26.0, *) {
let data = resultsJson.data(using: .utf8) ?? Data()
let results = ((try? JSONSerialization.jsonObject(with: data)) as? [[String: Any]]) ?? []
(engine as? LanguageModelEngine)?.resolve(results)
}
#endif
}
/// Abandon the generation (and any suspended tool calls) without further events.
public func cancel() {
#if canImport(FoundationModels)
if #available(iOS 26.0, *) { (engine as? LanguageModelEngine)?.cancel() }
#endif
engine = nil
}
private func emit(_ event: [String: Any]) {
// JSONSerialization raises (not throws) on NaN and other invalid values.
guard JSONSerialization.isValidJSONObject(event),
let data = try? JSONSerialization.data(withJSONObject: event),
let json = String(data: data, encoding: .utf8) else { return }
eventHandler?(json)
}
}
#if canImport(FoundationModels)
/// A JavaScript-side tool: the framework calls it mid-response, and the
/// engine parks the call until JavaScript resolves it.
@available(iOS 26.0, *)
private struct BridgedTool: Tool {
typealias Arguments = GeneratedContent
typealias Output = String
let name: String
let description: String
let parameters: GenerationSchema
let handler: @Sendable (String, String) async throws -> String
func call(arguments: GeneratedContent) async throws -> String {
try await handler(name, arguments.jsonString)
}
}
/// One tool from the request, convertible at two levels of verbosity.
@available(iOS 26.0, *)
private struct ToolSpec {
let name: String
let description: String
let parameters: [String: Any]
}
@available(iOS 26.0, *)
@MainActor
final class LanguageModelEngine {
private struct PendingCall {
let id: String
let name: String
let arguments: String
let continuation: CheckedContinuation<String, Error>
}
/// How long a call waits for any parallel calls before they're reported together.
private static let toolBatchDelayNs: UInt64 = 30_000_000
/// Tokens kept free beyond the reply budget: the framework's own framing and rounding.
private static let contextMargin = 64
/// Share of the context window tool definitions may take before tools are routed.
private static let toolBudgetFraction = 0.4
/// Most tools a routing request picks.
private static let maxRoutedTools = 5
/// Tools used in the latest exchanges stay offered, for follow-ups ("make it ten minutes").
private static let keptRecentTools = 2
private let emit: ([String: Any]) -> Void
private var task: Task<Void, Never>?
// Calls not yet reported, then reported calls awaiting results.
private var unreported: [PendingCall] = []
private var awaiting: [String: PendingCall] = [:]
private var flushScheduled = false
private var closed = false
init(emit: @escaping ([String: Any]) -> Void) { self.emit = emit }
func start(_ request: [String: Any]) {
task = Task { [weak self] in await self?.run(request) }
}
func resolve(_ results: [[String: Any]]) {
for result in results {
guard let id = result["id"] as? String, let call = awaiting.removeValue(forKey: id) else { continue }
let content = (result["content"] as? String) ?? ""
let isError = (result["isError"] as? Bool) ?? false
// An error goes back to the model as text: a thrown error would end the whole response.
call.continuation.resume(returning: isError ? "Error: \(content)" : (content.isEmpty ? "(no output)" : content))
}
// A call JavaScript didn't answer can't be left hanging.
for (_, call) in awaiting {
call.continuation.resume(returning: "Error: the tool returned no result")
}
awaiting.removeAll()
}
func cancel() {
closed = true
task?.cancel()
task = nil
for call in unreported { call.continuation.resume(throwing: CancellationError()) }
for (_, call) in awaiting { call.continuation.resume(throwing: CancellationError()) }
unreported.removeAll()
awaiting.removeAll()
}
/// Events stop once the session is cancelled, even from work still winding down.
private func send(_ event: [String: Any]) {
if !closed { emit(event) }
}
private func run(_ request: [String: Any]) async {
let system = (request["system"] as? String) ?? ""
let prompt = (request["prompt"] as? String) ?? ""
let maxTokens = (request["maxTokens"] as? NSNumber)?.intValue ?? 512
let permissive = (request["permissive"] as? Bool) ?? false
let toolSpecs: [ToolSpec] = ((request["tools"] as? [[String: Any]]) ?? []).compactMap { tool in
guard let name = tool["name"] as? String else { return nil }
return ToolSpec(name: name, description: (tool["description"] as? String) ?? "",
parameters: (tool["parameters"] as? [String: Any]) ?? [:])
}
let history = (request["history"] as? [[String: Any]]) ?? []
// Transformations of user text (translate, summarize) get the permissive
// guardrails; they apply to plain string replies, which tool-free requests are.
let model = SystemLanguageModel(useCase: .general,
guardrails: permissive ? .permissiveContentTransformations : .default)
var offered = toolSpecs
if !toolSpecs.isEmpty {
switch await selectTools(model: model, specs: toolSpecs, history: history, prompt: prompt) {
case .tools(let specs): offered = specs
case .failed(let event):
send(event)
return
}
if closed { return }
}
let fitted = await fit(model: model, system: system, tools: offered, history: history,
prompt: prompt, maxTokens: maxTokens)
if closed { return }
let handler: @Sendable (String, String) async throws -> String = { [weak self] name, arguments in
guard let self = self else { throw CancellationError() }
return try await self.toolInvoked(name: name, arguments: arguments)
}
let tools: [any Tool] = fitted.tools.map {
BridgedTool(name: $0.name, description: $0.description, parameters: $0.schema, handler: handler)
}
let session = LanguageModelSession(model: model, tools: tools, transcript: Transcript(entries: fitted.entries))
let options = GenerationOptions(maximumResponseTokens: maxTokens)
var last = ""
do {
for try await snapshot in session.streamResponse(to: prompt, options: options) {
if closed { return }
let text = snapshot.content
// Snapshots are cumulative; one that doesn't extend the last is a new segment.
let delta = text.hasPrefix(last) ? String(text.dropFirst(last.count)) : text
last = text
if !delta.isEmpty { send(["kind": "delta", "text": delta]) }
}
send(["kind": "done"])
} catch {
if closed || error is CancellationError { return }
send(describe(error))
}
}
// MARK: - Tool calls
private func toolInvoked(name: String, arguments: String) async throws -> String {
if closed { throw CancellationError() }
return try await withCheckedThrowingContinuation { continuation in
if closed {
continuation.resume(throwing: CancellationError())
return
}
let id = "fm_" + UUID().uuidString.replacingOccurrences(of: "-", with: "").prefix(16).lowercased()
unreported.append(PendingCall(id: id, name: name, arguments: arguments, continuation: continuation))
scheduleFlush()
}
}
/// Report calls in one event, after a moment for any parallel calls to arrive.
private func scheduleFlush() {
if flushScheduled { return }
flushScheduled = true
Task { [weak self] in
try? await Task.sleep(nanoseconds: LanguageModelEngine.toolBatchDelayNs)
self?.flush()
}
}
private func flush() {
flushScheduled = false
if closed || unreported.isEmpty { return }
let calls = unreported
unreported.removeAll()
for call in calls { awaiting[call.id] = call }
send(["kind": "toolCalls",
"calls": calls.map { ["id": $0.id, "name": $0.name, "arguments": $0.arguments] }])
}
// MARK: - Tool routing
private enum Selection {
case tools([ToolSpec])
case failed([String: Any])
}
/// All tools when their definitions fit their share of the context window;
/// otherwise the few the model picks for this request, plus recently used ones.
private func selectTools(model: SystemLanguageModel, specs: [ToolSpec], history: [[String: Any]],
prompt: String) async -> Selection {
let budget = Int(Double(model.contextSize) * LanguageModelEngine.toolBudgetFraction)
let cost = await toolTokens(model, buildTools(specs, compact: false))
if cost <= budget { return .tools(specs) }
let names = specs.map { $0.name }
let catalog = specs.map { "- \($0.name): \(SchemaBuilder.shorten($0.description, sentences: 1, limit: 100))" }
.joined(separator: "\n")
let instructions = "You pick the tools a voice assistant needs to handle the user's latest request. " +
"Choose at most \(LanguageModelEngine.maxRoutedTools) tools from this list that the assistant should be able to use, " +
"most important first. Choose none for small talk or questions it can answer from general knowledge.\n\n" + catalog
var query = prompt
if let previous = lastPromptText(history) {
query = "Previous request: \(SchemaBuilder.shorten(previous, sentences: 3, limit: 300))\n\nLatest request: \(prompt)"
}
do {
let schema = try GenerationSchema(root: DynamicGenerationSchema(name: "ToolSelection", properties: [
DynamicGenerationSchema.Property(
name: "tools", description: "Names of the tools needed, most important first.",
schema: DynamicGenerationSchema(arrayOf: DynamicGenerationSchema(type: String.self, guides: [.anyOf(names)]),
minimumElements: 0,
maximumElements: LanguageModelEngine.maxRoutedTools)),
]), dependencies: [])
let session = LanguageModelSession(model: model, instructions: instructions)
let response = try await session.respond(to: query, schema: schema,
options: GenerationOptions(sampling: .greedy, maximumResponseTokens: 100))
var chosen = Set(try response.content.value([String].self, forProperty: "tools"))
let recent = recentToolNames(history).prefix(LanguageModelEngine.keptRecentTools)
chosen.formUnion(recent)
let selected = specs.filter { chosen.contains($0.name) }
send(["kind": "log", "message": "routed \(specs.count) tools (\(cost) tokens) to: " +
(selected.isEmpty ? "none" : selected.map { $0.name }.joined(separator: ", "))])
return .tools(selected)
} catch {
if let generationError = error as? LanguageModelSession.GenerationError {
switch generationError {
case .rateLimited, .concurrentRequests, .assetsUnavailable, .guardrailViolation, .refusal,
.unsupportedLanguageOrLocale:
// The real request would fail the same way.
return .failed(describe(error))
default:
break
}
}
send(["kind": "log", "message": "tool routing failed (\(error.localizedDescription)); offering tools in order"])
return .tools(specs)
}
}
private func toolTokens(_ model: SystemLanguageModel, _ tools: [BuiltTool]) async -> Int {
let definitions = tools.map {
Transcript.ToolDefinition(name: $0.name, description: $0.description, parameters: $0.schema)
}
let instructions = Transcript.Entry.instructions(Transcript.Instructions(segments: [], toolDefinitions: definitions))
let schemaCharacters = tools.reduce(0) { total, tool in total + tool.schema.debugDescription.count }
return await tokenCount(model, [instructions], extraCharacters: schemaCharacters + 150 * tools.count)
}
private func lastPromptText(_ history: [[String: Any]]) -> String? {
for item in history.reversed() where item["type"] as? String == "prompt" {
if let text = item["text"] as? String, !text.isEmpty { return text }
}
return nil
}
/// Tool names from the newest tool calls back, without repeats.
private func recentToolNames(_ history: [[String: Any]]) -> [String] {
var names: [String] = []
for item in history.reversed() where item["type"] as? String == "toolCalls" {
for call in (item["calls"] as? [[String: Any]]) ?? [] {
if let name = call["name"] as? String, !names.contains(name) { names.append(name) }
}
}
return names
}
// MARK: - Context fitting
private struct BuiltTool {
let name: String
let description: String
let schema: GenerationSchema
}
private struct Fitted {
let tools: [BuiltTool]
let entries: [Transcript.Entry]
}
/// Instructions + tool definitions + history + prompt must leave the reply
/// budget free. In order, until it fits: shorten tool descriptions, drop the
/// oldest exchanges, then drop the last-registered tools.
private func fit(model: SystemLanguageModel, system: String, tools specs: [ToolSpec],
history: [[String: Any]], prompt: String, maxTokens: Int) async -> Fitted {
let contextSize = model.contextSize
let reserve = min(maxTokens, contextSize / 3) + LanguageModelEngine.contextMargin
var tools = buildTools(specs, compact: false)
var past = transcriptEntries(history)
let promptEntry = Transcript.Entry.prompt(Transcript.Prompt(segments: [.text(Transcript.TextSegment(content: prompt))]))
func entries() -> [Transcript.Entry] {
let definitions = tools.map {
Transcript.ToolDefinition(name: $0.name, description: $0.description, parameters: $0.schema)
}
let instructions = Transcript.Instructions(segments: [.text(Transcript.TextSegment(content: system))],
toolDefinitions: definitions)
return [.instructions(instructions)] + past
}
func overBudget() async -> Bool {
// The tokenizer can't see tool schemas' size from a ToolDefinition; the fallback estimate adds them.
let schemaCharacters = tools.reduce(0) { total, tool in total + tool.schema.debugDescription.count }
let used = await tokenCount(model, entries() + [promptEntry], extraCharacters: schemaCharacters)
return used + reserve > contextSize
}
if !tools.isEmpty, await overBudget() {
tools = buildTools(specs, compact: true)
send(["kind": "log", "message": "shortened tool descriptions to fit the context window"])
}
var droppedEntries = 0
while !past.isEmpty, await overBudget() {
// Drop the oldest exchange: through to the next user prompt.
past.removeFirst()
droppedEntries += 1
while let first = past.first, !isPrompt(first) {
past.removeFirst()
droppedEntries += 1
}
}
if droppedEntries > 0 {
send(["kind": "log", "message": "dropped \(droppedEntries) history entries to fit the context window"])
}
var droppedTools: [String] = []
while !tools.isEmpty, await overBudget() {
droppedTools.append(tools.removeLast().name)
}
if !droppedTools.isEmpty {
send(["kind": "log", "message": "dropped tools to fit the context window: \(droppedTools.joined(separator: ", "))"])
}
return Fitted(tools: tools, entries: entries())
}
private func tokenCount(_ model: SystemLanguageModel, _ entries: [Transcript.Entry], extraCharacters: Int) async -> Int {
if #available(iOS 26.4, macOS 26.4, *) {
if let count = try? await model.tokenCount(for: entries) { return count }
}
// Before iOS 26.4 there's no tokenizer API: estimate conservatively.
var characters = extraCharacters
for entry in entries { characters += estimateCharacters(entry) + 16 }
return characters / 3
}
private func estimateCharacters(_ entry: Transcript.Entry) -> Int {
func segments(_ list: [Transcript.Segment]) -> Int {
list.reduce(0) { total, segment in
switch segment {
case .text(let text): return total + text.content.count
case .structure(let structure): return total + structure.content.jsonString.count
@unknown default: return total
}
}
}
switch entry {
case .instructions(let instructions):
var total = segments(instructions.segments)
for definition in instructions.toolDefinitions {
total += definition.name.count + definition.description.count
}
return total
case .prompt(let prompt): return segments(prompt.segments)
case .response(let response): return segments(response.segments)
case .toolCalls(let calls): return calls.reduce(0) { $0 + $1.toolName.count + $1.arguments.jsonString.count }
case .toolOutput(let output): return segments(output.segments)
@unknown default: return 0
}
}
private func isPrompt(_ entry: Transcript.Entry) -> Bool {
if case .prompt = entry { return true }
return false
}
// MARK: - Request conversion
private func transcriptEntries(_ history: [[String: Any]]) -> [Transcript.Entry] {
var entries: [Transcript.Entry] = []
for item in history {
let text = (item["text"] as? String) ?? ""
switch item["type"] as? String {
case "prompt":
entries.append(.prompt(Transcript.Prompt(segments: [.text(Transcript.TextSegment(content: text))])))
case "response":
entries.append(.response(Transcript.Response(assetIDs: [], segments: [.text(Transcript.TextSegment(content: text))])))
case "toolCalls":
let calls: [Transcript.ToolCall] = ((item["calls"] as? [[String: Any]]) ?? []).compactMap { call in
guard let id = call["id"] as? String, let name = call["name"] as? String else { return nil }
let arguments = (try? GeneratedContent(json: (call["arguments"] as? String) ?? "{}"))
?? GeneratedContent(properties: [:])
return Transcript.ToolCall(id: id, toolName: name, arguments: arguments)
}
if !calls.isEmpty { entries.append(.toolCalls(Transcript.ToolCalls(calls))) }
case "toolOutput":
guard let id = item["id"] as? String else { continue }
entries.append(.toolOutput(Transcript.ToolOutput(
id: id, toolName: (item["name"] as? String) ?? "tool",
segments: [.text(Transcript.TextSegment(content: text))])))
default:
continue
}
}
return entries
}
private func buildTools(_ specs: [ToolSpec], compact: Bool) -> [BuiltTool] {
specs.map { spec in
var builder = SchemaBuilder(compact: compact)
let schema: GenerationSchema
do {
schema = try GenerationSchema(root: builder.object(spec.parameters, name: "\(spec.name)_arguments"),
dependencies: [])
} catch {
send(["kind": "log", "message": "tool \(spec.name): unusable parameter schema (\(error)); offering it without parameters"])
schema = (try? GenerationSchema(root: DynamicGenerationSchema(name: "\(spec.name)_arguments", properties: []),
dependencies: []))
?? GenerationSchema(type: GeneratedContent.self, properties: [])
}
let description = compact ? SchemaBuilder.shorten(spec.description, sentences: 1, limit: 160) : spec.description
return BuiltTool(name: spec.name, description: description, schema: schema)
}
}
// MARK: - Errors
private func describe(_ error: Error) -> [String: Any] {
if let error = error as? LanguageModelSession.GenerationError {
switch error {
case .exceededContextWindowSize:
return failure("context", "That conversation is too long for the on-device model. Start a new conversation and try again.")
case .assetsUnavailable:
return failure("unavailable", "The Apple Intelligence model isn't available right now. Check that Apple Intelligence is on and finished downloading.")
case .guardrailViolation, .refusal:
return failure("refusal", "The on-device model declined this request.")
case .unsupportedLanguageOrLocale:
return failure("language", "The on-device model doesn't support this language.")
case .rateLimited:
return failure("rateLimited", "iOS is limiting the on-device model while Faceclaw is in the background. Try again in a moment, or with the phone unlocked.")
case .concurrentRequests:
return failure("busy", "The on-device model is busy with another request. Try again in a moment.")
case .decodingFailure, .unsupportedGuide:
return failure("other", "The on-device model produced an unreadable reply: \(error.localizedDescription)")
@unknown default:
return failure("other", "On-device model failed: \(error.localizedDescription)")
}
}
if let error = error as? LanguageModelSession.ToolCallError {
return failure("other", "On-device model tool call failed: \(error.underlyingError.localizedDescription)")
}
return failure("other", "On-device model failed: \(error.localizedDescription)")
}
private func failure(_ code: String, _ message: String) -> [String: Any] {
["kind": "error", "code": code, "message": message]
}
}
/// JSON Schema (the subset our tool definitions use: objects of string, number,
/// integer, boolean, enum and array properties) to a DynamicGenerationSchema, so
/// the framework constrains tool arguments to valid values while decoding.
/// Anything it doesn't understand becomes a string rather than failing.
@available(iOS 26.0, *)
private struct SchemaBuilder {
let compact: Bool
private var names = Set<String>()
init(compact: Bool) { self.compact = compact }
mutating func object(_ json: [String: Any], name: String) -> DynamicGenerationSchema {
let properties = (json["properties"] as? [String: Any]) ?? [:]
let required = Set((json["required"] as? [String]) ?? [])
// JSON objects lose declaration order; keep required properties first, then by name.
let keys = properties.keys.sorted { a, b in
let ra = required.contains(a), rb = required.contains(b)
return ra != rb ? ra : a < b
}
let members: [DynamicGenerationSchema.Property] = keys.compactMap { key in
guard let property = properties[key] as? [String: Any] else { return nil }
return DynamicGenerationSchema.Property(
name: key,
description: describe(property),
schema: value(property, name: "\(name)_\(key)"),
isOptional: !required.contains(key))
}
return DynamicGenerationSchema(name: unique(name), description: nil, properties: members)
}
private mutating func value(_ json: [String: Any], name: String) -> DynamicGenerationSchema {
if let choices = json["enum"] as? [Any], !choices.isEmpty {
let strings = choices.compactMap { $0 as? String }
if strings.count == choices.count {
// A guide renders as one compact JSON Schema enum; an anyOf schema lists
// every choice as its own subschema, at over twice the tokens.
return DynamicGenerationSchema(type: String.self, guides: [.anyOf(strings)])
}
}
switch schemaType(json) {
case "integer":
var guides: [GenerationGuide<Int>] = []
if let minimum = (json["minimum"] as? NSNumber)?.intValue { guides.append(.minimum(minimum)) }
if let maximum = (json["maximum"] as? NSNumber)?.intValue { guides.append(.maximum(maximum)) }
return DynamicGenerationSchema(type: Int.self, guides: guides)
case "number":
var guides: [GenerationGuide<Double>] = []
if let minimum = (json["minimum"] as? NSNumber)?.doubleValue { guides.append(.minimum(minimum)) }
if let maximum = (json["maximum"] as? NSNumber)?.doubleValue { guides.append(.maximum(maximum)) }
return DynamicGenerationSchema(type: Double.self, guides: guides)
case "boolean":
return DynamicGenerationSchema(type: Bool.self)
case "array":
let items = (json["items"] as? [String: Any]) ?? ["type": "string"]
return DynamicGenerationSchema(arrayOf: value(items, name: "\(name)_item"),
minimumElements: (json["minItems"] as? NSNumber)?.intValue,
maximumElements: (json["maxItems"] as? NSNumber)?.intValue)
case "object":
return object(json, name: name)
default:
return DynamicGenerationSchema(type: String.self)
}
}
private func schemaType(_ json: [String: Any]) -> String {
if let type = json["type"] as? String { return type }
if let types = json["type"] as? [String], let type = types.first(where: { $0 != "null" }) { return type }
return json["properties"] != nil ? "object" : "string"
}
private func describe(_ json: [String: Any]) -> String? {
guard let text = json["description"] as? String, !text.isEmpty else { return nil }
return compact ? SchemaBuilder.shorten(text, sentences: 1, limit: 80) : text
}
/// Schema names must be unique within one GenerationSchema.
private mutating func unique(_ base: String) -> String {
var name = base
var suffix = 2
while names.contains(name) {
name = "\(base)_\(suffix)"
suffix += 1
}
names.insert(name)
return name
}
/// The first `sentences` sentences, cut to `limit` characters.
static func shorten(_ text: String, sentences: Int, limit: Int) -> String {
var result = ""
var count = 0
var remainder = Substring(text)
while count < sentences, !remainder.isEmpty {
if let end = remainder.range(of: ". ") {
result += remainder[..<end.lowerBound] + "."
remainder = remainder[end.upperBound...]
if count + 1 < sentences { result += " " }
} else {
result += remainder
remainder = ""
}
count += 1
}
if result.count > limit { result = String(result.prefix(limit - 1)) + "…" }
return result
}
}
#endif
+21
View File
@@ -0,0 +1,21 @@
#import <Foundation/Foundation.h>
/**
* OAuth sign-in in the system browser sheet (ASWebAuthenticationSession, which shares
* Safari's cookies, so a provider the user is already signed in to needs only a consent
* tap) for providers that redirect to a loopback address: a FaceclawLoopbackRedirect
* catches the redirect, then the sheet is closed. Presenting needs Faceclaw in the
* foreground, so a sign-in started from the glasses waits until the user opens the app.
* One sign-in at a time; starting another cancels the first. Call from the main thread.
*/
@interface FaceclawWebSignIn : NSObject
+ (instancetype)shared;
/**
* completion(redirectUrl, nil) once the provider redirects to http://127.0.0.1:<port><path>;
* (nil, "cancelled") when the user closes the sheet or cancel is called; (nil, message)
* when the sheet or the receiver cannot start. Called once, on the main queue.
*/
- (void)signIn:(NSString *)authorizeUrl port:(NSInteger)port path:(NSString *)path
completion:(void (^)(NSString *redirectUrl, NSString *error))completion;
- (void)cancel;
@end
+110
View File
@@ -0,0 +1,110 @@
#import "FaceclawWebSignIn.h"
#import "FaceclawLoopbackRedirect.h"
#import <AuthenticationServices/AuthenticationServices.h>
#import <UIKit/UIKit.h>
/** Never reached: the loopback receiver ends the session before any custom-scheme redirect. */
static NSString *const kUnusedCallbackScheme = @"faceclaw-signin";
@interface FaceclawWebSignIn () <ASWebAuthenticationPresentationContextProviding>
@property(nonatomic, strong) FaceclawLoopbackRedirect *receiver;
@property(nonatomic, strong) ASWebAuthenticationSession *session;
@property(nonatomic, copy) void (^completion)(NSString *redirectUrl, NSString *error);
/** The sign-in page waiting for Faceclaw to come to the foreground. */
@property(nonatomic, copy) NSURL *pendingUrl;
@property(nonatomic, strong) id activeObserver;
@end
@implementation FaceclawWebSignIn
+ (instancetype)shared {
static FaceclawWebSignIn *instance;
static dispatch_once_t once;
dispatch_once(&once, ^{ instance = [FaceclawWebSignIn new]; });
return instance;
}
- (void)signIn:(NSString *)authorizeUrl port:(NSInteger)port path:(NSString *)path
completion:(void (^)(NSString *, NSString *))completion {
[self finishWithUrl:nil error:@"cancelled"];
NSURL *url = [NSURL URLWithString:authorizeUrl];
if (!url || ![url.scheme isEqualToString:@"https"]) {
if (completion) completion(nil, @"Invalid sign-in address");
return;
}
FaceclawLoopbackRedirect *receiver = [FaceclawLoopbackRedirect new];
NSString *error = [receiver startOnPort:port path:path];
if (error) {
if (completion) completion(nil, error);
return;
}
__weak typeof(self) weakSelf = self;
receiver.redirectHandler = ^(NSString *redirectUrl) {
[weakSelf finishWithUrl:redirectUrl error:nil];
};
self.receiver = receiver;
self.completion = completion;
if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) {
[self present:url];
return;
}
self.pendingUrl = url;
self.activeObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {
FaceclawWebSignIn *strongSelf = weakSelf;
NSURL *pending = strongSelf.pendingUrl;
if (!pending) return;
[strongSelf removeActiveObserver];
[strongSelf present:pending];
}];
}
- (void)cancel {
[self finishWithUrl:nil error:@"cancelled"];
}
- (void)present:(NSURL *)url {
__weak typeof(self) weakSelf = self;
ASWebAuthenticationSession *session = [[ASWebAuthenticationSession alloc] initWithURL:url
callbackURLScheme:kUnusedCallbackScheme completionHandler:^(NSURL *callbackURL, NSError *error) {
// The sheet closed: the user dismissed it, or finishing already cancelled it.
BOOL dismissed = !error || ([error.domain isEqualToString:ASWebAuthenticationSessionErrorDomain]
&& error.code == ASWebAuthenticationSessionErrorCodeCanceledLogin);
[weakSelf finishWithUrl:callbackURL.absoluteString
error:callbackURL ? nil : dismissed ? @"cancelled" : error.localizedDescription];
}];
session.presentationContextProvider = self;
session.prefersEphemeralWebBrowserSession = NO;
self.session = session;
if (![session start]) [self finishWithUrl:nil error:@"Could not open the sign-in page"];
}
/** Report the outcome once; tear down the receiver and close the sheet. */
- (void)finishWithUrl:(NSString *)url error:(NSString *)error {
void (^completion)(NSString *, NSString *) = self.completion;
ASWebAuthenticationSession *session = self.session;
self.completion = nil;
self.session = nil;
self.pendingUrl = nil;
[self removeActiveObserver];
[self.receiver stop];
self.receiver = nil;
// Re-enters through the session's completion handler, which then finds nothing to report.
[session cancel];
if (completion) completion(url, url ? nil : error);
}
- (void)removeActiveObserver {
if (self.activeObserver) [NSNotificationCenter.defaultCenter removeObserver:self.activeObserver];
self.activeObserver = nil;
}
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session {
for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
if (![scene isKindOfClass:UIWindowScene.class]) continue;
NSArray<UIWindow *> *windows = ((UIWindowScene *)scene).windows;
for (UIWindow *window in windows) if (window.isKeyWindow) return window;
if (windows.firstObject) return windows.firstObject;
}
return [UIWindow new];
}
@end
+3
View File
@@ -11,5 +11,8 @@ module FaceclawGraphics {
header "FaceclawEvenHubWebView.h"
header "FaceclawCrypto.h"
header "FaceclawQrScanner.h"
header "FaceclawLoopbackRedirect.h"
header "FaceclawWebSignIn.h"
header "FaceclawResourceUsage.h"
export *
}