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

This commit is contained in:
James Babcock committed 2026-10-06 13:32:49 -07:00
commit e12cfaaf38
309 files changed
+15906 -3352

No files matched your search

@@ -9,6 +9,10 @@ class AndroidSessionLink(private val bleManager: FaceclawBleManager) : SessionLi
override fun connect(address: String, timeoutMs: Int, autoConnect: Boolean): Boolean =
bleManager.connect(address, timeoutMs, autoConnect)
override fun beginConnect(address: String, background: Boolean): Boolean = bleManager.beginConnect(address, background)
override fun isConnected(address: String): Boolean = bleManager.isConnected(address)
override fun requestHighPriority(address: String) {
bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH)
}
@@ -187,6 +187,8 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
fun setListener(listener: FaceclawBleCommunicatorListener?) = core.setListener(listener)
fun setRequiredFirmwareRevision(revision: Int) = core.setRequiredFirmwareRevision(revision)
fun start() {
if (core.start()) {
activeInstance = this
@@ -292,6 +294,10 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
fun stopG2AudioForwarding() = core.stopG2AudioForwarding()
fun addAudioMonitorListener(listener: FaceclawAudioPacketListener?) = core.addAudioMonitorListener(listener)
fun removeAudioMonitorListener(listener: FaceclawAudioPacketListener?) = core.removeAudioMonitorListener(listener)
fun addCompassListener(listener: FaceclawCompassListener?) = core.addCompassListener(listener)
fun removeCompassListener(listener: FaceclawCompassListener?) = core.removeCompassListener(listener)
@@ -316,6 +322,14 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green)
}
/**
* Posted to the main looper after each step of an animation the preview is replaying (menu
* highlight slides, scrolls), so the mirror can pull the new frame; frame metrics only cover
* frames sent to the glasses, which carry the whole animation in one display list.
*/
fun setPreviewAnimationListener(listener: Runnable?) =
core.setPreviewAnimationListener(listener?.let { { mainHandler.post(it); Unit } })
/** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */
@Throws(java.io.IOException::class)
fun saveCompositePngScreenshot(): String {
@@ -429,6 +443,9 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
contentFingerprint, paintMs, frameId, if (glyphs == null) null else AndroidByteReader(glyphs))
}
/** Whether submitting this full-surface content would change nothing (see [GlassesSessionCore.isSurfaceCurrent]). */
fun isSurfaceCurrent(surfaceId: String, fingerprint: String): Boolean = core.isSurfaceCurrent(surfaceId, fingerprint)
/** Play a tone sequence via CFW load_image_z mode 5 kind 4 (payload built on the TS side). */
fun playBuzzerSequence(payload: java.nio.ByteBuffer?) {
val bytes = ByteArray(if (payload == null) 0 else payload.remaining())
@@ -11,14 +11,13 @@ import android.bluetooth.BluetoothGattService
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.util.Log
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
@@ -28,6 +27,26 @@ 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)
// 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
// Android's Bluetooth stack has process-wide command-pipeline constraints on some
// devices, so every GATT operation in the process is serialized on this one lock,
// across manager instances too (the session and each stock-firmware flow own one).
// An operation holds it until its completion callback; waiting for a connection
// to come up, or backing off before a write retry, does not.
private val bluetoothApiLock = Any()
// Listener callbacks run here, in arrival order, instead of on the binder thread
// that delivered them. BluetoothGatt's callback interface is oneway, so a listener
// that blocks (the session core waits for its monitor) would otherwise hold back
// every later callback for that device, including the write completion that the
// monitor's holder may itself be waiting for.
private val callbackExecutor: ExecutorService = Executors.newSingleThreadExecutor { runnable ->
Thread(runnable, "FaceclawBleCallbacks").apply { isDaemon = true }
}
// 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.
@@ -48,29 +67,35 @@ class FaceclawBleManager(context: Context) {
}
}
private val context: Context
private val bluetoothAdapter: BluetoothAdapter
private val mainHandler = Handler(Looper.getMainLooper())
/**
* One BluetoothGatt client for an address: a pending connection attempt until
* [connected]. [gatt] is set once connectGatt returns; a callback can arrive before
* that, while it is still null.
*/
private class GattLink {
@Volatile var gatt: BluetoothGatt? = null
@Volatile var connected = false
val connectLatch = CountDownLatch(1)
}
private val gattClients = ConcurrentHashMap<String, BluetoothGatt>()
private val bluetoothApiLock = Any()
private enum class OpKind { MTU, SERVICES, DESCRIPTOR, WRITE }
/** The GATT operation awaiting its completion callback (the global lock allows one at a time). */
private class PendingOp(val address: String, val kind: OpKind) {
val latch = CountDownLatch(1)
@Volatile var status: Int? = null
}
private val context: Context
private val bluetoothManager: BluetoothManager
private val bluetoothAdapter: BluetoothAdapter
private val links = ConcurrentHashMap<String, GattLink>()
private val negotiatedMtus = ConcurrentHashMap<String, Int>()
private val messageLocks = ConcurrentHashMap<String, Any>()
private val connectLatches = ConcurrentHashMap<String, CountDownLatch>()
private val connectResults = ConcurrentHashMap<String, Boolean>()
private val servicesLatches = ConcurrentHashMap<String, CountDownLatch>()
private val servicesStatuses = ConcurrentHashMap<String, Int>()
private val mtuLatches = ConcurrentHashMap<String, CountDownLatch>()
private val negotiatedMtus = ConcurrentHashMap<String, Int>()
private val mtuStatuses = ConcurrentHashMap<String, Int>()
private val descriptorLatches = ConcurrentHashMap<String, CountDownLatch>()
private val descriptorStatuses = ConcurrentHashMap<String, Int>()
private val writeLatches = ConcurrentHashMap<String, CountDownLatch>()
private val writeStatuses = ConcurrentHashMap<String, Int>()
@Volatile
private var pendingOp: PendingOp? = null
@Volatile
private var listener: FaceclawBleListener? = null
@@ -81,6 +106,7 @@ class FaceclawBleManager(context: Context) {
if (bluetoothManager == null || bluetoothManager.adapter == null) {
throw IllegalStateException("Bluetooth adapter unavailable")
}
this.bluetoothManager = bluetoothManager
this.bluetoothAdapter = bluetoothManager.adapter
}
@@ -123,9 +149,19 @@ class FaceclawBleManager(context: Context) {
}
}
/** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */
/** Whether a connection to this address is up (not merely pending). */
fun isConnected(address: String?): Boolean {
return address != null && gattClients.containsKey(address)
return address != null && links[address]?.connected == true
}
/**
* Start connecting without waiting; a later [connect] waits on the same attempt.
* [background] uses Android's autoConnect: a low-duty-cycle attempt with no timeout
* that lands whenever the device next advertises (a direct attempt scans hard but
* gives up after about 30 s). Returns true while a connection exists or is pending.
*/
fun beginConnect(address: String?, background: Boolean): Boolean {
return startConnect(requireAddress(address), background) != null
}
fun connect(address: String?, timeoutMs: Int): Boolean = connect(address, timeoutMs, false)
@@ -136,71 +172,73 @@ class FaceclawBleManager(context: Context) {
* set (confirmed via its own BLE debug log, "connect() - device: ...,
* auto: true"), while every other faceclaw connection (both glasses arms)
* uses autoConnect=false. Direct connect (false) is faster to establish
* but Android supervises it less patiently; a live test tonight showed
* the ring-only direct connection dying almost exactly 5 seconds in,
* repeatedly, with nothing else competing for it - consistent with a
* companion device Android's stack expects to be babied via autoConnect,
* not direct-connected like the glasses. autoConnect=true can take much
* longer to actually complete (Android manages it as a background
* reconnect, not an immediate attempt), so this needs its own longer
* timeout - do not reuse CONNECT_TIMEOUT_MS/awaitLatch's short window for
* it, that would just trade one spurious failure for another.
*
* Only the connectGatt call and the timeout cleanup run under the global
* API lock; the wait for the link does not. A pending connection is not a
* GATT operation in flight, and holding the lock across it let an
* unreachable ring (20s autoConnect timeout, retried every 2s) starve every
* glasses write.
* but Android supervises it less patiently; a live test showed the
* ring-only direct connection dying almost exactly 5 seconds in,
* repeatedly, with nothing else competing for it. autoConnect=true can
* take much longer to actually complete (Android manages it as a
* background reconnect, not an immediate attempt), so this needs its own
* longer timeout. Like [connect], waits on an attempt already pending.
*/
fun connect(address: String?, timeoutMs: Int, autoConnect: Boolean): Boolean {
if (address == null || address.trim().isEmpty()) {
throw IllegalArgumentException("address is required")
val key = requireAddress(address)
val link = startConnect(key, autoConnect) ?: return false
if (link.connected) {
return true
}
val latch = CountDownLatch(1)
val gatt: BluetoothGatt
synchronized(gattLock(address)) {
if (gattClients[address] != null) {
return true
if (!awaitLatch(link.connectLatch, timeoutMs)) {
Log.w(TAG, "connect timed out after ${timeoutMs}ms: address=$key"
+ " (no connection event; the device may not be advertising, or may be connected to something else)")
dropLink(key, link)
return false
}
return link.connected
}
/** The current client for [address], starting a connection attempt when there is none. */
private fun startConnect(address: String, background: Boolean): GattLink? {
synchronized(bluetoothApiLock) {
val existing = links[address]
if (existing != null) {
if (!existing.connected || isOsConnected(existing)) {
return existing
}
// A client can go stale without a disconnect callback (the Bluetooth
// service drops its clients when the adapter turns off).
Log.w(TAG, "dropping stale client: address=$address (Android reports no GATT connection)")
dropLink(address, existing)
}
val device = bluetoothAdapter.getRemoteDevice(address)
?: throw IllegalArgumentException("remote device not found: $address")
connectLatches[address] = latch
connectResults.remove(address)
gatt = device.connectGatt(
val link = GattLink()
links[address] = link
val gatt: BluetoothGatt? = device.connectGatt(
context,
autoConnect,
background,
gattCallback,
BluetoothDevice.TRANSPORT_LE,
BluetoothDevice.PHY_LE_2M or BluetoothDevice.PHY_LE_1M
) ?: run {
connectLatches.remove(address, latch)
return false
)
if (gatt == null) {
links.remove(address, link)
Log.w(TAG, "connectGatt returned null: address=$address")
return null
}
gattClients[address] = gatt
link.gatt = gatt
Log.i(TAG, "connect started: address=$address background=$background")
return link
}
}
val completed = awaitLatch(latch, timeoutMs)
connectLatches.remove(address, latch)
val connected = connectResults.remove(address) == true
if (completed && connected && gattClients[address] === gatt) {
return true
private fun isOsConnected(link: GattLink): Boolean {
val gatt = link.gatt ?: return false
return try {
bluetoothManager.getConnectionState(gatt.device, BluetoothProfile.GATT) == BluetoothProfile.STATE_CONNECTED
} catch (t: Throwable) {
true
}
// Timed out, or failed. Whoever removed the client from gattClients
// (the disconnect callback, or disconnect()) also closes it.
synchronized(gattLock(address)) {
if (gattClients.remove(address, gatt)) {
negotiatedMtus.remove(address)
gatt.disconnect()
gatt.close()
}
}
return false
}
fun requestConnectionPriority(address: String, priority: Int): Boolean {
synchronized(gattLock(address)) {
synchronized(bluetoothApiLock) {
val gatt = requireGatt(address)
return gatt.requestConnectionPriority(priority)
}
@@ -208,7 +246,7 @@ class FaceclawBleManager(context: Context) {
/** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */
fun prepareBenchmarkLink(address: String, mode: Int) {
synchronized(gattLock(address)) {
synchronized(bluetoothApiLock) {
val gatt = requireGatt(address)
if ((mode and 1) != 0) {
val accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)
@@ -229,69 +267,27 @@ class FaceclawBleManager(context: Context) {
}
fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean {
synchronized(gattLock(address)) {
val latch = CountDownLatch(1)
mtuLatches[address] = latch
mtuStatuses.remove(address)
val gatt = requireGatt(address)
val started = gatt.requestMtu(mtu)
if (!started) {
mtuLatches.remove(address)
return false
}
if (!awaitLatch(latch, timeoutMs)) {
mtuLatches.remove(address)
mtuStatuses.remove(address)
return false
}
val status = mtuStatuses.remove(address)
mtuLatches.remove(address)
return status != null && status == BluetoothGatt.GATT_SUCCESS
}
val status = runOp(address, OpKind.MTU, timeoutMs) { gatt -> gatt.requestMtu(mtu) }
return status == BluetoothGatt.GATT_SUCCESS
}
fun discoverServices(address: String, timeoutMs: Int): Boolean {
synchronized(gattLock(address)) {
val latch = CountDownLatch(1)
servicesLatches[address] = latch
servicesStatuses.remove(address)
val gatt = requireGatt(address)
val started = gatt.discoverServices()
if (!started) {
servicesLatches.remove(address)
return false
}
if (!awaitLatch(latch, timeoutMs)) {
servicesLatches.remove(address)
servicesStatuses.remove(address)
return false
}
val status = servicesStatuses.remove(address)
servicesLatches.remove(address)
return status != null && status == BluetoothGatt.GATT_SUCCESS
}
val status = runOp(address, OpKind.SERVICES, timeoutMs) { gatt -> gatt.discoverServices() }
return status == BluetoothGatt.GATT_SUCCESS
}
fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean {
synchronized(gattLock(address)) {
val latch = CountDownLatch(1)
descriptorLatches[address] = latch
descriptorStatuses.remove(address)
synchronized(bluetoothApiLock) {
val gatt = requireGatt(address)
val characteristic = requireCharacteristic(gatt, characteristicUuid)
val notificationSet = gatt.setCharacteristicNotification(characteristic, enable)
if (!notificationSet) {
descriptorLatches.remove(address)
return false
}
val descriptor = characteristic.getDescriptor(java.util.UUID.fromString(BleProtocol.CCCD_UUID))
if (descriptor == null) {
descriptorLatches.remove(address)
return true
}
@@ -299,20 +295,39 @@ class FaceclawBleManager(context: Context) {
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
else
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
val started = gatt.writeDescriptor(descriptor)
if (!started) {
descriptorLatches.remove(address)
descriptorStatuses.remove(address)
return false
val status = runOp(address, OpKind.DESCRIPTOR, timeoutMs) { client -> client.writeDescriptor(descriptor) }
return status == BluetoothGatt.GATT_SUCCESS
}
}
/**
* Start one GATT operation and wait for its completion callback, holding the global
* lock throughout. Returns the callback's status ([STATUS_LINK_LOST] when the link
* dropped first), or null when Android refused the request or it timed out.
*/
private fun runOp(address: String, kind: OpKind, timeoutMs: Int, start: (BluetoothGatt) -> Boolean): Int? {
synchronized(bluetoothApiLock) {
val op = PendingOp(address, kind)
// Published before the connected check in requireGatt, so a disconnect racing
// with this start either fails that check or finds the op to release.
pendingOp = op
try {
val gatt = requireGatt(address)
if (!start(gatt)) {
Log.w(TAG, "$kind not started: address=$address")
return null
}
if (!awaitLatch(op.latch, timeoutMs)) {
Log.w(TAG, "$kind timed out after ${timeoutMs}ms: address=$address")
return null
}
if (op.status == STATUS_LINK_LOST) {
Log.w(TAG, "$kind abandoned: link lost: address=$address")
}
return op.status
} finally {
pendingOp = null
}
if (!awaitLatch(latch, timeoutMs)) {
descriptorLatches.remove(address)
descriptorStatuses.remove(address)
return false
}
val status = descriptorStatuses.remove(address)
descriptorLatches.remove(address)
return status != null && status == BluetoothGatt.GATT_SUCCESS
}
}
@@ -339,7 +354,7 @@ class FaceclawBleManager(context: Context) {
synchronized(messageLock(address)) {
val gatt: BluetoothGatt
val characteristic: BluetoothGattCharacteristic
synchronized(gattLock(address)) {
synchronized(bluetoothApiLock) {
gatt = requireGatt(address)
characteristic = requireCharacteristic(gatt, characteristicUuid)
}
@@ -369,37 +384,43 @@ class FaceclawBleManager(context: Context) {
var retryCount = 0
while (true) {
val retryReason: String
synchronized(gattLock(address)) {
if (gattClients[address] !== gatt) {
// Disconnected (and closed) since the write started.
Log.w(TAG, "writeCharacteristic abandoned: address=$address no longer connected")
return false
}
val latch = CountDownLatch(1)
writeLatches[address] = latch
writeStatuses.remove(address)
val result = gatt.writeCharacteristic(characteristic, data!!, writeType)
if (result == android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY) {
writeLatches.remove(address, latch)
writeStatuses.remove(address)
retryReason = "busy"
} else if (result != android.bluetooth.BluetoothStatusCodes.SUCCESS) {
writeLatches.remove(address, latch)
writeStatuses.remove(address)
retryReason = "start result=$result"
} else {
if (!awaitLatch(latch, timeoutMs)) {
writeLatches.remove(address, latch)
writeStatuses.remove(address)
synchronized(bluetoothApiLock) {
val op = PendingOp(address, OpKind.WRITE)
pendingOp = op
try {
// The lock is released between frames and retries, so the client may
// have dropped (or been replaced by a reconnect) since the write started.
if (!isCurrentClient(address, gatt)) {
Log.w(TAG, "writeCharacteristic abandoned: link lost: address=$address")
return false
}
val status = writeStatuses.remove(address)
writeLatches.remove(address, latch)
if (status != null && status == BluetoothGatt.GATT_SUCCESS) {
return true
val result = gatt.writeCharacteristic(characteristic, data!!, writeType)
if (result != android.bluetooth.BluetoothStatusCodes.SUCCESS) {
if (!isCurrentClient(address, gatt)) {
Log.w(TAG, "writeCharacteristic abandoned: link lost: address=$address")
return false
}
retryReason = if (result == android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY)
"busy"
else
"start result=$result"
} else {
if (!awaitLatch(op.latch, timeoutMs)) {
Log.w(TAG, "writeCharacteristic timed out after ${timeoutMs}ms waiting for completion: address=$address")
return false
}
val status = op.status
if (status == BluetoothGatt.GATT_SUCCESS) {
return true
}
if (status == STATUS_LINK_LOST) {
Log.w(TAG, "writeCharacteristic abandoned: link lost: address=$address")
return false
}
retryReason = "callback status=$status"
}
retryReason = "callback status=$status"
} finally {
pendingOp = null
}
}
// Back off outside the lock so other devices' operations proceed.
@@ -425,33 +446,81 @@ class FaceclawBleManager(context: Context) {
}
}
/** Close this address's client, cancelling a pending connection attempt. */
fun disconnect(address: String?) {
if (address == null) {
return
}
val gatt = gattClients.remove(address)
negotiatedMtus.remove(address)
// Wake anything waiting on this device first: a closed client never
// calls back, and the waiter may be holding the lock close() needs.
releasePendingOperations(address)
if (gatt == null) {
val link = links[address] ?: return
dropLink(address, link)
}
fun close() {
for (address in links.keys) {
disconnect(address)
}
}
/**
* Forget [link] if it is still current for [address], release anything waiting on it,
* then disconnect and close its client. Closing suppresses the client's remaining
* callbacks, so listeners hear nothing about a link dropped here.
*/
private fun dropLink(address: String, link: GattLink) {
if (!links.remove(address, link)) {
return
}
synchronized(gattLock(address)) {
link.connected = false
negotiatedMtus.remove(address)
failPendingOp(address)
link.connectLatch.countDown()
// startConnect holds the lock until link.gatt is set, so it is set by the time
// this block runs (or connectGatt failed and there is nothing to close).
synchronized(bluetoothApiLock) {
val gatt = link.gatt ?: return
gatt.disconnect()
gatt.close()
}
}
fun close() {
for (address in gattClients.keys) {
disconnect(address)
private fun failPendingOp(address: String) {
val op = pendingOp
if (op != null && op.address == address && op.latch.count > 0) {
op.status = STATUS_LINK_LOST
op.latch.countDown()
}
}
private fun completeOp(address: String, kind: OpKind, status: Int) {
val op = pendingOp
if (op != null && op.address == address && op.kind == kind) {
op.status = status
op.latch.countDown()
}
}
/** Whether [gatt] is still the connected client for [address]. */
private fun isCurrentClient(address: String, gatt: BluetoothGatt): Boolean {
val link = links[address]
return link != null && link.connected && link.gatt === gatt
}
/** Serializes whole multi-frame messages per device; taken before [bluetoothApiLock], never after. */
private fun messageLock(address: String): Any {
return messageLocks.getOrPut(address) { Any() }
}
private fun requireAddress(address: String?): String {
if (address == null || address.trim().isEmpty()) {
throw IllegalArgumentException("address is required")
}
return address
}
private fun requireGatt(address: String): BluetoothGatt {
val gatt = gattClients[address]
if (gatt == null) {
val link = links[address]
val gatt = link?.gatt
if (link == null || gatt == null || !link.connected) {
throw IllegalStateException("Not connected: $address")
}
return gatt
@@ -477,74 +546,54 @@ class FaceclawBleManager(context: Context) {
}
}
private fun gattLock(address: String?): Any {
// Android's Bluetooth stack has process-wide command-pipeline constraints on some devices.
// Keep every BluetoothGatt API call serialized globally, even for different MAC addresses.
// Never hold it across anything unbounded or slow that isn't a GATT operation in flight
// (connection waits, retry sleeps): every glasses frame write needs it.
return bluetoothApiLock
}
/** Serializes whole multi-frame messages per device; taken before [gattLock], never after. */
private fun messageLock(address: String): Any {
return messageLocks.getOrPut(address) { Any() }
}
/**
* Fail every operation still waiting on a callback from [address]. They see no status and
* return false, instead of holding the global lock until their timeouts expire.
*/
private fun releasePendingOperations(address: String) {
connectLatches.remove(address)?.countDown()
servicesLatches.remove(address)?.countDown()
mtuLatches.remove(address)?.countDown()
descriptorLatches.remove(address)?.countDown()
writeLatches.remove(address)?.countDown()
}
private val gattCallback: BluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
Log.i(TAG, "onConnectionStateChange: status=$status newState=$newState")
val address = gatt.device.address
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
connectResults[address] = true
val latch = connectLatches.remove(address)
latch?.countDown()
dispatchConnectionState(address, true)
Log.i(TAG, "onConnectionStateChange: address=$address status=$status newState=$newState")
val link = links[address]
val current = link?.gatt
if (link == null || (current != null && current !== gatt)) {
// A client this manager no longer tracks: make sure it is released.
synchronized(bluetoothApiLock) {
if (newState != BluetoothProfile.STATE_DISCONNECTED) gatt.disconnect()
gatt.close()
}
return
}
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
connectResults[address] = false
val latch = connectLatches.remove(address)
latch?.countDown()
// Unpublish and wake this client's waiters before taking the lock: one of them
// may hold it, waiting for a callback that will now never come.
if (gattClients.remove(address, gatt)) {
negotiatedMtus.remove(address)
releasePendingOperations(address)
}
synchronized(gattLock(address)) {
gatt.close()
}
dispatchConnectionState(address, false)
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
link.connected = true
link.connectLatch.countDown()
dispatchConnectionState(address, link, true)
return
}
if (newState != BluetoothProfile.STATE_DISCONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
return
}
// Disconnected, or a failed attempt (including a connection reported with an
// error status). Wake the waiters first: the lock below may be held by one of
// them, waiting for a callback that will now never come.
links.remove(address, link)
link.connected = false
negotiatedMtus.remove(address)
failPendingOp(address)
link.connectLatch.countDown()
dispatchConnectionState(address, link, false)
synchronized(bluetoothApiLock) {
if (newState != BluetoothProfile.STATE_DISCONNECTED) gatt.disconnect()
gatt.close()
}
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
val address = gatt.device.address
servicesStatuses[address] = status
val latch = servicesLatches.remove(address)
latch?.countDown()
completeOp(gatt.device.address, OpKind.SERVICES, status)
}
override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
val address = gatt.device.address
if (status == BluetoothGatt.GATT_SUCCESS) negotiatedMtus[address] = mtu
mtuStatuses[address] = status
val latch = mtuLatches.remove(address)
latch?.countDown()
completeOp(address, OpKind.MTU, status)
}
override fun onPhyRead(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) {
@@ -558,17 +607,11 @@ class FaceclawBleManager(context: Context) {
}
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
val address = gatt.device.address
descriptorStatuses[address] = status
val latch = descriptorLatches.remove(address)
latch?.countDown()
completeOp(gatt.device.address, OpKind.DESCRIPTOR, status)
}
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
val address = gatt.device.address
writeStatuses[address] = status
val latch = writeLatches.remove(address)
latch?.countDown()
completeOp(gatt.device.address, OpKind.WRITE, status)
}
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray) {
@@ -581,14 +624,31 @@ class FaceclawBleManager(context: Context) {
}
}
private fun dispatchConnectionState(address: String, connected: Boolean) {
val current = listener ?: return
current.onConnectionStateChange(address, connected)
private fun dispatchConnectionState(address: String, link: GattLink, connected: Boolean) {
callbackExecutor.execute {
// Drop news about a client that a newer attempt has already replaced.
val current = links[address]
if (if (connected) current !== link else current != null && current !== link) {
return@execute
}
val target = listener ?: return@execute
try {
target.onConnectionStateChange(address, connected)
} catch (t: Throwable) {
Log.w(TAG, "connection-state listener failed: address=$address", t)
}
}
}
private fun dispatchNotification(address: String, characteristicUuid: String, data: ByteArray?) {
val current = listener ?: return
val copy = data?.clone() ?: ByteArray(0)
current.onNotification(address, characteristicUuid, copy)
callbackExecutor.execute {
val target = listener ?: return@execute
try {
target.onNotification(address, characteristicUuid, copy)
} catch (t: Throwable) {
Log.w(TAG, "notification listener failed: address=$address", t)
}
}
}
}
@@ -0,0 +1,22 @@
package com.faceclaw.app
import android.util.Log
import android.webkit.ConsoleMessage
import android.webkit.WebChromeClient
/**
* Surfaces a hosted EvenHub app's console in logcat (tag FaceclawEvenHubConsole).
* Native rather than a NativeScript-extended WebChromeClient: chatty apps log
* many lines a second, and each one used to cost a Java->JS callback plus three
* JS->Java calls back to read the message, all on the main thread.
*/
class FaceclawEvenHubChromeClient : WebChromeClient() {
override fun onConsoleMessage(message: ConsoleMessage): Boolean {
Log.i(TAG, "${message.message()} (${message.sourceId()}:${message.lineNumber()})")
return true
}
private companion object {
const val TAG = "FaceclawEvenHubConsole"
}
}
@@ -23,4 +23,10 @@ class FaceclawEvenHubJsBridge(private val listener: FaceclawEvenHubListener) {
listener.onEvenAppMessage(handlerName, argsJson, callId)
}
}
/** The page's timer shim has work due before the host's next tick. */
@JavascriptInterface
fun wakeTimers() {
FaceclawEvenHubWebViewHost.getInstance().wakeTimers()
}
}
@@ -1,16 +1,27 @@
package com.faceclaw.app
import android.app.Activity
import android.content.Context
import android.content.MutableContextWrapper
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import java.util.ArrayList
import java.util.HashMap
/**
* Keeps EvenHub app WebViews alive and rendering while the phone shows the
@@ -33,15 +44,23 @@ import java.util.ArrayList
* the chosen WebView to the top of the overlay); hiding sends it back behind.
* All view work happens on the main thread (callers are on the NS/JS thread).
*
* Layout: the activity is edge-to-edge, so the overlay tracks the window
* insets itself. A top bar (the shown app's name and a close button) sits
* under the status bar, and the WebViews fill the area between it and the
* navigation bar / keyboard. That area is the same whether or not the overlay
* is shown, so showing an app doesn't resize (and re-lay-out) its page.
*
* Timer keep-alive: when the phone screen turns off, Chromium heavily throttles
* the page's own setTimeout/setInterval (intensive background throttling clamps
* them to ~1/sec), so timer-driven apps (e.g. snake's game loop) slow to a
* crawl even though the renderer is still alive. Host-initiated
* evaluateJavascript is NOT subject to that throttle, so we drive the app's
* timers ourselves: a document-start shim replaces setTimeout/setInterval (and
* requestAnimationFrame) with JS queues fired by window.__fcTimerTick() /
* __fcRafTick(), and this host ticks those on the main thread at a fixed rate
* regardless of screen state. The main Looper keeps running under the
* requestAnimationFrame) with JS queues fired by window.__fcTick(), and this
* host ticks those on the main thread regardless of screen state. The tick
* returns when the page next has work, so an idle page isn't evaluated at
* 60Hz; work scheduled between ticks wakes the host through
* FaceclawEvenHubJsBridge.wakeTimers. The main Looper keeps running under the
* foreground service, so the tick survives the screen turning off. (Matches the
* official Even app's approach.)
*
@@ -68,28 +87,85 @@ import java.util.ArrayList
*/
class FaceclawEvenHubWebViewHost {
private var overlay: FrameLayout? = null
/** Holds the WebViews, inside [overlay] below the top bar. */
private var stack: FrameLayout? = null
private var titleView: TextView? = null
private var overlayActivity: Activity? = null
private var shown = false
private var shownTitle = ""
private var onClose: Runnable? = null
private val mainHandler = Handler(Looper.getMainLooper())
private val webViews: MutableList<WebView> = ArrayList()
private var ticking = false
private val ticker: Runnable = object : Runnable {
private val tickers: MutableMap<WebView, Ticker> = HashMap()
/**
* Drives one page's timer/rAF queues (window.__fcTick). Each tick's result
* says when the page next has work; the ticker sleeps until then, or until
* the page wakes it. It also re-ticks after IDLE_POLL_MS regardless, so a
* lost evaluateJavascript callback (e.g. mid-navigation) or a page whose
* wake didn't reach us stalls its timers for at most that long.
*/
private inner class Ticker(private val web: WebView) : Runnable {
private var inFlight = false
private var wakePending = false
private var stopped = false
override fun run() {
for (i in 0 until webViews.size) {
webViews[i].evaluateJavascript(
"window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null)
if (stopped) return
inFlight = true
web.evaluateJavascript(TICK_JS) { result -> onTicked(result) }
schedule(IDLE_POLL_MS)
}
private fun onTicked(result: String?) {
inFlight = false
if (stopped) return
// -1 (nothing pending) and null (shim not installed yet) both leave
// only the idle poll; the page wakes us when it schedules work.
val next = result?.toDoubleOrNull()?.toLong() ?: -1L
val delay = when {
wakePending -> 0L
next < 0 -> IDLE_POLL_MS
else -> next.coerceIn(TICK_MS, IDLE_POLL_MS)
}
if (ticking) mainHandler.postDelayed(this, TICK_MS)
wakePending = false
schedule(delay)
}
fun wake() {
if (stopped) return
if (inFlight) wakePending = true else schedule(0)
}
fun stop() {
stopped = true
mainHandler.removeCallbacks(this)
}
private fun schedule(delayMs: Long) {
mainHandler.removeCallbacks(this)
mainHandler.postDelayed(this, delayMs)
}
}
companion object {
private var instance: FaceclawEvenHubWebViewHost? = null
/** How often the host fires the JS timer queue. 60Hz keeps games smooth. */
/** Shortest interval between ticks. 60Hz keeps games smooth. */
private const val TICK_MS = 16L
/** Longest a page goes without a tick, as a backstop for missed wakes. */
private const val IDLE_POLL_MS = 1000L
private const val TICK_JS = "window.__fcTick?__fcTick():null"
/** Top bar height, below the status bar (Material toolbar height). */
private const val BAR_HEIGHT_DP = 56f
/** Matches the NativeScript ActionBar (NativeScriptToolbarStyle). */
private val BAR_COLOR = Color.parseColor("#424242")
@JvmStatic
@Synchronized
fun getInstance(): FaceclawEvenHubWebViewHost {
@@ -103,36 +179,112 @@ class FaceclawEvenHubWebViewHost {
val existing = overlay
if (existing != null && overlayActivity === activity && existing.parent != null) return existing
val content = activity.findViewById<ViewGroup>(android.R.id.content)
(existing?.parent as ViewGroup?)?.removeView(existing)
val created = FrameLayout(activity)
if (existing != null) {
(existing.parent as ViewGroup?)?.removeView(existing)
for (web in webViews) {
existing.removeView(web)
rebaseContext(web, activity)
created.addView(web, FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
}
created.setBackgroundColor(Color.BLACK)
val createdStack = FrameLayout(activity)
created.addView(createdStack, FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
val bar = buildTopBar(activity)
created.addView(bar, FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP))
for (web in webViews) {
(web.parent as ViewGroup?)?.removeView(web)
rebaseContext(web, activity)
createdStack.addView(web, FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
}
// Insets are passed on unconsumed: before API 30 a consuming child
// would starve its later siblings (the NativeScript UI) of them.
ViewCompat.setOnApplyWindowInsetsListener(created) { _, insets ->
applyInsets(bar, createdStack, insets)
insets
}
overlay = created
stack = createdStack
overlayActivity = activity
// index 0 = behind the NativeScript content view (raised again below if shown).
content.addView(created, 0, FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
ViewCompat.requestApplyInsets(created)
if (shown) created.bringToFront()
return created
}
/** The bar across the top of a shown app: the app's name, then a close button. */
private fun buildTopBar(activity: Activity): LinearLayout {
val bar = LinearLayout(activity)
bar.orientation = LinearLayout.HORIZONTAL
bar.gravity = Gravity.CENTER_VERTICAL
bar.setBackgroundColor(BAR_COLOR)
// Swallow touches so they don't fall through to the WebViews.
bar.isClickable = true
val title = TextView(activity)
title.text = shownTitle
title.setTextColor(Color.WHITE)
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f)
title.typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL)
title.maxLines = 1
title.ellipsize = TextUtils.TruncateAt.END
bar.addView(title, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f).apply {
marginStart = dp(activity, 16f)
marginEnd = dp(activity, 12f)
})
val close = TextView(activity)
close.text = "\u2715"
close.setTextColor(Color.WHITE)
close.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f)
close.gravity = Gravity.CENTER
close.contentDescription = "Close"
val ripple = TypedValue()
if (activity.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, ripple, true)) {
close.setBackgroundResource(ripple.resourceId)
}
close.setOnClickListener { onClose?.run() }
val closeSize = dp(activity, 48f)
bar.addView(close, LinearLayout.LayoutParams(closeSize, closeSize).apply {
marginEnd = dp(activity, 4f)
})
titleView = title
return bar
}
/**
* Put the bar under the status bar (its background filling in behind it)
* and the WebViews between the bar and the navigation bar or keyboard.
*/
private fun applyInsets(bar: View, webStack: View, insets: WindowInsetsCompat) {
val bars = insets.getInsets(
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
val ime = insets.getInsets(WindowInsetsCompat.Type.ime())
val barHeight = dp(bar.context, BAR_HEIGHT_DP)
bar.setPadding(bars.left, bars.top, bars.right, 0)
bar.layoutParams = (bar.layoutParams as FrameLayout.LayoutParams).apply {
height = bars.top + barHeight
}
webStack.layoutParams = (webStack.layoutParams as FrameLayout.LayoutParams).apply {
setMargins(bars.left, bars.top + barHeight, bars.right, maxOf(bars.bottom, ime.bottom))
}
}
private fun dp(context: Context, value: Float): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, value, context.resources.displayMetrics).toInt()
}
private fun rebaseContext(web: WebView, activity: Activity) {
(web.context as? MutableContextWrapper)?.baseContext = activity
}
/** Add a WebView to the host, full-size and rendering, hidden behind the UI. */
fun attach(activity: Activity, web: WebView) {
val o = ensureOverlay(activity)
ensureOverlay(activity)
rebaseContext(web, activity)
web.visibility = View.VISIBLE
if (web.parent == null) {
o.addView(web, FrameLayout.LayoutParams(
stack!!.addView(web, FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
}
// resumeTimers() is process-global; make sure nothing left timers paused.
@@ -148,15 +300,33 @@ class FaceclawEvenHubWebViewHost {
web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false)
}
if (!webViews.contains(web)) webViews.add(web)
if (!ticking) {
ticking = true
if (!tickers.containsKey(web)) {
val ticker = Ticker(web)
tickers[web] = ticker
mainHandler.postDelayed(ticker, TICK_MS)
}
}
/** Bring an app's WebView to the front of [activity] so it is visible on the phone. */
fun showOnPhone(activity: Activity?, web: WebView) {
/**
* A page scheduled a timer or animation frame due before its next tick.
* Called from the JavaBridge thread; which page asked doesn't matter, as
* an extra tick for another page is harmless.
*/
fun wakeTimers() {
mainHandler.post {
for (ticker in tickers.values) ticker.wake()
}
}
/**
* Bring an app's WebView to the front of [activity] so it is visible on the
* phone, titled [title]. The top bar's close button runs [onClose].
*/
fun showOnPhone(activity: Activity?, web: WebView, title: String, onClose: Runnable) {
val o = if (activity != null) ensureOverlay(activity) else overlay ?: return
shownTitle = title
this.onClose = onClose
titleView?.text = title
web.bringToFront()
o.bringToFront()
shown = true
@@ -165,6 +335,7 @@ class FaceclawEvenHubWebViewHost {
/** Send the overlay back behind the NativeScript UI. */
fun hideOnPhone() {
shown = false
onClose = null
val o = overlay ?: return
val content = o.parent as ViewGroup?
if (content != null) {
@@ -180,11 +351,8 @@ class FaceclawEvenHubWebViewHost {
/** Remove and destroy a WebView (its app is closing). */
fun detach(web: WebView) {
webViews.remove(web)
if (webViews.isEmpty() && ticking) {
ticking = false
mainHandler.removeCallbacks(ticker)
}
overlay?.removeView(web)
tickers.remove(web)?.stop()
stack?.removeView(web)
web.destroy()
}
}
@@ -0,0 +1,90 @@
package com.faceclaw.app
import android.os.Handler
import android.os.Looper
import java.io.File
import java.io.FileOutputStream
import java.util.concurrent.TimeUnit
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
/**
* One GET streamed straight to a file on a worker thread. For binary bodies
* too large to cross into JS through NativeScript's fetch(), which copies the
* Java byte[] into a Uint8Array one bridge call per byte (a 43MB EvenHub
* package ran the process out of native memory). JS reads the finished file
* back with one bulk copy (readBinaryFile).
*/
class FaceclawFileDownload(
private val url: String,
private val destPath: String,
private val timeoutMs: Long,
listener: FaceclawFileDownloadListener?
) {
private val callbackHandler = Handler(Looper.myLooper() ?: Looper.getMainLooper())
private val listener: FaceclawFileDownloadListener =
listener ?: throw IllegalArgumentException("listener is required")
@Volatile
private var call: Call? = null
@Volatile
private var cancelled = false
fun start() {
Thread({ run() }, "FaceclawFileDl").start()
}
/** Aborts the request; no callback follows. */
fun cancel() {
cancelled = true
call?.cancel()
}
private fun run() {
val dest = File(destPath)
try {
dest.parentFile?.mkdirs()
val request = Request.Builder().url(url).build()
val call = getClient().newCall(request)
call.timeout().timeout(timeoutMs, TimeUnit.MILLISECONDS)
this.call = call
call.execute().use { response ->
val body = response.body
val contentLength = body?.contentLength() ?: -1L
if (response.isSuccessful && body != null) {
body.byteStream().use { input ->
FileOutputStream(dest).use { output -> input.copyTo(output, 1 shl 16) }
}
}
if (!cancelled) callbackHandler.post { listener.onDone(response.code, contentLength) }
}
} catch (e: Exception) {
dest.delete()
if (!cancelled) callbackHandler.post { listener.onFailure(e.message ?: e.toString()) }
}
}
companion object {
@Volatile
private var sharedClient: OkHttpClient? = null
private fun getClient(): OkHttpClient {
if (sharedClient == null) {
synchronized(FaceclawFileDownload::class.java) {
if (sharedClient == null) {
sharedClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.addInterceptor(FaceclawHttp.userAgentInterceptor())
.build()
}
}
}
return sharedClient!!
}
}
}
@@ -87,8 +87,14 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
return false
}
/**
* The tray's notification icons, iconSize*iconSize gray bytes each: one
* per notification group. When keysOut is given, each icon's
* notification key (the group member it was taken from) is appended
* to it in step, so the tray can tell which notification an icon is.
*/
@JvmStatic
fun getActiveNotificationIconGrays(iconSize: Int, maxIcons: Int): ByteArray {
fun getActiveNotificationIconGrays(iconSize: Int, maxIcons: Int, keysOut: MutableList<String>?): ByteArray {
val service = activeService
val size = Math.max(1, Math.min(96, iconSize))
val limit = Math.max(0, maxIcons)
@@ -130,6 +136,7 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() {
+ " drawable=" + drawable.javaClass.simpleName
+ " intrinsic=" + drawable.intrinsicWidth + "x" + drawable.intrinsicHeight)
appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.packageName)
keysOut?.add(statusBarNotification.key ?: "")
if (dedupeGroupKey != null) {
emittedGroupKeys.add(dedupeGroupKey)
}
@@ -51,11 +51,13 @@ class FaceclawPreviewCompositor(context: Context) {
if (activeInstance === this) {
activeInstance = null
}
frameListener = null
setFrameListener(null)
}
/** Also notified after each step of an animation the preview is replaying (see SurfaceCompositor). */
fun setFrameListener(listener: Runnable?) {
this.frameListener = listener
compositor.setPreviewAnimationListener(listener?.let { { mainHandler.post(it); Unit } })
}
/** Set the compositor's output frame size. Call before configuring surfaces. */
@@ -1,113 +0,0 @@
package com.faceclaw.app
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.telephony.SignalStrength
import android.telephony.TelephonyManager
import android.util.Log
import java.io.ByteArrayOutputStream
import java.lang.reflect.Method
/** Reads Wi-Fi/cell/hotspot state; the icon artwork is the shared StatusIconArt. */
class FaceclawSystemStatusIconProvider private constructor() {
companion object {
private const val TAG = "FaceclawStatusIcons"
@JvmStatic
fun getSystemStatusIconGrays(context: Context?, iconSize: Int): ByteArray {
if (context == null) {
return ByteArray(0)
}
val appContext = context.applicationContext
val size = Math.max(1, Math.min(96, iconSize))
val out = ByteArrayOutputStream(size * size * 3)
val wifiLevel = getWifiLevel(appContext)
if (wifiLevel >= 0) {
append(out, StatusIconArt.scale(StatusIconArt.wifi(wifiLevel), size))
}
val cellLevel = getCellLevel(appContext)
if (cellLevel >= 0) {
append(out, StatusIconArt.scale(StatusIconArt.cell(cellLevel), size))
}
if (isHotspotEnabled(appContext)) {
append(out, StatusIconArt.scale(StatusIconArt.hotspot(), size))
}
return out.toByteArray()
}
private fun getWifiLevel(context: Context): Int {
try {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
val activeNetwork: Network? = if (connectivityManager == null) null else connectivityManager.activeNetwork
val capabilities: NetworkCapabilities? = if (activeNetwork == null) null else connectivityManager!!.getNetworkCapabilities(activeNetwork)
if (capabilities == null || !capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return -1
}
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?
val info: WifiInfo? = if (wifiManager == null) null else wifiManager.connectionInfo
if (info == null || !isValidRssi(info.rssi)) {
return -1
}
return Math.max(0, Math.min(4, WifiManager.calculateSignalLevel(info.rssi, 5)))
} catch (t: Throwable) {
Log.w(TAG, "failed to read Wi-Fi status", t)
return -1
}
}
private fun getCellLevel(context: Context): Int {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return -1
}
try {
val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?
val signalStrength: SignalStrength? = if (telephonyManager == null) null else telephonyManager.signalStrength
if (signalStrength == null) {
return -1
}
return Math.max(0, Math.min(4, signalStrength.level))
} catch (e: SecurityException) {
// Most Android versions require READ_PHONE_STATE for this. We skip it
// rather than prompting for a broad phone permission just for an icon.
return -1
} catch (t: Throwable) {
Log.w(TAG, "failed to read cellular status", t)
return -1
}
}
private fun isHotspotEnabled(context: Context): Boolean {
try {
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?
if (wifiManager == null) {
return false
}
val method: Method = wifiManager.javaClass.getDeclaredMethod("getWifiApState")
method.isAccessible = true
val result: Any? = method.invoke(wifiManager)
val state = if (result is Int) result else -1
return state == 13 || state == 12 // WIFI_AP_STATE_ENABLED / ENABLING
} catch (ignored: Throwable) {
return false
}
}
private fun isValidRssi(rssi: Int): Boolean {
return rssi > -127 && rssi < 0
}
private fun append(out: ByteArrayOutputStream, bytes: ByteArray) {
out.write(bytes, 0, bytes.size)
}
}
}
@@ -0,0 +1,791 @@
package com.faceclaw.app
import android.content.Context
import android.media.AudioAttributes
import android.media.AudioFocusRequest
import android.media.AudioManager
import android.media.MediaCodec
import android.media.MediaCodecInfo
import android.media.MediaExtractor
import android.media.MediaFormat
import android.media.MediaPlayer
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import org.json.JSONObject
import java.io.IOException
import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock
import java.util.zip.CRC32
import kotlin.concurrent.withLock
/**
* Plays a video file onto the glasses. A background thread decodes the video track
* (MediaExtractor + MediaCodec), scales each due frame's luma to gray (VideoFrameScaler) and
* submits it straight into a rect of a window's compositor surface, through the same
* submitSurfaceFrame entry point app workers use, so no JS runs per frame. When wanted, the
* audio track plays through a MediaPlayer on the phone's default media route, and the video
* clock follows its position.
*
* The TS side (app/native/video-player.ts) owns the window: it paints the rest of the surface
* and repaints the video rect from [copyLastFrame] whenever it renders. Frames are submitted
* only while playing, and [pause] returns only once no further frame can be submitted, so a
* repaint right after it (the paused overlay) is never overwritten.
*
* Control methods are for the main thread; listener callbacks are posted to it.
*/
class FaceclawVideoPlayer(context: Context) {
interface Listener {
/**
* "playing", "paused" or "ended", with the media position. Also sent as "paused" when a
* seek made while paused has decoded its new frame (see [copyLastFrame]).
*/
fun onStateChanged(state: String?, positionMs: Long)
/** Playback stopped on an error (unreadable file, no decoder, ...). */
fun onError(message: String?)
}
companion object {
private const val TAG = "FaceclawVideo"
private const val CODEC_TIMEOUT_US = 10_000L
/** Longest single sleep while waiting for a frame to come due, so commands stay responsive. */
private const val MAX_WAIT_US = 20_000L
/** A frame this far behind the clock is dropped undrawn... */
private const val LATE_DROP_US = 50_000L
/** ...unless nothing has been drawn for this long, so a slow phone still shows progress. */
private const val MAX_DROP_GAP_NANOS = 250_000_000L
/**
* Decoded frames closer together than this are thinned: past ~30fps the glasses link
* could never show them, and each one still costs a scale and a composite.
*/
private const val MIN_FRAME_INTERVAL_US = 30_000L
/** Audio position is ignored this long after the clock starts (output startup latency). */
private const val AUDIO_SETTLE_NANOS = 500_000_000L
/** Clock drift from the audio position that makes the video clock jump to it. */
private const val AUDIO_RESYNC_US = 60_000L
/** How often the audio position is read (a binder call into the media server). */
private const val AUDIO_CHECK_INTERVAL_NANOS = 250_000_000L
/** A seek never lands closer than this to the end, so it always shows a frame. */
private const val SEEK_END_MARGIN_US = 500_000L
private val AUDIO_ATTRIBUTES: AudioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
.build()
private fun MediaFormat.intOr(key: String, fallback: Int): Int =
if (containsKey(key)) getInteger(key) else fallback
private fun MediaFormat.longOr(key: String, fallback: Long): Long =
if (containsKey(key)) getLong(key) else fallback
private fun findTrack(extractor: MediaExtractor, mimePrefix: String): Int {
for (i in 0 until extractor.trackCount) {
val mime = extractor.getTrackFormat(i).getString(MediaFormat.KEY_MIME) ?: continue
if (mime.startsWith(mimePrefix)) return i
}
return -1
}
/**
* Describe a video file for the settings panel, as JSON: {width, height} in display
* orientation (rotation applied), rotation, durationMs, hasAudio, mime; or {error}.
*/
@JvmStatic
fun probe(path: String?): String {
val result = JSONObject()
val extractor = MediaExtractor()
try {
extractor.setDataSource(path ?: throw IOException("no path"))
val videoTrack = findTrack(extractor, "video/")
if (videoTrack < 0) {
result.put("error", "No video track")
return result.toString()
}
var durationUs = 0L
for (i in 0 until extractor.trackCount) {
durationUs = Math.max(durationUs, extractor.getTrackFormat(i).longOr(MediaFormat.KEY_DURATION, 0L))
}
val format = extractor.getTrackFormat(videoTrack)
val rotation = VideoFrameScaler.normalizeRotation(format.intOr(MediaFormat.KEY_ROTATION, 0))
val codedWidth = format.intOr(MediaFormat.KEY_WIDTH, 0)
val codedHeight = format.intOr(MediaFormat.KEY_HEIGHT, 0)
val swap = rotation == 90 || rotation == 270
result.put("width", if (swap) codedHeight else codedWidth)
result.put("height", if (swap) codedWidth else codedHeight)
result.put("rotation", rotation)
result.put("durationMs", durationUs / 1000)
result.put("hasAudio", findTrack(extractor, "audio/") >= 0)
result.put("mime", format.getString(MediaFormat.KEY_MIME) ?: "")
} catch (e: Exception) {
Log.w(TAG, "probe failed for $path", e)
result.put("error", e.message ?: e.toString())
} finally {
extractor.release()
}
return result.toString()
}
}
/** What one start() call asked for; fixed for that playback's lifetime. */
private class Params(
val path: String,
val surfaceId: String,
val x: Int,
val y: Int,
val width: Int,
val height: Int,
val monochrome: Boolean,
val gamma: Float,
val audio: Boolean,
)
private enum class Wait { DUE, INTERRUPTED, STOP }
private val appContext: Context = context.applicationContext
private val mainHandler = Handler(Looper.getMainLooper())
private val audioManager = appContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
@Volatile private var listener: Listener? = null
private val lock = ReentrantLock()
private val wake = lock.newCondition()
// Everything below is guarded by lock.
/** Bumped by every start/stop; a playback thread whose generation is stale exits. */
private var generation = 0
private var params: Params? = null
/** The user's intent: playing or paused. */
private var playing = false
private var ended = false
/** A seek (or the initial start) is decoding toward its target; the clock waits for its frame. */
private var seeking = false
private var pendingSeekUs = -1L
private var durationUs = 0L
private var clockRunning = false
/** Media time while the clock is stopped. */
private var heldUs = 0L
private var clockBaseUs = 0L
private var clockBaseNanos = 0L
private var clockStartedNanos = 0L
private var lastAudioCheckNanos = 0L
private var audio: MediaPlayer? = null
private var hasAudioFocus = false
private var focusRequest: AudioFocusRequest? = null
/** The most recently decoded frame (params.width x params.height), shown or previewed. */
private var lastFrame: ByteArray? = null
/** Checksum of the frame last submitted, so an unchanged frame is not recomposited; -1 = none. */
private var lastSubmittedChecksum = -1L
private var submitFailures = 0
private val focusListener = AudioManager.OnAudioFocusChangeListener { change ->
when (change) {
AudioManager.AUDIOFOCUS_LOSS, AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
if (change == AudioManager.AUDIOFOCUS_LOSS) lock.withLock { hasAudioFocus = false }
// Another app (a call, a music player) took the audio: pause like any player would.
if (isPlaying()) {
pause()
notifyState(generationNow(), "paused", getPositionMs())
}
}
}
}
fun setListener(listener: Listener?) {
this.listener = listener
}
/**
* Start playing [path] from [startMs] into the (rectX, rectY, width, height) rect of
* compositor surface [surfaceId], replacing any current playback. Decoding and audio setup
* happen on the playback thread; failures arrive as [Listener.onError].
*/
fun start(
path: String,
surfaceId: String,
rectX: Int,
rectY: Int,
width: Int,
height: Int,
monochrome: Boolean,
gamma: Float,
withAudio: Boolean,
startMs: Long,
) {
require(width > 0 && height > 0) { "bad video rect ${width}x$height" }
val p = Params(path, surfaceId, rectX, rectY, width, height, monochrome, gamma, withAudio)
val gen = lock.withLock {
endPlaybackLocked()
params = p
playing = true
ended = false
seeking = true
pendingSeekUs = Math.max(0L, startMs) * 1000
heldUs = pendingSeekUs
// The old frame may not match the new settings (size, monochrome).
lastFrame = null
lastSubmittedChecksum = -1
submitFailures = 0
generation
}
val thread = Thread({ Playback(gen, p).run() }, "FaceclawVideo")
thread.priority = Thread.NORM_PRIORITY + 1
thread.start()
}
/** Pause; once this returns no further frame is submitted until [resume]. */
fun pause() {
lock.withLock {
if (!playing) return
playing = false
holdClockLocked(clockUsLocked())
wake.signalAll()
}
}
fun resume() {
lock.withLock {
if (playing || ended || params == null) return
playing = true
// A fresh frame must go out even if it matches the last one: the TS side painted over
// the surface while paused.
lastSubmittedChecksum = -1
if (!seeking) startClockLocked()
wake.signalAll()
}
}
/** Seek to [positionMs]; while paused, the new frame is decoded and announced as "paused". */
fun seekTo(positionMs: Long) {
lock.withLock {
if (params == null || ended) return
var targetUs = Math.max(0L, positionMs) * 1000
if (durationUs > 0) targetUs = Math.min(targetUs, Math.max(0L, durationUs - SEEK_END_MARGIN_US))
pendingSeekUs = targetUs
seeking = true
holdClockLocked(targetUs)
lastSubmittedChecksum = -1
wake.signalAll()
}
}
fun getPositionMs(): Long = lock.withLock { clockUsLocked() / 1000 }
fun getDurationMs(): Long = lock.withLock { durationUs / 1000 }
fun isPlaying(): Boolean = lock.withLock { playing && !ended && params != null }
/**
* Copy the most recent frame into [out], which must hold exactly the video rect's
* width*height bytes (row-major 8bpp gray). False when there is no frame of that size yet.
*/
fun copyLastFrame(out: ByteBuffer): Boolean {
lock.withLock {
val frame = lastFrame ?: return false
if (out.capacity() != frame.size) return false
out.clear()
out.put(frame)
out.clear()
return true
}
}
/** End playback and free the decoder and audio; the last frame stays readable. */
fun stop() {
lock.withLock { endPlaybackLocked() }
}
/** stop(), and drop the listener and frame. */
fun release() {
lock.withLock {
endPlaybackLocked()
lastFrame = null
}
listener = null
}
private fun generationNow(): Int = lock.withLock { generation }
private fun endPlaybackLocked() {
generation++
params = null
playing = false
seeking = false
pendingSeekUs = -1
// Keep the position readable (an error's caller resumes from it).
heldUs = clockUsLocked()
clockRunning = false
releaseAudioLocked()
wake.signalAll()
}
// ---- clock (all under lock) ----
private fun clockUsLocked(): Long =
if (!clockRunning) heldUs else clockBaseUs + (System.nanoTime() - clockBaseNanos) / 1000
private fun startClockLocked() {
if (clockRunning) return
clockRunning = true
clockBaseUs = heldUs
clockBaseNanos = System.nanoTime()
clockStartedNanos = clockBaseNanos
val player = audio ?: return
requestAudioFocusLocked()
try {
player.start()
} catch (e: IllegalStateException) {
Log.w(TAG, "audio start failed", e)
}
}
private fun holdClockLocked(atUs: Long) {
heldUs = atUs
if (!clockRunning) return
clockRunning = false
try {
audio?.let { if (it.isPlaying) it.pause() }
} catch (e: IllegalStateException) {
Log.w(TAG, "audio pause failed", e)
}
}
/**
* Keep the video clock on the audio position: output latency and seeks make the two drift,
* and a picture ahead of or behind its sound is the one sync error people notice.
*/
private fun followAudioLocked() {
val player = audio ?: return
val now = System.nanoTime()
if (!clockRunning || now - clockStartedNanos < AUDIO_SETTLE_NANOS) return
if (now - lastAudioCheckNanos < AUDIO_CHECK_INTERVAL_NANOS) return
lastAudioCheckNanos = now
val audioUs = try {
if (!player.isPlaying) return // finished (audio shorter than video) or not started
player.currentPosition * 1000L
} catch (e: IllegalStateException) {
return
}
if (Math.abs(audioUs - clockUsLocked()) > AUDIO_RESYNC_US) {
clockBaseUs = audioUs
clockBaseNanos = System.nanoTime()
}
}
private fun seekAudioLocked(targetUs: Long) {
val player = audio ?: return
try {
if (Build.VERSION.SDK_INT >= 26) {
player.seekTo(targetUs / 1000, MediaPlayer.SEEK_CLOSEST)
} else {
player.seekTo((targetUs / 1000).toInt())
}
} catch (e: IllegalStateException) {
Log.w(TAG, "audio seek failed", e)
}
}
private fun requestAudioFocusLocked() {
if (hasAudioFocus) return
val result = if (Build.VERSION.SDK_INT >= 26) {
val request = focusRequest ?: AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(AUDIO_ATTRIBUTES)
.setOnAudioFocusChangeListener(focusListener, mainHandler)
.build()
.also { focusRequest = it }
audioManager.requestAudioFocus(request)
} else {
@Suppress("DEPRECATION")
audioManager.requestAudioFocus(focusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)
}
// Play regardless: the sound was asked for, and a refusal is transient (a call ending).
hasAudioFocus = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
}
private fun releaseAudioLocked() {
val player = audio ?: return
audio = null
try {
player.release()
} catch (e: Exception) {
Log.w(TAG, "audio release failed", e)
}
if (hasAudioFocus) {
hasAudioFocus = false
if (Build.VERSION.SDK_INT >= 26) {
focusRequest?.let { audioManager.abandonAudioFocusRequest(it) }
} else {
@Suppress("DEPRECATION")
audioManager.abandonAudioFocus(focusListener)
}
}
}
// ---- notifications ----
private fun notifyState(gen: Int, state: String, positionMs: Long) {
mainHandler.post {
if (lock.withLock { gen == generation }) listener?.onStateChanged(state, positionMs)
}
}
private fun notifyError(gen: Int, message: String) {
mainHandler.post {
if (lock.withLock { gen == generation }) listener?.onError(message)
}
}
// ---- frame output (under lock) ----
private fun submitLocked(p: Params, pixels: ByteArray, checksum: Long) {
val buffer = ByteBuffer.wrap(pixels)
// Frame id 0: per-frame FrameTimings entries at video rate would crowd every real
// input/render frame out of the timing export.
val fingerprint = "video:$generation:" + java.lang.Long.toHexString(checksum)
try {
val communicator = FaceclawBleCommunicator.getActive()
if (communicator != null) {
communicator.submitSurfaceFrame(buffer, p.surfaceId, p.x, p.y, p.width, p.height, fingerprint, 0, 0, null)
} else {
FaceclawPreviewCompositor.getActive()?.submitSurfaceFrame(
buffer, p.surfaceId, p.x, p.y, p.width, p.height, fingerprint, 0, 0, null)
}
submitFailures = 0
} catch (e: RuntimeException) {
// The window's surface went away or shrank under us (closed, relaid out): drop the frame.
if (submitFailures++ == 0) Log.w(TAG, "video frame submit failed: ${e.message}")
}
}
/**
* One start() call's decode thread. Owns the extractor and decoder; everything shared with
* the control methods goes through the player's lock.
*/
private inner class Playback(private val gen: Int, private val p: Params) {
private var extractor: MediaExtractor? = null
private var codec: MediaCodec? = null
private val info = MediaCodec.BufferInfo()
private var inputDone = false
private var outputDone = false
private var trackEndUs = 0L
/** Frames before this are decoded only on the way to a seek target. */
private var skipUntilUs = 0L
private var lastDrawnPtsUs = Long.MIN_VALUE / 2
private var lastDrawnNanos = 0L
private var rotation = 0
private var lut = ByteArray(256)
private var scaler: VideoFrameScaler? = null
private var luma = ByteArray(0)
/** The frame being built; swapped with lastFrame when shown. */
private var frame = ByteArray(p.width * p.height)
private val crc = CRC32()
fun run() {
try {
open()
loop()
} catch (t: Throwable) {
Log.w(TAG, "playback failed for ${p.path}", t)
val current = lock.withLock {
val current = gen == generation
if (current) endPlaybackLocked()
current
}
// endPlaybackLocked bumped the generation; report under the new one.
if (current) notifyError(generationNow(), t.message ?: t.toString())
} finally {
try {
codec?.stop()
} catch (ignored: Throwable) {
}
codec?.release()
extractor?.release()
}
}
private fun current(): Boolean = lock.withLock { gen == generation }
private fun open() {
val ex = MediaExtractor()
extractor = ex
ex.setDataSource(p.path)
val track = findTrack(ex, "video/")
if (track < 0) throw IOException("No video track")
ex.selectTrack(track)
val format = ex.getTrackFormat(track)
rotation = VideoFrameScaler.normalizeRotation(format.intOr(MediaFormat.KEY_ROTATION, 0))
trackEndUs = format.longOr(MediaFormat.KEY_DURATION, 0L)
// Most video is limited ("TV") range; only an explicit full-range tag says otherwise.
val limited = format.intOr(MediaFormat.KEY_COLOR_RANGE, MediaFormat.COLOR_RANGE_LIMITED) !=
MediaFormat.COLOR_RANGE_FULL
lut = VideoFrameScaler.toneLut(limited, p.gamma, p.monochrome)
val mime = format.getString(MediaFormat.KEY_MIME) ?: throw IOException("Unknown video format")
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible)
val decoder = try {
MediaCodec.createDecoderByType(mime)
} catch (e: Exception) {
throw IOException("No decoder for $mime")
}
codec = decoder
decoder.configure(format, null, null, 0)
decoder.start()
val player = if (p.audio && findTrack(ex, "audio/") >= 0) openAudio() else null
lock.withLock {
if (gen != generation) {
player?.release()
return
}
durationUs = trackEndUs
// Positioned by the start seek, which is the first command loop() handles.
audio = player
}
}
/** A prepared MediaPlayer for the file's audio, or null (played silent) if it won't open. */
private fun openAudio(): MediaPlayer? {
val player = MediaPlayer()
return try {
player.setAudioAttributes(AUDIO_ATTRIBUTES)
player.setDataSource(p.path)
player.prepare()
player
} catch (e: Exception) {
Log.w(TAG, "audio unavailable for ${p.path}; playing silent", e)
player.release()
null
}
}
private fun loop() {
while (true) {
// Commands: stop, seek, or wait out a pause.
var seekUs = -1L
lock.withLock {
while (true) {
if (gen != generation) return
if (pendingSeekUs >= 0) {
seekUs = pendingSeekUs
pendingSeekUs = -1
break
}
if (playing || seeking) break
wake.await()
}
}
if (seekUs >= 0) {
seek(seekUs)
continue
}
if (outputDone) {
if (awaitEnd()) return
continue
}
feedInput()
val index = codec!!.dequeueOutputBuffer(info, CODEC_TIMEOUT_US)
if (index < 0) continue // try again later / format or buffers changed
val endOfStream = (info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0
if (info.size > 0) {
handleFrame(index, info.presentationTimeUs)
} else {
codec!!.releaseOutputBuffer(index, false)
}
if (endOfStream) outputDone = true
}
}
private fun seek(targetUs: Long) {
lock.withLock {
if (gen != generation) return
seekAudioLocked(targetUs)
}
extractor!!.seekTo(targetUs, MediaExtractor.SEEK_TO_PREVIOUS_SYNC)
codec!!.flush()
inputDone = false
outputDone = false
skipUntilUs = targetUs
lastDrawnPtsUs = Long.MIN_VALUE / 2
}
private fun feedInput() {
if (inputDone) return
val decoder = codec!!
val ex = extractor!!
while (true) {
val index = decoder.dequeueInputBuffer(0)
if (index < 0) return
val buffer = decoder.getInputBuffer(index) ?: return
val size = ex.readSampleData(buffer, 0)
if (size < 0) {
decoder.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
inputDone = true
return
}
decoder.queueInputBuffer(index, 0, size, ex.sampleTime, 0)
ex.advance()
}
}
/** Decide a decoded frame's fate (skip, preview, drop, draw); always releases it. */
private fun handleFrame(index: Int, ptsUs: Long) {
try {
if (ptsUs < skipUntilUs) return
val landingSeek = lock.withLock { seeking }
if (landingSeek) {
landSeek(index, ptsUs)
return
}
if (ptsUs - lastDrawnPtsUs < MIN_FRAME_INTERVAL_US) return
if (waitUntilDue(ptsUs) != Wait.DUE) return
val lateUs = lock.withLock { clockUsLocked() } - ptsUs
if (lateUs > LATE_DROP_US && System.nanoTime() - lastDrawnNanos < MAX_DROP_GAP_NANOS) return
val checksum = convert(index)
present(ptsUs, checksum)
} finally {
codec!!.releaseOutputBuffer(index, false)
}
}
/**
* The first frame at or past a seek target (or the start position): drawn and the clock
* started when playing, kept as the paused preview otherwise.
*/
private fun landSeek(index: Int, ptsUs: Long) {
val checksum = convert(index)
var previewed = false
lock.withLock {
if (gen != generation || pendingSeekUs >= 0) return
seeking = false
heldUs = ptsUs
swapInFrameLocked()
if (playing) {
submitLocked(p, lastFrame!!, checksum)
lastSubmittedChecksum = checksum
startClockLocked()
} else {
previewed = true
}
}
lastDrawnPtsUs = ptsUs
lastDrawnNanos = System.nanoTime()
notifyState(gen, if (previewed) "paused" else "playing", ptsUs / 1000)
}
/** Show a due frame, unless a pause or seek got in first. */
private fun present(ptsUs: Long, checksum: Long) {
lock.withLock {
if (gen != generation || !playing || pendingSeekUs >= 0) return
swapInFrameLocked()
if (checksum != lastSubmittedChecksum) {
submitLocked(p, lastFrame!!, checksum)
lastSubmittedChecksum = checksum
}
}
lastDrawnPtsUs = ptsUs
lastDrawnNanos = System.nanoTime()
}
private fun swapInFrameLocked() {
val shown = frame
val previous = lastFrame
frame = if (previous != null && previous.size == shown.size) previous else ByteArray(shown.size)
lastFrame = shown
}
/** Block until the clock reaches [ptsUs]; paused time doesn't count. */
private fun waitUntilDue(ptsUs: Long): Wait {
lock.withLock {
while (true) {
if (gen != generation) return Wait.STOP
if (pendingSeekUs >= 0) return Wait.INTERRUPTED
if (!playing) {
wake.await()
continue
}
followAudioLocked()
val now = clockUsLocked()
if (now >= ptsUs) return Wait.DUE
wake.await(Math.min(ptsUs - now, MAX_WAIT_US), TimeUnit.MICROSECONDS)
}
}
}
/**
* The decoder is drained: hold the last frame until the clock reaches the end, then
* report "ended". False when a seek interrupted the wait (playback continues).
*/
private fun awaitEnd(): Boolean {
val endUs = Math.max(trackEndUs, lastDrawnPtsUs)
val landing = lock.withLock { seeking }
if (!landing && waitUntilDue(endUs) != Wait.DUE) return !current()
lock.withLock {
if (gen != generation) return true
if (pendingSeekUs >= 0) return false
playing = false
seeking = false
ended = true
holdClockLocked(endUs)
releaseAudioLocked()
}
notifyState(gen, "ended", endUs / 1000)
return true
}
/** Scale the output buffer's frame into [frame]; returns its checksum. */
private fun convert(index: Int): Long {
val decoder = codec!!
val image = try {
decoder.getOutputImage(index)
} catch (e: Exception) {
null
}
if (image != null) {
try {
val plane = image.planes[0]
val crop = image.cropRect
val offset = copyLuma(plane.buffer) + crop.top * plane.rowStride + crop.left * plane.pixelStride
scaleInto(crop.width(), crop.height(), offset, plane.rowStride, plane.pixelStride)
} finally {
image.close()
}
} else {
// No Image access: every YUV420 layout starts with the Y plane at the buffer start.
val buffer = decoder.getOutputBuffer(index) ?: throw IOException("Decoder returned no frame")
buffer.position(info.offset)
buffer.limit(info.offset + info.size)
val format = decoder.outputFormat
val width = format.getInteger(MediaFormat.KEY_WIDTH)
val stride = format.intOr(MediaFormat.KEY_STRIDE, width)
val left = format.intOr("crop-left", 0)
val top = format.intOr("crop-top", 0)
val right = format.intOr("crop-right", width - 1)
val bottom = format.intOr("crop-bottom", format.getInteger(MediaFormat.KEY_HEIGHT) - 1)
copyLuma(buffer)
scaleInto(right - left + 1, bottom - top + 1, top * stride + left, stride, 1)
}
crc.reset()
crc.update(frame, 0, frame.size)
return crc.value
}
/** Bulk-copy the plane (one memcpy; per-byte reads from a direct buffer are slow). Returns 0. */
private fun copyLuma(buffer: ByteBuffer): Int {
val bytes = buffer.remaining()
if (luma.size < bytes) luma = ByteArray(bytes)
buffer.get(luma, 0, bytes)
return 0
}
private fun scaleInto(width: Int, height: Int, offset: Int, rowStride: Int, pixelStride: Int) {
var s = scaler
if (s == null || s.sourceWidth != width || s.sourceHeight != height) {
s = VideoFrameScaler(width, height, rotation, p.width, p.height)
scaler = s
}
s.scale(luma, offset, rowStride, pixelStride, lut, frame)
}
}
}
@@ -23,6 +23,8 @@ import java.nio.charset.StandardCharsets
import java.util.ArrayDeque
import java.util.ArrayList
import java.util.Collections
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* Phone side of the Wear OS integration: the single place that talks to the
@@ -61,6 +63,8 @@ class FaceclawWearBridge private constructor(context: Context) {
const val PATH_STATE_REQUEST = "/faceclaw/state/request"
/** Watch -> phone: the watch's own battery; carries no seq and is never acked. */
const val PATH_WATCH_BATTERY = "/faceclaw/battery"
/** Watch -> phone: touch-down ahead of a gesture; carries no seq and is never acked. */
const val PATH_WATCH_PRESS = "/faceclaw/press"
/** Phone -> watch: answered with PATH_WATCH_BATTERY even while the watch app is closed. */
const val PATH_WATCH_BATTERY_REQUEST = "/faceclaw/battery/request"
const val PATH_ACK = "/faceclaw/ack"
@@ -114,6 +118,16 @@ class FaceclawWearBridge private constructor(context: Context) {
private val pending = ArrayDeque<PendingMessage>()
private val watchNodes: MutableList<Node> = ArrayList()
private var lastPublishedState: String? = null
/** Latest state waiting for [publishExecutor]; null when none is queued. Guarded by [lock]. */
private var queuedState: String? = null
/**
* Serializing a DataMap and handing it to Play services costs ~10-25ms per
* publish, and the switcher publishes on every foreground change, so it
* runs here instead of on the main thread.
*/
private val publishExecutor: ExecutorService = Executors.newSingleThreadExecutor { runnable ->
Thread(runnable, "FaceclawWearPublish").apply { isDaemon = true }
}
private class PendingMessage(
val path: String,
@@ -210,12 +224,28 @@ class FaceclawWearBridge private constructor(context: Context) {
publishState(json, false)
}
/** As [publishState]; `force` re-sends an unchanged state. */
/**
* As [publishState]; `force` re-sends an unchanged state. The put happens
* on [publishExecutor]; states that arrive while one is still waiting
* replace it, since only the latest matters to the watch.
*/
fun publishState(json: String?, force: Boolean) {
if (!available || json == null) return
synchronized(lock) {
if (!force && json == lastPublishedState) return
lastPublishedState = json
val alreadyQueued = queuedState != null
queuedState = json
if (alreadyQueued) return
}
publishExecutor.execute { publishQueuedState() }
}
private fun publishQueuedState() {
val json: String
synchronized(lock) {
json = queuedState ?: return
queuedState = null
}
try {
val request = PutDataMapRequest.create(PATH_STATE)
@@ -303,6 +333,9 @@ class FaceclawWearBridge private constructor(context: Context) {
// dashboard to receive it, it is simply dropped (the dashboard polls
// again when it starts), and the watch does not expect an ack.
if (PATH_WATCH_BATTERY == path) return
// A touch-down only matters live; the gesture that follows it gets
// the failure ack below.
if (PATH_WATCH_PRESS == path) return
// No JS listener yet: tell the watch so it can explain why nothing is
// happening. Only state requests are kept for replay (see class doc);
@@ -30,5 +30,23 @@ class PreviewBitmapUtil private constructor() {
val colors = PreviewPalette.expand(bytes, 0, width * height, brightenGamma, green)
return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888)
}
/**
* A white-on-transparent bitmap of an 8bpp glasses icon (gray becomes
* alpha; black, which the glasses show as unlit, is transparent), for
* showing glasses artwork on the phone's dark action bar.
*/
@JvmStatic
fun whiteIconFromGray(gray: ByteBuffer?, width: Int, height: Int): Bitmap {
if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) {
throw IllegalArgumentException("invalid gray icon buffer")
}
val bytes = ByteArray(width * height)
gray.get(bytes)
val colors = IntArray(width * height) { i ->
((bytes[i].toInt() and 0xff) shl 24) or 0x00ffffff
}
return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888)
}
}
}