[verified] merge upstream 25cb255b into all-features

This commit is contained in:
MasterControl committed 2026-11-19 22:21:26 +00:00
commit ffcdae660d
169 files changed
+16201 -597

No files matched your search

@@ -292,6 +292,13 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
/** See [GlassesSessionCore.getRingStatus]. */
fun getRingStatus(): String = core.getRingStatus()
/** See [GlassesSessionCore.setHeadUpTrigger]. */
fun setHeadUpTrigger(degrees: Int, originalSwitch: Int, originalAngle: Int) =
core.setHeadUpTrigger(degrees, originalSwitch, originalAngle)
/** See [GlassesSessionCore.getHeadUpStatus]. */
fun getHeadUpStatus(): String = core.getHeadUpStatus()
/** See [GlassesSessionCore.setGlassesRingLink]. */
fun setGlassesRingLink(action: String, fallbackAddress: String?, fallbackName: String?): Boolean =
core.setGlassesRingLink(action, fallbackAddress, fallbackName)
@@ -324,6 +331,13 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean =
core.sendFaceclawMicControl(record, label, rightTemple, leftTemple)
fun addKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.addKeyboardEventListener(listener)
fun removeKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.removeKeyboardEventListener(listener)
fun sendFaceclawKeyboardControl(record: ByteArray?, label: String): Boolean =
core.sendFaceclawKeyboardControl(record, label)
fun startG2AudioForwarding(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioForwarding(listener)
fun stopG2AudioForwarding() = core.stopG2AudioForwarding()
@@ -0,0 +1,212 @@
package com.faceclaw.app
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.security.keystore.StrongBoxUnavailableException
import android.util.Base64
import android.util.Log
import com.faceclaw.ssh.faceclawssh.Channel
import com.faceclaw.ssh.faceclawssh.Faceclawssh
import com.faceclaw.ssh.faceclawssh.Listener
import com.faceclaw.ssh.faceclawssh.Signer
import java.security.KeyPairGenerator
import java.security.KeyStore
import java.security.PrivateKey
import java.security.Signature
import java.security.spec.ECGenParameterSpec
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
/**
* One SSH channel for the TypeScript side (app/native/ssh.ts), backed by the
* gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec JSON.
* Events arrive from Go goroutines and are posted to the Looper of the
* thread that constructed this object (a JS isolate's own thread), like
* FaceclawWebSocket.
*/
class FaceclawSshChannel(spec: String, useKey: Boolean, listener: FaceclawSshListener) {
private val handler = Handler(Looper.myLooper() ?: Looper.getMainLooper())
private val channel: Channel
init {
val signer = if (useKey) FaceclawSshKeys.signer() else null
channel = Faceclawssh.open(spec, signer, object : Listener {
override fun onEvent(kind: String?, payload: String?) {
handler.post {
try {
listener.onEvent(kind, payload)
} catch (t: Throwable) {
Log.w(TAG, "listener onEvent($kind) failed", t)
}
}
}
})
}
fun send(text: String): Boolean = channel.send(text)
fun close() = channel.close()
private companion object {
const val TAG = "FaceclawSsh"
}
}
/**
* The device's SSH client key and the Terminal app's saved SSH passwords.
*
* The client key is an ECDSA P-256 key generated inside Android Keystore
* (StrongBox when the device has one) and never exportable: only its public
* half leaves the keystore, as an authorized_keys line. Passwords are
* encrypted with an AES-GCM key that also lives in Keystore, and stored in a
* private SharedPreferences file kept apart from the settings store (which is
* exported by the settings port).
*/
object FaceclawSshKeys {
private const val TAG = "FaceclawSsh"
private const val CLIENT_KEY_ALIAS = "faceclaw-ssh-client"
private const val SECRET_KEY_ALIAS = "faceclaw-ssh-secrets"
private const val SECRETS_PREFS = "faceclaw_ssh_secrets"
private const val GCM_TAG_BITS = 128
private fun keyStore(): KeyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
@JvmStatic
@Synchronized
fun hasClientKey(): Boolean = keyStore().containsAlias(CLIENT_KEY_ALIAS)
/** Generate the client key if it doesn't exist yet. */
@JvmStatic
@Synchronized
fun ensureClientKey() {
if (keyStore().containsAlias(CLIENT_KEY_ALIAS)) return
fun spec(strongBox: Boolean): KeyGenParameterSpec {
val builder = KeyGenParameterSpec.Builder(CLIENT_KEY_ALIAS, KeyProperties.PURPOSE_SIGN)
.setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
.setDigests(KeyProperties.DIGEST_SHA256)
if (strongBox && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) builder.setIsStrongBoxBacked(true)
return builder.build()
}
val generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore")
try {
generator.initialize(spec(strongBox = true))
generator.generateKeyPair()
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && e is StrongBoxUnavailableException) {
Log.i(TAG, "StrongBox unavailable; generating the SSH key in the TEE")
} else {
Log.w(TAG, "StrongBox key generation failed; retrying in the TEE", e)
}
generator.initialize(spec(strongBox = false))
generator.generateKeyPair()
}
Log.i(TAG, "generated SSH client key")
}
/** X.509 SubjectPublicKeyInfo DER of the client key, or null if none. */
@JvmStatic
fun publicKey(): ByteArray? = keyStore().getCertificate(CLIENT_KEY_ALIAS)?.publicKey?.encoded
/** The client key's authorized_keys line (generating the key if needed). */
@JvmStatic
fun authorizedKey(comment: String): String {
ensureClientKey()
return Faceclawssh.authorizedKey(publicKey(), comment)
}
/** The client key's SHA256 fingerprint (generating the key if needed). */
@JvmStatic
fun fingerprint(): String {
ensureClientKey()
return Faceclawssh.fingerprint(publicKey())
}
/** A Go Signer over the client key, generating the key if needed. */
@JvmStatic
fun signer(): Signer? = try {
ensureClientKey()
val publicKey = publicKey() ?: throw IllegalStateException("client key has no certificate")
object : Signer {
override fun publicKey(): ByteArray = publicKey
override fun sign(data: ByteArray?): ByteArray {
val privateKey = keyStore().getKey(CLIENT_KEY_ALIAS, null) as PrivateKey
return Signature.getInstance("SHA256withECDSA").run {
initSign(privateKey)
update(data ?: ByteArray(0))
sign()
}
}
}
} catch (t: Throwable) {
Log.w(TAG, "SSH client key unavailable", t)
null
}
private fun secretKey(): SecretKey {
val store = keyStore()
(store.getKey(SECRET_KEY_ALIAS, null) as? SecretKey)?.let { return it }
val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
generator.init(
KeyGenParameterSpec.Builder(SECRET_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.build()
)
return generator.generateKey()
}
private fun prefs(context: Context) =
context.applicationContext.getSharedPreferences(SECRETS_PREFS, Context.MODE_PRIVATE)
@JvmStatic
@Synchronized
fun setSecret(context: Context, id: String, value: String) {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, secretKey())
val sealed = cipher.iv + cipher.doFinal(value.toByteArray(Charsets.UTF_8))
prefs(context).edit().putString(id, Base64.encodeToString(sealed, Base64.NO_WRAP)).apply()
}
/** The saved secret, or "" if none (or it can no longer be decrypted). */
@JvmStatic
@Synchronized
fun getSecret(context: Context, id: String): String {
val encoded = prefs(context).getString(id, null) ?: return ""
return try {
val sealed = Base64.decode(encoded, Base64.NO_WRAP)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
val ivLength = 12
cipher.init(Cipher.DECRYPT_MODE, secretKey(), GCMParameterSpec(GCM_TAG_BITS, sealed, 0, ivLength))
String(cipher.doFinal(sealed, ivLength, sealed.size - ivLength), Charsets.UTF_8)
} catch (t: Throwable) {
Log.w(TAG, "saved SSH secret unreadable; dropping it", t)
prefs(context).edit().remove(id).apply()
""
}
}
@JvmStatic
@Synchronized
fun removeSecret(context: Context, id: String) {
prefs(context).edit().remove(id).apply()
}
/** Put text on the phone's clipboard (from any thread). */
@JvmStatic
fun copyToClipboard(context: Context, label: String, text: String) {
val appContext = context.applicationContext
Handler(Looper.getMainLooper()).post {
val clipboard = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newPlainText(label, text))
}
}
}
@@ -105,6 +105,21 @@ class FaceclawVoiceController(context: Context) {
}
}
override fun micArrayLink(): MicArrayLink? {
val currentCommunicator = communicator ?: return null
return object : MicArrayLink {
override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean =
currentCommunicator.startG2AudioForwarding(listener)
override fun stopForwarding() = currentCommunicator.stopG2AudioForwarding()
override fun isForwarding(): Boolean = currentCommunicator.isAudioCaptureActive()
override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean =
currentCommunicator.sendFaceclawMicControl(record, label, rightTemple, leftTemple)
}
}
override fun createPacketDecoder(): AudioPacketDecoder =
if (communicator?.isRayNeoIo() == true) AndroidSpeechEngines.OpusDecoderAdapter()
else AndroidSpeechEngines.Lc3DecoderAdapter()
@@ -131,6 +146,11 @@ class FaceclawVoiceController(context: Context) {
private val session = VoiceCaptureSession(host)
// The recording of this controller's live captures (see startRecording). It outlives any one
// capture, and is only handed to the session while recording is enabled.
private var recording: MicRecordingWriter? = null
private var recordingEnabled = true
fun setListener(listener: FaceclawVoiceControllerListener?) {
session.setListener(listener)
}
@@ -203,6 +223,88 @@ class FaceclawVoiceController(context: Context) {
session.setBeamFilter(enabled, centerDeg, halfWidthDeg)
}
/**
* Capture glasses sessions from the temples' own microphones (the custom firmware's
* mic_control stream) instead of the stock mixed stream. [configureRecord] is the CONFIGURE
* record to arm both temples with; null goes back to the mixed stream. Must be set before
* {@link #start}.
*/
fun setMicArray(configureRecord: ByteArray?) {
session.setMicArray(configureRecord)
}
/**
* Have the next {@link #start} play the recording at [path] (a .mka file) from [startMs] in
* place of any microphone; null goes back to the microphones. Progress arrives through the
* listener's onReplay.
*/
fun setReplay(path: String?, startMs: Long) {
session.setReplay(path, startMs)
}
/** Continue the running replay from [positionMs] into its recording. */
fun seekReplay(positionMs: Long) {
session.seekReplay(positionMs)
}
fun setReplayPaused(paused: Boolean) {
session.setReplayPaused(paused)
}
/**
* Record live captures to a new Matroska audio file at [path], from now until
* {@link #stopRecording}, across however many captures run in between: every source's audio
* as it arrives, before any processing. [configJson] describes the capture and is stored in
* the file. Returns false when the file cannot be created.
*/
@Synchronized
fun startRecording(path: String, configJson: String?): Boolean {
stopRecording()
return try {
val parent = File(path).parentFile
if (parent != null && !parent.exists()) parent.mkdirs()
recording = MicRecordingWriter(openFileOutput(path), configJson, System.currentTimeMillis())
session.setRecorder(if (recordingEnabled) recording else null)
true
} catch (t: Throwable) {
Log.w(TAG, "could not create recording $path", t)
recording = null
false
}
}
/**
* Whether captures are written to the recording right now: off while the capture running
* isn't one that is to be recorded.
*/
@Synchronized
fun setRecordingEnabled(enabled: Boolean) {
recordingEnabled = enabled
session.setRecorder(if (enabled) recording else null)
}
/** Whether a recording is open and still being written (false once writing to it failed). */
@Synchronized
fun isRecording(): Boolean = recording?.failed == false
/**
* Note a change in what is being captured (a JSON object) at the recording's current
* position; the audio after it is placed afresh on the recording's timeline.
*/
@Synchronized
fun noteRecordingEvent(json: String) {
recording?.event(json)
}
/** Complete the recording's file. Returns its length in ms, or -1 when none was open. */
@Synchronized
fun stopRecording(): Long {
val current = recording ?: return -1
recording = null
session.setRecorder(null)
return current.finish()
}
fun start(requestedMode: String?) {
session.start(requestedMode, 0)
}