forked from Blendman974/faceclaw
Optimize phone CPU usage
This commit is contained in:
1 parent
449cb0bf73
commit
45d2ece0d2
19 files changed
+485
-103
No files matched your search
@@ -349,6 +349,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())
|
||||
|
||||
@@ -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
|
||||
@@ -114,6 +116,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 +222,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)
|
||||
|
||||
Reference in new issue
Block a user