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
}
}
}