diff --git a/.gitignore b/.gitignore index 9331ae0a..977b7b65 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ platforms/ # Release APKs from scripts/release.sh dist/ +# CPU captures and source maps pulled from the installed app +profiles/ + # Logs logs *.log diff --git a/App_Resources/Android/app.gradle b/App_Resources/Android/app.gradle index 832823ad..65d42a3c 100644 --- a/App_Resources/Android/app.gradle +++ b/App_Resources/Android/app.gradle @@ -296,7 +296,7 @@ def removeLegacyKotlinBridgeSource = tasks.register("removeLegacyKotlinBridgeSou // Explicit, reviewed source moves: never infer deletions from class names. def migrations = new groovy.json.JsonSlurper().parse(file("$rootDir/../../native/kotlin/migrated-java-sources.json")) migrations.each { migration -> - if (!file("$rootDir/../../native/kotlin/${migration.kotlin}").isFile()) { + if (!file("$rootDir/../../${migration.kotlin}").isFile()) { throw new GradleException("Missing Kotlin replacement: ${migration.kotlin}") } delete file("$projectDir/src/main/java/${migration.java}") diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java deleted file mode 100644 index 72757990..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.faceclaw.app; - -import android.media.audiofx.*; -import java.util.ArrayList; - -/** Android AudioRecord effect attachment; PCM processing lives in shared Kotlin. */ -public final class AndroidNoiseEffects { - /** Whether this Android build ships the built-in capture noise suppressor. */ - public static boolean platformSuppressorAvailable() { - try { - return NoiseSuppressor.isAvailable(); - } catch (Throwable t) { - return false; - } - } - - /** Short engine name for the UI. */ - public static String engineDescription() { - return "spectral (phone DSP)"; - } - - /** - * Attach the platform's built-in capture-effect chain (NoiseSuppressor, - * AcousticEchoCanceler, AutomaticGainControl — whichever this Android - * build provides) to a phone AudioRecord session. Only applicable when the - * phone itself captures the audio; the BLE glasses stream must go through - * {@link #process(byte[])} instead. The caller keeps the returned effects - * for the life of the recording and release()s them with it. - */ - public static AudioEffect[] attachPlatformEffects(int audioSessionId) { - ArrayList effects = new ArrayList<>(); - try { - if (NoiseSuppressor.isAvailable()) { - NoiseSuppressor ns = NoiseSuppressor.create(audioSessionId); - if (ns != null) { - ns.setEnabled(true); - effects.add(ns); - } - } - if (AcousticEchoCanceler.isAvailable()) { - AcousticEchoCanceler aec = AcousticEchoCanceler.create(audioSessionId); - if (aec != null) { - aec.setEnabled(true); - effects.add(aec); - } - } - if (AutomaticGainControl.isAvailable()) { - AutomaticGainControl agc = AutomaticGainControl.create(audioSessionId); - if (agc != null) { - agc.setEnabled(true); - effects.add(agc); - } - } - } catch (Throwable t) { - // A missing effect just means this tier is unavailable. - } - return effects.toArray(new AudioEffect[0]); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.kt new file mode 100644 index 00000000..a8db0cb5 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.kt @@ -0,0 +1,67 @@ +package com.faceclaw.app + +import android.media.audiofx.AcousticEchoCanceler +import android.media.audiofx.AudioEffect +import android.media.audiofx.AutomaticGainControl +import android.media.audiofx.NoiseSuppressor +import java.util.ArrayList + +/** Android AudioRecord effect attachment; PCM processing lives in shared Kotlin. */ +class AndroidNoiseEffects { + companion object { + /** Whether this Android build ships the built-in capture noise suppressor. */ + @JvmStatic + fun platformSuppressorAvailable(): Boolean { + return try { + NoiseSuppressor.isAvailable() + } catch (t: Throwable) { + false + } + } + + /** Short engine name for the UI. */ + @JvmStatic + fun engineDescription(): String { + return "spectral (phone DSP)" + } + + /** + * Attach the platform's built-in capture-effect chain (NoiseSuppressor, + * AcousticEchoCanceler, AutomaticGainControl — whichever this Android + * build provides) to a phone AudioRecord session. Only applicable when the + * phone itself captures the audio; the BLE glasses stream must go through + * {@link #process(byte[])} instead. The caller keeps the returned effects + * for the life of the recording and release()s them with it. + */ + @JvmStatic + fun attachPlatformEffects(audioSessionId: Int): Array { + val effects = ArrayList() + try { + if (NoiseSuppressor.isAvailable()) { + val ns = NoiseSuppressor.create(audioSessionId) + if (ns != null) { + ns.setEnabled(true) + effects.add(ns) + } + } + if (AcousticEchoCanceler.isAvailable()) { + val aec = AcousticEchoCanceler.create(audioSessionId) + if (aec != null) { + aec.setEnabled(true) + effects.add(aec) + } + } + if (AutomaticGainControl.isAvailable()) { + val agc = AutomaticGainControl.create(audioSessionId) + if (agc != null) { + agc.setEnabled(true) + effects.add(agc) + } + } + } catch (t: Throwable) { + // A missing effect just means this tier is unavailable. + } + return effects.toTypedArray() + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSessionLink.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSessionLink.kt new file mode 100644 index 00000000..8f7cb4cb --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSessionLink.kt @@ -0,0 +1,41 @@ +package com.faceclaw.app + +import android.bluetooth.BluetoothGatt + +/** [SessionLink] over the Android GATT boundary ([FaceclawBleManager]). */ +class AndroidSessionLink(private val bleManager: FaceclawBleManager) : SessionLink { + override fun connect(address: String, timeoutMs: Int): Boolean = bleManager.connect(address, timeoutMs) + + override fun requestHighPriority(address: String) { + bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH) + } + + override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean = + bleManager.requestMtu(address, mtu, timeoutMs) + + override fun negotiatedMtu(address: String): Int = bleManager.getNegotiatedMtu(address) + + override fun discoverServices(address: String, timeoutMs: Int): Boolean = + bleManager.discoverServices(address, timeoutMs) + + override fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean = + bleManager.enableNotifications(address, characteristicUuid, enable, timeoutMs) + + override fun writeFrames( + address: String, + characteristicUuid: String, + frames: List, + mode: GattWriteMode, + timeoutMs: Int, + ): Boolean = bleManager.writeFrames(address, characteristicUuid, frames, AndroidProtocolPlatform.writeType(mode), timeoutMs) + + override fun disconnect(address: String) = bleManager.disconnect(address) + + override fun close() = bleManager.close() + + override fun isBonded(address: String): Boolean = bleManager.isBonded(address) + + override fun prepareBenchmarkLink(address: String, mode: Int) = bleManager.prepareBenchmarkLink(address, mode) + + override fun recordDisplayFrameSent() = FaceclawBleManager.recordDisplayFrameSent() +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSpeechEngines.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSpeechEngines.kt new file mode 100644 index 00000000..2b149f09 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSpeechEngines.kt @@ -0,0 +1,147 @@ +package com.faceclaw.app + +import android.util.Log + +import com.k2fsa.sherpa.onnx.FeatureConfig +import com.k2fsa.sherpa.onnx.OfflineModelConfig +import com.k2fsa.sherpa.onnx.OfflineMoonshineModelConfig +import com.k2fsa.sherpa.onnx.OfflineRecognizer +import com.k2fsa.sherpa.onnx.OfflineRecognizerConfig +import com.k2fsa.sherpa.onnx.OfflineRecognizerResult +import com.k2fsa.sherpa.onnx.OfflineStream +import com.k2fsa.sherpa.onnx.OfflineWhisperModelConfig + +import java.io.File + +/** + * Android implementations of the shared voice ports: the sherpa-onnx offline recognizer as an + * [OfflineTranscriber], the phone microphone as a [PcmAudioSource], and the JNI LC3 decoder as + * an [AudioPacketDecoder]. The pipelines themselves live in the shared VoiceCaptureSession and + * CaptionSession. + */ +internal object AndroidSpeechEngines { + private const val TAG = "FaceclawVoice" + private const val SAMPLE_RATE = 16000 + private const val FEATURE_DIM = 80 + + /** Sherpa-onnx configuration for the on-device model files in [modelDir]. */ + fun recognizerConfig(modelDir: File, kind: VoiceModelKind): OfflineRecognizerConfig { + val modelConfig = OfflineModelConfig.builder() + .setNumThreads(1) + if (kind == VoiceModelKind.WHISPER) { + modelConfig + .setWhisper(OfflineWhisperModelConfig.builder() + .setEncoder(File(modelDir, "base.en-encoder.int8.onnx").absolutePath) + .setDecoder(File(modelDir, "base.en-decoder.int8.onnx").absolutePath) + .setLanguage("en") + .setTask("transcribe") + .build()) + .setTokens(File(modelDir, "base.en-tokens.txt").absolutePath) + } else { + modelConfig + .setMoonshine(OfflineMoonshineModelConfig.builder() + .setEncoder(File(modelDir, "encoder_model.ort").absolutePath) + .setMergedDecoder(File(modelDir, "decoder_model_merged.ort").absolutePath) + .build()) + .setTokens(File(modelDir, "tokens.txt").absolutePath) + } + return OfflineRecognizerConfig.builder() + .setFeatureConfig(FeatureConfig.builder() + .setSampleRate(SAMPLE_RATE) + .setFeatureDim(FEATURE_DIM) + .build()) + .setModelConfig(modelConfig.build()) + .build() + } + + class SherpaTranscriber(config: OfflineRecognizerConfig) : OfflineTranscriber { + private var recognizer: OfflineRecognizer? = OfflineRecognizer(config) + + override fun recognize(samples: FloatArray, count: Int): String { + val currentRecognizer = recognizer ?: return "" + val segment = if (count == samples.size) samples else samples.copyOf(count) + val offlineStream: OfflineStream = currentRecognizer.createStream() + try { + offlineStream.acceptWaveform(segment, SAMPLE_RATE) + currentRecognizer.decode(offlineStream) + val result: OfflineRecognizerResult? = currentRecognizer.getResult(offlineStream) + val raw: String? = if (result == null) "" else result.text + return raw?.trim() ?: "" + } finally { + offlineStream.release() + } + } + + override fun release() { + recognizer?.release() + recognizer = null + } + } + + /** The JNI LC3 decoder behind the shared packet-decoder port. */ + class Lc3DecoderAdapter : AudioPacketDecoder { + private val decoder = FaceclawLc3Decoder() + + override fun decodePacket(packet: ByteArray?, pcmOut: ShortArray?): Int = decoder.decodePacket(packet, pcmOut) + + override val lastAngleDegrees: Int get() = decoder.getLastAngleDegrees() + override val lastSsr: Int get() = decoder.getLastSsr() + override val realPackets: Long get() = decoder.getRealPackets() + override val duplicatePackets: Long get() = decoder.getDuplicatePackets() + override val missingPackets: Long get() = decoder.getMissingPackets() + override val decodeErrors: Long get() = decoder.getDecodeErrors() + + override fun close() = decoder.close() + } + + /** + * Open the phone's own microphone at the pipeline's native format (16 kHz + * mono PCM16), or null when it cannot start — the permission is missing + * (SecurityException) or the device refuses the configuration. + */ + fun openPhoneMic(chunkSamples: Int): PcmAudioSource? { + var record: android.media.AudioRecord? = null + try { + val minBytes = android.media.AudioRecord.getMinBufferSize( + SAMPLE_RATE, + android.media.AudioFormat.CHANNEL_IN_MONO, + android.media.AudioFormat.ENCODING_PCM_16BIT) + val bufferBytes = Math.max(minBytes, chunkSamples * 2 * 4) + record = android.media.AudioRecord( + android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION, + SAMPLE_RATE, + android.media.AudioFormat.CHANNEL_IN_MONO, + android.media.AudioFormat.ENCODING_PCM_16BIT, + bufferBytes) + if (record.state != android.media.AudioRecord.STATE_INITIALIZED) { + record.release() + return null + } + record.startRecording() + if (record.recordingState != android.media.AudioRecord.RECORDSTATE_RECORDING) { + record.release() + return null + } + return AudioRecordSource(record) + } catch (t: Throwable) { + Log.w(TAG, "phone mic open failed", t) + if (record != null) { + record.release() + } + return null + } + } + + private class AudioRecordSource(private val record: android.media.AudioRecord) : PcmAudioSource { + override fun read(pcm: ShortArray): Int = record.read(pcm, 0, pcm.size) + + override fun close() { + try { + record.stop() + } catch (ignored: Throwable) { + // Already stopped or never recording; release below either way. + } + record.release() + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidStockLink.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidStockLink.kt new file mode 100644 index 00000000..3e7d264e --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidStockLink.kt @@ -0,0 +1,64 @@ +package com.faceclaw.app + +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothGatt +import android.content.Context + +/** + * Android [StockLink]: a 1:1 wrapper over FaceclawBleManager for the stock-firmware flows + * (device-info probe, flash prompt, OTA flasher). Owns its own GATT manager, exactly as each + * flow did before the shared cores existed. + */ +class AndroidStockLink(context: Context) : StockLink, FaceclawBleListener { + private val bleManager = FaceclawBleManager(context.applicationContext) + + @Volatile + private var listener: StockLinkListener? = null + + init { + bleManager.setListener(this) + } + + override fun setListener(listener: StockLinkListener?) { + this.listener = listener + } + + override fun connect(address: String, timeoutMs: Int): Boolean = bleManager.connect(address, timeoutMs) + + override fun prepareLink(address: String, desiredMtu: Int, timeoutMs: Int) { + bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH) + bleManager.requestMtu(address, desiredMtu, timeoutMs) + } + + override fun discoverServices(address: String, timeoutMs: Int): Boolean = bleManager.discoverServices(address, timeoutMs) + + override fun enableNotifications(address: String, characteristicUuid: String, timeoutMs: Int): Boolean = + bleManager.enableNotifications(address, characteristicUuid, true, timeoutMs) + + override fun writeFrames(address: String, characteristicUuid: String, frames: List, mode: GattWriteMode, timeoutMs: Int): Boolean = + bleManager.writeFrames(address, characteristicUuid, frames, AndroidProtocolPlatform.writeType(mode), timeoutMs) + + override fun isConnected(address: String): Boolean = bleManager.isConnected(address) + + override fun bondState(address: String): BondState = + when (bleManager.getBondState(address)) { + BluetoothDevice.BOND_NONE -> BondState.NONE + BluetoothDevice.BOND_BONDING -> BondState.BONDING + BluetoothDevice.BOND_BONDED -> BondState.BONDED + else -> BondState.UNKNOWN + } + + override fun disconnect(address: String) = bleManager.disconnect(address) + + override fun close() = bleManager.close() + + override fun onNotification(address: String?, characteristicUuid: String?, data: ByteArray?) { + if (address == null || characteristicUuid == null || data == null) return + listener?.onNotification(address, characteristicUuid, data) + } + + override fun onConnectionStateChange(address: String?, connected: Boolean) { + if (address == null) return + listener?.onConnectionStateChange(address, connected) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.java deleted file mode 100644 index d490ed5d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.java +++ /dev/null @@ -1,181 +0,0 @@ -package com.faceclaw.app; - -import android.app.Activity; -import android.app.KeyguardManager; -import android.content.Context; -import android.content.Intent; -import android.graphics.Color; -import android.graphics.Typeface; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.util.TypedValue; -import android.view.Gravity; -import android.view.View; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.LinearLayout; -import android.widget.TextView; - -import java.text.DateFormat; -import java.util.Date; -import java.util.List; - -/** - * The phone's ringing screen: shown over the lock screen by the alarm - * notification's full-screen intent (or by tapping the notification). Pure - * Java, so it works with the JavaScript side asleep or gone. Snooze and - * Dismiss go to the service, which journals them for the engine. - */ -public class FaceclawAlarmActivity extends Activity { - private final Handler handler = new Handler(Looper.getMainLooper()); - private TextView titleView; - private TextView textView; - private TextView timeView; - private Button snoozeButton; - private long itemId; - - private final Runnable poll = new Runnable() { - @Override - public void run() { - List items = FaceclawAlarmService.snapshot(); - if (items.isEmpty()) { - finish(); - return; - } - FaceclawAlarmService.Ringing shown = null; - for (FaceclawAlarmService.Ringing item : items) { - if (item.id == itemId) { - shown = item; - } - } - if (shown == null) { - shown = items.get(0); - itemId = shown.id; - } - render(shown, items.size()); - handler.postDelayed(this, 1000); - } - }; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - showOverLockScreen(); - itemId = getIntent() == null ? 0 : getIntent().getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - setContentView(buildLayout()); - } - - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - setIntent(intent); - itemId = intent == null ? 0 : intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - } - - @Override - protected void onResume() { - super.onResume(); - handler.post(poll); - } - - @Override - protected void onPause() { - handler.removeCallbacks(poll); - super.onPause(); - } - - private void showOverLockScreen() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { - setShowWhenLocked(true); - setTurnScreenOn(true); - KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); - if (keyguard != null) { - keyguard.requestDismissKeyguard(this, null); - } - } else { - getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED - | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON - | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); - } - getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - - private View buildLayout() { - LinearLayout root = new LinearLayout(this); - root.setOrientation(LinearLayout.VERTICAL); - root.setGravity(Gravity.CENTER); - root.setBackgroundColor(Color.BLACK); - int pad = dp(32); - root.setPadding(pad, pad, pad, pad); - - timeView = new TextView(this); - timeView.setTextColor(Color.WHITE); - timeView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 64); - timeView.setTypeface(Typeface.DEFAULT_BOLD); - timeView.setGravity(Gravity.CENTER); - root.addView(timeView, wrap()); - - titleView = new TextView(this); - titleView.setTextColor(Color.WHITE); - titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28); - titleView.setGravity(Gravity.CENTER); - titleView.setPadding(0, dp(16), 0, 0); - root.addView(titleView, wrap()); - - textView = new TextView(this); - textView.setTextColor(Color.LTGRAY); - textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); - textView.setGravity(Gravity.CENTER); - textView.setPadding(0, dp(8), 0, dp(40)); - root.addView(textView, wrap()); - - snoozeButton = new Button(this); - snoozeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); - snoozeButton.setOnClickListener(view -> act(FaceclawAlarmService.ACTION_SNOOZE)); - root.addView(snoozeButton, button()); - - Button dismissButton = new Button(this); - dismissButton.setText("Dismiss"); - dismissButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); - dismissButton.setOnClickListener(view -> act(FaceclawAlarmService.ACTION_DISMISS)); - root.addView(dismissButton, button()); - return root; - } - - private void render(FaceclawAlarmService.Ringing item, int count) { - timeView.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date())); - titleView.setText(count > 1 ? item.title + " (+" + (count - 1) + " more)" : item.title); - String status = item.silenced ? "Not answered" : item.escalated ? "" : "Ringing on the glasses"; - textView.setText(item.text.isEmpty() ? status : status.isEmpty() ? item.text : item.text + "\n" + status); - snoozeButton.setText(FaceclawAlarms.KIND_TIMER.equals(item.kind) - ? "+" + item.snoozeMinutes + " min" - : "Snooze " + item.snoozeMinutes + " min"); - } - - /** Apply the action to every ringing item (one tap silences the phone). */ - private void act(String action) { - for (FaceclawAlarmService.Ringing item : FaceclawAlarmService.snapshot()) { - Intent intent = new Intent(this, FaceclawAlarmService.class) - .setAction(action) - .putExtra(FaceclawAlarms.EXTRA_ID, item.id); - startService(intent); - } - finish(); - } - - private LinearLayout.LayoutParams wrap() { - return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); - } - - private LinearLayout.LayoutParams button() { - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp(64)); - params.topMargin = dp(12); - return params; - } - - private int dp(int value) { - return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics())); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.kt new file mode 100644 index 00000000..3aab1c35 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.kt @@ -0,0 +1,179 @@ +package com.faceclaw.app + +import android.app.Activity +import android.app.KeyguardManager +import android.content.Context +import android.content.Intent +import android.graphics.Color +import android.graphics.Typeface +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.util.TypedValue +import android.view.Gravity +import android.view.View +import android.view.WindowManager +import android.widget.Button +import android.widget.LinearLayout +import android.widget.TextView + +import java.text.DateFormat +import java.util.Date + +/** + * The phone's ringing screen: shown over the lock screen by the alarm + * notification's full-screen intent (or by tapping the notification). Pure + * Java, so it works with the JavaScript side asleep or gone. Snooze and + * Dismiss go to the service, which journals them for the engine. + */ +class FaceclawAlarmActivity : Activity() { + private val handler = Handler(Looper.getMainLooper()) + private var titleView: TextView? = null + private var textView: TextView? = null + private var timeView: TextView? = null + private var snoozeButton: Button? = null + private var itemId: Long = 0 + + private val poll: Runnable = object : Runnable { + override fun run() { + val items = FaceclawAlarmService.snapshot() + if (items.isEmpty()) { + finish() + return + } + var shown: FaceclawAlarmService.Ringing? = null + for (item in items) { + if (item.id == itemId) { + shown = item + } + } + if (shown == null) { + shown = items[0] + itemId = shown.id + } + render(shown, items.size) + handler.postDelayed(this, 1000) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + showOverLockScreen() + itemId = if (intent == null) 0 else intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + setContentView(buildLayout()) + } + + override fun onNewIntent(intent: Intent?) { + super.onNewIntent(intent) + setIntent(intent) + itemId = if (intent == null) 0 else intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + } + + override fun onResume() { + super.onResume() + handler.post(poll) + } + + override fun onPause() { + handler.removeCallbacks(poll) + super.onPause() + } + + private fun showOverLockScreen() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + setShowWhenLocked(true) + setTurnScreenOn(true) + val keyguard = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager? + keyguard?.requestDismissKeyguard(this, null) + } else { + @Suppress("DEPRECATION") + window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON + or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD) + } + window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } + + private fun buildLayout(): View { + val root = LinearLayout(this) + root.orientation = LinearLayout.VERTICAL + root.gravity = Gravity.CENTER + root.setBackgroundColor(Color.BLACK) + val pad = dp(32) + root.setPadding(pad, pad, pad, pad) + + val time = TextView(this) + time.setTextColor(Color.WHITE) + time.setTextSize(TypedValue.COMPLEX_UNIT_SP, 64f) + time.typeface = Typeface.DEFAULT_BOLD + time.gravity = Gravity.CENTER + root.addView(time, wrap()) + timeView = time + + val title = TextView(this) + title.setTextColor(Color.WHITE) + title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28f) + title.gravity = Gravity.CENTER + title.setPadding(0, dp(16), 0, 0) + root.addView(title, wrap()) + titleView = title + + val text = TextView(this) + text.setTextColor(Color.LTGRAY) + text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f) + text.gravity = Gravity.CENTER + text.setPadding(0, dp(8), 0, dp(40)) + root.addView(text, wrap()) + textView = text + + val snooze = Button(this) + snooze.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) + snooze.setOnClickListener { act(FaceclawAlarmService.ACTION_SNOOZE) } + root.addView(snooze, button()) + snoozeButton = snooze + + val dismissButton = Button(this) + dismissButton.text = "Dismiss" + dismissButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) + dismissButton.setOnClickListener { act(FaceclawAlarmService.ACTION_DISMISS) } + root.addView(dismissButton, button()) + return root + } + + private fun render(item: FaceclawAlarmService.Ringing, count: Int) { + timeView!!.text = DateFormat.getTimeInstance(DateFormat.SHORT).format(Date()) + titleView!!.text = if (count > 1) item.title + " (+" + (count - 1) + " more)" else item.title + val status = if (item.silenced) "Not answered" else if (item.escalated) "" else "Ringing on the glasses" + textView!!.text = if (item.text.isEmpty()) status else if (status.isEmpty()) item.text else item.text + "\n" + status + snoozeButton!!.text = if (FaceclawAlarms.KIND_TIMER == item.kind) + "+" + item.snoozeMinutes + " min" + else + "Snooze " + item.snoozeMinutes + " min" + } + + /** Apply the action to every ringing item (one tap silences the phone). */ + private fun act(action: String) { + for (item in FaceclawAlarmService.snapshot()) { + val intent = Intent(this, FaceclawAlarmService::class.java) + .setAction(action) + .putExtra(FaceclawAlarms.EXTRA_ID, item.id) + startService(intent) + } + finish() + } + + private fun wrap(): LinearLayout.LayoutParams { + return LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) + } + + private fun button(): LinearLayout.LayoutParams { + val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp(64)) + params.topMargin = dp(12) + return params + } + + private fun dp(value: Int): Int { + return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value.toFloat(), resources.displayMetrics)) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.java deleted file mode 100644 index b8d44514..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.faceclaw.app; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; - -/** The AlarmManager target: an item came due, start the ringing service. */ -public class FaceclawAlarmReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - if (intent == null || !FaceclawAlarms.ACTION_EXPIRE.equals(intent.getAction())) { - return; - } - long id = intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - if (id == 0) { - return; - } - FaceclawAlarmService.ring( - context.getApplicationContext(), - id, - intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE), - intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), - intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), - intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) - ); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.kt new file mode 100644 index 00000000..c884bbf1 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.kt @@ -0,0 +1,26 @@ +package com.faceclaw.app + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +/** The AlarmManager target: an item came due, start the ringing service. */ +class FaceclawAlarmReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + if (intent == null || FaceclawAlarms.ACTION_EXPIRE != intent.action) { + return + } + val id = intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + if (id == 0L) { + return + } + FaceclawAlarmService.ring( + context.applicationContext, + id, + intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE), + intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), + intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), + intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) + ) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java deleted file mode 100644 index 4d7ed1b9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.faceclaw.app; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; - -/** - * Re-arms the persisted alarm schedule when the system would otherwise have - * dropped it: after a reboot (AlarmManager alarms do not survive one), a - * package update, or a wall-clock / time-zone change. Runs without the - * JavaScript side; only the device-protected schedule file is needed. - */ -public class FaceclawAlarmRescheduleReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent == null ? null : intent.getAction(); - if (action == null) { - return; - } - switch (action) { - case Intent.ACTION_BOOT_COMPLETED: - case Intent.ACTION_LOCKED_BOOT_COMPLETED: - case Intent.ACTION_MY_PACKAGE_REPLACED: - case Intent.ACTION_TIME_CHANGED: - case Intent.ACTION_TIMEZONE_CHANGED: - case "android.intent.action.QUICKBOOT_POWERON": - case "com.htc.intent.action.QUICKBOOT_POWERON": - FaceclawAlarms.rescheduleAll(context, action); - break; - default: - break; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.kt new file mode 100644 index 00000000..d591704a --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.kt @@ -0,0 +1,28 @@ +package com.faceclaw.app + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +/** + * Re-arms the persisted alarm schedule when the system would otherwise have + * dropped it: after a reboot (AlarmManager alarms do not survive one), a + * package update, or a wall-clock / time-zone change. Runs without the + * JavaScript side; only the device-protected schedule file is needed. + */ +class FaceclawAlarmRescheduleReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + val action = intent?.action ?: return + when (action) { + Intent.ACTION_BOOT_COMPLETED, + Intent.ACTION_LOCKED_BOOT_COMPLETED, + Intent.ACTION_MY_PACKAGE_REPLACED, + Intent.ACTION_TIME_CHANGED, + Intent.ACTION_TIMEZONE_CHANGED, + "android.intent.action.QUICKBOOT_POWERON", + "com.htc.intent.action.QUICKBOOT_POWERON" -> + FaceclawAlarms.rescheduleAll(context, action) + else -> {} + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.java deleted file mode 100644 index a9cac015..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.java +++ /dev/null @@ -1,649 +0,0 @@ -package com.faceclaw.app; - -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ServiceInfo; -import android.media.AudioAttributes; -import android.media.AudioFocusRequest; -import android.media.AudioManager; -import android.media.MediaPlayer; -import android.media.RingtoneManager; -import android.net.Uri; -import android.os.Build; -import android.os.Handler; -import android.os.IBinder; -import android.os.Looper; -import android.os.PowerManager; -import android.os.VibrationEffect; -import android.os.Vibrator; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -/** - * The ringing foreground service. One instance carries every item currently - * ringing. Each item starts silent on the phone (a heads-up / lock-screen - * notification only, the glasses do the ringing) and escalates to phone - * sound and vibration when the glasses cannot carry it: not connected, not - * on a head, charging, no delivery confirmation from the JS side within a - * few seconds, or no acknowledgement within 30 seconds of delivery. Sound - * uses the alarm stream, so media volume, the ringer switch and (unless set - * to total silence) Do Not Disturb do not silence it. - */ -public class FaceclawAlarmService extends Service { - public static final String CHANNEL_ID = "faceclaw-alarms"; - static final String ACTION_RING = "com.faceclaw.app.action.ALARM_RING"; - static final String ACTION_DELIVERED = "com.faceclaw.app.action.ALARM_DELIVERED"; - static final String ACTION_DISMISS = "com.faceclaw.app.action.ALARM_DISMISS"; - static final String ACTION_SNOOZE = "com.faceclaw.app.action.ALARM_SNOOZE"; - /** Stop an item quietly (acknowledged on the glasses / cancelled by the engine). */ - static final String ACTION_STOP_ITEM = "com.faceclaw.app.action.ALARM_STOP_ITEM"; - - /** How long to wait for the JS side to confirm the glasses are showing the item. */ - static final long DELIVERY_WAIT_MS = 5_000L; - /** How long after delivery to the glasses before the phone joins in. */ - static final long ACK_WAIT_MS = 30_000L; - /** Sound and vibration stop after this; the notification stays. */ - static final long AUTO_SILENCE_MS = 10 * 60_000L; - /** Alarm-stream level (fraction of max) used when the wearer has it muted. */ - static final float MUTED_VOLUME_FRACTION = 0.6f; - - static final class Ringing { - final long id; - final String title; - final String text; - final String kind; - final int snoozeMinutes; - final long ringAtMs; - long deliveredAtMs; - boolean escalated; - boolean silenced; - - Ringing(long id, String title, String text, String kind, int snoozeMinutes) { - this.id = id; - this.title = title == null || title.trim().isEmpty() ? "Alarm" : title; - this.text = text == null ? "" : text; - this.kind = kind == null ? FaceclawAlarms.KIND_ALARM : kind; - this.snoozeMinutes = Math.max(1, snoozeMinutes); - this.ringAtMs = System.currentTimeMillis(); - } - } - - /** Ringing items, oldest first; static so the activity can read them. */ - private static final Map ringing = Collections.synchronizedMap(new LinkedHashMap<>()); - /** - * Signals that arrived before the ring intent was processed (the JS - * engine fires, launches its window and reports delivery on the same - * main thread the service's onStartCommand queues behind), keyed by id - * with their arrival time. Consumed by startRinging. - */ - private static final Map deliveredEarly = Collections.synchronizedMap(new LinkedHashMap<>()); - private static final Map stoppedEarly = Collections.synchronizedMap(new LinkedHashMap<>()); - private static final long EARLY_SIGNAL_MAX_AGE_MS = 15_000L; - - private final Handler handler = new Handler(Looper.getMainLooper()); - private MediaPlayer player; - private Vibrator vibrator; - private PowerManager.WakeLock wakeLock; - private AudioManager audioManager; - private AudioFocusRequest focusRequest; - private int restoreAlarmVolume = -1; - private long foregroundId; - - // ------------------------------------------------------------------ - // Static entry points - - public static void ring(Context context, long id, String title, String text, String kind, int snoozeMinutes) { - Context appContext = context.getApplicationContext(); - Intent intent = new Intent(appContext, FaceclawAlarmService.class) - .setAction(ACTION_RING) - .putExtra(FaceclawAlarms.EXTRA_ID, id) - .putExtra(FaceclawAlarms.EXTRA_TITLE, title) - .putExtra(FaceclawAlarms.EXTRA_TEXT, text) - .putExtra(FaceclawAlarms.EXTRA_KIND, kind) - .putExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, snoozeMinutes); - FaceclawAlarms.startServiceCompat(appContext, intent); - } - - public static void delivered(Context context, long id) { - if (!ringing.containsKey(id)) { - deliveredEarly.put(id, System.currentTimeMillis()); - return; - } - Context appContext = context.getApplicationContext(); - appContext.startService(new Intent(appContext, FaceclawAlarmService.class) - .setAction(ACTION_DELIVERED) - .putExtra(FaceclawAlarms.EXTRA_ID, id)); - } - - public static void stopItem(Context context, long id) { - if (!ringing.containsKey(id)) { - stoppedEarly.put(id, System.currentTimeMillis()); - return; - } - Context appContext = context.getApplicationContext(); - appContext.startService(new Intent(appContext, FaceclawAlarmService.class) - .setAction(ACTION_STOP_ITEM) - .putExtra(FaceclawAlarms.EXTRA_ID, id)); - } - - public static boolean isRinging(long id) { - return ringing.containsKey(id); - } - - public static boolean isAnythingRinging() { - return !ringing.isEmpty(); - } - - static List snapshot() { - synchronized (ringing) { - return new ArrayList<>(ringing.values()); - } - } - - static PendingIntent actionIntent(Context appContext, String action, long id) { - Intent intent = new Intent(appContext, FaceclawAlarmService.class) - .setAction(action) - .putExtra(FaceclawAlarms.EXTRA_ID, id); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - int code = FaceclawAlarms.requestCode(id) ^ action.hashCode(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - return PendingIntent.getForegroundService(appContext, code, intent, flags); - } - return PendingIntent.getService(appContext, code, intent, flags); - } - - // ------------------------------------------------------------------ - // Service lifecycle - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent == null ? null : intent.getAction(); - long id = intent == null ? 0 : intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - if (action == null) { - // Restarted by the system with nothing to do. - finishIfIdle(); - return START_NOT_STICKY; - } - switch (action) { - case ACTION_RING: - startRinging(new Ringing( - id, - intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE), - intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), - intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), - intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) - )); - break; - case ACTION_DELIVERED: - markDelivered(id); - break; - case ACTION_DISMISS: - dismiss(id); - break; - case ACTION_SNOOZE: - snooze(id); - break; - case ACTION_STOP_ITEM: - stopItemQuietly(id, "acknowledged"); - break; - default: - break; - } - finishIfIdle(); - return START_NOT_STICKY; - } - - @Override - public void onDestroy() { - handler.removeCallbacksAndMessages(null); - stopSound(); - releaseWakeLock(); - super.onDestroy(); - } - - // ------------------------------------------------------------------ - // Ringing - - private void startRinging(Ringing item) { - if (item.id == 0) { - return; - } - ensureChannel(); - Ringing existing = ringing.get(item.id); - if (existing != null) { - // The engine's JS timeout and the AlarmManager alarm both fired: one ring. - showNotification(existing); - return; - } - if (takeEarlySignal(stoppedEarly, item.id)) { - // Acknowledged or cancelled before this intent was processed. - FaceclawAlarms.log(this, "ring " + item.id + " already acknowledged"); - return; - } - ringing.put(item.id, item); - FaceclawAlarms.log(this, "ring " + item.kind + " " + item.id + " (" + FaceclawAlarms.glassesStatusDescription() + ")"); - acquireWakeLock(); - showNotification(item); - if (!FaceclawAlarms.glassesCanCarryAlarm()) { - escalate(item, FaceclawAlarms.glassesStatusDescription()); - return; - } - if (takeEarlySignal(deliveredEarly, item.id)) { - markDelivered(item.id); - return; - } - // The glasses could carry it: give the JS side a moment to confirm - // they are actually showing it, then hold for the acknowledgement. - handler.postDelayed(() -> { - Ringing current = ringing.get(item.id); - if (current != null && !current.escalated && current.deliveredAtMs == 0) { - escalate(current, "not delivered to the glasses"); - } - }, DELIVERY_WAIT_MS); - } - - /** Consume an early signal for the id; stale entries are dropped. */ - private static boolean takeEarlySignal(Map signals, long id) { - Long at = signals.remove(id); - long now = System.currentTimeMillis(); - synchronized (signals) { - signals.values().removeIf(stamp -> now - stamp > EARLY_SIGNAL_MAX_AGE_MS); - } - return at != null && now - at <= EARLY_SIGNAL_MAX_AGE_MS; - } - - private void markDelivered(long id) { - Ringing item = ringing.get(id); - if (item == null || item.deliveredAtMs != 0) { - return; - } - item.deliveredAtMs = System.currentTimeMillis(); - FaceclawAlarms.log(this, "delivered " + id + " to glasses"); - handler.postDelayed(() -> { - Ringing current = ringing.get(id); - if (current != null && !current.escalated) { - escalate(current, "not acknowledged on the glasses"); - } - }, ACK_WAIT_MS); - } - - private void escalate(Ringing item, String reason) { - if (item.escalated) { - return; - } - item.escalated = true; - FaceclawAlarms.log(this, "phone sound for " + item.id + ": " + reason); - showNotification(item); - startSound(); - handler.postDelayed(() -> { - Ringing current = ringing.get(item.id); - if (current != null && current.escalated && !current.silenced) { - current.silenced = true; - FaceclawAlarms.log(this, "auto-silenced " + current.id); - showNotification(current); - syncSound(); - } - }, AUTO_SILENCE_MS); - } - - private void dismiss(long id) { - Ringing item = ringing.get(id); - if (item == null) { - return; - } - FaceclawAlarms.recordPhoneAction(this, id, "dismiss", 0); - removeItem(item); - // Also drop the schedule entry (a one-off is over; the engine re-arms repeats). - FaceclawAlarms.cancel(this, id); - } - - private void snooze(long id) { - Ringing item = ringing.get(id); - if (item == null) { - return; - } - int minutes = item.snoozeMinutes; - FaceclawAlarms.recordPhoneAction(this, id, "snooze", minutes); - removeItem(item); - // Re-arm on the phone directly so the snooze holds even with the JS - // side gone; the engine replays the journal and lands on the same id. - FaceclawAlarms.schedule( - this, - id, - System.currentTimeMillis() + minutes * 60_000L, - item.title, - item.text, - item.kind, - minutes - ); - } - - private void stopItemQuietly(long id, String reason) { - Ringing item = ringing.get(id); - if (item == null) { - return; - } - FaceclawAlarms.log(this, reason + " " + id); - removeItem(item); - } - - private void removeItem(Ringing item) { - ringing.remove(item.id); - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.cancel(notificationId(item.id)); - } - syncSound(); - if (item.id == foregroundId) { - promoteAnotherToForeground(); - } - } - - private void finishIfIdle() { - if (!ringing.isEmpty()) { - return; - } - handler.removeCallbacksAndMessages(null); - stopSound(); - releaseWakeLock(); - stopForeground(STOP_FOREGROUND_REMOVE); - stopSelf(); - } - - // ------------------------------------------------------------------ - // Notification (the phone's silent display of the alarm) - - private void showNotification(Ringing item) { - Notification notification = buildNotification(item); - if (foregroundId == 0 || foregroundId == item.id || !ringing.containsKey(foregroundId)) { - foregroundId = item.id; - startForegroundCompat(notificationId(item.id), notification); - } else { - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.notify(notificationId(item.id), notification); - } - } - } - - private void promoteAnotherToForeground() { - List items = snapshot(); - if (items.isEmpty()) { - foregroundId = 0; - return; - } - Ringing next = items.get(0); - foregroundId = next.id; - startForegroundCompat(notificationId(next.id), buildNotification(next)); - } - - private void startForegroundCompat(int id, Notification notification) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground(id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); - } else { - startForeground(id, notification); - } - } - - private Notification buildNotification(Ringing item) { - Intent activity = new Intent(this, FaceclawAlarmActivity.class) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP) - .putExtra(FaceclawAlarms.EXTRA_ID, item.id); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - PendingIntent open = PendingIntent.getActivity(this, FaceclawAlarms.requestCode(item.id), activity, flags); - - String status; - if (item.silenced) { - status = "Not answered"; - } else if (item.escalated) { - status = "Ringing"; - } else { - status = "Ringing on the glasses"; - } - String text = item.text.isEmpty() ? status : item.text + " · " + status; - - Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - ? new Notification.Builder(this, CHANNEL_ID) - : new Notification.Builder(this).setPriority(Notification.PRIORITY_MAX); - builder.setContentTitle(item.title) - .setContentText(text) - .setSmallIcon(getApplicationInfo().icon) - .setContentIntent(open) - .setFullScreenIntent(open, true) - .setCategory(Notification.CATEGORY_ALARM) - .setVisibility(Notification.VISIBILITY_PUBLIC) - .setOngoing(true) - .setAutoCancel(false) - .setOnlyAlertOnce(true) - .setWhen(item.ringAtMs) - .setShowWhen(true) - .addAction(new Notification.Action.Builder( - null, - FaceclawAlarms.KIND_TIMER.equals(item.kind) ? "+" + item.snoozeMinutes + " min" : "Snooze " + item.snoozeMinutes + " min", - actionIntent(this, ACTION_SNOOZE, item.id)).build()) - .addAction(new Notification.Action.Builder( - null, - "Dismiss", - actionIntent(this, ACTION_DISMISS, item.id)).build()); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - // Pre-channel: the builder controls sound, and we want none. - builder.setSound(null).setVibrate(null); - } - return builder.build(); - } - - private void ensureChannel() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - return; - } - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager == null) { - return; - } - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH); - channel.setDescription("Timers and alarms going off. Silent here: the sound is played by the alarm itself."); - // The service plays the sound itself (on the alarm stream) so the - // notification stays silent and vibration-free on purpose. - channel.setSound(null, null); - channel.enableVibration(false); - channel.setBypassDnd(true); - channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - manager.createNotificationChannel(channel); - } - - private static int notificationId(long id) { - return 0x41000000 | (FaceclawAlarms.requestCode(id) & 0x00ffffff); - } - - // ------------------------------------------------------------------ - // Sound and vibration (only while some item is escalated and not yet silenced) - - private void syncSound() { - boolean wanted = false; - for (Ringing item : snapshot()) { - if (item.escalated && !item.silenced) { - wanted = true; - break; - } - } - if (wanted) { - startSound(); - } else { - stopSound(); - } - } - - private void startSound() { - if (player != null) { - return; - } - audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); - AudioAttributes attributes = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ALARM) - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .build(); - raiseMutedAlarmVolume(); - requestAudioFocus(attributes); - Uri tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); - if (tone == null) { - tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); - } - try { - MediaPlayer created = new MediaPlayer(); - created.setAudioAttributes(attributes); - created.setDataSource(this, tone); - created.setLooping(true); - created.prepare(); - created.start(); - player = created; - } catch (Exception error) { - FaceclawAlarms.log(this, "alarm sound failed: " + error.getMessage()); - player = null; - } - startVibration(); - } - - private void stopSound() { - if (player != null) { - try { - player.stop(); - } catch (RuntimeException ignored) { - // Already stopped. - } - player.release(); - player = null; - } - if (vibrator != null) { - vibrator.cancel(); - vibrator = null; - } - abandonAudioFocus(); - restoreAlarmVolume(); - } - - /** - * A muted alarm stream is the one thing that would keep an alarm clock - * silent; raise it for the duration of the ring and put it back after. - */ - private void raiseMutedAlarmVolume() { - if (audioManager == null) { - return; - } - try { - int current = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); - int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM); - if (current == 0 && max > 0) { - restoreAlarmVolume = current; - audioManager.setStreamVolume(AudioManager.STREAM_ALARM, Math.max(1, Math.round(max * MUTED_VOLUME_FRACTION)), 0); - } - } catch (RuntimeException error) { - FaceclawAlarms.log(this, "alarm volume change failed: " + error.getMessage()); - } - } - - private void restoreAlarmVolume() { - if (audioManager == null || restoreAlarmVolume < 0) { - return; - } - try { - audioManager.setStreamVolume(AudioManager.STREAM_ALARM, restoreAlarmVolume, 0); - } catch (RuntimeException ignored) { - // Best effort. - } - restoreAlarmVolume = -1; - } - - private void requestAudioFocus(AudioAttributes attributes) { - if (audioManager == null) { - return; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) - .setAudioAttributes(attributes) - .build(); - audioManager.requestAudioFocus(focusRequest); - } else { - audioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); - } - } - - private void abandonAudioFocus() { - if (audioManager == null) { - return; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - if (focusRequest != null) { - audioManager.abandonAudioFocusRequest(focusRequest); - focusRequest = null; - } - } else { - audioManager.abandonAudioFocus(null); - } - } - - private void startVibration() { - Vibrator device = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); - if (device == null || !device.hasVibrator()) { - return; - } - vibrator = device; - long[] pattern = {0, 600, 400, 600, 1200}; - AudioAttributes attributes = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ALARM) - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .build(); - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - device.vibrate(VibrationEffect.createWaveform(pattern, 0), attributes); - } else { - device.vibrate(pattern, 0, attributes); - } - } catch (RuntimeException error) { - FaceclawAlarms.log(this, "vibration failed: " + error.getMessage()); - } - } - - // ------------------------------------------------------------------ - // Wake lock - - private void acquireWakeLock() { - if (wakeLock != null && wakeLock.isHeld()) { - return; - } - PowerManager power = (PowerManager) getSystemService(Context.POWER_SERVICE); - if (power == null) { - return; - } - wakeLock = power.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "faceclaw:alarm"); - // Bounded: sound auto-silences at AUTO_SILENCE_MS, and a stuck lock - // past that would only drain the battery. - wakeLock.acquire(AUTO_SILENCE_MS + ACK_WAIT_MS + DELIVERY_WAIT_MS + 10_000L); - } - - private void releaseWakeLock() { - if (wakeLock != null && wakeLock.isHeld()) { - wakeLock.release(); - } - wakeLock = null; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.kt new file mode 100644 index 00000000..3bddef2b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.kt @@ -0,0 +1,532 @@ +package com.faceclaw.app + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.content.pm.ServiceInfo +import android.media.AudioAttributes +import android.media.AudioFocusRequest +import android.media.AudioManager +import android.media.MediaPlayer +import android.media.RingtoneManager +import android.net.Uri +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Looper +import android.os.PowerManager +import android.os.VibrationEffect +import android.os.Vibrator + + +/** + * The ringing foreground service. One instance carries every item currently + * ringing. Each item starts silent on the phone (a heads-up / lock-screen + * notification only, the glasses do the ringing) and escalates to phone + * sound and vibration when the glasses cannot carry it: not connected, not + * on a head, charging, no delivery confirmation from the JS side within a + * few seconds, or no acknowledgement within 30 seconds of delivery. Sound + * uses the alarm stream, so media volume, the ringer switch and (unless set + * to total silence) Do Not Disturb do not silence it. + */ +class FaceclawAlarmService : Service() { + /** The shared item model; kept as a nested subclass so the lock-screen activity's references hold. */ + internal class Ringing( + id: Long, + title: String?, + text: String?, + kind: String?, + snoozeMinutes: Int + ) : RingingItem(id, title, text, kind, snoozeMinutes, System.currentTimeMillis()) + + companion object { + const val CHANNEL_ID = "faceclaw-alarms" + internal const val ACTION_RING = "com.faceclaw.app.action.ALARM_RING" + internal const val ACTION_DELIVERED = "com.faceclaw.app.action.ALARM_DELIVERED" + internal const val ACTION_DISMISS = "com.faceclaw.app.action.ALARM_DISMISS" + internal const val ACTION_SNOOZE = "com.faceclaw.app.action.ALARM_SNOOZE" + /** Stop an item quietly (acknowledged on the glasses / cancelled by the engine). */ + internal const val ACTION_STOP_ITEM = "com.faceclaw.app.action.ALARM_STOP_ITEM" + + /** How long to wait for the JS side to confirm the glasses are showing the item. */ + internal const val DELIVERY_WAIT_MS = 5_000L + /** How long after delivery to the glasses before the phone joins in. */ + internal const val ACK_WAIT_MS = 30_000L + /** Sound and vibration stop after this; the notification stays. */ + internal const val AUTO_SILENCE_MS = 10 * 60_000L + /** Alarm-stream level (fraction of max) used when the wearer has it muted. */ + internal const val MUTED_VOLUME_FRACTION = 0.6f + + /** + * The escalation policy is process-wide (the activity reads it, the static entry points + * consult it before the service exists); the live service attaches its timers/effects. + */ + private val policy = AlarmRingingPolicy(FaceclawAlarms.glassesGate) + + // ------------------------------------------------------------------ + // Static entry points + + @JvmStatic + fun ring(context: Context, id: Long, title: String?, text: String?, kind: String?, snoozeMinutes: Int) { + val appContext = context.applicationContext + val intent = Intent(appContext, FaceclawAlarmService::class.java) + .setAction(ACTION_RING) + .putExtra(FaceclawAlarms.EXTRA_ID, id) + .putExtra(FaceclawAlarms.EXTRA_TITLE, title) + .putExtra(FaceclawAlarms.EXTRA_TEXT, text) + .putExtra(FaceclawAlarms.EXTRA_KIND, kind) + .putExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, snoozeMinutes) + FaceclawAlarms.startServiceCompat(appContext, intent) + } + + @JvmStatic + fun delivered(context: Context, id: Long) { + if (!policy.deliveredOrDefer(id)) { + return + } + val appContext = context.applicationContext + appContext.startService(Intent(appContext, FaceclawAlarmService::class.java) + .setAction(ACTION_DELIVERED) + .putExtra(FaceclawAlarms.EXTRA_ID, id)) + } + + @JvmStatic + fun stopItem(context: Context, id: Long) { + if (!policy.stopOrDefer(id)) { + return + } + val appContext = context.applicationContext + appContext.startService(Intent(appContext, FaceclawAlarmService::class.java) + .setAction(ACTION_STOP_ITEM) + .putExtra(FaceclawAlarms.EXTRA_ID, id)) + } + + @JvmStatic + fun isRinging(id: Long): Boolean = policy.isRinging(id) + + @JvmStatic + fun isAnythingRinging(): Boolean = policy.isAnythingRinging() + + @JvmStatic + internal fun snapshot(): List = policy.snapshot().map { it as Ringing } + + @JvmStatic + internal fun actionIntent(appContext: Context, action: String, id: Long): PendingIntent { + val intent = Intent(appContext, FaceclawAlarmService::class.java) + .setAction(action) + .putExtra(FaceclawAlarms.EXTRA_ID, id) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + val code = FaceclawAlarms.requestCode(id) xor action.hashCode() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return PendingIntent.getForegroundService(appContext, code, intent, flags) + } + return PendingIntent.getService(appContext, code, intent, flags) + } + + private fun notificationId(id: Long): Int { + return 0x41000000 or (FaceclawAlarms.requestCode(id) and 0x00ffffff) + } + } + + private val handler = Handler(Looper.getMainLooper()) + private var player: MediaPlayer? = null + private var vibrator: Vibrator? = null + private var wakeLock: PowerManager.WakeLock? = null + private var audioManager: AudioManager? = null + private var focusRequest: AudioFocusRequest? = null + private var restoreAlarmVolume = -1 + private var foregroundId: Long = 0 + + // ------------------------------------------------------------------ + // Service lifecycle + + override fun onBind(intent: Intent?): IBinder? { + return null + } + + override fun onCreate() { + super.onCreate() + policy.attach(timers, effects) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + val action = intent?.action + val id = if (intent == null) 0L else intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + if (action == null) { + // Restarted by the system with nothing to do. + finishIfIdle() + return START_NOT_STICKY + } + when (action) { + ACTION_RING -> + startRinging(Ringing( + id, + intent!!.getStringExtra(FaceclawAlarms.EXTRA_TITLE), + intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), + intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), + intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) + )) + ACTION_DELIVERED -> + policy.markDelivered(id) + ACTION_DISMISS -> + policy.dismiss(id) + ACTION_SNOOZE -> + policy.snooze(id) + ACTION_STOP_ITEM -> + policy.stopQuietly(id, "acknowledged") + else -> {} + } + finishIfIdle() + return START_NOT_STICKY + } + + override fun onDestroy() { + policy.detach() + handler.removeCallbacksAndMessages(null) + stopSound() + releaseWakeLock() + super.onDestroy() + } + + // ------------------------------------------------------------------ + // Ringing + + private fun startRinging(item: Ringing) { + if (item.id == 0L) { + return + } + ensureChannel() + policy.ring(item) + } + + /** Escalation timers on the service's main-thread Handler. */ + private val timers = object : AlarmScheduler { + private val pending = HashMap() + + override fun postDelayed(token: Any, delayMs: Long, action: () -> Unit) { + pending.remove(token)?.let { handler.removeCallbacks(it) } + val runnable = Runnable { + pending.remove(token) + action() + } + pending[token] = runnable + handler.postDelayed(runnable, delayMs) + } + + override fun cancel(token: Any) { + pending.remove(token)?.let { handler.removeCallbacks(it) } + } + + override fun cancelAll() { + pending.clear() + handler.removeCallbacksAndMessages(null) + } + } + + /** The phone-side effects of the shared policy: notifications, sound, schedule, journal. */ + private val effects = object : RingingSink { + override fun showNotification(item: RingingItem) = this@FaceclawAlarmService.showNotification(item as Ringing) + + override fun itemRemoved(item: RingingItem) { + val manager = getSystemService(NotificationManager::class.java) + manager?.cancel(notificationId(item.id)) + if (item.id == foregroundId) { + promoteAnotherToForeground() + } + } + + override fun ringingStarted() = acquireWakeLock() + + override fun startSound() = this@FaceclawAlarmService.startSound() + + override fun stopSound() = this@FaceclawAlarmService.stopSound() + + override fun reschedule(item: RingingItem, atMs: Long, minutes: Int) { + // The engine replays the journal and lands on the same id. + FaceclawAlarms.schedule(this@FaceclawAlarmService, item.id, atMs, item.title, item.text, item.kind, minutes) + } + + override fun cancelSchedule(id: Long) = FaceclawAlarms.cancel(this@FaceclawAlarmService, id) + + override fun recordPhoneAction(id: Long, action: String, minutes: Int) = + FaceclawAlarms.recordPhoneAction(this@FaceclawAlarmService, id, action, minutes) + + override fun log(line: String) = FaceclawAlarms.log(this@FaceclawAlarmService, line) + } + + private fun finishIfIdle() { + if (policy.isAnythingRinging()) { + return + } + policy.cancelTimers() + stopSound() + releaseWakeLock() + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + } + + // ------------------------------------------------------------------ + // Notification (the phone's silent display of the alarm) + + private fun showNotification(item: Ringing) { + val notification = buildNotification(item) + if (foregroundId == 0L || foregroundId == item.id || !policy.isRinging(foregroundId)) { + foregroundId = item.id + startForegroundCompat(notificationId(item.id), notification) + } else { + val manager = getSystemService(NotificationManager::class.java) + manager?.notify(notificationId(item.id), notification) + } + } + + private fun promoteAnotherToForeground() { + val items = snapshot() + if (items.isEmpty()) { + foregroundId = 0 + return + } + val next = items[0] + foregroundId = next.id + startForegroundCompat(notificationId(next.id), buildNotification(next)) + } + + private fun startForegroundCompat(id: Int, notification: Notification) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK) + } else { + startForeground(id, notification) + } + } + + private fun buildNotification(item: Ringing): Notification { + val activity = Intent(this, FaceclawAlarmActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) + .putExtra(FaceclawAlarms.EXTRA_ID, item.id) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + val open = PendingIntent.getActivity(this, FaceclawAlarms.requestCode(item.id), activity, flags) + + val status: String = if (item.silenced) { + "Not answered" + } else if (item.escalated) { + "Ringing" + } else { + "Ringing on the glasses" + } + val text = if (item.text.isEmpty()) status else item.text + " · " + status + + val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + Notification.Builder(this, CHANNEL_ID) + else + Notification.Builder(this).setPriority(Notification.PRIORITY_MAX) + builder.setContentTitle(item.title) + .setContentText(text) + .setSmallIcon(applicationInfo.icon) + .setContentIntent(open) + .setFullScreenIntent(open, true) + .setCategory(Notification.CATEGORY_ALARM) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setOngoing(true) + .setAutoCancel(false) + .setOnlyAlertOnce(true) + .setWhen(item.ringAtMs) + .setShowWhen(true) + .addAction(Notification.Action.Builder( + null, + if (FaceclawAlarms.KIND_TIMER == item.kind) "+" + item.snoozeMinutes + " min" else "Snooze " + item.snoozeMinutes + " min", + actionIntent(this, ACTION_SNOOZE, item.id)).build()) + .addAction(Notification.Action.Builder( + null, + "Dismiss", + actionIntent(this, ACTION_DISMISS, item.id)).build()) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + // Pre-channel: the builder controls sound, and we want none. + builder.setSound(null).setVibrate(null) + } + return builder.build() + } + + private fun ensureChannel() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return + } + val manager = getSystemService(NotificationManager::class.java) ?: return + val channel = NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH) + channel.description = "Timers and alarms going off. Silent here: the sound is played by the alarm itself." + // The service plays the sound itself (on the alarm stream) so the + // notification stays silent and vibration-free on purpose. + channel.setSound(null, null) + channel.enableVibration(false) + channel.setBypassDnd(true) + channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + manager.createNotificationChannel(channel) + } + + // ------------------------------------------------------------------ + // Sound and vibration (only while some item is escalated and not yet silenced) + + private fun startSound() { + if (player != null) { + return + } + audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager? + val attributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .build() + raiseMutedAlarmVolume() + requestAudioFocus(attributes) + var tone: Uri? = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM) + if (tone == null) { + tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE) + } + try { + val created = MediaPlayer() + created.setAudioAttributes(attributes) + created.setDataSource(this, tone!!) + created.isLooping = true + created.prepare() + created.start() + player = created + } catch (error: Exception) { + FaceclawAlarms.log(this, "alarm sound failed: " + error.message) + player = null + } + startVibration() + } + + private fun stopSound() { + val current = player + if (current != null) { + try { + current.stop() + } catch (ignored: RuntimeException) { + // Already stopped. + } + current.release() + player = null + } + val device = vibrator + if (device != null) { + device.cancel() + vibrator = null + } + abandonAudioFocus() + restoreAlarmVolume() + } + + /** + * A muted alarm stream is the one thing that would keep an alarm clock + * silent; raise it for the duration of the ring and put it back after. + */ + private fun raiseMutedAlarmVolume() { + val manager = audioManager ?: return + try { + val current = manager.getStreamVolume(AudioManager.STREAM_ALARM) + val max = manager.getStreamMaxVolume(AudioManager.STREAM_ALARM) + if (current == 0 && max > 0) { + restoreAlarmVolume = current + manager.setStreamVolume(AudioManager.STREAM_ALARM, Math.max(1, Math.round(max * MUTED_VOLUME_FRACTION)), 0) + } + } catch (error: RuntimeException) { + FaceclawAlarms.log(this, "alarm volume change failed: " + error.message) + } + } + + private fun restoreAlarmVolume() { + val manager = audioManager + if (manager == null || restoreAlarmVolume < 0) { + return + } + try { + manager.setStreamVolume(AudioManager.STREAM_ALARM, restoreAlarmVolume, 0) + } catch (ignored: RuntimeException) { + // Best effort. + } + restoreAlarmVolume = -1 + } + + private fun requestAudioFocus(attributes: AudioAttributes) { + val manager = audioManager ?: return + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val request = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) + .setAudioAttributes(attributes) + .build() + focusRequest = request + manager.requestAudioFocus(request) + } else { + @Suppress("DEPRECATION") + manager.requestAudioFocus(null, AudioManager.STREAM_ALARM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) + } + } + + private fun abandonAudioFocus() { + val manager = audioManager ?: return + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val request = focusRequest + if (request != null) { + manager.abandonAudioFocusRequest(request) + focusRequest = null + } + } else { + @Suppress("DEPRECATION") + manager.abandonAudioFocus(null) + } + } + + private fun startVibration() { + val device = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? + if (device == null || !device.hasVibrator()) { + return + } + vibrator = device + val pattern = longArrayOf(0, 600, 400, 600, 1200) + val attributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .build() + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + device.vibrate(VibrationEffect.createWaveform(pattern, 0), attributes) + } else { + @Suppress("DEPRECATION") + device.vibrate(pattern, 0, attributes) + } + } catch (error: RuntimeException) { + FaceclawAlarms.log(this, "vibration failed: " + error.message) + } + } + + // ------------------------------------------------------------------ + // Wake lock + + private fun acquireWakeLock() { + val held = wakeLock + if (held != null && held.isHeld) { + return + } + val power = getSystemService(Context.POWER_SERVICE) as PowerManager? ?: return + val lock = power.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "faceclaw:alarm") + wakeLock = lock + // Bounded: sound auto-silences at AUTO_SILENCE_MS, and a stuck lock + // past that would only drain the battery. + lock.acquire(AUTO_SILENCE_MS + ACK_WAIT_MS + DELIVERY_WAIT_MS + 10_000L) + } + + private fun releaseWakeLock() { + val lock = wakeLock + if (lock != null && lock.isHeld) { + lock.release() + } + wakeLock = null + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.java deleted file mode 100644 index 3dd2b0f1..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.java +++ /dev/null @@ -1,620 +0,0 @@ -package com.faceclaw.app; - -import android.app.ActivityManager; -import android.app.AlarmManager; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.media.AudioManager; -import android.net.Uri; -import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.os.PowerManager; -import android.provider.Settings; - -import androidx.core.content.ContextCompat; - -import com.tns.NativeScriptActivity; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Locale; - -/** - * The phone side of the Timers app's alarms and countdown timers, built to - * ring without the JavaScript side: scheduling through AlarmManager's alarm - * clock API, a persisted schedule (in device-protected storage) that the - * reschedule receiver replays after reboots and time changes, the hand-off - * to the ringing foreground service, and a journal of what the wearer did - * on the phone (dismiss / snooze) for the JS engine to replay when it is - * next awake. Also the reliability self-check the UI surfaces. - * - * Ids are the JS engine's item ids (a millisecond epoch times 100 plus a - * serial), shared between timers and alarms; `kind` tells the two apart for - * wording only. - */ -public final class FaceclawAlarms { - public static final String ACTION_EXPIRE = "com.faceclaw.app.action.ALARM_EXPIRE"; - static final String EXTRA_ID = "id"; - static final String EXTRA_TITLE = "title"; - static final String EXTRA_TEXT = "text"; - static final String EXTRA_KIND = "kind"; - static final String EXTRA_SNOOZE_MINUTES = "snoozeMinutes"; - - public static final String KIND_TIMER = "timer"; - public static final String KIND_ALARM = "alarm"; - - private static final String PREFS_NAME = "faceclaw-alarms"; - private static final String KEY_SCHEDULED = "scheduled"; - private static final String KEY_JOURNAL = "journal"; - private static final String KEY_LOG = "log"; - private static final int LOG_LINES = 60; - - /** - * A schedule entry found already past due (after a reboot, or when the - * process was asleep) rings if it is at most this late; older ones are - * dropped as missed. Matches the JS engine's grace window. - */ - static final long LATE_GRACE_MS = 5 * 60_000L; - /** A glasses status report older than this is not trusted (the JS side is probably gone). */ - static final long GLASSES_STATUS_MAX_AGE_MS = 3 * 60_000L; - - private static volatile boolean glassesConnected; - private static volatile boolean glassesWorn; - private static volatile boolean glassesCharging; - private static volatile long glassesStatusAtMs; - - private static volatile FaceclawAlarmListener listener; - private static final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private FaceclawAlarms() {} - - // ------------------------------------------------------------------ - // Storage - - /** - * Device-protected storage, so the schedule is readable before the first - * unlock after a reboot (the reschedule receiver may run then on some - * devices) and survives credential-encrypted storage being unavailable. - */ - static SharedPreferences prefs(Context context) { - Context base = context.getApplicationContext(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - base = base.createDeviceProtectedStorageContext(); - } - return base.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - } - - private static synchronized JSONArray readArray(Context context, String key) { - String raw = prefs(context).getString(key, "[]"); - try { - return new JSONArray(raw); - } catch (JSONException error) { - return new JSONArray(); - } - } - - private static synchronized void writeArray(Context context, String key, JSONArray array) { - prefs(context).edit().putString(key, array.toString()).apply(); - } - - static JSONObject findScheduled(Context context, long id) { - JSONArray scheduled = readArray(context, KEY_SCHEDULED); - for (int index = 0; index < scheduled.length(); index++) { - JSONObject entry = scheduled.optJSONObject(index); - if (entry != null && entry.optLong("id") == id) { - return entry; - } - } - return null; - } - - private static synchronized void putScheduled(Context context, JSONObject entry) { - JSONArray scheduled = readArray(context, KEY_SCHEDULED); - JSONArray next = new JSONArray(); - long id = entry.optLong("id"); - for (int index = 0; index < scheduled.length(); index++) { - JSONObject existing = scheduled.optJSONObject(index); - if (existing != null && existing.optLong("id") != id) { - next.put(existing); - } - } - next.put(entry); - writeArray(context, KEY_SCHEDULED, next); - } - - private static synchronized void removeScheduled(Context context, long id) { - JSONArray scheduled = readArray(context, KEY_SCHEDULED); - JSONArray next = new JSONArray(); - for (int index = 0; index < scheduled.length(); index++) { - JSONObject existing = scheduled.optJSONObject(index); - if (existing != null && existing.optLong("id") != id) { - next.put(existing); - } - } - writeArray(context, KEY_SCHEDULED, next); - } - - // ------------------------------------------------------------------ - // Scheduling - - /** - * Schedule (or move) the phone alarm for an item. Idempotent: the same id - * always maps to the same PendingIntent, so a reschedule replaces rather - * than duplicates. setAlarmClock is exact, fires through Doze and battery - * savers, and shows the system's alarm indicator; the fallbacks only run - * where it is unavailable. - */ - public static void schedule( - Context context, - long id, - long triggerAtMs, - String title, - String text, - String kind, - int snoozeMinutes - ) { - Context appContext = context.getApplicationContext(); - JSONObject entry = new JSONObject(); - try { - entry.put("id", id); - entry.put("at", triggerAtMs); - entry.put("title", title == null ? "" : title); - entry.put("text", text == null ? "" : text); - entry.put("kind", kind == null ? KIND_ALARM : kind); - entry.put("snoozeMinutes", snoozeMinutes); - } catch (JSONException ignored) { - return; - } - putScheduled(appContext, entry); - armAlarm(appContext, entry); - } - - private static void armAlarm(Context appContext, JSONObject entry) { - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager == null) { - return; - } - long id = entry.optLong("id"); - long triggerAt = Math.max(System.currentTimeMillis(), entry.optLong("at")); - PendingIntent fire = expiryPendingIntent(appContext, entry); - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(triggerAt, showIntent(appContext, id)); - manager.setAlarmClock(info, fire); - } else { - manager.setExact(AlarmManager.RTC_WAKEUP, triggerAt, fire); - } - } catch (SecurityException error) { - // Exact-alarm access revoked (Android 12/13 without USE_EXACT_ALARM - // honoured): the inexact alarm is still a wake-up, just a late one. - log(appContext, "exact alarm refused for " + id + ": " + error.getMessage()); - manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, fire); - } - } - - /** Cancel the phone alarm for an item, stop it if ringing, and forget it. */ - public static void cancel(Context context, long id) { - Context appContext = context.getApplicationContext(); - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager != null) { - JSONObject entry = new JSONObject(); - try { - entry.put("id", id); - } catch (JSONException ignored) { - // unreachable for a numeric put - } - manager.cancel(expiryPendingIntent(appContext, entry)); - } - removeScheduled(appContext, id); - FaceclawAlarmService.stopItem(appContext, id); - } - - /** - * Replay the persisted schedule into AlarmManager: after a reboot (alarms - * are wiped), a package update, or a wall-clock / time-zone change. Entries - * already due ring now if inside the grace window, else are dropped. - */ - public static void rescheduleAll(Context context, String reason) { - Context appContext = context.getApplicationContext(); - JSONArray scheduled = readArray(appContext, KEY_SCHEDULED); - long now = System.currentTimeMillis(); - int armed = 0; - for (int index = 0; index < scheduled.length(); index++) { - JSONObject entry = scheduled.optJSONObject(index); - if (entry == null) { - continue; - } - long at = entry.optLong("at"); - if (at <= now) { - if (now - at <= LATE_GRACE_MS) { - ringEntry(appContext, entry); - } else { - log(appContext, "missed " + entry.optString("kind") + " " + entry.optLong("id") + " (" + reason + ")"); - removeScheduled(appContext, entry.optLong("id")); - } - continue; - } - armAlarm(appContext, entry); - armed++; - } - log(appContext, "rescheduled " + armed + " (" + reason + ")"); - } - - /** - * The schedule and AlarmManager can disagree (a force-stop clears alarms - * but not the file, an OEM killer drops them). True when the soonest - * stored entry is not what the system reports as the next alarm clock. - */ - public static boolean scheduleLooksStale(Context context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - return false; - } - Context appContext = context.getApplicationContext(); - JSONArray scheduled = readArray(appContext, KEY_SCHEDULED); - long now = System.currentTimeMillis(); - long soonest = Long.MAX_VALUE; - for (int index = 0; index < scheduled.length(); index++) { - JSONObject entry = scheduled.optJSONObject(index); - if (entry != null && entry.optLong("at") > now) { - soonest = Math.min(soonest, entry.optLong("at")); - } - } - if (soonest == Long.MAX_VALUE) { - return false; - } - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager == null) { - return false; - } - AlarmManager.AlarmClockInfo next = manager.getNextAlarmClock(); - // Another app's alarm may legitimately be sooner; only a later (or - // absent) system alarm proves ours is missing. - return next == null || next.getTriggerTime() > soonest + 1000; - } - - // ------------------------------------------------------------------ - // Ringing - - /** An item came due: hand it to the ringing service (idempotent per id). */ - static void ringEntry(Context appContext, JSONObject entry) { - FaceclawAlarmService.ring( - appContext, - entry.optLong("id"), - entry.optString("title"), - entry.optString("text"), - entry.optString("kind", KIND_ALARM), - entry.optInt("snoozeMinutes", 10) - ); - } - - /** The JS engine's own expiry path; the service deduplicates against the alarm's. */ - public static void ring(Context context, long id, String title, String text, String kind, int snoozeMinutes) { - Context appContext = context.getApplicationContext(); - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager != null) { - JSONObject probe = new JSONObject(); - try { - probe.put("id", id); - } catch (JSONException ignored) { - // unreachable for a numeric put - } - manager.cancel(expiryPendingIntent(appContext, probe)); - } - FaceclawAlarmService.ring(appContext, id, title, text, kind, snoozeMinutes); - } - - /** The glasses are showing the ringing item; the acknowledgement clock starts now. */ - public static void deliveredToGlasses(Context context, long id) { - FaceclawAlarmService.delivered(context.getApplicationContext(), id); - } - - /** The wearer dismissed or snoozed it on the glasses: the phone side goes quiet. */ - public static void acknowledge(Context context, long id) { - Context appContext = context.getApplicationContext(); - removeScheduled(appContext, id); - FaceclawAlarmService.stopItem(appContext, id); - } - - // ------------------------------------------------------------------ - // Glasses status (pushed by the JS side; in-memory, so a dead process reads as "no glasses") - - public static void setGlassesStatus(boolean connected, boolean worn, boolean charging) { - glassesConnected = connected; - glassesWorn = worn; - glassesCharging = charging; - glassesStatusAtMs = System.currentTimeMillis(); - } - - /** True only when a fresh report says the glasses are connected, on a head, and not charging. */ - static boolean glassesCanCarryAlarm() { - long age = System.currentTimeMillis() - glassesStatusAtMs; - if (glassesStatusAtMs == 0 || age > GLASSES_STATUS_MAX_AGE_MS) { - return false; - } - return glassesConnected && glassesWorn && !glassesCharging; - } - - static String glassesStatusDescription() { - if (glassesStatusAtMs == 0) { - return "no glasses status"; - } - long age = System.currentTimeMillis() - glassesStatusAtMs; - if (age > GLASSES_STATUS_MAX_AGE_MS) { - return "glasses status stale"; - } - if (!glassesConnected) { - return "glasses not connected"; - } - if (glassesCharging) { - return "glasses charging"; - } - if (!glassesWorn) { - return "glasses not worn"; - } - return "glasses worn"; - } - - // ------------------------------------------------------------------ - // Phone actions -> JS - - public static void setListener(FaceclawAlarmListener newListener) { - listener = newListener; - } - - /** - * Record a dismiss / snooze done on the phone. Delivered live to the JS - * listener when one is registered, and journaled regardless so a JS side - * that was asleep or gone can catch up on its next boot. - */ - static void recordPhoneAction(Context context, long id, String action, int minutes) { - Context appContext = context.getApplicationContext(); - JSONObject event = new JSONObject(); - try { - event.put("id", id); - event.put("action", action); - event.put("minutes", minutes); - event.put("at", System.currentTimeMillis()); - } catch (JSONException ignored) { - return; - } - synchronized (FaceclawAlarms.class) { - JSONArray journal = readArray(appContext, KEY_JOURNAL); - journal.put(event); - writeArray(appContext, KEY_JOURNAL, journal); - } - log(appContext, "phone " + action + " " + id + (minutes > 0 ? " (" + minutes + " min)" : "")); - final FaceclawAlarmListener current = listener; - if (current != null) { - mainHandler.post(() -> { - try { - current.onPhoneAction(id, action, minutes); - } catch (RuntimeException error) { - log(appContext, "listener failed: " + error.getMessage()); - } - }); - } - } - - /** Hand the journal to the JS side (as a JSON array) and clear it. */ - public static synchronized String drainJournal(Context context) { - Context appContext = context.getApplicationContext(); - JSONArray journal = readArray(appContext, KEY_JOURNAL); - writeArray(appContext, KEY_JOURNAL, new JSONArray()); - return journal.toString(); - } - - // ------------------------------------------------------------------ - // Reliability self-check - - /** - * Conditions under which the phone may fail to ring, as a JSON array of - * {code, message, fixable}, most serious first. Empty when everything is - * in order. The UI shows these before the wearer relies on an alarm. - */ - public static String checkReliability(Context context) { - Context appContext = context.getApplicationContext(); - JSONArray issues = new JSONArray(); - NotificationManager notifications = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE); - AlarmManager alarms = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarms != null && !alarms.canScheduleExactAlarms()) { - addIssue(issues, "exact-alarm", "Exact alarms are not allowed for Faceclaw, so alarms may ring late.", true); - } - if (notifications != null && !notifications.areNotificationsEnabled()) { - addIssue(issues, "notifications", "Notifications are turned off for Faceclaw, so the phone cannot show or sound an alarm.", true); - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notifications != null) { - NotificationChannel channel = notifications.getNotificationChannel(FaceclawAlarmService.CHANNEL_ID); - if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { - addIssue(issues, "alarm-channel", "The Alarms notification category is blocked, so the phone cannot show an alarm.", true); - } - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && notifications != null && !notifications.canUseFullScreenIntent()) { - addIssue(issues, "full-screen", "Full-screen alarms are not allowed, so a ringing alarm will not take over the lock screen.", true); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notifications != null) { - int filter = notifications.getCurrentInterruptionFilter(); - if (filter == NotificationManager.INTERRUPTION_FILTER_NONE) { - addIssue(issues, "dnd-total", "Do Not Disturb is set to total silence, which silences alarms too.", true); - } else if (filter == NotificationManager.INTERRUPTION_FILTER_PRIORITY) { - try { - NotificationManager.Policy policy = notifications.getNotificationPolicy(); - if (policy != null && (policy.priorityCategories & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { - addIssue(issues, "dnd-alarms", "Do Not Disturb does not allow alarms, so the phone will stay silent.", true); - } - } catch (SecurityException ignored) { - // No notification-policy access: cannot inspect the exceptions. - } - } - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE); - if (activityManager != null && activityManager.isBackgroundRestricted()) { - addIssue(issues, "background-restricted", "Background usage is restricted for Faceclaw, which blocks alarms while the app is not open.", true); - } - } - PowerManager power = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); - if (power != null && !power.isIgnoringBatteryOptimizations(appContext.getPackageName())) { - addIssue(issues, "battery-optimized", "Battery optimization is on for Faceclaw; some phones delay alarms because of it.", true); - } - AudioManager audio = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE); - if (audio != null && audio.getStreamVolume(AudioManager.STREAM_ALARM) == 0) { - addIssue(issues, "alarm-volume", "The phone's alarm volume is muted; Faceclaw raises it while ringing, but check it.", true); - } - String maker = Build.MANUFACTURER == null ? "" : Build.MANUFACTURER.toLowerCase(Locale.ROOT); - if (maker.contains("xiaomi") || maker.contains("huawei") || maker.contains("oppo") || maker.contains("vivo") - || maker.contains("oneplus") || maker.contains("realme") || maker.contains("meizu") || maker.contains("asus")) { - addIssue(issues, "oem-killer", "This phone maker is known to stop background apps; allow Faceclaw to auto-start and run unrestricted (see dontkillmyapp.com).", true); - } - if (scheduleLooksStale(appContext)) { - addIssue(issues, "schedule-stale", "The system lost Faceclaw's next alarm (the app may have been force-stopped); it was re-armed.", false); - rescheduleAll(appContext, "self-check"); - } - return issues.toString(); - } - - private static void addIssue(JSONArray issues, String code, String message, boolean fixable) { - JSONObject issue = new JSONObject(); - try { - issue.put("code", code); - issue.put("message", message); - issue.put("fixable", fixable); - } catch (JSONException ignored) { - return; - } - issues.put(issue); - } - - /** Open the system screen where the issue can be fixed. */ - public static void openReliabilityFix(Context context, String code) { - Context appContext = context.getApplicationContext(); - String pkg = appContext.getPackageName(); - Intent intent; - switch (code == null ? "" : code) { - case "exact-alarm": - intent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S - ? new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:" + pkg)) - : appDetails(pkg); - break; - case "notifications": - case "alarm-channel": - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) - .putExtra(Settings.EXTRA_APP_PACKAGE, pkg); - if ("alarm-channel".equals(code)) { - intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) - .putExtra(Settings.EXTRA_APP_PACKAGE, pkg) - .putExtra(Settings.EXTRA_CHANNEL_ID, FaceclawAlarmService.CHANNEL_ID); - } - } else { - intent = appDetails(pkg); - } - break; - case "full-screen": - intent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE - ? new Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT, Uri.parse("package:" + pkg)) - : appDetails(pkg); - break; - case "dnd-total": - case "dnd-alarms": - intent = new Intent(Settings.ACTION_SOUND_SETTINGS); - break; - case "battery-optimized": - intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:" + pkg)); - break; - case "alarm-volume": - intent = new Intent(Settings.ACTION_SOUND_SETTINGS); - break; - default: - intent = appDetails(pkg); - break; - } - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - appContext.startActivity(intent); - } catch (RuntimeException error) { - Intent fallback = appDetails(pkg).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - appContext.startActivity(fallback); - } catch (RuntimeException ignored) { - // No settings activity at all; nothing more to do. - } - } - } - - private static Intent appDetails(String pkg) { - return new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + pkg)); - } - - // ------------------------------------------------------------------ - // Diagnostics log - - static synchronized void log(Context context, String line) { - Context appContext = context.getApplicationContext(); - JSONArray log = readArray(appContext, KEY_LOG); - JSONArray next = new JSONArray(); - int start = Math.max(0, log.length() - (LOG_LINES - 1)); - for (int index = start; index < log.length(); index++) { - next.put(log.optString(index)); - } - next.put(System.currentTimeMillis() + " " + line); - writeArray(appContext, KEY_LOG, next); - } - - /** The ring / miss / action log, newest last, one line per entry. */ - public static String readLog(Context context) { - JSONArray log = readArray(context.getApplicationContext(), KEY_LOG); - StringBuilder out = new StringBuilder(); - for (int index = 0; index < log.length(); index++) { - if (index > 0) { - out.append('\n'); - } - out.append(log.optString(index)); - } - return out.toString(); - } - - // ------------------------------------------------------------------ - // Intents - - private static PendingIntent expiryPendingIntent(Context appContext, JSONObject entry) { - Intent intent = new Intent(appContext, FaceclawAlarmReceiver.class); - intent.setAction(ACTION_EXPIRE); - long id = entry.optLong("id"); - intent.putExtra(EXTRA_ID, id); - intent.putExtra(EXTRA_TITLE, entry.optString("title")); - intent.putExtra(EXTRA_TEXT, entry.optString("text")); - intent.putExtra(EXTRA_KIND, entry.optString("kind", KIND_ALARM)); - intent.putExtra(EXTRA_SNOOZE_MINUTES, entry.optInt("snoozeMinutes", 10)); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - return PendingIntent.getBroadcast(appContext, requestCode(id), intent, flags); - } - - /** What the system's alarm indicator opens: the app. */ - private static PendingIntent showIntent(Context appContext, long id) { - Intent intent = new Intent(appContext, NativeScriptActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - return PendingIntent.getActivity(appContext, requestCode(id), intent, flags); - } - - static int requestCode(long id) { - return (int) (id ^ (id >>> 32)) & 0x7fffffff; - } - - static void startServiceCompat(Context appContext, Intent intent) { - ContextCompat.startForegroundService(appContext, intent); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.kt new file mode 100644 index 00000000..fa6ad2ed --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.kt @@ -0,0 +1,470 @@ +package com.faceclaw.app + +import android.app.ActivityManager +import android.app.AlarmManager +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.media.AudioManager +import android.net.Uri +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.os.PowerManager +import android.provider.Settings + +import androidx.core.content.ContextCompat + +import com.tns.NativeScriptActivity + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +import java.util.Locale + +/** + * The phone side of the Timers app's alarms and countdown timers, built to + * ring without the JavaScript side: scheduling through AlarmManager's alarm + * clock API, a persisted schedule (in device-protected storage) that the + * reschedule receiver replays after reboots and time changes, the hand-off + * to the ringing foreground service, and a journal of what the wearer did + * on the phone (dismiss / snooze) for the JS engine to replay when it is + * next awake. Also the reliability self-check the UI surfaces. + * + * Ids are the JS engine's item ids (a millisecond epoch times 100 plus a + * serial), shared between timers and alarms; `kind` tells the two apart for + * wording only. + */ +class FaceclawAlarms private constructor() { + companion object { + const val ACTION_EXPIRE = "com.faceclaw.app.action.ALARM_EXPIRE" + internal const val EXTRA_ID = "id" + internal const val EXTRA_TITLE = "title" + internal const val EXTRA_TEXT = "text" + internal const val EXTRA_KIND = "kind" + internal const val EXTRA_SNOOZE_MINUTES = "snoozeMinutes" + + const val KIND_TIMER = AlarmSchedule.KIND_TIMER + const val KIND_ALARM = AlarmSchedule.KIND_ALARM + + private const val PREFS_NAME = "faceclaw-alarms" + + /** Grace window for past-due entries found on replay (shared policy). */ + internal const val LATE_GRACE_MS = AlarmSchedule.LATE_GRACE_MS + /** A glasses status report older than this is not trusted (shared policy). */ + internal const val GLASSES_STATUS_MAX_AGE_MS = GlassesStatusGate.MAX_AGE_MS + + /** The last glasses status pushed by the JS side; read by the ringing service. */ + @JvmField + internal val glassesGate = GlassesStatusGate() + + @Volatile + private var listener: FaceclawAlarmListener? = null + private val mainHandler = Handler(Looper.getMainLooper()) + + // ------------------------------------------------------------------ + // Storage + + /** + * Device-protected storage, so the schedule is readable before the first + * unlock after a reboot (the reschedule receiver may run then on some + * devices) and survives credential-encrypted storage being unavailable. + */ + @JvmStatic + internal fun prefs(context: Context): SharedPreferences { + var base = context.applicationContext + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + base = base.createDeviceProtectedStorageContext() + } + return base.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + } + + @Volatile + private var scheduleInstance: AlarmSchedule? = null + + /** The shared schedule/journal/log core over the device-protected prefs (one per process). */ + @JvmStatic + internal fun scheduleCore(context: Context): AlarmSchedule { + scheduleInstance?.let { return it } + synchronized(FaceclawAlarms::class.java) { + scheduleInstance?.let { return it } + val prefs = prefs(context) + val created = AlarmSchedule(object : AlarmStore { + override fun read(key: String): String? = prefs.getString(key, null) + + override fun write(key: String, value: String?) { + prefs.edit().putString(key, value).apply() + } + }) + scheduleInstance = created + return created + } + } + + @JvmStatic + internal fun findScheduled(context: Context, id: Long): AlarmEntry? = scheduleCore(context).find(id) + + // ------------------------------------------------------------------ + // Scheduling + + /** + * Schedule (or move) the phone alarm for an item. Idempotent: the same id + * always maps to the same PendingIntent, so a reschedule replaces rather + * than duplicates. setAlarmClock is exact, fires through Doze and battery + * savers, and shows the system's alarm indicator; the fallbacks only run + * where it is unavailable. + */ + @JvmStatic + fun schedule( + context: Context, + id: Long, + triggerAtMs: Long, + title: String?, + text: String?, + kind: String?, + snoozeMinutes: Int + ) { + val appContext = context.applicationContext + val entry = AlarmEntry(id, triggerAtMs, title, text, kind ?: KIND_ALARM, snoozeMinutes) + scheduleCore(appContext).put(entry) + armAlarm(appContext, entry) + } + + private fun armAlarm(appContext: Context, entry: AlarmEntry) { + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + ?: return + val id = entry.id + val triggerAt = Math.max(System.currentTimeMillis(), entry.at) + val fire = expiryPendingIntent(appContext, entry) + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + val info = AlarmManager.AlarmClockInfo(triggerAt, showIntent(appContext, id)) + manager.setAlarmClock(info, fire) + } else { + manager.setExact(AlarmManager.RTC_WAKEUP, triggerAt, fire) + } + } catch (error: SecurityException) { + // Exact-alarm access revoked (Android 12/13 without USE_EXACT_ALARM + // honoured): the inexact alarm is still a wake-up, just a late one. + log(appContext, "exact alarm refused for " + id + ": " + error.message) + manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, fire) + } + } + + /** Cancel the phone alarm for an item, stop it if ringing, and forget it. */ + @JvmStatic + fun cancel(context: Context, id: Long) { + val appContext = context.applicationContext + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + if (manager != null) { + manager.cancel(expiryPendingIntent(appContext, AlarmEntry(id, 0L, null, null, null, 10))) + } + scheduleCore(appContext).remove(id) + FaceclawAlarmService.stopItem(appContext, id) + } + + /** + * Replay the persisted schedule into AlarmManager: after a reboot (alarms + * are wiped), a package update, or a wall-clock / time-zone change. Entries + * already due ring now if inside the grace window, else are dropped. + */ + @JvmStatic + fun rescheduleAll(context: Context, reason: String?) { + val appContext = context.applicationContext + scheduleCore(appContext).replay(reason, { ringEntry(appContext, it) }, { armAlarm(appContext, it) }) + } + + /** + * The schedule and AlarmManager can disagree (a force-stop clears alarms + * but not the file, an OEM killer drops them). True when the soonest + * stored entry is not what the system reports as the next alarm clock. + */ + @JvmStatic + fun scheduleLooksStale(context: Context): Boolean { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + return false + } + val appContext = context.applicationContext + val core = scheduleCore(appContext) + if (core.soonestPendingAt() == null) { + return false + } + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + ?: return false + return core.looksStale(manager.nextAlarmClock?.triggerTime) + } + + // ------------------------------------------------------------------ + // Ringing + + /** An item came due: hand it to the ringing service (idempotent per id). */ + @JvmStatic + internal fun ringEntry(appContext: Context, entry: AlarmEntry) { + FaceclawAlarmService.ring(appContext, entry.id, entry.title, entry.text, entry.kind, entry.snoozeMinutes) + } + + /** The JS engine's own expiry path; the service deduplicates against the alarm's. */ + @JvmStatic + fun ring(context: Context, id: Long, title: String?, text: String?, kind: String?, snoozeMinutes: Int) { + val appContext = context.applicationContext + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + if (manager != null) { + manager.cancel(expiryPendingIntent(appContext, AlarmEntry(id, 0L, null, null, null, 10))) + } + FaceclawAlarmService.ring(appContext, id, title, text, kind, snoozeMinutes) + } + + /** The glasses are showing the ringing item; the acknowledgement clock starts now. */ + @JvmStatic + fun deliveredToGlasses(context: Context, id: Long) { + FaceclawAlarmService.delivered(context.applicationContext, id) + } + + /** The wearer dismissed or snoozed it on the glasses: the phone side goes quiet. */ + @JvmStatic + fun acknowledge(context: Context, id: Long) { + val appContext = context.applicationContext + scheduleCore(appContext).remove(id) + FaceclawAlarmService.stopItem(appContext, id) + } + + // ------------------------------------------------------------------ + // Glasses status (pushed by the JS side; in-memory, so a dead process reads as "no glasses") + + @JvmStatic + fun setGlassesStatus(connected: Boolean, worn: Boolean, charging: Boolean) { + glassesGate.set(connected, worn, charging) + } + + /** True only when a fresh report says the glasses are connected, on a head, and not charging. */ + @JvmStatic + internal fun glassesCanCarryAlarm(): Boolean = glassesGate.canCarryAlarm() + + @JvmStatic + internal fun glassesStatusDescription(): String = glassesGate.description() + + // ------------------------------------------------------------------ + // Phone actions -> JS + + @JvmStatic + fun setListener(newListener: FaceclawAlarmListener?) { + listener = newListener + } + + /** + * Record a dismiss / snooze done on the phone. Delivered live to the JS + * listener when one is registered, and journaled regardless so a JS side + * that was asleep or gone can catch up on its next boot. + */ + @JvmStatic + internal fun recordPhoneAction(context: Context, id: Long, action: String, minutes: Int) { + val appContext = context.applicationContext + scheduleCore(appContext).appendJournal(id, action, minutes) + val current = listener + if (current != null) { + mainHandler.post { + try { + current.onPhoneAction(id, action, minutes) + } catch (error: RuntimeException) { + log(appContext, "listener failed: " + error.message) + } + } + } + } + + /** Hand the journal to the JS side (as a JSON array) and clear it. */ + @JvmStatic + fun drainJournal(context: Context): String = scheduleCore(context.applicationContext).drainJournal() + + // ------------------------------------------------------------------ + // Reliability self-check + + /** + * Conditions under which the phone may fail to ring, as a JSON array of + * {code, message, fixable}, most serious first. Empty when everything is + * in order. The UI shows these before the wearer relies on an alarm. + */ + @JvmStatic + fun checkReliability(context: Context): String { + val appContext = context.applicationContext + val issues = JSONArray() + val notifications = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager? + val alarms = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarms != null && !alarms.canScheduleExactAlarms()) { + addIssue(issues, "exact-alarm", "Exact alarms are not allowed for Faceclaw, so alarms may ring late.", true) + } + if (notifications != null && !notifications.areNotificationsEnabled()) { + addIssue(issues, "notifications", "Notifications are turned off for Faceclaw, so the phone cannot show or sound an alarm.", true) + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notifications != null) { + val channel = notifications.getNotificationChannel(FaceclawAlarmService.CHANNEL_ID) + if (channel != null && channel.importance == NotificationManager.IMPORTANCE_NONE) { + addIssue(issues, "alarm-channel", "The Alarms notification category is blocked, so the phone cannot show an alarm.", true) + } + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && notifications != null && !notifications.canUseFullScreenIntent()) { + addIssue(issues, "full-screen", "Full-screen alarms are not allowed, so a ringing alarm will not take over the lock screen.", true) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notifications != null) { + val filter = notifications.currentInterruptionFilter + if (filter == NotificationManager.INTERRUPTION_FILTER_NONE) { + addIssue(issues, "dnd-total", "Do Not Disturb is set to total silence, which silences alarms too.", true) + } else if (filter == NotificationManager.INTERRUPTION_FILTER_PRIORITY) { + try { + val policy = notifications.notificationPolicy + if (policy != null && (policy.priorityCategories and NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { + addIssue(issues, "dnd-alarms", "Do Not Disturb does not allow alarms, so the phone will stay silent.", true) + } + } catch (ignored: SecurityException) { + // No notification-policy access: cannot inspect the exceptions. + } + } + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + val activityManager = appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager? + if (activityManager != null && activityManager.isBackgroundRestricted) { + addIssue(issues, "background-restricted", "Background usage is restricted for Faceclaw, which blocks alarms while the app is not open.", true) + } + } + val power = appContext.getSystemService(Context.POWER_SERVICE) as PowerManager? + if (power != null && !power.isIgnoringBatteryOptimizations(appContext.packageName)) { + addIssue(issues, "battery-optimized", "Battery optimization is on for Faceclaw; some phones delay alarms because of it.", true) + } + val audio = appContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager? + if (audio != null && audio.getStreamVolume(AudioManager.STREAM_ALARM) == 0) { + addIssue(issues, "alarm-volume", "The phone's alarm volume is muted; Faceclaw raises it while ringing, but check it.", true) + } + val maker = if (Build.MANUFACTURER == null) "" else Build.MANUFACTURER.lowercase(Locale.ROOT) + if (maker.contains("xiaomi") || maker.contains("huawei") || maker.contains("oppo") || maker.contains("vivo") + || maker.contains("oneplus") || maker.contains("realme") || maker.contains("meizu") || maker.contains("asus")) { + addIssue(issues, "oem-killer", "This phone maker is known to stop background apps; allow Faceclaw to auto-start and run unrestricted (see dontkillmyapp.com).", true) + } + if (scheduleLooksStale(appContext)) { + addIssue(issues, "schedule-stale", "The system lost Faceclaw's next alarm (the app may have been force-stopped); it was re-armed.", false) + rescheduleAll(appContext, "self-check") + } + return issues.toString() + } + + private fun addIssue(issues: JSONArray, code: String, message: String, fixable: Boolean) { + val issue = JSONObject() + try { + issue.put("code", code) + issue.put("message", message) + issue.put("fixable", fixable) + } catch (ignored: JSONException) { + return + } + issues.put(issue) + } + + /** Open the system screen where the issue can be fixed. */ + @JvmStatic + fun openReliabilityFix(context: Context, code: String?) { + val appContext = context.applicationContext + val pkg = appContext.packageName + var intent: Intent + when (code ?: "") { + "exact-alarm" -> + intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:$pkg")) + else + appDetails(pkg) + "notifications", "alarm-channel" -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) + .putExtra(Settings.EXTRA_APP_PACKAGE, pkg) + if ("alarm-channel" == code) { + intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) + .putExtra(Settings.EXTRA_APP_PACKAGE, pkg) + .putExtra(Settings.EXTRA_CHANNEL_ID, FaceclawAlarmService.CHANNEL_ID) + } + } else { + intent = appDetails(pkg) + } + } + "full-screen" -> + intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) + Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT, Uri.parse("package:$pkg")) + else + appDetails(pkg) + "dnd-total", "dnd-alarms" -> + intent = Intent(Settings.ACTION_SOUND_SETTINGS) + "battery-optimized" -> + intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:$pkg")) + "alarm-volume" -> + intent = Intent(Settings.ACTION_SOUND_SETTINGS) + else -> + intent = appDetails(pkg) + } + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + try { + appContext.startActivity(intent) + } catch (error: RuntimeException) { + val fallback = appDetails(pkg).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + try { + appContext.startActivity(fallback) + } catch (ignored: RuntimeException) { + // No settings activity at all; nothing more to do. + } + } + } + + private fun appDetails(pkg: String): Intent { + return Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:$pkg")) + } + + // ------------------------------------------------------------------ + // Diagnostics log + + @JvmStatic + internal fun log(context: Context, line: String) { + scheduleCore(context.applicationContext).log(line) + } + + /** The ring / miss / action log, newest last, one line per entry. */ + @JvmStatic + fun readLog(context: Context): String = scheduleCore(context.applicationContext).readLog() + + // ------------------------------------------------------------------ + // Intents + + private fun expiryPendingIntent(appContext: Context, entry: AlarmEntry): PendingIntent { + val intent = Intent(appContext, FaceclawAlarmReceiver::class.java) + intent.action = ACTION_EXPIRE + val id = entry.id + intent.putExtra(EXTRA_ID, id) + intent.putExtra(EXTRA_TITLE, entry.title) + intent.putExtra(EXTRA_TEXT, entry.text) + intent.putExtra(EXTRA_KIND, entry.kind) + intent.putExtra(EXTRA_SNOOZE_MINUTES, entry.snoozeMinutes) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + return PendingIntent.getBroadcast(appContext, requestCode(id), intent, flags) + } + + /** What the system's alarm indicator opens: the app. */ + private fun showIntent(appContext: Context, id: Long): PendingIntent { + val intent = Intent(appContext, NativeScriptActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + return PendingIntent.getActivity(appContext, requestCode(id), intent, flags) + } + + @JvmStatic + internal fun requestCode(id: Long): Int = AlarmSchedule.requestCode(id) + + @JvmStatic + internal fun startServiceCompat(appContext: Context, intent: Intent) { + ContextCompat.startForegroundService(appContext, intent) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.java deleted file mode 100644 index 0aab2ff6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.java +++ /dev/null @@ -1,242 +0,0 @@ -package com.faceclaw.app; - -import android.media.MediaCodec; -import android.media.MediaCodecInfo; -import android.media.MediaFormat; -import android.media.MediaMuxer; -import android.util.Log; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -/** - * Transcodes a 16-bit PCM WAV conversation recording into AAC-LC in an M4A - * container, cutting speech recordings to roughly a tenth of their WAV size. - * Runs on its own thread; the source WAV is deleted on success when requested. - */ -public class FaceclawAudioTranscoder { - private static final String TAG = "FaceclawTranscode"; - private static final long CODEC_TIMEOUT_US = 10_000; - - public interface Listener { - void onDone(String outputPath, long outputBytes); - - void onError(String message); - } - - private final String wavPath; - private final String outputPath; - private final int bitrateBps; - private final boolean deleteSourceOnSuccess; - private final Listener listener; - - public FaceclawAudioTranscoder( - String wavPath, - String outputPath, - int bitrateBps, - boolean deleteSourceOnSuccess, - Listener listener) { - this.wavPath = wavPath; - this.outputPath = outputPath; - this.bitrateBps = bitrateBps > 0 ? bitrateBps : 32_000; - this.deleteSourceOnSuccess = deleteSourceOnSuccess; - this.listener = listener; - } - - public void start() { - Thread thread = new Thread(this::run, "FaceclawTranscode"); - thread.setPriority(Thread.MIN_PRIORITY); - thread.start(); - } - - private void run() { - try { - transcode(); - long size = new File(outputPath).length(); - if (deleteSourceOnSuccess) { - new File(wavPath).delete(); - } - if (listener != null) { - listener.onDone(outputPath, size); - } - } catch (Throwable t) { - Log.w(TAG, "transcode failed for " + wavPath, t); - new File(outputPath).delete(); - if (listener != null) { - listener.onError(t.getMessage() == null ? t.toString() : t.getMessage()); - } - } - } - - private void transcode() throws IOException { - WavInfo wav = readWavHeader(wavPath); - MediaFormat format = MediaFormat.createAudioFormat( - MediaFormat.MIMETYPE_AUDIO_AAC, wav.sampleRate, wav.channels); - format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); - format.setInteger(MediaFormat.KEY_BIT_RATE, bitrateBps); - format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16 * 1024); - - MediaCodec codec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC); - MediaMuxer muxer = null; - FileInputStream input = null; - try { - codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); - codec.start(); - muxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); - input = new FileInputStream(wavPath); - long skipped = 0; - while (skipped < wav.dataOffset) { - long step = input.skip(wav.dataOffset - skipped); - if (step <= 0) { - throw new IOException("could not seek past WAV header"); - } - skipped += step; - } - - MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); - byte[] chunk = new byte[8192]; - int track = -1; - boolean muxerStarted = false; - boolean inputDone = false; - boolean outputDone = false; - long remaining = wav.dataBytes; - long presentationUs = 0; - long bytesPerSecond = (long) wav.sampleRate * wav.channels * 2; - - while (!outputDone) { - if (!inputDone) { - int inIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_US); - if (inIndex >= 0) { - ByteBuffer inBuf = codec.getInputBuffer(inIndex); - int wanted = (int) Math.min(chunk.length, Math.min(remaining, inBuf.capacity())); - int read = wanted > 0 ? input.read(chunk, 0, wanted) : -1; - if (read <= 0) { - codec.queueInputBuffer(inIndex, 0, 0, presentationUs, - MediaCodec.BUFFER_FLAG_END_OF_STREAM); - inputDone = true; - } else { - inBuf.clear(); - inBuf.put(chunk, 0, read); - codec.queueInputBuffer(inIndex, 0, read, presentationUs, 0); - remaining -= read; - presentationUs += read * 1_000_000L / bytesPerSecond; - } - } - } - int outIndex = codec.dequeueOutputBuffer(info, CODEC_TIMEOUT_US); - if (outIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { - track = muxer.addTrack(codec.getOutputFormat()); - muxer.start(); - muxerStarted = true; - } else if (outIndex >= 0) { - if (info.size > 0 && (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) { - if (!muxerStarted) { - throw new IOException("encoder produced data before format"); - } - ByteBuffer outBuf = codec.getOutputBuffer(outIndex); - outBuf.position(info.offset); - outBuf.limit(info.offset + info.size); - muxer.writeSampleData(track, outBuf, info); - } - codec.releaseOutputBuffer(outIndex, false); - if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { - outputDone = true; - } - } - } - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException ignored) { - } - } - try { - codec.stop(); - } catch (Throwable ignored) { - } - codec.release(); - if (muxer != null) { - try { - muxer.stop(); - } catch (Throwable ignored) { - } - muxer.release(); - } - } - } - - private static final class WavInfo { - int sampleRate; - int channels; - long dataOffset; - long dataBytes; - } - - /** Minimal RIFF walk: locates fmt and data chunks, 16-bit PCM only. */ - private static WavInfo readWavHeader(String path) throws IOException { - try (FileInputStream in = new FileInputStream(path)) { - byte[] head = new byte[12]; - if (in.read(head) != 12 || head[0] != 'R' || head[1] != 'I' || head[8] != 'W') { - throw new IOException("not a WAV file"); - } - WavInfo info = new WavInfo(); - long offset = 12; - byte[] chunkHead = new byte[8]; - while (in.read(chunkHead) == 8) { - ByteBuffer b = ByteBuffer.wrap(chunkHead).order(ByteOrder.LITTLE_ENDIAN); - int id = b.getInt(); - long size = b.getInt() & 0xffffffffL; - offset += 8; - if (id == 0x20746d66) { // "fmt " - byte[] fmt = new byte[(int) Math.min(size, 16)]; - if (in.read(fmt) != fmt.length) { - throw new IOException("truncated fmt chunk"); - } - ByteBuffer f = ByteBuffer.wrap(fmt).order(ByteOrder.LITTLE_ENDIAN); - int audioFormat = f.getShort(); - info.channels = f.getShort(); - info.sampleRate = f.getInt(); - f.getInt(); - f.getShort(); - int bits = f.getShort(); - if (audioFormat != 1 || bits != 16) { - throw new IOException("only 16-bit PCM WAV is supported"); - } - long skip = size - fmt.length; - while (skip > 0) { - long step = in.skip(skip); - if (step <= 0) { - break; - } - skip -= step; - } - offset += size; - } else if (id == 0x61746164) { // "data" - info.dataOffset = offset; - // A zero size means the header was never patched (crash - // mid-recording); recover the length from the file size. - info.dataBytes = size > 0 ? size : new File(path).length() - offset; - break; - } else { - long skip = size; - while (skip > 0) { - long step = in.skip(skip); - if (step <= 0) { - break; - } - skip -= step; - } - offset += size; - } - } - if (info.sampleRate == 0 || info.dataOffset == 0) { - throw new IOException("missing fmt or data chunk"); - } - return info; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.kt new file mode 100644 index 00000000..58e016cd --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.kt @@ -0,0 +1,183 @@ +package com.faceclaw.app + +import android.media.MediaCodec +import android.media.MediaCodecInfo +import android.media.MediaFormat +import android.media.MediaMuxer +import android.util.Log + +import java.io.File +import java.io.FileInputStream +import java.io.IOException + +/** + * Transcodes a 16-bit PCM WAV conversation recording into AAC-LC in an M4A + * container, cutting speech recordings to roughly a tenth of their WAV size. + * Runs on its own thread; the source WAV is deleted on success when requested. + */ +class FaceclawAudioTranscoder( + private val wavPath: String?, + private val outputPath: String?, + bitrateBps: Int, + private val deleteSourceOnSuccess: Boolean, + private val listener: Listener? +) { + companion object { + private const val TAG = "FaceclawTranscode" + private const val CODEC_TIMEOUT_US: Long = 10_000 + + /** Shared RIFF walk over the file's first bytes: locates fmt and data chunks, 16-bit PCM only. */ + @JvmStatic + @Throws(IOException::class) + private fun readWavHeader(path: String?): WavPcmReader.WavInfo { + val file = File(path) + val fileLength = file.length() + FileInputStream(file).use { `in` -> + val prefix = ByteArray(Math.min(fileLength, WavPcmReader.HEADER_PREFIX_BYTES.toLong()).toInt()) + var filled = 0 + while (filled < prefix.size) { + val read = `in`.read(prefix, filled, prefix.size - filled) + if (read <= 0) break + filled += read + } + try { + return WavPcmReader.parseHeader(if (filled == prefix.size) prefix else prefix.copyOf(filled), fileLength) + } catch (e: IllegalArgumentException) { + throw IOException(e.message ?: "not a WAV file") + } + } + } + } + + interface Listener { + fun onDone(outputPath: String?, outputBytes: Long) + + fun onError(message: String?) + } + + private val bitrateBps: Int = if (bitrateBps > 0) bitrateBps else 32_000 + + fun start() { + val thread = Thread(Runnable { run() }, "FaceclawTranscode") + thread.priority = Thread.MIN_PRIORITY + thread.start() + } + + private fun run() { + try { + transcode() + val size = File(outputPath).length() + if (deleteSourceOnSuccess) { + File(wavPath).delete() + } + if (listener != null) { + listener.onDone(outputPath, size) + } + } catch (t: Throwable) { + Log.w(TAG, "transcode failed for $wavPath", t) + File(outputPath).delete() + if (listener != null) { + listener.onError(if (t.message == null) t.toString() else t.message) + } + } + } + + @Throws(IOException::class) + private fun transcode() { + val wav = readWavHeader(wavPath) + val format = MediaFormat.createAudioFormat( + MediaFormat.MIMETYPE_AUDIO_AAC, wav.sampleRate, wav.channels) + format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC) + format.setInteger(MediaFormat.KEY_BIT_RATE, bitrateBps) + format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16 * 1024) + + val codec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC) + var muxer: MediaMuxer? = null + var input: FileInputStream? = null + try { + codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE) + codec.start() + muxer = MediaMuxer(outputPath!!, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4) + input = FileInputStream(wavPath) + var skipped: Long = 0 + while (skipped < wav.dataOffset) { + val step = input.skip(wav.dataOffset - skipped) + if (step <= 0) { + throw IOException("could not seek past WAV header") + } + skipped += step + } + + val info = MediaCodec.BufferInfo() + val chunk = ByteArray(8192) + var track = -1 + var muxerStarted = false + var inputDone = false + var outputDone = false + var remaining = wav.dataBytes + var presentationUs: Long = 0 + val bytesPerSecond = wav.sampleRate.toLong() * wav.channels * 2 + + while (!outputDone) { + if (!inputDone) { + val inIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_US) + if (inIndex >= 0) { + val inBuf = codec.getInputBuffer(inIndex)!! + val wanted = Math.min(chunk.size.toLong(), Math.min(remaining, inBuf.capacity().toLong())).toInt() + val read = if (wanted > 0) input.read(chunk, 0, wanted) else -1 + if (read <= 0) { + codec.queueInputBuffer(inIndex, 0, 0, presentationUs, + MediaCodec.BUFFER_FLAG_END_OF_STREAM) + inputDone = true + } else { + inBuf.clear() + inBuf.put(chunk, 0, read) + codec.queueInputBuffer(inIndex, 0, read, presentationUs, 0) + remaining -= read.toLong() + presentationUs += read * 1_000_000L / bytesPerSecond + } + } + } + val outIndex = codec.dequeueOutputBuffer(info, CODEC_TIMEOUT_US) + if (outIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { + track = muxer.addTrack(codec.outputFormat) + muxer.start() + muxerStarted = true + } else if (outIndex >= 0) { + if (info.size > 0 && (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) { + if (!muxerStarted) { + throw IOException("encoder produced data before format") + } + val outBuf = codec.getOutputBuffer(outIndex)!! + outBuf.position(info.offset) + outBuf.limit(info.offset + info.size) + muxer.writeSampleData(track, outBuf, info) + } + codec.releaseOutputBuffer(outIndex, false) + if ((info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { + outputDone = true + } + } + } + } finally { + if (input != null) { + try { + input.close() + } catch (ignored: IOException) { + } + } + try { + codec.stop() + } catch (ignored: Throwable) { + } + codec.release() + if (muxer != null) { + try { + muxer.stop() + } catch (ignored: Throwable) { + } + muxer.release() + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java deleted file mode 100644 index 9f32fadb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ /dev/null @@ -1,4135 +0,0 @@ -package com.faceclaw.app; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothGatt; -import android.bluetooth.BluetoothGattCharacteristic; -import android.app.KeyguardManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.Handler; -import android.os.Looper; -import android.os.PowerManager; -import android.os.SystemClock; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Random; - -@SuppressLint("MissingPermission") -public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { - private static final String TAG = "FaceclawComm"; - - // Local metadata for custom-command bookkeeping, not an EvenHub container. - // Submitted frames supply pixel geometry; the stock layout only captures input. - private static final BleProtocol.ImageTileOptions DASHBOARD_TILE = - new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 576, 288); - - private static final String G2_SCREEN_WAKE_LOCK_TAG = "Faceclaw:G2Screen"; - private static final long FACECLAW_WAKE_LEASE_RENEW_MS = 45_000; - private static final long FACECLAW_WAKE_CONTROL_WAIT_MS = 1_500; - private static final long CFW_CLEANUP_WAIT_MS = 4_000; - private static final int COMPASS_REPORT_INTERVAL_MS = 100; - private static final int COMPASS_MIN_CHANGE_DEGREES = 0; - - private final Context appContext; - private final PowerManager powerManager; - private final KeyguardManager keyguardManager; - private final FaceclawBleManager bleManager; - private final InterruptibleSleep interruptibleSleep = new InterruptibleSleep(); - private final Object lock = new Object(); - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final String rightAddress; - private final String leftAddress; - private final String ringAddress; - - private volatile FaceclawBleCommunicatorListener listener; - private final java.util.List imuListeners = - new java.util.concurrent.CopyOnWriteArrayList<>(); - /** A compass subscriber plus the Looper it registered from (see addCompassListener). */ - private static final class CompassSubscription { - final FaceclawCompassListener listener; - final Handler handler; - - CompassSubscription(FaceclawCompassListener listener, Handler handler) { - this.listener = listener; - this.handler = handler; - } - } - - private final java.util.List compassSubscriptions = - new java.util.concurrent.CopyOnWriteArrayList<>(); - private final java.util.List ambientLightListeners = - new java.util.concurrent.CopyOnWriteArrayList<>(); - private final java.util.List micStatusListeners = - new java.util.concurrent.CopyOnWriteArrayList<>(); - private volatile Thread workerThread; - private volatile boolean running; - private volatile boolean userDisconnectRequested; - // Set when a connect attempt failed while an arm's Android bond is gone: - // retrying is pointless until the user re-pairs, so the worker loop parks - // instead of redialing. Cleared by start() (a fresh explicit connect). - private volatile boolean reconnectHalted; - - private String phase = "disconnected"; - private String status = "Disconnected."; - - private boolean rightConnected; - private boolean leftConnected; - private boolean ringConnected; - private boolean ringNotificationsReady; - private boolean sessionReady; - private boolean fixedLayoutCreated; - private boolean shutdownRequested; - // CFW firmware-debug-flags overlay (mode 7). Desired value pushed from TS; the - // sub-op last sent this session (-1 = not yet), reset on (re)connect so the - // overlay state is re-asserted on every reconnect and whenever the value changes. - private volatile boolean firmwareDebugFlagsEnabled; - private int firmwareDebugFlagsLastSent = -1; - // Desired CFW mode-10 compass state. It survives reconnects; lastSent is - // reset with each session so an open Compass window is re-asserted. - /** - * Who currently wants the stock compass running (the Compass window, the - * Navigate worker, ...). The magnetometer is one shared resource, so it - * stays on while any owner holds it and is released when the last lets go; - * this keeps one app's release from silently switching off another's feed. - */ - private final java.util.Set compassOwners = new java.util.HashSet<>(); - private int compassControlLastSent = -1; - // Whether the glasses-side compass may still be running: set when an enable - // is enqueued, cleared only when a disable is acked. Drives the forced - // disable sent ahead of an EvenHub shutdown/suspend, since a pending - // disable can be wiped by the shutdown's queue flush and the retry loop - // does not run while shutdownRequested (magnetometer left on = battery drain). - private boolean compassMaybeOn; - private boolean startupProbePending; - // Desired ownership of CFW's fail-open stock-wake lease (dashboard launch - // and Even AI foreground takeover). This survives a transport reconnect; - // the lease itself is volatile firmware state and is re-acquired once both - // arms are ready. - private boolean faceclawWakeLeaseEnabled; - private long lastFaceclawWakeLeaseQueuedAtMs; - private int faceclawWakeControlGeneration; - private int faceclawWakeControlSentCount; - private long lastFaceclawFramebufferLeaseQueuedAtMs; - private int faceclawFramebufferControlGeneration; - private int faceclawFramebufferControlSentCount; - private int faceclawWakePendingNonce = -1; - /** - * The last firmware-info read said the glasses run Faceclaw's custom - * firmware. Gates the private modes (cleanup, texture cache, ...) so stock - * or third-party firmware never sees them; the TS side checks the actual - * revision and disconnects on a mismatch, so no per-feature gating is - * needed here. - */ - private boolean customFirmwareDetected; - private boolean cfwCleanupDelivered; - private int lastCfwCleanupAckMagic; - - private long reconnectAfterMs; - private long ringReconnectAfterMs; - private long lastAckAtMs; - private long lastIncomingAtMs; - private long lastHeartbeatSentAtMs; - private long lastHeartbeatAckedAtMs; - private long lastConnectionOrInputAtMs; - private long lastBatteryRefreshAtMs; - private long imageRetryAfterMs; - private long lastSessionReadyAtMs; - private long lastEvenAppConflictAtMs; - private int consecutiveAckTimeouts; - private int lastAudioControlAckMagic = 0; - - private ConnectionOptions connectionOptions = new ConnectionOptions(); - private final BleMagicPool magicPool = new BleMagicPool(AndroidProtocolPlatform.INSTANCE); - private MessageBuilder messageBuilder = new MessageBuilder(magicPool); - private int nextTransportSeq = 0x40; - private int nextMapSessionId = 0; - private int nextImageUpdateId = 1; - // Wire frame id for mode-3 deltas (CFW reorder/skip/dup diagnostic). uint16, - // advanced by 1 per emitted delta; kept in [1, 0xfffe] to avoid the CFW's - // 0xffff "empty" sentinel. - private int nextImageFrameId = 1; - private int lastShutdownAckMagic = 0; - private long lastShutdownExitAtMs = 0; - private int headsetBattery = -1; - private int headsetCharging = -1; - private int ringBattery = -1; - private int ringCharging = -1; - // Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked. - private int silentMode = -1; - private int wearState = -1; - private int phoneLockState = -1; - private long lastPhoneLockCheckAtMs; - private boolean phoneLockReceiverRegistered; - private boolean audioCaptureActive; - private boolean firmwareInfoQueried; - // Glasses are in the charging case: nobody is wearing them, so display - // communication pauses and only battery polls flow (see driveSession). - private boolean chargingMode; - private volatile FaceclawAudioPacketListener audioPacketListener; - private PowerManager.WakeLock g2ScreenWakeLock; - - // BLE bandwidth benchmark (Developer app). Streams no-op image payloads - // (CFW mode 7 with an unused sub-op: parsed, acked, and discarded — stock - // firmware likewise ignores unknown image modes) for a fixed duration with - // a selectable message size and pipeline window, then reports throughput. - // While active, desired-frame sends are held back and heartbeats are - // satisfied by the benchmark's own acks, so the stream is the only image - // traffic. All state below is guarded by `lock`; results are read with - // getBandwidthBenchmarkStatus() and survive until the next run starts. - private boolean benchmarkActive; - private boolean benchmarkAborted; - private final Random benchmarkRandom = new Random(); - private int benchmarkMessageSize; - private int benchmarkWindowSize; - private int benchmarkDurationMs; - private int benchmarkLinkMode; - private boolean benchmarkLinkPending; - private long benchmarkReadyAtMs; - private long benchmarkStartAtMs; // first benchmark write; 0 until then - private long benchmarkDeadlineAtMs; // start + duration; MAX_VALUE until first write - private long benchmarkLastAckAtMs; - private long benchmarkEndAtMs; // 0 while running; set when the run drains - private int benchmarkMessagesSent; - private int benchmarkMessagesAcked; - private int benchmarkTimeouts; - private long benchmarkPayloadBytesAcked; - private long benchmarkWireBytesAcked; - // A timed-out benchmark message aborts the run, but its already-in-flight - // peers still time out one by one; keep the window comfortably below - // MAX_CONSECUTIVE_ACK_TIMEOUTS so a dead run can't escalate into a - // transport-failure reconnect all by itself. - private static final int BENCHMARK_MAX_WINDOW = 6; - - private final BroadcastReceiver phoneLockReceiver = new BroadcastReceiver() { - @Override public void onReceive(Context context, Intent intent) { - emitPhoneLockStateIfChanged(true); - interruptibleSleep.interrupt(); - } - }; - - private String displayedFingerprint = ""; - // The frame the firmware shadow will hold once the current image pipeline - // drains: the most recently ENQUEUED image (headerless packed 4bpp, see - // BmpUtil.pack4bppFromGray8), which is the correct base for the next delta - // when frames are pipelined. Set at enqueue; cleared whenever the image - // pipeline is cleared (clearAllMessagesLocked / clearMessagesOfKindLocked - // "image"), so it is only ever read while it holds a valid current-session base. - private byte[] lastEnqueuedPacked = new byte[0]; - private int lastEnqueuedWidth; - private int lastEnqueuedHeight; - private String lastEnqueuedFingerprint = ""; - private final Map imageUpdateStats = new HashMap<>(); - - private final Object desiredTilesLock = new Object(); - private String desiredFingerprint = ""; - // Headerless packed 4bpp frame (see BmpUtil.pack4bppFromGray8) plus its - // pixel dimensions. - private byte[] desiredPacked = new byte[0]; - private int desiredWidth; - private int desiredHeight; - private int desiredPaintMs; - private int desiredFrameId; - // Screen-space deferred draws (glyphs + images) whose pixels are baked - // into desiredPacked; the texture-cache planner may replay them as - // on-glasses cached draws. - private SurfaceCompositor.ScreenDraw[] desiredDraws = new SurfaceCompositor.ScreenDraw[0]; - // (frame, reason) of the last "waiting to send" line, so a frame that - // stalls for seconds records one line per state change (see - // noteImageStallLocked). Send-loop thread only. - private int stallFrameId; - private String stallReason = ""; - // Highest compositor sequence stored as the desired frame; composites that - // lost a store race to a newer one are discarded (their content is already - // included in the newer composite). - private long lastStoredCompositeSeq; - - private final SurfaceCompositor compositor = new SurfaceCompositor(); - - // Phone-side model of the CFW's 64 KiB texture cache (modes 12/13/14). - // Reset whenever the image pipeline / EvenHub session is torn down: the - // firmware frees the cache with the fb lease, and after any resync the - // cheap safe assumption is an empty cache (glyphs re-upload lazily). - private final TextureCacheState textureCache = new TextureCacheState(); - - private final ArrayDeque pendingMessages = new ArrayDeque<>(); - private final CfwTransport[] cfwTransports = { new CfwTransport(AndroidProtocolPlatform.INSTANCE), new CfwTransport(AndroidProtocolPlatform.INSTANCE) }; - private final ArrayDeque inFlightMessages = new ArrayDeque<>(); - private OutboundMessage prewrittenMessage; - private List prewrittenFrames = Collections.emptyList(); - - public FaceclawBleCommunicator(Context context, String rightAddress, String leftAddress, String ringAddress) { - this.appContext = context.getApplicationContext(); - FrameTimings.getInstance().init(appContext); - this.powerManager = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); - this.keyguardManager = (KeyguardManager) appContext.getSystemService(Context.KEYGUARD_SERVICE); - this.bleManager = new FaceclawBleManager(appContext); - this.bleManager.setListener(this); - this.rightAddress = requireAddress("rightAddress", rightAddress); - this.leftAddress = requireAddress("leftAddress", leftAddress); - this.ringAddress = ringAddress == null ? "" : ringAddress.trim(); - IntentFilter phoneLockFilter = new IntentFilter(); - phoneLockFilter.addAction(Intent.ACTION_SCREEN_ON); - phoneLockFilter.addAction(Intent.ACTION_SCREEN_OFF); - phoneLockFilter.addAction(Intent.ACTION_USER_PRESENT); - appContext.registerReceiver(phoneLockReceiver, phoneLockFilter); - phoneLockReceiverRegistered = true; - } - - - public void setListener(FaceclawBleCommunicatorListener listener) { - this.listener = listener; - emitState(); - emitPhoneLockStateIfChanged(true); - } - - public void start() { - synchronized (lock) { - if (running) { - return; - } - running = true; - userDisconnectRequested = false; - reconnectHalted = false; - shutdownRequested = false; - activeInstance = this; - workerThread = new Thread(this, "FaceclawBleCommunicator"); - workerThread.start(); - } - } - - public void disconnect() { - /* On the normal path DashboardController already sent mode 11 after - * quiescing its producers. Also cover direct/early close callers here; - * a successful cleanup must remain the final BLE message. Older CFWs - * fall back to the standalone framebuffer-lease release. */ - if (!cfwCleanupDelivered && !sendCfwCleanup()) { - releaseFaceclawFramebufferLease(); - } - Thread threadToJoin; - synchronized (lock) { - userDisconnectRequested = true; - running = false; - audioCaptureActive = false; - audioPacketListener = null; - threadToJoin = workerThread; - } - setStateDisplay("disconnecting", "Disconnecting..."); - interruptibleSleep.interrupt(); - if (threadToJoin != null) { - threadToJoin.interrupt(); - try { - threadToJoin.join(5_000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - synchronized (lock) { - workerThread = null; - resetSessionStateLocked(); - clearAllMessagesLocked("disconnect"); - // Unknown until the next connection's first push or settings poll. - silentMode = -1; - } - bleManager.disconnect(rightAddress); - bleManager.disconnect(leftAddress); - if (hasRingAddress()) { - bleManager.disconnect(ringAddress); - } - bleManager.close(); - releaseG2ScreenWakeLock(); - setStateDisplay("disconnected", "Disconnected."); - } - - public void close() { - if (activeInstance == this) { - activeInstance = null; - } - disconnect(); - for (CfwTransport transport : cfwTransports) transport.close(); - if (phoneLockReceiverRegistered) { - phoneLockReceiverRegistered = false; - appContext.unregisterReceiver(phoneLockReceiver); - } - } - - public void setG2ScreenOn(boolean screenOn) { - mainHandler.post(() -> updateG2ScreenWakeLock(screenOn)); - } - - public void setFirmwareDebugFlags(boolean enabled) { - // Just record it; the drive loop emits the mode-7 control message when the - // display path is ready and idle, and re-emits when this value changes. - firmwareDebugFlagsEnabled = enabled; - } - - public boolean startG2AudioCapture(FaceclawAudioPacketListener listener) { - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - int magic; - synchronized (lock) { - if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) { - logLine("skip G2 mic enable; EvenHub display path not ready"); - return false; - } - audioPacketListener = listener; - OutboundMessage message = createAudioControlMessageLocked(true); - magic = message.magic; - pendingMessages.addFirst(message); - logLine("queue G2 mic enable"); - } - interruptibleSleep.interrupt(); - return waitForAudioControlAck(magic, "enable"); - } - - public void stopG2AudioCapture() { - int magic = 0; - synchronized (lock) { - audioPacketListener = null; - audioCaptureActive = false; - clearMessagesOfKindLocked("audio-control"); - if (running && sessionReady) { - OutboundMessage message = createAudioControlMessageLocked(false); - magic = message.magic; - pendingMessages.addFirst(message); - logLine("queue G2 mic disable"); - } - } - interruptibleSleep.interrupt(); - if (magic != 0) { - waitForAudioControlAck(magic, "disable"); - } - } - - public boolean isSessionReady() { - synchronized (lock) { - return running && sessionReady; - } - } - - /** - * Whether the glasses mic is enabled right now. The enable lives in the - * current EvenHub session, so it dies with a transport drop, the charging - * case, or a suspend — silently, from the phone's point of view. Callers - * that track a capture across those events must check this rather than - * assume their earlier enable still holds. - */ - public boolean isAudioCaptureActive() { - synchronized (lock) { - return running && sessionReady && !shutdownRequested && audioCaptureActive; - } - } - - /** - * Acquire/renew or release CFW's volatile wake-takeover lease on both - * arms. Delivery (not a protocol ACK) is awaited so a caller can ensure - * the fail-open firmware policy is installed before relying on wakeword - * interception or suspending EvenHub. - */ - public boolean setFaceclawWakeLeaseEnabled(boolean enabled) { - int generation; - synchronized (lock) { - faceclawWakeLeaseEnabled = enabled; - if (!running || !sessionReady) { - return !enabled; - } - generation = enqueueFaceclawWakeControlLocked( - enabled ? BleProtocol.FACECLAW_WAKE_OP_ACQUIRE : BleProtocol.FACECLAW_WAKE_OP_RELEASE, - 0, - true - ); - if (!enabled) { - faceclawWakePendingNonce = -1; - } - } - interruptibleSleep.interrupt(); - return waitForFaceclawWakeControlDelivery(generation, FACECLAW_WAKE_CONTROL_WAIT_MS); - } - - /** - * Wait until the recreated layout and retained compositor frame have both - * landed. If this wake came from CFW's deferred double tap, READY - * is then sent to both arms to cancel their stock-dashboard fallback. - */ - public boolean awaitEvenHubSessionReady(int timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + Math.max(0, timeoutMs); - int readyGeneration = 0; - synchronized (lock) { - while (running && sessionReady) { - boolean frameReady = false; - synchronized (desiredTilesLock) { - frameReady = !desiredFingerprint.isEmpty() - && desiredFingerprint.equals(displayedFingerprint); - } - if (!shutdownRequested && fixedLayoutCreated && frameReady) { - if (faceclawWakePendingNonce >= 0) { - readyGeneration = enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_READY, - faceclawWakePendingNonce, - true - ); - faceclawWakePendingNonce = -1; - } - break; - } - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return false; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - if (!running || !sessionReady) { - return false; - } - } - if (readyGeneration != 0) { - interruptibleSleep.interrupt(); - if (!waitForFaceclawWakeControlDelivery(readyGeneration, FACECLAW_WAKE_CONTROL_WAIT_MS)) { - logLine("wake READY delivery not confirmed before fallback deadline"); - } - } - return true; - } - - /** - * Enable or disable the IMU (accelerometer) report stream. Fire-and-forget: - * the control message is queued ahead of other traffic; readings arrive via - * registered FaceclawImuListeners. reportFrq is the requested sample rate - * (ignored on disable). - */ - public void setImuReportEnabled(boolean enable, int reportFrq) { - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip IMU " + (enable ? "enable" : "disable") + "; session not ready"); - return; - } - clearMessagesOfKindLocked("imu-control"); - OutboundMessage message = messageBuilder.enableOrDisableImu(enable, reportFrq); - message.onTimeout = () -> logLine("IMU control ack timeout"); - pendingMessages.addFirst(message); - logLine("queue IMU " + (enable ? "enable freq=" + reportFrq : "disable")); - } - interruptibleSleep.interrupt(); - } - - /** - * Enable/disable the stock compass through CFW image-handler mode 10. The - * desired state is retained across reconnects; headings arrive through - * stock sid-0x08 navigation notifications and FaceclawCompassListeners. - */ - public void setCompassEnabled(boolean enable) { - setCompassEnabled("compass", enable); - } - - /** - * As above, on behalf of a named owner. The compass runs while at least - * one owner has enabled it; an owner disabling it only takes effect once - * no other owner still wants it. - */ - public void setCompassEnabled(String owner, boolean enable) { - synchronized (lock) { - boolean before = !compassOwners.isEmpty(); - if (enable) { - compassOwners.add(owner); - } else { - compassOwners.remove(owner); - } - boolean wanted = !compassOwners.isEmpty(); - if (wanted == before && compassControlLastSent == (wanted ? 1 : 0)) { - logLine("compass " + (enable ? "enable" : "disable") + " by " + owner - + "; state unchanged (owners=" + compassOwners + ")"); - return; - } - compassControlLastSent = -1; - clearMessagesOfKindLocked("compass-control"); - if (running && sessionReady && !shutdownRequested && fixedLayoutCreated) { - enqueueCompassControlLocked(true, wanted); - } else { - logLine("defer compass " + (enable ? "enable" : "disable") + "; display path not ready"); - } - } - interruptibleSleep.interrupt(); - } - - /** - * Set the lens brightness. Fire-and-forget, like the IMU control: the - * message is queued ahead of other traffic and any not-yet-sent brightness - * message is superseded. When autoAdjust is true the ambient-light sensor - * drives brightness and brightnessLevel is ignored; otherwise - * brightnessLevel (0-100) is applied directly. - */ - public void setBrightness(boolean autoAdjust, int brightnessLevel) { - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip brightness set; session not ready"); - return; - } - clearMessagesOfKindLocked("brightness-control"); - OutboundMessage message = messageBuilder.setBrightness(autoAdjust, brightnessLevel); - message.onTimeout = () -> logLine("brightness control ack timeout"); - pendingMessages.addFirst(message); - logLine("queue brightness " + (autoAdjust ? "auto" : "level=" + brightnessLevel)); - } - interruptibleSleep.interrupt(); - } - - /** - * Enable the stock wear detector, then ask CFW to emit its current cached - * state. The queue order matters: the query must run after the setting is - * applied, including on a fresh install where wear detection was disabled. - */ - public void enableWearDetectionAndRequestState() { - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip wear detector setup; session not ready"); - return; - } - clearMessagesOfKindLocked("wear-detection-control"); - clearMessagesOfKindLocked("wear-query-control"); - OutboundMessage queryLeft = messageBuilder.faceclawWearQuery(true); - OutboundMessage queryRight = messageBuilder.faceclawWearQuery(false); - OutboundMessage enable = messageBuilder.setWearDetection(true); - enable.onTimeout = () -> logLine("wear detection enable ack timeout"); - pendingMessages.addFirst(queryLeft); - pendingMessages.addFirst(queryRight); - pendingMessages.addFirst(enable); - logLine("queue wear detection enable + current-state query"); - } - interruptibleSleep.interrupt(); - } - - /** - * Start the BLE bandwidth benchmark: stream messageSize-byte no-op image - * payloads for durationMs, keeping up to windowSize messages awaiting ack - * at once, then leave the results for getBandwidthBenchmarkStatus(). - * Returns false when a run is already active or the image path is not - * ready. The duration clock starts at the first benchmark write, so - * traffic already queued ahead of the run doesn't count against it. - */ - public boolean startBandwidthBenchmark(int messageSize, int windowSize, int durationMs) { - return startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, 0); - } - - // 0: current link; 1: re-request HIGH; 2: request 2M; 3: both. - public boolean startBandwidthBenchmarkWithLinkMode(int messageSize, int windowSize, - int durationMs, int linkMode) { - synchronized (lock) { - if (benchmarkActive || !running || !sessionReady || !fixedLayoutCreated - || shutdownRequested || chargingMode) { - logLine("skip bandwidth benchmark; already running or image path not ready"); - return false; - } - benchmarkMessageSize = Math.max(2, Math.min(messageSize, ConnectionOptions.IMAGE_FRAGMENT_SIZE)); - benchmarkWindowSize = Math.max(1, Math.min(windowSize, BENCHMARK_MAX_WINDOW)); - benchmarkDurationMs = Math.max(1_000, durationMs); - benchmarkLinkMode = linkMode & 3; - benchmarkLinkPending = true; - benchmarkReadyAtMs = Long.MAX_VALUE; - benchmarkStartAtMs = 0; - benchmarkDeadlineAtMs = Long.MAX_VALUE; - benchmarkLastAckAtMs = 0; - benchmarkEndAtMs = 0; - benchmarkMessagesSent = 0; - benchmarkMessagesAcked = 0; - benchmarkTimeouts = 0; - benchmarkPayloadBytesAcked = 0; - benchmarkWireBytesAcked = 0; - benchmarkAborted = false; - benchmarkActive = true; - logLine("bandwidth benchmark start: size=" + benchmarkMessageSize - + "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs - + "ms linkMode=" + benchmarkLinkMode); - } - interruptibleSleep.interrupt(); - return true; - } - - /** - * Cancel an in-progress benchmark (benchmark page closed). Queued no-op - * messages are dropped; in-flight ones drain through their normal acks. - */ - public void cancelBandwidthBenchmark() { - synchronized (lock) { - if (!benchmarkActive) { - return; - } - clearMessagesOfKindLocked("bandwidth"); - finishBenchmarkLocked(true, "cancelled"); - } - interruptibleSleep.interrupt(); - } - - /** Status/results of the current or most recent benchmark run, as JSON. */ - public String getBandwidthBenchmarkStatus() { - synchronized (lock) { - String state = benchmarkActive - ? (benchmarkStartAtMs == 0 ? "starting" : "running") - : (benchmarkEndAtMs != 0 ? "done" : "idle"); - long end = benchmarkActive ? SystemClock.elapsedRealtime() : benchmarkEndAtMs; - long elapsed = benchmarkStartAtMs == 0 ? 0 : Math.max(0, end - benchmarkStartAtMs); - try { - org.json.JSONObject status = new org.json.JSONObject(); - status.put("state", state); - status.put("messageSize", benchmarkMessageSize); - status.put("windowSize", benchmarkWindowSize); - status.put("linkMode", benchmarkLinkMode); - status.put("elapsedMs", elapsed); - status.put("messagesSent", benchmarkMessagesSent); - status.put("messagesAcked", benchmarkMessagesAcked); - status.put("timeouts", benchmarkTimeouts); - status.put("payloadBytesAcked", benchmarkPayloadBytesAcked); - status.put("wireBytesAcked", benchmarkWireBytesAcked); - status.put("aborted", benchmarkAborted); - return status.toString(); - } catch (org.json.JSONException e) { - return "{\"state\":\"idle\"}"; - } - } - } - - /** Pending + in-flight benchmark no-op messages. */ - private int benchmarkOutstandingLocked() { - int count = 0; - for (OutboundMessage message : pendingMessages) { - if ("bandwidth".equals(message.kind)) count++; - } - for (OutboundMessage message : inFlightMessages) { - if ("bandwidth".equals(message.kind)) count++; - } - return count; - } - - /** - * Keep the benchmark stream fed: top the pending queue up so the send - * window never starves, stop enqueueing once the run expires (or a message - * times out), and finish the run when the last outstanding message drains. - */ - private void maintainBenchmarkLocked(long now) { - if (!sessionReady || !fixedLayoutCreated || shutdownRequested) { - finishBenchmarkLocked(true, "session no longer ready"); - return; - } - if (benchmarkLinkPending || now < benchmarkReadyAtMs) { - return; - } - if (benchmarkAborted || now >= benchmarkDeadlineAtMs) { - // The run is over: drop queued-but-unsent no-ops (sending them - // would stretch the run past its deadline) and finish once the - // in-flight tail has acked or timed out. - Iterator pendingIterator = pendingMessages.iterator(); - while (pendingIterator.hasNext()) { - OutboundMessage queued = pendingIterator.next(); - if ("bandwidth".equals(queued.kind)) { - pendingIterator.remove(); - magicPool.release(queued.sid, queued.magic, queued.label, "benchmark over"); - } - } - if (benchmarkOutstandingLocked() == 0) { - finishBenchmarkLocked(false, "complete"); - } - return; - } - // One more than the window so a fresh message is always ready to write - // the moment an ack frees a slot. - for (int outstanding = benchmarkOutstandingLocked(); outstanding <= benchmarkWindowSize; outstanding++) { - enqueueBenchmarkMessageLocked(); - } - } - - private void finishBenchmarkLocked(boolean aborted, String reason) { - if (!benchmarkActive) { - return; - } - benchmarkActive = false; - benchmarkAborted |= aborted; - // Prefer the last ack as the end time so drain lag after the deadline - // doesn't dilute the throughput figure. - benchmarkEndAtMs = benchmarkLastAckAtMs != 0 ? benchmarkLastAckAtMs : SystemClock.elapsedRealtime(); - logLine("bandwidth benchmark " + (benchmarkAborted ? "aborted" : "finished") + " (" + reason + "): " - + benchmarkMessagesAcked + "/" + benchmarkMessagesSent + " acked, " - + benchmarkPayloadBytesAcked + "B payload, " + benchmarkTimeouts + " timeouts"); - } - - private void enqueueBenchmarkMessageLocked() { - // Fresh bytes per message: the transport's persistent compression history - // would compress even a random payload if we reused it across the run. - byte[] payload = new byte[benchmarkMessageSize]; - benchmarkRandom.nextBytes(payload); - payload[0] = 7; // CFW diagnostic-control mode... - payload[1] = (byte) 0x7f; // ...with an unused sub-op: acked, no effect - OutboundMessage message = messageBuilder.imagePayload( - "bandwidth", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - "bandwidth no-op " + payload.length + "B", - connectionOptions.sendImagesToLeft); - final int payloadBytes = payload.length; - // Logical custom-message bytes; excludes length tag and packet framing. - final int wireBytes = message.message.length; - message.onSent = () -> { - benchmarkMessagesSent++; - if (benchmarkStartAtMs == 0) { - benchmarkStartAtMs = SystemClock.elapsedRealtime(); - benchmarkDeadlineAtMs = benchmarkStartAtMs + benchmarkDurationMs; - } - }; - message.onAck = () -> { - long ackedAtMs = SystemClock.elapsedRealtime(); - benchmarkMessagesAcked++; - benchmarkPayloadBytesAcked += payloadBytes; - benchmarkWireBytesAcked += wireBytes; - benchmarkLastAckAtMs = ackedAtMs; - // No-op payloads ride the image path, so the firmware resets its - // heartbeat timer on them just like real image messages. - lastHeartbeatAckedAtMs = ackedAtMs; - }; - message.onTimeout = () -> { - benchmarkTimeouts++; - benchmarkAborted = true; - logLine("bandwidth benchmark ack timeout"); - }; - pendingMessages.addLast(message); - } - - public void addImuListener(FaceclawImuListener listener) { - if (listener != null) { - imuListeners.add(listener); - } - } - - public void removeImuListener(FaceclawImuListener listener) { - if (listener != null) { - imuListeners.remove(listener); - } - } - - public void addAmbientLightListener(FaceclawAmbientLightListener listener) { - if (listener != null) { - ambientLightListeners.add(listener); - } - } - - public void removeAmbientLightListener(FaceclawAmbientLightListener listener) { - if (listener != null) { - ambientLightListeners.remove(listener); - } - } - - private void emitAmbientLight(byte[] body) { - if (ambientLightListeners.isEmpty()) { - return; - } - byte[] copy = java.util.Arrays.copyOf(body, body.length); - mainHandler.post(() -> { - for (FaceclawAmbientLightListener alsListener : ambientLightListeners) { - try { - alsListener.onAmbientLight(copy); - } catch (Throwable t) { - Log.w(TAG, "ambient light listener failed", t); - } - } - }); - } - - /** Request one CFW ambient-light report (image-handler mode 16 op 0). */ - public void queryAmbientLight() { - synchronized (lock) { - enqueueAmbientLightControlLocked(new byte[] { (byte) 16, (byte) 0 }, "als query", false); - } - interruptibleSleep.interrupt(); - } - - /** - * Start or stop CFW passive light-sensor polling (image-handler mode 16 - * ops 1/2). While polling, the firmware reads its OPT3001 every intervalMs - * (clamped 100..5000 by the firmware), never steps the panel brightness - * itself, and pushes a field-105 report when the reading moved by at least - * minDelta or heartbeatMs elapsed. bindToLease stops polling automatically - * when the Faceclaw framebuffer lease is released or lapses. Starting is - * idempotent and re-opens the sensor if the stock firmware closed it. - */ - public void setAmbientLightPolling(boolean enable, int intervalMs, int minDelta, - int heartbeatMs, boolean bindToLease) { - byte[] payload = enable - ? new byte[] { - (byte) 16, - (byte) 1, - (byte) (bindToLease ? 1 : 0), - (byte) (intervalMs & 0xff), - (byte) ((intervalMs >> 8) & 0xff), - (byte) (minDelta & 0xff), - (byte) ((minDelta >> 8) & 0xff), - (byte) (heartbeatMs & 0xff), - (byte) ((heartbeatMs >> 8) & 0xff), - } - : new byte[] { (byte) 16, (byte) 2 }; - synchronized (lock) { - clearMessagesOfKindLocked("als-control"); - enqueueAmbientLightControlLocked(payload, "als polling " + (enable ? "start" : "stop"), true); - } - interruptibleSleep.interrupt(); - } - - private void enqueueAmbientLightControlLocked(byte[] payload, String label, boolean priority) { - if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) { - logLine("skip " + label + "; display path not ready"); - return; - } - OutboundMessage message = messageBuilder.imagePayload( - "als-control", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - label, - connectionOptions.sendImagesToLeft); - message.onTimeout = () -> logLine(label + " ack timeout"); - if (priority) pendingMessages.addFirst(message); - else pendingMessages.addLast(message); - logLine("queue " + label); - } - - public void addMicStatusListener(FaceclawMicStatusListener listener) { - if (listener != null) { - micStatusListeners.add(listener); - } - } - - public void removeMicStatusListener(FaceclawMicStatusListener listener) { - if (listener != null) { - micStatusListeners.remove(listener); - } - } - - private void emitMicStatus(byte[] body, String address) { - if (micStatusListeners.isEmpty()) { - return; - } - String arm = address.equalsIgnoreCase(leftAddress) ? "L" - : address.equalsIgnoreCase(rightAddress) ? "R" : "?"; - byte[] copy = java.util.Arrays.copyOf(body, body.length); - mainHandler.post(() -> { - for (FaceclawMicStatusListener micListener : micStatusListeners) { - try { - micListener.onMicStatus(copy, arm); - } catch (Throwable t) { - Log.w(TAG, "mic status listener failed", t); - } - } - }); - } - - /** - * Queue a CFW mic_control record (['M','C',ver,op,...]) as a settings - * field-103 write to both temples, or to a single one. Fire-and-forget: - * the firmware answers with a field-104 status notify per temple, which - * arrives through addMicStatusListener. - */ - public void sendFaceclawMicControl(byte[] record, String label, boolean rightTemple, boolean leftTemple) { - if (record == null || record.length < 4) { - return; - } - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip mic control (" + label + "); session not ready"); - return; - } - if (rightTemple) { - pendingMessages.addLast(messageBuilder.faceclawMicControl(record, label, false)); - } - if (leftTemple) { - pendingMessages.addLast(messageBuilder.faceclawMicControl(record, label, true)); - } - logLine("queue mic control " + label); - } - interruptibleSleep.interrupt(); - } - - /** - * Forward render-characteristic audio packets to the listener WITHOUT - * sending the stock EvenHub audio-control enable. Used for the CFW - * mic_control streaming path, where capture is armed through settings - * field 103 and the temples emit 'SM' frames on the same characteristic - * that stock mono LC3 uses. Returns false when no session is up. - */ - public boolean startG2AudioForwarding(FaceclawAudioPacketListener listener) { - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - synchronized (lock) { - if (!running || !sessionReady || shutdownRequested) { - logLine("skip G2 audio forwarding; session not ready"); - return false; - } - audioPacketListener = listener; - audioCaptureActive = true; - logLine("G2 audio forwarding enabled"); - } - return true; - } - - public void stopG2AudioForwarding() { - synchronized (lock) { - audioPacketListener = null; - audioCaptureActive = false; - logLine("G2 audio forwarding disabled"); - } - } - - /** - * Subscribe to compass events. Callbacks are delivered on the Looper of - * the thread that registered (falling back to the main thread), so app - * worker isolates can listen without a cross-thread hop into their JS. - */ - public void addCompassListener(FaceclawCompassListener listener) { - if (listener == null) { - return; - } - Looper looper = Looper.myLooper(); - Handler handler = looper != null ? new Handler(looper) : mainHandler; - compassSubscriptions.add(new CompassSubscription(listener, handler)); - } - - public void removeCompassListener(FaceclawCompassListener listener) { - if (listener == null) { - return; - } - for (CompassSubscription subscription : compassSubscriptions) { - if (subscription.listener == listener) { - compassSubscriptions.remove(subscription); - } - } - } - - - // The most recently started communicator; lets app worker threads submit - // surface frames without holding a cross-isolate reference to the bridge - // object (JS wrappers do not cross isolates, but the Java instance does). - private static volatile FaceclawBleCommunicator activeInstance; - - public static FaceclawBleCommunicator getActive() { - return activeInstance; - } - - /** Set the compositor's output frame size. Call before configuring surfaces. */ - public void configureCompositorScreen(int width, int height) { - compositor.configureScreen(width, height); - } - - /** - * The current composited screen as a phone-UI preview bitmap, or null - * before any surface has been configured. Built from the compositor so - * the preview reflects every surface (chrome + whichever app is - * foreground), including worker-app frames the TS side never sees. - */ - public android.graphics.Bitmap getCompositePreviewBitmap(double brightenGamma) { - return getCompositePreviewBitmap(brightenGamma, false); - } - - /** As above; `green` renders the preview green-on-black (Settings > Phone display > Preview color). */ - public android.graphics.Bitmap getCompositePreviewBitmap(double brightenGamma, boolean green) { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return null; - } - return PreviewBitmapUtil.fromGray( - java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green); - } - - /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ - public String saveCompositePngScreenshot() throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - - /** - * Save the current composite cropped to the given screen rect (the region - * the shell says is actually occupied). The rect is clamped to the screen; - * a degenerate rect falls back to the full screen. - */ - public String saveCompositePngScreenshot(int cropX, int cropY, int cropWidth, int cropHeight) - throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - int x = Math.max(0, cropX); - int y = Math.max(0, cropY); - int width = Math.min(composite.width - x, cropWidth - (x - cropX)); - int height = Math.min(composite.height - y, cropHeight - (y - cropY)); - if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - byte[] cropped = new byte[width * height]; - for (int row = 0; row < height; row++) { - System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width); - } - return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height); - } - - // Active animated-GIF screen recording, or null when idle. Frames are - // pushed by recordScreenFrame(), which the TS side calls at each - // phone-preview flush. - private volatile GifScreenRecorder screenRecorder; - - /** Begin collecting composite frames for an animated-GIF screen recording. */ - public void startScreenRecording() { - screenRecorder = new GifScreenRecorder(); - } - - /** Capture the current composite into the active recording; no-op when idle. */ - public void recordScreenFrame() { - GifScreenRecorder recorder = screenRecorder; - if (recorder == null) { - return; - } - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return; - } - recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()); - } - - /** Finish the recording and save it as an animated GIF; returns the path or "". */ - public String stopScreenRecording() throws java.io.IOException { - GifScreenRecorder recorder = screenRecorder; - screenRecorder = null; - if (recorder == null) { - return ""; - } - if (recorder.isOverflowed()) { - logLine("screen recording hit its frame cap; the tail was dropped"); - } - return recorder.save(appContext); - } - - /** - * Show or hide a compositor surface, immediately submitting the resulting - * frame. Recompositing here (rather than waiting for the next surface - * update) is what makes a just-foregrounded window's retained frame - * actually appear — otherwise a static window (e.g. the terminal hub) whose - * frame landed while briefly hidden would stay blank until its next repaint. - */ - public void setSurfaceVisible(String id, boolean visible) { - // Its own frame: this recomposite is a real screen update with real - // latency, and without one it would show up in other frames' logs only - // as an anonymous "superseded by frame#0". - int frameId = FrameTimings.getInstance().startFrame( - "compositor:visible " + id + "=" + visible); - compositor.setSurfaceVisible(id, visible); - SurfaceCompositor.Composite composite = compositor.composite(); - byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); - storeDesiredComposite(composite, packed, 0, frameId); - } - - /** - * Blank (screen off) or unblank the composited output, immediately - * submitting the resulting frame. Retained surface state is untouched, so - * unblanking restores the previous screen content without repaints. - */ - public void setScreenBlanked(boolean blanked) { - int frameId = FrameTimings.getInstance().startFrame( - "compositor:" + (blanked ? "blank" : "unblank")); - compositor.setBlanked(blanked); - SurfaceCompositor.Composite composite = compositor.composite(); - byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); - storeDesiredComposite(composite, packed, 0, frameId); - } - - /** - * Create or reconfigure a compositor surface. transparency is one of the - * SurfaceCompositor.TRANSPARENCY_* constants. Geometry changes take effect - * when the next frame is submitted. - */ - public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) { - compositor.configureSurface(id, x, y, width, height, zOrder, transparency); - } - - public void removeSurface(String id) { - compositor.removeSurface(id); - } - - /** - * Dim every surface below zOrder belowZOrder to factor256/256 (see - * SurfaceCompositor.setUnderlayDim). Takes effect with the next submitted - * frame: the shell always submits its own surface right after changing - * this, so no recomposite happens here. - */ - public void setUnderlayDim(int belowZOrder, int factor256) { - compositor.setUnderlayDim(belowZOrder, factor256); - } - - /** - * Apply an update to one compositor surface and submit the recomposited - * screen as the desired frame. The update covers the rect (rectX, rectY, - * rectWidth, rectHeight) in surface-local coordinates; contentFingerprint - * identifies the surface's full content after the update. - * - * pixels8bpp arrives as a ByteBuffer because NativeScript marshals a JS - * ArrayBuffer to one without the per-element bridge copy that a byte[] - * parameter would need (~150ms for a full frame). - */ - public void submitSurfaceFrame( - java.nio.ByteBuffer pixels8bpp, - String surfaceId, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - int paintMs, - int frameId - ) { - submitSurfaceFrame(pixels8bpp, surfaceId, rectX, rectY, rectWidth, rectHeight, - contentFingerprint, paintMs, frameId, null); - } - - /** - * As above, with the frame's glyph draws (see SurfaceCompositor's glyph - * overload for the buffer format): the surface's full text content as - * structured draws, letting the texture-cache planner ship glyphs as - * on-glasses cached draws instead of pixels. Null when the submitter has - * no glyph metadata; the pixels alone remain fully correct. - */ - public void submitSurfaceFrame( - java.nio.ByteBuffer pixels8bpp, - String surfaceId, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - int paintMs, - int frameId, - java.nio.ByteBuffer glyphs - ) { - Log.i(TAG, "Received an updated frame for surface " + surfaceId); - FrameTimings.getInstance().log(frameId, "surface " + surfaceId + " updated rect=" - + rectWidth + "x" + rectHeight + "+" + rectX + "+" + rectY - + (glyphs == null ? " (no glyph draws)" : "")); - FrameTimings.getInstance().spanStart(frameId, "composite"); - SurfaceCompositor.Composite composite = compositor.applyAndComposite( - surfaceId, new AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint, - glyphs == null ? null : new AndroidByteReader(glyphs)); - FrameTimings.getInstance().spanEnd(frameId, "composite"); - // Pack the composited 8bpp buffer down to the headerless 4bpp frame - // format the wire planners consume; BMP framing is added later only for - // the uncompressed fallback. - FrameTimings.getInstance().spanStart(frameId, "pack-4bpp"); - byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); - FrameTimings.getInstance().spanEnd(frameId, "pack-4bpp"); - storeDesiredComposite(composite, packed, paintMs, frameId); - } - - /** Store a composite as the desired frame unless a newer one won the race. */ - private void storeDesiredComposite(SurfaceCompositor.Composite composite, byte[] packed, int paintMs, int frameId) { - int supersededFrameId = 0; - boolean stale = false; - synchronized (desiredTilesLock) { - if (composite.seq <= lastStoredCompositeSeq) { - // A concurrent submission composited after us and stored first; - // its composite already includes this surface update. - stale = true; - } else { - lastStoredCompositeSeq = composite.seq; - supersededFrameId = desiredFrameId; - desiredPacked = packed; - desiredWidth = composite.width; - desiredHeight = composite.height; - desiredFingerprint = composite.fingerprint; - desiredPaintMs = paintMs; - desiredFrameId = frameId; - desiredDraws = composite.draws; - } - } - if (stale) { - finishFrame(frameId, "discarded: composite superseded before store"); - return; - } - if (supersededFrameId != 0 && supersededFrameId != frameId) { - finishFrame(supersededFrameId, "discarded: superseded by frame#" + frameId + " before send"); - } - FrameTimings.getInstance().log(frameId, "image submitted as desired frame"); - interruptibleSleep.interrupt(); - } - - /** - * Play a tone sequence via CFW load_image_z mode 5 kind 4. The payload is - * the complete wire buffer ([5][4][nSteps][freqLo,freqHi,duty,msLo,msHi]*n, - * up to 48 steps), built on the TS side; it rides the arbitrary-payload - * image path like the other mode-5 controls. - */ - public void playBuzzerSequence(java.nio.ByteBuffer payload) { - synchronized (lock) { - if (!running || !sessionReady || !fixedLayoutCreated) { - logLine("skip buzzer sequence; session not ready"); - return; - } - byte[] bytes = new byte[payload == null ? 0 : payload.remaining()]; - if (payload != null) { - payload.get(bytes); - } - if (bytes.length < 3) { - logLine("skip buzzer sequence; empty payload"); - return; - } - OutboundMessage message = messageBuilder.imagePayload( - DASHBOARD_TILE, - nextMapSessionId(), - bytes, - "buzzer sequence " + bytes.length + "B", - connectionOptions.sendImagesToLeft - ); - message.onTimeout = () -> { - handleTransportFailure("buzzer sequence ack timeout"); - }; - pendingMessages.addLast(message); - logLine("queue " + message.label); - } - interruptibleSleep.interrupt(); - } - - public boolean sendShutdown(int exitMode) { - return sendShutdownInternal(exitMode, true); - } - - /** - * Send CFW image-handler mode 11 after quiescing normal traffic. A successful - * return means the cleanup was ACKed and no later Faceclaw message should be - * emitted before closing BLE. Unsupported/older CFWs return false so callers - * can use the legacy shutdown-and-lease-release path. - */ - public boolean sendCfwCleanup() { - int magic; - synchronized (lock) { - if (cfwCleanupDelivered) { - return true; - } - if (!customFirmwareDetected || !running || !sessionReady - || shutdownRequested || !fixedLayoutCreated) { - logLine("skip CFW cleanup; mode 11 unavailable or image path not ready"); - return false; - } - - /* Stop auto-renewals, heartbeats, image generation, and control - * retries, then discard everything that has not reached BLE yet. */ - shutdownRequested = true; - lastCfwCleanupAckMagic = 0; - clearPendingMessagesLocked("CFW cleanup requested"); - logLine("quiescing transport for CFW cleanup"); - } - interruptibleSleep.interrupt(); - - /* WINDOW_SIZE can exceed one, so merely appending cleanup would allow it - * to overlap previously-written image fragments. Wait until all of those - * ACK or time out; shutdownRequested prevents the drive loop from adding - * any fresh automatic traffic meanwhile. */ - long drainDeadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS; - synchronized (lock) { - while (running && sessionReady && !inFlightMessages.isEmpty()) { - long remaining = drainDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) break; - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - if (!running || !sessionReady || !inFlightMessages.isEmpty()) { - shutdownRequested = false; - logLine("CFW cleanup could not drain prior traffic"); - return false; - } - - /* External producers are expected to be stopped by the caller, but - * clear once more at the barrier so cleanup is definitely last. */ - clearPendingMessagesLocked("CFW cleanup barrier"); - OutboundMessage message = messageBuilder.cfwCleanup( - DASHBOARD_TILE, - nextMapSessionId(), - connectionOptions.sendImagesToLeft - ); - magic = message.magic; - message.onAck = () -> { - lastCfwCleanupAckMagic = message.magic; - cfwCleanupDelivered = true; - compassMaybeOn = false; - faceclawWakeLeaseEnabled = false; - logLine("CFW cleanup completed"); - }; - message.onTimeout = () -> logLine("CFW cleanup ack timeout"); - pendingMessages.addLast(message); - logLine("queue CFW cleanup"); - } - interruptibleSleep.interrupt(); - - long deadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS; - synchronized (lock) { - while (running - && sessionReady - && lastCfwCleanupAckMagic != magic - && hasPendingOrInflightMagicLocked(magic)) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) break; - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - boolean acked = lastCfwCleanupAckMagic == magic; - if (!acked) shutdownRequested = false; - return acked; - } - } - - /** - * End the EvenHub page while retaining both arm GATT connections and all - * notification subscriptions. A missed ACK is intentionally non-fatal: - * reconnecting Bluetooth here would defeat the power-saving mode. - */ - public boolean suspendEvenHubSession() { - // Ending the plugin task releases the fb lease, which frees the - // on-glasses texture cache; forget it phone-side either way (a lost - // ack may still have taken effect). - synchronized (lock) { - textureCache.reset(); - } - if (sendShutdownInternal(0, false)) { - return true; - } - synchronized (lock) { - // A shutdown ACK can be lost even though the command took effect. - // If the transport remains intentionally quiesced, callers still - // need to remember to run the resume path on the next wake. - return running && sessionReady && shutdownRequested; - } - } - - /** - * Start a fresh EvenHub plugin task on the existing BLE transport, then let - * the session driver create the layout, warm up the image path, and send - * the desired frame. - */ - public boolean resumeEvenHubSession() { - int claimGeneration = 0; - synchronized (lock) { - if (!running || !sessionReady || chargingMode) { - logLine("skip EvenHub resume; transport not ready"); - return false; - } - if (!shutdownRequested) { - return true; - } - if (faceclawWakePendingNonce >= 0 - && hasPendingOrInflightKindLocked("wake-lease-control")) { - claimGeneration = faceclawWakeControlGeneration; - } - logLine("replaying session prelude for EvenHub resume"); - } - - // A custom double-tap wake has only a short unclaimed fail-open - // deadline. Let the worker put CLAIM on both arms before the direct - // prelude write begins. - if (claimGeneration != 0) { - waitForFaceclawWakeControlDelivery(claimGeneration, 500); - } - - try { - // Empty-name Cmd=9 tears down the whole plugin task, not just its - // image container. Re-run the launch prelude before Cmd=0 CREATE. - sendPrelude(true); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - handleTransportFailure("EvenHub resume prelude interrupted"); - return false; - } catch (Throwable t) { - logLine("EvenHub resume prelude failed: " + safeMessage(t)); - handleTransportFailure("EvenHub resume prelude failed"); - return false; - } - - synchronized (lock) { - if (!running || !sessionReady || chargingMode) { - return false; - } - shutdownRequested = false; - fixedLayoutCreated = false; - startupProbePending = false; - audioCaptureActive = false; - clearAllMessagesPreservingWakeLeaseLocked("EvenHub resume"); - displayedFingerprint = ""; - imageRetryAfterMs = 0; - lastHeartbeatSentAtMs = 0; - lastHeartbeatAckedAtMs = 0; - logLine("EvenHub session resume requested"); - } - interruptibleSleep.interrupt(); - return true; - } - - private boolean sendShutdownInternal(int exitMode, boolean reconnectOnTimeout) { - int magic; - long startedAtMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip shutdown; session not ready"); - return false; - } - if (shutdownRequested) { - return true; - } - shutdownRequested = true; - // Magic values wrap, so an ACK from a much older suspend must not - // satisfy this request after enough sleep/wake cycles. - lastShutdownAckMagic = 0; - clearPendingMessagesLocked("shutdown requested"); - OutboundMessage message = messageBuilder.shutdown(exitMode); - magic = message.magic; - message.onAck = () -> { - lastShutdownAckMagic = message.magic; - fixedLayoutCreated = false; - displayedFingerprint = ""; - }; - message.onTimeout = () -> { - if (reconnectOnTimeout) { - handleTransportFailure("shutdown ack timeout"); - } else { - logLine("EvenHub shutdown ack timeout; keeping BLE connected"); - } - }; - pendingMessages.addFirst(message); - logLine("queue shutdown"); - // The stock compass keeps the magnetometer sampling independently of - // the plugin task, so ending the page does not stop it. Force a - // disable ahead of the shutdown command whenever it may be running: - // this also covers a disable that was wiped by the queue flush above - // or whose ack was lost, and the charging-mode/exit paths where the - // Compass window never got a chance to release it. - if (compassMaybeOn && fixedLayoutCreated) { - enqueueCompassControlLocked(true, false); - } - } - interruptibleSleep.interrupt(); - - long ackDeadline = SystemClock.elapsedRealtime() + ConnectionOptions.ACK_TIMEOUT_MS + 500; - synchronized (lock) { - while (running - && sessionReady - && lastShutdownAckMagic != magic - && lastShutdownExitAtMs < startedAtMs - && hasPendingOrInflightMagicLocked(magic)) { - long remaining = ackDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - boolean acked = lastShutdownAckMagic == magic; - if (acked) { - long exitDeadline = SystemClock.elapsedRealtime() + ConnectionOptions.ACK_TIMEOUT_MS + 500; - while (running && sessionReady && lastShutdownExitAtMs < startedAtMs) { - long remaining = exitDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - } - return acked || lastShutdownExitAtMs >= startedAtMs; - } - } - - private boolean waitForAudioControlAck(int magic, String operation) { - long deadline = SystemClock.elapsedRealtime() + ConnectionOptions.ACK_TIMEOUT_MS + ConnectionOptions.WRITE_TIMEOUT_MS + 500; - synchronized (lock) { - while (running && sessionReady && lastAudioControlAckMagic != magic && hasPendingOrInflightMagicLocked(magic)) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - boolean acked = lastAudioControlAckMagic == magic; - if (!acked) { - if ("enable".equals(operation)) { - audioPacketListener = null; - audioCaptureActive = false; - } - logLine("G2 mic " + operation + " ack timeout"); - } - return acked; - } - } - - - @Override public void run() { - logLine(String.format(Locale.US, "communicator start R=%s L=%s ring=%s", rightAddress, leftAddress, ringAddress)); - while (true) { - try { - if (!running) { - Log.w(TAG, "Exiting event looop"); - break; - } - emitPhoneLockStateIfChanged(false); - if (!sessionReady) { - if (reconnectHalted) { - interruptibleSleep.sleep(ConnectionOptions.IDLE_SLEEP_MS); - continue; - } - long now = SystemClock.elapsedRealtime(); - if (now < reconnectAfterMs) { - interruptibleSleep.sleep(Math.min(ConnectionOptions.IDLE_SLEEP_MS, reconnectAfterMs - now)); - continue; - } - Log.w(TAG, "Attempting to connect"); - connectLoopOnce(); - continue; - } - - if (shouldAttemptRingConnect()) { - tryConnectRing("retry"); - continue; - } - - long sleepMs = driveSession(); - if (sleepMs > 0) { - interruptibleSleep.sleep(sleepMs); - } - } catch (Throwable t) { - logLine("communicator loop error: " + safeMessage(t)); - handleTransportFailure("loop error"); - } - } - logLine("communicator stop"); - } - - @Override public void onNotification(String address, String characteristicUuid, byte[] data) { - if (address == null || characteristicUuid == null || data == null) { - return; - } - String uuid = characteristicUuid.toLowerCase(Locale.US); - if (isDirectRingNotification(address, uuid)) { - handleDirectRingNotification(uuid, data); - return; - } - if (BleProtocol.RENDER_NOTIFY_UUID.equals(uuid)) { - handleRenderNotification(address, data); - return; - } - if (!BleProtocol.NOTIFY_CHAR_UUID.equals(uuid)) { - return; - } - if (data.length >= 7 && (data[6] & 255) == CfwTransport.SID) { - CfwTransport.Ack[] acks = CfwTransport.parseAcks(data); - if (acks == null) return; - synchronized (lock) { - lastIncomingAtMs = SystemClock.elapsedRealtime(); - for (CfwTransport.Ack ack : acks) { - for (OutboundMessage message : inFlightMessages) { - String ingress = message.isLeftArmMessage ? leftAddress : rightAddress; - if (message.sid == CfwTransport.SID && address.equalsIgnoreCase(ingress) && message.magic == ack.streamId) { - message.acceptCfwAck(ack); - Log.i(TAG, "CFW " + (ack.nack ? "NACK" : "ACK") + " id=" + ack.streamId - + " ordinal=" + ack.messageId + " lens=" + ack.lens + " txseq=" + (data[2] & 255) - + " redundant=" + (ack != acks[0]) - + " size=" + ack.size + " crc=" + ack.checksum - + " expected=" + message.message.length + "/" + message.cfwChecksum - + " ackedLenses=" + message.cfwAckLenses - + " ageMs=" + (lastIncomingAtMs - message.sentAtMs)); - message.ackPayload = Arrays.copyOf(data, data.length); - break; - } - } - } - drainCfwAcknowledgementsLocked(); - } - interruptibleSleep.interrupt(); - return; - } - Log.d(TAG, "onNotification: address=" + address + " characteristicUuid=" + characteristicUuid + " data.length=" + data.length); - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); - int decodedWearState = BleProtocol.parseWearState(frame); - BleProtocol.CompassEvent compassEvent = address.equalsIgnoreCase(rightAddress) - ? BleProtocol.parseCompassEvent(frame) - : null; - boolean emitWearState = false; - G2Event event = null; - synchronized (lock) { - lastIncomingAtMs = SystemClock.elapsedRealtime(); - if (decodedWearState >= 0 && decodedWearState != wearState) { - wearState = decodedWearState; - emitWearState = true; - } - boolean faceclawWakeNotification = false; - if (shutdownRequested - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING - && address.equalsIgnoreCase(rightAddress)) { - int wakeNonce = BleProtocol.parseFaceclawWakeEvent(frame.pb); - if (wakeNonce >= 0) { - faceclawWakePendingNonce = wakeNonce; - enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_CLAIM, - wakeNonce, - true - ); - faceclawWakeNotification = true; - lastConnectionOrInputAtMs = lastIncomingAtMs; - // The event type says which gesture woke the glasses: TS - // gives a head-up the Glanceboard and a double tap the - // regular UI. The claim handshake is the same for both. - boolean headUp = BleProtocol.parseFaceclawWakeEventCode(frame.pb) - == BleProtocol.FACECLAW_WAKE_EVENT_HEAD_UP; - event = new G2Event( - "display-wake", - "", - headUp ? BleProtocol.EVENT_HEAD_UP : BleProtocol.EVENT_DOUBLE_CLICK, - 0, - 0 - ); - logLine("claimed deferred dashboard wake nonce=" + wakeNonce - + (headUp ? " (head-up)" : "")); - } - } - if (!faceclawWakeNotification - && shutdownRequested - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING - && address.equalsIgnoreCase(rightAddress)) { - // CFW idle-gesture forwarding (firmware revision 2+): with no - // EvenHub page the stock display thread drops taps, long - // presses and releases; the CFW reports them on the settings - // sid while our wake lease is held. Deliver them as the - // sys-events a live page would have produced, so TS treats a - // sleep-time tap or hold exactly like one during soft sleep - // (the Glanceboard). - BleProtocol.FaceclawGestureEvent gesture = BleProtocol.parseFaceclawGestureEvent(frame.pb); - if (gesture != null) { - lastConnectionOrInputAtMs = lastIncomingAtMs; - event = new G2Event("sys-event", "", gesture.eventType, gesture.eventSource, 0); - logLine("idle gesture forwarded by CFW: type=" + gesture.eventType - + " source=" + gesture.eventSource); - } - } - if (!faceclawWakeNotification - && event == null - && shutdownRequested - && address.equalsIgnoreCase(rightAddress) - && BleProtocol.isDisplayWakeStateChange(frame)) { - // With no EvenHub page, ring/arm double-taps are handled by the - // stock display lifecycle and surface only as this state ping. - // Translate it back into an input event so TS can wake the shell - // and recreate the page. - event = new G2Event( - "display-wake", - "", - BleProtocol.EVENT_DOUBLE_CLICK, - 0, - 0 - ); - } - if (!faceclawWakeNotification - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING) { - // Mode-17 query notifications; settings READs are handled by - // createBatteryQueryMessageLocked so headset/ring update together. - if (address.equalsIgnoreCase(rightAddress) - && (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) { - BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(frame.pb); - if (ring != null) { - ringBattery = ring.battery; - ringCharging = ring.charging; - emitBatteryState(headsetBattery, headsetCharging); - } - } - // CFW mic status (field 104) rides both standalone pushes and - // settings read acks, from each temple on its own link. - byte[] micStatus = BleProtocol.parseFaceclawMicStatus(frame.pb); - if (micStatus != null) { - emitMicStatus(micStatus, address); - } - } - if (!faceclawWakeNotification - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING) { - // CFW ambient-light report (field 105) from the master temple. - byte[] alsReport = BleProtocol.parseFaceclawAlsReport(frame.pb); - if (alsReport != null) { - emitAmbientLight(alsReport); - } - } - if (!faceclawWakeNotification - && decodedWearState < 0 - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING) { - // Device-initiated settings push. It carries a magic the glasses - // chose, so it would otherwise fall through to resolveAckLocked, - // match nothing, and be logged as an unexpected ack. - int pushedSilentMode = BleProtocol.parseSilentModePush(frame.pb); - if (pushedSilentMode >= 0) { - updateSilentModeLocked(pushedSilentMode > 0); - return; - } - } - if (!faceclawWakeNotification - && decodedWearState < 0 - && frame.ok - && frame.msgSeq >= 0 - && frame.flag != BleProtocol.FLAG_NOTIFY - && frame.flag != BleProtocol.FLAG_NOTIFY_ALT) { - lastAckAtMs = lastIncomingAtMs; - resolveAckLocked(frame.sid, frame.msgSeq, frame.pb); - } - if (!faceclawWakeNotification - && event == null - && frame.ok - && address.equalsIgnoreCase(rightAddress) - && (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) { - event = G2Event.decode(frame); - if (event != null - && "sys-event".equals(event.kind) - && event.eventType == BleProtocol.EVENT_HEAD_UP) { - // CFW forwards the IMU head-up while our page is on screen - // (soft sleep). Surface it as the same wake-only input the - // deferred head-up wake produces from a dark display. - event = new G2Event("display-wake", "", BleProtocol.EVENT_HEAD_UP, event.eventSource, 0); - logLine("head-up forwarded by CFW while page on screen"); - } - if (event != null) { - // Pure IMU samples arrive continuously; don't let them count - // as user input (which would starve battery polling). - boolean pureImuSample = "sys-event".equals(event.kind) - && event.eventType == BleProtocol.EVENT_IMU_DATA_REPORT; - if (!pureImuSample) { - lastConnectionOrInputAtMs = lastIncomingAtMs; - } - if ("list-click".equals(event.kind) || "text-click".equals(event.kind)) { - // Container-routed touchpad input reached us, so the - // firmware is dispatching input: silent mode is off, - // whether or not its end-of-silent push arrived. - updateSilentModeLocked(false); - } - if ("sys-event".equals(event.kind)) { - if (event.eventType == BleProtocol.EVENT_FOREGROUND_EXIT || event.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || event.eventType == BleProtocol.EVENT_SYSTEM_EXIT) { - if (shutdownRequested) { - lastShutdownExitAtMs = SystemClock.elapsedRealtime(); - } - fixedLayoutCreated = false; - displayedFingerprint = ""; - clearAllMessagesLocked("firmware exit event"); - } - } - } - } - } - interruptibleSleep.interrupt(); - if (emitWearState) { - logLine(decodedWearState > 0 ? "wear state ON_HEAD" : "wear state OFF_HEAD"); - emitWearState(decodedWearState > 0); - } - if (compassEvent != null) { - emitCompassEvent(compassEvent); - } - if (event != null) { - if (event.hasImu) { - emitImuData(event.imuX, event.imuY, event.imuZ, event.eventSource); - } - // A standalone IMU_DATA_REPORT is a sensor sample, not a gesture: - // deliver it only to IMU listeners, skipping the input pipeline (and - // its per-frame latency bookkeeping) to avoid flooding it. - boolean pureImuSample = "sys-event".equals(event.kind) - && event.eventType == BleProtocol.EVENT_IMU_DATA_REPORT; - if (!pureImuSample) { - int frameId = FrameTimings.getInstance().startFrame( - "input:" + event.kind + " type=" + event.eventType + " src=" + event.eventSource); - FrameTimings.getInstance().log(frameId, "input event decoded from BLE notification"); - emitRingEvent(event, frameId); - } - } - } - - private void handleDirectRingNotification(String characteristicUuid, byte[] data) { - FaceclawRingEventDecoder.DirectRingEvent decoded = FaceclawRingEventDecoder.decode(data); - if (decoded == null) { - Log.d(TAG, "direct ring notify ignored: characteristicUuid=" + characteristicUuid + " raw=" + hex(data)); - return; - } - - G2Event event = decoded.event; - long arrivalMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - lastIncomingAtMs = arrivalMs; - lastConnectionOrInputAtMs = arrivalMs; - } - logLine("direct ring " + decoded.label + " " + decoded.detail + " raw=" + hex(data)); - int frameId = FrameTimings.getInstance().startFrame("input:ring:" + decoded.label); - FrameTimings.getInstance().log(frameId, "input event decoded from direct ring notification"); - emitRingEvent(event, frameId); - interruptibleSleep.interrupt(); - } - - private void handleRenderNotification(String address, byte[] data) { - FaceclawAudioPacketListener listenerToCall; - long arrivalMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - lastIncomingAtMs = arrivalMs; - listenerToCall = audioCaptureActive ? audioPacketListener : null; - } - if (listenerToCall == null) { - return; - } - String arm = address.equalsIgnoreCase(leftAddress) ? "L" : address.equalsIgnoreCase(rightAddress) ? "R" : "?"; - try { - listenerToCall.onAudioPacket(Arrays.copyOf(data, data.length), arm, arrivalMs); - } catch (Throwable t) { - logLine("G2 mic packet listener failed: " + safeMessage(t)); - } - } - - @Override public void onConnectionStateChange(String address, boolean connected) { - synchronized (lock) { - if (address == null) { - return; - } - if (isConfiguredRingAddress(address)) { - ringConnected = connected; - ringNotificationsReady = false; - if (!connected) { - ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS; - } - logLine(connected ? "direct ring BLE connected" : "direct ring BLE disconnected"); - return; - } - if (address.equalsIgnoreCase(rightAddress)) { - rightConnected = connected; - } else if (address.equalsIgnoreCase(leftAddress)) { - leftConnected = connected; - } else { - return; - } - if (!connected) { - sessionReady = false; - fixedLayoutCreated = false; - startupProbePending = false; - chargingMode = false; - audioCaptureActive = false; - audioPacketListener = null; - clearAllMessagesLocked("connection lost"); - displayedFingerprint = ""; - if (!reconnectHalted) { - reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; - } - } - } - interruptibleSleep.interrupt(); - if (connected) { - setStateDisplay("connected", "Connected."); - } else if (!reconnectHalted) { - // While parked on a missing bond, keep the "unpaired" display: this - // callback is just the teardown of the arm that did connect. - setStateDisplay("connecting", "Connecting to the glasses..."); - } - } - - private void connectLoopOnce() throws InterruptedException { - setStateDisplay("connecting", "Connecting to the glasses..."); - try { - connectArm(rightAddress, true); - connectArm(leftAddress, true); - if (!sleepDuringConnectSettling(800)) { - return; - } - authenticateArms(); - sendPrelude(); - - synchronized (lock) { - sessionReady = true; - // A fresh transport prelude always starts an active EvenHub - // lifecycle, even if the previous connection dropped while - // its page was intentionally suspended. - shutdownRequested = false; - fixedLayoutCreated = false; - clearAllMessagesLocked("session ready"); - displayedFingerprint = ""; - lastAckAtMs = SystemClock.elapsedRealtime(); - lastIncomingAtMs = lastAckAtMs; - lastConnectionOrInputAtMs = lastAckAtMs; - lastSessionReadyAtMs = lastAckAtMs; - lastBatteryRefreshAtMs = 0; - imageRetryAfterMs = 0; - lastHeartbeatSentAtMs = 0; - lastHeartbeatAckedAtMs = 0; - consecutiveAckTimeouts = 0; - lastAudioControlAckMagic = 0; - audioCaptureActive = false; - faceclawWakePendingNonce = -1; - cfwCleanupDelivered = false; - for (CfwTransport transport : cfwTransports) transport.reset(); - lastCfwCleanupAckMagic = 0; - lastFaceclawWakeLeaseQueuedAtMs = 0; - lastFaceclawFramebufferLeaseQueuedAtMs = 0; - enqueueFaceclawFramebufferControlLocked( - BleProtocol.FACECLAW_FB_OP_ACQUIRE, - true - ); - if (faceclawWakeLeaseEnabled) { - enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_ACQUIRE, - 0, - true - ); - } - } - setStateDisplay("connected", "Connected."); - logLine("session ready"); - synchronized (lock) { - // Query settings promptly on the first session so firmware - // version/extension (and battery) arrive without waiting for - // the input-quiet battery poll. The settings response doubles as - // the firmware-compatibility check surfaced during onboarding. - if (!firmwareInfoQueried) { - firmwareInfoQueried = true; - lastBatteryRefreshAtMs = SystemClock.elapsedRealtime(); - pendingMessages.addLast(createBatteryQueryMessageLocked()); - logLine("queue settings query for firmware info"); - } - } - tryConnectRing("initial"); - } catch (Throwable t) { - logLine("connect failed: " + safeMessage(t)); - String unpairedArm = firstUnpairedArm(); - if (unpairedArm != null) { - handleUnpairedFailure(unpairedArm); - } else { - handleTransportFailure("connect failed"); - } - } - } - - private boolean sleepDuringConnectSettling(long delayMs) throws InterruptedException { - long deadline = SystemClock.elapsedRealtime() + delayMs; - synchronized (lock) { - while (running && !userDisconnectRequested) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return true; - } - lock.wait(Math.min(remaining, 100)); - } - return false; - } - } - - private void connectArm(String address, boolean enableRenderNotify) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("connect failed: " + address); - } - // requestConnectionPriority has no callback in this Android compile target, so there is - // no reliable completion point to keep it in the global GATT operation pipeline. But it's - // important enough for performance that we call it anyways. - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("discoverServices failed: " + address); - } - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - throw new IllegalStateException("enableNotifications failed: " + address + " " + BleProtocol.NOTIFY_CHAR_UUID); - } - if (enableRenderNotify) { - bleManager.enableNotifications(address, BleProtocol.RENDER_NOTIFY_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS); - } - synchronized (lock) { - if (address.equalsIgnoreCase(rightAddress)) { - rightConnected = true; - } else if (address.equalsIgnoreCase(leftAddress)) { - leftConnected = true; - } - } - } - - private boolean shouldAttemptRingConnect() { - if (!hasRingAddress()) { - return false; - } - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - return running - && sessionReady - && !ringNotificationsReady - && now >= ringReconnectAfterMs - && pendingMessages.isEmpty() - && inFlightMessages.isEmpty(); - } - } - - private void tryConnectRing(String reason) { - if (!hasRingAddress()) { - return; - } - try { - connectRing(); - } catch (Throwable t) { - synchronized (lock) { - ringConnected = false; - ringNotificationsReady = false; - ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS; - } - logLine("direct ring connect failed (" + reason + "): " + safeMessage(t)); - } - } - - private void connectRing() { - logLine("connecting direct ring " + ringAddress); - if (!bleManager.connect(ringAddress, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("connect failed: " + ringAddress); - } - - bleManager.requestConnectionPriority(ringAddress, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(ringAddress, ConnectionOptions.RING_DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - - if (!bleManager.discoverServices(ringAddress, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("discoverServices failed: " + ringAddress); - } - - boolean phoneNotify = enableRingNotification(BleProtocol.R1_PHONE_NOTIFY_CHAR_UUID); - boolean dataNotify = enableRingNotification(BleProtocol.R1_NOTIFY_CHAR_UUID); - if (!phoneNotify && !dataNotify) { - throw new IllegalStateException("no R1 notify characteristic subscribed"); - } - - synchronized (lock) { - ringConnected = true; - ringNotificationsReady = true; - ringReconnectAfterMs = 0; - } - logLine("direct ring ready phoneNotify=" + phoneNotify + " dataNotify=" + dataNotify); - } - - private boolean enableRingNotification(String characteristicUuid) { - try { - return bleManager.enableNotifications( - ringAddress, - characteristicUuid, - true, - ConnectionOptions.DESCRIPTOR_TIMEOUT_MS - ); - } catch (Throwable t) { - Log.d(TAG, "direct ring notify subscribe skipped: " + characteristicUuid + " " + safeMessage(t)); - return false; - } - } - - /** - * Complete the sid-0x80 security-auth exchange on both freshly opened arm - * connections. Firmware 2.2.9 answers no queries until it completes over an - * encrypted link and closes unauthenticated links after ~30 s (see - * ../notes/ble-connections-2.2.9.md); on an unbonded phone the exchange is - * also what triggers SMP pairing. Deliberately soft: on timeout we log and - * continue rather than fail the connect — the custom firmware's response - * behavior is not yet hardware-verified, and on stock firmware an - * unanswered auth just means the prelude fails exactly as it did before. - * A pairing prompt accepted after our window still bonds at the OS level, - * so the next reconnect attempt authenticates promptly. - */ - private void authenticateArms() throws InterruptedException { - OutboundMessage right = messageBuilder.securityAuth(false); - OutboundMessage left = messageBuilder.securityAuth(true); - long now = SystemClock.elapsedRealtime(); - for (OutboundMessage message : new OutboundMessage[] {right, left}) { - message.onAck = () -> { - }; - message.onTimeout = () -> { - }; - message.sentAtMs = now; - writeMessage(message); - } - long deadline = SystemClock.elapsedRealtime() + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS; - while (running && !userDisconnectRequested && !inFlightMessages.isEmpty()) { - synchronized (lock) { - if (!running || userDisconnectRequested || inFlightMessages.isEmpty()) { - break; - } - } - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - interruptibleSleep.sleep(Math.min(remaining, 100)); - } - synchronized (lock) { - if (!inFlightMessages.isEmpty()) { - clearInFlightMessagesLocked("security auth timeout"); - logLine("security auth not acknowledged; continuing (2.2.9 stock requires it; older/custom firmware may not answer)"); - return; - } - } - boolean rightOk = BleProtocol.isAuthenticationSuccess(right.ackPayload, right.magic); - boolean leftOk = BleProtocol.isAuthenticationSuccess(left.ackPayload, left.magic); - logLine("security auth R=" + (rightOk ? "ok" : "unconfirmed") + " L=" + (leftOk ? "ok" : "unconfirmed")); - } - - private void sendPrelude() throws InterruptedException { - sendPrelude(false); - } - - private void sendPrelude(boolean preserveWakeLeaseControls) throws InterruptedException { - synchronized (lock) { - if (preserveWakeLeaseControls) { - clearAllMessagesPreservingWakeLeaseLocked("prelude"); - } else { - clearAllMessagesLocked("prelude"); - } - } - long now = SystemClock.elapsedRealtime(); - OutboundMessage prelude = messageBuilder.prelude(); - prelude.onAck = () -> { - }; - prelude.onTimeout = () -> { - handleTransportFailure("ack timeout"); - }; - prelude.sentAtMs = now; - writeMessage(prelude); - - long deadline = SystemClock.elapsedRealtime() + ConnectionOptions.PRELUDE_TIMEOUT_MS; - while (running && !userDisconnectRequested && !inFlightMessages.isEmpty()) { - synchronized (lock) { - if (!running || userDisconnectRequested || inFlightMessages.isEmpty()) { - break; - } - } - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - interruptibleSleep.sleep(Math.min(remaining, 100)); - } - synchronized (lock) { - if (!inFlightMessages.isEmpty()) { - clearInFlightMessagesLocked("prelude timeout"); - throw new IllegalStateException("prelude ack timeout"); - } - } - } - - private long driveSession() { - //Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")"); - while (true) { - // Run on the sender thread, outside `lock`: Bluetooth API calls can - // wait on the global GATT lock, and callbacks need the session lock. - // Keep negotiation outside the timed stream. A fixed settling period - // is only an experiment boundary; HCI must confirm actual parameters. - int linkMode = -1; - String[] benchmarkAddresses = null; - synchronized (lock) { - if (benchmarkActive && benchmarkLinkPending) { - benchmarkLinkPending = false; - linkMode = benchmarkLinkMode; - benchmarkAddresses = new String[] {leftAddress, rightAddress}; - } - } - if (benchmarkAddresses != null) { - for (String address : benchmarkAddresses) { - try { - bleManager.prepareBenchmarkLink(address, linkMode); - } catch (RuntimeException error) { - logLine("benchmark link request failed: " + safeMessage(error)); - } - } - synchronized (lock) { - // Cancellation/new-run races leave the new run pending; its - // own preparation will replace this deadline before sending. - benchmarkReadyAtMs = SystemClock.elapsedRealtime() + 1_000; - } - } - OutboundMessage messageToWrite = null; - OutboundMessage messageToPrewrite = null; - long now = SystemClock.elapsedRealtime(); - - maybeFinishNoChangeDesiredFrame(); - - synchronized (lock) { - drainCfwAcknowledgementsLocked(); - if (cfwCleanupDelivered) { - /* Successful mode 11 must be the last Faceclaw write. Drop - * anything a late external producer attempted to enqueue - * while DashboardController was closing the transport. */ - clearPendingMessagesLocked("after CFW cleanup"); - return 250; - } - if (sessionReady - && now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS - && !hasPendingOrInflightKindLocked("framebuffer-lease-control")) { - enqueueFaceclawFramebufferControlLocked( - BleProtocol.FACECLAW_FB_OP_ACQUIRE, - false - ); - } - if (faceclawWakeLeaseEnabled - && sessionReady - && now - lastFaceclawWakeLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS - && !hasPendingOrInflightKindLocked("wake-lease-control")) { - enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_ACQUIRE, - 0, - false - ); - } - if (!inFlightMessages.isEmpty()) { - OutboundMessage oldest = inFlightMessages.peekFirst(); - List replay = CfwMessageWindow.replayWindow(inFlightMessages, now); - if (!replay.isEmpty()) { - logLine("CFW recovery: replay " + replay.size() - + " unresolved message(s) from id=" + replay.get(0).magic); - for (OutboundMessage candidate : replay) { - if (candidate.cfwRetries >= CfwMessageWindow.MAX_RETRIES) { - handleTransportFailure("CFW recovery retry limit"); - return 0; - } - logLine("CFW replay id=" + candidate.magic + " label=" + candidate.label - + " cause=" + (candidate.cfwRetryPending ? "NACK" - : candidate.cfwAckLenses == CfwTransport.BOTH ? "ordered tail" : "missing ACK") - + " ackedLenses=" + candidate.cfwAckLenses - + " ageMs=" + (now - candidate.sentAtMs)); - inFlightMessages.remove(candidate); - magicPool.release(candidate.sid, candidate.magic, candidate.label, "CFW replay"); - candidate.prepareCfwReplay(magicPool.allocate()); - } - for (int i = replay.size() - 1; i >= 0; --i) pendingMessages.addFirst(replay.get(i)); - return 0; - } - if (oldest != null && oldest.ackDeadlineAtMs <= now) { - Log.i(TAG, "message timed out: " + oldest.label); - inFlightMessages.removeFirst(); - logLine("message timed out: " + oldest.label + " sid=" + oldest.sid - + " id=" + oldest.magic + " ackedLenses=" + oldest.cfwAckLenses - + " ageMs=" + (now - oldest.sentAtMs)); - magicPool.release(oldest.sid, oldest.magic, oldest.label, "timeout"); - if (oldest.sid == CfwTransport.SID) - cfwTransports[oldest.isLeftArmMessage ? 0 : 1].reset(); - handleAckTimeoutLocked(oldest); - return 0; - } - if (connectionOptions.WINDOW_SIZE <= 1 - && sessionReady - && prewrittenMessage == null - && !pendingMessages.isEmpty() - && canPrewriteCandidate(pendingMessages.peekFirst()) - && !shouldBlockPrewriteForHeartbeatLocked(now)) { - // Only the serial (window==1) path pre-sends the all-but-last - // packet; with a real window we just send the next message fully. - messageToPrewrite = pendingMessages.peekFirst(); - } - } - - if (chargingMode) { - // Glasses are in the case: no display traffic, only battery - // polls (which also detect the end of charging). - finishDesiredFrameLocked("discarded: glasses charging"); - if (sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) { - messageToWrite = pendingMessages.removeFirst(); - Log.i(TAG, "sending pending message (charging): " + messageToWrite.label); - } else if (sessionReady && pendingMessages.isEmpty() && inFlightMessages.isEmpty() - && now - lastBatteryRefreshAtMs >= ConnectionOptions.CHARGING_BATTERY_POLL_MS) { - Log.i(TAG, "Writing charging-mode battery poll"); - messageToWrite = createBatteryQueryMessageLocked(); - lastBatteryRefreshAtMs = now; - } else { - return 1_000; - } - } else { - if (messageToPrewrite == null - && !shutdownRequested - && !fixedLayoutCreated - && pendingMessages.isEmpty() - && inFlightMessages.isEmpty()) { - Log.i(TAG, "enqueueing create layout"); - enqueueCreateLayoutLocked(); - } else if (messageToPrewrite != null) { - // Prewrite outside the lock; the logical message remains pending until - // its final BLE frame is sent after the current protocol ACK. - } - if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated - && (firmwareDebugFlagsEnabled ? 2 : 1) != firmwareDebugFlagsLastSent - && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) { - Log.i(TAG, "enqueueing firmware debug flags " + (firmwareDebugFlagsEnabled ? "show" : "hide")); - enqueueFirmwareDebugFlagsLocked(); - } - - if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated - && (compassOwners.isEmpty() ? 0 : 1) != compassControlLastSent - && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) { - boolean wanted = !compassOwners.isEmpty(); - Log.i(TAG, "enqueueing compass " + (wanted ? "enable" : "disable")); - enqueueCompassControlLocked(false, wanted); - } - - if (benchmarkActive) { - maintainBenchmarkLocked(now); - } - - // Up to WINDOW_SIZE messages may be in flight at once (full - // pipelining); a slot frees when an ack arrives. An active - // bandwidth benchmark measures its own selected window instead. - boolean windowHasRoom = inFlightMessages.size() - < Math.max(1, benchmarkActive ? benchmarkWindowSize : connectionOptions.WINDOW_SIZE); - // A frame ready to send right now: don't inject a fresh - // heartbeat in front of it (the image's own ack resets the - // firmware heartbeat timer, so the heartbeat is redundant). - // "Ready" covers both a fresh composite still to be planned - // and a planned image already queued but not yet written -- - // enqueueing sets lastEnqueuedFingerprint, so testing only - // the desired fingerprint left the queued-but-unwritten - // window unprotected and a heartbeat that came due there - // cost the frame a full ack round trip (measured 124ms on - // frame#232 of the 2026-08-20 02:27 capture). - // A benchmark run counts as image traffic here for the same - // reason: its acks reset the firmware heartbeat timer, so a - // fresh heartbeat in front of it is redundant. - boolean imageWaiting = !shutdownRequested && fixedLayoutCreated - && !hasPendingOrInflightKindLocked("heartbeat") - && (benchmarkActive - || (now >= imageRetryAfterMs - && (hasPendingImageLocked() - || !getDesiredFingerprint().equals(lastEnqueuedFingerprint)))); - noteImageStallLocked(now, windowHasRoom); - if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) { - return ConnectionOptions.IDLE_SLEEP_MS; - } - - if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()) { - messageToWrite = pendingMessages.removeFirst(); - Log.i(TAG, "sending pending message: " + messageToWrite.label); - } else if (messageToPrewrite == null && !shutdownRequested && !benchmarkActive - && fixedLayoutCreated - && windowHasRoom && !hasPendingImageLocked() - && now >= imageRetryAfterMs - && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) { - // Enqueue the next frame's delta against lastEnqueuedPacked - // (what the shadow will be), so it can pipeline behind an - // image still awaiting its ack. - Log.i(TAG, "Enqueued image update"); - enqueueDesiredImageLocked(); - return 0; - } else if (messageToPrewrite == null && shouldPollBatteryLocked(now)) { - Log.i(TAG, "Writing battery query"); - messageToWrite = createBatteryQueryMessageLocked(); - lastBatteryRefreshAtMs = now; - } else if (messageToPrewrite == null && (!pendingMessages.isEmpty() || !inFlightMessages.isEmpty())) { - return ConnectionOptions.IDLE_SLEEP_MS; - } else if (messageToPrewrite == null) { - return 250; - } - } - } - - if (messageToPrewrite != null) { - if (prewriteMessage(messageToPrewrite)) { - return 0; - } - return ConnectionOptions.IDLE_SLEEP_MS; - } - - if (!writeMessage(messageToWrite)) { - synchronized (lock) { - if (removePreparedMessageLocked(messageToWrite) || messageToWrite.magic == 0) { - handleTransportFailure("write failed"); - } - } - return 0; - } - } - } - - /** - * If the desired image already matches what the glasses display, nothing will - * ever be enqueued for it, so finish its frame now (otherwise the TS side - * would block on it until its backpressure timeout). - */ - private void maybeFinishNoChangeDesiredFrame() { - int frameIdToFinish = 0; - synchronized (lock) { - synchronized (desiredTilesLock) { - if (desiredFrameId != 0 && !lastEnqueuedFingerprint.isEmpty() && desiredFingerprint.equals(lastEnqueuedFingerprint)) { - frameIdToFinish = desiredFrameId; - desiredFrameId = 0; - } - } - } - if (frameIdToFinish != 0) { - finishFrame(frameIdToFinish, "discarded: no change from displayed image"); - } - } - - private boolean shouldBlockPrewriteForHeartbeatLocked(long now) { - if (shutdownRequested || !fixedLayoutCreated) { - return false; - } - return hasPendingOrInflightKindLocked("heartbeat") - || now - lastHeartbeatAckedAtMs >= ConnectionOptions.HEARTBEAT_READY_MS; - } - - private boolean canPrewriteCandidate(OutboundMessage message) { - if (message == null || message.sid == CfwTransport.SID || !message.isLeftArmMessage) { - return false; - } - if (!"image".equals(message.kind)) { - return false; - } - return message.message.length + 2 > 232; - } - - private boolean handleHeartbeat(boolean imageWaiting) { - long now = SystemClock.elapsedRealtime(); - boolean heartbeatEligible = !shutdownRequested && fixedLayoutCreated; - boolean heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat"); - long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs; - boolean heartbeatReady = heartbeatEligible && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS; - boolean heartbeatUrgent = heartbeatEligible && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_URGENT_MS; - boolean heartbeatBlocksLeftWrites = heartbeatReady || heartbeatPending; - - if (heartbeatReady && !heartbeatPending && inFlightMessages.isEmpty()) { - if (imageWaiting && !heartbeatUrgent) { - // Defer to the waiting frame: sending it now satisfies the - // firmware heartbeat deadline (its ack resets the timer), and - // the heartbeat still fires once we reach the URGENT threshold - // if rendering goes quiet again. Preserves the pending-heartbeat - // inter-lens-sync invariant below (that path is untouched). - return false; - } - Log.i(TAG, "Writing heartbeat"); - OutboundMessage heartbeatMessage = createHeartbeatMessage(); - lastHeartbeatSentAtMs = now; - writeMessage(heartbeatMessage); - return true; - } else if (heartbeatUrgent) { - return true; - } else if (heartbeatPending) { - // Don't send other message types while a heartbeat is pending because that - // can lead to inter-lens sync issues - return true; - } - - return false; - } - - private OutboundMessage createHeartbeatMessage() { - OutboundMessage message = messageBuilder.heartbeat(); - message.onAck = () -> { - synchronized (lock) { - lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime(); - } - }; - message.onTimeout = () -> { - // If a heartbeat fails to ack and we're over the heartbeat deadline, assume the connection is failed and reconnect. - // Otherwise ignore it, which will cause a retransmission attempt. - boolean isPastDeadline; - synchronized (lock) { - isPastDeadline = SystemClock.elapsedRealtime() - lastHeartbeatSentAtMs >= ConnectionOptions.HEARTBEAT_FAILURE_DEADLINE_MS; - } - if (isPastDeadline) { - handleTransportFailure("heartbeat ack timeout"); - } - }; - return message; - } - - - private boolean writeMessage(OutboundMessage message) { - long now = SystemClock.elapsedRealtime(); - message.writeStartedAtMs = now; - message.sentAtMs = now; - message.ackDeadlineAtMs = now + message.ackTimeoutMs + ConnectionOptions.WRITE_TIMEOUT_MS; - if (message.magic != 0) { - synchronized (lock) { - inFlightMessages.addLast(message); - } - } - if (message.imageUpdateId > 0 && message.imageMessageNumber == 1) { - synchronized(lock) { - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId); - if (stats != null && stats.firstWriteStartedAtMs <= 0) { - stats.firstWriteStartedAtMs = now; - } - } - } - - String writeAddress = message.isLeftArmMessage ? leftAddress : rightAddress; - List frames; - if (prewrittenMessage != null && prewrittenMessage != message) { - if (!spoilPrewrittenMessage("before " + message.label)) { - return false; - } - } - if (prewrittenMessage == message) { - frames = Collections.singletonList(prewrittenFrames.get(prewrittenFrames.size() - 1)); - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - } else if (message.sid == CfwTransport.SID) { - if (message.cfwRetries > 0) cfwTransports[message.isLeftArmMessage ? 0 : 1].reset(); - frames = cfwTransports[message.isLeftArmMessage ? 0 : 1].encode( - message.message, message.magic, CfwTransport.BOTH, - bleManager.getNegotiatedMtu(writeAddress)); - } else { - frames = BleProtocol.framePb( - message.message, - message.sid, - message.flag, - nextTransportSeq++ - ); - } - boolean result = bleManager.writeFrames( - writeAddress, - BleProtocol.WRITE_CHAR_UUID, - frames, - AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), - ConnectionOptions.WRITE_TIMEOUT_MS - ); - - synchronized (lock) { - long sentAtMs = SystemClock.elapsedRealtime(); - message.sentAtMs = sentAtMs; - message.ackDeadlineAtMs = sentAtMs + message.ackTimeoutMs; - logImageUpdateSendLandmarkLocked(message); - if (result && message.onSent != null) { - message.onSent.run(); - lock.notifyAll(); - } - } - - if (!result && message.sid == CfwTransport.SID) - cfwTransports[message.isLeftArmMessage ? 0 : 1].reset(); - return result; - } - - private boolean prewriteMessage(OutboundMessage message) { - if (prewrittenMessage == message) { - return true; - } - if (prewrittenMessage != null && !spoilPrewrittenMessage("before prewrite " + message.label)) { - return false; - } - if (!canPrewriteCandidate(message)) { - return false; - } - - List frames = BleProtocol.framePb( - message.message, - message.sid, - message.flag, - nextTransportSeq++ - ); - if (frames.size() <= 1) { - return false; - } - - String writeAddress = message.isLeftArmMessage ? leftAddress : rightAddress; - List prefixFrames = frames.subList(0, frames.size() - 1); - boolean result = bleManager.writeFrames( - writeAddress, - BleProtocol.WRITE_CHAR_UUID, - prefixFrames, - AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), - ConnectionOptions.WRITE_TIMEOUT_MS - ); - if (!result) { - return false; - } - - prewrittenMessage = message; - prewrittenFrames = new ArrayList<>(frames); - logLine("prewrote " + message.label + " frames=" + prefixFrames.size() + "/" + frames.size()); - return true; - } - - private boolean spoilPrewrittenMessage(String reason) { - if (prewrittenMessage == null || prewrittenFrames.isEmpty()) { - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - return true; - } - OutboundMessage message = prewrittenMessage; - byte[] finalFrame = Arrays.copyOf( - prewrittenFrames.get(prewrittenFrames.size() - 1), - prewrittenFrames.get(prewrittenFrames.size() - 1).length - ); - if (finalFrame.length > 8) { - finalFrame[finalFrame.length - 1] ^= (byte) 0xff; - } - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - - String writeAddress = message.isLeftArmMessage ? leftAddress : rightAddress; - logLine("spoiling prewritten " + message.label + ": " + reason); - return bleManager.writeFrames( - writeAddress, - BleProtocol.WRITE_CHAR_UUID, - Collections.singletonList(finalFrame), - AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), - ConnectionOptions.WRITE_TIMEOUT_MS - ); - } - - private boolean removePreparedMessageLocked(OutboundMessage message) { - if (message == null || message.magic == 0) { - return false; - } - Iterator iterator = inFlightMessages.iterator(); - while (iterator.hasNext()) { - if (iterator.next() == message) { - iterator.remove(); - magicPool.release(message.sid, message.magic, message.label, "write failed"); - return true; - } - } - return false; - } - - - /** Also drain on the worker: a clear/timeout may have removed a blocking - * head since the last notification. Fully ACKed controls must never reach - * the generic timeout path just because no further BLE reply arrived. */ - private void drainCfwAcknowledgementsLocked() { - while (true) { - OutboundMessage first = CfwMessageWindow.acknowledgedHead(inFlightMessages); - if (first == null) return; - lastAckAtMs = SystemClock.elapsedRealtime(); - resolveAckLocked(first, first.ackPayload); - } - } - - private void resolveAckLocked(int sid, int magic, byte[] pb) { - Iterator iterator = inFlightMessages.iterator(); - while (iterator.hasNext()) { - OutboundMessage message = iterator.next(); - if (message.sid == sid && message.magic == magic) { - resolveAckLocked(message, pb); - return; - } - } - recordUnexpectedAckLocked(sid, magic); - } - - private void resolveAckLocked(OutboundMessage message, byte[] pb) { - Log.i(TAG, "Got ACK for " + message.label + "(sid=" + message.sid + ", id=" + message.magic + ")"); - inFlightMessages.remove(message); - message.ackPayload = pb == null ? new byte[0] : Arrays.copyOf(pb, pb.length); - magicPool.release(message.sid, message.magic, message.label, "ack"); - if (message.onAck != null) { - message.onAck.run(); - } - consecutiveAckTimeouts = 0; - // onAck may have just satisfied a waiter blocked on lock.wait() (e.g. - // awaitEvenHubSessionReady polling fixedLayoutCreated/displayedFingerprint - // after a create-layout or image ack). Without this, that waiter only - // notices on its own up-to-100ms poll tick, adding avoidable latency to - // every EvenHub wake. Always called with lock held (see call site). - lock.notifyAll(); - } - - private void logImageUpdateSendLandmarkLocked(OutboundMessage message) { - if (message.imageUpdateId <= 0) { - return; - } - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId); - int frameId = stats == null ? 0 : stats.frameId; - if (message.imageMessageNumber == 1) { - if (stats != null && stats.firstWriteStartedAtMs <= 0) { - stats.firstWriteStartedAtMs = message.writeStartedAtMs > 0 ? message.writeStartedAtMs : message.sentAtMs; - } - FrameTimings.getInstance().log(frameId, "first bluetooth packet sent"); - logImageUpdateLandmarkLocked("first bluetooth message sent", message, message.sentAtMs); - } - if (message.imageMessageNumber == message.imageMessageCount) { - FrameTimings.getInstance().log(frameId, - "last bluetooth packet sent (message " + message.imageMessageNumber + "/" + message.imageMessageCount + ")"); - logImageUpdateLandmarkLocked("last bluetooth message sent", message, message.sentAtMs); - } - } - - private void logImageUpdateAckLandmarkLocked(OutboundMessage message) { - if (message.imageUpdateId <= 0 || message.imageMessageNumber != message.imageMessageCount) { - return; - } - long ackedAtMs = SystemClock.elapsedRealtime(); - FaceclawBleManager.recordDisplayFrameSent(); - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(message.imageUpdateId); - if (stats != null && stats.firstWriteStartedAtMs > 0) { - emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount); - } - if (stats != null) { - finishFrame(stats.frameId, "sent"); - } - logImageUpdateLandmarkLocked("last bluetooth message acked", message, ackedAtMs); - } - - /** Remove the stats entry for an image update that will not complete, finishing its frame. */ - private void discardImageUpdateStatsLocked(int imageUpdateId, String reason) { - if (imageUpdateId <= 0) { - return; - } - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(imageUpdateId); - if (stats != null) { - finishFrame(stats.frameId, "discarded: " + reason); - } - } - - private void logImageUpdateLandmarkLocked(String event, OutboundMessage message, long elapsedMs) { - logLine("image update#" + message.imageUpdateId + " " + event - + " at " + timestamp(elapsedMs) - + " message=" + message.imageMessageNumber + "/" + message.imageMessageCount - + " label=" + message.label); - } - - private void enqueueCreateLayoutLocked() { - // New session: re-assert the firmware-debug-flags overlay once - // the layout is ready (the mode-7 send is gated on this having reset). - firmwareDebugFlagsLastSent = -1; - OutboundMessage message = messageBuilder.createLayout(); - message.onAck = () -> { - startupProbePending = false; - clearMessagesOfKindLocked("startup-text-probe"); - fixedLayoutCreated = true; - displayedFingerprint = ""; - }; - message.onTimeout = () -> { - if (startupProbePending) { - logLine("create layout timed out while startup text probe is pending"); - if (hasPendingOrInflightKindLocked("startup-text-probe")) { - return; - } - startupProbePending = false; - } - handleTransportFailure("ack timeout"); - }; - pendingMessages.addLast(message); - logLine("queue create layout"); - } - - private void enqueueStartupProbeLocked() { - enqueueCreateLayoutLocked(); - - OutboundMessage message = messageBuilder.startupTextProbe(); - message.onAck = () -> { - startupProbePending = false; - clearMessagesOfKindLocked("create-layout"); - fixedLayoutCreated = true; - displayedFingerprint = ""; - logLine("existing dashboard layout accepted text probe"); - }; - message.onTimeout = () -> { - startupProbePending = false; - if (hasPendingOrInflightKindLocked("create-layout")) { - return; - } - handleTransportFailure("ack timeout"); - }; - pendingMessages.addLast(message); - startupProbePending = true; - logLine("queue startup text probe"); - } - - /** - * Send the CFW mode-7 diagnostic-flag control op through the private stream: - * [7][2] to show the on-glasses debug-flag overlay, [7][1] to hide it. Uses the - * arbitrary-payload custom path (no bmp/dedup/frame-timing interaction). - */ - private void enqueueFirmwareDebugFlagsLocked() { - boolean show = firmwareDebugFlagsEnabled; - int sub = show ? 2 : 1; - byte[] payload = new byte[] { (byte) 7, (byte) sub }; - OutboundMessage message = messageBuilder.imagePayload( - DASHBOARD_TILE, nextMapSessionId(), payload, - "fw-debug-flags " + (show ? "show" : "hide"), - connectionOptions.sendImagesToLeft); - pendingMessages.addLast(message); - firmwareDebugFlagsLastSent = sub; - logLine("queue firmware debug flags " + (show ? "show" : "hide")); - } - - /** - * Send CFW image-handler mode 10. Enable uses the configurable form - * [10][2][interval-ms LE16][minimum-change-degrees LE16]; disable remains [10][0]. - */ - private void enqueueCompassControlLocked(boolean priority, boolean enable) { - int sentState = enable ? 1 : 0; - byte[] payload = enable - ? new byte[] { - (byte) 10, - (byte) 2, - (byte) (COMPASS_REPORT_INTERVAL_MS & 0xff), - (byte) ((COMPASS_REPORT_INTERVAL_MS >> 8) & 0xff), - (byte) (COMPASS_MIN_CHANGE_DEGREES & 0xff), - (byte) ((COMPASS_MIN_CHANGE_DEGREES >> 8) & 0xff), - } - : new byte[] { (byte) 10, (byte) 0 }; - OutboundMessage message = messageBuilder.imagePayload( - "compass-control", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - "compass " + (enable ? "enable" : "disable"), - connectionOptions.sendImagesToLeft); - message.onTimeout = () -> { - compassControlLastSent = -1; - logLine("compass control ack timeout"); - }; - if (enable) { - compassMaybeOn = true; - } else { - message.onAck = () -> compassMaybeOn = false; - } - if (priority) pendingMessages.addFirst(message); - else pendingMessages.addLast(message); - compassControlLastSent = sentState; - logLine("queue " + message.label); - } - - private void enqueueDesiredImageLocked() { - String fingerprint = getDesiredFingerprint(); - byte[] packed; - int width; - int height; - int paintMs; - int frameId; - SurfaceCompositor.ScreenDraw[] draws; - synchronized (desiredTilesLock) { - packed = desiredPacked; - width = desiredWidth; - height = desiredHeight; - paintMs = desiredPaintMs; - frameId = desiredFrameId; - draws = desiredDraws; - desiredFrameId = 0; - } - if (packed == null) { - packed = new byte[0]; - } - if (lastEnqueuedWidth == width && lastEnqueuedHeight == height - && Arrays.equals(packed, lastEnqueuedPacked)) { - lastEnqueuedFingerprint = fingerprint; - finishFrame(frameId, "discarded: image content identical to displayed"); - return; - } - - // Texture-cache path: ship text as cached-glyph draws, punching their - // ink out of the baked deltas. Uploads (mode 12) ride ahead of the - // image message on the ordered transport. Falls through to the plain - // paths whenever the planner has nothing to draw. - if (customFirmwareDetected && connectionOptions.TEXTURE_CACHE_FRAMES - && draws != null && draws.length > 0 && packed.length > 0) { - byte[] deltaBase = (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0 - && lastEnqueuedWidth == width && lastEnqueuedHeight == height) - ? lastEnqueuedPacked : null; - FrameTimings.getInstance().spanStart(frameId, "texture-plan"); - TexturePlanner.Result tex = TexturePlanner.plan( - deltaBase, packed, width, height, draws, textureCache, - nextImageFrameId, - connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS); - if (tex != null) { - nextImageFrameId = tex.nextFid; - for (byte[] upload : tex.uploads) { - enqueueTextureUploadLocked(upload); - } - BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan( - 0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), tex.payload); - FrameTimings.getInstance().spanEnd(frameId, "texture-plan"); - String texLog = "texture update " + (tex.fullFrame ? "full" : ("rects=" + tex.rectCount)) - + " glyphs=" + tex.drawnGlyphs + " runs=" + tex.runCount - + " images=" + tex.drawnImages - + " fw=" + tex.fwGlyphs + "/" + tex.fwRuns - + " baked=" + tex.bakedCandidates - + (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "") - + " cache=" + textureCache.usedBytes() + "B" - + " payload=" + tex.payload.length + "B" - + " (rects " + tex.rectsMs + "ms, match " + tex.matchMs - + "ms, cache " + tex.cacheMs + "ms, punch " + tex.punchMs - + "ms, encode " + tex.encodeMs + "ms)"; - FrameTimings.getInstance().log(frameId, texLog); - finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId); - return; - } - FrameTimings.getInstance().spanEnd(frameId, "texture-plan"); - } - - FrameTimings.getInstance().spanStart(frameId, "compress-and-plan"); - // Incremental (mode 3 bounding box) update against the last ENQUEUED frame - // (the base the firmware shadow will hold when this update is applied). - // lastEnqueuedPacked is cleared whenever the image pipeline is cleared, so - // a non-empty value means the display base is trusted. - byte[] incrementalPayload = null; - String incrementalLog = null; - if (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0 - && lastEnqueuedWidth == width && lastEnqueuedHeight == height) { - int baseFid = nextImageFrameId; - BleImageOptimizer.IncrementalPlan single = - BleImageOptimizer.buildIncrementalImagePayload(lastEnqueuedPacked, packed, width, height, baseFid); - if (single != null) { - incrementalPayload = single.payload; - // advance only when a delta is actually emitted, so consecutive - // deltas carry consecutive ids (CFW skip/reorder detection) - nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1; - incrementalLog = "incremental update bbox=" - + ((single.payload[3] & 0xff) * 4) + "x" + ((single.payload[4] & 0xff) * 2) - + "+" + ((single.payload[1] & 0xff) * 4) + "+" + ((single.payload[2] & 0xff) * 2) - + " changed=" + single.changedBytes + "/" + single.boxBytes + "B" - + " clusters=" + single.clusterCount; - - // When the bounding box spans multiple clusters or is sizeable, try - // splitting into tight rects (CFW mode-8). Only replace the single - // box if the multi-rect message is actually smaller on the wire. - if (connectionOptions.MULTI_RECT_FRAMES - && (single.clusterCount > 1 || single.payload.length > ConnectionOptions.MULTI_RECT_MIN_PAYLOAD)) { - BleImageOptimizer.MultiRectPlan multi = BleImageOptimizer.buildMultiRectImagePayload( - lastEnqueuedPacked, packed, width, height, baseFid, ConnectionOptions.MULTI_RECT_MAX_RECTS); - if (multi != null && multi.payload.length < single.payload.length) { - incrementalPayload = multi.payload; - nextImageFrameId = multi.nextFid; // rectCount fids consumed - incrementalLog = "multi-rect update n=" + multi.rectCount - + " covered=" + multi.coveredBytes + "B" - + " payload=" + multi.payload.length + "B (vs bbox " + single.payload.length + "B)"; - } - } - } - } - BleImageOptimizer.TileImagePlan plan = incrementalPayload != null - ? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), incrementalPayload) - : new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId()); - FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan"); - if (incrementalLog != null) { - FrameTimings.getInstance().log(frameId, incrementalLog); - } - finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId); - } - - /** Queue complete private commands, advance the delta base, and retain frame/ACK bookkeeping. */ - private void finishEnqueueDesiredImageLocked( - BleImageOptimizer.TileImagePlan plan, String fingerprint, int paintMs, int frameId) { - int updateId = nextImageUpdateId++; - List commands = new ArrayList<>(); - if (plan.payload.length <= CfwTransport.MAX_MESSAGE) { - commands.add(plan.payload); - } else { - // A noisy full frame can exceed the stream record limit. Repaint it - // with independently decodable RLE bands below the decoded-message limit. - commands.addAll(BleImageOptimizer.encodeFullFrameBands( - plan.packed, plan.width, plan.height, nextImageFrameId)); - for (int i = 0; i < commands.size(); i++) { - nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1; - } - } - int messageCount = commands.size(); - imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId)); - for (int i = 0; i < commands.size(); i++) { - enqueueCustomImageLocked(plan, commands.get(i), fingerprint, updateId, i + 1, messageCount); - } - // This frame is now the base for the next delta (it will be the firmware - // shadow once applied), even though it hasn't been acked yet — that is what - // lets the next frame pipeline behind it. plan.packed is the full frame; - // frames are immutable by convention, so referencing it is safe. - lastEnqueuedPacked = plan.packed; - lastEnqueuedWidth = plan.width; - lastEnqueuedHeight = plan.height; - lastEnqueuedFingerprint = fingerprint; - - FrameTimings.getInstance().log(frameId, "queued image update#" + updateId - + " messages=" + messageCount + " payload=" + plan.payload.length + "B"); - logLine("queue image update#" + updateId + " fingerprint=" + fingerprint - + " messages=" + messageCount); - } - - /** - * Enqueue one mode-12 texture-cache upload ahead of the image message that - * references its glyphs (the transport is FIFO, so no ack round trip is - * needed before use). A timeout means the on-glasses cache state is - * unknown; forget everything phone-side (glyphs re-upload lazily) and let - * the accompanying image update's own timeout drive the frame resync. - */ - private void enqueueTextureUploadLocked(byte[] payload) { - OutboundMessage message = messageBuilder.imagePayload( - "texcache", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - "texture upload " + payload.length + "B", - connectionOptions.sendImagesToLeft); - message.onTimeout = () -> { - textureCache.reset(); - logLine("texture upload ack timeout; texture cache state reset"); - }; - pendingMessages.addLast(message); - logLine("queue " + message.label); - } - - private void enqueueCustomImageLocked( - BleImageOptimizer.TileImagePlan plan, - byte[] payload, - String fingerprint, - int updateId, - int messageNumber, - int messageCount - ) { - OutboundMessage message = messageBuilder.customMessage("image", payload, - "image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, connectionOptions.sendImagesToLeft); - message.setImageUpdatePosition(updateId, messageNumber, messageCount); - message.onAck = () -> { - imageRetryAfterMs = 0; - // Firmware >= 2.2.4.34 resets its heartbeat timer when it receives - // image messages (not just heartbeats), so an acked image fragment - // satisfies the heartbeat deadline and heartbeats stop contending - // with active rendering. - lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime(); - logImageUpdateAckLandmarkLocked(message); - boolean imageStillInFlight = false; - for (OutboundMessage inFlight : inFlightMessages) { - if ("image".equals(inFlight.kind)) { - imageStillInFlight = true; - break; - } - } - if (!imageStillInFlight) { - boolean imageStillQueued = false; - for (OutboundMessage queued : pendingMessages) { - if ("image".equals(queued.kind)) { - imageStillQueued = true; - break; - } - } - if (!imageStillQueued) { - displayedFingerprint = fingerprint; - } - } - }; - message.onTimeout = () -> { - discardImageUpdateStatsLocked(message.imageUpdateId, "image ack timeout (will retry)"); - clearMessagesOfKindLocked("image"); - displayedFingerprint = ""; - imageRetryAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.IMAGE_RETRY_DELAY_MS; - }; - pendingMessages.addLast(message); - } - - private OutboundMessage createAudioControlMessageLocked(boolean enable) { - OutboundMessage message = messageBuilder.enableOrDisableMic(enable); - message.onAck = () -> { - lastAudioControlAckMagic = message.magic; - audioCaptureActive = message.label != null && message.label.contains("enable"); - logLine(audioCaptureActive ? "G2 mic enabled" : "G2 mic disabled"); - }; - message.onTimeout = () -> { - handleTransportFailure("audio control ack timeout"); - }; - return message; - } - - private boolean shouldPollBatteryLocked(long now) { - return !shutdownRequested - && sessionReady - && pendingMessages.isEmpty() - && inFlightMessages.isEmpty() - && now - lastConnectionOrInputAtMs >= ConnectionOptions.BATTERY_INPUT_QUIET_MS - && (lastBatteryRefreshAtMs == 0 || now - lastBatteryRefreshAtMs >= ConnectionOptions.BATTERY_REFRESH_INTERVAL_MS); - } - - private OutboundMessage createBatteryQueryMessageLocked() { - OutboundMessage message = messageBuilder.batteryQuery(); - message.onAck = () -> { - BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(message.ackPayload); - // Old firmware and missing/malformed extensions must clear any prior reading. - ringBattery = ring == null ? -1 : ring.battery; - ringCharging = ring == null ? -1 : ring.charging; - BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload); - if (snapshot != null) { - headsetBattery = snapshot.battery; - headsetCharging = snapshot.charging; - emitBatteryState(headsetBattery, headsetCharging); - if (snapshot.silentMode >= 0) { - // Backstop for the push in onNotification: the firmware is - // confirmed to push silent-mode-on, but the off transition is - // not, so re-read the authoritative value on every poll. - updateSilentModeLocked(snapshot.silentMode > 0); - } - updateChargingModeLocked(snapshot.charging > 0, snapshot.battery); - } - BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload); - if (firmwareInfo != null) { - customFirmwareDetected = firmwareInfo.isFaceclawFirmware(); - emitFirmwareInfo(firmwareInfo); - } - }; - message.onTimeout = () -> { - logLine("Battery query timed out"); - }; - return message; - } - - /** - * Track silent mode, which the wearer toggles by long-pressing both - * touchpads at once. While it is on the firmware refuses input events and - * app launches and powers the display down, so the glasses look dead even - * though the BLE session is healthy; the phone UI says so explicitly. - */ - private void updateSilentModeLocked(boolean silent) { - int next = silent ? 1 : 0; - if (silentMode == next) { - return; - } - silentMode = next; - logLine(silent ? "glasses entered silent mode" : "glasses left silent mode"); - emitSilentMode(silent); - } - - /** - * Track whether the glasses are in the charging case. Charging means nobody - * is wearing them: display communication pauses (no heartbeats, so the - * firmware tears down its EvenHub context on its own) and only battery polls - * continue. When charging stops, tear the transport down and let the normal - * reconnect loop rebuild the session, layout, and first frame. - */ - private void updateChargingModeLocked(boolean charging, int battery) { - if (charging == chargingMode) { - if (chargingMode) { - setStateDisplay("charging", chargingStatusText(battery)); - } - return; - } - if (charging) { - chargingMode = true; - clearAllMessagesLocked("glasses charging"); - fixedLayoutCreated = false; - startupProbePending = false; - displayedFingerprint = ""; - finishDesiredFrameLocked("discarded: glasses charging"); - logLine("glasses are charging; pausing display communication"); - setStateDisplay("charging", chargingStatusText(battery)); - } else { - chargingMode = false; - logLine("glasses removed from charger; reconnecting"); - handleTransportFailure("charging ended"); - } - } - - private static String chargingStatusText(int battery) { - return battery >= 0 - ? "Glasses charging. Battery " + battery + "%." - : "Glasses charging."; - } - - /** - * Record, into the frame that is waiting, why it did not go out on this - * pass of the send loop. Without this the export shows a bare multi-second - * jump between "image submitted as desired frame" and the first BLE - * packet, with no hint whether we were blocked on a heartbeat, the - * BLE window, or another message queued ahead. Deduped on (frame, reason), - * so a frame stalled for seconds gets one line per state change rather - * than one per loop pass. - */ - private void noteImageStallLocked(long now, boolean windowHasRoom) { - int frameId; - synchronized (desiredTilesLock) { - frameId = desiredFrameId; - } - String reason; - if (frameId != 0 && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) { - reason = describeEnqueueBlockerLocked(now, windowHasRoom); - } else { - // Nothing waiting to be planned; an already-planned image may still - // be queued behind other traffic. - OutboundMessage queuedImage = firstPendingImageLocked(); - frameId = queuedImage == null ? 0 : imageUpdateFrameIdLocked(queuedImage.imageUpdateId); - reason = queuedImage == null ? null : describeWriteBlockerLocked(now, windowHasRoom, queuedImage); - } - if (frameId == 0 || reason == null) { - stallFrameId = 0; - stallReason = ""; - return; - } - if (frameId == stallFrameId && reason.equals(stallReason)) { - return; - } - stallFrameId = frameId; - stallReason = reason; - FrameTimings.getInstance().log(frameId, "waiting to send: " + reason); - } - - /** Why the desired composite has not been turned into wire messages yet, or null. */ - private String describeEnqueueBlockerLocked(long now, boolean windowHasRoom) { - if (shutdownRequested) { - return "shutdown requested"; - } - if (!sessionReady) { - return "BLE session not ready"; - } - if (!fixedLayoutCreated) { - return "display layout not created yet"; - } - if (now < imageRetryAfterMs) { - return "image retry backoff (" + (imageRetryAfterMs - now) + "ms left)"; - } - if (hasPendingImageLocked()) { - return "an earlier image is still queued"; - } - if (!windowHasRoom) { - return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)"; - } - if (hasPendingOrInflightKindLocked("heartbeat")) { - return "heartbeat in flight"; - } - if (!pendingMessages.isEmpty()) { - return pendingMessages.size() + " message(s) queued ahead, next " - + pendingMessages.peekFirst().label; - } - return null; - } - - /** Why a planned image message has not been written to BLE yet, or null. */ - private String describeWriteBlockerLocked(long now, boolean windowHasRoom, OutboundMessage queuedImage) { - if (!sessionReady) { - return "BLE session not ready"; - } - if (!windowHasRoom) { - return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)"; - } - if (hasPendingOrInflightKindLocked("heartbeat")) { - return "heartbeat in flight"; - } - // handleHeartbeat is a barrier: while one is due it holds back every - // other write, so a frame queued at the wrong moment waits a heartbeat - // round trip. Reported explicitly because it is otherwise invisible -- - // heartbeats belong to no frame. - long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs; - if (fixedLayoutCreated && !shutdownRequested - && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS) { - return "heartbeat due (" + heartbeatElapsedMs + "ms since the last one acked)"; - } - OutboundMessage head = pendingMessages.peekFirst(); - if (head != null && head != queuedImage) { - return "queued behind " + head.label; - } - return null; - } - - private OutboundMessage firstPendingImageLocked() { - for (OutboundMessage message : pendingMessages) { - if (message.imageUpdateId > 0) { - return message; - } - } - return null; - } - - private int imageUpdateFrameIdLocked(int imageUpdateId) { - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(imageUpdateId); - return stats == null ? 0 : stats.frameId; - } - - private void finishDesiredFrameLocked(String outcome) { - int frameIdToFinish; - synchronized (desiredTilesLock) { - frameIdToFinish = desiredFrameId; - desiredFrameId = 0; - } - finishFrame(frameIdToFinish, outcome); - } - - private boolean hasPendingOrInflightKindLocked(String kind) { - for (OutboundMessage queued : pendingMessages) { - if (kind.equals(queued.kind)) { - return true; - } - } - for (OutboundMessage inFlight : inFlightMessages) { - if (kind.equals(inFlight.kind)) { - return true; - } - } - return false; - } - - private boolean hasPendingOrInflightMagicLocked(int magic) { - for (OutboundMessage queued : pendingMessages) { - if (queued.magic == magic) { - return true; - } - } - for (OutboundMessage inFlight : inFlightMessages) { - if (inFlight.magic == magic) { - return true; - } - } - return false; - } - - private boolean hasPendingMagicLocked(int sid, int magic) { - for (OutboundMessage queued : pendingMessages) { - if (queued.sid == sid && queued.magic == magic) { - return true; - } - } - return false; - } - - private void handleAckTimeoutLocked(OutboundMessage message) { - consecutiveAckTimeouts += 1; - - if (message.onTimeout != null) { - message.onTimeout.run(); - } - - if (consecutiveAckTimeouts > ConnectionOptions.MAX_CONSECUTIVE_ACK_TIMEOUTS) { - handleTransportFailure("too many ack timeouts"); - } - } - - /** - * Replace any stale lease control with one right-arm and one left-arm - * fire-and-forget write. With priority=true the right arm is sent first so - * CLAIM reaches the lens that originated the deferred wake immediately. - */ - private int enqueueFaceclawWakeControlLocked(int operation, int nonce, boolean priority) { - clearMessagesOfKindLocked("wake-lease-control"); - final int generation = ++faceclawWakeControlGeneration; - faceclawWakeControlSentCount = 0; - if (operation == BleProtocol.FACECLAW_WAKE_OP_ACQUIRE) { - lastFaceclawWakeLeaseQueuedAtMs = SystemClock.elapsedRealtime(); - } - MessageCallback onSent = () -> { - if (faceclawWakeControlGeneration == generation) { - faceclawWakeControlSentCount += 1; - } - }; - OutboundMessage right = messageBuilder.faceclawWakeControl(operation, nonce, false); - OutboundMessage left = messageBuilder.faceclawWakeControl(operation, nonce, true); - right.onSent = onSent; - left.onSent = onSent; - if (priority) { - pendingMessages.addFirst(left); - pendingMessages.addFirst(right); - } else { - pendingMessages.addLast(right); - pendingMessages.addLast(left); - } - logLine("queue " + right.label + " + L"); - return generation; - } - - private boolean waitForFaceclawWakeControlDelivery(int generation, long timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + Math.max(0, timeoutMs); - synchronized (lock) { - while (running - && sessionReady - && faceclawWakeControlGeneration == generation - && faceclawWakeControlSentCount < 2) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - return faceclawWakeControlGeneration == generation - && faceclawWakeControlSentCount >= 2; - } - } - - /** - * Acquire/renew or release CFW's independent direct-framebuffer repaint - * guard on both arms. It is separate from the optional idle-wake lease: - * every Faceclaw display session needs this guard while its EvenHub layout - * contains swipe-capturing stock widgets. - */ - private int enqueueFaceclawFramebufferControlLocked(int operation, boolean priority) { - clearMessagesOfKindLocked("framebuffer-lease-control"); - final int generation = ++faceclawFramebufferControlGeneration; - faceclawFramebufferControlSentCount = 0; - if (operation == BleProtocol.FACECLAW_FB_OP_ACQUIRE) { - lastFaceclawFramebufferLeaseQueuedAtMs = SystemClock.elapsedRealtime(); - } - MessageCallback onSent = () -> { - if (faceclawFramebufferControlGeneration == generation) { - faceclawFramebufferControlSentCount += 1; - } - }; - OutboundMessage right = messageBuilder.faceclawFramebufferControl(operation, false); - OutboundMessage left = messageBuilder.faceclawFramebufferControl(operation, true); - right.onSent = onSent; - left.onSent = onSent; - if (priority) { - pendingMessages.addFirst(left); - pendingMessages.addFirst(right); - } else { - pendingMessages.addLast(right); - pendingMessages.addLast(left); - } - logLine("queue " + right.label + " + L"); - return generation; - } - - private boolean waitForFaceclawFramebufferControlDelivery(int generation, long timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + Math.max(0, timeoutMs); - synchronized (lock) { - while (running - && sessionReady - && faceclawFramebufferControlGeneration == generation - && faceclawFramebufferControlSentCount < 2) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - return faceclawFramebufferControlGeneration == generation - && faceclawFramebufferControlSentCount >= 2; - } - } - - private boolean releaseFaceclawFramebufferLease() { - int generation; - synchronized (lock) { - if (!running || !sessionReady) { - return true; - } - generation = enqueueFaceclawFramebufferControlLocked( - BleProtocol.FACECLAW_FB_OP_RELEASE, - true - ); - } - interruptibleSleep.interrupt(); - return waitForFaceclawFramebufferControlDelivery( - generation, - FACECLAW_WAKE_CONTROL_WAIT_MS - ); - } - - private void clearMessagesOfKindLocked(String kind) { - Iterator pendingIterator = pendingMessages.iterator(); - while (pendingIterator.hasNext()) { - OutboundMessage message = pendingIterator.next(); - if (kind.equals(message.kind)) { - pendingIterator.remove(); - discardPrewriteIfMatchesLocked(message); - if ("image".equals(kind)) { - discardImageUpdateStatsLocked(message.imageUpdateId, "pending messages cleared (" + kind + ")"); - } - magicPool.release(message.sid, message.magic, message.label, "cleared pending " + kind); - } - } - Iterator inFlightIterator = inFlightMessages.iterator(); - while (inFlightIterator.hasNext()) { - OutboundMessage message = inFlightIterator.next(); - if (kind.equals(message.kind)) { - inFlightIterator.remove(); - if ("image".equals(kind)) { - discardImageUpdateStatsLocked(message.imageUpdateId, "inflight messages cleared (" + kind + ")"); - } - magicPool.release(message.sid, message.magic, message.label, "cleared inflight " + kind); - } - } - if ("image".equals(kind)) { - // The image pipeline was flushed (e.g. ack timeout -> keyframe resync): - // drop the pipelined delta base so the next image is a full keyframe. - lastEnqueuedPacked = new byte[0]; - lastEnqueuedFingerprint = ""; - } - } - - private void clearAllMessagesLocked(String reason) { - clearPendingMessagesLocked(reason); - clearInFlightMessagesLocked(reason); - // The image pipeline is gone: drop the pipelined delta base so the next - // image is a full keyframe rather than a delta onto a stale base. - lastEnqueuedPacked = new byte[0]; - lastEnqueuedFingerprint = ""; - // Queued texture uploads (if any) were dropped with the rest, and the - // session churn behind a full clear may have freed the on-glasses - // cache; forget it phone-side so glyphs re-upload lazily. - textureCache.reset(); - } - - /** - * A custom wake queues CLAIM before NativeScript asks for a resume. Keep - * that private control while flushing stale EvenHub traffic around the - * direct prelude write. - */ - private void clearAllMessagesPreservingWakeLeaseLocked(String reason) { - Iterator pendingIterator = pendingMessages.iterator(); - while (pendingIterator.hasNext()) { - OutboundMessage message = pendingIterator.next(); - if ("wake-lease-control".equals(message.kind)) { - continue; - } - pendingIterator.remove(); - discardPrewriteIfMatchesLocked(message); - discardImageUpdateStatsLocked( - message.imageUpdateId, - "pending messages cleared: " + reason - ); - magicPool.release( - message.sid, - message.magic, - message.label, - "cleared pending: " + reason - ); - } - clearInFlightMessagesLocked(reason); - lastEnqueuedPacked = new byte[0]; - lastEnqueuedFingerprint = ""; - textureCache.reset(); - } - - /** Any image update whose fragments are still queued (not yet sent). */ - private boolean hasPendingImageLocked() { - for (OutboundMessage message : pendingMessages) { - if ("image".equals(message.kind)) { - return true; - } - } - return false; - } - - private void clearPendingMessagesLocked(String reason) { - while (!pendingMessages.isEmpty()) { - var message = pendingMessages.removeFirst(); - discardPrewriteIfMatchesLocked(message); - discardImageUpdateStatsLocked(message.imageUpdateId, "pending messages cleared: " + reason); - magicPool.release(message.sid, message.magic, message.label, "cleared pending: " + reason); - } - } - - private void clearInFlightMessagesLocked(String reason) { - while (!inFlightMessages.isEmpty()) { - var message = inFlightMessages.removeFirst(); - discardImageUpdateStatsLocked(message.imageUpdateId, "inflight messages cleared: " + reason); - magicPool.release(message.sid, message.magic, message.label, "cleared inflight: " + reason); - } - } - - private void discardPrewriteIfMatchesLocked(OutboundMessage message) { - if (message != null && message == prewrittenMessage) { - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - } - } - - private void recordUnexpectedAckLocked(int sid, int magic) { - if (magic < BleMagicPool.MIN_MAGIC || magic > BleMagicPool.MAX_MAGIC) { - return; - } - BleMagicPool.ReleaseRecord previous = magicPool.getReleaseRecord(sid, magic); - if (previous == null) { - String pendingNote = hasPendingMagicLocked(sid, magic) ? " while that magic is only pending locally" : ""; - logLine("unexpected ACK sid=" + sid + " magic=" + magic + pendingNote - + "; possible Even app BLE contention"); - return; - } - if ("timeout".equals(previous.reason)) { - logLine("late ACK after timeout sid=" + sid + " magic=" + magic - + " label=" + previous.label - + "; ACK timeout may be too short"); - return; - } - if ("ack".equals(previous.reason)) { - logLine("duplicate ACK for already-acked message sid=" + sid + " magic=" + magic - + " label=" + previous.label - + "; possible Even app BLE contention"); - return; - } - logLine("late ACK for released message sid=" + sid + " magic=" + magic - + " label=" + previous.label - + " release=" + previous.reason); - } - - /** The address of a configured arm Android no longer holds a bond for, or null. */ - private String firstUnpairedArm() { - if (!bleManager.isBonded(rightAddress)) return rightAddress; - if (!bleManager.isBonded(leftAddress)) return leftAddress; - return null; - } - - /** - * A connect failure while an arm's bond is missing (the pairing was - * forgotten in Android settings, or the address was typed in by hand and - * never paired) will repeat forever, so instead of scheduling a retry, - * park the worker loop and tell the user to re-pair. Only an explicit - * connect (which builds a fresh communicator) starts a new attempt. - */ - private void handleUnpairedFailure(String address) { - Log.e(TAG, "Connect failed and " + address + " is not paired; suspending reconnect"); - synchronized (lock) { - reconnectHalted = true; - sessionReady = false; - fixedLayoutCreated = false; - startupProbePending = false; - shutdownRequested = false; - chargingMode = false; - imageRetryAfterMs = 0; - displayedFingerprint = ""; - faceclawWakePendingNonce = -1; - lastFaceclawWakeLeaseQueuedAtMs = 0; - faceclawWakeControlSentCount = 0; - clearAllMessagesLocked("arm not paired: " + address); - reconnectAfterMs = Long.MAX_VALUE; - bleManager.disconnect(rightAddress); - bleManager.disconnect(leftAddress); - } - if (!userDisconnectRequested) { - setStateDisplay( - "unpaired", - "The glasses (" + address + ") are not paired with this phone." - + " Use \"Pair glasses\" to pair them again, then connect." - ); - } - interruptibleSleep.interrupt(); - } - - private void handleTransportFailure(String reason) { - Log.e(TAG, "Transport failure: "+reason); - synchronized (lock) { - maybeEmitEvenAppConflictLocked(reason); - finishBenchmarkLocked(true, "transport failure"); - sessionReady = false; - fixedLayoutCreated = false; - startupProbePending = false; - shutdownRequested = false; - chargingMode = false; - imageRetryAfterMs = 0; - displayedFingerprint = ""; - faceclawWakePendingNonce = -1; - lastFaceclawWakeLeaseQueuedAtMs = 0; - faceclawWakeControlSentCount = 0; - clearAllMessagesLocked("transport failure: " + reason); - reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; - bleManager.disconnect(rightAddress); - bleManager.disconnect(leftAddress); - } - if (!userDisconnectRequested) { - setStateDisplay("retrying", reason == null || reason.isEmpty() ? "Reconnecting..." : "Reconnecting after " + reason); - } - interruptibleSleep.interrupt(); - } - - private void resetSessionStateLocked() { - ringBattery = -1; - ringCharging = -1; - sessionReady = false; - shutdownRequested = false; - fixedLayoutCreated = false; - chargingMode = false; - rightConnected = false; - leftConnected = false; - ringConnected = false; - ringNotificationsReady = false; - reconnectAfterMs = 0; - reconnectHalted = false; - ringReconnectAfterMs = 0; - lastAckAtMs = 0; - lastIncomingAtMs = 0; - lastHeartbeatSentAtMs = 0; - lastSessionReadyAtMs = 0; - consecutiveAckTimeouts = 0; - lastAudioControlAckMagic = 0; - audioCaptureActive = false; - audioPacketListener = null; - compassControlLastSent = -1; - // A dead transport orphans any glasses-side compass state; the fresh - // session re-asserts the desired state once its layout is ready. - compassMaybeOn = false; - faceclawWakePendingNonce = -1; - lastFaceclawWakeLeaseQueuedAtMs = 0; - faceclawWakeControlSentCount = 0; - cfwCleanupDelivered = false; - for (CfwTransport transport : cfwTransports) transport.reset(); - lastCfwCleanupAckMagic = 0; - wearState = -1; - displayedFingerprint = ""; - // Deliberately not clearing silentMode: it is a property of the glasses, - // not of our session, and silent mode blocks app launches, so it can be - // the very cause of the session teardown that got us here. - } - - private void emitRingEvent(G2Event event, int frameId) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - FrameTimings.getInstance().finishFrame(frameId, "discarded: no listener attached"); - return; - } - final String containerNameSnapshot = event.containerName == null ? "" : event.containerName; - mainHandler.post(() -> { - FrameTimings.getInstance().log(frameId, "dispatching input event on main thread"); - try { - current.onRingEvent(event.kind, containerNameSnapshot, event.eventType, event.eventSource, event.systemExitReasonCode, frameId, - event.ringTick, event.ringType, event.ringAux, event.ringSpeed); - } catch (Throwable t) { - Log.w(TAG, "listener onRingEvent failed", t); - FrameTimings.getInstance().finishFrame(frameId, "discarded: listener onRingEvent failed"); - } - }); - } - - /** Finish a frame owned by the communicator and tell the TS side, which may be awaiting it. */ - private void finishFrame(int frameId, String outcome) { - if (frameId <= 0) { - return; - } - FrameTimings.getInstance().finishFrame(frameId, outcome); - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onFrameFinished(frameId, outcome); - } catch (Throwable t) { - Log.w(TAG, "listener onFrameFinished failed", t); - } - }); - } - - private void emitImuData(double x, double y, double z, int eventSource) { - if (imuListeners.isEmpty()) { - return; - } - mainHandler.post(() -> { - for (FaceclawImuListener imuListener : imuListeners) { - try { - imuListener.onImuData(x, y, z, eventSource); - } catch (Throwable t) { - Log.w(TAG, "listener onImuData failed", t); - } - } - }); - } - - private void emitCompassEvent(BleProtocol.CompassEvent event) { - if (event.diagnosticFlags >= 0) { - String[] sources = { "unknown", "GRV", "GMRV", "RV" }; - Log.i("FaceclawCompass", "heading=" + event.headingDegrees - + " magneticAccuracy=" + event.magneticAccuracy - + " magneticAnomalies=" + event.magneticAnomalies - + " orientationSource=" + sources[event.orientationSource] - + " flags=0x" + Integer.toHexString(event.diagnosticFlags) - + " sampleTimeMs=" + event.sampleTimeMs); - } - for (CompassSubscription subscription : compassSubscriptions) { - subscription.handler.post(() -> { - try { - subscription.listener.onCompassEvent(event.command, event.headingDegrees, - event.magneticAccuracy, event.magneticAnomalies, event.orientationSource, - event.diagnosticFlags, event.sampleTimeMs); - } catch (Throwable t) { - Log.w(TAG, "listener onCompassEvent failed", t); - } - }); - } - } - - private void emitSilentMode(boolean silent) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onSilentMode(silent); - } catch (Throwable t) { - Log.w(TAG, "listener onSilentMode failed", t); - } - }); - } - - private void emitWearState(boolean wearing) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) return; - mainHandler.post(() -> { - try { - current.onWearState(wearing); - } catch (Throwable t) { - Log.w(TAG, "listener onWearState failed", t); - } - }); - } - - private void emitPhoneLockStateIfChanged(boolean force) { - final boolean locked; - synchronized (lock) { - long now = SystemClock.elapsedRealtime(); - if (!force && now - lastPhoneLockCheckAtMs < 1_000) return; - lastPhoneLockCheckAtMs = now; - locked = keyguardManager != null && keyguardManager.isDeviceLocked(); - int value = locked ? 1 : 0; - if (!force && value == phoneLockState) return; - phoneLockState = value; - } - final FaceclawBleCommunicatorListener current = listener; - if (current == null) return; - mainHandler.post(() -> { - try { - current.onPhoneLockState(locked); - } catch (Throwable t) { - Log.w(TAG, "listener onPhoneLockState failed", t); - } - }); - } - - private void emitBatteryState(int headsetBattery, int headsetCharging) { - final int reportedRingBattery = ringBattery; - final int reportedRingCharging = ringCharging; - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onBatteryState(headsetBattery, headsetCharging, reportedRingBattery, reportedRingCharging); - } catch (Throwable t) { - Log.w(TAG, "listener onBatteryState failed", t); - } - }); - } - - private void emitFirmwareInfo(BleProtocol.FirmwareInfo info) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onFirmwareInfo(info.leftVersion, info.rightVersion, info.extension); - } catch (Throwable t) { - Log.w(TAG, "listener onFirmwareInfo failed", t); - } - }); - } - - private void emitFrameMetrics(int paintMs, int transmitMs, int tileCount) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onFrameMetrics(paintMs, transmitMs, tileCount); - } catch (Throwable t) { - Log.w(TAG, "listener onFrameMetrics failed", t); - } - }); - } - - private void maybeEmitEvenAppConflictLocked(String reason) { - if (!"write failed".equals(reason)) { - return; - } - long now = SystemClock.elapsedRealtime(); - if (lastSessionReadyAtMs <= 0 || now - lastSessionReadyAtMs > ConnectionOptions.EVEN_APP_WRITE_FAILURE_WINDOW_MS) { - return; - } - if (lastEvenAppConflictAtMs > 0 && now - lastEvenAppConflictAtMs < 60_000) { - return; - } - if (!FaceclawEvenAppDetector.isEvenNotificationActive(appContext)) { - return; - } - lastEvenAppConflictAtMs = now; - emitEvenAppConflict("The Even Realities app still appears to be running. It can hold the glasses BLE link and cause Faceclaw write failures. Open its app settings and force stop it, then reconnect Faceclaw."); - } - - private void emitEvenAppConflict(String message) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - final String messageSnapshot = message == null ? "" : message; - mainHandler.post(() -> { - try { - current.onEvenAppConflict(messageSnapshot); - } catch (Throwable t) { - Log.w(TAG, "listener onEvenAppConflict failed", t); - } - }); - } - - private void setStateDisplay(String nextPhase, String nextStatus) { - synchronized (lock) { - phase = nextPhase; - status = nextStatus; - } - emitState(); - } - - private void emitState() { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - final String phaseSnapshot; - final String statusSnapshot; - synchronized (lock) { - phaseSnapshot = phase; - statusSnapshot = status; - } - mainHandler.post(() -> { - try { - current.onStateChange(phaseSnapshot, statusSnapshot); - } catch (Throwable t) { - Log.w(TAG, "listener onStateChange failed", t); - } - }); - } - - private void updateG2ScreenWakeLock(boolean screenOn) { - if (screenOn) { - if (g2ScreenWakeLock == null) { - g2ScreenWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, G2_SCREEN_WAKE_LOCK_TAG); - g2ScreenWakeLock.setReferenceCounted(false); - } - if (!g2ScreenWakeLock.isHeld()) { - g2ScreenWakeLock.acquire(); - logLine("G2 screen wake lock acquired"); - } - return; - } - releaseG2ScreenWakeLock(); - } - - private void releaseG2ScreenWakeLock() { - if (g2ScreenWakeLock != null && g2ScreenWakeLock.isHeld()) { - g2ScreenWakeLock.release(); - logLine("G2 screen wake lock released"); - } - } - - private void logLine(String line) { - Log.i(TAG, line); - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onLog(line); - } catch (Throwable t) { - Log.w(TAG, "listener onLog failed", t); - } - }); - } - - private static String timestamp(long elapsedMs) { - long wallMs = System.currentTimeMillis(); - return String.format(Locale.US, "%tF %tT.%tL elapsed=%dms", wallMs, wallMs, wallMs, elapsedMs); - } - - private int nextMapSessionId() { - int id = nextMapSessionId; - int increment = connectionOptions.skipSessionIds ? 2 : 1; - nextMapSessionId = (nextMapSessionId + increment) & 0xff; - return id; - } - - private static String requireAddress(String name, String address) { - if (address == null || address.trim().isEmpty()) { - throw new IllegalArgumentException(name + " is required"); - } - return address.trim(); - } - - private boolean hasRingAddress() { - return ringAddress != null && !ringAddress.trim().isEmpty(); - } - - private boolean isConfiguredRingAddress(String address) { - return hasRingAddress() && address != null && address.equalsIgnoreCase(ringAddress); - } - - private boolean isDirectRingNotification(String address, String characteristicUuid) { - if (!isConfiguredRingAddress(address) || characteristicUuid == null) { - return false; - } - return BleProtocol.R1_PHONE_NOTIFY_CHAR_UUID.equals(characteristicUuid) - || BleProtocol.R1_NOTIFY_CHAR_UUID.equals(characteristicUuid); - } - - private static String hex(byte[] data) { - if (data == null || data.length == 0) { - return ""; - } - char[] out = new char[data.length * 2]; - char[] digits = "0123456789abcdef".toCharArray(); - for (int i = 0; i < data.length; i++) { - int value = data[i] & 0xff; - out[i * 2] = digits[value >>> 4]; - out[i * 2 + 1] = digits[value & 0x0f]; - } - return new String(out); - } - - private static String safeMessage(Throwable t) { - if (t == null) { - return "unknown"; - } - StringWriter writer = new StringWriter(); - t.printStackTrace(new PrintWriter(writer)); - String trace = writer.toString(); - if (!trace.trim().isEmpty()) { - return trace; - } - String message = t.getMessage(); - return message == null || message.trim().isEmpty() ? String.valueOf(t) : message; - } - - private String getDesiredFingerprint() { - synchronized (desiredTilesLock) { - return desiredFingerprint; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt new file mode 100644 index 00000000..65d9c9c8 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt @@ -0,0 +1,391 @@ +package com.faceclaw.app + +import android.annotation.SuppressLint +import android.app.KeyguardManager +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.os.Handler +import android.os.Looper +import android.os.PowerManager +import android.util.Log + +/** + * Android facade over the shared [GlassesSessionCore]: the JVM API TypeScript + * and the other Android classes call by name, plus the Android-only pieces: + * the GATT link ([AndroidSessionLink] over [FaceclawBleManager]), main-thread + * marshalling, the worker thread, the screen wake lock, keyguard/screen + * broadcast state, the Even-app conflict check, and the Bitmap/ByteBuffer/ + * Context entry points for previews, screenshots and recordings. + */ +@SuppressLint("MissingPermission") +class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddress: String?, ringAddress: String?) { + companion object { + private const val TAG = GlassesSessionCore.TAG + private const val G2_SCREEN_WAKE_LOCK_TAG = "Faceclaw:G2Screen" + + // The most recently started communicator; lets app worker threads submit + // surface frames without holding a cross-isolate reference to the bridge + // object (JS wrappers do not cross isolates, but the Java instance does). + @Volatile private var activeInstance: FaceclawBleCommunicator? = null + + @JvmStatic + fun getActive(): FaceclawBleCommunicator? { + return activeInstance + } + } + + private val appContext: Context = context.applicationContext + init { + FrameTimings.getInstance().init(appContext) + } + private val powerManager: PowerManager = appContext.getSystemService(Context.POWER_SERVICE) as PowerManager + private val keyguardManager: KeyguardManager? = appContext.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager? + private val bleManager: FaceclawBleManager = FaceclawBleManager(appContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val mainDispatcher = SessionDispatcher { action -> mainHandler.post { action() } } + @Volatile private var workerThread: Thread? = null + private var g2ScreenWakeLock: PowerManager.WakeLock? = null + private var phoneLockReceiverRegistered = false + + private val host = object : SessionHost { + override fun postToMain(action: () -> Unit) { + mainHandler.post { action() } + } + + override fun currentThreadDispatcher(): SessionDispatcher { + val looper = Looper.myLooper() ?: return mainDispatcher + val handler = Handler(looper) + return SessionDispatcher { action -> handler.post { action() } } + } + + override fun isPhoneLocked(): Boolean = keyguardManager != null && keyguardManager.isDeviceLocked + + override fun setScreenWakeLock(on: Boolean) = updateG2ScreenWakeLock(on) + + override fun isEvenAppActive(): Boolean = FaceclawEvenAppDetector.isEvenNotificationActive(appContext) + + override fun startWorker(body: () -> Unit) { + val thread = Thread(body, "FaceclawBleCommunicator") + workerThread = thread + thread.start() + } + + override fun joinWorker(timeoutMs: Long) { + val threadToJoin = workerThread ?: return + threadToJoin.interrupt() + try { + threadToJoin.join(timeoutMs) + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + } + workerThread = null + } + + override fun log(level: SessionLogLevel, tag: String, message: String, error: Throwable?) { + when (level) { + SessionLogLevel.DEBUG -> Log.d(tag, message) + SessionLogLevel.INFO -> Log.i(tag, message) + SessionLogLevel.WARN -> if (error != null) Log.w(tag, message, error) else Log.w(tag, message) + SessionLogLevel.ERROR -> if (error != null) Log.e(tag, message, error) else Log.e(tag, message) + } + } + } + + private val core = GlassesSessionCore( + AndroidSessionLink(bleManager), + host, + FrameTimings.getInstance().core, + AndroidProtocolPlatform, + rightAddress, + leftAddress, + ringAddress, + ) + + private val phoneLockReceiver: BroadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + core.onPhoneLockSignal() + } + } + + // Active animated-GIF screen recording, or null when idle. Frames are + // pushed by recordScreenFrame(), which the TS side calls at each + // phone-preview flush. + @Volatile private var screenRecorder: GifScreenRecorder? = null + + init { + bleManager.setListener(core) + val phoneLockFilter = IntentFilter() + phoneLockFilter.addAction(Intent.ACTION_SCREEN_ON) + phoneLockFilter.addAction(Intent.ACTION_SCREEN_OFF) + phoneLockFilter.addAction(Intent.ACTION_USER_PRESENT) + appContext.registerReceiver(phoneLockReceiver, phoneLockFilter) + phoneLockReceiverRegistered = true + } + + fun setListener(listener: FaceclawBleCommunicatorListener?) = core.setListener(listener) + + fun start() { + if (core.start()) { + activeInstance = this + } + } + + fun disconnect() = core.disconnect() + + fun close() { + if (activeInstance === this) { + activeInstance = null + } + core.close() + if (phoneLockReceiverRegistered) { + phoneLockReceiverRegistered = false + appContext.unregisterReceiver(phoneLockReceiver) + } + } + + fun setG2ScreenOn(screenOn: Boolean) = core.setG2ScreenOn(screenOn) + + fun setFirmwareDebugFlags(enabled: Boolean) = core.setFirmwareDebugFlags(enabled) + + fun startG2AudioCapture(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioCapture(listener) + + fun stopG2AudioCapture() = core.stopG2AudioCapture() + + fun isSessionReady(): Boolean = core.isSessionReady() + + /** See [GlassesSessionCore.isAudioCaptureActive]. */ + fun isAudioCaptureActive(): Boolean = core.isAudioCaptureActive() + + fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled) + + fun awaitEvenHubSessionReady(timeoutMs: Int): Boolean = core.awaitEvenHubSessionReady(timeoutMs) + + fun setImuReportEnabled(enable: Boolean, reportFrq: Int) = core.setImuReportEnabled(enable, reportFrq) + + fun setCompassEnabled(enable: Boolean) = core.setCompassEnabled(enable) + + fun setCompassEnabled(owner: String?, enable: Boolean) = core.setCompassEnabled(owner, enable) + + fun setBrightness(autoAdjust: Boolean, brightnessLevel: Int) = core.setBrightness(autoAdjust, brightnessLevel) + fun configureBrightness(auto: Boolean, level: Int, minimum: Int, maximum: Int, curve: String, fadeMs: Int) = + core.configureBrightness(auto, level, minimum, maximum, curve, fadeMs) + + fun enableWearDetectionAndRequestState() = core.enableWearDetectionAndRequestState() + + fun startBandwidthBenchmark(messageSize: Int, windowSize: Int, durationMs: Int): Boolean = + core.startBandwidthBenchmark(messageSize, windowSize, durationMs) + + // 0: current link; 1: re-request HIGH; 2: request 2M; 3: both. + fun startBandwidthBenchmarkWithLinkMode(messageSize: Int, windowSize: Int, + durationMs: Int, linkMode: Int): Boolean = + core.startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, linkMode) + + fun cancelBandwidthBenchmark() = core.cancelBandwidthBenchmark() + + /** Status/results of the current or most recent benchmark run, as JSON. */ + fun getBandwidthBenchmarkStatus(): String = core.getBandwidthBenchmarkStatus() + + fun addImuListener(listener: FaceclawImuListener?) = core.addImuListener(listener) + + fun removeImuListener(listener: FaceclawImuListener?) = core.removeImuListener(listener) + + fun addAmbientLightListener(listener: FaceclawAmbientLightListener?) = core.addAmbientLightListener(listener) + + fun removeAmbientLightListener(listener: FaceclawAmbientLightListener?) = core.removeAmbientLightListener(listener) + + fun queryAmbientLight() = core.queryAmbientLight() + + fun setAmbientLightPolling(enable: Boolean, intervalMs: Int, minDelta: Int, + heartbeatMs: Int, bindToLease: Boolean) = + core.setAmbientLightPolling(enable, intervalMs, minDelta, heartbeatMs, bindToLease) + + fun addMicStatusListener(listener: FaceclawMicStatusListener?) = core.addMicStatusListener(listener) + + fun removeMicStatusListener(listener: FaceclawMicStatusListener?) = core.removeMicStatusListener(listener) + + fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean) = + core.sendFaceclawMicControl(record, label, rightTemple, leftTemple) + + fun startG2AudioForwarding(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioForwarding(listener) + + fun stopG2AudioForwarding() = core.stopG2AudioForwarding() + + fun addCompassListener(listener: FaceclawCompassListener?) = core.addCompassListener(listener) + + fun removeCompassListener(listener: FaceclawCompassListener?) = core.removeCompassListener(listener) + + /** Set the compositor's output frame size. Call before configuring surfaces. */ + fun configureCompositorScreen(width: Int, height: Int) = core.configureCompositorScreen(width, height) + + /** + * The current composited screen as a phone-UI preview bitmap, or null + * before any surface has been configured. Built from the compositor so + * the preview reflects every surface (chrome + whichever app is + * foreground), including worker-app frames the TS side never sees. + */ + fun getCompositePreviewBitmap(brightenGamma: Double): android.graphics.Bitmap? { + return getCompositePreviewBitmap(brightenGamma, false) + } + + /** As above; `green` renders the preview green-on-black (Settings > Phone display > Preview color). */ + fun getCompositePreviewBitmap(brightenGamma: Double, green: Boolean): android.graphics.Bitmap? { + val composite = core.previewComposite() ?: return null + return PreviewBitmapUtil.fromGray( + java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green) + } + + /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(): String { + val composite = core.previewComposite() ?: return "" + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + + /** + * Save the current composite cropped to the given screen rect (the region + * the shell says is actually occupied). The rect is clamped to the screen; + * a degenerate rect falls back to the full screen. + */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): String { + val composite = core.previewComposite() ?: return "" + val x = Math.max(0, cropX) + val y = Math.max(0, cropY) + val width = Math.min(composite.width - x, cropWidth - (x - cropX)) + val height = Math.min(composite.height - y, cropHeight - (y - cropY)) + if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + val cropped = ByteArray(width * height) + for (row in 0 until height) { + System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width) + } + return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height) + } + + /** Begin collecting composite frames for an animated-GIF screen recording. */ + fun startScreenRecording() { + screenRecorder = GifScreenRecorder() + } + + /** Capture the current composite into the active recording; no-op when idle. */ + fun recordScreenFrame() { + val recorder = screenRecorder ?: return + val composite = core.previewComposite() ?: return + recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()) + } + + /** Finish the recording and save it as an animated GIF; returns the path or "". */ + @Throws(java.io.IOException::class) + fun stopScreenRecording(): String { + val recorder = screenRecorder + screenRecorder = null + if (recorder == null) { + return "" + } + if (recorder.isOverflowed()) { + Log.i(TAG, "screen recording hit its frame cap; the tail was dropped") + } + return recorder.save(appContext) + } + + fun setSurfaceVisible(id: String, visible: Boolean) = core.setSurfaceVisible(id, visible) + + fun setSurfaceDepth(id: String, depth: Int) = core.setSurfaceDepth(id, depth) + + fun setScreenBlanked(blanked: Boolean) = core.setScreenBlanked(blanked) + + fun configureSurface(id: String?, x: Int, y: Int, width: Int, height: Int, zOrder: Int, transparency: Int) = + core.configureSurface(id, x, y, width, height, zOrder, transparency) + + fun removeSurface(id: String) = core.removeSurface(id) + + /** Stage an immutable shell snapshot alongside the retained app screen. */ + fun submitShellScene(bytes: java.nio.ByteBuffer, paintMs: Int, frameId: Int) = + core.submitShellScene(AndroidByteReader(bytes), paintMs, frameId) + + /** Legacy compositor dimming for callers without a shell scene. */ + fun setUnderlayDim(belowZOrder: Int, factor256: Int) = core.setUnderlayDim(belowZOrder, factor256) + + /** + * Apply an update to one compositor surface and submit the recomposited + * screen as the desired frame (see [GlassesSessionCore.submitSurfaceFrame]). + * + * pixels8bpp arrives as a ByteBuffer because NativeScript marshals a JS + * ArrayBuffer to one without the per-element bridge copy that a byte[] + * parameter would need (~150ms for a full frame). + */ + fun submitSurfaceFrame( + pixels8bpp: java.nio.ByteBuffer, + surfaceId: String, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String?, + paintMs: Int, + frameId: Int + ) { + submitSurfaceFrame(pixels8bpp, surfaceId, rectX, rectY, rectWidth, rectHeight, + contentFingerprint, paintMs, frameId, null) + } + + /** As above, with the frame's glyph draws (null when the submitter has no glyph metadata). */ + fun submitSurfaceFrame( + pixels8bpp: java.nio.ByteBuffer, + surfaceId: String, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String?, + paintMs: Int, + frameId: Int, + glyphs: java.nio.ByteBuffer? + ) { + core.submitSurfaceFrame(AndroidByteReader(pixels8bpp), surfaceId, rectX, rectY, rectWidth, rectHeight, + contentFingerprint, paintMs, frameId, if (glyphs == null) null else AndroidByteReader(glyphs)) + } + + /** 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()) + if (payload != null) { + payload.get(bytes) + } + core.playBuzzerSequence(bytes) + } + + fun sendShutdown(exitMode: Int): Boolean = core.sendShutdown(exitMode) + + fun sendCfwCleanup(): Boolean = core.sendCfwCleanup() + + fun suspendEvenHubSession(): Boolean = core.suspendEvenHubSession() + + fun resumeEvenHubSession(): Boolean = core.resumeEvenHubSession() + + // --------------------------------------------------------------------- + + private fun updateG2ScreenWakeLock(screenOn: Boolean) { + if (screenOn) { + var wakeLock = g2ScreenWakeLock + if (wakeLock == null) { + wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, G2_SCREEN_WAKE_LOCK_TAG) + wakeLock.setReferenceCounted(false) + g2ScreenWakeLock = wakeLock + } + if (!wakeLock.isHeld) { + wakeLock.acquire() + Log.i(TAG, "G2 screen wake lock acquired") + } + return + } + val wakeLock = g2ScreenWakeLock + if (wakeLock != null && wakeLock.isHeld) { + wakeLock.release() + Log.i(TAG, "G2 screen wake lock released") + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java deleted file mode 100644 index 7531a50e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java +++ /dev/null @@ -1,558 +0,0 @@ -package com.faceclaw.app; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothGatt; -import android.bluetooth.BluetoothGattCallback; -import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothGattDescriptor; -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.List; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -@SuppressLint("MissingPermission") -public class FaceclawBleManager { - private static final String TAG = "FaceclawBle"; - private static final int[] WRITE_RETRY_DELAYS_MS = new int[] {1, 1, 1, 2, 4, 8, 12, 20, 35, 100, 200}; - - private final Context context; - private final BluetoothAdapter bluetoothAdapter; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final ConcurrentHashMap gattClients = new ConcurrentHashMap<>(); - private final Object bluetoothApiLock = new Object(); - - private final ConcurrentHashMap connectLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap connectResults = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap servicesLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap servicesStatuses = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap mtuLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap negotiatedMtus = new ConcurrentHashMap<>(); - private final ConcurrentHashMap mtuStatuses = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap descriptorLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap descriptorStatuses = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap writeLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap writeStatuses = new ConcurrentHashMap<>(); - - private volatile FaceclawBleListener listener; - - // 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. - private static final AtomicLong outboundMessages = new AtomicLong(); - private static final AtomicLong outboundBytes = new AtomicLong(); - private static final AtomicLong displayFramesSent = new AtomicLong(); - - /** Called by the communicator when a display frame's last message is acked. */ - public static void recordDisplayFrameSent() { - displayFramesSent.incrementAndGet(); - } - - /** Running totals of outbound BLE traffic since process start: [messages, bytes, display frames]. */ - public static long[] sampleOutboundTraffic() { - return new long[] { outboundMessages.get(), outboundBytes.get(), displayFramesSent.get() }; - } - - public FaceclawBleManager(Context context) { - this.context = context.getApplicationContext(); - BluetoothManager bluetoothManager = (BluetoothManager) this.context.getSystemService(Context.BLUETOOTH_SERVICE); - if (bluetoothManager == null || bluetoothManager.getAdapter() == null) { - throw new IllegalStateException("Bluetooth adapter unavailable"); - } - this.bluetoothAdapter = bluetoothManager.getAdapter(); - } - - public void setListener(FaceclawBleListener listener) { - this.listener = listener; - } - - /** - * Whether Android still holds a bond (pairing) for this address. Returns - * true when the state cannot be determined, so callers only act on a - * definite BOND_NONE. - */ - public boolean isBonded(String address) { - if (address == null || address.trim().isEmpty()) { - return true; - } - try { - BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); - return device == null || device.getBondState() != BluetoothDevice.BOND_NONE; - } catch (Throwable t) { - return true; - } - } - - /** - * Android's bond state for this address (BluetoothDevice.BOND_NONE / - * BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined. - * BOND_BONDING covers the whole OS pairing flow, including a pairing - * dialog that is still waiting for the user. - */ - public int getBondState(String address) { - if (address == null || address.trim().isEmpty()) { - return -1; - } - try { - BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); - return device == null ? -1 : device.getBondState(); - } catch (Throwable t) { - return -1; - } - } - - /** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */ - public boolean isConnected(String address) { - return address != null && gattClients.containsKey(address); - } - - public boolean connect(String address, int timeoutMs) { - if (address == null || address.trim().isEmpty()) { - throw new IllegalArgumentException("address is required"); - } - CountDownLatch latch = new CountDownLatch(1); - connectLatches.put(address, latch); - connectResults.remove(address); - - Object gattLock = gattLock(address); - BluetoothGatt gatt; - synchronized (gattLock) { - BluetoothGatt existing = gattClients.get(address); - if (existing != null) { - connectLatches.remove(address); - return true; - } - - final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); - if (device == null) { - connectLatches.remove(address); - throw new IllegalArgumentException("remote device not found: " + address); - } - - gatt = device.connectGatt( - context, - false, - gattCallback, - BluetoothDevice.TRANSPORT_LE, - BluetoothDevice.PHY_LE_2M|BluetoothDevice.PHY_LE_1M - ); - if (gatt != null) { - gattClients.put(address, gatt); - } - - if (gatt == null) { - connectLatches.remove(address); - return false; - } - - if (!awaitLatch(latch, timeoutMs)) { - connectLatches.remove(address); - connectResults.remove(address); - gattClients.remove(address, gatt); - negotiatedMtus.remove(address); - gatt.disconnect(); - gatt.close(); - return false; - } - - Boolean connected = connectResults.remove(address); - connectLatches.remove(address); - return Boolean.TRUE.equals(connected); - } - } - - public boolean requestConnectionPriority(String address, int priority) { - synchronized (gattLock(address)) { - BluetoothGatt gatt = requireGatt(address); - return gatt.requestConnectionPriority(priority); - } - } - - /** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */ - public void prepareBenchmarkLink(String address, int mode) { - synchronized (gattLock(address)) { - BluetoothGatt gatt = requireGatt(address); - if ((mode & 1) != 0) { - boolean accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); - Log.i(TAG, "benchmark HIGH address=" + address + " submitted=" + accepted); - } - if ((mode & 2) != 0) { - Log.i(TAG, "benchmark request 2M address=" + address); - gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, - BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED); - } - gatt.readPhy(); - } - } - - public int getNegotiatedMtu(String address) { - Integer mtu = negotiatedMtus.get(address); - return mtu == null ? 23 : mtu; - } - - public boolean requestMtu(String address, int mtu, int timeoutMs) { - synchronized (gattLock(address)) { - CountDownLatch latch = new CountDownLatch(1); - mtuLatches.put(address, latch); - mtuStatuses.remove(address); - - BluetoothGatt gatt = requireGatt(address); - boolean started = gatt.requestMtu(mtu); - if (!started) { - mtuLatches.remove(address); - return false; - } - if (!awaitLatch(latch, timeoutMs)) { - mtuLatches.remove(address); - mtuStatuses.remove(address); - return false; - } - Integer status = mtuStatuses.remove(address); - mtuLatches.remove(address); - return status != null && status == BluetoothGatt.GATT_SUCCESS; - } - } - - public boolean discoverServices(String address, int timeoutMs) { - synchronized (gattLock(address)) { - CountDownLatch latch = new CountDownLatch(1); - servicesLatches.put(address, latch); - servicesStatuses.remove(address); - - BluetoothGatt gatt = requireGatt(address); - boolean started = gatt.discoverServices(); - if (!started) { - servicesLatches.remove(address); - return false; - } - if (!awaitLatch(latch, timeoutMs)) { - servicesLatches.remove(address); - servicesStatuses.remove(address); - return false; - } - Integer status = servicesStatuses.remove(address); - servicesLatches.remove(address); - return status != null && status == BluetoothGatt.GATT_SUCCESS; - } - } - - public boolean enableNotifications(String address, String characteristicUuid, boolean enable, int timeoutMs) { - synchronized (gattLock(address)) { - CountDownLatch latch = new CountDownLatch(1); - descriptorLatches.put(address, latch); - descriptorStatuses.remove(address); - - BluetoothGatt gatt = requireGatt(address); - BluetoothGattCharacteristic characteristic = requireCharacteristic(gatt, characteristicUuid); - - boolean notificationSet = gatt.setCharacteristicNotification(characteristic, enable); - if (!notificationSet) { - descriptorLatches.remove(address); - return false; - } - - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(java.util.UUID.fromString(BleProtocol.CCCD_UUID)); - if (descriptor == null) { - descriptorLatches.remove(address); - return true; - } - - descriptor.setValue(enable - ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE - : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); - boolean started = gatt.writeDescriptor(descriptor); - if (!started) { - descriptorLatches.remove(address); - descriptorStatuses.remove(address); - return false; - } - if (!awaitLatch(latch, timeoutMs)) { - descriptorLatches.remove(address); - descriptorStatuses.remove(address); - return false; - } - Integer status = descriptorStatuses.remove(address); - descriptorLatches.remove(address); - return status != null && status == BluetoothGatt.GATT_SUCCESS; - } - } - - public boolean writeFrames( - String address, - String characteristicUuid, - List frames, - int writeType, - int timeoutMs - ) { - if (frames == null || frames.isEmpty()) { - return true; - } - - long startMs = System.currentTimeMillis(); - synchronized (gattLock(address)) { - BluetoothGatt gatt = requireGatt(address); - BluetoothGattCharacteristic characteristic = requireCharacteristic(gatt, characteristicUuid); - - for (int i = 0; i < frames.size(); i++) { - byte[] frame = frames.get(i); - if (!startWrite(gatt, characteristic, address, frame, writeType, timeoutMs)) { - return false; - } - outboundBytes.addAndGet(frame != null ? frame.length : 0); - } - } - outboundMessages.incrementAndGet(); - int totalSize = frames.stream().mapToInt(frame -> frame != null ? frame.length : 0).sum(); - Log.i(TAG, "writeFrames wrote " + frames.size() + " frames totaling " + totalSize + " bytes in " + (System.currentTimeMillis() - startMs) + "ms"); - return true; - } - - private boolean startWrite( - BluetoothGatt gatt, - BluetoothGattCharacteristic characteristic, - String address, - byte[] data, - int writeType, - int timeoutMs - ) { - int retryCount = 0; - while (true) { - CountDownLatch latch = new CountDownLatch(1); - writeLatches.put(address, latch); - writeStatuses.remove(address); - - long currentTime = System.currentTimeMillis(); - int result = gatt.writeCharacteristic(characteristic, data, writeType); - long timeAsleep = System.currentTimeMillis() - currentTime; - //Log.i(TAG, "writeCharacteristic: spent " + timeAsleep + "ms"); - - if (result == android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY) { - writeLatches.remove(address, latch); - writeStatuses.remove(address); - if (!sleepBeforeWriteRetry(address, "busy", retryCount++)) { - return false; - } - continue; - } else { - //Log.i(TAG, "writeCharacteristic with writeType=" + writeType + " result=" + result + " retryCount=" + retryCount); - if (result != android.bluetooth.BluetoothStatusCodes.SUCCESS) { - writeLatches.remove(address, latch); - writeStatuses.remove(address); - if (!sleepBeforeWriteRetry(address, "start result=" + result, retryCount++)) { - return false; - } - continue; - } - if (!awaitLatch(latch, timeoutMs)) { - writeLatches.remove(address, latch); - writeStatuses.remove(address); - return false; - } - Integer status = writeStatuses.remove(address); - writeLatches.remove(address); - if (status != null && status == BluetoothGatt.GATT_SUCCESS) { - return true; - } - if (!sleepBeforeWriteRetry(address, "callback status=" + status, retryCount++)) { - return false; - } - } - } - } - - private boolean sleepBeforeWriteRetry(String address, String reason, int retryIndex) { - if (retryIndex >= WRITE_RETRY_DELAYS_MS.length) { - Log.w(TAG, "writeCharacteristic retry exhausted: address=" + address + " reason=" + reason); - return false; - } - int delayMs = WRITE_RETRY_DELAYS_MS[retryIndex]; - Log.w(TAG, "writeCharacteristic retry: address=" + address + " reason=" + reason + " delayMs=" + delayMs); - try { - Thread.sleep(delayMs); - return true; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - - public void disconnect(String address) { - Object gattLock = gattLock(address); - synchronized (gattLock) { - BluetoothGatt gatt = gattClients.remove(address); - negotiatedMtus.remove(address); - if (gatt == null) { - return; - } - gatt.disconnect(); - gatt.close(); - } - } - - public void close() { - for (String address : gattClients.keySet()) { - disconnect(address); - } - } - - private BluetoothGatt requireGatt(String address) { - BluetoothGatt gatt = gattClients.get(address); - if (gatt == null) { - throw new IllegalStateException("Not connected: " + address); - } - return gatt; - } - - private BluetoothGattCharacteristic requireCharacteristic(BluetoothGatt gatt, String characteristicUuid) { - UUID target = UUID.fromString(characteristicUuid); - for (BluetoothGattService service : gatt.getServices()) { - BluetoothGattCharacteristic characteristic = service.getCharacteristic(target); - if (characteristic != null) { - return characteristic; - } - } - throw new IllegalStateException("Characteristic not found: " + characteristicUuid); - } - - private boolean awaitLatch(CountDownLatch latch, int timeoutMs) { - try { - return latch.await(timeoutMs, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - - private Object gattLock(String address) { - // 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. - return bluetoothApiLock; - } - - private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { - @Override - public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { - Log.i(TAG, "onConnectionStateChange: status=" + status + " newState=" + newState); - String address = gatt.getDevice().getAddress(); - - if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { - connectResults.put(address, true); - CountDownLatch latch = connectLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - dispatchConnectionState(address, true); - return; - } - - if (newState == BluetoothProfile.STATE_DISCONNECTED) { - connectResults.put(address, false); - CountDownLatch latch = connectLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - Object gattLock = gattLock(address); - synchronized (gattLock) { - gattClients.remove(address, gatt); - negotiatedMtus.remove(address); - gatt.close(); - } - dispatchConnectionState(address, false); - } - } - - @Override - public void onServicesDiscovered(BluetoothGatt gatt, int status) { - String address = gatt.getDevice().getAddress(); - servicesStatuses.put(address, status); - CountDownLatch latch = servicesLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { - String address = gatt.getDevice().getAddress(); - if (status == BluetoothGatt.GATT_SUCCESS) negotiatedMtus.put(address, mtu); - mtuStatuses.put(address, status); - CountDownLatch latch = mtuLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) { - Log.i(TAG, "onPhyRead: address=" + gatt.getDevice().getAddress() - + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status); - } - - @Override - public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) { - Log.i(TAG, "onPhyUpdate: address=" + gatt.getDevice().getAddress() - + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status); - } - - @Override - public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - String address = gatt.getDevice().getAddress(); - descriptorStatuses.put(address, status); - CountDownLatch latch = descriptorLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - String address = gatt.getDevice().getAddress(); - writeStatuses.put(address, status); - CountDownLatch latch = writeLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value) { - dispatchNotification(gatt.getDevice().getAddress(), characteristic.getUuid().toString(), value); - } - - @Deprecated - @Override - public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - dispatchNotification(gatt.getDevice().getAddress(), characteristic.getUuid().toString(), characteristic.getValue()); - } - }; - - private void dispatchConnectionState(String address, boolean connected) { - FaceclawBleListener current = listener; - if (current == null) return; - current.onConnectionStateChange(address, connected); - } - - private void dispatchNotification(String address, String characteristicUuid, byte[] data) { - FaceclawBleListener current = listener; - if (current == null) return; - byte[] copy = data != null ? data.clone() : new byte[0]; - current.onNotification(address, characteristicUuid, copy); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.kt new file mode 100644 index 00000000..2335605f --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.kt @@ -0,0 +1,539 @@ +package com.faceclaw.app + +import android.annotation.SuppressLint +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothGatt +import android.bluetooth.BluetoothGattCallback +import android.bluetooth.BluetoothGattCharacteristic +import android.bluetooth.BluetoothGattDescriptor +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.TimeUnit +import java.util.concurrent.atomic.AtomicLong + +@SuppressLint("MissingPermission") +class FaceclawBleManager(context: Context) { + companion object { + private const val TAG = "FaceclawBle" + private val WRITE_RETRY_DELAYS_MS = intArrayOf(1, 1, 1, 2, 4, 8, 12, 20, 35, 100, 200) + + // 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. + private val outboundMessages = AtomicLong() + private val outboundBytes = AtomicLong() + private val displayFramesSent = AtomicLong() + + /** Called by the communicator when a display frame's last message is acked. */ + @JvmStatic + fun recordDisplayFrameSent() { + displayFramesSent.incrementAndGet() + } + + /** Running totals of outbound BLE traffic since process start: [messages, bytes, display frames]. */ + @JvmStatic + fun sampleOutboundTraffic(): LongArray { + return longArrayOf(outboundMessages.get(), outboundBytes.get(), displayFramesSent.get()) + } + } + + private val context: Context + private val bluetoothAdapter: BluetoothAdapter + private val mainHandler = Handler(Looper.getMainLooper()) + + private val gattClients = ConcurrentHashMap() + private val bluetoothApiLock = Any() + + private val connectLatches = ConcurrentHashMap() + private val connectResults = ConcurrentHashMap() + + private val servicesLatches = ConcurrentHashMap() + private val servicesStatuses = ConcurrentHashMap() + + private val mtuLatches = ConcurrentHashMap() + private val negotiatedMtus = ConcurrentHashMap() + private val mtuStatuses = ConcurrentHashMap() + + private val descriptorLatches = ConcurrentHashMap() + private val descriptorStatuses = ConcurrentHashMap() + + private val writeLatches = ConcurrentHashMap() + private val writeStatuses = ConcurrentHashMap() + + @Volatile + private var listener: FaceclawBleListener? = null + + init { + this.context = context.applicationContext + val bluetoothManager = this.context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager? + if (bluetoothManager == null || bluetoothManager.adapter == null) { + throw IllegalStateException("Bluetooth adapter unavailable") + } + this.bluetoothAdapter = bluetoothManager.adapter + } + + fun setListener(listener: FaceclawBleListener?) { + this.listener = listener + } + + /** + * Whether Android still holds a bond (pairing) for this address. Returns + * true when the state cannot be determined, so callers only act on a + * definite BOND_NONE. + */ + fun isBonded(address: String?): Boolean { + if (address == null || address.trim().isEmpty()) { + return true + } + try { + val device = bluetoothAdapter.getRemoteDevice(address) + return device == null || device.bondState != BluetoothDevice.BOND_NONE + } catch (t: Throwable) { + return true + } + } + + /** + * Android's bond state for this address (BluetoothDevice.BOND_NONE / + * BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined. + * BOND_BONDING covers the whole OS pairing flow, including a pairing + * dialog that is still waiting for the user. + */ + fun getBondState(address: String?): Int { + if (address == null || address.trim().isEmpty()) { + return -1 + } + try { + val device = bluetoothAdapter.getRemoteDevice(address) + return if (device == null) -1 else device.bondState + } catch (t: Throwable) { + return -1 + } + } + + /** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */ + fun isConnected(address: String?): Boolean { + return address != null && gattClients.containsKey(address) + } + + fun connect(address: String?, timeoutMs: Int): Boolean { + if (address == null || address.trim().isEmpty()) { + throw IllegalArgumentException("address is required") + } + val latch = CountDownLatch(1) + connectLatches[address] = latch + connectResults.remove(address) + + val gattLock = gattLock(address) + synchronized(gattLock) { + val existing = gattClients[address] + if (existing != null) { + connectLatches.remove(address) + return true + } + + val device = bluetoothAdapter.getRemoteDevice(address) + if (device == null) { + connectLatches.remove(address) + throw IllegalArgumentException("remote device not found: $address") + } + + val gatt: BluetoothGatt? = device.connectGatt( + context, + false, + gattCallback, + BluetoothDevice.TRANSPORT_LE, + BluetoothDevice.PHY_LE_2M or BluetoothDevice.PHY_LE_1M + ) + if (gatt != null) { + gattClients[address] = gatt + } + + if (gatt == null) { + connectLatches.remove(address) + return false + } + + if (!awaitLatch(latch, timeoutMs)) { + connectLatches.remove(address) + connectResults.remove(address) + gattClients.remove(address, gatt) + negotiatedMtus.remove(address) + gatt.disconnect() + gatt.close() + return false + } + + val connected = connectResults.remove(address) + connectLatches.remove(address) + return connected == true + } + } + + fun requestConnectionPriority(address: String, priority: Int): Boolean { + synchronized(gattLock(address)) { + val gatt = requireGatt(address) + return gatt.requestConnectionPriority(priority) + } + } + + /** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */ + fun prepareBenchmarkLink(address: String, mode: Int) { + synchronized(gattLock(address)) { + val gatt = requireGatt(address) + if ((mode and 1) != 0) { + val accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH) + Log.i(TAG, "benchmark HIGH address=$address submitted=$accepted") + } + if ((mode and 2) != 0) { + Log.i(TAG, "benchmark request 2M address=$address") + gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, + BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED) + } + gatt.readPhy() + } + } + + fun getNegotiatedMtu(address: String): Int { + val mtu = negotiatedMtus[address] + return mtu ?: 23 + } + + 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 + } + } + + 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 + } + } + + fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean { + synchronized(gattLock(address)) { + val latch = CountDownLatch(1) + descriptorLatches[address] = latch + descriptorStatuses.remove(address) + + 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 + } + + descriptor.value = if (enable) + BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE + else + BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE + val started = gatt.writeDescriptor(descriptor) + if (!started) { + descriptorLatches.remove(address) + descriptorStatuses.remove(address) + return false + } + 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 + } + } + + fun writeFrames( + address: String, + characteristicUuid: String, + frames: List?, + writeType: Int, + timeoutMs: Int + ): Boolean { + if (frames == null || frames.isEmpty()) { + return true + } + + val startMs = System.currentTimeMillis() + synchronized(gattLock(address)) { + val gatt = requireGatt(address) + val characteristic = requireCharacteristic(gatt, characteristicUuid) + + for (i in frames.indices) { + val frame = frames[i] + if (!startWrite(gatt, characteristic, address, frame, writeType, timeoutMs)) { + return false + } + outboundBytes.addAndGet((frame?.size ?: 0).toLong()) + } + } + outboundMessages.incrementAndGet() + val totalSize = frames.sumOf { frame -> frame?.size ?: 0 } + Log.i(TAG, "writeFrames wrote " + frames.size + " frames totaling " + totalSize + " bytes in " + (System.currentTimeMillis() - startMs) + "ms") + return true + } + + private fun startWrite( + gatt: BluetoothGatt, + characteristic: BluetoothGattCharacteristic, + address: String, + data: ByteArray?, + writeType: Int, + timeoutMs: Int + ): Boolean { + var retryCount = 0 + while (true) { + val latch = CountDownLatch(1) + writeLatches[address] = latch + writeStatuses.remove(address) + + val currentTime = System.currentTimeMillis() + val result = gatt.writeCharacteristic(characteristic, data!!, writeType) + val timeAsleep = System.currentTimeMillis() - currentTime + //Log.i(TAG, "writeCharacteristic: spent " + timeAsleep + "ms"); + + if (result == android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY) { + writeLatches.remove(address, latch) + writeStatuses.remove(address) + if (!sleepBeforeWriteRetry(address, "busy", retryCount++)) { + return false + } + continue + } else { + //Log.i(TAG, "writeCharacteristic with writeType=" + writeType + " result=" + result + " retryCount=" + retryCount); + if (result != android.bluetooth.BluetoothStatusCodes.SUCCESS) { + writeLatches.remove(address, latch) + writeStatuses.remove(address) + if (!sleepBeforeWriteRetry(address, "start result=$result", retryCount++)) { + return false + } + continue + } + if (!awaitLatch(latch, timeoutMs)) { + writeLatches.remove(address, latch) + writeStatuses.remove(address) + return false + } + val status = writeStatuses.remove(address) + writeLatches.remove(address) + if (status != null && status == BluetoothGatt.GATT_SUCCESS) { + return true + } + if (!sleepBeforeWriteRetry(address, "callback status=$status", retryCount++)) { + return false + } + } + } + } + + private fun sleepBeforeWriteRetry(address: String, reason: String, retryIndex: Int): Boolean { + if (retryIndex >= WRITE_RETRY_DELAYS_MS.size) { + Log.w(TAG, "writeCharacteristic retry exhausted: address=$address reason=$reason") + return false + } + val delayMs = WRITE_RETRY_DELAYS_MS[retryIndex] + Log.w(TAG, "writeCharacteristic retry: address=$address reason=$reason delayMs=$delayMs") + try { + Thread.sleep(delayMs.toLong()) + return true + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + return false + } + } + + fun disconnect(address: String?) { + val gattLock = gattLock(address) + synchronized(gattLock) { + val gatt = if (address != null) gattClients.remove(address) else null + if (address != null) negotiatedMtus.remove(address) + if (gatt == null) { + return + } + gatt.disconnect() + gatt.close() + } + } + + fun close() { + for (address in gattClients.keys) { + disconnect(address) + } + } + + private fun requireGatt(address: String): BluetoothGatt { + val gatt = gattClients[address] + if (gatt == null) { + throw IllegalStateException("Not connected: $address") + } + return gatt + } + + private fun requireCharacteristic(gatt: BluetoothGatt, characteristicUuid: String): BluetoothGattCharacteristic { + val target = UUID.fromString(characteristicUuid) + for (service: BluetoothGattService in gatt.services) { + val characteristic = service.getCharacteristic(target) + if (characteristic != null) { + return characteristic + } + } + throw IllegalStateException("Characteristic not found: $characteristicUuid") + } + + private fun awaitLatch(latch: CountDownLatch, timeoutMs: Int): Boolean { + try { + return latch.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + return false + } + } + + 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. + return bluetoothApiLock + } + + 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) + return + } + + if (newState == BluetoothProfile.STATE_DISCONNECTED) { + connectResults[address] = false + val latch = connectLatches.remove(address) + latch?.countDown() + val gattLock = gattLock(address) + synchronized(gattLock) { + gattClients.remove(address, gatt) + negotiatedMtus.remove(address) + gatt.close() + } + dispatchConnectionState(address, false) + } + } + + override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { + val address = gatt.device.address + servicesStatuses[address] = status + val latch = servicesLatches.remove(address) + latch?.countDown() + } + + 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() + } + + override fun onPhyRead(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) { + Log.i(TAG, "onPhyRead: address=" + gatt.device.address + + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status) + } + + override fun onPhyUpdate(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) { + Log.i(TAG, "onPhyUpdate: address=" + gatt.device.address + + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status) + } + + override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { + val address = gatt.device.address + descriptorStatuses[address] = status + val latch = descriptorLatches.remove(address) + latch?.countDown() + } + + override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) { + val address = gatt.device.address + writeStatuses[address] = status + val latch = writeLatches.remove(address) + latch?.countDown() + } + + override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray) { + dispatchNotification(gatt.device.address, characteristic.uuid.toString(), value) + } + + @Deprecated("Deprecated in Java") + override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { + dispatchNotification(gatt.device.address, characteristic.uuid.toString(), characteristic.value) + } + } + + private fun dispatchConnectionState(address: String, connected: Boolean) { + val current = listener ?: return + current.onConnectionStateChange(address, connected) + } + + private fun dispatchNotification(address: String, characteristicUuid: String, data: ByteArray?) { + val current = listener ?: return + val copy = data?.clone() ?: ByteArray(0) + current.onNotification(address, characteristicUuid, copy) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.java deleted file mode 100644 index fbdb25af..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.faceclaw.app; - -import android.content.ContentUris; -import android.content.Context; -import android.database.Cursor; -import android.net.Uri; -import android.provider.CalendarContract; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -/** - * Reads upcoming events from the Android Calendar provider for the Calendar - * app. Queries the Instances table (rather than Events) so that recurring - * events are expanded into concrete occurrences within the requested window. - * Requires the READ_CALENDAR runtime permission; without it the content - * resolver throws SecurityException and this returns an empty array. - */ -public final class FaceclawCalendarProvider { - private static final String TAG = "FaceclawCalendar"; - - private static final String[] PROJECTION = { - CalendarContract.Instances.EVENT_ID, - CalendarContract.Instances.TITLE, - CalendarContract.Instances.BEGIN, - CalendarContract.Instances.END, - CalendarContract.Instances.ALL_DAY, - CalendarContract.Instances.EVENT_LOCATION, - CalendarContract.Instances.CALENDAR_DISPLAY_NAME, - }; - - private FaceclawCalendarProvider() { - } - - /** - * JSON array of upcoming events starting from now through now+windowMs, - * ordered by start time, capped at maxEvents. Each element carries id, - * title, startMs, endMs, allDay, location, and calendarName. - */ - public static String getUpcomingEventsJson(Context context, int maxEvents, long windowMs) { - if (context == null || maxEvents <= 0) { - return "[]"; - } - long now = System.currentTimeMillis(); - long end = now + Math.max(0L, windowMs); - int limit = Math.min(200, maxEvents); - - Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); - ContentUris.appendId(builder, now); - ContentUris.appendId(builder, end); - Uri uri = builder.build(); - - JSONArray out = new JSONArray(); - Cursor cursor = null; - try { - cursor = context.getContentResolver().query( - uri, - PROJECTION, - null, - null, - CalendarContract.Instances.BEGIN + " ASC"); - if (cursor != null) { - while (cursor.moveToNext() && out.length() < limit) { - try { - out.put(buildEventJson(cursor)); - } catch (JSONException e) { - Log.w(TAG, "failed to serialize calendar event", e); - } - } - } - } catch (SecurityException e) { - Log.w(TAG, "calendar access denied while reading events", e); - return "[]"; - } catch (Throwable t) { - Log.w(TAG, "failed to read calendar events", t); - return "[]"; - } finally { - if (cursor != null) { - cursor.close(); - } - } - return out.toString(); - } - - private static JSONObject buildEventJson(Cursor cursor) throws JSONException { - JSONObject event = new JSONObject(); - event.put("id", cursor.getLong(0)); - event.put("title", cursor.isNull(1) ? "" : cursor.getString(1)); - event.put("startMs", cursor.getLong(2)); - event.put("endMs", cursor.getLong(3)); - event.put("allDay", cursor.getInt(4) != 0); - event.put("location", cursor.isNull(5) ? "" : cursor.getString(5)); - event.put("calendarName", cursor.isNull(6) ? "" : cursor.getString(6)); - return event; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt new file mode 100644 index 00000000..baa36749 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt @@ -0,0 +1,96 @@ +package com.faceclaw.app + +import android.content.ContentUris +import android.content.Context +import android.database.Cursor +import android.provider.CalendarContract +import android.util.Log + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +/** + * Reads upcoming events from the Android Calendar provider for the Calendar + * app. Queries the Instances table (rather than Events) so that recurring + * events are expanded into concrete occurrences within the requested window. + * Requires the READ_CALENDAR runtime permission; without it the content + * resolver throws SecurityException and this returns an empty array. + */ +class FaceclawCalendarProvider private constructor() { + companion object { + private const val TAG = "FaceclawCalendar" + + private val PROJECTION = arrayOf( + CalendarContract.Instances.EVENT_ID, + CalendarContract.Instances.TITLE, + CalendarContract.Instances.BEGIN, + CalendarContract.Instances.END, + CalendarContract.Instances.ALL_DAY, + CalendarContract.Instances.EVENT_LOCATION, + CalendarContract.Instances.CALENDAR_DISPLAY_NAME, + ) + + /** + * JSON array of upcoming events starting from now through now+windowMs, + * ordered by start time, capped at maxEvents. Each element carries id, + * title, startMs, endMs, allDay, location, and calendarName. + */ + @JvmStatic + fun getUpcomingEventsJson(context: Context?, maxEvents: Int, windowMs: Long): String { + if (context == null || maxEvents <= 0) { + return "[]" + } + val now = System.currentTimeMillis() + val end = now + Math.max(0L, windowMs) + val limit = Math.min(200, maxEvents) + + val builder = CalendarContract.Instances.CONTENT_URI.buildUpon() + ContentUris.appendId(builder, now) + ContentUris.appendId(builder, end) + val uri = builder.build() + + val out = JSONArray() + var cursor: Cursor? = null + try { + cursor = context.contentResolver.query( + uri, + PROJECTION, + null, + null, + CalendarContract.Instances.BEGIN + " ASC") + if (cursor != null) { + while (cursor.moveToNext() && out.length() < limit) { + try { + out.put(buildEventJson(cursor)) + } catch (e: JSONException) { + Log.w(TAG, "failed to serialize calendar event", e) + } + } + } + } catch (e: SecurityException) { + Log.w(TAG, "calendar access denied while reading events", e) + return "[]" + } catch (t: Throwable) { + Log.w(TAG, "failed to read calendar events", t) + return "[]" + } finally { + cursor?.close() + } + return out.toString() + } + + @Throws(JSONException::class) + private fun buildEventJson(cursor: Cursor): JSONObject { + val event = JSONObject() + event.put("id", cursor.getLong(0)) + event.put("title", if (cursor.isNull(1)) "" else cursor.getString(1)) + event.put("startMs", cursor.getLong(2)) + event.put("endMs", cursor.getLong(3)) + event.put("allDay", cursor.getInt(4) != 0) + event.put("location", if (cursor.isNull(5)) "" else cursor.getString(5)) + event.put("calendarName", if (cursor.isNull(6)) "" else cursor.getString(6)) + return event + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.java deleted file mode 100644 index 488d2bca..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.java +++ /dev/null @@ -1,446 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.k2fsa.sherpa.onnx.FeatureConfig; -import com.k2fsa.sherpa.onnx.OfflineModelConfig; -import com.k2fsa.sherpa.onnx.OfflineMoonshineModelConfig; -import com.k2fsa.sherpa.onnx.OfflineRecognizer; -import com.k2fsa.sherpa.onnx.OfflineRecognizerConfig; -import com.k2fsa.sherpa.onnx.OfflineRecognizerResult; -import com.k2fsa.sherpa.onnx.OfflineStream; - -import java.io.File; -import java.util.ArrayDeque; -import java.util.Arrays; - -/** - * Continuous captioning over the decoded mic PCM: segments speech into - * utterances with an adaptive energy gate, transcribes each utterance with - * the on-device Moonshine model, and attaches a speaker voice-print per - * utterance. PCM is pushed in from the TS side (which owns mic arbitration - * and any beam-direction gating), so this engine has no BLE dependencies. - * - * Utterance boundaries are measured on the sample clock, not the wall clock: - * BLE delivers mic packets in bursts, so elapsed real time overestimates the - * audio heard. The TS side converts startMs/endMs to wall-clock times using - * the engine start timestamp. - */ -public class FaceclawCaptionEngine { - private static final String TAG = "FaceclawCaptions"; - private static final int SAMPLE_RATE = 16000; - private static final int FEATURE_DIM = 80; - private static final int MAX_QUEUE_PACKETS = 200; - - // Segmentation: RMS thresholds relative to a rolling noise floor, in the - // same spirit as FaceclawVoiceController.EndpointDetector but recurring. - private static final double ONSET_FACTOR = 3.0; - private static final double RELEASE_FACTOR = 1.8; - private static final double MIN_RMS = 220.0; - private static final double NOISE_EMA_ALPHA = 0.05; - private static final int PRE_ROLL_MS = 400; - private static final int DEFAULT_SILENCE_MS = 800; - private static final int MIN_UTTERANCE_MS = 350; - private static final int MAX_UTTERANCE_MS = 15000; - // Moonshine v2 fails past ~9.1 s of input; decode long utterances in - // segments cut at the quietest window (mirrors FaceclawVoiceController). - private static final int DECODE_SEGMENT_MAX_SAMPLES = SAMPLE_RATE * 8; - private static final int CUT_SEARCH_SAMPLES = SAMPLE_RATE * 2; - private static final int CUT_WINDOW_SAMPLES = SAMPLE_RATE * 30 / 1000; - private static final float NORMALIZE_TARGET_PEAK = 0.9f; - private static final float NORMALIZE_MAX_GAIN = 30f; - // Voice-prints degrade on very long inputs; embed at most the first 10 s. - private static final int EMBED_MAX_SAMPLES = SAMPLE_RATE * 10; - - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final Object lock = new Object(); - private final Object queueLock = new Object(); - private final ArrayDeque queue = new ArrayDeque<>(); - - private volatile FaceclawCaptionEngineListener listener; - private volatile String asrModelDir; - private volatile String speakerModelPath; - private volatile int silenceMs = DEFAULT_SILENCE_MS; - private Thread workerThread; - private volatile boolean started; - - private OfflineRecognizer recognizer; - private FaceclawSpeakerId speakerId; - - // Segmentation state (worker thread only). - private long totalSamples; - private double noiseFloor; - private boolean inUtterance; - private long utteranceStartSample; - private long silenceRunSamples; - private double utterancePeakRms; - private short[] utterance = new short[SAMPLE_RATE * (MAX_UTTERANCE_MS / 1000)]; - private int utteranceLength; - private final short[] preRoll = new short[SAMPLE_RATE * PRE_ROLL_MS / 1000]; - private int preRollLength; - - public void setListener(FaceclawCaptionEngineListener listener) { - this.listener = listener; - } - - /** Directory holding the Moonshine model files, or null to disable ASR. */ - public void setAsrModelDir(String dir) { - this.asrModelDir = dir; - } - - /** Speaker-embedding ONNX model path, or null to disable voice-prints. */ - public void setSpeakerModelPath(String path) { - this.speakerModelPath = path; - } - - public void setSilenceMs(int ms) { - this.silenceMs = Math.max(200, Math.min(3000, ms)); - } - - public void start() { - synchronized (lock) { - if (started) { - return; - } - started = true; - workerThread = new Thread(this::runLoop, "FaceclawCaptionEngine"); - workerThread.start(); - } - } - - public void stop() { - Thread threadToJoin; - synchronized (lock) { - if (!started) { - return; - } - started = false; - threadToJoin = workerThread; - workerThread = null; - } - synchronized (queueLock) { - queueLock.notifyAll(); - } - if (threadToJoin != null) { - threadToJoin.interrupt(); - if (Thread.currentThread() != threadToJoin) { - try { - threadToJoin.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - } - - /** Push decoded 16 kHz mono S16LE PCM (any chunking). */ - public void acceptPcm(byte[] pcm16le) { - if (!started || pcm16le == null || pcm16le.length < 2) { - return; - } - synchronized (queueLock) { - if (queue.size() >= MAX_QUEUE_PACKETS) { - queue.removeFirst(); - } - queue.addLast(pcm16le); - queueLock.notifyAll(); - } - } - - private void runLoop() { - try { - loadModels(); - resetSegmentation(); - while (started && !Thread.currentThread().isInterrupted()) { - byte[] chunk = takeChunk(); - if (chunk == null) { - continue; - } - processChunk(chunk); - } - // Flush a trailing utterance so its text isn't lost on stop. - if (inUtterance && utteranceLength > 0) { - finalizeUtterance(); - } - } catch (Throwable t) { - Log.e(TAG, "caption engine failed", t); - emitStatus("Captions failed: " + t.getMessage()); - } finally { - releaseModels(); - } - } - - private void loadModels() { - String modelDir = asrModelDir; - if (modelDir != null && new File(modelDir, "tokens.txt").exists()) { - emitStatus("Loading caption model..."); - recognizer = new OfflineRecognizer(OfflineRecognizerConfig.builder() - .setFeatureConfig(FeatureConfig.builder() - .setSampleRate(SAMPLE_RATE) - .setFeatureDim(FEATURE_DIM) - .build()) - .setModelConfig(OfflineModelConfig.builder() - .setMoonshine(OfflineMoonshineModelConfig.builder() - .setEncoder(new File(modelDir, "encoder_model.ort").getAbsolutePath()) - .setMergedDecoder(new File(modelDir, "decoder_model_merged.ort").getAbsolutePath()) - .build()) - .setTokens(new File(modelDir, "tokens.txt").getAbsolutePath()) - .setNumThreads(1) - .build()) - .build()); - } else { - recognizer = null; - } - String embedModel = speakerModelPath; - if (embedModel != null && new File(embedModel).exists()) { - speakerId = new FaceclawSpeakerId(embedModel); - speakerId.ensureLoaded(); - } else { - speakerId = null; - } - emitStatus(recognizer != null ? "Captions listening..." : "Captions listening (no ASR model)..."); - } - - private void releaseModels() { - if (recognizer != null) { - recognizer.release(); - recognizer = null; - } - if (speakerId != null) { - speakerId.close(); - speakerId = null; - } - } - - private byte[] takeChunk() { - synchronized (queueLock) { - while (started && queue.isEmpty()) { - try { - queueLock.wait(250); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return null; - } - } - return queue.pollFirst(); - } - } - - private void resetSegmentation() { - totalSamples = 0; - noiseFloor = 0; - inUtterance = false; - utteranceLength = 0; - preRollLength = 0; - silenceRunSamples = 0; - utterancePeakRms = 0; - } - - private void processChunk(byte[] chunk) { - int count = chunk.length / 2; - short[] pcm = new short[count]; - double sumSquares = 0; - for (int i = 0; i < count; i++) { - short s = (short) ((chunk[i * 2] & 0xff) | (chunk[i * 2 + 1] << 8)); - pcm[i] = s; - sumSquares += (double) s * s; - } - double rms = Math.sqrt(sumSquares / Math.max(1, count)); - totalSamples += count; - - if (!inUtterance) { - // Track the noise floor only while idle so speech doesn't raise it. - noiseFloor = noiseFloor == 0 ? rms : noiseFloor * (1 - NOISE_EMA_ALPHA) + rms * NOISE_EMA_ALPHA; - double threshold = Math.max(noiseFloor, MIN_RMS); - if (rms >= threshold * ONSET_FACTOR) { - inUtterance = true; - utteranceLength = 0; - silenceRunSamples = 0; - utterancePeakRms = rms; - appendUtterance(preRoll, preRollLength); - utteranceStartSample = Math.max(0, totalSamples - count - preRollLength); - appendUtterance(pcm, count); - emitSpeechStart(utteranceStartSample * 1000 / SAMPLE_RATE); - } else { - appendPreRoll(pcm, count); - } - return; - } - - appendUtterance(pcm, count); - utterancePeakRms = Math.max(utterancePeakRms, rms); - double threshold = Math.max(noiseFloor, MIN_RMS); - if (rms < threshold * RELEASE_FACTOR) { - silenceRunSamples += count; - } else { - silenceRunSamples = 0; - } - long utteranceMs = (long) utteranceLength * 1000 / SAMPLE_RATE; - boolean silenceEnded = silenceRunSamples * 1000L / SAMPLE_RATE >= silenceMs; - if (silenceEnded || utteranceMs >= MAX_UTTERANCE_MS) { - finalizeUtterance(); - inUtterance = false; - preRollLength = 0; - } - } - - private void appendPreRoll(short[] pcm, int count) { - // Keep the last PRE_ROLL_MS of idle audio so onset consonants survive. - if (count >= preRoll.length) { - System.arraycopy(pcm, count - preRoll.length, preRoll, 0, preRoll.length); - preRollLength = preRoll.length; - return; - } - int keep = Math.min(preRollLength, preRoll.length - count); - System.arraycopy(preRoll, preRollLength - keep, preRoll, 0, keep); - System.arraycopy(pcm, 0, preRoll, keep, count); - preRollLength = keep + count; - } - - private void appendUtterance(short[] pcm, int count) { - int room = utterance.length - utteranceLength; - int copied = Math.min(room, count); - if (copied > 0) { - System.arraycopy(pcm, 0, utterance, utteranceLength, copied); - utteranceLength += copied; - } - } - - private void finalizeUtterance() { - int length = utteranceLength; - long startMs = utteranceStartSample * 1000 / SAMPLE_RATE; - long endMs = (utteranceStartSample + length) * 1000 / SAMPLE_RATE; - if (endMs - startMs < MIN_UTTERANCE_MS) { - return; - } - String text = recognizeUtterance(length); - float[] embedding = embedUtterance(length); - emitUtterance(text, embedding, startMs, endMs, utterancePeakRms); - } - - private String recognizeUtterance(int length) { - if (recognizer == null || length <= 0) { - return ""; - } - StringBuilder joined = new StringBuilder(); - int offset = 0; - while (offset < length) { - int remaining = length - offset; - int segment = Math.min(remaining, DECODE_SEGMENT_MAX_SAMPLES); - if (remaining > DECODE_SEGMENT_MAX_SAMPLES) { - segment = findQuietCut(offset, segment); - } - String part = recognizeRange(offset, segment); - if (part.length() > 0) { - if (joined.length() > 0 && ".,!?;:%)]}".indexOf(part.charAt(0)) < 0) { - joined.append(' '); - } - joined.append(part); - } - offset += segment; - } - return joined.toString().trim(); - } - - /** - * End a decode segment at the center of the quietest window near its end - * so the cut lands between words (same approach as the PTT controller). - */ - private int findQuietCut(int offset, int segment) { - int searchStart = Math.max(0, segment - CUT_SEARCH_SAMPLES); - int win = CUT_WINDOW_SAMPLES; - if (segment - searchStart <= win) { - return segment; - } - double sum = 0; - for (int i = searchStart; i < searchStart + win; i++) { - double s = utterance[offset + i]; - sum += s * s; - } - double best = sum; - int bestStart = searchStart; - for (int start = searchStart + 1; start + win <= segment; start++) { - double dropped = utterance[offset + start - 1]; - double added = utterance[offset + start + win - 1]; - sum += added * added - dropped * dropped; - if (sum < best) { - best = sum; - bestStart = start; - } - } - return bestStart + win / 2; - } - - private String recognizeRange(int offset, int count) { - float[] samples = new float[count]; - float peak = 0f; - for (int i = 0; i < count; i++) { - float v = utterance[offset + i] / 32768.0f; - samples[i] = v; - float a = Math.abs(v); - if (a > peak) { - peak = a; - } - } - if (peak > 0f) { - float gain = Math.min(NORMALIZE_TARGET_PEAK / peak, NORMALIZE_MAX_GAIN); - if (gain > 1f) { - for (int i = 0; i < count; i++) { - samples[i] *= gain; - } - } - } - OfflineStream stream = recognizer.createStream(); - try { - stream.acceptWaveform(samples, SAMPLE_RATE); - recognizer.decode(stream); - OfflineRecognizerResult result = recognizer.getResult(stream); - String raw = result == null ? "" : result.getText(); - return raw == null ? "" : raw.trim(); - } finally { - stream.release(); - } - } - - private float[] embedUtterance(int length) { - FaceclawSpeakerId currentSpeakerId = speakerId; - if (currentSpeakerId == null || length <= 0) { - return null; - } - int count = Math.min(length, EMBED_MAX_SAMPLES); - byte[] le = new byte[count * 2]; - for (int i = 0; i < count; i++) { - short s = utterance[i]; - le[i * 2] = (byte) (s & 0xff); - le[i * 2 + 1] = (byte) ((s >> 8) & 0xff); - } - return currentSpeakerId.embed(le, SAMPLE_RATE); - } - - private void emitUtterance(String text, float[] embedding, long startMs, long endMs, double peakRms) { - FaceclawCaptionEngineListener currentListener = listener; - if (currentListener == null) { - return; - } - float[] embeddingCopy = embedding == null ? null : Arrays.copyOf(embedding, embedding.length); - mainHandler.post(() -> currentListener.onUtterance(text, embeddingCopy, startMs, endMs, peakRms)); - } - - private void emitSpeechStart(long startMs) { - FaceclawCaptionEngineListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onSpeechStart(startMs)); - } - - private void emitStatus(String status) { - FaceclawCaptionEngineListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onStatus(status)); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.kt new file mode 100644 index 00000000..d985766e --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.kt @@ -0,0 +1,75 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper + +import java.io.File + +/** + * Continuous captioning over the decoded mic PCM: the shared [CaptionSession] segments speech + * into utterances with an adaptive energy gate, transcribes each with the on-device Moonshine + * model and attaches a speaker voice-print. This Android class only supplies the sherpa-onnx + * recognizer and speaker model and delivers callbacks on the main thread. PCM is pushed in + * from the TS side (which owns mic arbitration and any beam-direction gating), so the engine + * has no BLE dependencies. + * + * Utterance boundaries are measured on the sample clock, not the wall clock; the TS side + * converts startMs/endMs to wall-clock times using the engine start timestamp. + */ +class FaceclawCaptionEngine { + private val mainHandler = Handler(Looper.getMainLooper()) + + private val host = object : CaptionHost { + override val dispatcher = CallbackDispatcher { action -> mainHandler.post { action() } } + + override fun loadTranscriber(modelDir: String): OfflineTranscriber? { + if (!File(modelDir, "tokens.txt").exists()) { + return null + } + return AndroidSpeechEngines.SherpaTranscriber( + AndroidSpeechEngines.recognizerConfig(File(modelDir), VoiceModelKind.MOONSHINE)) + } + + override fun loadEmbedder(modelPath: String): SpeakerEmbedder? { + if (!File(modelPath).exists()) { + return null + } + val speakerId = FaceclawSpeakerId(modelPath) + speakerId.ensureLoaded() + return speakerId + } + } + + private val session = CaptionSession(host) + + fun setListener(listener: FaceclawCaptionEngineListener?) { + session.setListener(listener) + } + + /** Directory holding the Moonshine model files, or null to disable ASR. */ + fun setAsrModelDir(dir: String?) { + session.setAsrModelDir(dir) + } + + /** Speaker-embedding ONNX model path, or null to disable voice-prints. */ + fun setSpeakerModelPath(path: String?) { + session.setSpeakerModelPath(path) + } + + fun setSilenceMs(ms: Int) { + session.setSilenceMs(ms) + } + + fun start() { + session.start() + } + + fun stop() { + session.stop() + } + + /** Push decoded 16 kHz mono S16LE PCM (any chunking). */ + fun acceptPcm(pcm16le: ByteArray?) { + session.acceptPcm(pcm16le) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.java deleted file mode 100644 index 38f723fe..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.java +++ /dev/null @@ -1,778 +0,0 @@ -package com.faceclaw.app; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.util.Base64; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.File; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.ArrayList; -import java.util.List; - -/** - * Persistent store for caption sessions, transcript segments, and speaker - * voice profiles, backing the Microphones app and its phone-side review UI. - * All complex values cross the JS bridge as JSON strings; voice-print - * embeddings are float32 LE blobs, exposed as base64. - */ -public class FaceclawConversationStore extends SQLiteOpenHelper { - private static final String TAG = "FaceclawConvStore"; - private static final String DB_NAME = "faceclaw-conversations.db"; - private static final int DB_VERSION = 2; - - private static FaceclawConversationStore instance; - - public static synchronized FaceclawConversationStore getInstance(Context context) { - if (instance == null) { - instance = new FaceclawConversationStore(context.getApplicationContext()); - } - return instance; - } - - private FaceclawConversationStore(Context context) { - super(context, DB_NAME, null, DB_VERSION); - } - - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE speakers (" - + "id INTEGER PRIMARY KEY AUTOINCREMENT," - + "name TEXT NOT NULL," - + "color TEXT NOT NULL DEFAULT '#4FC3F7'," - + "is_wearer INTEGER NOT NULL DEFAULT 0," - + "embedding BLOB," - + "embedding_count INTEGER NOT NULL DEFAULT 0," - + "created_at INTEGER NOT NULL," - + "last_heard_at INTEGER," - + "tag TEXT," - + "last_recap TEXT," - + "action_items TEXT," - + "facts TEXT," - + "insights_updated_at INTEGER," - + "insights_session_id INTEGER)"); - db.execSQL("CREATE TABLE sessions (" - + "id INTEGER PRIMARY KEY AUTOINCREMENT," - + "started_at INTEGER NOT NULL," - + "ended_at INTEGER," - + "title TEXT," - + "audio_path TEXT," - + "audio_codec TEXT," - + "avg_sentiment REAL," - + "segment_count INTEGER NOT NULL DEFAULT 0)"); - db.execSQL("CREATE TABLE segments (" - + "id INTEGER PRIMARY KEY AUTOINCREMENT," - + "session_id INTEGER NOT NULL," - + "speaker_id INTEGER," - + "started_at INTEGER NOT NULL," - + "ended_at INTEGER NOT NULL," - + "audio_offset_ms INTEGER," - + "text TEXT NOT NULL," - + "lang TEXT," - + "translation TEXT," - + "translation_lang TEXT," - + "sentiment REAL," - + "emotion TEXT," - + "search_meta TEXT," - + "angle INTEGER," - + "embedding BLOB)"); - db.execSQL("CREATE INDEX idx_segments_session ON segments(session_id)"); - db.execSQL("CREATE INDEX idx_segments_speaker ON segments(speaker_id)"); - db.execSQL("CREATE INDEX idx_segments_time ON segments(started_at)"); - db.execSQL("CREATE INDEX idx_sessions_time ON sessions(started_at)"); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion < 2) { - // v2: per-speaker conversation insights (recap of the most recent - // conversation, its action items, and accumulated inferred facts). - db.execSQL("ALTER TABLE speakers ADD COLUMN last_recap TEXT"); - db.execSQL("ALTER TABLE speakers ADD COLUMN action_items TEXT"); - db.execSQL("ALTER TABLE speakers ADD COLUMN facts TEXT"); - db.execSQL("ALTER TABLE speakers ADD COLUMN insights_updated_at INTEGER"); - db.execSQL("ALTER TABLE speakers ADD COLUMN insights_session_id INTEGER"); - } - } - - // ---- sessions ---- - - public long startSession(long startedAtMs, String title) { - ContentValues values = new ContentValues(); - values.put("started_at", startedAtMs); - values.put("title", title == null || title.isEmpty() ? null : title); - return getWritableDatabase().insert("sessions", null, values); - } - - public void endSession(long sessionId, long endedAtMs, String audioPath, String audioCodec, double avgSentiment) { - ContentValues values = new ContentValues(); - values.put("ended_at", endedAtMs); - if (audioPath != null && !audioPath.isEmpty()) { - values.put("audio_path", audioPath); - values.put("audio_codec", audioCodec); - } - values.put("avg_sentiment", avgSentiment); - getWritableDatabase().update("sessions", values, "id=?", new String[]{String.valueOf(sessionId)}); - } - - public void setSessionAudio(long sessionId, String audioPath, String audioCodec) { - ContentValues values = new ContentValues(); - values.put("audio_path", audioPath); - values.put("audio_codec", audioCodec); - getWritableDatabase().update("sessions", values, "id=?", new String[]{String.valueOf(sessionId)}); - } - - public String getSessionAudioPath(long sessionId) { - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT audio_path FROM sessions WHERE id=?", new String[]{String.valueOf(sessionId)})) { - if (c.moveToFirst()) { - return c.isNull(0) ? "" : c.getString(0); - } - } - return ""; - } - - /** - * Sessions, newest first, as JSON. filterJson supports: sinceMs, untilMs, - * speakerId, emotion (matches any segment emotion in the session), query - * (LIKE over title and segment text/search metadata), limit. - */ - public String querySessions(String filterJson) { - try { - JSONObject filter = filterJson == null || filterJson.isEmpty() - ? new JSONObject() : new JSONObject(filterJson); - StringBuilder sql = new StringBuilder( - "SELECT DISTINCT s.id, s.started_at, s.ended_at, s.title, s.audio_path, s.audio_codec," - + " s.avg_sentiment, s.segment_count FROM sessions s"); - List args = new ArrayList<>(); - List where = new ArrayList<>(); - boolean joinSegments = filter.has("speakerId") || filter.has("emotion") || filter.has("query"); - if (joinSegments) { - sql.append(" LEFT JOIN segments g ON g.session_id = s.id"); - } - if (filter.has("sinceMs")) { - where.add("s.started_at >= ?"); - args.add(String.valueOf(filter.getLong("sinceMs"))); - } - if (filter.has("untilMs")) { - where.add("s.started_at <= ?"); - args.add(String.valueOf(filter.getLong("untilMs"))); - } - if (filter.has("speakerId")) { - where.add("g.speaker_id = ?"); - args.add(String.valueOf(filter.getLong("speakerId"))); - } - if (filter.has("emotion")) { - where.add("g.emotion = ?"); - args.add(filter.getString("emotion")); - } - if (filter.has("query")) { - String like = "%" + filter.getString("query") + "%"; - where.add("(s.title LIKE ? OR g.text LIKE ? OR g.search_meta LIKE ?)"); - args.add(like); - args.add(like); - args.add(like); - } - if (!where.isEmpty()) { - sql.append(" WHERE ").append(String.join(" AND ", where)); - } - sql.append(" ORDER BY s.started_at DESC"); - int limit = filter.optInt("limit", 200); - sql.append(" LIMIT ").append(Math.max(1, Math.min(limit, 1000))); - - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[0]))) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - long id = c.getLong(0); - row.put("id", id); - row.put("startedAt", c.getLong(1)); - row.put("endedAt", c.isNull(2) ? JSONObject.NULL : c.getLong(2)); - row.put("title", c.isNull(3) ? "" : c.getString(3)); - row.put("audioPath", c.isNull(4) ? "" : c.getString(4)); - row.put("audioCodec", c.isNull(5) ? "" : c.getString(5)); - row.put("avgSentiment", c.isNull(6) ? 0 : c.getDouble(6)); - row.put("segmentCount", c.getLong(7)); - row.put("speakers", sessionSpeakersJson(id)); - out.put(row); - } - } - return out.toString(); - } catch (JSONException e) { - Log.w(TAG, "querySessions failed", e); - return "[]"; - } - } - - private JSONArray sessionSpeakersJson(long sessionId) throws JSONException { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT DISTINCT p.id, p.name, p.color FROM segments g" - + " JOIN speakers p ON p.id = g.speaker_id WHERE g.session_id=?", - new String[]{String.valueOf(sessionId)})) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("name", c.getString(1)); - row.put("color", c.getString(2)); - out.put(row); - } - } - return out; - } - - // ---- segments ---- - - /** - * Insert one transcript segment. json fields: sessionId, speakerId?, - * startedAt, endedAt, audioOffsetMs?, text, lang?, translation?, - * translationLang?, sentiment?, emotion?, searchMeta?, angle?, - * embeddingBase64?. Returns the new row id, or -1. - */ - public long insertSegment(String json) { - try { - JSONObject s = new JSONObject(json); - ContentValues values = new ContentValues(); - long sessionId = s.getLong("sessionId"); - values.put("session_id", sessionId); - if (s.has("speakerId")) { - values.put("speaker_id", s.getLong("speakerId")); - } - values.put("started_at", s.getLong("startedAt")); - values.put("ended_at", s.getLong("endedAt")); - if (s.has("audioOffsetMs")) { - values.put("audio_offset_ms", s.getLong("audioOffsetMs")); - } - values.put("text", s.getString("text")); - putOptString(values, s, "lang", "lang"); - putOptString(values, s, "translation", "translation"); - putOptString(values, s, "translationLang", "translation_lang"); - if (s.has("sentiment")) { - values.put("sentiment", s.getDouble("sentiment")); - } - putOptString(values, s, "emotion", "emotion"); - putOptString(values, s, "searchMeta", "search_meta"); - if (s.has("angle")) { - values.put("angle", s.getInt("angle")); - } - if (s.has("embeddingBase64")) { - values.put("embedding", Base64.decode(s.getString("embeddingBase64"), Base64.NO_WRAP)); - } - SQLiteDatabase db = getWritableDatabase(); - long id = db.insert("segments", null, values); - db.execSQL("UPDATE sessions SET segment_count = segment_count + 1 WHERE id=?", - new Object[]{sessionId}); - return id; - } catch (Throwable t) { - Log.w(TAG, "insertSegment failed", t); - return -1; - } - } - - private static void putOptString(ContentValues values, JSONObject s, String jsonKey, String column) - throws JSONException { - if (s.has(jsonKey) && !s.isNull(jsonKey)) { - values.put(column, s.getString(jsonKey)); - } - } - - /** Update mutable fields of a segment (same keys as insertSegment). */ - public void updateSegment(long segmentId, String json) { - try { - JSONObject s = new JSONObject(json); - ContentValues values = new ContentValues(); - if (s.has("speakerId")) { - if (s.isNull("speakerId")) { - values.putNull("speaker_id"); - } else { - values.put("speaker_id", s.getLong("speakerId")); - } - } - if (s.has("text")) { - values.put("text", s.getString("text")); - } - putOptString(values, s, "lang", "lang"); - putOptString(values, s, "translation", "translation"); - putOptString(values, s, "translationLang", "translation_lang"); - if (s.has("sentiment")) { - values.put("sentiment", s.getDouble("sentiment")); - } - putOptString(values, s, "emotion", "emotion"); - putOptString(values, s, "searchMeta", "search_meta"); - if (values.size() > 0) { - getWritableDatabase().update("segments", values, "id=?", - new String[]{String.valueOf(segmentId)}); - } - } catch (Throwable t) { - Log.w(TAG, "updateSegment failed", t); - } - } - - public String querySegments(long sessionId) { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," - + " translation, translation_lang, sentiment, emotion, angle" - + " FROM segments WHERE session_id=? ORDER BY started_at ASC", - new String[]{String.valueOf(sessionId)})) { - while (c.moveToNext()) { - out.put(segmentRowJson(c)); - } - } catch (JSONException e) { - Log.w(TAG, "querySegments failed", e); - } - return out.toString(); - } - - private JSONObject segmentRowJson(Cursor c) throws JSONException { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("speakerId", c.isNull(1) ? JSONObject.NULL : c.getLong(1)); - row.put("startedAt", c.getLong(2)); - row.put("endedAt", c.getLong(3)); - row.put("audioOffsetMs", c.isNull(4) ? JSONObject.NULL : c.getLong(4)); - row.put("text", c.getString(5)); - row.put("lang", c.isNull(6) ? "" : c.getString(6)); - row.put("translation", c.isNull(7) ? "" : c.getString(7)); - row.put("translationLang", c.isNull(8) ? "" : c.getString(8)); - row.put("sentiment", c.isNull(9) ? 0 : c.getDouble(9)); - row.put("emotion", c.isNull(10) ? "" : c.getString(10)); - row.put("angle", c.isNull(11) ? JSONObject.NULL : c.getInt(11)); - return row; - } - - /** - * Search segments across sessions. filterJson: query?, emotion?, - * speakerId?, sinceMs?, untilMs?, limit?. Matches text, translation, and - * the sentiment search metadata. - */ - public String searchSegments(String filterJson) { - try { - JSONObject filter = filterJson == null || filterJson.isEmpty() - ? new JSONObject() : new JSONObject(filterJson); - StringBuilder sql = new StringBuilder( - "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," - + " translation, translation_lang, sentiment, emotion, angle, session_id" - + " FROM segments"); - List args = new ArrayList<>(); - List where = new ArrayList<>(); - if (filter.has("query") && !filter.getString("query").isEmpty()) { - String like = "%" + filter.getString("query") + "%"; - where.add("(text LIKE ? OR translation LIKE ? OR search_meta LIKE ?)"); - args.add(like); - args.add(like); - args.add(like); - } - if (filter.has("emotion")) { - where.add("emotion = ?"); - args.add(filter.getString("emotion")); - } - if (filter.has("speakerId")) { - where.add("speaker_id = ?"); - args.add(String.valueOf(filter.getLong("speakerId"))); - } - if (filter.has("sinceMs")) { - where.add("started_at >= ?"); - args.add(String.valueOf(filter.getLong("sinceMs"))); - } - if (filter.has("untilMs")) { - where.add("started_at <= ?"); - args.add(String.valueOf(filter.getLong("untilMs"))); - } - if (!where.isEmpty()) { - sql.append(" WHERE ").append(String.join(" AND ", where)); - } - sql.append(" ORDER BY started_at DESC LIMIT ") - .append(Math.max(1, Math.min(filter.optInt("limit", 300), 1000))); - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[0]))) { - while (c.moveToNext()) { - JSONObject row = segmentRowJson(c); - row.put("sessionId", c.getLong(12)); - out.put(row); - } - } - return out.toString(); - } catch (JSONException e) { - Log.w(TAG, "searchSegments failed", e); - return "[]"; - } - } - - /** Per-segment voice-print embeddings of a session (for re-diarization). */ - public String querySegmentEmbeddings(long sessionId) { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT id, embedding FROM segments WHERE session_id=? AND embedding IS NOT NULL", - new String[]{String.valueOf(sessionId)})) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("embeddingBase64", Base64.encodeToString(c.getBlob(1), Base64.NO_WRAP)); - out.put(row); - } - } catch (JSONException e) { - Log.w(TAG, "querySegmentEmbeddings failed", e); - } - return out.toString(); - } - - // ---- speakers ---- - - public long createSpeaker(String name, String color, boolean isWearer, String embeddingBase64) { - ContentValues values = new ContentValues(); - values.put("name", name); - values.put("color", color == null || color.isEmpty() ? "#4FC3F7" : color); - values.put("is_wearer", isWearer ? 1 : 0); - values.put("created_at", System.currentTimeMillis()); - values.put("last_heard_at", System.currentTimeMillis()); - if (embeddingBase64 != null && !embeddingBase64.isEmpty()) { - values.put("embedding", Base64.decode(embeddingBase64, Base64.NO_WRAP)); - values.put("embedding_count", 1); - } - return getWritableDatabase().insert("speakers", null, values); - } - - public String querySpeakers() { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT p.id, p.name, p.color, p.is_wearer, p.embedding, p.embedding_count," - + " p.last_heard_at, p.tag," - + " p.last_recap, p.action_items, p.facts, p.insights_updated_at, p.insights_session_id," - + " (SELECT COUNT(*) FROM segments g WHERE g.speaker_id = p.id) AS segments" - + " FROM speakers p ORDER BY p.last_heard_at DESC", null)) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("name", c.getString(1)); - row.put("color", c.getString(2)); - row.put("isWearer", c.getInt(3) != 0); - byte[] blob = c.isNull(4) ? null : c.getBlob(4); - row.put("embeddingBase64", blob == null ? "" : Base64.encodeToString(blob, Base64.NO_WRAP)); - row.put("embeddingCount", c.getInt(5)); - row.put("lastHeardAt", c.isNull(6) ? 0 : c.getLong(6)); - row.put("tag", c.isNull(7) ? "" : c.getString(7)); - row.put("lastRecap", c.isNull(8) ? "" : c.getString(8)); - row.put("actionItemsJson", c.isNull(9) ? "" : c.getString(9)); - row.put("factsJson", c.isNull(10) ? "" : c.getString(10)); - row.put("insightsUpdatedAt", c.isNull(11) ? 0 : c.getLong(11)); - row.put("insightsSessionId", c.isNull(12) ? 0 : c.getLong(12)); - row.put("segmentCount", c.getLong(13)); - out.put(row); - } - } catch (JSONException e) { - Log.w(TAG, "querySpeakers failed", e); - } - return out.toString(); - } - - public void renameSpeaker(long speakerId, String name) { - ContentValues values = new ContentValues(); - values.put("name", name); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - public void setSpeakerColor(long speakerId, String color) { - ContentValues values = new ContentValues(); - values.put("color", color); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - public void setSpeakerTag(long speakerId, String tag) { - ContentValues values = new ContentValues(); - values.put("tag", tag); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - /** - * Store LLM-derived conversation insights for a speaker: a recap of their - * most recent conversation, its open action items (JSON string array), and - * the accumulated inferred facts about them (JSON string array). - */ - public void setSpeakerInsights(long speakerId, String recap, String actionItemsJson, - String factsJson, long sessionId) { - ContentValues values = new ContentValues(); - values.put("last_recap", recap == null ? "" : recap); - values.put("action_items", actionItemsJson == null ? "" : actionItemsJson); - values.put("facts", factsJson == null ? "" : factsJson); - values.put("insights_updated_at", System.currentTimeMillis()); - values.put("insights_session_id", sessionId); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - public void setSpeakerWearer(long speakerId, boolean isWearer) { - SQLiteDatabase db = getWritableDatabase(); - if (isWearer) { - // Exactly one wearer profile at a time. - db.execSQL("UPDATE speakers SET is_wearer = 0"); - } - ContentValues values = new ContentValues(); - values.put("is_wearer", isWearer ? 1 : 0); - db.update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - /** - * Running-mean centroid update for a speaker's voice-print, capped so a - * long session cannot drown the enrolled voice. - */ - public void updateSpeakerEmbedding(long speakerId, String embeddingBase64, int maxCount) { - try { - byte[] incoming = Base64.decode(embeddingBase64, Base64.NO_WRAP); - float[] update = blobToFloats(incoming); - if (update == null) { - return; - } - float[] current = null; - int count = 0; - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT embedding, embedding_count FROM speakers WHERE id=?", - new String[]{String.valueOf(speakerId)})) { - if (c.moveToFirst()) { - current = c.isNull(0) ? null : blobToFloats(c.getBlob(0)); - count = c.getInt(1); - } - } - float[] merged; - if (current == null || current.length != update.length) { - merged = update; - count = 1; - } else { - int effective = Math.min(count, Math.max(1, maxCount)); - merged = new float[current.length]; - for (int i = 0; i < merged.length; i++) { - merged[i] = (current[i] * effective + update[i]) / (effective + 1); - } - l2NormalizeInPlace(merged); - count = count + 1; - } - ContentValues values = new ContentValues(); - values.put("embedding", floatsToBlob(merged)); - values.put("embedding_count", count); - values.put("last_heard_at", System.currentTimeMillis()); - getWritableDatabase().update("speakers", values, "id=?", - new String[]{String.valueOf(speakerId)}); - } catch (Throwable t) { - Log.w(TAG, "updateSpeakerEmbedding failed", t); - } - } - - /** Move every segment from one speaker onto another and drop the source. */ - public void mergeSpeakers(long fromId, long intoId) { - if (fromId == intoId) { - return; - } - SQLiteDatabase db = getWritableDatabase(); - db.beginTransaction(); - try { - db.execSQL("UPDATE segments SET speaker_id=? WHERE speaker_id=?", - new Object[]{intoId, fromId}); - // Blend the two centroids weighted by their sample counts. - float[] from = null; - float[] into = null; - int fromCount = 0; - int intoCount = 0; - boolean fromWearer = false; - try (Cursor c = db.rawQuery( - "SELECT id, embedding, embedding_count, is_wearer FROM speakers WHERE id IN (?,?)", - new String[]{String.valueOf(fromId), String.valueOf(intoId)})) { - while (c.moveToNext()) { - long id = c.getLong(0); - float[] embedding = c.isNull(1) ? null : blobToFloats(c.getBlob(1)); - int count = c.getInt(2); - if (id == fromId) { - from = embedding; - fromCount = count; - fromWearer = c.getInt(3) != 0; - } else { - into = embedding; - intoCount = count; - } - } - } - float[] merged = into; - if (from != null && into != null && from.length == into.length) { - merged = new float[into.length]; - int total = Math.max(1, fromCount + intoCount); - for (int i = 0; i < merged.length; i++) { - merged[i] = (into[i] * intoCount + from[i] * fromCount) / total; - } - l2NormalizeInPlace(merged); - } else if (into == null) { - merged = from; - } - ContentValues values = new ContentValues(); - if (merged != null) { - values.put("embedding", floatsToBlob(merged)); - values.put("embedding_count", fromCount + intoCount); - } - if (fromWearer) { - values.put("is_wearer", 1); - } - values.put("last_heard_at", System.currentTimeMillis()); - // Keep the target's conversation insights; adopt the source's only - // where the target has none (e.g. merging a rich profile into a - // freshly auto-created one). - try (Cursor c = db.rawQuery( - "SELECT last_recap, action_items, facts, insights_updated_at, insights_session_id" - + " FROM speakers WHERE id IN (?,?) ORDER BY CASE id WHEN ? THEN 0 ELSE 1 END", - new String[]{String.valueOf(fromId), String.valueOf(intoId), String.valueOf(intoId)})) { - boolean targetHasInsights = false; - while (c.moveToNext()) { - boolean hasInsights = !c.isNull(0) && !c.getString(0).isEmpty(); - if (c.getPosition() == 0) { - targetHasInsights = hasInsights; - } else if (!targetHasInsights && hasInsights) { - values.put("last_recap", c.getString(0)); - values.put("action_items", c.isNull(1) ? "" : c.getString(1)); - values.put("facts", c.isNull(2) ? "" : c.getString(2)); - values.put("insights_updated_at", c.isNull(3) ? 0 : c.getLong(3)); - values.put("insights_session_id", c.isNull(4) ? 0 : c.getLong(4)); - } - } - } - db.update("speakers", values, "id=?", new String[]{String.valueOf(intoId)}); - db.delete("speakers", "id=?", new String[]{String.valueOf(fromId)}); - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - } - - public void reassignSegmentSpeaker(long segmentId, long speakerId) { - ContentValues values = new ContentValues(); - values.put("speaker_id", speakerId); - getWritableDatabase().update("segments", values, "id=?", new String[]{String.valueOf(segmentId)}); - } - - /** Reassign every segment of a speaker within one session (split support). */ - public void reassignSessionSpeaker(long sessionId, long fromSpeakerId, long toSpeakerId) { - getWritableDatabase().execSQL( - "UPDATE segments SET speaker_id=? WHERE session_id=? AND speaker_id=?", - new Object[]{toSpeakerId, sessionId, fromSpeakerId}); - } - - public void deleteSpeaker(long speakerId) { - SQLiteDatabase db = getWritableDatabase(); - db.execSQL("UPDATE segments SET speaker_id=NULL WHERE speaker_id=?", new Object[]{speakerId}); - db.delete("speakers", "id=?", new String[]{String.valueOf(speakerId)}); - } - - // ---- retention ---- - - /** - * Delete caption sessions older than the cutoff (0 disables), and strip - * recordings older than their own cutoff while keeping the transcript. - * Returns a JSON summary of what was removed. - */ - public String applyRetention(long captionCutoffMs, long recordingCutoffMs) { - SQLiteDatabase db = getWritableDatabase(); - int sessionsDeleted = 0; - int recordingsDeleted = 0; - if (recordingCutoffMs > 0) { - List paths = new ArrayList<>(); - try (Cursor c = db.rawQuery( - "SELECT id, audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", - new String[]{String.valueOf(recordingCutoffMs)})) { - while (c.moveToNext()) { - if (!c.isNull(1)) { - paths.add(c.getString(1)); - } - } - } - for (String path : paths) { - if (new File(path).delete()) { - recordingsDeleted++; - } - } - db.execSQL("UPDATE sessions SET audio_path=NULL, audio_codec=NULL WHERE started_at < ?", - new Object[]{recordingCutoffMs}); - } - if (captionCutoffMs > 0) { - List paths = new ArrayList<>(); - try (Cursor c = db.rawQuery( - "SELECT audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", - new String[]{String.valueOf(captionCutoffMs)})) { - while (c.moveToNext()) { - paths.add(c.getString(0)); - } - } - for (String path : paths) { - if (path != null && new File(path).delete()) { - recordingsDeleted++; - } - } - db.execSQL("DELETE FROM segments WHERE session_id IN (SELECT id FROM sessions WHERE started_at < ?)", - new Object[]{captionCutoffMs}); - Cursor count = db.rawQuery("SELECT changes()", null); - count.close(); - db.execSQL("DELETE FROM sessions WHERE started_at < ?", new Object[]{captionCutoffMs}); - try (Cursor c = db.rawQuery("SELECT changes()", null)) { - if (c.moveToFirst()) { - sessionsDeleted = c.getInt(0); - } - } - } - try { - JSONObject summary = new JSONObject(); - summary.put("sessionsDeleted", sessionsDeleted); - summary.put("recordingsDeleted", recordingsDeleted); - return summary.toString(); - } catch (JSONException e) { - return "{}"; - } - } - - public void deleteSession(long sessionId) { - SQLiteDatabase db = getWritableDatabase(); - String path = getSessionAudioPath(sessionId); - if (!path.isEmpty()) { - new File(path).delete(); - } - db.delete("segments", "session_id=?", new String[]{String.valueOf(sessionId)}); - db.delete("sessions", "id=?", new String[]{String.valueOf(sessionId)}); - } - - // ---- blob helpers ---- - - private static float[] blobToFloats(byte[] blob) { - if (blob == null || blob.length < 4 || blob.length % 4 != 0) { - return null; - } - ByteBuffer buffer = ByteBuffer.wrap(blob).order(ByteOrder.LITTLE_ENDIAN); - float[] out = new float[blob.length / 4]; - for (int i = 0; i < out.length; i++) { - out[i] = buffer.getFloat(); - } - return out; - } - - private static byte[] floatsToBlob(float[] values) { - ByteBuffer buffer = ByteBuffer.allocate(values.length * 4).order(ByteOrder.LITTLE_ENDIAN); - for (float v : values) { - buffer.putFloat(v); - } - return buffer.array(); - } - - private static void l2NormalizeInPlace(float[] v) { - double sum = 0; - for (float x : v) { - sum += (double) x * x; - } - double norm = Math.sqrt(sum); - if (norm <= 0) { - return; - } - for (int i = 0; i < v.length; i++) { - v[i] = (float) (v[i] / norm); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.kt new file mode 100644 index 00000000..2d9f316f --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.kt @@ -0,0 +1,730 @@ +package com.faceclaw.app + +import android.content.ContentValues +import android.content.Context +import android.database.Cursor +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import android.util.Base64 +import android.util.Log + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +import java.io.File +import java.util.ArrayList + +/** + * Persistent store for caption sessions, transcript segments, and speaker + * voice profiles, backing the Microphones app and its phone-side review UI. + * All complex values cross the JS bridge as JSON strings; voice-print + * embeddings are float32 LE blobs, exposed as base64. + */ +class FaceclawConversationStore private constructor(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { + companion object { + private const val TAG = "FaceclawConvStore" + private const val DB_NAME = "faceclaw-conversations.db" + private const val DB_VERSION = 2 + + private var instance: FaceclawConversationStore? = null + + @JvmStatic + @Synchronized + fun getInstance(context: Context): FaceclawConversationStore { + if (instance == null) { + instance = FaceclawConversationStore(context.applicationContext) + } + return instance!! + } + + @Throws(JSONException::class) + private fun putOptString(values: ContentValues, s: JSONObject, jsonKey: String, column: String) { + if (s.has(jsonKey) && !s.isNull(jsonKey)) { + values.put(column, s.getString(jsonKey)) + } + } + + } + + override fun onCreate(db: SQLiteDatabase) { + db.execSQL("CREATE TABLE speakers (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "name TEXT NOT NULL," + + "color TEXT NOT NULL DEFAULT '#4FC3F7'," + + "is_wearer INTEGER NOT NULL DEFAULT 0," + + "embedding BLOB," + + "embedding_count INTEGER NOT NULL DEFAULT 0," + + "created_at INTEGER NOT NULL," + + "last_heard_at INTEGER," + + "tag TEXT," + + "last_recap TEXT," + + "action_items TEXT," + + "facts TEXT," + + "insights_updated_at INTEGER," + + "insights_session_id INTEGER)") + db.execSQL("CREATE TABLE sessions (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "started_at INTEGER NOT NULL," + + "ended_at INTEGER," + + "title TEXT," + + "audio_path TEXT," + + "audio_codec TEXT," + + "avg_sentiment REAL," + + "segment_count INTEGER NOT NULL DEFAULT 0)") + db.execSQL("CREATE TABLE segments (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "session_id INTEGER NOT NULL," + + "speaker_id INTEGER," + + "started_at INTEGER NOT NULL," + + "ended_at INTEGER NOT NULL," + + "audio_offset_ms INTEGER," + + "text TEXT NOT NULL," + + "lang TEXT," + + "translation TEXT," + + "translation_lang TEXT," + + "sentiment REAL," + + "emotion TEXT," + + "search_meta TEXT," + + "angle INTEGER," + + "embedding BLOB)") + db.execSQL("CREATE INDEX idx_segments_session ON segments(session_id)") + db.execSQL("CREATE INDEX idx_segments_speaker ON segments(speaker_id)") + db.execSQL("CREATE INDEX idx_segments_time ON segments(started_at)") + db.execSQL("CREATE INDEX idx_sessions_time ON sessions(started_at)") + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + if (oldVersion < 2) { + // v2: per-speaker conversation insights (recap of the most recent + // conversation, its action items, and accumulated inferred facts). + db.execSQL("ALTER TABLE speakers ADD COLUMN last_recap TEXT") + db.execSQL("ALTER TABLE speakers ADD COLUMN action_items TEXT") + db.execSQL("ALTER TABLE speakers ADD COLUMN facts TEXT") + db.execSQL("ALTER TABLE speakers ADD COLUMN insights_updated_at INTEGER") + db.execSQL("ALTER TABLE speakers ADD COLUMN insights_session_id INTEGER") + } + } + + // ---- sessions ---- + + fun startSession(startedAtMs: Long, title: String?): Long { + val values = ContentValues() + values.put("started_at", startedAtMs) + values.put("title", if (title == null || title.isEmpty()) null else title) + return writableDatabase.insert("sessions", null, values) + } + + fun endSession(sessionId: Long, endedAtMs: Long, audioPath: String?, audioCodec: String?, avgSentiment: Double) { + val values = ContentValues() + values.put("ended_at", endedAtMs) + if (audioPath != null && !audioPath.isEmpty()) { + values.put("audio_path", audioPath) + values.put("audio_codec", audioCodec) + } + values.put("avg_sentiment", avgSentiment) + writableDatabase.update("sessions", values, "id=?", arrayOf(sessionId.toString())) + } + + fun setSessionAudio(sessionId: Long, audioPath: String?, audioCodec: String?) { + val values = ContentValues() + values.put("audio_path", audioPath) + values.put("audio_codec", audioCodec) + writableDatabase.update("sessions", values, "id=?", arrayOf(sessionId.toString())) + } + + fun getSessionAudioPath(sessionId: Long): String { + readableDatabase.rawQuery( + "SELECT audio_path FROM sessions WHERE id=?", arrayOf(sessionId.toString())).use { c -> + if (c.moveToFirst()) { + return if (c.isNull(0)) "" else c.getString(0) + } + } + return "" + } + + /** + * Sessions, newest first, as JSON. filterJson supports: sinceMs, untilMs, + * speakerId, emotion (matches any segment emotion in the session), query + * (LIKE over title and segment text/search metadata), limit. + */ + fun querySessions(filterJson: String?): String { + try { + val filter = if (filterJson == null || filterJson.isEmpty()) + JSONObject() else JSONObject(filterJson) + val sql = StringBuilder( + "SELECT DISTINCT s.id, s.started_at, s.ended_at, s.title, s.audio_path, s.audio_codec," + + " s.avg_sentiment, s.segment_count FROM sessions s") + val args: MutableList = ArrayList() + val where: MutableList = ArrayList() + val joinSegments = filter.has("speakerId") || filter.has("emotion") || filter.has("query") + if (joinSegments) { + sql.append(" LEFT JOIN segments g ON g.session_id = s.id") + } + if (filter.has("sinceMs")) { + where.add("s.started_at >= ?") + args.add(filter.getLong("sinceMs").toString()) + } + if (filter.has("untilMs")) { + where.add("s.started_at <= ?") + args.add(filter.getLong("untilMs").toString()) + } + if (filter.has("speakerId")) { + where.add("g.speaker_id = ?") + args.add(filter.getLong("speakerId").toString()) + } + if (filter.has("emotion")) { + where.add("g.emotion = ?") + args.add(filter.getString("emotion")) + } + if (filter.has("query")) { + val like = "%" + filter.getString("query") + "%" + where.add("(s.title LIKE ? OR g.text LIKE ? OR g.search_meta LIKE ?)") + args.add(like) + args.add(like) + args.add(like) + } + if (!where.isEmpty()) { + sql.append(" WHERE ").append(where.joinToString(" AND ")) + } + sql.append(" ORDER BY s.started_at DESC") + val limit = filter.optInt("limit", 200) + sql.append(" LIMIT ").append(Math.max(1, Math.min(limit, 1000))) + + val out = JSONArray() + readableDatabase.rawQuery(sql.toString(), args.toTypedArray()).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + val id = c.getLong(0) + row.put("id", id) + row.put("startedAt", c.getLong(1)) + row.put("endedAt", if (c.isNull(2)) JSONObject.NULL else c.getLong(2)) + row.put("title", if (c.isNull(3)) "" else c.getString(3)) + row.put("audioPath", if (c.isNull(4)) "" else c.getString(4)) + row.put("audioCodec", if (c.isNull(5)) "" else c.getString(5)) + row.put("avgSentiment", if (c.isNull(6)) 0.0 else c.getDouble(6)) + row.put("segmentCount", c.getLong(7)) + row.put("speakers", sessionSpeakersJson(id)) + out.put(row) + } + } + return out.toString() + } catch (e: JSONException) { + Log.w(TAG, "querySessions failed", e) + return "[]" + } + } + + @Throws(JSONException::class) + private fun sessionSpeakersJson(sessionId: Long): JSONArray { + val out = JSONArray() + readableDatabase.rawQuery( + "SELECT DISTINCT p.id, p.name, p.color FROM segments g" + + " JOIN speakers p ON p.id = g.speaker_id WHERE g.session_id=?", + arrayOf(sessionId.toString())).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("name", c.getString(1)) + row.put("color", c.getString(2)) + out.put(row) + } + } + return out + } + + // ---- segments ---- + + /** + * Insert one transcript segment. json fields: sessionId, speakerId?, + * startedAt, endedAt, audioOffsetMs?, text, lang?, translation?, + * translationLang?, sentiment?, emotion?, searchMeta?, angle?, + * embeddingBase64?. Returns the new row id, or -1. + */ + fun insertSegment(json: String?): Long { + try { + val s = JSONObject(json) + val values = ContentValues() + val sessionId = s.getLong("sessionId") + values.put("session_id", sessionId) + if (s.has("speakerId")) { + values.put("speaker_id", s.getLong("speakerId")) + } + values.put("started_at", s.getLong("startedAt")) + values.put("ended_at", s.getLong("endedAt")) + if (s.has("audioOffsetMs")) { + values.put("audio_offset_ms", s.getLong("audioOffsetMs")) + } + values.put("text", s.getString("text")) + putOptString(values, s, "lang", "lang") + putOptString(values, s, "translation", "translation") + putOptString(values, s, "translationLang", "translation_lang") + if (s.has("sentiment")) { + values.put("sentiment", s.getDouble("sentiment")) + } + putOptString(values, s, "emotion", "emotion") + putOptString(values, s, "searchMeta", "search_meta") + if (s.has("angle")) { + values.put("angle", s.getInt("angle")) + } + if (s.has("embeddingBase64")) { + values.put("embedding", Base64.decode(s.getString("embeddingBase64"), Base64.NO_WRAP)) + } + val db = writableDatabase + val id = db.insert("segments", null, values) + db.execSQL("UPDATE sessions SET segment_count = segment_count + 1 WHERE id=?", + arrayOf(sessionId)) + return id + } catch (t: Throwable) { + Log.w(TAG, "insertSegment failed", t) + return -1 + } + } + + /** Update mutable fields of a segment (same keys as insertSegment). */ + fun updateSegment(segmentId: Long, json: String?) { + try { + val s = JSONObject(json) + val values = ContentValues() + if (s.has("speakerId")) { + if (s.isNull("speakerId")) { + values.putNull("speaker_id") + } else { + values.put("speaker_id", s.getLong("speakerId")) + } + } + if (s.has("text")) { + values.put("text", s.getString("text")) + } + putOptString(values, s, "lang", "lang") + putOptString(values, s, "translation", "translation") + putOptString(values, s, "translationLang", "translation_lang") + if (s.has("sentiment")) { + values.put("sentiment", s.getDouble("sentiment")) + } + putOptString(values, s, "emotion", "emotion") + putOptString(values, s, "searchMeta", "search_meta") + if (values.size() > 0) { + writableDatabase.update("segments", values, "id=?", + arrayOf(segmentId.toString())) + } + } catch (t: Throwable) { + Log.w(TAG, "updateSegment failed", t) + } + } + + fun querySegments(sessionId: Long): String { + val out = JSONArray() + try { + readableDatabase.rawQuery( + "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," + + " translation, translation_lang, sentiment, emotion, angle" + + " FROM segments WHERE session_id=? ORDER BY started_at ASC", + arrayOf(sessionId.toString())).use { c -> + while (c.moveToNext()) { + out.put(segmentRowJson(c)) + } + } + } catch (e: JSONException) { + Log.w(TAG, "querySegments failed", e) + } + return out.toString() + } + + @Throws(JSONException::class) + private fun segmentRowJson(c: Cursor): JSONObject { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("speakerId", if (c.isNull(1)) JSONObject.NULL else c.getLong(1)) + row.put("startedAt", c.getLong(2)) + row.put("endedAt", c.getLong(3)) + row.put("audioOffsetMs", if (c.isNull(4)) JSONObject.NULL else c.getLong(4)) + row.put("text", c.getString(5)) + row.put("lang", if (c.isNull(6)) "" else c.getString(6)) + row.put("translation", if (c.isNull(7)) "" else c.getString(7)) + row.put("translationLang", if (c.isNull(8)) "" else c.getString(8)) + row.put("sentiment", if (c.isNull(9)) 0.0 else c.getDouble(9)) + row.put("emotion", if (c.isNull(10)) "" else c.getString(10)) + row.put("angle", if (c.isNull(11)) JSONObject.NULL else c.getInt(11)) + return row + } + + /** + * Search segments across sessions. filterJson: query?, emotion?, + * speakerId?, sinceMs?, untilMs?, limit?. Matches text, translation, and + * the sentiment search metadata. + */ + fun searchSegments(filterJson: String?): String { + try { + val filter = if (filterJson == null || filterJson.isEmpty()) + JSONObject() else JSONObject(filterJson) + val sql = StringBuilder( + "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," + + " translation, translation_lang, sentiment, emotion, angle, session_id" + + " FROM segments") + val args: MutableList = ArrayList() + val where: MutableList = ArrayList() + if (filter.has("query") && !filter.getString("query").isEmpty()) { + val like = "%" + filter.getString("query") + "%" + where.add("(text LIKE ? OR translation LIKE ? OR search_meta LIKE ?)") + args.add(like) + args.add(like) + args.add(like) + } + if (filter.has("emotion")) { + where.add("emotion = ?") + args.add(filter.getString("emotion")) + } + if (filter.has("speakerId")) { + where.add("speaker_id = ?") + args.add(filter.getLong("speakerId").toString()) + } + if (filter.has("sinceMs")) { + where.add("started_at >= ?") + args.add(filter.getLong("sinceMs").toString()) + } + if (filter.has("untilMs")) { + where.add("started_at <= ?") + args.add(filter.getLong("untilMs").toString()) + } + if (!where.isEmpty()) { + sql.append(" WHERE ").append(where.joinToString(" AND ")) + } + sql.append(" ORDER BY started_at DESC LIMIT ") + .append(Math.max(1, Math.min(filter.optInt("limit", 300), 1000))) + val out = JSONArray() + readableDatabase.rawQuery(sql.toString(), args.toTypedArray()).use { c -> + while (c.moveToNext()) { + val row = segmentRowJson(c) + row.put("sessionId", c.getLong(12)) + out.put(row) + } + } + return out.toString() + } catch (e: JSONException) { + Log.w(TAG, "searchSegments failed", e) + return "[]" + } + } + + /** Per-segment voice-print embeddings of a session (for re-diarization). */ + fun querySegmentEmbeddings(sessionId: Long): String { + val out = JSONArray() + try { + readableDatabase.rawQuery( + "SELECT id, embedding FROM segments WHERE session_id=? AND embedding IS NOT NULL", + arrayOf(sessionId.toString())).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("embeddingBase64", Base64.encodeToString(c.getBlob(1), Base64.NO_WRAP)) + out.put(row) + } + } + } catch (e: JSONException) { + Log.w(TAG, "querySegmentEmbeddings failed", e) + } + return out.toString() + } + + // ---- speakers ---- + + fun createSpeaker(name: String?, color: String?, isWearer: Boolean, embeddingBase64: String?): Long { + val values = ContentValues() + values.put("name", name) + values.put("color", if (color == null || color.isEmpty()) "#4FC3F7" else color) + values.put("is_wearer", if (isWearer) 1 else 0) + values.put("created_at", System.currentTimeMillis()) + values.put("last_heard_at", System.currentTimeMillis()) + if (embeddingBase64 != null && !embeddingBase64.isEmpty()) { + values.put("embedding", Base64.decode(embeddingBase64, Base64.NO_WRAP)) + values.put("embedding_count", 1) + } + return writableDatabase.insert("speakers", null, values) + } + + fun querySpeakers(): String { + val out = JSONArray() + try { + readableDatabase.rawQuery( + "SELECT p.id, p.name, p.color, p.is_wearer, p.embedding, p.embedding_count," + + " p.last_heard_at, p.tag," + + " p.last_recap, p.action_items, p.facts, p.insights_updated_at, p.insights_session_id," + + " (SELECT COUNT(*) FROM segments g WHERE g.speaker_id = p.id) AS segments" + + " FROM speakers p ORDER BY p.last_heard_at DESC", null).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("name", c.getString(1)) + row.put("color", c.getString(2)) + row.put("isWearer", c.getInt(3) != 0) + val blob = if (c.isNull(4)) null else c.getBlob(4) + row.put("embeddingBase64", if (blob == null) "" else Base64.encodeToString(blob, Base64.NO_WRAP)) + row.put("embeddingCount", c.getInt(5)) + row.put("lastHeardAt", if (c.isNull(6)) 0L else c.getLong(6)) + row.put("tag", if (c.isNull(7)) "" else c.getString(7)) + row.put("lastRecap", if (c.isNull(8)) "" else c.getString(8)) + row.put("actionItemsJson", if (c.isNull(9)) "" else c.getString(9)) + row.put("factsJson", if (c.isNull(10)) "" else c.getString(10)) + row.put("insightsUpdatedAt", if (c.isNull(11)) 0L else c.getLong(11)) + row.put("insightsSessionId", if (c.isNull(12)) 0L else c.getLong(12)) + row.put("segmentCount", c.getLong(13)) + out.put(row) + } + } + } catch (e: JSONException) { + Log.w(TAG, "querySpeakers failed", e) + } + return out.toString() + } + + fun renameSpeaker(speakerId: Long, name: String?) { + val values = ContentValues() + values.put("name", name) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + fun setSpeakerColor(speakerId: Long, color: String?) { + val values = ContentValues() + values.put("color", color) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + fun setSpeakerTag(speakerId: Long, tag: String?) { + val values = ContentValues() + values.put("tag", tag) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + /** + * Store LLM-derived conversation insights for a speaker: a recap of their + * most recent conversation, its open action items (JSON string array), and + * the accumulated inferred facts about them (JSON string array). + */ + fun setSpeakerInsights(speakerId: Long, recap: String?, actionItemsJson: String?, + factsJson: String?, sessionId: Long) { + val values = ContentValues() + values.put("last_recap", recap ?: "") + values.put("action_items", actionItemsJson ?: "") + values.put("facts", factsJson ?: "") + values.put("insights_updated_at", System.currentTimeMillis()) + values.put("insights_session_id", sessionId) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + fun setSpeakerWearer(speakerId: Long, isWearer: Boolean) { + val db = writableDatabase + if (isWearer) { + // Exactly one wearer profile at a time. + db.execSQL("UPDATE speakers SET is_wearer = 0") + } + val values = ContentValues() + values.put("is_wearer", if (isWearer) 1 else 0) + db.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + /** + * Running-mean centroid update for a speaker's voice-print, capped so a + * long session cannot drown the enrolled voice. + */ + fun updateSpeakerEmbedding(speakerId: Long, embeddingBase64: String?, maxCount: Int) { + try { + val incoming = Base64.decode(embeddingBase64, Base64.NO_WRAP) + val update = SpeakerEmbeddings.blobToFloats(incoming) + if (update == null) { + return + } + var current: FloatArray? = null + var count = 0 + val c = readableDatabase.rawQuery( + "SELECT embedding, embedding_count FROM speakers WHERE id=?", + arrayOf(speakerId.toString())) + try { + if (c.moveToFirst()) { + current = if (c.isNull(0)) null else SpeakerEmbeddings.blobToFloats(c.getBlob(0)) + count = c.getInt(1) + } + } finally { + c.close() + } + val centroid = SpeakerEmbeddings.runningMean(current, count, update, maxCount) + val values = ContentValues() + values.put("embedding", SpeakerEmbeddings.floatsToBlob(centroid.embedding)) + values.put("embedding_count", centroid.count) + values.put("last_heard_at", System.currentTimeMillis()) + writableDatabase.update("speakers", values, "id=?", + arrayOf(speakerId.toString())) + } catch (t: Throwable) { + Log.w(TAG, "updateSpeakerEmbedding failed", t) + } + } + + /** Move every segment from one speaker onto another and drop the source. */ + fun mergeSpeakers(fromId: Long, intoId: Long) { + if (fromId == intoId) { + return + } + val db = writableDatabase + db.beginTransaction() + try { + db.execSQL("UPDATE segments SET speaker_id=? WHERE speaker_id=?", + arrayOf(intoId, fromId)) + // Blend the two centroids weighted by their sample counts. + var from: FloatArray? = null + var into: FloatArray? = null + var fromCount = 0 + var intoCount = 0 + var fromWearer = false + val c = db.rawQuery( + "SELECT id, embedding, embedding_count, is_wearer FROM speakers WHERE id IN (?,?)", + arrayOf(fromId.toString(), intoId.toString())) + try { + while (c.moveToNext()) { + val id = c.getLong(0) + val embedding = if (c.isNull(1)) null else SpeakerEmbeddings.blobToFloats(c.getBlob(1)) + val count = c.getInt(2) + if (id == fromId) { + from = embedding + fromCount = count + fromWearer = c.getInt(3) != 0 + } else { + into = embedding + intoCount = count + } + } + } finally { + c.close() + } + val merged = SpeakerEmbeddings.blend(from, fromCount, into, intoCount) + val values = ContentValues() + if (merged != null) { + values.put("embedding", SpeakerEmbeddings.floatsToBlob(merged)) + values.put("embedding_count", fromCount + intoCount) + } + if (fromWearer) { + values.put("is_wearer", 1) + } + values.put("last_heard_at", System.currentTimeMillis()) + // Keep the target's conversation insights; adopt the source's only + // where the target has none (e.g. merging a rich profile into a + // freshly auto-created one). + db.rawQuery( + "SELECT last_recap, action_items, facts, insights_updated_at, insights_session_id" + + " FROM speakers WHERE id IN (?,?) ORDER BY CASE id WHEN ? THEN 0 ELSE 1 END", + arrayOf(fromId.toString(), intoId.toString(), intoId.toString())).use { c2 -> + var targetHasInsights = false + while (c2.moveToNext()) { + val hasInsights = !c2.isNull(0) && !c2.getString(0).isEmpty() + if (c2.position == 0) { + targetHasInsights = hasInsights + } else if (SpeakerEmbeddings.shouldAdoptInsights(targetHasInsights, hasInsights)) { + values.put("last_recap", c2.getString(0)) + values.put("action_items", if (c2.isNull(1)) "" else c2.getString(1)) + values.put("facts", if (c2.isNull(2)) "" else c2.getString(2)) + values.put("insights_updated_at", if (c2.isNull(3)) 0L else c2.getLong(3)) + values.put("insights_session_id", if (c2.isNull(4)) 0L else c2.getLong(4)) + } + } + } + db.update("speakers", values, "id=?", arrayOf(intoId.toString())) + db.delete("speakers", "id=?", arrayOf(fromId.toString())) + db.setTransactionSuccessful() + } finally { + db.endTransaction() + } + } + + fun reassignSegmentSpeaker(segmentId: Long, speakerId: Long) { + val values = ContentValues() + values.put("speaker_id", speakerId) + writableDatabase.update("segments", values, "id=?", arrayOf(segmentId.toString())) + } + + /** Reassign every segment of a speaker within one session (split support). */ + fun reassignSessionSpeaker(sessionId: Long, fromSpeakerId: Long, toSpeakerId: Long) { + writableDatabase.execSQL( + "UPDATE segments SET speaker_id=? WHERE session_id=? AND speaker_id=?", + arrayOf(toSpeakerId, sessionId, fromSpeakerId)) + } + + fun deleteSpeaker(speakerId: Long) { + val db = writableDatabase + db.execSQL("UPDATE segments SET speaker_id=NULL WHERE speaker_id=?", arrayOf(speakerId)) + db.delete("speakers", "id=?", arrayOf(speakerId.toString())) + } + + // ---- retention ---- + + /** + * Delete caption sessions older than the cutoff (0 disables), and strip + * recordings older than their own cutoff while keeping the transcript. + * Returns a JSON summary of what was removed. + */ + fun applyRetention(captionCutoffMs: Long, recordingCutoffMs: Long): String { + val db = writableDatabase + var sessionsDeleted = 0 + var recordingsDeleted = 0 + if (recordingCutoffMs > 0) { + val paths: MutableList = ArrayList() + db.rawQuery( + "SELECT id, audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", + arrayOf(recordingCutoffMs.toString())).use { c -> + while (c.moveToNext()) { + if (!c.isNull(1)) { + paths.add(c.getString(1)) + } + } + } + for (path in paths) { + if (File(path).delete()) { + recordingsDeleted++ + } + } + db.execSQL("UPDATE sessions SET audio_path=NULL, audio_codec=NULL WHERE started_at < ?", + arrayOf(recordingCutoffMs)) + } + if (captionCutoffMs > 0) { + val paths: MutableList = ArrayList() + db.rawQuery( + "SELECT audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", + arrayOf(captionCutoffMs.toString())).use { c -> + while (c.moveToNext()) { + paths.add(c.getString(0)) + } + } + for (path in paths) { + if (path != null && File(path).delete()) { + recordingsDeleted++ + } + } + db.execSQL("DELETE FROM segments WHERE session_id IN (SELECT id FROM sessions WHERE started_at < ?)", + arrayOf(captionCutoffMs)) + val count = db.rawQuery("SELECT changes()", null) + count.close() + db.execSQL("DELETE FROM sessions WHERE started_at < ?", arrayOf(captionCutoffMs)) + db.rawQuery("SELECT changes()", null).use { c -> + if (c.moveToFirst()) { + sessionsDeleted = c.getInt(0) + } + } + } + try { + val summary = JSONObject() + summary.put("sessionsDeleted", sessionsDeleted) + summary.put("recordingsDeleted", recordingsDeleted) + return summary.toString() + } catch (e: JSONException) { + return "{}" + } + } + + fun deleteSession(sessionId: Long) { + val db = writableDatabase + val path = getSessionAudioPath(sessionId) + if (!path.isEmpty()) { + File(path).delete() + } + db.delete("segments", "session_id=?", arrayOf(sessionId.toString())) + db.delete("sessions", "id=?", arrayOf(sessionId.toString())) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.java deleted file mode 100644 index 53645f27..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.java +++ /dev/null @@ -1,386 +0,0 @@ -package com.faceclaw.app; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothManager; -import android.bluetooth.le.BluetoothLeScanner; -import android.bluetooth.le.ScanCallback; -import android.bluetooth.le.ScanRecord; -import android.bluetooth.le.ScanResult; -import android.bluetooth.le.ScanSettings; -import android.content.Context; -import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.util.SparseArray; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -/** - * Finds Even Realities hardware over BLE and hands every advertisement up to - * TypeScript with the raw fields the pairing screen needs: the LIVE local name - * from the scan record (BluetoothDevice.getName() is a cache that can hold a - * pre-reset value indefinitely), the full manufacturer-specific payload with - * its company identifier restored so the "ER"+serial+MAC layout survives - * intact, RSSI, advertised TX power, and connectability. - * - * Admission is deliberately loose — a name containing G2, the stock - * "EVEN R1…" ring prefix, or the Even "ER" manufacturer signature — because - * renamed custom firmware may drop the stock name. Side, serial, and pair - * matching are decided in TypeScript (app/g2/even-advertisement.ts, - * app/g2/pairing-candidates.ts) where they can be unit-tested; this filter - * must stay a superset of the TS classifier's admission. - */ -@SuppressLint("MissingPermission") -public class FaceclawDeviceDiscovery { - /** Even Realities' company identifier: the ASCII bytes "ER" read little-endian. */ - public static final int EVEN_COMPANY_ID = 0x5245; - - private final BluetoothAdapter bluetoothAdapter; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private ScanCallback activeCallback; - private volatile FaceclawDeviceDiscoveryListener listener; - - public FaceclawDeviceDiscovery(Context context) { - Context appContext = context.getApplicationContext(); - BluetoothManager bluetoothManager = - (BluetoothManager) appContext.getSystemService(Context.BLUETOOTH_SERVICE); - if (bluetoothManager == null || bluetoothManager.getAdapter() == null) { - throw new IllegalStateException("Bluetooth adapter unavailable"); - } - this.bluetoothAdapter = bluetoothManager.getAdapter(); - } - - public boolean isBluetoothEnabled() { - return bluetoothAdapter.isEnabled(); - } - - public void setListener(FaceclawDeviceDiscoveryListener newListener) { - this.listener = newListener; - } - - // ------------------------------------------------------------------ - // Live scan - // ------------------------------------------------------------------ - - /** - * Start streaming advertisements to the listener. Returns false when the - * radio is off or the scanner is unavailable; a later platform refusal - * arrives through onScanFailed. Calling while a scan is running is a no-op. - */ - public boolean startScan() { - synchronized (lock) { - if (activeCallback != null) { - return true; - } - if (!bluetoothAdapter.isEnabled()) { - emitLog("scan not started: Bluetooth is off"); - return false; - } - BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); - if (scanner == null) { - emitLog("scan not started: no BLE scanner"); - return false; - } - final ScanCallback callback = new ScanCallback() { - @Override - public void onScanResult(int callbackType, ScanResult result) { - if (result != null) { - emitAdvertisement(result); - } - } - - @Override - public void onBatchScanResults(List results) { - if (results == null) { - return; - } - for (ScanResult result : results) { - if (result != null) { - emitAdvertisement(result); - } - } - } - - @Override - public void onScanFailed(int errorCode) { - synchronized (lock) { - if (activeCallback == this) { - activeCallback = null; - } - } - final FaceclawDeviceDiscoveryListener l = listener; - if (l != null) { - final String message = describeScanError(errorCode); - mainHandler.post(() -> l.onScanFailed(errorCode, message)); - } - } - }; - activeCallback = callback; - try { - scanner.startScan(null, buildScanSettings(), callback); - } catch (Throwable t) { - activeCallback = null; - emitLog("scan not started: " + t); - return false; - } - emitLog("scan started (unfiltered, low latency)"); - return true; - } - } - - public void stopScan() { - ScanCallback callback; - synchronized (lock) { - callback = activeCallback; - activeCallback = null; - } - if (callback == null) { - return; - } - BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); - if (scanner != null) { - try { - scanner.stopScan(callback); - } catch (Throwable ignored) { - } - } - emitLog("scan stopped"); - } - - public boolean isScanning() { - synchronized (lock) { - return activeCallback != null; - } - } - - /** - * Replay bonded Even devices to the listener. A bonded listing has no - * signal sample and no manufacturer data, so its serial stays unknown until - * the device is also heard advertising. - */ - public void emitBondedDevices() { - final FaceclawDeviceDiscoveryListener l = listener; - if (l == null) { - return; - } - for (String json : bondedCandidateJsonList()) { - l.onAdvertisement(json); - } - } - - /** Bonded Even devices as one JSON array, for callers without a listener. */ - public String getBondedCandidatesJson() { - JSONArray array = new JSONArray(); - for (String json : bondedCandidateJsonList()) { - try { - array.put(new JSONObject(json)); - } catch (JSONException ignored) { - } - } - return array.toString(); - } - - // ------------------------------------------------------------------ - // Internals - // ------------------------------------------------------------------ - - private static ScanSettings buildScanSettings() { - ScanSettings.Builder builder = new ScanSettings.Builder() - .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) - .setReportDelay(0); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES); - builder.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE); - builder.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // Accept extended advertising too; setLegacy(true) would hide it. - builder.setLegacy(false); - } - return builder.build(); - } - - private void emitAdvertisement(ScanResult result) { - final JSONObject object = toJson(result); - if (object == null) { - return; - } - final FaceclawDeviceDiscoveryListener l = listener; - if (l == null) { - return; - } - final String json = object.toString(); - if (Looper.myLooper() == Looper.getMainLooper()) { - l.onAdvertisement(json); - } else { - mainHandler.post(() -> { - FaceclawDeviceDiscoveryListener current = listener; - if (current != null) { - current.onAdvertisement(json); - } - }); - } - } - - private List bondedCandidateJsonList() { - List out = new ArrayList<>(); - Set bonded = bluetoothAdapter.getBondedDevices(); - if (bonded == null) { - return out; - } - long now = System.currentTimeMillis(); - for (BluetoothDevice device : bonded) { - if (device == null) { - continue; - } - String name = device.getName(); - if (!isAdmissible(name, null)) { - continue; - } - JSONObject object = buildJson( - device.getAddress(), name, "", null, null, null, true, "paired", now); - if (object != null) { - out.add(object.toString()); - } - } - return out; - } - - /** Null when the result is not something we pair with. */ - private static JSONObject toJson(ScanResult result) { - BluetoothDevice device = result.getDevice(); - if (device == null) { - return null; - } - ScanRecord record = result.getScanRecord(); - String liveName = record == null ? null : record.getDeviceName(); - String name = liveName != null && !liveName.isEmpty() ? liveName : device.getName(); - byte[] manufacturerData = extractEvenManufacturerData(record); - if (!isAdmissible(name, manufacturerData)) { - return null; - } - Integer txPower = null; - if (record != null && record.getTxPowerLevel() != Integer.MIN_VALUE) { - txPower = record.getTxPowerLevel(); - } - if (txPower == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - && result.getTxPower() != ScanResult.TX_POWER_NOT_PRESENT) { - txPower = result.getTxPower(); - } - Boolean connectable = null; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - connectable = result.isConnectable(); - } - int rssi = result.getRssi(); - // 127 is the "unavailable" sentinel; 0 dBm never happens for a real sample. - Integer rssiValue = (rssi == 127 || rssi == 0) ? null : rssi; - boolean bonded = device.getBondState() == BluetoothDevice.BOND_BONDED; - return buildJson( - device.getAddress(), name, FaceclawFirmwareUtil.bytesToHex(manufacturerData), rssiValue, txPower, - connectable, bonded, "scan", System.currentTimeMillis()); - } - - /** - * Android strips the 2-byte company identifier from each manufacturer - * record and keys the SparseArray by it. Put the bytes back (little-endian) - * so TypeScript sees the same "ER"+… layout the HCI captures - * document. Prefers Even's own id; otherwise returns the first record so an - * unexpected company id still reaches the diagnostics log. - */ - private static byte[] extractEvenManufacturerData(ScanRecord record) { - if (record == null) { - return new byte[0]; - } - SparseArray all = record.getManufacturerSpecificData(); - if (all == null || all.size() == 0) { - return new byte[0]; - } - byte[] payload = all.get(EVEN_COMPANY_ID); - int companyId = EVEN_COMPANY_ID; - if (payload == null) { - companyId = all.keyAt(0); - payload = all.valueAt(0); - } - if (payload == null) { - return new byte[0]; - } - byte[] out = new byte[payload.length + 2]; - out[0] = (byte) (companyId & 0xff); - out[1] = (byte) ((companyId >> 8) & 0xff); - System.arraycopy(payload, 0, out, 2, payload.length); - return out; - } - - private static boolean isAdmissible(String name, byte[] manufacturerData) { - if (manufacturerData != null && manufacturerData.length >= 2 - && manufacturerData[0] == 0x45 && manufacturerData[1] == 0x52) { - return true; - } - if (name == null) { - return false; - } - String upper = name.toUpperCase(); - // A bare "R1" substring also matches unrelated hardware ("Oppo Enco - // R1"), and TS only classifies rings from the stock name prefix. - return upper.contains("G2") || upper.startsWith("EVEN R1"); - } - - private static JSONObject buildJson( - String address, String name, String manufacturerHex, Integer rssi, Integer txPower, - Boolean connectable, boolean bonded, String source, long seenAtMs) { - if (address == null || address.isEmpty()) { - return null; - } - try { - JSONObject object = new JSONObject(); - object.put("address", address); - object.put("name", name == null ? "" : name); - object.put("manufacturerData", manufacturerHex == null ? "" : manufacturerHex); - object.put("rssi", rssi == null ? JSONObject.NULL : rssi); - object.put("txPower", txPower == null ? JSONObject.NULL : txPower); - object.put("connectable", connectable == null ? JSONObject.NULL : connectable); - object.put("bonded", bonded); - object.put("source", source); - object.put("seenAtMs", seenAtMs); - return object; - } catch (JSONException e) { - return null; - } - } - - private static String describeScanError(int errorCode) { - switch (errorCode) { - case ScanCallback.SCAN_FAILED_ALREADY_STARTED: - return "a scan is already running"; - case ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED: - return "the Bluetooth stack refused to register the scan (try toggling Bluetooth)"; - case ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED: - return "BLE scanning is not supported on this device"; - case ScanCallback.SCAN_FAILED_INTERNAL_ERROR: - return "internal Bluetooth error"; - case 5: // SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES (API 31) - return "out of Bluetooth hardware resources"; - case 6: // SCAN_FAILED_SCANNING_TOO_FREQUENTLY (API 31) - return "scanning too frequently; wait a moment and try again"; - default: - return "scan failed (code " + errorCode + ")"; - } - } - - private void emitLog(String line) { - FaceclawDeviceDiscoveryListener l = listener; - if (l != null) { - l.onLog(line); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt new file mode 100644 index 00000000..b43bcaae --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt @@ -0,0 +1,372 @@ +package com.faceclaw.app + +import android.annotation.SuppressLint +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothManager +import android.bluetooth.le.BluetoothLeScanner +import android.bluetooth.le.ScanCallback +import android.bluetooth.le.ScanRecord +import android.bluetooth.le.ScanResult +import android.bluetooth.le.ScanSettings +import android.content.Context +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.util.SparseArray + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +import java.util.ArrayList + +/** + * Finds Even Realities hardware over BLE and hands every advertisement up to + * TypeScript with the raw fields the pairing screen needs: the LIVE local name + * from the scan record (BluetoothDevice.getName() is a cache that can hold a + * pre-reset value indefinitely), the full manufacturer-specific payload with + * its company identifier restored so the "ER"+serial+MAC layout survives + * intact, RSSI, advertised TX power, and connectability. + * + * Admission is deliberately loose — a name containing G2, the stock + * "EVEN R1…" ring prefix, or the Even "ER" manufacturer signature — because + * renamed custom firmware may drop the stock name. Side, serial, and pair + * matching are decided in TypeScript (app/g2/even-advertisement.ts, + * app/g2/pairing-candidates.ts) where they can be unit-tested; this filter + * must stay a superset of the TS classifier's admission. + */ +@SuppressLint("MissingPermission") +class FaceclawDeviceDiscovery(context: Context) { + companion object { + /** Even Realities' company identifier: the ASCII bytes "ER" read little-endian. */ + const val EVEN_COMPANY_ID = 0x5245 + + // ------------------------------------------------------------------ + // Internals + // ------------------------------------------------------------------ + + private fun buildScanSettings(): ScanSettings { + val builder = ScanSettings.Builder() + .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) + .setReportDelay(0) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) + builder.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE) + builder.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Accept extended advertising too; setLegacy(true) would hide it. + builder.setLegacy(false) + } + return builder.build() + } + + /** Null when the result is not something we pair with. */ + private fun toJson(result: ScanResult): JSONObject? { + val device = result.device ?: return null + val record = result.scanRecord + val liveName = record?.deviceName + val name = if (liveName != null && !liveName.isEmpty()) liveName else device.name + val manufacturerData = extractEvenManufacturerData(record) + if (!isAdmissible(name, manufacturerData)) { + return null + } + var txPower: Int? = null + if (record != null && record.txPowerLevel != Int.MIN_VALUE) { + txPower = record.txPowerLevel + } + if (txPower == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && result.txPower != ScanResult.TX_POWER_NOT_PRESENT) { + txPower = result.txPower + } + var connectable: Boolean? = null + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + connectable = result.isConnectable + } + val rssi = result.rssi + // 127 is the "unavailable" sentinel; 0 dBm never happens for a real sample. + val rssiValue: Int? = if (rssi == 127 || rssi == 0) null else rssi + val bonded = device.bondState == BluetoothDevice.BOND_BONDED + return buildJson( + device.address, name, FaceclawFirmwareUtil.bytesToHex(manufacturerData), rssiValue, txPower, + connectable, bonded, "scan", System.currentTimeMillis()) + } + + /** + * Android strips the 2-byte company identifier from each manufacturer + * record and keys the SparseArray by it. Put the bytes back (little-endian) + * so TypeScript sees the same "ER"+… layout the HCI captures + * document. Prefers Even's own id; otherwise returns the first record so an + * unexpected company id still reaches the diagnostics log. + */ + private fun extractEvenManufacturerData(record: ScanRecord?): ByteArray { + if (record == null) { + return ByteArray(0) + } + val all: SparseArray? = record.manufacturerSpecificData + if (all == null || all.size() == 0) { + return ByteArray(0) + } + var payload: ByteArray? = all.get(EVEN_COMPANY_ID) + var companyId = EVEN_COMPANY_ID + if (payload == null) { + companyId = all.keyAt(0) + payload = all.valueAt(0) + } + if (payload == null) { + return ByteArray(0) + } + val out = ByteArray(payload.size + 2) + out[0] = (companyId and 0xff).toByte() + out[1] = ((companyId shr 8) and 0xff).toByte() + System.arraycopy(payload, 0, out, 2, payload.size) + return out + } + + private fun isAdmissible(name: String?, manufacturerData: ByteArray?): Boolean { + if (manufacturerData != null && manufacturerData.size >= 2 + && manufacturerData[0] == 0x45.toByte() && manufacturerData[1] == 0x52.toByte()) { + return true + } + if (name == null) { + return false + } + val upper = name.uppercase() + // A bare "R1" substring also matches unrelated hardware ("Oppo Enco + // R1"), and TS only classifies rings from the stock name prefix. + return upper.contains("G2") || upper.startsWith("EVEN R1") + } + + private fun buildJson( + address: String?, name: String?, manufacturerHex: String?, rssi: Int?, txPower: Int?, + connectable: Boolean?, bonded: Boolean, source: String, seenAtMs: Long): JSONObject? { + if (address == null || address.isEmpty()) { + return null + } + try { + val obj = JSONObject() + obj.put("address", address) + obj.put("name", name ?: "") + obj.put("manufacturerData", manufacturerHex ?: "") + obj.put("rssi", rssi ?: JSONObject.NULL) + obj.put("txPower", txPower ?: JSONObject.NULL) + obj.put("connectable", connectable ?: JSONObject.NULL) + obj.put("bonded", bonded) + obj.put("source", source) + obj.put("seenAtMs", seenAtMs) + return obj + } catch (e: JSONException) { + return null + } + } + + private fun describeScanError(errorCode: Int): String { + return when (errorCode) { + ScanCallback.SCAN_FAILED_ALREADY_STARTED -> + "a scan is already running" + ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED -> + "the Bluetooth stack refused to register the scan (try toggling Bluetooth)" + ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED -> + "BLE scanning is not supported on this device" + ScanCallback.SCAN_FAILED_INTERNAL_ERROR -> + "internal Bluetooth error" + 5 -> // SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES (API 31) + "out of Bluetooth hardware resources" + 6 -> // SCAN_FAILED_SCANNING_TOO_FREQUENTLY (API 31) + "scanning too frequently; wait a moment and try again" + else -> + "scan failed (code $errorCode)" + } + } + } + + private val bluetoothAdapter: BluetoothAdapter + private val mainHandler = Handler(Looper.getMainLooper()) + + private val lock = Any() + private var activeCallback: ScanCallback? = null + @Volatile + private var listener: FaceclawDeviceDiscoveryListener? = null + + init { + val appContext = context.applicationContext + val bluetoothManager = + appContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager? + if (bluetoothManager == null || bluetoothManager.adapter == null) { + throw IllegalStateException("Bluetooth adapter unavailable") + } + this.bluetoothAdapter = bluetoothManager.adapter + } + + fun isBluetoothEnabled(): Boolean { + return bluetoothAdapter.isEnabled + } + + fun setListener(newListener: FaceclawDeviceDiscoveryListener?) { + this.listener = newListener + } + + // ------------------------------------------------------------------ + // Live scan + // ------------------------------------------------------------------ + + /** + * Start streaming advertisements to the listener. Returns false when the + * radio is off or the scanner is unavailable; a later platform refusal + * arrives through onScanFailed. Calling while a scan is running is a no-op. + */ + fun startScan(): Boolean { + synchronized(lock) { + if (activeCallback != null) { + return true + } + if (!bluetoothAdapter.isEnabled) { + emitLog("scan not started: Bluetooth is off") + return false + } + val scanner: BluetoothLeScanner? = bluetoothAdapter.bluetoothLeScanner + if (scanner == null) { + emitLog("scan not started: no BLE scanner") + return false + } + val callback = object : ScanCallback() { + override fun onScanResult(callbackType: Int, result: ScanResult?) { + if (result != null) { + emitAdvertisement(result) + } + } + + override fun onBatchScanResults(results: List?) { + if (results == null) { + return + } + for (result in results) { + if (result != null) { + emitAdvertisement(result) + } + } + } + + override fun onScanFailed(errorCode: Int) { + synchronized(lock) { + if (activeCallback === this) { + activeCallback = null + } + } + val l = listener + if (l != null) { + val message = describeScanError(errorCode) + mainHandler.post { l.onScanFailed(errorCode, message) } + } + } + } + activeCallback = callback + try { + scanner.startScan(null, buildScanSettings(), callback) + } catch (t: Throwable) { + activeCallback = null + emitLog("scan not started: $t") + return false + } + emitLog("scan started (unfiltered, low latency)") + return true + } + } + + fun stopScan() { + val callback: ScanCallback? + synchronized(lock) { + callback = activeCallback + activeCallback = null + } + if (callback == null) { + return + } + val scanner = bluetoothAdapter.bluetoothLeScanner + if (scanner != null) { + try { + scanner.stopScan(callback) + } catch (ignored: Throwable) { + } + } + emitLog("scan stopped") + } + + fun isScanning(): Boolean { + synchronized(lock) { + return activeCallback != null + } + } + + /** + * Replay bonded Even devices to the listener. A bonded listing has no + * signal sample and no manufacturer data, so its serial stays unknown until + * the device is also heard advertising. + */ + fun emitBondedDevices() { + val l = listener ?: return + for (json in bondedCandidateJsonList()) { + l.onAdvertisement(json) + } + } + + /** Bonded Even devices as one JSON array, for callers without a listener. */ + fun getBondedCandidatesJson(): String { + val array = JSONArray() + for (json in bondedCandidateJsonList()) { + try { + array.put(JSONObject(json)) + } catch (ignored: JSONException) { + } + } + return array.toString() + } + + private fun emitAdvertisement(result: ScanResult) { + val obj = toJson(result) ?: return + val l = listener ?: return + val json = obj.toString() + if (Looper.myLooper() == Looper.getMainLooper()) { + l.onAdvertisement(json) + } else { + mainHandler.post { + val current = listener + if (current != null) { + current.onAdvertisement(json) + } + } + } + } + + private fun bondedCandidateJsonList(): List { + val out = ArrayList() + val bonded: Set? = bluetoothAdapter.bondedDevices + if (bonded == null) { + return out + } + val now = System.currentTimeMillis() + for (device in bonded) { + if (device == null) { + continue + } + val name = device.name + if (!isAdmissible(name, null)) { + continue + } + val obj = buildJson( + device.address, name, "", null, null, null, true, "paired", now) + if (obj != null) { + out.add(obj.toString()) + } + } + return out + } + + private fun emitLog(line: String) { + val l = listener + if (l != null) { + l.onLog(line) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java deleted file mode 100644 index 7e7e5991..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java +++ /dev/null @@ -1,619 +0,0 @@ -package com.faceclaw.app; - -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothGatt; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -/** - * Connects to the glasses (stock-firmware compatible), reads the device-info / - * settings response, and reports the firmware versions plus the firmware- - * extension string (empty on stock firmware). Used by onboarding to decide whether to - * flash. Reuses FaceclawBleManager + BleProtocol; owns its own connection and - * runs on a single worker thread. Shows nothing on the lens. - */ -public class FaceclawDeviceInfoProbe implements FaceclawBleListener { - private static final String TAG = "FaceclawDeviceInfo"; - private static final int QUERY_TIMEOUT_MS = 4_000; - /** Connect + auth attempts per arm; a link that drops mid-pairing is retried once the bond settles. */ - private static final int ARM_ATTEMPTS = 3; - private static final int ARM_RETRY_DELAY_MS = 1_000; - /** - * Upper bound on waiting for the auth success while Android reports the arm - * as BOND_BONDING — i.e. an OS pairing dialog may be sitting there waiting - * for the user, which can take a lot longer than the normal 30 s window. - */ - private static final int PAIRING_WAIT_CAP_MS = 90_000; - /** After a fresh bond, how long the firmware gets to notify success on its own before the request is re-sent. */ - private static final int POST_BOND_GRACE_MS = 2_000; - private static final int POLL_MS = 250; - - private final Context context; - private final String rightAddress; - private final String leftAddress; - private final FaceclawBleManager bleManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private int nextSeq = 0x40; - private int nextMagic = 100; - - private int awaitSid = -1; - private int awaitMagic = -1; - private byte[] awaitPb = null; - private CountDownLatch awaitLatch = null; - - /** - * Security-auth wait, kept separate from the generic ack wait: only the - * firmware's SUCCESS result (empty result message) may satisfy it. The - * firmware answers the request as soon as it arrives, but with a - * non-success result until the link is encrypted; accepting that first - * reply as "the ack" is what let the probe move on to the other arm while - * Android's pairing dialog for this one was still up. - */ - private final Set authMagics = new HashSet<>(); - private CountDownLatch authLatch = null; - - /** - * A sid-0x09 frame that carried firmware versions without matching the - * awaited magic — some firmware pushes the settings snapshot on a magic it - * picked itself instead of (or in addition to) acking the read. - */ - private volatile byte[] unsolicitedSettingsPb; - - private volatile FaceclawDeviceInfoProbeListener listener; - private volatile Thread worker; - private volatile boolean cancelled = false; - - public FaceclawDeviceInfoProbe(Context context, String rightAddress, String leftAddress) { - this.context = context.getApplicationContext(); - this.rightAddress = rightAddress == null ? "" : rightAddress; - this.leftAddress = leftAddress == null ? "" : leftAddress; - this.bleManager = new FaceclawBleManager(this.context); - this.bleManager.setListener(this); - } - - public void setListener(FaceclawDeviceInfoProbeListener listener) { - this.listener = listener; - } - - public void start() { - synchronized (lock) { - if (worker != null) { - return; - } - worker = new Thread(this::run, "faceclaw-device-info"); - worker.start(); - } - } - - public void cancel() { - cancelled = true; - Thread w = worker; - if (w != null) { - w.interrupt(); - } - } - - public void close() { - cancel(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void run() { - try { - if (rightAddress.trim().isEmpty()) { - emitError("No right-arm address configured."); - return; - } - - boolean haveLeft = !leftAddress.trim().isEmpty() && !leftAddress.equalsIgnoreCase(rightAddress); - - // Bring up and authenticate BOTH arms before asking for versions. - // Each arm is its own peripheral with its own bond, so on a fresh - // phone each raises its own pairing prompt; completing both here - // means the flash step (and the app proper) find both bonds in - // place. Strictly one arm at a time: Android pairs with one device - // at a time, and a second arm connected while the first was still - // pairing has been seen to drop within a second and never pair - // (2026-09-11 logcat). - boolean rightAuthenticated = bringUpArm(rightAddress, "right"); - boolean leftAuthenticated = false; - if (haveLeft && !cancelled) { - leftAuthenticated = bringUpArm(leftAddress, "left"); - } - if (cancelled) { - emitError("Cancelled."); - return; - } - if (!bleManager.isConnected(rightAddress)) { - // The right lens may have dropped while the left one paired; - // it is bonded by now, so this comes back quickly. - emitLog("right lens disconnected while the left lens was set up; reconnecting"); - rightAuthenticated = bringUpArm(rightAddress, "right"); - if (cancelled) { - emitError("Cancelled."); - return; - } - } - - byte[] ack = null; - String rightFailure = null; - try { - ack = queryArm(rightAddress, "right", rightAuthenticated); - } catch (IllegalStateException e) { - rightFailure = e.getMessage() == null ? e.toString() : e.getMessage(); - emitLog("right-lens query failed: " + rightFailure); - } - // The right lens is the documented control endpoint, but a silent - // right lens has been observed on stock 2.2.9 even after a - // successful security auth — run the same probe against the left - // lens rather than giving up, and log which lens answered. - if (ack == null && !cancelled && haveLeft) { - emitLog("right lens did not answer the settings query; probing the left lens"); - try { - ack = queryArm(leftAddress, "left", leftAuthenticated); - } catch (Exception e) { - emitLog("left-lens probe failed: " + (e.getMessage() == null ? e.toString() : e.getMessage())); - } - } - if (cancelled) { - emitError("Cancelled."); - return; - } - if (ack == null) { - throw new IllegalStateException(rightFailure != null - ? rightFailure - : "no response to the device-info query on either lens — check `adb logcat -s " - + TAG + "` for the frame trace"); - } - - BleProtocol.FirmwareInfo info = BleProtocol.parseSettingsFirmwareInfo(ack); - String left = info == null ? "" : info.leftVersion; - String right = info == null ? "" : info.rightVersion; - String extension = info == null ? "" : info.extension; - emitLog("device-info: L=" + left + " R=" + right + " ext=[" + extension + "]"); - emitResult(left, right, extension); - } catch (Exception e) { - String message = cancelled ? "Cancelled." : (e.getMessage() == null ? e.toString() : e.getMessage()); - emitError(message); - } finally { - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - } - - /** - * Connect to one arm and complete its security-auth exchange, retrying - * when the link drops mid-pairing. Returns whether the auth success was - * seen (false = connected but unconfirmed, tolerated because the custom - * firmware's response to the exchange is not yet hardware-verified); - * throws when the arm cannot be brought up at all. - */ - private boolean bringUpArm(String address, String label) throws InterruptedException { - String lastFailure = null; - for (int attempt = 1; attempt <= ARM_ATTEMPTS && !cancelled; attempt++) { - if (attempt > 1) { - // A link Android dropped while it was still pairing comes back - // once the bond settles; reconnecting sooner just fails again. - waitForBondToSettle(address, label); - if (cancelled) { - break; - } - emitLog("retrying the " + label + " lens (attempt " + attempt + "/" + ARM_ATTEMPTS + "): " + lastFailure); - Thread.sleep(ARM_RETRY_DELAY_MS); - } - emitState("connecting", label); - try { - connectArm(address); - } catch (IllegalStateException e) { - lastFailure = e.getMessage() == null ? e.toString() : e.getMessage(); - emitLog(label + " lens: " + lastFailure); - continue; - } - if (cancelled) { - break; - } - - // Firmware 2.2.9 answers no queries until the security-auth - // exchange completes over an encrypted link; on a phone with no - // existing bond this is also what triggers SMP pairing (and its OS - // prompt), so it must come before the prelude and query. - emitState("authenticating", label); - AuthResult result = authenticate(address, label); - if (result == AuthResult.SUCCESS) { - return true; - } - if (result == AuthResult.UNCONFIRMED) { - return false; - } - lastFailure = "link dropped during authentication"; - } - if (cancelled) { - return false; - } - throw new IllegalStateException("could not connect to the " + label + " lens (" + address + "): " - + lastFailure + " — if Android showed a Bluetooth pairing request, accept it and try again"); - } - - private enum AuthResult { SUCCESS, UNCONFIRMED, LINK_DROPPED } - - /** - * Send the sid-0x80 authentication request and wait for the firmware's - * SUCCESS notification, which only arrives once the link is encrypted. - * On an unbonded phone that means waiting through Android's pairing flow - * (BOND_BONDING, possibly with a dialog up), so the wait is extended while - * the OS reports pairing in progress and the request is re-sent once - * after a fresh bond in case the pre-pairing write was dropped. Bond - * state is polled rather than awaited via broadcast; 250 ms granularity - * is plenty here. - */ - private AuthResult authenticate(String address, String label) throws InterruptedException { - long start = SystemClock.elapsedRealtime(); - long deadline = start + ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS; - long hardDeadline = start + PAIRING_WAIT_CAP_MS; - CountDownLatch latch = new CountDownLatch(1); - synchronized (lock) { - authMagics.clear(); - authLatch = latch; - } - int initialBond = bleManager.getBondState(address); - emitLog("security auth: " + label + " lens, Android bond state " + bondStateName(initialBond)); - try { - int sends = 0; - boolean lastSendFailed = false; - long nextSendAt = start; - long bondedAt = -1; - boolean resentAfterBond = false; - boolean loggedBonding = false; - while (!cancelled) { - long now = SystemClock.elapsedRealtime(); - int bond = bleManager.getBondState(address); - - if (bond == BluetoothDevice.BOND_BONDING) { - // OS pairing in progress — the firmware will answer once the - // link is encrypted. Don't write, don't give up, don't move - // on to the other arm. - if (!loggedBonding) { - emitLog("Android is pairing with the " + label + " lens; waiting for the user to accept"); - loggedBonding = true; - } - if (now >= hardDeadline) { - break; - } - } else { - if (bond == BluetoothDevice.BOND_BONDED && initialBond != BluetoothDevice.BOND_BONDED - && bondedAt < 0) { - bondedAt = now; - emitLog("Android bond established with the " + label + " lens; waiting for the auth success"); - deadline = Math.max(deadline, now + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS); - } - boolean wantSend = sends == 0 - || (lastSendFailed && sends < 3 && now >= nextSendAt) - || (bondedAt >= 0 && !resentAfterBond && now - bondedAt >= POST_BOND_GRACE_MS); - if (wantSend) { - if (bondedAt >= 0) { - resentAfterBond = true; - if (sends > 0) { - emitLog("re-sending the auth request after pairing (" + label + " lens)"); - } - } - lastSendFailed = !sendAuthRequest(address); - sends++; - nextSendAt = SystemClock.elapsedRealtime() + ARM_RETRY_DELAY_MS; - if (lastSendFailed && !bleManager.isConnected(address)) { - return AuthResult.LINK_DROPPED; - } - } - if (now >= deadline) { - break; - } - } - - if (latch.await(POLL_MS, TimeUnit.MILLISECONDS)) { - emitLog("security auth complete: " + label + " lens (" + address + ")"); - return AuthResult.SUCCESS; - } - if (!bleManager.isConnected(address)) { - emitLog("security auth: " + label + " lens disconnected while waiting" - + (bond == BluetoothDevice.BOND_BONDING ? " (Android was still pairing)" : "")); - return AuthResult.LINK_DROPPED; - } - } - if (cancelled) { - return AuthResult.UNCONFIRMED; - } - emitLog("security auth unconfirmed: " + label + " lens (" + address + "), Android bond state " - + bondStateName(bleManager.getBondState(address))); - return AuthResult.UNCONFIRMED; - } finally { - synchronized (lock) { - authLatch = null; - authMagics.clear(); - } - } - } - - private boolean sendAuthRequest(String address) { - int magic = allocMagic(); - int seq; - synchronized (lock) { - authMagics.add(magic); - seq = nextSeq++ & 0xff; - } - byte[] payload = BleProtocol.buildAuthenticationRequest(magic); - emitLog(String.format("tx %s sid=0x%02x flag=0x%02x magic=%d seq=0x%02x len=%d", - address, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, magic, seq, payload.length)); - List frames = BleProtocol.framePb(payload, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, seq); - boolean written; - try { - written = bleManager.writeFrames( - address, BleProtocol.WRITE_CHAR_UUID, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); - } catch (IllegalStateException e) { - // "Not connected" — the link went away underneath us. - written = false; - } - if (!written) { - emitLog("tx write FAILED sid=0x80 (" + address + ")"); - } - return written; - } - - /** Block while Android reports this arm as BOND_BONDING (pairing dialog up / SMP in flight). */ - private void waitForBondToSettle(String address, String label) throws InterruptedException { - long cap = SystemClock.elapsedRealtime() + PAIRING_WAIT_CAP_MS; - boolean logged = false; - while (!cancelled && bleManager.getBondState(address) == BluetoothDevice.BOND_BONDING - && SystemClock.elapsedRealtime() < cap) { - if (!logged) { - emitLog("waiting for Android to finish pairing with the " + label + " lens before reconnecting"); - logged = true; - } - Thread.sleep(POLL_MS); - } - } - - private static String bondStateName(int state) { - switch (state) { - case BluetoothDevice.BOND_NONE: - return "none"; - case BluetoothDevice.BOND_BONDING: - return "bonding"; - case BluetoothDevice.BOND_BONDED: - return "bonded"; - default: - return "unknown"; - } - } - - /** - * Prelude + settings read on an already connected/authenticated lens. - * Returns the settings ack protobuf, an unsolicited settings push that - * carried firmware versions, or null when the lens never answered the read. - * Throws on prelude failure; the caller wraps the fallback lens's attempt - * so its failure cannot mask the primary lens's outcome. - */ - private byte[] queryArm(String address, String label, boolean authenticated) throws InterruptedException { - if (cancelled) { - return null; - } - - emitState("querying", label); - // Session prelude, then a settings/device-info read (both arms' - // versions and the firmware-extension string ride back in one response). - if (writeAndAwaitAck(address, BleProtocol.PRELUDE_ACK_SID, BleProtocol.FLAG_REQUEST, - BleProtocol.PRELUDE_ACK_MAGIC, BleProtocol.PRELUDE_F5872_PAYLOAD, - ConnectionOptions.PRELUDE_TIMEOUT_MS) == null) { - throw new IllegalStateException("session prelude not acked (" + label + " lens)" - + (authenticated ? "" : "; authentication did not complete —" - + " if Android shows a Bluetooth pairing request, accept it and try again")); - } - - // Two attempts: the first read straight after a fresh pairing has been - // seen to go unanswered while a later one succeeds. - for (int attempt = 0; attempt < 2 && !cancelled; attempt++) { - int magic = allocMagic(); - byte[] ack = writeAndAwaitAck(address, BleProtocol.SID_UI_SETTING, BleProtocol.FLAG_REQUEST, - magic, BleProtocol.buildSettingsQuery(magic), QUERY_TIMEOUT_MS); - if (ack != null) { - return ack; - } - // A push with the firmware versions on the device's own magic is - // as good as the ack we asked for. - byte[] pushed = unsolicitedSettingsPb; - if (pushed != null) { - emitLog("using unsolicited settings push instead of the read ack (" + label + " lens)"); - return pushed; - } - emitLog("settings query attempt " + (attempt + 1) + " unanswered (" + label + " lens)"); - } - return null; - } - - private void connectArm(String address) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("connect failed"); - } - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("service discovery failed"); - } - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - throw new IllegalStateException("could not subscribe to notifications"); - } - } - - private byte[] writeAndAwaitAck(String address, int sid, int flag, int magic, byte[] payload, int timeoutMs) - throws InterruptedException { - CountDownLatch latch = new CountDownLatch(1); - synchronized (lock) { - awaitSid = sid; - awaitMagic = magic; - awaitPb = null; - awaitLatch = latch; - } - int seq; - synchronized (lock) { - seq = nextSeq++ & 0xff; - } - emitLog(String.format("tx %s sid=0x%02x flag=0x%02x magic=%d seq=0x%02x len=%d", - address, sid, flag, magic, seq, payload.length)); - List frames = BleProtocol.framePb(payload, sid, flag, seq); - boolean written = bleManager.writeFrames( - address, BleProtocol.WRITE_CHAR_UUID, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); - if (!written) { - emitLog("tx write FAILED sid=0x" + Integer.toHexString(sid)); - synchronized (lock) { - awaitLatch = null; - } - return null; - } - boolean acked = latch.await(timeoutMs, TimeUnit.MILLISECONDS); - synchronized (lock) { - awaitLatch = null; - return acked ? awaitPb : null; - } - } - - @Override - public void onNotification(String address, String characteristicUuid, byte[] data) { - if (!BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { - return; - } - // One notification value can carry several envelope frames back to - // back; reading only the first would silently drop the rest. - List frames = BleProtocol.splitFrames(data); - if (frames.size() > 1) { - emitLog("rx " + address + " value carries " + frames.size() + " frames (raw " + data.length + " bytes)"); - } - for (byte[] buf : frames) { - handleFrame(address, buf, data.length); - } - } - - private void handleFrame(String address, byte[] buf, int rawValueLength) { - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); - if (!frame.ok) { - emitLog("rx " + address + " unparseable frame len=" + buf.length + " (raw value " + rawValueLength + ")" - + " head=" + FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(buf, Math.min(16, buf.length)))); - return; - } - // Log every control frame while diagnosing 2.2.9: sid/flag/type/magic - // plus a payload prefix is enough to reconstruct what the lens said. - int declared = buf.length > 3 ? buf[3] & 0xff : 0; - String truncated = buf.length < 8 + declared ? " TRUNCATED(declared=" + declared + ")" : ""; - emitLog(String.format("rx %s sid=0x%02x flag=0x%02x type=%d magic=%d frag=%d/%d len=%d%s pb=%s", - address, frame.sid, frame.flag, frame.msgType, frame.msgSeq, - buf.length > 5 ? buf[5] & 0xff : 0, buf.length > 4 ? buf[4] & 0xff : 0, frame.pb.length, truncated, - FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(frame.pb, Math.min(48, frame.pb.length))))); - if (frame.sid == BleProtocol.SID_UI_SETTING - && BleProtocol.parseSettingsFirmwareInfo(frame.pb) != null) { - // Any settings frame carrying firmware versions answers the probe's - // question, whether or not it matches the magic we asked with. - unsolicitedSettingsPb = frame.pb; - } - if (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT) { - // Async event, not an ack. This includes the lens's own periodic - // sid-0x80 notifies (device-chosen magic, non-empty result). - return; - } - if (frame.sid == BleProtocol.SID_SECURITY_AUTH) { - boolean success = false; - boolean ours = false; - synchronized (lock) { - if (authLatch != null) { - ours = authMagics.contains(frame.msgSeq); - for (int magic : authMagics) { - if (BleProtocol.isAuthenticationSuccess(frame.pb, magic)) { - success = true; - authLatch.countDown(); - break; - } - } - } - } - if (ours && !success) { - // Expected before the link is encrypted: the firmware answers - // the request straight away with a non-success result and - // sends the real success once pairing/encryption completes. - emitLog("auth reply for magic " + frame.msgSeq + " is not the success result; still waiting"); - } - return; - } - synchronized (lock) { - if (awaitLatch != null && frame.sid == awaitSid && frame.msgSeq == awaitMagic) { - awaitPb = frame.pb; - awaitLatch.countDown(); - } - } - } - - @Override - public void onConnectionStateChange(String address, boolean connected) { - if (!connected) { - Log.i(TAG, "disconnected: " + address); - } - } - - private int allocMagic() { - synchronized (lock) { - int magic = nextMagic; - nextMagic = nextMagic >= 255 ? 100 : nextMagic + 1; - return magic; - } - } - - private void emitLog(String line) { - Log.i(TAG, line); - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onLog(line); - } - }); - } - - private void emitState(String state, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onState(state, safeDetail); - } - }); - } - - private void emitResult(String left, String right, String extension) { - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onResult(left, right, extension); - } - }); - } - - private void emitError(String message) { - final String safeMessage = message == null ? "" : message; - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onError(safeMessage); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.kt new file mode 100644 index 00000000..5568a6ce --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.kt @@ -0,0 +1,78 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper + +/** + * Android host for the shared [DeviceInfoProbeFlow]: owns the GATT link (AndroidStockLink), + * the worker thread the flow runs on, and delivers listener callbacks on the main thread. + * Public API unchanged for the TypeScript wrapper (app/native/device-info-probe.ts). + */ +class FaceclawDeviceInfoProbe(context: Context, rightAddress: String?, leftAddress: String?) { + private val link = AndroidStockLink(context.applicationContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val lock = Any() + + @Volatile + private var listener: FaceclawDeviceInfoProbeListener? = null + + @Volatile + private var worker: Thread? = null + + private val flow = + DeviceInfoProbeFlow( + link, + rightAddress ?: "", + leftAddress ?: "", + object : FaceclawDeviceInfoProbeListener { + override fun onLog(line: String?) { + android.util.Log.i(DeviceInfoProbeFlow.TAG, line ?: "") + post { it.onLog(line) } + } + + override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") } + + override fun onResult(leftVersion: String?, rightVersion: String?, extension: String?) = + post { it.onResult(leftVersion, rightVersion, extension) } + + override fun onError(message: String?) = post { it.onError(message ?: "") } + }, + ) + + fun setListener(listener: FaceclawDeviceInfoProbeListener?) { + this.listener = listener + } + + fun start() { + synchronized(lock) { + if (worker != null) { + return + } + val w = Thread({ flow.run() }, "faceclaw-device-info") + worker = w + w.start() + } + } + + fun cancel() { + flow.cancel() + } + + fun close() { + cancel() + try { + link.close() + } catch (ignored: Exception) { + } + } + + private fun post(action: (FaceclawDeviceInfoProbeListener) -> Unit) { + mainHandler.post { + val current = listener + if (current != null) { + action(current) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.java deleted file mode 100644 index ea9352aa..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.java +++ /dev/null @@ -1,262 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.k2fsa.sherpa.onnx.FastClusteringConfig; -import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarization; -import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationConfig; -import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationSegment; -import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationModelConfig; -import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationPyannoteModelConfig; -import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.DataInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * Offline re-diarization of a stored conversation recording (16-bit mono - * WAV): pyannote segmentation plus the shared speaker-embedding model with - * clustering, via sherpa-onnx. Runs on its own thread; results are handed to - * TS as JSON turns for reconciliation against the transcript segments. - */ -public class FaceclawDiarizer { - private static final String TAG = "FaceclawDiarizer"; - - public interface Listener { - /** progress in [0, 1]. */ - void onProgress(double progress); - - /** turns: [{startMs, endMs, cluster}], sorted by start time. */ - void onDone(String turnsJson); - - void onError(String message); - } - - private final String segmentationModelPath; - private final String embeddingModelPath; - private final String wavPath; - private final int numClusters; - private final float threshold; - private final Listener listener; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private volatile boolean cancelled; - - public FaceclawDiarizer( - String segmentationModelPath, - String embeddingModelPath, - String wavPath, - int numClusters, - float threshold, - Listener listener) { - this.segmentationModelPath = segmentationModelPath; - this.embeddingModelPath = embeddingModelPath; - this.wavPath = wavPath; - this.numClusters = numClusters; - this.threshold = threshold <= 0 ? 0.5f : threshold; - this.listener = listener; - } - - public void start() { - Thread thread = new Thread(this::run, "FaceclawDiarizer"); - thread.setPriority(Thread.MIN_PRIORITY); - thread.start(); - } - - public void cancel() { - cancelled = true; - } - - private void run() { - OfflineSpeakerDiarization diarization = null; - try { - float[] samples = wavPath.endsWith(".wav") - ? readWavMono16(wavPath) - : decodeCompressedMono(wavPath); - OfflineSpeakerDiarizationConfig config = OfflineSpeakerDiarizationConfig.builder() - .setSegmentation(OfflineSpeakerSegmentationModelConfig.builder() - .setPyannote(OfflineSpeakerSegmentationPyannoteModelConfig.builder() - .setModel(segmentationModelPath) - .build()) - .setNumThreads(1) - .build()) - .setEmbedding(SpeakerEmbeddingExtractorConfig.builder() - .setModel(embeddingModelPath) - .setNumThreads(1) - .build()) - .setClustering(FastClusteringConfig.builder() - .setNumClusters(numClusters > 0 ? numClusters : -1) - .setThreshold(threshold) - .build()) - .setMinDurationOn(0.3f) - .setMinDurationOff(0.5f) - .build(); - diarization = new OfflineSpeakerDiarization(config); - OfflineSpeakerDiarizationSegment[] segments = diarization.processWithCallback( - samples, - (numProcessedChunks, numTotalChunks, arg) -> { - emitProgress(numTotalChunks <= 0 ? 0 - : (double) numProcessedChunks / numTotalChunks); - return cancelled ? 1 : 0; - }); - JSONArray turns = new JSONArray(); - if (segments != null) { - for (OfflineSpeakerDiarizationSegment segment : segments) { - JSONObject turn = new JSONObject(); - turn.put("startMs", Math.round(segment.getStart() * 1000)); - turn.put("endMs", Math.round(segment.getEnd() * 1000)); - turn.put("cluster", segment.getSpeaker()); - turns.put(turn); - } - } - String result = turns.toString(); - mainHandler.post(() -> listener.onDone(result)); - } catch (Throwable t) { - Log.w(TAG, "diarization failed for " + wavPath, t); - String message = t.getMessage() == null ? t.toString() : t.getMessage(); - mainHandler.post(() -> listener.onError(message)); - } finally { - if (diarization != null) { - diarization.release(); - } - } - } - - private void emitProgress(double progress) { - mainHandler.post(() -> listener.onProgress(Math.max(0, Math.min(1, progress)))); - } - - /** - * Decode a compressed recording (M4A/AAC from the transcode sweep) into - * [-1, 1] floats via MediaExtractor + MediaCodec. Recordings are written - * as 16 kHz mono, so no resampling is needed. - */ - private static float[] decodeCompressedMono(String path) throws IOException { - android.media.MediaExtractor extractor = new android.media.MediaExtractor(); - android.media.MediaCodec codec = null; - java.io.ByteArrayOutputStream pcm = new java.io.ByteArrayOutputStream(); - try { - extractor.setDataSource(path); - int trackIndex = -1; - android.media.MediaFormat format = null; - for (int i = 0; i < extractor.getTrackCount(); i++) { - android.media.MediaFormat candidate = extractor.getTrackFormat(i); - String mime = candidate.getString(android.media.MediaFormat.KEY_MIME); - if (mime != null && mime.startsWith("audio/")) { - trackIndex = i; - format = candidate; - break; - } - } - if (trackIndex < 0 || format == null) { - throw new IOException("no audio track in " + path); - } - extractor.selectTrack(trackIndex); - codec = android.media.MediaCodec.createDecoderByType( - format.getString(android.media.MediaFormat.KEY_MIME)); - codec.configure(format, null, null, 0); - codec.start(); - android.media.MediaCodec.BufferInfo info = new android.media.MediaCodec.BufferInfo(); - boolean inputDone = false; - boolean outputDone = false; - while (!outputDone) { - if (!inputDone) { - int inIndex = codec.dequeueInputBuffer(10_000); - if (inIndex >= 0) { - ByteBuffer inBuf = codec.getInputBuffer(inIndex); - int size = extractor.readSampleData(inBuf, 0); - if (size < 0) { - codec.queueInputBuffer(inIndex, 0, 0, 0, - android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM); - inputDone = true; - } else { - codec.queueInputBuffer(inIndex, 0, size, extractor.getSampleTime(), 0); - extractor.advance(); - } - } - } - int outIndex = codec.dequeueOutputBuffer(info, 10_000); - if (outIndex >= 0) { - ByteBuffer outBuf = codec.getOutputBuffer(outIndex); - byte[] chunk = new byte[info.size]; - outBuf.position(info.offset); - outBuf.get(chunk); - pcm.write(chunk); - codec.releaseOutputBuffer(outIndex, false); - if ((info.flags & android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { - outputDone = true; - } - } - } - } finally { - extractor.release(); - if (codec != null) { - try { - codec.stop(); - } catch (Throwable ignored) { - } - codec.release(); - } - } - byte[] bytes = pcm.toByteArray(); - int count = bytes.length / 2; - float[] samples = new float[count]; - for (int i = 0; i < count; i++) { - samples[i] = ((short) ((bytes[i * 2] & 0xff) | (bytes[i * 2 + 1] << 8))) / 32768.0f; - } - return samples; - } - - /** Load a 16-bit mono PCM WAV into [-1, 1] floats. */ - private static float[] readWavMono16(String path) throws IOException { - File file = new File(path); - long fileLength = file.length(); - try (DataInputStream in = new DataInputStream(new FileInputStream(file))) { - byte[] header = new byte[44]; - in.readFully(header); - if (header[0] != 'R' || header[1] != 'I' || header[8] != 'W') { - throw new IOException("not a WAV file"); - } - int channels = (header[22] & 0xff) | (header[23] << 8); - int bits = (header[34] & 0xff) | (header[35] << 8); - if (channels != 1 || bits != 16) { - throw new IOException("only 16-bit mono WAV is supported"); - } - long dataBytes = fileLength - 44; - int count = (int) Math.min(dataBytes / 2, Integer.MAX_VALUE); - float[] samples = new float[count]; - byte[] chunk = new byte[65536]; - int sampleIndex = 0; - int carried = 0; - byte carriedByte = 0; - int read; - while (sampleIndex < count && (read = in.read(chunk)) > 0) { - int offset = 0; - if (carried == 1) { - samples[sampleIndex++] = - ((short) ((carriedByte & 0xff) | (chunk[0] << 8))) / 32768.0f; - offset = 1; - carried = 0; - } - int pairs = (read - offset) / 2; - for (int i = 0; i < pairs && sampleIndex < count; i++) { - int base = offset + i * 2; - samples[sampleIndex++] = - ((short) ((chunk[base] & 0xff) | (chunk[base + 1] << 8))) / 32768.0f; - } - if (((read - offset) & 1) == 1) { - carried = 1; - carriedByte = chunk[read - 1]; - } - } - return samples; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.kt new file mode 100644 index 00000000..86c911d6 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.kt @@ -0,0 +1,277 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import com.k2fsa.sherpa.onnx.FastClusteringConfig +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarization +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationCallback +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationConfig +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationSegment +import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationModelConfig +import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationPyannoteModelConfig +import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig + +import org.json.JSONArray +import org.json.JSONObject + +import java.io.DataInputStream +import java.io.File +import java.io.FileInputStream +import java.io.IOException +import java.nio.ByteBuffer + +/** + * Offline re-diarization of a stored conversation recording (16-bit mono + * WAV): pyannote segmentation plus the shared speaker-embedding model with + * clustering, via sherpa-onnx. Runs on its own thread; results are handed to + * TS as JSON turns for reconciliation against the transcript segments. + */ +class FaceclawDiarizer( + private val segmentationModelPath: String?, + private val embeddingModelPath: String?, + private val wavPath: String?, + private val numClusters: Int, + threshold: Float, + private val listener: Listener +) { + companion object { + private const val TAG = "FaceclawDiarizer" + + /** + * Decode a compressed recording (M4A/AAC from the transcode sweep) into + * [-1, 1] floats via MediaExtractor + MediaCodec. Recordings are written + * as 16 kHz mono, so no resampling is needed. + */ + @JvmStatic + @Throws(IOException::class) + private fun decodeCompressedMono(path: String?): FloatArray { + val extractor = android.media.MediaExtractor() + var codec: android.media.MediaCodec? = null + val pcm = java.io.ByteArrayOutputStream() + try { + extractor.setDataSource(path!!) + var trackIndex = -1 + var format: android.media.MediaFormat? = null + for (i in 0 until extractor.trackCount) { + val candidate = extractor.getTrackFormat(i) + val mime = candidate.getString(android.media.MediaFormat.KEY_MIME) + if (mime != null && mime.startsWith("audio/")) { + trackIndex = i + format = candidate + break + } + } + if (trackIndex < 0 || format == null) { + throw IOException("no audio track in $path") + } + extractor.selectTrack(trackIndex) + codec = android.media.MediaCodec.createDecoderByType( + format.getString(android.media.MediaFormat.KEY_MIME)!!) + codec.configure(format, null, null, 0) + codec.start() + val info = android.media.MediaCodec.BufferInfo() + var inputDone = false + var outputDone = false + while (!outputDone) { + if (!inputDone) { + val inIndex = codec.dequeueInputBuffer(10_000) + if (inIndex >= 0) { + val inBuf = codec.getInputBuffer(inIndex)!! + val size = extractor.readSampleData(inBuf, 0) + if (size < 0) { + codec.queueInputBuffer(inIndex, 0, 0, 0, + android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM) + inputDone = true + } else { + codec.queueInputBuffer(inIndex, 0, size, extractor.sampleTime, 0) + extractor.advance() + } + } + } + val outIndex = codec.dequeueOutputBuffer(info, 10_000) + if (outIndex >= 0) { + val outBuf = codec.getOutputBuffer(outIndex)!! + val chunk = ByteArray(info.size) + outBuf.position(info.offset) + outBuf.get(chunk) + pcm.write(chunk) + codec.releaseOutputBuffer(outIndex, false) + if ((info.flags and android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { + outputDone = true + } + } + } + } finally { + extractor.release() + if (codec != null) { + try { + codec.stop() + } catch (ignored: Throwable) { + } + codec.release() + } + } + val bytes = pcm.toByteArray() + val count = bytes.size / 2 + val samples = FloatArray(count) + for (i in 0 until count) { + samples[i] = ((bytes[i * 2].toInt() and 0xff) or (bytes[i * 2 + 1].toInt() shl 8)).toShort() / 32768.0f + } + return samples + } + + /** Load a 16-bit mono PCM WAV into [-1, 1] floats. */ + @JvmStatic + @Throws(IOException::class) + private fun readWavMono16(path: String?): FloatArray { + val file = File(path) + val fileLength = file.length() + DataInputStream(FileInputStream(file)).use { `in` -> + // Shared RIFF walk (handles extra chunks and unpatched data sizes); the + // stream is then positioned at the first PCM sample. + val prefix = ByteArray(Math.min(fileLength, WavPcmReader.HEADER_PREFIX_BYTES.toLong()).toInt()) + `in`.readFully(prefix) + val info = try { + WavPcmReader.parseHeader(prefix, fileLength) + } catch (e: IllegalArgumentException) { + throw IOException(e.message ?: "not a WAV file") + } + if (info.channels != 1) { + throw IOException("only 16-bit mono WAV is supported") + } + val dataBytes = info.dataBytes + val count = Math.min(dataBytes / 2, Int.MAX_VALUE.toLong()).toInt() + val samples = FloatArray(count) + val chunk = ByteArray(65536) + var sampleIndex = 0 + var carried = 0 + var carriedByte: Byte = 0 + var read = 0 + // The prefix already holds the start of the PCM data; consume it first. + val prefixData = prefix.size - info.dataOffset.toInt() + if (prefixData > 0) { + val pairs = prefixData / 2 + var i = 0 + while (i < pairs && sampleIndex < count) { + val base = info.dataOffset.toInt() + i * 2 + samples[sampleIndex++] = AudioSegmentation.pcm16le(prefix[base], prefix[base + 1]) / 32768.0f + i++ + } + if ((prefixData and 1) == 1) { + carried = 1 + carriedByte = prefix[prefix.size - 1] + } + } + while (sampleIndex < count && `in`.read(chunk).also { read = it } > 0) { + var offset = 0 + if (carried == 1) { + samples[sampleIndex++] = + ((carriedByte.toInt() and 0xff) or (chunk[0].toInt() shl 8)).toShort() / 32768.0f + offset = 1 + carried = 0 + } + val pairs = (read - offset) / 2 + var i = 0 + while (i < pairs && sampleIndex < count) { + val base = offset + i * 2 + samples[sampleIndex++] = + ((chunk[base].toInt() and 0xff) or (chunk[base + 1].toInt() shl 8)).toShort() / 32768.0f + i++ + } + if (((read - offset) and 1) == 1) { + carried = 1 + carriedByte = chunk[read - 1] + } + } + return samples + } + } + } + + interface Listener { + /** progress in [0, 1]. */ + fun onProgress(progress: Double) + + /** turns: [{startMs, endMs, cluster}], sorted by start time. */ + fun onDone(turnsJson: String?) + + fun onError(message: String?) + } + + private val threshold: Float = if (threshold <= 0) 0.5f else threshold + private val mainHandler = Handler(Looper.getMainLooper()) + @Volatile + private var cancelled = false + + fun start() { + val thread = Thread(Runnable { run() }, "FaceclawDiarizer") + thread.priority = Thread.MIN_PRIORITY + thread.start() + } + + fun cancel() { + cancelled = true + } + + private fun run() { + var diarization: OfflineSpeakerDiarization? = null + try { + val samples = if (wavPath!!.endsWith(".wav")) + readWavMono16(wavPath) + else + decodeCompressedMono(wavPath) + val config = OfflineSpeakerDiarizationConfig.builder() + .setSegmentation(OfflineSpeakerSegmentationModelConfig.builder() + .setPyannote(OfflineSpeakerSegmentationPyannoteModelConfig.builder() + .setModel(segmentationModelPath) + .build()) + .setNumThreads(1) + .build()) + .setEmbedding(SpeakerEmbeddingExtractorConfig.builder() + .setModel(embeddingModelPath) + .setNumThreads(1) + .build()) + .setClustering(FastClusteringConfig.builder() + .setNumClusters(if (numClusters > 0) numClusters else -1) + .setThreshold(threshold) + .build()) + .setMinDurationOn(0.3f) + .setMinDurationOff(0.5f) + .build() + diarization = OfflineSpeakerDiarization(config) + val segments: Array? = diarization.processWithCallback( + samples, + OfflineSpeakerDiarizationCallback { numProcessedChunks, numTotalChunks, _ -> + emitProgress(if (numTotalChunks <= 0) 0.0 + else numProcessedChunks.toDouble() / numTotalChunks) + if (cancelled) 1 else 0 + }) + val turns = JSONArray() + if (segments != null) { + for (segment in segments) { + val turn = JSONObject() + turn.put("startMs", Math.round(segment.start * 1000)) + turn.put("endMs", Math.round(segment.end * 1000)) + turn.put("cluster", segment.speaker) + turns.put(turn) + } + } + val result = turns.toString() + mainHandler.post { listener.onDone(result) } + } catch (t: Throwable) { + Log.w(TAG, "diarization failed for $wavPath", t) + val message = if (t.message == null) t.toString() else t.message + mainHandler.post { listener.onError(message) } + } finally { + if (diarization != null) { + diarization.release() + } + } + } + + private fun emitProgress(progress: Double) { + mainHandler.post { listener.onProgress(Math.max(0.0, Math.min(1.0, progress))) } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.java deleted file mode 100644 index bb3e952d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.faceclaw.app; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.provider.Settings; -import android.util.Log; - -public final class FaceclawEvenAppDetector { - private static final String TAG = "FaceclawEvenApp"; - - public static final String EVEN_PACKAGE_NAME = "com.even.sg"; - public static final String EVEN_NOTIFICATION_TITLE = "Even Notification Service"; - - private FaceclawEvenAppDetector() { - } - - public static boolean isNotificationAccessEnabled(Context context) { - if (context == null) { - return false; - } - Context appContext = context.getApplicationContext(); - ComponentName listenerComponent = new ComponentName(appContext, FaceclawMediaNotificationListenerService.class); - String enabledListeners = Settings.Secure.getString( - appContext.getContentResolver(), - "enabled_notification_listeners" - ); - if (enabledListeners == null || enabledListeners.isEmpty()) { - return false; - } - String fullName = listenerComponent.flattenToString(); - String shortName = listenerComponent.flattenToShortString(); - return enabledListeners.contains(fullName) - || enabledListeners.contains(shortName) - || enabledListeners.contains(appContext.getPackageName()); - } - - public static boolean isEvenNotificationActive(Context context) { - boolean hasAccess = isNotificationAccessEnabled(context); - if (!hasAccess) { - Log.i(TAG, "Can't check for Even Realities app notification, permission is not enabled"); - return false; - } - boolean hasNotfication = FaceclawMediaNotificationListenerService.hasActiveNotificationTitle(EVEN_NOTIFICATION_TITLE); - Log.i(TAG, "isEvenNotificationActive: " + hasNotfication); - return hasNotfication; - } - - /** Returns "opened", "not-installed", or "failed" for the phone UI. */ - public static String openEvenAppSettings(Context context) { - if (context == null) { - return "failed"; - } - Context appContext = context.getApplicationContext(); - try { - // Settings may launch successfully and then immediately close for a - // missing package, so check before starting the activity. - appContext.getPackageManager().getApplicationInfo(EVEN_PACKAGE_NAME, 0); - Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); - intent.setData(Uri.parse("package:" + EVEN_PACKAGE_NAME)); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - appContext.startActivity(intent); - return "opened"; - } catch (PackageManager.NameNotFoundException e) { - return "not-installed"; - } catch (Exception t) { - Log.w(TAG, "failed to open Even app settings", t); - return "failed"; - } - } - - public static void openNotificationAccessSettings(Context context) { - if (context == null) { - return; - } - Context appContext = context.getApplicationContext(); - Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - appContext.startActivity(intent); - } catch (Throwable t) { - Log.w(TAG, "failed to open notification access settings", t); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.kt new file mode 100644 index 00000000..4a0b64be --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.kt @@ -0,0 +1,90 @@ +package com.faceclaw.app + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.provider.Settings +import android.util.Log + +class FaceclawEvenAppDetector private constructor() { + companion object { + private const val TAG = "FaceclawEvenApp" + + const val EVEN_PACKAGE_NAME = "com.even.sg" + const val EVEN_NOTIFICATION_TITLE = "Even Notification Service" + + @JvmStatic + fun isNotificationAccessEnabled(context: Context?): Boolean { + if (context == null) { + return false + } + val appContext = context.applicationContext + val listenerComponent = ComponentName(appContext, FaceclawMediaNotificationListenerService::class.java) + val enabledListeners = Settings.Secure.getString( + appContext.contentResolver, + "enabled_notification_listeners" + ) + if (enabledListeners == null || enabledListeners.isEmpty()) { + return false + } + val fullName = listenerComponent.flattenToString() + val shortName = listenerComponent.flattenToShortString() + return enabledListeners.contains(fullName) + || enabledListeners.contains(shortName) + || enabledListeners.contains(appContext.packageName) + } + + @JvmStatic + fun isEvenNotificationActive(context: Context?): Boolean { + val hasAccess = isNotificationAccessEnabled(context) + if (!hasAccess) { + Log.i(TAG, "Can't check for Even Realities app notification, permission is not enabled") + return false + } + val hasNotfication = FaceclawMediaNotificationListenerService.hasActiveNotificationTitle(EVEN_NOTIFICATION_TITLE) + Log.i(TAG, "isEvenNotificationActive: $hasNotfication") + return hasNotfication + } + + /** Returns "opened", "not-installed", or "failed" for the phone UI. */ + @JvmStatic + fun openEvenAppSettings(context: Context?): String { + if (context == null) { + return "failed" + } + val appContext = context.applicationContext + try { + // Settings may launch successfully and then immediately close for a + // missing package, so check before starting the activity. + appContext.packageManager.getApplicationInfo(EVEN_PACKAGE_NAME, 0) + val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + intent.data = Uri.parse("package:$EVEN_PACKAGE_NAME") + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + appContext.startActivity(intent) + return "opened" + } catch (e: PackageManager.NameNotFoundException) { + return "not-installed" + } catch (t: Exception) { + Log.w(TAG, "failed to open Even app settings", t) + return "failed" + } + } + + @JvmStatic + fun openNotificationAccessSettings(context: Context?) { + if (context == null) { + return + } + val appContext = context.applicationContext + val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + try { + appContext.startActivity(intent) + } catch (t: Throwable) { + Log.w(TAG, "failed to open notification access settings", t) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java deleted file mode 100644 index 2f9c0e90..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; -import android.webkit.WebView; - -import androidx.webkit.WebViewCompat; -import androidx.webkit.WebViewFeature; - -import java.util.Collections; -import java.util.Set; - -/** - * Injects the EvenHub bridge shim at document start into a WebView that loads - * a page over the real network (the developer "Load app from URL" flow). - * - * Packaged apps get the shim spliced into the HTML they are served - * (FaceclawEvenHubWebViewClient), which is not an option for a page fetched - * from a live server. WebViewCompat.addDocumentStartJavaScript is the - * supported way to run script before any page script, and unlike an - * onPageStarted evaluateJavascript it is guaranteed to win the race — apps - * poll for window.flutter_inappwebview with a few seconds' patience, but the - * timer shim must be in place before the app captures setTimeout. - * - * Requires WebView 83+; the caller falls back to onPageStarted injection when - * this returns false. - */ -public final class FaceclawEvenHubDocumentStart { - private static final String TAG = "FaceclawEvenHub"; - - private FaceclawEvenHubDocumentStart() {} - - /** - * @param originRule an allowed-origin rule, e.g. "https://example.com" or "*". - * @return whether the script was registered. - */ - public static boolean install(WebView webView, String script, String originRule) { - if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { - Log.w(TAG, "document-start scripts unsupported; falling back to onPageStarted"); - return false; - } - try { - Set rules = Collections.singleton(originRule); - WebViewCompat.addDocumentStartJavaScript(webView, script, rules); - return true; - } catch (Throwable error) { - Log.w(TAG, "addDocumentStartJavaScript failed for " + originRule, error); - return false; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.kt new file mode 100644 index 00000000..3f50eaec --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.kt @@ -0,0 +1,50 @@ +package com.faceclaw.app + +import android.util.Log +import android.webkit.WebView + +import androidx.webkit.WebViewCompat +import androidx.webkit.WebViewFeature + +import java.util.Collections + +/** + * Injects the EvenHub bridge shim at document start into a WebView that loads + * a page over the real network (the developer "Load app from URL" flow). + * + * Packaged apps get the shim spliced into the HTML they are served + * (FaceclawEvenHubWebViewClient), which is not an option for a page fetched + * from a live server. WebViewCompat.addDocumentStartJavaScript is the + * supported way to run script before any page script, and unlike an + * onPageStarted evaluateJavascript it is guaranteed to win the race — apps + * poll for window.flutter_inappwebview with a few seconds' patience, but the + * timer shim must be in place before the app captures setTimeout. + * + * Requires WebView 83+; the caller falls back to onPageStarted injection when + * this returns false. + */ +class FaceclawEvenHubDocumentStart private constructor() { + companion object { + private const val TAG = "FaceclawEvenHub" + + /** + * @param originRule an allowed-origin rule, e.g. "https://example.com" or "*". + * @return whether the script was registered. + */ + @JvmStatic + fun install(webView: WebView, script: String, originRule: String): Boolean { + if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { + Log.w(TAG, "document-start scripts unsupported; falling back to onPageStarted") + return false + } + try { + val rules: Set = Collections.singleton(originRule) + WebViewCompat.addDocumentStartJavaScript(webView, script, rules) + return true + } catch (error: Throwable) { + Log.w(TAG, "addDocumentStartJavaScript failed for $originRule", error) + return false + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java deleted file mode 100644 index 6e40bbef..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.webkit.JavascriptInterface; - -/** - * The object injected into a hosted EvenHub app's WebView as - * window.__faceclawEvenHub. The document-start shim (served inline by - * FaceclawEvenHubWebViewClient) wraps it in a promise-returning - * window.flutter_inappwebview.callHandler, which is what the EvenHub SDK - * actually talks to. - * - * JavascriptInterface methods run on a WebView-internal thread; everything - * is bounced to the main thread before touching the NativeScript listener. - */ -public class FaceclawEvenHubJsBridge { - private final FaceclawEvenHubListener listener; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - public FaceclawEvenHubJsBridge(FaceclawEvenHubListener listener) { - this.listener = listener; - } - - @JavascriptInterface - public void postMessage(final String handlerName, final String argsJson, final int callId) { - mainHandler.post(new Runnable() { - @Override - public void run() { - listener.onEvenAppMessage(handlerName, argsJson, callId); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.kt new file mode 100644 index 00000000..90c8c1e7 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.kt @@ -0,0 +1,26 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.webkit.JavascriptInterface + +/** + * The object injected into a hosted EvenHub app's WebView as + * window.__faceclawEvenHub. The document-start shim (served inline by + * FaceclawEvenHubWebViewClient) wraps it in a promise-returning + * window.flutter_inappwebview.callHandler, which is what the EvenHub SDK + * actually talks to. + * + * JavascriptInterface methods run on a WebView-internal thread; everything + * is bounced to the main thread before touching the NativeScript listener. + */ +class FaceclawEvenHubJsBridge(private val listener: FaceclawEvenHubListener) { + private val mainHandler = Handler(Looper.getMainLooper()) + + @JavascriptInterface + fun postMessage(handlerName: String?, argsJson: String?, callId: Int) { + mainHandler.post { + listener.onEvenAppMessage(handlerName, argsJson, callId) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.kt similarity index 78% rename from App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java rename to App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.kt index 5a590902..4f5024a6 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.kt @@ -1,8 +1,8 @@ -package com.faceclaw.app; +package com.faceclaw.app -import android.content.Context; -import android.view.View; -import android.webkit.WebView; +import android.content.Context +import android.view.View +import android.webkit.WebView /** * A WebView for hosting an EvenHub app that keeps rendering and running JS even @@ -25,16 +25,11 @@ import android.webkit.WebView; * service regardless of the phone's screen, so "always rendering" is what we * actually want; there is no on-phone cost while the app is unseen. */ -public class FaceclawEvenHubWebView extends WebView { +class FaceclawEvenHubWebView(context: Context) : WebView(context) { - public FaceclawEvenHubWebView(Context context) { - super(context); - } - - @Override - protected void onWindowVisibilityChanged(int visibility) { + override fun onWindowVisibilityChanged(visibility: Int) { // Never propagate a hidden window: keep Chromium's page-visibility // "visible" so the renderer isn't frozen when Faceclaw backgrounds. - super.onWindowVisibilityChanged(View.VISIBLE); + super.onWindowVisibilityChanged(View.VISIBLE) } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java deleted file mode 100644 index f8c21348..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.webkit.WebResourceRequest; -import android.webkit.WebResourceResponse; -import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.net.Uri; -import android.util.Log; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Locale; - -/** - * Serves an unpacked EvenHub app's files to its WebView from a fake per-app - * https origin, entirely offline. HTML responses get the host bridge shim - * injected at the top of the document, guaranteeing - * window.flutter_inappwebview exists before any app JS runs (apps race - * waitForEvenAppBridge against 4-6s timeouts and silently fall back to demo - * modes if the handler appears late). - * - * shouldInterceptRequest runs on a WebView-internal thread; it only touches - * files, never the NativeScript runtime. Requests to other hosts fall - * through to the network (the manifest whitelist is not yet enforced). - * - * A developer-loaded app ("Load app from URL") has no local root: host is - * empty, nothing is intercepted, and the page loads normally from its server. - * The shim is registered as a document-start script instead - * (FaceclawEvenHubDocumentStart); injectOnPageStarted is the fallback for - * WebView versions that lack that API. - */ -public class FaceclawEvenHubWebViewClient extends WebViewClient { - private static final String TAG = "FaceclawEvenHub"; - - private final String rootDir; - private final String host; - private final String injectScript; - private final boolean injectOnPageStarted; - private final FaceclawEvenHubListener listener; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - public FaceclawEvenHubWebViewClient( - String rootDir, - String host, - String injectScript, - boolean injectOnPageStarted, - FaceclawEvenHubListener listener) { - this.rootDir = rootDir; - this.host = host; - this.injectScript = injectScript; - this.injectOnPageStarted = injectOnPageStarted; - this.listener = listener; - } - - @Override - public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { - if (host == null || host.isEmpty()) { - return null; // Remote app: everything comes from the network. - } - Uri url = request.getUrl(); - if (url == null || !host.equals(url.getHost())) { - return null; // External hosts: normal network handling. - } - String path = url.getPath(); - if (path == null || path.isEmpty() || path.equals("/")) { - path = "/index.html"; - } - try { - File root = new File(rootDir).getCanonicalFile(); - File target = new File(root, path.substring(1)).getCanonicalFile(); - if (!target.getPath().startsWith(root.getPath()) || !target.isFile()) { - Log.w(TAG, "404 " + path); - return new WebResourceResponse( - "text/plain", "utf-8", - new ByteArrayInputStream("not found".getBytes(StandardCharsets.UTF_8))); - } - String mime = mimeTypeFor(target.getName()); - if (mime.equals("text/html")) { - return new WebResourceResponse(mime, "utf-8", new ByteArrayInputStream(injectIntoHtml(target))); - } - InputStream stream = new FileInputStream(target); - return new WebResourceResponse(mime, null, stream); - } catch (IOException e) { - Log.e(TAG, "serve failed for " + path, e); - return new WebResourceResponse( - "text/plain", "utf-8", - new ByteArrayInputStream("error".getBytes(StandardCharsets.UTF_8))); - } - } - - @Override - public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) { - super.onPageStarted(view, url, favicon); - if (!injectOnPageStarted) return; - // Best-effort: the document has committed but the parser has not run - // the page's own scripts yet, so this usually lands first. Only used - // when document-start scripts are unavailable. - view.evaluateJavascript(injectScript, null); - } - - @Override - public void onPageFinished(WebView view, final String url) { - mainHandler.post(new Runnable() { - @Override - public void run() { - listener.onPageFinished(url); - } - }); - } - - private byte[] injectIntoHtml(File file) throws IOException { - byte[] raw = readAll(file); - String html = new String(raw, StandardCharsets.UTF_8); - String tag = ""; - // After if present, else before everything. - int headIndex = indexOfIgnoreCase(html, "= 0) { - int close = html.indexOf('>', headIndex); - if (close >= 0) { - return (html.substring(0, close + 1) + tag + html.substring(close + 1)) - .getBytes(StandardCharsets.UTF_8); - } - } - return (tag + html).getBytes(StandardCharsets.UTF_8); - } - - private static int indexOfIgnoreCase(String haystack, String needle) { - return haystack.toLowerCase(Locale.ROOT).indexOf(needle); - } - - private static byte[] readAll(File file) throws IOException { - FileInputStream in = new FileInputStream(file); - try { - byte[] data = new byte[(int) file.length()]; - int off = 0; - while (off < data.length) { - int n = in.read(data, off, data.length - off); - if (n < 0) throw new IOException("short read: " + file); - off += n; - } - return data; - } finally { - in.close(); - } - } - - private static String mimeTypeFor(String name) { - String lower = name.toLowerCase(Locale.ROOT); - if (lower.endsWith(".html") || lower.endsWith(".htm")) return "text/html"; - if (lower.endsWith(".js") || lower.endsWith(".mjs")) return "application/javascript"; - if (lower.endsWith(".css")) return "text/css"; - if (lower.endsWith(".json")) return "application/json"; - if (lower.endsWith(".png")) return "image/png"; - if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg"; - if (lower.endsWith(".gif")) return "image/gif"; - if (lower.endsWith(".svg")) return "image/svg+xml"; - if (lower.endsWith(".webp")) return "image/webp"; - if (lower.endsWith(".ico")) return "image/x-icon"; - if (lower.endsWith(".wasm")) return "application/wasm"; - if (lower.endsWith(".woff")) return "font/woff"; - if (lower.endsWith(".woff2")) return "font/woff2"; - if (lower.endsWith(".ttf")) return "font/ttf"; - if (lower.endsWith(".txt") || lower.endsWith(".map")) return "text/plain"; - return "application/octet-stream"; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.kt new file mode 100644 index 00000000..af821b0b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.kt @@ -0,0 +1,116 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView +import android.webkit.WebViewClient +import android.net.Uri +import android.util.Log + +import java.io.ByteArrayInputStream +import java.io.File +import java.io.FileInputStream +import java.io.IOException +import java.io.InputStream +import java.nio.charset.StandardCharsets + +/** + * Serves an unpacked EvenHub app's files to its WebView from a fake per-app + * https origin, entirely offline. HTML responses get the host bridge shim + * injected at the top of the document, guaranteeing + * window.flutter_inappwebview exists before any app JS runs (apps race + * waitForEvenAppBridge against 4-6s timeouts and silently fall back to demo + * modes if the handler appears late). + * + * shouldInterceptRequest runs on a WebView-internal thread; it only touches + * files, never the NativeScript runtime. Requests to other hosts fall + * through to the network (the manifest whitelist is not yet enforced). + * + * A developer-loaded app ("Load app from URL") has no local root: host is + * empty, nothing is intercepted, and the page loads normally from its server. + * The shim is registered as a document-start script instead + * (FaceclawEvenHubDocumentStart); injectOnPageStarted is the fallback for + * WebView versions that lack that API. + */ +class FaceclawEvenHubWebViewClient( + private val rootDir: String?, + private val host: String?, + private val injectScript: String, + private val injectOnPageStarted: Boolean, + private val listener: FaceclawEvenHubListener +) : WebViewClient() { + private val mainHandler = Handler(Looper.getMainLooper()) + + override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? { + if (host == null || host.isEmpty()) { + return null // Remote app: everything comes from the network. + } + val url: Uri? = request.url + if (url == null || host != url.host) { + return null // External hosts: normal network handling. + } + val path = url.path + try { + // Path rules, MIME table and shim injection are shared (EvenHubAssetServer); + // the canonical-file check below additionally defeats symlinks. + val asset = EvenHubAssetServer.resolve(path) + val root = File(rootDir).canonicalFile + val target = asset?.let { File(root, it.relativePath).canonicalFile } + if (asset == null || target == null || !EvenHubAssetServer.isInsideRoot(root.path, target.path) || !target.isFile) { + Log.w(TAG, "404 $path") + return WebResourceResponse( + "text/plain", "utf-8", + ByteArrayInputStream("not found".toByteArray(StandardCharsets.UTF_8))) + } + if (asset.isHtml) { + val html = EvenHubAssetServer.injectIntoHtml(String(readAll(target), StandardCharsets.UTF_8), injectScript) + return WebResourceResponse(asset.mime, "utf-8", ByteArrayInputStream(html.toByteArray(StandardCharsets.UTF_8))) + } + val stream: InputStream = FileInputStream(target) + return WebResourceResponse(asset.mime, null, stream) + } catch (e: IOException) { + Log.e(TAG, "serve failed for $path", e) + return WebResourceResponse( + "text/plain", "utf-8", + ByteArrayInputStream("error".toByteArray(StandardCharsets.UTF_8))) + } + } + + override fun onPageStarted(view: WebView, url: String?, favicon: android.graphics.Bitmap?) { + super.onPageStarted(view, url, favicon) + if (!injectOnPageStarted) return + // Best-effort: the document has committed but the parser has not run + // the page's own scripts yet, so this usually lands first. Only used + // when document-start scripts are unavailable. + view.evaluateJavascript(injectScript, null) + } + + override fun onPageFinished(view: WebView, url: String?) { + mainHandler.post { + listener.onPageFinished(url) + } + } + + companion object { + private const val TAG = "FaceclawEvenHub" + + @Throws(IOException::class) + private fun readAll(file: File): ByteArray { + val input = FileInputStream(file) + try { + val data = ByteArray(file.length().toInt()) + var off = 0 + while (off < data.size) { + val n = input.read(data, off, data.size - off) + if (n < 0) throw IOException("short read: $file") + off += n + } + return data + } finally { + input.close() + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt similarity index 56% rename from App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java rename to App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt index d50d00ae..7e563d0e 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt @@ -1,16 +1,15 @@ -package com.faceclaw.app; +package com.faceclaw.app -import android.app.Activity; -import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.view.View; -import android.view.ViewGroup; -import android.webkit.WebView; -import android.widget.FrameLayout; +import android.app.Activity +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.view.View +import android.view.ViewGroup +import android.webkit.WebView +import android.widget.FrameLayout -import java.util.ArrayList; -import java.util.List; +import java.util.ArrayList /** * Keeps EvenHub app WebViews alive and rendering while the phone shows the @@ -49,109 +48,113 @@ import java.util.List; * activity is foreground. When Faceclaw itself is backgrounded, the window goes * invisible and Chromium freezes the renderer — the timer/rAF ticks and pushed * input events then queue up and only run once foregrounded again. Two things - * prevent that: the WebViews are {@link FaceclawEvenHubWebView}, which lies to + * prevent that: the WebViews are [FaceclawEvenHubWebView], which lies to * Chromium about window visibility so the page never goes hidden; and each is * pinned to IMPORTANT renderer priority (not waived when not visible) with * timers/onResume asserted on attach. The main Looper keeps ticking under the * foreground service, so apps keep driving their glasses windows in the * background. */ -public class FaceclawEvenHubWebViewHost { - private static FaceclawEvenHubWebViewHost instance; +class FaceclawEvenHubWebViewHost { + private var overlay: FrameLayout? = null + private var shown = false - /** How often the host fires the JS timer queue. 60Hz keeps games smooth. */ - private static final long TICK_MS = 16; - - private FrameLayout overlay; - private boolean shown = false; - - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final List webViews = new ArrayList<>(); - private boolean ticking = false; - private final Runnable ticker = new Runnable() { - @Override - public void run() { - for (int i = 0; i < webViews.size(); i++) { - webViews.get(i).evaluateJavascript( - "window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null); + private val mainHandler = Handler(Looper.getMainLooper()) + private val webViews: MutableList = ArrayList() + private var ticking = false + private val ticker: Runnable = object : Runnable { + override fun run() { + for (i in 0 until webViews.size) { + webViews[i].evaluateJavascript( + "window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null) } - if (ticking) mainHandler.postDelayed(this, TICK_MS); + if (ticking) mainHandler.postDelayed(this, TICK_MS) } - }; - - public static synchronized FaceclawEvenHubWebViewHost getInstance() { - if (instance == null) instance = new FaceclawEvenHubWebViewHost(); - return instance; } - private FrameLayout ensureOverlay(Activity activity) { - if (overlay != null) return overlay; - ViewGroup content = activity.findViewById(android.R.id.content); - overlay = new FrameLayout(activity); + companion object { + private var instance: FaceclawEvenHubWebViewHost? = null + + /** How often the host fires the JS timer queue. 60Hz keeps games smooth. */ + private const val TICK_MS = 16L + + @JvmStatic + @Synchronized + fun getInstance(): FaceclawEvenHubWebViewHost { + if (instance == null) instance = FaceclawEvenHubWebViewHost() + return instance!! + } + } + + private fun ensureOverlay(activity: Activity): FrameLayout { + overlay?.let { return it } + val content = activity.findViewById(android.R.id.content) + val created = FrameLayout(activity) + overlay = created // index 0 = behind the NativeScript content view. - content.addView(overlay, 0, new FrameLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - return overlay; + content.addView(created, 0, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + return created } /** Add a WebView to the host, full-size and rendering, hidden behind the UI. */ - public void attach(Activity activity, WebView web) { - FrameLayout o = ensureOverlay(activity); - web.setVisibility(View.VISIBLE); - if (web.getParent() == null) { - o.addView(web, new FrameLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + fun attach(activity: Activity, web: WebView) { + val o = ensureOverlay(activity) + web.visibility = View.VISIBLE + if (web.parent == null) { + o.addView(web, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) } // resumeTimers() is process-global; make sure nothing left timers paused. // onResume() undoes any per-WebView pause. Together with // FaceclawEvenHubWebView faking window visibility, this keeps the page // running JS while Faceclaw is backgrounded. - web.resumeTimers(); - web.onResume(); + web.resumeTimers() + web.onResume() // Keep the renderer process at IMPORTANT priority even when the WebView // isn't visible (waivedWhenNotVisible=false), so Android doesn't // deprioritize/kill it in the background. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false); + web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false) } - if (!webViews.contains(web)) webViews.add(web); + if (!webViews.contains(web)) webViews.add(web) if (!ticking) { - ticking = true; - mainHandler.postDelayed(ticker, TICK_MS); + ticking = true + mainHandler.postDelayed(ticker, TICK_MS) } } /** Bring an app's WebView to the front so it is visible on the phone. */ - public void showOnPhone(WebView web) { - if (overlay == null) return; - web.bringToFront(); - overlay.bringToFront(); - shown = true; + fun showOnPhone(web: WebView) { + val o = overlay ?: return + web.bringToFront() + o.bringToFront() + shown = true } /** Send the overlay back behind the NativeScript UI. */ - public void hideOnPhone() { - shown = false; - if (overlay == null) return; - ViewGroup content = (ViewGroup) overlay.getParent(); + fun hideOnPhone() { + shown = false + val o = overlay ?: return + val content = o.parent as ViewGroup? if (content != null) { - content.removeView(overlay); - content.addView(overlay, 0); + content.removeView(o) + content.addView(o, 0) } } - public boolean isShown() { - return shown; + fun isShown(): Boolean { + return shown } /** Remove and destroy a WebView (its app is closing). */ - public void detach(WebView web) { - webViews.remove(web); + fun detach(web: WebView) { + webViews.remove(web) if (webViews.isEmpty() && ticking) { - ticking = false; - mainHandler.removeCallbacks(ticker); + ticking = false + mainHandler.removeCallbacks(ticker) } - if (overlay != null) overlay.removeView(web); - web.destroy(); + overlay?.removeView(web) + web.destroy() } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java deleted file mode 100644 index 3a941ac6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java +++ /dev/null @@ -1,695 +0,0 @@ -package com.faceclaw.app; - -import android.bluetooth.BluetoothGatt; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -/** - * Ports g2flash.py's OTA flash procedure to Android. Streams the EVENOTA - * firmware container to each lens over the firmware-data service using the - * aa21 envelope (BleProtocol.framePb) and the c0/c1 control/data protocol. - * - * Each lens connection completes the sid-0x80 security-auth exchange before - * OTA BEGIN — firmware 2.2.9 closes unauthenticated links ~30 s in, killing - * the transfer mid-component. No control-channel traffic is injected during - * the transfer itself: official OTA captures show none between BEGIN and the - * final END, heartbeats demonstrably do not satisfy the 2.2.9 deadline, and a - * concurrent control writer would interleave with the marker/data - * transactions. (See ../notes/ble-connections-2.2.9.md.) - * - * Lenses are flashed one at a time (left, then right). Finishing either lens - * reboots BOTH lenses, so the second lens is briefly unreachable — the reconnect - * for it uses a generous window rather than failing fast. - * - * Separate from FaceclawBleCommunicator on purpose: this speaks the stock OTA - * protocol and must run before the custom firmware exists. It reuses the GATT - * wrapper (FaceclawBleManager) and framing (BleProtocol). - */ -public class FaceclawFirmwareFlasher implements FaceclawBleListener { - private static final String TAG = "FaceclawFlasher"; - - // OTA message types (envelope sid byte) and control opcodes. - private static final int SID_CTRL = 0xc0; - private static final int SID_DATA = 0xc1; - private static final int FLAG_OTA = 0x00; - private static final int OP_BEGIN = 0x00; - private static final int OP_FILE_CHECK = 0x01; - private static final int OP_BLOCK = 0x02; - private static final int OP_END = 0x03; - - private static final int BLOCK_SIZE = 4096; - private static final int BLOCK_ACK_TIMEOUT_MS = 4_000; - private static final int CTRL_ACK_TIMEOUT_MS = 8_000; - private static final int BLOCK_NAK_RETRIES = 3; - private static final int COMPONENT_RETRIES = 3; - - // Reconnect windows. The first lens connects from an idle device; the second - // must ride out the post-first-lens reboot of both lenses. - private static final int FIRST_LENS_CONNECT_WINDOW_MS = 30_000; - private static final int SECOND_LENS_CONNECT_WINDOW_MS = 120_000; - private static final int REBOOT_SETTLE_MS = 5_000; - private static final int NOTIFY_SETTLE_MS = 2_500; - private static final int RETRY_DELAY_MS = 2_500; - - // END ack statuses that mean "component accepted": SUCCESS, UPDATING, SYS_RESTART. - private static final int[] END_OK = new int[] {0, 8, 9}; - - // Firmware layout expectations (mirrors g2flash.py). Firmware 2.2.4 has 5 - // components; 2.2.6 has 6. - private static final int MIN_SEGMENTS = 5; - private static final int MAX_SEGMENTS = 6; - private static final String REQUIRED_SEGMENT = "ota/s200_firmware_ota.bin"; - private static final long APP_LOAD_ADDR = 0x00438000L; - private static final long APP_MAX_END = 0x007F0000L; - private static final int APP_PREAMBLE = 0x20; - - private final Context context; - private final String leftAddress; - private final String rightAddress; - private final String firmwarePath; - private final FaceclawBleManager bleManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private final LinkedBlockingQueue dataAcks = new LinkedBlockingQueue<>(); - - private volatile FaceclawFirmwareFlasherListener listener; - private volatile Thread worker; - private volatile boolean cancelled = false; - - private int nextSeq = 0; - private int nextAuthMagic = 0x60; - private int pendingAuthMagic = -1; - private CountDownLatch authLatch; - private byte[] authAckPb; - - public FaceclawFirmwareFlasher(Context context, String rightAddress, String leftAddress, String firmwarePath) { - this.context = context.getApplicationContext(); - this.rightAddress = rightAddress == null ? "" : rightAddress; - this.leftAddress = leftAddress == null ? "" : leftAddress; - this.firmwarePath = firmwarePath == null ? "" : firmwarePath; - this.bleManager = new FaceclawBleManager(this.context); - this.bleManager.setListener(this); - } - - public void setListener(FaceclawFirmwareFlasherListener listener) { - this.listener = listener; - } - - public void start() { - synchronized (lock) { - if (worker != null) { - return; - } - worker = new Thread(this::run, "faceclaw-flasher"); - worker.start(); - } - } - - public void cancel() { - cancelled = true; - Thread w = worker; - if (w != null) { - w.interrupt(); - } - } - - public void close() { - cancel(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void run() { - try { - if (leftAddress.trim().isEmpty() || rightAddress.trim().isEmpty()) { - throw new IllegalStateException("Both lens addresses are required to flash."); - } - - emitState("validating", ""); - byte[] img = readFile(firmwarePath); - List segs = validate(img); - emitLog("firmware validated: " + segs.size() + " components, " + img.length + " bytes"); - - flashLens("left", leftAddress, img, segs, FIRST_LENS_CONNECT_WINDOW_MS); - if (cancelled) { - emitState("error", "Cancelled."); - emitComplete(false, "Cancelled after the left lens."); - return; - } - - emitState("rebooting", "Left lens done. Both lenses reboot briefly; reconnecting for the right lens."); - sleepInterruptibly(REBOOT_SETTLE_MS); - flashLens("right", rightAddress, img, segs, SECOND_LENS_CONNECT_WINDOW_MS); - - emitState("done", ""); - emitComplete(true, "Both lenses flashed. The glasses are rebooting into the custom firmware."); - } catch (Exception e) { - String message = e.getMessage() == null ? e.toString() : e.getMessage(); - if (cancelled) { - message = "Cancelled."; - } - emitState("error", message); - emitComplete(false, message); - } finally { - teardown(); - } - } - - private void flashLens(String lens, String address, byte[] img, List segs, int connectWindowMs) - throws TimeoutException { - emitState("connecting", lens); - connectLensResilient(lens, address, connectWindowMs); - sleepInterruptibly(NOTIFY_SETTLE_MS); - drainAcks(); - // Fresh envelope counter for the OTA stream (the auth exchange during - // bring-up used its own); mirrors the stock sequence. - resetSeq(); - - emitState("flashing", lens); - int beginStatus = sendCtrlAndWait(address, OP_BEGIN, EMPTY, CTRL_ACK_TIMEOUT_MS); - if (!isEndOk(beginStatus)) { - emitLog("warning: unexpected begin status " + beginStatus + "; continuing"); - } - // Progress is reported in bytes across the whole image: the segments - // are one large firmware blob plus several small ones, so a bar - // stepped per segment would sit still through the big one and then - // sprint through the rest. - long totalBytes = 0; - for (Segment seg : segs) { - totalBytes += seg.ps; - } - long bytesBefore = 0; - for (int i = 0; i < segs.size(); i++) { - if (cancelled) { - throw new IllegalStateException("Cancelled."); - } - flashComponentWithRetry(lens, address, i, segs.size(), segs.get(i), img, bytesBefore, totalBytes); - bytesBefore += segs.get(i).ps; - } - emitLog(lens + " lens: all components verified"); - try { - bleManager.disconnect(address); - } catch (Exception ignored) { - } - } - - private void flashComponentWithRetry(String lens, String address, int index, int count, Segment seg, byte[] img, - long bytesBefore, long totalBytes) { - for (int attempt = 0; attempt < COMPONENT_RETRIES; attempt++) { - if (attempt > 0) { - emitLog(seg.name + ": re-flash attempt " + (attempt + 1) + "/" + COMPONENT_RETRIES); - } - int endStatus; - try { - endStatus = flashComponent(lens, address, index, count, seg, img, bytesBefore, totalBytes); - } catch (TimeoutException | RuntimeException e) { - emitLog(seg.name + ": block phase failed: " + e.getMessage()); - endStatus = -1; - } - if (isEndOk(endStatus)) { - emitLog(seg.name + ": END verify OK (status " + endStatus + ")"); - return; - } - if (endStatus >= 0) { - emitLog(seg.name + ": END verify FAILED (status " + endStatus + ")"); - } - drainAcks(); - sleepInterruptibly(1_500); - } - throw new IllegalStateException("component " + seg.name + " failed after " + COMPONENT_RETRIES + " attempts"); - } - - private int flashComponent(String lens, String address, int index, int count, Segment seg, byte[] img, - long bytesBefore, long totalBytes) throws TimeoutException { - byte[] sub = Arrays.copyOfRange(img, seg.off, seg.off + 128); - int ps = seg.ps; - int payloadStart = seg.off + 128; - - int checkStatus = sendCtrlAndWait(address, OP_FILE_CHECK, sub, CTRL_ACK_TIMEOUT_MS); - if (checkStatus != 0) { - throw new IllegalStateException("FILE_CHECK rejected status=" + checkStatus); - } - - int blockCount = (ps + BLOCK_SIZE - 1) / BLOCK_SIZE; - for (int b = 0; b < blockCount; b++) { - if (cancelled) { - throw new IllegalStateException("Cancelled."); - } - int start = payloadStart + b * BLOCK_SIZE; - int end = Math.min(start + BLOCK_SIZE, payloadStart + ps); - byte[] block = Arrays.copyOfRange(img, start, end); - - boolean accepted = false; - for (int tries = 0; tries < BLOCK_NAK_RETRIES; tries++) { - int status = sendBlock(address, block); // throws TimeoutException -> component re-flash - if (status == 0) { - accepted = true; - break; - } - emitLog(seg.name + ": block " + b + "/" + blockCount + " NAK=" + status - + " resend " + (tries + 1) + "/" + BLOCK_NAK_RETRIES); - } - if (!accepted) { - throw new IllegalStateException("block " + b + " NAK'd " + BLOCK_NAK_RETRIES + " times"); - } - if (b % 20 == 0 || b == blockCount - 1) { - long bytesSent = bytesBefore + Math.min((long) (b + 1) * BLOCK_SIZE, (long) ps); - emitProgress(lens, index + 1, count, b + 1, blockCount, bytesSent, totalBytes); - } - } - emitLog(seg.name + ": data phase done; sending END"); - return sendCtrlAndWait(address, OP_END, EMPTY, CTRL_ACK_TIMEOUT_MS); - } - - /** Send one 4 KB block as a marker + data pair sharing one envelope seq. */ - private int sendBlock(String address, byte[] block) throws TimeoutException { - int seq = nextSeq(); - drainAcks(); - writeOta(address, BleProtocol.OTA_DATA_WRITE_UUID, SID_CTRL, new byte[] {(byte) OP_BLOCK}, seq); - writeOta(address, BleProtocol.OTA_DATA_WRITE_UUID, SID_DATA, block, seq); - return waitAck(OP_BLOCK, BLOCK_ACK_TIMEOUT_MS); - } - - private int sendCtrlAndWait(String address, int op, byte[] data, int timeoutMs) throws TimeoutException { - int seq = nextSeq(); - drainAcks(); - byte[] payload = new byte[1 + data.length]; - payload[0] = (byte) op; - System.arraycopy(data, 0, payload, 1, data.length); - writeOta(address, BleProtocol.OTA_DATA_WRITE_UUID, SID_CTRL, payload, seq); - return waitAck(op, timeoutMs); - } - - private boolean writeOta(String address, String writeChar, int sid, byte[] payload, int seq) { - List frames = BleProtocol.framePb(payload, sid, FLAG_OTA, seq); - return bleManager.writeFrames( - address, writeChar, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS); - } - - private int waitAck(int wantOp, int timeoutMs) throws TimeoutException { - long deadline = SystemClock.elapsedRealtime() + timeoutMs; - while (true) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - byte[] pb; - try { - pb = dataAcks.poll(remaining, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new TimeoutException("interrupted waiting for ack op=" + wantOp); - } - if (pb == null) { - break; - } - if (pb.length >= 2 && (pb[0] & 0xff) == wantOp) { - return pb[1] & 0xff; - } - // Different opcode (e.g. a late ack from a prior stage) — ignore, keep waiting. - } - throw new TimeoutException("no ack op=0x" + Integer.toHexString(wantOp) + " within " + timeoutMs + "ms"); - } - - // ---- connection ---------------------------------------------------------- - - private void connectLensResilient(String lens, String address, int windowMs) { - long deadline = SystemClock.elapsedRealtime() + windowMs; - String lastError = "unknown"; - int attempt = 0; - while (SystemClock.elapsedRealtime() < deadline && !cancelled) { - attempt++; - try { - if (bringUpLens(address)) { - emitLog("connected " + lens + " lens (attempt " + attempt + ")"); - return; - } - lastError = "connect/discover incomplete"; - } catch (Exception e) { - lastError = e.getMessage() == null ? e.toString() : e.getMessage(); - } - emitLog(lens + " connect attempt " + attempt + " failed (" + lastError + "); retrying..."); - try { - bleManager.disconnect(address); - } catch (Exception ignored) { - } - sleepInterruptibly(RETRY_DELAY_MS); - } - if (cancelled) { - throw new IllegalStateException("Cancelled."); - } - throw new IllegalStateException("could not reach " + lens + " lens: " + lastError); - } - - private boolean bringUpLens(String address) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - return false; - } - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - return false; - } - // Control-channel notify carries the auth response; mandatory. - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - return false; - } - if (!bleManager.enableNotifications(address, BleProtocol.OTA_DATA_NOTIFY_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - return false; - } - if (!authenticate(address)) { - throw new IllegalStateException( - "security auth not acknowledged — if Android shows a Bluetooth pairing request, accept it"); - } - return true; - } - - /** - * Complete the sid-0x80 security-auth exchange on this connection. On an - * unbonded phone this triggers SMP pairing (possibly with an OS prompt), - * so the wait is generous. Must succeed before any OTA traffic. - */ - private boolean authenticate(String address) { - int magic; - CountDownLatch latch = new CountDownLatch(1); - synchronized (lock) { - magic = nextAuthMagic; - nextAuthMagic = nextAuthMagic >= 0x7f ? 0x60 : nextAuthMagic + 1; - pendingAuthMagic = magic; - authAckPb = null; - authLatch = latch; - } - try { - if (!writeOta(address, BleProtocol.WRITE_CHAR_UUID, BleProtocol.SID_SECURITY_AUTH, - BleProtocol.buildAuthenticationRequest(magic), nextSeq())) { - emitLog("auth write failed: " + address); - return false; - } - boolean signalled; - try { - signalled = latch.await(ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - byte[] pb; - synchronized (lock) { - pb = authAckPb; - } - if (!signalled || !BleProtocol.isAuthenticationSuccess(pb, magic)) { - emitLog("security auth not acknowledged (" + address - + ") — if Android shows a Bluetooth pairing request, accept it"); - return false; - } - emitLog("security auth complete: " + address); - return true; - } finally { - synchronized (lock) { - pendingAuthMagic = -1; - authLatch = null; - } - } - } - - // ---- firmware container parsing / validation ----------------------------- - - private static final byte[] EMPTY = new byte[0]; - - private static final class Segment { - final int off; - final int ps; - final long crc; - final String name; - - Segment(int off, int ps, long crc, String name) { - this.off = off; - this.ps = ps; - this.crc = crc; - this.name = name; - } - } - - private List parseSegments(byte[] img) { - if (img.length < 0x40) { - throw new IllegalStateException("file is too small to be a firmware image"); - } - long n = readU32(img, 8); - if (n <= 0 || n > 64) { - throw new IllegalStateException("implausible component count " + n + " (corrupt image?)"); - } - List segs = new ArrayList<>(); - for (int i = 0; i < n; i++) { - int base = 0x40 + i * 16; - long crc = readU32(img, base + 12); - int off = (int) readU32(img, base + 4); - if (off + 128 > img.length) { - throw new IllegalStateException("segment " + i + " subheader runs past end of file"); - } - int ps = (int) readU32(img, off + 8); - String name = readCString(img, off + 48, 80); - segs.add(new Segment(off, ps, crc, name)); - } - return segs; - } - - private List validate(byte[] img) { - List segs = parseSegments(img); - if (segs.size() < MIN_SEGMENTS || segs.size() > MAX_SEGMENTS) { - throw new IllegalStateException( - "expected " + MIN_SEGMENTS + "-" + MAX_SEGMENTS + " components, found " + segs.size()); - } - Segment main = null; - for (Segment s : segs) { - byte[] payload = Arrays.copyOfRange(img, s.off + 128, s.off + 128 + s.ps); - long calc = crc32cMsb(payload) & 0xffffffffL; - long subCrc = readU32(img, s.off + 12); - if (calc != s.crc || calc != subCrc) { - throw new IllegalStateException("component " + s.name + " CRC32C is stale (image not checksum-fixed)"); - } - if (REQUIRED_SEGMENT.equals(s.name)) { - main = s; - } - } - if (main == null) { - throw new IllegalStateException("required component " + REQUIRED_SEGMENT + " not found"); - } - checkMainAppFitsMram(img, main); - return segs; - } - - private void checkMainAppFitsMram(byte[] img, Segment main) { - if (main.ps < APP_PREAMBLE) { - throw new IllegalStateException("main-app payload is smaller than its preamble"); - } - long loadAddr = readU32(img, main.off + 128 + 0x14); - long preLen = readU32(img, main.off + 128) & 0xFFFFFFL; - if (loadAddr != APP_LOAD_ADDR) { - throw new IllegalStateException( - "main-app preamble load address is 0x" + Long.toHexString(loadAddr) - + ", expected 0x" + Long.toHexString(APP_LOAD_ADDR)); - } - if (preLen != main.ps) { - throw new IllegalStateException( - "main-app preamble length (" + preLen + ") != staged payload size (" + main.ps + ")"); - } - long progEnd = APP_LOAD_ADDR + main.ps - APP_PREAMBLE; - if (progEnd > APP_MAX_END) { - long over = progEnd - APP_MAX_END; - throw new IllegalStateException( - "main-app is too large: programmed region ends at 0x" + Long.toHexString(progEnd) - + ", " + over + " bytes past the safe MRAM ceiling — refusing to flash (brick risk)"); - } - } - - // ---- listener callbacks -------------------------------------------------- - - @Override - public void onNotification(String address, String characteristicUuid, byte[] data) { - if (BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { - // Control channel: only the security-auth response is awaited here. - // A value can carry several envelope frames back to back (e.g. the - // response packed with one of the lens's own sid-0x80 notifies). - for (byte[] buf : BleProtocol.splitFrames(data)) { - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); - if (frame.ok && frame.sid == BleProtocol.SID_SECURITY_AUTH) { - synchronized (lock) { - if (authLatch != null && frame.msgSeq == pendingAuthMagic) { - authAckPb = frame.pb; - authLatch.countDown(); - } - } - } - } - return; - } - if (!BleProtocol.OTA_DATA_NOTIFY_UUID.equalsIgnoreCase(characteristicUuid)) { - return; // OTA acks arrive on the data-notify char - } - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); - if (!frame.ok || frame.pb.length < 2) { - return; - } - dataAcks.add(Arrays.copyOf(frame.pb, Math.min(frame.pb.length, 2))); - } - - @Override - public void onConnectionStateChange(String address, boolean connected) { - if (!connected) { - Log.i(TAG, "disconnected: " + address); - } - } - - // ---- helpers ------------------------------------------------------------- - - private void teardown() { - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void resetSeq() { - synchronized (lock) { - nextSeq = 0; - } - } - - private int nextSeq() { - synchronized (lock) { - nextSeq = (nextSeq + 1) & 0xff; - return nextSeq; - } - } - - private void drainAcks() { - dataAcks.clear(); - } - - private static boolean isEndOk(int status) { - for (int ok : END_OK) { - if (ok == status) { - return true; - } - } - return false; - } - - private void sleepInterruptibly(int ms) { - try { - Thread.sleep(ms); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - private static byte[] readFile(String path) { - try (FileInputStream fis = new FileInputStream(path)) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buf = new byte[65536]; - int read; - while ((read = fis.read(buf)) >= 0) { - out.write(buf, 0, read); - } - return out.toByteArray(); - } catch (IOException e) { - throw new IllegalStateException("could not read firmware: " + e.getMessage(), e); - } - } - - private static long readU32(byte[] buf, int offset) { - return (buf[offset] & 0xffL) - | ((buf[offset + 1] & 0xffL) << 8) - | ((buf[offset + 2] & 0xffL) << 16) - | ((buf[offset + 3] & 0xffL) << 24); - } - - private static String readCString(byte[] buf, int offset, int maxLen) { - int end = offset; - int limit = Math.min(buf.length, offset + maxLen); - while (end < limit && buf[end] != 0) { - end++; - } - return new String(buf, offset, end - offset, StandardCharsets.ISO_8859_1); - } - - private static final int[] CRC32C_TABLE = buildCrc32cTable(); - - private static int[] buildCrc32cTable() { - int[] table = new int[256]; - for (int b = 0; b < 256; b++) { - int c = b << 24; - for (int i = 0; i < 8; i++) { - c = (c & 0x80000000) != 0 ? (c << 1) ^ 0x1edc6f41 : c << 1; - } - table[b] = c; - } - return table; - } - - /** CRC-32C, MSB-first, init 0, no final xor (matches g2flash.py crc32c_msb). */ - private static int crc32cMsb(byte[] data) { - int crc = 0; - for (byte value : data) { - crc = (crc << 8) ^ CRC32C_TABLE[((crc >>> 24) ^ (value & 0xff)) & 0xff]; - } - return crc; - } - - private void emitLog(String line) { - Log.i(TAG, line); - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onLog(line); - } - }); - } - - private void emitProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount, - long bytesSent, long bytesTotal) { - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onProgress(lens, componentIndex, componentCount, blockIndex, blockCount, bytesSent, bytesTotal); - } - }); - } - - private void emitState(String state, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onState(state, safeDetail); - } - }); - } - - private void emitComplete(boolean success, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onComplete(success, safeDetail); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.kt new file mode 100644 index 00000000..6169c965 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper + +/** + * Android host for the shared [OtaFlashFlow] (g2flash.py's OTA procedure over the stock + * firmware-data service): owns the GATT link (AndroidStockLink) and the worker thread, and + * delivers listener callbacks on the main thread. Separate from FaceclawBleCommunicator on + * purpose: this speaks the stock OTA protocol and must run before the custom firmware exists. + * Public API unchanged for app/native/firmware-flasher.ts. + */ +class FaceclawFirmwareFlasher(context: Context, rightAddress: String?, leftAddress: String?, firmwarePath: String?) { + private val link = AndroidStockLink(context.applicationContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val lock = Any() + + @Volatile + private var listener: FaceclawFirmwareFlasherListener? = null + + @Volatile + private var worker: Thread? = null + + private val flow = + OtaFlashFlow( + link, + rightAddress ?: "", + leftAddress ?: "", + firmwarePath ?: "", + object : FaceclawFirmwareFlasherListener { + override fun onLog(line: String?) { + android.util.Log.i(OtaFlashFlow.TAG, line ?: "") + post { it.onLog(line) } + } + + override fun onProgress( + lens: String?, + componentIndex: Int, + componentCount: Int, + blockIndex: Int, + blockCount: Int, + bytesSent: Long, + bytesTotal: Long, + ) = post { it.onProgress(lens, componentIndex, componentCount, blockIndex, blockCount, bytesSent, bytesTotal) } + + override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") } + + override fun onComplete(success: Boolean, detail: String?) = post { it.onComplete(success, detail ?: "") } + }, + ) + + fun setListener(listener: FaceclawFirmwareFlasherListener?) { + this.listener = listener + } + + fun start() { + synchronized(lock) { + if (worker != null) { + return + } + val w = Thread({ flow.run() }, "faceclaw-flasher") + worker = w + w.start() + } + } + + fun cancel() { + flow.cancel() + } + + fun close() { + cancel() + try { + link.close() + } catch (ignored: Exception) { + } + } + + private fun post(action: (FaceclawFirmwareFlasherListener) -> Unit) { + mainHandler.post { + val current = listener + if (current != null) { + action(current) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java deleted file mode 100644 index ef342706..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ /dev/null @@ -1,546 +0,0 @@ -package com.faceclaw.app; - -import android.bluetooth.BluetoothGatt; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -/** - * A minimal, stock-firmware-compatible BLE communicator used only to show the - * pre-flash confirmation prompt on the glasses and read back the user's Yes/No - * choice. It is deliberately separate from FaceclawBleCommunicator: the main - * app assumes the custom firmware (image containers, compositor, etc.), whereas - * this must work on unmodified glasses, so it speaks only the stock subset — - * session prelude, a text container, a list container, and heartbeats. No - * images. - * - * It reuses the low-level GATT wrapper (FaceclawBleManager) and the framing / - * protobuf helpers (BleProtocol), but owns its own GATT connection and runs its - * whole lifecycle on a single worker thread. It expects the main app to be - * disconnected while it runs (onboarding, before flashing). - * - * Both arms are connected and security-authenticated up front, even though the - * prompt itself only needs the right arm (the lenses relay messages to each - * other, and acks/events only ever come from the right arm): on an unbonded - * phone the auth exchange is what raises the OS pairing prompt, and doing both - * here means both prompts appear at the start rather than one popping up half - * way through flashing the second lens. While connected, each arm's battery - * level is read (they are independent batteries) so the caller can refuse to - * flash on a low charge. - * - * With `skipPrompt` the on-glasses confirmation is not shown: the flow is just - * connect + auth + battery read + result(approved). Used to re-check the - * battery after the user has already confirmed once. - */ -public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { - private static final String TAG = "FaceclawFlashPrompt"; - - private static final String TEXT_NAME = "flashwarn"; - private static final String LIST_NAME = "flashmenu"; - private static final int TEXT_CONTAINER_ID = 1; - private static final int LIST_CONTAINER_ID = 2; - // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. - private static final String[] ITEMS = new String[] {"No, cancel", "Yes, flash"}; - - private static final int HEARTBEAT_INTERVAL_MS = 4_000; - private static final int SELECTION_TIMEOUT_MS = 120_000; - private static final int CREATE_ACK_TIMEOUT_MS = 3_000; - private static final int BATTERY_ACK_TIMEOUT_MS = 3_000; - - private final Context context; - private final String rightAddress; - private final String leftAddress; - private final String warningText; - private final boolean skipPrompt; - private final FaceclawBleManager bleManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private final ConcurrentHashMap pendingAcks = new ConcurrentHashMap<>(); - private final ConcurrentHashMap ackPayloads = new ConcurrentHashMap<>(); - private final CountDownLatch selectionLatch = new CountDownLatch(1); - - private volatile FaceclawFlashPromptListener listener; - private volatile Thread worker; - private volatile boolean cancelled = false; - private volatile boolean finished = false; - private volatile boolean rightConnected = false; - private volatile boolean leftConnected = false; - private volatile boolean rightLost = false; - private volatile Boolean approved = null; - private volatile ScheduledExecutorService heartbeatExecutor; - - private int nextMagic = 100; - private int nextSeq = 0x40; - - public FaceclawFlashPromptCommunicator( - Context context, String rightAddress, String leftAddress, String warningText, boolean skipPrompt) { - this.context = context.getApplicationContext(); - this.rightAddress = rightAddress == null ? "" : rightAddress; - this.leftAddress = leftAddress == null ? "" : leftAddress; - this.warningText = warningText == null ? "" : warningText; - this.skipPrompt = skipPrompt; - this.bleManager = new FaceclawBleManager(this.context); - this.bleManager.setListener(this); - } - - public void setListener(FaceclawFlashPromptListener listener) { - this.listener = listener; - } - - public void start() { - synchronized (lock) { - if (worker != null) { - return; - } - worker = new Thread(this::run, "faceclaw-flash-prompt"); - worker.start(); - } - } - - /** Abort the prompt (e.g. the user backed out on the phone). */ - public void cancel() { - cancelled = true; - selectionLatch.countDown(); - Thread w = worker; - if (w != null) { - w.interrupt(); - } - } - - public void close() { - cancel(); - stopHeartbeat(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void run() { - try { - if (rightAddress.trim().isEmpty()) { - emitState("error", "No right-arm address configured."); - return; - } - - emitState("connecting", ""); - connectArm(rightAddress, "right"); - rightConnected = true; - if (cancelled) { - teardown(); - return; - } - if (!leftAddress.trim().isEmpty()) { - connectArm(leftAddress, "left"); - leftConnected = true; - if (cancelled) { - teardown(); - return; - } - } - - emitState("connected", ""); - // Auth both arms now (pairing prompts, if any, happen here) before - // anything else, so both bonds exist by the time flashing starts. - authenticateArm(rightAddress, "right"); - if (leftConnected) { - authenticateArm(leftAddress, "left"); - } - if (cancelled) { - teardown(); - return; - } - sendPrelude(rightAddress); - - if (!skipPrompt) { - showPrompt(rightAddress); - startHeartbeat(); - emitState("prompting", ""); - - selectionLatch.await(SELECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS); - stopHeartbeat(); - - if (cancelled) { - emitState("cancelled", ""); - teardown(); - return; - } - if (approved == null) { - if (rightLost) { - emitState("disconnected", "Lost connection to the glasses."); - } else { - emitState("timeout", "No response from the glasses."); - } - teardown(); - return; - } - try { - sendShutdown(); - } catch (Exception ignored) { - } - if (!Boolean.TRUE.equals(approved)) { - emitResult(false); - emitState("result", "declined"); - teardown(); - return; - } - } - - // Approved (or prompt skipped): read each arm's battery while the - // links are still up, then report the result. - emitState("battery", ""); - readBatteries(); - if (cancelled) { - emitState("cancelled", ""); - teardown(); - return; - } - if (rightLost) { - emitState("disconnected", "Lost connection to the glasses."); - teardown(); - return; - } - finished = true; - emitResult(true); - emitState("result", "approved"); - teardown(); - } catch (Exception e) { - stopHeartbeat(); - if (!cancelled) { - String message = e.getMessage() == null ? e.toString() : e.getMessage(); - emitState("error", message); - } - teardown(); - } - } - - private void connectArm(String address, String arm) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("could not connect to the " + arm + " arm (" + address + ")"); - } - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("discoverServices failed: " + arm + " arm (" + address + ")"); - } - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - throw new IllegalStateException("enableNotifications failed: " + arm + " arm (" + address + ")"); - } - emitLog("connected " + arm + " arm"); - } - - /** - * Security-auth exchange on sid=0x80: firmware 2.2.9 will not run a - * session (and closes the link after ~30 s) without it, and on an - * unbonded phone this is what triggers SMP pairing, so it may sit - * waiting on an OS pairing prompt. - */ - private void authenticateArm(String address, String arm) throws InterruptedException { - int authMagic = allocMagic(); - byte[] authAck = writeAndAwaitAck( - address, - BleProtocol.SID_SECURITY_AUTH, - BleProtocol.FLAG_SECURITY_AUTH, - authMagic, - BleProtocol.buildAuthenticationRequest(authMagic), - ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS); - if (authAck == null || !BleProtocol.isAuthenticationSuccess(authAck, authMagic)) { - throw new IllegalStateException( - "could not authenticate with the " + arm + " arm (" + address - + ") — if Android shows a Bluetooth pairing request, accept it and try again"); - } - emitLog("security auth complete: " + arm + " arm"); - } - - /** Mandatory session prelude on sid=0x01 (app-launch). Right arm only; it relays. */ - private void sendPrelude(String address) throws InterruptedException { - if (writeAndAwaitAck( - address, - BleProtocol.PRELUDE_ACK_SID, - BleProtocol.FLAG_REQUEST, - BleProtocol.PRELUDE_ACK_MAGIC, - BleProtocol.PRELUDE_F5872_PAYLOAD, - ConnectionOptions.PRELUDE_TIMEOUT_MS) == null) { - throw new IllegalStateException("session prelude not acked: " + address); - } - } - - private void showPrompt(String address) throws InterruptedException { - // Create the prompt page (warning text + No/Yes list) on sid=0xe0 Cmd=0. - // Two attempts: on 2.2.9 the first request sent right after the - // prelude can be dropped while the lens emits its own sid-0x80 - // notifications (observed with the device-info settings read). - byte[] pageAck = null; - for (int attempt = 0; attempt < 2 && pageAck == null && !cancelled; attempt++) { - int magic = allocMagic(); - byte[] page = BleProtocol.buildCreatePromptPage( - magic, TEXT_NAME, TEXT_CONTAINER_ID, warningText, LIST_NAME, LIST_CONTAINER_ID, ITEMS); - pageAck = writeAndAwaitAck(address, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, magic, page, CREATE_ACK_TIMEOUT_MS); - if (pageAck == null) { - emitLog("prompt page attempt " + (attempt + 1) + " unacked: " + address); - } - } - if (pageAck == null) { - throw new IllegalStateException("prompt page not acked: " + address); - } - emitLog("prompt page shown on " + address); - } - - /** - * Read each arm's battery via a sid-0x09 settings read on that arm's own - * link (the arms have independent batteries and each answers with its - * own). An arm that does not answer reports -1; the caller decides what - * to do with a partial reading. - */ - private void readBatteries() throws InterruptedException { - int right = rightConnected ? readBattery(rightAddress, "right") : -1; - int left = leftConnected ? readBattery(leftAddress, "left") : -1; - emitLog("battery R=" + (right < 0 ? "?" : right + "%") + " L=" + (left < 0 ? "?" : left + "%")); - emitBattery(right, left); - } - - private int readBattery(String address, String arm) throws InterruptedException { - for (int attempt = 0; attempt < 2 && !cancelled; attempt++) { - int magic = allocMagic(); - byte[] ack = writeAndAwaitAck( - address, - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSettingsQuery(magic), - BATTERY_ACK_TIMEOUT_MS); - if (ack == null) { - emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm"); - continue; - } - BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(ack); - if (snapshot != null) { - return snapshot.battery; - } - emitLog("battery read ack had no battery field: " + arm + " arm"); - } - return -1; - } - - /** Write and wait for the matching ack; returns the ack's protobuf (may be empty), or null on timeout/write failure. */ - private byte[] writeAndAwaitAck(String address, int sid, int flag, int magic, byte[] payload, int timeoutMs) - throws InterruptedException { - CountDownLatch latch = new CountDownLatch(1); - String key = ackKey(sid, magic); - pendingAcks.put(key, latch); - try { - if (!writeFrame(address, sid, flag, payload)) { - return null; - } - if (!latch.await(timeoutMs, TimeUnit.MILLISECONDS)) { - return null; - } - byte[] pb = ackPayloads.get(key); - return pb == null ? new byte[0] : pb; - } finally { - pendingAcks.remove(key, latch); - ackPayloads.remove(key); - } - } - - private boolean writeFrame(String address, int sid, int flag, byte[] payload) { - int seq; - synchronized (lock) { - seq = nextSeq++ & 0xff; - } - List frames = BleProtocol.framePb(payload, sid, flag, seq); - return bleManager.writeFrames( - address, - BleProtocol.WRITE_CHAR_UUID, - frames, - AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), - ConnectionOptions.WRITE_TIMEOUT_MS); - } - - private void startHeartbeat() { - stopHeartbeat(); - ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(runnable -> { - Thread t = new Thread(runnable, "faceclaw-flash-hb"); - t.setDaemon(true); - return t; - }); - heartbeatExecutor = executor; - executor.scheduleWithFixedDelay( - this::sendHeartbeat, HEARTBEAT_INTERVAL_MS, HEARTBEAT_INTERVAL_MS, TimeUnit.MILLISECONDS); - } - - private void sendHeartbeat() { - if (finished || cancelled) { - return; - } - try { - byte[] heartbeat = BleProtocol.buildHeartbeat(allocMagic()); - if (rightConnected) { - writeFrame(rightAddress, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, heartbeat); - } - } catch (Exception e) { - Log.w(TAG, "heartbeat failed: " + e.getMessage()); - } - } - - private void stopHeartbeat() { - ScheduledExecutorService executor = heartbeatExecutor; - heartbeatExecutor = null; - if (executor != null) { - executor.shutdownNow(); - } - } - - private void sendShutdown() { - byte[] shutdown = BleProtocol.buildShutdown(allocMagic(), 0); - if (rightConnected) { - writeFrame(rightAddress, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, shutdown); - } - } - - private void teardown() { - finished = true; - stopHeartbeat(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - rightConnected = false; - leftConnected = false; - } - - @Override - public void onNotification(String address, String characteristicUuid, byte[] data) { - if (!BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { - return; - } - // A value can carry several envelope frames back to back. - for (byte[] buf : BleProtocol.splitFrames(data)) { - handleFrame(address, buf); - } - } - - private void handleFrame(String address, byte[] buf) { - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); - if (!frame.ok) { - return; - } - if (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT) { - handleEvent(frame); - return; - } - if (frame.msgSeq >= 0) { - String key = ackKey(frame.sid, frame.msgSeq); - CountDownLatch latch = pendingAcks.get(key); - if (latch != null) { - ackPayloads.put(key, frame.pb); - latch.countDown(); - } - } - } - - private void handleEvent(BleProtocol.ParsedFrame frame) { - BleProtocol.ListSelection selection = BleProtocol.parseListSelection(frame); - if (selection == null || !LIST_NAME.equals(selection.containerName)) { - return; - } - // Only a confirmed click is a decision; scroll/highlight changes are ignored. - if (selection.eventType != BleProtocol.EVENT_CLICK) { - return; - } - boolean yes = selection.itemIndex == 1 - || (selection.itemName != null && selection.itemName.toLowerCase().startsWith("yes")); - synchronized (lock) { - if (finished) { - return; - } - finished = true; - approved = yes; - } - emitLog("selection: " + (yes ? "flash" : "cancel") + " (index " + selection.itemIndex + ")"); - selectionLatch.countDown(); - } - - @Override - public void onConnectionStateChange(String address, boolean connected) { - if (connected) { - return; - } - if (address.equalsIgnoreCase(leftAddress)) { - // The prompt itself only needs the right arm; a dropped left link - // just means no left battery reading. - leftConnected = false; - emitLog("left arm disconnected"); - return; - } - if (address.equalsIgnoreCase(rightAddress)) { - rightConnected = false; - synchronized (lock) { - if (!finished && !cancelled) { - rightLost = true; - finished = true; - selectionLatch.countDown(); - } - } - } - } - - private int allocMagic() { - synchronized (lock) { - int magic = nextMagic; - nextMagic = nextMagic >= 255 ? 100 : nextMagic + 1; - return magic; - } - } - - private static String ackKey(int sid, int magic) { - return sid + ":" + magic; - } - - private void emitLog(String line) { - Log.i(TAG, line); - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onLog(line); - } - }); - } - - private void emitState(String state, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onState(state, safeDetail); - } - }); - } - - private void emitBattery(int rightPercent, int leftPercent) { - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onBattery(rightPercent, leftPercent); - } - }); - } - - private void emitResult(boolean approvedResult) { - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onResult(approvedResult); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.kt new file mode 100644 index 00000000..37ad4593 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper + +/** + * Android host for the shared [FlashPromptFlow] (the stock-firmware pre-flash Yes/No prompt + * plus battery read): owns the GATT link (AndroidStockLink) and the worker thread, and + * delivers listener callbacks on the main thread. It expects the main app to be disconnected + * while it runs (onboarding, before flashing). Public API unchanged for + * app/native/flash-prompt-communicator.ts. + */ +class FaceclawFlashPromptCommunicator( + context: Context, + rightAddress: String?, + leftAddress: String?, + warningText: String?, + skipPrompt: Boolean, +) { + private val link = AndroidStockLink(context.applicationContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val lock = Any() + + @Volatile + private var listener: FaceclawFlashPromptListener? = null + + @Volatile + private var worker: Thread? = null + + private val flow = + FlashPromptFlow( + link, + rightAddress ?: "", + leftAddress ?: "", + warningText ?: "", + skipPrompt, + object : FaceclawFlashPromptListener { + override fun onLog(line: String?) { + android.util.Log.i(FlashPromptFlow.TAG, line ?: "") + post { it.onLog(line) } + } + + override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") } + + override fun onBattery(rightPercent: Int, leftPercent: Int) = post { it.onBattery(rightPercent, leftPercent) } + + override fun onResult(approved: Boolean) = post { it.onResult(approved) } + }, + ) + + fun setListener(listener: FaceclawFlashPromptListener?) { + this.listener = listener + } + + fun start() { + synchronized(lock) { + if (worker != null) { + return + } + val w = Thread({ flow.run() }, "faceclaw-flash-prompt") + worker = w + w.start() + } + } + + /** Abort the prompt (e.g. the user backed out on the phone). */ + fun cancel() { + flow.cancel() + } + + fun close() { + cancel() + try { + link.close() + } catch (ignored: Exception) { + } + } + + private fun post(action: (FaceclawFlashPromptListener) -> Unit) { + mainHandler.post { + val current = listener + if (current != null) { + action(current) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.java deleted file mode 100644 index 44e38a0b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.java +++ /dev/null @@ -1,142 +0,0 @@ -package com.faceclaw.app; - -import android.Manifest; -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ServiceInfo; -import android.os.Build; -import android.os.IBinder; - -import androidx.core.content.ContextCompat; - -import com.tns.NativeScriptActivity; - -public class FaceclawForegroundService extends Service { - public static final String ACTION_START = "com.faceclaw.app.action.START"; - public static final String ACTION_UPDATE = "com.faceclaw.app.action.UPDATE"; - public static final String ACTION_STOP = "com.faceclaw.app.action.STOP"; - public static final String EXTRA_TEXT = "text"; - - // Channel id was bumped from "faceclaw-dashboard" when the badge setting - // changed: Android freezes a channel's showBadge flag at creation, so the - // old channel (which let Samsung's launcher count the pinned notification - // as a red "1" badge) is deleted on upgrade rather than reused. - private static final String LEGACY_CHANNEL_ID = "faceclaw-dashboard"; - private static final String CHANNEL_ID = "faceclaw-connection"; - private static final int NOTIFICATION_ID = 4201; - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent != null ? intent.getAction() : ACTION_START; - String text = intent != null ? intent.getStringExtra(EXTRA_TEXT) : null; - - if (ACTION_STOP.equals(action)) { - stopForeground(STOP_FOREGROUND_REMOVE); - stopSelf(); - return START_NOT_STICKY; - } - - ensureNotificationChannel(); - Notification notification = buildNotification( - text != null && !text.trim().isEmpty() ? text : "Connected to glasses" - ); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground(NOTIFICATION_ID, notification, foregroundServiceType()); - } else { - startForeground(NOTIFICATION_ID, notification); - } - - if (ACTION_UPDATE.equals(action)) { - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.notify(NOTIFICATION_ID, notification); - } - } - - return START_STICKY; - } - - private void ensureNotificationChannel() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - return; - } - - NotificationChannel channel = new NotificationChannel( - CHANNEL_ID, - "Glasses connection", - NotificationManager.IMPORTANCE_LOW - ); - channel.setDescription("Keeps Faceclaw connected to the glasses."); - // The pinned status notification must not count toward the launcher - // icon's notification badge. - channel.setShowBadge(false); - - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.createNotificationChannel(channel); - manager.deleteNotificationChannel(LEGACY_CHANNEL_ID); - } - } - - private Notification buildNotification(String text) { - Intent launchIntent = new Intent(this, NativeScriptActivity.class); - launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, flags); - - Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - ? new Notification.Builder(this, CHANNEL_ID) - : new Notification.Builder(this); - - return builder - .setContentTitle("Faceclaw") - .setContentText(text) - .setSmallIcon(getApplicationInfo().icon) - .setContentIntent(contentIntent) - .setOngoing(true) - .setOnlyAlertOnce(true) - .build(); - } - - private int foregroundServiceType() { - int type = ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE; - // TODO: Make this depend on which audio path (G2 vs phone) is selected - if (hasRecordAudioPermission()) { - type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE; - } - // The location type keeps while-in-use location flowing to the - // Navigate app when the phone screen locks. Only claimed once the - // permission exists: on API 34+ claiming it without the permission - // makes startForeground throw. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && hasFineLocationPermission()) { - type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION; - } - return type; - } - - private boolean hasRecordAudioPermission() { - return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) - == PackageManager.PERMISSION_GRANTED; - } - - private boolean hasFineLocationPermission() { - return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.kt new file mode 100644 index 00000000..9b50bd3e --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.kt @@ -0,0 +1,141 @@ +package com.faceclaw.app + +import android.Manifest +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Intent +import android.content.pm.PackageManager +import android.content.pm.ServiceInfo +import android.os.Build +import android.os.IBinder + +import androidx.core.content.ContextCompat + +import com.tns.NativeScriptActivity + +class FaceclawForegroundService : Service() { + companion object { + const val ACTION_START = "com.faceclaw.app.action.START" + const val ACTION_UPDATE = "com.faceclaw.app.action.UPDATE" + const val ACTION_STOP = "com.faceclaw.app.action.STOP" + const val EXTRA_TEXT = "text" + + // Channel id was bumped from "faceclaw-dashboard" when the badge setting + // changed: Android freezes a channel's showBadge flag at creation, so the + // old channel (which let Samsung's launcher count the pinned notification + // as a red "1" badge) is deleted on upgrade rather than reused. + private const val LEGACY_CHANNEL_ID = "faceclaw-dashboard" + private const val CHANNEL_ID = "faceclaw-connection" + private const val NOTIFICATION_ID = 4201 + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + val action = if (intent != null) intent.action else ACTION_START + val text = intent?.getStringExtra(EXTRA_TEXT) + + if (ACTION_STOP == action) { + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + return START_NOT_STICKY + } + + ensureNotificationChannel() + val notification = buildNotification( + if (text != null && !text.trim().isEmpty()) text else "Connected to glasses" + ) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(NOTIFICATION_ID, notification, foregroundServiceType()) + } else { + startForeground(NOTIFICATION_ID, notification) + } + + if (ACTION_UPDATE == action) { + val manager = getSystemService(NotificationManager::class.java) + manager?.notify(NOTIFICATION_ID, notification) + } + + return START_STICKY + } + + private fun ensureNotificationChannel() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return + } + + val channel = NotificationChannel( + CHANNEL_ID, + "Glasses connection", + NotificationManager.IMPORTANCE_LOW + ) + channel.description = "Keeps Faceclaw connected to the glasses." + // The pinned status notification must not count toward the launcher + // icon's notification badge. + channel.setShowBadge(false) + + val manager = getSystemService(NotificationManager::class.java) + if (manager != null) { + manager.createNotificationChannel(channel) + manager.deleteNotificationChannel(LEGACY_CHANNEL_ID) + } + } + + private fun buildNotification(text: String): Notification { + val launchIntent = Intent(this, NativeScriptActivity::class.java) + launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) + + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + + val contentIntent = PendingIntent.getActivity(this, 0, launchIntent, flags) + + val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + Notification.Builder(this, CHANNEL_ID) + else + Notification.Builder(this) + + return builder + .setContentTitle("Faceclaw") + .setContentText(text) + .setSmallIcon(applicationInfo.icon) + .setContentIntent(contentIntent) + .setOngoing(true) + .setOnlyAlertOnce(true) + .build() + } + + private fun foregroundServiceType(): Int { + var type = ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE + // TODO: Make this depend on which audio path (G2 vs phone) is selected + if (hasRecordAudioPermission()) { + type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE + } + // The location type keeps while-in-use location flowing to the + // Navigate app when the phone screen locks. Only claimed once the + // permission exists: on API 34+ claiming it without the permission + // makes startForeground throw. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && hasFineLocationPermission()) { + type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + } + return type + } + + private fun hasRecordAudioPermission(): Boolean { + return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == + PackageManager.PERMISSION_GRANTED + } + + private fun hasFineLocationPermission(): Boolean { + return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == + PackageManager.PERMISSION_GRANTED + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.java deleted file mode 100644 index 9f9287a9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.faceclaw.app; - -import java.io.IOException; - -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -/** - * Shared HTTP identity for the Java networking helpers. The version lives in - * app/version.ts, so the TypeScript side pushes the real string in at startup - * (see installNativeUserAgent in app/util/http.ts); the default below is only - * a fallback for requests that somehow beat that call. - */ -public final class FaceclawHttp { - private static volatile String userAgent = "Faceclaw"; - - private FaceclawHttp() {} - - public static void setUserAgent(String value) { - if (value != null && !value.trim().isEmpty()) { - userAgent = value.trim(); - } - } - - public static String getUserAgent() { - return userAgent; - } - - /** Stamps our User-Agent on any request that doesn't already carry one. */ - public static Interceptor userAgentInterceptor() { - return new Interceptor() { - @Override public Response intercept(Chain chain) throws IOException { - Request request = chain.request(); - if (request.header("User-Agent") != null) { - return chain.proceed(request); - } - return chain.proceed(request.newBuilder().header("User-Agent", userAgent).build()); - } - }; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.kt new file mode 100644 index 00000000..9f26efab --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.kt @@ -0,0 +1,38 @@ +package com.faceclaw.app + +import java.io.IOException + +import okhttp3.Interceptor +import okhttp3.Request +import okhttp3.Response + +/** + * Android okhttp face of the shared HTTP identity. The version lives in + * app/version.ts, so the TypeScript side pushes the real string in at startup + * (see installNativeUserAgent in app/util/http.ts); the value itself is held + * by the shared HttpIdentity so every platform's networking reads one string. + */ +class FaceclawHttp private constructor() { + companion object { + @JvmStatic + fun setUserAgent(value: String?) = HttpIdentity.setUserAgent(value) + + @JvmStatic + fun getUserAgent(): String = HttpIdentity.getUserAgent() + + /** Stamps our User-Agent on any request that doesn't already carry one. */ + @JvmStatic + fun userAgentInterceptor(): Interceptor { + return object : Interceptor { + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + val request: Request = chain.request() + if (request.header("User-Agent") != null) { + return chain.proceed(request) + } + return chain.proceed(request.newBuilder().header("User-Agent", HttpIdentity.getUserAgent()).build()) + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.java deleted file mode 100644 index 1ed6b950..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -public class FaceclawLc3Decoder { - private static final String TAG = "FaceclawLc3"; - public static final int SAMPLE_RATE = 16000; - public static final int FRAME_US = 10000; - public static final int FRAME_BYTES = 40; - public static final int FRAMES_PER_PACKET = 5; - public static final int PACKET_BYTES = 205; - public static final int COUNTER_OFFSET = 204; - public static final int SAMPLES_PER_FRAME = 160; - public static final int SAMPLES_PER_PACKET = FRAMES_PER_PACKET * SAMPLES_PER_FRAME; - // Trailer layout (firmware service_audio.c): after the 200 LC3 bytes the - // glasses DSP appends two signed 16-bit LE values computed on the raw - // stereo capture before the mono downmix — a signal-strength ratio and the - // asin-derived direction-of-arrival in degrees — then the packet counter. - public static final int SSR_OFFSET = 200; - public static final int ANGLE_OFFSET = 202; - - static { - System.loadLibrary("faceclaw_lc3"); - } - - private long nativeHandle; - private int lastCounter = -1; - private long realPackets; - private long duplicatePackets; - private long missingPackets; - private long decodeErrors; - private int lastSsr; - private int lastAngleDegrees; - - public FaceclawLc3Decoder() { - nativeHandle = nativeCreate(FRAME_US, SAMPLE_RATE); - if (nativeHandle == 0) { - throw new IllegalStateException("Could not create LC3 decoder"); - } - } - - public synchronized int decodePacket(byte[] packet, short[] pcmOut) { - if (nativeHandle == 0) { - throw new IllegalStateException("LC3 decoder is closed"); - } - if (packet == null || packet.length != PACKET_BYTES) { - decodeErrors++; - Log.w(TAG, "unexpected G2 audio packet length=" + (packet == null ? -1 : packet.length)); - return 0; - } - if (pcmOut == null || pcmOut.length < SAMPLES_PER_PACKET) { - throw new IllegalArgumentException("pcmOut must hold " + SAMPLES_PER_PACKET + " samples"); - } - - int counter = packet[COUNTER_OFFSET] & 0xff; - if (lastCounter >= 0) { - int gap = (counter - lastCounter) & 0xff; - if (gap == 0) { - duplicatePackets++; - return 0; - } - int missing = gap - 1; - if (missing > 0) { - missingPackets += missing; - } - } - - int decoded = nativeDecodePacket(nativeHandle, packet, pcmOut); - if (decoded <= 0) { - decodeErrors++; - return 0; - } - realPackets++; - lastCounter = counter; - lastSsr = (short) ((packet[SSR_OFFSET] & 0xff) | (packet[SSR_OFFSET + 1] << 8)); - lastAngleDegrees = (short) ((packet[ANGLE_OFFSET] & 0xff) | (packet[ANGLE_OFFSET + 1] << 8)); - return decoded; - } - - /** Firmware signal-strength ratio from the most recent decoded packet. */ - public synchronized int getLastSsr() { - return lastSsr; - } - - /** Firmware direction-of-arrival (signed degrees) from the most recent decoded packet. */ - public synchronized int getLastAngleDegrees() { - return lastAngleDegrees; - } - - public synchronized long getRealPackets() { - return realPackets; - } - - public synchronized long getDuplicatePackets() { - return duplicatePackets; - } - - public synchronized long getMissingPackets() { - return missingPackets; - } - - public synchronized long getDecodeErrors() { - return decodeErrors; - } - - public synchronized void close() { - long handle = nativeHandle; - nativeHandle = 0; - if (handle != 0) { - nativeDestroy(handle); - } - } - - private static native long nativeCreate(int frameUs, int sampleRate); - private static native int nativeDecodePacket(long handle, byte[] packet, short[] pcmOut); - private static native void nativeDestroy(long handle); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.kt new file mode 100644 index 00000000..1028f3bf --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.util.Log + +/** + * Android LC3 decoder for G2 microphone packets: the liblc3 JNI codec behind the shared + * [Lc3PacketFramer], which owns the counter/duplicate/trailer logic and statistics. + */ +class FaceclawLc3Decoder { + companion object { + private const val TAG = "FaceclawLc3" + const val SAMPLE_RATE = Lc3PacketFramer.SAMPLE_RATE + const val FRAME_US = Lc3PacketFramer.FRAME_US + const val FRAME_BYTES = Lc3PacketFramer.FRAME_BYTES + const val FRAMES_PER_PACKET = Lc3PacketFramer.FRAMES_PER_PACKET + const val PACKET_BYTES = Lc3PacketFramer.PACKET_BYTES + const val COUNTER_OFFSET = Lc3PacketFramer.COUNTER_OFFSET + const val SAMPLES_PER_FRAME = Lc3PacketFramer.SAMPLES_PER_FRAME + const val SAMPLES_PER_PACKET = Lc3PacketFramer.SAMPLES_PER_PACKET + const val SSR_OFFSET = Lc3PacketFramer.SSR_OFFSET + const val ANGLE_OFFSET = Lc3PacketFramer.ANGLE_OFFSET + + init { + System.loadLibrary("faceclaw_lc3") + } + + @JvmStatic + private external fun nativeCreate(frameUs: Int, sampleRate: Int): Long + @JvmStatic + private external fun nativeDecodePacket(handle: Long, packet: ByteArray, pcmOut: ShortArray): Int + @JvmStatic + private external fun nativeDestroy(handle: Long) + } + + private var nativeHandle: Long = nativeCreate(FRAME_US, SAMPLE_RATE) + private val framer = Lc3PacketFramer(object : Lc3Codec { + override fun decodeFrames(packet: ByteArray, offset: Int, length: Int, pcmOut: ShortArray): Int { + // The JNI entry decodes the five 40-byte frames at the start of the packet. + return nativeDecodePacket(nativeHandle, packet, pcmOut) + } + }) + + init { + if (nativeHandle == 0L) { + throw IllegalStateException("Could not create LC3 decoder") + } + } + + @Synchronized + fun decodePacket(packet: ByteArray?, pcmOut: ShortArray?): Int { + if (nativeHandle == 0L) { + throw IllegalStateException("LC3 decoder is closed") + } + if (packet == null || packet.size != PACKET_BYTES) { + Log.w(TAG, "unexpected G2 audio packet length=" + (packet?.size ?: -1)) + } + return framer.decodePacket(packet, pcmOut) + } + + /** Firmware signal-strength ratio from the most recent decoded packet. */ + @Synchronized + fun getLastSsr(): Int = framer.lastSsr + + /** Firmware direction-of-arrival (signed degrees) from the most recent decoded packet. */ + @Synchronized + fun getLastAngleDegrees(): Int = framer.lastAngleDegrees + + @Synchronized + fun getRealPackets(): Long = framer.realPackets + + @Synchronized + fun getDuplicatePackets(): Long = framer.duplicatePackets + + @Synchronized + fun getMissingPackets(): Long = framer.missingPackets + + @Synchronized + fun getDecodeErrors(): Long = framer.decodeErrors + + @Synchronized + fun close() { + val handle = nativeHandle + nativeHandle = 0 + if (handle != 0L) { + nativeDestroy(handle) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.java deleted file mode 100644 index 17e98834..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * On-phone LLM inference for the voice assistant (llama.cpp over JNI). - * - * All model work (load, generate, free) runs on a single background executor - * thread so calls are naturally serialized; listener callbacks are posted to - * the Looper of the thread that constructed this object (the JS thread), the - * same contract as FaceclawSseRequest. cancel() may be called from any thread - * and interrupts the in-flight generation at the next token boundary. - * - * The model stays loaded between generations (turn iterations reuse the KV - * cache prefix); the TypeScript side calls unload() on an idle timer to give - * the ~3GB back. - */ -public class FaceclawLlamaRunner { - private static final String TAG = "FaceclawLlamaRunner"; - - static { - System.loadLibrary("faceclaw_llama"); - } - - private final Handler callbackHandler; - private final ExecutorService executor = Executors.newSingleThreadExecutor(runnable -> { - Thread thread = new Thread(runnable, "FaceclawLlama"); - thread.setPriority(Thread.NORM_PRIORITY); - return thread; - }); - - // Mutated only on the executor thread; volatile so cancel() can read it. - private volatile long handle; - private String loadedPath; - - public FaceclawLlamaRunner() { - Looper looper = Looper.myLooper(); - callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - } - - public boolean isModelLoaded() { - return handle != 0; - } - - /** - * Generate a completion, loading (or swapping) the model first if needed. - * grammar may be null/empty for unconstrained generation. - */ - public void generate(final String modelPath, final int nCtx, final int nThreads, - final String prompt, final String grammar, final int maxTokens, - final float temperature, final float topP, final int topK, - final FaceclawLlamaListener listener) { - executor.execute(() -> { - try { - if (handle != 0 && !modelPath.equals(loadedPath)) { - nativeFree(handle); - handle = 0; - loadedPath = null; - } - if (handle == 0) { - long loaded = nativeLoadModel(modelPath, nCtx, nThreads); - if (loaded == 0) { - post(() -> listener.onError("Could not load the on-phone model")); - return; - } - handle = loaded; - loadedPath = modelPath; - } - nativeGenerate(handle, prompt, grammar == null ? "" : grammar, - maxTokens, temperature, topP, topK, new FaceclawLlamaListener() { - @Override public void onToken(final String piece) { - post(() -> listener.onToken(piece)); - } - @Override public void onDone(final String stopReason) { - post(() -> listener.onDone(stopReason)); - } - @Override public void onError(final String message) { - post(() -> listener.onError(message)); - } - }); - } catch (Throwable t) { - Log.e(TAG, "generation failed", t); - final String message = String.valueOf(t); - post(() -> listener.onError("On-phone model failed: " + message)); - } - }); - } - - /** Interrupt the current generation (its listener gets onDone("cancelled")). */ - public void cancel() { - long current = handle; - if (current != 0) { - nativeCancel(current); - } - } - - /** Free the model and its KV cache. Safe to call when nothing is loaded. */ - public void unload() { - executor.execute(() -> { - if (handle != 0) { - nativeFree(handle); - handle = 0; - loadedPath = null; - } - }); - } - - private void post(Runnable runnable) { - callbackHandler.post(runnable); - } - - private static native long nativeLoadModel(String path, int nCtx, int nThreads); - private static native void nativeGenerate(long handle, String prompt, String grammar, - int maxTokens, float temperature, float topP, int topK, FaceclawLlamaListener listener); - private static native void nativeCancel(long handle); - private static native void nativeFree(long handle); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.kt new file mode 100644 index 00000000..9cc3fa66 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.kt @@ -0,0 +1,133 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +/** + * On-phone LLM inference for the voice assistant (llama.cpp over JNI). + * + * All model work (load, generate, free) runs on a single background executor + * thread so calls are naturally serialized; listener callbacks are posted to + * the Looper of the thread that constructed this object (the JS thread), the + * same contract as FaceclawSseRequest. cancel() may be called from any thread + * and interrupts the in-flight generation at the next token boundary. + * + * The model stays loaded between generations (turn iterations reuse the KV + * cache prefix); the TypeScript side calls unload() on an idle timer to give + * the ~3GB back. + */ +class FaceclawLlamaRunner { + companion object { + private const val TAG = "FaceclawLlamaRunner" + + init { + System.loadLibrary("faceclaw_llama") + } + + @JvmStatic + private external fun nativeLoadModel(path: String?, nCtx: Int, nThreads: Int): Long + @JvmStatic + private external fun nativeGenerate( + handle: Long, prompt: String?, grammar: String, + maxTokens: Int, temperature: Float, topP: Float, topK: Int, listener: FaceclawLlamaListener + ) + @JvmStatic + private external fun nativeCancel(handle: Long) + @JvmStatic + private external fun nativeFree(handle: Long) + } + + private val callbackHandler: Handler + private val executor: ExecutorService = Executors.newSingleThreadExecutor { runnable -> + val thread = Thread(runnable, "FaceclawLlama") + thread.priority = Thread.NORM_PRIORITY + thread + } + + // Mutated only on the executor thread; volatile so cancel() can read it. + @Volatile + private var handle: Long = 0 + private var loadedPath: String? = null + + init { + val looper = Looper.myLooper() + callbackHandler = Handler(looper ?: Looper.getMainLooper()) + } + + fun isModelLoaded(): Boolean { + return handle != 0L + } + + /** + * Generate a completion, loading (or swapping) the model first if needed. + * grammar may be null/empty for unconstrained generation. + */ + fun generate( + modelPath: String?, nCtx: Int, nThreads: Int, + prompt: String?, grammar: String?, maxTokens: Int, + temperature: Float, topP: Float, topK: Int, + listener: FaceclawLlamaListener + ) { + executor.execute { + try { + if (handle != 0L && modelPath != loadedPath) { + nativeFree(handle) + handle = 0 + loadedPath = null + } + if (handle == 0L) { + val loaded = nativeLoadModel(modelPath, nCtx, nThreads) + if (loaded == 0L) { + post { listener.onError("Could not load the on-phone model") } + return@execute + } + handle = loaded + loadedPath = modelPath + } + nativeGenerate(handle, prompt, grammar ?: "", + maxTokens, temperature, topP, topK, object : FaceclawLlamaListener { + override fun onToken(piece: String?) { + post { listener.onToken(piece) } + } + override fun onDone(stopReason: String?) { + post { listener.onDone(stopReason) } + } + override fun onError(message: String?) { + post { listener.onError(message) } + } + }) + } catch (t: Throwable) { + Log.e(TAG, "generation failed", t) + val message = t.toString() + post { listener.onError("On-phone model failed: $message") } + } + } + } + + /** Interrupt the current generation (its listener gets onDone("cancelled")). */ + fun cancel() { + val current = handle + if (current != 0L) { + nativeCancel(current) + } + } + + /** Free the model and its KV cache. Safe to call when nothing is loaded. */ + fun unload() { + executor.execute { + if (handle != 0L) { + nativeFree(handle) + handle = 0 + loadedPath = null + } + } + } + + private fun post(runnable: Runnable) { + callbackHandler.post(runnable) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.java deleted file mode 100644 index 90e610c9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.faceclaw.app; - -import android.Manifest; -import android.content.Context; -import android.content.pm.PackageManager; -import android.location.Criteria; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; - -import androidx.core.content.ContextCompat; - -import java.util.List; - -/** - * One-shot, foreground-only location lookup for Weather. It prefers a fresh - * coarse network fix, but can fall back to the newest cached provider fix if - * Android cannot produce a new location before the timeout. - */ -public final class FaceclawLocationProvider implements LocationListener { - private static final long FRESH_CACHE_MS = 10L * 60L * 1000L; - private static final long MAX_CACHE_MS = 24L * 60L * 60L * 1000L; - private static final long TIMEOUT_MS = 15L * 1000L; - - private final Context context; - private final LocationManager locationManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final Runnable timeout = this::onTimeout; - - private FaceclawLocationListener listener; - private Location cachedLocation; - private boolean running; - - public FaceclawLocationProvider(Context context) { - this.context = context.getApplicationContext(); - this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE); - } - - public void setListener(FaceclawLocationListener listener) { - this.listener = listener; - } - - public void start() { - mainHandler.post(this::startOnMainThread); - } - - public void cancel() { - mainHandler.post(() -> finish(null, null)); - } - - private void startOnMainThread() { - if (running) { - return; - } - if (locationManager == null) { - deliverError("Android location service is unavailable."); - return; - } - if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) - != PackageManager.PERMISSION_GRANTED - && ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) - != PackageManager.PERMISSION_GRANTED) { - deliverError("Location permission is required."); - return; - } - - cachedLocation = newestCachedLocation(); - if (isRecent(cachedLocation, FRESH_CACHE_MS)) { - deliverLocation(cachedLocation); - return; - } - - String provider = chooseProvider(); - if (provider == null) { - if (isRecent(cachedLocation, MAX_CACHE_MS)) { - deliverLocation(cachedLocation); - } else { - deliverError("Turn on Location on your phone, then retry."); - } - return; - } - - try { - running = true; - locationManager.requestSingleUpdate(provider, this, Looper.getMainLooper()); - mainHandler.postDelayed(timeout, TIMEOUT_MS); - } catch (SecurityException error) { - finish(null, "Location permission is required."); - } catch (Throwable error) { - finish(null, "Unable to request the current location."); - } - } - - private String chooseProvider() { - try { - if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { - return LocationManager.NETWORK_PROVIDER; - } - Criteria criteria = new Criteria(); - criteria.setAccuracy(Criteria.ACCURACY_COARSE); - criteria.setPowerRequirement(Criteria.POWER_LOW); - return locationManager.getBestProvider(criteria, true); - } catch (Throwable ignored) { - return null; - } - } - - private Location newestCachedLocation() { - Location newest = null; - try { - List providers = locationManager.getProviders(true); - for (String provider : providers) { - Location candidate = locationManager.getLastKnownLocation(provider); - if (candidate != null && (newest == null || candidate.getTime() > newest.getTime())) { - newest = candidate; - } - } - } catch (SecurityException ignored) { - // The explicit permission check above owns the user-facing error. - } catch (Throwable ignored) { - // A fresh request may still succeed even if cached providers fail. - } - return newest; - } - - private static boolean isRecent(Location location, long maximumAgeMs) { - return location != null - && location.getTime() > 0L - && System.currentTimeMillis() - location.getTime() <= maximumAgeMs; - } - - private void onTimeout() { - if (isRecent(cachedLocation, MAX_CACHE_MS)) { - finish(cachedLocation, null); - } else { - finish(null, "Couldn't get your current location. Tap to retry."); - } - } - - @Override - public void onLocationChanged(Location location) { - finish(location, null); - } - - @Override - public void onProviderDisabled(String provider) { - // Keep waiting: Android may still deliver a queued fix or the timeout - // can fall back to another provider's cached location. - } - - @Override - public void onProviderEnabled(String provider) { - } - - @Override - @SuppressWarnings("deprecation") - public void onStatusChanged(String provider, int status, Bundle extras) { - } - - private void finish(Location location, String error) { - if (running) { - try { - locationManager.removeUpdates(this); - } catch (Throwable ignored) { - } - } - running = false; - mainHandler.removeCallbacks(timeout); - if (location != null) { - deliverLocation(location); - } else if (error != null) { - deliverError(error); - } - } - - private void deliverLocation(Location location) { - FaceclawLocationListener current = listener; - if (current != null && location != null) { - current.onLocation( - location.getLatitude(), - location.getLongitude(), - location.hasAccuracy() ? location.getAccuracy() : -1f, - location.getTime()); - } - } - - private void deliverError(String message) { - FaceclawLocationListener current = listener; - if (current != null) { - current.onError(message); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.kt new file mode 100644 index 00000000..ce3a7568 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.kt @@ -0,0 +1,188 @@ +package com.faceclaw.app + +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import android.location.Criteria +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper + +import androidx.core.content.ContextCompat + +/** + * One-shot, foreground-only location lookup for Weather. It prefers a fresh + * coarse network fix, but can fall back to the newest cached provider fix if + * Android cannot produce a new location before the timeout. + */ +class FaceclawLocationProvider(context: Context) : LocationListener { + companion object { + private const val FRESH_CACHE_MS = 10L * 60L * 1000L + private const val MAX_CACHE_MS = 24L * 60L * 60L * 1000L + private const val TIMEOUT_MS = 15L * 1000L + + private fun isRecent(location: Location?, maximumAgeMs: Long): Boolean { + return location != null && + location.time > 0L && + System.currentTimeMillis() - location.time <= maximumAgeMs + } + } + + private val context: Context = context.applicationContext + private val locationManager: LocationManager? = + this.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager? + private val mainHandler = Handler(Looper.getMainLooper()) + private val timeout = Runnable { onTimeout() } + + private var listener: FaceclawLocationListener? = null + private var cachedLocation: Location? = null + private var running = false + + fun setListener(listener: FaceclawLocationListener?) { + this.listener = listener + } + + fun start() { + mainHandler.post { startOnMainThread() } + } + + fun cancel() { + mainHandler.post { finish(null, null) } + } + + private fun startOnMainThread() { + if (running) { + return + } + val manager = locationManager + if (manager == null) { + deliverError("Android location service is unavailable.") + return + } + if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != + PackageManager.PERMISSION_GRANTED && + ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != + PackageManager.PERMISSION_GRANTED) { + deliverError("Location permission is required.") + return + } + + cachedLocation = newestCachedLocation() + if (isRecent(cachedLocation, FRESH_CACHE_MS)) { + deliverLocation(cachedLocation) + return + } + + val provider = chooseProvider() + if (provider == null) { + if (isRecent(cachedLocation, MAX_CACHE_MS)) { + deliverLocation(cachedLocation) + } else { + deliverError("Turn on Location on your phone, then retry.") + } + return + } + + try { + running = true + @Suppress("DEPRECATION") + manager.requestSingleUpdate(provider, this, Looper.getMainLooper()) + mainHandler.postDelayed(timeout, TIMEOUT_MS) + } catch (error: SecurityException) { + finish(null, "Location permission is required.") + } catch (error: Throwable) { + finish(null, "Unable to request the current location.") + } + } + + private fun chooseProvider(): String? { + try { + if (locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { + return LocationManager.NETWORK_PROVIDER + } + val criteria = Criteria() + criteria.accuracy = Criteria.ACCURACY_COARSE + criteria.powerRequirement = Criteria.POWER_LOW + return locationManager.getBestProvider(criteria, true) + } catch (ignored: Throwable) { + return null + } + } + + private fun newestCachedLocation(): Location? { + var newest: Location? = null + try { + val providers = locationManager!!.getProviders(true) + for (provider in providers) { + val candidate = locationManager.getLastKnownLocation(provider) + if (candidate != null && (newest == null || candidate.time > newest.time)) { + newest = candidate + } + } + } catch (ignored: SecurityException) { + // The explicit permission check above owns the user-facing error. + } catch (ignored: Throwable) { + // A fresh request may still succeed even if cached providers fail. + } + return newest + } + + private fun onTimeout() { + if (isRecent(cachedLocation, MAX_CACHE_MS)) { + finish(cachedLocation, null) + } else { + finish(null, "Couldn't get your current location. Tap to retry.") + } + } + + override fun onLocationChanged(location: Location) { + finish(location, null) + } + + override fun onProviderDisabled(provider: String) { + // Keep waiting: Android may still deliver a queued fix or the timeout + // can fall back to another provider's cached location. + } + + override fun onProviderEnabled(provider: String) { + } + + @Deprecated("Deprecated in Java") + override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { + } + + private fun finish(location: Location?, error: String?) { + if (running) { + try { + locationManager!!.removeUpdates(this) + } catch (ignored: Throwable) { + } + } + running = false + mainHandler.removeCallbacks(timeout) + if (location != null) { + deliverLocation(location) + } else if (error != null) { + deliverError(error) + } + } + + private fun deliverLocation(location: Location?) { + val current = listener + if (current != null && location != null) { + current.onLocation( + location.latitude, + location.longitude, + if (location.hasAccuracy()) location.accuracy else -1f, + location.time) + } + } + + private fun deliverError(message: String) { + val current = listener + current?.onError(message) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.java deleted file mode 100644 index c39f3fa6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.java +++ /dev/null @@ -1,188 +0,0 @@ -package com.faceclaw.app; - -import android.Manifest; -import android.content.Context; -import android.content.pm.PackageManager; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; - -import androidx.core.content.ContextCompat; - -/** - * Continuous location updates for turn-by-turn navigation. Unlike - * FaceclawLocationProvider (one-shot, coarse, for Weather), this streams GPS - * fixes with bearing and speed until stopped. - * - * Callbacks are delivered on the Looper of the thread that constructed the - * tracker, so a worker isolate receives them on its own thread (the same - * convention as FaceclawSseRequest). - */ -public final class FaceclawLocationTracker implements LocationListener { - private final Context context; - private final LocationManager locationManager; - private final Handler callbackHandler; - - /** - * Only seed from the platform's cached fix when it is this fresh. An older - * one is likely wherever the phone was last used (for example the previous - * navigation's destination), and guidance built on it is wrong from the - * first frame: the route starts there and arrival fires immediately. - */ - private static final long MAX_SEED_AGE_MS = 60L * 1000L; - - private FaceclawLocationTrackerListener listener; - private boolean running; - - public FaceclawLocationTracker(Context context) { - this.context = context.getApplicationContext(); - this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE); - Looper looper = Looper.myLooper(); - this.callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - } - - public void setListener(FaceclawLocationTrackerListener listener) { - this.listener = listener; - } - - /** Begin streaming fixes at roughly the requested interval. */ - public void start(long intervalMs) { - callbackHandler.post(() -> startOnCallbackThread(intervalMs)); - } - - public void stop() { - callbackHandler.post(() -> { - if (!running) { - return; - } - running = false; - try { - locationManager.removeUpdates(this); - } catch (Throwable ignored) { - } - }); - } - - private void startOnCallbackThread(long intervalMs) { - if (running) { - return; - } - if (locationManager == null) { - deliverError("Android location service is unavailable."); - return; - } - if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) - != PackageManager.PERMISSION_GRANTED) { - deliverError("Precise location permission is required for navigation."); - return; - } - - String provider = chooseProvider(); - if (provider == null) { - deliverError("Turn on Location on your phone, then retry."); - return; - } - - try { - running = true; - locationManager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this, - callbackHandler.getLooper()); - // Seed with the freshest cached fix so the UI has a position - // before the first live fix (GPS cold starts can take a while), - // but only if it's recent enough to still be where the user is. - Location cached = freshestCachedLocation(provider); - if (cached != null) { - deliverLocation(cached); - } - } catch (SecurityException error) { - running = false; - deliverError("Precise location permission is required for navigation."); - } catch (Throwable error) { - running = false; - deliverError("Unable to start location updates."); - } - } - - private Location freshestCachedLocation(String primaryProvider) { - Location best = null; - for (String provider : new String[] {primaryProvider, LocationManager.NETWORK_PROVIDER, - LocationManager.GPS_PROVIDER}) { - Location candidate; - try { - candidate = locationManager.getLastKnownLocation(provider); - } catch (Throwable ignored) { - continue; - } - if (candidate == null || ageMs(candidate) > MAX_SEED_AGE_MS) { - continue; - } - if (best == null || candidate.getElapsedRealtimeNanos() > best.getElapsedRealtimeNanos()) { - best = candidate; - } - } - return best; - } - - /** Age via the monotonic clock, so a wall-clock change can't make an old fix look fresh. */ - private static long ageMs(Location location) { - long ageNanos = SystemClock.elapsedRealtimeNanos() - location.getElapsedRealtimeNanos(); - return ageNanos / 1_000_000L; - } - - private String chooseProvider() { - try { - if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { - return LocationManager.GPS_PROVIDER; - } - if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { - return LocationManager.NETWORK_PROVIDER; - } - } catch (Throwable ignored) { - } - return null; - } - - @Override - public void onLocationChanged(Location location) { - deliverLocation(location); - } - - @Override - public void onProviderDisabled(String provider) { - deliverError("Location was turned off on the phone."); - } - - @Override - public void onProviderEnabled(String provider) { - } - - @Override - @SuppressWarnings("deprecation") - public void onStatusChanged(String provider, int status, Bundle extras) { - } - - private void deliverLocation(Location location) { - FaceclawLocationTrackerListener current = listener; - if (current == null || location == null) { - return; - } - current.onLocation( - location.getLatitude(), - location.getLongitude(), - location.hasAccuracy() ? location.getAccuracy() : -1f, - location.hasBearing() ? location.getBearing() : -1f, - location.hasSpeed() ? location.getSpeed() : -1f, - location.getTime()); - } - - private void deliverError(String message) { - FaceclawLocationTrackerListener current = listener; - if (current != null) { - current.onError(message); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.kt new file mode 100644 index 00000000..9eb4e255 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.kt @@ -0,0 +1,184 @@ +package com.faceclaw.app + +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.os.SystemClock + +import androidx.core.content.ContextCompat + +/** + * Continuous location updates for turn-by-turn navigation. Unlike + * FaceclawLocationProvider (one-shot, coarse, for Weather), this streams GPS + * fixes with bearing and speed until stopped. + * + * Callbacks are delivered on the Looper of the thread that constructed the + * tracker, so a worker isolate receives them on its own thread (the same + * convention as FaceclawSseRequest). + */ +class FaceclawLocationTracker(context: Context) : LocationListener { + companion object { + /** + * Only seed from the platform's cached fix when it is this fresh. An older + * one is likely wherever the phone was last used (for example the previous + * navigation's destination), and guidance built on it is wrong from the + * first frame: the route starts there and arrival fires immediately. + */ + private const val MAX_SEED_AGE_MS = 60L * 1000L + + /** Age via the monotonic clock, so a wall-clock change can't make an old fix look fresh. */ + private fun ageMs(location: Location): Long { + val ageNanos = SystemClock.elapsedRealtimeNanos() - location.elapsedRealtimeNanos + return ageNanos / 1_000_000L + } + } + + private val context: Context = context.applicationContext + private val locationManager: LocationManager? = + this.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager? + private val callbackHandler: Handler + + private var listener: FaceclawLocationTrackerListener? = null + private var running = false + + init { + val looper = Looper.myLooper() + this.callbackHandler = Handler(looper ?: Looper.getMainLooper()) + } + + fun setListener(listener: FaceclawLocationTrackerListener?) { + this.listener = listener + } + + /** Begin streaming fixes at roughly the requested interval. */ + fun start(intervalMs: Long) { + callbackHandler.post { startOnCallbackThread(intervalMs) } + } + + fun stop() { + callbackHandler.post { + if (!running) { + return@post + } + running = false + try { + locationManager!!.removeUpdates(this) + } catch (ignored: Throwable) { + } + } + } + + private fun startOnCallbackThread(intervalMs: Long) { + if (running) { + return + } + val manager = locationManager + if (manager == null) { + deliverError("Android location service is unavailable.") + return + } + if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != + PackageManager.PERMISSION_GRANTED) { + deliverError("Precise location permission is required for navigation.") + return + } + + val provider = chooseProvider() + if (provider == null) { + deliverError("Turn on Location on your phone, then retry.") + return + } + + try { + running = true + manager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this, + callbackHandler.looper) + // Seed with the freshest cached fix so the UI has a position + // before the first live fix (GPS cold starts can take a while), + // but only if it's recent enough to still be where the user is. + val cached = freshestCachedLocation(provider) + if (cached != null) { + deliverLocation(cached) + } + } catch (error: SecurityException) { + running = false + deliverError("Precise location permission is required for navigation.") + } catch (error: Throwable) { + running = false + deliverError("Unable to start location updates.") + } + } + + private fun freshestCachedLocation(primaryProvider: String): Location? { + var best: Location? = null + for (provider in arrayOf(primaryProvider, LocationManager.NETWORK_PROVIDER, + LocationManager.GPS_PROVIDER)) { + val candidate: Location? + try { + candidate = locationManager!!.getLastKnownLocation(provider) + } catch (ignored: Throwable) { + continue + } + if (candidate == null || ageMs(candidate) > MAX_SEED_AGE_MS) { + continue + } + if (best == null || candidate.elapsedRealtimeNanos > best.elapsedRealtimeNanos) { + best = candidate + } + } + return best + } + + private fun chooseProvider(): String? { + try { + if (locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + return LocationManager.GPS_PROVIDER + } + if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { + return LocationManager.NETWORK_PROVIDER + } + } catch (ignored: Throwable) { + } + return null + } + + override fun onLocationChanged(location: Location) { + deliverLocation(location) + } + + override fun onProviderDisabled(provider: String) { + deliverError("Location was turned off on the phone.") + } + + override fun onProviderEnabled(provider: String) { + } + + @Deprecated("Deprecated in Java") + override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { + } + + private fun deliverLocation(location: Location?) { + val current = listener + if (current == null || location == null) { + return + } + current.onLocation( + location.latitude, + location.longitude, + if (location.hasAccuracy()) location.accuracy else -1f, + if (location.hasBearing()) location.bearing else -1f, + if (location.hasSpeed()) location.speed else -1f, + location.time) + } + + private fun deliverError(message: String) { + val current = listener + current?.onError(message) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.java deleted file mode 100644 index 07bef961..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.java +++ /dev/null @@ -1,321 +0,0 @@ -package com.faceclaw.app; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.media.browse.MediaBrowser; -import android.media.session.MediaController; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Client for other apps' MediaBrowserService implementations (the mechanism - * Android Auto uses). Binding starts the target player's process even when it - * is not running, so this can list a player's library (playlists, albums, ...) - * and start playback without the phone being unlocked. - * - * Holds at most one connection at a time. All MediaBrowser interaction happens - * on the main thread; results are reported through - * FaceclawMediaBrowserListener, also on the main thread. - */ -public class FaceclawMediaBrowser { - private static final String TAG = "FaceclawMediaBrowser"; - private static final String BROWSER_SERVICE_ACTION = "android.media.browse.MediaBrowserService"; - // Generous: connecting may cold-start the player's process. - private static final long CONNECT_TIMEOUT_MS = 15_000; - private static final long BROWSE_TIMEOUT_MS = 15_000; - // Unbinding immediately after a play command can race the player promoting - // itself to a foreground service, letting the system kill its process - // before playback starts; keep the binding alive briefly instead. - private static final long PLAY_DISCONNECT_GRACE_MS = 5_000; - - private final Context appContext; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private volatile FaceclawMediaBrowserListener listener; - - // Main-thread only. - private MediaBrowser browser; - private int generation; - private long lastPlayCommandAtMs; - - public FaceclawMediaBrowser(Context context) { - this.appContext = context.getApplicationContext(); - } - - public void setListener(FaceclawMediaBrowserListener listener) { - this.listener = listener; - } - - /** - * Installed apps exposing a media browser service, as JSON sorted by app - * name: [{"packageName": ..., "serviceClass": ..., "appName": ...}, ...]. - * Synchronous (PackageManager lookup only). - */ - public String listBrowsableAppsJson() { - PackageManager packageManager = appContext.getPackageManager(); - List services; - try { - services = packageManager.queryIntentServices(new Intent(BROWSER_SERVICE_ACTION), 0); - } catch (Exception e) { - Log.w(TAG, "media browser service query failed", e); - return "[]"; - } - List apps = new ArrayList<>(); - for (ResolveInfo service : services) { - if (service.serviceInfo == null) { - continue; - } - try { - CharSequence label = service.serviceInfo.applicationInfo.loadLabel(packageManager); - JSONObject app = new JSONObject(); - app.put("packageName", service.serviceInfo.packageName); - app.put("serviceClass", service.serviceInfo.name); - app.put("appName", label == null ? service.serviceInfo.packageName : label.toString()); - apps.add(app); - } catch (Exception e) { - Log.w(TAG, "media browser service entry failed", e); - } - } - Collections.sort(apps, (a, b) -> a.optString("appName").compareToIgnoreCase(b.optString("appName"))); - return new JSONArray(apps).toString(); - } - - /** - * Connect to an app's media browser service, starting its process if - * needed; replaces any existing connection. Async; reports through - * onConnectResult with the browse-tree root id on success. - */ - public void connect(int requestId, String packageName, String serviceClass) { - mainHandler.post(() -> { - closeBrowser(); - final int myGeneration = generation; - final boolean[] settled = {false}; - final MediaBrowser[] browserRef = new MediaBrowser[1]; - MediaBrowser newBrowser = new MediaBrowser( - appContext, - new ComponentName(packageName, serviceClass), - new MediaBrowser.ConnectionCallback() { - @Override - public void onConnected() { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - String rootId = ""; - try { - rootId = browserRef[0].getRoot(); - } catch (Exception e) { - Log.w(TAG, "getRoot failed", e); - } - emitConnectResult(requestId, true, rootId, ""); - } - - @Override - public void onConnectionFailed() { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - closeBrowser(); - emitConnectResult(requestId, false, "", "Connection refused by " + packageName); - } - - @Override - public void onConnectionSuspended() { - if (generation != myGeneration) { - return; - } - FaceclawMediaBrowserListener currentListener = listener; - if (currentListener != null) { - currentListener.onDisconnected(); - } - } - }, - null - ); - browserRef[0] = newBrowser; - browser = newBrowser; - mainHandler.postDelayed(() -> { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - closeBrowser(); - emitConnectResult(requestId, false, "", "Connection to " + packageName + " timed out"); - }, CONNECT_TIMEOUT_MS); - try { - newBrowser.connect(); - } catch (Exception e) { - if (!settled[0]) { - settled[0] = true; - closeBrowser(); - emitConnectResult(requestId, false, "", "Connection failed: " + e.getMessage()); - } - } - }); - } - - /** - * Fetch one level of the connected service's content tree. Async; reports - * through onBrowseResult with children as JSON: - * [{"mediaId": ..., "title": ..., "subtitle": ..., "browsable": bool, "playable": bool}, ...]. - */ - public void browse(int requestId, String parentId) { - mainHandler.post(() -> { - final MediaBrowser currentBrowser = browser; - if (currentBrowser == null || !currentBrowser.isConnected()) { - emitBrowseResult(requestId, "", "Not connected"); - return; - } - final int myGeneration = generation; - final boolean[] settled = {false}; - mainHandler.postDelayed(() -> { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - try { - currentBrowser.unsubscribe(parentId); - } catch (Exception ignored) { - } - emitBrowseResult(requestId, "", "Browse timed out"); - }, BROWSE_TIMEOUT_MS); - try { - currentBrowser.subscribe(parentId, new MediaBrowser.SubscriptionCallback() { - @Override - public void onChildrenLoaded(String loadedParentId, List children) { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - // One snapshot is all we want; unsubscribe outside the callback. - mainHandler.post(() -> { - try { - currentBrowser.unsubscribe(loadedParentId); - } catch (Exception ignored) { - } - }); - emitBrowseResult(requestId, serializeChildren(children), ""); - } - - @Override - public void onError(String erroredParentId) { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - mainHandler.post(() -> { - try { - currentBrowser.unsubscribe(erroredParentId); - } catch (Exception ignored) { - } - }); - emitBrowseResult(requestId, "", "Browse failed"); - } - }); - } catch (Exception e) { - if (!settled[0]) { - settled[0] = true; - emitBrowseResult(requestId, "", "Browse failed: " + e.getMessage()); - } - } - }); - } - - /** Start playback of a browsed item through the connected service's session. */ - public void playFromMediaId(String mediaId) { - mainHandler.post(() -> { - if (browser == null || !browser.isConnected()) { - return; - } - try { - MediaController controller = new MediaController(appContext, browser.getSessionToken()); - controller.getTransportControls().playFromMediaId(mediaId, null); - lastPlayCommandAtMs = SystemClock.elapsedRealtime(); - } catch (Exception e) { - Log.w(TAG, "playFromMediaId failed", e); - } - }); - } - - public void disconnect() { - mainHandler.post(() -> { - long sincePlayMs = SystemClock.elapsedRealtime() - lastPlayCommandAtMs; - long graceMs = lastPlayCommandAtMs == 0 ? 0 : PLAY_DISCONNECT_GRACE_MS - sincePlayMs; - if (graceMs <= 0) { - closeBrowser(); - return; - } - final MediaBrowser lingering = browser; - browser = null; - generation++; - if (lingering != null) { - mainHandler.postDelayed(() -> { - try { - lingering.disconnect(); - } catch (Exception ignored) { - } - }, graceMs); - } - }); - } - - private void closeBrowser() { - generation++; - if (browser != null) { - try { - browser.disconnect(); - } catch (Exception ignored) { - } - browser = null; - } - } - - private String serializeChildren(List children) { - JSONArray out = new JSONArray(); - if (children == null) { - return out.toString(); - } - for (MediaBrowser.MediaItem item : children) { - try { - CharSequence title = item.getDescription() == null ? null : item.getDescription().getTitle(); - CharSequence subtitle = item.getDescription() == null ? null : item.getDescription().getSubtitle(); - JSONObject entry = new JSONObject(); - entry.put("mediaId", item.getMediaId() == null ? "" : item.getMediaId()); - entry.put("title", title == null ? "" : title.toString()); - entry.put("subtitle", subtitle == null ? "" : subtitle.toString()); - entry.put("browsable", item.isBrowsable()); - entry.put("playable", item.isPlayable()); - out.put(entry); - } catch (Exception e) { - Log.w(TAG, "media item serialization failed", e); - } - } - return out.toString(); - } - - private void emitConnectResult(int requestId, boolean connected, String rootId, String error) { - FaceclawMediaBrowserListener currentListener = listener; - if (currentListener != null) { - currentListener.onConnectResult(requestId, connected, rootId, error); - } - } - - private void emitBrowseResult(int requestId, String childrenJson, String error) { - FaceclawMediaBrowserListener currentListener = listener; - if (currentListener != null) { - currentListener.onBrowseResult(requestId, childrenJson, error); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.kt new file mode 100644 index 00000000..fb716c84 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.kt @@ -0,0 +1,316 @@ +package com.faceclaw.app + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.content.pm.ResolveInfo +import android.media.browse.MediaBrowser +import android.media.session.MediaController +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.util.Log + +import org.json.JSONArray +import org.json.JSONObject + +import java.util.ArrayList +import java.util.Collections + +/** + * Client for other apps' MediaBrowserService implementations (the mechanism + * Android Auto uses). Binding starts the target player's process even when it + * is not running, so this can list a player's library (playlists, albums, ...) + * and start playback without the phone being unlocked. + * + * Holds at most one connection at a time. All MediaBrowser interaction happens + * on the main thread; results are reported through + * FaceclawMediaBrowserListener, also on the main thread. + */ +class FaceclawMediaBrowser(context: Context) { + companion object { + private const val TAG = "FaceclawMediaBrowser" + private const val BROWSER_SERVICE_ACTION = "android.media.browse.MediaBrowserService" + // Generous: connecting may cold-start the player's process. + private const val CONNECT_TIMEOUT_MS = 15_000L + private const val BROWSE_TIMEOUT_MS = 15_000L + // Unbinding immediately after a play command can race the player promoting + // itself to a foreground service, letting the system kill its process + // before playback starts; keep the binding alive briefly instead. + private const val PLAY_DISCONNECT_GRACE_MS = 5_000L + } + + private val appContext: Context = context.applicationContext + private val mainHandler = Handler(Looper.getMainLooper()) + @Volatile + private var listener: FaceclawMediaBrowserListener? = null + + // Main-thread only. + private var browser: MediaBrowser? = null + private var generation = 0 + private var lastPlayCommandAtMs = 0L + + fun setListener(listener: FaceclawMediaBrowserListener?) { + this.listener = listener + } + + /** + * Installed apps exposing a media browser service, as JSON sorted by app + * name: [{"packageName": ..., "serviceClass": ..., "appName": ...}, ...]. + * Synchronous (PackageManager lookup only). + */ + fun listBrowsableAppsJson(): String { + val packageManager = appContext.packageManager + val services: List + try { + services = packageManager.queryIntentServices(Intent(BROWSER_SERVICE_ACTION), 0) + } catch (e: Exception) { + Log.w(TAG, "media browser service query failed", e) + return "[]" + } + val apps: MutableList = ArrayList() + for (service in services) { + if (service.serviceInfo == null) { + continue + } + try { + val label: CharSequence? = service.serviceInfo.applicationInfo.loadLabel(packageManager) + val app = JSONObject() + app.put("packageName", service.serviceInfo.packageName) + app.put("serviceClass", service.serviceInfo.name) + app.put("appName", if (label == null) service.serviceInfo.packageName else label.toString()) + apps.add(app) + } catch (e: Exception) { + Log.w(TAG, "media browser service entry failed", e) + } + } + Collections.sort(apps) { a, b -> a.optString("appName").compareTo(b.optString("appName"), ignoreCase = true) } + return JSONArray(apps).toString() + } + + /** + * Connect to an app's media browser service, starting its process if + * needed; replaces any existing connection. Async; reports through + * onConnectResult with the browse-tree root id on success. + */ + fun connect(requestId: Int, packageName: String?, serviceClass: String?) { + mainHandler.post { + closeBrowser() + val myGeneration = generation + val settled = booleanArrayOf(false) + val browserRef = arrayOfNulls(1) + val newBrowser = MediaBrowser( + appContext, + ComponentName(packageName!!, serviceClass!!), + object : MediaBrowser.ConnectionCallback() { + override fun onConnected() { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + var rootId = "" + try { + rootId = browserRef[0]!!.root + } catch (e: Exception) { + Log.w(TAG, "getRoot failed", e) + } + emitConnectResult(requestId, true, rootId, "") + } + + override fun onConnectionFailed() { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + closeBrowser() + emitConnectResult(requestId, false, "", "Connection refused by $packageName") + } + + override fun onConnectionSuspended() { + if (generation != myGeneration) { + return + } + val currentListener = listener + if (currentListener != null) { + currentListener.onDisconnected() + } + } + }, + null + ) + browserRef[0] = newBrowser + browser = newBrowser + mainHandler.postDelayed({ + if (generation != myGeneration || settled[0]) { + return@postDelayed + } + settled[0] = true + closeBrowser() + emitConnectResult(requestId, false, "", "Connection to $packageName timed out") + }, CONNECT_TIMEOUT_MS) + try { + newBrowser.connect() + } catch (e: Exception) { + if (!settled[0]) { + settled[0] = true + closeBrowser() + emitConnectResult(requestId, false, "", "Connection failed: " + e.message) + } + } + } + } + + /** + * Fetch one level of the connected service's content tree. Async; reports + * through onBrowseResult with children as JSON: + * [{"mediaId": ..., "title": ..., "subtitle": ..., "browsable": bool, "playable": bool}, ...]. + */ + fun browse(requestId: Int, parentId: String?) { + mainHandler.post { + val currentBrowser = browser + if (currentBrowser == null || !currentBrowser.isConnected) { + emitBrowseResult(requestId, "", "Not connected") + return@post + } + val myGeneration = generation + val settled = booleanArrayOf(false) + mainHandler.postDelayed({ + if (generation != myGeneration || settled[0]) { + return@postDelayed + } + settled[0] = true + try { + currentBrowser.unsubscribe(parentId!!) + } catch (ignored: Exception) { + } + emitBrowseResult(requestId, "", "Browse timed out") + }, BROWSE_TIMEOUT_MS) + try { + currentBrowser.subscribe(parentId!!, object : MediaBrowser.SubscriptionCallback() { + override fun onChildrenLoaded(loadedParentId: String, children: List) { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + // One snapshot is all we want; unsubscribe outside the callback. + mainHandler.post { + try { + currentBrowser.unsubscribe(loadedParentId) + } catch (ignored: Exception) { + } + } + emitBrowseResult(requestId, serializeChildren(children), "") + } + + override fun onError(erroredParentId: String) { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + mainHandler.post { + try { + currentBrowser.unsubscribe(erroredParentId) + } catch (ignored: Exception) { + } + } + emitBrowseResult(requestId, "", "Browse failed") + } + }) + } catch (e: Exception) { + if (!settled[0]) { + settled[0] = true + emitBrowseResult(requestId, "", "Browse failed: " + e.message) + } + } + } + } + + /** Start playback of a browsed item through the connected service's session. */ + fun playFromMediaId(mediaId: String?) { + mainHandler.post { + val currentBrowser = browser + if (currentBrowser == null || !currentBrowser.isConnected) { + return@post + } + try { + val controller = MediaController(appContext, currentBrowser.sessionToken) + controller.transportControls.playFromMediaId(mediaId, null) + lastPlayCommandAtMs = SystemClock.elapsedRealtime() + } catch (e: Exception) { + Log.w(TAG, "playFromMediaId failed", e) + } + } + } + + fun disconnect() { + mainHandler.post { + val sincePlayMs = SystemClock.elapsedRealtime() - lastPlayCommandAtMs + val graceMs = if (lastPlayCommandAtMs == 0L) 0 else PLAY_DISCONNECT_GRACE_MS - sincePlayMs + if (graceMs <= 0) { + closeBrowser() + return@post + } + val lingering = browser + browser = null + generation++ + if (lingering != null) { + mainHandler.postDelayed({ + try { + lingering.disconnect() + } catch (ignored: Exception) { + } + }, graceMs) + } + } + } + + private fun closeBrowser() { + generation++ + val currentBrowser = browser + if (currentBrowser != null) { + try { + currentBrowser.disconnect() + } catch (ignored: Exception) { + } + browser = null + } + } + + private fun serializeChildren(children: List?): String { + val out = JSONArray() + if (children == null) { + return out.toString() + } + for (item in children) { + try { + val title: CharSequence? = if (item.description == null) null else item.description.title + val subtitle: CharSequence? = if (item.description == null) null else item.description.subtitle + val entry = JSONObject() + entry.put("mediaId", if (item.mediaId == null) "" else item.mediaId) + entry.put("title", if (title == null) "" else title.toString()) + entry.put("subtitle", if (subtitle == null) "" else subtitle.toString()) + entry.put("browsable", item.isBrowsable) + entry.put("playable", item.isPlayable) + out.put(entry) + } catch (e: Exception) { + Log.w(TAG, "media item serialization failed", e) + } + } + return out.toString() + } + + private fun emitConnectResult(requestId: Int, connected: Boolean, rootId: String?, error: String?) { + val currentListener = listener + if (currentListener != null) { + currentListener.onConnectResult(requestId, connected, rootId, error) + } + } + + private fun emitBrowseResult(requestId: Int, childrenJson: String?, error: String?) { + val currentListener = listener + if (currentListener != null) { + currentListener.onBrowseResult(requestId, childrenJson, error) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java deleted file mode 100644 index c8775f40..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java +++ /dev/null @@ -1,576 +0,0 @@ -package com.faceclaw.app; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.database.ContentObserver; -import android.graphics.Bitmap; -import android.media.AudioManager; -import android.media.MediaDescription; -import android.media.MediaMetadata; -import android.media.session.MediaController; -import android.media.session.MediaSession; -import android.media.session.MediaSessionManager; -import android.media.session.PlaybackState; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.provider.Settings; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.util.List; -import java.util.HashSet; -import java.util.Set; - -public class FaceclawMediaController { - private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners"; - - private final Context appContext; - private final Object lock = new Object(); - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final ComponentName listenerComponent; - private final MediaSessionManager sessionManager; - private final AudioManager audioManager; - - private final MediaController.Callback controllerCallback = new MediaController.Callback() { - @Override - public void onPlaybackStateChanged(PlaybackState state) { - synchronized (lock) { - emitStateLocked(); - } - } - - @Override - public void onMetadataChanged(MediaMetadata metadata) { - synchronized (lock) { - emitStateLocked(); - } - } - - @Override - public void onQueueChanged(List queue) { - synchronized (lock) { - emitStateLocked(); - } - } - - @Override - public void onSessionDestroyed() { - synchronized (lock) { - refreshActiveControllerLocked(null); - } - } - }; - - private final MediaSessionManager.OnActiveSessionsChangedListener sessionsChangedListener = controllers -> { - synchronized (lock) { - refreshActiveControllerLocked(controllers); - } - }; - - /** - * Fires when the user grants or revokes notification-listener access in - * system settings. Session access depends on that grant, so the sessions - * listener is (re)registered and the state re-emitted whenever it changes; - * otherwise a grant made after start() would never be noticed. - */ - private final ContentObserver notificationAccessObserver = new ContentObserver(mainHandler) { - @Override - public void onChange(boolean selfChange) { - synchronized (lock) { - if (!started) return; - syncSessionsListenerLocked(); - refreshActiveControllerLocked(null); - } - } - }; - - private volatile FaceclawMediaControllerListener listener; - private MediaController activeController; - private boolean started; - private boolean sessionsListenerRegistered; - private Set ignoredPackages = new HashSet<>(); - - public FaceclawMediaController(Context context) { - this.appContext = context.getApplicationContext(); - this.listenerComponent = new ComponentName(appContext, FaceclawMediaNotificationListenerService.class); - this.sessionManager = (MediaSessionManager) appContext.getSystemService(Context.MEDIA_SESSION_SERVICE); - this.audioManager = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE); - } - - public void setListener(FaceclawMediaControllerListener listener) { - synchronized (lock) { - this.listener = listener; - emitStateLocked(); - } - } - - public void start() { - synchronized (lock) { - if (started) { - emitStateLocked(); - return; - } - started = true; - try { - appContext.getContentResolver().registerContentObserver( - Settings.Secure.getUriFor(ENABLED_NOTIFICATION_LISTENERS), - false, - notificationAccessObserver - ); - } catch (Exception e) { - Log.w("FaceclawMedia", "notification access observer registration failed", e); - } - syncSessionsListenerLocked(); - refreshActiveControllerLocked(null); - } - } - - /** - * Keep the active-sessions listener registered exactly while notification - * access is granted. Registration throws SecurityException without the - * grant, so this is retried from the settings observer rather than only - * attempted once at start(). - */ - private void syncSessionsListenerLocked() { - if (sessionManager == null) return; - boolean accessEnabled = started && isNotificationAccessEnabled(); - if (accessEnabled && !sessionsListenerRegistered) { - try { - sessionManager.addOnActiveSessionsChangedListener( - sessionsChangedListener, - listenerComponent, - mainHandler - ); - sessionsListenerRegistered = true; - } catch (SecurityException ignored) { - } - } else if (!accessEnabled && sessionsListenerRegistered) { - try { - sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener); - } catch (SecurityException ignored) { - } - sessionsListenerRegistered = false; - } - } - - /** Apply the complete preference set and immediately choose an allowed session. */ - public void setIgnoredPackagesJson(String packagesJson) { - Set packages = new HashSet<>(); - try { - JSONArray array = new JSONArray(packagesJson); - for (int i = 0; i < array.length(); i++) packages.add(array.getString(i)); - } catch (Exception e) { - Log.w("FaceclawMedia", "invalid ignored packages", e); - return; - } - synchronized (lock) { - if (ignoredPackages.equals(packages)) return; - ignoredPackages = packages; - refreshActiveControllerLocked(null); - } - } - - public void stop() { - synchronized (lock) { - if (!started) { - return; - } - started = false; - try { - appContext.getContentResolver().unregisterContentObserver(notificationAccessObserver); - } catch (Exception ignored) { - } - syncSessionsListenerLocked(); - setActiveControllerLocked(null); - emitStateLocked(); - } - } - - public void playPause() { - synchronized (lock) { - if (activeController == null) { - return; - } - PlaybackState playbackState = activeController.getPlaybackState(); - MediaController.TransportControls controls = activeController.getTransportControls(); - if (playbackState == null) { - controls.play(); - return; - } - switch (playbackState.getState()) { - case PlaybackState.STATE_PLAYING: - case PlaybackState.STATE_BUFFERING: - case PlaybackState.STATE_CONNECTING: - controls.pause(); - return; - default: - controls.play(); - } - } - } - - public void skipNext() { - synchronized (lock) { - if (activeController != null) { - activeController.getTransportControls().skipToNext(); - } - } - } - - public void skipPrevious() { - synchronized (lock) { - if (activeController != null) { - activeController.getTransportControls().skipToPrevious(); - } - } - } - - public void skipToQueueItem(long queueId) { - synchronized (lock) { - if (activeController != null) { - activeController.getTransportControls().skipToQueueItem(queueId); - } - } - } - - /** Current phone media-stream volume normalized to the range 0..100. */ - public int getMediaVolumePercent() { - if (audioManager == null) { - return -1; - } - int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); - if (maxVolume <= 0) { - return 0; - } - int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); - return Math.max(0, Math.min(100, Math.round(currentVolume * 100f / maxVolume))); - } - - /** Set the phone media-stream volume from a normalized 0..100 value. */ - public void setMediaVolumePercent(int volumePercent) { - if (audioManager == null) { - return; - } - int clampedPercent = Math.max(0, Math.min(100, volumePercent)); - int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); - int streamVolume = Math.round(clampedPercent * maxVolume / 100f); - audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume, 0); - } - - /** - * Album art for the active session's current item, grayscale, scaled to - * fit within maxSize x maxSize preserving aspect. Returns a gray packet - * (see ImageFileLoader.bitmapToGrayPacket) or an empty array when no art - * is available. gamma and dither are the photographic tone handling - * described on ImageFileLoader.bitmapToGrayPacket; the TS side passes - * its shared photo preset. - */ - public byte[] getAlbumArtGray(int maxSize, float gamma, boolean dither) { - Bitmap art; - synchronized (lock) { - if (activeController == null) { - return new byte[0]; - } - MediaMetadata metadata = activeController.getMetadata(); - if (metadata == null) { - return new byte[0]; - } - art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART); - if (art == null) { - art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART); - } - if (art == null) { - art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON); - } - } - return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, dither); - } - - /** - * The active session's queue (playlist) as JSON: - * [{"id": long, "title": string, "active": bool}, ...]. Empty string when - * the player exposes no queue. - */ - public String getQueueJson() { - synchronized (lock) { - if (activeController == null) { - return ""; - } - List queue = activeController.getQueue(); - if (queue == null || queue.isEmpty()) { - return ""; - } - PlaybackState state = activeController.getPlaybackState(); - long activeId = state == null ? -1 : state.getActiveQueueItemId(); - try { - JSONArray out = new JSONArray(); - for (MediaSession.QueueItem item : queue) { - MediaDescription description = item.getDescription(); - CharSequence title = description == null ? null : description.getTitle(); - JSONObject entry = new JSONObject(); - entry.put("id", item.getQueueId()); - entry.put("title", title == null ? "" : title.toString()); - entry.put("active", item.getQueueId() == activeId); - out.put(entry); - } - return out.toString(); - } catch (Exception e) { - Log.w("FaceclawMedia", "queue serialization failed", e); - return ""; - } - } - } - - public void openNotificationAccessSettings() { - Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - appContext.startActivity(intent); - } - - private void refreshActiveControllerLocked(List controllers) { - if (!started) { - setActiveControllerLocked(null); - emitStateLocked(); - return; - } - if (!isNotificationAccessEnabled()) { - setActiveControllerLocked(null); - emitStateLocked(); - return; - } - if (controllers == null && sessionManager != null) { - try { - controllers = sessionManager.getActiveSessions(listenerComponent); - } catch (SecurityException ignored) { - controllers = null; - } - } - emitSessionAppsLocked(controllers); - setActiveControllerLocked(chooseController(controllers)); - emitStateLocked(); - } - - private MediaController chooseController(List controllers) { - if (controllers == null || controllers.isEmpty()) { - return null; - } - MediaController first = null; - for (MediaController controller : controllers) { - if (ignoredPackages.contains(controller.getPackageName())) continue; - if (first == null) first = controller; - PlaybackState playbackState = controller.getPlaybackState(); - if (playbackState != null && playbackState.getState() == PlaybackState.STATE_PLAYING) { - return controller; - } - } - return first; - } - - private void emitSessionAppsLocked(List controllers) { - FaceclawMediaControllerListener currentListener = listener; - if (currentListener == null || controllers == null) return; - JSONArray apps = new JSONArray(); - Set seen = new HashSet<>(); - for (MediaController controller : controllers) { - String packageName = safe(controller.getPackageName()); - if (packageName.isEmpty() || !seen.add(packageName)) continue; - try { - JSONObject app = new JSONObject(); - app.put("packageName", packageName); - app.put("appName", getApplicationLabel(packageName)); - apps.put(app); - } catch (Exception e) { - Log.w("FaceclawMedia", "session app serialization failed", e); - } - } - String json = apps.toString(); - mainHandler.post(() -> currentListener.onSessionAppsChanged(json)); - } - - private void setActiveControllerLocked(MediaController controller) { - if (sameController(activeController, controller)) { - return; - } - if (activeController != null) { - activeController.unregisterCallback(controllerCallback); - } - activeController = controller; - if (activeController != null) { - activeController.registerCallback(controllerCallback, mainHandler); - } - } - - private boolean sameController(MediaController a, MediaController b) { - if (a == b) { - return true; - } - if (a == null || b == null) { - return false; - } - return a.getSessionToken().equals(b.getSessionToken()); - } - - private boolean isNotificationAccessEnabled() { - String enabledListeners = Settings.Secure.getString( - appContext.getContentResolver(), - ENABLED_NOTIFICATION_LISTENERS - ); - if (enabledListeners == null || enabledListeners.isEmpty()) { - return false; - } - String fullName = listenerComponent.flattenToString(); - String shortName = listenerComponent.flattenToShortString(); - return enabledListeners.contains(fullName) - || enabledListeners.contains(shortName) - || enabledListeners.contains(appContext.getPackageName()); - } - - private void emitStateLocked() { - FaceclawMediaControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - - String playbackState = started ? "idle" : "stopped"; - String packageName = ""; - String appName = ""; - String title = ""; - String artist = ""; - String album = ""; - long positionMs = -1L; - long durationMs = -1L; - float playbackSpeed = 0f; - boolean canPlayPause = false; - boolean canSkipNext = false; - boolean canSkipPrevious = false; - boolean accessEnabled = isNotificationAccessEnabled(); - String status; - - if (!accessEnabled) { - playbackState = "notification-access-required"; - status = "Notification access required."; - } else if (activeController == null) { - status = "No active media session."; - } else { - packageName = safe(activeController.getPackageName()); - appName = getApplicationLabel(packageName); - PlaybackState state = activeController.getPlaybackState(); - MediaMetadata metadata = activeController.getMetadata(); - playbackState = playbackStateName(state); - if (metadata != null) { - title = safe(metadata.getString(MediaMetadata.METADATA_KEY_TITLE)); - artist = safe(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST)); - album = safe(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM)); - durationMs = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION); - if (durationMs <= 0L) { - durationMs = -1L; - } - } - if (state != null) { - positionMs = estimatedPositionMs(state, durationMs); - playbackSpeed = state.getPlaybackSpeed(); - } - long actions = state == null ? 0L : state.getActions(); - canPlayPause = (actions & PlaybackState.ACTION_PLAY_PAUSE) != 0 - || (actions & PlaybackState.ACTION_PLAY) != 0 - || (actions & PlaybackState.ACTION_PAUSE) != 0; - canSkipNext = (actions & PlaybackState.ACTION_SKIP_TO_NEXT) != 0; - canSkipPrevious = (actions & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0; - status = packageName.isEmpty() ? "Active media session." : "Active session: " + packageName; - } - - final String finalPlaybackState = playbackState; - final String finalPackageName = packageName; - final String finalAppName = appName; - final String finalTitle = title; - final String finalArtist = artist; - final String finalAlbum = album; - final long finalPositionMs = positionMs; - final long finalDurationMs = durationMs; - final float finalPlaybackSpeed = playbackSpeed; - final boolean finalCanPlayPause = canPlayPause; - final boolean finalCanSkipNext = canSkipNext; - final boolean finalCanSkipPrevious = canSkipPrevious; - final boolean finalAccessEnabled = accessEnabled; - final String finalStatus = status; - - mainHandler.post(() -> currentListener.onStateChange( - finalPlaybackState, - finalPackageName, - finalAppName, - finalTitle, - finalArtist, - finalAlbum, - finalPositionMs, - finalDurationMs, - finalPlaybackSpeed, - finalCanPlayPause, - finalCanSkipNext, - finalCanSkipPrevious, - finalAccessEnabled, - finalStatus - )); - } - - private String playbackStateName(PlaybackState state) { - if (state == null) { - return "idle"; - } - switch (state.getState()) { - case PlaybackState.STATE_PLAYING: - return "playing"; - case PlaybackState.STATE_PAUSED: - return "paused"; - case PlaybackState.STATE_BUFFERING: - case PlaybackState.STATE_CONNECTING: - return "buffering"; - case PlaybackState.STATE_STOPPED: - return "stopped"; - default: - return "idle"; - } - } - - /** PlaybackState positions are snapshots; advance a playing snapshot to now. */ - private long estimatedPositionMs(PlaybackState state, long durationMs) { - long positionMs = state.getPosition(); - if (positionMs == PlaybackState.PLAYBACK_POSITION_UNKNOWN) { - return -1L; - } - if (state.getState() == PlaybackState.STATE_PLAYING) { - long updatedAtMs = state.getLastPositionUpdateTime(); - if (updatedAtMs > 0L) { - positionMs += (long) ((SystemClock.elapsedRealtime() - updatedAtMs) * state.getPlaybackSpeed()); - } - } - positionMs = Math.max(0L, positionMs); - return durationMs > 0L ? Math.min(positionMs, durationMs) : positionMs; - } - - /** Resolve a media session's package id to the user-facing installed app name. */ - private String getApplicationLabel(String packageName) { - if (packageName == null || packageName.isEmpty()) { - return ""; - } - PackageManager packageManager = appContext.getPackageManager(); - try { - ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0); - CharSequence label = packageManager.getApplicationLabel(applicationInfo); - if (label != null && label.length() > 0) { - return label.toString(); - } - } catch (PackageManager.NameNotFoundException ignored) { - } - return packageName; - } - - private String safe(String value) { - return value == null ? "" : value; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.kt new file mode 100644 index 00000000..b7f8ca5a --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.kt @@ -0,0 +1,577 @@ +package com.faceclaw.app + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.ApplicationInfo +import android.content.pm.PackageManager +import android.database.ContentObserver +import android.graphics.Bitmap +import android.media.AudioManager +import android.media.MediaDescription +import android.media.MediaMetadata +import android.media.session.MediaController +import android.media.session.MediaSession +import android.media.session.MediaSessionManager +import android.media.session.PlaybackState +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.provider.Settings +import android.util.Log + +import org.json.JSONArray +import org.json.JSONObject + +import java.util.HashSet + +class FaceclawMediaController(context: Context) { + companion object { + private const val ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners" + } + + private val appContext: Context = context.applicationContext + private val lock = Any() + private val mainHandler = Handler(Looper.getMainLooper()) + private val listenerComponent: ComponentName + private val sessionManager: MediaSessionManager? + private val audioManager: AudioManager? + + private val controllerCallback: MediaController.Callback = object : MediaController.Callback() { + override fun onPlaybackStateChanged(state: PlaybackState?) { + synchronized(lock) { + emitStateLocked() + } + } + + override fun onMetadataChanged(metadata: MediaMetadata?) { + synchronized(lock) { + emitStateLocked() + } + } + + override fun onQueueChanged(queue: MutableList?) { + synchronized(lock) { + emitStateLocked() + } + } + + override fun onSessionDestroyed() { + synchronized(lock) { + refreshActiveControllerLocked(null) + } + } + } + + private val sessionsChangedListener = MediaSessionManager.OnActiveSessionsChangedListener { controllers -> + synchronized(lock) { + refreshActiveControllerLocked(controllers) + } + } + + /** + * Fires when the user grants or revokes notification-listener access in + * system settings. Session access depends on that grant, so the sessions + * listener is (re)registered and the state re-emitted whenever it changes; + * otherwise a grant made after start() would never be noticed. + */ + private val notificationAccessObserver: ContentObserver = object : ContentObserver(mainHandler) { + override fun onChange(selfChange: Boolean) { + synchronized(lock) { + if (!started) return + syncSessionsListenerLocked() + refreshActiveControllerLocked(null) + } + } + } + + @Volatile + private var listener: FaceclawMediaControllerListener? = null + private var activeController: MediaController? = null + private var started = false + private var sessionsListenerRegistered = false + private var ignoredPackages: Set = HashSet() + + init { + this.listenerComponent = ComponentName(appContext, FaceclawMediaNotificationListenerService::class.java) + this.sessionManager = appContext.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager? + this.audioManager = appContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager? + } + + fun setListener(listener: FaceclawMediaControllerListener?) { + synchronized(lock) { + this.listener = listener + emitStateLocked() + } + } + + fun start() { + synchronized(lock) { + if (started) { + emitStateLocked() + return + } + started = true + try { + appContext.contentResolver.registerContentObserver( + Settings.Secure.getUriFor(ENABLED_NOTIFICATION_LISTENERS), + false, + notificationAccessObserver + ) + } catch (e: Exception) { + Log.w("FaceclawMedia", "notification access observer registration failed", e) + } + syncSessionsListenerLocked() + refreshActiveControllerLocked(null) + } + } + + /** + * Keep the active-sessions listener registered exactly while notification + * access is granted. Registration throws SecurityException without the + * grant, so this is retried from the settings observer rather than only + * attempted once at start(). + */ + private fun syncSessionsListenerLocked() { + if (sessionManager == null) return + val accessEnabled = started && isNotificationAccessEnabled() + if (accessEnabled && !sessionsListenerRegistered) { + try { + sessionManager.addOnActiveSessionsChangedListener( + sessionsChangedListener, + listenerComponent, + mainHandler + ) + sessionsListenerRegistered = true + } catch (ignored: SecurityException) { + } + } else if (!accessEnabled && sessionsListenerRegistered) { + try { + sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener) + } catch (ignored: SecurityException) { + } + sessionsListenerRegistered = false + } + } + + /** Apply the complete preference set and immediately choose an allowed session. */ + fun setIgnoredPackagesJson(packagesJson: String?) { + val packages: MutableSet = HashSet() + try { + val array = JSONArray(packagesJson) + for (i in 0 until array.length()) packages.add(array.getString(i)) + } catch (e: Exception) { + Log.w("FaceclawMedia", "invalid ignored packages", e) + return + } + synchronized(lock) { + if (ignoredPackages == packages) return + ignoredPackages = packages + refreshActiveControllerLocked(null) + } + } + + fun stop() { + synchronized(lock) { + if (!started) { + return + } + started = false + try { + appContext.contentResolver.unregisterContentObserver(notificationAccessObserver) + } catch (ignored: Exception) { + } + syncSessionsListenerLocked() + setActiveControllerLocked(null) + emitStateLocked() + } + } + + fun playPause() { + synchronized(lock) { + val controller = activeController + if (controller == null) { + return + } + val playbackState: PlaybackState? = controller.playbackState + val controls: MediaController.TransportControls = controller.transportControls + if (playbackState == null) { + controls.play() + return + } + when (playbackState.state) { + PlaybackState.STATE_PLAYING, + PlaybackState.STATE_BUFFERING, + PlaybackState.STATE_CONNECTING -> { + controls.pause() + return + } + else -> controls.play() + } + } + } + + fun skipNext() { + synchronized(lock) { + val controller = activeController + if (controller != null) { + controller.transportControls.skipToNext() + } + } + } + + fun skipPrevious() { + synchronized(lock) { + val controller = activeController + if (controller != null) { + controller.transportControls.skipToPrevious() + } + } + } + + fun skipToQueueItem(queueId: Long) { + synchronized(lock) { + val controller = activeController + if (controller != null) { + controller.transportControls.skipToQueueItem(queueId) + } + } + } + + /** Current phone media-stream volume normalized to the range 0..100. */ + fun getMediaVolumePercent(): Int { + if (audioManager == null) { + return -1 + } + val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) + if (maxVolume <= 0) { + return 0 + } + val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + return Math.max(0, Math.min(100, Math.round(currentVolume * 100f / maxVolume))) + } + + /** Set the phone media-stream volume from a normalized 0..100 value. */ + fun setMediaVolumePercent(volumePercent: Int) { + if (audioManager == null) { + return + } + val clampedPercent = Math.max(0, Math.min(100, volumePercent)) + val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) + val streamVolume = Math.round(clampedPercent * maxVolume / 100f) + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume, 0) + } + + /** + * Album art for the active session's current item, grayscale, scaled to + * fit within maxSize x maxSize preserving aspect. Returns a gray packet + * (see ImageFileLoader.bitmapToGrayPacket) or an empty array when no art + * is available. gamma and dither are the photographic tone handling + * described on ImageFileLoader.bitmapToGrayPacket; the TS side passes + * its shared photo preset. + */ + fun getAlbumArtGray(maxSize: Int, gamma: Float, dither: Boolean): ByteArray { + var art: Bitmap? + synchronized(lock) { + val controller = activeController + if (controller == null) { + return ByteArray(0) + } + val metadata: MediaMetadata? = controller.metadata + if (metadata == null) { + return ByteArray(0) + } + art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART) + if (art == null) { + art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART) + } + if (art == null) { + art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON) + } + } + return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, dither) + } + + /** + * The active session's queue (playlist) as JSON: + * [{"id": long, "title": string, "active": bool}, ...]. Empty string when + * the player exposes no queue. + */ + fun getQueueJson(): String { + synchronized(lock) { + val controller = activeController + if (controller == null) { + return "" + } + val queue: List? = controller.queue + if (queue == null || queue.isEmpty()) { + return "" + } + val state: PlaybackState? = controller.playbackState + val activeId = if (state == null) -1L else state.activeQueueItemId + try { + val out = JSONArray() + for (item in queue) { + val description: MediaDescription? = item.description + val title: CharSequence? = if (description == null) null else description.title + val entry = JSONObject() + entry.put("id", item.queueId) + entry.put("title", if (title == null) "" else title.toString()) + entry.put("active", item.queueId == activeId) + out.put(entry) + } + return out.toString() + } catch (e: Exception) { + Log.w("FaceclawMedia", "queue serialization failed", e) + return "" + } + } + } + + fun openNotificationAccessSettings() { + val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + appContext.startActivity(intent) + } + + private fun refreshActiveControllerLocked(controllersIn: List?) { + var controllers = controllersIn + if (!started) { + setActiveControllerLocked(null) + emitStateLocked() + return + } + if (!isNotificationAccessEnabled()) { + setActiveControllerLocked(null) + emitStateLocked() + return + } + if (controllers == null && sessionManager != null) { + try { + controllers = sessionManager.getActiveSessions(listenerComponent) + } catch (ignored: SecurityException) { + controllers = null + } + } + emitSessionAppsLocked(controllers) + setActiveControllerLocked(chooseController(controllers)) + emitStateLocked() + } + + private fun chooseController(controllers: List?): MediaController? { + if (controllers == null || controllers.isEmpty()) { + return null + } + var first: MediaController? = null + for (controller in controllers) { + if (ignoredPackages.contains(controller.packageName)) continue + if (first == null) first = controller + val playbackState: PlaybackState? = controller.playbackState + if (playbackState != null && playbackState.state == PlaybackState.STATE_PLAYING) { + return controller + } + } + return first + } + + private fun emitSessionAppsLocked(controllers: List?) { + val currentListener = listener + if (currentListener == null || controllers == null) return + val apps = JSONArray() + val seen: MutableSet = HashSet() + for (controller in controllers) { + val packageName = safe(controller.packageName) + if (packageName.isEmpty() || !seen.add(packageName)) continue + try { + val app = JSONObject() + app.put("packageName", packageName) + app.put("appName", getApplicationLabel(packageName)) + apps.put(app) + } catch (e: Exception) { + Log.w("FaceclawMedia", "session app serialization failed", e) + } + } + val json = apps.toString() + mainHandler.post { currentListener.onSessionAppsChanged(json) } + } + + private fun setActiveControllerLocked(controller: MediaController?) { + if (sameController(activeController, controller)) { + return + } + val previous = activeController + if (previous != null) { + previous.unregisterCallback(controllerCallback) + } + activeController = controller + if (controller != null) { + controller.registerCallback(controllerCallback, mainHandler) + } + } + + private fun sameController(a: MediaController?, b: MediaController?): Boolean { + if (a === b) { + return true + } + if (a == null || b == null) { + return false + } + return a.sessionToken == b.sessionToken + } + + private fun isNotificationAccessEnabled(): Boolean { + val enabledListeners: String? = Settings.Secure.getString( + appContext.contentResolver, + ENABLED_NOTIFICATION_LISTENERS + ) + if (enabledListeners == null || enabledListeners.isEmpty()) { + return false + } + val fullName = listenerComponent.flattenToString() + val shortName = listenerComponent.flattenToShortString() + return enabledListeners.contains(fullName) + || enabledListeners.contains(shortName) + || enabledListeners.contains(appContext.packageName) + } + + private fun emitStateLocked() { + val currentListener = listener + if (currentListener == null) { + return + } + + var playbackState = if (started) "idle" else "stopped" + var packageName = "" + var appName = "" + var title = "" + var artist = "" + var album = "" + var positionMs = -1L + var durationMs = -1L + var playbackSpeed = 0f + var canPlayPause = false + var canSkipNext = false + var canSkipPrevious = false + val accessEnabled = isNotificationAccessEnabled() + val status: String + + val controller = activeController + if (!accessEnabled) { + playbackState = "notification-access-required" + status = "Notification access required." + } else if (controller == null) { + status = "No active media session." + } else { + packageName = safe(controller.packageName) + appName = getApplicationLabel(packageName) + val state: PlaybackState? = controller.playbackState + val metadata: MediaMetadata? = controller.metadata + playbackState = playbackStateName(state) + if (metadata != null) { + title = safe(metadata.getString(MediaMetadata.METADATA_KEY_TITLE)) + artist = safe(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST)) + album = safe(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM)) + durationMs = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION) + if (durationMs <= 0L) { + durationMs = -1L + } + } + if (state != null) { + positionMs = estimatedPositionMs(state, durationMs) + playbackSpeed = state.playbackSpeed + } + val actions = if (state == null) 0L else state.actions + canPlayPause = (actions and PlaybackState.ACTION_PLAY_PAUSE) != 0L + || (actions and PlaybackState.ACTION_PLAY) != 0L + || (actions and PlaybackState.ACTION_PAUSE) != 0L + canSkipNext = (actions and PlaybackState.ACTION_SKIP_TO_NEXT) != 0L + canSkipPrevious = (actions and PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0L + status = if (packageName.isEmpty()) "Active media session." else "Active session: $packageName" + } + + val finalPlaybackState = playbackState + val finalPackageName = packageName + val finalAppName = appName + val finalTitle = title + val finalArtist = artist + val finalAlbum = album + val finalPositionMs = positionMs + val finalDurationMs = durationMs + val finalPlaybackSpeed = playbackSpeed + val finalCanPlayPause = canPlayPause + val finalCanSkipNext = canSkipNext + val finalCanSkipPrevious = canSkipPrevious + val finalAccessEnabled = accessEnabled + val finalStatus = status + + mainHandler.post { + currentListener.onStateChange( + finalPlaybackState, + finalPackageName, + finalAppName, + finalTitle, + finalArtist, + finalAlbum, + finalPositionMs, + finalDurationMs, + finalPlaybackSpeed, + finalCanPlayPause, + finalCanSkipNext, + finalCanSkipPrevious, + finalAccessEnabled, + finalStatus + ) + } + } + + private fun playbackStateName(state: PlaybackState?): String { + if (state == null) { + return "idle" + } + return when (state.state) { + PlaybackState.STATE_PLAYING -> "playing" + PlaybackState.STATE_PAUSED -> "paused" + PlaybackState.STATE_BUFFERING, + PlaybackState.STATE_CONNECTING -> "buffering" + PlaybackState.STATE_STOPPED -> "stopped" + else -> "idle" + } + } + + /** PlaybackState positions are snapshots; advance a playing snapshot to now. */ + private fun estimatedPositionMs(state: PlaybackState, durationMs: Long): Long { + var positionMs = state.position + if (positionMs == PlaybackState.PLAYBACK_POSITION_UNKNOWN) { + return -1L + } + if (state.state == PlaybackState.STATE_PLAYING) { + val updatedAtMs = state.lastPositionUpdateTime + if (updatedAtMs > 0L) { + positionMs += ((SystemClock.elapsedRealtime() - updatedAtMs) * state.playbackSpeed).toLong() + } + } + positionMs = Math.max(0L, positionMs) + return if (durationMs > 0L) Math.min(positionMs, durationMs) else positionMs + } + + /** Resolve a media session's package id to the user-facing installed app name. */ + private fun getApplicationLabel(packageName: String?): String { + if (packageName == null || packageName.isEmpty()) { + return "" + } + val packageManager = appContext.packageManager + try { + val applicationInfo: ApplicationInfo = packageManager.getApplicationInfo(packageName, 0) + val label: CharSequence? = packageManager.getApplicationLabel(applicationInfo) + if (label != null && label.length > 0) { + return label.toString() + } + } catch (ignored: PackageManager.NameNotFoundException) { + } + return packageName + } + + private fun safe(value: String?): String { + return value ?: "" + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java deleted file mode 100644 index 8ca97f00..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java +++ /dev/null @@ -1,657 +0,0 @@ -package com.faceclaw.app; - -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.service.notification.NotificationListenerService; -import android.service.notification.StatusBarNotification; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArraySet; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -public class FaceclawMediaNotificationListenerService extends NotificationListenerService { - private static final String TAG = "FaceclawNotify"; - private static final double NOTIFICATION_ICON_GAMMA = 1.6; - private static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName"; - - private static volatile FaceclawMediaNotificationListenerService activeService; - private static final Handler mainHandler = new Handler(Looper.getMainLooper()); - private static final Set notificationListeners = new CopyOnWriteArraySet<>(); - private static final Set activeNotificationWakeKeys = new HashSet<>(); - - @Override - public void onCreate() { - super.onCreate(); - activeService = this; - } - - @Override - public void onDestroy() { - if (activeService == this) { - activeService = null; - } - super.onDestroy(); - } - - @Override - public void onListenerConnected() { - activeService = this; - super.onListenerConnected(); - refreshActiveNotificationWakeKeys(this); - } - - @Override - public void onListenerDisconnected() { - if (activeService == this) { - activeService = null; - } - super.onListenerDisconnected(); - } - - @Override - public void onNotificationPosted(StatusBarNotification statusBarNotification) { - super.onNotificationPosted(statusBarNotification); - if (!shouldShowNotificationInList(this, statusBarNotification)) { - forgetActiveNotificationWakeKey(statusBarNotification); - return; - } - if (shouldEmitNotificationPosted(statusBarNotification)) { - emitNotificationPosted(statusBarNotification.getKey()); - } - } - - @Override - public void onNotificationRemoved(StatusBarNotification statusBarNotification) { - forgetActiveNotificationWakeKey(statusBarNotification); - super.onNotificationRemoved(statusBarNotification); - } - - public static void addNotificationListener(FaceclawNotificationListener listener) { - if (listener != null) { - notificationListeners.add(listener); - } - } - - public static void removeNotificationListener(FaceclawNotificationListener listener) { - if (listener != null) { - notificationListeners.remove(listener); - } - } - - public static boolean hasActiveNotificationTitle(String expectedTitle) { - FaceclawMediaNotificationListenerService service = activeService; - if (service == null || expectedTitle == null || expectedTitle.isEmpty()) { - return false; - } - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while checking active notifications", e); - return false; - } catch (Throwable t) { - Log.w(TAG, "failed to check active notifications", t); - return false; - } - if (notifications == null || notifications.length == 0) { - return false; - } - for (StatusBarNotification notification : notifications) { - if (notification == null || notification.getNotification() == null) { - continue; - } - Bundle extras = notification.getNotification().extras; - if (extras == null) { - continue; - } - CharSequence title = extras.getCharSequence(Notification.EXTRA_TITLE); - if (title != null && expectedTitle.contentEquals(title)) { - return true; - } - } - return false; - } - - public static byte[] getActiveNotificationIconGrays(int iconSize, int maxIcons) { - FaceclawMediaNotificationListenerService service = activeService; - int size = Math.max(1, Math.min(96, iconSize)); - int limit = Math.max(0, maxIcons); - if (service == null || limit == 0) { - return new byte[0]; - } - - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while reading icons", e); - return new byte[0]; - } catch (Throwable t) { - Log.w(TAG, "failed to read notification icons", t); - return new byte[0]; - } - if (notifications == null || notifications.length == 0) { - return new byte[0]; - } - - ByteArrayOutputStream out = new ByteArrayOutputStream(size * size * Math.min(limit, notifications.length)); - Set emittedGroupKeys = new HashSet<>(); - int emitted = 0; - for (StatusBarNotification statusBarNotification : notifications) { - if (!shouldShowNotificationIcon(service, statusBarNotification)) { - continue; - } - String dedupeGroupKey = getNotificationDedupeGroupKey(statusBarNotification); - if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) { - continue; - } - Drawable drawable = loadNotificationIcon(service, statusBarNotification.getNotification()); - if (drawable == null) { - Log.i(TAG, "icon skipped (no drawable): " + statusBarNotification.getPackageName()); - continue; - } - Log.i(TAG, "icon[" + emitted + "] pkg=" + statusBarNotification.getPackageName() - + " drawable=" + drawable.getClass().getSimpleName() - + " intrinsic=" + drawable.getIntrinsicWidth() + "x" + drawable.getIntrinsicHeight()); - appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.getPackageName()); - if (dedupeGroupKey != null) { - emittedGroupKeys.add(dedupeGroupKey); - } - emitted += 1; - if (emitted >= limit) { - break; - } - } - return out.toByteArray(); - } - - /** - * Grayscale icon (iconSize*iconSize bytes) for one active notification, - * identified by its key; empty array when the notification or its icon is - * unavailable. Shares the extraction/scaling pipeline with the tray icon - * strip above. - */ - public static byte[] getNotificationIconGrayForKey(String key, int iconSize) { - FaceclawMediaNotificationListenerService service = activeService; - int size = Math.max(1, Math.min(96, iconSize)); - if (service == null) { - return new byte[0]; - } - StatusBarNotification statusBarNotification = findActiveNotificationByKey(service, key); - if (statusBarNotification == null || statusBarNotification.getNotification() == null) { - return new byte[0]; - } - Drawable drawable = loadNotificationIcon(service, statusBarNotification.getNotification()); - if (drawable == null) { - return new byte[0]; - } - ByteArrayOutputStream out = new ByteArrayOutputStream(size * size); - appendIconGrayBytes(drawable, size, out, service, -1, statusBarNotification.getPackageName()); - return out.toByteArray(); - } - - public static String getActiveNotificationsJson(int maxNotifications) { - FaceclawMediaNotificationListenerService service = activeService; - int limit = Math.max(0, maxNotifications); - if (service == null || limit == 0) { - return "[]"; - } - - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while reading notifications", e); - return "[]"; - } catch (Throwable t) { - Log.w(TAG, "failed to read notifications", t); - return "[]"; - } - if (notifications == null || notifications.length == 0) { - return "[]"; - } - - Arrays.sort(notifications, new Comparator() { - @Override - public int compare(StatusBarNotification a, StatusBarNotification b) { - long left = a == null ? 0 : a.getPostTime(); - long right = b == null ? 0 : b.getPostTime(); - return Long.compare(right, left); - } - }); - - JSONArray out = new JSONArray(); - for (StatusBarNotification statusBarNotification : notifications) { - if (out.length() >= limit) { - break; - } - if (!shouldShowNotificationInList(service, statusBarNotification)) { - continue; - } - try { - out.put(buildNotificationJson(service, statusBarNotification)); - } catch (Throwable t) { - Log.w(TAG, "failed to serialize notification", t); - } - } - return out.toString(); - } - - public static boolean invokeNotificationAction(String key, int actionIndex) { - FaceclawMediaNotificationListenerService service = activeService; - StatusBarNotification statusBarNotification = findActiveNotificationByKey(service, key); - if (statusBarNotification == null || statusBarNotification.getNotification() == null) { - return false; - } - Notification.Action[] actions = statusBarNotification.getNotification().actions; - if (actions == null || actionIndex < 0 || actionIndex >= actions.length) { - return false; - } - PendingIntent intent = actions[actionIndex].actionIntent; - if (intent == null) { - return false; - } - try { - intent.send(); - return true; - } catch (PendingIntent.CanceledException e) { - Log.w(TAG, "notification action pending intent was canceled", e); - return false; - } catch (Throwable t) { - Log.w(TAG, "failed to invoke notification action", t); - return false; - } - } - - public static boolean dismissNotification(String key) { - FaceclawMediaNotificationListenerService service = activeService; - if (service == null || key == null || key.isEmpty()) { - return false; - } - try { - service.cancelNotification(key); - return true; - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while dismissing notification", e); - return false; - } catch (Throwable t) { - Log.w(TAG, "failed to dismiss notification", t); - return false; - } - } - - private static void emitNotificationPosted(String key) { - if (key == null || key.isEmpty() || notificationListeners.isEmpty()) { - return; - } - for (FaceclawNotificationListener listener : notificationListeners) { - mainHandler.post(() -> { - try { - listener.onNotificationPosted(key); - } catch (Throwable t) { - Log.w(TAG, "notification listener failed", t); - } - }); - } - } - - private static void refreshActiveNotificationWakeKeys(FaceclawMediaNotificationListenerService service) { - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (Throwable t) { - Log.w(TAG, "failed to refresh active notification wake keys", t); - return; - } - synchronized (activeNotificationWakeKeys) { - activeNotificationWakeKeys.clear(); - if (notifications == null) { - return; - } - for (StatusBarNotification statusBarNotification : notifications) { - if (shouldShowNotificationInList(service, statusBarNotification)) { - String key = statusBarNotification.getKey(); - if (key != null && !key.isEmpty()) { - activeNotificationWakeKeys.add(key); - } - } - } - } - } - - private static boolean shouldEmitNotificationPosted(StatusBarNotification statusBarNotification) { - String key = statusBarNotification.getKey(); - if (key == null || key.isEmpty()) { - return true; - } - - boolean alreadyActive; - synchronized (activeNotificationWakeKeys) { - alreadyActive = activeNotificationWakeKeys.contains(key); - activeNotificationWakeKeys.add(key); - } - return !alreadyActive || !isPersistentNotification(statusBarNotification); - } - - private static void forgetActiveNotificationWakeKey(StatusBarNotification statusBarNotification) { - if (statusBarNotification == null) { - return; - } - String key = statusBarNotification.getKey(); - if (key == null || key.isEmpty()) { - return; - } - synchronized (activeNotificationWakeKeys) { - activeNotificationWakeKeys.remove(key); - } - } - - private static boolean isPersistentNotification(StatusBarNotification statusBarNotification) { - Notification notification = statusBarNotification.getNotification(); - if (notification == null) { - return false; - } - int persistentFlags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; - return (notification.flags & persistentFlags) != 0; - } - - private static boolean shouldShowNotificationIcon(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { - if (!shouldShowNotificationInList(service, statusBarNotification)) { - return false; - } - Notification notification = statusBarNotification.getNotification(); - if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != 0) { - return false; - } - return true; - } - - private static boolean shouldShowNotificationInList(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { - if (statusBarNotification == null || statusBarNotification.getNotification() == null) { - return false; - } - // Our own notifications stay out of the mirror: the foreground-service - // one is noise, and the Timers app rings on the glasses itself (its - // phone notification is for the phone), so mirroring it would stack a - // notification modal over the ringing screen. - if (service.getPackageName().equals(statusBarNotification.getPackageName())) { - return false; - } - Notification notification = statusBarNotification.getNotification(); - if (Notification.CATEGORY_TRANSPORT.equals(notification.category)) { - return false; - } - Bundle extras = notification.extras; - if (extras != null && extras.containsKey("android.mediaSession")) { - return false; - } - - NotificationListenerService.RankingMap rankingMap = service.getCurrentRanking(); - if (rankingMap == null) { - return true; - } - NotificationListenerService.Ranking ranking = new NotificationListenerService.Ranking(); - if (!rankingMap.getRanking(statusBarNotification.getKey(), ranking)) { - return true; - } - int importance = ranking.getImportance(); - return importance > NotificationManager.IMPORTANCE_MIN; - } - - private static String getNotificationDedupeGroupKey(StatusBarNotification statusBarNotification) { - Notification notification = statusBarNotification.getNotification(); - if (notification.getGroup() == null && statusBarNotification.getOverrideGroupKey() == null) { - return null; - } - String groupKey = statusBarNotification.getGroupKey(); - if (groupKey == null || groupKey.isEmpty()) { - return null; - } - // Group children often share the same small icon. Emit only one icon for the group. - return groupKey; - } - - private static Drawable loadNotificationIcon(FaceclawMediaNotificationListenerService service, Notification notification) { - try { - Icon smallIcon = notification.getSmallIcon(); - if (smallIcon != null) { - Drawable drawable = smallIcon.loadDrawable(service); - if (drawable != null) { - // Status-bar small icons are alpha templates: the platform - // draws them tinted and ignores their color channels, which - // apps may fill with garbage (Discord ships noise there). - // Tint white so the shape comes from alpha alone, matching - // how the status bar renders them. - drawable.mutate(); - drawable.setTint(Color.WHITE); - if (drawable instanceof BitmapDrawable) { - ((BitmapDrawable) drawable).setFilterBitmap(true); - } - return drawable; - } - } - } catch (Throwable t) { - Log.w(TAG, "failed to load small notification icon", t); - } - try { - // Large icons are real color images (avatars, album art); keep color. - Icon largeIcon = notification.getLargeIcon(); - if (largeIcon != null) { - return largeIcon.loadDrawable(service); - } - } catch (Throwable t) { - Log.w(TAG, "failed to load large notification icon", t); - } - return null; - } - - private static void appendIconGrayBytes(Drawable drawable, int size, ByteArrayOutputStream out, - FaceclawMediaNotificationListenerService service, int index, String packageName) { - Bitmap bitmap = renderIconScaled(drawable, size); - dumpIconDebugPng(service, index, packageName, bitmap); - for (int y = 0; y < size; y++) { - for (int x = 0; x < size; x++) { - int color = bitmap.getPixel(x, y); - int alpha = Color.alpha(color); - double grayLinear = (0.2126 * Color.red(color) + 0.7152 * Color.green(color) + 0.0722 * Color.blue(color)) * alpha / (255.0 * 255.0); - int gray = (int) Math.round(255.0 * Math.pow(Math.max(0.0, Math.min(1.0, grayLinear)), NOTIFICATION_ICON_GAMMA)); - out.write(gray & 0xff); - } - } - bitmap.recycle(); - } - - /** - * Render a drawable at the target size with proper downscaling. Detailed - * sources (e.g. avatar bitmaps used as notification icons) are rendered at - * native resolution and reduced by repeated halving: a single filtered pass - * from, say, 126px to 24px samples too sparsely and turns fine detail into - * speckle that reads as a garbled icon. - */ - private static Bitmap renderIconScaled(Drawable drawable, int size) { - int renderW = Math.max(size, drawable.getIntrinsicWidth()); - int renderH = Math.max(size, drawable.getIntrinsicHeight()); - Bitmap bitmap = Bitmap.createBitmap(renderW, renderH, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - drawable.setBounds(0, 0, renderW, renderH); - drawable.draw(canvas); - while (bitmap.getWidth() >= size * 2 && bitmap.getHeight() >= size * 2) { - Bitmap halved = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 2, bitmap.getHeight() / 2, true); - bitmap.recycle(); - bitmap = halved; - } - if (bitmap.getWidth() != size || bitmap.getHeight() != size) { - Bitmap scaled = Bitmap.createScaledBitmap(bitmap, size, size, true); - bitmap.recycle(); - bitmap = scaled; - } - return bitmap; - } - - /** - * Debug aid for garbled-icon reports: saves each rendered icon to - * /debug-icons/ (adb-pullable) so extraction problems can - * be told apart from downstream compositing/transmission problems. Cheap: - * runs at most once per icon-cache refresh on tiny bitmaps. - */ - private static void dumpIconDebugPng(FaceclawMediaNotificationListenerService service, int index, String packageName, Bitmap bitmap) { - if (index < 0) { - return; - } - try { - java.io.File dir = new java.io.File(service.getExternalFilesDir(null), "debug-icons"); - if (!dir.exists() && !dir.mkdirs()) { - return; - } - String safeName = packageName == null ? "unknown" : packageName.replaceAll("[^A-Za-z0-9._-]", "_"); - java.io.File file = new java.io.File(dir, "icon-" + index + "-" + safeName + ".png"); - try (java.io.FileOutputStream stream = new java.io.FileOutputStream(file)) { - bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); - } - } catch (Throwable t) { - Log.w(TAG, "failed to dump debug icon", t); - } - } - - private static StatusBarNotification findActiveNotificationByKey(FaceclawMediaNotificationListenerService service, String key) { - if (service == null || key == null || key.isEmpty()) { - return null; - } - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (Throwable t) { - Log.w(TAG, "failed to find active notification", t); - return null; - } - if (notifications == null) { - return null; - } - for (StatusBarNotification statusBarNotification : notifications) { - if (statusBarNotification != null && key.equals(statusBarNotification.getKey())) { - return statusBarNotification; - } - } - return null; - } - - private static JSONObject buildNotificationJson(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) - throws JSONException { - Notification notification = statusBarNotification.getNotification(); - Bundle extras = notification.extras; - JSONObject out = new JSONObject(); - out.put("key", statusBarNotification.getKey()); - out.put("packageName", statusBarNotification.getPackageName()); - out.put("appName", getNotificationAppName(service, statusBarNotification)); - out.put("postTime", statusBarNotification.getPostTime()); - out.put("when", notification.when); - putString(out, "category", notification.category); - if (extras != null) { - putCharSequence(out, "title", firstNonEmpty( - extras.getCharSequence(Notification.EXTRA_TITLE_BIG), - extras.getCharSequence(Notification.EXTRA_TITLE) - )); - putCharSequence(out, "text", extras.getCharSequence(Notification.EXTRA_TEXT)); - putCharSequence(out, "bigText", extras.getCharSequence(Notification.EXTRA_BIG_TEXT)); - putCharSequence(out, "subText", extras.getCharSequence(Notification.EXTRA_SUB_TEXT)); - putCharSequence(out, "infoText", extras.getCharSequence(Notification.EXTRA_INFO_TEXT)); - putCharSequence(out, "summaryText", extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)); - CharSequence[] textLines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); - JSONArray lines = new JSONArray(); - if (textLines != null) { - for (CharSequence line : textLines) { - String text = charSequenceToString(line); - if (!text.isEmpty()) { - lines.put(text); - } - } - } - out.put("lines", lines); - } else { - out.put("lines", new JSONArray()); - } - - JSONArray actionsJson = new JSONArray(); - Notification.Action[] actions = notification.actions; - if (actions != null) { - for (int index = 0; index < actions.length; index++) { - Notification.Action action = actions[index]; - if (action == null) { - continue; - } - String title = charSequenceToString(action.title); - if (title.isEmpty()) { - continue; - } - JSONObject actionJson = new JSONObject(); - actionJson.put("index", index); - actionJson.put("title", title); - actionJson.put("enabled", action.actionIntent != null); - actionsJson.put(actionJson); - } - } - out.put("actions", actionsJson); - return out; - } - - private static String getNotificationAppName(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { - Notification notification = statusBarNotification.getNotification(); - Bundle extras = notification.extras; - if (extras != null) { - String substituteName = charSequenceToString(extras.getCharSequence(EXTRA_SUBSTITUTE_APP_NAME)); - if (!substituteName.isEmpty()) { - return substituteName; - } - } - return getAppLabel(service, statusBarNotification.getPackageName()); - } - - private static String getAppLabel(FaceclawMediaNotificationListenerService service, String packageName) { - if (packageName == null || packageName.isEmpty()) { - return ""; - } - try { - CharSequence label = service - .getPackageManager() - .getApplicationLabel(service.getPackageManager().getApplicationInfo(packageName, 0)); - String text = charSequenceToString(label); - return text.isEmpty() ? packageName : text; - } catch (Throwable t) { - return packageName; - } - } - - private static void putString(JSONObject out, String key, String value) throws JSONException { - out.put(key, value == null ? "" : value); - } - - private static void putCharSequence(JSONObject out, String key, CharSequence value) throws JSONException { - out.put(key, charSequenceToString(value)); - } - - private static CharSequence firstNonEmpty(CharSequence first, CharSequence second) { - return charSequenceToString(first).isEmpty() ? second : first; - } - - private static String charSequenceToString(CharSequence value) { - return value == null ? "" : value.toString(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt new file mode 100644 index 00000000..7c40de56 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt @@ -0,0 +1,662 @@ +package com.faceclaw.app + +import android.app.Notification +import android.app.NotificationManager +import android.app.PendingIntent +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable +import android.graphics.drawable.Icon +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.service.notification.NotificationListenerService +import android.service.notification.StatusBarNotification +import android.util.Log + +import java.io.ByteArrayOutputStream +import java.util.Arrays +import java.util.Comparator +import java.util.HashSet +import java.util.concurrent.CopyOnWriteArraySet + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +class FaceclawMediaNotificationListenerService : NotificationListenerService() { + companion object { + private const val TAG = "FaceclawNotify" + private const val NOTIFICATION_ICON_GAMMA = 1.6 + private const val EXTRA_SUBSTITUTE_APP_NAME = "android.substName" + + @Volatile + private var activeService: FaceclawMediaNotificationListenerService? = null + private val mainHandler = Handler(Looper.getMainLooper()) + private val notificationListeners: MutableSet = CopyOnWriteArraySet() + private val activeNotificationWakeKeys: MutableSet = HashSet() + + @JvmStatic + fun addNotificationListener(listener: FaceclawNotificationListener?) { + if (listener != null) { + notificationListeners.add(listener) + } + } + + @JvmStatic + fun removeNotificationListener(listener: FaceclawNotificationListener?) { + if (listener != null) { + notificationListeners.remove(listener) + } + } + + @JvmStatic + fun hasActiveNotificationTitle(expectedTitle: String?): Boolean { + val service = activeService + if (service == null || expectedTitle == null || expectedTitle.isEmpty()) { + return false + } + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while checking active notifications", e) + return false + } catch (t: Throwable) { + Log.w(TAG, "failed to check active notifications", t) + return false + } + if (notifications == null || notifications.size == 0) { + return false + } + for (notification in notifications) { + if (notification == null || notification.notification == null) { + continue + } + val extras: Bundle? = notification.notification.extras + if (extras == null) { + continue + } + val title: CharSequence? = extras.getCharSequence(Notification.EXTRA_TITLE) + if (title != null && expectedTitle.contentEquals(title)) { + return true + } + } + return false + } + + @JvmStatic + fun getActiveNotificationIconGrays(iconSize: Int, maxIcons: Int): ByteArray { + val service = activeService + val size = Math.max(1, Math.min(96, iconSize)) + val limit = Math.max(0, maxIcons) + if (service == null || limit == 0) { + return ByteArray(0) + } + + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while reading icons", e) + return ByteArray(0) + } catch (t: Throwable) { + Log.w(TAG, "failed to read notification icons", t) + return ByteArray(0) + } + if (notifications == null || notifications.size == 0) { + return ByteArray(0) + } + + val out = ByteArrayOutputStream(size * size * Math.min(limit, notifications.size)) + val emittedGroupKeys: MutableSet = HashSet() + var emitted = 0 + for (statusBarNotification in notifications) { + if (!shouldShowNotificationIcon(service, statusBarNotification)) { + continue + } + val dedupeGroupKey = getNotificationDedupeGroupKey(statusBarNotification!!) + if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) { + continue + } + val drawable = loadNotificationIcon(service, statusBarNotification.notification) + if (drawable == null) { + Log.i(TAG, "icon skipped (no drawable): " + statusBarNotification.packageName) + continue + } + Log.i(TAG, "icon[" + emitted + "] pkg=" + statusBarNotification.packageName + + " drawable=" + drawable.javaClass.simpleName + + " intrinsic=" + drawable.intrinsicWidth + "x" + drawable.intrinsicHeight) + appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.packageName) + if (dedupeGroupKey != null) { + emittedGroupKeys.add(dedupeGroupKey) + } + emitted += 1 + if (emitted >= limit) { + break + } + } + return out.toByteArray() + } + + /** + * Grayscale icon (iconSize*iconSize bytes) for one active notification, + * identified by its key; empty array when the notification or its icon is + * unavailable. Shares the extraction/scaling pipeline with the tray icon + * strip above. + */ + @JvmStatic + fun getNotificationIconGrayForKey(key: String?, iconSize: Int): ByteArray { + val service = activeService + val size = Math.max(1, Math.min(96, iconSize)) + if (service == null) { + return ByteArray(0) + } + val statusBarNotification = findActiveNotificationByKey(service, key) + if (statusBarNotification == null || statusBarNotification.notification == null) { + return ByteArray(0) + } + val drawable = loadNotificationIcon(service, statusBarNotification.notification) + if (drawable == null) { + return ByteArray(0) + } + val out = ByteArrayOutputStream(size * size) + appendIconGrayBytes(drawable, size, out, service, -1, statusBarNotification.packageName) + return out.toByteArray() + } + + @JvmStatic + fun getActiveNotificationsJson(maxNotifications: Int): String { + val service = activeService + val limit = Math.max(0, maxNotifications) + if (service == null || limit == 0) { + return "[]" + } + + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while reading notifications", e) + return "[]" + } catch (t: Throwable) { + Log.w(TAG, "failed to read notifications", t) + return "[]" + } + if (notifications == null || notifications.size == 0) { + return "[]" + } + + Arrays.sort(notifications, object : Comparator { + override fun compare(a: StatusBarNotification?, b: StatusBarNotification?): Int { + val left = if (a == null) 0L else a.postTime + val right = if (b == null) 0L else b.postTime + return java.lang.Long.compare(right, left) + } + }) + + val out = JSONArray() + for (statusBarNotification in notifications) { + if (out.length() >= limit) { + break + } + if (!shouldShowNotificationInList(service, statusBarNotification)) { + continue + } + try { + out.put(buildNotificationJson(service, statusBarNotification!!)) + } catch (t: Throwable) { + Log.w(TAG, "failed to serialize notification", t) + } + } + return out.toString() + } + + @JvmStatic + fun invokeNotificationAction(key: String?, actionIndex: Int): Boolean { + val service = activeService + val statusBarNotification = findActiveNotificationByKey(service, key) + if (statusBarNotification == null || statusBarNotification.notification == null) { + return false + } + val actions: Array? = statusBarNotification.notification.actions + if (actions == null || actionIndex < 0 || actionIndex >= actions.size) { + return false + } + val intent: PendingIntent? = actions[actionIndex].actionIntent + if (intent == null) { + return false + } + try { + intent.send() + return true + } catch (e: PendingIntent.CanceledException) { + Log.w(TAG, "notification action pending intent was canceled", e) + return false + } catch (t: Throwable) { + Log.w(TAG, "failed to invoke notification action", t) + return false + } + } + + @JvmStatic + fun dismissNotification(key: String?): Boolean { + val service = activeService + if (service == null || key == null || key.isEmpty()) { + return false + } + try { + service.cancelNotification(key) + return true + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while dismissing notification", e) + return false + } catch (t: Throwable) { + Log.w(TAG, "failed to dismiss notification", t) + return false + } + } + + private fun emitNotificationPosted(key: String?) { + if (key == null || key.isEmpty() || notificationListeners.isEmpty()) { + return + } + for (listener in notificationListeners) { + mainHandler.post { + try { + listener.onNotificationPosted(key) + } catch (t: Throwable) { + Log.w(TAG, "notification listener failed", t) + } + } + } + } + + private fun refreshActiveNotificationWakeKeys(service: FaceclawMediaNotificationListenerService) { + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (t: Throwable) { + Log.w(TAG, "failed to refresh active notification wake keys", t) + return + } + synchronized(activeNotificationWakeKeys) { + activeNotificationWakeKeys.clear() + if (notifications == null) { + return + } + for (statusBarNotification in notifications) { + if (shouldShowNotificationInList(service, statusBarNotification)) { + val key: String? = statusBarNotification!!.key + if (key != null && !key.isEmpty()) { + activeNotificationWakeKeys.add(key) + } + } + } + } + } + + private fun shouldEmitNotificationPosted(statusBarNotification: StatusBarNotification): Boolean { + val key: String? = statusBarNotification.key + if (key == null || key.isEmpty()) { + return true + } + + val alreadyActive: Boolean + synchronized(activeNotificationWakeKeys) { + alreadyActive = activeNotificationWakeKeys.contains(key) + activeNotificationWakeKeys.add(key) + } + return !alreadyActive || !isPersistentNotification(statusBarNotification) + } + + private fun forgetActiveNotificationWakeKey(statusBarNotification: StatusBarNotification?) { + if (statusBarNotification == null) { + return + } + val key: String? = statusBarNotification.key + if (key == null || key.isEmpty()) { + return + } + synchronized(activeNotificationWakeKeys) { + activeNotificationWakeKeys.remove(key) + } + } + + private fun isPersistentNotification(statusBarNotification: StatusBarNotification): Boolean { + val notification: Notification? = statusBarNotification.notification + if (notification == null) { + return false + } + val persistentFlags = Notification.FLAG_ONGOING_EVENT or Notification.FLAG_NO_CLEAR + return (notification.flags and persistentFlags) != 0 + } + + private fun shouldShowNotificationIcon(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification?): Boolean { + if (!shouldShowNotificationInList(service, statusBarNotification)) { + return false + } + val notification = statusBarNotification!!.notification + if ((notification.flags and Notification.FLAG_GROUP_SUMMARY) != 0) { + return false + } + return true + } + + private fun shouldShowNotificationInList(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification?): Boolean { + if (statusBarNotification == null || statusBarNotification.notification == null) { + return false + } + // Our own notifications stay out of the mirror: the foreground-service + // one is noise, and the Timers app rings on the glasses itself (its + // phone notification is for the phone), so mirroring it would stack a + // notification modal over the ringing screen. + if (service.packageName == statusBarNotification.packageName) { + return false + } + val notification = statusBarNotification.notification + if (Notification.CATEGORY_TRANSPORT == notification.category) { + return false + } + val extras: Bundle? = notification.extras + if (extras != null && extras.containsKey("android.mediaSession")) { + return false + } + + val rankingMap: NotificationListenerService.RankingMap? = service.currentRanking + if (rankingMap == null) { + return true + } + val ranking = NotificationListenerService.Ranking() + if (!rankingMap.getRanking(statusBarNotification.key, ranking)) { + return true + } + val importance = ranking.importance + return importance > NotificationManager.IMPORTANCE_MIN + } + + private fun getNotificationDedupeGroupKey(statusBarNotification: StatusBarNotification): String? { + val notification = statusBarNotification.notification + if (notification.group == null && statusBarNotification.overrideGroupKey == null) { + return null + } + val groupKey: String? = statusBarNotification.groupKey + if (groupKey == null || groupKey.isEmpty()) { + return null + } + // Group children often share the same small icon. Emit only one icon for the group. + return groupKey + } + + private fun loadNotificationIcon(service: FaceclawMediaNotificationListenerService, notification: Notification): Drawable? { + try { + val smallIcon: Icon? = notification.smallIcon + if (smallIcon != null) { + val drawable: Drawable? = smallIcon.loadDrawable(service) + if (drawable != null) { + // Status-bar small icons are alpha templates: the platform + // draws them tinted and ignores their color channels, which + // apps may fill with garbage (Discord ships noise there). + // Tint white so the shape comes from alpha alone, matching + // how the status bar renders them. + drawable.mutate() + drawable.setTint(Color.WHITE) + if (drawable is BitmapDrawable) { + drawable.setFilterBitmap(true) + } + return drawable + } + } + } catch (t: Throwable) { + Log.w(TAG, "failed to load small notification icon", t) + } + try { + // Large icons are real color images (avatars, album art); keep color. + val largeIcon: Icon? = notification.getLargeIcon() + if (largeIcon != null) { + return largeIcon.loadDrawable(service) + } + } catch (t: Throwable) { + Log.w(TAG, "failed to load large notification icon", t) + } + return null + } + + private fun appendIconGrayBytes(drawable: Drawable, size: Int, out: ByteArrayOutputStream, + service: FaceclawMediaNotificationListenerService, index: Int, packageName: String?) { + val bitmap = renderIconScaled(drawable, size) + dumpIconDebugPng(service, index, packageName, bitmap) + for (y in 0 until size) { + for (x in 0 until size) { + val color = bitmap.getPixel(x, y) + val alpha = Color.alpha(color) + val grayLinear = (0.2126 * Color.red(color) + 0.7152 * Color.green(color) + 0.0722 * Color.blue(color)) * alpha / (255.0 * 255.0) + val gray = Math.round(255.0 * Math.pow(Math.max(0.0, Math.min(1.0, grayLinear)), NOTIFICATION_ICON_GAMMA)).toInt() + out.write(gray and 0xff) + } + } + bitmap.recycle() + } + + /** + * Render a drawable at the target size with proper downscaling. Detailed + * sources (e.g. avatar bitmaps used as notification icons) are rendered at + * native resolution and reduced by repeated halving: a single filtered pass + * from, say, 126px to 24px samples too sparsely and turns fine detail into + * speckle that reads as a garbled icon. + */ + private fun renderIconScaled(drawable: Drawable, size: Int): Bitmap { + val renderW = Math.max(size, drawable.intrinsicWidth) + val renderH = Math.max(size, drawable.intrinsicHeight) + var bitmap = Bitmap.createBitmap(renderW, renderH, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + drawable.setBounds(0, 0, renderW, renderH) + drawable.draw(canvas) + while (bitmap.width >= size * 2 && bitmap.height >= size * 2) { + val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true) + bitmap.recycle() + bitmap = halved + } + if (bitmap.width != size || bitmap.height != size) { + val scaled = Bitmap.createScaledBitmap(bitmap, size, size, true) + bitmap.recycle() + bitmap = scaled + } + return bitmap + } + + /** + * Debug aid for garbled-icon reports: saves each rendered icon to + * /debug-icons/ (adb-pullable) so extraction problems can + * be told apart from downstream compositing/transmission problems. Cheap: + * runs at most once per icon-cache refresh on tiny bitmaps. + */ + private fun dumpIconDebugPng(service: FaceclawMediaNotificationListenerService, index: Int, packageName: String?, bitmap: Bitmap) { + if (index < 0) { + return + } + try { + val dir = java.io.File(service.getExternalFilesDir(null), "debug-icons") + if (!dir.exists() && !dir.mkdirs()) { + return + } + val safeName = if (packageName == null) "unknown" else packageName.replace(Regex("[^A-Za-z0-9._-]"), "_") + val file = java.io.File(dir, "icon-$index-$safeName.png") + java.io.FileOutputStream(file).use { stream -> + bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) + } + } catch (t: Throwable) { + Log.w(TAG, "failed to dump debug icon", t) + } + } + + private fun findActiveNotificationByKey(service: FaceclawMediaNotificationListenerService?, key: String?): StatusBarNotification? { + if (service == null || key == null || key.isEmpty()) { + return null + } + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (t: Throwable) { + Log.w(TAG, "failed to find active notification", t) + return null + } + if (notifications == null) { + return null + } + for (statusBarNotification in notifications) { + if (statusBarNotification != null && key == statusBarNotification.key) { + return statusBarNotification + } + } + return null + } + + @Throws(JSONException::class) + private fun buildNotificationJson(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification): JSONObject { + val notification = statusBarNotification.notification + val extras: Bundle? = notification.extras + val out = JSONObject() + out.put("key", statusBarNotification.key) + out.put("packageName", statusBarNotification.packageName) + out.put("appName", getNotificationAppName(service, statusBarNotification)) + out.put("postTime", statusBarNotification.postTime) + out.put("when", notification.`when`) + putString(out, "category", notification.category) + if (extras != null) { + putCharSequence(out, "title", firstNonEmpty( + extras.getCharSequence(Notification.EXTRA_TITLE_BIG), + extras.getCharSequence(Notification.EXTRA_TITLE) + )) + putCharSequence(out, "text", extras.getCharSequence(Notification.EXTRA_TEXT)) + putCharSequence(out, "bigText", extras.getCharSequence(Notification.EXTRA_BIG_TEXT)) + putCharSequence(out, "subText", extras.getCharSequence(Notification.EXTRA_SUB_TEXT)) + putCharSequence(out, "infoText", extras.getCharSequence(Notification.EXTRA_INFO_TEXT)) + putCharSequence(out, "summaryText", extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)) + val textLines: Array? = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES) + val lines = JSONArray() + if (textLines != null) { + for (line in textLines) { + val text = charSequenceToString(line) + if (!text.isEmpty()) { + lines.put(text) + } + } + } + out.put("lines", lines) + } else { + out.put("lines", JSONArray()) + } + + val actionsJson = JSONArray() + val actions: Array? = notification.actions + if (actions != null) { + for (index in actions.indices) { + val action = actions[index] + if (action == null) { + continue + } + val title = charSequenceToString(action.title) + if (title.isEmpty()) { + continue + } + val actionJson = JSONObject() + actionJson.put("index", index) + actionJson.put("title", title) + actionJson.put("enabled", action.actionIntent != null) + actionsJson.put(actionJson) + } + } + out.put("actions", actionsJson) + return out + } + + private fun getNotificationAppName(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification): String { + val notification = statusBarNotification.notification + val extras: Bundle? = notification.extras + if (extras != null) { + val substituteName = charSequenceToString(extras.getCharSequence(EXTRA_SUBSTITUTE_APP_NAME)) + if (!substituteName.isEmpty()) { + return substituteName + } + } + return getAppLabel(service, statusBarNotification.packageName) + } + + private fun getAppLabel(service: FaceclawMediaNotificationListenerService, packageName: String?): String { + if (packageName == null || packageName.isEmpty()) { + return "" + } + try { + val label: CharSequence? = service + .packageManager + .getApplicationLabel(service.packageManager.getApplicationInfo(packageName, 0)) + val text = charSequenceToString(label) + return if (text.isEmpty()) packageName else text + } catch (t: Throwable) { + return packageName + } + } + + @Throws(JSONException::class) + private fun putString(out: JSONObject, key: String, value: String?) { + out.put(key, value ?: "") + } + + @Throws(JSONException::class) + private fun putCharSequence(out: JSONObject, key: String, value: CharSequence?) { + out.put(key, charSequenceToString(value)) + } + + private fun firstNonEmpty(first: CharSequence?, second: CharSequence?): CharSequence? { + return if (charSequenceToString(first).isEmpty()) second else first + } + + private fun charSequenceToString(value: CharSequence?): String { + return value?.toString() ?: "" + } + } + + override fun onCreate() { + super.onCreate() + activeService = this + } + + override fun onDestroy() { + if (activeService === this) { + activeService = null + } + super.onDestroy() + } + + override fun onListenerConnected() { + activeService = this + super.onListenerConnected() + refreshActiveNotificationWakeKeys(this) + } + + override fun onListenerDisconnected() { + if (activeService === this) { + activeService = null + } + super.onListenerDisconnected() + } + + override fun onNotificationPosted(statusBarNotification: StatusBarNotification?) { + super.onNotificationPosted(statusBarNotification) + if (!shouldShowNotificationInList(this, statusBarNotification)) { + forgetActiveNotificationWakeKey(statusBarNotification) + return + } + if (shouldEmitNotificationPosted(statusBarNotification!!)) { + emitNotificationPosted(statusBarNotification.key) + } + } + + override fun onNotificationRemoved(statusBarNotification: StatusBarNotification?) { + forgetActiveNotificationWakeKey(statusBarNotification) + super.onNotificationRemoved(statusBarNotification) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.java deleted file mode 100644 index c1764d6e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.java +++ /dev/null @@ -1,187 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.MessageDigest; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.ResponseBody; - -/** - * Large-file downloader for on-phone model weights (multi-GB, from Hugging - * Face). Downloads to ".part" with HTTP Range resume across app - * restarts, verifies a pinned sha256 (hashed incrementally, including the - * resumed prefix), then renames into place. Listener callbacks are posted to - * the constructing thread's Looper, like the other Faceclaw bridges. - */ -public class FaceclawModelDownloader { - private static final String TAG = "FaceclawModelDl"; - private static final long PROGRESS_INTERVAL_MS = 500; - - private static volatile OkHttpClient sharedClient; - - private final Handler callbackHandler; - private final String url; - private final File destFile; - private final File partFile; - private final String expectedSha256; - private final long expectedTotalBytes; - private final FaceclawModelDownloaderListener listener; - - private volatile boolean cancelled; - private volatile Call call; - private Thread worker; - - public FaceclawModelDownloader(String url, String destPath, String expectedSha256, - long expectedTotalBytes, FaceclawModelDownloaderListener listener) { - if (listener == null) throw new IllegalArgumentException("listener is required"); - Looper looper = Looper.myLooper(); - this.callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - this.url = url; - this.destFile = new File(destPath); - this.partFile = new File(destPath + ".part"); - this.expectedSha256 = expectedSha256 == null ? "" : expectedSha256.toLowerCase(); - this.expectedTotalBytes = expectedTotalBytes; - this.listener = listener; - } - - private static OkHttpClient getClient() { - if (sharedClient == null) { - synchronized (FaceclawModelDownloader.class) { - if (sharedClient == null) { - sharedClient = new OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(60, TimeUnit.SECONDS) - .addInterceptor(FaceclawHttp.userAgentInterceptor()) - .build(); - } - } - } - return sharedClient; - } - - public void start() { - worker = new Thread(this::run, "FaceclawModelDl"); - worker.start(); - } - - /** Stops the download but keeps the .part file so a later start() resumes. */ - public void cancel() { - cancelled = true; - Call current = call; - if (current != null) current.cancel(); - } - - private void run() { - try { - if (destFile.exists() && destFile.length() > 0) { - post(() -> listener.onDone(destFile.getAbsolutePath())); - return; - } - destFile.getParentFile().mkdirs(); - - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - long offset = 0; - if (partFile.exists()) { - offset = hashExistingPrefix(digest); - if (cancelled) return; - } - - Request.Builder builder = new Request.Builder().url(url); - if (offset > 0) builder.header("Range", "bytes=" + offset + "-"); - call = getClient().newCall(builder.build()); - - try (Response response = call.execute()) { - ResponseBody body = response.body(); - if (!response.isSuccessful() || body == null) { - postError("Model download failed: HTTP " + response.code()); - return; - } - boolean resumed = response.code() == 206; - if (offset > 0 && !resumed) { - // Server ignored the Range header; start over. - offset = 0; - digest = MessageDigest.getInstance("SHA-256"); - } - long total = expectedTotalBytes > 0 ? expectedTotalBytes - : (body.contentLength() > 0 ? offset + body.contentLength() : -1); - - try (FileOutputStream out = new FileOutputStream(partFile, resumed); - InputStream in = body.byteStream()) { - byte[] buffer = new byte[1 << 16]; - long done = offset; - long lastProgressAt = 0; - int read; - while ((read = in.read(buffer)) != -1) { - if (cancelled) return; - out.write(buffer, 0, read); - digest.update(buffer, 0, read); - done += read; - long now = System.currentTimeMillis(); - if (now - lastProgressAt >= PROGRESS_INTERVAL_MS) { - lastProgressAt = now; - final long doneFinal = done; - final long totalFinal = total; - post(() -> listener.onProgress(doneFinal, totalFinal)); - } - } - } - } - - if (expectedTotalBytes > 0 && partFile.length() != expectedTotalBytes) { - postError("Model download ended early (" + partFile.length() + " of " - + expectedTotalBytes + " bytes); try again to resume"); - return; - } - String actualSha = FaceclawFirmwareUtil.bytesToHex(digest.digest()); - if (!expectedSha256.isEmpty() && !actualSha.equals(expectedSha256)) { - partFile.delete(); - postError("Model download was corrupted (checksum mismatch); download it again"); - return; - } - if (!partFile.renameTo(destFile)) { - postError("Could not move the downloaded model into place"); - return; - } - post(() -> listener.onDone(destFile.getAbsolutePath())); - } catch (Exception e) { - if (cancelled) return; - Log.w(TAG, "download failed", e); - postError("Model download failed: " + e); - } - } - - /** Hash the already-downloaded prefix so the final digest covers the whole file. */ - private long hashExistingPrefix(MessageDigest digest) throws IOException { - long hashed = 0; - try (FileInputStream in = new FileInputStream(partFile)) { - byte[] buffer = new byte[1 << 16]; - int read; - while ((read = in.read(buffer)) != -1) { - if (cancelled) return hashed; - digest.update(buffer, 0, read); - hashed += read; - } - } - return hashed; - } - - private void post(Runnable runnable) { - callbackHandler.post(runnable); - } - - private void postError(final String message) { - post(() -> listener.onError(message)); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.kt new file mode 100644 index 00000000..96cb52ff --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.kt @@ -0,0 +1,148 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper + +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.InputStream +import java.util.concurrent.TimeUnit + +import okhttp3.Call +import okhttp3.OkHttpClient +import okhttp3.Request + +/** + * Large-file downloader for on-phone model weights (multi-GB, from Hugging + * Face). The resume/hash/verify/rename policy is the shared ResumableDownload; + * this class supplies okhttp, java.io storage, the worker thread, and posts + * listener callbacks to the constructing thread's Looper, like the other + * Faceclaw bridges. + */ +class FaceclawModelDownloader( + url: String, + destPath: String, + expectedSha256: String?, + expectedTotalBytes: Long, + listener: FaceclawModelDownloaderListener? +) { + private val callbackHandler: Handler + private val transport = OkHttpTransport() + private val download: ResumableDownload + private var worker: Thread? = null + + init { + if (listener == null) throw IllegalArgumentException("listener is required") + val looper = Looper.myLooper() + this.callbackHandler = Handler(looper ?: Looper.getMainLooper()) + val posted = object : FaceclawModelDownloaderListener { + override fun onProgress(bytesDownloaded: Long, totalBytes: Long) { + callbackHandler.post { listener.onProgress(bytesDownloaded, totalBytes) } + } + + override fun onDone(path: String?) { + callbackHandler.post { listener.onDone(path) } + } + + override fun onError(message: String?) { + callbackHandler.post { listener.onError(message) } + } + } + download = ResumableDownload(url, File(destPath).absolutePath, expectedSha256, expectedTotalBytes, transport, FileStorage, posted) + } + + companion object { + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + if (sharedClient == null) { + synchronized(FaceclawModelDownloader::class.java) { + if (sharedClient == null) { + sharedClient = OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + } + } + return sharedClient!! + } + } + + fun start() { + val thread = Thread({ download.run() }, "FaceclawModelDl") + worker = thread + thread.start() + } + + /** Stops the download but keeps the .part file so a later start() resumes. */ + fun cancel() { + download.cancel() + transport.call?.cancel() + } + + /** One call at a time per downloader, so cancel() can abort a connect in progress. */ + private class OkHttpTransport : DownloadTransport { + @Volatile + var call: Call? = null + + override fun open(url: String, rangeFrom: Long): DownloadResponse { + val builder = Request.Builder().url(url) + if (rangeFrom > 0) builder.header("Range", "bytes=$rangeFrom-") + val call: Call = getClient().newCall(builder.build()) + this.call = call + val response = call.execute() + val body = response.body + val input: InputStream? = body?.byteStream() + return object : DownloadResponse { + override val code: Int = response.code + override val contentLength: Long = body?.contentLength() ?: -1L + + override fun read(buffer: ByteArray): Int = input?.read(buffer) ?: -1 + + override fun close() { + call.cancel() + response.close() + } + } + } + } + + private object FileStorage : DownloadStorage { + override fun length(path: String): Long = File(path).let { if (it.exists()) it.length() else -1L } + + override fun prepareParent(path: String) { + File(path).parentFile?.mkdirs() + } + + override fun openWrite(path: String, append: Boolean): FileSink { + val out = FileOutputStream(path, append) + return object : FileSink { + override fun write(bytes: ByteArray, offset: Int, length: Int) = out.write(bytes, offset, length) + + override fun close() = out.close() + } + } + + override fun readAll(path: String, consumer: (ByteArray, Int) -> Boolean): Long { + var consumed = 0L + FileInputStream(path).use { input -> + val buffer = ByteArray(1 shl 16) + while (true) { + val read = input.read(buffer) + if (read < 0) break + if (!consumer(buffer, read)) break + consumed += read + } + } + return consumed + } + + override fun rename(from: String, to: String): Boolean = File(from).renameTo(File(to)) + + override fun delete(path: String): Boolean = File(path).delete() + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java deleted file mode 100644 index b95f9061..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java +++ /dev/null @@ -1,191 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; - -/** - * Headless display target for preview-only mode (no glasses paired). Hosts - * the same SurfaceCompositor the BLE communicator uses, so the TS side can - * run its full surface/render pipeline unchanged; composited frames simply - * have nowhere to go, and the phone-mirror preview, screenshots, and GIF - * recordings read the retained composite back exactly as they do on a live - * connection. - * - * The method signatures mirror the display subset of FaceclawBleCommunicator - * (the TS DisplayTarget interface is typed against that subset). - */ -public final class FaceclawPreviewCompositor { - private final Context appContext; - private final SurfaceCompositor compositor = new SurfaceCompositor(); - private final android.os.Handler mainHandler = new android.os.Handler(android.os.Looper.getMainLooper()); - - // Notified (on the main looper, where the phone UI's JS runs) after each - // applied surface frame, standing in for the BLE path's per-frame metrics - // callback so the phone mirror refreshes promptly instead of waiting for - // its safety-net poll. - private volatile Runnable frameListener; - - // Active animated-GIF screen recording, or null when idle. Frames are - // pushed by recordScreenFrame(), which the TS side calls at each - // phone-preview flush. - private volatile GifScreenRecorder screenRecorder; - - // Worker-app isolates locate their frame target through a static, like - // FaceclawBleCommunicator.getActive(); Java statics are shared across - // isolates, unlike anything on the JS side. - private static volatile FaceclawPreviewCompositor activeInstance; - - public static FaceclawPreviewCompositor getActive() { - return activeInstance; - } - - public FaceclawPreviewCompositor(Context context) { - this.appContext = context.getApplicationContext(); - } - - /** Publish this compositor as the workers' frame target. */ - public void makeActive() { - activeInstance = this; - } - - /** Withdraw from workers (a real connection is taking over). */ - public void release() { - if (activeInstance == this) { - activeInstance = null; - } - frameListener = null; - } - - public void setFrameListener(Runnable listener) { - this.frameListener = listener; - } - - /** Set the compositor's output frame size. Call before configuring surfaces. */ - public void configureCompositorScreen(int width, int height) { - compositor.configureScreen(width, height); - } - - public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) { - compositor.configureSurface(id, x, y, width, height, zOrder, transparency); - } - - public void removeSurface(String id) { - compositor.removeSurface(id); - } - - /** Dim every surface below zOrder belowZOrder to factor256/256 (see SurfaceCompositor). */ - public void setUnderlayDim(int belowZOrder, int factor256) { - compositor.setUnderlayDim(belowZOrder, factor256); - } - - public void setSurfaceVisible(String id, boolean visible) { - compositor.setSurfaceVisible(id, visible); - } - - /** Blank (screen off) or unblank the output; retained surface state survives. */ - public void setScreenBlanked(boolean blanked) { - compositor.setBlanked(blanked); - } - - /** - * Apply an update to one compositor surface. Unlike the connected path - * there is no desired-frame store or transmit pipeline downstream; the - * preview/screenshot calls recomposite the retained state on demand, so - * applying the update is the whole job and the frame finishes here. - */ - public void submitSurfaceFrame( - java.nio.ByteBuffer pixels8bpp, - String surfaceId, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - int paintMs, - int frameId, - java.nio.ByteBuffer glyphs - ) { - compositor.applyAndComposite( - surfaceId, new AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint, - glyphs == null ? null : new AndroidByteReader(glyphs)); - FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)"); - Runnable listener = frameListener; - if (listener != null) { - mainHandler.post(listener); - } - } - - /** - * The current composited screen as a phone-UI preview bitmap, or null - * before any surface has been configured. - */ - public android.graphics.Bitmap getCompositePreviewBitmap(double brightenGamma, boolean green) { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return null; - } - return PreviewBitmapUtil.fromGray( - java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green); - } - - /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ - public String saveCompositePngScreenshot() throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - - /** - * Save the current composite cropped to the given screen rect (the region - * the shell says is actually occupied). The rect is clamped to the screen; - * a degenerate rect falls back to the full screen. - */ - public String saveCompositePngScreenshot(int cropX, int cropY, int cropWidth, int cropHeight) - throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - int x = Math.max(0, cropX); - int y = Math.max(0, cropY); - int width = Math.min(composite.width - x, cropWidth - (x - cropX)); - int height = Math.min(composite.height - y, cropHeight - (y - cropY)); - if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - byte[] cropped = new byte[width * height]; - for (int row = 0; row < height; row++) { - System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width); - } - return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height); - } - - /** Begin collecting composite frames for an animated-GIF screen recording. */ - public void startScreenRecording() { - screenRecorder = new GifScreenRecorder(); - } - - /** Capture the current composite into the active recording; no-op when idle. */ - public void recordScreenFrame() { - GifScreenRecorder recorder = screenRecorder; - if (recorder == null) { - return; - } - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return; - } - recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()); - } - - /** Finish the recording and save it as an animated GIF; returns the path or "". */ - public String stopScreenRecording() throws java.io.IOException { - GifScreenRecorder recorder = screenRecorder; - screenRecorder = null; - if (recorder == null) { - return ""; - } - return recorder.save(appContext); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt new file mode 100644 index 00000000..4f4c65fa --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt @@ -0,0 +1,203 @@ +package com.faceclaw.app + +import android.content.Context + +/** + * Headless display target for preview-only mode (no glasses paired). Hosts + * the same SurfaceCompositor the BLE communicator uses, so the TS side can + * run its full surface/render pipeline unchanged; composited frames simply + * have nowhere to go, and the phone-mirror preview, screenshots, and GIF + * recordings read the retained composite back exactly as they do on a live + * connection. + * + * The method signatures mirror the display subset of FaceclawBleCommunicator + * (the TS DisplayTarget interface is typed against that subset). + */ +class FaceclawPreviewCompositor(context: Context) { + private val appContext: Context = context.applicationContext + private val compositor = SurfaceCompositor() + private val mainHandler = android.os.Handler(android.os.Looper.getMainLooper()) + + // Notified (on the main looper, where the phone UI's JS runs) after each + // applied surface frame, standing in for the BLE path's per-frame metrics + // callback so the phone mirror refreshes promptly instead of waiting for + // its safety-net poll. + @Volatile private var frameListener: Runnable? = null + + // Active animated-GIF screen recording, or null when idle. Frames are + // pushed by recordScreenFrame(), which the TS side calls at each + // phone-preview flush. + @Volatile private var screenRecorder: GifScreenRecorder? = null + + companion object { + // Worker-app isolates locate their frame target through a static, like + // FaceclawBleCommunicator.getActive(); Java statics are shared across + // isolates, unlike anything on the JS side. + @Volatile private var activeInstance: FaceclawPreviewCompositor? = null + + @JvmStatic + fun getActive(): FaceclawPreviewCompositor? { + return activeInstance + } + } + + /** Publish this compositor as the workers' frame target. */ + fun makeActive() { + activeInstance = this + } + + /** Withdraw from workers (a real connection is taking over). */ + fun release() { + if (activeInstance === this) { + activeInstance = null + } + frameListener = null + } + + fun setFrameListener(listener: Runnable?) { + this.frameListener = listener + } + + /** Set the compositor's output frame size. Call before configuring surfaces. */ + fun configureCompositorScreen(width: Int, height: Int) { + compositor.configureScreen(width, height) + } + + fun configureSurface(id: String?, x: Int, y: Int, width: Int, height: Int, zOrder: Int, transparency: Int) { + compositor.configureSurface(id, x, y, width, height, zOrder, transparency) + } + + fun removeSurface(id: String) { + compositor.removeSurface(id) + } + + /** Dim every surface below zOrder belowZOrder to factor256/256 (see SurfaceCompositor). */ + fun submitShellScene(bytes: java.nio.ByteBuffer, paintMs: Int, frameId: Int) { + compositor.setShellScene(AndroidByteReader(bytes)) + FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)") + val listener = frameListener + if (listener != null) mainHandler.post(listener) + } + + fun setUnderlayDim(belowZOrder: Int, factor256: Int) { + compositor.setUnderlayDim(belowZOrder, factor256) + } + + fun setSurfaceVisible(id: String, visible: Boolean) { + compositor.setSurfaceVisible(id, visible) + } + + fun setSurfaceDepth(id: String, depth: Int) { + compositor.setSurfaceDepth(id, depth) + } + + /** Blank (screen off) or unblank the output; retained surface state survives. */ + fun setScreenBlanked(blanked: Boolean) { + compositor.setBlanked(blanked) + } + + /** + * Apply an update to one compositor surface. Unlike the connected path + * there is no desired-frame store or transmit pipeline downstream; the + * preview/screenshot calls recomposite the retained state on demand, so + * applying the update is the whole job and the frame finishes here. + */ + fun submitSurfaceFrame( + pixels8bpp: java.nio.ByteBuffer, + surfaceId: String, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String, + paintMs: Int, + frameId: Int, + glyphs: java.nio.ByteBuffer? + ) { + compositor.submitSurface( + surfaceId, AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint, + if (glyphs == null) null else AndroidByteReader(glyphs)) + FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)") + val listener = frameListener + if (listener != null) { + mainHandler.post(listener) + } + } + + /** + * The current composited screen as a phone-UI preview bitmap, or null + * before any surface has been configured. + */ + fun getCompositePreviewBitmap(brightenGamma: Double, green: Boolean): android.graphics.Bitmap? { + val composite = compositor.previewComposite() + if (composite == null) { + return null + } + return PreviewBitmapUtil.fromGray( + java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green) + } + + /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(): String { + val composite = compositor.previewComposite() + if (composite == null) { + return "" + } + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + + /** + * Save the current composite cropped to the given screen rect (the region + * the shell says is actually occupied). The rect is clamped to the screen; + * a degenerate rect falls back to the full screen. + */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): String { + val composite = compositor.previewComposite() + if (composite == null) { + return "" + } + val x = Math.max(0, cropX) + val y = Math.max(0, cropY) + val width = Math.min(composite.width - x, cropWidth - (x - cropX)) + val height = Math.min(composite.height - y, cropHeight - (y - cropY)) + if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + val cropped = ByteArray(width * height) + for (row in 0 until height) { + System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width) + } + return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height) + } + + /** Begin collecting composite frames for an animated-GIF screen recording. */ + fun startScreenRecording() { + screenRecorder = GifScreenRecorder() + } + + /** Capture the current composite into the active recording; no-op when idle. */ + fun recordScreenFrame() { + val recorder = screenRecorder + if (recorder == null) { + return + } + val composite = compositor.previewComposite() + if (composite == null) { + return + } + recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()) + } + + /** Finish the recording and save it as an animated GIF; returns the path or "". */ + @Throws(java.io.IOException::class) + fun stopScreenRecording(): String { + val recorder = screenRecorder + screenRecorder = null + if (recorder == null) { + return "" + } + return recorder.save(appContext) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.java deleted file mode 100644 index 4b7cd5c4..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.faceclaw.app; - -import android.app.Activity; -import android.content.Context; -import android.util.Log; - -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.tasks.OnCanceledListener; -import com.google.android.gms.tasks.OnFailureListener; -import com.google.android.gms.tasks.OnSuccessListener; -import com.google.mlkit.vision.barcode.common.Barcode; -import com.google.mlkit.vision.codescanner.GmsBarcodeScanner; -import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions; -import com.google.mlkit.vision.codescanner.GmsBarcodeScanning; - -/** - * QR scanning through Play Services' ML Kit code scanner. The scanner UI and - * the camera both live inside Play Services, so Faceclaw needs no camera - * permission of its own and no third-party scanner app — which is what the - * earlier ZXing SCAN intent needed, and no app on a stock Pixel registers for - * it. - * - * The scanner module is downloaded on demand the first time it is used (the - * manifest's com.google.mlkit.vision.DEPENDENCIES meta-data asks Play Services - * to fetch it at install time instead); a scan attempted before that finishes - * fails with a module-unavailable error rather than blocking. - * - * Callbacks fire on the main thread. - */ -public final class FaceclawQrScanner { - private static final String TAG = "FaceclawQrScanner"; - - private FaceclawQrScanner() {} - - /** Whether Play Services is present and current enough to scan. */ - public static boolean isAvailable(Context context) { - try { - int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context); - return status == ConnectionResult.SUCCESS; - } catch (Throwable error) { - Log.w(TAG, "Play Services availability check failed", error); - return false; - } - } - - /** Open the scanner UI; the listener gets the decoded text, or a cancel/error. */ - public static void scan(Activity activity, final FaceclawQrScannerListener listener) { - GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder() - .setBarcodeFormats(Barcode.FORMAT_QR_CODE) - .enableAutoZoom() - .build(); - GmsBarcodeScanner scanner = GmsBarcodeScanning.getClient(activity, options); - scanner.startScan() - .addOnSuccessListener(new OnSuccessListener() { - @Override - public void onSuccess(Barcode barcode) { - String value = barcode == null ? null : barcode.getRawValue(); - if (value == null) { - listener.onError("The code held no text."); - } else { - listener.onResult(value); - } - } - }) - .addOnCanceledListener(new OnCanceledListener() { - @Override - public void onCanceled() { - listener.onCancelled(); - } - }) - .addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(Exception error) { - Log.w(TAG, "scan failed", error); - String message = error.getMessage(); - listener.onError(message == null ? error.toString() : message); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.kt new file mode 100644 index 00000000..a3857f44 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.kt @@ -0,0 +1,74 @@ +package com.faceclaw.app + +import android.app.Activity +import android.content.Context +import android.util.Log + +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailability +import com.google.android.gms.tasks.OnCanceledListener +import com.google.android.gms.tasks.OnFailureListener +import com.google.android.gms.tasks.OnSuccessListener +import com.google.mlkit.vision.barcode.common.Barcode +import com.google.mlkit.vision.codescanner.GmsBarcodeScanner +import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions +import com.google.mlkit.vision.codescanner.GmsBarcodeScanning + +/** + * QR scanning through Play Services' ML Kit code scanner. The scanner UI and + * the camera both live inside Play Services, so Faceclaw needs no camera + * permission of its own and no third-party scanner app — which is what the + * earlier ZXing SCAN intent needed, and no app on a stock Pixel registers for + * it. + * + * The scanner module is downloaded on demand the first time it is used (the + * manifest's com.google.mlkit.vision.DEPENDENCIES meta-data asks Play Services + * to fetch it at install time instead); a scan attempted before that finishes + * fails with a module-unavailable error rather than blocking. + * + * Callbacks fire on the main thread. + */ +class FaceclawQrScanner private constructor() { + companion object { + private const val TAG = "FaceclawQrScanner" + + /** Whether Play Services is present and current enough to scan. */ + @JvmStatic + fun isAvailable(context: Context): Boolean { + try { + val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) + return status == ConnectionResult.SUCCESS + } catch (error: Throwable) { + Log.w(TAG, "Play Services availability check failed", error) + return false + } + } + + /** Open the scanner UI; the listener gets the decoded text, or a cancel/error. */ + @JvmStatic + fun scan(activity: Activity, listener: FaceclawQrScannerListener) { + val options = GmsBarcodeScannerOptions.Builder() + .setBarcodeFormats(Barcode.FORMAT_QR_CODE) + .enableAutoZoom() + .build() + val scanner: GmsBarcodeScanner = GmsBarcodeScanning.getClient(activity, options) + scanner.startScan() + .addOnSuccessListener(OnSuccessListener { barcode -> + val value = barcode?.rawValue + if (value == null) { + listener.onError("The code held no text.") + } else { + listener.onResult(value) + } + }) + .addOnCanceledListener(OnCanceledListener { + listener.onCancelled() + }) + .addOnFailureListener(OnFailureListener { error -> + Log.w(TAG, "scan failed", error) + val message = error.message + listener.onError(message ?: error.toString()) + }) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.java deleted file mode 100644 index 1b602978..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.util.Collections; -import org.json.JSONArray; -import java.net.ServerSocket; -import java.net.Socket; -import java.nio.charset.StandardCharsets; -import java.nio.ByteBuffer; -import java.security.MessageDigest; -import java.security.SecureRandom; -import org.json.JSONObject; - -/** Explicit-address, bounded, one-request-per-connection input port. JS polls on its own thread. */ -public final class FaceclawRemoteInput { - private static final FaceclawRemoteInput INSTANCE = new FaceclawRemoteInput(); - public static FaceclawRemoteInput getInstance() { return INSTANCE; } - private ServerSocket server; - private Socket client; - private String pending, reply; - private long nextId, requestId; - private long deadline; - - public synchronized String start(int port) { return startAddress(port, "127.0.0.1"); } - public synchronized String startAddress(int port, String address) { - if (server != null) return ""; - try { - // Numeric local addresses only; never resolve hostnames or bind a wildcard. - if (!address.matches("[0-9.]+|[0-9a-fA-F]*:[0-9a-fA-F:.]*(%[a-zA-Z0-9_.-]+)?")) return "Invalid local address."; - InetAddress local = InetAddress.getByName(address); - if (local.isAnyLocalAddress() || local.isMulticastAddress() || NetworkInterface.getByInetAddress(local) == null) - return "Address is not assigned to a local interface."; - ServerSocket listener = new ServerSocket(port, 8, local); - server = listener; - Thread thread = new Thread(() -> serve(listener), "FaceclawInput"); - thread.setDaemon(true); - thread.start(); - return ""; - } catch (Exception e) { return "Could not open local input port " + port + "."; } - } - public synchronized void stop() { - try { if (server != null) server.close(); } catch (Exception ignored) {} - try { if (client != null) client.close(); } catch (Exception ignored) {} - server = null; client = null; pending = null; requestId = 0; reply = null; - notifyAll(); - } - public synchronized String nextRequest() { - if (System.currentTimeMillis() >= deadline) { pending = null; return null; } - String result = pending; pending = null; return result; - } - public synchronized void complete(long id, String value) { - if (id == requestId && System.currentTimeMillis() < deadline) { reply = value; notifyAll(); } - } - private void serve(ServerSocket listener) { - while (!listener.isClosed()) { - try (Socket socket = listener.accept()) { - synchronized (this) { if (server != listener) return; client = socket; } - socket.setSoTimeout(5000); - socket.setTcpNoDelay(true); - long readDeadline = System.currentTimeMillis() + 5000; - ByteArrayOutputStream body = new ByteArrayOutputStream(); - int b; - for (;;) { - long remaining = readDeadline - System.currentTimeMillis(); - if (remaining <= 0) throw new Exception("Read timeout"); - socket.setSoTimeout((int) remaining); - b = socket.getInputStream().read(); - if (b == -1 || b == '\n') break; - if (body.size() >= 65536) throw new Exception("Invalid frame"); - body.write(b); - } - if (b != '\n') continue; - String text = StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(body.toByteArray())).toString(); - String response; - synchronized (this) { - if (server != listener) return; - requestId = ++nextId; - deadline = System.currentTimeMillis() + 5000; - reply = null; - pending = new JSONObject().put("id", requestId).put("expiresAt", deadline) - .put("body", text).toString(); - while (reply == null && server == listener && System.currentTimeMillis() < deadline) { - wait(Math.max(1, deadline - System.currentTimeMillis())); - } - response = reply != null ? reply : "{\"ok\":false,\"error\":\"timeout\",\"message\":\"Faceclaw did not respond in time.\"}"; - if (server == listener) { pending = null; reply = null; requestId = 0; } - } - socket.getOutputStream().write((response + "\n").getBytes(StandardCharsets.UTF_8)); - } catch (Exception ignored) { - // Never log request bodies: they contain credentials and possibly private text. - } finally { - synchronized (this) { if (server == listener) client = null; } - } - } - } - public static String interfaces() { - JSONArray result = new JSONArray(); - try { - for (NetworkInterface device : Collections.list(NetworkInterface.getNetworkInterfaces())) { - if (!device.isUp()) continue; - for (InetAddress address : Collections.list(device.getInetAddresses())) { - if (address.isLoopbackAddress() || address.isLinkLocalAddress() || address.isAnyLocalAddress()) continue; - result.put(new JSONObject().put("name", device.getName()).put("address", address.getHostAddress()) - .put("pointToPoint", device.isPointToPoint())); - } - } - } catch (Exception ignored) {} - return result.toString(); - } - public String randomSecret() { - byte[] bytes = new byte[32]; new SecureRandom().nextBytes(bytes); return hex(bytes); - } - public String hash(String value) { - try { return hex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8))); } - catch (Exception e) { throw new IllegalStateException("SHA-256 unavailable", e); } - } - private static String hex(byte[] bytes) { - StringBuilder result = new StringBuilder(bytes.length * 2); - for (byte b : bytes) result.append(String.format(java.util.Locale.ROOT, "%02x", b & 255)); - return result.toString(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.kt new file mode 100644 index 00000000..10203b7c --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.kt @@ -0,0 +1,112 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import java.io.IOException +import java.net.InetAddress +import java.net.NetworkInterface +import java.net.ServerSocket +import java.net.Socket +import java.security.SecureRandom +import java.util.Collections + +/** + * Explicit-address, bounded input port. The request state machine lives in the shared + * [RemoteInputSession]; this adapter supplies java.net sockets, SecureRandom and main-thread + * delivery, and keeps the JVM API TypeScript calls (app/native/remote-input.ts). + */ +class FaceclawRemoteInput { + companion object { + private val INSTANCE = FaceclawRemoteInput() + @JvmStatic fun getInstance(): FaceclawRemoteInput { return INSTANCE } + + @JvmStatic + fun interfaces(): String { + val all = ArrayList() + try { + for (device in Collections.list(NetworkInterface.getNetworkInterfaces())) { + for (address in Collections.list(device.inetAddresses)) { + all.add(InterfaceAddress( + interfaceName = device.name, + address = address.hostAddress ?: continue, + up = device.isUp, + pointToPoint = device.isPointToPoint, + loopback = address.isLoopbackAddress, + linkLocal = address.isLinkLocalAddress, + anyLocal = address.isAnyLocalAddress, + )) + } + } + } catch (ignored: Exception) {} + return RemoteInputSession.interfacesJson(all) + } + } + + private val mainHandler = Handler(Looper.getMainLooper()) + private val session = RemoteInputSession( + port = AndroidLocalServerPort, + random = object : RandomSource { + private val secureRandom = SecureRandom() + override fun nextBytes(count: Int): ByteArray = ByteArray(count).also { secureRandom.nextBytes(it) } + }, + dispatch = { action -> mainHandler.post(action) }, + platform = AndroidProtocolPlatform, + ) + + fun setRequestListener(listener: Runnable?) { session.setRequestListener(listener?.let { { it.run() } }) } + + fun start(port: Int): String { return startAddress(port, "127.0.0.1") } + fun startAddress(port: Int, address: String): String = session.start(port, address) + fun stop() = session.stop() + fun nextRequest(): String? = session.nextRequest() + fun complete(id: Long, value: String?) = session.complete(id, value) + fun randomSecret(): String = session.randomSecret() + fun hash(value: String): String = RemoteInputSession.tokenHash(value) +} + +private object AndroidLocalServerPort : LocalServerPort { + override fun isLocalUnicastAddress(address: String): Boolean { + return try { + // Numeric literals only reach here, so getByName never resolves a hostname. + val local = InetAddress.getByName(address) + !(local.isAnyLocalAddress || local.isMulticastAddress || NetworkInterface.getByInetAddress(local) == null) + } catch (e: Exception) { + false + } + } + + override fun bind(address: String, port: Int): RemoteInputListener { + val server = ServerSocket(port, 8, InetAddress.getByName(address)) + return object : RemoteInputListener { + override fun accept(): RemoteInputConnection? { + val socket = try { server.accept() } catch (e: IOException) { return null } + return AndroidConnection(socket) + } + override val isClosed: Boolean get() = server.isClosed + override fun close() = server.close() + } + } +} + +private class AndroidConnection(private val socket: Socket) : RemoteInputConnection { + init { + socket.soTimeout = 5000 + socket.tcpNoDelay = true + } + + override fun readByte(timeoutMs: Long): Int { + return try { + socket.soTimeout = timeoutMs.coerceIn(1L, Int.MAX_VALUE.toLong()).toInt() + socket.getInputStream().read() + } catch (e: IOException) { + -1 + } + } + + override fun write(bytes: ByteArray) { + socket.getOutputStream().write(bytes) + socket.getOutputStream().flush() + } + + override fun close() = socket.close() +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.java deleted file mode 100644 index d2358878..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.faceclaw.app; - -import android.os.Debug; -import android.os.Process; -import android.os.SystemClock; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; - -/** Lightweight process counters for the on-glasses resource-usage monitor. */ -public final class FaceclawResourceUsage { - private FaceclawResourceUsage() {} - - /** - * Returns elapsedMs, processCpuMs, rssKb, nativeHeapKb, javaHeapKb, - * javaHeapCommittedKb, and threadCount. Reading /proc/self/status avoids - * the considerably heavier full Debug.MemoryInfo/PSS collection once per - * second while still reporting the RSS that Android records at exit. - */ - public static long[] sample() { - long[] status = readProcessStatus(); - Runtime runtime = Runtime.getRuntime(); - long javaUsedBytes = runtime.totalMemory() - runtime.freeMemory(); - return new long[] { - SystemClock.elapsedRealtime(), - Process.getElapsedCpuTime(), - status[0], - Debug.getNativeHeapAllocatedSize() / 1024L, - javaUsedBytes / 1024L, - runtime.totalMemory() / 1024L, - status[1], - }; - } - - /** Returns {VmRSS in KiB, thread count}; unavailable values are zero. */ - private static long[] readProcessStatus() { - long rssKb = 0; - long threadCount = 0; - try (BufferedReader reader = new BufferedReader(new FileReader("/proc/self/status"))) { - String line; - while ((line = reader.readLine()) != null) { - if (line.startsWith("VmRSS:")) { - rssKb = firstNumber(line); - } else if (line.startsWith("Threads:")) { - threadCount = firstNumber(line); - } - if (rssKb > 0 && threadCount > 0) break; - } - } catch (IOException ignored) { - // The graph remains useful with the runtime heap counters alone. - } - return new long[] { rssKb, threadCount }; - } - - private static long firstNumber(String line) { - int start = 0; - while (start < line.length() && !Character.isDigit(line.charAt(start))) start++; - int end = start; - while (end < line.length() && Character.isDigit(line.charAt(end))) end++; - if (start == end) return 0; - try { - return Long.parseLong(line.substring(start, end)); - } catch (NumberFormatException ignored) { - return 0; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.kt new file mode 100644 index 00000000..c5a94b4c --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.kt @@ -0,0 +1,72 @@ +package com.faceclaw.app + +import android.os.Debug +import android.os.Process +import android.os.SystemClock + +import java.io.BufferedReader +import java.io.FileReader +import java.io.IOException + +/** Lightweight process counters for the on-glasses resource-usage monitor. */ +class FaceclawResourceUsage private constructor() { + companion object { + /** + * Returns elapsedMs, processCpuMs, rssKb, nativeHeapKb, javaHeapKb, + * javaHeapCommittedKb, and threadCount. Reading /proc/self/status avoids + * the considerably heavier full Debug.MemoryInfo/PSS collection once per + * second while still reporting the RSS that Android records at exit. + */ + @JvmStatic + fun sample(): LongArray { + val status = readProcessStatus() + val runtime = Runtime.getRuntime() + val javaUsedBytes = runtime.totalMemory() - runtime.freeMemory() + return longArrayOf( + SystemClock.elapsedRealtime(), + Process.getElapsedCpuTime(), + status[0], + Debug.getNativeHeapAllocatedSize() / 1024L, + javaUsedBytes / 1024L, + runtime.totalMemory() / 1024L, + status[1], + ) + } + + /** Returns {VmRSS in KiB, thread count}; unavailable values are zero. */ + private fun readProcessStatus(): LongArray { + var rssKb = 0L + var threadCount = 0L + try { + BufferedReader(FileReader("/proc/self/status")).use { reader -> + var line: String? + while (reader.readLine().also { line = it } != null) { + val current = line!! + if (current.startsWith("VmRSS:")) { + rssKb = firstNumber(current) + } else if (current.startsWith("Threads:")) { + threadCount = firstNumber(current) + } + if (rssKb > 0 && threadCount > 0) break + } + } + } catch (ignored: IOException) { + // The graph remains useful with the runtime heap counters alone. + } + return longArrayOf(rssKb, threadCount) + } + + private fun firstNumber(line: String): Long { + var start = 0 + while (start < line.length && !Character.isDigit(line[start])) start++ + var end = start + while (end < line.length && Character.isDigit(line[end])) end++ + if (start == end) return 0 + return try { + line.substring(start, end).toLong() + } catch (ignored: NumberFormatException) { + 0 + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.java deleted file mode 100644 index bec8c494..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.content.SharedPreferences; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * App settings store shared by every JS isolate (main thread and app - * workers). Values live in a dedicated SharedPreferences file, distinct from - * NativeScript's ApplicationSettings file, so this store owns its keys - * outright (the old TS-side settings were deliberately abandoned, not - * migrated). - * - * Change notifications: each isolate registers one listener from its own - * thread. The registering thread's Looper is captured, and notifications are - * posted through it so the JS callback always runs on the isolate's own - * thread (calling into an isolate from a foreign thread is not allowed). - * NativeScript worker threads run a message loop, so both the main thread - * and workers have a Looper; a listener registered from a Looper-less thread - * is accepted but never notified (it can still read fresh values on demand). - */ -public final class FaceclawSettings { - private static final String TAG = "FaceclawSettings"; - private static final String PREFS_NAME = "faceclaw_settings"; - private static volatile FaceclawSettings instance; - - private final SharedPreferences prefs; - private final CopyOnWriteArrayList listeners = new CopyOnWriteArrayList<>(); - - private static final class ListenerEntry { - final FaceclawSettingsListener listener; - final Handler handler; - - ListenerEntry(FaceclawSettingsListener listener, Handler handler) { - this.listener = listener; - this.handler = handler; - } - } - - private FaceclawSettings(Context context) { - this.prefs = context.getApplicationContext() - .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - } - - /** Initialize (idempotent) and return the singleton. */ - public static FaceclawSettings getInstance(Context context) { - if (instance == null) { - synchronized (FaceclawSettings.class) { - if (instance == null) { - instance = new FaceclawSettings(context); - } - } - } - return instance; - } - - /** Return the singleton; the main isolate must have initialized it first. */ - public static FaceclawSettings getInstance() { - FaceclawSettings result = instance; - if (result == null) { - throw new IllegalStateException("FaceclawSettings not initialized; call getInstance(context) first"); - } - return result; - } - - public String getString(String key, String defaultValue) { - return prefs.getString(key, defaultValue); - } - - public void setString(String key, String value) { - prefs.edit().putString(key, value).apply(); - notifyChanged(key); - } - - public boolean getBoolean(String key, boolean defaultValue) { - return prefs.getBoolean(key, defaultValue); - } - - public void setBoolean(String key, boolean value) { - prefs.edit().putBoolean(key, value).apply(); - notifyChanged(key); - } - - /** - * Register a change listener. Must be called from the thread whose - * isolate owns the listener; that thread's Looper is captured for - * dispatch. - */ - public void registerListener(FaceclawSettingsListener listener) { - Looper looper = Looper.myLooper(); - if (looper == null) { - Log.w(TAG, "settings listener registered from a Looper-less thread; it will never be notified"); - } - listeners.add(new ListenerEntry(listener, looper != null ? new Handler(looper) : null)); - } - - public void unregisterListener(FaceclawSettingsListener listener) { - for (ListenerEntry entry : listeners) { - if (entry.listener == listener) { - listeners.remove(entry); - } - } - } - - private void notifyChanged(String key) { - for (ListenerEntry entry : listeners) { - if (entry.handler == null) continue; - entry.handler.post(() -> { - try { - entry.listener.onSettingChanged(key); - } catch (Exception e) { - Log.w(TAG, "settings listener failed for key " + key, e); - } - }); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.kt new file mode 100644 index 00000000..6a6b1450 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.content.Context +import android.content.SharedPreferences +import android.os.Handler +import android.os.Looper + +/** + * App settings store shared by every JS isolate (main thread and app + * workers). Values live in a dedicated SharedPreferences file, distinct from + * NativeScript's ApplicationSettings file, so this store owns its keys + * outright (the old TS-side settings were deliberately abandoned, not + * migrated). + * + * Change notifications: each isolate registers one listener from its own + * thread. The registering thread's Looper is captured, and notifications are + * posted through it so the JS callback always runs on the isolate's own + * thread (calling into an isolate from a foreign thread is not allowed). + * NativeScript worker threads run a message loop, so both the main thread + * and workers have a Looper; a listener registered from a Looper-less thread + * is accepted but never notified (it can still read fresh values on demand). + * The reads/writes and the fan-out live in the shared SettingsChangeHub. + */ +class FaceclawSettings private constructor(context: Context) { + private val prefs: SharedPreferences = context.applicationContext + .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + private val hub = SettingsChangeHub(object : SettingsStorage { + override fun getString(key: String?, defaultValue: String?): String? = prefs.getString(key, defaultValue) + + override fun putString(key: String?, value: String?) { + prefs.edit().putString(key, value).apply() + } + + override fun getBoolean(key: String?, defaultValue: Boolean): Boolean = prefs.getBoolean(key, defaultValue) + + override fun putBoolean(key: String?, value: Boolean) { + prefs.edit().putBoolean(key, value).apply() + } + }) + + companion object { + private const val PREFS_NAME = "faceclaw_settings" + @Volatile + private var instance: FaceclawSettings? = null + + /** Initialize (idempotent) and return the singleton. */ + @JvmStatic + fun getInstance(context: Context): FaceclawSettings { + if (instance == null) { + synchronized(FaceclawSettings::class.java) { + if (instance == null) { + instance = FaceclawSettings(context) + } + } + } + return instance!! + } + + /** Return the singleton; the main isolate must have initialized it first. */ + @JvmStatic + fun getInstance(): FaceclawSettings { + val result = instance + ?: throw IllegalStateException("FaceclawSettings not initialized; call getInstance(context) first") + return result + } + } + + fun getString(key: String?, defaultValue: String?): String? = hub.getString(key, defaultValue) + + fun setString(key: String?, value: String?) = hub.setString(key, value) + + fun getBoolean(key: String?, defaultValue: Boolean): Boolean = hub.getBoolean(key, defaultValue) + + fun setBoolean(key: String?, value: Boolean) = hub.setBoolean(key, value) + + /** + * Register a change listener. Must be called from the thread whose + * isolate owns the listener; that thread's Looper is captured for + * dispatch. + */ + fun registerListener(listener: FaceclawSettingsListener) { + val looper = Looper.myLooper() + val handler = if (looper != null) Handler(looper) else null + hub.register(listener, if (handler != null) SettingsDispatcher { action -> handler.post(action) } else null) + } + + fun unregisterListener(listener: FaceclawSettingsListener?) = hub.unregister(listener) +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.java deleted file mode 100644 index 90becbef..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.faceclaw.app; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; -import android.util.Xml; - -import org.xmlpull.v1.XmlPullParser; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * adb-triggered export/import of the faceclaw_settings SharedPreferences - * file, so configuration (API keys, device addresses, ...) can be moved off a - * release build, where run-as is unavailable. See scripts/pull_config.sh and - * scripts/push_config.sh, which use run-as on debuggable builds and fall back - * to this receiver otherwise. - * - * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ - * -a com.faceclaw.app.SETTINGS_EXPORT - * adb pull /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-export.xml - * - * adb push settings.xml /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-import.xml - * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ - * -a com.faceclaw.app.SETTINGS_IMPORT - * - * Security: the settings file contains API tokens, so the manifest guards - * this receiver with android.permission.DUMP — a development permission the - * adb shell holds but ordinary apps cannot obtain without adb's help, keeping - * the trigger reachable from adb only. The transfer file lives in - * getExternalFilesDir(), which other apps cannot read on Android 11+ (and - * which the scripts delete as soon as the transfer completes); on the - * Android 7-10 devices minSdk still admits, apps holding READ_EXTERNAL_STORAGE - * could read it during that window. - */ -public class FaceclawSettingsPortReceiver extends BroadcastReceiver { - private static final String TAG = "FaceclawSettingsPort"; - public static final String ACTION_EXPORT = "com.faceclaw.app.SETTINGS_EXPORT"; - public static final String ACTION_IMPORT = "com.faceclaw.app.SETTINGS_IMPORT"; - private static final String PREFS_FILE = "faceclaw_settings.xml"; - private static final String EXPORT_NAME = "faceclaw-settings-export.xml"; - private static final String IMPORT_NAME = "faceclaw-settings-import.xml"; - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent == null ? null : intent.getAction(); - try { - if (ACTION_EXPORT.equals(action)) { - setResultData(exportSettings(context)); - } else if (ACTION_IMPORT.equals(action)) { - setResultData(importSettings(context)); - // Exit once the broadcast result has been delivered: this - // process may hold stale in-memory SharedPreferences that a - // later apply() would flush over the imported file, and the - // next launch must re-read from disk. - new Handler(Looper.getMainLooper()).postDelayed(() -> { - Log.i(TAG, "Exiting after settings import"); - System.exit(0); - }, 500); - } - } catch (Exception e) { - Log.e(TAG, "Settings " + action + " failed", e); - setResultData("error: " + e); - } - } - - private File prefsFile(Context context) { - return new File(context.getApplicationInfo().dataDir, "shared_prefs/" + PREFS_FILE); - } - - private File externalFile(Context context, String name) throws IOException { - File dir = context.getExternalFilesDir(null); - if (dir == null) { - throw new IOException("external files dir unavailable"); - } - return new File(dir, name); - } - - private String exportSettings(Context context) throws IOException { - File prefs = prefsFile(context); - if (!prefs.exists()) { - return "error: no settings file yet (" + prefs + ")"; - } - File out = externalFile(context, EXPORT_NAME); - copy(prefs, out); - return "exported: " + out; - } - - private String importSettings(Context context) throws Exception { - File staged = externalFile(context, IMPORT_NAME); - if (!staged.exists()) { - return "error: nothing staged at " + staged; - } - validatePrefsXml(staged); - File prefs = prefsFile(context); - prefs.getParentFile().mkdirs(); - if (prefs.exists()) { - copy(prefs, new File(prefs.getPath() + ".bak")); - } - copy(staged, prefs); - staged.delete(); - return "imported: " + prefs + " (previous settings in " + PREFS_FILE + ".bak)"; - } - - /** Reject files that are not a SharedPreferences document. */ - private void validatePrefsXml(File file) throws Exception { - try (InputStream in = new FileInputStream(file)) { - XmlPullParser parser = Xml.newPullParser(); - parser.setInput(in, null); - int event = parser.next(); - while (event != XmlPullParser.START_TAG && event != XmlPullParser.END_DOCUMENT) { - event = parser.next(); - } - if (event != XmlPullParser.START_TAG || !"map".equals(parser.getName())) { - throw new IOException("not a SharedPreferences document"); - } - // Walk the rest so malformed XML is caught before it replaces - // the live settings file. - while (parser.next() != XmlPullParser.END_DOCUMENT) { } - } - } - - private void copy(File from, File to) throws IOException { - try (InputStream in = new FileInputStream(from); - OutputStream out = new FileOutputStream(to)) { - byte[] buffer = new byte[8192]; - int n; - while ((n = in.read(buffer)) > 0) { - out.write(buffer, 0, n); - } - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt new file mode 100644 index 00000000..788c93a1 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt @@ -0,0 +1,146 @@ +package com.faceclaw.app + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Handler +import android.os.Looper +import android.util.Log +import android.util.Xml + +import org.xmlpull.v1.XmlPullParser + +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream + +/** + * adb-triggered export/import of the faceclaw_settings SharedPreferences + * file, so configuration (API keys, device addresses, ...) can be moved off a + * release build, where run-as is unavailable. See scripts/pull_config.sh and + * scripts/push_config.sh, which use run-as on debuggable builds and fall back + * to this receiver otherwise. + * + * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ + * -a com.faceclaw.app.SETTINGS_EXPORT + * adb pull /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-export.xml + * + * adb push settings.xml /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-import.xml + * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ + * -a com.faceclaw.app.SETTINGS_IMPORT + * + * Security: the settings file contains API tokens, so the manifest guards + * this receiver with android.permission.DUMP — a development permission the + * adb shell holds but ordinary apps cannot obtain without adb's help, keeping + * the trigger reachable from adb only. The transfer file lives in + * getExternalFilesDir(), which other apps cannot read on Android 11+ (and + * which the scripts delete as soon as the transfer completes); on the + * Android 7-10 devices minSdk still admits, apps holding READ_EXTERNAL_STORAGE + * could read it during that window. + */ +class FaceclawSettingsPortReceiver : BroadcastReceiver() { + companion object { + private const val TAG = "FaceclawSettingsPort" + const val ACTION_EXPORT = "com.faceclaw.app.SETTINGS_EXPORT" + const val ACTION_IMPORT = "com.faceclaw.app.SETTINGS_IMPORT" + private const val PREFS_FILE = "faceclaw_settings.xml" + private const val EXPORT_NAME = "faceclaw-settings-export.xml" + private const val IMPORT_NAME = "faceclaw-settings-import.xml" + } + + override fun onReceive(context: Context, intent: Intent?) { + val action = intent?.action + try { + if (ACTION_EXPORT == action) { + resultData = exportSettings(context) + } else if (ACTION_IMPORT == action) { + resultData = importSettings(context) + // Exit once the broadcast result has been delivered: this + // process may hold stale in-memory SharedPreferences that a + // later apply() would flush over the imported file, and the + // next launch must re-read from disk. + Handler(Looper.getMainLooper()).postDelayed({ + Log.i(TAG, "Exiting after settings import") + System.exit(0) + }, 500) + } + } catch (e: Exception) { + Log.e(TAG, "Settings $action failed", e) + resultData = "error: $e" + } + } + + private fun prefsFile(context: Context): File { + return File(context.applicationInfo.dataDir, "shared_prefs/$PREFS_FILE") + } + + @Throws(IOException::class) + private fun externalFile(context: Context, name: String): File { + val dir = context.getExternalFilesDir(null) + ?: throw IOException("external files dir unavailable") + return File(dir, name) + } + + @Throws(IOException::class) + private fun exportSettings(context: Context): String { + val prefs = prefsFile(context) + if (!prefs.exists()) { + return "error: no settings file yet ($prefs)" + } + val out = externalFile(context, EXPORT_NAME) + copy(prefs, out) + return "exported: $out" + } + + @Throws(Exception::class) + private fun importSettings(context: Context): String { + val staged = externalFile(context, IMPORT_NAME) + if (!staged.exists()) { + return "error: nothing staged at $staged" + } + validatePrefsXml(staged) + val prefs = prefsFile(context) + prefs.parentFile?.mkdirs() + if (prefs.exists()) { + copy(prefs, File(prefs.path + ".bak")) + } + copy(staged, prefs) + staged.delete() + return "imported: $prefs (previous settings in $PREFS_FILE.bak)" + } + + /** Reject files that are not a SharedPreferences document. */ + @Throws(Exception::class) + private fun validatePrefsXml(file: File) { + FileInputStream(file).use { input -> + val parser = Xml.newPullParser() + parser.setInput(input, null) + var event = parser.next() + while (event != XmlPullParser.START_TAG && event != XmlPullParser.END_DOCUMENT) { + event = parser.next() + } + if (event != XmlPullParser.START_TAG || "map" != parser.name) { + throw IOException("not a SharedPreferences document") + } + // Walk the rest so malformed XML is caught before it replaces + // the live settings file. + while (parser.next() != XmlPullParser.END_DOCUMENT) { } + } + } + + @Throws(IOException::class) + private fun copy(from: File, to: File) { + FileInputStream(from).use { input: InputStream -> + FileOutputStream(to).use { out: OutputStream -> + val buffer = ByteArray(8192) + var n: Int + while (input.read(buffer).also { n = it } > 0) { + out.write(buffer, 0, n) + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.java deleted file mode 100644 index c258ad91..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import com.k2fsa.sherpa.onnx.OnlineStream; -import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractor; -import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig; - -/** - * Speaker voice-print extraction over sherpa-onnx. One embedding per - * utterance; matching/enrollment happens on the TS side (cosine similarity - * against the stored speaker profiles). - */ -public class FaceclawSpeakerId { - private static final String TAG = "FaceclawSpeakerId"; - - private SpeakerEmbeddingExtractor extractor; - private final String modelPath; - - public FaceclawSpeakerId(String modelPath) { - this.modelPath = modelPath; - } - - /** Lazily loads the model; returns false when it cannot be loaded. */ - public synchronized boolean ensureLoaded() { - if (extractor != null) { - return true; - } - try { - SpeakerEmbeddingExtractorConfig config = SpeakerEmbeddingExtractorConfig.builder() - .setModel(modelPath) - .setNumThreads(1) - .setDebug(false) - .setProvider("cpu") - .build(); - extractor = new SpeakerEmbeddingExtractor(config); - return true; - } catch (Throwable t) { - Log.w(TAG, "could not load speaker model at " + modelPath, t); - extractor = null; - return false; - } - } - - public synchronized int embeddingDim() { - return extractor == null ? 0 : extractor.getDim(); - } - - /** - * Compute an L2-normalized voice-print for one utterance of 16 kHz mono - * S16LE PCM. Returns null when the model is unavailable or the utterance - * is too short to embed (under ~0.5 s). - */ - public synchronized float[] embed(byte[] pcm16le, int sampleRate) { - if (pcm16le == null || pcm16le.length < sampleRate) { - return null; - } - if (!ensureLoaded()) { - return null; - } - int count = pcm16le.length / 2; - float[] samples = new float[count]; - for (int i = 0; i < count; i++) { - short s = (short) ((pcm16le[i * 2] & 0xff) | (pcm16le[i * 2 + 1] << 8)); - samples[i] = s / 32768.0f; - } - OnlineStream stream = null; - try { - stream = extractor.createStream(); - stream.acceptWaveform(samples, sampleRate); - stream.inputFinished(); - if (!extractor.isReady(stream)) { - return null; - } - float[] embedding = extractor.compute(stream); - return l2Normalize(embedding); - } catch (Throwable t) { - Log.w(TAG, "embedding failed", t); - return null; - } finally { - if (stream != null) { - stream.release(); - } - } - } - - private static float[] l2Normalize(float[] v) { - if (v == null) { - return null; - } - double sum = 0; - for (float x : v) { - sum += (double) x * x; - } - double norm = Math.sqrt(sum); - if (norm <= 0) { - return v; - } - for (int i = 0; i < v.length; i++) { - v[i] = (float) (v[i] / norm); - } - return v; - } - - public synchronized void close() { - if (extractor != null) { - extractor.release(); - extractor = null; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.kt new file mode 100644 index 00000000..87dd6c22 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.kt @@ -0,0 +1,97 @@ +package com.faceclaw.app + +import android.util.Log + +import com.k2fsa.sherpa.onnx.OnlineStream +import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractor +import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig + +/** + * Speaker voice-print extraction over sherpa-onnx. One embedding per + * utterance; matching/enrollment happens on the TS side (cosine similarity + * against the stored speaker profiles). + */ +class FaceclawSpeakerId(private val modelPath: String?) : SpeakerEmbedder { + companion object { + private const val TAG = "FaceclawSpeakerId" + } + + private var extractor: SpeakerEmbeddingExtractor? = null + + /** Lazily loads the model; returns false when it cannot be loaded. */ + @Synchronized + fun ensureLoaded(): Boolean { + if (extractor != null) { + return true + } + try { + val config = SpeakerEmbeddingExtractorConfig.builder() + .setModel(modelPath) + .setNumThreads(1) + .setDebug(false) + .setProvider("cpu") + .build() + extractor = SpeakerEmbeddingExtractor(config) + return true + } catch (t: Throwable) { + Log.w(TAG, "could not load speaker model at $modelPath", t) + extractor = null + return false + } + } + + @Synchronized + fun embeddingDim(): Int { + val extractor = this.extractor + return if (extractor == null) 0 else extractor.dim + } + + /** + * Compute an L2-normalized voice-print for one utterance of 16 kHz mono + * S16LE PCM. Returns null when the model is unavailable or the utterance + * is too short to embed (under ~0.5 s). + */ + @Synchronized + override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = embedNullable(pcm16le, sampleRate) + + @Synchronized + fun embedNullable(pcm16le: ByteArray?, sampleRate: Int): FloatArray? { + if (pcm16le == null || !VoicePrint.isLongEnoughToEmbed(pcm16le.size, sampleRate)) { + return null + } + if (!ensureLoaded()) { + return null + } + val extractor = this.extractor!! + val samples = AudioSegmentation.pcm16leToFloat(pcm16le) + var stream: OnlineStream? = null + try { + stream = extractor.createStream() + stream.acceptWaveform(samples, sampleRate) + stream.inputFinished() + if (!extractor.isReady(stream)) { + return null + } + val embedding = extractor.compute(stream) + return VoicePrint.l2Normalize(embedding) + } catch (t: Throwable) { + Log.w(TAG, "embedding failed", t) + return null + } finally { + if (stream != null) { + stream.release() + } + } + } + + override fun release() = close() + + @Synchronized + fun close() { + val extractor = this.extractor + if (extractor != null) { + extractor.release() + this.extractor = null + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.java deleted file mode 100644 index 5ec20fe9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.io.BufferedReader; -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; -import okhttp3.ResponseBody; - -/** - * Streaming HTTP POST for the TypeScript side, used for server-sent-events - * APIs (e.g. the Anthropic Messages API with stream=true). The response body - * is delivered line by line as it arrives, so the JS side can parse SSE - * events incrementally. Like FaceclawWebSocket, listener callbacks are posted - * to the Looper of the thread that constructed this object. - */ -public class FaceclawSseRequest { - private static final String TAG = "FaceclawSseRequest"; - private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); - private static volatile OkHttpClient sharedClient; - - private final Handler callbackHandler; - private final Call call; - private volatile boolean cancelled; - - /** - * Starts the request immediately. headers is a flat alternating - * [name, value, name, value, ...] array (okhttp Headers can't cross the - * JS bridge, and parallel arrays are easy to build with Array.create). - */ - public FaceclawSseRequest(String url, String jsonBody, String[] headers, final FaceclawSseListener listener) { - if (url == null || url.trim().isEmpty()) { - throw new IllegalArgumentException("url is required"); - } - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - Looper looper = Looper.myLooper(); - callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - Request.Builder builder = new Request.Builder() - .url(url.trim()) - .post(RequestBody.create(JSON, jsonBody == null ? "" : jsonBody)); - if (headers != null) { - for (int i = 0; i + 1 < headers.length; i += 2) { - if (headers[i] != null && !headers[i].isEmpty() && headers[i + 1] != null) { - builder.addHeader(headers[i], headers[i + 1]); - } - } - } - call = getClient().newCall(builder.build()); - call.enqueue(new Callback() { - @Override public void onFailure(Call call, IOException e) { - postFailure(listener, String.valueOf(e)); - } - - @Override public void onResponse(Call call, Response response) { - try (ResponseBody body = response.body()) { - if (!response.isSuccessful()) { - final int code = response.code(); - String errorBody = ""; - try { - errorBody = body == null ? "" : body.string(); - } catch (IOException e) { - Log.w(TAG, "error body read failed", e); - } - final String finalBody = errorBody; - post(() -> listener.onHttpError(code, finalBody)); - return; - } - if (body == null) { - post(listener::onComplete); - return; - } - BufferedReader reader = new BufferedReader(body.charStream()); - String line; - while ((line = reader.readLine()) != null) { - final String finalLine = line; - post(() -> listener.onLine(finalLine)); - } - post(listener::onComplete); - } catch (IOException e) { - postFailure(listener, String.valueOf(e)); - } - } - }); - } - - public void cancel() { - cancelled = true; - call.cancel(); - } - - private void post(Runnable runnable) { - if (cancelled) { - return; - } - callbackHandler.post(() -> { - if (cancelled) { - return; - } - try { - runnable.run(); - } catch (Throwable t) { - Log.w(TAG, "listener callback failed", t); - } - }); - } - - private void postFailure(FaceclawSseListener listener, String message) { - if (cancelled) { - return; - } - post(() -> listener.onFailure(message)); - } - - private static OkHttpClient getClient() { - OkHttpClient client = sharedClient; - if (client == null) { - synchronized (FaceclawSseRequest.class) { - if (sharedClient == null) { - // Streaming responses can pause between events (e.g. while - // the model thinks), so the read timeout is generous. The - // Anthropic API sends periodic ping events well within it. - sharedClient = new OkHttpClient.Builder() - .connectTimeout(15, TimeUnit.SECONDS) - .readTimeout(180, TimeUnit.SECONDS) - .addInterceptor(FaceclawHttp.userAgentInterceptor()) - .build(); - } - client = sharedClient; - } - } - return client; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.kt new file mode 100644 index 00000000..245ebc95 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.kt @@ -0,0 +1,179 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.io.IOException +import java.util.concurrent.TimeUnit + +import okhttp3.Call +import okhttp3.Callback +import okhttp3.MediaType +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response + +/** + * Streaming HTTP POST for the TypeScript side, used for server-sent-events + * APIs (e.g. the Anthropic Messages API with stream=true). The response body + * is delivered line by line as it arrives, so the JS side can parse SSE + * events incrementally. Like FaceclawWebSocket, listener callbacks are posted + * to the Looper of the thread that constructed this object. + * + * The line splitting, error-body and overflow policy live in the shared + * SseStream; this class only supplies okhttp as the transport and the Handler + * delivery. + */ +class FaceclawSseRequest +/** + * Starts the request immediately. headers is a flat alternating + * [name, value, name, value, ...] array (okhttp Headers can't cross the + * JS bridge, and parallel arrays are easy to build with Array.create). + */ +constructor(url: String?, jsonBody: String?, headers: Array?, listener: FaceclawSseListener?) { + private val callbackHandler: Handler + private val stream = SseStream() + private val transfer: Cancellable + @Volatile + private var cancelled = false + + init { + if (url == null || url.trim().isEmpty()) { + throw IllegalArgumentException("url is required") + } + if (listener == null) { + throw IllegalArgumentException("listener is required") + } + val looper = Looper.myLooper() + callbackHandler = Handler(looper ?: Looper.getMainLooper()) + val sink = object : ChunkSink { + override fun onStatus(code: Int) { + stream.onStatus(code) + } + + override fun onChunk(bytes: ByteArray, offset: Int, length: Int) { + stream.onChunk(bytes, offset, length) + drain(listener) + } + + override fun onComplete() { + stream.onComplete() + drain(listener) + } + + override fun onFailure(message: String) { + stream.onFailure(message) + drain(listener) + } + } + transfer = OkHttpStreaming.post(url.trim(), jsonBody ?: "", SseStream.unpackHeaders(headers), sink) + } + + fun cancel() { + cancelled = true + stream.cancel() + transfer.cancel() + } + + /** Hands every queued event to the listener on the constructing thread, in order. */ + private fun drain(listener: FaceclawSseListener) { + val events = stream.takeEvents() + if (events.isEmpty()) return + post { + for (event in events) { + if (cancelled) return@post + try { + SseStream.deliver(event, listener) + } catch (t: Throwable) { + Log.w(TAG, "listener callback failed", t) + } + } + } + } + + private fun post(runnable: Runnable) { + if (cancelled) { + return + } + callbackHandler.post { + if (cancelled) { + return@post + } + runnable.run() + } + } + + /** okhttp transport: enqueues the call and feeds the body to the sink in chunks. */ + private object OkHttpStreaming : StreamingHttp { + override fun post(url: String, body: String, headers: List>, sink: ChunkSink): Cancellable { + val builder = Request.Builder() + .url(url) + .post(body.toRequestBody(JSON)) + for ((name, value) in headers) builder.addHeader(name, value) + val call = getClient().newCall(builder.build()) + call.enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + sink.onFailure(e.toString()) + } + + override fun onResponse(call: Call, response: Response) { + try { + response.use { + sink.onStatus(response.code) + val input = response.body?.byteStream() + if (input != null) { + val buffer = ByteArray(8192) + while (true) { + val read = input.read(buffer) + if (read < 0) break + if (read > 0) sink.onChunk(buffer, 0, read) + } + } + sink.onComplete() + } + } catch (e: IOException) { + sink.onFailure(e.toString()) + } + } + }) + return object : Cancellable { + override fun cancel() = call.cancel() + } + } + } + + companion object { + private const val TAG = "FaceclawSseRequest" + private val JSON: MediaType = "application/json; charset=utf-8".toMediaType() + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + var client = sharedClient + if (client == null) { + synchronized(FaceclawSseRequest::class.java) { + if (sharedClient == null) { + // Streaming responses can pause between events (e.g. while + // the model thinks), so the read timeout is generous. The + // Anthropic API sends periodic ping events well within it. + // Redirects are not followed: provider credentials in the + // headers must never be forwarded to another endpoint (a + // 3xx surfaces to the listener as an HTTP error instead). + sharedClient = OkHttpClient.Builder() + .connectTimeout(15, TimeUnit.SECONDS) + .readTimeout(180, TimeUnit.SECONDS) + .followRedirects(false) + .followSslRedirects(false) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + client = sharedClient + } + } + return client!! + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java deleted file mode 100644 index 34209e16..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java +++ /dev/null @@ -1,193 +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; - -public final class FaceclawSystemStatusIconProvider { - private static final String TAG = "FaceclawStatusIcons"; - private static final int ICON_SIZE = 24; - - private FaceclawSystemStatusIconProvider() { - } - - public static byte[] getSystemStatusIconGrays(Context context, int iconSize) { - if (context == null) { - return new byte[0]; - } - Context appContext = context.getApplicationContext(); - int size = Math.max(1, Math.min(96, iconSize)); - ByteArrayOutputStream out = new ByteArrayOutputStream(size * size * 3); - - int wifiLevel = getWifiLevel(appContext); - if (wifiLevel >= 0) { - append(out, scaleIcon(drawWifiIcon(wifiLevel), size)); - } - - int cellLevel = getCellLevel(appContext); - if (cellLevel >= 0) { - append(out, scaleIcon(drawCellIcon(cellLevel), size)); - } - - if (isHotspotEnabled(appContext)) { - append(out, scaleIcon(drawHotspotIcon(), size)); - } - - return out.toByteArray(); - } - - private static int getWifiLevel(Context context) { - try { - ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - Network activeNetwork = connectivityManager == null ? null : connectivityManager.getActiveNetwork(); - NetworkCapabilities capabilities = activeNetwork == null ? null : connectivityManager.getNetworkCapabilities(activeNetwork); - if (capabilities == null || !capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { - return -1; - } - WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - WifiInfo info = wifiManager == null ? null : wifiManager.getConnectionInfo(); - if (info == null || !isValidRssi(info.getRssi())) { - return -1; - } - return Math.max(0, Math.min(4, WifiManager.calculateSignalLevel(info.getRssi(), 5))); - } catch (Throwable t) { - Log.w(TAG, "failed to read Wi-Fi status", t); - return -1; - } - } - - private static int getCellLevel(Context context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { - return -1; - } - try { - TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - SignalStrength signalStrength = telephonyManager == null ? null : telephonyManager.getSignalStrength(); - if (signalStrength == null) { - return -1; - } - return Math.max(0, Math.min(4, signalStrength.getLevel())); - } catch (SecurityException e) { - // 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 (Throwable t) { - Log.w(TAG, "failed to read cellular status", t); - return -1; - } - } - - private static boolean isHotspotEnabled(Context context) { - try { - WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - if (wifiManager == null) { - return false; - } - Method method = wifiManager.getClass().getDeclaredMethod("getWifiApState"); - method.setAccessible(true); - Object result = method.invoke(wifiManager); - int state = result instanceof Integer ? (Integer) result : -1; - return state == 13 || state == 12; // WIFI_AP_STATE_ENABLED / ENABLING - } catch (Throwable ignored) { - return false; - } - } - - private static boolean isValidRssi(int rssi) { - return rssi > -127 && rssi < 0; - } - - private static byte[] drawWifiIcon(int level) { - byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; - int cx = 12; - int cy = 18; - fillRect(icon, cx - 1, cy - 1, 3, 3, 230); - int[] radii = new int[] {5, 9, 13, 17}; - for (int index = 0; index < radii.length; index++) { - if (level <= index) { - continue; - } - drawWifiArc(icon, cx, cy, radii[index], 210); - } - return icon; - } - - private static void drawWifiArc(byte[] icon, int cx, int cy, int radius, int value) { - int inner = (radius - 1) * (radius - 1); - int outer = (radius + 1) * (radius + 1); - for (int y = 0; y < ICON_SIZE; y++) { - for (int x = 0; x < ICON_SIZE; x++) { - int dx = x - cx; - int dy = y - cy; - int dist = dx * dx + dy * dy; - if (dy <= 0 && dist >= inner && dist <= outer && Math.abs(dx) <= radius) { - setPixel(icon, x, y, value); - } - } - } - } - - private static byte[] drawCellIcon(int level) { - byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; - for (int bar = 0; bar < 4; bar++) { - int x = 5 + bar * 4; - int height = 5 + bar * 4; - int value = level > bar ? 220 : 55; - fillRect(icon, x, 20 - height, 3, height, value); - } - return icon; - } - - private static byte[] drawHotspotIcon() { - byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; - fillRect(icon, 11, 17, 3, 3, 230); - drawWifiArc(icon, 12, 19, 7, 210); - drawWifiArc(icon, 12, 19, 12, 210); - fillRect(icon, 6, 5, 12, 2, 170); - return icon; - } - - private static byte[] scaleIcon(byte[] source, int size) { - if (size == ICON_SIZE) { - return source; - } - byte[] scaled = new byte[size * size]; - for (int y = 0; y < size; y++) { - int sy = Math.min(ICON_SIZE - 1, (y * ICON_SIZE) / size); - for (int x = 0; x < size; x++) { - int sx = Math.min(ICON_SIZE - 1, (x * ICON_SIZE) / size); - scaled[y * size + x] = source[sy * ICON_SIZE + sx]; - } - } - return scaled; - } - - private static void fillRect(byte[] icon, int x, int y, int width, int height, int value) { - for (int row = y; row < y + height; row++) { - for (int col = x; col < x + width; col++) { - setPixel(icon, col, row, value); - } - } - } - - private static void setPixel(byte[] icon, int x, int y, int value) { - if (x < 0 || y < 0 || x >= ICON_SIZE || y >= ICON_SIZE) { - return; - } - icon[y * ICON_SIZE + x] = (byte) Math.max(0, Math.min(255, value)); - } - - private static void append(ByteArrayOutputStream out, byte[] bytes) { - out.write(bytes, 0, bytes.length); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.kt new file mode 100644 index 00000000..b5108f48 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.kt @@ -0,0 +1,113 @@ +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) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.java deleted file mode 100644 index de09ae61..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.java +++ /dev/null @@ -1,1162 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import com.k2fsa.sherpa.onnx.FeatureConfig; -import com.k2fsa.sherpa.onnx.OfflineModelConfig; -import com.k2fsa.sherpa.onnx.OfflineMoonshineModelConfig; -import com.k2fsa.sherpa.onnx.OfflineRecognizer; -import com.k2fsa.sherpa.onnx.OfflineRecognizerConfig; -import com.k2fsa.sherpa.onnx.OfflineRecognizerResult; -import com.k2fsa.sherpa.onnx.OfflineStream; -import com.k2fsa.sherpa.onnx.OfflineWhisperModelConfig; - -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.Arrays; - -public class FaceclawVoiceController { - private static final String TAG = "FaceclawVoice"; - private static final int SAMPLE_RATE = 16000; - private static final int FEATURE_DIM = 80; - private static final int MAX_AUDIO_QUEUE_PACKETS = 80; - private static final int EXPECTED_PACKET_INTERVAL_MS = 50; - private static final int LATE_PACKET_INTERVAL_MS = 90; - private static final int STATS_INTERVAL_MS = 5_000; - // Push-to-talk utterance boundaries come from the button. We re-decode the - // current audio segment in full for each live partial and emit the complete - // utterance text (REPLACE, never a delta). The sherpa Moonshine v2 decoder - // used here fails once a single input grows past roughly 9.1 seconds, so - // longer utterances are committed in model-safe segments. - private static final int TRANSCRIPT_DECODE_INTERVAL_MS = 700; - private static final int TRANSCRIPT_MIN_SAMPLES = SAMPLE_RATE / 3; - private static final int TRANSCRIPT_SEGMENT_MAX_SAMPLES = SAMPLE_RATE * 8; - // When a segment fills, cut at the quietest window within the last - // TRANSCRIPT_CUT_SEARCH_SAMPLES rather than mid-word at the 8s mark; the - // audio after the cut carries over into the next segment. - private static final int TRANSCRIPT_CUT_SEARCH_SAMPLES = SAMPLE_RATE * 2; - private static final int TRANSCRIPT_CUT_WINDOW_SAMPLES = SAMPLE_RATE * 30 / 1000; - // Glasses-mic PCM peaks around 0.1 full scale, and at that level the - // quantized Moonshine model often returns empty or garbled text. Boost - // each decode window toward this peak, with a gain cap so near-silent - // buffers aren't amplified into pure noise. - private static final float TRANSCRIPT_NORMALIZE_TARGET_PEAK = 0.9f; - private static final float TRANSCRIPT_NORMALIZE_MAX_GAIN = 30f; - private static final int TRANSCRIPT_LOG_PREVIEW_CHARS = 80; - // Model directory shared with the TS-side download flow (asr-model.ts), - // which fetches the Moonshine files here on demand; they are no longer - // bundled in the APK. - private static final String ASR_ROOT = "faceclaw-voice-asr"; - private static final String ASR_MODEL_DIR = "sherpa-onnx-moonshine-base-en-quantized-2026-02-27"; - // Model files for the retired on-phone wake-word spotter, copied to - // filesDir by earlier releases; deleted on sight to reclaim the space. - // (The wakeword is now detected by the glasses firmware itself.) - private static final String LEGACY_KWS_ROOT = "faceclaw-voice"; - private static final String[] ASR_MODEL_FILES = { - "encoder_model.ort", - "decoder_model_merged.ort", - "tokens.txt" - }; - // Second on-device model: sherpa-onnx's offline Whisper backend (base.en, - // int8-quantized -- see the model-choice note in asr-model.ts). Directory - // shared with the TS-side download flow, same convention as ASR_MODEL_DIR. - // - // On every call, sherpa-onnx (offline-recognizer-whisper-impl.h, v1.13.0) - // runs Whisper's encoder over the whole buffer plus 1000 frames (~10s) of - // zero tail padding, capped at 30s. Re-decoding on Moonshine's - // TRANSCRIPT_DECODE_INTERVAL_MS live-partial cadence would repeat that - // encode, growing with the utterance, roughly 1.4x/second while the user is - // still speaking, so onboardModelKind gates that loop off for Whisper -- - // see processRecognizer(). Whisper is also known to hallucinate text on - // near-silent input; recognizeTranscriptSegment() gates that too. - private static final String ASR_WHISPER_MODEL_DIR = "sherpa-onnx-whisper-base-en-int8"; - private static final String[] ASR_WHISPER_MODEL_FILES = { - "base.en-encoder.int8.onnx", - "base.en-decoder.int8.onnx", - "base.en-tokens.txt" - }; - // Below this peak amplitude (pre-normalization, of a full-scale +/-1.0f - // buffer) a segment is treated as silence and never reaches the Whisper - // recognizer at all, rather than risking a hallucinated non-answer. Picked - // conservatively low (well under typical mic noise floor already seen in - // this pipeline's normalization target) -- UNTESTED on real hardware, tune - // against real glasses captures rather than trusting this number. - private static final float WHISPER_SILENCE_PEAK_THRESHOLD = 0.01f; - - private enum VoiceInputMode { - ONBOARD, // on-phone transcription (Moonshine or Whisper; see onboardModelKind) - CLOUD // decode locally, emit PCM for a cloud recognizer on the TS side - } - - /** Which on-device model ONBOARD mode uses. Set via setOnboardModelKind() before start(). */ - private enum OnboardModelKind { - MOONSHINE, - WHISPER - } - - private final Context appContext; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final Object lock = new Object(); - private final Object audioQueueLock = new Object(); - private final ArrayDeque audioQueue = new ArrayDeque<>(); - private volatile FaceclawVoiceControllerListener listener; - private volatile FaceclawBleCommunicator communicator; - private Thread workerThread; - private volatile boolean started; - // Set once the worker has the glasses mic enabled for this session. - // Read and written under `lock`, so it flips with `started` atomically. - private boolean audioStarted; - // Capture from the phone's own microphone instead of the G2 over BLE - // (preview-only mode, where no glasses are connected). Latched into - // activePhoneMic at start() (under `lock`) so a mid-session setter call - // can't switch pipelines underneath the worker. - private volatile boolean usePhoneMic; - private boolean activePhoneMic; - private VoiceInputMode mode = VoiceInputMode.CLOUD; - private volatile OnboardModelKind onboardModelKind = OnboardModelKind.MOONSHINE; - private OfflineRecognizer recognizer; - private FaceclawLc3Decoder lc3Decoder; - private final float[] transcriptSamples = new float[TRANSCRIPT_SEGMENT_MAX_SAMPLES]; - private int transcriptSampleCount; - private long committedTranscriptSampleCount; - private String committedTranscript = ""; - private String currentSegmentTranscript = ""; - private long lastTranscriptDecodeAtMs; - private String lastTranscript = ""; - private volatile boolean saveRecordings; - private volatile boolean endpointing; - private final VoiceEndpointDetector endpointDetector = new VoiceEndpointDetector(); - private java.io.ByteArrayOutputStream recordingPcm; - // Speaker verification against the enrolled wearer voice-print ("my voice - // only" command gating). Configured before start(); the utterance PCM is - // buffered (capped) and verified once at session end. - private static final int VERIFY_MAX_SAMPLES = SAMPLE_RATE * 10; - private static final int VERIFY_MIN_SAMPLES = SAMPLE_RATE; - private volatile String verifySpeakerModelPath; - private volatile float[] verifyWearerEmbedding; - private volatile float verifyThreshold = 0.8f; - private short[] verifyBuffer; - private int verifyCount; - // Global mic processing (Microphones app config): spectral noise - // suppression and firmware-DoA beam gating, applied to every capture - // session that opts in (assistant push-to-talk, Transcribe, hands-free). - // The raw tap opts out — raw means raw, and the Microphones session does - // its own beam-compensated processing on that path. - private volatile boolean suppressionEnabled; - private volatile boolean beamFilterEnabled; - private volatile int beamCenterDeg; - private volatile int beamHalfWidthDeg = 180; - private FaceclawNoiseSuppressor suppressor; - private long queuedPackets; - private long queueDroppedPackets; - private long decodedSamples; - private long latePackets; - private long wrongArmPackets; - private long lastPacketArrivalMs; - private long maxInterPacketMs; - private long lastStatsAtMs; - - public FaceclawVoiceController(Context context) { - this.appContext = context.getApplicationContext(); - } - - public void setListener(FaceclawVoiceControllerListener listener) { - this.listener = listener; - } - - public void setCommunicator(FaceclawBleCommunicator communicator) { - this.communicator = communicator; - } - - /** Source the next capture from the phone microphone (no glasses paired). */ - public void setUsePhoneMic(boolean usePhoneMic) { - this.usePhoneMic = usePhoneMic; - } - - /** When true, the decoded mic PCM for each session is saved as a WAV. */ - public void setSaveRecordings(boolean saveRecordings) { - this.saveRecordings = saveRecordings; - } - - /** - * Which on-device model {@link #start}("onboard") should load: "whisper" - * selects the second on-device model (sherpa-onnx offline Whisper); any - * other value (including null/absent) keeps the existing Moonshine model, - * so callers that never call this see unchanged behavior. Must be set - * before {@link #start}; has no effect in CLOUD mode. - */ - public void setOnboardModelKind(String kind) { - this.onboardModelKind = "whisper".equals(kind) ? OnboardModelKind.WHISPER : OnboardModelKind.MOONSHINE; - } - - /** - * When true, watch the decoded PCM and fire {@code onSpeechEnd} once the - * speaker stops. Used by hands-free ("Hey Even") capture, which has no - * button release to end the utterance. Must be set before {@link #start}. - */ - public void setEndpointing(boolean endpointing) { - this.endpointing = endpointing; - } - - /** - * Verify this session's speaker against the enrolled wearer voice-print - * and report the result via onSpeakerVerified just before the final - * transcript. Must be set before {@link #start}; pass a null model path - * to disable. - */ - public void setSpeakerVerification(String speakerModelPath, float[] wearerEmbedding, float threshold) { - this.verifySpeakerModelPath = speakerModelPath; - this.verifyWearerEmbedding = wearerEmbedding; - this.verifyThreshold = threshold > 0 ? threshold : 0.8f; - } - - public void clearSpeakerVerification() { - this.verifySpeakerModelPath = null; - this.verifyWearerEmbedding = null; - } - - /** Spectral noise suppression on the decoded stream. Safe to flip mid-run. */ - public void setNoiseSuppression(boolean enabled) { - this.suppressionEnabled = enabled; - } - - /** - * Direction gating from the Sonic Radar beam: packets whose firmware - * direction-of-arrival falls outside centerDeg ± halfWidthDeg (device - * frame, 0 = straight ahead, positive right) are dropped before any - * consumer sees them. Safe to update mid-run. - */ - public void setBeamFilter(boolean enabled, int centerDeg, int halfWidthDeg) { - this.beamFilterEnabled = enabled; - this.beamCenterDeg = centerDeg; - this.beamHalfWidthDeg = Math.max(5, Math.min(180, halfWidthDeg)); - } - - private boolean withinBeam(int angleDegrees) { - int delta = angleDegrees - beamCenterDeg; - while (delta > 180) delta -= 360; - while (delta < -180) delta += 360; - return Math.abs(delta) <= beamHalfWidthDeg; - } - - private void applySuppression(short[] pcm, int count) { - try { - if (suppressor == null) { - suppressor = new FaceclawNoiseSuppressor(SAMPLE_RATE); - } - byte[] le = new byte[count * 2]; - for (int i = 0; i < count; i++) { - le[i * 2] = (byte) (pcm[i] & 0xff); - le[i * 2 + 1] = (byte) ((pcm[i] >> 8) & 0xff); - } - byte[] cleaned = suppressor.process(le); - int cleanedCount = Math.min(count, cleaned.length / 2); - for (int i = 0; i < cleanedCount; i++) { - pcm[i] = (short) ((cleaned[i * 2] & 0xff) | (cleaned[i * 2 + 1] << 8)); - } - } catch (Throwable t) { - Log.w(TAG, "noise suppression failed; passing audio through", t); - suppressionEnabled = false; - } - } - - public void start(String requestedMode) { - start(requestedMode, 0); - } - - public void start(String requestedMode, int captureId) { - synchronized (lock) { - if (started) { - emitStatus("Voice control is already listening."); - emitStopped(captureId); - return; - } - if (!usePhoneMic && (communicator == null || !communicator.isSessionReady())) { - emitStatus("Voice control needs an active G2 connection."); - emitStopped(captureId); - return; - } - mode = parseMode(requestedMode); - activePhoneMic = usePhoneMic; - started = true; - audioStarted = false; - workerThread = new Thread(() -> runLoop(captureId), "FaceclawVoiceController"); - workerThread.start(); - } - } - - /** - * Whether mic audio is actually flowing. {@link #start} only records - * intent: the enable lives in the glasses' EvenHub session, so a transport - * drop or a session suspend can leave this controller started with a - * worker that will never see another packet. Anything deciding whether to - * (re)start capture must ask this rather than assume its own bookkeeping. - */ - public boolean isCapturing() { - boolean audioUp; - boolean phoneMic; - synchronized (lock) { - if (!started) { - return false; - } - audioUp = audioStarted; - phoneMic = activePhoneMic; - } - if (!audioUp) { - // The worker is still bringing the mic up; report it as running so - // a concurrent request shares it instead of restarting it. - return true; - } - if (phoneMic) { - // AudioRecord has no session to lose the enable to; it runs until stop(). - return true; - } - FaceclawBleCommunicator currentCommunicator = communicator; - return currentCommunicator != null && currentCommunicator.isAudioCaptureActive(); - } - - public void stop() { - Thread threadToJoin; - synchronized (lock) { - if (!started) { - return; - } - started = false; - threadToJoin = workerThread; - } - stopG2Audio(); - synchronized (audioQueueLock) { - audioQueueLock.notifyAll(); - } - if (threadToJoin != null) { - threadToJoin.interrupt(); - if (Thread.currentThread() != threadToJoin) { - try { - threadToJoin.join(1500); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - } - - public void close() { - stop(); - } - - private VoiceInputMode parseMode(String requestedMode) { - if ("cloud".equals(requestedMode)) { - return VoiceInputMode.CLOUD; - } - return VoiceInputMode.ONBOARD; - } - - private void runLoop(int captureId) { - try { - deleteLegacyKwsFiles(); - VoiceInputMode currentMode = mode; - OnboardModelKind currentOnboardKind = onboardModelKind; - if (currentMode == VoiceInputMode.ONBOARD) { - File modelDir = findAsrModelDir(currentOnboardKind); - if (modelDir == null) { - emitStatus("Voice model not downloaded (see Settings > Voice)."); - return; - } - emitStatus("Loading transcription model..."); - recognizer = new OfflineRecognizer(buildRecognizerConfig(modelDir, currentOnboardKind)); - resetTranscriptState(); - lastTranscript = ""; - } - endpointDetector.reset(); - if (suppressor != null) { - suppressor.reset(); - } - recordingPcm = saveRecordings ? new java.io.ByteArrayOutputStream(SAMPLE_RATE * 2 * 4) : null; - boolean verifying = verifySpeakerModelPath != null && verifyWearerEmbedding != null; - verifyBuffer = verifying ? new short[VERIFY_MAX_SAMPLES] : null; - verifyCount = 0; - if (activePhoneMic) { - android.media.AudioRecord record = openPhoneMic(); - if (record == null) { - emitStatus("Could not start the phone microphone."); - return; - } - synchronized (lock) { - audioStarted = true; - } - emitStatus(currentMode == VoiceInputMode.CLOUD - ? "Listening (cloud)..." - : "Listening..."); - try { - processPhoneAudio(record); - } finally { - try { - record.stop(); - } catch (Throwable ignored) { - // Already stopped or never recording; release below either way. - } - record.release(); - } - } else { - lc3Decoder = new FaceclawLc3Decoder(); - if (!startG2Audio()) { - emitStatus("Could not start G2 microphone input."); - return; - } - synchronized (lock) { - audioStarted = true; - } - emitStatus(currentMode == VoiceInputMode.CLOUD - ? "Listening (cloud)..." - : "Listening..."); - processG2Audio(); - } - // Verification result must precede the final transcript so the - // TS bridge can suppress a non-wearer command before it is acted - // on (the callbacks are posted in order to the main handler). - runSpeakerVerification(); - // Button released / stop requested: emit one final full-utterance - // transcript so the UI can freeze it. - if (currentMode == VoiceInputMode.ONBOARD) { - decodeTranscript(true); - } - } catch (Throwable error) { - Log.e(TAG, "Voice control failed", error); - emitStatus("Voice control failed: " + error.getMessage()); - } finally { - stopG2Audio(); - writeRecordingIfAny(); - releaseSherpa(); - releaseLc3(); - synchronized (lock) { - started = false; - audioStarted = false; - workerThread = null; - } - emitStopped(captureId); - } - } - - private void appendRecording(short[] pcm, int count) { - java.io.ByteArrayOutputStream out = recordingPcm; - if (out == null) { - return; - } - for (int i = 0; i < count; i++) { - short s = pcm[i]; - out.write(s & 0xff); - out.write((s >> 8) & 0xff); - } - } - - /** Save the session's decoded mic PCM as a 16 kHz mono 16-bit WAV. */ - private void writeRecordingIfAny() { - java.io.ByteArrayOutputStream out = recordingPcm; - recordingPcm = null; - if (out == null || out.size() == 0) { - return; - } - try { - byte[] pcmBytes = out.toByteArray(); - java.io.File dir = new java.io.File(appContext.getExternalFilesDir(null), "voice-recordings"); - if (!dir.exists() && !dir.mkdirs()) { - Log.w(TAG, "could not create voice-recordings dir"); - return; - } - String stamp = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss-SSS", java.util.Locale.US) - .format(new java.util.Date()); - java.io.File file = new java.io.File(dir, "voice-" + stamp + ".wav"); - try (java.io.FileOutputStream fos = new java.io.FileOutputStream(file)) { - fos.write(buildWavHeader(pcmBytes.length, SAMPLE_RATE, 1, 16)); - fos.write(pcmBytes); - } - Log.i(TAG, "saved voice recording " + file.getAbsolutePath() - + " samples=" + (pcmBytes.length / 2) - + " sec=" + String.format(java.util.Locale.US, "%.2f", pcmBytes.length / 2.0 / SAMPLE_RATE)); - } catch (Throwable t) { - Log.w(TAG, "failed to save voice recording", t); - } - } - - private static byte[] buildWavHeader(int pcmBytes, int sampleRate, int channels, int bitsPerSample) { - int byteRate = sampleRate * channels * bitsPerSample / 8; - int blockAlign = channels * bitsPerSample / 8; - int dataSize = pcmBytes; - int riffSize = 36 + dataSize; - java.nio.ByteBuffer b = java.nio.ByteBuffer.allocate(44).order(java.nio.ByteOrder.LITTLE_ENDIAN); - b.put("RIFF".getBytes(StandardCharsets.US_ASCII)); - b.putInt(riffSize); - b.put("WAVE".getBytes(StandardCharsets.US_ASCII)); - b.put("fmt ".getBytes(StandardCharsets.US_ASCII)); - b.putInt(16); // PCM fmt chunk size - b.putShort((short) 1); // PCM - b.putShort((short) channels); - b.putInt(sampleRate); - b.putInt(byteRate); - b.putShort((short) blockAlign); - b.putShort((short) bitsPerSample); - b.put("data".getBytes(StandardCharsets.US_ASCII)); - b.putInt(dataSize); - return b.array(); - } - - private OfflineRecognizerConfig buildRecognizerConfig(File modelDir, OnboardModelKind kind) { - OfflineModelConfig.Builder modelConfig = OfflineModelConfig.builder() - .setNumThreads(1); - if (kind == OnboardModelKind.WHISPER) { - modelConfig - .setWhisper(OfflineWhisperModelConfig.builder() - .setEncoder(new File(modelDir, "base.en-encoder.int8.onnx").getAbsolutePath()) - .setDecoder(new File(modelDir, "base.en-decoder.int8.onnx").getAbsolutePath()) - .setLanguage("en") - .setTask("transcribe") - .build()) - .setTokens(new File(modelDir, "base.en-tokens.txt").getAbsolutePath()); - } else { - modelConfig - .setMoonshine(OfflineMoonshineModelConfig.builder() - .setEncoder(new File(modelDir, "encoder_model.ort").getAbsolutePath()) - .setMergedDecoder(new File(modelDir, "decoder_model_merged.ort").getAbsolutePath()) - .build()) - .setTokens(new File(modelDir, "tokens.txt").getAbsolutePath()); - } - return OfflineRecognizerConfig.builder() - .setFeatureConfig(FeatureConfig.builder() - .setSampleRate(SAMPLE_RATE) - .setFeatureDim(FEATURE_DIM) - .build()) - .setModelConfig(modelConfig.build()) - .build(); - } - - /** - * The on-device model directory for the given kind, populated by the - * download flow in asr-model.ts (releases before 0.5.0 copied the - * Moonshine files out of the APK directly, so upgraded installs are - * already complete for that model). Null when any expected file is - * missing, i.e. the model still needs to be downloaded. - */ - private File findAsrModelDir(OnboardModelKind kind) { - boolean whisper = kind == OnboardModelKind.WHISPER; - String dirName = whisper ? ASR_WHISPER_MODEL_DIR : ASR_MODEL_DIR; - String[] fileNames = whisper ? ASR_WHISPER_MODEL_FILES : ASR_MODEL_FILES; - File modelDir = new File(appContext.getFilesDir(), ASR_ROOT + File.separator + dirName); - for (String fileName : fileNames) { - File file = new File(modelDir, fileName); - if (!file.exists() || file.length() == 0) { - return null; - } - } - return modelDir; - } - - private void deleteLegacyKwsFiles() { - try { - deleteRecursively(new File(appContext.getFilesDir(), LEGACY_KWS_ROOT)); - } catch (Throwable t) { - Log.w(TAG, "failed to delete legacy wake-word files", t); - } - } - - private static void deleteRecursively(File file) { - if (!file.exists()) { - return; - } - File[] children = file.listFiles(); - if (children != null) { - for (File child : children) { - deleteRecursively(child); - } - } - file.delete(); - } - - private boolean startG2Audio() { - FaceclawBleCommunicator currentCommunicator = communicator; - if (currentCommunicator == null) { - return false; - } - resetAudioStats(); - synchronized (audioQueueLock) { - audioQueue.clear(); - } - return currentCommunicator.startG2AudioCapture(this::queueAudioPacket); - } - - private void processG2Audio() { - short[] pcm = new short[FaceclawLc3Decoder.SAMPLES_PER_PACKET]; - while (started && !Thread.currentThread().isInterrupted()) { - FaceclawLc3Decoder currentDecoder = lc3Decoder; - if (currentDecoder == null) { - return; - } - - AudioPacket packet = takeAudioPacket(); - if (packet == null) { - continue; - } - - int count = currentDecoder.decodePacket(packet.data, pcm); - if (count <= 0) { - maybeEmitAudioStats(false); - continue; - } - decodedSamples += count; - // Global mic processing from the Microphones app config: the - // beam filter drops packets whose firmware direction-of-arrival - // falls outside the listening wedge (isolating the aimed talker - // for every consumer, recognition included), and the spectral - // noise suppressor cleans what remains before it reaches the - // recognizer, cloud PCM, endpointing, or speaker verification. - int angleDegrees = currentDecoder.getLastAngleDegrees(); - int ssr = currentDecoder.getLastSsr(); - if (beamFilterEnabled && ssr > 0 && !withinBeam(angleDegrees)) { - emitFrameMeta(angleDegrees, ssr); - maybeEmitAudioStats(false); - continue; - } - processPcmChunk(pcm, count, angleDegrees, ssr, true); - maybeEmitAudioStats(false); - } - } - - /** - * Per-chunk processing shared by the G2 and phone-mic paths, downstream of - * decode and the beam filter. hasFrameMeta is false for the phone mic, - * which has no firmware DSP metadata to report. - */ - private void processPcmChunk(short[] pcm, int count, int angleDegrees, int ssr, boolean hasFrameMeta) { - if (suppressionEnabled) { - applySuppression(pcm, count); - } - if (recordingPcm != null) { - appendRecording(pcm, count); - } - if (verifyBuffer != null && verifyCount < VERIFY_MAX_SAMPLES) { - int copied = Math.min(count, VERIFY_MAX_SAMPLES - verifyCount); - System.arraycopy(pcm, 0, verifyBuffer, verifyCount, copied); - verifyCount += copied; - } - if (endpointing && endpointDetector.accept(pcm, count)) { - emitSpeechEnd(); - } - // PCM and frame metadata flow in every mode so levels, recording, - // and the Microphones radar keep working alongside onboard ASR. - emitPcm(pcm, count); - if (hasFrameMeta) { - emitFrameMeta(angleDegrees, ssr); - } - if (mode != VoiceInputMode.CLOUD) { - float[] samples = new float[count]; - for (int i = 0; i < count; i++) { - samples[i] = pcm[i] / 32768.0f; - } - processRecognizer(samples); - } - } - - // 50 ms chunks match the G2 packet cadence the rest of the pipeline - // (endpointing, transcript pacing) is tuned for. - private static final int PHONE_MIC_CHUNK_SAMPLES = SAMPLE_RATE / 20; - - /** - * Open the phone's own microphone at the pipeline's native format - * (16 kHz mono PCM16), or null when it cannot start — the permission is - * missing (SecurityException) or the device refuses the configuration. - */ - private android.media.AudioRecord openPhoneMic() { - android.media.AudioRecord record = null; - try { - int minBytes = android.media.AudioRecord.getMinBufferSize( - SAMPLE_RATE, - android.media.AudioFormat.CHANNEL_IN_MONO, - android.media.AudioFormat.ENCODING_PCM_16BIT); - int bufferBytes = Math.max(minBytes, PHONE_MIC_CHUNK_SAMPLES * 2 * 4); - record = new android.media.AudioRecord( - android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION, - SAMPLE_RATE, - android.media.AudioFormat.CHANNEL_IN_MONO, - android.media.AudioFormat.ENCODING_PCM_16BIT, - bufferBytes); - if (record.getState() != android.media.AudioRecord.STATE_INITIALIZED) { - record.release(); - return null; - } - record.startRecording(); - if (record.getRecordingState() != android.media.AudioRecord.RECORDSTATE_RECORDING) { - record.release(); - return null; - } - return record; - } catch (Throwable t) { - Log.w(TAG, "phone mic open failed", t); - if (record != null) { - record.release(); - } - return null; - } - } - - /** - * Phone-mic capture loop: no LC3 decode, no arm bookkeeping, no frame - * metadata — AudioRecord already delivers the pipeline's PCM format. The - * blocking read returns every chunk (50 ms), which bounds how long a - * stop() waits for the loop to notice `started` dropped. - */ - private void processPhoneAudio(android.media.AudioRecord record) { - short[] pcm = new short[PHONE_MIC_CHUNK_SAMPLES]; - while (started && !Thread.currentThread().isInterrupted()) { - int read = record.read(pcm, 0, pcm.length); - if (read < 0) { - Log.w(TAG, "phone mic read failed: " + read); - return; - } - if (read == 0) { - continue; - } - decodedSamples += read; - processPcmChunk(pcm, read, 0, 0, false); - } - } - - private void processRecognizer(float[] samples) { - appendTranscriptSamples(samples); - // Each Whisper call re-encodes the whole buffer plus ~10s of tail - // padding (see the ASR_WHISPER_* comment above), - // so it skips the live-partial redecode Moonshine does on this - // interval and only decodes when a segment commits (8s buffer fill) - // or the utterance ends (decodeTranscript(true) in runLoop()). This - // means no live preview text while speaking in Whisper mode -- status - // stays "Listening..." until release. This is a deliberate tradeoff. - if (onboardModelKind == OnboardModelKind.WHISPER) { - return; - } - long now = SystemClock.elapsedRealtime(); - if (transcriptSampleCount >= TRANSCRIPT_MIN_SAMPLES - && now - lastTranscriptDecodeAtMs >= TRANSCRIPT_DECODE_INTERVAL_MS) { - decodeTranscript(false); - lastTranscriptDecodeAtMs = now; - } - } - - private void appendTranscriptSamples(float[] samples) { - int sourceOffset = 0; - while (sourceOffset < samples.length) { - int available = TRANSCRIPT_SEGMENT_MAX_SAMPLES - transcriptSampleCount; - int count = Math.min(available, samples.length - sourceOffset); - System.arraycopy(samples, sourceOffset, transcriptSamples, transcriptSampleCount, count); - transcriptSampleCount += count; - sourceOffset += count; - - if (transcriptSampleCount == TRANSCRIPT_SEGMENT_MAX_SAMPLES) { - commitTranscriptSegment(); - } - } - } - - /** - * Decode the current model-safe segment and emit the best transcript of the - * complete utterance (REPLACE semantics — the caller displays it as-is). - */ - private void decodeTranscript(boolean isFinal) { - if (recognizer == null || transcriptSampleCount <= 0) { - if (isFinal) { - emitTranscript(lastTranscript, true); - } - return; - } - int segmentSampleCount = transcriptSampleCount; - String segmentText = recognizeTranscriptSegment(segmentSampleCount); - if (segmentText.length() > 0) { - currentSegmentTranscript = segmentText; - } else { - segmentText = currentSegmentTranscript; - } - String text = joinTranscript(committedTranscript, segmentText); - lastTranscript = text; - logTranscriptDecode(isFinal, segmentSampleCount, text); - emitTranscript(text, isFinal); - } - - /** - * Finalize a full segment before accepting more audio. This keeps every - * Moonshine invocation below its failing sequence length while retaining - * all earlier text in the replace-semantics preview. - */ - private void commitTranscriptSegment() { - int cut = findSegmentCutPoint(); - String segmentText = recognizeTranscriptSegment(cut); - if (segmentText.length() == 0) { - // Fallback text came from partial decodes of the full buffer, so it - // may include words from the carried-over tail; rare now that decode - // windows are peak-normalized. - segmentText = currentSegmentTranscript; - } - committedTranscript = joinTranscript(committedTranscript, segmentText); - currentSegmentTranscript = ""; - committedTranscriptSampleCount += cut; - int tail = transcriptSampleCount - cut; - System.arraycopy(transcriptSamples, cut, transcriptSamples, 0, tail); - transcriptSampleCount = tail; - lastTranscript = committedTranscript; - lastTranscriptDecodeAtMs = SystemClock.elapsedRealtime(); - logTranscriptDecode(false, cut, committedTranscript); - emitTranscript(committedTranscript, false); - } - - /** - * Pick where to end the committed segment: the center of the quietest - * window within the search region at the end of the buffer, so the cut - * lands between words instead of splitting one. - */ - private int findSegmentCutPoint() { - int count = transcriptSampleCount; - int searchStart = Math.max(0, count - TRANSCRIPT_CUT_SEARCH_SAMPLES); - int win = TRANSCRIPT_CUT_WINDOW_SAMPLES; - if (count - searchStart <= win) { - return count; - } - double sum = 0; - for (int i = searchStart; i < searchStart + win; i++) { - sum += (double) transcriptSamples[i] * transcriptSamples[i]; - } - double best = sum; - int bestStart = searchStart; - for (int start = searchStart + 1; start + win <= count; start++) { - float dropped = transcriptSamples[start - 1]; - float added = transcriptSamples[start + win - 1]; - sum += (double) added * added - (double) dropped * dropped; - if (sum < best) { - best = sum; - bestStart = start; - } - } - return bestStart + win / 2; - } - - private String recognizeTranscriptSegment(int sampleCount) { - OfflineRecognizer currentRecognizer = recognizer; - if (currentRecognizer == null || sampleCount <= 0) { - return ""; - } - float[] segment = Arrays.copyOf(transcriptSamples, sampleCount); - // Whisper hallucinates text on near-silent input (a known quirk of the - // model, not this pipeline); gate it on the PRE-normalization peak, since - // normalizePeak() below would otherwise amplify true silence right up to - // the target level and hide the very thing being checked for. Moonshine - // does not share this failure mode in practice, so it is left unchanged. - if (onboardModelKind == OnboardModelKind.WHISPER && peakAmplitude(segment) < WHISPER_SILENCE_PEAK_THRESHOLD) { - return ""; - } - normalizePeak(segment); - OfflineStream offlineStream = currentRecognizer.createStream(); - try { - offlineStream.acceptWaveform(segment, SAMPLE_RATE); - currentRecognizer.decode(offlineStream); - OfflineRecognizerResult result = currentRecognizer.getResult(offlineStream); - String raw = result == null ? "" : result.getText(); - return raw == null ? "" : raw.trim(); - } finally { - offlineStream.release(); - } - } - - private static float peakAmplitude(float[] samples) { - float peak = 0f; - for (float s : samples) { - float a = Math.abs(s); - if (a > peak) { - peak = a; - } - } - return peak; - } - - private static void normalizePeak(float[] samples) { - float peak = peakAmplitude(samples); - if (peak <= 0f) { - return; - } - float gain = Math.min(TRANSCRIPT_NORMALIZE_TARGET_PEAK / peak, TRANSCRIPT_NORMALIZE_MAX_GAIN); - if (gain <= 1f) { - return; - } - for (int i = 0; i < samples.length; i++) { - samples[i] *= gain; - } - } - - private void logTranscriptDecode(boolean isFinal, int segmentSampleCount, String text) { - double totalAudioSec = - (committedTranscriptSampleCount + transcriptSampleCount) / (double) SAMPLE_RATE; - String preview = text.length() <= TRANSCRIPT_LOG_PREVIEW_CHARS - ? text : text.substring(0, TRANSCRIPT_LOG_PREVIEW_CHARS) + "..."; - Log.i(TAG, (onboardModelKind == OnboardModelKind.WHISPER ? "Whisper" : "Moonshine") + " decode final=" + isFinal - + " audioSec=" + String.format(java.util.Locale.US, "%.2f", totalAudioSec) - + " segmentAudioSec=" + String.format(java.util.Locale.US, "%.2f", segmentSampleCount / (double) SAMPLE_RATE) - + " textLen=" + text.length() + " text=\"" + preview + "\""); - } - - private static String joinTranscript(String prefix, String suffix) { - if (prefix == null || prefix.length() == 0) { - return suffix == null ? "" : suffix; - } - if (suffix == null || suffix.length() == 0) { - return prefix; - } - char first = suffix.charAt(0); - boolean attachesToPrevious = ".,!?;:%)]}".indexOf(first) >= 0; - return prefix + (attachesToPrevious ? "" : " ") + suffix; - } - - private void resetTranscriptState() { - transcriptSampleCount = 0; - committedTranscriptSampleCount = 0; - committedTranscript = ""; - currentSegmentTranscript = ""; - lastTranscriptDecodeAtMs = 0; - } - - private void emitPcm(short[] pcm, int count) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null || count <= 0) { - return; - } - byte[] le = new byte[count * 2]; - for (int i = 0; i < count; i++) { - short s = pcm[i]; - le[i * 2] = (byte) (s & 0xff); - le[i * 2 + 1] = (byte) ((s >> 8) & 0xff); - } - mainHandler.post(() -> currentListener.onPcm(le)); - } - - /** - * Embed the session's buffered utterance and compare it to the enrolled - * wearer voice-print. Fails open: a session too short to verify, or a - * model that will not load, counts as the wearer rather than silencing - * every command. - */ - private void runSpeakerVerification() { - short[] buffer = verifyBuffer; - float[] wearer = verifyWearerEmbedding; - String modelPath = verifySpeakerModelPath; - verifyBuffer = null; - if (buffer == null || wearer == null || modelPath == null) { - return; - } - if (verifyCount < VERIFY_MIN_SAMPLES) { - emitSpeakerVerified(true, 0f); - return; - } - FaceclawSpeakerId speakerId = cachedSpeakerId(modelPath); - try { - byte[] le = new byte[verifyCount * 2]; - for (int i = 0; i < verifyCount; i++) { - short s = buffer[i]; - le[i * 2] = (byte) (s & 0xff); - le[i * 2 + 1] = (byte) ((s >> 8) & 0xff); - } - float[] embedding = speakerId.embed(le, SAMPLE_RATE); - if (embedding == null || embedding.length != wearer.length) { - emitSpeakerVerified(true, 0f); - return; - } - double dot = 0; - for (int i = 0; i < embedding.length; i++) { - dot += (double) embedding[i] * wearer[i]; - } - boolean isWearer = dot >= verifyThreshold; - Log.i(TAG, "speaker verification similarity=" + String.format(java.util.Locale.US, "%.3f", dot) - + " threshold=" + verifyThreshold + " isWearer=" + isWearer); - emitSpeakerVerified(isWearer, (float) dot); - } catch (Throwable t) { - Log.w(TAG, "speaker verification failed", t); - emitSpeakerVerified(true, 0f); - } - } - - // The 28 MB embedding model takes seconds to load; keep one instance - // across capture sessions so verification adds only the embed time. - private static FaceclawSpeakerId sharedSpeakerId; - private static String sharedSpeakerIdPath; - - private static synchronized FaceclawSpeakerId cachedSpeakerId(String modelPath) { - if (sharedSpeakerId == null || !modelPath.equals(sharedSpeakerIdPath)) { - if (sharedSpeakerId != null) { - sharedSpeakerId.close(); - } - sharedSpeakerId = new FaceclawSpeakerId(modelPath); - sharedSpeakerIdPath = modelPath; - } - return sharedSpeakerId; - } - - private void emitSpeakerVerified(boolean isWearer, float similarity) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onSpeakerVerified(isWearer, similarity)); - } - - private void emitFrameMeta(int angleDegrees, int ssr) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onFrameMeta(angleDegrees, ssr)); - } - - private void emitSpeechEnd() { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(currentListener::onSpeechEnd); - } - - private void emitStopped(int captureId) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener != null) { - mainHandler.post(() -> currentListener.onStopped(captureId)); - } - } - - private void stopG2Audio() { - FaceclawBleCommunicator currentCommunicator = communicator; - if (currentCommunicator != null) { - currentCommunicator.stopG2AudioCapture(); - } - maybeEmitAudioStats(true); - } - - private void releaseSherpa() { - if (recognizer != null) { - recognizer.release(); - recognizer = null; - } - } - - private void releaseLc3() { - if (lc3Decoder != null) { - lc3Decoder.close(); - lc3Decoder = null; - } - } - - private void queueAudioPacket(byte[] data, String arm, long arrivalMs) { - if (!started || data == null) { - return; - } - if (!"L".equals(arm)) { - wrongArmPackets++; - } - synchronized (audioQueueLock) { - if (audioQueue.size() >= MAX_AUDIO_QUEUE_PACKETS) { - audioQueue.removeFirst(); - queueDroppedPackets++; - } - audioQueue.addLast(new AudioPacket(data, arm, arrivalMs)); - queuedPackets++; - if (lastPacketArrivalMs > 0) { - long delta = arrivalMs - lastPacketArrivalMs; - if (delta > maxInterPacketMs) { - maxInterPacketMs = delta; - } - if (delta > LATE_PACKET_INTERVAL_MS) { - latePackets++; - } - } - lastPacketArrivalMs = arrivalMs; - audioQueueLock.notifyAll(); - } - } - - private AudioPacket takeAudioPacket() { - synchronized (audioQueueLock) { - while (started && audioQueue.isEmpty()) { - try { - audioQueueLock.wait(250); - maybeEmitAudioStats(false); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return null; - } - } - return audioQueue.pollFirst(); - } - } - - private void resetAudioStats() { - queuedPackets = 0; - queueDroppedPackets = 0; - decodedSamples = 0; - latePackets = 0; - wrongArmPackets = 0; - lastPacketArrivalMs = 0; - maxInterPacketMs = 0; - lastStatsAtMs = SystemClock.elapsedRealtime(); - } - - private void maybeEmitAudioStats(boolean force) { - long now = SystemClock.elapsedRealtime(); - if (!force && now - lastStatsAtMs < STATS_INTERVAL_MS) { - return; - } - lastStatsAtMs = now; - FaceclawLc3Decoder currentDecoder = lc3Decoder; - long real = currentDecoder == null ? 0 : currentDecoder.getRealPackets(); - long duplicate = currentDecoder == null ? 0 : currentDecoder.getDuplicatePackets(); - long missing = currentDecoder == null ? 0 : currentDecoder.getMissingPackets(); - long decodeErrors = currentDecoder == null ? 0 : currentDecoder.getDecodeErrors(); - String status = "G2 mic packets=" + queuedPackets - + " decoded=" + real - + " missing=" + missing - + " duplicate=" + duplicate - + " late=" + latePackets - + " maxGapMs=" + maxInterPacketMs - + "\n" - + " queueDrop=" + queueDroppedPackets - + " decodeErrors=" + decodeErrors - + " wrongArm=" + wrongArmPackets - + " audioSec=" + String.format(java.util.Locale.US, "%.1f", decodedSamples / (double) SAMPLE_RATE); - // Audio-pipeline stats are diagnostic; keep them in logcat only, out of - // the on-glasses voice UI. - Log.i(TAG, status.replace('\n', ' ') + " expectedIntervalMs=" + EXPECTED_PACKET_INTERVAL_MS); - } - - private void emitStatus(String status) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onStatus(status)); - } - - private void emitTranscript(String text, boolean isFinal) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - Log.i(TAG, "Emit transcript final=" + isFinal + " textLen=" + (text == null ? 0 : text.trim().length())); - mainHandler.post(() -> currentListener.onTranscript(text, isFinal)); - } - - private static final class AudioPacket { - final byte[] data; - final String arm; - final long arrivalMs; - - AudioPacket(byte[] data, String arm, long arrivalMs) { - this.data = data; - this.arm = arm == null ? "?" : arm; - this.arrivalMs = arrivalMs; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt new file mode 100644 index 00000000..ad0531bb --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt @@ -0,0 +1,285 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.io.File + +/** + * Android host for the shared [VoiceCaptureSession]: supplies the glasses packet source (the + * BLE communicator), the phone microphone, the sherpa-onnx recognizer and speaker model, model + * directory lookup, recording storage, and main-thread callback delivery. The capture pipeline + * itself (packet queue, beam gate, endpointing, segmented transcript, verification) is shared + * with iOS. + */ +class FaceclawVoiceController(context: Context) { + companion object { + private const val TAG = "FaceclawVoice" + private const val SAMPLE_RATE = 16000 + // Model directory shared with the TS-side download flow (asr-model.ts), + // which fetches the Moonshine files here on demand; they are no longer + // bundled in the APK. + private const val ASR_ROOT = "faceclaw-voice-asr" + private const val ASR_MODEL_DIR = "sherpa-onnx-moonshine-base-en-quantized-2026-02-27" + // Model files for the retired on-phone wake-word spotter, copied to + // filesDir by earlier releases; deleted on sight to reclaim the space. + // (The wakeword is now detected by the glasses firmware itself.) + private const val LEGACY_KWS_ROOT = "faceclaw-voice" + private val ASR_MODEL_FILES = arrayOf( + "encoder_model.ort", + "decoder_model_merged.ort", + "tokens.txt" + ) + // Second on-device model: sherpa-onnx's offline Whisper backend (base.en, + // int8-quantized -- see the model-choice note in asr-model.ts). Directory + // shared with the TS-side download flow, same convention as ASR_MODEL_DIR. + // Whisper re-encodes the whole buffer on every call, so the shared session + // skips live partials for it; see VoiceCaptureSession.processRecognizer. + private const val ASR_WHISPER_MODEL_DIR = "sherpa-onnx-whisper-base-en-int8" + private val ASR_WHISPER_MODEL_FILES = arrayOf( + "base.en-encoder.int8.onnx", + "base.en-decoder.int8.onnx", + "base.en-tokens.txt" + ) + // 50 ms chunks match the G2 packet cadence the rest of the pipeline + // (endpointing, transcript pacing) is tuned for. + private const val PHONE_MIC_CHUNK_SAMPLES = SAMPLE_RATE / 20 + + @JvmStatic + private fun deleteRecursively(file: File) { + if (!file.exists()) { + return + } + val children = file.listFiles() + if (children != null) { + for (child in children) { + deleteRecursively(child) + } + } + file.delete() + } + + // The 28 MB embedding model takes seconds to load; keep one instance + // across capture sessions so verification adds only the embed time. + private var sharedSpeakerId: FaceclawSpeakerId? = null + private var sharedSpeakerIdPath: String? = null + + @JvmStatic + @Synchronized + private fun cachedSpeakerId(modelPath: String): FaceclawSpeakerId { + if (sharedSpeakerId == null || modelPath != sharedSpeakerIdPath) { + if (sharedSpeakerId != null) { + sharedSpeakerId!!.close() + } + sharedSpeakerId = FaceclawSpeakerId(modelPath) + sharedSpeakerIdPath = modelPath + } + return sharedSpeakerId!! + } + } + + private val appContext: Context = context.applicationContext + private val mainHandler = Handler(Looper.getMainLooper()) + @Volatile + private var communicator: FaceclawBleCommunicator? = null + + private val host = object : VoiceCaptureHost { + override val dispatcher = CallbackDispatcher { action -> mainHandler.post { action() } } + + override fun onSessionStarting() { + deleteLegacyKwsFiles() + } + + override fun isG2SessionReady(): Boolean { + val currentCommunicator = communicator + return currentCommunicator != null && currentCommunicator.isSessionReady() + } + + override fun isG2AudioCaptureActive(): Boolean { + val currentCommunicator = communicator + return currentCommunicator != null && currentCommunicator.isAudioCaptureActive() + } + + override fun g2AudioSource(): PacketAudioSource? { + val currentCommunicator = communicator ?: return null + return object : PacketAudioSource { + override fun start(listener: FaceclawAudioPacketListener): Boolean = + currentCommunicator.startG2AudioCapture(listener) + + override fun stop() = currentCommunicator.stopG2AudioCapture() + + override fun isActive(): Boolean = currentCommunicator.isAudioCaptureActive() + } + } + + override fun createPacketDecoder(): AudioPacketDecoder = AndroidSpeechEngines.Lc3DecoderAdapter() + + override fun openPhoneMic(): PcmAudioSource? = AndroidSpeechEngines.openPhoneMic(PHONE_MIC_CHUNK_SAMPLES) + + override fun hasTranscriberModel(kind: VoiceModelKind): Boolean = findAsrModelDir(kind) != null + + override fun loadTranscriber(kind: VoiceModelKind): OfflineTranscriber { + val modelDir = findAsrModelDir(kind) ?: throw IllegalStateException("voice model missing") + return AndroidSpeechEngines.SherpaTranscriber(AndroidSpeechEngines.recognizerConfig(modelDir, kind)) + } + + override fun speakerEmbedder(modelPath: String): SpeakerEmbedder { + val cached = cachedSpeakerId(modelPath) + // The cached model outlives the session; never release it here. + return object : SpeakerEmbedder { + override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = cached.embed(pcm16le, sampleRate) + + override fun release() {} + } + } + + override fun recordingSink(): RecordingSink = RecordingSink { pcmBytes, sampleRate -> saveRecording(pcmBytes, sampleRate) } + } + + private val session = VoiceCaptureSession(host) + + fun setListener(listener: FaceclawVoiceControllerListener?) { + session.setListener(listener) + } + + fun setCommunicator(communicator: FaceclawBleCommunicator?) { + this.communicator = communicator + } + + /** Source the next capture from the phone microphone (no glasses paired). */ + fun setUsePhoneMic(usePhoneMic: Boolean) { + session.setUsePhoneMic(usePhoneMic) + } + + /** When true, the decoded mic PCM for each session is saved as a WAV. */ + fun setSaveRecordings(saveRecordings: Boolean) { + session.setSaveRecordings(saveRecordings) + } + + /** + * Which on-device model {@link #start}("onboard") should load: "whisper" + * selects the second on-device model (sherpa-onnx offline Whisper); any + * other value (including null/absent) keeps the existing Moonshine model. + * Must be set before {@link #start}; has no effect in CLOUD mode. + */ + fun setOnboardModelKind(kind: String?) { + session.setOnboardModelKind(kind) + } + + /** + * When true, watch the decoded PCM and fire {@code onSpeechEnd} once the + * speaker stops. Used by hands-free ("Hey Even") capture, which has no + * button release to end the utterance. Must be set before {@link #start}. + */ + fun setEndpointing(endpointing: Boolean) { + session.setEndpointing(endpointing) + } + + /** + * Verify this session's speaker against the enrolled wearer voice-print + * and report the result via onSpeakerVerified just before the final + * transcript. Must be set before {@link #start}; pass a null model path + * to disable. + */ + fun setSpeakerVerification(speakerModelPath: String?, wearerEmbedding: FloatArray?, threshold: Float) { + session.setSpeakerVerification(speakerModelPath, wearerEmbedding, threshold) + } + + fun clearSpeakerVerification() { + session.clearSpeakerVerification() + } + + /** Spectral noise suppression on the decoded stream. Safe to flip mid-run. */ + fun setNoiseSuppression(enabled: Boolean) { + session.setNoiseSuppression(enabled) + } + + /** + * Direction gating from the Sonic Radar beam: packets whose firmware + * direction-of-arrival falls outside centerDeg ± halfWidthDeg (device + * frame, 0 = straight ahead, positive right) are dropped before any + * consumer sees them. Safe to update mid-run. + */ + fun setBeamFilter(enabled: Boolean, centerDeg: Int, halfWidthDeg: Int) { + session.setBeamFilter(enabled, centerDeg, halfWidthDeg) + } + + fun start(requestedMode: String?) { + session.start(requestedMode, 0) + } + + fun start(requestedMode: String?, captureId: Int) { + session.start(requestedMode, captureId) + } + + /** + * Whether mic audio is actually flowing. {@link #start} only records + * intent: the enable lives in the glasses' EvenHub session, so a transport + * drop or a session suspend can leave this controller started with a + * worker that will never see another packet. Anything deciding whether to + * (re)start capture must ask this rather than assume its own bookkeeping. + */ + fun isCapturing(): Boolean = session.isCapturing() + + fun stop() { + session.stop() + } + + fun close() { + stop() + } + + /** Save the session's decoded mic PCM as a 16 kHz mono 16-bit WAV. */ + private fun saveRecording(pcmBytes: ByteArray, sampleRate: Int) { + try { + val dir = File(appContext.getExternalFilesDir(null), "voice-recordings") + if (!dir.exists() && !dir.mkdirs()) { + Log.w(TAG, "could not create voice-recordings dir") + return + } + val stamp = java.text.SimpleDateFormat("yyyyMMdd-HHmmss-SSS", java.util.Locale.US) + .format(java.util.Date()) + val file = File(dir, "voice-$stamp.wav") + java.io.FileOutputStream(file).use { fos -> + fos.write(BinaryEncoding.wavHeader(pcmBytes.size, sampleRate, 1)) + fos.write(pcmBytes) + } + Log.i(TAG, "saved voice recording " + file.absolutePath + + " samples=" + (pcmBytes.size / 2) + + " sec=" + String.format(java.util.Locale.US, "%.2f", pcmBytes.size / 2.0 / sampleRate)) + } catch (t: Throwable) { + Log.w(TAG, "failed to save voice recording", t) + } + } + + /** + * The on-device model directory for the given kind, populated by the + * download flow in asr-model.ts (releases before 0.5.0 copied the + * Moonshine files out of the APK directly, so upgraded installs are + * already complete for that model). Null when any expected file is + * missing, i.e. the model still needs to be downloaded. + */ + private fun findAsrModelDir(kind: VoiceModelKind): File? { + val whisper = kind == VoiceModelKind.WHISPER + val dirName = if (whisper) ASR_WHISPER_MODEL_DIR else ASR_MODEL_DIR + val fileNames = if (whisper) ASR_WHISPER_MODEL_FILES else ASR_MODEL_FILES + val modelDir = File(appContext.filesDir, ASR_ROOT + File.separator + dirName) + for (fileName in fileNames) { + val file = File(modelDir, fileName) + if (!file.exists() || file.length() == 0L) { + return null + } + } + return modelDir + } + + private fun deleteLegacyKwsFiles() { + try { + deleteRecursively(File(appContext.filesDir, LEGACY_KWS_ROOT)) + } catch (t: Throwable) { + Log.w(TAG, "failed to delete legacy wake-word files", t) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java deleted file mode 100644 index cb0da66f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java +++ /dev/null @@ -1,391 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.wearable.CapabilityClient; -import com.google.android.gms.wearable.CapabilityInfo; -import com.google.android.gms.wearable.DataMap; -import com.google.android.gms.wearable.MessageEvent; -import com.google.android.gms.wearable.Node; -import com.google.android.gms.wearable.PutDataMapRequest; -import com.google.android.gms.wearable.PutDataRequest; -import com.google.android.gms.wearable.Wearable; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -/** - * Phone side of the Wear OS integration: the single place that talks to the - * Wearable Data Layer (Google Play services), so the JS side only ever sees - * JSON strings and never touches Play services types. - * - * Inbound: FaceclawWearListenerService hands every watch message to - * {@link #handleMessage}; it is forwarded to the JS listener on the thread - * that registered it (the main isolate), mirroring FaceclawSettings. Messages - * that arrive before a listener exists (the watch poked a phone whose JS - * dashboard is not up yet) are acked with jsReady=false so the watch can say - * "open Faceclaw on your phone"; only idempotent state requests are kept for - * replay on registration — the watch already reported gestures and commands - * as failed, and executing them tens of seconds later would act on whatever - * the glasses happen to show by then. - * - * Outbound: {@link #publishState} mirrors the dashboard state into a Data - * Layer item (delivered even if the watch is out of range right now, and - * available to the watch app the moment it opens), while {@link #sendToWatch} - * sends fire-and-forget messages (acks, assistant text) to every reachable - * watch that advertises the Faceclaw watch-app capability. - * - * Everything is a no-op on phones without Google Play services. - */ -public final class FaceclawWearBridge { - private static final String TAG = "FaceclawWear"; - - /** Advertised by the watch app (wear/app/src/main/res/values/wear.xml). */ - public static final String CAPABILITY_WATCH = "faceclaw_watch"; - /** Advertised by this app (res/values/wear.xml) so the watch can find the phone. */ - public static final String CAPABILITY_PHONE = "faceclaw_phone"; - - public static final String PATH_PREFIX = "/faceclaw"; - public static final String PATH_STATE = "/faceclaw/state"; - public static final String PATH_STATE_REQUEST = "/faceclaw/state/request"; - /** Watch -> phone: the watch's own battery; carries no seq and is never acked. */ - public static final String PATH_WATCH_BATTERY = "/faceclaw/battery"; - /** Phone -> watch: answered with PATH_WATCH_BATTERY even while the watch app is closed. */ - public static final String PATH_WATCH_BATTERY_REQUEST = "/faceclaw/battery/request"; - public static final String PATH_ACK = "/faceclaw/ack"; - public static final String PATH_EVENT = "/faceclaw/event"; - - private static final String STATE_KEY_JSON = "json"; - private static final String STATE_KEY_UPDATED_AT = "updatedAt"; - - /** Messages kept for a listener that is not registered yet. */ - private static final int PENDING_LIMIT = 16; - private static final long PENDING_MAX_AGE_MS = 30_000; - - private static volatile FaceclawWearBridge instance; - - private final Context context; - private final boolean available; - private final Object lock = new Object(); - private FaceclawWearListener listener; - private Handler listenerHandler; - private final ArrayDeque pending = new ArrayDeque<>(); - private final List watchNodes = new ArrayList<>(); - private String lastPublishedState = null; - - private static final class PendingMessage { - final String path; - final String json; - final String nodeId; - final long receivedAt; - - PendingMessage(String path, String json, String nodeId, long receivedAt) { - this.path = path; - this.json = json; - this.nodeId = nodeId; - this.receivedAt = receivedAt; - } - } - - private FaceclawWearBridge(Context context) { - this.context = context.getApplicationContext(); - boolean playServices; - try { - playServices = GoogleApiAvailability.getInstance() - .isGooglePlayServicesAvailable(this.context) == ConnectionResult.SUCCESS; - } catch (Throwable error) { - Log.w(TAG, "Play services availability check failed", error); - playServices = false; - } - this.available = playServices; - if (!available) { - Log.i(TAG, "Google Play services unavailable; watch integration disabled"); - } - } - - /** Initialize (idempotent) and return the singleton. */ - public static FaceclawWearBridge getInstance(Context context) { - if (instance == null) { - synchronized (FaceclawWearBridge.class) { - if (instance == null) { - instance = new FaceclawWearBridge(context); - } - } - } - return instance; - } - - /** Whether the Wearable Data Layer can be used on this phone at all. */ - public boolean isAvailable() { - return available; - } - - /** - * Register the JS listener. Must be called from the thread whose isolate - * owns it; that thread's Looper is captured for dispatch. Replays any - * recently queued watch messages and refreshes the reachable-watch set. - */ - public void setListener(FaceclawWearListener newListener) { - Looper looper = Looper.myLooper(); - if (looper == null) { - Log.w(TAG, "wear listener registered from a Looper-less thread; it will never be notified"); - } - List replay; - synchronized (lock) { - listener = newListener; - listenerHandler = looper != null ? new Handler(looper) : null; - replay = new ArrayList<>(pending); - pending.clear(); - } - long now = System.currentTimeMillis(); - for (PendingMessage message : replay) { - if (now - message.receivedAt <= PENDING_MAX_AGE_MS) { - dispatchMessage(message.path, message.json, message.nodeId); - } - } - refreshWatchNodes(); - } - - public void clearListener() { - synchronized (lock) { - listener = null; - listenerHandler = null; - } - } - - public boolean isWatchReachable() { - synchronized (lock) { - return !watchNodes.isEmpty(); - } - } - - /** Display name of a reachable watch ("" if none). */ - public String getWatchName() { - synchronized (lock) { - return watchNodes.isEmpty() ? "" : watchNodes.get(0).getDisplayName(); - } - } - - /** Re-query which watches advertise the Faceclaw watch-app capability. */ - public void refreshWatchNodes() { - if (!available) return; - try { - Wearable.getCapabilityClient(context) - .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) - .addOnSuccessListener(this::updateWatchNodes) - .addOnFailureListener(error -> Log.w(TAG, "capability query failed", error)); - } catch (Throwable error) { - Log.w(TAG, "capability query failed", error); - } - } - - /** - * Mirror the dashboard state to the watch. Identical consecutive states - * are not re-sent (the Data Layer would drop them anyway). - */ - public void publishState(String json) { - publishState(json, false); - } - - /** As {@link #publishState(String)}; `force` re-sends an unchanged state. */ - public void publishState(String json, boolean force) { - if (!available || json == null) return; - synchronized (lock) { - if (!force && json.equals(lastPublishedState)) return; - lastPublishedState = json; - } - try { - PutDataMapRequest request = PutDataMapRequest.create(PATH_STATE); - DataMap map = request.getDataMap(); - map.putString(STATE_KEY_JSON, json); - map.putLong(STATE_KEY_UPDATED_AT, System.currentTimeMillis()); - PutDataRequest put = request.asPutDataRequest().setUrgent(); - Wearable.getDataClient(context) - .putDataItem(put) - .addOnFailureListener(error -> Log.w(TAG, "state publish failed", error)); - } catch (Throwable error) { - Log.w(TAG, "state publish failed", error); - } - } - - /** Send a message to every reachable watch running the Faceclaw watch app. */ - public void sendToWatch(String path, String json) { - if (!available) return; - List targets; - synchronized (lock) { - targets = new ArrayList<>(watchNodes); - } - if (targets.isEmpty()) { - // Nothing known yet (first message after boot): look the watch up, then send. - try { - Wearable.getCapabilityClient(context) - .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) - .addOnSuccessListener(info -> { - updateWatchNodes(info); - for (Node node : info.getNodes()) { - sendToNode(node.getId(), path, json); - } - }) - .addOnFailureListener(error -> Log.w(TAG, "capability query failed", error)); - } catch (Throwable error) { - Log.w(TAG, "capability query failed", error); - } - return; - } - for (Node node : targets) { - sendToNode(node.getId(), path, json); - } - } - - /** Send a message to one specific node (e.g. an ack back to the sender). */ - public void sendToNode(String nodeId, String path, String json) { - if (!available || nodeId == null || nodeId.isEmpty()) return; - try { - byte[] payload = (json != null ? json : "{}").getBytes(StandardCharsets.UTF_8); - Wearable.getMessageClient(context) - .sendMessage(nodeId, path, payload) - .addOnFailureListener(error -> Log.w(TAG, "send " + path + " to " + nodeId + " failed", error)); - } catch (Throwable error) { - Log.w(TAG, "send " + path + " failed", error); - } - } - - /** Reply to a watch message; `seq` echoes the watch's sequence number. */ - public void sendAck(String nodeId, long seq, boolean ok, boolean jsReady, String message) { - JSONObject ack = new JSONObject(); - try { - ack.put("seq", seq); - ack.put("ok", ok); - ack.put("jsReady", jsReady); - ack.put("message", message != null ? message : ""); - } catch (JSONException ignored) { - // The keys are constants; this cannot happen. - } - sendToNode(nodeId, PATH_ACK, ack.toString()); - } - - /** Entry point for FaceclawWearListenerService; may run on any thread. */ - void handleMessage(MessageEvent event) { - if (event == null) return; - String path = event.getPath(); - if (path == null || !path.startsWith(PATH_PREFIX)) return; - byte[] data = event.getData(); - String json = data != null && data.length > 0 ? new String(data, StandardCharsets.UTF_8) : "{}"; - String nodeId = event.getSourceNodeId(); - Log.d(TAG, "recv " + path + " seq=" + readSeq(json)); - - boolean delivered = dispatchMessage(path, json, nodeId); - if (delivered) return; - // A battery report answers a poll the dashboard sent; with no - // 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.equals(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); - // everything else is dropped after the failure ack. - if (PATH_STATE_REQUEST.equals(path)) { - synchronized (lock) { - long now = System.currentTimeMillis(); - while (!pending.isEmpty() && now - pending.peekFirst().receivedAt > PENDING_MAX_AGE_MS) { - pending.pollFirst(); - } - if (pending.size() >= PENDING_LIMIT) { - pending.pollFirst(); - } - pending.addLast(new PendingMessage(path, json, nodeId, now)); - } - } - sendAck(nodeId, readSeq(json), false, false, "Faceclaw is not running on the phone. Open it to connect the glasses."); - } - - /** Entry point for FaceclawWearListenerService's capability callback. */ - void handleCapabilityChanged(CapabilityInfo info) { - if (info == null || !CAPABILITY_WATCH.equals(info.getName())) return; - updateWatchNodes(info); - } - - private void updateWatchNodes(CapabilityInfo info) { - Set nodes = info != null ? info.getNodes() : Collections.emptySet(); - // Every reachable watch gets messages; a nearby (Bluetooth-linked) - // one is listed first so it is the name the phone UI shows. - List reachable = new ArrayList<>(); - for (Node node : nodes) { - if (node.isNearby()) reachable.add(0, node); else reachable.add(node); - } - boolean changed; - String name; - synchronized (lock) { - changed = !sameNodeIds(watchNodes, reachable); - watchNodes.clear(); - watchNodes.addAll(reachable); - name = watchNodes.isEmpty() ? "" : watchNodes.get(0).getDisplayName(); - } - if (changed) { - Log.i(TAG, reachable.isEmpty() ? "no Faceclaw watch reachable" : "watch reachable: " + name); - boolean reachableNow = !reachable.isEmpty(); - FaceclawWearListener target; - Handler handler; - synchronized (lock) { - target = listener; - handler = listenerHandler; - } - if (target != null && handler != null) { - handler.post(() -> { - try { - target.onWatchConnection(reachableNow, name); - } catch (Exception error) { - Log.w(TAG, "watch connection listener failed", error); - } - }); - } - } - } - - private static boolean sameNodeIds(List a, List b) { - if (a.size() != b.size()) return false; - for (int i = 0; i < a.size(); i++) { - if (!a.get(i).getId().equals(b.get(i).getId())) return false; - } - return true; - } - - /** Post to the JS listener; false when there is none to post to. */ - private boolean dispatchMessage(String path, String json, String nodeId) { - FaceclawWearListener target; - Handler handler; - synchronized (lock) { - target = listener; - handler = listenerHandler; - } - if (target == null || handler == null) return false; - handler.post(() -> { - try { - target.onMessage(path, json, nodeId); - } catch (Exception error) { - Log.w(TAG, "wear message listener failed for " + path, error); - } - }); - return true; - } - - private static long readSeq(String json) { - try { - return new JSONObject(json).optLong("seq", 0); - } catch (JSONException error) { - return 0; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.kt new file mode 100644 index 00000000..5be4a511 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.kt @@ -0,0 +1,386 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.util.Log + +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailability +import com.google.android.gms.wearable.CapabilityClient +import com.google.android.gms.wearable.CapabilityInfo +import com.google.android.gms.wearable.DataMap +import com.google.android.gms.wearable.MessageEvent +import com.google.android.gms.wearable.Node +import com.google.android.gms.wearable.PutDataMapRequest +import com.google.android.gms.wearable.PutDataRequest +import com.google.android.gms.wearable.Wearable + +import org.json.JSONException +import org.json.JSONObject + +import java.nio.charset.StandardCharsets +import java.util.ArrayDeque +import java.util.ArrayList +import java.util.Collections + +/** + * Phone side of the Wear OS integration: the single place that talks to the + * Wearable Data Layer (Google Play services), so the JS side only ever sees + * JSON strings and never touches Play services types. + * + * Inbound: FaceclawWearListenerService hands every watch message to + * [handleMessage]; it is forwarded to the JS listener on the thread + * that registered it (the main isolate), mirroring FaceclawSettings. Messages + * that arrive before a listener exists (the watch poked a phone whose JS + * dashboard is not up yet) are acked with jsReady=false so the watch can say + * "open Faceclaw on your phone"; only idempotent state requests are kept for + * replay on registration — the watch already reported gestures and commands + * as failed, and executing them tens of seconds later would act on whatever + * the glasses happen to show by then. + * + * Outbound: [publishState] mirrors the dashboard state into a Data + * Layer item (delivered even if the watch is out of range right now, and + * available to the watch app the moment it opens), while [sendToWatch] + * sends fire-and-forget messages (acks, assistant text) to every reachable + * watch that advertises the Faceclaw watch-app capability. + * + * Everything is a no-op on phones without Google Play services. + */ +class FaceclawWearBridge private constructor(context: Context) { + companion object { + private const val TAG = "FaceclawWear" + + /** Advertised by the watch app (wear/app/src/main/res/values/wear.xml). */ + const val CAPABILITY_WATCH = "faceclaw_watch" + /** Advertised by this app (res/values/wear.xml) so the watch can find the phone. */ + const val CAPABILITY_PHONE = "faceclaw_phone" + + const val PATH_PREFIX = "/faceclaw" + const val PATH_STATE = "/faceclaw/state" + 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" + /** 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" + const val PATH_EVENT = "/faceclaw/event" + + private const val STATE_KEY_JSON = "json" + private const val STATE_KEY_UPDATED_AT = "updatedAt" + + /** Messages kept for a listener that is not registered yet. */ + private const val PENDING_LIMIT = 16 + private const val PENDING_MAX_AGE_MS = 30_000L + + @Volatile + private var instance: FaceclawWearBridge? = null + + /** Initialize (idempotent) and return the singleton. */ + @JvmStatic + fun getInstance(context: Context): FaceclawWearBridge { + if (instance == null) { + synchronized(FaceclawWearBridge::class.java) { + if (instance == null) { + instance = FaceclawWearBridge(context) + } + } + } + return instance!! + } + + private fun sameNodeIds(a: List, b: List): Boolean { + if (a.size != b.size) return false + for (i in a.indices) { + if (a[i].id != b[i].id) return false + } + return true + } + + private fun readSeq(json: String): Long { + try { + return JSONObject(json).optLong("seq", 0) + } catch (error: JSONException) { + return 0 + } + } + } + + private val context: Context + private val available: Boolean + private val lock = Any() + private var listener: FaceclawWearListener? = null + private var listenerHandler: Handler? = null + private val pending = ArrayDeque() + private val watchNodes: MutableList = ArrayList() + private var lastPublishedState: String? = null + + private class PendingMessage( + val path: String, + val json: String, + val nodeId: String?, + val receivedAt: Long, + ) + + init { + this.context = context.applicationContext + var playServices: Boolean + try { + playServices = GoogleApiAvailability.getInstance() + .isGooglePlayServicesAvailable(this.context) == ConnectionResult.SUCCESS + } catch (error: Throwable) { + Log.w(TAG, "Play services availability check failed", error) + playServices = false + } + this.available = playServices + if (!available) { + Log.i(TAG, "Google Play services unavailable; watch integration disabled") + } + } + + /** Whether the Wearable Data Layer can be used on this phone at all. */ + fun isAvailable(): Boolean { + return available + } + + /** + * Register the JS listener. Must be called from the thread whose isolate + * owns it; that thread's Looper is captured for dispatch. Replays any + * recently queued watch messages and refreshes the reachable-watch set. + */ + fun setListener(newListener: FaceclawWearListener?) { + val looper = Looper.myLooper() + if (looper == null) { + Log.w(TAG, "wear listener registered from a Looper-less thread; it will never be notified") + } + val replay: List + synchronized(lock) { + listener = newListener + listenerHandler = if (looper != null) Handler(looper) else null + replay = ArrayList(pending) + pending.clear() + } + val now = System.currentTimeMillis() + for (message in replay) { + if (now - message.receivedAt <= PENDING_MAX_AGE_MS) { + dispatchMessage(message.path, message.json, message.nodeId) + } + } + refreshWatchNodes() + } + + fun clearListener() { + synchronized(lock) { + listener = null + listenerHandler = null + } + } + + fun isWatchReachable(): Boolean { + synchronized(lock) { + return !watchNodes.isEmpty() + } + } + + /** Display name of a reachable watch ("" if none). */ + fun getWatchName(): String { + synchronized(lock) { + return if (watchNodes.isEmpty()) "" else watchNodes[0].displayName + } + } + + /** Re-query which watches advertise the Faceclaw watch-app capability. */ + fun refreshWatchNodes() { + if (!available) return + try { + Wearable.getCapabilityClient(context) + .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) + .addOnSuccessListener { info -> updateWatchNodes(info) } + .addOnFailureListener { error -> Log.w(TAG, "capability query failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "capability query failed", error) + } + } + + /** + * Mirror the dashboard state to the watch. Identical consecutive states + * are not re-sent (the Data Layer would drop them anyway). + */ + fun publishState(json: String?) { + publishState(json, false) + } + + /** As [publishState]; `force` re-sends an unchanged state. */ + fun publishState(json: String?, force: Boolean) { + if (!available || json == null) return + synchronized(lock) { + if (!force && json == lastPublishedState) return + lastPublishedState = json + } + try { + val request = PutDataMapRequest.create(PATH_STATE) + val map: DataMap = request.dataMap + map.putString(STATE_KEY_JSON, json) + map.putLong(STATE_KEY_UPDATED_AT, System.currentTimeMillis()) + val put: PutDataRequest = request.asPutDataRequest().setUrgent() + Wearable.getDataClient(context) + .putDataItem(put) + .addOnFailureListener { error -> Log.w(TAG, "state publish failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "state publish failed", error) + } + } + + /** Send a message to every reachable watch running the Faceclaw watch app. */ + fun sendToWatch(path: String, json: String?) { + if (!available) return + val targets: List + synchronized(lock) { + targets = ArrayList(watchNodes) + } + if (targets.isEmpty()) { + // Nothing known yet (first message after boot): look the watch up, then send. + try { + Wearable.getCapabilityClient(context) + .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) + .addOnSuccessListener { info -> + updateWatchNodes(info) + for (node in info.nodes) { + sendToNode(node.id, path, json) + } + } + .addOnFailureListener { error -> Log.w(TAG, "capability query failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "capability query failed", error) + } + return + } + for (node in targets) { + sendToNode(node.id, path, json) + } + } + + /** Send a message to one specific node (e.g. an ack back to the sender). */ + fun sendToNode(nodeId: String?, path: String, json: String?) { + if (!available || nodeId == null || nodeId.isEmpty()) return + try { + val payload = (json ?: "{}").toByteArray(StandardCharsets.UTF_8) + Wearable.getMessageClient(context) + .sendMessage(nodeId, path, payload) + .addOnFailureListener { error -> Log.w(TAG, "send $path to $nodeId failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "send $path failed", error) + } + } + + /** Reply to a watch message; `seq` echoes the watch's sequence number. */ + fun sendAck(nodeId: String?, seq: Long, ok: Boolean, jsReady: Boolean, message: String?) { + val ack = JSONObject() + try { + ack.put("seq", seq) + ack.put("ok", ok) + ack.put("jsReady", jsReady) + ack.put("message", message ?: "") + } catch (ignored: JSONException) { + // The keys are constants; this cannot happen. + } + sendToNode(nodeId, PATH_ACK, ack.toString()) + } + + /** Entry point for FaceclawWearListenerService; may run on any thread. */ + internal fun handleMessage(event: MessageEvent?) { + if (event == null) return + val path: String? = event.path + if (path == null || !path.startsWith(PATH_PREFIX)) return + val data: ByteArray? = event.data + val json = if (data != null && data.size > 0) String(data, StandardCharsets.UTF_8) else "{}" + val nodeId: String? = event.sourceNodeId + Log.d(TAG, "recv " + path + " seq=" + readSeq(json)) + + val delivered = dispatchMessage(path, json, nodeId) + if (delivered) return + // A battery report answers a poll the dashboard sent; with no + // 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 + + // 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); + // everything else is dropped after the failure ack. + if (PATH_STATE_REQUEST == path) { + synchronized(lock) { + val now = System.currentTimeMillis() + while (!pending.isEmpty() && now - pending.peekFirst().receivedAt > PENDING_MAX_AGE_MS) { + pending.pollFirst() + } + if (pending.size >= PENDING_LIMIT) { + pending.pollFirst() + } + pending.addLast(PendingMessage(path, json, nodeId, now)) + } + } + sendAck(nodeId, readSeq(json), false, false, "Faceclaw is not running on the phone. Open it to connect the glasses.") + } + + /** Entry point for FaceclawWearListenerService's capability callback. */ + internal fun handleCapabilityChanged(info: CapabilityInfo?) { + if (info == null || CAPABILITY_WATCH != info.name) return + updateWatchNodes(info) + } + + private fun updateWatchNodes(info: CapabilityInfo?) { + val nodes: Set = if (info != null) info.nodes else Collections.emptySet() + // Every reachable watch gets messages; a nearby (Bluetooth-linked) + // one is listed first so it is the name the phone UI shows. + val reachable: MutableList = ArrayList() + for (node in nodes) { + if (node.isNearby) reachable.add(0, node) else reachable.add(node) + } + val changed: Boolean + val name: String + synchronized(lock) { + changed = !sameNodeIds(watchNodes, reachable) + watchNodes.clear() + watchNodes.addAll(reachable) + name = if (watchNodes.isEmpty()) "" else watchNodes[0].displayName + } + if (changed) { + Log.i(TAG, if (reachable.isEmpty()) "no Faceclaw watch reachable" else "watch reachable: $name") + val reachableNow = !reachable.isEmpty() + val target: FaceclawWearListener? + val handler: Handler? + synchronized(lock) { + target = listener + handler = listenerHandler + } + if (target != null && handler != null) { + handler.post { + try { + target.onWatchConnection(reachableNow, name) + } catch (error: Exception) { + Log.w(TAG, "watch connection listener failed", error) + } + } + } + } + } + + /** Post to the JS listener; false when there is none to post to. */ + private fun dispatchMessage(path: String, json: String, nodeId: String?): Boolean { + val target: FaceclawWearListener? + val handler: Handler? + synchronized(lock) { + target = listener + handler = listenerHandler + } + if (target == null || handler == null) return false + handler.post { + try { + target.onMessage(path, json, nodeId) + } catch (error: Exception) { + Log.w(TAG, "wear message listener failed for $path", error) + } + } + return true + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.java deleted file mode 100644 index 7520d780..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import com.google.android.gms.wearable.CapabilityInfo; -import com.google.android.gms.wearable.MessageEvent; -import com.google.android.gms.wearable.WearableListenerService; - -/** - * Play services starts this service (in the app process) for every Data - * Layer message under /faceclaw and for capability changes, whether or not - * the app is running. It only forwards to FaceclawWearBridge, which owns the - * hand-off to the JS side. - */ -public class FaceclawWearListenerService extends WearableListenerService { - @Override - public void onMessageReceived(MessageEvent event) { - Log.d("FaceclawWear", "service onMessageReceived " + event.getPath() + " from " + event.getSourceNodeId()); - FaceclawWearBridge.getInstance(this).handleMessage(event); - } - - @Override - public void onCapabilityChanged(CapabilityInfo info) { - FaceclawWearBridge.getInstance(this).handleCapabilityChanged(info); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.kt new file mode 100644 index 00000000..ed0ce360 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.kt @@ -0,0 +1,24 @@ +package com.faceclaw.app + +import android.util.Log + +import com.google.android.gms.wearable.CapabilityInfo +import com.google.android.gms.wearable.MessageEvent +import com.google.android.gms.wearable.WearableListenerService + +/** + * Play services starts this service (in the app process) for every Data + * Layer message under /faceclaw and for capability changes, whether or not + * the app is running. It only forwards to FaceclawWearBridge, which owns the + * hand-off to the JS side. + */ +class FaceclawWearListenerService : WearableListenerService() { + override fun onMessageReceived(event: MessageEvent) { + Log.d("FaceclawWear", "service onMessageReceived " + event.path + " from " + event.sourceNodeId) + FaceclawWearBridge.getInstance(this).handleMessage(event) + } + + override fun onCapabilityChanged(info: CapabilityInfo) { + FaceclawWearBridge.getInstance(this).handleCapabilityChanged(info) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java deleted file mode 100644 index f228ed7a..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.concurrent.TimeUnit; - -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.WebSocket; -import okhttp3.WebSocketListener; -import okio.ByteString; - -/** - * Thin okhttp WebSocket wrapper for the TypeScript side. Sends text frames - * (JSON protocols) and binary frames (raw audio for Soniox). Listener callbacks are - * posted to the Looper of the thread that constructed this object, so a JS - * isolate (main thread or app worker) always receives them on its own - * thread; a Looper-less constructing thread falls back to the main thread. - */ -public class FaceclawWebSocket { - private static final String TAG = "FaceclawWebSocket"; - private static volatile OkHttpClient sharedClient; - - private final Handler callbackHandler; - private final WebSocket socket; - private volatile boolean closeRequested; - - // Single constructor (no overloads) so NativeScript constructor resolution - // can never pick a variant that drops the auth header; callers with no - // header pass null/null. - public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener, String headerName, String headerValue) { - if (url == null || url.trim().isEmpty()) { - throw new IllegalArgumentException("url is required"); - } - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - Looper looper = Looper.myLooper(); - callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - Request.Builder builder = new Request.Builder().url(url.trim()); - if (headerName != null && !headerName.isEmpty() && headerValue != null) { - builder.addHeader(headerName, headerValue); - } - Request request = builder.build(); - // Redacted diagnostics: confirm the auth header is actually present on - // the handshake and carries a plausible value (not empty/truncated). - StringBuilder headerLog = new StringBuilder(); - for (String name : request.headers().names()) { - String value = request.header(name); - headerLog.append(name).append('=').append(redact(value)).append(' '); - } - Log.i(TAG, "ws connect host=" + request.url().host() + request.url().encodedPath() - + " headers=[" + headerLog.toString().trim() + "]"); - socket = getClient().newWebSocket(request, new WebSocketListener() { - @Override public void onOpen(WebSocket webSocket, Response response) { - callbackHandler.post(() -> { - try { - listener.onOpen(); - } catch (Throwable t) { - Log.w(TAG, "listener onOpen failed", t); - } - }); - } - - @Override public void onMessage(WebSocket webSocket, String text) { - callbackHandler.post(() -> { - try { - listener.onTextMessage(text); - } catch (Throwable t) { - Log.w(TAG, "listener onTextMessage failed", t); - } - }); - } - - @Override public void onClosing(WebSocket webSocket, int code, String reason) { - webSocket.close(code, reason); - } - - @Override public void onClosed(WebSocket webSocket, int code, String reason) { - callbackHandler.post(() -> { - try { - listener.onClosed(code, reason == null ? "" : reason); - } catch (Throwable t) { - Log.w(TAG, "listener onClosed failed", t); - } - }); - } - - @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { - if (closeRequested) { - return; - } - final String message = t == null ? "unknown websocket failure" : String.valueOf(t); - callbackHandler.post(() -> { - try { - listener.onFailure(message); - } catch (Throwable inner) { - Log.w(TAG, "listener onFailure failed", inner); - } - }); - } - }); - } - - private static OkHttpClient getClient() { - OkHttpClient client = sharedClient; - if (client == null) { - synchronized (FaceclawWebSocket.class) { - if (sharedClient == null) { - sharedClient = new OkHttpClient.Builder() - .pingInterval(20, TimeUnit.SECONDS) - .addInterceptor(FaceclawHttp.userAgentInterceptor()) - .build(); - } - client = sharedClient; - } - } - return client; - } - - private static String redact(String value) { - if (value == null) { - return "null"; - } - int len = value.length(); - String prefix = value.substring(0, Math.min(4, len)); - return "len" + len + ":" + prefix + "..."; - } - - public boolean sendText(String message) { - return socket.send(message == null ? "" : message); - } - - public boolean sendBinary(byte[] bytes) { - return socket.send(ByteString.of(bytes == null ? new byte[0] : bytes)); - } - - public void close(int code, String reason) { - closeRequested = true; - try { - if (!socket.close(code, reason)) { - socket.cancel(); - } - } catch (Throwable t) { - socket.cancel(); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.kt new file mode 100644 index 00000000..f4a793bd --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.kt @@ -0,0 +1,156 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.util.concurrent.TimeUnit + +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import okhttp3.WebSocket +import okhttp3.WebSocketListener +import okio.ByteString.Companion.toByteString + +/** + * Thin okhttp WebSocket wrapper for the TypeScript side. Sends text frames + * (JSON protocols) and binary frames (raw audio for Soniox). Listener callbacks are + * posted to the Looper of the thread that constructed this object, so a JS + * isolate (main thread or app worker) always receives them on its own + * thread; a Looper-less constructing thread falls back to the main thread. + */ +class FaceclawWebSocket +// Single constructor (no overloads) so NativeScript constructor resolution +// can never pick a variant that drops the auth header; callers with no +// header pass null/null. +constructor(url: String?, listener: FaceclawWebSocketListener?, headerName: String?, headerValue: String?) { + private val callbackHandler: Handler + private val socket: WebSocket + @Volatile + private var closeRequested = false + + init { + if (url == null || url.trim().isEmpty()) { + throw IllegalArgumentException("url is required") + } + if (listener == null) { + throw IllegalArgumentException("listener is required") + } + val looper = Looper.myLooper() + callbackHandler = Handler(looper ?: Looper.getMainLooper()) + val builder = Request.Builder().url(url.trim()) + if (headerName != null && !headerName.isEmpty() && headerValue != null) { + builder.addHeader(headerName, headerValue) + } + val request = builder.build() + // Redacted diagnostics: confirm the auth header is actually present on + // the handshake and carries a plausible value (not empty/truncated). + val headerLog = StringBuilder() + for (name in request.headers.names()) { + val value = request.header(name) + headerLog.append(name).append('=').append(redact(value)).append(' ') + } + Log.i(TAG, "ws connect host=" + request.url.host + request.url.encodedPath + + " headers=[" + headerLog.toString().trim() + "]") + socket = getClient().newWebSocket(request, object : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + callbackHandler.post { + try { + listener.onOpen() + } catch (t: Throwable) { + Log.w(TAG, "listener onOpen failed", t) + } + } + } + + override fun onMessage(webSocket: WebSocket, text: String) { + callbackHandler.post { + try { + listener.onTextMessage(text) + } catch (t: Throwable) { + Log.w(TAG, "listener onTextMessage failed", t) + } + } + } + + override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { + webSocket.close(code, reason) + } + + override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { + callbackHandler.post { + try { + listener.onClosed(code, reason) + } catch (t: Throwable) { + Log.w(TAG, "listener onClosed failed", t) + } + } + } + + override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { + if (closeRequested) { + return + } + val message = t.toString() + callbackHandler.post { + try { + listener.onFailure(message) + } catch (inner: Throwable) { + Log.w(TAG, "listener onFailure failed", inner) + } + } + } + }) + } + + companion object { + private const val TAG = "FaceclawWebSocket" + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + var client = sharedClient + if (client == null) { + synchronized(FaceclawWebSocket::class.java) { + if (sharedClient == null) { + sharedClient = OkHttpClient.Builder() + .pingInterval(20, TimeUnit.SECONDS) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + client = sharedClient + } + } + return client!! + } + + private fun redact(value: String?): String { + if (value == null) { + return "null" + } + val len = value.length + val prefix = value.substring(0, Math.min(4, len)) + return "len$len:$prefix..." + } + } + + fun sendText(message: String?): Boolean { + return socket.send(message ?: "") + } + + fun sendBinary(bytes: ByteArray?): Boolean { + return socket.send((bytes ?: ByteArray(0)).toByteString()) + } + + fun close(code: Int, reason: String?) { + closeRequested = true + try { + if (!socket.close(code, reason)) { + socket.cancel() + } + } catch (t: Throwable) { + socket.cancel() + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java deleted file mode 100644 index 0eca05e5..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java +++ /dev/null @@ -1,537 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Typeface; -import android.os.Build; -import android.util.Log; - -import java.io.File; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Rasterizes text using font files (TTF/OTF/TTC) via the Android text stack, - * so shaping, kerning, and antialiasing are all delegated to - * minikin/HarfBuzz. Output uses the same grayscale packet format as - * ImageFileLoader ([widthLo, widthHi, heightLo, heightHi, pixels...], one - * byte per pixel, row-major), which the TS side turns into a GrayImage and - * the compositor later quantizes to the 4bpp the firmware wants. - * - * The gamma parameter maps antialiased coverage to output shade: - * out = 255 * (coverage/255)^gamma. The G2 display response looks roughly - * linear, so 1.0 is the expected default; the font previewer exposes it for - * on-hardware comparison. - */ -public final class FontFileRenderer { - private static final String TAG = "FontFileRenderer"; - private static final int TYPEFACE_CACHE_SIZE = 4; - - /** Small LRU of loaded typefaces keyed by file path. */ - private static final Map typefaceCache = - new LinkedHashMap(TYPEFACE_CACHE_SIZE, 0.75f, true) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > TYPEFACE_CACHE_SIZE; - } - }; - - private FontFileRenderer() {} - - /** - * Render one line of text with the given font file at the given pixel - * size. The returned image is the font's full vertical extent (top to - * bottom, taller than ascent+descent), plus any horizontal glyph - * overhang beyond the advance width. - * - * Returns an empty array if the font cannot be loaded or the text - * renders to nothing. - */ - public static byte[] renderText(String path, String text, float sizePx, double gamma) { - if (path == null || text == null || sizePx <= 0) { - return new byte[0]; - } - Typeface typeface = loadTypeface(path); - if (typeface == null) { - return new byte[0]; - } - try { - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - paint.setColor(Color.WHITE); - - // top/bottom (the font's maximal extent) rather than - // ascent/descent, so diacritics and swashes don't clip. - Paint.FontMetricsInt fm = paint.getFontMetricsInt(); - int ascent = -fm.top; - int height = -fm.top + fm.bottom; - // measureText gives advance width; italic/swash glyphs can paint - // outside it, so pad by half an em on each side and trim after. - int pad = (int) Math.ceil(sizePx / 2); - int advance = (int) Math.ceil(paint.measureText(text)); - int width = advance + 2 * pad; - if (width <= 0 || height <= 0 || width * height > 4_000_000) { - return new byte[0]; - } - - Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(bitmap); - canvas.drawText(text, pad, ascent, paint); - byte[] pixels = alpha8Pixels(bitmap, width, height); - bitmap.recycle(); - - // Trim the horizontal padding down to the painted extent, but - // keep at least the advance width so spacing stays truthful. - int left = pad; - int right = pad + advance; - int[] painted = paintedColumnRange(pixels, width, height); - if (painted != null) { - left = Math.min(left, painted[0]); - right = Math.max(right, painted[1] + 1); - } - int outWidth = Math.max(1, right - left); - - byte[] lut = gammaLut(gamma); - byte[] out = new byte[4 + outWidth * height]; - out[0] = (byte) (outWidth & 0xff); - out[1] = (byte) ((outWidth >> 8) & 0xff); - out[2] = (byte) (height & 0xff); - out[3] = (byte) ((height >> 8) & 0xff); - for (int y = 0; y < height; y++) { - int srcRow = y * width; - int dstRow = 4 + y * outWidth; - for (int x = 0; x < outWidth; x++) { - out[dstRow + x] = lut[pixels[srcRow + left + x] & 0xff]; - } - } - return out; - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "text render failed: " + path, e); - return new byte[0]; - } - } - - /** - * Render a multi-line block of text wrapped to maxWidth, with line - * breaking delegated to StaticLayout (so it matches Android text - * behavior, including breaking inside long words). Truncated with an - * ellipsis past maxLines. Same packet format as renderText. - */ - public static byte[] renderWrapped( - String path, String text, float sizePx, int maxWidth, double gamma, int maxLines) { - if (path == null || text == null || sizePx <= 0 || maxWidth <= 0 || maxLines <= 0) { - return new byte[0]; - } - Typeface typeface = loadTypeface(path); - if (typeface == null) { - return new byte[0]; - } - try { - android.text.TextPaint paint = - new android.text.TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - paint.setColor(Color.WHITE); - paint.setHinting(Paint.HINTING_ON); - - android.text.StaticLayout layout = android.text.StaticLayout.Builder - .obtain(text, 0, text.length(), paint, maxWidth) - .setAlignment(android.text.Layout.Alignment.ALIGN_NORMAL) - .setIncludePad(false) - .setMaxLines(maxLines) - .setEllipsize(android.text.TextUtils.TruncateAt.END) - .build(); - int height = Math.max(1, layout.getHeight()); - if ((long) maxWidth * height > 4_000_000L) { - return new byte[0]; - } - - Bitmap bitmap = Bitmap.createBitmap(maxWidth, height, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(bitmap); - layout.draw(canvas); - byte[] pixels = alpha8Pixels(bitmap, maxWidth, height); - bitmap.recycle(); - - byte[] lut = gammaLut(gamma); - byte[] out = new byte[4 + pixels.length]; - out[0] = (byte) (maxWidth & 0xff); - out[1] = (byte) ((maxWidth >> 8) & 0xff); - out[2] = (byte) (height & 0xff); - out[3] = (byte) ((height >> 8) & 0xff); - for (int i = 0; i < pixels.length; i++) { - out[4 + i] = lut[pixels[i] & 0xff]; - } - return out; - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "wrapped render failed: " + path, e); - return new byte[0]; - } - } - - /** - * Render one codepoint as a tight-ink-box antialiased glyph cell for the - * texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures - * are disabled so a per-codepoint raster plus pairwise kerning (measured - * via measureTextExact) fully describes a line. Little-endian packet: - * [advance u16, 26.6 fixed point] - * [bearingX s16] ink left relative to the pen position - * [inkTop s16] ink top relative to the line top (ascent above baseline) - * [width u16][height u16] - * [width*height coverage bytes, gamma-mapped] - * An ink-free glyph (space) has width = height = 0 but a valid advance. - * Empty array when the font cannot be loaded or the render fails. - */ - public static byte[] renderGlyphCell(String path, float sizePx, int codePoint, double gamma) { - Typeface typeface = loadTypeface(path); - if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) { - return new byte[0]; - } - try { - Paint paint = glyphPaint(typeface, sizePx); - String text = new String(Character.toChars(codePoint)); - float advance = paint.measureText(text); - int advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f))); - - android.graphics.Rect bounds = new android.graphics.Rect(); - paint.getTextBounds(text, 0, text.length(), bounds); - if (bounds.isEmpty()) { - return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null); - } - // getTextBounds can be off by a hair on AA edges; render with - // padding, then trim to the actually painted box. - int pad = 2; - int bw = bounds.width() + 2 * pad; - int bh = bounds.height() + 2 * pad; - if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) { - return new byte[0]; - } - int penX = pad - bounds.left; - int baselineY = pad - bounds.top; - Bitmap bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(bitmap); - canvas.drawText(text, penX, baselineY, paint); - byte[] pixels = alpha8Pixels(bitmap, bw, bh); - bitmap.recycle(); - - int x0 = bw, x1 = -1, y0 = bh, y1 = -1; - for (int y = 0; y < bh; y++) { - int row = y * bw; - for (int x = 0; x < bw; x++) { - if (pixels[row + x] != 0) { - if (x < x0) x0 = x; - if (x > x1) x1 = x; - if (y < y0) y0 = y; - if (y > y1) y1 = y; - } - } - } - if (x1 < x0) { - return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null); - } - int width = x1 - x0 + 1; - int height = y1 - y0 + 1; - Paint.FontMetricsInt fm = paint.getFontMetricsInt(); - int bearingX = x0 - penX; - int inkTop = (-fm.ascent) + (y0 - baselineY); - - byte[] lut = gammaLut(gamma); - byte[] coverage = new byte[width * height]; - for (int y = 0; y < height; y++) { - int src = (y0 + y) * bw + x0; - int dst = y * width; - for (int x = 0; x < width; x++) { - coverage[dst + x] = lut[pixels[src + x] & 0xff]; - } - } - return glyphCellPacket(advanceFixed, bearingX, inkTop, width, height, coverage); - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e); - return new byte[0]; - } - } - - /** - * Exact (float) advance width of a text run, with the same paint setup as - * renderGlyphCell (subpixel advances, ligatures off), so pair kerning can - * be derived as measure(ab) - measure(a) - measure(b). - */ - public static double measureTextExact(String path, String text, float sizePx) { - Typeface typeface = loadTypeface(path); - if (typeface == null || text == null || sizePx <= 0) { - return 0; - } - return glyphPaint(typeface, sizePx).measureText(text); - } - - /** Paint used by both the glyph-cell renderer and its measurements. */ - private static Paint glyphPaint(Typeface typeface, float sizePx) { - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - paint.setColor(Color.WHITE); - // Ligatures merge codepoints into one glyph, which the per-codepoint - // cache model cannot represent; kerning still applies. - paint.setFontFeatureSettings("'liga' off, 'clig' off"); - return paint; - } - - private static byte[] glyphCellPacket( - int advanceFixed, int bearingX, int inkTop, int width, int height, byte[] coverage) { - byte[] out = new byte[10 + (coverage == null ? 0 : coverage.length)]; - out[0] = (byte) (advanceFixed & 0xff); - out[1] = (byte) ((advanceFixed >> 8) & 0xff); - out[2] = (byte) (bearingX & 0xff); - out[3] = (byte) ((bearingX >> 8) & 0xff); - out[4] = (byte) (inkTop & 0xff); - out[5] = (byte) ((inkTop >> 8) & 0xff); - out[6] = (byte) (width & 0xff); - out[7] = (byte) ((width >> 8) & 0xff); - out[8] = (byte) (height & 0xff); - out[9] = (byte) ((height >> 8) & 0xff); - if (coverage != null) { - System.arraycopy(coverage, 0, out, 10, coverage.length); - } - return out; - } - - /** - * Line metrics for a font at a pixel size, as "ascent descent lineGap" - * (integers, pixels). Empty string if the font cannot be loaded. - */ - public static String getFontMetrics(String path, float sizePx) { - Typeface typeface = loadTypeface(path); - if (typeface == null || sizePx <= 0) { - return ""; - } - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - Paint.FontMetricsInt fm = paint.getFontMetricsInt(); - int lineGap = fm.leading; - return (-fm.ascent) + " " + fm.descent + " " + lineGap; - } - - /** Advance width in pixels of a line of text (rounded up). */ - public static int measureText(String path, String text, float sizePx) { - Typeface typeface = loadTypeface(path); - if (typeface == null || text == null || sizePx <= 0) { - return 0; - } - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - return (int) Math.ceil(paint.measureText(text)); - } - - /** - * Whether the font file loads at all (strictly validated on API 29+; - * best-effort below that). - */ - public static boolean canLoadFont(String path) { - return loadTypeface(path) != null; - } - - /** - * The font's family and style names from its 'name' table, as - * "Family\nStyle" ("Style" may be empty). Falls back to "" when the - * table cannot be parsed; callers should then use the filename. - */ - public static String getFontName(String path) { - try { - return parseNameTable(path); - } catch (Exception e) { - Log.w(TAG, "name table parse failed: " + path, e); - return ""; - } - } - - private static synchronized Typeface loadTypeface(String path) { - if (path == null) { - return null; - } - Typeface cached = typefaceCache.get(path); - if (cached != null) { - return cached; - } - File file = new File(path); - if (!file.isFile() || !file.canRead()) { - return null; - } - Typeface typeface = null; - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - // Strict path: Font.Builder throws on files that are not - // valid fonts, unlike createFromFile which silently falls - // back to the default typeface. - android.graphics.fonts.Font font = - new android.graphics.fonts.Font.Builder(file).build(); - typeface = new Typeface.CustomFallbackBuilder( - new android.graphics.fonts.FontFamily.Builder(font).build()) - .build(); - } else { - typeface = Typeface.createFromFile(file); - if (typeface == null || typeface.equals(Typeface.DEFAULT)) { - typeface = null; - } - } - } catch (Exception | LinkageError e) { - Log.w(TAG, "font load failed: " + path + ": " + e); - typeface = null; - } - if (typeface != null) { - typefaceCache.put(path, typeface); - } - return typeface; - } - - /** - * The coverage bytes of an ALPHA_8 bitmap as a tightly-packed - * width*height array (copyPixelsToBuffer copies rowBytes*height, and the - * row stride is not guaranteed to equal the width). - */ - private static byte[] alpha8Pixels(Bitmap bitmap, int width, int height) { - int rowBytes = bitmap.getRowBytes(); - byte[] raw = new byte[rowBytes * height]; - bitmap.copyPixelsToBuffer(ByteBuffer.wrap(raw)); - if (rowBytes == width) { - return raw; - } - byte[] pixels = new byte[width * height]; - for (int y = 0; y < height; y++) { - System.arraycopy(raw, y * rowBytes, pixels, y * width, width); - } - return pixels; - } - - /** [first, last] painted (nonzero) column indexes, or null if blank. */ - private static int[] paintedColumnRange(byte[] pixels, int width, int height) { - int first = width; - int last = -1; - for (int y = 0; y < height; y++) { - int row = y * width; - for (int x = 0; x < first; x++) { - if (pixels[row + x] != 0) { - first = x; - break; - } - } - for (int x = width - 1; x > last; x--) { - if (pixels[row + x] != 0) { - last = x; - break; - } - } - } - return last >= first ? new int[] { first, last } : null; - } - - private static byte[] gammaLut(double gamma) { - byte[] lut = new byte[256]; - if (gamma <= 0) { - gamma = 1.0; - } - for (int i = 0; i < 256; i++) { - lut[i] = (byte) Math.max(0, Math.min(255, - (int) Math.round(255.0 * Math.pow(i / 255.0, gamma)))); - } - return lut; - } - - // --- Minimal 'name' table reader (TTF/OTF/TTC) for preview titles --- - - private static String parseNameTable(String path) throws Exception { - try (RandomAccessFile raf = new RandomAccessFile(path, "r")) { - long fileSize = raf.length(); - if (fileSize < 12) { - return ""; - } - long offset = 0; - int tag = readU32(raf, 0); - if (tag == 0x74746366) { // 'ttcf': use the first face - if (readU32(raf, 8) < 1) { - return ""; - } - offset = readU32(raf, 12) & 0xffffffffL; - } - int numTables = readU16(raf, offset + 4); - long nameOffset = -1; - for (int i = 0; i < numTables; i++) { - long rec = offset + 12 + i * 16L; - if (rec + 16 > fileSize) { - return ""; - } - if (readU32(raf, rec) == 0x6e616d65) { // 'name' - nameOffset = readU32(raf, rec + 8) & 0xffffffffL; - break; - } - } - if (nameOffset < 0 || nameOffset + 6 > fileSize) { - return ""; - } - int count = readU16(raf, nameOffset + 2); - long stringStorage = nameOffset + readU16(raf, nameOffset + 4); - String family = null; - String style = null; - String preferredFamily = null; - String preferredStyle = null; - for (int i = 0; i < count; i++) { - long rec = nameOffset + 6 + i * 12L; - if (rec + 12 > fileSize) { - break; - } - int platform = readU16(raf, rec); - int nameId = readU16(raf, rec + 6); - if (nameId != 1 && nameId != 2 && nameId != 16 && nameId != 17) { - continue; - } - int length = readU16(raf, rec + 8); - long strOffset = stringStorage + readU16(raf, rec + 10); - if (strOffset + length > fileSize || length <= 0 || length > 512) { - continue; - } - byte[] data = new byte[length]; - raf.seek(strOffset); - raf.readFully(data); - // Platform 0 (Unicode) and 3 (Windows) store UTF-16BE; - // platform 1 (Mac) is close enough to Latin-1 for names. - String value = platform == 1 - ? new String(data, java.nio.charset.StandardCharsets.ISO_8859_1) - : new String(data, java.nio.charset.StandardCharsets.UTF_16BE); - value = value.trim(); - if (value.isEmpty()) { - continue; - } - if (nameId == 1 && family == null) family = value; - if (nameId == 2 && style == null) style = value; - if (nameId == 16 && preferredFamily == null) preferredFamily = value; - if (nameId == 17 && preferredStyle == null) preferredStyle = value; - } - String outFamily = preferredFamily != null ? preferredFamily : family; - String outStyle = preferredStyle != null ? preferredStyle : style; - if (outFamily == null) { - return ""; - } - return outFamily + "\n" + (outStyle == null ? "" : outStyle); - } - } - - private static int readU16(RandomAccessFile raf, long offset) throws Exception { - raf.seek(offset); - byte[] b = new byte[2]; - raf.readFully(b); - return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getShort() & 0xffff; - } - - private static int readU32(RandomAccessFile raf, long offset) throws Exception { - raf.seek(offset); - byte[] b = new byte[4]; - raf.readFully(b); - return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getInt(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.kt new file mode 100644 index 00000000..fa33d1c3 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.kt @@ -0,0 +1,377 @@ +package com.faceclaw.app + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Typeface +import android.os.Build +import android.util.Log + +import java.io.File +import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.util.LinkedHashMap + +/** + * Rasterizes text using font files (TTF/OTF/TTC) via the Android text stack, + * so shaping, kerning, and antialiasing are all delegated to + * minikin/HarfBuzz. Output uses the same grayscale packet format as + * ImageFileLoader ([widthLo, widthHi, heightLo, heightHi, pixels...], one + * byte per pixel, row-major), which the TS side turns into a GrayImage and + * the compositor later quantizes to the 4bpp the firmware wants. The packet + * layouts, gamma mapping and ink cropping are shared with iOS in GlyphPacket; + * only rasterization lives here. + * + * The gamma parameter maps antialiased coverage to output shade: + * out = 255 * (coverage/255)^gamma. The G2 display response looks roughly + * linear, so 1.0 is the expected default; the font previewer exposes it for + * on-hardware comparison. + */ +class FontFileRenderer private constructor() { + companion object { + private const val TAG = "FontFileRenderer" + private const val TYPEFACE_CACHE_SIZE = 4 + + /** Small LRU of loaded typefaces keyed by file path. */ + private val typefaceCache: MutableMap = + object : LinkedHashMap(TYPEFACE_CACHE_SIZE, 0.75f, true) { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry): Boolean { + return size > TYPEFACE_CACHE_SIZE + } + } + + /** + * Render one line of text with the given font file at the given pixel + * size. The returned image is the font's full vertical extent (top to + * bottom, taller than ascent+descent), plus any horizontal glyph + * overhang beyond the advance width. + * + * Returns an empty array if the font cannot be loaded or the text + * renders to nothing. + */ + @JvmStatic + fun renderText(path: String?, text: String?, sizePx: Float, gamma: Double): ByteArray { + if (path == null || text == null || sizePx <= 0) { + return ByteArray(0) + } + val typeface = loadTypeface(path) + if (typeface == null) { + return ByteArray(0) + } + try { + val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + paint.color = Color.WHITE + + // top/bottom (the font's maximal extent) rather than + // ascent/descent, so diacritics and swashes don't clip. + val fm = paint.fontMetricsInt + val ascent = -fm.top + val height = -fm.top + fm.bottom + // measureText gives advance width; italic/swash glyphs can paint + // outside it, so pad by half an em on each side and trim after. + val pad = Math.ceil((sizePx / 2).toDouble()).toInt() + val advance = Math.ceil(paint.measureText(text).toDouble()).toInt() + val width = advance + 2 * pad + if (width <= 0 || height <= 0 || width * height > 4_000_000) { + return ByteArray(0) + } + + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8) + val canvas = Canvas(bitmap) + canvas.drawText(text, pad.toFloat(), ascent.toFloat(), paint) + val pixels = alpha8Pixels(bitmap, width, height) + bitmap.recycle() + + // Trim the horizontal padding down to the painted extent, but + // keep at least the advance width so spacing stays truthful. + var left = pad + var right = pad + advance + val painted = GlyphPacket.paintedColumnRange(pixels, width, height) + if (painted != null) { + left = Math.min(left, painted[0]) + right = Math.max(right, painted[1] + 1) + } + val outWidth = Math.max(1, right - left) + return GlyphPacket.packImageRegion(pixels, width, left, 0, outWidth, height, gamma) + } catch (e: Exception) { + Log.w(TAG, "text render failed: $path", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "text render failed: $path", e) + return ByteArray(0) + } + } + + /** + * Render a multi-line block of text wrapped to maxWidth, with line + * breaking delegated to StaticLayout (so it matches Android text + * behavior, including breaking inside long words). Truncated with an + * ellipsis past maxLines. Same packet format as renderText. + */ + @JvmStatic + fun renderWrapped( + path: String?, text: String?, sizePx: Float, maxWidth: Int, gamma: Double, maxLines: Int): ByteArray { + if (path == null || text == null || sizePx <= 0 || maxWidth <= 0 || maxLines <= 0) { + return ByteArray(0) + } + val typeface = loadTypeface(path) + if (typeface == null) { + return ByteArray(0) + } + try { + val paint = android.text.TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + paint.color = Color.WHITE + paint.hinting = Paint.HINTING_ON + + val layout = android.text.StaticLayout.Builder + .obtain(text, 0, text.length, paint, maxWidth) + .setAlignment(android.text.Layout.Alignment.ALIGN_NORMAL) + .setIncludePad(false) + .setMaxLines(maxLines) + .setEllipsize(android.text.TextUtils.TruncateAt.END) + .build() + val height = Math.max(1, layout.height) + if (maxWidth.toLong() * height > 4_000_000L) { + return ByteArray(0) + } + + val bitmap = Bitmap.createBitmap(maxWidth, height, Bitmap.Config.ALPHA_8) + val canvas = Canvas(bitmap) + layout.draw(canvas) + val pixels = alpha8Pixels(bitmap, maxWidth, height) + bitmap.recycle() + + return GlyphPacket.packImage(pixels, maxWidth, height, gamma) + } catch (e: Exception) { + Log.w(TAG, "wrapped render failed: $path", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "wrapped render failed: $path", e) + return ByteArray(0) + } + } + + /** + * Render one codepoint as a tight-ink-box antialiased glyph cell for the + * texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures + * are disabled so a per-codepoint raster plus pairwise kerning (measured + * via measureTextExact) fully describes a line. Little-endian packet: + * [advance u16, 26.6 fixed point] + * [bearingX s16] ink left relative to the pen position + * [inkTop s16] ink top relative to the line top (ascent above baseline) + * [width u16][height u16] + * [width*height coverage bytes, gamma-mapped] + * An ink-free glyph (space) has width = height = 0 but a valid advance. + * Empty array when the font cannot be loaded or the render fails. + */ + @JvmStatic + fun renderGlyphCell(path: String?, sizePx: Float, codePoint: Int, gamma: Double): ByteArray { + val typeface = loadTypeface(path) + if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) { + return ByteArray(0) + } + try { + val paint = glyphPaint(typeface, sizePx) + val text = String(Character.toChars(codePoint)) + val advance = paint.measureText(text) + val advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f))) + + val bounds = android.graphics.Rect() + paint.getTextBounds(text, 0, text.length, bounds) + if (bounds.isEmpty) { + return GlyphPacket.emptyGlyphCell(advanceFixed) + } + // getTextBounds can be off by a hair on AA edges; render with + // padding, then trim to the actually painted box. + val pad = 2 + val bw = bounds.width() + 2 * pad + val bh = bounds.height() + 2 * pad + if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) { + return ByteArray(0) + } + val penX = pad - bounds.left + val baselineY = pad - bounds.top + val bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8) + val canvas = Canvas(bitmap) + canvas.drawText(text, penX.toFloat(), baselineY.toFloat(), paint) + val pixels = alpha8Pixels(bitmap, bw, bh) + bitmap.recycle() + + // Trim to the tight ink box; bearing/inkTop become pen- and + // line-relative inside the shared packer. + val fm = paint.fontMetricsInt + return GlyphPacket.packGlyphCell( + advanceFixed, pixels, bw, bh, penX, baselineY, -fm.ascent, gamma) + } catch (e: Exception) { + Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e) + return ByteArray(0) + } + } + + /** + * Exact (float) advance width of a text run, with the same paint setup as + * renderGlyphCell (subpixel advances, ligatures off), so pair kerning can + * be derived as measure(ab) - measure(a) - measure(b). + */ + @JvmStatic + fun measureTextExact(path: String?, text: String?, sizePx: Float): Double { + val typeface = loadTypeface(path) + if (typeface == null || text == null || sizePx <= 0) { + return 0.0 + } + return glyphPaint(typeface, sizePx).measureText(text).toDouble() + } + + /** Paint used by both the glyph-cell renderer and its measurements. */ + @JvmStatic + private fun glyphPaint(typeface: Typeface, sizePx: Float): Paint { + val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + paint.color = Color.WHITE + // Ligatures merge codepoints into one glyph, which the per-codepoint + // cache model cannot represent; kerning still applies. + paint.fontFeatureSettings = "'liga' off, 'clig' off" + return paint + } + + /** + * Line metrics for a font at a pixel size, as "ascent descent lineGap" + * (integers, pixels). Empty string if the font cannot be loaded. + */ + @JvmStatic + fun getFontMetrics(path: String?, sizePx: Float): String { + val typeface = loadTypeface(path) + if (typeface == null || sizePx <= 0) { + return "" + } + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + val fm = paint.fontMetricsInt + val lineGap = fm.leading + return (-fm.ascent).toString() + " " + fm.descent + " " + lineGap + } + + /** Advance width in pixels of a line of text (rounded up). */ + @JvmStatic + fun measureText(path: String?, text: String?, sizePx: Float): Int { + val typeface = loadTypeface(path) + if (typeface == null || text == null || sizePx <= 0) { + return 0 + } + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + return Math.ceil(paint.measureText(text).toDouble()).toInt() + } + + /** + * Whether the font file loads at all (strictly validated on API 29+; + * best-effort below that). + */ + @JvmStatic + fun canLoadFont(path: String?): Boolean { + return loadTypeface(path) != null + } + + /** + * The font's family and style names from its 'name' table, as + * "Family\nStyle" ("Style" may be empty). Falls back to "" when the + * table cannot be parsed; callers should then use the filename. + */ + @JvmStatic + fun getFontName(path: String?): String { + if (path == null) { + return "" + } + try { + RandomAccessFile(path, "r").use { raf -> + return OpenTypeNames.parse(object : RandomAccessBytes { + override val length: Long + get() = raf.length() + + override fun read(offset: Long, into: ByteArray, count: Int) { + raf.seek(offset) + raf.readFully(into, 0, count) + } + }) + } + } catch (e: Exception) { + Log.w(TAG, "name table parse failed: $path", e) + return "" + } + } + + @JvmStatic + @Synchronized + private fun loadTypeface(path: String?): Typeface? { + if (path == null) { + return null + } + val cached = typefaceCache[path] + if (cached != null) { + return cached + } + val file = File(path) + if (!file.isFile || !file.canRead()) { + return null + } + var typeface: Typeface? = null + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + // Strict path: Font.Builder throws on files that are not + // valid fonts, unlike createFromFile which silently falls + // back to the default typeface. + val font = android.graphics.fonts.Font.Builder(file).build() + typeface = Typeface.CustomFallbackBuilder( + android.graphics.fonts.FontFamily.Builder(font).build()) + .build() + } else { + typeface = Typeface.createFromFile(file) + if (typeface == null || typeface == Typeface.DEFAULT) { + typeface = null + } + } + } catch (e: Exception) { + Log.w(TAG, "font load failed: $path: $e") + typeface = null + } catch (e: LinkageError) { + Log.w(TAG, "font load failed: $path: $e") + typeface = null + } + if (typeface != null) { + typefaceCache[path] = typeface + } + return typeface + } + + /** + * The coverage bytes of an ALPHA_8 bitmap as a tightly-packed + * width*height array (copyPixelsToBuffer copies rowBytes*height, and the + * row stride is not guaranteed to equal the width). + */ + @JvmStatic + private fun alpha8Pixels(bitmap: Bitmap, width: Int, height: Int): ByteArray { + val rowBytes = bitmap.rowBytes + val raw = ByteArray(rowBytes * height) + bitmap.copyPixelsToBuffer(ByteBuffer.wrap(raw)) + if (rowBytes == width) { + return raw + } + val pixels = ByteArray(width * height) + for (y in 0 until height) { + System.arraycopy(raw, y * rowBytes, pixels, y * width, width) + } + return pixels + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java deleted file mode 100644 index 4dba5ac0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java +++ /dev/null @@ -1,567 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.os.SystemClock; -import android.util.Log; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -/** - * Per-frame timing instrumentation, shared between the Java BLE layer and the - * Typescript UI layer. A "frame" starts when we receive an input event (or a - * timer fires and we decide to redraw), and finishes when the resulting screen - * update has been fully transmitted to the glasses, is discarded unsent, or - * times out because nobody reported finishing it. - * - * Frames form a tree. One input event can fan out into several renders (the - * focused app's window plus the shell chrome, say), and each of those is its - * own frame linked back to the input frame that caused it via - * {@link #startFrame(String, int)}. The export nests descendants under their - * root and renders every line against the root's clock, so a single block - * shows the whole input-to-pixels story. Latency percentiles are measured over - * roots: root start to the first descendant that actually reached the glasses. - * - * All public methods are thread-safe and cheap enough to call from the BLE - * worker thread, the Android main thread, the JS thread, and app worker - * threads. Frame IDs are positive ints; 0 means "no frame" and is silently - * ignored everywhere, so callers do not need to null-check. - * - * Statistics and full log lines for recent and slowest frames are periodically - * exported to getExternalFilesDir()/frame-timings.txt, retrievable via - * adb pull /sdcard/Android/data//files/frame-timings.txt - */ -public final class FrameTimings { - private static final String TAG = "FrameTimings"; - private static final FrameTimings INSTANCE = new FrameTimings(); - - /** Frames still open after this long are finished as "timeout". */ - private static final long FRAME_TIMEOUT_MS = 30_000; - private static final long EXPORT_INTERVAL_MS = 15_000; - private static final long SWEEP_INTERVAL_MS = 5_000; - private static final int RECENT_ROOTS_KEPT = 40; - private static final int SLOWEST_ROOTS_KEPT = 20; - private static final int SENT_DURATIONS_WINDOW = 512; - private static final int MAX_LINES_PER_FRAME = 200; - /** - * Frames retained for ID lookup. Well above the number any export can - * reference, so a late log/span/finish call always finds its frame; frames - * older than this are unreachable and get collected. - */ - private static final int FRAMES_RETAINED = 1024; - private static final String EXPORT_FILE_NAME = "frame-timings.txt"; - - public static FrameTimings getInstance() { - return INSTANCE; - } - - private static final class Line { - final long atMs; // SystemClock.elapsedRealtime() - final String thread; - final String message; - - Line(long atMs, String thread, String message) { - this.atMs = atMs; - this.thread = thread; - this.message = message; - } - } - - private static final class Frame { - final int id; - final long startedAtMs; // SystemClock.elapsedRealtime() - final long startedWallClockMs; - final Frame parent; - final List children = new ArrayList<>(0); - final List lines = new ArrayList<>(); - final Map openSpans = new LinkedHashMap<>(); - String reason; - long finishedAtMs; - String outcome; // null while open; "sent", "discarded: ...", "timeout: ..." - /** Set on the root once some frame in its subtree reached the glasses. */ - boolean subtreeSentRecorded; - - Frame(int id, String reason, long startedAtMs, long startedWallClockMs, Frame parent) { - this.id = id; - this.reason = reason; - this.startedAtMs = startedAtMs; - this.startedWallClockMs = startedWallClockMs; - this.parent = parent; - } - - Frame root() { - Frame frame = this; - while (frame.parent != null) { - frame = frame.parent; - } - return frame; - } - - boolean isOpen() { - return outcome == null; - } - - long durationMs() { - return finishedAtMs - startedAtMs; - } - - boolean wasSent() { - return outcome != null && outcome.startsWith("sent"); - } - - /** Last moment anything in this subtree happened, for whole-tree duration. */ - long subtreeEndAtMs(long nowMs) { - long end = isOpen() ? nowMs : finishedAtMs; - for (Frame child : children) { - end = Math.max(end, child.subtreeEndAtMs(nowMs)); - } - return end; - } - - boolean subtreeHasOpenFrame() { - if (isOpen()) { - return true; - } - for (Frame child : children) { - if (child.subtreeHasOpenFrame()) { - return true; - } - } - return false; - } - } - - private final Object lock = new Object(); - /** - * Every frame we still retain, keyed by ID: open frames plus enough - * history that late calls and the export can still resolve one. - */ - private final LinkedHashMap framesById = - new LinkedHashMap(64, 0.75f, false) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > FRAMES_RETAINED && !eldest.getValue().isOpen(); - } - }; - private final ArrayDeque recentRoots = new ArrayDeque<>(); - private final List slowestRoots = new ArrayList<>(); - private final ArrayDeque inputLatencies = new ArrayDeque<>(); - private final ArrayDeque renderLatencies = new ArrayDeque<>(); - private int nextFrameId = 1; - private long framesStarted; - private long framesSent; - private long framesDiscarded; - private long framesTimedOut; - private boolean exportDirty; - private File exportDir; - private Thread exportThread; - - private FrameTimings() { - } - - /** Idempotent; enables filesystem export. Safe to call from any constructor path. */ - public void init(Context context) { - File dir = context.getApplicationContext().getExternalFilesDir(null); - if (dir == null) { - dir = context.getApplicationContext().getFilesDir(); - } - synchronized (lock) { - exportDir = dir; - if (exportThread == null) { - exportThread = new Thread(this::exportLoop, "FrameTimingsExport"); - exportThread.setDaemon(true); - exportThread.start(); - } - } - } - - /** Begin a root frame; reason is a short label like "input:sys-event type=0". */ - public int startFrame(String reason) { - return startFrame(reason, 0); - } - - /** - * Begin a frame caused by an existing one (parentFrameId; 0 for a root). - * The child is nested under its root in the export and its latency counts - * against the root, so an input event that fans out into an app render and - * a shell-chrome render reads as one timeline instead of three. - */ - public int startFrame(String reason, int parentFrameId) { - long now = SystemClock.elapsedRealtime(); - Frame frame; - synchronized (lock) { - Frame parent = framesById.get(parentFrameId); - frame = new Frame(nextFrameId++, reason == null ? "" : reason, now, - System.currentTimeMillis(), parent); - framesById.put(frame.id, frame); - framesStarted++; - if (parent != null) { - parent.children.add(frame); - addLineLocked(parent, now, "spawned frame#" + frame.id + " (" + frame.reason + ")"); - } - } - return frame.id; - } - - /** - * Append to a frame's label, for facts only known after it started (which - * app is in the foreground, which window a render is for). Shows up in the - * frame's header line, so the export is scannable without reading bodies. - */ - public void annotate(int frameId, String text) { - if (text == null || text.isEmpty()) { - return; - } - synchronized (lock) { - Frame frame = framesById.get(frameId); - if (frame == null) { - return; - } - frame.reason = frame.reason.isEmpty() ? text : frame.reason + " " + text; - } - } - - public void log(int frameId, String message) { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - addLineLocked(frame, now, message); - } - } - - public void spanStart(int frameId, String name) { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - frame.openSpans.put(name, now); - addLineLocked(frame, now, "span " + name + " start"); - } - } - - public void spanEnd(int frameId, String name) { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - Long startedAt = frame.openSpans.remove(name); - if (startedAt == null) { - addLineLocked(frame, now, "span " + name + " end (start not recorded)"); - } else { - addLineLocked(frame, now, "span " + name + " end (" + (now - startedAt) + "ms)"); - } - } - } - - /** - * Finish a frame. Outcomes: "sent" (counted in latency percentiles), anything - * starting with "discarded", or "timeout". First finish wins; later calls for - * the same frame are ignored, so racing completion paths are safe. - */ - public void finishFrame(int frameId, String outcome) { - long now = SystemClock.elapsedRealtime(); - Frame finished; - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - finishFrameLocked(frame, now, outcome == null ? "discarded: no outcome given" : outcome); - finished = frame; - } - Log.i(TAG, "frame#" + finished.id + " [" + finished.reason + "] -> " + finished.outcome - + " in " + finished.durationMs() + "ms"); - } - - /** One-line stats summary, e.g. for showing in the phone UI. */ - public String statsSummary() { - synchronized (lock) { - long[] input = percentilesLocked(inputLatencies); - return "frames started=" + framesStarted + " sent=" + framesSent - + " discarded=" + framesDiscarded + " timeout=" + framesTimedOut - + (input == null - ? "" - : " | input-to-display p50=" + input[0] + "ms p90=" + input[1] - + "ms p99=" + input[2] + "ms max=" + input[3] + "ms"); - } - } - - // --------------------------------------------------------------------- - - /** The frame with this ID if it exists and is still open, else null. */ - private Frame openFrameLocked(int frameId) { - if (frameId <= 0) { - return null; - } - Frame frame = framesById.get(frameId); - return frame != null && frame.isOpen() ? frame : null; - } - - private void addLineLocked(Frame frame, long now, String message) { - if (frame.lines.size() >= MAX_LINES_PER_FRAME) { - if (frame.lines.size() == MAX_LINES_PER_FRAME) { - frame.lines.add(new Line(now, Thread.currentThread().getName(), "... line cap reached")); - } - return; - } - frame.lines.add(new Line(now, Thread.currentThread().getName(), message)); - } - - private void finishFrameLocked(Frame frame, long now, String outcome) { - frame.finishedAtMs = now; - frame.outcome = outcome; - for (Map.Entry open : frame.openSpans.entrySet()) { - frame.lines.add(new Line(now, Thread.currentThread().getName(), - "span " + open.getKey() + " never ended (started at +" - + (open.getValue() - frame.startedAtMs) + "ms)")); - } - frame.openSpans.clear(); - addLineLocked(frame, now, "finished: " + outcome); - - Frame root = frame.root(); - if (frame.wasSent()) { - framesSent++; - // The user-visible latency is input (or timer) to the first pixels - // that actually reached the glasses, wherever in the tree that was. - if (!root.subtreeSentRecorded) { - root.subtreeSentRecorded = true; - recordLatencyLocked(root, now - root.startedAtMs); - insertSlowestLocked(root); - } - } else if (outcome.startsWith("timeout")) { - framesTimedOut++; - } else { - framesDiscarded++; - } - - if (root == frame) { - recentRoots.addLast(frame); - while (recentRoots.size() > RECENT_ROOTS_KEPT) { - recentRoots.removeFirst(); - } - } - exportDirty = true; - } - - private void recordLatencyLocked(Frame root, long latencyMs) { - ArrayDeque bucket = root.reason.startsWith("input:") ? inputLatencies : renderLatencies; - bucket.addLast(latencyMs); - while (bucket.size() > SENT_DURATIONS_WINDOW) { - bucket.removeFirst(); - } - } - - private void insertSlowestLocked(Frame root) { - if (!slowestRoots.contains(root)) { - slowestRoots.add(root); - } - // Sorted and trimmed at export time, when every subtree duration is final. - } - - /** {p50, p90, p99, max} over a rolling latency window, or null if empty. */ - private long[] percentilesLocked(ArrayDeque window) { - if (window.isEmpty()) { - return null; - } - List sorted = new ArrayList<>(window); - Collections.sort(sorted); - return new long[] { - percentileOfSorted(sorted, 50), - percentileOfSorted(sorted, 90), - percentileOfSorted(sorted, 99), - sorted.get(sorted.size() - 1), - }; - } - - private static long percentileOfSorted(List sorted, int percentile) { - int index = (int) Math.ceil(percentile / 100.0 * sorted.size()) - 1; - return sorted.get(Math.max(0, Math.min(sorted.size() - 1, index))); - } - - private void sweepTimedOutFrames() { - long now = SystemClock.elapsedRealtime(); - List timedOut = new ArrayList<>(); - synchronized (lock) { - for (Frame frame : new ArrayList<>(framesById.values())) { - if (frame.isOpen() && now - frame.startedAtMs >= FRAME_TIMEOUT_MS) { - finishFrameLocked(frame, now, "timeout: never reported finished"); - timedOut.add(frame); - } - } - } - for (Frame frame : timedOut) { - Log.w(TAG, "frame#" + frame.id + " [" + frame.reason + "] timed out after " - + frame.durationMs() + "ms without being finished"); - } - } - - private void exportLoop() { - long lastExportAtMs = 0; - while (true) { - try { - Thread.sleep(SWEEP_INTERVAL_MS); - } catch (InterruptedException e) { - return; - } - try { - sweepTimedOutFrames(); - long now = SystemClock.elapsedRealtime(); - boolean shouldExport; - synchronized (lock) { - shouldExport = exportDirty && exportDir != null && now - lastExportAtMs >= EXPORT_INTERVAL_MS; - } - if (shouldExport) { - lastExportAtMs = now; - exportToFile(); - } - } catch (Throwable t) { - Log.w(TAG, "export loop error", t); - } - } - } - - private void exportToFile() { - String content; - File dir; - synchronized (lock) { - dir = exportDir; - if (dir == null) { - return; - } - content = buildExportLocked(); - exportDirty = false; - } - File target = new File(dir, EXPORT_FILE_NAME); - File temp = new File(dir, EXPORT_FILE_NAME + ".tmp"); - try { - try (Writer writer = new OutputStreamWriter(new FileOutputStream(temp), StandardCharsets.UTF_8)) { - writer.write(content); - } - if (!temp.renameTo(target)) { - Log.w(TAG, "failed to rename " + temp + " to " + target); - } - } catch (Throwable t) { - Log.w(TAG, "failed to export frame timings", t); - } - } - - private String buildExportLocked() { - long now = SystemClock.elapsedRealtime(); - SimpleDateFormat wallClockFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US); - StringBuilder out = new StringBuilder(64 * 1024); - out.append("FrameTimings export at ").append(wallClockFormat.format(new Date())).append('\n'); - out.append(statsSummaryLocked()).append('\n'); - out.append('\n'); - out.append("Frames are trees: an input event's own frame is the root, and the renders it\n"); - out.append("caused are indented under it with offsets measured from the root's start.\n"); - out.append('\n'); - - Collections.sort(slowestRoots, (a, b) -> - Long.compare(b.subtreeEndAtMs(now) - b.startedAtMs, a.subtreeEndAtMs(now) - a.startedAtMs)); - while (slowestRoots.size() > SLOWEST_ROOTS_KEPT) { - slowestRoots.remove(slowestRoots.size() - 1); - } - - out.append("=== slowest frames that reached the glasses ===\n"); - for (Frame frame : slowestRoots) { - appendTreeLocked(out, frame, frame, wallClockFormat, now); - } - - out.append("=== recent frames (oldest first) ===\n"); - for (Frame frame : recentRoots) { - appendTreeLocked(out, frame, frame, wallClockFormat, now); - } - return out.toString(); - } - - private String statsSummaryLocked() { - long[] input = percentilesLocked(inputLatencies); - long[] render = percentilesLocked(renderLatencies); - StringBuilder out = new StringBuilder(); - out.append("frames started=").append(framesStarted).append(" sent=").append(framesSent) - .append(" discarded=").append(framesDiscarded).append(" timeout=").append(framesTimedOut) - .append(" open=").append(openFrameCountLocked()); - appendPercentilesLocked(out, "input-to-display latency", inputLatencies.size(), input); - appendPercentilesLocked(out, "render-to-display latency", renderLatencies.size(), render); - return out.toString(); - } - - private void appendPercentilesLocked(StringBuilder out, String label, int count, long[] percentiles) { - if (percentiles == null) { - return; - } - out.append('\n').append(label).append(" (last ").append(count).append("): p50=") - .append(percentiles[0]).append("ms p90=").append(percentiles[1]) - .append("ms p99=").append(percentiles[2]).append("ms max=").append(percentiles[3]).append("ms"); - } - - private int openFrameCountLocked() { - int open = 0; - for (Frame frame : framesById.values()) { - if (frame.isOpen()) { - open++; - } - } - return open; - } - - /** Print a frame and its descendants, all timed against root's start. */ - private void appendTreeLocked( - StringBuilder out, Frame frame, Frame root, SimpleDateFormat wallClockFormat, long now) { - int depth = 0; - for (Frame walk = frame; walk != root; walk = walk.parent) { - depth++; - } - String indent = repeat(" ", depth); - long startOffsetMs = frame.startedAtMs - root.startedAtMs; - out.append(indent).append("frame#").append(frame.id) - .append(" [").append(frame.reason).append(']'); - if (frame == root) { - out.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs))); - long totalMs = frame.subtreeEndAtMs(now) - frame.startedAtMs; - out.append(" duration ").append(frame.durationMs()).append("ms"); - if (!frame.children.isEmpty()) { - out.append(" (tree ").append(totalMs).append("ms") - .append(frame.subtreeHasOpenFrame() ? ", still open" : "").append(')'); - } - } else { - out.append(" started +").append(startOffsetMs).append("ms") - .append(" duration ").append(frame.isOpen() ? "open" : (frame.durationMs() + "ms")); - } - out.append(" outcome ").append(frame.outcome == null ? "(open)" : frame.outcome).append('\n'); - for (Line line : frame.lines) { - out.append(indent).append(String.format(Locale.US, " %+7dms [%s] %s", - line.atMs - root.startedAtMs, line.thread, line.message)).append('\n'); - } - for (Frame child : frame.children) { - appendTreeLocked(out, child, root, wallClockFormat, now); - } - } - - private static String repeat(String unit, int times) { - StringBuilder out = new StringBuilder(unit.length() * times); - for (int i = 0; i < times; i++) { - out.append(unit); - } - return out.toString(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.kt new file mode 100644 index 00000000..f59a9d56 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.kt @@ -0,0 +1,123 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.SystemClock +import android.util.Log + +import java.io.File +import java.io.FileOutputStream +import java.io.OutputStreamWriter +import java.io.Writer +import java.nio.charset.StandardCharsets + +/** + * Android facade over the shared [FrameTimingsCore] (per-frame latency + * instrumentation; see that class for the frame-tree model). This class keeps + * the process-wide singleton the BLE layer and TypeScript call by name, and owns + * the Android-only parts: the export directory derived from the Context and the + * daemon thread that sweeps timed-out frames and writes the export file. + * + * Statistics and full log lines for recent and slowest frames are periodically + * exported to getExternalFilesDir()/frame-timings.txt, retrievable via + * adb pull /sdcard/Android/data//files/frame-timings.txt + */ +class FrameTimings private constructor() { + companion object { + private const val TAG = FrameTimingsCore.TAG + private val INSTANCE = FrameTimings() + private const val EXPORT_FILE_NAME = "frame-timings.txt" + + @JvmStatic + fun getInstance(): FrameTimings { + return INSTANCE + } + } + + /** The shared core; the glasses session core records frames into it directly. */ + val core = FrameTimingsCore(AndroidProtocolPlatform) + private val lock = Any() + private var exportDir: File? = null + private var exportThread: Thread? = null + + /** Idempotent; enables filesystem export. Safe to call from any constructor path. */ + fun init(context: Context) { + var dir = context.applicationContext.getExternalFilesDir(null) + if (dir == null) { + dir = context.applicationContext.filesDir + } + synchronized(lock) { + exportDir = dir + if (exportThread == null) { + val thread = Thread(Runnable { exportLoop() }, "FrameTimingsExport") + thread.isDaemon = true + thread.start() + exportThread = thread + } + } + } + + /** Begin a root frame; reason is a short label like "input:sys-event type=0". */ + fun startFrame(reason: String?): Int = core.startFrame(reason) + + /** Begin a frame caused by an existing one (parentFrameId; 0 for a root). */ + fun startFrame(reason: String?, parentFrameId: Int): Int = core.startFrame(reason, parentFrameId) + + /** Append to a frame's label, for facts only known after it started. */ + fun annotate(frameId: Int, text: String?) = core.annotate(frameId, text) + + fun log(frameId: Int, message: String?) = core.log(frameId, message) + + fun spanStart(frameId: Int, name: String) = core.spanStart(frameId, name) + + fun spanEnd(frameId: Int, name: String) = core.spanEnd(frameId, name) + + /** + * Finish a frame. Outcomes: "sent" (counted in latency percentiles), anything + * starting with "discarded", or "timeout". First finish wins. + */ + fun finishFrame(frameId: Int, outcome: String?) = core.finishFrame(frameId, outcome) + + /** One-line stats summary, e.g. for showing in the phone UI. */ + fun statsSummary(): String = core.statsSummary() + + // --------------------------------------------------------------------- + + private fun exportLoop() { + var lastExportAtMs = 0L + while (true) { + try { + Thread.sleep(FrameTimingsCore.SWEEP_INTERVAL_MS) + } catch (e: InterruptedException) { + return + } + try { + core.sweepTimedOut() + val now = SystemClock.elapsedRealtime() + val dir: File? + synchronized(lock) { dir = exportDir } + if (dir != null && core.isExportDirty && now - lastExportAtMs >= FrameTimingsCore.EXPORT_INTERVAL_MS) { + lastExportAtMs = now + exportToFile(dir) + } + } catch (t: Throwable) { + Log.w(TAG, "export loop error", t) + } + } + } + + private fun exportToFile(dir: File) { + val content = core.takeExport() ?: return + val target = File(dir, EXPORT_FILE_NAME) + val temp = File(dir, "$EXPORT_FILE_NAME.tmp") + try { + (OutputStreamWriter(FileOutputStream(temp), StandardCharsets.UTF_8) as Writer).use { writer -> + writer.write(content) + } + if (!temp.renameTo(target)) { + Log.w(TAG, "failed to rename $temp to $target") + } + } catch (t: Throwable) { + Log.w(TAG, "failed to export frame timings", t) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java deleted file mode 100644 index 1346b07e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -/** Android storage adapter for the shared GIF recorder. */ -public final class GifScreenRecorder { - private final SharedGifScreenRecorder recorder = new SharedGifScreenRecorder(); - - public void addFrame(byte[] gray, int width, int height, long timestampMs) { - recorder.addFrame(gray, width, height, timestampMs); - } - - public boolean isOverflowed() { return recorder.isOverflowed(); } - - public String save(Context context) throws IOException { - byte[] gif = recorder.encode(); - if (gif.length == 0) return ""; - File file = new File(ScreenshotUtil.ensureScreenshotsDir(context), - "recording-" + ScreenshotUtil.timestamp() + ".gif"); - try (FileOutputStream out = new FileOutputStream(file)) { out.write(gif); } - return file.getAbsolutePath(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.kt new file mode 100644 index 00000000..e2fde296 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.kt @@ -0,0 +1,27 @@ +package com.faceclaw.app + +import android.content.Context +import java.io.File +import java.io.FileOutputStream +import java.io.IOException + +/** Android storage adapter for the shared GIF recorder. */ +class GifScreenRecorder { + private val recorder = SharedGifScreenRecorder() + + fun addFrame(gray: ByteArray?, width: Int, height: Int, timestampMs: Long) { + recorder.addFrame(gray, width, height, timestampMs) + } + + fun isOverflowed(): Boolean = recorder.isOverflowed() + + @Throws(IOException::class) + fun save(context: Context): String { + val gif = recorder.encode() + if (gif.isEmpty()) return "" + val file = File(ScreenshotUtil.ensureScreenshotsDir(context), + "recording-" + ScreenshotUtil.timestamp() + ".gif") + FileOutputStream(file).use { out -> out.write(gif) } + return file.absolutePath + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java deleted file mode 100644 index 80589f47..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java +++ /dev/null @@ -1,185 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Path; -import android.util.Log; - -import androidx.core.graphics.PathParser; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Renders a small subset of SVG (the shapes used by Lucide / simple Noun - * Project icons: path, circle, ellipse, rect, line, polyline, polygon) to a - * grayscale coverage buffer for use as an on-glasses icon. Icons are stroked - * (Lucide style: fill="none", 2px stroke, round caps/joins); the returned - * bytes are one coverage value per pixel (0=transparent .. 255=opaque white), - * row-major, size*size. Called once per icon and cached on the TS side. - */ -public final class IconRenderer { - private static final String TAG = "IconRenderer"; - private static final Pattern TAG_RE = Pattern.compile("<(path|circle|ellipse|rect|line|polyline|polygon)\\b([^>]*)>"); - private static final Pattern VIEWBOX_RE = Pattern.compile("viewBox\\s*=\\s*\"([^\"]*)\""); - - private IconRenderer() {} - - public static byte[] renderSvgGray(String svg, int size, float strokeWidth) { - if (svg == null || size <= 0) { - return new byte[0]; - } - try { - float minX = 0f, minY = 0f, viewW = 24f, viewH = 24f; - Matcher vb = VIEWBOX_RE.matcher(svg); - if (vb.find()) { - String[] parts = vb.group(1).trim().split("[\\s,]+"); - if (parts.length == 4) { - minX = Float.parseFloat(parts[0]); - minY = Float.parseFloat(parts[1]); - viewW = Float.parseFloat(parts[2]); - viewH = Float.parseFloat(parts[3]); - } - } - // Lucide-style icons are stroked outlines (fill="none"); everything - // else (Noun Project glyphs, brand logos) is a filled shape. - boolean stroked = svg.contains("fill=\"none\""); - Path path = new Path(); - Matcher m = TAG_RE.matcher(svg); - while (m.find()) { - appendElement(path, m.group(1), m.group(2)); - } - if (path.isEmpty()) { - return new byte[0]; - } - - Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - float scale = Math.min(size / viewW, size / viewH); - // Center the (square) viewBox in the bitmap, then map its origin. - canvas.translate((size - viewW * scale) / 2f, (size - viewH * scale) / 2f); - canvas.scale(scale, scale); - canvas.translate(-minX, -minY); - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setColor(0xffffffff); - if (stroked) { - paint.setStyle(Paint.Style.STROKE); - paint.setStrokeWidth(strokeWidth); - paint.setStrokeCap(Paint.Cap.ROUND); - paint.setStrokeJoin(Paint.Join.ROUND); - } else { - paint.setStyle(Paint.Style.FILL); - } - canvas.drawPath(path, paint); - - int[] pixels = new int[size * size]; - bitmap.getPixels(pixels, 0, size, 0, 0, size, size); - byte[] gray = new byte[size * size]; - for (int i = 0; i < pixels.length; i++) { - gray[i] = (byte) quantizeCoverage((pixels[i] >>> 24) & 0xff); // alpha = coverage - } - return gray; - } catch (Exception e) { - Log.w(TAG, "icon render failed", e); - return new byte[0]; - } - } - - /** - * Snap antialiased coverage toward hard edges so the 4bpp frames sent - * over BLE deflate better: mostly-transparent and mostly-opaque pixels - * become exactly 0/255 (long runs, and 0 stays color-key transparent for - * bitBlt), and only genuinely half-covered edge pixels keep one mid gray. - * The packed frame then uses a 3-symbol alphabet instead of all 16 - * levels, at icon sizes this is visually near-identical. - */ - private static int quantizeCoverage(int alpha) { - if (alpha < 64) return 0; - if (alpha >= 192) return 255; - return 128; - } - - private static void appendElement(Path path, String tag, String attrs) { - switch (tag) { - case "path": { - String d = attr(attrs, "d"); - if (d != null) { - Path sub = PathParser.createPathFromPathData(d); - if (sub != null) { - path.addPath(sub); - } - } - break; - } - case "circle": { - float cx = num(attrs, "cx", 0), cy = num(attrs, "cy", 0), r = num(attrs, "r", 0); - if (r > 0) path.addCircle(cx, cy, r, Path.Direction.CW); - break; - } - case "ellipse": { - float cx = num(attrs, "cx", 0), cy = num(attrs, "cy", 0); - float rx = num(attrs, "rx", 0), ry = num(attrs, "ry", 0); - if (rx > 0 && ry > 0) path.addOval(cx - rx, cy - ry, cx + rx, cy + ry, Path.Direction.CW); - break; - } - case "rect": { - float x = num(attrs, "x", 0), y = num(attrs, "y", 0); - float w = num(attrs, "width", 0), h = num(attrs, "height", 0); - float rx = num(attrs, "rx", 0), ry = num(attrs, "ry", rx > 0 ? rx : 0); - if (rx <= 0) rx = ry; - if (ry <= 0) ry = rx; - if (w > 0 && h > 0) { - if (rx > 0 || ry > 0) { - path.addRoundRect(x, y, x + w, y + h, rx, ry, Path.Direction.CW); - } else { - path.addRect(x, y, x + w, y + h, Path.Direction.CW); - } - } - break; - } - case "line": { - path.moveTo(num(attrs, "x1", 0), num(attrs, "y1", 0)); - path.lineTo(num(attrs, "x2", 0), num(attrs, "y2", 0)); - break; - } - case "polyline": - case "polygon": { - String points = attr(attrs, "points"); - if (points != null) { - String[] nums = points.trim().split("[\\s,]+"); - boolean started = false; - for (int i = 0; i + 1 < nums.length; i += 2) { - float px = Float.parseFloat(nums[i]); - float py = Float.parseFloat(nums[i + 1]); - if (!started) { - path.moveTo(px, py); - started = true; - } else { - path.lineTo(px, py); - } - } - if (tag.equals("polygon")) path.close(); - } - break; - } - default: - break; - } - } - - private static String attr(String attrs, String name) { - Matcher m = Pattern.compile("\\b" + name + "\\s*=\\s*\"([^\"]*)\"").matcher(attrs); - return m.find() ? m.group(1) : null; - } - - private static float num(String attrs, String name, float fallback) { - String value = attr(attrs, name); - if (value == null) return fallback; - try { - return Float.parseFloat(value.trim()); - } catch (NumberFormatException e) { - return fallback; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.kt new file mode 100644 index 00000000..efaf4ac4 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.kt @@ -0,0 +1,107 @@ +package com.faceclaw.app + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Path +import android.util.Log + +import androidx.core.graphics.PathParser + +/** + * Renders a small subset of SVG (the shapes used by Lucide / simple Noun + * Project icons: path, circle, ellipse, rect, line, polyline, polygon) to a + * grayscale coverage buffer for use as an on-glasses icon. Icons are stroked + * (Lucide style: fill="none", 2px stroke, round caps/joins); the returned + * bytes are one coverage value per pixel (0=transparent .. 255=opaque white), + * row-major, size*size. Called once per icon and cached on the TS side. + * + * Parsing, placement math and the coverage quantization are shared with iOS + * (SvgIconSource); this class only maps shapes onto android.graphics.Path and + * rasterizes them. + */ +class IconRenderer private constructor() { + companion object { + private const val TAG = "IconRenderer" + + @JvmStatic + fun renderSvgGray(svg: String?, size: Int, strokeWidth: Float): ByteArray { + if (svg == null || size <= 0) { + return ByteArray(0) + } + try { + val icon = SvgIconSource.parse(svg) ?: return ByteArray(0) + val path = Path() + for (shape in icon.shapes) { + appendShape(path, shape) + } + if (path.isEmpty) { + return ByteArray(0) + } + + val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + // Center the (square) viewBox in the bitmap, then map its origin. + val placement = icon.placement(size) + canvas.translate(placement[0], placement[1]) + canvas.scale(placement[2], placement[2]) + canvas.translate(-icon.minX, -icon.minY) + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.color = 0xffffffff.toInt() + if (icon.stroked) { + paint.style = Paint.Style.STROKE + paint.strokeWidth = strokeWidth + paint.strokeCap = Paint.Cap.ROUND + paint.strokeJoin = Paint.Join.ROUND + } else { + paint.style = Paint.Style.FILL + } + canvas.drawPath(path, paint) + + val pixels = IntArray(size * size) + bitmap.getPixels(pixels, 0, size, 0, 0, size, size) + return SvgIconSource.quantizeArgb(pixels) // alpha = coverage + } catch (e: Exception) { + Log.w(TAG, "icon render failed", e) + return ByteArray(0) + } + } + + @JvmStatic + private fun appendShape(path: Path, shape: SvgShape) { + when (shape) { + is SvgShape.PathData -> { + val sub = PathParser.createPathFromPathData(shape.d) + if (sub != null) { + path.addPath(sub) + } + } + is SvgShape.Circle -> path.addCircle(shape.cx, shape.cy, shape.r, Path.Direction.CW) + is SvgShape.Ellipse -> path.addOval( + shape.cx - shape.rx, shape.cy - shape.ry, shape.cx + shape.rx, shape.cy + shape.ry, Path.Direction.CW) + is SvgShape.Rect -> { + if (shape.rx > 0 || shape.ry > 0) { + path.addRoundRect(shape.x, shape.y, shape.x + shape.width, shape.y + shape.height, + shape.rx, shape.ry, Path.Direction.CW) + } else { + path.addRect(shape.x, shape.y, shape.x + shape.width, shape.y + shape.height, Path.Direction.CW) + } + } + is SvgShape.Line -> { + path.moveTo(shape.x1, shape.y1) + path.lineTo(shape.x2, shape.y2) + } + is SvgShape.Polyline -> { + val p = shape.points + path.moveTo(p[0], p[1]) + var i = 2 + while (i + 1 < p.size) { + path.lineTo(p[i], p[i + 1]) + i += 2 + } + if (shape.closed) path.close() + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java deleted file mode 100644 index ef6debbb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.util.Log; - -/** - * Decodes image files (and bitmaps generally) to the grayscale packet format - * used across the TS bridge: [widthLo, widthHi, heightLo, heightHi, - * pixels...] with one byte per pixel, row-major, or an empty array on - * failure. FaceclawMediaController's album art shares bitmapToGrayPacket; - * the TS side (app/native/image-files.ts) owns the shared photo-tone preset - * passed as (gamma, dither). - */ -public final class ImageFileLoader { - private static final String TAG = "ImageFileLoader"; - - private ImageFileLoader() {} - - /** - * Decode an image file and downscale it to fit within maxWidth x - * maxHeight, preserving aspect ratio and never upscaling. Flat - * conversion (no tone curve, per-pixel rounding): right for UI raster - * such as icons. - */ - public static byte[] loadGray(String path, int maxWidth, int maxHeight) { - return loadGray(path, maxWidth, maxHeight, 1f, false); - } - - /** - * As above, with the photographic tone handling described on - * bitmapToGrayPacket(Bitmap, int, int, float, boolean): use for - * continuous-tone images (photos) viewed on the glasses. - */ - public static byte[] loadGray( - String path, int maxWidth, int maxHeight, float gamma, boolean dither) { - if (path == null || maxWidth <= 0 || maxHeight <= 0) { - return new byte[0]; - } - try { - BitmapFactory.Options bounds = new BitmapFactory.Options(); - bounds.inJustDecodeBounds = true; - BitmapFactory.decodeFile(path, bounds); - if (bounds.outWidth <= 0 || bounds.outHeight <= 0) { - return new byte[0]; - } - // Power-of-two subsampling during decode keeps large photos from - // allocating full-size ARGB buffers; exact fitting happens in - // bitmapToGrayPacket. - BitmapFactory.Options opts = new BitmapFactory.Options(); - opts.inSampleSize = 1; - while (bounds.outWidth / (opts.inSampleSize * 2) >= maxWidth - && bounds.outHeight / (opts.inSampleSize * 2) >= maxHeight) { - opts.inSampleSize *= 2; - } - Bitmap bitmap = BitmapFactory.decodeFile(path, opts); - if (bitmap == null) { - return new byte[0]; - } - return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither); - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "image decode failed: " + path, e); - return new byte[0]; - } - } - - /** - * Decode an in-memory image file (PNG/JPEG/WebP bytes, e.g. a fetched - * map tile) and downscale to fit within maxWidth x maxHeight. Takes a - * ByteBuffer because NativeScript marshals a JS ArrayBuffer to one. - */ - public static byte[] loadGrayFromBytes(java.nio.ByteBuffer buffer, int maxWidth, int maxHeight) { - return loadGrayFromBytes(buffer, maxWidth, maxHeight, 1f, false); - } - - /** As above, with photographic tone handling (see bitmapToGrayPacket). */ - public static byte[] loadGrayFromBytes( - java.nio.ByteBuffer buffer, int maxWidth, int maxHeight, float gamma, boolean dither) { - if (buffer == null || maxWidth <= 0 || maxHeight <= 0) { - return new byte[0]; - } - try { - java.nio.ByteBuffer source = buffer.duplicate(); - source.rewind(); - byte[] data = new byte[source.remaining()]; - source.get(data); - if (data.length == 0) { - return new byte[0]; - } - Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); - if (bitmap == null) { - return new byte[0]; - } - return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither); - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "byte-array image decode failed", e); - return new byte[0]; - } - } - - /** - * Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect, - * never upscaling) and convert it to the grayscale packet format. - * Transparent pixels darken toward black (the on-glasses background). - */ - public static byte[] bitmapToGrayPacket(Bitmap source, int maxWidth, int maxHeight) { - return bitmapToGrayPacket(source, maxWidth, maxHeight, 1f, false); - } - - /** - * As above, with photographic tone handling for continuous-tone images - * (album art, photos) rather than UI raster: - * - * gamma > 1 darkens midtones (out = 255 * (in/255)^gamma). Source pixels - * are sRGB-encoded but the G2 drives its 16 levels roughly linearly, so - * mid-gray otherwise displays far brighter than intended; 2.2 undoes the - * sRGB encoding outright. - * - * dither error-diffuses against the 16 levels the frame is eventually - * packed to (BmpUtil.GRAY_TO_NIBBLE) instead of leaving each pixel to - * round on its own, which turns smooth gradients into visible bands. - * Dithered output is already quantized: every byte is a level times 16, - * which BmpUtil re-quantizes back to exactly that level. - */ - public static byte[] bitmapToGrayPacket( - Bitmap source, int maxWidth, int maxHeight, float gamma, boolean dither) { - if (source == null || source.getWidth() <= 0 || source.getHeight() <= 0 - || maxWidth <= 0 || maxHeight <= 0) { - return new byte[0]; - } - try { - float scale = Math.min(1f, Math.min( - (float) maxWidth / source.getWidth(), - (float) maxHeight / source.getHeight())); - int width = Math.max(1, Math.round(source.getWidth() * scale)); - int height = Math.max(1, Math.round(source.getHeight() * scale)); - Bitmap scaled = Bitmap.createScaledBitmap(source, width, height, true); - if (scaled.getConfig() == Bitmap.Config.HARDWARE) { - scaled = scaled.copy(Bitmap.Config.ARGB_8888, false); - } - int[] pixels = new int[width * height]; - scaled.getPixels(pixels, 0, width, 0, 0, width, height); - byte[] out = new byte[4 + width * height]; - out[0] = (byte) (width & 0xff); - out[1] = (byte) ((width >> 8) & 0xff); - out[2] = (byte) (height & 0xff); - out[3] = (byte) ((height >> 8) & 0xff); - int[] tone = toneCurve(gamma); - for (int i = 0; i < pixels.length; i++) { - int p = pixels[i]; - int a = (p >>> 24) & 0xff; - int r = (p >> 16) & 0xff; - int g = (p >> 8) & 0xff; - int b = p & 0xff; - out[4 + i] = (byte) tone[((r * 299 + g * 587 + b * 114) / 1000) * a / 255]; - } - if (dither) { - ditherToDisplayLevels(out, 4, width, height); - } - return out; - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "bitmap conversion failed", e); - return new byte[0]; - } - } - - /** 256-entry map of source gray to displayed gray for out = in^gamma. */ - private static int[] toneCurve(float gamma) { - int[] curve = new int[256]; - for (int v = 0; v < 256; v++) { - curve[v] = gamma == 1f - ? v - : Math.round(255f * (float) Math.pow(v / 255f, gamma)); - } - return curve; - } - - /** - * Floyd-Steinberg error diffusion onto the display's 16 gray levels, in - * place over an 8bpp plane. Serpentine scanning keeps the diffusion from - * building up a directional texture across wide flat areas. - * - * Level n is written back as n * 16 rather than the n * 17 it stands for - * so that BmpUtil's (v + 8) >> 4 reproduces n exactly; level 0 is written - * as 1, not 0, because 0 is the shell's color-key for transparent. - */ - private static void ditherToDisplayLevels(byte[] plane, int offset, int width, int height) { - float[] curr = new float[width]; - float[] next = new float[width]; - for (int x = 0; x < width; x++) { - curr[x] = plane[offset + x] & 0xff; - } - for (int y = 0; y < height; y++) { - int rowStart = offset + y * width; - int nextStart = rowStart + width; - boolean hasNext = y + 1 < height; - for (int x = 0; x < width; x++) { - next[x] = hasNext ? (plane[nextStart + x] & 0xff) : 0f; - } - boolean leftToRight = (y & 1) == 0; - int start = leftToRight ? 0 : width - 1; - int step = leftToRight ? 1 : -1; - for (int i = 0; i < width; i++) { - int x = start + i * step; - float wanted = curr[x]; - int level = Math.round(Math.min(255f, Math.max(0f, wanted)) / 17f); - float error = wanted - level * 17f; - plane[rowStart + x] = (byte) (level == 0 ? 1 : level * 16); - int ahead = x + step; - if (ahead >= 0 && ahead < width) { - curr[ahead] += error * (7f / 16f); - } - if (hasNext) { - if (ahead >= 0 && ahead < width) { - next[ahead] += error * (1f / 16f); - } - next[x] += error * (5f / 16f); - int behind = x - step; - if (behind >= 0 && behind < width) { - next[behind] += error * (3f / 16f); - } - } - } - float[] swap = curr; - curr = next; - next = swap; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt new file mode 100644 index 00000000..b73baee1 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt @@ -0,0 +1,165 @@ +package com.faceclaw.app + +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.util.Log + +/** + * Decodes image files (and bitmaps generally) to the grayscale packet format + * used across the TS bridge: [widthLo, widthHi, heightLo, heightHi, + * pixels...] with one byte per pixel, row-major, or an empty array on + * failure. FaceclawMediaController's album art shares bitmapToGrayPacket; + * the TS side (app/native/image-files.ts) owns the shared photo-tone preset + * passed as (gamma, dither). The pixel math (tone curve, alpha darkening, + * dither) lives in the shared GrayPacket so iOS produces identical bytes. + */ +class ImageFileLoader private constructor() { + companion object { + private const val TAG = "ImageFileLoader" + + /** + * Decode an image file and downscale it to fit within maxWidth x + * maxHeight, preserving aspect ratio and never upscaling. Flat + * conversion (no tone curve, per-pixel rounding): right for UI raster + * such as icons. + */ + @JvmStatic + fun loadGray(path: String?, maxWidth: Int, maxHeight: Int): ByteArray { + return loadGray(path, maxWidth, maxHeight, 1f, false) + } + + /** + * As above, with the photographic tone handling described on + * bitmapToGrayPacket(Bitmap, int, int, float, boolean): use for + * continuous-tone images (photos) viewed on the glasses. + */ + @JvmStatic + fun loadGray( + path: String?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + if (path == null || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + try { + val bounds = BitmapFactory.Options() + bounds.inJustDecodeBounds = true + BitmapFactory.decodeFile(path, bounds) + if (bounds.outWidth <= 0 || bounds.outHeight <= 0) { + return ByteArray(0) + } + // Power-of-two subsampling during decode keeps large photos from + // allocating full-size ARGB buffers; exact fitting happens in + // bitmapToGrayPacket. + val opts = BitmapFactory.Options() + opts.inSampleSize = 1 + while (bounds.outWidth / (opts.inSampleSize * 2) >= maxWidth + && bounds.outHeight / (opts.inSampleSize * 2) >= maxHeight) { + opts.inSampleSize *= 2 + } + val bitmap = BitmapFactory.decodeFile(path, opts) + if (bitmap == null) { + return ByteArray(0) + } + return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither) + } catch (e: Exception) { + Log.w(TAG, "image decode failed: $path", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "image decode failed: $path", e) + return ByteArray(0) + } + } + + /** + * Decode an in-memory image file (PNG/JPEG/WebP bytes, e.g. a fetched + * map tile) and downscale to fit within maxWidth x maxHeight. Takes a + * ByteBuffer because NativeScript marshals a JS ArrayBuffer to one. + */ + @JvmStatic + fun loadGrayFromBytes(buffer: java.nio.ByteBuffer?, maxWidth: Int, maxHeight: Int): ByteArray { + return loadGrayFromBytes(buffer, maxWidth, maxHeight, 1f, false) + } + + /** As above, with photographic tone handling (see bitmapToGrayPacket). */ + @JvmStatic + fun loadGrayFromBytes( + buffer: java.nio.ByteBuffer?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + if (buffer == null || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + try { + val source = buffer.duplicate() + source.rewind() + val data = ByteArray(source.remaining()) + source.get(data) + if (data.isEmpty()) { + return ByteArray(0) + } + val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size) + if (bitmap == null) { + return ByteArray(0) + } + return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither) + } catch (e: Exception) { + Log.w(TAG, "byte-array image decode failed", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "byte-array image decode failed", e) + return ByteArray(0) + } + } + + /** + * Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect, + * never upscaling) and convert it to the grayscale packet format. + * Transparent pixels darken toward black (the on-glasses background). + */ + @JvmStatic + fun bitmapToGrayPacket(source: Bitmap?, maxWidth: Int, maxHeight: Int): ByteArray { + return bitmapToGrayPacket(source, maxWidth, maxHeight, 1f, false) + } + + /** + * As above, with photographic tone handling for continuous-tone images + * (album art, photos) rather than UI raster: + * + * gamma > 1 darkens midtones (out = 255 * (in/255)^gamma). Source pixels + * are sRGB-encoded but the G2 drives its 16 levels roughly linearly, so + * mid-gray otherwise displays far brighter than intended; 2.2 undoes the + * sRGB encoding outright. + * + * dither error-diffuses against the 16 levels the frame is eventually + * packed to (BmpUtil.GRAY_TO_NIBBLE) instead of leaving each pixel to + * round on its own, which turns smooth gradients into visible bands. + * Dithered output is already quantized: every byte is a level times 16, + * which BmpUtil re-quantizes back to exactly that level. + */ + @JvmStatic + fun bitmapToGrayPacket( + source: Bitmap?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + if (source == null || source.width <= 0 || source.height <= 0 + || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + try { + val scale = Math.min(1f, Math.min( + maxWidth.toFloat() / source.width, + maxHeight.toFloat() / source.height)) + val width = Math.max(1, Math.round(source.width * scale)) + val height = Math.max(1, Math.round(source.height * scale)) + var scaled = Bitmap.createScaledBitmap(source, width, height, true) + if (scaled.config == Bitmap.Config.HARDWARE) { + scaled = scaled.copy(Bitmap.Config.ARGB_8888, false) + } + val pixels = IntArray(width * height) + scaled.getPixels(pixels, 0, width, 0, 0, width, height) + return GrayPacket.fromArgb(pixels, width, height, gamma, dither) + } catch (e: Exception) { + Log.w(TAG, "bitmap conversion failed", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "bitmap conversion failed", e) + return ByteArray(0) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.java deleted file mode 100644 index 4b599d67..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; - -import java.nio.ByteBuffer; - -/** - * Builds the phone-UI preview bitmap from an 8bpp grayscale frame. The - * grayscale buffer arrives as a ByteBuffer (NativeScript marshals a JS - * ArrayBuffer to one without copying element-by-element); doing the - * gray-to-ARGB expansion here keeps the 165KB-per-frame loop out of the - * JS/Java bridge, where it used to cost ~150ms per preview. - */ -public final class PreviewBitmapUtil { - private PreviewBitmapUtil() {} - - public static Bitmap fromGray(ByteBuffer gray, int width, int height, double brightenGamma) { - return fromGray(gray, width, height, brightenGamma, false); - } - - /** As above; `green` renders green-on-black (matching the physical glasses) instead of grayscale. */ - public static Bitmap fromGray(ByteBuffer gray, int width, int height, double brightenGamma, boolean green) { - if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) { - throw new IllegalArgumentException("invalid gray preview buffer"); - } - int[] lut = new int[256]; - for (int g = 0; g < 256; g++) { - int v = (int) Math.max(0, Math.min(255, Math.round(255 * Math.pow(g / 255.0, brightenGamma)))); - lut[g] = green ? (0xff000000 | (v << 8)) : (0xff000000 | (v << 16) | (v << 8) | v); - } - byte[] bytes = new byte[width * height]; - gray.get(bytes); - int[] colors = new int[width * height]; - for (int i = 0; i < colors.length; i++) { - colors[i] = lut[bytes[i] & 0xff]; - } - return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt new file mode 100644 index 00000000..54f31c42 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt @@ -0,0 +1,34 @@ +package com.faceclaw.app + +import android.graphics.Bitmap + +import java.nio.ByteBuffer + +/** + * Builds the phone-UI preview bitmap from an 8bpp grayscale frame. The + * grayscale buffer arrives as a ByteBuffer (NativeScript marshals a JS + * ArrayBuffer to one without copying element-by-element); doing the + * gray-to-ARGB expansion here keeps the 165KB-per-frame loop out of the + * JS/Java bridge, where it used to cost ~150ms per preview. The palette + * itself is the shared PreviewPalette. + */ +class PreviewBitmapUtil private constructor() { + companion object { + @JvmStatic + fun fromGray(gray: ByteBuffer?, width: Int, height: Int, brightenGamma: Double): Bitmap { + return fromGray(gray, width, height, brightenGamma, false) + } + + /** As above; `green` renders green-on-black (matching the physical glasses) instead of grayscale. */ + @JvmStatic + fun fromGray(gray: ByteBuffer?, width: Int, height: Int, brightenGamma: Double, green: Boolean): Bitmap { + if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) { + throw IllegalArgumentException("invalid gray preview buffer") + } + val bytes = ByteArray(width * height) + gray.get(bytes) + val colors = PreviewPalette.expand(bytes, 0, width * height, brightenGamma, green) + return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java deleted file mode 100644 index 313abece..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -/** - * Saves screenshots of the composited screen as 4-bit grayscale PNGs (the - * display's native depth). Retrieve with - * adb pull /sdcard/Android/data/com.faceclaw.app/files/screenshots/ - */ -public final class ScreenshotUtil { - private ScreenshotUtil() {} - - /** Returns the absolute path of the written file. */ - public static String savePngScreenshot(Context context, byte[] gray, int width, int height) throws IOException { - if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) { - throw new IllegalArgumentException("invalid screenshot buffer"); - } - byte[] png = SharedScreenshots.encode4BitGrayPng(gray, width, height); - File file = new File(ensureScreenshotsDir(context), "screen-" + timestamp() + ".png"); - try (FileOutputStream out = new FileOutputStream(file)) { - out.write(png); - } - return file.getAbsolutePath(); - } - - static File ensureScreenshotsDir(Context context) throws IOException { - File dir = new File(context.getExternalFilesDir(null), "screenshots"); - if (!dir.exists() && !dir.mkdirs()) { - throw new IOException("failed to create " + dir); - } - return dir; - } - - static String timestamp() { - return new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date()); - } - -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.kt new file mode 100644 index 00000000..c0d33c9b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.kt @@ -0,0 +1,49 @@ +package com.faceclaw.app + +import android.content.Context + +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +/** + * Saves screenshots of the composited screen as 4-bit grayscale PNGs (the + * display's native depth). Retrieve with + * adb pull /sdcard/Android/data/com.faceclaw.app/files/screenshots/ + */ +class ScreenshotUtil private constructor() { + companion object { + /** Returns the absolute path of the written file. */ + @JvmStatic + @Throws(IOException::class) + fun savePngScreenshot(context: Context, gray: ByteArray?, width: Int, height: Int): String { + if (gray == null || width <= 0 || height <= 0 || gray.size < width * height) { + throw IllegalArgumentException("invalid screenshot buffer") + } + val png = SharedScreenshots.encode4BitGrayPng(gray, width, height) + val file = File(ensureScreenshotsDir(context), "screen-" + timestamp() + ".png") + FileOutputStream(file).use { out -> + out.write(png) + } + return file.absolutePath + } + + @JvmStatic + @Throws(IOException::class) + fun ensureScreenshotsDir(context: Context): File { + val dir = File(context.getExternalFilesDir(null), "screenshots") + if (!dir.exists() && !dir.mkdirs()) { + throw IOException("failed to create $dir") + } + return dir + } + + @JvmStatic + fun timestamp(): String { + return SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(Date()) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/InterruptibleSleep.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/InterruptibleSleep.java deleted file mode 100644 index a77454a6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/InterruptibleSleep.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.faceclaw.app; - -import android.os.SystemClock; - -final class InterruptibleSleep { - private final Object lock = new Object(); - private boolean interrupted; - - boolean sleep(long ms) { - long delayMs = Math.max(1, ms); - long deadline = SystemClock.elapsedRealtime() + delayMs; - synchronized (lock) { - // Consume (do not clear-and-ignore) an interrupt delivered before this - // sleep began. interrupt() runs on other threads with no lock held by - // the sleeper between driveSession() returning and sleep() starting, so - // clearing the flag at entry lost that wake and forced the worker to - // sleep the full interval — up to 250ms per idle frame. Honoring it - // costs at most one extra (cheap) driveSession pass. - if (interrupted) { - interrupted = false; - return false; - } - while (true) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return true; - } - try { - lock.wait(remaining); - } catch (InterruptedException ignored) { - interrupted = false; - return false; - } - if (interrupted) { - interrupted = false; - return false; - } - } - } - } - - void interrupt() { - synchronized (lock) { - interrupted = true; - lock.notifyAll(); - } - } -} diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig index abc28fff..f657ddce 100644 --- a/App_Resources/iOS/build.xcconfig +++ b/App_Resources/iOS/build.xcconfig @@ -8,6 +8,10 @@ OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -f ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; HEADER_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../../App_Resources/iOS/src/lc3" +// NativeScript's xcconfig merge keeps only the CocoaPods HEADER_SEARCH_PATHS when the +// generated project is rebuilt, so the vendored liblc3 include dir is also passed as a flag +// (OTHER_CFLAGS merges like OTHER_LDFLAGS). +OTHER_CFLAGS = $(inherited) -I"$(SRCROOT)/../../App_Resources/iOS/src/lc3" // FaceclawKit ships arm64 device and Apple-silicon simulator slices. EXCLUDED_ARCHS[sdk=iphonesimulator*] = x86_64; diff --git a/App_Resources/iOS/src/FaceclawBluetooth.h b/App_Resources/iOS/src/FaceclawBluetooth.h deleted file mode 100644 index e59bf88c..00000000 --- a/App_Resources/iOS/src/FaceclawBluetooth.h +++ /dev/null @@ -1,14 +0,0 @@ -#import -#import - -/** Main-queue CoreBluetooth adapter. Protocol and MAC matching live in TS. */ -@interface FaceclawBluetooth : NSObject -@property (nonatomic, copy) void (^eventHandler)(NSString *json); -- (void)initializeBluetooth; -- (void)scan:(BOOL)enabled; -- (void)connect:(NSString *)identifier requestId:(NSInteger)requestId; -- (void)connect:(NSString *)identifier requiresANCS:(BOOL)requiresANCS requestId:(NSInteger)requestId; -- (void)subscribe:(NSString *)identifier characteristic:(NSString *)uuid requestId:(NSInteger)requestId; -- (void)write:(NSString *)identifier characteristic:(NSString *)uuid data:(NSData *)data requestId:(NSInteger)requestId; -- (void)disconnect:(NSString *)identifier; -@end diff --git a/App_Resources/iOS/src/FaceclawBluetooth.m b/App_Resources/iOS/src/FaceclawBluetooth.m deleted file mode 100644 index 6ba03e75..00000000 --- a/App_Resources/iOS/src/FaceclawBluetooth.m +++ /dev/null @@ -1,197 +0,0 @@ -#import "FaceclawBluetooth.h" - -@interface FCBPeripheral : NSObject -@property (nonatomic, strong) CBPeripheral *peripheral; -@property (nonatomic, strong) NSMutableDictionary *characteristics; -@property (nonatomic, strong) NSMutableDictionary *subscriptions; -@property (nonatomic, strong) NSMutableArray *writes; -@property (nonatomic, strong) NSNumber *connectRequest; -@property (nonatomic) NSInteger pendingServices; -@property (nonatomic) BOOL awaitingWriteResponse; -@end -@implementation FCBPeripheral -- (instancetype)init { - if ((self = [super init])) { - _characteristics = [NSMutableDictionary new]; _subscriptions = [NSMutableDictionary new]; - _writes = [NSMutableArray new]; - } - return self; -} -@end - -@interface FaceclawBluetooth () -@property (nonatomic, strong) CBCentralManager *central; -@property (nonatomic, strong) NSMutableDictionary *peers; -@property (nonatomic) BOOL scanRequested; -@end - -@implementation FaceclawBluetooth -- (instancetype)init { if ((self = [super init])) _peers = [NSMutableDictionary new]; return self; } -- (void)emit:(NSDictionary *)event { - if (!self.eventHandler) return; - NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; - if (data) self.eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); -} -- (void)complete:(NSNumber *)request error:(NSString *)error details:(NSDictionary *)details { - if (!request) return; - NSMutableDictionary *event = [@{ @"kind": @"completion", @"id": request } mutableCopy]; - if (error) event[@"error"] = error; - if (details) event[@"details"] = details; - [self emit:event]; -} -- (void)initializeBluetooth { - if (!self.central) self.central = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()]; - else [self centralManagerDidUpdateState:self.central]; -} -- (void)scan:(BOOL)enabled { - self.scanRequested = enabled; - if (!enabled) { [self.central stopScan]; return; } - [self initializeBluetooth]; - if (self.central.state == CBManagerStatePoweredOn) - [self.central scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}]; -} -- (void)centralManagerDidUpdateState:(CBCentralManager *)central { - [self emit:@{@"kind": @"state", @"state": @(central.state), @"authorization": @(CBManager.authorization)}]; - if (central.state != CBManagerStatePoweredOn) { - for (FCBPeripheral *peer in self.peers.allValues) [self failPeer:peer reason:@"Bluetooth is unavailable"]; - } else if (self.scanRequested) { - [central scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}]; - } -} -- (FCBPeripheral *)remember:(CBPeripheral *)peripheral { - NSString *identifier = peripheral.identifier.UUIDString; - FCBPeripheral *peer = self.peers[identifier]; - if (!peer) { peer = [FCBPeripheral new]; peer.peripheral = peripheral; self.peers[identifier] = peer; } - peripheral.delegate = self; - return peer; -} -- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisement RSSI:(NSNumber *)RSSI { - [self remember:peripheral]; - NSData *data = advertisement[CBAdvertisementDataManufacturerDataKey]; - NSMutableString *hex = [NSMutableString new]; - const uint8_t *bytes = data.bytes; - for (NSUInteger i = 0; i < data.length; i++) [hex appendFormat:@"%02x", bytes[i]]; - [self emit:@{@"kind": @"advertisement", @"identifier": peripheral.identifier.UUIDString, - @"name": advertisement[CBAdvertisementDataLocalNameKey] ?: peripheral.name ?: @"", - @"manufacturerData": hex, @"rssi": RSSI, - @"connectable": advertisement[CBAdvertisementDataIsConnectable] ?: @YES}]; -} -- (void)connect:(NSString *)identifier requestId:(NSInteger)requestId { - [self connect:identifier requiresANCS:NO requestId:requestId]; -} -- (void)connect:(NSString *)identifier requiresANCS:(BOOL)requiresANCS requestId:(NSInteger)requestId { - if (self.central.state != CBManagerStatePoweredOn) { [self complete:@(requestId) error:@"Bluetooth is not ready" details:nil]; return; } - FCBPeripheral *peer = self.peers[identifier.uppercaseString]; - if (!peer) { - NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:identifier]; - CBPeripheral *peripheral = uuid ? [self.central retrievePeripheralsWithIdentifiers:@[uuid]].firstObject : nil; - if (peripheral) peer = [self remember:peripheral]; - } - if (!peer) { [self complete:@(requestId) error:@"Peripheral is unknown; scan for the device first" details:nil]; return; } - if (peer.connectRequest || peer.peripheral.state != CBPeripheralStateDisconnected) { - [self complete:@(requestId) error:@"Peripheral is already connecting or connected" details:nil]; return; - } - [peer.characteristics removeAllObjects]; peer.connectRequest = @(requestId); - [self.central connectPeripheral:peer.peripheral options:requiresANCS ? @{CBConnectPeripheralOptionRequiresANCS: @YES} : nil]; -} -- (void)centralManager:(CBCentralManager *)central didUpdateANCSAuthorizationForPeripheral:(CBPeripheral *)peripheral { - [self emit:@{@"kind": @"ancs-authorization", @"identifier": peripheral.identifier.UUIDString, - @"authorized": @(peripheral.ancsAuthorized)}]; -} -- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { - [peripheral discoverServices:nil]; -} -- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { - [self failPeer:[self remember:peripheral] reason:error.localizedDescription ?: @"Connection failed"]; -} -- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { - [self failPeer:[self remember:peripheral] reason:error.localizedDescription ?: @"Disconnected"]; -} -- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { - FCBPeripheral *peer = [self remember:peripheral]; - if (error || !peripheral.services.count) { [self failPeer:peer reason:error.localizedDescription ?: @"No services found"]; return; } - peer.pendingServices = peripheral.services.count; - for (CBService *service in peripheral.services) [peripheral discoverCharacteristics:nil forService:service]; -} -- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { - FCBPeripheral *peer = [self remember:peripheral]; - if (!peer.connectRequest) return; - if (error) { [self failPeer:peer reason:error.localizedDescription]; return; } - for (CBCharacteristic *characteristic in service.characteristics) peer.characteristics[characteristic.UUID.UUIDString.lowercaseString] = characteristic; - if (--peer.pendingServices == 0) { - NSNumber *request = peer.connectRequest; peer.connectRequest = nil; - [self complete:request error:nil details:@{@"characteristics": peer.characteristics.allKeys, - @"maxWrite": @([peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithoutResponse])}]; - } -} -- (void)subscribe:(NSString *)identifier characteristic:(NSString *)uuid requestId:(NSInteger)requestId { - FCBPeripheral *peer = self.peers[identifier.uppercaseString]; - CBCharacteristic *characteristic = peer.characteristics[uuid.lowercaseString]; - if (!characteristic || !(characteristic.properties & (CBCharacteristicPropertyNotify | CBCharacteristicPropertyIndicate))) { - [self complete:@(requestId) error:@"Notification characteristic is unavailable" details:nil]; return; - } - if (characteristic.isNotifying) { [self complete:@(requestId) error:nil details:nil]; return; } - if (peer.subscriptions[uuid.lowercaseString]) { [self complete:@(requestId) error:@"Subscription already pending" details:nil]; return; } - peer.subscriptions[uuid.lowercaseString] = @(requestId); - [peer.peripheral setNotifyValue:YES forCharacteristic:characteristic]; -} -- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { - FCBPeripheral *peer = [self remember:peripheral]; NSString *key = characteristic.UUID.UUIDString.lowercaseString; - NSNumber *request = peer.subscriptions[key]; [peer.subscriptions removeObjectForKey:key]; - [self complete:request error:error.localizedDescription ?: (characteristic.isNotifying ? nil : @"Notifications not enabled") details:nil]; -} -- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { - if (error) { [self emit:@{@"kind": @"error", @"message": error.localizedDescription}]; return; } - NSMutableString *hex = [NSMutableString new]; NSData *data = characteristic.value; const uint8_t *bytes = data.bytes; - for (NSUInteger i = 0; i < data.length; i++) [hex appendFormat:@"%02x", bytes[i]]; - [self emit:@{@"kind": @"notification", @"identifier": peripheral.identifier.UUIDString, - @"characteristic": characteristic.UUID.UUIDString.lowercaseString, @"data": hex}]; -} -- (void)write:(NSString *)identifier characteristic:(NSString *)uuid data:(NSData *)data requestId:(NSInteger)requestId { - FCBPeripheral *peer = self.peers[identifier.uppercaseString]; - CBCharacteristic *characteristic = peer.characteristics[uuid.lowercaseString]; - if (!characteristic || peer.peripheral.state != CBPeripheralStateConnected) { - [self complete:@(requestId) error:@"Write characteristic is disconnected or unavailable" details:nil]; return; - } - CBCharacteristicWriteType type = (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) - ? CBCharacteristicWriteWithoutResponse : CBCharacteristicWriteWithResponse; - if (!(characteristic.properties & (CBCharacteristicPropertyWriteWithoutResponse | CBCharacteristicPropertyWrite)) || - data.length > [peer.peripheral maximumWriteValueLengthForType:type]) { - [self complete:@(requestId) error:@"Write exceeds the negotiated BLE payload size or is unsupported" details:nil]; return; - } - if (peer.writes.count >= 256) { [self complete:@(requestId) error:@"Bluetooth write queue is full" details:nil]; return; } - [peer.writes addObject:@{@"id": @(requestId), @"characteristic": characteristic, @"data": [data copy], @"type": @(type)}]; - [self drain:peer]; -} -- (void)drain:(FCBPeripheral *)peer { - while (peer.writes.count && !peer.awaitingWriteResponse && peer.peripheral.state == CBPeripheralStateConnected) { - NSDictionary *write = peer.writes.firstObject; - CBCharacteristicWriteType type = [write[@"type"] integerValue]; - if (type == CBCharacteristicWriteWithoutResponse && !peer.peripheral.canSendWriteWithoutResponse) return; - if (type == CBCharacteristicWriteWithResponse) peer.awaitingWriteResponse = YES; - [peer.peripheral writeValue:write[@"data"] forCharacteristic:write[@"characteristic"] type:type]; - if (peer.awaitingWriteResponse) return; - [peer.writes removeObjectAtIndex:0]; [self complete:write[@"id"] error:nil details:nil]; - } -} -- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)peripheral { [self drain:[self remember:peripheral]]; } -- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { - FCBPeripheral *peer = [self remember:peripheral]; - if (!peer.awaitingWriteResponse || !peer.writes.count) return; - NSDictionary *write = peer.writes.firstObject; [peer.writes removeObjectAtIndex:0]; peer.awaitingWriteResponse = NO; - [self complete:write[@"id"] error:error.localizedDescription details:nil]; [self drain:peer]; -} -- (void)failPeer:(FCBPeripheral *)peer reason:(NSString *)reason { - if (!peer) return; - NSNumber *connect = peer.connectRequest; peer.connectRequest = nil; - [self complete:connect error:reason details:nil]; - NSArray *subscriptions = peer.subscriptions.allValues; [peer.subscriptions removeAllObjects]; - NSArray *writes = [peer.writes copy]; [peer.writes removeAllObjects]; peer.awaitingWriteResponse = NO; - for (NSNumber *request in subscriptions) [self complete:request error:reason details:nil]; - for (NSDictionary *write in writes) [self complete:write[@"id"] error:reason details:nil]; - [peer.characteristics removeAllObjects]; - if (peer.peripheral.state != CBPeripheralStateDisconnected) [self.central cancelPeripheralConnection:peer.peripheral]; - [self emit:@{@"kind": @"disconnected", @"identifier": peer.peripheral.identifier.UUIDString, @"message": reason}]; -} -- (void)disconnect:(NSString *)identifier { [self failPeer:self.peers[identifier.uppercaseString] reason:@"Disconnected"]; } -@end diff --git a/App_Resources/iOS/src/FaceclawRemoteInput.h b/App_Resources/iOS/src/FaceclawRemoteInput.h deleted file mode 100644 index dbbce5e0..00000000 --- a/App_Resources/iOS/src/FaceclawRemoteInput.h +++ /dev/null @@ -1,12 +0,0 @@ -#import -@interface FaceclawRemoteInput : NSObject -+ (instancetype)shared; -+ (NSString *)interfaces; -- (NSString *)start:(int)port; -- (NSString *)start:(int)port address:(NSString *)address; -- (void)stop; -- (NSString *)nextRequest; -- (void)complete:(long long)identifier response:(NSString *)response; -- (NSString *)randomSecret; -- (NSString *)tokenDigest:(NSString *)value; -@end diff --git a/App_Resources/iOS/src/FaceclawRemoteInput.m b/App_Resources/iOS/src/FaceclawRemoteInput.m deleted file mode 100644 index 0c1c46af..00000000 --- a/App_Resources/iOS/src/FaceclawRemoteInput.m +++ /dev/null @@ -1,150 +0,0 @@ -#import "FaceclawRemoteInput.h" -#import "FaceclawCrypto.h" -#import -#import -#import -#import -#import -#import -#import -#import -#import - -@implementation FaceclawRemoteInput { - NSCondition *_condition; - int _server, _client; - NSUInteger _generation; - long long _nextId, _requestId; - NSTimeInterval _deadline; - NSString *_pending, *_reply; -} -+ (instancetype)shared { - static FaceclawRemoteInput *instance; static dispatch_once_t once; - dispatch_once(&once, ^{ instance = [self new]; }); return instance; -} -- (instancetype)init { - if ((self = [super init])) { _condition = [NSCondition new]; _server = _client = -1; } - return self; -} -- (NSString *)start:(int)port { return [self start:port address:@"127.0.0.1"]; } -- (NSString *)start:(int)port address:(NSString *)address { - [_condition lock]; - if (_server >= 0) { [_condition unlock]; return @""; } - struct addrinfo hints = {0}, *resolved = NULL; - hints.ai_flags = AI_NUMERICHOST; hints.ai_socktype = SOCK_STREAM; - NSString *service = [NSString stringWithFormat:@"%d", port]; - if (getaddrinfo(address.UTF8String, service.UTF8String, &hints, &resolved) != 0) { - [_condition unlock]; return @"Invalid local address."; - } - BOOL wildcard = resolved->ai_family == AF_INET ? - ((struct sockaddr_in *)resolved->ai_addr)->sin_addr.s_addr == INADDR_ANY : - IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)resolved->ai_addr)->sin6_addr); - if (wildcard) { freeaddrinfo(resolved); [_condition unlock]; return @"Wildcard addresses are not allowed."; } - int fd = socket(resolved->ai_family, SOCK_STREAM, 0), yes = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); - int result = fd < 0 ? -1 : bind(fd, resolved->ai_addr, (socklen_t)resolved->ai_addrlen); - freeaddrinfo(resolved); - if (result || listen(fd, 8)) { - if (fd >= 0) close(fd); [_condition unlock]; return @"Could not open input port on this address."; - } - _server = fd; NSUInteger generation = ++_generation; - [_condition unlock]; - dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{ [self serve:fd generation:generation]; }); - return @""; -} -- (void)stop { - [_condition lock]; ++_generation; - // The serving thread owns close(); shutdown interrupts its blocking I/O. - if (_server >= 0) shutdown(_server, SHUT_RDWR); - if (_client >= 0) shutdown(_client, SHUT_RDWR); - _server = _client = -1; _requestId = 0; _pending = _reply = nil; - [_condition broadcast]; [_condition unlock]; -} -- (NSString *)nextRequest { - [_condition lock]; - NSString *result = NSDate.date.timeIntervalSince1970 < _deadline ? _pending : nil; - _pending = nil; [_condition unlock]; return result; -} -- (void)complete:(long long)identifier response:(NSString *)response { - [_condition lock]; - if (_requestId == identifier && NSDate.date.timeIntervalSince1970 < _deadline) { _reply = response; [_condition signal]; } - [_condition unlock]; -} -- (void)serve:(int)listener generation:(NSUInteger)generation { - for (;;) { - [_condition lock]; BOOL current = generation == _generation; [_condition unlock]; - if (!current) break; - // Poll also guarantees stop is observed on platforms where shutdown does not wake accept. - struct pollfd p = { .fd = listener, .events = POLLIN }; - if (poll(&p, 1, 250) <= 0) continue; - int client = accept(listener, NULL, NULL); - if (client < 0) break; - @autoreleasepool { - [_condition lock]; current = generation == _generation; - if (current) _client = client; - [_condition unlock]; - if (!current) { close(client); break; } - struct timeval timeout = {5, 0}; int yes = 1; - setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes)); - NSTimeInterval readDeadline = NSDate.date.timeIntervalSince1970 + 5; - NSMutableData *body = [NSMutableData new]; unsigned char byte; BOOL complete = NO; - while (body.length <= 65536) { - NSTimeInterval remaining = readDeadline - NSDate.date.timeIntervalSince1970; - struct pollfd input = { .fd = client, .events = POLLIN }; - if (remaining <= 0 || poll(&input, 1, (int)(remaining * 1000)) <= 0 || recv(client, &byte, 1, 0) != 1) break; - if (byte == '\n') { complete = YES; break; } - [body appendBytes:&byte length:1]; - } - NSString *text = complete ? [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] : nil; - if (text) { - [_condition lock]; - if (generation == _generation) { - _requestId = ++_nextId; _deadline = NSDate.date.timeIntervalSince1970 + 5; _reply = nil; - NSData *json = [NSJSONSerialization dataWithJSONObject:@{@"id":@(_requestId), @"expiresAt":@(_deadline * 1000), @"body":text} options:0 error:nil]; - _pending = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; - while (!_reply && generation == _generation && NSDate.date.timeIntervalSince1970 < _deadline) - [_condition waitUntilDate:[NSDate dateWithTimeIntervalSince1970:_deadline]]; - NSString *reply = _reply ?: @"{\"ok\":false,\"error\":\"timeout\",\"message\":\"Faceclaw did not respond in time.\"}"; - if (generation == _generation) { _pending = _reply = nil; _requestId = 0; } - [_condition unlock]; - NSData *data = [[reply stringByAppendingString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]; - NSUInteger offset = 0; - while (offset < data.length) { - ssize_t n = send(client, (const char *)data.bytes + offset, data.length - offset, 0); - if (n <= 0) break; offset += n; - } - } else [_condition unlock]; - } - [_condition lock]; if (generation == _generation) _client = -1; [_condition unlock]; - close(client); - } - } - close(listener); - [_condition lock]; if (generation == _generation) _server = -1; [_condition unlock]; -} -+ (NSString *)interfaces { - NSMutableArray *result = [NSMutableArray new]; struct ifaddrs *interfaces = NULL; - if (getifaddrs(&interfaces) == 0) { - for (struct ifaddrs *item = interfaces; item; item = item->ifa_next) { - if (!item->ifa_addr || !(item->ifa_flags & IFF_UP) || (item->ifa_flags & IFF_LOOPBACK)) continue; - int family = item->ifa_addr->sa_family; - if (family != AF_INET && family != AF_INET6) continue; - char address[NI_MAXHOST]; - if (getnameinfo(item->ifa_addr, item->ifa_addr->sa_len, address, sizeof(address), NULL, 0, NI_NUMERICHOST)) continue; - [result addObject:@{@"name":@(item->ifa_name), @"address":@(address), @"pointToPoint":@((item->ifa_flags & IFF_POINTOPOINT) != 0)}]; - } - freeifaddrs(interfaces); - } - return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:result options:0 error:nil] encoding:NSUTF8StringEncoding]; -} -- (NSString *)randomSecret { - unsigned char bytes[32]; - if (SecRandomCopyBytes(kSecRandomDefault, sizeof(bytes), bytes) != errSecSuccess) return @""; - NSMutableString *hex = [NSMutableString new]; - for (NSUInteger i = 0; i < sizeof(bytes); i++) [hex appendFormat:@"%02x", bytes[i]]; - return hex; -} -- (NSString *)tokenDigest:(NSString *)value { return [FaceclawCrypto sha256:[value dataUsingEncoding:NSUTF8StringEncoding]]; } -@end diff --git a/App_Resources/iOS/src/FaceclawSseRequest.h b/App_Resources/iOS/src/FaceclawSseRequest.h deleted file mode 100644 index 3177e20b..00000000 --- a/App_Resources/iOS/src/FaceclawSseRequest.h +++ /dev/null @@ -1,8 +0,0 @@ -#import - -/** Streaming POST. Events are polled by the owning JS isolate, including workers. */ -@interface FaceclawSseRequest : NSObject -- (instancetype)initWithURL:(NSString *)url body:(NSString *)body headers:(NSString *)headersJson; -- (NSString *)takeEvents; -- (void)cancel; -@end diff --git a/App_Resources/iOS/src/FaceclawSseRequest.m b/App_Resources/iOS/src/FaceclawSseRequest.m deleted file mode 100644 index 7e793257..00000000 --- a/App_Resources/iOS/src/FaceclawSseRequest.m +++ /dev/null @@ -1,153 +0,0 @@ -#import "FaceclawSseRequest.h" -#import -#if TARGET_OS_IOS -#import -#endif - -@interface FaceclawSseRequest () -@property(nonatomic, strong) NSURLSession *session; -@property(nonatomic, strong) NSURLSessionDataTask *task; -@property(nonatomic, strong) NSMutableData *buffer; -@property(nonatomic, strong) NSMutableArray *events; -@property(nonatomic) NSUInteger queuedBytes; -@property(nonatomic) NSInteger status; -@property(nonatomic) BOOL finished; -@property(nonatomic) BOOL cancelled; -@property(nonatomic) BOOL firstLine; -#if TARGET_OS_IOS -@property(nonatomic) UIBackgroundTaskIdentifier backgroundTask; -#endif -@end - -@implementation FaceclawSseRequest -- (instancetype)initWithURL:(NSString *)url body:(NSString *)body headers:(NSString *)headersJson { - if ((self = [super init])) { - _buffer = [NSMutableData new]; _events = [NSMutableArray new]; _firstLine = YES; -#if TARGET_OS_IOS - _backgroundTask = UIBackgroundTaskInvalid; - dispatch_async(dispatch_get_main_queue(), ^{ - @synchronized(self) { - if (self.cancelled || self.finished) return; - self.backgroundTask = [UIApplication.sharedApplication beginBackgroundTaskWithName:@"Assistant response" expirationHandler:^{ - [self finish:@{@"kind": @"failure", @"text": @"iOS background time expired. Open Faceclaw and retry."}]; - [self endBackgroundTask]; - }]; - } - }); -#endif - NSURL *target = [NSURL URLWithString:url]; - if (!target || ![@[@"https", @"http"] containsObject:target.scheme.lowercaseString]) { - [self finish:@{@"kind": @"failure", @"text": @"Invalid streaming URL"}]; return self; - } - NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:target]; - request.HTTPMethod = @"POST"; - request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding]; - [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; - [request setValue:@"text/event-stream" forHTTPHeaderField:@"Accept"]; - NSDictionary *headers = [NSJSONSerialization JSONObjectWithData:[headersJson dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; - if ([headers isKindOfClass:NSDictionary.class]) for (NSString *name in headers) { - if ([headers[name] isKindOfClass:NSString.class]) [request setValue:headers[name] forHTTPHeaderField:name]; - } - NSURLSessionConfiguration *config = NSURLSessionConfiguration.ephemeralSessionConfiguration; - config.timeoutIntervalForRequest = 180; - config.timeoutIntervalForResource = 180; - _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; - _task = [_session dataTaskWithRequest:request]; - [_task resume]; - } - return self; -} -- (void)endBackgroundTask { -#if TARGET_OS_IOS - dispatch_async(dispatch_get_main_queue(), ^{ - @synchronized(self) { - if (self.backgroundTask == UIBackgroundTaskInvalid) return; - [UIApplication.sharedApplication endBackgroundTask:self.backgroundTask]; - self.backgroundTask = UIBackgroundTaskInvalid; - } - }); -#endif -} -- (void)finish:(NSDictionary *)event { - @synchronized(self) { - if (_finished || _cancelled) return; - _finished = YES; [_events addObject:event]; - [_session invalidateAndCancel]; _session = nil; _task = nil; - [_buffer setLength:0]; - } -} -- (NSString *)takeEvents { - @synchronized(self) { - NSData *data = [NSJSONSerialization dataWithJSONObject:_events options:0 error:nil]; - [_events removeAllObjects]; _queuedBytes = 0; - if (_finished) [self endBackgroundTask]; - return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]"; - } -} -- (void)cancel { - @synchronized(self) { - _cancelled = YES; - [_events removeAllObjects]; [_buffer setLength:0]; _queuedBytes = 0; - [_session invalidateAndCancel]; _session = nil; _task = nil; - [self endBackgroundTask]; - } -} -- (void)line:(NSData *)data { - NSString *line = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - if (!line) { [self finish:@{@"kind": @"failure", @"text": @"Invalid UTF-8 in streaming response"}]; return; } - if (_firstLine && [line hasPrefix:@"\uFEFF"]) line = [line substringFromIndex:1]; - _firstLine = NO; - _queuedBytes += data.length; - if (_queuedBytes > 8 * 1024 * 1024 || _events.count >= 16384) { - [_events removeAllObjects]; - [self finish:@{@"kind": @"failure", @"text": @"Streaming response queue overflow"}]; return; - } - [_events addObject:@{@"kind": @"line", @"text": line}]; -} -- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { - @synchronized(self) { _status = [(NSHTTPURLResponse *)response statusCode]; } - completionHandler(NSURLSessionResponseAllow); -} -- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler { - // Never forward provider credentials to a redirected endpoint. - completionHandler(nil); -} -- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data { - @synchronized(self) { - if (_finished || _cancelled) return; - if (_status < 200 || _status >= 300) { - NSUInteger remaining = 65536 - _buffer.length; - [_buffer appendData:[data subdataWithRange:NSMakeRange(0, MIN(remaining, data.length))]]; - return; - } - [_buffer appendData:data]; - NSUInteger start = 0; - const uint8_t *bytes = _buffer.bytes; - for (NSUInteger i = 0; i < _buffer.length; i++) { - if (bytes[i] != '\r' && bytes[i] != '\n') continue; - if (bytes[i] == '\r' && i + 1 == _buffer.length) break; - [self line:[_buffer subdataWithRange:NSMakeRange(start, i - start)]]; - if (_finished) return; - if (bytes[i] == '\r' && bytes[i + 1] == '\n') i++; - start = i + 1; - } - if (start) [_buffer replaceBytesInRange:NSMakeRange(0, start) withBytes:NULL length:0]; - if (_buffer.length > 2 * 1024 * 1024) [self finish:@{@"kind": @"failure", @"text": @"Streaming response line too large"}]; - } -} -- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { - @synchronized(self) { - if (_finished || _cancelled) return; - if (error) { [self finish:@{@"kind": @"failure", @"text": error.localizedDescription}]; return; } - if (_status < 200 || _status >= 300) { - NSString *body = [[NSString alloc] initWithData:_buffer encoding:NSUTF8StringEncoding] ?: @""; - [self finish:@{@"kind": @"http-error", @"code": @(_status), @"text": body}]; return; - } - if (_buffer.length) { - if (((const uint8_t *)_buffer.bytes)[_buffer.length - 1] == '\r') [_buffer setLength:_buffer.length - 1]; - [self line:_buffer]; - } - [self finish:@{@"kind": @"complete"}]; - } -} -@end diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap index 73b6e464..14291369 100644 --- a/App_Resources/iOS/src/module.modulemap +++ b/App_Resources/iOS/src/module.modulemap @@ -1,17 +1,14 @@ module FaceclawGraphics { header "FaceclawGraphics.h" header "FaceclawFontRenderer.h" - header "FaceclawBluetooth.h" header "FaceclawSocket.h" header "FaceclawLc3Decoder.h" header "FaceclawSpeech.h" - header "FaceclawSseRequest.h" header "FaceclawLocation.h" header "FaceclawCalendar.h" header "FaceclawConfigPort.h" header "FaceclawEvenHubWebView.h" header "FaceclawCrypto.h" - header "FaceclawRemoteInput.h" header "FaceclawQrScanner.h" export * } diff --git a/CHANGELOG b/CHANGELOG index 202fd417..e78d956e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,10 +2,22 @@ ## 0.7.3 - Future -### Other Features +### Depth and Animation + +* A new display-list system for glasses-side drawing, unlocking z-effects and animation +* Menus animate when changing selection and scrolling +* Menus bounce at the ends, and show part of the next row when there is more below +* Fade-in/out effects when turning the screen on/off +* Custom auto-brightness control algorithm, with configurable min, max and curve +* Depth effects for the window switcher, top bar, and modals +* Glanceboard has configurable distance + +### Other Features and Bugfixes * Add tokens and faceclaw-input script, which allows using a CLI utility as an input device for the glasses * Navigate app: Add map-without-destination mode +* Nightscout app: Glanceboard widget still shows out-of-bounds values when out of space +* EvenHub: Fix login form being stuck open without a cancel button ### iOS port: diff --git a/CLAUDE.md b/CLAUDE.md index 3ef2a977..7dd039ef 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,13 +1,16 @@ This is Faceclaw, an Android program that provides user interface on the Even Realities G2 smart glasses. It is written in a mix of Typescript/NativeScript -(for the user interface parts) and Java (for the low-level bluetooth parts and +(for the user interface parts) and Kotlin (for the low-level bluetooth parts and for interfacing with Android SDK). -Typescript parts are in app/. Java parts are in App_Resources/Android/src/main/java/com/faceclaw/app/. +Typescript parts are in app/. Android-specific Kotlin parts are in +App_Resources/Android/src/main/java/com/faceclaw/app/ (compiled by the NativeScript +Android build; only the vendored com.k2fsa.sherpa.onnx JNI classes there remain Java). +Shared Kotlin Multiplatform code is in native/kotlin/shared/ (see native/kotlin/README.md). wear/ is a separate Gradle project (Kotlin + Compose for Wear OS): the watch remote that drives Faceclaw over the Wearable Data Layer. Its phone-side -counterparts are FaceclawWearBridge.java, app/native/wear-bridge.ts and +counterparts are FaceclawWearBridge.kt, app/native/wear-bridge.ts and app/g2/wear-remote.ts; the message format is in wear/PROTOCOL.md. `build.sh` and `build_wear.sh` build the Android and Wear OS apps respectively. diff --git a/app/apps/app-definition.ts b/app/apps/app-definition.ts index 0f73ad9d..b2e69a01 100644 --- a/app/apps/app-definition.ts +++ b/app/apps/app-definition.ts @@ -54,6 +54,8 @@ export type GlanceboardProvider = { showOnLongPress: () => boolean; /** Whether the head-tilt wake goes to the board rather than the regular UI. */ showOnHeadTilt: () => boolean; + /** Stereo depth (firmware units; positive is nearer) while the board covers the screen. */ + depth: () => number; /** A fresh board; requestRender asks the host to repaint it. */ createBoard: (requestRender: () => void) => GlanceBoardInstance; }; diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index 67566f92..b17adaf0 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -11,6 +11,7 @@ * focus, tap-then-hold opens the window menu. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -187,6 +188,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: BlocksWindow = { windowId: message.windowId, diff --git a/app/apps/developer/bandwidth-benchmark.ts b/app/apps/developer/bandwidth-benchmark.ts index 8af80ceb..192b967e 100644 --- a/app/apps/developer/bandwidth-benchmark.ts +++ b/app/apps/developer/bandwidth-benchmark.ts @@ -2,8 +2,8 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { Layer, type LayerContext } from "../../ui/layers"; import { type InputEvent } from "../../ui/gestures"; -import { drawSelectionHighlight } from "../../ui/menu"; -import { tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, tightRowHeight } from "../../ui/metrics"; import { cancelBandwidthBenchmark, getBandwidthBenchmarkStatus, @@ -29,6 +29,7 @@ const LIST_X = 20; const VALUE_X = 150; type Phase = "config" | "arming" | "running" | "done"; +type ConfigRow = "size" | "window" | "link" | "start"; /** * BLE bandwidth benchmark: pick a message size and pipeline window, stream @@ -38,7 +39,14 @@ type Phase = "config" | "arming" | "running" | "done"; */ export class BandwidthBenchmarkLayer implements Layer { private phase: Phase = "config"; - private selectedRow = 0; // size, window, link preference, start + private readonly menu = new Menu({ + items: ["size", "window", "link", "start"], + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()) + 4, + draw: (args) => this.drawRow(args), + }); private sizeIndex = 2; private windowIndex = 2; private linkModeIndex = 0; @@ -134,26 +142,14 @@ export class BandwidthBenchmarkLayer implements Layer { } const rowH = tightRowHeight(font) + 4; - const rows: Array<[string, string]> = [ - ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`], - ["Window size", `${WINDOW_SIZES[this.windowIndex]}`], - ["Link request", LINK_MODES[this.linkModeIndex]!], - [this.phase === "done" ? "Run again" : "Start", ""], - ]; - for (let index = 0; index < rows.length; index++) { - const [label, value] = rows[index]!; - const y = HEADER_HEIGHT + index * rowH; - const selected = index === this.selectedRow; - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4); - } - image.drawText(font, LIST_X, y + 2, label, selected ? 255 : 200); - if (value) { - image.drawText(font, VALUE_X, y + 2, value, selected ? 235 : 160); - } - } + const rowCount = this.menu.items.length; + this.menu.paint( + image, + { x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: rowCount * rowH }, + ctx.stack.isFocused(), + ); - let y = HEADER_HEIGHT + rows.length * rowH + 10; + let y = HEADER_HEIGHT + rowCount * rowH + 10; if (this.error) { image.drawText(font, LIST_X, y, this.error, 180); } else if (this.result) { @@ -176,6 +172,21 @@ export class BandwidthBenchmarkLayer implements Layer { return image; } + private drawRow({ image, item, x, y, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const [label, value]: [string, string] = + item === "size" ? ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`] + : item === "window" ? ["Window size", `${WINDOW_SIZES[this.windowIndex]}`] + : item === "link" ? ["Link request", LINK_MODES[this.linkModeIndex]!] + : [this.phase === "done" ? "Run again" : "Start", ""]; + const textY = centeredTextY(font, y, height); + // Text sits 6px in from the row box (LIST_X), values at VALUE_X. + image.drawText(font, x + 6, textY, label, selected ? 255 : 200); + if (value) { + image.drawText(font, x + 6 + VALUE_X - LIST_X, textY, value, selected ? 235 : 160); + } + } + async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (this.phase === "arming" || this.phase === "running") { // A static screen during the run: only allow bailing out. @@ -186,22 +197,22 @@ export class BandwidthBenchmarkLayer implements Layer { } switch (event.type) { case "scroll-up": - this.selectedRow = Math.max(0, this.selectedRow - 1); - return; case "scroll-down": - this.selectedRow = Math.min(3, this.selectedRow + 1); + await this.menu.handleInput(event); return; - case "click": - if (this.selectedRow === 0) { + case "click": { + const row = this.menu.selectedItem; + if (row === "size") { this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length; - } else if (this.selectedRow === 1) { + } else if (row === "window") { this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length; - } else if (this.selectedRow === 2) { + } else if (row === "link") { this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length; - } else { + } else if (row === "start") { this.startRun(); } return; + } case "double-click": ctx.stack.pop(); return; diff --git a/app/apps/developer/buzzer-demo.ts b/app/apps/developer/buzzer-demo.ts index c88e18c2..1772d555 100644 --- a/app/apps/developer/buzzer-demo.ts +++ b/app/apps/developer/buzzer-demo.ts @@ -1,10 +1,9 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; -import { clamp } from "../../util/numeric-util"; import { Layer, type LayerActions, type LayerContext } from "../../ui/layers"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures"; -import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu"; -import { tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, tightRowHeight } from "../../ui/metrics"; import { buildSoundSequencePayload, CFW_SEQ_MAX, @@ -28,8 +27,14 @@ function sleep(ms: number): Promise { * sequencer message lands as the previous phrase finishes. */ export class BuzzerDemoLayer implements Layer { - private selectedIndex = 0; - private scrollRow = 0; + private readonly menu = new Menu({ + items: SOUND_EFFECTS, + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => drawEffectRow(args), + }); private playing: string | null = null; paint(ctx: LayerContext): GrayImage { @@ -42,21 +47,11 @@ export class BuzzerDemoLayer implements Layer { image.drawText(font, width - 24 - font.measureText(status), 8, status, 140); const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT; - const rowH = tightRowHeight(font); - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length); - - const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows); - for (let index = this.scrollRow; index < lastVisible; index++) { - const effect = SOUND_EFFECTS[index]!; - const y = HEADER_HEIGHT + (index - this.scrollRow) * rowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4); - } - image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200); - image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120); - } + this.menu.paint( + image, + { x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: listHeight }, + ctx.stack.isFocused(), + ); return image; } @@ -64,13 +59,11 @@ export class BuzzerDemoLayer implements Layer { async handleInput(event: InputEvent, ctx: LayerContext): Promise { switch (event.type) { case "scroll-up": - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - return; case "scroll-down": - this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1); + await this.menu.handleInput(event); return; case "click": { - const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)]; + const effect = this.menu.selectedItem; if (effect && !this.playing) { // Fire-and-forget: playback paces itself with sleeps between // phrases; awaiting it here would stall input for seconds. @@ -106,3 +99,11 @@ export class BuzzerDemoLayer implements Layer { } } } + +/** Effect name, then its description in a second column 110px to the right. */ +function drawEffectRow({ image, item: effect, x, y, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const textY = centeredTextY(font, y, height); + image.drawText(font, x + 6, textY, effect.name, selected ? 255 : 200); + image.drawText(font, x + 6 + 110, textY, effect.desc, selected ? 180 : 120); +} diff --git a/app/apps/evenhub/api-key-dialog.ts b/app/apps/evenhub/api-key-dialog.ts index bd41fec8..825357bf 100644 --- a/app/apps/evenhub/api-key-dialog.ts +++ b/app/apps/evenhub/api-key-dialog.ts @@ -3,8 +3,8 @@ import { GrayImage } from "../../graphics/image"; import { truncateText } from "../../graphics/textwrap"; import { type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; -import { drawSelectionHighlight } from "../../ui/menu"; -import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { lineStep, listRowHeight } from "../../ui/metrics"; const DIALOG_X = 8; const DIALOG_Y = 8; @@ -21,6 +21,8 @@ const SERVICE_LABELS: Record = { export type ApiKeyRequestItem = { id: string; configured: boolean }; +type ConsentAction = "Allow" | "Deny"; + /** * Consent dialog shown when an EvenHub app calls requestApiKeyAccess: lists the * requested API-key services (marking any the user hasn't configured) and asks @@ -28,8 +30,14 @@ export type ApiKeyRequestItem = { id: string; configured: boolean }; * double-click) shares nothing. */ export class EvenHubApiKeyDialogLayer implements Layer { - /** 0 = Allow, 1 = Deny. */ - private selectedIndex = 0; + private readonly menu = new Menu({ + items: ["Allow", "Deny"], + wrap: true, + rowGap: 1, + highlight: { radius: 8 }, + getHeight: () => listRowHeight(getDefaultSmallFont()), + draw: (args) => drawActionRow(args), + }); private resolved = false; constructor( @@ -67,16 +75,11 @@ export class EvenHubApiKeyDialogLayer implements Layer { } y += 4; - const focused = ctx.stack.isFocused(); - const actions = ["Allow", "Deny"]; - for (let index = 0; index < actions.length; index++) { - const rowY = y + index * actionRowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8); - } - image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200); - } + this.menu.paint( + image, + { x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) }, + ctx.stack.isFocused(), + ); return image; } @@ -84,10 +87,10 @@ export class EvenHubApiKeyDialogLayer implements Layer { switch (event.type) { case "scroll-up": case "scroll-down": - this.selectedIndex = (this.selectedIndex + 1) % 2; + void this.menu.handleInput(event); return; case "click": - this.resolve(ctx, this.selectedIndex === 0); + this.resolve(ctx, this.menu.selectedItem === "Allow"); return; case "double-click": this.resolve(ctx, false); @@ -113,3 +116,7 @@ export class EvenHubApiKeyDialogLayer implements Layer { else this.onCancel(); } } + +function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs): void { + image.drawText(getDefaultSmallFont(), x + 10, y + 3, item, selected ? 255 : 200); +} diff --git a/app/apps/evenhub/even-api.ts b/app/apps/evenhub/even-api.ts index 932e008c..051568f9 100644 --- a/app/apps/evenhub/even-api.ts +++ b/app/apps/evenhub/even-api.ts @@ -181,12 +181,18 @@ export class EvenHubApiClient { path: string, options: { query?: Record; body?: Record } = {}, ): Promise { - let token = await this.ensureLogin(); + const token = await this.ensureLogin(); const response = await this.request(method, path, { ...options, token, commonVersion: 3 }); - if (response.httpStatus === 401) { - invalidateEvenHubToken(); + try { + return unwrap(response.envelope, response.httpStatus, path); + } catch (error) { + // A rejected session fails every later request too, so drop the token + // (unless a fresh sign-in replaced it while this request was in flight). + if (error instanceof EvenHubAuthenticationError && getEvenHubToken() === token) { + invalidateEvenHubToken(); + } + throw error; } - return unwrap(response.envelope, response.httpStatus, path); } private ensureLogin(): Promise { @@ -350,15 +356,17 @@ async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs = REQU } function unwrap(envelope: ApiEnvelope, httpStatus: number, path: string): unknown { - if (httpStatus === 401) { - throw new EvenHubAuthenticationError("Even rejected the login session."); + const message = typeof envelope.msg === "string" ? envelope.msg.trim() : ""; + // An expired session can come back as HTTP 401, or as HTTP 200 with code + // 401 in the envelope ("Your login is expired"). + if (httpStatus === 401 || Number(envelope.code) === 401) { + throw new EvenHubAuthenticationError(message || "Even rejected the login session."); } if (httpStatus < 200 || httpStatus >= 300) { throw new Error(`EvenHub request failed (HTTP ${httpStatus}, ${path}).`); } if (envelope.code !== 0) { - const detail = typeof envelope.msg === "string" && envelope.msg ? `: ${envelope.msg}` : ""; - throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${detail}`); + throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${message ? `: ${message}` : ""}`); } return envelope.data; } diff --git a/app/apps/evenhub/store-detail-layer.ts b/app/apps/evenhub/store-detail-layer.ts index f3b93803..ba179d86 100644 --- a/app/apps/evenhub/store-detail-layer.ts +++ b/app/apps/evenhub/store-detail-layer.ts @@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image"; import { truncateText, wrapText } from "../../graphics/textwrap"; import { type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; -import { drawSelectionHighlight } from "../../ui/menu"; +import { Menu } from "../../ui/menu-core"; import { TextViewerLayer } from "../files/text-viewer"; import { evenHubApi, type EvenHubStoreApp } from "./even-api"; import { @@ -18,17 +18,36 @@ import { lineStep, listRowHeight } from "../../ui/metrics"; const X = 18; +type DetailAction = "about" | "whats-new" | "primary"; +const ACTIONS: readonly DetailAction[] = ["about", "whats-new", "primary"]; + export type EvenHubStoreDetailOptions = { launchApp: (appId: string) => Promise | void; appendLog: (message: string) => void; /** A package was installed, updated, or reinstalled from this page. */ onInstalled?: (app: InstalledEvenHubApp) => void; + /** Offered every failure first; returns true if it was a rejected session (and was dealt with). */ + onAuthError?: (ctx: LayerContext, error: unknown) => boolean; }; /** Storefront metadata and the Install/Launch action for one public app. */ export class EvenHubStoreDetailLayer implements Layer { - /** About, What's New, Install/Launch. */ - private selectedIndex = 2; + /** About, What's New, Install/Launch; starts on the primary action. */ + private readonly actions = new Menu({ + items: ACTIONS, + selectedIndex: 2, + rowGap: 1, + highlight: { radius: 8 }, + getHeight: () => listRowHeight(getDefaultSmallFont()) + 4, + draw: ({ image, item, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + const label = item === "about" ? "About" : item === "whats-new" ? "What's New" : this.primaryLabel; + const value = this.working && item === "primary" ? 145 : selected ? 245 : 190; + image.drawText(font, x + 11, y + 5, truncateText(font, label, width - 18), value); + }, + }); + /** The primary action's label for the paint in progress. */ + private primaryLabel = ""; private working = false; private detailPromise: Promise | null = null; private status = ""; @@ -47,7 +66,7 @@ export class EvenHubStoreDetailLayer implements Layer { const actionRowH = listRowHeight(font) + 4; const installed = getInstalledEvenHubApp(this.app.packageId); - const actions = ["About", "What's New", this.working ? "Installing..." : this.primaryActionLabel(installed)]; + this.primaryLabel = this.working ? "Installing..." : this.primaryActionLabel(installed); // Detail lines as (text, shade, extra gap below); built per candidate // width so the layout choice below can measure before drawing. @@ -90,7 +109,7 @@ export class EvenHubStoreDetailLayer implements Layer { // Stacked layout (details full-width, actions at the bottom) when it // fits; otherwise the actions become a narrow top-right menu and the // details flow down a left column beside it. - const stackedActionTop = height - 6 - actions.length * actionRowH; + const stackedActionTop = height - 6 - ACTIONS.length * actionRowH; const stacked = 8 + linesHeight(buildLines(width - X * 2)) + 6 <= stackedActionTop; const menuW = 150; const menuX = width - menuW - 12; @@ -105,15 +124,11 @@ export class EvenHubStoreDetailLayer implements Layer { const actionX = stacked ? X : menuX; const actionW = stacked ? textWidth : menuW; const actionTop = stacked ? stackedActionTop : 8; - for (let index = 0; index < actions.length; index++) { - const actionY = actionTop + index * actionRowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, actionX - 5, actionY, actionW + 10, actionRowH - 1, ctx.stack.isFocused(), 8); - } - const value = this.working && index === 2 ? 145 : selected ? 245 : 190; - image.drawText(font, actionX + 6, actionY + 5, truncateText(font, actions[index]!, actionW - 8), value); - } + this.actions.paint( + image, + { x: actionX - 5, y: actionTop, width: actionW + 10, height: ACTIONS.length * actionRowH - 1 }, + ctx.stack.isFocused(), + ); return image; } @@ -123,12 +138,8 @@ export class EvenHubStoreDetailLayer implements Layer { return; } if (this.working) return; - if (event.type === "scroll-up") { - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - return; - } - if (event.type === "scroll-down") { - this.selectedIndex = Math.min(2, this.selectedIndex + 1); + if (event.type === "scroll-up" || event.type === "scroll-down") { + await this.actions.handleInput(event); return; } if (event.type !== "click") return; @@ -143,11 +154,12 @@ export class EvenHubStoreDetailLayer implements Layer { try { await this.ensureDetail(ctx); - if (this.selectedIndex === 0) { + const action = this.actions.selectedItem; + if (action === "about") { ctx.stack.push(new TextViewerLayer(this.app.description || "No description supplied.", "About")); return; } - if (this.selectedIndex === 1) { + if (action === "whats-new") { ctx.stack.push(new TextViewerLayer(this.app.changelog || "No release notes supplied.", "What's New")); return; } @@ -160,6 +172,7 @@ export class EvenHubStoreDetailLayer implements Layer { await this.install(ctx); } catch (error) { + if (this.options.onAuthError?.(ctx, error)) return; this.status = cleanError(error); this.options.appendLog(`evenhub store: ${this.status}`); } finally { this.working = false; ctx.actions.requestRender(); } @@ -193,6 +206,7 @@ export class EvenHubStoreDetailLayer implements Layer { ctx.actions.requestRender(); await this.options.launchApp(installedEvenHubAppId(result.packageId)); } catch (error) { + if (this.options.onAuthError?.(ctx, error)) return; this.status = cleanError(error); this.options.appendLog(`evenhub store: ${this.status}`); } finally { @@ -216,6 +230,7 @@ export class EvenHubStoreDetailLayer implements Layer { return undefined; }) .catch((error) => { + if (this.options.onAuthError?.(ctx, error)) return; this.options.appendLog(`evenhub store: app details unavailable: ${cleanError(error)}`); }) .finally(() => ctx.actions.requestRender()); diff --git a/app/apps/evenhub/store-install.ts b/app/apps/evenhub/store-install.ts index 5c63e1a9..faa60f8c 100644 --- a/app/apps/evenhub/store-install.ts +++ b/app/apps/evenhub/store-install.ts @@ -53,8 +53,6 @@ export async function installStoreApp( const needsPrompt = (manifest.permissions.length > 0 || privacyPolicyUrl) && !alreadyGranted(app.packageId, manifest); if (needsPrompt) { - options.onStatus(""); - ctx.actions.requestRender(); const accepted = await new Promise((resolve) => { ctx.stack.push( new EvenHubPermissionDialogLayer( @@ -65,6 +63,10 @@ export async function installStoreApp( () => resolve(false), ), ); + // Only once the dialog is stacked: an in-process window's requestRender + // paints synchronously, and nothing else repaints after a download. + options.onStatus(""); + ctx.actions.requestRender(); }); if (!accepted) return null; } diff --git a/app/apps/evenhub/store-layer.ts b/app/apps/evenhub/store-layer.ts index a19a8c15..966fc279 100644 --- a/app/apps/evenhub/store-layer.ts +++ b/app/apps/evenhub/store-layer.ts @@ -1,16 +1,16 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; import { truncateText, wrapText } from "../../graphics/textwrap"; -import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers"; -import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; +import { drawSelectionHighlight, type MenuItem } from "../../ui/menu"; +import { Menu } from "../../ui/menu-core"; import { shell } from "../../ui/shell/shell"; import { ConfigSettingString } from "../../ui/dashboard-settings"; import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp, type EvenHubStorePage } from "./even-api"; import { clearEvenHubLoginForm, clearEvenHubSession, clearTransientEvenHubSession, evenHubLoginEmailSetting, evenHubLoginPasswordSetting, evenHubRememberMeSetting, resetEvenHubLoginForm } from "./credentials"; import { getInstalledEvenHubApps, uninstallEvenHubPackage, type InstalledEvenHubApp } from "./installed-apps"; -import { EvenHubStoreDetailLayer } from "./store-detail-layer"; +import { EvenHubStoreDetailLayer, type EvenHubStoreDetailOptions } from "./store-detail-layer"; import { cleanError, installStoreApp } from "./store-install"; import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates"; import { lineStep } from "../../ui/metrics"; @@ -24,6 +24,11 @@ function headerHeight(font: UiFont): number { return 8 + lineStep(font) + font.lineHeight + 6; } +/** Row pitch of the two-line app list (title + detail), including the 2px gap between highlights. */ +function storeRowHeight(font: UiFont): number { + return Math.max(30, 2 * lineStep(font) + 4); +} + /** Last search typed on the phone; kept so reopening the store shows it again. */ export const evenHubSearchQuerySetting = new ConfigSettingString({ id: "evenhub-search-query", @@ -64,27 +69,46 @@ type TabState = { loading: boolean; /** Progress or error text for the subtitle line. */ status: string; - selectedIndex: number; - scrollRow: number; + /** Selection and scroll over `rows` (resynced before each paint and input). */ + menu: Menu; /** Search only: the query the rows belong to. */ query: string; }; function emptyTab(): TabState { + const rows: StoreRow[] = []; return { - rows: [], + rows, total: 0, nextPage: 1, exhausted: false, loaded: false, loading: false, status: "", - selectedIndex: 0, - scrollRow: 0, + menu: new Menu({ + items: rows, + rowGap: 2, + highlight: { radius: 5 }, + getHeight: () => storeRowHeight(getDefaultSmallFont()), + draw: ({ image, item, x, y, width }) => { + const font = getDefaultSmallFont(); + const { title, detail, titleValue } = rowText(item); + const textX = x + 6; + const textW = width - 12; + image.drawText(font, textX, y + 2, truncateText(font, title, textW), titleValue); + image.drawText(font, textX, y + 2 + lineStep(font), truncateText(font, detail, textW), 115); + }, + }), query: "", }; } +/** The tab's menu, with its items brought up to date with `rows` (which loads replace or extend). */ +function tabMenu(tab: TabState): Menu { + if (tab.menu.items !== tab.rows) tab.menu.setItems(tab.rows); + return tab.menu; +} + export type EvenHubStoreLayerOptions = { launchApp: (appId: string) => Promise | void; appendLog: (message: string) => void; @@ -126,13 +150,11 @@ export class EvenHubStoreLayer implements Layer { this.started = true; // Do NOT open the phone credential editor here. This runs on the first // paint, which includes the restore of a still-open EvenHub at app - // startup -- so an unfinished sign-in re-opened the form on every launch, - // and the form has no way out: it has no Cancel control, its onCancel is - // dropped before it reaches the controller, and Back leaves the app - // rather than closing it. The login pane already says "Enter your Even - // account email and password in the phone app" and already opens the - // editor on click (handleInput), so the editor is still one tap away for - // someone who actually wants it. + // startup -- so an unfinished sign-in re-opened the form on every + // launch, unasked. The login pane already says "Enter your Even account + // email and password in the phone app" and already opens the editor on + // click (handleInput), so the editor is still one tap away for someone + // who actually wants it. if (!this.showingLogin) { void this.ensureTabLoaded(ctx, this.activeTab); } @@ -170,25 +192,16 @@ export class EvenHubStoreLayer implements Layer { return image; } - tab.selectedIndex = clamp(tab.selectedIndex, 0, tab.rows.length - 1); - const rowH = Math.max(30, 2 * lineStep(font) + 4); + const rowH = storeRowHeight(font); const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH)); - tab.scrollRow = scrollToKeepSelectionVisible(tab.scrollRow, tab.selectedIndex, visibleRows, tab.rows.length); - const last = Math.min(tab.rows.length, tab.scrollRow + visibleRows); const listFocused = ctx.stack.isFocused() && this.focus === "list"; - for (let index = tab.scrollRow; index < last; index++) { - const row = tab.rows[index]!; - const y = headerH + (index - tab.scrollRow) * rowH; - if (index === tab.selectedIndex) { - drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, listFocused, 5); - } - const { title, detail, titleValue } = this.rowText(row); - image.drawText(font, LIST_X, y + 2, truncateText(font, title, width - LIST_X * 2), titleValue); - image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115); - } - if (tab.rows.length > visibleRows) { - drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, tab.scrollRow, visibleRows, tab.rows.length); - } + const menu = tabMenu(tab); + menu.paint( + image, + { x: LIST_X - 6, y: headerH, width: width - LIST_X * 2 + 12, height: visibleRows * rowH - 2 }, + listFocused, + ); + menu.drawScrollbar(image, width - 5, headerH, visibleRows * rowH - 3); return image; } @@ -250,41 +263,6 @@ export class EvenHubStoreLayer implements Layer { } } - private rowText(row: StoreRow): { title: string; detail: string; titleValue: number } { - switch (row.kind) { - case "search-query": { - const query = evenHubSearchQuerySetting.get(); - return { - title: query ? `Search: ${query}` : "Search: (enter a query)", - detail: "Click to type on the phone, or use voice input.", - titleValue: 200, - }; - } - case "orphan": - return { - title: row.update.installed.name, - detail: row.update.error - ? `Package missing · store lookup failed: ${row.update.error}` - : "Package missing · not in the store · click to forget", - titleValue: 170, - }; - case "app": { - const { app, update } = row; - if (!update) { - return { - title: app.name, - detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`, - titleValue: 215, - }; - } - const detail = update.packageMissing - ? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim() - : `Version ${update.installed.version} installed · ${app.version} available`; - return { title: app.name, detail, titleValue: 215 }; - } - } - } - async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (this.showingLogin) { if (event.type === "click" && !this.loginBusy) { @@ -299,16 +277,17 @@ export class EvenHubStoreLayer implements Layer { return; } const tab = this.tabs[this.activeTab]; + const menu = tabMenu(tab); switch (event.type) { case "scroll-up": - tab.selectedIndex = Math.max(0, tab.selectedIndex - 1); + await menu.handleInput(event); return; case "scroll-down": - tab.selectedIndex = Math.min(Math.max(0, tab.rows.length - 1), tab.selectedIndex + 1); - if (tab.selectedIndex >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab); + await menu.handleInput(event); + if ((menu.selectedIndex ?? 0) >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab); return; case "click": - await this.activateRow(ctx, tab.rows[tab.selectedIndex]); + await this.activateRow(ctx, menu.selectedItem ?? undefined); return; case "double-click": this.focus = "tabs"; @@ -367,13 +346,7 @@ export class EvenHubStoreLayer implements Layer { } case "app": if (this.updatingAll) return; - ctx.stack.push( - new EvenHubStoreDetailLayer(row.app, { - launchApp: this.options.launchApp, - appendLog: this.options.appendLog, - onInstalled: (installed) => this.onInstalled(installed), - }), - ); + ctx.stack.push(new EvenHubStoreDetailLayer(row.app, this.detailOptions())); return; } } @@ -391,13 +364,7 @@ export class EvenHubStoreLayer implements Layer { } this.pendingPackage = null; stack.clearToBase(); - stack.push( - new EvenHubStoreDetailLayer(placeholderStoreApp(installed), { - launchApp: this.options.launchApp, - appendLog: this.options.appendLog, - onInstalled: (result) => this.onInstalled(result), - }), - ); + stack.push(new EvenHubStoreDetailLayer(placeholderStoreApp(installed), this.detailOptions())); } /** Dictated text becomes a search (the shell's "Type Into App" / voice input). */ @@ -540,7 +507,7 @@ export class EvenHubStoreLayer implements Layer { state.rows = [{ kind: "search-query" }]; state.loaded = !query; // Keep the cursor on the first result once there are some. - state.selectedIndex = query ? 1 : 0; + state.menu.setItems(state.rows, query ? 1 : 0); this.tabs.search = state; ctx.actions.requestRender(); if (query) await this.loadNextPage(ctx, "search"); @@ -566,8 +533,8 @@ export class EvenHubStoreLayer implements Layer { update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update }, ); state.rows = rows; - state.selectedIndex = 0; - state.scrollRow = 0; + state.menu.setItems(rows, 0); + state.menu.scrollTop = 0; state.status = ""; state.exhausted = true; } catch (error) { @@ -614,6 +581,8 @@ export class EvenHubStoreLayer implements Layer { failures.push(`${row.app.name} (declined)`); } } catch (error) { + // An expired session fails every remaining update the same way. + if (this.handleAuthError(ctx, error)) return; const message = cleanError(error); this.options.appendLog(`evenhub store: update of ${row.app.packageId} failed: ${message}`); failures.push(row.app.name); @@ -643,20 +612,38 @@ export class EvenHubStoreLayer implements Layer { state.rows = state.rows.filter((row) => row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId, ); - state.selectedIndex = clamp(state.selectedIndex, 0, Math.max(0, state.rows.length - 1)); + state.menu.setItems(state.rows); } // ---- login and phone editors -------------------------------------------- - /** Route a rejected session back to the login form; true if it was one. */ + /** + * Drop back to the logged-out pane when Even rejects the session (an + * expired login); true if the error was one. Anything open above the list + * (an app page, a permission dialog) needed the session too, so it goes. + * The phone form is not opened: this can happen on the first load of a + * store window restored at startup (see paint), and the pane's click + * opens it. + */ private handleAuthError(ctx: LayerContext, error: unknown): boolean { if (!(error instanceof EvenHubAuthenticationError)) return false; const message = cleanError(error); - this.options.appendLog(`evenhub store login failed: ${message}`); - this.enterLogin(ctx, `Login failed: ${message}`); + this.options.appendLog(`evenhub store: session rejected: ${message}`); + if (this.closed) return true; + ctx.stack.clearToBase(); + this.enterLogin(ctx, `Signed out: ${message}`, false); return true; } + private detailOptions(): EvenHubStoreDetailOptions { + return { + launchApp: this.options.launchApp, + appendLog: this.options.appendLog, + onInstalled: (installed) => this.onInstalled(installed), + onAuthError: (ctx, error) => this.handleAuthError(ctx, error), + }; + } + private enterLogin( ctx: LayerContext, status = "Sign in to browse public apps.", @@ -710,13 +697,11 @@ export class EvenHubStoreLayer implements Layer { const email = evenHubLoginEmailSetting.get(); const password = evenHubLoginPasswordSetting.get(); if (!email.trim() || !password) { - // The phone editor has no Cancel control, so submitting an untouched form - // with the keyboard's Done key is the only way someone can back out of - // it. Reopening the editor there makes that a loop with no exit, so a - // wholly empty submission is treated as a cancel and leaves the editor - // closed -- the login pane still says how to get back in, and a click - // reopens the editor. A half-filled form is a real mistake, so that - // still reopens with the message. + // A wholly empty submission (the keyboard's Done key on an untouched + // form) is treated as a cancel and leaves the editor closed -- the login + // pane still says how to get back in, and a click reopens the editor. + // A half-filled form is a real mistake, so that still reopens with the + // message. const cancelled = !email.trim() && !password; this.enterLogin( ctx, @@ -755,6 +740,42 @@ export class EvenHubStoreLayer implements Layer { } } +/** A list row's title (with its shade) and dim detail line. */ +function rowText(row: StoreRow): { title: string; detail: string; titleValue: number } { + switch (row.kind) { + case "search-query": { + const query = evenHubSearchQuerySetting.get(); + return { + title: query ? `Search: ${query}` : "Search: (enter a query)", + detail: "Click to type on the phone, or use voice input.", + titleValue: 200, + }; + } + case "orphan": + return { + title: row.update.installed.name, + detail: row.update.error + ? `Package missing · store lookup failed: ${row.update.error}` + : "Package missing · not in the store · click to forget", + titleValue: 170, + }; + case "app": { + const { app, update } = row; + if (!update) { + return { + title: app.name, + detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`, + titleValue: 215, + }; + } + const detail = update.packageMissing + ? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim() + : `Version ${update.installed.version} installed · ${app.version} available`; + return { title: app.name, detail, titleValue: 215 }; + } + } +} + /** A registry entry dressed as a store record, for pages opened before the store record arrives. */ function placeholderStoreApp(installed: InstalledEvenHubApp): EvenHubStoreApp { return { diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index eac9645e..2227b4cc 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -4,7 +4,8 @@ import { GrayImage, type UiFont } from "../../graphics/image"; import { renderIcon, type IconName } from "../../graphics/icons"; import { clamp } from "../../util/numeric-util"; import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; -import { iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics"; import { ConfigSettingEnum } from "../../ui/dashboard-settings"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { @@ -20,6 +21,8 @@ import { Layer, type LayerContext } from "../../ui/layers"; import { shell } from "../../ui/shell/shell"; const LIST_X = 20; +/** List view: the selection box extends this far left and right of the row text. */ +const LIST_HIGHLIGHT_PAD = 6; /** Header: one line holding both the title and the current path. */ function headerHeight(font: UiFont): number { @@ -133,7 +136,14 @@ export class FileBrowserLayer implements Layer { private entries: DirectoryEntry[] | null = null; private listingFailed = false; - private listIndex = 0; + /** List view. Its selected index is the flat-row index both views translate through (setViewMode). */ + private readonly listMenu = new Menu({ + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawListRow(args), + }); private selectedRow = 0; private selectedCol = 0; private iconMode: IconMode = "row"; @@ -162,25 +172,20 @@ export class FileBrowserLayer implements Layer { private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void { const { width, height } = ctx.stack.getBaseSize(); - this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1)); - - const rowH = tightRowHeight(font); const headerH = headerHeight(font); - const listHeight = height - headerH - 4; - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.listIndex, visibleRows, rows.length); + this.listMenu.setItems(rows); + // Row boxes start one pixel above the header's bottom edge. + this.listMenu.paint( + image, + { x: LIST_X - LIST_HIGHLIGHT_PAD, y: headerH - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: height - headerH - 4 }, + ctx.stack.isFocused(), + ); + } - const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows); - for (let index = this.scrollRow; index < lastVisible; index++) { - const row = rows[index]!; - const y = headerH + (index - this.scrollRow) * rowH; - const selected = index === this.listIndex; - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4); - } - const value = itemValue(row, selected); - image.drawText(font, LIST_X, y + 1, truncateText(font, row.label, width - 2 * LIST_X), value); - } + private drawListRow({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const label = truncateText(font, item.label, width - 2 * LIST_HIGHLIGHT_PAD); + image.drawText(font, x + LIST_HIGHLIGHT_PAD, centeredTextY(font, y, height), label, itemValue(item, selected)); } private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void { @@ -276,16 +281,14 @@ export class FileBrowserLayer implements Layer { } private async handleListInput(event: InputEvent, ctx: LayerContext): Promise { - const rows = this.flatRows(); + this.listMenu.setItems(this.flatRows()); switch (event.type) { case "scroll-up": - this.listIndex = Math.max(0, this.listIndex - 1); - return; case "scroll-down": - this.listIndex = Math.min(Math.max(0, rows.length - 1), this.listIndex + 1); + await this.listMenu.handleInput(event); return; case "click": { - const row = rows[clamp(this.listIndex, 0, Math.max(0, rows.length - 1))]; + const row = this.listMenu.selectedItem; if (row) await this.activateItem(row, ctx); return; } @@ -461,8 +464,7 @@ export class FileBrowserLayer implements Layer { label: isBookmarked(path) ? `Remove bookmark: ${name}` : `Bookmark: ${name}`, onSelect: (ctx) => { toggleBookmark(path); - const rows = this.flatRows(); - this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1)); + this.listMenu.setItems(this.flatRows()); ctx.stack.pop(); }, }); @@ -475,7 +477,8 @@ export class FileBrowserLayer implements Layer { const rows = this.flatRows(); if (!rows.length) return null; if (filesViewModeSetting.get() === "list") { - return rows[clamp(this.listIndex, 0, rows.length - 1)] ?? null; + this.listMenu.setItems(rows); + return this.listMenu.selectedItem; } const iconRows = buildIconRows(rows); const row = iconRows[clamp(this.selectedRow, 0, iconRows.length - 1)]; @@ -489,20 +492,23 @@ export class FileBrowserLayer implements Layer { if (filesViewModeSetting.get() === mode) return; const rows = this.flatRows(); const iconRows = buildIconRows(rows); + this.listMenu.setItems(rows); if (mode === "list") { // Carry the icon-grid selection into the flat list (clamped column when // only a row was selected). const row = iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))]; if (row) { - this.listIndex = - row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1); + this.listMenu.select( + row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1), + ); } } else { - this.setGridSelectionFromFlatIndex(rows, clamp(this.listIndex, 0, Math.max(0, rows.length - 1))); + this.setGridSelectionFromFlatIndex(rows, this.listMenu.selectedIndex ?? 0); this.iconMode = "row"; } filesViewModeSetting.set(mode); this.scrollRow = 0; + this.listMenu.scrollTop = 0; } private async activateItem(item: BrowserItem, ctx: LayerContext): Promise { @@ -548,7 +554,8 @@ export class FileBrowserLayer implements Layer { } private resetSelection(): void { - this.listIndex = 0; + this.listMenu.select(0); + this.listMenu.scrollTop = 0; this.selectedRow = 0; this.selectedCol = 0; this.iconMode = "row"; @@ -560,7 +567,9 @@ export class FileBrowserLayer implements Layer { const rows = this.flatRows(); const index = rows.findIndex((row) => row.kind === "entry" && row.entry.path === path); if (index < 0) return; - this.listIndex = index; + // Items first, then the selection, so the viewport scrolls minimally from the top. + this.listMenu.setItems(rows); + this.listMenu.select(index); this.setGridSelectionFromFlatIndex(rows, index); } diff --git a/app/apps/files/file-info-dialog.ts b/app/apps/files/file-info-dialog.ts index ff44fec2..5a35528b 100644 --- a/app/apps/files/file-info-dialog.ts +++ b/app/apps/files/file-info-dialog.ts @@ -4,7 +4,7 @@ import { wrapText } from "../../graphics/textwrap"; import type { DirectoryEntry } from "../../native/file-access"; import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; -import { drawSelectionHighlight } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; const DIALOG_X = 8; @@ -25,12 +25,21 @@ export type FileInfoAction = { * double-click closes it. */ export class FileInfoDialogLayer implements Layer { - private selectedIndex = 0; + private readonly menu: Menu; constructor( private readonly entry: DirectoryEntry, private readonly actions: FileInfoAction[], - ) {} + ) { + this.menu = new Menu({ + items: actions, + wrap: true, + rowGap: 1, + highlight: { radius: 8 }, + getHeight: () => listRowHeight(getDefaultSmallFont()), + draw: (args) => drawActionRow(args), + }); + } paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { const font = getDefaultSmallFont(); @@ -69,32 +78,23 @@ export class FileInfoDialogLayer implements Layer { image.drawText(font, DIALOG_X + PADDING + 2, y + 3, `${GESTURE_DOUBLE_CLICK} close`, 110); return image; } - const focused = ctx.stack.isFocused(); - for (let index = 0; index < this.actions.length; index++) { - const rowY = y + index * rowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, rowH - 1, focused, 8); - } - image.drawText(font, DIALOG_X + 22, rowY + LIST_ROW_TEXT_INSET, this.actions[index]!.label, selected ? 255 : 200); - } + // The rows fill the rest of the dialog (and scroll if a clamped dialog is too short). + this.menu.paint( + image, + { x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) }, + ctx.stack.isFocused(), + ); return image; } async handleInput(event: InputEvent, ctx: LayerContext): Promise { switch (event.type) { case "scroll-up": - if (this.actions.length) { - this.selectedIndex = (this.selectedIndex + this.actions.length - 1) % this.actions.length; - } - return; case "scroll-down": - if (this.actions.length) { - this.selectedIndex = (this.selectedIndex + 1) % this.actions.length; - } + await this.menu.handleInput(event); return; case "click": - await this.actions[this.selectedIndex]?.onSelect(ctx); + await this.menu.selectedItem?.onSelect(ctx); return; case "double-click": ctx.stack.pop(); @@ -105,6 +105,10 @@ export class FileInfoDialogLayer implements Layer { } } +function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs): void { + image.drawText(getDefaultSmallFont(), x + 10, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200); +} + function formatSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; diff --git a/app/apps/flappy/flappy-app.worker.ts b/app/apps/flappy/flappy-app.worker.ts index d3cf5cce..d880c239 100644 --- a/app/apps/flappy/flappy-app.worker.ts +++ b/app/apps/flappy/flappy-app.worker.ts @@ -25,6 +25,7 @@ * focus to the sidebar) pauses, as does backgrounding or screen-off. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -180,6 +181,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: FlappyWindow = { windowId: message.windowId, @@ -761,17 +768,19 @@ function renderAndSubmit(window: FlappyWindow, inputFrameId: number): void { } const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes)); const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); - communicator.submitSurfaceFrame( - buffer.buffer, - window.surfaceId, - 0, - 0, - image.width, - image.height, - fingerprint, - paintMs, - frameId, - frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)), + frameTimings.span(frameId, "submitSurfaceFrame", () => + communicator.submitSurfaceFrame( + buffer.buffer, + window.surfaceId, + 0, + 0, + image.width, + image.height, + fingerprint, + paintMs, + frameId, + frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)), + ) ); window.lastSubmittedFingerprint = fingerprint; } catch (error) { diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index f829dfc8..ae4f2dc0 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -18,6 +18,7 @@ * Safe cards auto-play to the foundations after every move. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -144,6 +145,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: FreecellWindow = { windowId: message.windowId, diff --git a/app/apps/glanceboard/configuration.ts b/app/apps/glanceboard/configuration.ts new file mode 100644 index 00000000..a783df47 --- /dev/null +++ b/app/apps/glanceboard/configuration.ts @@ -0,0 +1,11 @@ +import { getStringSetting } from "../../native/settings-store"; +import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT } from "./layout"; + +/** Configuration-only query usable by workers without loading widget/UI modules. */ +export function isTerminalWidgetConfigured(): boolean { + const layout = getStringSetting("glanceboard.layout", "2x2") === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT; + // Both layouts store their slots under the original quadrants key. None + // of the default slots needs a worker, so absent keys cannot retain one. + return layout.slots.some((_, index) => + getStringSetting(`glanceboard.${QUADRANT_LAYOUT.id}.slot.${index}`, "none") === "terminal"); +} diff --git a/app/apps/glanceboard/glanceboard-app.ts b/app/apps/glanceboard/glanceboard-app.ts index a0e2b236..e6bc38f7 100644 --- a/app/apps/glanceboard/glanceboard-app.ts +++ b/app/apps/glanceboard/glanceboard-app.ts @@ -3,15 +3,17 @@ import { truncateText, wrapText } from "../../graphics/textwrap"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings"; import { type InputEvent } from "../../ui/gestures"; -import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; -import { drawSelectionHighlight, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu"; -import { LIST_ROW_TEXT_INSET, lineStep } from "../../ui/metrics"; +import { type Layer, type LayerContext } from "../../ui/layers"; +import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu"; +import { Menu } from "../../ui/menu-core"; +import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; import { shell } from "../../ui/shell/shell"; import { GlanceBoard } from "./board"; import { clearConflictingSlots, glanceboardEnabledSetting, + glanceDepthSetting, glanceLayout, glanceLayoutSetting, glanceShowLinesSetting, @@ -35,6 +37,15 @@ const PARAGRAPH_X = 24; const PREVIEW_MARGIN = 8; const PREVIEW_MIN_LINE_VALUE = 20; const PREVIEW_LINE_VALUE = 100; +/** The entry menu's row boxes: left inset, gap below the paragraph, and inset from the page's bottom. */ +const HOME_MENU_X = 20; +const HOME_MENU_TOP_GAP = 12; +const HOME_MENU_BOTTOM_MARGIN = 8; +/** Horizontal inset of row text (and custom row content) from its selection box. */ +const HOME_ROW_TEXT_X = 10; +// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the +// entry page's items never use it (they act via ctx only). +const NO_MENU = new MenuLayer(null, []); const INTRO = "Your Glanceboard is a display for things you want to look at quickly. Starting " + @@ -55,25 +66,21 @@ function pageMenuLayout(width: number) { * a double-tap returns focus to the menu. */ class GlanceboardHomeLayer implements Layer { - private menu: MenuLayer | null = null; - private menuTop = 0; - private menuHeight = 0; + private readonly menu: Menu; private focus: "menu" | "preview" = "menu"; private selectedSlot = 0; + /** The context of the paint in progress, handed to item render callbacks. */ + private paintCtx: LayerContext | null = null; - constructor(private readonly setPreviewVisible: (visible: boolean) => void) {} - - private currentMenu(ctx: LayerContext, menuTop: number): MenuLayer { - const { width, height } = ctx.stack.getBaseSize(); - if (!this.menu || this.menuTop !== menuTop || this.menuHeight !== height) { - const half = (width / 2) | 0; - const items: MenuItem[] = [ + constructor(private readonly setPreviewVisible: (visible: boolean) => void) { + this.menu = new Menu({ + items: [ toggleSettingMenuItem(glanceboardEnabledSetting), { label: "Preview", - onSelect: (menuCtx) => { - menuCtx.stack.push(new GlancePreviewLayer( - () => menuCtx.actions.requestRender(), + onSelect: (ctx) => { + ctx.stack.push(new GlancePreviewLayer( + () => ctx.actions.requestRender(), () => this.setPreviewVisible(false), )); this.setPreviewVisible(true); @@ -87,17 +94,32 @@ class GlanceboardHomeLayer implements Layer { }, { label: "Settings", - onSelect: (menuCtx) => menuCtx.stack.push(new GlanceSettingsLayer()), + onSelect: openGlanceSettings, }, - ]; - this.menuTop = menuTop; - this.menuHeight = height; - this.menu = new MenuLayer(null, items, { - x: 8, y: menuTop, width: half - 16, - showBorder: false, minHeight: 0, maxHeight: height - menuTop, - }); - } - return this.menu; + ], + wrap: true, + rowGap: 1, + getHeight: () => listRowHeight(getDefaultSmallFont()), + draw: ({ image, item, x, y, width, height, selected }) => { + const disabled = isMenuItemDisabled(item); + if (item.render) { + item.render({ + image, + x: x + HOME_ROW_TEXT_X, + y, + width: width - 2 * HOME_ROW_TEXT_X, + height: height - 2, + selected, + disabled, + text: item.label, + ctx: this.paintCtx!, + }); + } else { + image.drawText(getDefaultSmallFont(), x + HOME_ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label, + disabled ? 70 : selected ? 255 : 200); + } + }, + }); } /** Where the scaled board sits: the right half, board aspect, vertically centred. */ @@ -128,13 +150,19 @@ class GlanceboardHomeLayer implements Layer { image.drawText(font, PARAGRAPH_X, y, line, 160); y += step; } - // The menu paints onto this page below the paragraph (its fill is raster, - // so the deferred paragraph glyphs above stay untouched either way). - // While the preview has focus the menu keeps its outline-only selection. - const menu = this.currentMenu(ctx, y + 4); - const menuFocused = ctx.stack.isFocused() && this.focus === "menu"; - const menuCtx: LayerContext = { ...ctx, stack: Object.assign(Object.create(ctx.stack), { isFocused: () => menuFocused }) }; - menu.paint(menuCtx, () => image); + // The menu sits below the paragraph in the left half. While the preview + // has focus the menu keeps its outline-only selection. + const menuTop = y + HOME_MENU_TOP_GAP; + const menuWidth = half - 2 * HOME_MENU_X; + const menuHeight = Math.max(0, height - menuTop - HOME_MENU_BOTTOM_MARGIN); + this.paintCtx = ctx; + try { + this.menu.paint(image, { x: HOME_MENU_X, y: menuTop, width: menuWidth, height: menuHeight }, + ctx.stack.isFocused() && this.focus === "menu"); + } finally { + this.paintCtx = null; + } + this.menu.drawScrollbar(image, HOME_MENU_X + menuWidth + 5, menuTop, menuHeight - 4); this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1); const preview = this.previewRect(width, height); @@ -173,7 +201,12 @@ class GlanceboardHomeLayer implements Layer { shell.yieldFocusToSidebar(); return; } - await this.currentMenu(ctx, this.menuTop).handleInput(event, ctx); + if (event.type === "click") { + const item = this.menu.selectedItem; + if (item && !isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU); + return; + } + await this.menu.handleInput(event); } } @@ -265,32 +298,17 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void { openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current))); } -/** Settings: layout, sleep gestures, duration, and slot lines. */ -class GlanceSettingsLayer implements Layer { - private menu: MenuLayer | null = null; - - private currentMenu(ctx: LayerContext): MenuLayer { - if (!this.menu) { - const { width } = ctx.stack.getBaseSize(); - const items: MenuItem[] = [ - enumSettingMenuItem(glanceLayoutSetting), - enumSettingMenuItem(glanceTapDurationSetting), - toggleSettingMenuItem(glanceShowOnLongPressSetting), - toggleSettingMenuItem(glanceShowOnHeadTiltSetting), - toggleSettingMenuItem(glanceShowLinesSetting), - ]; - this.menu = new MenuLayer("Glanceboard settings", items, pageMenuLayout(width)); - } - return this.menu; - } - - paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { - return this.currentMenu(ctx).paint(ctx, paintBelow); - } - - async handleInput(event: InputEvent, ctx: LayerContext): Promise { - await this.currentMenu(ctx).handleInput(event, ctx); - } +/** Settings: layout, sleep gestures, duration, slot lines, and stereo depth. */ +function openGlanceSettings(ctx: LayerContext): void { + const items: MenuItem[] = [ + enumSettingMenuItem(glanceLayoutSetting), + enumSettingMenuItem(glanceTapDurationSetting), + toggleSettingMenuItem(glanceShowOnLongPressSetting), + toggleSettingMenuItem(glanceShowOnHeadTiltSetting), + toggleSettingMenuItem(glanceShowLinesSetting), + enumSettingMenuItem(glanceDepthSetting), + ]; + ctx.stack.push(new MenuLayer("Glanceboard settings", items, pageMenuLayout(ctx.stack.getBaseSize().width))); } /** The live board in the window; any click or double-click returns to the list. */ diff --git a/app/apps/glanceboard/glanceboard-settings.ts b/app/apps/glanceboard/glanceboard-settings.ts index bcb13948..9478e695 100644 --- a/app/apps/glanceboard/glanceboard-settings.ts +++ b/app/apps/glanceboard/glanceboard-settings.ts @@ -103,6 +103,31 @@ export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({ description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.", }); +const DEPTH_VALUES = ["-64", "-48", "-32", "-16", "0", "16", "32", "48", "64"] as const; +export type GlanceDepth = (typeof DEPTH_VALUES)[number]; + +/** + * Stereo depth of the board, in the firmware's depth units: the lenses shift + * it by half this many pixels each, in opposite directions. + */ +export const glanceDepthSetting = new ConfigSettingEnum({ + id: "glanceboard-depth", + label: "Depth", + storageKey: "glanceboard.depth", + defaultValue: "0", + values: DEPTH_VALUES, + formatValue: (value) => { + const depth = Number(value); + return depth === 0 ? "0" : depth > 0 ? `+${depth} (nearer)` : `${depth} (farther)`; + }, + description: + "Moves the Glanceboard nearer (positive) or farther away (negative) by shifting it in opposite directions on the two lenses.", +}); + +export function glanceDepth(): number { + return Number(glanceDepthSetting.get()); +} + /** Hairlines between the board's slots. */ export const glanceShowLinesSetting = new ConfigSettingBoolean({ id: "glanceboard-show-lines", diff --git a/app/apps/glanceboard/index.ts b/app/apps/glanceboard/index.ts index 354bef25..65affdfa 100644 --- a/app/apps/glanceboard/index.ts +++ b/app/apps/glanceboard/index.ts @@ -3,6 +3,7 @@ import { GlanceBoard } from "./board"; import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app"; import { glanceboardEnabledSetting, + glanceDepth, glanceLayout, glanceShowOnHeadTiltSetting, glanceShowOnLongPressSetting, @@ -25,6 +26,7 @@ const glanceboardApp: AppDefinition = { tapTimeoutMs: glanceTapTimeoutMs, showOnLongPress: () => glanceShowOnLongPressSetting.get(), showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(), + depth: glanceDepth, createBoard: (requestRender) => new GlanceBoard(requestRender), }, }; diff --git a/app/apps/glanceboard/widgets/nightscout-widget.ts b/app/apps/glanceboard/widgets/nightscout-widget.ts index d9425d84..14139486 100644 --- a/app/apps/glanceboard/widgets/nightscout-widget.ts +++ b/app/apps/glanceboard/widgets/nightscout-widget.ts @@ -1,4 +1,4 @@ -import { type GrayImage } from "../../../graphics/image"; +import { GrayImage } from "../../../graphics/image"; import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts"; import { truncateText } from "../../../graphics/textwrap"; import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge"; @@ -91,22 +91,6 @@ export class NightscoutWidget implements GlanceWidget { image.drawText(small, PAD, y, `IOB ${state.iob === null ? "--" : state.iob.toFixed(2)}`, 160); y += step; - // Breached limits only, one per line while they fit, inverted like the - // app's warning fields. Nothing here means nothing is out of range. - const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs); - const warnings: string[] = []; - if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`); - if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`); - if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`); - if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`); - for (const warning of warnings) { - if (y + small.lineHeight > image.height - 2) break; - const text = truncateText(small, warning, READOUT_WIDTH - 4); - image.fillRect(PAD - 2, y - 1, small.measureText(text) + 4, small.lineHeight + 2, 230); - image.drawText(small, PAD, y, text, 1); - y += step; - } - const graphX = PAD + READOUT_WIDTH + PAD; drawNightscoutGraph( image, @@ -115,5 +99,32 @@ export class NightscoutWidget implements GlanceWidget { nowMs, small, ); + + // Breached limits only, inverted like the app's warning fields. Continue + // at the top of a second column over the graph when the readout is full. + const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs); + const warnings: string[] = []; + if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`); + if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`); + if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`); + if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`); + let warningX = PAD; + for (const warning of warnings) { + if (y + small.lineHeight > image.height - 2) { + warningX = graphX; + y = PAD; + } + const text = truncateText(small, warning, READOUT_WIDTH - 4); + const width = Math.ceil(small.measureText(text)) + 4; + if (warningX === PAD) { + image.fillRect(warningX - 2, y - 1, width, small.lineHeight + 2, 230); + } else { + // Graph labels are deferred glyphs: a deferred opaque background + // covers those as well as the graph's raster lines. + image.drawImage(new GrayImage(width, small.lineHeight + 2, 230), warningX - 2, y - 1); + } + image.drawText(small, warningX, y, text, 1); + y += step; + } } } diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index dcb4189d..ebc22c9f 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -18,6 +18,7 @@ * launches. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -152,6 +153,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: MinesweeperWindow = { windowId: message.windowId, diff --git a/app/apps/music/media-browse.ts b/app/apps/music/media-browse.ts index 51e50d9a..3876c67e 100644 --- a/app/apps/music/media-browse.ts +++ b/app/apps/music/media-browse.ts @@ -1,15 +1,9 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; -import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures"; -import { - MenuLayer, - drawListScrollbar, - drawSelectionHighlight, - drawSubmenuIndicator, - scrollToKeepSelectionVisible, -} from "../../ui/menu"; -import { lineStep, tightRowHeight } from "../../ui/metrics"; +import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; import { mediaBrowserBridge, type MediaBrowseItem, @@ -22,6 +16,8 @@ function headerHeight(font: UiFont): number { return 8 + lineStep(font) + font.lineHeight; } const LIST_X = 20; +/** The selection box extends this far left and right of the row text. */ +const LIST_HIGHLIGHT_PAD = 6; export type MediaBrowseOptions = { app: MediaBrowserApp; @@ -37,8 +33,8 @@ type BrowseLevel = { /** null while the listing is loading. */ items: MediaBrowseItem[] | null; error: string; - selectedIndex: number; - scrollRow: number; + /** Holds the loaded items; keeps this level's selection and scroll while a child level is open. */ + menu: Menu; }; type BrowsePhase = "idle" | "connecting" | "ready" | "failed"; @@ -97,50 +93,16 @@ export class MediaBrowseLayer implements Layer { return image; } - level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1); - const listHeight = height - headerHeight(font) - font.lineHeight - 10; - const rowH = tightRowHeight(font); - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length); - - const rowWidth = width - 2 * LIST_X; - const lastVisible = Math.min(items.length, level.scrollRow + visibleRows); - for (let index = level.scrollRow; index < lastVisible; index++) { - const item = items[index]!; - const y = headerHeight(font) + (index - level.scrollRow) * rowH; - const selected = index === level.selectedIndex; - const highlightX = LIST_X - 6; - const highlightY = y - 1; - const highlightWidth = rowWidth + 12; - const highlightHeight = rowH - 1; - if (selected) { - drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4); - } - const value = selected ? 255 : 200; - const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16); - image.drawText(font, LIST_X, y + 1, title, value); - if (item.subtitle) { - const titleWidth = font.measureText(title); - const subtitleWidth = font.measureText(item.subtitle); - if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) { - image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110); - } - } - if (item.browsable) { - drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110); - } - } - if (items.length > visibleRows) { - drawListScrollbar( - image, - width - 12, - headerHeight(font), - visibleRows * rowH - 4, - level.scrollRow, - visibleRows, - items.length, - ); - } + const listTop = headerHeight(font); + const listHeight = height - listTop - font.lineHeight - 10; + const visibleRows = Math.max(1, (listHeight / tightRowHeight(font)) | 0); + // Row boxes start one pixel above the header's bottom edge. + level.menu.paint( + image, + { x: LIST_X - LIST_HIGHLIGHT_PAD, y: listTop - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: listHeight }, + ctx.stack.isFocused(), + ); + level.menu.drawScrollbar(image, width - 12, listTop, visibleRows * tightRowHeight(font) - 4); image.drawText( font, @@ -175,13 +137,11 @@ export class MediaBrowseLayer implements Layer { if (!level || !items || !items.length) return; switch (event.type) { case "scroll-up": - level.selectedIndex = Math.max(0, level.selectedIndex - 1); - return; case "scroll-down": - level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1); + await level.menu.handleInput(event); return; case "click": { - const item = items[clamp(level.selectedIndex, 0, items.length - 1)]; + const item = level.menu.selectedItem; if (!item) return; if (item.browsable && item.playable) { ctx.stack.push( @@ -233,18 +193,46 @@ export class MediaBrowseLayer implements Layer { } private descendInto(ctx: LayerContext, item: MediaBrowseItem): void { - const level: BrowseLevel = { - parentId: item.mediaId, - title: item.title || "(untitled)", - items: null, - error: "", - selectedIndex: 0, - scrollRow: 0, - }; + const level = this.createLevel(item.mediaId, item.title || "(untitled)"); this.levels.push(level); this.loadLevel(ctx, level); } + private createLevel(parentId: string, title: string): BrowseLevel { + return { + parentId, + title, + items: null, + error: "", + menu: new Menu({ + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawItem(args), + }), + }; + } + + private drawItem({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const value = selected ? 255 : 200; + const textWidth = width - 2 * LIST_HIGHLIGHT_PAD - 16; + const textY = centeredTextY(font, y, height); + const title = truncateRight(font, item.title || "(untitled)", textWidth); + image.drawText(font, x + LIST_HIGHLIGHT_PAD, textY, title, value); + if (item.subtitle) { + const titleWidth = font.measureText(title); + const subtitleWidth = font.measureText(item.subtitle); + if (titleWidth + subtitleWidth + 24 <= textWidth) { + image.drawText(font, x + LIST_HIGHLIGHT_PAD + textWidth - subtitleWidth, textY, item.subtitle, 110); + } + } + if (item.browsable) { + drawSubmenuIndicator(image, font, x, y, width, height, selected ? value : 110); + } + } + private startConnect(ctx: LayerContext): void { this.phase = "connecting"; mediaBrowserBridge @@ -252,14 +240,7 @@ export class MediaBrowseLayer implements Layer { .then((rootId) => { if (this.removed) return; this.phase = "ready"; - const root: BrowseLevel = { - parentId: rootId, - title: "Library", - items: null, - error: "", - selectedIndex: 0, - scrollRow: 0, - }; + const root = this.createLevel(rootId, "Library"); this.levels = [root]; this.loadLevel(ctx, root); ctx.actions.requestRender(); @@ -278,6 +259,7 @@ export class MediaBrowseLayer implements Layer { .then((items) => { if (this.removed) return; level.items = items; + level.menu.setItems(items, 0); ctx.actions.requestRender(); }) .catch((error) => { diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts index 40eb5600..7c08867a 100644 --- a/app/apps/music/music-app.ts +++ b/app/apps/music/music-app.ts @@ -3,8 +3,9 @@ import { GrayImage } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL_DOWN, GESTURE_SCROLL_UP, type InputEvent, isDirectionalInput } from "../../ui/gestures"; -import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../../ui/menu"; -import { lineStep, tightRowHeight } from "../../ui/metrics"; +import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller"; import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser"; import { MediaBrowseLayer } from "./media-browse"; @@ -28,6 +29,8 @@ const ACTION_X = 22; const ACTION_WIDTH = 154; const COLUMN_DIVIDER_X = 190; const QUEUE_X = 204; +/** Horizontal inset of row text from its selection box. */ +const LIST_TEXT_INSET = 6; const PLAYLIST_ACTION_INDEX = 1; type MusicAction = @@ -49,9 +52,20 @@ class MusicAppLayer implements Layer { // Watch swipes skip tracks: right = next, left = previous. readonly acceptsDirectional = true; private focusColumn: FocusColumn = "actions"; - private selectedActionIndex = 0; - private selectedQueueIndex = 0; - private queueScrollRow = 0; + private readonly actionsMenu = new Menu({ + wrap: true, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawAction(args), + }); + private readonly queueMenu = new Menu({ + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawQueueItem(args), + }); private art: GrayImage | null = null; private artKey = ""; @@ -124,83 +138,49 @@ class MusicAppLayer implements Layer { const actions = this.buildActions(media, queue); this.reconcileSelection(actions, queue); const listHeight = height - LIST_TOP - LIST_BOTTOM_MARGIN; - const rowH = tightRowHeight(font); - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - if (this.selectedQueueIndex < this.queueScrollRow) { - this.queueScrollRow = this.selectedQueueIndex; - } else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) { - this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1; - } - this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows)); - - for (let index = 0; index < actions.length; index++) { - const action = actions[index]!; - const y = LIST_TOP + index * rowH; - const selected = index === this.selectedActionIndex; - const highlightX = ACTION_X - 6; - const highlightY = y - 1; - const highlightWidth = ACTION_WIDTH + 8; - const highlightHeight = rowH - 1; - if (selected) { - drawSelectionHighlight( - image, - highlightX, - highlightY, - highlightWidth, - highlightHeight, - ctx.stack.isFocused() && this.focusColumn === "actions", - 4, - ); - } - const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200; - image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value); - if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) { - drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value); - } - } + const focused = ctx.stack.isFocused(); + // Row boxes start one pixel above LIST_TOP, level with the column divider. + this.actionsMenu.paint( + image, + { x: ACTION_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4, height: listHeight }, + focused && this.focusColumn === "actions", + ); image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - LIST_BOTTOM_MARGIN - 3, 45); if (!queue.length) { image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90); } else { const queueWidth = width - QUEUE_X - 20; - const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows); - for (let index = this.queueScrollRow; index < lastVisible; index++) { - const item = queue[index]!; - const y = LIST_TOP + (index - this.queueScrollRow) * rowH; - const selected = index === this.selectedQueueIndex; - if (selected) { - drawSelectionHighlight( - image, - QUEUE_X - 6, - y - 1, - queueWidth + 8, - rowH - 1, - ctx.stack.isFocused() && this.focusColumn === "playlist", - 4, - ); - } - const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`; - image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200); - } - if (queue.length > visibleRows) { - const trackHeight = visibleRows * rowH - 4; - const trackX = width - 12; - image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30); - const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0); - const maxScrollRow = queue.length - visibleRows; - const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0); - image.fillRect(trackX, thumbY, 3, thumbHeight, 120); - } + this.queueMenu.paint( + image, + { x: QUEUE_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: queueWidth + 2 * LIST_TEXT_INSET - 4, height: listHeight }, + focused && this.focusColumn === "playlist", + ); + this.queueMenu.drawScrollbar(image, width - 12, LIST_TOP, listHeight - 4); } return image; } + private drawAction({ image, item: action, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200; + image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, action.label, ACTION_WIDTH - 16), value); + if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) { + drawSubmenuIndicator(image, font, x, y, width, height, value); + } + } + + private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`; + image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200); + } + async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (event.type === "double-click" && this.focusColumn === "playlist") { this.focusColumn = "actions"; - this.selectedActionIndex = PLAYLIST_ACTION_INDEX; + this.actionsMenu.select(PLAYLIST_ACTION_INDEX); return; } const media = mediaControllerBridge.snapshot(); @@ -237,31 +217,20 @@ class MusicAppLayer implements Layer { case "swipe-down": return this.handleInput({ type: "scroll-down", timestampMs: event.timestampMs }, ctx); case "scroll-up": - if (this.focusColumn === "playlist") { - this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1); - } else { - this.selectedActionIndex = (this.selectedActionIndex + actions.length - 1) % actions.length; - } - return; case "scroll-down": - if (this.focusColumn === "playlist") { - this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1); - } else { - this.selectedActionIndex = (this.selectedActionIndex + 1) % actions.length; - } + await (this.focusColumn === "playlist" ? this.queueMenu : this.actionsMenu).handleInput(event); return; case "click": { if (this.focusColumn === "playlist") { - const item = queue[this.selectedQueueIndex]; + const item = this.queueMenu.selectedItem; if (item) { await mediaControllerBridge.skipToQueueItem(item.id); // The player rebuilds the queue with the chosen track at index 0. - this.selectedQueueIndex = 0; - this.queueScrollRow = 0; + this.queueMenu.select(0); } return; } - const action = actions[this.selectedActionIndex]; + const action = this.actionsMenu.selectedItem; if (!action || !action.enabled) return; if (action.kind === "play-pause") await mediaControllerBridge.playPause(); else if (action.kind === "previous") await mediaControllerBridge.skipPrevious(); @@ -271,7 +240,7 @@ class MusicAppLayer implements Layer { ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent())); } else if (action.kind === "playlist") { const activeIndex = queue.findIndex((item) => item.active); - if (activeIndex >= 0) this.selectedQueueIndex = activeIndex; + if (activeIndex >= 0) this.queueMenu.select(activeIndex); this.focusColumn = "playlist"; } return; @@ -332,12 +301,13 @@ class MusicAppLayer implements Layer { ]; } + /** Hand the menus the freshly built rows; each keeps its selection index, clamped. */ private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void { - this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1); - this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1)); + this.actionsMenu.setItems(actions); + this.queueMenu.setItems(queue); if (!queue.length && this.focusColumn === "playlist") { this.focusColumn = "actions"; - this.selectedActionIndex = PLAYLIST_ACTION_INDEX; + this.actionsMenu.select(PLAYLIST_ACTION_INDEX); } } diff --git a/app/apps/music/music-settings.ts b/app/apps/music/music-settings.ts index 52dac95d..c4c45207 100644 --- a/app/apps/music/music-settings.ts +++ b/app/apps/music/music-settings.ts @@ -10,39 +10,45 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu"; /** Discovered apps stay visible even when ignored or no longer publishing a session. */ export class MusicSettingsLayer implements Layer { private menu: MenuLayer | null = null; - private signature = ""; - private selectedPackage = ""; + private menuWidth = 0; + /** The package behind each current row, parallel to the menu's items. */ + private packages: string[] = []; constructor() { // Refresh installed browser services, remembering ignored services too. mediaBrowserBridge.listBrowsableApps(true); } + /** Refresh the rows from the remembered apps, keeping the selection on the same package. */ private currentMenu(ctx: LayerContext): MenuLayer { const apps = readMediaApps().sort((a, b) => a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName)); + const items: MenuItem[] = apps.map((app) => ({ + label: app.appName, + onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)), + render: ({ image, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + const duplicateName = apps.some((other) => other !== app && other.appName === app.appName); + const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName; + drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48), + isMediaAppEnabled(app.packageName), selected); + }, + })); + if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} }); + const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined; + const index = apps.findIndex((app) => app.packageName === selectedPackage); + this.packages = apps.map((app) => app.packageName); + const width = ctx.stack.getBaseSize().width; - const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]); - if (!this.menu || signature !== this.signature) { - this.signature = signature; - const items: MenuItem[] = apps.map((app) => ({ - label: app.appName, - onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)), - render: ({ image, x, y, width, selected }) => { - if (selected) this.selectedPackage = app.packageName; - const font = getDefaultSmallFont(); - const duplicateName = apps.some((other) => other !== app && other.appName === app.appName); - const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName; - drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48), - isMediaAppEnabled(app.packageName), selected); - }, - })); - if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} }); + if (!this.menu || width !== this.menuWidth) { + this.menuWidth = width; this.menu = new MenuLayer("Music settings", items, { x: 8, y: 8, width: width - 16, opaque: true, showBorder: false, footer: "On = enabled Off = ignored", - }).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage))); + }).selectItem(Math.max(0, index)); + } else { + this.menu.setItems(items, index >= 0 ? index : undefined); } return this.menu; } diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts index 0acefbc9..14cdd830 100644 --- a/app/apps/navigate/navigate-app.worker.ts +++ b/app/apps/navigate/navigate-app.worker.ts @@ -7,6 +7,7 @@ * guidance text (right pane) updates per GPS fix and ships as small deltas. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -15,14 +16,12 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import { getActiveDisplay } from "../../native/active-display"; import { - drawListScrollbar, drawRightValueMenuItem, - drawSelectionHighlight, drawSubmenuIndicator, drawToggleMenuItem, - scrollToKeepSelectionVisible, type MenuItem, } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics"; import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu"; import type { LayerContext } from "../../ui/layers"; @@ -204,9 +203,8 @@ type IdleEntry = | { kind: "recent"; destination: RecentDestination } | { kind: "action"; label: string; detail: string; run: () => void }; -/** Idle-page list selection (index into idleEntries()). */ -let idleSelection = 0; -let idleScrollRow = 0; +/** The idle page's list; created on first use (idleList) so module load stays free of paint dependencies. */ +let idleMenu: Menu | null = null; /** * In-progress destination edit: the phone text editor is open on `setting` @@ -275,6 +273,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "navigation-sensors": handleNavigationSensorEvent(message.event); break; @@ -941,7 +945,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { onSelect: () => { const enabled = navigateRememberRecentSetting.toggle(); if (!enabled) clearRecentDestinations(); - clampIdleSelection(); + syncIdleList(); }, render: ({ image, x, y, width, selected }) => { drawToggleMenuItem( @@ -956,7 +960,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { ); }, }); - const selectedEntry = phase === "idle" ? idleEntries()[idleSelection] : undefined; + const selectedEntry = phase === "idle" ? syncIdleList().selectedItem : null; if (selectedEntry?.kind === "recent") { const recent = selectedEntry.destination; items.push({ @@ -964,7 +968,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { onSelect: (ctx) => { ctx.stack.pop(); removeRecentDestination(recent); - clampIdleSelection(); + syncIdleList(); }, }); } @@ -974,7 +978,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { onSelect: (ctx) => { ctx.stack.pop(); clearRecentDestinations(); - clampIdleSelection(); + syncIdleList(); }, }); } @@ -1059,7 +1063,7 @@ function customDestinationMenuItems(destination: SavedDestination): MenuItem[] { onSelect: (ctx) => { closeMenusAnd(ctx, () => { removeCustomDestination(destination.id); - clampIdleSelection(); + syncIdleList(); }); }, }, @@ -1114,7 +1118,7 @@ function finishEdit(confirmed: boolean): void { if (!editing) { navigateDestinationNameDraftSetting.set(""); navigateDestinationAddressDraftSetting.set(""); - clampIdleSelection(); + syncIdleList(); render(); } } @@ -1199,9 +1203,27 @@ function idleEntries(): IdleEntry[] { return entries; } -function clampIdleSelection(): void { - const count = idleEntries().length; - idleSelection = Math.max(0, Math.min(idleSelection, count - 1)); +/** Horizontal inset of the idle list's selection boxes. */ +const IDLE_LIST_X = 20; +/** Pixels between consecutive idle rows' selection boxes. */ +const IDLE_ROW_GAP = 2; + +function idleList(): Menu { + idleMenu ??= new Menu({ + wrap: false, + rowGap: IDLE_ROW_GAP, + highlight: { radius: 6 }, + getHeight: () => listRowHeight(smallFont), + draw: drawIdleRow, + }); + return idleMenu; +} + +/** Refresh the idle list from idleEntries(); the selection keeps its index, clamped into range. */ +function syncIdleList(): Menu { + const list = idleList(); + list.setItems(idleEntries()); + return list; } function navigateToIdleEntry(entry: IdleEntry): void { @@ -1275,7 +1297,7 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void { } else if (phase === "arrived" || phase === "map") { stopNavigation(""); } else if (phase === "idle") { - const entry = idleEntries()[idleSelection]; + const entry = syncIdleList().selectedItem; if (!entry) { frameTimings.finishFrame(frameId, "discarded: navigate ignored click"); return; @@ -1294,8 +1316,7 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void { maybeRefreshMap(); renderAndSubmit(win, frameId); } else if (phase === "idle" && idleEntries().length > 1) { - const count = idleEntries().length; - idleSelection = Math.max(0, Math.min(count - 1, idleSelection + (event.type === "scroll-down" ? 1 : -1))); + syncIdleList().moveSelection(event.type === "scroll-down" ? 1 : -1); renderAndSubmit(win, frameId); } else { frameTimings.finishFrame(frameId, "discarded: navigate ignored scroll"); @@ -1385,59 +1406,33 @@ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], t const rowHeight = listRowHeight(smallFont); const listBottom = image.height - 30; const visibleRows = Math.max(1, Math.floor((listBottom - top) / rowHeight)); - idleSelection = Math.max(0, Math.min(idleSelection, entries.length - 1)); - idleScrollRow = scrollToKeepSelectionVisible(idleScrollRow, idleSelection, visibleRows, entries.length); - const rowX = 20; const rowWidth = image.width - 40 - (entries.length > visibleRows ? 10 : 0); - const labelWidth = Math.floor(rowWidth * 0.4); - for (let row = 0; row < visibleRows; row++) { - const index = idleScrollRow + row; - const entry = entries[index]; - if (!entry) break; - const y = top + row * rowHeight; - const selected = index === idleSelection; - if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused); - const textX = rowX + 6; - const textY = y + LIST_ROW_TEXT_INSET; - if (entry.kind === "action") { - const label = truncateText(smallFont, entry.label, labelWidth); - image.drawText(smallFont, textX, textY, label, selected ? 245 : 210); - const detailX = textX + labelWidth + 8; - image.drawText( - smallFont, - detailX, - textY, - truncateText(smallFont, entry.detail, rowX + rowWidth - 6 - detailX), - selected ? 170 : 130, - ); - } else if (entry.kind === "saved") { - const label = truncateText(smallFont, entry.destination.name, labelWidth); - image.drawText(smallFont, textX, textY, label, selected ? 245 : 210); - const detailX = textX + labelWidth + 8; - image.drawText( - smallFont, - detailX, - textY, - truncateText(smallFont, entry.destination.address, rowX + rowWidth - 6 - detailX), - selected ? 170 : 130, - ); - } else { - const label = truncateText(smallFont, entry.destination.name, labelWidth); - image.drawText(smallFont, textX, textY, label, selected ? 245 : 210); - const detailX = textX + labelWidth + 8; - const detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)"; - image.drawText( - smallFont, - detailX, - textY, - truncateText(smallFont, detail, rowX + rowWidth - 6 - detailX), - selected ? 170 : 130, - ); - } - } - if (entries.length > visibleRows) { - drawListScrollbar(image, rowX + rowWidth + 4, top, visibleRows * rowHeight - 2, idleScrollRow, visibleRows, entries.length); + const list = idleList(); + list.setItems(entries); + list.paint(image, { x: IDLE_LIST_X, y: top, width: rowWidth, height: visibleRows * rowHeight - IDLE_ROW_GAP }, win.focused); + list.drawScrollbar(image, IDLE_LIST_X + rowWidth + 4, top, visibleRows * rowHeight - 2); +} + +/** Two columns: the entry's name, then its detail (action hint, address or place). */ +function drawIdleRow({ image, item: entry, x, y, width, selected }: MenuDrawArgs): void { + const labelWidth = Math.floor(width * 0.4); + const textX = x + 6; + const textY = y + LIST_ROW_TEXT_INSET; + let label: string; + let detail: string; + if (entry.kind === "action") { + label = entry.label; + detail = entry.detail; + } else if (entry.kind === "saved") { + label = entry.destination.name; + detail = entry.destination.address; + } else { + label = entry.destination.name; + detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)"; } + image.drawText(smallFont, textX, textY, truncateText(smallFont, label, labelWidth), selected ? 245 : 210); + const detailX = textX + labelWidth + 8; + image.drawText(smallFont, detailX, textY, truncateText(smallFont, detail, x + width - 6 - detailX), selected ? 170 : 130); } function paintNavigating(image: GrayImage, win: NavWindow): void { diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 8a0878da..675a07f8 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -420,11 +420,11 @@ export function nightscoutMenuItems(): MenuItem[] { openSettingsSubMenu(ctx, "Nightscout settings", [ textSettingMenuItem(nightscoutSiteUrlSetting), textSettingMenuItem(nightscoutApiTokenSetting), + toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting), textSettingMenuItem(nightscoutMaxCannulaAgeSetting), textSettingMenuItem(nightscoutCartridgeLowSetting), textSettingMenuItem(nightscoutBatteryLowSetting), textSettingMenuItem(nightscoutMaxLoopAgeSetting), - toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting), ]); }, }, diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index 23abb009..80c4b1b4 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -21,6 +21,7 @@ * sidebar) pauses, as does backgrounding or screen-off. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -290,6 +291,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: PinballWindow = { windowId: message.windowId, diff --git a/app/apps/roam/roam-app.worker.ts b/app/apps/roam/roam-app.worker.ts index f35d09b3..5ec556eb 100644 --- a/app/apps/roam/roam-app.worker.ts +++ b/app/apps/roam/roam-app.worker.ts @@ -8,6 +8,7 @@ * menu, via the phone app's text editor. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -165,6 +166,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": window = { windowId: message.windowId, diff --git a/app/apps/teleprompter/teleprompter-app.ts b/app/apps/teleprompter/teleprompter-app.ts index bbd3be46..c615297b 100644 --- a/app/apps/teleprompter/teleprompter-app.ts +++ b/app/apps/teleprompter/teleprompter-app.ts @@ -52,10 +52,8 @@ export type TeleprompterAppOptions = InProcessAppOptions & { * over the home page. */ export function createTeleprompterAppWindow(options: TeleprompterAppOptions): InProcessWindow { - // The home menu's items are rebuilt in place whenever the recents change - // (MenuLayer reads the array each paint). - const homeItems: MenuItem[] = []; - const home = new MenuLayer("Teleprompter", homeItems, HOME_LAYOUT); + // The home menu's items are replaced (refreshHome) whenever the recents change. + const home = new MenuLayer("Teleprompter", [], HOME_LAYOUT); let created: InProcessWindow | null = null; let notice = ""; @@ -142,10 +140,8 @@ export function createTeleprompterAppWindow(options: TeleprompterAppOptions): In if (notice) { items.push({ label: notice, disabled: true, onSelect: () => {} }); } - homeItems.length = 0; - homeItems.push(...items); // Land on the most recent script (one click resumes it), else on Browse. - home.selectItem(getRecentScripts().length > 0 ? 1 : 0); + home.setItems(items, getRecentScripts().length > 0 ? 1 : 0); } refreshHome(); diff --git a/app/apps/terminal/background.ts b/app/apps/terminal/background.ts new file mode 100644 index 00000000..73e74aaa --- /dev/null +++ b/app/apps/terminal/background.ts @@ -0,0 +1,8 @@ +import { isTerminalWidgetConfigured } from "../glanceboard/configuration"; +import { loadConnections, parseConnectionString } from "./connections"; + +/** Keep session-list connections alive for a configured widget, even while it is hidden. */ +export function hasTerminalBackgroundWork(): boolean { + return isTerminalWidgetConfigured() && + loadConnections().some(connection => connection.enabled && parseConnectionString(connection.url) !== null); +} diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index cf914747..fd110ad5 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -31,6 +31,8 @@ * re-attach snapshot resyncs contents and scrollback only once it's needed. */ import "@nativescript/core/globals"; +import { hasTerminalBackgroundWork } from "./background"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage, type UiFont } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -52,7 +54,8 @@ import { clamp } from "../../util/numeric-util"; import { terminalAutoReconnectSetting, terminalLaunchPresetsSetting, terminalNewConnectionSetting, terminalWakeOnBellSetting } from "../../ui/dashboard-settings"; import { connectionDisplayName, loadConnections, parseConnectionString, saveConnections, TERMINAL_CONNECTIONS_KEY, updateConnection, type TerminalConnection } from "./connections"; import { TerminalEmulator } from "./terminal-emulator"; -import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; +import { type MenuItem } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { lineStep, listRowHeight } from "../../ui/metrics"; import { WindowMenu } from "../../ui/window-menu"; import { appViewportSize } from "../../ui/shell/geometry"; @@ -124,8 +127,8 @@ type HubMode = "sessions" | "connections" | "add"; type HubWindow = BaseWindow & { kind: "hub"; mode: HubMode; - selectedIndex: number; - scrollRow: number; + /** The section's item list; created on first paint or input (hubMenu). */ + list: Menu | null; /** * Session recency keys in display order, captured when the window last * became visible so the list doesn't reshuffle under the user while it's @@ -311,6 +314,14 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + reportIdle(); + break; + case "shutdown": + for (const control of controls.values()) stopControl(control); + controls.clear(); + finishWorkerShutdown(); + break; case "open-window": openWindow(message.windowId, message.surfaceId, message.title, message.viewport); break; @@ -424,12 +435,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { // toggles edited in the Settings app, the Add-connection draft typed on the // phone). onSettingsStoreChanged((key) => { + if (key.startsWith("glanceboard.")) { + reportIdle(); + return; + } if (!key.startsWith("terminal.")) return; switch (key) { case TERMINAL_CONNECTIONS_KEY: // Only once the app has actually been opened. if (controlsInitialized) syncControlsFromSettings(); renderHubWindows(); + reportIdle(); return; case NEW_CONNECTION_DRAFT_KEY: // Live keystrokes from the phone editor; repaint the add screen. @@ -438,7 +454,7 @@ onSettingsStoreChanged((key) => { case "terminal.autoReconnect": if (!terminalAutoReconnectSetting.get()) { cancelPendingReconnects(); - } else if (windows.size > 0) { + } else if (windows.size > 0 || hasTerminalBackgroundWork()) { for (const control of controls.values()) { if ((control.state?.phase ?? "idle") === "failed") scheduleControlReconnect(control); } @@ -450,6 +466,10 @@ onSettingsStoreChanged((key) => { } }); +function reportIdle(): void { + if (windows.size === 0 && !hasTerminalBackgroundWork()) post({ type: "worker-idle" }); +} + /** Cancel scheduled retries (auto-reconnect turned off); stale views stay marked. */ function cancelPendingReconnects(): void { for (const control of controls.values()) { @@ -488,8 +508,7 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport lastSubmittedFingerprint: "", menu: null, mode: "sessions", - selectedIndex: 0, - scrollRow: 0, + list: null, sessionOrder: [], addError: "", }); @@ -521,8 +540,8 @@ function closeWindow(windowId: string): void { windows.delete(windowId); windowIconActivity.delete(windowId); updateHubAnimation(); - // Auto-reconnect only runs while at least one terminal window is open. - if (windows.size === 0) { + // A configured widget owns the control connections after the last window closes. + if (windows.size === 0 && !hasTerminalBackgroundWork()) { for (const control of controls.values()) { cancelControlReconnect(control); } @@ -676,19 +695,18 @@ function noteSessionListRecency(control: ControlConnection, state: G2MirrorState /** * Retry a control connection after a backoff delay. The delay doubles per * scheduled attempt and resets when a connection reaches "connected" (or on - * a manual Connect). No-op when auto-reconnect is off, no terminal window is - * open, or the connection was disabled/removed meanwhile. + * a manual Connect). Windows and a configured widget can both own the client. */ function scheduleControlReconnect(control: ControlConnection): void { if (control.reconnectTimer) return; if (!terminalAutoReconnectSetting.get()) return; - if (windows.size === 0) return; + if (windows.size === 0 && !hasTerminalBackgroundWork()) return; if (!control.config.enabled) return; const delayMs = control.reconnectDelayMs; control.reconnectDelayMs = Math.min(control.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS); control.reconnectTimer = setTimeout(() => { control.reconnectTimer = null; - if (!terminalAutoReconnectSetting.get() || windows.size === 0) return; + if (!terminalAutoReconnectSetting.get() || (windows.size === 0 && !hasTerminalBackgroundWork())) return; if (controls.get(control.config.id) !== control || !control.config.enabled) return; if ((control.state?.phase ?? "idle") === "failed") startControl(control); }, delayMs); @@ -1171,11 +1189,48 @@ type HubItem = { onSelect?: () => void; }; +/** Vertical offset of a row's text line from the top of its selection box. */ +const HUB_ROW_TEXT_INSET = 4; +/** Horizontal inset of the list's selection boxes from the viewport edges. */ +const HUB_LIST_X = 20; + +/** The hub window's list, created on first use so openWindow stays free of paint dependencies. */ +function hubMenu(window: HubWindow): Menu { + window.list ??= new Menu({ + wrap: false, + rowGap: 1, + getHeight: () => listRowHeight(chromeFont()), + isSelectable: (item) => Boolean(item.onSelect), + onSelect: (item) => item.onSelect?.(), + draw: drawHubRow, + }); + return window.list; +} + +function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs): void { + const font = chromeFont(); + if (item.heading) { + image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, 140); + return; + } + // Activity gutter, open-window number column, then the label, truncated + // to the row (shared with the Glanceboard's Terminal widget). + drawSessionRow(image, font, { + x: x + 2, + y: y + HUB_ROW_TEXT_INSET, + width: width - 8, + label: item.label, + openGlyph: item.openGlyph ?? null, + active: Boolean(item.active), + phase: hubAnimationPhase, + value: selected ? 255 : 200, + }); +} + /** Switch the hub between its sections, resetting selection state. */ function setHubMode(window: HubWindow, mode: HubMode): void { window.mode = mode; - window.selectedIndex = 0; - window.scrollRow = 0; + window.list?.select(0); window.addError = ""; } @@ -1405,34 +1460,6 @@ function endAddConnection(window: HubWindow): void { post({ type: "end-text-setting-edit" }); } -/** Move the hub selection to the next selectable item in `direction`. */ -function moveHubSelection(window: HubWindow, items: HubItem[], direction: -1 | 1): void { - let index = window.selectedIndex + direction; - while (index >= 0 && index < items.length && !items[index]!.onSelect) { - index += direction; - } - if (index >= 0 && index < items.length) { - window.selectedIndex = index; - } -} - -/** Clamp the selection into range and off heading rows (prefer moving down). */ -function clampHubSelection(window: HubWindow, items: HubItem[]): void { - if (!items.length) { - window.selectedIndex = 0; - return; - } - let index = Math.max(0, Math.min(window.selectedIndex, items.length - 1)); - if (!items[index]!.onSelect) { - let forward = index; - while (forward < items.length && !items[forward]!.onSelect) forward++; - let backward = index; - while (backward >= 0 && !items[backward]!.onSelect) backward--; - index = forward < items.length ? forward : Math.max(0, backward); - } - window.selectedIndex = index; -} - function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): void { if (window.mode === "add") { if (event.type === "click") { @@ -1443,23 +1470,18 @@ function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): frameTimings.finishFrame(frameId, "discarded: terminal add-connection ignored input"); return; } - const items = hubItems(window); - clampHubSelection(window, items); + const list = hubMenu(window); + list.setItems(hubItems(window)); switch (event.type) { case "scroll-up": - moveHubSelection(window, items, -1); - renderAndSubmit(window, frameId); - return; case "scroll-down": - moveHubSelection(window, items, 1); + case "click": + // The menu's input handler is async only to await onSelect; hub item + // callbacks are synchronous, so the selection move or action has + // already applied when it returns and the frame can paint right away. + list.handleInput(event).catch((error) => console.error("terminal hub action failed", error)); renderAndSubmit(window, frameId); return; - case "click": { - const item = items[window.selectedIndex]; - item?.onSelect?.(); - renderAndSubmit(window, frameId); - return; - } default: frameTimings.finishFrame(frameId, "discarded: terminal hub ignored input"); return; @@ -1583,49 +1605,18 @@ function paintHub(window: HubWindow): GrayImage { listTop += 2 * step + 6; } - const items = hubItems(window); - clampHubSelection(window, items); - const hubRowH = listRowHeight(chromeFont()); - const visibleRowCount = Math.max(1, ((window.viewportHeight - 6 - listTop) / hubRowH) | 0); - window.scrollRow = scrollToKeepSelectionVisible(window.scrollRow, window.selectedIndex, visibleRowCount, items.length); - const lastVisibleRow = Math.min(items.length, window.scrollRow + visibleRowCount); - for (let index = window.scrollRow; index < lastVisibleRow; index++) { - const y = listTop + (index - window.scrollRow) * hubRowH; - const item = items[index]!; - if (item.heading) { - image.drawText(chromeFont(), 20, y + 2, item.label, 140); - continue; - } - const selected = index === window.selectedIndex; - if (selected) { - // Match the shell convention: fill only when this window has focus, so - // an outline-only selection signals the sidebar owns input. - drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8); - } - // Activity gutter, open-window number column, then the label, truncated - // to the row (shared with the Glanceboard's Terminal widget). - drawSessionRow(image, chromeFont(), { - x: 22, - y: y + 2, - width: window.viewportWidth - 22 - 26, - label: item.label, - openGlyph: item.openGlyph ?? null, - active: Boolean(item.active), - phase: hubAnimationPhase, - value: selected ? 255 : 200, - }); - } - if (items.length > visibleRowCount) { - drawListScrollbar( - image, - window.viewportWidth - 10, - listTop, - visibleRowCount * hubRowH - 4, - window.scrollRow, - visibleRowCount, - items.length, - ); - } + const list = hubMenu(window); + list.setItems(hubItems(window)); + // Selection boxes start above the text line; the menu fills the selected + // box only while this window has focus (an outline alone means the sidebar + // owns input), matching the shell convention. + const listHeight = window.viewportHeight - 6 - listTop; + list.paint( + image, + { x: HUB_LIST_X, y: listTop - 2, width: window.viewportWidth - 2 * HUB_LIST_X, height: listHeight }, + window.focused, + ); + list.drawScrollbar(image, window.viewportWidth - 10, listTop, listHeight - 4); return image; } diff --git a/app/apps/timer/timer-app.ts b/app/apps/timer/timer-app.ts index 0555aecf..af61c195 100644 --- a/app/apps/timer/timer-app.ts +++ b/app/apps/timer/timer-app.ts @@ -31,14 +31,12 @@ import { } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; import { - drawListScrollbar, drawRightValueMenuItem, - drawSelectionHighlight, drawToggleMenuItem, - scrollToKeepSelectionVisible, TextPageLayer, type MenuItem, } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; import { shell } from "../../ui/shell/shell"; @@ -91,6 +89,8 @@ const LIST_SHARE = 0.4; const LIST_MIN_WIDTH = 190; const LIST_MAX_WIDTH = 240; const LIST_TOP = 6; +/** Pixels between consecutive list rows' selection boxes. */ +const LIST_ROW_GAP = 1; const STAGE_X = 10; /** Gap between the stage's right edge and the list's divider. */ const STAGE_LIST_GAP = 14; @@ -183,8 +183,22 @@ function fittingDigitFont(text: string, maxWidth: number, sizes: readonly number export class TimersLayer implements Layer { readonly acceptsDirectional = true; + /** + * The selected row's key: the source of truth for the selection, which + * survives row rebuilds and is pinned to newly ringing items. The list + * menu is re-synced from it before each paint, hit test and move. + */ private selectionKey = ""; - private scrollRow = 0; + private readonly list = new Menu({ + wrap: true, + rowGap: LIST_ROW_GAP, + highlight: { radius: 6 }, + getHeight: () => listRowHeight(smallFont()), + isSelectable: (row) => row.kind !== "header", + draw: (args) => this.drawListRow(args), + }); + /** Per-paint inputs to drawListRow. */ + private listPaint = { dim: 1, now: 0 }; private mode: Mode = { kind: "list" }; /** Which of the ringing stage's two buttons is highlighted. */ private ringButton = 0; @@ -232,7 +246,8 @@ export class TimersLayer implements Layer { let index = rows.findIndex((row) => row.key === this.selectionKey); if (index < 0) { // The row went away (a dismissed timer, a deleted alarm): stay in place. - index = clamp(this.scrollRow, 0, rows.length - 1); + const firstVisibleRow = Math.floor(this.list.scrollTop / listRowHeight(smallFont())); + index = clamp(firstVisibleRow, 0, rows.length - 1); while (index < rows.length && rows[index]!.kind === "header") index++; if (index >= rows.length) index = rows.findIndex((row) => row.kind !== "header"); this.selectionKey = rows[index]!.key; @@ -244,14 +259,16 @@ export class TimersLayer implements Layer { return rows[this.reconcileSelection(rows)]!; } - private moveSelection(rows: Row[], delta: number): void { - const count = rows.length; - let index = this.reconcileSelection(rows); - for (let step = 0; step < count; step++) { - index = (index + delta + count) % count; - if (rows[index]!.kind !== "header") break; - } - this.selectionKey = rows[index]!.key; + /** Point the list menu at these rows and the current selection. */ + private syncList(rows: Row[]): void { + this.list.setItems(rows, this.reconcileSelection(rows)); + } + + private moveSelection(rows: Row[], delta: -1 | 1): void { + this.syncList(rows); + this.list.moveSelection(delta); + const index = this.list.selectedIndex; + if (index !== null) this.selectionKey = rows[index]!.key; } /** Called on every engine change: jump to a newly ringing item. */ @@ -547,15 +564,14 @@ export class TimersLayer implements Layer { /** A mirror touch: on the list, select the row under it. */ hitTest(x: number, y: number, ctx: LayerContext): boolean { if (this.mode.kind !== "list") return false; - const { width, height } = ctx.stack.getBaseSize(); + const { width } = ctx.stack.getBaseSize(); const listX = width - listWidthFor(width); if (x < listX) return false; const rows = this.buildRows(Date.now()); - this.reconcileSelection(rows); - const rowHeight = listRowHeight(smallFont()); - const index = this.scrollRow + Math.floor((y - LIST_TOP) / rowHeight); - const row = rows[index]; - if (!row || row.kind === "header" || y < LIST_TOP || y > height) return true; + this.syncList(rows); + const index = this.list.indexAt(y); + if (index === null) return true; + const row = rows[index]!; if (this.selectionKey === row.key) { this.activateRow(row); } else { @@ -882,79 +898,78 @@ export class TimersLayer implements Layer { dim: number, now: number, ): void { - const font = smallFont(); - const rowHeight = listRowHeight(font); + const rowHeight = listRowHeight(smallFont()); const visible = Math.max(1, Math.floor((height - 2 * LIST_TOP) / rowHeight)); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, selectedIndex, visible, rows.length); + this.listPaint = { dim, now }; + // While a picker owns the stage the list has no live selection, so no + // highlight; drawListRow still brightens the selected row by key. + this.list.setItems(rows, dim === 1 ? selectedIndex : null); + this.list.paint(image, { x: listX, y: LIST_TOP, width: listWidth - 12, height: visible * rowHeight - LIST_ROW_GAP }, focused); + this.list.drawScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4); + } + + private drawListRow({ image, item: row, x, y, width, height }: MenuDrawArgs): void { + const font = smallFont(); + const { dim, now } = this.listPaint; + const rowHeight = height + LIST_ROW_GAP; const shade = (value: number) => Math.round(value * dim); - const textX = listX + 8; - const textWidth = listWidth - 24; - const last = Math.min(rows.length, this.scrollRow + visible); - for (let index = this.scrollRow; index < last; index++) { - const row = rows[index]!; - const y = LIST_TOP + (index - this.scrollRow) * rowHeight; - const textY = y + LIST_ROW_TEXT_INSET; - if (row.kind === "header") { - image.drawText(font, textX, textY, row.title!, shade(90)); - continue; - } - const selected = index === selectedIndex; - if (selected && dim === 1) { - drawSelectionHighlight(image, listX, y, listWidth - 12, rowHeight - 1, focused, 6); - } - const bright = shade(selected ? 250 : 210); - const soft = shade(selected ? 170 : 130); - switch (row.kind) { - case "timer": { - const timer = row.timer!; - const phase = timerPhase(timer); - const digits = phase === "rung" ? "0:00" : formatCountdown(timerRemainingMs(timer, now)); - const digitsValue = phase === "rung" ? (Math.floor(now / 1000) % 2 ? bright : soft) : phase === "paused" ? soft : bright; - image.drawText(font, textX, textY, digits, digitsValue); - const digitsWidth = font.measureText(digits) + 8; - const nameWidth = textWidth - digitsWidth - (phase === "paused" ? 12 : 0); - if (nameWidth > 12) { - image.drawText(font, textX + digitsWidth, textY, truncateText(font, timerDisplayName(timer), nameWidth), soft); - } - if (phase === "paused") drawPauseMark(image, listX + listWidth - 22, y + Math.floor(rowHeight / 2) - 5, soft); - break; - } - case "new-timer": - image.drawText(font, textX, textY, "+ New timer", shade(selected ? 220 : 150)); - break; - case "stopwatch": { - const stopwatch = timerEngine.state.stopwatch; - const running = stopwatchIsRunning(stopwatch); - const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, now), false); - image.drawText(font, textX, textY, "Stopwatch", running ? bright : soft); - image.drawText(font, listX + listWidth - 16 - font.measureText(elapsed), textY, elapsed, running ? bright : soft); - break; - } - case "alarm": { - const alarm = row.alarm!; - const on = alarm.enabled; - const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour()); - image.drawText(font, textX, textY, time, on ? bright : soft); - const timeWidth = font.measureText(time) + 8; - const daysWidth = textWidth - timeWidth - 14; - if (daysWidth > 12) { - image.drawText(font, textX + timeWidth, textY, truncateText(font, alarm.label || formatDays(alarm.days), daysWidth), on ? soft : shade(80)); - } - const dotX = listX + listWidth - 22; - const dotY = y + Math.floor(rowHeight / 2) - 4; - if (alarm.ringingSinceMs !== null || on) image.fillRoundedRect(dotX, dotY, 8, 8, on ? shade(220) : shade(90), 4); - else image.drawRoundedRect(dotX, dotY, 8, 8, shade(90), 4); - break; - } - case "new-alarm": - image.drawText(font, textX, textY, "+ New alarm", shade(selected ? 220 : 150)); - break; - default: - break; - } + const textX = x + 8; + const textWidth = width - 12; + const textY = y + LIST_ROW_TEXT_INSET; + if (row.kind === "header") { + image.drawText(font, textX, textY, row.title!, shade(90)); + return; } - if (rows.length > visible) { - drawListScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4, this.scrollRow, visible, rows.length); + const selected = row.key === this.selectionKey; + const bright = shade(selected ? 250 : 210); + const soft = shade(selected ? 170 : 130); + switch (row.kind) { + case "timer": { + const timer = row.timer!; + const phase = timerPhase(timer); + const digits = phase === "rung" ? "0:00" : formatCountdown(timerRemainingMs(timer, now)); + const digitsValue = phase === "rung" ? (Math.floor(now / 1000) % 2 ? bright : soft) : phase === "paused" ? soft : bright; + image.drawText(font, textX, textY, digits, digitsValue); + const digitsWidth = font.measureText(digits) + 8; + const nameWidth = textWidth - digitsWidth - (phase === "paused" ? 12 : 0); + if (nameWidth > 12) { + image.drawText(font, textX + digitsWidth, textY, truncateText(font, timerDisplayName(timer), nameWidth), soft); + } + if (phase === "paused") drawPauseMark(image, x + width - 10, y + Math.floor(rowHeight / 2) - 5, soft); + break; + } + case "new-timer": + image.drawText(font, textX, textY, "+ New timer", shade(selected ? 220 : 150)); + break; + case "stopwatch": { + const stopwatch = timerEngine.state.stopwatch; + const running = stopwatchIsRunning(stopwatch); + const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, now), false); + image.drawText(font, textX, textY, "Stopwatch", running ? bright : soft); + image.drawText(font, x + width - 4 - font.measureText(elapsed), textY, elapsed, running ? bright : soft); + break; + } + case "alarm": { + const alarm = row.alarm!; + const on = alarm.enabled; + const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour()); + image.drawText(font, textX, textY, time, on ? bright : soft); + const timeWidth = font.measureText(time) + 8; + const daysWidth = textWidth - timeWidth - 14; + if (daysWidth > 12) { + image.drawText(font, textX + timeWidth, textY, truncateText(font, alarm.label || formatDays(alarm.days), daysWidth), on ? soft : shade(80)); + } + const dotX = x + width - 10; + const dotY = y + Math.floor(rowHeight / 2) - 4; + if (alarm.ringingSinceMs !== null || on) image.fillRoundedRect(dotX, dotY, 8, 8, on ? shade(220) : shade(90), 4); + else image.drawRoundedRect(dotX, dotY, 8, 8, shade(90), 4); + break; + } + case "new-alarm": + image.drawText(font, textX, textY, "+ New alarm", shade(selected ? 220 : 150)); + break; + default: + break; } } diff --git a/app/apps/timer/timer-engine.ts b/app/apps/timer/timer-engine.ts index 57bb5e88..34dbf3d7 100644 --- a/app/apps/timer/timer-engine.ts +++ b/app/apps/timer/timer-engine.ts @@ -8,7 +8,7 @@ * * Two expiry paths run side by side: a JS deadline for prompt on-glasses * ringing while the process is awake, and an AlarmManager alarm-clock alarm - * (FaceclawAlarms.java) that rings on the phone at the right moment even if + * (FaceclawAlarms.kt) that rings on the phone at the right moment even if * the process is asleep or dead. The phone shows the item silently first * and adds its own sound when the glasses cannot carry it (not connected, * not worn, charging, not shown within seconds, or not acknowledged within diff --git a/app/apps/weather/weather.ts b/app/apps/weather/weather.ts index 5b16fbf8..d32d2e3f 100644 --- a/app/apps/weather/weather.ts +++ b/app/apps/weather/weather.ts @@ -1,12 +1,11 @@ import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; -import { clamp } from "../../util/numeric-util"; import { type ForecastPeriod, type WeatherState } from "../../native/weather"; import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; -import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu"; -import { lineStep, tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; const PAGE_X = 18; const HEADER_Y = 8; @@ -18,11 +17,26 @@ const FORECAST_TOP = 124; const FORECAST_MIN_ROW_HEIGHT = 23; /** Gap between forecast columns. */ const FORECAST_COL_GAP = 14; +/** Left edge of the forecast rows' selection boxes. */ +const FORECAST_BOX_X = PAGE_X - 7; +/** Pixels between consecutive forecast rows' selection boxes. */ +const FORECAST_ROW_GAP = 2; + +/** Forecast column x offsets from the row box's left edge. */ +type ForecastColumns = { name: number; temp: number; precip: number; summary: number }; /** Weather's current-conditions summary and scrollable 12-hour forecast. */ export class WeatherLayer implements Layer { - private selectedIndex = 0; - private scrollRow = 0; + /** Forecast table rows: selection only, a click does nothing there. */ + private readonly forecastMenu = new Menu({ + wrap: false, + rowGap: FORECAST_ROW_GAP, + highlight: { radius: 5 }, + getHeight: () => forecastRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawForecastRow(args), + }); + /** Column layout for drawForecastRow, set by each drawForecast. */ + private columns: ForecastColumns = { name: 0, temp: 0, precip: 0, summary: 0 }; constructor( private readonly state: () => WeatherState, @@ -81,10 +95,9 @@ export class WeatherLayer implements Layer { return; } if (!weather.forecast.length || weather.phase !== "ready") return; - if (event.type === "scroll-up") { - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - } else if (event.type === "scroll-down") { - this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1); + if (event.type === "scroll-up" || event.type === "scroll-down") { + this.forecastMenu.setItems(weather.forecast); + void this.forecastMenu.handleInput(event); } } @@ -134,12 +147,14 @@ export class WeatherLayer implements Layer { image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160); return; } - this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1); - const rowH = Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3); + const rowH = forecastRowHeight(font); // The last row only needs its text line (not a full row pitch of // clearance below), which usually fits one more row before the bottom. + // The menu only draws rows whose whole selection box fits, so the box + // holds that many rows, cut at the image's bottom edge. const visibleRows = Math.max(1, 1 + Math.floor((height - FORECAST_TOP - (font.lineHeight + 4)) / rowH)); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length); + const boxTop = FORECAST_TOP - 2; + const boxHeight = Math.min(visibleRows * rowH - FORECAST_ROW_GAP, height - boxTop); // Columns are sized to the widest period name so names ("Wednesday // Night") never truncate; the summary column absorbs what's left. @@ -155,20 +170,31 @@ export class WeatherLayer implements Layer { image.drawText(font, tempX, FORECAST_HEADER_Y, "Temp", 105); image.drawText(font, precipX, FORECAST_HEADER_Y, "Rain", 105); - const last = Math.min(forecast.length, this.scrollRow + visibleRows); - for (let index = this.scrollRow; index < last; index++) { - const period = forecast[index]!; - const y = FORECAST_TOP + (index - this.scrollRow) * rowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), rowH - 2, ctx.stack.isFocused(), 5); - } - const shade = selected ? 245 : 190; - image.drawText(font, PAGE_X, y + 2, period.name, shade); - image.drawText(font, tempX, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade); - image.drawText(font, precipX, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155); - image.drawText(font, summaryX, y + 2, truncateText(font, period.shortForecast, width - summaryX - PAGE_X), selected ? 220 : 165); - } + this.columns = { + name: PAGE_X - FORECAST_BOX_X, + temp: tempX - FORECAST_BOX_X, + precip: precipX - FORECAST_BOX_X, + summary: summaryX - FORECAST_BOX_X, + }; + this.forecastMenu.setItems(forecast); + this.forecastMenu.paint( + image, + { x: FORECAST_BOX_X, y: boxTop, width: width - 2 * FORECAST_BOX_X, height: boxHeight }, + ctx.stack.isFocused(), + ); + } + + private drawForecastRow({ image, item: period, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const columns = this.columns; + const shade = selected ? 245 : 190; + const textY = centeredTextY(font, y, height); + image.drawText(font, x + columns.name, textY, period.name, shade); + image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade); + image.drawText(font, x + columns.precip, textY, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155); + // The summary stops PAGE_X short of the viewport's right edge, as the name starts PAGE_X in from the left. + const summaryWidth = width - columns.summary - columns.name; + image.drawText(font, x + columns.summary, textY, truncateText(font, period.shortForecast, summaryWidth), selected ? 220 : 165); } private drawMessage( @@ -190,6 +216,11 @@ export class WeatherLayer implements Layer { } } +/** Forecast row pitch, including the gap between selection boxes. */ +function forecastRowHeight(font: UiFont): number { + return Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3); +} + function formatAge(ageMs: number): string { const minutes = Math.max(0, Math.round(ageMs / 60_000)); if (minutes < 1) return "just now"; diff --git a/app/g2/ble-image-optimizer.ios.ts b/app/g2/ble-image-optimizer.ios.ts deleted file mode 100644 index 989ca1c2..00000000 --- a/app/g2/ble-image-optimizer.ios.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { fromData, toData, nativeArray } from '../native/kotlin-data' -declare const FaceclawKitIosProtocol: any -const protocol = FaceclawKitIosProtocol.new() -export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null { - if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 || - width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 || - !previous || previous.length !== width * height / 2 || next.length !== previous.length) return null - const result = protocol.boundingBoxPreviousNextWidthHeightFrameId(toData(previous), toData(next), width, height, frameId) - return result ? fromData(result) : null -} -export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] { - if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2) - throw new Error('Invalid framebuffer dimensions') - return nativeArray(protocol.fullFrameBandsDataWidthHeightFirstId(toData(packed), width, height, firstId), fromData) -} diff --git a/app/g2/ble-image-optimizer.ts b/app/g2/ble-image-optimizer.ts deleted file mode 100644 index b874b53a..00000000 --- a/app/g2/ble-image-optimizer.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { concat, rle4 } from './ble-protocol' - -/** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in - * units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first. - * null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport. - */ -export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, - width: number, height: number, frameId: number): Uint8Array | null { - if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 || - width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 || - !previous || previous.length !== width * height / 2 || next.length !== previous.length) return null - const stride = width / 2 - let minX = stride, maxX = -1, minY = height, maxY = -1 - for (let y = 0; y < height; y++) { - const row = y * stride - for (let x = 0; x < stride; x++) { - if (previous[row + x] === next[row + x]) continue - minX = Math.min(minX, x); maxX = Math.max(maxX, x) - minY = Math.min(minY, y); maxY = y - } - } - if (maxY < 0) return null - const left = (minX * 2) & ~3, right = Math.min(width, ((maxX + 1) * 2 + 3) & ~3) - const top = minY & ~1, bottom = Math.min(height, (maxY + 2) & ~1) - const boxWidth = right - left, boxHeight = bottom - top - if (boxWidth === width && boxHeight === height) return null - const regionStride = boxWidth / 2, region = new Uint8Array(regionStride * boxHeight) - for (let y = 0; y < boxHeight; y++) { - const start = (top + y) * stride + left / 2 - region.set(next.subarray(start, start + regionStride), y * regionStride) - } - return concat(new Uint8Array([3, left / 4, top / 2, boxWidth / 4, boxHeight / 2, - frameId & 255, (frameId >>> 8) & 255]), rle4(region)) -} - -/** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */ -export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] { - if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2) - throw new Error('Invalid framebuffer dimensions') - const bands: Uint8Array[] = [] - let id = firstId - for (let top = 0; top < height; top += 64) { - const rows = Math.min(64, height - top) - bands.push(concat(new Uint8Array([3, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]), - rle4(packed.subarray(top * width / 2, (top + rows) * width / 2)))) - id = id >= 0xfffe ? 1 : id + 1 - } - return bands -} diff --git a/app/g2/ble-protocol.ts b/app/g2/ble-protocol.ts deleted file mode 100644 index 42c80796..00000000 --- a/app/g2/ble-protocol.ts +++ /dev/null @@ -1,203 +0,0 @@ -import { type RingInput, decodeRingMetadata } from "./ring-input"; -import { type CompassEvent } from '../native/compass-types' -/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */ -import { OsEventTypeList } from './events' -export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401' -export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402' -export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402' -export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f'] -export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const -export function concat(...parts: Uint8Array[]): Uint8Array { - const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)) - let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output -} -function varint(value: number): Uint8Array { - const bytes: number[] = [] - do { const next = value & 127; value >>>= 7; bytes.push(next | (value ? 128 : 0)) } while (value) - return new Uint8Array(bytes) -} -export const integer = (field: number, value: number): Uint8Array => concat(varint(field * 8), varint(value)) -export const bytes = (field: number, value: Uint8Array): Uint8Array => concat(varint(field * 8 + 2), varint(value.length), value) -// Outbound names/text here are fixed ASCII protocol identifiers. -const string = (field: number, value: string): Uint8Array => bytes(field, new Uint8Array(Array.from(value, c => c.charCodeAt(0)))) -const wrap = (command: number, magic: number, field: number, body: Uint8Array) => concat(integer(1, command), integer(2, magic), bytes(field, body)) -export const authentication = (magic: number) => wrap(4, magic, 3, concat(integer(1, 1), integer(2, 4))) -export const prelude = () => wrap(2, 156, 4, bytes(3, bytes(2, bytes(2, concat(integer(1, 0), integer(2, 0)))))) -export const heartbeat = (magic: number) => wrap(12, magic, 14, integer(1, 0)) -export const audioControl = (magic: number, enabled: boolean) => wrap(15, magic, 18, integer(1, enabled ? 1 : 0)) -export const settingsQuery = (magic: number) => wrap(2, magic, 4, integer(1, 1)) -export const shutdown = (magic: number) => wrap(9, magic, 11, integer(1, 1)) -export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, acquire ? 5 : 6, 0, 0]))) -export function createLayout(magic: number): Uint8Array { - const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288)) - const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' ')) - return wrap(0, magic, 3, concat(integer(1, 1), bytes(3, text), integer(5, 10000))) -} -export function crc16(data: Uint8Array): number { - let crc = 0xffff - for (const byte of data) { - crc ^= byte << 8 - for (let i = 0; i < 8; i++) crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0)) & 0xffff - } - return crc -} -export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] { - const chunkSize = Math.min(232, Math.floor(maxWrite) - 8) - if (chunkSize < 12) throw new Error('Negotiated BLE write size is too small') - const crc = crc16(payload), withCrc = concat(payload, new Uint8Array([crc & 255, crc >>> 8])) - const count = Math.ceil(withCrc.length / chunkSize) - if (count > 255) throw new Error('Protocol message exceeds the fragment limit') - return Array.from({ length: count }, (_, i) => { - const chunk = withCrc.subarray(i * chunkSize, (i + 1) * chunkSize) - return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk) - }) -} -export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array } -/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */ -export class MessageReceiver { - private streams = new Map() - private messages = new Map() - clear(): void { this.streams.clear(); this.messages.clear() } - receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] { - for (const [key, message] of this.messages) if (now - message.seen > 5000) this.messages.delete(key) - let stream = concat(this.streams.get(link) ?? new Uint8Array(), data) - const output: ProtocolMessage[] = [] - while (stream.length >= 8) { - if (stream[0] !== 0xaa || ![0x21, 0x12].includes(stream[1])) { stream = stream.subarray(1); continue } - const length = stream[3], count = stream[4], index = stream[5] - if (stream.length < length + 8) break - const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}` - const packet = stream.slice(0, length + 8) - const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8) - if (sid === SID.cfw) { - output.push({ sid, flag, payload: chunk, command: -1, magic: -1, packet }) - continue - } - if (!count || !index || index > count) { this.messages.delete(key); continue } - if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 }) - const message = this.messages.get(key) - if (!message || message.total !== count || message.chunks.length + 1 !== index) { this.messages.delete(key); continue } - message.chunks.push(chunk); message.size += chunk.length; message.seen = now - if (message.size > 65536) { this.messages.delete(key); continue } - if (index !== count) continue - this.messages.delete(key) - const complete = concat(...message.chunks) - if (complete.length < 2) continue - const payload = complete.subarray(0, -2), crc = complete[complete.length - 2] | (complete[complete.length - 1] << 8) - if (crc16(payload) !== crc) continue - try { output.push({ sid, flag, payload, command: readInteger(payload, 1, -1), magic: readInteger(payload, 2, -1) }) } - catch { /* Malformed notifications cannot satisfy an acknowledgement. */ } - } - this.streams.set(link, stream.slice()) - return output - } -} -function readVarint(data: Uint8Array, offset: number): [number, number] { - let value = 0 - for (let i = 0; i < 5 && offset < data.length; i++) { - const byte = data[offset++]; value += (byte & 127) * 2 ** (i * 7) - if (!(byte & 128)) return [value, offset] - } - throw new Error('Truncated or oversized protobuf varint') -} -function field(data: Uint8Array, number: number): number | Uint8Array | undefined { - let offset = 0 - while (offset < data.length) { - let key: number; [key, offset] = readVarint(data, offset) - const tag = key >>> 3, wire = key & 7 - if (!tag) throw new Error('Invalid protobuf field') - if (wire === 0) { - let value: number; [value, offset] = readVarint(data, offset) - if (tag === number) return value - } else { - let length: number - if (wire === 2) [length, offset] = readVarint(data, offset) - else if (wire === 1) length = 8 - else if (wire === 5) length = 4 - else throw new Error('Unsupported protobuf wire type') - if (offset + length > data.length) throw new Error('Truncated protobuf field') - if (tag === number && wire === 2) return data.subarray(offset, offset + length) - offset += length - } - } - return undefined -} -export function readInteger(data: Uint8Array, number: number, fallback = 0): number { const value = field(data, number); return typeof value === 'number' ? value : fallback } -export function readBytes(data: Uint8Array, number: number): Uint8Array | undefined { const value = field(data, number); return value instanceof Uint8Array ? value : undefined } -export function readString(data: Uint8Array, number: number): string { return Array.from(readBytes(data, number) ?? [], value => String.fromCharCode(value)).join('') } -export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean { - return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0 -} -export type GlassesInput = { ringInput?: RingInput; kind: 'sys-event' | 'list-click' | 'text-click' | 'display-wake' | 'even-ai'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number } -export function decodeGlassesInput(message: ProtocolMessage): GlassesInput | null { - if (message.sid !== SID.hub || ![1, 6].includes(message.flag)) return null - const events = readBytes(message.payload, 13); if (!events) return null - for (const [field, kind, typeField] of [[1, 'list-click', 5], [2, 'text-click', 3], [3, 'sys-event', 1]] as const) { - const event = readBytes(events, field) - if (event) return { kind: kind === 'sys-event' && readInteger(event, typeField) === OsEventTypeList.HEAD_UP_EVENT ? 'display-wake' : kind, - eventType: readInteger(event, typeField), eventSource: field === 3 ? readInteger(event, 2) : 0, - ringInput: field === 3 && readInteger(event, 2) === 2 ? decodeRingMetadata(readBytes(event, 100)) : undefined, - systemExitReasonCode: field === 3 ? readInteger(event, 4) : 0, containerName: field === 3 ? '' : readString(event, 2), frameId: 0 } - } - return null -} -export function decodeRingInput(data: Uint8Array): GlassesInput | null { - let eventType: number | undefined - if (data.length === 11 && data[0] === 0 && data[1] === 9 && data[2] === 0x61 && data[3] === 0) - eventType = ({ 0: 9, 1: 0, 2: 3, 4: 1, 5: 2, 8: 10 } as Record)[data[4]] - if (data.length === 3 && data[0] === 255) { - if (data[1] === 3 && data[2] === 32) eventType = 9 - if (data[1] === 4 && data[2] === 1) eventType = 0 - if (data[1] === 4 && data[2] === 2) eventType = 3 - if (data[1] === 5) eventType = data[2] <= 1 ? 2 : 1 - } - return eventType === undefined ? null : { kind: 'sys-event', eventType, eventSource: 2, systemExitReasonCode: 0, containerName: '', frameId: 0 } -} -export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array { - if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame') - const stride = Math.ceil(width / 2), packed = new Uint8Array(stride * height) - for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) { - const nibble = Math.min(15, (gray[y * width + x] + 8) >> 4) - packed[y * stride + (x >> 1)] |= nibble << (x % 2 ? 0 : 4) - } - return packed -} -export function rle4(packed: Uint8Array): Uint8Array { - const output = new Uint8Array(packed.length * 2) - const nibble = (i: number) => (packed[i >> 1] >> (i % 2 ? 0 : 4)) & 15 - let used = 0, i = 0 - while (i < packed.length * 2) { - const color = nibble(i); let end = i + 1 - while (end < packed.length * 2 && nibble(end) === color) end++ - let remaining = end - i - while (remaining) { - const count = Math.min(65535, remaining) - if (count <= 15) output[used++] = (count << 4) | color - else { output[used++] = color; if (count <= 255) output[used++] = count - else { output[used++] = 0; output[used++] = count & 255; output[used++] = count >>> 8 } } - remaining -= count - } - i = end - } - return output.slice(0, used) -} - -/** Reference decoder for host tests; iOS uses the shared Kotlin implementation. */ -export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null { - if (message.sid !== 8 || ![1, 6].includes(message.flag)) return null - const command = readInteger(message.payload, 1, -1) - if (command === 16 || command === 17) return { command, headingDegrees: -1 } - if (command !== 15) return null - const compass = readBytes(message.payload, 10) - const headingDegrees = compass ? readInteger(compass, 1, -1) : -1 - if (headingDegrees < 0 || headingDegrees >= 360) return null - const event: CompassEvent = { command, headingDegrees } - let d: Uint8Array | undefined - try { d = readBytes(message.payload, 100) } catch { return event } - if (d?.length === 12 && d[0] === 67 && d[1] === 77 && d[2] === 1 && d[7] === 0 && - (d[3] <= 3 || d[3] === 255) && (d[4] <= 2 || d[4] === 255) && d[5] <= 3) { - event.diagnostics = { magneticAccuracy: d[3] === 255 ? -1 : d[3], magneticAnomalies: d[4] === 255 ? -1 : d[4], - orientationSource: d[5], flags: d[6], sampleTimeMs: new DataView(d.buffer, d.byteOffset + 8, 4).getUint32(0, true) } - } - return event -} diff --git a/app/g2/ble-traffic-counters.ts b/app/g2/ble-traffic-counters.ts deleted file mode 100644 index d37023aa..00000000 --- a/app/g2/ble-traffic-counters.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** Same units as Android FaceclawBleManager: protocol messages, framed write - * bytes accepted by the transport, and fully acknowledged display updates. - * Totals survive reconnects and the setting being switched off. - */ -export class BleTrafficCounters { - private messages = 0 - private bytes = 0 - private frames = 0 - recordWrite(bytes: number): void { this.bytes += bytes } - recordMessage(): void { this.messages++ } - recordDisplayFrame(): void { this.frames++ } - sample(): { messages: number; bytes: number; frames: number } { - return { messages: this.messages, bytes: this.bytes, frames: this.frames } - } -} -// The iOS BLE session and phone UI both run in the main isolate. -export const iosBleTraffic = new BleTrafficCounters() diff --git a/app/g2/brightness-curve.ts b/app/g2/brightness-curve.ts new file mode 100644 index 00000000..1695ae8d --- /dev/null +++ b/app/g2/brightness-curve.ts @@ -0,0 +1,26 @@ +export const DEFAULT_BRIGHTNESS_CURVE = "0:0,0.2:7,1.5:33,5:100"; +const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100"; + +/** Percent of the configured range; defaults (20–100) give 20/26/46/100. */ +export function brightnessCurveError(value: string): string | null { + const pairs = value.split(",").map(part => part.trim().split(":")); + if (pairs.length < 2 || pairs.length > 16 || pairs.some(p => p.length !== 2 || p.some(s => !s.trim()))) + return "Use 2–16 lux:percent pairs separated by commas."; + const points = pairs.map(pair => pair.map(Number)); + if (points.some(([lux, percent]) => !Number.isFinite(lux) || !Number.isFinite(percent) + || lux < 0 || lux > 1_000_000 || percent < 0 || percent > 100)) + return "Lux must be 0–1000000 and percent must be 0–100."; + if (points[0][0] !== 0 || points[0][1] !== 0 || points[points.length - 1][1] !== 100) + return "Start at 0:0 and end at 100%. Set dark-room brightness with Auto minimum."; + if (points.some(([lux, percent], i) => i > 0 && (lux <= points[i-1][0] || percent < points[i-1][1]))) + return "Lux must increase and brightness must not decrease."; + return null; +} + +/** Keep incomplete drafts intact. Validation must never replace typed input. */ +export function normalizeBrightnessCurve(value: string | null | undefined): string { + if (value == null) return DEFAULT_BRIGHTNESS_CURVE; + if (brightnessCurveError(value)) return value; + const canonical = value.split(",").map(pair => pair.split(":").map(Number).join(":")).join(","); + return canonical === PREVIOUS_DEFAULT_CURVE ? DEFAULT_BRIGHTNESS_CURVE : canonical; +} diff --git a/app/g2/brightness-protocol.ts b/app/g2/brightness-protocol.ts deleted file mode 100644 index 79b64d57..00000000 --- a/app/g2/brightness-protocol.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { bytes, concat, integer } from './ble-protocol' - -/** Same G2SettingPackage as Kotlin BleProtocol.buildSetBrightness. */ -export function setBrightness(magic: number, level: number | null): Uint8Array { - const fields = level === null ? integer(1, 1) - : concat(integer(1, 0), integer(2, Math.max(0, Math.min(100, Math.round(level))))) - return concat(integer(1, 1), integer(2, magic), bytes(3, bytes(1, fields))) -} diff --git a/app/g2/cfw-message-type.ts b/app/g2/cfw-message-type.ts new file mode 100644 index 00000000..5dd24a61 --- /dev/null +++ b/app/g2/cfw-message-type.ts @@ -0,0 +1,37 @@ +/** Message IDs and header flags mirror g2flash/patches/zlib_glue.c. */ +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_BOUNDING_BOX = 3 +export const CFW_MSG_BUZZER = 5 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_FULL_FRAME = 6 +export const CFW_MSG_DIAGNOSTICS = 7 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_MULTI_SEGMENT = 8 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_RECT_COPY = 9 +export const CFW_MSG_COMPASS = 10 +export const CFW_MSG_CLEANUP = 11 +export const CFW_MSG_RETIRED_12 = 12 +export const CFW_MSG_RETIRED_13 = 13 +export const CFW_MSG_RETIRED_14 = 14 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_STOCK_FONT_STRING = 15 +export const CFW_MSG_AMBIENT_LIGHT = 16 +export const CFW_MSG_RING_BATTERY = 17 +export const CFW_MSG_RETIRED_CACHE_WRITE = 18 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_CACHED_IMAGE = 19 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_CACHED_TEXT = 20 +export const CFW_MSG_UPLOAD_RESOURCE = 21 +export const CFW_MSG_EVICT_RESOURCE = 22 +export const CFW_MSG_PANEL_READ = 23 +export const CFW_MSG_PANEL_WRITE = 24 +export const CFW_MSG_PANEL_PATTERN = 25 +export const CFW_MSG_DRAW_CALLS = 26 +export const CFW_MSG_SET_ROOT_DISPLAY_LIST = 27 +export const CFW_MSG_PRESENT = 28 +export const CFW_MSG_CREATE_SURFACE = 29 +export const CFW_MSG_BRIGHTNESS = 30 +export const CFW_MSG_TYPE_MASK = 127 +export const CFW_MSG_FLAG_LENSES_DIFFER = 128 diff --git a/app/g2/cfw-transport.ios.ts b/app/g2/cfw-transport.ios.ts deleted file mode 100644 index 135f31d1..00000000 --- a/app/g2/cfw-transport.ios.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { fromData, toData, nativeArray } from '../native/kotlin-data' -declare const FaceclawKitIosCfwTransport: any, FaceclawKitIosProtocol: any -export const CFW_MAX_MESSAGE = 65535 -export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number } -/** maxWrite is the ATT value limit; Kotlin owns the persistent zlib stream. */ -export class CfwTransport { - private native = FaceclawKitIosCfwTransport.new() - reset(): void { this.native.reset() } - encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] { - if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 || - ![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514) - throw new Error('Invalid CFW stream parameters') - return nativeArray(this.native.encodeDataStreamIdLensesMaxWrite(toData(message), streamId, lenses, maxWrite), fromData) - } -} -const protocol = FaceclawKitIosProtocol.new() -export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null { - const acks = protocol.acksData(toData(packet)) - return acks ? nativeArray(acks, (ack: any) => ({ nack: ack.nack, streamId: ack.streamId, messageId: ack.messageId, - lens: ack.lens, size: ack.size, checksum: ack.checksum })) : null -} diff --git a/app/g2/cfw-transport.ts b/app/g2/cfw-transport.ts deleted file mode 100644 index 922e0c20..00000000 --- a/app/g2/cfw-transport.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { concat, crc16, SID } from './ble-protocol' - -declare const require: (name: string) => any -export const CFW_MAX_MESSAGE = 65535 -export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number } -const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8) - -/** Matches Android CfwTransport. maxWrite is the ATT value limit (MTU - 3). */ -export class CfwTransport { - private compressor: any - private resetPending = true - private lenses = 0 - reset(): void { this.compressor = null; this.resetPending = true; this.lenses = 0 } - encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] { - if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 || - ![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514) - throw new Error('Invalid CFW stream parameters') - if (this.lenses !== lenses) this.reset() - if (!this.compressor) this.compressor = new (require('pako').Deflate)() - const chunks: Uint8Array[] = [] - // SYNC_FLUSH emits this record while retaining history for the next one. - this.compressor.onData = (chunk: Uint8Array) => chunks.push(chunk.slice()) - if (!this.compressor.push(message, 2)) throw new Error(this.compressor.msg || 'CFW compression failed') - let body = concat(...chunks), flags = lenses | 4 | (this.resetPending ? 8 : 0) - if (!body.length) flags &= ~4 - if (body.length > CFW_MAX_MESSAGE) { - this.reset(); body = message; flags = lenses | 8 - } else { this.resetPending = false; this.lenses = lenses } - const checksum = crc16(message) - const record = concat(new Uint8Array([flags, body.length & 255, body.length >>> 8, checksum & 255, checksum >>> 8]), body) - const capacity = Math.min(252, maxWrite - 11), packets: Uint8Array[] = [] - for (let offset = 0, index = 0; offset < record.length; offset += capacity, index++) { - const body = concat(new Uint8Array([lenses | (offset === 0 ? 0x80 : 0) | - (offset + capacity >= record.length ? 0x40 : 0)]), record.subarray(offset, offset + capacity)) - const crc = crc16(body) - packets.push(concat(new Uint8Array([0xaa, 0x21, (streamId + index) & 255, body.length + 2, 1, 1, SID.cfw, 0]), - body, new Uint8Array([crc & 255, crc >>> 8]))) - } - return packets - } -} - -/** Validate the entire primary + redundant-ACK packet before applying any ACK. */ -export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null { - const n = packet.length - if (n < 19 || n > 40 || (n - 19) % 7 || packet[0] !== 0xaa || packet[1] !== 0x12 || - packet[3] !== n - 8 || packet[4] !== 1 || packet[5] !== 1 || packet[6] !== SID.cfw || packet[7] !== 0 || - ![1, 3].includes(packet[8]) || ![1, 2].includes(packet[12]) || (packet[8] === 3 && n !== 19) || - crc16(packet.subarray(8, -2)) !== u16(packet, n - 2)) return null - const acks = [{ nack: packet[8] === 3, streamId: packet[9], messageId: u16(packet, 10), - lens: packet[12], size: u16(packet, 13), checksum: u16(packet, 15) }] - for (let offset = 17; offset < n - 2; offset += 7) - acks.push({ nack: false, streamId: packet[offset], messageId: u16(packet, offset + 1), lens: packet[12], - size: u16(packet, offset + 3), checksum: u16(packet, offset + 5) }) - return acks -} diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 1edfb46c..16722af6 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -1,6 +1,6 @@ import { startRemoteInput } from "../remote/service"; import { acceptInput, resetRingInputFilter } from "../ui/input-monitor"; -import { Application, ImageSource } from "@nativescript/core"; +import { Application, Dialogs, ImageSource } from "@nativescript/core"; import { EvenAIStatus, EvenAIStatusName, EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName, WatchGestureType, WatchGestureTypeName } from "./events"; import { isValidMacAddress, loadDeviceAddresses } from "./device-addresses"; import { @@ -71,7 +71,7 @@ import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-a import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry"; import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; -import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; +import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; import { getInstalledEvenHubAppById, @@ -213,6 +213,13 @@ class DashboardController { */ private activeTextEditorOnCancel: (() => void) | null = null; private activeTextEditorToggle: TextSettingsEditToggle | null = null; + /** + * The active settings' values (and the toggle's) when the edit began. The + * phone fields write through on every keystroke, so this is what the + * editor's Cancel button puts back. + */ + private activeTextSettingOriginalValues: string[] = []; + private activeTextEditorToggleOriginalValue = false; private evenNotificationActive = false; private evenAppConflictMessage = ""; private firmwareWarningMessage = ""; @@ -276,7 +283,6 @@ class DashboardController { private lockSurfaceConfigured = false; private lastLockScreenEnabled = lockScreenEnabledSetting.get(); private offState: (() => void) | null = null; - private offLog: (() => void) | null = null; private offRing: (() => void) | null = null; private offBattery: (() => void) | null = null; private offSilentMode: (() => void) | null = null; @@ -909,11 +915,14 @@ class DashboardController { */ private pushBrightness(force = false): void { if (!this.communicator) return; - const value = brightnessSetting.get(); + const preferences = getBrightnessPreferences(); + const value = JSON.stringify(preferences); if (!force && value === this.lastPushedBrightness) return; this.lastPushedBrightness = value; - const level = brightnessSettingToLevel(value); - void this.communicator.setBrightness(level === null, level ?? 0).catch(() => {}); + void this.communicator.configureBrightness(preferences).catch((error) => { + this.lastPushedBrightness = null; + this.appendLog(`brightness configuration failed: ${this.formatError(error)}`); + }); } /** Save the occupied part of the composited screen as a 4-bit grayscale PNG. */ @@ -1336,9 +1345,13 @@ class DashboardController { ring: ringAddress, }); this.communicator = communicator; - this.offLog = communicator.onLog((line) => { - this.appendLog(line); - }); + // Size the compositor before anything else is awaited: windows opened + // during the bridge start-ups below (the terminal hub, restored apps) + // configure their surfaces through this.communicator, and the Java call + // queue is FIFO, so enqueuing the screen size first guarantees it lands + // ahead of them. Mirrors ensurePreviewDisplay. + await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); + await communicator.configureBrightness(getBrightnessPreferences()); this.offState = communicator.onStateChange((state) => { if (state.phase !== "connected") resetRingInputFilter(); if (state.phase === "unpaired") { @@ -1506,7 +1519,6 @@ class DashboardController { await nightscoutBridge.start(); // Register the compositor surfaces: the shell chrome above all windows, // and a surface per live window (only the foreground one is composited). - await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); await communicator.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, @@ -1551,8 +1563,6 @@ class DashboardController { const message = this.formatError(error); this.offState?.(); this.offState = null; - this.offLog?.(); - this.offLog = null; this.offRing?.(); this.offRing = null; this.offBattery?.(); @@ -1691,8 +1701,6 @@ class DashboardController { this.clearDashboardTimer(); this.offState?.(); this.offState = null; - this.offLog?.(); - this.offLog = null; this.offRing?.(); this.offRing = null; this.offBattery?.(); @@ -1896,9 +1904,11 @@ class DashboardController { onCancel?: () => void, ): void { this.activeTextSettings = Array.from(settings.slice(0, 2)); + this.activeTextSettingOriginalValues = this.activeTextSettings.map((setting) => setting.get()); this.activeTextEditorTitle = title; this.activeTextEditorOnFinish = onFinish ?? null; this.activeTextEditorToggle = toggle ?? null; + this.activeTextEditorToggleOriginalValue = toggle?.setting.get() ?? false; this.activeTextEditorOnCancel = onCancel ?? null; this.emit(); } @@ -2020,6 +2030,7 @@ class DashboardController { // finishTextSettingEdit clears this first, so a completed edit never fires it. const onCancel = this.activeTextEditorOnCancel; this.activeTextSettings = []; + this.activeTextSettingOriginalValues = []; this.activeTextEditorTitle = ""; this.activeTextEditorOnFinish = null; this.activeTextEditorToggle = null; @@ -2038,6 +2049,11 @@ class DashboardController { */ finishActiveTextSettingEdit(): void { if (!this.activeTextSettings.length) return; + const invalid = this.activeTextSettings.find(setting => setting.validationError()); + if (invalid) { + void Dialogs.alert({ title: invalid.editorTitle, message: invalid.validationError()!, okButtonText: "OK" }); + return; + } const onFinish = this.activeTextEditorOnFinish; // Completing is not cancelling: drop the cancel callback before // endTextSettingEdit, which fires whatever is still set. @@ -2050,6 +2066,28 @@ class DashboardController { onFinish?.(); } + /** + * The phone editor's Cancel button: put back the values the edit started + * with, end the edit as a cancel (the caller's onCancel fires), and + * navigate the Settings app's glasses editor out of the edit page. + */ + cancelActiveTextSettingEdit(): void { + if (!this.activeTextSettings.length) return; + this.activeTextSettings.forEach((setting, index) => { + const original = this.activeTextSettingOriginalValues[index] ?? ""; + if (setting.get() !== original) setting.set(original); + }); + const toggle = this.activeTextEditorToggle; + if (toggle && toggle.setting.get() !== this.activeTextEditorToggleOriginalValue) { + toggle.setting.set(this.activeTextEditorToggleOriginalValue); + } + const closesGlassesEditor = this.activeTextSettings.length === 1; + this.endTextSettingEdit(); + if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) { + this.textEditorHost.requestRender(); + } + } + private updateTextSetting(setting: ConfigSettingString, value: string): void { if (setting.get() !== value) { setting.set(value); @@ -2255,6 +2293,7 @@ class DashboardController { const host = new WorkerAppHost({ appId, worker: createWorker(), + onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId); }, configureSurface: (surfaceId, visible, heightMode) => this.configureWindowSurface(surfaceId, visible, heightMode), setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible), @@ -2499,7 +2538,7 @@ class DashboardController { beginRenderPass(!wantFreshData); const paintStartedAtMs = Date.now(); const planes = frameTimings.span(frameId, "paint", () => - frameTimings.runWithFrame(frameId, () => shell.paintSurface()), + frameTimings.runWithFrame(frameId, () => shell.paintScene()), ); const paintMs = Date.now() - paintStartedAtMs; const paintUsedStaleData = endRenderPass(); @@ -2513,36 +2552,11 @@ class DashboardController { } // The display-target check that used to live here now runs before the // paint, at the top of this function. Nothing between the two can change - // it -- paintSurface is synchronous and there is no await in between -- so + // it -- paintScene is synchronous and there is no await in between -- so // repeating it here is dead code, and the compiler agrees: the early // return narrows this.phase, making the "charging" comparison impossible. const display = this.display; - // A shell overlay that dims what it covers (a context menu) must dim the - // window surfaces too, which live below the shell surface in the - // compositor: forward the factor before this frame composites. - const underlayDim = shell.underlayDim(); - if (underlayDim !== this.appliedUnderlayDim) { - this.appliedUnderlayDim = underlayDim; - await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim); - } - const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes)); - const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes)); - const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); - const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)); - // Spanned because the bridge serializes Java calls: a frame can sit here - // behind another surface's submission, which is otherwise an unexplained - // jump between the paint spans and the composite. - await frameTimings.spanAsync(frameId, "submit", () => - display.submitSurfaceFrame( - SHELL_SURFACE_ID, - buffer, - { x: 0, y: 0, width: image.width, height: image.height }, - fingerprint, - paintMs, - frameId, - preparedDraws, - ), - ); + await frameTimings.spanAsync(frameId, "submit", () => display.submitShellScene(planes, paintMs, frameId)); // Backpressure: the next shell render waits for this one to reach the // glasses. Timing out here means the loop was blocked for the full timeout // and any input arriving meanwhile had its chrome repaint delayed, so say @@ -2651,7 +2665,7 @@ class DashboardController { } private appendLog(line: string): void { - console.log(`[${formatTimestamp(new Date())}] ${line}`); + //console.log(`[${formatTimestamp(new Date())}] ${line}`); } private setDisplayPreview(preview: ImageSource | null): void { @@ -2668,6 +2682,22 @@ class DashboardController { */ private previewTrailingTimer: ReturnType | null = null; private lastRecordCaptureAtMs = 0; + private phonePreviewVisible: (() => boolean) | null = null; + + /** The main page owns the mirror; other phone pages must not keep it rendering. */ + attachPhonePreview(isVisible: () => boolean): () => void { + this.phonePreviewVisible = isVisible; + this.lastConnectedPreviewUpdateAtMs = 0; + this.updateCompositePreview(); + return () => { + if (this.phonePreviewVisible !== isVisible) return; + this.phonePreviewVisible = null; + if (this.previewTrailingTimer) { + clearTimeout(this.previewTrailingTimer); + this.previewTrailingTimer = null; + } + }; + } /** * Refresh the preview now if the floor allows, otherwise once the floor @@ -2675,6 +2705,7 @@ class DashboardController { * of waiting for the safety-net poll. */ private schedulePreviewUpdate(): void { + if (!this.phonePreviewVisible && !this.screenRecordingActive) return; if (this.previewTrailingTimer) return; const wait = this.lastConnectedPreviewUpdateAtMs + CONNECTED_PREVIEW_MIN_UPDATE_MS - Date.now(); if (wait <= 0) { @@ -2702,6 +2733,13 @@ class DashboardController { return; } this.lastConnectedPreviewUpdateAtMs = now; + // A user-requested GIF is an independent consumer, including while the + // phone is asleep or another page/app is foregrounded. + if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) { + this.lastRecordCaptureAtMs = now; + display.recordScreenFrame(); + } + if (!this.phonePreviewVisible?.()) return; // The connected foreground service intentionally keeps this controller // alive after the phone UI is backgrounded. Do not keep constructing // 640x480 Android Bitmaps for a window that cannot display them: besides @@ -2710,10 +2748,8 @@ class DashboardController { if (global.isAndroid) { const activity = Application.android.foregroundActivity; if (!activity || !activity.hasWindowFocus()) return; - } - if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) { - this.lastRecordCaptureAtMs = now; - display.recordScreenFrame(); + const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager; + if (!power?.isInteractive()) return; } const preview = display.getCompositePreview(previewColorSetting.get() === "green"); if (preview) { diff --git a/app/g2/firmware-compat.ts b/app/g2/firmware-compat.ts index 2da29b2d..00d1dde5 100644 --- a/app/g2/firmware-compat.ts +++ b/app/g2/firmware-compat.ts @@ -26,7 +26,7 @@ export type FirmwareInfo = { }; /** The Faceclaw firmware revision this build of the app needs. */ -export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 22; +export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 34; const FACECLAW_PREFIX = "Faceclaw/"; const LEGACY_PREFIX = "EVENCFW"; diff --git a/app/g2/firmware-ota.ts b/app/g2/firmware-ota.ts deleted file mode 100644 index 78cc4701..00000000 --- a/app/g2/firmware-ota.ts +++ /dev/null @@ -1,91 +0,0 @@ -/** Stock EVENOTA validation and transfer, matching Android's flasher/g2flash.py. */ -import { StockConnection, OTA_WRITE, OTA_NOTIFY } from './stock-connection' -import { concat } from './ble-protocol' -import type { FlashProgress } from './firmware-types' - -export type FirmwareSegment = { offset: number; size: number; name: string } -const CRC_TABLE = Array.from({ length: 256 }, (_, b) => { - let c = b << 24 - for (let i = 0; i < 8; i++) c = c & 0x80000000 ? (c << 1) ^ 0x1edc6f41 : c << 1 - return c >>> 0 -}) -export function firmwareCrc(data: Uint8Array): number { - let crc = 0 - for (const byte of data) crc = (crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ byte) & 255] - return crc >>> 0 -} -export function validateFirmware(image: Uint8Array): FirmwareSegment[] { - const u32 = (offset: number) => { - if (offset < 0 || offset + 4 > image.length) throw new Error('Firmware header is truncated.') - return new DataView(image.buffer, image.byteOffset, image.byteLength).getUint32(offset, true) - } - if (image.length < 0x40) throw new Error('File is too small to be a firmware image.') - const count = u32(8) - if (count < 5 || count > 6 || 0x40 + count * 16 > image.length) throw new Error('Expected 5–6 firmware components.') - const segments: FirmwareSegment[] = [] - for (let i = 0; i < count; i++) { - const toc = 0x40 + i * 16, offset = u32(toc + 4) - if (offset < 0x40 + count * 16 || offset + 128 > image.length) throw new Error('Firmware component header is out of bounds.') - const size = u32(offset + 8), end = offset + 128 + size - if (!size || end > image.length) throw new Error('Firmware component payload is truncated.') - if (segments.some(s => offset < s.offset + 128 + s.size && s.offset < end)) throw new Error('Firmware components overlap.') - const nameBytes = image.subarray(offset + 48, offset + 128) - const name = String.fromCharCode(...nameBytes.subarray(0, nameBytes.indexOf(0) < 0 ? 80 : nameBytes.indexOf(0))) - const crc = firmwareCrc(image.subarray(offset + 128, end)) - if (crc !== u32(toc + 12) || crc !== u32(offset + 12)) throw new Error(`Component ${name} CRC32C is stale (image not checksum-fixed).`) - segments.push({ offset, size, name }) - } - const mains = segments.filter(s => s.name === 'ota/s200_firmware_ota.bin') - if (mains.length !== 1) throw new Error('Required main firmware component is missing or duplicated.') - const main = mains[0], payload = main.offset + 128 - if (main.size < 32 || u32(payload + 0x14) !== 0x00438000 || (u32(payload) & 0xffffff) !== main.size) - throw new Error('Main-app preamble load address or length is invalid.') - if (0x00438000 + main.size - 32 > 0x007f0000) throw new Error('Main-app exceeds the safe MRAM ceiling — refusing to flash.') - return segments -} - -export async function flashLensImage(link: StockConnection, role: string, image: Uint8Array, segments: FirmwareSegment[], - progress: (value: FlashProgress) => void, log: (line: string) => void, - delay: (ms: number) => Promise): Promise { - let sequence = 0 - const ctrl = async (op: number, payload = new Uint8Array(), data?: Uint8Array): Promise => { - sequence = (sequence + 1) & 255 - const message = await link.exchange(role, 0xc0, concat(new Uint8Array([op]), payload), - message => message.command === op, data ? 4000 : 8000, OTA_WRITE, OTA_NOTIFY, 0, sequence, data) - return message.payload[1] - } - const accepted = (status: number) => [0, 8, 9].includes(status) - const begin = await ctrl(0) - if (!accepted(begin)) throw new Error(`OTA BEGIN rejected (status ${begin}).`) - const total = segments.reduce((sum, segment) => sum + segment.size, 0) - let before = 0 - for (const [index, segment] of segments.entries()) { - let verified = false, failure: unknown - for (let attempt = 0; attempt < 3; attempt++) { - link.check() - if (attempt) { log(`${segment.name}: re-flash attempt ${attempt + 1}/3`); await delay(1500) } - try { - const status = await ctrl(1, image.slice(segment.offset, segment.offset + 128)) - if (status !== 0) throw new Error(`FILE_CHECK rejected (status ${status}).`) - const blocks = Math.ceil(segment.size / 4096) - for (let block = 0; block < blocks; block++) { - const start = segment.offset + 128 + block * 4096 - const data = image.slice(start, Math.min(start + 4096, segment.offset + 128 + segment.size)) - let ok = false - for (let retry = 0; retry < 3; retry++) { - if (await ctrl(2, new Uint8Array(), data) === 0) { ok = true; break } - } - if (!ok) throw new Error(`Block ${block + 1} rejected three times.`) - if (block % 20 === 0 || block === blocks - 1) - progress({ lens: role, componentIndex: index + 1, componentCount: segments.length, blockIndex: block + 1, - blockCount: blocks, bytesSent: before + Math.min((block + 1) * 4096, segment.size), bytesTotal: total }) - } - const end = await ctrl(3) - if (!accepted(end)) throw new Error(`END verification failed (status ${end}).`) - log(`${role} ${segment.name}: END verify OK (status ${end})`); verified = true; break - } catch (error) { link.check(); failure = error } - } - if (!verified) throw new Error(`Component ${segment.name} failed after 3 attempts: ${failure}`) - before += segment.size - } -} diff --git a/app/g2/firmware/cfw-patches.ts b/app/g2/firmware/cfw-patches.ts index b5c7b0ff..3b961ac9 100644 --- a/app/g2/firmware/cfw-patches.ts +++ b/app/g2/firmware/cfw-patches.ts @@ -19,12 +19,12 @@ export type FirmwarePatchSet = { export const CFW_PATCH_SET: FirmwarePatchSet = { "base": "g2_2.3.0.24.bin", "baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979", - "outputSha256": "059ff37a5455b9b272b715ab62c2910ee4dc4966fbfb2e117159bda746bd7315", + "outputSha256": "7d8764f8b720252354dcd1695d23578b11f9b7cf5df9895d08b9f6668e9d0ee7", "patches": [ { "offset": 1045797, "old": "38b584b0", - "new": "5af317bf", + "new": "5df3a9b8", "desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease" }, { @@ -36,49 +36,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = { { "offset": 1437923, "old": "fef788ff", - "new": "fbf226fc", + "new": "fdf2b8fd", "desc": "cfw_ancs_event" }, { "offset": 1437951, "old": "fef7e8fb", - "new": "fbf260f9", + "new": "fdf2f2fa", "desc": "cfw_ancs_open" }, { "offset": 1437969, "old": "fef7e7fb", - "new": "fbf277f9", + "new": "fdf209fb", "desc": "cfw_ancs_close" }, { "offset": 1388387, "old": "39694d00", - "new": "35407d00", + "new": "59637d00", "desc": "connection-bound ANCS EUS write callback" }, { "offset": 971497, "old": "fff717f8", - "new": "6df347f8", + "new": "6ff3d9f9", "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { "offset": 971845, "old": "fef769ff", - "new": "6cf399ff", + "new": "6ff32bf9", "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { "offset": 979579, "old": "fdf74ef8", - "new": "6bf37ef8", + "new": "6df310fa", "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { "offset": 1428895, "old": "f8f76cfd", - "new": "faf224f9", + "new": "fcf228fa", "desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)" }, { @@ -108,145 +108,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = { { "offset": 1248487, "old": "d4f712fb", - "new": "28f378f9", + "new": "2af3f2fa", "desc": "bl settings_send_wrapper (append caps field 100)" }, { "offset": 1246183, "old": "f5f7e2f8", - "new": "28f310fb", + "new": "2af376fc", "desc": "bl settings_decode_wrapper (Faceclaw lease field 101)" }, { "offset": 975219, "old": "02f083fc", - "new": "6af372fb", + "new": "6cf3e2fc", "desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)" }, { "offset": 975411, "old": "02f023fc", - "new": "6af3e0fb", + "new": "6cf346fd", "desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)" }, { "offset": 975103, "old": "07f085fb", - "new": "6cf350f8", + "new": "6ef3e2f9", "desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)" }, { "offset": 830549, "old": "19f03efb", - "new": "8ff3fff9", + "new": "91f391fb", "desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)" }, { "offset": 830795, "old": "1ff071ff", - "new": "8ff3b8f9", + "new": "91f34afb", "desc": "bl gesture_press (owned event 9 before stock menu gate)" }, { "offset": 830887, "old": "1ff043ff", - "new": "8ff3b2f9", + "new": "91f344fb", "desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)" }, { "offset": 831763, "old": "1ff08dfd", - "new": "8ef3e8ff", + "new": "91f37af9", "desc": "bl gesture_release (owned event 10 before stock menu gate)" }, { "offset": 1570209, "old": "7fb50600", - "new": "d9f209bd", + "new": "dbf283be", "desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline" }, { "offset": 1051545, "old": "f6f787f9", - "new": "53f3d7fe", + "new": "55f311f9", "desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)" }, { "offset": 1051861, "old": "f6f7e9f8", - "new": "53f339fe", + "new": "55f373f8", "desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)" }, { "offset": 1262237, "old": "d9f7d0fd", - "new": "24f3cff9", + "new": "26f333fb", "desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)" }, { "offset": 1262337, "old": "d9f79efd", - "new": "24f39df9", + "new": "26f301fb", "desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)" }, { "offset": 1309405, "old": "67f02dff", - "new": "18f3b7fa", + "new": "1af30bfc", "desc": "bl compass_decode_capture (sample-matched GAF diagnostics)" }, { "offset": 1307865, "old": "fff7c9fb", - "new": "18f335fe", + "new": "1af389ff", "desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)" }, { "offset": 1300937, "old": "00210170", - "new": "1af31bfb", + "new": "1cf36ffc", "desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)" }, { "offset": 4537963, "old": "", - "new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff29c5416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2a351c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c3804ff4407005f073f9002800f0b7824ff60051cff6ff714218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff42fafbff35f8f00bf44e8000121b154e8001f5145f8d023e7aa4645ea064100260127c0f8701206e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c2804ff4407005f090f8002800f0d4814ff60051cff6ff7100bf4218403182f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382f8196382f81a6382f81b6382f81c6382f81d6382f81e6382f81f6382f8206382f8216382f8226382f8236382f8246382f8256382f8266382f8276382f8286382f8296382f82a6382f82b6382f82c6382f82d6382f82e6382f82f6382f8306382f8316382f8326382f8336382f8346382f8356382f8366382f8376382f8386382f8396382f83a6382f83b6382f83c6382f83d6382f83e6382f83f637ff47cafc0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff430afbff35f8f44e8000121b154e8001fa942f8d025e7d0f8746242f6f767c0f24407002ebff35f8f00f0f38007f19a0230464ff0ff314ff0ff379047002840f0498147f6df40c0f24f00804700274ff001080397adf8107006e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e9804ff4407004f094ff002800f0e1804ff60051cff6ff7100bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff42fafbff35f8f00bf44e8000149b354e8001fa942f8d023e705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f1e64ff0ff3742e007f5a9718847eae60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f061fa03ab28465146012206f093fa9df8111007464846002918bf4ff0ff3702f08dfbbbf1000f044608d19df8100028b94af21f30c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b14af21f31c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e72de9f04f81b04ef66c6b4ff29c554ff6005848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0b2804ff4407004f019fe002800f0aa80414600bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff447afbff35f8f00bf45e8000129b155e8001fa142f8d03be7002001b0bde8f08f70b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f845504bf6394c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04f9fb04ff29c50c2f229000668bff35f8f30466ff39600b0f1005f43d130684ef66c65ccf2ff05a8423cd196f8640138b3d6f8604124b340f24030c2f20000d0f8787504a802f0a6f8002f00f0738140f2030a384621464ff416320296cff6807a4ff6ff760397fff759fa4ff6005b48f2a358cff6ff7b00270124c0f245081ce096f8660170b1d6f8680140b147f64c61c2f207010968401a002802f37180002086f8660140f6a120c0f247001fb0bde8f04f0047bff32f8fc0474ff29c50c2f22900d0f80090bff35f8f0068bff35f8f00ea0a01b1f1005f03d10168a94200f0b3804ff4407004f091fc002802f03080594600bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47caf4ff29c52056084728683c683068446848684c684068546858685c685068646868686c68606874687c2f2290252e8001f49457ff441afbff35f8f00bf42e8000121b152e8001f4945f8d035e7817a002941f08087027e0021002a8df81c107cd04ff29c5507aac2f22905535c002b00f0b68053185f78002f74d09f78002f73d0df78002f72d01f79002f71d05f79002f70d09f79002f6fd0df79002f6ed01f7a002f6dd05f7a002f6cd09f7a002f6bd0df7a002f6ad01f7b002f69d05f7b002f68d09f7b002f67d0df7b002f66d01f7c002f65d05f7c002f64d09f7c002f63d0df7c002f62d01f7d002f61d05f7d002f60d09f7d002f5fd0df7d002f5ed01f7e002f5dd05f7e002f5cd09f7e002f5bd0df7e002f5ad01f7f002f59d05f7f002f58d09f7f002f57d0db7f002b56d0203160297ff49aaf76e0002086f86401c6f8600186f86601012086f8650140f6a120c0f24700804701f011bf4ff29c55c2f2290564e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04f235d29937014d052235c29d37010d044235b2913710cd045235a29537108d052235929937104d020235829d37141f0d68300218df87b100121427e002a00f0c280002207ab9f5c002f00f0a0809f187e78002e5ed0be78002e5dd0fe78002e5cd03e79002e5bd07e79002e5ad0be79002e59d0fe79002e58d03e7a002e57d07e7a002e56d0be7a002e55d0fe7a002e54d03e7b002e53d07e7b002e52d0be7b002e51d0fe7b002e50d03e7c002e4fd07e7c002e4ed0be7c002e4dd0fe7c002e4cd03e7d002e4bd07e7d002e4ad0be7d002e49d0fe7d002e48d03e7e002e47d07e7e002e46d0be7e002e45d0fe7e002e44d03e7f002e43d07e7f002e42d0be7f002e41d0ff7f002f40d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff053079f5411d013444b275e2a5f700cd049275d2a9f7008d050275c2adf7004d020275b2a1f7140f0f08100228df87b200131827e002a00f0be80002107aa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f0098100218df87b1001216a6846f23f53c5f28803b3eb520f40f08b80d20700f08d80002107aa535c002b00f0858153185f78002f00f0f9809f78002f00f0f980df78002f00f0f9801f79002f00f0f9805f79002f00f0f9809f79002f00f0f980df79002f00f0f9801f7a002f00f0fc805f7a002f00f0fc809f7a002f00f0fc80df7a002f00f0fc801f7b002f00f0fc805f7b002f00f0fc809f7b002f00f0fc80df7b002f00f0fc801f7c002f00f0fc805f7c002f00f0fc809f7c002f00f0fc80df7c002f00f0fc801f7d002f00f0fc805f7d002f00f0fc809f7d002f00f0fc80df7d002f00f0fc801f7e002f00f0fc805f7e002f00f0fc809f7e002f00f0fc80df7e002f00f0fc801f7f002f00f00d815f7f002f00f00b819f7f002f00f00981db7f002b00f00781203160297ff47baf1de14cf67e22caf210126a60002940f01981002107aa535c002b00f0dc8053185f78002f61d09f78002f60d0df78002f5fd01f79002f5ed05f79002f5fd09f79002f60d0df79002f61d01f7a002f62d05f7a002f63d09f7a002f64d0df7a002f68d01f7b002f69d05f7b002f6ad09f7b002f6bd0df7b002f6cd01f7c002f6dd05f7c002f6ed09f7c002f6fd0df7c002f70d01f7d002f71d05f7d002f72d09f7d002f73d0df7d002f74d01f7e002f75d05f7e002f76d09f7e002f77d0df7e002f78d01f7f002f79d05f7f002f7ad09f7f002f7bd0db7f002b7cd0203160297ff49aaface000211171f6e6013173e0023171e003316fe004316de0013184e0053169e0023180e0063165e003317ce0073161e0043178e008315de0053174e0093159e0063170e00a3155e007316ce000225a710fe60b314ee0083165e00c314ae0093161e00d3146e00a315de00e3142e00b3159e00f313ee00c3155e010313ae00d3151e0113136e00e314de0123132e00f3149e013312ee0103145e014312ae0113141e0153126e012313de0163122e0133139e017311ee0143135e018311ae0153131e0193116e016312de01a3112e0173129e01b310ee0183125e01c310ae0193121e01d3106e01a311de01e3102e01b3119e01f315f2931d84ff04f0353542dd00a444b235e29537028d020235d29937024d00021d17024e01c3104e01d3102e01e3100e01f315f2918d84ff04103535414d00a444c235e2953700fd05d2993700cd04f235c29d37008d043235b29137104d020235a29537141f0948000218df87b1000210df11c0c1cf80120002a00f0a1800ceb01025378002b5ed09378002b5dd0d378002b5cd01379002b5bd05379002b5ad09379002b59d0d379002b58d0137a002b57d0537a002b56d0937a002b55d0d37a002b54d0137b002b53d0537b002b52d0937b002b51d0d37b002b50d0137c002b4fd0537c002b4ed0937c002b4dd0d37c002b4cd0137d002b4bd0537d002b4ad0937d002b49d0d37d002b48d0137e002b47d0537e002b46d0937e002b45d0d37e002b44d0137f002b43d0537f002b42d0937f002b41d0d27f002a40d0203160297ff498af43e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff077020cf8012003d100218df87b1002e0614400224a70c2684ff0000e1cf80e10002900f0c4800ceb0e035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002962d0197c002962d0597c002962d0997c002962d0d97c002962d0197d002962d0597d002962d0997d002962d0d97d002962d0197e002962d0597e002962d0997e002962d0d97e002962d0197f002962d0597f002962d0997f002962d0d97f002962d00ef1200ebef1600f7ff496af4ff0600e5be00ef1010e58e00ef1020e55e00ef1030e52e00ef1040e4fe00ef1050e4ce00ef1060e49e00ef1070e46e00ef1080e43e00ef1090e40e00ef10a0e3de00ef10b0e3ae00ef10c0e37e00ef10d0e34e00ef10e0e31e00ef10f0e2ee00ef1100e2be00ef1110e28e00ef1120e25e00ef1130e22e00ef1140e1fe00ef1150e1ce00ef1160e19e00ef1170e16e00ef1180e13e00ef1190e10e00ef11a0e0de00ef11b0e0ae00ef11c0e07e00ef11d0e04e00ef11e0e01e00ef11f0e01230a2a1ed364274ff47a7642f2107503eb83014900b2fbf1f40a2c11d303fb07f1b2fbf1f40a2c0bd303fb06f1b2fbf1f40a2c05d36b43b2fbf3f10929e7d800e00b46f146bef15f0f98bf4ff05f09c9f1000818eb0e0a52d0b2fbf3f40a2b04f130010cf80e104cf6cd41ccf6cc41a3fb011646d31af1010141d004fb1324f708b4fbf7fb0ceb0e020bf13001517048f21f51c5f2eb11642ba3fb011634d31af102012cd00bfb17447709b4fbf7f6b3f57a7f06f13001917044f6d351c1f26201a3fb015123d31af1030518d006fb1744890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb114242f210710ef1040e8b424fea5733aad209e0ce4607e00ef1010e04e00ef1020e01e00ef1030e0022e2460cf80e201af80210002900f0a1800aeb02014b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff498af52e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a13d84ff075010af802100ed00aeb020173235e2a4b7008d020235d2a8b7004d070235c2acb7040f09d8000218df87b1002694ff000081af80810002900f0c7800aeb08035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002965d0197c002965d0597c002965d0997c002965d0d97c002965d0197d002965d0597d002965d0997d002965d0d97d002965d0197e002965d0597e002965d0997e002965d0d97e002965d0197f002965d0597f002965d0997f002965d0d97f002965d008f12008b8f1600f7ff496af4ff060085ee008f101085be008f1020858e008f1030855e008f1040852e008f105084fe008f106084ce008f1070849e008f1080846e008f1090843e008f10a0840e008f10b083de008f10c083ae008f10d0837e008f10e0834e000220a7162e708f10f082ee008f110082be008f1110828e008f1120825e008f1130822e008f114081fe008f115081ce008f1160819e008f1170816e008f1180813e008f1190810e008f11a080de008f11b080ae008f11c0807e008f11d0804e008f11e0801e008f11f0801230a2a1ed364274ff47a7642f2107503eb83014900b2fbf1f40a2c11d303fb07f1b2fbf1f40a2c0bd303fb06f1b2fbf1f40a2c05d36b43b2fbf3f10929e7d800e00b46c646b8f15f0f98bf4ff05f0ecef1000b42f2107c1beb080950d0b2fbf3f40a2b04f130010af808104cf6cd41ccf6cc41a3fb011644d319f101013fd004fb1324f708b4fbf7f60aeb080206f13001517048f21f51c5f2eb11642ba3fb015132d319f102052ad006fb17444e09b4fbf6f7b3f57a7f07f13001917044f6d351c1f26201a3fb015121d319f1030516d007fb1644890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb114208f1040863454fea5733acd209e0f04607e008f1010804e008f1020801e008f10308039f00220af808201af80210002900f0a1800aeb02014b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff498af4ae001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a0bd84ff075010af8021006d00aeb020173235e2a4b7040f08780044600208df87b0038460221524608f0d6fdd4f870c272208df81c0078208df81d0020204ff000091ffa8cf28df81e008df81f901af80900002800f0c8800aeb09035878002866d09878002866d0d878002866d01879002866d05879002866d09879002866d0d879002866d0187a002866d0587a002866d0987a002866d0d87a002866d0187b002866d0587b002866d0987b002866d0d87b002866d0187c002866d0587c002866d0987c002866d0d87c002866d0187d002866d0587d002866d0987d002866d0d87d002866d0187e002866d0587e002866d0987e002866d0d87e002866d0187f002866d0587f002866d0987f002866d0d87f002866d009f12009b9f1600f7ff496af4ff060095fe004460020887078e709f1010958e009f1020955e009f1030952e009f104094fe009f105094ce009f1060949e009f1070946e009f1080943e009f1090940e009f10a093de009f10b093ae009f10c0937e009f10d0934e009f10e0931e009f10f092ee009f110092be009f1110928e009f1120925e009f1130922e009f114091fe009f115091ce009f1160919e009f1170916e009f1180913e009f1190910e009f11a090de009f11b090ae009f11c0907e009f11d0904e009f11e0901e009f11f0901230a2a1ed364274ff47a7642f2107503eb83004000b2fbf0f40a2c11d303fb07f0b2fbf0f40a2c0bd303fb06f0b2fbf0f40a2c05d36b43b2fbf3f00928e7d800e00346cb46b9f15f0f98bf4ff05f0bcbf1000842f2107e18eb09054dd0b2fbf3f04cf6cd4100f13004ccf6cc410af809400a2ba3fb016441d36e1c3dd000fb1320e408b0fbf4f748f21f5107f130060aeb0902c5f2eb115670642ba3fb011630d3a91c29d007fb14007409b0fbf4f7b3f57a7f07f13001917044f6d351c1f26201a3fb011620d3e91c16d007fb1400b10941f25975b0fbf1f4cdf2b715a3fb055604f13005d57004fb110209f1040973454fea5633afd209e0d94607e009f1010904e009f1020901e009f1030900220af8092040f24039c2f200091af80200002800f0a1800aeb0201487800285ed0887800285dd0c87800285cd0087900285bd0487900285ad08879002859d0c879002858d0087a002857d0487a002856d0887a002855d0c87a002854d0087b002853d0487b002852d0887b002851d0c87b002850d0087c00284fd0487c00284ed0887c00284dd0c87c00284cd0087d00284bd0487d00284ad0887d002849d0c87d002848d0087e002847d0487e002846d0887e002845d0c87e002844d0087f002843d0487f002842d0887f002841d0c87f002840d02032602a7ff498af56e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a17d84ff020000af8020012d00aeb020163205e2a48700cd072205d2a887008d063205c2ac87004d020205b2a087140f0628100208df87b0037214fea1c7001eb1c71bcf1204f38bf00f13001ccf303608df8171000f137010a2838bf00f13001ccf303508df8181000f137010a2838bf00f13001ccf303408df8191000f137010a2838bf00f1300100208df81b001f208df81a100aeb000111f81f2c002a00f0818011f81e2c002a7ed011f81d2c002a7cd011f81c2c002a7ad011f81b2c002a78d011f81a2c002a76d011f8192c002a74d011f8182c002a72d011f8172c002a70d011f8162c002a6ed011f8152c002a6cd011f8142c002a6ad011f8132c002a68d011f8122c002a66d011f8112c002a64d011f8102c002a62d011f80f2c002a60d011f80e2c002a5ed011f80d2c002a5cd011f80c2c002a5ad011f80b2c002a58d011f80a2c002a56d011f8092c002a54d011f8082c002a52d011f8072c002a50d011f8062c002a4ed011f8052c002a4cd011f8042c002a4ad011f8032c002a48d011f8022c002a46d011f8011c002944d01af80010002941d020307f287ff479af61e01f383ae01e3838e01d3836e01c3834e01b3832e01a3830e019382ee018382ce017382ae0163828e0153826e0143824e0133822e0123820e011381ee010381ce00f381ae00e3818e00d3816e00c3814e00b3812e00a3810e009380ee008380ce007380ae0063808e0053806e0043804e0033802e0023800e001385f2822d8c0f15d0ec0f15e02c0f15f03c0f1600c504400260df11705af5d8755b7b1b34211d0af1979788419617079b1b2420ad0b978a17051b1b64505d0f978e17029b10436b445e8d100208df87b00039842f2941845f29c1407aa0e21c2f22008c2f2130408f07ffad9f81c00a0425fd1d9f82800b0f54d2f5ad1d9f82c00a04256d145f610630020c2f213034cf6394508f1040c002763454ad81c6834f003061cd098454ff0ff3228bf0c2c42d3a8eb03018e424ff0ff313ed83946be4288bf3146e2071cbf0f4630449919013d01f10403e0d12ce0002048719de60421b7f5807f1ed300223e46b6f5007f14d3b6f5806f13d3b6f5006f12d30901b2eb163f4fea1616f0d10ce000219171fef76bbf00211172fef729bc490002e0890000e0c900634504d1494207ea0102014603e04ff0ff324ff0ff3147f64c60c2f20700d0f8840008f1080342f6941c9842c2f2270c4dd142f610674ff0000bc2f2200746f6b9750cf1040a00264ff0ff394ff0ff3857453cd83c6834f0030015d0bc4528bf0c2c36d3aceb0703984234d8b042334688bf0346e4071cbf1e4683443844013d00f10407e4d122e00425b6f5807f17d34ff0000e3046b0f5007f0cd3b0f5806f0bd3b0f5006f0ad32d01beeb103f4fea1010f0d104e06d0002e0ad0000e0ed00574504d1684206ea0009d84603e04ff0ff394ff0ff3840f24037c2f200073b680cf1080083425bd1fb68b3f5333f57d13b69834254d14ff29c5043f21063c2f229004ff0000bc2f2270342f6f934a0f1040ca0f1080e00264ff0ff3a63453fd81d6835f0030718d09e454ff0ff30019028bf0c2d37d3aeeb0300874235d83046b74288bf3846ed071cbf0646bb44d819013c00f10403e1d122e00425b6f5807f16d300273446b4f5007f0cd3b4f5806f0bd3b4f5006f0ad32d01b7eb143f4fea1414f0d104e06d0002e0ad0000e0ed00634505d16b4206ea03000190da4604e04ff0ff3001904ff0ff3a66208df81c0072208df81d0065208df81e008df81f002f208df820006d204b238df8210061208df8253069238df8220078208df82630422356278df8230020208df827303a238df82b70472707ac8df824008df828308df829004c238df82c708df82e00002720468df82a308df82d308df82f7008f08ffae05d002800f0a080e019417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02037602f7ff49aaf64e001373ae0023738e0033736e0043734e0053732e0063730e007372ee008372ce009372ae00a3728e00b3726e00c3724e00d3722e00e3720e00f371ee010371ce011371ae0123718e0133716e0143714e0153712e0163710e017370ee018370ce019370ae01a3708e01b3706e01c3704e01d3702e01e3700e01f375f2f25d84ff02000e05521d0e01945215e2f41701cd076215d2f817018d065215c2fc17014d06e215b2f017110d048215a2f41710cd07521592f817108d06221582fc17104d02021572f017240f0f98000208df87b00204641464a4608f0bbf9039d0020215c002900f0a08021184a78002a5ed08a78002a5dd0ca78002a5cd00a79002a5bd04a79002a5ad08a79002a59d0ca79002a58d00a7a002a57d04a7a002a56d08a7a002a55d0ca7a002a54d00a7b002a53d04a7b002a52d08a7b002a51d0ca7b002a50d00a7c002a4fd04a7c002a4ed08a7c002a4dd0ca7c002a4cd00a7d002a4bd04a7d002a4ad08a7d002a49d0ca7d002a48d00a7e002a47d04a7e002a46d08a7e002a45d0ca7e002a44d00a7f002a43d04a7f002a42d08a7f002a41d0c97f002940d0203060287ff49aaf5be001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281cd84ff02001215418d021184f225e284a7013d074225d288a700fd068225c28ca700bd065225b280a7107d072225a284a7103d0202259288a7128d100208df87b0007ac019a2046514608f0edf828461a21224607f06aff029e002086f86401c6f8600103984ef2173107904ff41630089007a8c0f247018847012086f8660104a800f047f930611fb0bde8f08f0020c871d6e70021417206e700bf2de9f04f81b0009041f204024cf65560cef20002ccf2ac50c2f8ac0f4ef6fc50cef2000001684ef66c6641f08071016052f8040c4ff29c574ff6005a48f2a358ccf2ff06c2f229074ff6ff7440f00100cff6ff7a00254ff0010bc0f2450842f8040c02e0bff32f8fc047d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168b14200f0ae804ff4407002f0f2fb002800f0e48051464218403182f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382f8195382f81a5382f81b5382f81c5382f81d5382f81e5382f81f5382f8205382f8215382f8225382f8235382f8245382f8255382f8265382f8275382f8285382f8295382f82a5382f82b5382f82c5382f82d5382f82e5382f82f5382f8305382f8315382f8325382f8335382f8345382f8355382f8365382f8375382f8385382f8395382f83a5382f83b5382f83c5382f83d5382f83e5382f83f537ff47caf066080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff448afbff35f8f47e8000121b157e8001f4945f8d03de74169d1bb47f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f421ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94206d041f20401cef200010968c91a416141f20400cef2000000680099086001b0bde8f08f00bf2de9f04f81b041f20401cef200014ef66c674ff29c56d1f8009000684ff6005bccf2ff07c2f229064ff6ff74cff6ff7b00254ff00108009007e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168b94200f0ae804ff4407002f0c6fa002800f0028159464218403182f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382f8195382f81a5382f81b5382f81c5382f81d5382f81e5382f81f5382f8205382f8215382f8225382f8235382f8245382f8255382f8265382f8275382f8285382f8295382f82a5382f82b5382f82c5382f82d5382f82e5382f82f5382f8305382f8315382f8325382f8335382f8345382f8355382f8365382f8375382f8385382f8395382f83a5382f83b5382f83c5382f83d5382f83e5382f83f537ff47caf076080f80a808483c483048444848484c484048544858485c485048644868486c4860487448756e8001f51457ff444afbff35f8f46e8000121b156e8001f5145f8d039e74169009d61b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce047f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422fd041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94217d041f20401cef20001096844f6d357ca1ac1f26207a2fb07764261b00901284ff0010088bfb009994208bffa2001e0009dfa20a9eb0501b1fbf0f001b0bde8f08f002858d1002956d0082a54d30b78aa2b51d18b79f02b4ed12de9f04f83b00b2a4ed3c878a2f10803834249d14878212848d10879012845d14879012842d1c87900283fd10c4614f8080f10f03c0f39d18818884610f8021c10f8010c164641ea0025a2f10a0081b2204607f0e3fa854228d145f64d50c0f246008047811eb1fa81f14f09012808bf0227002f00f03381207887f00301084200f02f81012039464246334600f07afa0446002898f8080018bf062422e14ff27143c0f24c0318470b2400e00a2445f64d50c0f246008047811eb1fa81f14d09012808bf0225002d00f005814ef66c6a4ff29c574ff600580294ccf2ff0ac2f229074ff6ff74cff6ff7800264ff0010b06e048f2a351c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168514500f0b1804ff4407002f017f9002800f0d280414600bf4218403182f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382f8196382f81a6382f81b6382f81c6382f81d6382f81e6382f81f6382f8206382f8216382f8226382f8236382f8246382f8256382f8266382f8276382f8286382f8296382f82a6382f82b6382f82c6382f82d6382f82e6382f82f6382f8306382f8316382f8326382f8336382f8346382f8356382f8366382f8376382f8386382f8396382f83a6382f83b6382f83c6382f83d6382f83e6382f83f637ff47cafc0f800a080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff442afbff35f8f00bf47e8000121b157e8001f4945f8d036e705eb450100eb011090f85d1201b300f513760024304629462a460323009407f073fa706840b14bf69b71c0f25d018847706807f0e3f874603068b475002818bf07f0dcf80020029c3060b060308200e0029c204603b0bde8f08f0624f9e700243842f6d0404631463a463b4600f06af9002818bf0446ece70b291dd3c278a1f10803934218d10278aa2a13d14278212a10d10279012a0dd14279012a0ad18279f02a07d1c2792ab9024612f8083f13f03c0f03d00a2070470b20704710b5084410f8023c10f8010c43ea0024a1f10a0081b2104607f060f9201a18bf0a2010bd002808bf7047b0b582b00446407cd0b1002520460323009507f006fa606840b14bf69b71c0f25d018847606807f076f865602068a575002818bf07f06ff800202060a060208202b0bde8b040704700bf2de9f04f81b04ef66c654ff29c564ff600598046ccf2ff05c2f229064ff6ff77cff6ff7900244ff0010a06e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0af804ff4407001f0a0ff002800f0a78049464218403182f8004382f8014382f8024382f8034382f8044382f8054382f8064382f8074382f8084382f8094382f80a4382f80b4382f80c4382f80d4382f80e4382f80f4382f8104382f8114382f8124382f8134382f8144382f8154382f8164382f8174382f8184382f8194382f81a4382f81b4382f81c4382f81d4382f81e4382f81f4382f8204382f8214382f8224382f8234382f8244382f8254382f8264382f8274382f8284382f8294382f82a4382f82b4382f82c4382f82d4382f82e4382f82f4382f8304382f8314382f8324382f8334382f8344382f8354382f8364382f8374382f8384382f8394382f83a4382f83b4382f83c4382f83d4382f83e4382f83f437ff47caf056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff444afbff35f8f46e8000129b156e8001f5945f8d039e7002008eb480100eb0111002818bf01f5137001b0bde8f08fb0b5c4b0dc08202c03d94ff0ff3044b0b0bd0df107048df807008df80810a01c11461a461d46fcf75dfca81c4af6610582b2c0f24605f02021460023a84744b0b0bd00bf2de9f04f8bb04ef66c654ff29c564ff6005948f2a35bccf2ff05c2f229064ff6ff77cff6ff7900244ff0010ac0f2450bcde90612cde9080303e000bfbff32f8fd847d6f80080bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0ae804ff4407001f092fe002800f04a8449464218403182f8004382f8014382f8024382f8034382f8044382f8054382f8064382f8074382f8084382f8094382f80a4382f80b4382f80c4382f80d4382f80e4382f80f4382f8104382f8114382f8124382f8134382f8144382f8154382f8164382f8174382f8184382f8194382f81a4382f81b4382f81c4382f81d4382f81e4382f81f4382f8204382f8214382f8224382f8234382f8244382f8254382f8264382f8274382f8284382f8294382f82a4382f82b4382f82c4382f82d4382f82e4382f82f4382f8304382f8314382f8324382f8334382f8344382f8354382f8364382f8374382f8384382f8394382f83a4382f83b4382f83c4382f83d4382f83e4382f83f437ff47caf056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f41457ff448afbff35f8f46e8000121b156e8001f4145f8d03de70999089d01eb410100eb011095f9081000f51376b1f1ff3f01f0030409dd374617f8110fd0bb3068002818bf06f064fe2ee3307cf0b1707ce0b10799099a002730460323009706f0ddff706840b14bf69b71c0f25d018847706806f04dfe77603068b775002818bf06f046fe00203060b06030823068002818bf06f03dfe00204ff480713060b0603182a978b081092006f11107f174b17434757076b07ca978884240f0da82317da14240f0d682069b411ca3f10b00727e02971e2828bf1e209042b17488bf7076981e84b200270a2c4ff00008c0f0e682092241f221090920039419e030684f46002818bf06f004feb089376001303774b760b081002c18bf4ff00608039cd146ddf814a00aeb0b0082b2a24280f0c682317c80b20429824600f28880aa5c00f1010a4b1c0129337405dd022906d0032970d1f28106e021b9727503e0338943ea02223281a24580f0a98204296ed015f80a204ff0000c831c8f1c0129377406ddff290bd0032967460dd19a4652e000291cbf378947ea0222328100e072756746a34204d38ae2f281a34280f08782022936d8ea5c00f1030acb1c0129337407dd022937d0fe2907d17275a2450cd375e231b9338943ea02223281a24504d36de2f281a24580f06a8202292fd015f80a20031d41f00407fc29377408ddfd2909d0fe291cbf378947ea0222328103e079b9f28100e072756746a34280f04f8209b19a4613e0ea5c00f1050a0520307401e09a466746f189308941ea0221f18128b106f070fd0028306000f0698233897089191a1ffa81fb1ffa8af1621a5a45b8bf93465fea0b4208d03268294410441ffa8bf2fcf741fa70893389584481b2994270817ff44faf0025cdf81080d6f800808df82b50707db8f1000f06930df12b0108bf88460f2806d8317d00f00302511ab1fa81f14d09000706d4317e707e814238bf30760fe000bf706840b14bf69b71c0f25d018847706806f01afd7760707e30760120f082cdf814a09db1707db57d400714d4002d4ff0000400f0888106984ff6ff7381b2002900f00d8101295ed10020d2e0707d400753d4002477e14ff4803006f0fffc002d804600f06f81b07d002800f06b81b8f1000f00f067817568002d00f0ff8030890428c0f05f8131680a1812f8043c002b40f0588112f8033c002b40f0538112f8023cff2b40f04e8112f8012cff2a40f049814ff4803429606860c5f80c802c610028019000f066814bf69b70c0f25d0000f58572284600219047002840f032812969002900f02e8168688c420c46e7d1019a0c468242e3d124e14ff4803006f0adfc04461fe1069a4ff6fe701040a44642426ff0010000bf08eb0005ac78ed7883ea04231cb25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead73787ea430383ea05231fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead737023087ea430309ead5371518023587ea430395d1023000276446069ad20732d018f8000083ea002002b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead33282ea4003f0899ab282426fd14046fcf7b6f900286ad141f2210a4ff00009012375e0382006f0edfb00285ed00546077047708770c770077147718771c771077247728772c772077347738773c773077447748774c774077547758775c775077647768776c776077747778777c77780f8287080f8297080f82a7080f82b7080f82c7080f82d7080f82e7080f82f7080f8307080f8317080f8327080f8337080f8347080f8357080f8367080f8377046f6c530c0f200007844286246f6cd30c0f20000784468624bf69b70c0f25d0045f6e46200f13c0c28460f21c0f27c023823e04740b3284606f082fb4446706841f2210a40b14bf69b71c0f25d018847706806f075fb77604ff000090323b77506980799099a089dddf8108087b23046009706f0ebfc002c214604463ff456ad084606f05dfb51e5756074e6c4f58030b4f5803f444606902f60ef607ff647aecde70799099a002430460323009406f0cdfc706840b14bf69b71c0f25d018847706806f03dfb74603068b475002818bf06f036fb0020387000203060b06030824ff00a0840460bb0bde8f08f287a4006f8d5307c08b302980078e0b10799099a002430460323009406f0a0fc706840b14bf69b71c0f25d018847706806f010fb74603068b475002818bf06f009fb00203060b06030824ff00b083068002818bf06f0fefa00203060b0603082cae702980078e0b10799099a002430460323009406f074fc706840b14bf69b71c0f25d018847706806f0e4fa74603068b475002818bf06f0ddfa00203060b06030824ff00608a7e72de9f041f02846d100294ff00b0000f08880022ac0f0858045f64d50c0f246000e4617468047811eb1fa81f14c09012808bf0224002c4ff00a0072d07578e91ec9b2fe2938bfbde8f081307833463946022829d0012866d1ac4204bf0020bde8f0814808842884bf0b20bde8f081881e03f1020887b240463946fff7d5f920b306462846fff72cfa21462a46fff700fa3046bde8f08145f64d57c0f2460707f5e27cbde8f0416047ac4219d1a1f1210010f1160f06d36ff0480048432330c0b2242810d90b20bde8f081b07a204207d04046394622462b46bde8f041fff70abb0020bde8f0811a4612f8020f40f00207032f18d19b796340032b14d10b2904d0032804bf0b20bde8f081881e4ef60b6783b2c0f247070120f021b847002818bf0620bde8f0810a20bde8f0814ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f15d10a684ef66c63ccf2ff039a420ed1d1f8681159b147f64c62c2f207021268891a012903db0178042938bf7047002101707047b0b541f23154c0f252041d46a0474ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f08d10a684ef66c63ccf2ff039a4218bf002100e00021421c18bf002900d1b0bd95f84d20002afad091f84321002a08bfb0bd46f25863c2f206031a68132aeed8c2ebc20403eb04135b6801ebc2014ff40042c1f8c031a1f8c6212a7a01f5e071002a0fbf802248f20112ca808122ab7c33b1ab7d23b1022342f002028b71ca7195f820303bb195f8243023b1032342f004028b71ca7195f846304bb195f8443042f008020b7195f84530ca714b71b0bd48f27342c2f207021378002b14bf9378ff230b711378002b14bf5278ff224a71b0bdf0b589b048f66502c0f24b020c46054690474ff29c51c2f229010e68bff35f8f31466ff39601b1f1005f08d131684ef66c62ccf2ff02914208bf092d01d009b0f0bde1082c29fad896f843110029f6d045f64d51c0f2460107468847014638460129ecd1ff22079244f6435246f258610020cff60172c2f20601089006920b685a1e132a4fd8c3ebc30501eb05111a1fa1f5de7191f85451ad0618d4d51c42d091f8e450ad0616d4951c3cd091f87450ad0613d4551c36d00d79ad0610d4043aa1f5e071151da3f10403e3d12be05a1e01f5a87104e09a1ee03101e0da1e703106ebc203096893f9c721b2f1ff3f1adc03f5e0731e688e4215d11e798df81b605e798df81c609b798df81e200a0a8df820108df821200a0c090e8df81d308df822208df823100f228df8012010228df80220522208218df80420b4f5804f4ff080028df800108df8061038bf4ff48042b4f5001f6ff07f0604f07f0138bf0026802c28bf80318df807109df809509df80a108df8030004d29df808200223002415e0c4f3c610b4f5804f024403d20323012400200be0b4f5001f03d204230224012003e0052303240120002135468df8053004f108038df808208df809508df80a1070b96846a222c118c254062048700c208870c81c06a90c22fbf7a3fd04f117034ff225066a46c0f2470601200821b047384609b0f0bd70b582b00546ff208df8040044f25220c0f20100009040f2c960c0f2480016460c46804798b148f27e30c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400301b28bf08280cd3d221281929550621417005218170033069460522fbf75ffd0834204602b070bd00bf024600284ff0ff3009d0022918bf70471078112801d1507810b14ff0ff30704710b586b045f64d50c0f24600804701284ff000003ed1cde9020040f20830c0f210000190ff208df8140044f25220c0f20100049040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8132008d80721b0f5807f38bf03218df813108df8140040f2d260adf80800052001ac8df80a00e01d04a90522fbf709fd4ff2250cc0f2470c0120092122460c23e04706b010bd4ff29c50c2f22900016808466ff39600b0f1005f16d108684ef66c62ccf2ff0290420fd1d1f8380160b147f64c62c2f207021268801a0028cebf01200020c1f838017047002070474ff29c50c2f22900026810466ff39600b0f1005f2bd110684ef66c61ccf2ff01884224d1d2f8680108b347f64c61c2f207010968401a0028c4bf01207047d2f86c1100200029c2f8680182f8660108bf704710b5c2f86c0148f2a352c0f245020446084690472046bde8104070470020704700bff0b101684ef66c62ccf2ff02914218bf704790f8421199b110b54af273644ff0000cc0f24604864680f842c10120002100220023aef840c1a446bde810406047704700bf70b5002862d0044600796179a279e37940ea012040ea024050ea036007d00e2803d0032852d1032502e0042500e00225fff766ff002849d04ff29c50c2f22900066830466ff39600b0f1005f3ed130684ef66c61ccf2ff01884218bf70bd45f64d50a478c0f24600804701282ed1082086f86002032086f86102102086f86202b22086f86402062086f8650286f86602462086f8670243204ff0000e86f8680201204ef6057c06f5187286f863e286f8690286f86a5286f86b42c0f2470c09210d2386f86ce2bde87040604770bd00bf2de9f04f81b04ef66c694ff29c564ff600580446ccf2ff09c2f229064ff6ff77cff6ff7800254ff0010a06e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168494500f0b0804ff4407000f05cfe002800f0d58041464218403182f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382f8195382f81a5382f81b5382f81c5382f81d5382f81e5382f81f5382f8205382f8215382f8225382f8235382f8245382f8255382f8265382f8275382f8285382f8295382f82a5382f82b5382f82c5382f82d5382f82e5382f82f5382f8305382f8315382f8325382f8335382f8345382f8355382f8365382f8375382f8385382f8395382f83a5382f83b5382f83c5382f83d5382f83e5382f83f537ff47cafc0f8009080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff443afbff35f8f00bf46e8000121b156e8001f5945f8d037e7002780f857712a270821032380f85871042780f8541180f85531102380f8597180f85a1101214ff2250c074600f5aa7280f8563180f85b1180f85c31002c18bf0124c0f2470c012010210a2387f85d4101b0bde8f04f604701b0bde8f08f00bf80b582b04ff0010ccdf800c000f002f802b080bd2de9f04381b04ff29c57c2f229073f684ff000093e466ff39606b6f1005f07d13e684ef66c65ccf2ff05ae4208bfb946012811d1ddf820801c4615460e460746fff7defd2346044631462a463846002c18bfb9f1000f07d14af2736cc0f2460c01b0bde8f043604799f8420188b1b8f1010f40f08580d9f83c01002189f84211a9f84011e0b342f68d41c0f24401884736e0b9f840010121013080b2012898bf0846a9f84001d9f83c0142f68d44c0f2440489f8421168b94ff6f530cff6ff70a4f59477784400214a460023b847c9f83c0140b1a047d9f83c01a4f140024ff4c8719047a8b199f84201002848d0d9f83c01002189f84211a9f8401100b1a0474af2736cc0f2460c0120002100220023a6e745f64d50c0f24600804701282fd1082089f84401032089f84501102089f84601002089f84701b22089f84801062089f8490189f84a01462089f84b01432089f84c01012089f84d01b9f840014ef6057c070a09f5a27289f84e8189f84f01c0f2470c012009210d2389f850716fe701b0bde8f08300bf80b582b04ff0050ccdf800c0fff734ff02b080bd2de9f04f83b04468002c00f0c7828368002b00f0c38204eb030b4ff000090027002614e047f64c63c2f207031b6845f69077c0f201073b44c6f838310ceb0a0300275b451c464ff0000680f0a782a04618f9013b03f07f05bd40002b46ea050610d406f00703012b16dc0bb3012b40f09582abeb0803082bc0f0908204f10903dee700bfd84580f08982182f07f107074446dcd982e200bf022b3ad0052b40f07d82abeb0803042bc0f07882631dc7e7d84580f0738294f90130002b04f10203bed55b4580f06a8294f90230b3f1ff3f04f10303b4dc5b4580f0608294f90330b3f1ff3f04f10403aadc5b4580f0568294f90430b3f1ff3f04f10503a0dc5b4580f04c8294f90530002b00f14782a31d96e7d84580f0428294f9013004f1020c002b03f07f0a1ed5dc4580f0378294f90270e31c07f07f05b7f1ff3f4aeac51a02dde0469c460ee05b4580f0278294f9037004f1040c07f07f05b7f1ff3f4aea853a33dd9846abeb0c039a4500f21682f308672b1dd0652b7ff460afbaf1060ffff45caf9cf80030462b7ff457af98f80230432b7ff452af98f80330012b7ff44daf4ff29c54c2f229040290cde9002132e0044660460e4651461746654600f021fcac4631463a46204637e7dc4580f0e58194f90470631d07f07f05b7f1ff3f4aea455aaddc5b4580f0d88194f90570002f00f1d38104f1060c4aea077ab1e748f2a3513046c0f24501bff32f8f8847dde900210298ac462768bff35f8f2668bff35f8f33466ff39603b3f1005f07d133684ef66c65ccf2ff05ab4200f0c2804ff44070654600f083fbdde9002106460298ac46002e3ff4f1ae4ff60053cff6ff7300bff418403384f8009384f8019384f8029384f8039384f8049384f8059384f8069384f8079384f8089384f8099384f80a9384f80b9384f80c9384f80d9384f80e9384f80f9384f8109384f8119384f8129384f8139384f8149384f8159384f8169384f8179384f8189384f8199384f81a9384f81b9384f81c9384f81d9384f81e9384f81f9384f8209384f8219384f8229384f8239384f8249384f8259384f8269384f8279384f8289384f8299384f82a9384f82b9384f82c9384f82d9384f82e9384f82f9384f8309384f8319384f8329384f8339384f8349384f8359384f8369384f8379384f8389384f8399384f83a9384f83b9384f83c9384f83d9384f83e9384f83f937ff47caf4ef66c63ccf2ff0333600123b3724ff6ff734ff29c54b383f38333847384b384f38433857385b385f38533867386b386f38633877387c2f2290454e8003fbb427ff428afbff35f8f44e8006323b154e8003fbb42f8d01de798f8043098f8057098f80650032b47ea052433dd052b54dc042b74d0052b7ff42baed6f868313bb147f64c67c2f207073f68db1b002b14dcd6f86c3186f866917bb10c4648f2a35180461846c0f24501c6f86c91174665468847ac4621463a46404647f64c63c2f207031b6845f69077c0f201073b44c6f86831fde5012b3ff4efad022b00f08380032b7ff4f5ad174688466546fff704fb03460298ac4641463a46002b3ff4e8ad96f84231002b3ff4e3ad304641f28831a6f8404105f04efd47e0062b4ad0072b7ff4d6ad80464df6db50c0f24a0016460f466446804703465d1ea446394632464046012d3ff6c4ad981eb0fa80f04009fff7bafba446394632464046b8e5174688466546fff7c8fa03460298ac4641463a46002b3ff4acad96f84231002b3ff4a7adb6f84031a3427ff4a2add6f83c3186f84291002ba6f840913ff499ad42f68d411846c0f2440188470298ac4641463a468de5d6f86c31c6f86891002b86f866913ff485ad0f4648f2a35104461846c0f24501c6f86c91164665468847ac46394632463ae696f84231c6f83891002b3ff46ead0446d6f83c0165468846174686f84291a6f8409120b142f68d41c0f2440188474af273660120002100220023c0f24606b04720463a464146ac464fe54ff2a513c0f2490303b0bde8f04f18477fb50646002806d1fff756fa002807d19de80f00064649f29b1cc0f24f0c60477fbd00bff0b583b01d46144609294ed106460f46f22d10d8a22160196155062141700b21817046f24c71c0f20001033079440b22faf72eff0e35204629464ff4807200f03df80546ff208df8080044f25220c0f20100019040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8072008d80721b0f5807f38bf03218df807108df80800f82d0cd8d221601961550621417005218170033001a90522faf7f6fe0835394630464ef60577c0f2470722462b46b84703b0f0bdf0b587b00c464ff29c51c2f229010d68bff35f8f29466ff39601b1f1005f52d129684ef66c63ccf2ff0399424bd14d218df8041043218df8051001218df8061095f8701195f871318df807108df8083095f87511b5f878318df80b108df80c30190ab5f87a318df80d10190a8df80f1095f8761195f8737195f874618df80e3095f877318df8101045f64d51c0f246018df809708df80a608df81130064617468847d5f87c3101468df81210190a8df81410190c3a4630468df813308df81510190ea02328e04d218df8041043218df8051001218df80610002100238df807108df808108df809108df80a108df80b108df80c108df80d108df80e108df80f108df810108df811108df812108df813108df814108df815108df816108df817300023111b8df8183028bf18290cd3c222011902550620487015208870c81c01a91522faf747fe1834204607b0f0bd00bf2de9f04f81b04ff29c50c2f229000468bff35f8f20466ff39600b0f1005f0dd120684ef66c63ccf2ff03984206d194f8700118b1002a18bf002902d101b0bde8f08f94f877010028f8d0d4f880010028f4d047f64c69c2f20709d9f80030c01a0128ebdb94f87101012802d14ff0010b06e094f873b1bbf1010f18bf4ff0020b94f876510020eb078846adf80200adf8000013d0bbf1020f10d194f8750168b96b4641f281570df10201c0f25b07404615461a46b8472a4694f876511646b2f5c86f28bf4ff4c86606f1150a5046174600f050f80028b1d0b7f5c86f88bf45f08005d9f80010d4f87c2153230271120a42710a0a8171c2710a0c090e03704d234172a02143700123c17200218370c570027280f80ab0017394f87511bdf802204173bdf800108373c173090a0174110a8174310a017500f115014274c6740746084641463246faf79dfd4ff27d12c0f24702384651469047d4f87c0148f2a3510130c4f87c01c0f245013846884701b0bde8f08fb0b54ff2a054c2f22904216846f23f52c5f28802b2eb510f48f25f514cf67e25c0f24501caf2101518bf25608847002804bf691c2160b0bd10b301684ef66c62ccf2ff0291421bd190f87711c1b1d0f88011b1b147f64c62c2f207021368cb1a012b0edb1268d0f88401002808bf7047891a42f64d42c0f2440208bf01211047704710b5002149f25962c0f8801180f87711c0f25902002044f2436190474ef6e374c0f25a04a04704f1bc00bde81040004700bf2de9f04f81b00429c0f0ff800c46014600784d2840f0f9804878432840f0f5808878012840f0f1804ef66c684ff29c57ccf2ff08c2f229074ff6ff7600254ff0010b8a4607e048f2a351c0f24501bff32f8f88475146d7f80090bff35f8f3868bff35f8f02466ff39602b2f1005f03d10268424500f0b4804ff44070fff766ff002800f0c2804ff600525146cff6ff728318403283f8005383f8015383f8025383f8035383f8045383f8055383f8065383f8075383f8085383f8095383f80a5383f80b5383f80c5383f80d5383f80e5383f80f5383f8105383f8115383f8125383f8135383f8145383f8155383f8165383f8175383f8185383f8195383f81a5383f81b5383f81c5383f81d5383f81e5383f81f5383f8205383f8215383f8225383f8235383f8245383f8255383f8265383f8275383f8285383f8295383f82a5383f82b5383f82c5383f82d5383f82e5383f82f5383f8305383f8315383f8325383f8335383f8345383f8355383f8365383f8375383f8385383f8395383f83a5383f83b5383f83c5383f83d5383f83e5383f83f537ff47cafc0f8008080f80ab08683c683068446848684c684068546858685c685068646868686c6860687468757e8002f4a457ff43eafbff35f8f00bf47e8000222b157e8002f4a45f8d032e7ca78022a04dc012a0dd0022a79d00ce0032a0dd0042a08d190f8701129b101b0bde8f04faae00d2c06d201b0bde8f08f044600f045f963e090f87721aab1002149f2596280f87711c0f259020446002044f2436190474ef6e375c0f25a05a84705f1bc008047204651460a790446012a88bf002280f871214a7902f00303012b4ff0010298bf134680f873318f79012f88bf002780f87471cf79022f88bf002780f875710f7a4e7a47ea0627502f98bf5027b7f5f07f28bf4ff4f077a0f878718f7ace7a47ea062741f28836b74238bf3e46a0f87a61097b80f8702101f0030180f8761103f0010101eb530180f872110021c0f87c1100f009f8204600f03ef8204601b0bde8f04f6ae000bfb0b5044690f87601800758bfb0bd94f871014ef633750128c0f25a0507d105f1b000804705f58b71002088470ee005f5b670804794f87101012801d1002004e094f87301013818bf0120a8474ff69122cff6ff7249f201537a44c0f25903002044f243619847012084f87701b0bd00bf70b5044647f64c60c2f20700006894f8771145f69075c0f2010500292844c4f8800108bf70bdd4f8840142f66536c0f2440658b94ff6e930cff6ff707844002122460023b047c4f8840150b106f594718847d4f8840106f1e8022946bde87040104770bd70b5044600f5c475082084f88801032084f88901102084f88a01c22084f88c01062084f88d014d2084f88f01432084f89001012084f8910194f8700194f8711184f8920184f8931194f87501b4f8781184f8960184f89711080ab4f87a1184f89801080a84f89a0194f8760194f8732194f8743184f8991194f8771184f89b0145f64d500026c0f2460084f88b6184f8942184f8953184f89c118047d4f87c1184f89d01080a84f89f01080c84f8a001080e84f8a101a02084f8a36115264ef6057c84f89e1184f8a201c0f2470c012009212a461c2384f88e61bde87040604770b54ff29c50c2f229000468bff35f8f20466ff39600b0f1005f33d120684ef66c61ccf2ff01884218bf70bd94f877010025c4f8805188b149f25962c0f25902002044f2436184f8775190474ef6e376c0f25a06b04706f1bc008047d4f8840184f8705170b142f68d45c0f24405a847d4f8840105f166018847002804bf0020c4f8840170bd00bf2de9f04182b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f75d121684ef66c62ccf2ff0291426ed194f8a81179b394f8aa01c0070ad0d4f8680178b347f64c61c2f207010968401a002827dd47f24c45c2f2070555f8040c002854d042f29707c0f24c074af6e908002607f5437101a8c0f24b0801968847a8b1032084f8ab612860b4f8ac01c04702b0bde8f081d4f8b411c1b302b0bde8f0410847204602b0bde8f04133e0019841f60d56c0f24c06a861b04706f5af708047e861b847012084f8ab0103202860b4f8ac01c047d4f8bc01a0b10199d4f8b821b4f8ae71531a38bf8b1abb420ad2b4f8b01159b147f64c62c2f207021268101a884203d32046022100f078f802b0bde8f08100bfb0b590f8a841002c4cd044f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114614d14a68b2f5870f10d36ff35602012a0cd14a684ff65b63cff6ff737b449a4204d1d0f8b421002a18bf4a60002180f8a81190f8a911b1b1002180f8a91145f60a61c2f20701097869b947f24841c2f20701096839b14af68171c0f24b010546042088472846002c08bfb0bd0321bde8b04000e000bfb0b588b0044645f64d50c0f246000d468047012840f08f8008208df8010003208df8020010208df8030000208df80400ca208df8050006208df8060018208df8070047f26440c2f2070050f81c2c94f8a811002a18bf0122002945f60961c2f207014b7818bf0232002b94f8ab3118bf0432002b4ff0410318bf08328df808304c238df8093001238df80a308df80b508df80c2050f8182c4ef605758df80d200268c0f24705130a8df80e208df80f30130c120e8df810308df811204268130a8df812208df81330130c120e8df814308df8152002698df81620120a8df8172009788df8181081688df819106821c2f2000109688df81a10090a8df81b1047f64c61c2f207010a68130a8df81c208df81d30130c120e8df81e308df81f2000680df10102c4f8b80108680921c4f8bc0101201f23a84708b0b0bd00bf2de9f04f83b0022902d24ff0ff3504e1cde901100120cff680704ef66c644ff29c564ff6005bccf2ff04c2f229064ff6ff77851ccff6ff7b4ff000084ff0010907e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f00ea0501b1f1005f03d10168a14200f0ad804ff44070fff739fb0028c5d059464218403182f8008382f8018382f8028382f8038382f8048382f8058382f8068382f8078382f8088382f8098382f80a8382f80b8382f80c8382f80d8382f80e8382f80f8382f8108382f8118382f8128382f8138382f8148382f8158382f8168382f8178382f8188382f8198382f81a8382f81b8382f81c8382f81d8382f81e8382f81f8382f8208382f8218382f8228382f8238382f8248382f8258382f8268382f8278382f8288382f8298382f82a8382f82b8382f82c8382f82d8382f82e8382f82f8382f8308382f8318382f8328382f8338382f8348382f8358382f8368382f8378382f8388382f8398382f83a8382f83b8382f83c8382f83d8382f83e8382f83f837ff47caf046080f80a908783c783078447848784c784078547858785c785078647868786c7860787478756e8001f51457ff446afbff35f8f46e8000121b156e8001f5145f8d03be7044645f64d50c0f24600804701281dd1029d6978022916d00129204607d000297ff40daf00210025fff766fe0fe0019f41f28831022f0ed14ff4fa730022012741f2883624e02046fff7e2fd0025284603b0bde8f08faa78052f11d3eb782e79072f43ea062310d395f805c0ae79092f4cea0627e4d3ee792d7a46ea052607e041f288364ff4fa7301e041f288360127642b98bf64238b4228bf0b4690f8a81180f8aa21a0f8ac31a0f8ae71a0f8b06171b147f24841c2f20701096800295ed001210446fff718fe0025c4f8bc51bee744f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114632d14a686ff00105b2f5870ffff483af0123cff680731a40012a7ff47caf4b684ff69912cff6ff727a44934214bfc0f8b431d0f8b431002b3ff46daf4a60012180f8a81199e74af60771c0f24b010420884720b105462046fff73efd5be70121204684f8a9118fe76ff0010553e700bf10b582b00c464df6cb61c0f24501884724b12178042901d1617809b102b010bda1780d29fad1e1780029f7d121790029f4d161790029f1d10028efd00168e029ecd10446fef746fa014620460029e5d004204ff6591e01904ff0000cc0f24e0e0020002100220e23cdf800c0f047204602b010bd3146fff7c3bf00bfb0b582b00c460546fef726fa0146002d4ff0000018bf002901d102b0b0bd47f2fc70c2f2070000684df6cb61c0f24501884790b10068e0284ff00000edd129784ff6591c0191c0f24e0c0021002223460090e047012002b0b0bd002002b0b0bd08b530460921fff7cbff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460a21fff7b7ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460b21fff7a3ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf10b589b38968012805d104460846fef713fa204628e039b30a794b798c79c97942ea032242ea044242ea016106291bd147f29121c0f24601044688470146204691b144f2e970c0f24400804701462046002918bf10bd45f2ca50c0f2000078440c21fff761ffd4e710bd00bf10b5059c4ff20340c0f2460080472146fff7c0ff10bd00bfb0b5c4b0044600284ff0000000f0c38089b20b2940f0bf80207838b96078092804d1a078612801d1e07810b1002044b0b0bdfef763f90028f8d045f64d50c0f24600804701281cd00228efd103208df8070001200df107058df80800a81c21460b22f9f725fe4af66104c0f24604f02029460d220023a047b0fa80f0400944b0b0bd4ff29c50c2f22900006801466ff39601b1f1005fc9d101684ef66c62ccf2ff029142c2d122797f21042a06dc012a0cdcbab1012a08bf00211ae0082a0bdc052a11d0082a08bf0a2112e0022a0dd0042a08bf01210ce0092a09d00a2a08bf0e2106e0092104e0022102e0032100e00b2180f8eb1210216a2580f8ec12a22180f8e652152580f8ee12062180f8e7521a2580f8ef120c2180f8e852132580f8f01252210822022380f8e95280f8f1124921012580f8e42280f8e53280f8ea2280f8ed3280f8f21280f8f35280f8f452217900f5397280f8f5126179192380f8f612a17980f8f712002180f8f812e17980f8f912217a80f8fa12617a80f8fb12a17a4ff2250480f8fc12c0f247040120e021a047284644b0b0bd00bf1fb5fff72fff002803d0bde81f4000207047bde81f4038b584b049f21f13c0f24703184710b5f0281bd1d1b1cab10c78032c16d10d2a1cbf0b2010bd487801281cbf0a2010bd45f64d50c0f246000c468047012803d1a01c0b21fff703ff002010bdbde81040fdf78bbd00bf4ff29c52c2f229021268bff35f8f13466ff39603b3f1005f0cd1d2f800c04ef66c63ccf2ff039c4502bf0023c2f8dc32c2f8e03247f2c942c0f24d02104700bf4ff29c50c2f229000068bff35f8f01466ff39601b1f1005f0bd101684ef66c62ccf2ff02914202bf0021c0f8dc12c0f8e01247f2d940c0f24d0000472de9f04184b0dde90a76022f16d340f64705a94212d08eb13578412d0ed175784e2d0bd1014602f0bf0012284ff00d0008bf002b0ed004b0bde8f0810c9d0097cde9016546f63916c0f24d06b04704b0bde8f081082feed3b078012802d1f078022803d90d2004b0bde8f08100294ed045f6f444c2f2060414f8040c814246d12068002843d048f22732c0f254020d46084602219047d0b324682088b8b36088a8b3a08898b3e08888b330797179b279f37940ea012040ea024050ea0368d1d04ff29c50c2f229000068bff35f8f01466ff39601b1f1005f17d101684ef66c62ccf2ff02914210d1f178a1b190f8dc22aa420cd1d0f8e022424508d101295bd1082fafd100f09cf875e0f07818b1082004b0bde8f081082fa4d1faf7b5f8c8b10646d0f868010028f1d047f64c61c2f207010968401a0128e9db96f8dc0268b196f8dd02032809d00628e0d1304600f077f8dce7112004b0bde8f08146f63917c0f24d0707f53a60804707f539622846214690470120002786f8dd020df10f02304600210123a6f8de7286f8dc52c6f8e0828df80f70adf80c7000f07ff861884af2bd6603abc0f24c0628460222b047384604b0bde8f081a7f1510189b24ff6b87291424ff00d0119d390f8dd02032808467ff430af334613f8080fa7f108018ab280b9052a0ed9a1884af2bd67c0f24c072846b847002004b0bde8f081084604b0bde8f08101280dd1042a0bd3981810f8021c00297ff425af10f8010c00287ff420afe0e702284ff00d0008bf062a7ff401af707b01283ff614afd4e700bf90f8dd120a1f022a38bf7047022905d0012907d1042180f8dd127047052180f8dd12704780b582b0052180f8dd120121adf8061045f6f441c2f2060190f8dc0209684af2bd6c49880df10603c0f24c0c0222e04702b0bde8804070472de9f04fc7b0054690f8dc0203920591d8b145f6f041c2f206010978884214d148f22732c0f2540202211c46904760b195f8dc0240f66502c0f255020021904718b100690088172802d247b0bde8f08f033880b2f42828bff42007af0b3807f101084ff000094ff0000a01900495029414e000bf0699cde94509adf810112121adf8121143f23d5144a8c0f255018847049d029c1ffa8af0a042d6d2a4eb0a01019a1ffa81fbb5f8de1241235a45b8bf93468df81c304e234a1c8df81d300123a5f8de22d5f8e0228df81e30059b1ffa8af08df81f30130a8df82410090a8df820208df82130130c120e8df82510b0fa80f18df823204a091ffa8bf111fa8afaa2458df8223008bf0232bbf1000f8df826202fd001390329039901eb000c01d2002115e04ff6fc700bea00020cf10103002100bf5e1816f8014c7d187078ec725c5cb67804318a422c736873ae73f1d1049d1bf003020dd01cf80160cb19012ade7207d00ceb01004178022a19731cbf8078587395f8dc020bf10b0406900b2010fa84f649f6cd713046c0f24c01884700283ff47eaf26f003031ffa84fc06f003025e426ff00303c1180433f518043581f8049081f8059081f8069081f80790f2d152b10344012a83f8049005d0022a83f8059018bf83f806900bf10e0202701b22442108230272220480f801908170c370417283723ff43dafbcf1040f01d2002214e047f6fc710cea0103002200bfb95c8618f17218f8021008eb020531736978ad78043293427173b573f0d11cf003033ff41fafbe5c00f10b01012b8e543ff418af561cbd5d022b8d553ff412af0232bb5c8b540de7f0b583b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f47d121684ef66c62ccf2ff02914240d194f8dc12e9b3002840d002888a423dd145f6f446c2f2060616f8041c8a4235d1316899b3356894f8dd12032919d8d4f8681139b147f64c62c2f207021268891a00290edc02218df80b100df10b020746204600210123fff791fe2046fff760fe38468278092a1bd141896b8894f8dd72994217d1032f3adc012f6ad0022f3dd004e018b181780939022901d203b0f0bd47f6ed31c0f24d0103b0bde8f040084794f8dd72032ff1d8a2f10d0102290cd2c178d1b302208df8040001aa204600210123fff75bfe03b0f0bd092a08bf032fdcd14189aa889142d8d1c078002800f0818002200df105028df80500e6e7052f04d0042fcad1c07805262fe0c078002848d0062084f8dd0202208df807000df10702204600210123fff730fe052fb5d02046fff7fdfd03b0f0bd4268002aadd00389a3f2012189b2b1f57e4fa6d34089082b48d12988884245d12046012108234ce0c0780526002808bf022650b102208df80a000df10a02204600210123fff705fe6988012294f8dc024af2bd67adf8082002abc0f24c07022284f8dd62b84703b0f0bd052f15d1002094f8dc523668c4f8dc02c4f8e00247f2c944c0f24d0404f11000804728463146224603b0bde8f0401047032084f8dd0201208df806000df1060272e7e98888427ff457af2046022103e0204603210022002303b0bde8f040c3e52de9f04f8bb0044600284ff0ff3000f03f830d46002900f03b83217801f07f070a2f0edd102f27dc0b2f00f09282102f40f03282204629460bb0bde8f04ffff76db8052f00f03881072f00f024830a2f40f02282022dc0f01b834ef66c6b4ff29c57ccf2ff0bc2f229074ff6ff764ff0000a4ff0010858e0112f00f00483122f40f00a82b5f1010b01f08f8004f1010a00230021abeb03000628c0f0ce860aeb0304207962799d1d40ea0222abeb050082424ff0ff3000f2eb821af803306778a678e47843ea072343ea064343ea0463b3f5802f00f2dc82c3f580239a4200f2d782002a02eb050318bf01215b45d1d3002901f05a80fdf735fc002800f09d864ef66c674ff29c55ccf2ff07c2f229054ff6ff764ff0000800f045bc48f2a351c0f24501bff32f8f8847d7f80090bff35f8f38680321cff680710140b1f1005fbff35f8f03d10168594500f0b8834ff44070fef759fb002800f06f864ff60051cff6ff714218403182f800a382f801a382f802a382f803a382f804a382f805a382f806a382f807a382f808a382f809a382f80aa382f80ba382f80ca382f80da382f80ea382f80fa382f810a382f811a382f812a382f813a382f814a382f815a382f816a382f817a382f818a382f819a382f81aa382f81ba382f81ca382f81da382f81ea382f81fa382f820a382f821a382f822a382f823a382f824a382f825a382f826a382f827a382f828a382f829a382f82aa382f82ba382f82ca382f82da382f82ea382f82fa382f830a382f831a382f832a382f833a382f834a382f835a382f836a382f837a382f838a382f839a382f83aa382f83ba382f83ca382f83da382f83ea382f83fa37ff47cafc0f800b080f80a808683c683068446848684c684068546858685c685068646868686c6860687468757e8001f49457ff43fafbff35f8f00bf47e80001002900f0088357e8001f4945f6d031e74ef66c664ff29c57ccf2ff06c2f229074ff6ff7b4ff000084ff00109022d2cbf6078ff20059006e048f2a351c0f24501bff32f8f8847d7f800a0bff35f8f38680321cff680710140b1f1005fbff35f8f03d10168b14200f09f824ff44070fef777fa002800f0ee844ff60051cff6ff714218403182f8008382f8018382f8028382f8038382f8048382f8058382f8068382f8078382f8088382f8098382f80a8382f80b8382f80c8382f80d8382f80e8382f80f8382f8108382f8118382f8128382f8138382f8148382f8158382f8168382f8178382f8188382f8198382f81a8382f81b8382f81c8382f81d8382f81e8382f81f8382f8208382f8218382f8228382f8238382f8248382f8258382f8268382f8278382f8288382f8298382f82a8382f82b8382f82c8382f82d8382f82e8382f82f8382f8308382f8318382f8328382f8338382f8348382f8358382f8368382f8378382f8388382f8398382f83a8382f83b8382f83c8382f83d8382f83e8382f83f837ff47caf066080f80a90a0f81cb0a0f81eb0a0f820b0a0f822b0a0f824b0a0f826b0a0f828b0a0f82ab0a0f82cb0a0f82eb0a0f830b0a0f832b0a0f834b0a0f836b0a0f838b0a0f83ab057e8001f51457ff430afbff35f8f47e80001002900f0e18157e8001f5145f6d023e70f2f9b4600f0fc81a7f11300012840f2f781092f00f06c83082f40f0e483002a4ff0ff3000f0e880022dc0f0e58094f80190b9f1000f00f0bc8402264ff0010848f2482ab21caa4200f2ab84a019a15d407851ea002100f0a4848e18ae4200f2a084a05cde4600f07f07153fffb2fd2f16d8c0b20f280ed9a0f1830cbcf10c0f4ff0ff3000f2b88008fa0cf741f249031f4205d1b0e008fa00f717ea0a0febd0a01800227346fff75ffd002840f07984b9f10109c6d100f07cbc4ff29c56c2f229063468bff35f8f20466ff39600b0f1005f40f01e8620684ef66c67ccf2ff07b8424ff0000040f08780c4f8680184f8660184f86401d4f86c11c4f8600184f8650139b148f2a352c4f86c01c0f2450208469047d4f87802002818bf03f0bcf90021c4f87812206c40f2030942f68d48cff68079c0f24408a4f8441040b1c047206c08f166018847002804bf002020644bf63940c0f2510080473568bff35f8f05ea0900b0f1005f2bd12868b84228d195f877014ff0000ac5f880a188b149f25962c0f25902002044f2436185f877a190474ef6e37bc0f25a0bd8470bf1bc008047d5f8840185f870a150b1c047d5f8840108f166018847002804bf0020c5f884013068bff35f8f00ea0901b1f1005f40f038850168b94240f0348590f8a811002900f02985fef73bfc00f02bbd022d08bf112900f033810bb0bde8f08f4ef66c6a4ff29c57ccf2ff0ac2f229074ff6ff764ff0000b4ff0010806e048f2a351c0f24501bff32f8f8847d7f80090bff35f8f38680321cff680710140b1f1005fbff35f8f03d10168514500f06d834ff44070fef78df8002800f065834ff60051cff6ff714218403182f800b382f801b382f802b382f803b382f804b382f805b382f806b382f807b382f808b382f809b382f80ab382f80bb382f80cb382f80db382f80eb382f80fb382f810b382f811b382f812b382f813b382f814b382f815b382f816b382f817b382f818b382f819b382f81ab382f81bb382f81cb382f81db382f81eb382f81fb382f820b382f821b382f822b382f823b382f824b382f825b382f826b382f827b382f828b382f829b382f82ab382f82bb382f82cb382f82db382f82eb382f82fb382f830b382f831b382f832b382f833b382f834b382f835b382f836b382f837b382f838b382f839b382f83ab382f83bb382f83cb382f83db382f83eb382f83fb37ff47cafc0f800a080f80a808683c683068446848684c684068546858685c685068646868686c6860687468757e8001f49457ff43fafbff35f8f00bf47e80001002900f0bd8257e8001f4945f6d031e790f84410059f61b1016c39b142f68d42c0f244020646084690473046002180f844100121002f40f04782032dc0f04482a078082800f291844bf63941c0f251019631884700f089bc164600f0affc002800f0cb82611c142fa5f1010200f0be82132f5b4640f0be8200f0fcfdbce26678002e00f04c81022e00f2b78240f04484062dc0f0b282a178e2782379677941ea022143ea072232290691079280f02e84322100f030bc6078002840f09e8245f64d50c0f24600804701284ff000007ff4bfaecde9070040f20830c0f210000690ff208df8280044f25220c0f20100099040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8272008d80721b0f5807f38bf03218df827108df8280040f2d260adf81c00052006ac8df81e00e01d09a90522f8f7cdfc4ff22507c0f247070120092122460c23b8470bb0bde8f08f48f2a3514846c0f24501bff32f8f88472c68bff35f8fd5f800900320cff6807000ea0900b0f1005fbff35f8f04d1d9f80000b84200f042824ff44070fdf719ff002800f02f8281464ff60050cff6ff7000bf09eb0001403081f8008381f8018381f8028381f8038381f8048381f8058381f8068381f8078381f8088381f8098381f80a8381f80b8381f80c8381f80d8381f80e8381f80f8381f8108381f8118381f8128381f8138381f8148381f8158381f8168381f8178381f8188381f8198381f81a8381f81b8381f81c8381f81d8381f81e8381f81f8381f8208381f8218381f8228381f8238381f8248381f8258381f8268381f8278381f8288381f8298381f82a8381f82b8381f82c8381f82d8381f82e8381f82f8381f8308381f8318381f8328381f8338381f8348381f8358381f8368381f8378381f8388381f8398381f83a8381f83b8381f83c8381f83d8381f83e8381f83f837ff47baf0120c9f8007089f80a00a9f81c60a9f81e60a9f82060a9f82260a9f82460a9f82660a9f82860a9f82a60a9f82c60a9f82e60a9f83060a9f83260a9f83460a9f83660a9f83860a9f83a6055e8000fa0427ff428afbff35f8f45e80090002800f07f8155e8000fa042f6d01be7002480f8434145f64d50c0f2460080470146204601297ff48cad43f2a310c0f256000bb0bde8f04f004708064ff0210158bf11218d42c0f05181002804f101060ad545f64d50c0f24600154680472a46022818bf04f111063079717950ea01244ff0ff301ebfb179f37951ea03253ff460ad317b737b41ea03218c427ff459adb17bf37b41ea03218d427ff452ad3078717840ea012804eb0800b0f5207f4ff0ff303ff646adb178f37841ea0327e919b1f5f07f3ff63dad317a737a41ea032904eb0901b1f5207f3ff633adb17af37a41ea0326a919b1f5f07f3ff62aad924600f0ddfa002800f0f98041463a464b46cde90145009602f0c6f9dbf800100f2909d80bebc1020131a2f80690168154819581cbf80010baf1000f40f0e78095e2f81e18bf0120bb1f18bf012318424ff0ff307ff4feac032dfff4fbac032f00f0b281062f7ff4f5ac00200021164602f006fb00f0a4fa002800f0c0800246601c691e4ff4f0774ff4a073009702f03dfc002800f0b380dbf800100f2900f2b3800bebc1020023d38013814ff4207353814ff4f07301319381cbf80010a3e0059f00210020002f3ff4b9ad012f19d1052d17d3a378581e06284ff000003ff6b9ace17803293ff6b5ac2279002a3ff4b1ac4bf63940c0f2510000f59a771846b84734e2022f05d14bf63940c0f2510080472ce2032f40f01381072dc0f01081a078e278217940ea02206579a67901284ff0010498bf204644f62062904228bf10464bf63942c0f2510202f5e272642928bf6421904747f63400c2f207000068002800f0048245ea0621012988bf0c4642f68d41c0f24401a1f1400221469047f5e10020022d2cbf6178ff21002800f0ee81024602294ff0000025d0012920d000297ff457ac4cf67e21caf21011907610837960108150609683d683168456849684d684168556859685d685168656869686d6861687568782f83c000bb0bde8f08f01209072c2e190720bb0bde8f08f5b4600f0f9fe01e001f07afb20b14ff0ff300bb0bde8f08f002e00f0b081584601f0baffabe1d9f86c0100286ed14ff48020fdf7d3fc0028e9d000210022831881544032b2f5802f59709970d970197159719971d971197259729972d972197359739973d973197459749974d974197559759975d975197659769976d976197759779977d97783f8201083f8211083f8221083f8231083f8241083f8251083f8261083f8271083f8281083f8291083f82a1083f82b1083f82c1083f82d1083f82e1083f82f1083f8301083f8311083f8321083f8331083f8341083f8351083f8361083f8371083f8381083f8391083f83a1083f83b1083f83c1083f83d1083f83e1083f83f109ad1c9f86c0100210aeb010006794579437846ea0525d9f86c611af8012087788c1d06eb0321c078114401eb074101eb00600aeb04012a46f8f7d5f9291959454ff00000e0d3fff78abb3a1f80460020b2fa82f2022d88bf012052091040084001284ff000007ff47aab4cf6cd42e91eccf6cc42a578a1fb0212b5eb920f88bf95082f4608f14603302d28bf3027e11c07eb870204461846f8f7a5f9002d88f8447088f8454000f0b480d8f8400038b94046f8f7fafe0028c8f8400000f0fc8042f68d41c0f24401a1f1400201219047d2e0080600f1d480082dfff415af07214ff0050c0123e71897f802804ff0ff30b8f1000f3ff433abfe78002e3ff42fab14f8039005914fea880101eb8903b3f5207f04913ff623ab97f801a0770007eb4a03b3f5f07f3ff61aab04eb0c0014f80c104078039241ea002081b2012002f024f900f0c2f800283ff4deae05990aeb8a020c4400ebc2104fea4803691a00eb49022046009702f056fa00283ff4ccaedbf800100f290dd84fea89020bebc106f280049a4fea4a03013133817281b781cbf80010039900297ff4beae6ce090f8a911002918bffdf70eff94f84301002684f8436158b145f64d50c0f246008047012804d143f2a310c0f256008047d4f83c0194f84251c4f8386184f84261a4f8406150b1c047d4f83c0108f166018847002804bf0020c4f83c010120a072ddb34af27367c0f246070021002200230024b84720460bb0bde8f08fb1f5fa6f02d94ff4fa6106910121044680f8431145f64d50c0f24600804701281dd143f2a315c0f25605a5f188008047022e08bf002801d058b910e04bf2110206a9c0f24b020220904740b10646a8473046002184f843110bb0bde8f08f00200bb0bde8f08f0c2dfff441aee078e17988427ff43cae2079217a88427ff437ae45f64d50c0f24600164680470523022808bf01230b214ff0090c324616e74bf639400024c0f2510088f844408047a3e72de9f04f81b04ef66c6b4ff29c554ff6005848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0b1804ff44070fdf7d5fa002800f02281414600bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff447afbff35f8f00bf45e8000121b155e8001fa142f8d03be7d0f87812002974d104464ff4163002f0bdfa014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8781200e00021084601b0bde8f08f00bf092a1cbf4ff0ff3070472de9f04f8db08a4683460693fcf74bfa002800f01b824ef66c684ff29c57ccf2ff08c2f229074ff6ff740026012507e000bf48f2a351c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168414500f0b3804ff44070fdf770f9002800f0ab804ff60051cff6ff7100bf4218403182f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382f8196382f81a6382f81b6382f81c6382f81d6382f81e6382f81f6382f8206382f8216382f8226382f8236382f8246382f8256382f8266382f8276382f8286382f8296382f82a6382f82b6382f82c6382f82d6382f82e6382f82f6382f8306382f8316382f8326382f8336382f8346382f8356382f8366382f8376382f8386382f8396382f83a6382f83b6382f83c6382f83d6382f83e6382f83f637ff47cafc0f8008085728483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff440afbff35f8f47e8000129b157e8001f4945f8d035e70020002800f03f819af800109af801209af802309af8037041ea022141ea034141ea07624ff6fe77c0f20307ba424ff0ff3100f28982d0f86c310846002b00f02481995c4ff0ff30002900f01e81134493f80190b9f1000f00f0178109fb01f09d1c4ff0000c4ff0000368b3a7f101089e18be4200f007819e462b441c78240904d00126a0421bd2fde000bf464500f0fa805c781cb10226a04211d2f3e00524cff6fc743444042cc0f0ed809c78db7854ea032400f0e7800426a042c0f0e380001b06eb0e03d3d19af804009af8052098469af806309af807609af80870cdf8009040ea022943ea0626b8060f2307f00f0403eae07000fb04f389204343db0a8df824300e2317f0200f08bf4ff00f0c08bf012304fb03f300fb03f34fead3238df825304ff00d0308bf022304fb03f300fb03f34fead3238df826304ff00c0308bf032304fb03f300fb03f34fead3238df827304ff00b0308bf042304fb03f300fb03f34fead3238df828304ff00a0308bf052304fb03f300fb03f34fead3238df829304ff0090308bf062304fb03f300fb03f34fead3238df82a304ff0080308bf072304fb03f300fb03f34fead3238df82b304ff0070308bf082304fb03f300fb03f34fead3238df82c304ff0060308bf092304fb03f300fb03f34fead3238df82d304ff0050308bf0a2304fb03f300fb03f34fead3238df82e304ff0040308bf0b2304fb03f300fb03f34fead3238df82f304ff0030308bf0c2304fb03f300fb03f34fead3238df830304ff0020308bf0d2304fb03f300fb03f34fead3238df831304ff0010308bf0e2363434343db0a8df832300cfb04f358434ff0010ec00ab8f1000fcdf814808df8330000f06f8107f010008eea1010002403904ff0000804950ce04ff0ff300db0bde8f08fdde90704049d04440598844280f05881285d0894020901d0012313e0059a171b012f00f04d812b195a780ab1022309e0042fc0f045819f78db7857ea032200f03f810423079310f00f0309a810f803a018bf012303984fea0a1e40ea030c12f00300474600f08080b8fbf1f300259f19b7f1ff3fc8bf012505ea0c05012d1dd103fb11834b44dd09042d17d87d090e2d14d807eb87050beb85155f08ec5ddb076ff00f03019408bf0f230290504608bf7046019c234003430298eb55012808f1010751d0b7fbf1f300249d19b5f1ff3fc8bf012404ea0c04012c1ad103fb11734b44dc09042c14d86c090e2c11d805eb85040beb84145d08db070290605d6ff00f03574604bf0f237746034002983b436355022808f1020726d0b7fbf1f300259c19b4f1ff3fc8bf012505ea0c05012d18d103fb11734b44dd09042d12d865090e2d0fd804eb84040beb84145d08db07605d6ff00f03574604bf0f23774603403b43635508f10307531e032b904405d246e700bf0437b8453ff442afb7fbf1f40025a319b3f1ff3fc8bf012505ea0c05012d18d104fb11744c44e509042d12d85d090e2d0fd803eb83030beb831365085a5de4076ff00f04504604bf0f2470462240104358557c1cb4fbf1f50020ab19b3f1ff3fc8bf012000ea0c00012819d105fb114000eb0904e009042812d858090e280fd803eb83000beb80106208835ce4076ff00f04554604bf0f24754623402b438354bc1cb4fbf1f50020ab19b3f1ff3fc8bf012000ea0c00012819d105fb114000eb0904e009042812d858090e280fd803eb83000beb80106208835ce4076ff00f04554604bf0f24754623402b438354fc1cb4fbf1f50020ab19b3f1ff3fc8bf012000ea0c0001287ff47baf05fb114000eb0904e00904283ff673af58090e283ff66faf03eb83000beb80106208835ce4076ff00f04554604bf0f24754623402b4383545de708460db0bde8f08f09eb01000099b0f5207f06eb0103a8bf4ff42070b3f5f07fa8bf4ff4f0730699484588bfb34203d800200db0bde8f08f0f680f2ff8d89a1ba0eb090001ebc7035881781ca3f806901e819a810860ebe70a2a3cbf4ff0ff3070472de9f04f95b017468a4605461c46fbf792fe002800f0ff809af8096006f10a0087424ff0ff3040f0f8809af800109af801209af802309af8037041ea022141ea034141ea07624ff68061c0f203018a4200f2e38010960e95cde90c429af808004ef66c644ff29c554ff60056ccf2ff04c2f229054ff6ff77cff6ff764ff0000b4ff001080f9007e000bf48f2a351c0f24501bff32f8f8847d5f80090bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168a14200f0b3804ff44070fcf78cfd002800f0ec8131464218403182f800b382f801b382f802b382f803b382f804b382f805b382f806b382f807b382f808b382f809b382f80ab382f80bb382f80cb382f80db382f80eb382f80fb382f810b382f811b382f812b382f813b382f814b382f815b382f816b382f817b382f818b382f819b382f81ab382f81bb382f81cb382f81db382f81eb382f81fb382f820b382f821b382f822b382f823b382f824b382f825b382f826b382f827b382f828b382f829b382f82ab382f82bb382f82cb382f82db382f82eb382f82fb382f830b382f831b382f832b382f833b382f834b382f835b382f836b382f837b382f838b382f839b382f83ab382f83bb382f83cb382f83db382f83eb382f83fb37ff47caf046080f80a808783c783078447848784c784078547858785c785078647868786c7860787478755e8001f49457ff444afbff35f8f45e8000149b155e8001f4945f8d039e74ff0ff3015b0bde8f08fd0f86c91109eb9f1000f00f039810f9c0f23a10604f00f0203eae171514389235943c90a8df844100e219af804c09af805709af806e09af8075014f0200f08bf012102fb01f103fb01f14fead1218df845104ff00d0108bf022102fb01f103fb01f14fead1218df846104ff00c0108bf032102fb01f103fb01f14fead1218df847104ff00b0108bf042102fb01f103fb01f14fead1218df848104ff00a0108bf052102fb01f103fb01f14fead1218df849104ff0090108bf062102fb01f103fb01f14fead1218df84a104ff0080108bf072102fb01f103fb01f14fead1218df84b104ff0070108bf082102fb01f103fb01f14fead1218df84c104ff0060108bf092102fb01f103fb01f14fead1218df84d104ff0050108bf0a2102fb01f103fb01f14fead1218df84e104ff0040108bf0b2102fb01f103fb01f14fead1218df84f104ff0030108bf0c2102fb01f103fb01f14fead1218df850104ff0020108bf0d2102fb01f103fb01f14fead1218df851104ff0010108bf0e2102fb01f103fb01f14fead1218df852104ff0000108bf0f21514359430024c90a002e8df853106ad00d994ff6fe7449440af10a0a4cea07284eea052b4ff0000cc0f203040d9104e010990cf1010c8c455ed01af80c104a1e1f2af5d34ab2202a4bdb0d9a02eb810111f8802c11f87f3c11f87e7c11f87d1c42ea032242ea074242ea0165a54238d819f805104ff0ff34a9b309eb050253788bb34ff6fe7402f1020e03fb01f60023c0f2030406e000bf0121be4221d3f61b0b44c5d05a19a2421bd00eeb03010f783f09f1d1671eba4213d04f781fb10221be42ecd20de00527cff6fc773a44042a07d38a78c97852ea012702d00421be42ddd24ff0ff34204615b0bde8f08f4ff0ff3015b0bde8f08f0c990122063100910f99002601f0100182ea1111cde9071a1af806101f2908d84144109f0b3901360024be428846f3d1dde7d0f86c214ff0ff34002ad7d00d9b03eb810111f8803c11f87f7c11f87e5c11f87d1c43ea072343ea054343ea01614ff6fe73c0f203039942c0d812f801e0bef1000fbbd053185f78002fb7d007fb0ef2ddf83890002acde9027600f0ac819d1c00244ff6fe736618c0f203039e42a3d02f193b781b0901d0012718e04ff6fe73c0f20303013b9e4296d07b780bb102270de00523cff6fc733344042bfff48cafbb78ff7853ea07233ff486af04279a42fff482afd21a3c44d3d1002c00f07b81002100220f91cde9054509e0dde90a12059c0a44ddf820a0069da24280f06b81a95c0b920e0901d0012212e0a71a012f00f06181ab185e780eb1022209e0042fc0f059819f78db7857ea032600f0538104220a9211f00f0111aa525c18bf0121079b0f9c19434fea021a16f00305a446099600f08c80b4fbfef7002607fb1e434344b3f1ff3fc8bf01260e40012e1fd117eb0b071cd4de09042e19d87e090e2e16d807eb870709eb871c5e081cf80670db076ff00f03019708bf0f230495154608bf5546019f3b402b43049d0cf80630012d04f1010c5ad0bcfbfef6002706fb1ec34344b3f1ff3fc8bf01270f40012f1fd116eb0b071cd4de09042e19d87e090e2e16d807eb870709eb871c5e081cf80670db076ff00f03019708bf0f230495154608bf5546019f3b402b43049d0cf80630022d04f1020c29d0bcfbfef5002705fb1ec34344b3f1ff3fc8bf01270f40012f1ad115eb0b0717d4de09042e14d87e090e2e11d807eb870709eb87175e08db0717f806c06ff00f03154604bf0f23554603ea0c032b43bb5504f1030c099d6b1e032b2c440f9407d237e700bf0f9b0cf1040c63453ff431afbcfbfef5002605fb1ec34344b3f1ff3fc8bf01260e40012e1bd115eb0b0518d4de09042e15d86e090e2e12d805eb850509eb85155e08db07af5d6ff00f03914604bf0f23d1463b4043ea0903ddf83890ab550cf10103b3fbfef5002605fb1e334344b3f1ff3fc8bf01260e40012e18d115eb0b0515d4de09042e12d86e090e2e0fd805eb850509eb85155e08db07af5d6ff00f03144604bf0f2354463b402343ab550cf10203b3fbfef5002405fb1e334344b3f1ff3fc8bf01240c40012c18d115eb0b0515d4dc09042c12d86c090e2c0fd805eb850409eb84145d08db07665d6ff00f03174604bf0f23574633403b4363550cf10303b3fbfef5002405fb1e334344b3f1ff3fc8bf01240c40012c7ff471af15eb0b053ff56dafdc09042c3ff669af6c090e2c3ff665af05eb850409eb84145d08db07665d6ff00f03174604bf0f23574633403b43635553e7029a08eb0e010beb0203b1f5207f0d4628eae872a8bf4ff42075b3f5f07fa8bf4ff4f07395421edd039e109f5b457ff606aeddf830e0def800400f2c3ff6ffada3eb0b0c009ba5eb020803ebc40523f83420621ca5f802b0a5f80480a5f806c0cef80020ebe5039e109fe8e5062a3cbf4ff0ff3070472de9f04fadf2944d9a46154688460f90fbf70dfa002800f0d68098f80510881d854240f0298447f65420c2f20700d0f800b0bbf1000f00f01f849bf80c009bf80d209bf80e309bf80f7040ea022040ea034040ea0769a9f1810010f1800f4ff0ff30c0f0b2809bf810209bf811309bf8137042ea03229bf8123042ea034242ea07620692813a12f5817f40f29e800029cdf828b000f09d8008f1060b002426aa4ff4e01c00270ae000bf05f1004042f8240001378f4204f1010480f08e801bf80750681e1e28f0d968b21f2801dd012075e0a5f1c2000beb07061d28a1eb07000fd8012800f0c88396f9010010f1410f00f3c28300f03f0365f38a1302201d465de005f0f003e02b24d10328c0f0b48396f9013013f1410f00f3ae8396f9020010f1410f00f3a883e02ddeb202d1a02ec0f0a283ed2d02d19f2e00f29d832b039bb206f03f0643ea861300f03f001d18032034e004284ff0ff3036d3a5f1f50313f1050f31d396f9013013f1410f2cdc96f9020010f1410f00f37e8396f9036016f1410f00f37883f02ddbb202d1902bc0f07283f42d02d18f2b00f26d830cea854e03f03f054eea053300f03f0043ea801006f03f0326aac518042000bf074442f8245076e74ff0ff300df2944dbde8f08f0021012007915ee0002c57d0a4f1010bcdf86090cdf810a000250df1780a5f46914608e026aaab4505f1010509f10409a7f1010739d052f825100029f3d400202b4687421dd009eb80067268b2f1ff3f18dc5a1c934514d0b268b2f1ff3f11dc9a1c93450dd0f268b2f1ff3f0adcda1c934506d032690433002a00f10400e0d400e000220a9853461aae009601f0f8f8002800f008831a980028c3d04af20d315046c0f24e018847bce7ddf810a0ddf828b0ddf860900020079404e00020ddf828b00790012098f804400f21a20604f00f0501eae271694389225143c90a8df8681001260e2198f800c098f801e098f8023098f8037014f0200f08bf0e2608bf012105fb01f102fb01f14fead1218df869104ff00d0108bf022105fb01f102fb01f14fead1218df86a104ff00c0108bf032105fb01f102fb01f14fead1218df86b104ff00b0108bf042105fb01f102fb01f14fead1218df86c104ff00a0108bf052105fb01f102fb01f14fead1218df86d104ff0090108bf062105fb01f102fb01f14fead1218df86e104ff0080108bf072105fb01f102fb01f14fead1218df86f104ff0070108bf082105fb01f102fb01f14fead1218df870104ff0060108bf092105fb01f102fb01f14fead1218df871104ff0050108bf0a2105fb01f102fb01f14fead1218df872104ff0040108bf0b2105fb01f102fb01f14fead1218df873104ff0030108bf0c2105fb01f102fb01f14fead1218df874104ff0020108bf0d2105fb01f102fb01f14fead1218df8751006fb05f102fb01f14fead1218df876104ff0000108bf0f2169435143c90a00284ff000008df877107ff4cdae43ea0720079a484403904cea0e290af10600571e00260df198080190cdf810a0029718e000bf4af20d311ea8c0f24e0188479df87c009df87d10dde90ab9029fdde9072640ea01200df1980801369642814400f0ff8158f82610b1f1ff3f1edd33469f421fd008eb83004268b2f1ff3f1adc0133bb4216d08268b2f1ff3f13dc0133bb420fd0c268b2f1ff3f0cdc0133bb4208d002690133002ae2d404e001f01f000b38d2e7002258461eab19ad009500f0b7ff002800f0c781199900290d9100f0ba819df880209df881309df87e009df87f1052ea03259df8822040ea01279df8833010979df884704fea024208969df8856042ea0362069b4fea074747ea06672b4403eb2743039fcdf82c90a7eb030609eb22491aaf149500f05e810902109b014400f0030809f101004ff0010c099009f102000ceb530305900d980c934b1e0e930b9b0130121415900ceb510003eb020e9a1acde91180a1eb08001890d0074ff0000a17904feace70169013960ae014980af1010a824512981599139e0144159100f029810aeb06014b090e2befd810980028ecd00e9801eb810103280f984ff0000600eb811e80f0848011980028ded0dde90c105feac67c0afb01084fea560018f8000000f00f03c1b208bf0b09b3fa83f0400910ea141f14d106eb0901c80904280fd848081ef80020fb5c02f00f0b02f0f0021a431aafc90708bf4bea03120ef8002011980128b1d0701c400818f80000bcf1000fc1b24fea111308bf00f00f03d8b2b0fa80f0400910ea141f14d109983118c80904280fd848081ef80020fb5c02f00f0b02f0f0021a431aafc90708bf4bea03120ef80020119802283ff486afb01c400818f80000bcf1000f00f00f01c2b208bf1109b1fa81f0400910ea141f7ff474af05983318d80904283ff66eaf58081ef80020795c02f00f0602f0f0020a43db0708bf46ea01120ef800205de7ddf8548007e000bf18980436b04208f102083ff471af18f8011c0023c9b2102938bf012313ea141f09eb060c16d14feadc13042b12d84fea5c0309091ef80320795c02f00f0b02f0f0020a431aaf5feac97008bf4bea01120ef8032018f8010c00f00f01b1fa81f0400910ea141f15d10cf10103d809042810d858081ef80020795c02f00f03179d43ea011302f0f002002d08bf42ea01030ef8003098f80000c1b210294ff0000038bf012010ea141f15d10cf10203d809042810d8080959081ef80120385c02f00f0302f0f002169d0243002d08bf43ea00120ef8012098f8000000f00f01b1fa81f0400910ea141f8ad10cf10303d80904283ff685af58081ef80020795c02f00f03179d43ea011302f0f002002d08bf42ea01030ef8003072e700bf109809eb00011498b1f5207f06eb000229eae970a8bf4ff42071b2f5f07fa8bf4ff4f072ddf810a081427ff70fae26eae6739a427ff70aaedaf800700f2f3ff605ae019dd21a091a05ebc70625f83700781c7380b180f280caf80000f6e59df87c009df87d10079a40ea012001e64ff0ff300df2944dbde8f08f00200df2944dbde8f08f2de9f04f83b04ef66c654ff29c564ff6005948f2a35bccf2ff05c2f229064ff6ff77cff6ff7900244ff0010ac0f2450b029002e0bff32f8fd847d6f80080bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0ae804ff44070fbf7f0fc002800f0d88049464218403182f8004382f8014382f8024382f8034382f8044382f8054382f8064382f8074382f8084382f8094382f80a4382f80b4382f80c4382f80d4382f80e4382f80f4382f8104382f8114382f8124382f8134382f8144382f8154382f8164382f8174382f8184382f8194382f81a4382f81b4382f81c4382f81d4382f81e4382f81f4382f8204382f8214382f8224382f8234382f8244382f8254382f8264382f8274382f8284382f8294382f82a4382f82b4382f82c4382f82d4382f82e4382f82f4382f8304382f8314382f8324382f8334382f8344382f8354382f8364382f8374382f8384382f8394382f83a4382f83b4382f83c4382f83d4382f83e4382f83f437ff47caf056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f41457ff448afbff35f8f46e8000121b156e8001f4145f8d03de70446fef73df9002c18bf002805d102990120487103b0bde8f08fc4f8600101254ff4f07084f8645101904ff420704af61f470090c0f2470700200021002200230026b84740b184f864610298c4f86061457103b0bde8f08f0298057103b0bde8f08f0024cde700bf2de9f04f87b0dde91065924643ea0102ddf848b02a43d20722d14fea550800eb510900eb5304abf1010700252846564588bf384600eb0a01304401eb810100eb800009eb811104eb80104246f6f7bef9013f781c05f10105e8d107b0bde8f08f4ff6fc722a4005f0030406926a1e002701920494cde902a005e000bf059f039801375f45e9d0fa435645059788bf0beb020707eb0a0202eb820200eb821cba1902eb8202042d00eb821e4ff00008c0f09c80ddf8049000bf40468b4288bf484600eb010200eb03044fea52001cf800706ff0010088ea000608f1010088bfa8196ff0020686ea080608f1020b88bf05eb060b6ff0030686ea080608f1030a4feac27288bf05eb060a07f00f06002a4fea540208bf3e091ef80270e40707f00f0407f0f007374408bf44ea06170ef80270421854081cf804401844d20704f00f064fea500208bf26091ef80240c00704f00f0004f0f004344408bf40ea06140beb01000ef8024042081cf802200beb0304c00702f00f064fea540008bf16091ef80020e40702f00f0402f0f002324408bf44ea06120ef800200aeb010042081cf802200aeb0304c00702f00f064fea540008bf16091ef80020e40702f00f0402f0f002324408bf44ea06120ef80020069808f104088045a9f104097ff46dafddf808a0109eddf848b0049c002c3ff446af6fea080047468b4288bf2f18781842081f441cf80220c0074fea57091ef8090002f00f0408bf140900f00f0200f0f0002044ff0708bf42ea0410049c0ef80900012c3ff423af08f10102a5eb08098b4288bfa9f102025418670802eb030ae4074fea5a041cf807701ef8042007f00f0002f00f0b02f0f00208bf380902445feaca77ddf808a008bf4bea00120498109eddf848b00ef80420049c02283ff4f6ae08f102008b4288bfa9f10300421854081cf8047000eb030cd2074fea5c021ef8020007f00f0400f00f0800f0f00008bf3c0920445feacc7708bf48ea0410049c0ef80200d2e600bf2de9f04f83b04ef66c664ff29c574ff6005a48f2a358ccf2ff06c2f229074ff6ff74cff6ff7a00254ff0010bc0f24508cde9010103e000bfbff32f8fc047d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168b14200f0ae804ff44070fbf772fa002800f00b8151464218403182f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382f8195382f81a5382f81b5382f81c5382f81d5382f81e5382f81f5382f8205382f8215382f8225382f8235382f8245382f8255382f8265382f8275382f8285382f8295382f82a5382f82b5382f82c5382f82d5382f82e5382f82f5382f8305382f8315382f8325382f8335382f8345382f8355382f8365382f8375382f8385382f8395382f83a5382f83b5382f83c5382f83d5382f83e5382f83f537ff47caf066080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff448afbff35f8f47e8000121b157e8001f4945f8d03de7019a029b01210172b2b3014631f81c2f9a422cd0c28b9a4229d0028c9a4226d0428c9a4223d0828c9a4220d0c28c9a421dd0028d9a421ad0428d9a4217d0828d9a4214d0c28d9a4211d0028e9a420ed0428e9a420bd0828e9a4208d0c28e9a4205d0028f9a4202d0428f9a4208d10121817603b0bde8f08f417203b0bde8f08f427a72b98288029b9a1a13b2b3f1ff3f05dd92b2022a24bf0122427601e00122027600224272029ac38882809a4284bf029ac28090f83c20029b21f81230511c01f00f0180f83c1003b0bde8f08f00bf2de9f04f89b0129f029003fb07f000294fea400b069300f0d58100200890029807924ff0010a4ff000084ff0000e002400264ff0000c0022009107e04ff0000c03244e4601328a4200f0c181bcf1000f40f0bb8110f80290022cefd0012c15d0ddf81cc0129b002c554640f095805fea191409f00f070596089700f09b80a34580f0a1804ff0010c002499e1ddf81cc0129bb9f1000f00f08980cb45c0f0828008980392069a5546abeb090b00eb001700010596cdf810b0019016e010f80810089e01f0f001314300f8081008f10108a9f101090125904524bf4ff000080ef1010eb9f1000f00f062819e4580f05a810eeb8e00002d0ceb8010dfd0a2eb0801b1eb590f88bf4fea590169b38c0800eb080b1cd000220bf10106a2eb840a6ff0030c00bf06eb0c020cf1040c0aeb0c040434d770177157719771f3d10cf10406dde9062c129b11f0030404d10ce0002611f0030408d0012c0bf8067004d05e44022c777018bfb770a9eb4109b9f1000f88440dd09045aed210f80810019c01f00f012143a9f10109002500f80810a8e74ff000099fe700bf56ea09260ed0b34580f088804ff0010c00244be74ff0000c022447e74ff0000c012401e14ff0010c002400263ee708980392069aabeb040b00eb00170001cdf810b0019015e000bf1af80800089900f0f00008440af8080008f10108013c0125904524bf4ff000080ef1010e002c00f0d2809e4580f0ca800eeb8e00002d0ceb801ae1d0a2eb0800b0eb540f88bf600880b381080aeb08091ed00022ab4609f10106a2eb81016ff0030c00bf06eb0c020cf1040c01eb0c050435d770177157719771f3d10cf10401dde9062c129b5d4610f0030604d10de0002110f0030609d0012e09f8017005d04944022e4f7018bf8f7000bfa4eb400480446cb19045afd21af80800019900f00f000843013c00250af80800aae700bf0024a1e708980392069aabeb060b314600eb00170001cdf810b00190059613e01af80800089e00f0f00030430af8080008f1010801390125904524bf4ff000080ef1010e00295ad09e4553d20eeb8e00002d0ceb801ae3d0a2eb0800b0eb510f88bf480880b384080aeb08091ed00022ab4609f10106a2eb84046ff0030c00bf06eb0c020cf1040c04eb0c050435d770177157719771f3d10cf10406dde9062c129b5d4610f0030404d10de0002610f0030409d0012c09f8067005d04e44022c777018bfb77000bfa1eb4001804479b19045b1d21af80800019c00f00f002043013900250af808000029add107e000bf0021a1e7aa464ff0010c002403e0aa4600244ff0000c0df1080b00999be80508059e3fe60120012106e04ff0010cb4fa84f08cf001014009bbfa8bf252091140084009b0bde8f08f014640f258304df69f22c2f20000c0f248021047b0b54ff2a054c2f229040146206846f23f52c5f28802b2eb500f40f258304df681124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf2de9f04188b04af2bb261c46c0f24e0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f081f0b500294ff6ff7e00f0af80012903d10022c90775d1a8e021f00102c2f1000c6ff0010241f221038418a578e7788eea05252cb26e0036b203ead43484ea450503ead6366c0086ea450624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead53585ea460686ea07273eb27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead636023286ea470703ead5360ceb0205023586ea470e95d10232c90733d0805c41f221038eea002001b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead23181ea400e1ffa8ef0f0bd2de9f04fcbb00646c07c8b468df80600b08954998df80700000a8df808008df80a10080af1898df80b00080a17468df80d000020012b8df805308df809b08df80c108df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f008df820008df821008df822004cd1307e96f81780a0f1090144f62510c2f29240a1fb0002891a02eb510100970df10500b8eb910f88bf4fea9108c8ebc80706f11a0400f1010a00f10509093021463a46f5f7fffa06f1210506f1280029460722f5f7f7fa284621460722f5f7f2fa20465146032207f10905f5f7ebfa06f11d0049460422f5f7e5faf07d02289cbf0130f075009fbb450cd0b8f1240f13d94ff0ff304bb0bde8f08fbb454ff00905f07509d14ef60b67abb20df10502c0f247070120f02115e002200df12304aab28df82300a01c0df105018df82470f5f7b9faa81c4af6610782b2c0f24607f02021460023b8474bb0bde8f08f2ab1a2fb0103002b1cbf0020704702fb01f0b7e50846abe570b50446d0f83c0142f665360d46c0f2440668b949f6e130cff6ff707844002122460023b0470028c4f83c0108bf70bd06f594718847d4f83c0106f1e8022946bde87040104700bf2de9f04f81b0934612788ab1022300bf0beb030717f8012c62b11bf8032062b17a784ab1ba780433002af1d1023b04e0002205e0013b00e0013303eb4302520042eae272931c02f1010c01eb810203f0030e23f0030a00eb8213a3f5a0784ff0ff3906e009f10109b9f10d0f08f5a0786bd009eb010202eb8202bcf1030f00eb821605d24ff0ff34bef1000f2ed1e9e70024474604e000bfa24507f1020721d0621cd209042a03d83a7802f0f0023a70a21cd209042a03d87a7802f00f027a70e21cd209042a03d87a7802f0f0027a700434e209042adfd86208b55c05f00f05b554d9e7013cbef1000fbbd0a21cd509042d08d85208b55ce7076ff00f0708bf0f273d40b554bef1010fabd0e21cd509042d09d85208b55ce7074ff00f0708bf6ff00f073d40b554bef1020f9ad0221dd509042d96d85208e407b55c6ff00f0408bf0f242c40b4548ce79bf80010cdf800b0002900f0ce80022041f2410a41f2420b41f2430808e0009a063012f8011f03330029009200f0bd80a1f17f0717f15f0ff1d340f65d42c0f2000201eb41017a4402eb81014ff20014a1f5c079cff6ff7402e014f52074ded0494619f9027bc509042d09d8b7f1ff3f06dc1a1992f8016f46f0f00682f8016f5ffa87fe5fea4e6208d5042d06d81a1992f8017f47f00f0782f8017f5fea8e620ad540f27d22904206d81a1992f8027f47f0f00782f8027f5feace6209d5b0f51f7f06d81a1992f8027f47f00f0782f8027f5fea0e724fea900c09d5bcf19e0f06d81a1992f8036f46f0f00682f8036f5fea4e720ad540f27a22904206d81a1992f8037f47f00f0782f8037f91f90110042d09d8b1f1ff3f06dc1a1912f80a7047f0f00702f80a70c9b24a0608d5042d06d81a1912f80a7047f00f0702f80a708a060ad540f27d22904206d81a1912f80b7047f0f00702f80b70ca0609d5b0f51f7f06d81a1912f80b7047f00f0702f80b700a0709d5bcf19e0f06d81a1912f8087047f0f00702f8087049077ff565af40f27a2188423ff660af191911f8082042f00f0201f8082057e701b0bde8f08f00bf2de9f04f81b0134681464a1c00f08f808e0a4ff0000b19f80b20002a00f0888109eb0b077a78002a00f00a81ba78002a00f00981fa78002a00f008813a79002a00f007817a79002a00f00681ba79002a00f00581fa79002a00f004813a7a002a00f003817a7a002a00f00281ba7a002a00f00181fa7a002a00f000813a7b002a00f0ff807a7b002a00f0fe80ba7b002a00f0fd80fa7b002a00f0fc803a7c002a00f0fb807a7c002a00f0fa80ba7c002a00f0fb80fa7c002a00f0fc803a7d002a00f0fd807a7d002a00f0fe80ba7d002a00f0ff80fa7d002a00f000813a7e002a00f001817a7e002a00f00281ba7e002a00f00381fa7e002a00f004813a7f002a00f005817a7f002a00f00681ba7f002a00f00781fa7f002a00f008810bf1200bbbf1600f7ff477af4ff0600b00e1002119f80120002a00f0af8109eb01056a78002a00f0b080aa78002a00f0b180ea78002a00f0b2802a79002a00f0b3806a79002a00f0b480aa79002a00f0b580ea79002a00f0b6802a7a002a00f0b7806a7a002a00f0b880aa7a002a00f0b980ea7a002a00f0ba802a7b002a00f0bb806a7b002a00f0bc80aa7b002a00f0bd80ea7b002a00f050812a7c002a00f04e816a7c002a00f04c81aa7c002a00f04a81ea7c002a00f048812a7d002a00f046816a7d002a00f04481aa7d002a00f04281ea7d002a00f040812a7e002a00f03e816a7e002a00f03c81aa7e002a00f03a81ea7e002a00f038812a7f002a00f036816a7f002a00f03481aa7f002a00f03281ea7f002a00f03081203160297ff479af32e10bf1010b74e00bf1020b71e00bf1030b6ee00bf1040b6be00bf1050b68e00bf1060b65e00bf1070b62e00bf1080b5fe00bf1090b5ce00bf10a0b59e00bf10b0b56e00bf10c0b53e00bf10d0b50e00bf10e0b4de00bf10f0b4ae00bf1100b47e00bf1110b44e00131f6e00bf1120b3fe00231f1e00bf1130b3ae00331ece00bf1140b35e00431e7e00bf1150b30e00531e2e00bf1160b2be00631dde00bf1170b26e00731d8e00bf1180b21e00831d3e00bf1190b1ce00931cee00bf11a0b17e00a31c9e00bf11b0b12e00b31c4e00bf11c0b0de00c31bfe00bf11d0b08e00d31bae00bf11e0b03e00e31b5e00bf11f0bb1f5205f4ff0010100931fd364234ff47a7442f2107700bf01eb81025200b6fbf2f50a2d11d301fb03f2b6fbf2f50a2d0bd301fb04f2b6fbf2f50a2d05d37943b6fbf1f2092ae7d800e011465846bbf15f0f98bf5f20c0f1000c1ceb0b0a53d0b6fbf1f74cf6cd4207f13003ccf6cc4209f80b300a29a1fb023547d31af1010342d007fb11674fead508b7fbf8fe48f21f520ef1300309eb0b06c5f2eb1273706429a1fb023434d31af102032cd00efb18776409b7fbf4f544f6d35205f13003c1f26202b370b1f57a7fa1fb022323d31af1030218d005fb14729b0941f25975b2fbf3f7cdf2b715a1fb055407f13005f57007fb132642f210720bf1040b91424fea5431a9d209e0834607e00bf1010b04e00bf1020b01e00bf1030b0020009b09f80b002de00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f020a7003d1002189f85f1001e000204870002119f80100002800f0a18009eb0105687800285ed0a87800285dd0e87800285cd0287900285bd0687900285ad0a879002859d0e879002858d0287a002857d0687a002856d0a87a002855d0e87a002854d0287b002853d0687b002852d0a87b002851d0e87b002850d0287c00284fd0687c00284ed0a87c00284dd0e87c00284cd0287d00284bd0687d00284ad0a87d002849d0e87d002848d0287e002847d0687e002846d0a87e002845d0e87e002844d0287f002843d0687f002842d0a87f002841d0e87f002840d0203160297ff498af44e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2905d849444ff02f00087040f09580002089f85f00581c00f094809d0a4ff0000e19f80e00002800f08d8109eb0e067078002800f00f81b078002800f00e81f078002800f00d813079002800f00c817079002800f00b81b079002800f00a81f079002800f00981307a002800f00881707a002800f00781b07a002800f00681f07a002800f00581307b002800f00481707b002800f00381b07b002800f00281f07b002800f00181307c002800f00081707c002800f0ff80b07c002800f00081f07c002800f00181307d002800f00281707d002800f00381b07d002800f00481f07d002800f00581307e002800f00681707e002800f00781b07e002800f00881f07e002800f00981307f002800f00a81707f002800f00b81b07f002800f00c81f07f002800f00d810ef1200ebef1600f7ff477af4ff0600e05e100204870581c7ff46caf002119f80100002800f0aa8109eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04b81107c002800f04981507c002800f04781907c002800f04581d07c002800f04381107d002800f04181507d002800f03f81907d002800f03d81d07d002800f03b81107e002800f03981507e002800f03781907e002800f03581d07e002800f03381107f002800f03181507f002800f02f81907f002800f02d81d07f002800f02b81203160297ff479af2de10ef1010e74e00ef1020e71e00ef1030e6ee00ef1040e6be00ef1050e68e00ef1060e65e00ef1070e62e00ef1080e5fe00ef1090e5ce00ef10a0e59e00ef10b0e56e00ef10c0e53e00ef10d0e50e00ef10e0e4de00ef10f0e4ae00ef1100e47e00ef1110e44e00131f1e00ef1120e3fe00231ece00ef1130e3ae00331e7e00ef1140e35e00431e2e00ef1150e30e00531dde00ef1160e2be00631d8e00ef1170e26e00731d3e00ef1180e21e00831cee00ef1190e1ce00931c9e00ef11a0e17e00a31c4e00ef11b0e12e00b31bfe00ef11c0e0de00c31bae00ef11d0e08e00d31b5e00ef11e0e03e00e31b0e00ef11f0e0122b3f5205f1ed364234ff47a7442f2107602eb82004100b5fbf1f00a2811d302fb03f1b5fbf1f00a280bd302fb04f1b5fbf1f00a2805d37243b5fbf2f00928e7d800e00a46f046bef15f0f98bf4ff05f08c8f1000c42f2107a1ceb0e074dd0b5fbf2f30a2a03f1300009f80e004cf6cd40ccf6cc40a2fb000641d3781c3dd003fb1253f608b3fbf6f409eb0e0504f13000687048f21f50c5f2eb10642aa2fb000130d3b81c29d004fb16334e09b3fbf6f4b2f57a7f04f13000a87044f6d350c1f26200a2fb000120d3f81c16d004fb1630890941f25974b0fbf1f3cdf2b714a2fb044603f13004ec7003fb11050ef1040e52454fea5632afd209e0c64607e00ef1010e04e00ef1020e01e00ef1030e002009f80e0001b0bde8f08f0f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f00087005d1002089f85f0001b0bde8f08f0020487001b0bde8f08f434d01ffff0000000000000052420100ff00000008031000000000000000000046616365636c61772f3232000000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a8900000000000000000000000", + "new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc1b04ff62c484ef66c694ff29c5b1446cff6ff78ccf2ff09c2f2290b4ff6ff75002701260993cde90a0106e048f2a351c0f24501bff32f8f8847dbf800a0bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0a6fa002800f0078241464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001f51457ff436afbff35f8f4be8000121b15be8001f5145f8d02be70b99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0caf9002800f02b8141464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb9427ff436afbff35f8f4be8000121b15be8001fb942f8d02be7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f09d810b98002861d00a980778a7f1170002285bd80b980c2840f086810a9e7078b178084300f08081f0780138022800f27b81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f01082172f40f02682002a40f06281fe2800f06d86812800f06086002840f0738651ea030040f0558100f0bdbe04460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0f58051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0e480824692e74ff0ff3737e147f6df40c0f24f008047002701240d97adf8387006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0c1804ff4757007f070f8002800f0b98041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000159b15be8001fb142f8d02be706f5a9718847ace6012110e700200a9999b10b9a8ab1097801f07f02b2f10b0618bf0126b2f11c0718bf01278a2924dc0b2926d01c2924d03fe009900ca804f097fa4ff0ff3701249df8390000280ca818bf4ff0ff3704f0d3fb054644b99df8380028b94af21f30c0f247004c3080470998002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010446884794f8641119b994f80413204669b14ff0ff3742f6f760c0f2440000f5807150468847384641b0bde8f08f09900ca806ea070404f055fa0a98017801f07f061e2e04d10b9908f076fb0746b5e71d2e01f07e0018bf1a2811d1dbf800100322cff680720a40b2f1005fbff35f8f0ad10a684a4504bf002281f8812303e0a6f115010129e9d90f2e0adc092e00f37381052e00f0ef82072e40f01c8300279ae0142e00f32182102e00f0eb82112e40f011830b98022841f0c6800a990878112841f0c1804878002841f0bd8045f64d50c0f24600804701284ff000077ff46daf40f20830c0f210003190ff208df8c00044f25220c0f201002f9040f2c960c0f24800cde93277804798b148f27e30c2f2070000880122c1b264298df8bf2008d80721b0f5807f38bf03218df8bf108df8c00040f2d260adf8c800052031ae8df8ca00f01d2fa90522fff7dbfa4ff22507c0f247070120092132460c23b84774e700294ff0ff3708bf002b7ff453af1328bff450af102a4ff0000138bf01211228b2fa82f218bf510901297ff443af00f0a9bc182f4ff0ff3708bf00297ff43aaf002b7ff437af352800f32c84002800f08184012800f08d84312840f07f84082abff428af00f08ebc48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f00f864ff4757006f071fe002800f0078641464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470af0121c0f8009081728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff435afbff35f8f4be80001002900f057855be8001fb142f6d028e70a2e00f088810b2e40f0a981dbf800500320cff680702840b0f1005fbff35f8f40f0f9872868002748457ff40baed5f8700342f68d48c0f244080028a5f8807318bfc0470021a5f8b013c5f8ac13c5f8681185f8661185f86411d5f86c01c5f8601185f86511c5f86c11c5f80013a5f86c1320b148f2a351c0f245018847d5f87802002818bf04f03bfb0026c5f87862d5f8680320b148f2a351c0f245018847c5f86863a5f86c63286ca5f8446040b1c047286c08f166018847002804bf002028644bf63940c0f251008047dbf800700320cff680703840b0f1005fbff35f8f2ed1386848452bd197f877010026c7f8806188b149f25962c0f25902002044f2436187f8776190474ef6e378c0f25a08c04708f1bc008047d7f8840142f68d48c0f2440887f8706150b1c047d7f8840108f166018847002804bf0020c7f88401dbf800000321cff680710140b1f1005fbff35f8f40f040860168494540f03c8690f8a811002900f0318607f085f800f033be152e00f0ec80162e40f0f0800b9801380228c0f0a4860a9a4ff0ff375078917840ea0120b0f5007f3ff653ad03210b9b01eb400199427ff44cad002700288df8c4708df8c5708df8c6708df8c7708df8c8708df8c9708df8ca708df8cb708df8cc708df8cd708df8ce708df8cf708df8d0708df8d1708df8d2708df8d3708df8d4708df8d5708df8d6708df8d7708df8d8708df8d9708df8da708df8db708df8dc708df8dd708df8de708df8df708df8e0708df8e1708df8e2708df8e3708df8e4708df8e5708df8e6708df8e7708df8e8708df8e9708df8ea708df8eb708df8ec708df8ed708df8ee708df8ef708df8f0708df8f1708df8f2708df8f3708df8f4708df8f5708df8f6708df8f7708df8f8708df8f9708df8fa708df8fb708df8fc708df8fd708df8fe708df8ff708df800718df801718df802718df803713ff4c8acd11c31ae012208944b78012b00f206860f7847ea0323db08f45c07f0070702fa07f7274240f0f784f45c013847ea0407f754089c01f10201e6d105f0c7fc002800f0e984002700f017bc0b980894022835d30a98407833e0dde90a0107f0c4f8dee40b980228c0f0d98500f091fe002800f0d4850a994d78002d00f09f83022d00f2cc8540f0458601460b982646062880f032854ff0ff37344679e40a980b990130013908f088f8bae41d2e40f029810a980b990130013908f092fdb0e4ff2000270124079006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757006f0dcfb002800f06b8341464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000121b15be8001fb142f8d02be790f84410ddf82880dde9076461b1016c39b142f68d42c0f244020546084690472846002180f8441001210b9a032a0ed36eb998f80200082800f25b854bf63941c0f25101963188470027fff769bb0b9a052ac0f0d780012e40f0d48098f802000027411e06293ff65bab98f8031003293ff656ab98f80420002a3ff451ab4bf63943c0f2510303f59a7398470027fff747bb1a2818bf1c2e40f0888405f05efb002800f0838400f03bfd814608f07affb9f1000f4ff0ff3718bf00283ff430ab804647f63400c2f20700d0f81806d9f874131b2ea0eb0100c9f87c0300f04a841a2e40f067840a9900254f1c0b994ff4f072a1f1010b4ff4a0714ff48053cde9375031a84ff42076cde931350590cde90121484639465a4643463995cde90355009609f0b1fa002840f040844ff48050cde93105d9f87c033946cde9380531a805900120cde903504ff4a07002904ff4f070019048465a4643464ff420773795009709f091fafff723bb36286dd0462868d0572855d1002e7ff4ffaa65e00c294ff0ff377ff4f9aab3f5005f5dd3fff7f4ba481e1f284ff0ff373ff6eeaac1f51630834251d9fff7e8ba002b7ff4e3aa421f18bf0122b0f1350318bf01231a40b0f1590318bf01231a40b0f1470318bf0123134208bf012937d0b0f1370218bf0122b0f1c10318bf01231a4208bf02292bd09f284ff0ff377ff4bfaa042924d0fff7bbba022e40f062814bf63940c0f2510080470027fff783ba022abff4adaa13e0002a7ff4a9aaa0f1fd0102290cd397287ff4a2aa08e06ff0780002427ff49caa02e0402abff498aa45f64d50c0f2460080470a990128c9784ff001004ff0010408bf022008424ff0000708d1fff784ba48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757006f0cff900283ff461aa41464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000121b15be8001fb142f8d02be74af21f36c0f247060546b04795f8640100287ff4a3a995f80403294600287ff49da90a98007817281bd0d1f8640342f68d47c0f2440768b947f61320c0f20000a7f594747844012100220023a047c5f8640340b3a7f140024ff47a719047294608bb01f206300c460a990c22fef7effc0998002184f805030120bff35f8f84f804034ff4f07001904ff4207006f5106400900020002200230027a04700283ff45fa985f8047306f14c0080474ff0ff37fff756b90b9a072ac0f06d81032e40f06a8198f8020098f8032098f8041040ea022098f8055098f8067001284ff0010698bf304644f62062904228bf10464bf63942c0f2510202f5e272642928bf6421904747f63400c2f207000068002800f0e38245ea0721012988bf0e4642f68d41c0f24401a1f14002314690470027fff7eab8002780f8437145f64d50c0f24600804701287ff4dfa843f2a310c0f256008047fff720b9ddf82880dde907640021002055e500200b9902290a992cbf4978ff21002800f0b08202294ff000072cd0012926d000297ff4bea84cf67e21caf2101187760783cbf80410078147608583c583058445848584c584058545858585c585058645868586c5860587458780f83c7080f8b17380f8b073c0f8ac73fff79ab801218172fff796b88772fff793b848f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f007814ff4757006f00bf8002800f0b58041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470af0121c0f8009081728583c583058445848584c584058545858585c585058645868586c586058745875be8001fa1427ff435afbff35f8f4be8000100294fd05be8001fa142f7d029e7089c4ff0ff37fef7b8bf80460b98a14603284ff000073bd3301fb0fa80f04009084035d00b980a9a4cf6cd410338ccf6cc419478a0fb0101b4eb910f88bf8c082646302c28bf302608f14600d11c06eb8602fef736fb002788f8446088f84570b4b1d8f8400038b9404600f0c6fa0028c8f8400000f0768142f68d41c0f24401a1f140020121904700274c46fef775bf4c46fef772bfd0f86c110027002900f058814ff400624ff0ff3cb64605e02b681a44b2f5403f80f0c0808d18ab68b7eb532ff4d1dc081ef8044003f0070624fa06f6f6071ebf41f82370c5f808c0ef60e5e70a9fb878fa783b797f7940ea022043ea072232283190329280f0f5803220f9e090f8a911002918bf06f050fadbf800000321cff680710140b1f1005fbff35f8f1cd10168494519d1d0f8b813b1b14ff00041bff35f8fc0f8b4134af21f30c0f2470000f510674ff4f07001904ff4207600200021002200230096b84795f84301002685f8436158b145f64d50c0f246008047012804d143f2a310c0f256008047d5f83c0195f84271c5f8386185f84261a5f8406150b1c047d5f83c0108f166018847002804bf0020c5f83c010120002fa87200f0ca804af27366c0f246060021002200230027b047fef7d6be0b9803287ef4d2ae0a994ff4f07748788978424640ea01260020cde902004ff4a0700190484631464ff42073009708f067fc002800f0a9804ff0ff37fef7b6be0b9801287ef4b2ae09f57170bff35f8f50e8001f013140e80012002af8d10da94846012208f07afcfef7e8be4ff0000c4ff400634ff0000ec0f800c30de0bef1000fc7f804c00cbfc0f80033cef804303344b3f5403fbe463bd2cf18ba68ce58013201d07746f3e7f218140c022ce6d88d18ac680134e2d12c6826443e602c682244140c022cdad88d18ac680134d6d12c6826443e602c682244140c022cced88d18ac680134cad12c6826443e602c682244140c022cc2d88d18ac680134bed12c6826443e602c682244b2f5403fcfd3b5e7089c0027fef749beb0f5fa6f0846344604d94ff4fa603190084634460121064680f8431145f64d50c0f24600804701281cd143f2a318c0f25608a8f188008047022d074608bf002f01d057b90ee04bf2110231a9c0f24b020220904730b10746c047002086f84301fef717be0027fef714be089cfef711be701ca9f86c030027fef70bbe4bf639400027c0f2510088f8447080474c46fef700be2de9f04f81b04ef66c6b4ff29c554ff62c4848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0be804ff4757005f065fd002800f0b680414600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff43bafbff35f8f00bf45e8000129b155e8001fa142f8d02fe7002001b0bde8f08f70b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f845504bf6394c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04fcfb04ff29c57c2f229073c6840f2030a20464ef66c696ff39600cff6807accf2ff09b0f1005fbff35f8f40f0e3812068484540f0df8194f8510347f64c65c2f2070504f5417628b347f61800c2f20700016d4bf67c52c0f2720291421ad1406cc0b1d4f85c032968401a012802db04f095fc78b93046b8460df02efa50b142f69570c0f25b0080474746002084f8520384f8510394f8500368b1d4f860032968401a012802db04f079fc20b9002084f8500384f866013078bff35f8f70b194f84f83ac46b8f1000f01961cd094f8050394f81e13091a18bf012115e094f85103002840f0878194f85003002840f08281d4f86403002800f07d8142f68d41c0f24401884776e1012194f8070394f81133069094f8080394f81d23079094f8065394f80a0394f80b9394f80ce394f80db394f80ea394f80f63934235d194f8102394f81c339a422fd194f81b2396422bd194f81a23924527d194f81923934523d194f8182396451fd194f8172391451bd194f81623904217d194f8092394f815339a4211d194f81423079b93420cd194f81323069b934207d194f81223954203d111b994f805b3a9e00023012284f8203384f8233384f8243384f8253384f8263384f8273384f8283384f8293384f82a3384f82b3384f82c3384f82d3384f82e3384f82f3384f8303384f8313384f8323384f8333384f8343384f8353384f8363384f8373384f8383384f8393384f83a3384f83b3384f83c3384f83d3384f83e3384f83f3384f8403384f8413384f8423384f8433384f8443384f8453384f8463384f8473384f8483384f8493384f84a3384f84b3384f84c3384f84d3384f84e3384f81f2384f8215384f82203dcf8002047f61803170a84f8232384f82473170c120e84f826230822c2f2070384f8257384f84f231a6d4bf67c57c0f2720704f20631ba4204f212381bd15a6ccab140f6b803c2f200031a689ab1182d00f03784172d40f04384fe2804f2273c0d4600f06484002840f06e8401980df0aff9294602e0022084f8200340460c22fdf7dcfe94f805b394f84f8384f81eb345f64d50c0f246008047811eb1fa81f14fea511a012808bf4ff0020ab8f1000f59d004f21f3009a9059040f261400631c0f2470004910ca9023100254ff00309a0f5b84000240391029014e0039809a91a468df830908df831b0fdf7a7fe47f00802029ff0200ca90023b8470334e0b24045b1462fd2a8eb0400c7b20420032f28bf4f468df8240006983a468df8250007988df827a08df8260005988df8284050fa84f104988df829808df82a508df82b508df82c50fdf779feb81dda45c3b24ff00306c2d14ef60b670120f02109aac0f24707c9e7bff35f8f01994ff29c574ef66c6940f2030a0020c2f22907ccf2ff09cff6807a08703d68bff35f8f05ea0a00b0f1005f55d12868484552d105f56d700021bff35f8f50e8006f40e80012002af9d147f64c60bff35f8fc2f207000468b6f1004f0fd0b84604f0b1fa58b1002e00f0658195f8cc03320a002800f0fb8195f8cd1304e295f8cc03e8b147f61800c2f207000068b8b145f60960c2f207000478002040f261430c9009900ca909aac0f247032046984709990c9a43f69b53c0f24e0320469847002085f8ce0385f8cc03d5f8b80320b142f68d41c0f244018847d7f80080bff35f8f08ea0a00b0f1005f43d1d8f8000048453fd198f8500338b1002088f86401c8f860014fb0bde8f08f98f86401f8b1d8f86041e4b140f6b800c2f20000056808a802f0c6fc002d00f005834e464ff62c49284621464ff41632cff6ff794ff6ff770795fdf7c5fd00244ff0010b20e098f8660170b1d8f8680140b147f64c61c2f207010968401a002802f39884002088f8660140f6a120c0f2470080474fb0bde8f08f48f2a351c0f24501bff32f8f88474ff29c50c2f229000568bff35f8f0068bff35f8f00ea0a01b1f1005f03d10168b14200f0c5804ff4757005f07bf9002802f05484494600bf4218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470af4ff29c52066080f80ab08783c783078447848784c784078547858785c785078647868786c78607874787c2f2290252e8001fa9427ff431afbff35f8f42e8000151b152e8001fa942f8d026e795f8cc03002840f0db80cbe6817a002942f09283027e0021002a8df8301000f023824ff29c550caac2f22905535c002b00f08d8253185f78002f00f04b829f78002f00f04982df78002f00f047821f79002f00f045825f79002f00f043829f79002f00f04182df79002f00f03f821f7a002f00f03d825f7a002f00f03b829f7a002f00f03982df7a002f00f037821f7b002f00f035825f7b002f00f033829f7b002f00f03182df7b002f00f02f821f7c002f00f02d825f7c002f00f02b829f7c002f00f02982df7c002f00f027821f7d002f00f025825f7d002f00f023829f7d002f00f02182df7d002f00f01f821f7e002f00f01d825f7e002f00f01b829f7e002f00f01982df7e002f00f017821f7f002f00f015825f7f002f00f013829f7f002f00f01182db7f002b00f00f82203160297ff47baf2ee20120a5f8cc0340f20220a5f8d0030220002185f8cf03c5f8c01302f00103c6f300208b42c6f34b2206d123b395f8d233f7b2bb421fd00ce05bb995f8cf13a5f8ca2385f8d013022185f8d113c5f8bc4311e095f8cf3385f8d03385f8d163a5f8ca23c5f8bc4331b9012185f8ce13c6f34951a5f8c813002804bf002185f8ce1385f8cd0385f8d26395f8ce13b9b195f8640160b1d5f8c403bff35f8fb5f8c8136ff39f20884205d195f8ce1339b195f8cf632fe0002085f8ce03c5f8bc43d5f8bc13b5f8ca0395f8d163611a814220d28902b1fbf0f04ff4406395f8d01300fb00f2a3eb40005043000d721a00fb02f34ff62c47002b4ff40073cff6ff7748bf07f5ea7310fb0230c21700eb925001eb9026002147f61800c2f207000068e0b1d5f8c003f4b2a0421ad0002040f261430c9009900ca909aac0f247032046984709990c9a43f69b53c0f24e0320469847c5f8c04395f8ce1302e00020c5f8c003d5f8b80385f8cf6311b14ff47a7108e095f8d113f2b28a424ff0280108bf4ff47a7142f68d42c0f24402403a904795f8ce034746a0b13868bff35f8f00ea0a01b1f1005f42f044820168494542f04082002180f86411c0f860114fb0bde8f08f95f8cd0300287ff451adf0b20228e2d140f6b800c2f2000001680029dbd001684ff416322139002301f8403f403a01f81f3c01f81e3c01f81d3c01f81c3c01f81b3c01f81a3c01f8193c01f8183c01f8173c01f8163c01f8153c01f8143c01f8133c01f8123c01f8113c01f8103c01f80f3c01f80e3c01f80d3c01f80c3c01f80b3c01f80a3c01f8093c01f8083c01f8073c01f8063c01f8053c01f8043c01f8033c01f8023c01f8013c4b708b70cb700b714b718b71cb710b724b728b72cb720b734b738b73cb730b744b748b74cb740b754b758b75cb750b764b768b76cb760b774b778b77cb7781f820309cd100684ef217310c904ff416300d900ca8c0f247018847474666e7002088f86401c8f8600188f86601012088f8650140f6a120c0f24700804702f09eb901980d460cf0e7fe10b302304ff0030008bf022084f820032946fff7cebb84f85003dcf800204ef660231a44c4f86023b8b101980d460df0acf80df080f8294660b9fff7babb4ff29c55c2f2290595e00df075f8002829463ff4afab0320fff7aabb002084f86601fff7a7bb18684a4600eb0b20704400eb0a4000eb06616046fdf77bfa0ee04eea0b2141ea0a4141ea066361464a460cf074fe00281cbf032084f8200309f1080084f84f032946fff784bb01313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04f235d29937014d052235c29d37010d044235b2913710cd045235a29537108d052235929937104d020235829d37141f0058600218df88f100121427e002a00f0c28000220cab9f5c002f00f0a0809f187e78002e5ed0be78002e5dd0fe78002e5cd03e79002e5bd07e79002e5ad0be79002e59d0fe79002e58d03e7a002e57d07e7a002e56d0be7a002e55d0fe7a002e54d03e7b002e53d07e7b002e52d0be7b002e51d0fe7b002e50d03e7c002e4fd07e7c002e4ed0be7c002e4dd0fe7c002e4cd03e7d002e4bd07e7d002e4ad0be7d002e49d0fe7d002e48d03e7e002e47d07e7e002e46d0be7e002e45d0fe7e002e44d03e7f002e43d07e7f002e42d0be7f002e41d0ff7f002f40d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff053079f5411d013444b275e2a5f700cd049275d2a9f7008d050275c2adf7004d020275b2a1f7140f0f08100228df88f200131827e002a00f0be8000210caa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f0098100218df88f1001216a6846f23f53c5f28803b3eb520f40f08b80d20700f08d8000210caa535c002b00f0858153185f78002f00f0f9809f78002f00f0f980df78002f00f0f9801f79002f00f0f9805f79002f00f0f9809f79002f00f0f980df79002f00f0f9801f7a002f00f0fc805f7a002f00f0fc809f7a002f00f0fc80df7a002f00f0fc801f7b002f00f0fc805f7b002f00f0fc809f7b002f00f0fc80df7b002f00f0fc801f7c002f00f0fc805f7c002f00f0fc809f7c002f00f0fc80df7c002f00f0fc801f7d002f00f0fc805f7d002f00f0fc809f7d002f00f0fc80df7d002f00f0fc801f7e002f00f0fc805f7e002f00f0fc809f7e002f00f0fc80df7e002f00f0fc801f7f002f00f00d815f7f002f00f00b819f7f002f00f00981db7f002b00f00781203160297ff47baf1de14cf67e22caf210126a60002940f0198100210caa535c002b00f0dc8053185f78002f61d09f78002f60d0df78002f5fd01f79002f5ed05f79002f5fd09f79002f60d0df79002f61d01f7a002f62d05f7a002f63d09f7a002f64d0df7a002f68d01f7b002f69d05f7b002f6ad09f7b002f6bd0df7b002f6cd01f7c002f6dd05f7c002f6ed09f7c002f6fd0df7c002f70d01f7d002f71d05f7d002f72d09f7d002f73d0df7d002f74d01f7e002f75d05f7e002f76d09f7e002f77d0df7e002f78d01f7f002f79d05f7f002f7ad09f7f002f7bd0db7f002b7cd0203160297ff49aaface000211171f6e6013173e0023171e003316fe004316de0013184e0053169e0023180e0063165e003317ce0073161e0043178e008315de0053174e0093159e0063170e00a3155e007316ce000225a710fe60b314ee0083165e00c314ae0093161e00d3146e00a315de00e3142e00b3159e00f313ee00c3155e010313ae00d3151e0113136e00e314de0123132e00f3149e013312ee0103145e014312ae0113141e0153126e012313de0163122e0133139e017311ee0143135e018311ae0153131e0193116e016312de01a3112e0173129e01b310ee0183125e01c310ae0193121e01d3106e01a311de01e3102e01b3119e01f315f2931d84ff04f0353542dd00a444b235e29537028d020235d29937024d00021d17024e01c3104e01d3102e01e3100e01f315f2918d84ff04103535414d00a444c235e2953700fd05d2993700cd04f235c29d37008d043235b29137104d020235a29537141f0c38200218df88f1000210caf7a5c002a00f0a0807a185378002b5ed09378002b5dd0d378002b5cd01379002b5bd05379002b5ad09379002b59d0d379002b58d0137a002b57d0537a002b56d0937a002b55d0d37a002b54d0137b002b53d0537b002b52d0937b002b51d0d37b002b50d0137c002b4fd0537c002b4ed0937c002b4dd0d37c002b4cd0137d002b4bd0537d002b4ad0937d002b49d0d37d002b48d0137e002b47d0537e002b46d0937e002b45d0d37e002b44d0137f002b43d0537f002b42d0937f002b41d0d27f002a40d0203160297ff49aaf42e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2903d84ff077027a5403d100218df88f1002e0394400224a70c2684ff0000e17f80e10002900f0c48007eb0e035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002962d0197c002962d0597c002962d0997c002962d0d97c002962d0197d002962d0597d002962d0997d002962d0d97d002962d0197e002962d0597e002962d0997e002962d0d97e002962d0197f002962d0597f002962d0997f002962d0d97f002962d00ef1200ebef1600f7ff496af4ff0600e5be00ef1010e58e00ef1020e55e00ef1030e52e00ef1040e4fe00ef1050e4ce00ef1060e49e00ef1070e46e00ef1080e43e00ef1090e40e00ef10a0e3de00ef10b0e3ae00ef10c0e37e00ef10d0e34e00ef10e0e31e00ef10f0e2ee00ef1100e2be00ef1110e28e00ef1120e25e00ef1130e22e00ef1140e1fe00ef1150e1ce00ef1160e19e00ef1170e16e00ef1180e13e00ef1190e10e00ef11a0e0de00ef11b0e0ae00ef11c0e07e00ef11d0e04e00ef11e0e01e00ef11f0e01230a2a1ed364274ff47a7542f2107403eb83014900b2fbf1f60a2e11d303fb07f1b2fbf1f60a2e0bd303fb05f1b2fbf1f60a2e05d36343b2fbf3f10929e7d800e00b46f346bef15f0f98bf4ff05f0bcbf1000a0cad1aeb0e0c53d0b2fbf3f60a2b06f1300105f80e104cf6cd41ccf6cc41a3fb011447d31cf1010142d006fb1326e708b6fbf7f905eb0e0209f13001517048f21f51c5f2eb11642ba3fb011435d31cf102012dd009fb17666709b6fbf7f4b3f57a7f04f13001917044f6d351c1f26201a3fb015124d31cf1030524d004fb1767890941f25975b7fbf1f6cdf2b715a3fb055406f13005d57006fb117242f210710ef1040e8b424fea54330cada9d20ce0de460ae00ef1010e07e00ef1020e04e00ef1030e00e0de460cad002205f80e20a95c002900f0a080a9184b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff49aaf50e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a11d84ff07501a9540dd0a91873235e2a4b7008d020235d2a8b7004d070235c2acb7040f09d8000218df88f1002694ff0000a15f80a10002900f0c78005eb0a035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002965d0197c002965d0597c002965d0997c002965d0d97c002965d0197d002965d0597d002965d0997d002965d0d97d002965d0197e002965d0597e002965d0997e002965d0d97e002965d0197f002965d0597f002965d0997f002965d0d97f002965d00af1200abaf1600f7ff496af4ff0600a5ee00af1010a5be00af1020a58e00af1030a55e00af1040a52e00af1050a4fe00af1060a4ce00af1070a49e00af1080a46e00af1090a43e00af10a0a40e00af10b0a3de00af10c0a3ae00af10d0a37e00af10e0a34e000220a7162e70af10f0a2ee00af1100a2be00af1110a28e00af1120a25e00af1130a22e00af1140a1fe00af1150a1ce00af1160a19e00af1170a16e00af1180a13e00af1190a10e00af11a0a0de00af11b0a0ae00af11c0a07e00af11d0a04e00af11e0a01e00af11f0a01230a2a1ed364274ff47a7542f2107403eb83014900b2fbf1f60a2e11d303fb07f1b2fbf1f60a2e0bd303fb05f1b2fbf1f60a2e05d36343b2fbf3f10929e7d800e00b46d646baf15f0f98bf4ff05f0ecef1000942f2107c0cad19eb0a0b51d0b2fbf3f40a2b04f1300105f80a104cf6cd41ccf6cc41a3fb011747d31bf1010140d004fb1326ff08b6fbf7f405eb0a0204f13001517048f21f51c5f2eb11642ba3fb015135d31bf102052dd004fb17664c09b6fbf4f7b3f57a7f07f13001917044f6d351c1f26201a3fb015124d31bf1030519d007fb1464890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb11420af1040a63454fea57330cadabd20ce0f2460ae0f24607e00af1010a05e00af1020a01e00af1030a0cad002205f80a20a95c002900f0a080a9184b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff49aaf5ce001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a1dd84ff07501a95419d0a91873235e2a4b7014d020235d2a8b7010d074235c2acb700cd031235b2a0b7108d030235a2a4b7104d02023592a8b7140f0cb8500218df88f10d0f8ac234ff0000a15f80a10002900f0c48005eb0a035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002962d0197c002962d0597c002962d0997c002962d0d97c002962d0197d002962d0597d002962d0997d002962d0d97d002962d0197e002962d0597e002962d0997e002962d0d97e002962d0197f002962d0597f002962d0997f002962d0d97f002962d00af1200abaf1600f7ff496af4ff0600a5be00af1010a58e00af1020a55e00af1030a52e00af1040a4fe00af1050a4ce00af1060a49e00af1070a46e00af1080a43e00af1090a40e00af10a0a3de00af10b0a3ae00af10c0a37e00af10d0a34e00af10e0a31e00af10f0a2ee00af1100a2be00af1110a28e00af1120a25e00af1130a22e00af1140a1fe00af1150a1ce00af1160a19e00af1170a16e00af1180a13e00af1190a10e00af11a0a0de00af11b0a0ae00af11c0a07e00af11d0a04e00af11e0a01e00af11f0a01230a2a1ed364274ff47a7542f2107403eb83014900b2fbf1f60a2e11d303fb07f1b2fbf1f60a2e0bd303fb05f1b2fbf1f60a2e05d36343b2fbf3f10929e7d800e00b46d646baf15f0f98bf4ff05f0ecef1000942f2107c0cad19eb0a0b51d0b2fbf3f40a2b04f1300105f80a104cf6cd41ccf6cc41a3fb011747d31bf1010140d004fb1326ff08b6fbf7f405eb0a0204f13001517048f21f51c5f2eb11642ba3fb015135d31bf102052dd004fb17664c09b6fbf4f7b3f57a7f07f13001917044f6d351c1f26201a3fb015124d31bf1030519d007fb1464890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb11420af1040a63454fea57330cadabd20ce0f2460ae0f24607e00af1010a05e00af1020a01e00af1030a0cad079f002205f80a20a95c002900f0a080a9184b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff49aaf48e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a09d84ff07501a95405d0a91873235e2a4b7040f08780044600208df88f00384602212a460bf0a1fed4f870c272208df8300078208df8310020204ff0000b1ffa8cf28df832008df833b015f80b00002800f0c88005eb0b035878002866d09878002866d0d878002866d01879002866d05879002866d09879002866d0d879002866d0187a002866d0587a002866d0987a002866d0d87a002866d0187b002866d0587b002866d0987b002866d0d87b002866d0187c002866d0587c002866d0987c002866d0d87c002866d0187d002866d0587d002866d0987d002866d0d87d002866d0187e002866d0587e002866d0987e002866d0d87e002866d0187f002866d0587f002866d0987f002866d0d87f002866d00bf1200bbbf1600f7ff496af4ff0600b5fe004460020887078e70bf1010b58e00bf1020b55e00bf1030b52e00bf1040b4fe00bf1050b4ce00bf1060b49e00bf1070b46e00bf1080b43e00bf1090b40e00bf10a0b3de00bf10b0b3ae00bf10c0b37e00bf10d0b34e00bf10e0b31e00bf10f0b2ee00bf1100b2be00bf1110b28e00bf1120b25e00bf1130b22e00bf1140b1fe00bf1150b1ce00bf1160b19e00bf1170b16e00bf1180b13e00bf1190b10e00bf11a0b0de00bf11b0b0ae00bf11c0b07e00bf11d0b04e00bf11e0b01e00bf11f0b01230a2a1ed364204ff47a7742f2107503eb83067600b2fbf6f40a2c11d303fb00f6b2fbf6f40a2c0bd303fb07f6b2fbf6f40a2c05d36b43b2fbf3f6092ee7d800e03346d946bbf15f0f98bf4ff05f09c9f1000a42f2107e1aeb0b004fd0b2fbf3f50ca905f1300401f80b404cf6cd41ccf6cc410a2ba3fb014742d3441c3ed005fb1326fd08b6fbf5f70ca901eb0b0248f21f5107f13004c5f2eb115470642ba3fb011430d3811c29d007fb15656609b5fbf6f7b3f57a7f07f13001917044f6d351c1f26201a3fb011420d3033016d007fb1650a10941f25975b0fbf1f4cdf2b715a3fb055604f13005d57004fb11020bf1040b73454fea5633add209e0cb4607e00bf1010b04e00bf1020b01e00bf1030bddf81c9000220cac04f80b20a05c002800f0a080a118487800285ed0887800285dd0c87800285cd0087900285bd0487900285ad08879002859d0c879002858d0087a002857d0487a002856d0887a002855d0c87a002854d0087b002853d0487b002852d0887b002851d0c87b002850d0087c00284fd0487c00284ed0887c00284dd0c87c00284cd0087d00284bd0487d00284ad0887d002849d0c87d002848d0087e002847d0487e002846d0887e002845d0c87e002844d0087f002843d0487f002842d0887f002841d0c87f002840d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff02000a05411d0a01863215e2a41700cd072215d2a817008d063215c2ac17004d020215b2a017140f06d8100208df88f0037214fea1c7001eb1c71bcf1204f38bf00f13001ccf303608df8241000f137010a2838bf00f13001ccf303508df8251000f137010a2838bf00f13001ccf303408df8261000f137010a2838bf00f1300100208df828001f208df82710211811f81f2c002a00f0808011f81e2c002a7dd011f81d2c002a7bd011f81c2c002a79d011f81b2c002a77d011f81a2c002a75d011f8192c002a73d011f8182c002a71d011f8172c002a6fd011f8162c002a6dd011f8152c002a6bd011f8142c002a69d011f8132c002a67d011f8122c002a65d011f8112c002a63d011f8102c002a61d011f80f2c002a5fd011f80e2c002a5dd011f80d2c002a5bd011f80c2c002a59d011f80b2c002a57d011f80a2c002a55d011f8092c002a53d011f8082c002a51d011f8072c002a4fd011f8062c002a4dd011f8052c002a4bd011f8042c002a49d011f8032c002a47d011f8022c002a45d011f8011c002943d0215c002941d020307f287ff47baf60e01f383ae01e3838e01d3836e01c3834e01b3832e01a3830e019382ee018382ce017382ae0163828e0153826e0143824e0133822e0123820e011381ee010381ce00f381ae00e3818e00d3816e00c3814e00b3812e00a3810e009380ee008380ce007380ae0063808e0053806e0043804e0033802e0023800e001385f2821d8c0f15d0ec0f15e02c0f15f03c0f1600c2044002509ac675d4755b7b1ab4211d0661971784719797079b1aa420ad0b178b97051b1ae4505d0f178f97029b10435ac45e8d100208df88f0042f2941645f29c140caa48460e21c2f22006c2f213040bf050fb4ff6a42040f6b801cff6ff70c2f200010858a0426ad14ff6b020cff6ff700858b0f54d2f62d14ff6b420cff6ff700858a0425bd145f610634ff0000ec2f213034cf6394506f1040c00274ff0ff3263454cd8186830f003041ad09e4228bf0c2846d3f11a8c424ff0ff3143d8bc42394688bf2146c00703eb04001cbf0f46a644013d00f10403e2d130e00021417192e60421b7f5807f22d300223d46b5f5007f18d3b5f5806f17d3b5f5006f16d30901b2eb153f4fea1515f0d110e00022ca71fff734ba00219171fef73cbd00211172fef7fab9490002e0890000e0c900634504d1484207ea0002714603e04ff0ff324ff0ff3147f64c60c2f20700d0f8840006f1080342f6941c9842c2f2270c4cd142f610674ff0000bc2f2200746f6b9700cf1040e00244ff0ff3a4ff0ff3977453bd83e6836f0030515d0bc4528bf0c2e35d3aceb07039d4233d82346a54288bf2b46f6071cbf1c46ab447b19013803f10407e4d121e00420b4f5807f16d300262546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad30001b6eb153f4fea1515f0d104e0400002e0800000e0c000774504d1404204ea000ad94603e04ff0ff3a4ff0ff394ff6882040f6b807cff6ff70c2f200073b580cf10800834264d14ff69423cff6ff73fb58b3f5333f5cd14ff69823cff6ff73fb58834255d14ff29c5043f21063c2f229004ff0000bc2f2270342f6f935a0f1040ca0f1080e0024634542d81e6836f003071ad09e454ff0ff30069028bf0c2e3ad3aeeb030087424ff0ff3034d82046a74288bf3846f6071cbf0446bb44d819013d00f10403dfd123e00427b4f5807f17d34ff0000e2546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad33f01beeb153f4fea1515f0d104e07f0002e0bf0000e0ff00634505d17b4204ea03000690584602e04ff0ff300690064666208df8300072208df8310065208df832008df833002f208df834006d204b238df8350061208df8393069238df8360078208df83a30422356278df8370020208df83b303a238df83f7047270cac8df838008df83c308df83d004c238df840708df84200002720468df83e308df841308df843700bf043fb079de05d002800f0a080e019417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02037602f7ff49aaf64e001373ae0023738e0033736e0043734e0053732e0063730e007372ee008372ce009372ae00a3728e00b3726e00c3724e00d3722e00e3720e00f371ee010371ce011371ae0123718e0133716e0143714e0153712e0163710e017370ee018370ce019370ae01a3708e01b3706e01c3704e01d3702e01e3700e01f375f2f25d84ff02000e05521d0e01945215e2f41701cd076215d2f817018d065215c2fc17014d06e215b2f017110d048215a2f41710cd07521592f817108d06221582fc17104d02021572f017240f0f88000208df88f002046494652460bf06efa0020215c002900f0a08021184a78002a5ed08a78002a5dd0ca78002a5cd00a79002a5bd04a79002a5ad08a79002a59d0ca79002a58d00a7a002a57d04a7a002a56d08a7a002a55d0ca7a002a54d00a7b002a53d04a7b002a52d08a7b002a51d0ca7b002a50d00a7c002a4fd04a7c002a4ed08a7c002a4dd0ca7c002a4cd00a7d002a4bd04a7d002a4ad08a7d002a49d0ca7d002a48d00a7e002a47d04a7e002a46d08a7e002a45d0ca7e002a44d00a7f002a43d04a7f002a42d08a7f002a41d0c97f002940d0203060287ff49aaf5be001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281cd84ff02001215418d021184f225e284a7013d074225d288a700fd068225c28ca700bd065225b280a7107d072225a284a7103d0202259288a7128d100208df88f000cac069a204631460bf0a1f928461a2122460bf01ef8002088f86401c8f8600107984ef217310c904ff416300d900ca8c0f247018847012088f8660108a800f054f9c8f810004fb0bde8f08f0020c871d6e70021417207e700bf2de9f04f81b0009041f204024cf65560cef20002ccf2ac50c2f8ac0f4ef6fc50cef2000001684ef66c6641f08071016052f8040c4ff29c574ff62c4a48f2a358ccf2ff06c2f229074ff6ff7440f00100cff6ff7a00254ff0010bc0f2450842f8040c02e0bff32f8fc047d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168b14200f0ba804ff4757002f0b8fc002800f0f08051464218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470af066080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff43cafbff35f8f47e8000121b157e8001f4945f8d031e74169d1bb47f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f421ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94206d041f20401cef200010968c91a416141f20400cef2000000680099086001b0bde8f08f00bf2de9f04f81b041f20401cef200014ef66c674ff29c56d1f8009000684ff62c4bccf2ff07c2f229064ff6ff74cff6ff7b00254ff00108009007e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168b94200f0ba804ff4757002f080fb002800f00e8159464218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470af076080f80a808483c483048444848484c484048544858485c485048644868486c4860487448756e8001f51457ff438afbff35f8f46e8000121b156e8001f5145f8d02de74169009d61b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce047f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422fd041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94217d041f20401cef20001096844f6d357ca1ac1f26207a2fb07764261b00901284ff0010088bfb009994208bffa2001e0009dfa20a9eb0501b1fbf0f001b0bde8f08f2ab1a2fb0103002b1cbf0020704702fb01f0ffe7b0b54ff2a054c2f229040146206846f23f52c5f28802b2eb500f40f258304df681124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304df69f22c2f20000c0f248021047002858d1002956d0082a54d30b78aa2b51d18b79f02b4ed12de9f04f83b00b2a4ed3c878a2f10803834249d14878212848d10879012845d14879012842d1c87900283fd10c4614f8080f10f03c0f39d18818884610f8021c10f8010c164641ea0025a2f10a0081b2204609f0a3fe854228d145f64d50c0f246008047811eb1fa81f14f09012808bf0227002f00f03f81207887f00301084200f03b81012039464246334600f092fa0446002898f8080018bf06242ee14ff27143c0f24c0318470b2400e00a2445f64d50c0f246008047811eb1fa81f14d09012808bf0225002d00f011814ef66c6a4ff29c574ff62c480294ccf2ff0ac2f229074ff6ff74cff6ff7800264ff0010b06e048f2a351c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168514500f0bd804ff4757002f08df9002800f0de80414600bf4218463182f8d46382f8d56382f8d66382f8d76382f8d86382f8d96382f8da6382f8db6382f8dc6382f8dd6382f8de6382f8df6382f8e06382f8e16382f8e26382f8e36382f8e46382f8e56382f8e66382f8e76382f8e86382f8e96382f8ea6382f8eb6382f8ec6382f8ed6382f8ee6382f8ef6382f8f06382f8f16382f8f26382f8f36382f8f46382f8f56382f8f66382f8f76382f8f86382f8f96382f8fa6382f8fb6382f8fc6382f8fd6382f8fe6382f8ff6382f8006482f8016482f8026482f8036482f8046482f8056482f8066482f8076482f8086482f8096482f80a6482f80b6482f80c6482f80d6482f80e6482f80f6482f8106482f8116482f8126482f8136482f8146482f8156482f8166482f8176482f8186482f819647ff470afc0f800a080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff436afbff35f8f00bf47e8000121b157e8001f4945f8d02ae705eb450100eb011090f85d1201b300f513760024304629462a460323009409f027fe706840b14bf69b71c0f25d0188477068fff781fe74603068b475002818bffff77afe0020029c3060b060308200e0029c204603b0bde8f08f0624f9e700243842f6d0404631463a463b4600f076f9002818bf0446ece70b291dd3c278a1f10803934218d10278aa2a13d14278212a10d10279012a0dd14279012a0ad18279f02a07d1c2792ab9024612f8083f13f03c0f03d00a2070470b20704710b5084410f8023c10f8010c43ea0024a1f10a0081b2104609f014fd201a18bf0a2010bd002808bf7047b0b582b00446407cd0b1002520460323009509f0bafd606840b14bf69b71c0f25d0188476068fff714fe65602068a575002818bffff70dfe00202060a060208202b0bde8b040704700bf2de9f04f81b04ef66c654ff29c564ff62c498046ccf2ff05c2f229064ff6ff77cff6ff7900244ff0010a06e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0bb804ff4757002f00af8002800f0b38049464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470af056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff438afbff35f8f46e8000129b156e8001f5945f8d02de7002008eb480100eb0111002818bf01f5137001b0bde8f08fb0b5c4b0dc08202c03d94ff0ff3044b0b0bd0df107048df807008df80810a01c11461a461d46faf741fba81c4af6610582b2c0f24605f02021460023a84744b0b0bd00bf2de9f04f8bb04ef66c654ff29c564ff62c4948f2a35bccf2ff05c2f229064ff6ff77cff6ff7900244ff0010ac0f2450bcde90612cde9080303e000bfbff32f8fd847d6f80080bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0ba804ff4757001f0f0fe002800f0578449464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470af056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f41457ff43cafbff35f8f46e8000121b156e8001f4145f8d031e70999089d01eb410100eb011095f9081000f51376b1f1ff3f01f0030409dd374617f8110fd0bb3068002818bffff7eafb2fe3307cf0b1707ce0b10799099a002730460323009709f079fb706840b14bf69b71c0f25d0188477068fff7d3fb77603068b775002818bffff7ccfb00203060b06030823068002818bffff7c3fb00204ff480713060b0603182a978b081092006f11107f174b17434757076b07ca978884240f0db82317da14240f0d782069b411ca3f10b00727e02971e2828bf1e209042b17488bf7076981e84b200270a2c4ff00008c0f0e782092241f221090920039419e030684f46002818bffff78afbb089376001303774b760b081002c18bf4ff00608039cd146ddf814a00aeb0b0082b2a24280f0c782317c80b20429824600f28880aa5c00f1010a4b1c0129337405dd022906d0032970d1f28106e021b9727503e0338943ea02223281a24580f0aa8204296ed015f80a204ff0000c831c8f1c0129377406ddff290bd0032967460dd19a4652e000291cbf378947ea0222328100e072756746a34204d38be2f281a34280f08882022936d8ea5c00f1030acb1c0129337407dd022937d0fe2907d17275a2450cd376e231b9338943ea02223281a24504d36ee2f281a24580f06b8202292fd015f80a20031d41f00407fc29377408ddfd2909d0fe291cbf378947ea0222328103e079b9f28100e072756746a34280f0508209b19a4613e0ea5c00f1050a0520307401e09a466746f189308941ea0221f18128b1fff7c8fa0028306000f06a8233897089191a1ffa81fb1ffa8af1621a5a45b8bf93465fea0b4208d03268294410441ffa8bf2faf719f970893389584481b2994270817ff44faf0025cdf81080d6f800808df82b50707db8f1000f06930df12b0108bf88460f2806d8317d00f00302511ab1fa81f14d09000706d4317e707e814238bf30760fe000bf706840b14bf69b71c0f25d0188477068fff7a0fa7760707e30760120f082cdf814a09db1707db57d400714d4002d4ff0000400f0898106984ff6ff7381b2002900f00d8101295ed10020d2e0707d400753d4002478e14ff48030fff757fa002d804600f07081b07d002800f06c81b8f1000f00f068817568002d00f0008130890428c0f0608131680a1812f8043c002b40f0598112f8033c002b40f0548112f8023cff2b40f04f8112f8012cff2a40f04a814ff4803429606860c5f80c802c610028019000f067814bf69b70c0f25d0000f58572284600219047002840f033812969002900f02f8168688c420c46e7d1019a0c468242e3d125e14ff48030fff705fa044620e1069a4ff6fe701040a44642426ff0010000bf08eb0005ac78ed7883ea04231cb25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead73787ea430383ea05231fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead737023087ea430309ead5371518023587ea430395d1023000276446069ad20732d018f8000083ea002002b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead33282ea4003f0899ab2824270d1099b4046faf7cff800286ad141f2210a4ff00009012375e03820fff744f900285ed00546077047708770c770077147718771c771077247728772c772077347738773c773077447748774c774077547758775c775077647768776c776077747778777c77780f8287080f8297080f82a7080f82b7080f82c7080f82d7080f82e7080f82f7080f8307080f8317080f8327080f8337080f8347080f8357080f8367080f837704ff2e310cff6ff70784428624ff22f20cff6ff70784468624bf69b70c0f25d0045f6e46200f13c0c28460f21c0f27c023823e04740b32846fff707f94446706841f2210a40b14bf69b71c0f25d0188477068fff7faf877604ff000090323b77506980799099a089dddf8108087b23046009709f086f8002c214604463ff455ad0846fff7e2f850e5756073e6c4f58030b4f5803f444606902f60ef607ff646aecde70799099a002430460323009409f068f8706840b14bf69b71c0f25d0188477068fff7c2f874603068b475002818bffff7bbf80020387000203060b06030824ff00a0840460bb0bde8f08f287a4006f8d5307c08b302980078e0b10799099a002430460323009409f03bf8706840b14bf69b71c0f25d0188477068fff795f874603068b475002818bffff78ef800203060b06030824ff00b083068002818bffff783f800203060b0603082cae702980078e0b10799099a002430460323009409f00ff8706840b14bf69b71c0f25d0188477068fff769f874603068b475002818bffff762f800203060b06030824ff00608a7e700bf2de9f041f0284bd100294ff00b0000f07f80022a7cd345f64d50c0f246000e4617468047811eb1fa81f14c09012808bf0224002c4ff00a006ad07578e91ec9b2fe2938bfbde8f081307833463946032860d002282dd001281cbf0a20bde8f081ac4204bf0020bde8f0814808842884bf0b20bde8f081881e03f1020887b240463946fff7b7f9002869d006462846fff70dfa21462a46fff7e1f93046bde8f08145f64d57c0f2460707f5e27cbde8f0416047ac421cbf0020bde8f081a1f1210010f1160f48d36ff0480048432330c0b2242841d81a4612f8020f40f00207032f4bd19b796340032b47d10b2904d0032804bf0b20bde8f081881e4ef60b6783b2c0f247070120f021b847002818bf0620bde8f081ac422dd1a1f10c0010f1030f1ad31a4612f8020f042815d158796040032811d1d879a0f1310717f1300f0bd39b79984208d9a1f10807c01a874284bf0b20bde8f081cfe70b20bde8f081b07a204207d04046394622462b46bde8f041fff7b2ba0020bde8f0810a20bde8f0814ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f15d10a684ef66c63ccf2ff039a420ed1d1f8681159b147f64c62c2f207021268891a012903db0178042938bf7047002101707047b0b541f23154c0f252041d46a0474ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f08d10a684ef66c63ccf2ff039a4218bf002100e00021421c18bf002900d1b0bd95f84d20002afad091f84321002a08bfb0bd46f25863c2f206031a68132aeed8c2ebc20403eb04135b6801ebc2014ff40042c1f8c031a1f8c6212a7a01f5e071002a0fbf802248f20112ca808122ab7c33b1ab7d23b1022342f002028b71ca7195f820303bb195f8243023b1032342f004028b71ca7195f846304bb195f8443042f008020b7195f84530ca714b71b0bd48f27342c2f207021378002b14bf9378ff230b711378002b14bf5278ff224a71b0bdf0b589b048f66502c0f24b020c46054690474ff29c51c2f229010e68bff35f8f31466ff39601b1f1005f08d131684ef66c62ccf2ff02914208bf092d01d009b0f0bde1082c29fad896f843110029f6d045f64d51c0f2460107468847014638460129ecd1ff22079244f6435246f258610020cff60172c2f20601089006920b685a1e132a4fd8c3ebc30501eb05111a1fa1f5de7191f85451ad0618d4d51c42d091f8e450ad0616d4951c3cd091f87450ad0613d4551c36d00d79ad0610d4043aa1f5e071151da3f10403e3d12be05a1e01f5a87104e09a1ee03101e0da1e703106ebc203096893f9c721b2f1ff3f1adc03f5e0731e688e4215d11e798df81b605e798df81c609b798df81e200a0a8df820108df821200a0c090e8df81d308df822208df823100f228df8012010228df80220522208218df80420b4f5804f4ff080028df800108df8061038bf4ff48042b4f5001f6ff07f0604f07f0138bf0026802c28bf80318df807109df809509df80a108df8030004d29df808200223002415e0c4f3c610b4f5804f024403d20323012400200be0b4f5001f03d204230224012003e0052303240120002135468df8053004f108038df808208df809508df80a1070b96846a222c118c254062048700c208870c81c06a90c22f9f74ffc04f117034ff225066a46c0f2470601200821b047384609b0f0bd70b582b00546ff208df8040044f25220c0f20100009040f2c960c0f2480016460c46804798b148f27e30c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400301b28bf08280cd3d221281929550621417005218170033069460522f9f70bfc0834204602b070bd00bf024600284ff0ff3009d0022918bf70471078112801d1507810b14ff0ff30704710b586b045f64d50c0f24600804701284ff000003ed1cde9020040f20830c0f210000190ff208df8140044f25220c0f20100049040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8132008d80721b0f5807f38bf03218df813108df8140040f2d260adf80800052001ac8df80a00e01d04a90522f9f7b5fb4ff2250cc0f2470c0120092122460c23e04706b010bd4ff29c50c2f22900016808466ff39600b0f1005f16d108684ef66c62ccf2ff0290420fd1d1f8380160b147f64c62c2f207021268801a0028cebf01200020c1f838017047002070474ff29c50c2f22900026810466ff39600b0f1005f2ed110684ef66c61ccf2ff01884227d1d2f8680120b347f64c61c2f207010968401a0028c4bf01207047d2f86c1100200029c2f8680182f86601c2f86c01c2f80003a2f86c0308bf704780b548f2a352c0f24502084690470020bde88040704700207047f0b101684ef66c62ccf2ff02914218bf704790f8421199b110b54af273644ff0000cc0f24604864680f842c10120002100220023aef840c1a446bde810406047704700bf02464ff6ab70cff6ff7042f6653c7844c0f2440c00210023604700bf70b5002862d0044600796179a279e37940ea012040ea024050ea036007d00e2803d0032852d1032502e0042500e00225fff756ff002849d04ff29c50c2f22900066830466ff39600b0f1005f3ed130684ef66c61ccf2ff01884218bf70bd45f64d50a478c0f24600804701282ed1082086f86002032086f86102102086f86202b22086f86402062086f8650286f86602462086f8670243204ff0000e86f8680201204ef6057c06f5187286f863e286f8690286f86a5286f86b42c0f2470c09210d2386f86ce2bde87040604770bd00bf2de9f04f81b04ef66c694ff29c564ff62c480446ccf2ff09c2f229064ff6ff77cff6ff7800254ff0010a06e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168494500f0bc804ff4757000f072fe002800f0e18041464218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470afc0f8009080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff437afbff35f8f00bf46e8000121b156e8001f5945f8d02be7002780f857712a270821032380f85871042780f8541180f85531102380f8597180f85a1101214ff2250c074600f5aa7280f8563180f85b1180f85c31002c18bf0124c0f2470c012010210a2387f85d4101b0bde8f04f604701b0bde8f08f00bf80b582b04ff0010ccdf800c000f002f802b080bd2de9f04381b04ff29c57c2f229073f684ff000093e466ff39606b6f1005f07d13e684ef66c65ccf2ff05ae4208bfb946012811d1ddf820801c4615460e460746fff7c2fd2346044631462a463846002c18bfb9f1000f07d14af2736cc0f2460c01b0bde8f043604799f8420180b1b8f1010f7cd1d9f83c01002189f84211a9f84011a0b342f68d41c0f2440188472ee0b9f840010121013080b2012898bf0846a9f84001d9f83c0142f68d44c0f2440489f8421128b94846fff708fec9f83c0140b1a047d9f83c01a4f140024ff4c8719047a8b199f84201002848d0d9f83c01002189f84211a9f8401100b1a0474af2736cc0f2460c0120002100220023afe745f64d50c0f24600804701282fd1082089f84401032089f84501102089f84601002089f84701b22089f84801062089f8490189f84a01462089f84b01432089f84c01012089f84d01b9f840014ef6057c070a09f5a27289f84e8189f84f01c0f2470c012009210d2389f8507178e701b0bde8f08380b582b04ff0050ccdf800c0fff73eff02b080bd2de9f04f83b04468002c00f0db828368002b00f0d78204eb030b4ff000090027002614e047f64c63c2f207031b6845f69077c0f201073b44c6f838310ceb0a0300275b451c464ff0000680f0bb82a04618f9013b03f07f05bd40002b46ea050610d406f00703012b16dc0bb3012b40f0a982abeb0803082bc0f0a48204f10903dee700bfd84580f09d82182f07f107074446dcd996e200bf022b3ad0052b40f09182abeb0803042bc0f08c82631dc7e7d84580f0878294f90130002b04f10203bed55b4580f07e8294f90230b3f1ff3f04f10303b4dc5b4580f0748294f90330b3f1ff3f04f10403aadc5b4580f06a8294f90430b3f1ff3f04f10503a0dc5b4580f0608294f90530002b00f15b82a31d96e7d84580f0568294f9013004f1020c002b03f07f0a1ed5dc4580f04b8294f90270e31c07f07f05b7f1ff3f4aeac51a02dde0469c460ee05b4580f03b8294f9037004f1040c07f07f05b7f1ff3f4aea853a33dd9846abeb0c039a4500f22a82f308672b1dd0652b7ff460afbaf1060ffff45caf9cf80030462b7ff457af98f80230432b7ff452af98f80330012b7ff44daf4ff29c54c2f229040290cde9002132e0044660460e4651461746654600f035fcac4631463a46204637e7dc4580f0f98194f90470631d07f07f05b7f1ff3f4aea455aaddc5b4580f0ec8194f90570002f00f1e78104f1060c4aea077ab1e748f2a3513046c0f24501bff32f8f8847dde900210298ac462768bff35f8f2668bff35f8f33466ff39603b3f1005f07d133684ef66c65ccf2ff05ab4200f0ce804ff47570654600f097fbdde9002106460298ac46002e3ff4f1ae4ff62c43cff6ff7300bff418463384f8d49384f8d59384f8d69384f8d79384f8d89384f8d99384f8da9384f8db9384f8dc9384f8dd9384f8de9384f8df9384f8e09384f8e19384f8e29384f8e39384f8e49384f8e59384f8e69384f8e79384f8e89384f8e99384f8ea9384f8eb9384f8ec9384f8ed9384f8ee9384f8ef9384f8f09384f8f19384f8f29384f8f39384f8f49384f8f59384f8f69384f8f79384f8f89384f8f99384f8fa9384f8fb9384f8fc9384f8fd9384f8fe9384f8ff9384f8009484f8019484f8029484f8039484f8049484f8059484f8069484f8079484f8089484f8099484f80a9484f80b9484f80c9484f80d9484f80e9484f80f9484f8109484f8119484f8129484f8139484f8149484f8159484f8169484f8179484f8189484f819947ff470af4ef66c63ccf2ff0333600123b3724ff6ff734ff29c54b383f38333847384b384f38433857385b385f38533867386b386f38633877387c2f2290454e8003fbb427ff41cafbff35f8f44e8006323b154e8003fbb42f8d011e798f8043098f8057098f80650032b47ea052437dd052b58dc042b78d0052b7ff41faed6f868313bb147f64c67c2f207073f68db1b002b18dcd6f86c3186f86691c6f86c91c6f80093a6f86c936bb10c4648f2a35180461846c0f24501174665468847ac4621463a46404647f64c63c2f207031b6845f69077c0f201073b44c6f86831ede5012b3ff4dfad022b00f08780032b7ff4e5ad174688466546fff7e2fa03460298ac4641463a46002b3ff4d8ad96f84231002b3ff4d3ad304641f28831a6f8404108f048fd47e0062b4ad0072b7ff4c6ad80464df6db50c0f24a0016460f466446804703465d1ea446394632464046012d3ff6b4ad981eb0fa80f04009fff7a8fba446394632464046a8e5174688466546fff7a6fa03460298ac4641463a46002b3ff49cad96f84231002b3ff497adb6f84031a3427ff492add6f83c3186f84291002ba6f840913ff489ad42f68d411846c0f2440188470298ac4641463a467de5d6f86c31c6f86891002b86f86691c6f86c91c6f80093a6f86c933ff46fad0f4648f2a35104461846c0f24501164665468847ac463946324626e696f84231c6f83891002b3ff45aad0446d6f83c0165468846174686f84291a6f8409120b142f68d41c0f2440188474af273660120002100220023c0f24606b04720463a464146ac463be54ff2a513c0f2490303b0bde8f04f18477fb50646002806d1fff730fa002807d19de80f00064649f29b1cc0f24f0c60477fbd00bff0b583b01d46144609294ed106460f46f22d10d8a22160196155062141700b21817049f22871c0f20001033079440b22f8f7b4fd0e35204629464ff4807200f03df80546ff208df8080044f25220c0f20100019040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8072008d80721b0f5807f38bf03218df807108df80800f82d0cd8d221601961550621417005218170033001a90522f8f77cfd0835394630464ef60577c0f2470722462b46b84703b0f0bdf0b587b00c464ff29c51c2f229010d68bff35f8f29466ff39601b1f1005f52d129684ef66c63ccf2ff0399424bd14d218df8041043218df8051001218df8061095f8701195f871318df807108df8083095f87511b5f878318df80b108df80c30190ab5f87a318df80d10190a8df80f1095f8761195f8737195f874618df80e3095f877318df8101045f64d51c0f246018df809708df80a608df81130064617468847d5f87c3101468df81210190a8df81410190c3a4630468df813308df81510190ea02328e04d218df8041043218df8051001218df80610002100238df807108df808108df809108df80a108df80b108df80c108df80d108df80e108df80f108df810108df811108df812108df813108df814108df815108df816108df817300023111b8df8183028bf18290cd3c222011902550620487015208870c81c01a91522f8f7cdfc1834204607b0f0bd00bf2de9f04f81b04ff29c50c2f229000468bff35f8f20466ff39600b0f1005f0dd120684ef66c63ccf2ff03984206d194f8700118b1002a18bf002902d101b0bde8f08f94f877010028f8d0d4f880010028f4d047f64c69c2f20709d9f80030c01a0128ebdb94f87101012802d14ff0010b06e094f873b1bbf1010f18bf4ff0020b94f876510020eb078846adf80200adf8000013d0bbf1020f10d194f8750168b96b4641f281570df10201c0f25b07404615461a46b8472a4694f876511646b2f5c86f28bf4ff4c86606f1150a5046174600f050f80028b1d0b7f5c86f88bf45f08005d9f80010d4f87c2153230271120a42710a0a8171c2710a0c090e03704d234172a02143700123c17200218370c570027280f80ab0017394f87511bdf802204173bdf800108373c173090a0174110a8174310a017500f115014274c6740746084641463246f8f723fc4ff27d12c0f24702384651469047d4f87c0148f2a3510130c4f87c01c0f245013846884701b0bde8f08fb0b54ff2a054c2f22904216846f23f52c5f28802b2eb510f48f25f514cf67e25c0f24501caf2101518bf25608847002804bf691c2160b0bd10b301684ef66c62ccf2ff0291421bd190f87711c1b1d0f88011b1b147f64c62c2f207021368cb1a012b0edb1268d0f88401002808bf7047891a42f64d42c0f2440208bf01211047704710b5002149f25962c0f8801180f87711c0f25902002044f2436190474ef6e374c0f25a04a04704f1bc00bde81040004700bf2de9f04f81b00429c0f00b810c46014600784d2840f005814878432840f001818878012840f0fd804ef66c684ff29c57ccf2ff08c2f229074ff6ff7600254ff0010b8a4607e048f2a351c0f24501bff32f8f88475146d7f80090bff35f8f3868bff35f8f02466ff39602b2f1005f03d10268424500f0c0804ff47570fff766ff002800f0ce804ff62c425146cff6ff728318463283f8d45383f8d55383f8d65383f8d75383f8d85383f8d95383f8da5383f8db5383f8dc5383f8dd5383f8de5383f8df5383f8e05383f8e15383f8e25383f8e35383f8e45383f8e55383f8e65383f8e75383f8e85383f8e95383f8ea5383f8eb5383f8ec5383f8ed5383f8ee5383f8ef5383f8f05383f8f15383f8f25383f8f35383f8f45383f8f55383f8f65383f8f75383f8f85383f8f95383f8fa5383f8fb5383f8fc5383f8fd5383f8fe5383f8ff5383f8005483f8015483f8025483f8035483f8045483f8055483f8065483f8075483f8085483f8095483f80a5483f80b5483f80c5483f80d5483f80e5483f80f5483f8105483f8115483f8125483f8135483f8145483f8155483f8165483f8175483f8185483f819547ff470afc0f8008080f80ab08683c683068446848684c684068546858685c685068646868686c6860687468757e8002f4a457ff432afbff35f8f00bf47e8000222b157e8002f4a45f8d026e7ca78022a04dc012a0dd0022a79d00ce0032a0dd0042a08d190f8701129b101b0bde8f04faae00d2c06d201b0bde8f08f044600f045f963e090f87721aab1002149f2596280f87711c0f259020446002044f2436190474ef6e375c0f25a05a84705f1bc008047204651460a790446012a88bf002280f871214a7902f00303012b4ff0010298bf134680f873318f79012f88bf002780f87471cf79022f88bf002780f875710f7a4e7a47ea0627502f98bf5027b7f5f07f28bf4ff4f077a0f878718f7ace7a47ea062741f28836b74238bf3e46a0f87a61097b80f8702101f0030180f8761103f0010101eb530180f872110021c0f87c1100f009f8204600f03ef8204601b0bde8f04f6ae000bfb0b5044690f87601800758bfb0bd94f871014ef633750128c0f25a0507d105f1b000804705f58b71002088470ee005f5b670804794f87101012801d1002004e094f87301013818bf0120a8474ff67922cff6ff7249f201537a44c0f25903002044f243619847012084f87701b0bd00bf70b5044647f64c60c2f20700006894f8771145f69075c0f2010500292844c4f8800108bf70bdd4f8840142f66536c0f2440658b94ff6d130cff6ff707844002122460023b047c4f8840150b106f594718847d4f8840106f1e8022946bde87040104770bd70b5044600f5c475082084f88801032084f88901102084f88a01c22084f88c01062084f88d014d2084f88f01432084f89001012084f8910194f8700194f8711184f8920184f8931194f87501b4f8781184f8960184f89711080ab4f87a1184f89801080a84f89a0194f8760194f8732194f8743184f8991194f8771184f89b0145f64d500026c0f2460084f88b6184f8942184f8953184f89c118047d4f87c1184f89d01080a84f89f01080c84f8a001080e84f8a101a02084f8a36115264ef6057c84f89e1184f8a201c0f2470c012009212a461c2384f88e61bde87040604770b54ff29c50c2f229000468bff35f8f20466ff39600b0f1005f33d120684ef66c61ccf2ff01884218bf70bd94f877010025c4f8805188b149f25962c0f25902002044f2436184f8775190474ef6e376c0f25a06b04706f1bc008047d4f8840184f8705170b142f68d45c0f24405a847d4f8840105f166018847002804bf0020c4f8840170bd00bf2de9f04182b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f75d121684ef66c62ccf2ff0291426ed194f8a81179b394f8aa01c0070ad0d4f8680178b347f64c61c2f207010968401a002827dd47f24c45c2f2070555f8040c002854d042f29707c0f24c074af6e908002607f5437101a8c0f24b0801968847a8b1032084f8ab612860b4f8ac01c04702b0bde8f081d4f8b411c1b302b0bde8f0410847204602b0bde8f04133e0019841f60d56c0f24c06a861b04706f5af708047e861b847012084f8ab0103202860b4f8ac01c047d4f8bc01a0b10199d4f8b821b4f8ae71531a38bf8b1abb420ad2b4f8b01159b147f64c62c2f207021268101a884203d32046022100f078f802b0bde8f08100bfb0b590f8a841002c4cd044f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114614d14a68b2f5870f10d36ff35602012a0cd14a684ff65b63cff6ff737b449a4204d1d0f8b421002a18bf4a60002180f8a81190f8a911b1b1002180f8a91145f60a61c2f20701097869b947f24841c2f20701096839b14af68171c0f24b010546042088472846002c08bfb0bd0321bde8b04000e000bfb0b588b0044645f64d50c0f246000d468047012840f08f8008208df8010003208df8020010208df8030000208df80400ca208df8050006208df8060018208df8070047f26440c2f2070050f81c2c94f8a811002a18bf0122002945f60961c2f207014b7818bf0232002b94f8ab3118bf0432002b4ff0410318bf08328df808304c238df8093001238df80a308df80b508df80c2050f8182c4ef605758df80d200268c0f24705130a8df80e208df80f30130c120e8df810308df811204268130a8df812208df81330130c120e8df814308df8152002698df81620120a8df8172009788df8181081688df819106821c2f2000109688df81a10090a8df81b1047f64c61c2f207010a68130a8df81c208df81d30130c120e8df81e308df81f2000680df10102c4f8b80108680921c4f8bc0101201f23a84708b0b0bd00bf2de9f04f83b0022902d24ff0ff3510e1cde901100120cff680704ef66c644ff29c564ff62c4bccf2ff04c2f229064ff6ff77851ccff6ff7b4ff000084ff0010907e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f00ea0501b1f1005f03d10168a14200f0b9804ff47570fff72dfb0028c5d059464218463182f8d48382f8d58382f8d68382f8d78382f8d88382f8d98382f8da8382f8db8382f8dc8382f8dd8382f8de8382f8df8382f8e08382f8e18382f8e28382f8e38382f8e48382f8e58382f8e68382f8e78382f8e88382f8e98382f8ea8382f8eb8382f8ec8382f8ed8382f8ee8382f8ef8382f8f08382f8f18382f8f28382f8f38382f8f48382f8f58382f8f68382f8f78382f8f88382f8f98382f8fa8382f8fb8382f8fc8382f8fd8382f8fe8382f8ff8382f8008482f8018482f8028482f8038482f8048482f8058482f8068482f8078482f8088482f8098482f80a8482f80b8482f80c8482f80d8482f80e8482f80f8482f8108482f8118482f8128482f8138482f8148482f8158482f8168482f8178482f8188482f819847ff470af046080f80a908783c783078447848784c784078547858785c785078647868786c7860787478756e8001f51457ff43aafbff35f8f46e8000121b156e8001f5145f8d02fe7044645f64d50c0f24600804701281dd1029d6978022916d00129204607d000297ff401af00210025fff75afe0fe0019f41f28831022f0ed14ff4fa730022012741f2883624e02046fff7d6fd0025284603b0bde8f08faa78052f11d3eb782e79072f43ea062310d395f805c0ae79092f4cea0627e4d3ee792d7a46ea052607e041f288364ff4fa7301e041f288360127642b98bf64238b4228bf0b4690f8a81180f8aa21a0f8ac31a0f8ae71a0f8b06171b147f24841c2f20701096800295ed001210446fff70cfe0025c4f8bc51bee744f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114632d14a686ff00105b2f5870ffff483af0123cff680731a40012a7ff47caf4b684ff68112cff6ff727a44934214bfc0f8b431d0f8b431002b3ff46daf4a60012180f8a81199e74af60771c0f24b010420884720b105462046fff732fd5be70121204684f8a9118fe76ff0010553e700bf10b582b00c464df6cb61c0f24501884724b12178042901d1617809b102b010bda1780d29fad1e1780029f7d121790029f4d161790029f1d10028efd00168e029ecd10446fef708fa014620460029e5d004204ff6591e01904ff0000cc0f24e0e0020002100220e23cdf800c0f047204602b010bd3146fff7c3bf00bfb0b582b00c460546fef7e8f90146002d4ff0000018bf002901d102b0b0bd47f2fc70c2f2070000684df6cb61c0f24501884790b10068e0284ff00000edd129784ff6591c0191c0f24e0c0021002223460090e047012002b0b0bd002002b0b0bd08b530460921fff7cbff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460a21fff7b7ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460b21fff7a3ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf10b589b38968012805d104460846fef7e5f9204628e039b30a794b798c79c97942ea032242ea044242ea016106291bd147f29121c0f24601044688470146204691b144f2e970c0f24400804701462046002918bf10bd48f27650c0f2000078440c21fff761ffd4e710bd00bf10b5059c4ff20340c0f2460080472146fff7c0ff10bd00bfb0b5c4b0044600284ff0000000f0c38089b20b2940f0bf80207838b96078092804d1a078612801d1e07810b1002044b0b0bdfef725f90028f8d045f64d50c0f24600804701281cd00228efd103208df8070001200df107058df80800a81c21460b22f7f793fc4af66104c0f24604f02029460d220023a047b0fa80f0400944b0b0bd4ff29c50c2f22900006801466ff39601b1f1005fc9d101684ef66c62ccf2ff029142c2d122797f21042a06dc012a0cdcbab1012a08bf00211ae0082a0bdc052a11d0082a08bf0a2112e0022a0dd0042a08bf01210ce0092a09d00a2a08bf0e2106e0092104e0022102e0032100e00b2180f8eb1210216a2580f8ec12a22180f8e652152580f8ee12062180f8e7521a2580f8ef120c2180f8e852132580f8f01252210822022380f8e95280f8f1124921012580f8e42280f8e53280f8ea2280f8ed3280f8f21280f8f35280f8f452217900f5397280f8f5126179192380f8f612a17980f8f712002180f8f812e17980f8f912217a80f8fa12617a80f8fb12a17a4ff2250480f8fc12c0f247040120e021a047284644b0b0bd00bf1fb5fff72fff002803d0bde81f4000207047bde81f4038b584b049f21f13c0f24703184710b5f0281bd1d1b1cab10c78032c16d10d2a1cbf0b2010bd487801281cbf0a2010bd45f64d50c0f246000c468047012803d1a01c0b21fff703ff002010bdbde81040fdf723bd00bf4ff29c52c2f229021268bff35f8f13466ff39603b3f1005f0cd1d2f800c04ef66c63ccf2ff039c4502bf0023c2f8dc32c2f8e03247f2c942c0f24d02104700bf4ff29c50c2f229000068bff35f8f01466ff39601b1f1005f0bd101684ef66c62ccf2ff02914202bf0021c0f8dc12c0f8e01247f2d940c0f24d0000472de9f04184b0dde90a76022f16d340f64705a94212d08eb13578412d0ed175784e2d0bd1014602f0bf0012284ff00d0008bf002b0ed004b0bde8f0810c9d0097cde9016546f63916c0f24d06b04704b0bde8f081082feed3b078012802d1f078022803d90d2004b0bde8f08100294ed045f6f444c2f2060414f8040c814246d12068002843d048f22732c0f254020d46084602219047d0b324682088b8b36088a8b3a08898b3e08888b330797179b279f37940ea012040ea024050ea0368d1d04ff29c50c2f229000068bff35f8f01466ff39601b1f1005f17d101684ef66c62ccf2ff02914210d1f178a1b190f8dc22aa420cd1d0f8e022424508d101295bd1082fafd100f09cf875e0f07818b1082004b0bde8f081082fa4d1f9f751f9c8b10646d0f868010028f1d047f64c61c2f207010968401a0128e9db96f8dc0268b196f8dd02032809d00628e0d1304600f077f8dce7112004b0bde8f08146f63917c0f24d0707f53a60804707f539622846214690470120002786f8dd020df10f02304600210123a6f8de7286f8dc52c6f8e0828df80f70adf80c7000f07ff861884af2bd6603abc0f24c0628460222b047384604b0bde8f081a7f1510189b24ff6b87291424ff00d0119d390f8dd02032808467ff430af334613f8080fa7f108018ab280b9052a0ed9a1884af2bd67c0f24c072846b847002004b0bde8f081084604b0bde8f08101280dd1042a0bd3981810f8021c00297ff425af10f8010c00287ff420afe0e702284ff00d0008bf062a7ff401af707b01283ff614afd4e700bf90f8dd120a1f022a38bf7047022905d0012907d1042180f8dd127047052180f8dd12704780b582b0052180f8dd120121adf8061045f6f441c2f2060190f8dc0209684af2bd6c49880df10603c0f24c0c0222e04702b0bde8804070472de9f04fc7b0054690f8dc0203920591d8b145f6f041c2f206010978884214d148f22732c0f2540202211c46904760b195f8dc0240f66502c0f255020021904718b100690088172802d247b0bde8f08f033880b2f42828bff42007af0b3807f101084ff000094ff0000a01900495029414e000bf0699cde94509adf810112121adf8121143f23d5144a8c0f255018847049d029c1ffa8af0a042d6d2a4eb0a01019a1ffa81fbb5f8de1241235a45b8bf93468df81c304e234a1c8df81d300123a5f8de22d5f8e0228df81e30059b1ffa8af08df81f30130a8df82410090a8df820208df82130130c120e8df82510b0fa80f18df823204a091ffa8bf111fa8afaa2458df8223008bf0232bbf1000f8df826202fd001390329039901eb000c01d2002115e04ff6fc700bea00020cf10103002100bf5e1816f8014c7d187078ec725c5cb67804318a422c736873ae73f1d1049d1bf003020dd01cf80160cb19012ade7207d00ceb01004178022a19731cbf8078587395f8dc020bf10b0406900b2010fa84f649f6cd713046c0f24c01884700283ff47eaf26f003031ffa84fc06f003025e426ff00303c1180433f518043581f8049081f8059081f8069081f80790f2d152b10344012a83f8049005d0022a83f8059018bf83f806900bf10e0202701b22442108230272220480f801908170c370417283723ff43dafbcf1040f01d2002214e047f6fc710cea0103002200bfb95c8618f17218f8021008eb020531736978ad78043293427173b573f0d11cf003033ff41fafbe5c00f10b01012b8e543ff418af561cbd5d022b8d553ff412af0232bb5c8b540de7f0b583b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f47d121684ef66c62ccf2ff02914240d194f8dc12e9b3002840d002888a423dd145f6f446c2f2060616f8041c8a4235d1316899b3356894f8dd12032919d8d4f8681139b147f64c62c2f207021268891a00290edc02218df80b100df10b020746204600210123fff791fe2046fff760fe38468278092a1bd141896b8894f8dd72994217d1032f3adc012f6ad0022f3dd004e018b181780939022901d203b0f0bd47f6ed31c0f24d0103b0bde8f040084794f8dd72032ff1d8a2f10d0102290cd2c178d1b302208df8040001aa204600210123fff75bfe03b0f0bd092a08bf032fdcd14189aa889142d8d1c078002800f0818002200df105028df80500e6e7052f04d0042fcad1c07805262fe0c078002848d0062084f8dd0202208df807000df10702204600210123fff730fe052fb5d02046fff7fdfd03b0f0bd4268002aadd00389a3f2012189b2b1f57e4fa6d34089082b48d12988884245d12046012108234ce0c0780526002808bf022650b102208df80a000df10a02204600210123fff705fe6988012294f8dc024af2bd67adf8082002abc0f24c07022284f8dd62b84703b0f0bd052f15d1002094f8dc523668c4f8dc02c4f8e00247f2c944c0f24d0404f11000804728463146224603b0bde8f0401047032084f8dd0201208df806000df1060272e7e98888427ff457af2046022103e0204603210022002303b0bde8f040c3e580b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd06290ad14178012907d18178653911f1630f02d3c178012902d94ff0ff307047f0b581b00179427941ea022540f6b8318d421fd80646fdf743fcd8b1f8f722fec0b10446d0f8b80342f66537c0f2440758b941f26770c0f200007844012100220023b847c4f8b80320b107f1e8022821904718b14ff0ff3001b0f0bdb078f178d4f8c4230023c7f6e07340ea452003ea425240ea0120bff35f8f1043bff35f8fc4f8b403002001b0f0bd00bf02293cbf4ff0ff3070472de9f04f9bb00278437842ea0325b5f5007f04d94ff0ff301bb0bde8f08f4ef66c6a4ff29c56ccf2ff0ac2f229064ff6ff774ff000084ff0010b0791099008e000bf48f2a3512046c0f24501bff32f8f8847d6f80090bff35f8f3468bff35f8f20466ff39600b0f1005f03d12068504500f0c2804ff47570fef73ffb002800f0ba8004464ff62c40cff6ff7000bf2118463081f8d48381f8d58381f8d68381f8d78381f8d88381f8d98381f8da8381f8db8381f8dc8381f8dd8381f8de8381f8df8381f8e08381f8e18381f8e28381f8e38381f8e48381f8e58381f8e68381f8e78381f8e88381f8e98381f8ea8381f8eb8381f8ec8381f8ed8381f8ee8381f8ef8381f8f08381f8f18381f8f28381f8f38381f8f48381f8f58381f8f68381f8f78381f8f88381f8f98381f8fa8381f8fb8381f8fc8381f8fd8381f8fe8381f8ff8381f8008481f8018481f8028481f8038481f8048481f8058481f8068481f8078481f8088481f8098481f80a8481f80b8481f80c8481f80d8481f80e8481f80f8481f8108481f8118481f8128481f8138481f8148481f8158481f8168481f8178481f8188481f819847ff470afc4f800a084f80ab0a783e78327846784a784e78427856785a785e78527866786a786e7862787678756e8000f48457ff431afbff35f8f00bf46e8004028b156e8000f4845f8d025e70024ddf824805db1fdf704fb0146002c4ff0ff3018bf002902d11bb0bde8f08fb5fa85f040094ff000098df82c908df82d908df82e908df82f908df830908df831908df832908df833908df834908df835908df836908df837908df838908df839908df83a908df83b908df83c908df83d908df83e908df83f908df840908df841908df842908df843908df844908df845908df846908df847908df848908df849908df84a908df84b908df84c908df84d908df84e908df84f908df850908df851908df852908df853908df854908df855908df856908df857908df858908df859908df85a908df85b908df85c908df85d908df85e908df85f908df860908df861908df862908df863908df864908df865908df866908df867908df868908df869908df86a908df86b90bcb1d4f86c21a2b14ff4006304e03f683b44b3f5403f0cd2d718be680136f6d07e68033626f0030609eb060101f11009ede7002d4ff0020a039500f00581ddf81cc0acf102020a2ac0f00081002608eb0a0b9bf801504ff0ff30012d3ff648af9bf802209bf803309bf8047042ea03229bf8053042ea074747ea0361a1f1011212f5803f0a91fff433af9bf808109bf809e0029141ea0e210af10a020497aceb02034f1e9f42bff423afcde905269bf806209bf807300a9e42ea032cb6eb0c07fff416afb9423ff613af18f80a70089147ea052a4feada000ba90e5c07f00705012707fa05f5354240f003830e5c45ea06050d5407994ff6ff75c0f20205b4b3d4f86c0198b317461a0201924fea0e264ff4006200eb0208d8f8083053454bd0d8f800301a44aa4221d800eb0208d8f80830534540d0d8f800301a44aa4216d800eb0208d8f80830534535d0d8f800301a44aa420bd800eb0208d8f8083053452ad0d8f800301a44aa42d3d900bfbcf1000f40f0be8204980a464ff6fc710330c0f2030108404844944600f1100908980599069e00eb010a0398013686424ff0000028bf0120ddf824804fd2aceb0a02092a3ff64caf49e0d8f804000a9a904240f09782d8f80c006045c0f0928236d9089aa0eb0c00824200f28b827ab3029831180198c1f1000cc2199044a1f1010e002208eb02050beb0203287c9e7ab04240f0778296451cd06e7cdf7abe4240f070820ceb0207be1c13d0ae7c187b864240f06782079ef81c07d0e87c5b7b984240f05f8204329142dbd1b446a7e78c46a5e7ddf81cc0a2e7ddf81cc0002800f05082baeb0c004ff0000118bf0120b9f53e3f88bf0121014303980028c1f100003ff43eae00297ff43baed4f86cb1bbf1000f40f08b804ff44030fef7a6f8002800f02f82834600206ff0200100220beb02030bf8020040320beb0107b2f5006f01f1400158709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d87618775877987787f8400087f8410087f8420083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f0093d14ff43e30cbf800080020cbf804084ff0ff31cbf80c084ff40060c4f86cb1cbf80818c4f800034ff6ff760222c0f2020600210ae00aeb080209f10101ddf8248071454ff000003ff498ad08eb02050a9218f80200aa786f7805922a7a95f803e095f8049095f805c095f806a0eb7908926a7a40ea0728cde906215cb3012f29d84ff400670beb0705a868404500f0a28028680744b7421dd80beb0705a868404500f0988028680744b74213d80beb0705a868404500f08e8028680744b74209d80beb0705a868404500f0848028680744b742d7d90598cdf810a040ea0e2040ea094040ea0c67f81c20f00300d4f8009300f1100a002002930597b9f1000f26d00beb09035bf809105a68514580f09c80eab10beb02005bf80210d0f80490514580f08d80b9f1000f11d00beb09035bf809105a68514580f0878042b10beb02005bf80210d0f804905145d6d377e000204ff400694ff4006504e000bf3d44b5f5403f23d20beb05018a685bf805700132f4d04868ca68033020f0030000f110060beb09001032f6f7c7fb4168c2684bf809608368d4f86cb18a424ff00001b14408bf00f110014bf82310d7e7c9f540310f294ff00002c4f8002311d90beb09004bf809104ff0ff3142608160c260c4f800930be0e868ddf80ce00a9a089f60e010b10268114401604ff000090020b9f1000f7bd00beb09035bf809105a68514522d2002a72d00beb02005bf80210d0f80490514513d2b9f1000f67d00beb09035bf809105a6851450ed2002a5ed00beb02005bf80210d0f804905145d7d30546cc469146184601e01d469446a1eb0a0110290fd309eb0a020beb02034bf802104ff0ff31c3f804c0996000219446d960c5f800a0ddf80ce00a9addf810a0029b089f00280cbfc4f800c3c0f804c005984ff6ff7668600020c0f20206c5f80880e8604aea0321069bddf81c9047ea032a814202f10a087ff4bdae05f110077818099952464144f346f6f7dafae968de466868aa68d4f86cb15144e96081424ff0000018bf07464bf82270a3e600204ff400684ff4006703e02f44b7f5403f23d20beb07018a685bf807500132f4d04868ca68033020f0030000f110060beb08001032f6f7f9fa4168c2684bf808608368d4f86cb18a424ff00001b04408bf00f110014bf82310d7e7c8f5403200210f2ac4f800130dd90beb08034ff0ff304bf8082059609860d960c4f800831bb0bde8f08f00281ebf0168114401604ff0ff301bb0bde8f08f00bf02293cbf4ff0ff3070472de9f04f8bb00278437842ea0325b5f5007f00f2148105eb4502022303eb4202914240f00c810590fcf7dbfe002800f006814ef66c694ff29c564ff62c48ccf2ff09c2f229064ff6ff77cff6ff7800244ff0010b06e048f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168494500f0bc804ff47570fdf722fe002800f0d58041464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009080f80ab08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f51457ff437afbff35f8f00bf46e8000121b156e8001f5145f8d02be7d0f86cc10495bcf1000f20d000234ff4006104e03f683944b1f5403f0bd20ceb0107be680136f5d07e68033626f0030633441033eee775b9b3f53e3f40f2e0804ff0ff310bb0bde8f04f08467047002d00f0d680002305994ff0000901f1020a153103914ff0ff31059109eb49060aeb46056c784ff0ff31012ce3d8af78ea7857ea022e1ebf2a796d7952ea0528d9d0bef5207fd6d8b8f5f07fd3d80ef10102520802fb08fb4ff6fb724ff6ff779345c0f20207c6d81af81610059e41ea0421039cb9f1000f28d014f8132c14f8125c42ea05228a42b3d0feb114f80d2c14f80c5c42ea05228a42aad0012e15d014f8072c14f8065c42ea05228a42a0d0022e0bd014f8012c257842ea05228a4297d0043e721c04f11804d6d15646bcf1000f2fd00bf105054ff400640ceb040adaf808208a4230d0daf800201444bc4220d80ceb040adaf808208a4225d0daf800201444bc4215d80ceb040adaf808208a421ad0daf800201444bc420ad80ceb040adaf808208a420fd0daf800201444bc42d3d94ff6fc720bf10801c0f201021140194401f1100320e0daf80410a9427ff44fafdaf80c10a9427ff44aaf9af8101004297ff445af9af811109af8122041ea022171457ff43caf9af813109af8142041ea022141457ff433af049909f1010989450599b24601f1010105917ff43dafb3f53e3f3ff624af00240025019019e000211fe7d0f86cc14a688b684ff6ff7793454ff00002c0f20207c1f80cb008bf01f110024cf82320013575454ff000013ff408af05eb45000aeb40011af8100091f8018091f802e091f803b091f804904979bcf1000f40ea082605912cd0b8f1010f29d84ff400610ceb01039a68b24200f0a8801a681144b9421dd80ceb01039a68b24200f09e801a681144b94213d80ceb01039a68b24200f094801a681144b94209d80ceb01039a68b24200f08a801a681144b942d7d94eea0b21059b013149ea0322490801fb02f75a4607f1050b8df81d004fea1b208df820004fea1b408df8210005208df82500042001218df8270048468df81b1003908df82a000df11b0011218df81c408df81e808df81fb08df822408df824408df823408df82640cdf808e08df828e091468df829208df82b30fff74df800287ff482ae01980021d0f86cc1bcf1000f3fd0ddf810e0b8f1010f3cd84ff400630ceb03018a68b24235d009680b444ff6ff71c0f202018b4226d80ceb03018a68b24228d009680b444ff6ff71c0f202018b4219d80ceb03018a68b2421bd009680b444ff6ff71c0f202018b420cd80ceb03018a68b2420ed009680b444ff6ff71c0f202018b42cbd9002104e0ddf810e02ee7ddf810e0002f3ff41aaf4fea0922029f059b42f001023a44039f1b0252083b4353435a1e032a01d200220ae023f0030700228e18043297427475b475f4753476f7d113f003033ff4f8ae01f11507012bbc543ff4f2ae3a44022b547018bf9470ebe62de9f04f81b04ef66c6b4ff29c554ff62c4848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0bd804ff47570fdf723fb002800f02e81414600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff43bafbff35f8f00bf45e8000121b155e8001fa142f8d02fe7d0f86813002974d104464ff41630fdf761fa014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8681300e00021084601b0bde8f08f00bf84464ff6ff7081424ff0000008bf7047f0b58fb04ff48057cde90670dcf87c73dde91646dde9145e0c90cde90d7006a8cde903606046cde901e46446009504f059fa3eb130b99df8381094f88023114384f880130fb0bde8f04070472de9f04f8fb00446002047f64c68a4f88003c2f20708d8f80000002a8b461abfc4f874030146d4f87413401ac4f87c03fcf72cfa002800f07181d4f86803002800f06c8104f080fb002800f067810646b4f86c03002848d0a0f10108d4f87c0300254ff4a0714ff4f0774ff48059cde90d050df1180acde901152046414632464ff42073cde906950c95cde9035a009704f002fa002840f04181d4f87c034146cde90d050120cde902504ff4a0700190204632464ff420734ff4f077cde906950c95cdf810a0009704f0e6f9002840f025819df8380094f8801347f64c68084384f88003c2f2070806e0d4f8681330464ff41632f5f750fd4ef66c694ff29c57ccf2ff09c2f229074ff6ff76002506e048f2a351c0f24501bff32f8f8847d7f800a0bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168494500f0c0804ff47570fdf71af9002800f0ec804ff62c41cff6ff7100bf4218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470af0121c0f8009081728683c683068446848684c684068546858685c685068646868686c6860687468757e8001f51457ff433afbff35f8f00bf47e8000121b157e8001f5145f8d027e7054604f041fa002d18bf002803d101208bf8050022e0c5f860014ff001094ff4f07085f8649101904ff420704af61f460090c0f2470600200021002200230027b04730b185f86471c5f860718bf8059004e09bf805008bf8049030b14ff0ff300fb0bde8f08f0025cbe794f8800384f8810348b3d4f8700342f66535c0f2440528b1d8f800102d31c4f8781311e044f22770c0f200007844002122460023a847c4f87003d8f8001001f12d01c4f8781320b105f1e8022d21904728b1002084f881030fb0bde8f08f00200fb0bde8f08f80b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd022a3cbf4ff0ff3070472de9f04fadf2fc4d1d460f784b780a4457ea032301f10204239302f08383229040a806300027169021951f92944202f07383601c904202f06f8320786178a31c40ea0121d01a81422793289102f26483ddf82005ddf834252f90ddf82405ddf82c153090ddf828054ff00008319010683291411e002825972e95116002f04c832898002802f048832898871c032f02f04383e678032e02f23f83279890f80090f00702d104f1040e27e0289820f00100022802f0318394f804a0657922984aea052b59462eaa02f036fb002842f02483ddf82c0504f1060e012d32900dd8ddf83405806948b14feadb020af00701835c012505fa01f119438154022e04eb070203d2ddf82cb528e000bf964503d0744614f9015b01e00025144645f64d57c0f24607f2469346b847012808bf0135681e81b26e0800ebd130002d48bf4610b8470128ddf82c0508bf76423044da45a6465a468346329002f0df82b9f1040fcdf8b0e02addb9f1060f40f3e480b9f1070f00f07782b9f1080f00f05081b9f1090f42f0ca82964502f0c7821ef8011b964542f0c2820f2902f2bf82ddf83005002800f0d787dde930232e9801eb01115a43f5f7e5fb00f0cdbf00bfb9f1020f2adcb9f1010f00f04d81b9f1020f42f0a4822e9800272c902f98964529903098cde938e22a9031983a972b90ddf834053597c069349000f0ff820ef1010038909ef80070904200f0f7820ef1020038909ef801000502f2e2b9f1030f00f07981b9f1040f42f07982b2eb0e0402f075820ef10100904202f070822298002802f06c82a01e0528c2f068820ef10700904242f063822298d0f86c01002802f05d829ef80110012902f258829ef8003043ea012250f82210002902f04f820f1ab90742f04b826ff4403140f60f063944cff6fd76b142c2f04182384450f8081c914242f03b8250f80c1ca1f1011616f5803fc2f03382002802f0308250f8046c8e4242f02b82c7f54037b94202f22682ddf83475bf6947b1d20803f00703be5c012505fa03f33343bb54dde92e69dde93075002238ab02f06cfa00284ff0000a08bf4ff0ff3a00f0d181ddf830052d971f9a259f002801f0ef87072c4ff0ff3a41f0ea87002e01f0e787fbf7d0fe002840f02a8400f091beb9f1050f00f0ba81b9f1060f42f0ea81964502f0e7810ef10100904202f0e2810ef10200904202f0dd810ef10300904202f0d8810ef10400904202f0d3810ef10500904202f0ce810ef10600904202f0c9810ef10700904202f0c4810ef1080ca2eb0c001028c2f0bd810ef11800904242f0b8819ef804009ef8051050ea01261ebf9ef806009ef8071050ea012502f0a9819ef800009ef801202f9940ea0224081bc2f09f81864202f29c819ef802009ef80330309a40ea0327d01bc2f09281854202f28f81ddf83005002800f0a7862e98319bcde9045ccde90276cde900b403f029fd00f09bbeddf8340540adc06938afdde92e49dde9306acde9380828463946cde940e2cdf8088102f030fc80462846394602f02bfc4299002900f07f82219d259f4ff0ff3a1f9a01f04abf964502f05881714611f8010b012802f252812e9b00282a932f9b1d922d93309b2c93319b2b9300f03583914202f043810ef10201914202f03e810ef10301914202f039810ef10401914202f034810ef10501914202f02f810ef10601914202f02a810ef10701914202f025810ef10801914202f020819ef801109ef802209ef8043041ea02219ef803209ef805709ef8064042ea03269ef807209ef8083047ea04240ef10907189742ea032213e3a2eb0e0abaf1050fc2f0fe800ef10505954202f0f9800ef106079ef80540d01ba042c2f0f1803819904242f0ed802e981b902f982d903098249031981a90b4b1002603e000bf0136a64210d2b85d01381e28f8d9b819a11b40aa38ab02f0d7ff002802f0d18040980644a642eed3ddf83005002800f0e585baf1050f02f0c4801b98002802f0c080fbf797fd002802f0bb802d78a81d824542f0b68047f65420c2f20700d0f80080b8f1000f02f0ac8098f80c0098f80d1098f80e2098f80f3040ea012040ea024040ea0369a9f1810010f1800fc2f0988098f8100098f8111098f8132040ea012098f8121040ea014040ea02600d90813810f5817f42f28480002d4ff0000a00f07185002200240be000bf00f1004040a941f826000134ac4206f1010280f01385385d1646411e1e29efd93819291b38aa34ab02f068ff002800f05a853899349840aa0c4442f82600e5e7964502f058800ef10100904202f053800ef10200904242f04e809ef800009ef801102eaf40ea0121ddf834058ccf0490ddf83005319e03902298cde9016b009703f018fd82461f9a259f01f022beb2eb0e0902f030800ef10100904202f02b80a9f102000528c2f026800ef10700904202f021802298002802f01d800ef108009ef807702d90101ab842c2f014802d983844904242f00f802298d0f86c31002b02f009809ef80100012802f204809ef8001041ea002053f82020002a01f0fb87d21a960741f0f7876ff4403640f60f051644cff6fd75ae42c1f0ed879e1856f8083c834241f0e78756f80c5ca5f1011313f5803fc1f0df87002e01f0dc8756f8043cab4241f0d787c2f54032954201f2d287ddf83425926942b1c00801f00701135c012404fa01f119431154c12dc1f0c2873078012841f0be872e98002f2b902f982a90309829903198269055d0ddf8b4a0a6f13f0401e0013f4ed01af8010b411e1f29f8d341b22029c1f2a48704eb400114f81000497840ea0122c12ac1f09a873046294640ab01f0f0ff0028e3d101f091bf01203a90002538ae34ac3046214602f04dfa2d903046214602f048fa3a9906460029269545d04ff0000a002024900120d9463a9000200df1e00b34ac20905846214602f033fa05465846214602f02efa3a9900294ad01f9a4ff0ff31219d259f8a4601f04dbdddf83005002800f07684b9f1080f4ff0ff3acdf80081c1f03d852b98259f002801f03985fbf723fc002800f0e6832c99cd7905f10800814540f0df8308784f7891f8069040ea072600f061bcdde938018842b5d0024612f801ab8a423892b1d0821c389243788a424aea032aaad0c21c389283788a422493a6d0011d3891c078d9460002a4e78446dde938011f9a88424ff0ff31afd1ddf8b4e0bef1000faad4002ea8d4ddf834353598196aab460843269d186229983d4341904ff6fe7085422b982c9b2a9c439022984093429400f023854ff6ff718d4240f0fb84d0f868534ff4207041904ff4f07042904ff4a070439000f012bddde94021219d259f8a42cdf8b4903ff47aad531c8b423ff476ad931c8b423ff472add31c8b423ff46ead131d8b423ff46aad531d8b423ff466ad931d8b423ff462add31d8b423ff45ead02f108038b427ff459ad11785378b646d14651ea03264ff0ff3a01f0b7849378d77853ea07232b9301f0b084b6f5207f01f2ac842b9b259fb3f5f07f3ff640ad93790f2b1e933ff63badd17910292a913ff636adddf83435299039981f6a11793843ddf830755279002f2494186201f082840beb0600404280454ff0000ac1f285842d985b46a0eb0b00804581f27e842b9847422998b842c1f278842998dde924c786457ff70cad41ea0221b1eb560f28bf71082b98b1eb500f28bf41081891013938bf51460f9129994a4202eae174aeeb01018142a8bf01468c421d91bff6efacddf860e008eb03004fea4e010efb0ef21b9191001791311820910021032e38bf01212b9b0022032b38bf012211431591591e1491991e12910f991c904a00494389000c912a990130109008010e9041ea01106ff008010d9001eb4e00139029984ff0000b20eae07109fb01c011922c9000201a9001f0dab9914201f00e860ef10201914201f009860ef10301914201f004860ef10401914201f0ff859ef802209ef8011056009ef803209ef8043094000ef10502890018925a001d9b002c199201f0eb85199a002a01f0e7852d9a521ac1f0e385944201f2e0852c9a921bc1f0dc85199f974201f2d885002800f0f08001eb0b020025f9e048f2a351c0f24501bff32f8f88474ff29c50c2f229000468bff35f8f00680321cff680710140b1f1005fbff35f8f07d101684ef66c62ccf2ff02914201f045804ff47570fcf7fef9002800f049824ff62c41cff6ff7100bf4218463182f8d48382f8d58382f8d68382f8d78382f8d88382f8d98382f8da8382f8db8382f8dc8382f8dd8382f8de8382f8df8382f8e08382f8e18382f8e28382f8e38382f8e48382f8e58382f8e68382f8e78382f8e88382f8e98382f8ea8382f8eb8382f8ec8382f8ed8382f8ee8382f8ef8382f8f08382f8f18382f8f28382f8f38382f8f48382f8f58382f8f68382f8f78382f8f88382f8f98382f8fa8382f8fb8382f8fc8382f8fd8382f8fe8382f8ff8382f8008482f8018482f8028482f8038482f8048482f8058482f8068482f8078482f8088482f8098482f80a8482f80b8482f80c8482f80d8482f80e8482f80f8482f8108482f8118482f8128482f8138482f8148482f8158482f8168482f8178482f8188482f819847ff470af4ef66c61ccf2ff010160012181724ff6ff714ff29c528183c183018441848184c184018541858185c185018641868186c18601874187c2f2290252e8001f2c9ba1427ff421afbff35f8f00bf42e80001002900f07f8752e8001fa142f6d013e701eb0b025feacb704ff0000506d1002a04d42d99a0188842d8bf01251899199f581a1590ddf8300507fb04feb0fa80f040092843c0074ff0000b1c900f46002017952990cdf868e013e000bf29982699084429901e98249981424ff0000088bf01204bea000b209f5feacb7008bf0f441d9b5feacb7005d02998864509d801f0a4bc00bf9f4200f09c812998864540f29881dd1b01f099843878030902d0012112e000bf012d01f090847b780bb102210ae0042dc1f08984b978fb7851ea032301f08384042100bf24912999aeeb01018b4201f27a841c99269300291e952097b4d1269900f00f084b1e032b4fea0019cdf86cb074d24ff0000c2698dde91aeb10f0030aa2d029986044b0fbf4f101fb14078b19174453ea070115d42d998f4212d22c998b420fd2dde92a5103fb01517b08cd5cff0705f00f0c05f0f005454408bf4cea0905cd54baf1010f3ff47eaf411cb1fbf4f303fb141133448f1853ea070115d42d998f4212d22c998b420fd2dde92a5103fb01517b08cd5cff0705f00f0c05f0f005454408bf4cea0905cd54baf1020f3ff45aaf0230b0fbf4f101fb14008b19104453ea00013ff54faf2d998842bff44baf2c998b42bff447afdde92a7103fb01714308cf5cc00707f00f0507f0f007474408bf45ea0907cf5435e721f0030b4ff0000c05e000bf0cf1040ce3453ff482af299800eb0c0abafbf4f303fb14a703eb060e17445eea070315d42d98874212d22c9886450fd2dde92a107d080efb0013ff07585d00f00f0100f0f000404408bf41ea090058550af10100b0fbf4f101fb14008b19871853ea070015d42d98874212d22c9883420fd2dde92a1003fb00107908435cff0703f00f0503f0f003434408bf45ea090343540af10200b0fbf4f101fb14008b19871853ea070015d42d98874212d22c9883420fd2dde92a1003fb00107908435cff0703f00f0503f0f003434408bf45ea090343540af10300b0fbf4f101fb14008b19871853ea07008ad42d988742bff487af2c988342bff483afdde92a1003fb00107908435cff0703f00f0503f0f003434408bf45ea0903435471e74ff0ff3a01f04ab9002acdf8ac9000f0ee812c9f9146a2f1010a002540ac09e02c9faa4505f10105a6f1010604f1040400f0d98140a850f825100029f1d400202b4686421dd004eb80077a68b2f1ff3f18dc5a1c924514d0ba68b2f1ff3f11dc9a1c92450dd0fa68b2f1ff3f0adcda1c924506d03a690433002a00f10400e0d400e00022404638ab34af009702f0a8fa78b134980028c3d04af20d3138a8c0f24e018847bce7cdf8ac9001204ff00009a0e14ff0ff3afff7ccba9f4241f001832998864541f0fd82ddf83005002818bf012017990840012810d1dde92a03159906fb0300670800eb5202199800970190189801f0b5fb002801f0e3821f9a259f219d01f0d1b848f2a351c0f24501bff32f8f88474ff29c50c2f229000468bff35f8f00680321cff680710140b1f1005fbff35f8f07d101684ef66c62ccf2ff02914200f030844ff47570fbf70bff002801f0a1804ff62c41cff6ff714218463182f8d48382f8d58382f8d68382f8d78382f8d88382f8d98382f8da8382f8db8382f8dc8382f8dd8382f8de8382f8df8382f8e08382f8e18382f8e28382f8e38382f8e48382f8e58382f8e68382f8e78382f8e88382f8e98382f8ea8382f8eb8382f8ec8382f8ed8382f8ee8382f8ef8382f8f08382f8f18382f8f28382f8f38382f8f48382f8f58382f8f68382f8f78382f8f88382f8f98382f8fa8382f8fb8382f8fc8382f8fd8382f8fe8382f8ff8382f8008482f8018482f8028482f8038482f8048482f8058482f8068482f8078482f8088482f8098482f80a8482f80b8482f80c8482f80d8482f80e8482f80f8482f8108482f8118482f8128482f8138482f8148482f8158482f8168482f8178482f8188482f819847ff470af4ef66c61ccf2ff010160012181724ff6ff714ff29c528183c183018441848184c184018541858185c185018641868186c18601874187c2f2290252e8001fa1427ff423afbff35f8f42e80001002900f06c8352e8001fa142f6d016e7294640aacdf89890e14601f0fdf900287ff485aaddf834051f9a8069ddf8b4e0cc462c9b2a9cddf8989060b1b8eb552f09d1ea0807f00701855c012707fa01f1294381541f9a409d5746002d3ff468aa002f4ff0ff313ff465aaca4699462498209b03433ff45eaa41987045fff45aaaa0eb0e0087423ff655aa4298b042fff451aa801b83423ff64daaddf83005002100283ff447aaabf1011010f1021f7ff641aa2998a0460beb0a048442b8bfc445bff638aa784284427ff734aa584284457ff730aa04eae471aeeb010a0f44299924eae4700ceaec7b0beb030e2ceaec720b1a9f4238bf3b46a8eb0207be451e92269728bfbe464d45a6eb0b071d970b95cdf854e000f07387002700f089bf00204ff0000a03e04ff0000901202c9f39790f248d0601f00f0604eae575754389246543ed0a97f800c08df8d0500e257a78bb78ff782c9111f0200f08bf012506fb05f504fb05f54fead5258df8d1504ff00d0508bf022506fb05f504fb05f54fead5258df8d2504ff00c0508bf032506fb05f504fb05f54fead5258df8d3504ff00b0508bf042506fb05f504fb05f54fead5258df8d4504ff00a0508bf052506fb05f504fb05f54fead5258df8d5504ff0090508bf062506fb05f504fb05f54fead5258df8d6504ff0080508bf072506fb05f504fb05f54fead5258df8d7504ff0070508bf082506fb05f504fb05f54fead5258df8d8504ff0060508bf092506fb05f504fb05f54fead5258df8d9504ff0050508bf0a2506fb05f504fb05f54fead5258df8da504ff0040508bf0b2506fb05f504fb05f54fead5258df8db504ff0030508bf0c2506fb05f504fb05f54fead5258df8dc504ff0020508bf0d2506fb05f504fb05f54fead5258df8dd504ff0010508bf0e2506fb05f504fb05f54fead5258df8de504ff0000508bf0f256e436643219df60a00288df8df6040f00b82120642ea0c4138060beb214a2b9940ea0340a9f1010601eb204000240c90cdf84080cde90e6907e001f01f000b3801344c45824400f0ea8140a850f82410b1f1ff3ff1dd23469e421cd040a800eb83004268b2f1ff3f16dc0133b34212d08268b2f1ff3f0fdc0133b3420bd0c268b2f1ff3f08dc0133b34204d002690133002ae1d400e00022404638ab33af009701f0e3ff002800f0bd8133990029189100f0ae819df8e8009df8e910129450ea012ecdf850a000f090819df8ec709df8ed603f049df8e7009df8e61047ea06670d9e4fea002c41ea00229df8ea0076449df8eb3006eb27470c9e0004149d40ea0360f41b0126031405eb204a06eb520015900ceb01001e92421e199201f003020af1010113910af1020111911899ef18eb1a01f1010906eb5001801a2b90d8074ff0000c2a90f807269120922990cde91c4e07e000bf26980cf1010cf445814400f043811ceb0400f5d424998842f2d21e990029efd0dde91a21002600fb02181998032880f097802098dde91c4e0028e1d01898159901220cfb01072c987108795c00f0100082ea101301f00f00cab25feac67b08bf10090146002818bf01211943012917d116eb0a0514d42d998d4211d2690818f80120179734af385c02f00f0702f0f0020243ed0708bf47ea0012179f08f8012020980128acd0701c4008385cbbf1000fc1b24fea111e08bf00f00f0e5ffa8ef0002818bf01201843012818d11398801915d42d99884212d2410818f8012034ac179714f80e7002f00f0502f0f0023a43c00708bf45ea0712179f08f801202098dde91c4e02283ff47bafb01c4008385cbbf1000fc1b200f00f0008bf08090146002818bf0121194301297ff469af119973183ff565af2d998b42bff461af590818f8012034af385c02f00f0702f0f0020243db0708bf47ea001208f801204fe7ce4606e02b980436b0420ef1020e3ff460af2c981ef8015c00f01000012181ea1013edb200200f2d88bf0120184301280aeb060016d1002814d42d99884211d247082d0918f8071034aa555d01f00f0401f0f00129435feaca7208bf44ea051108f807101ef8011c11f00f0b594618bf01211943012917d1451c15d42d998d4212d2690818f8012034ac14f80b4002f00f052a9f45ea041502f0f002002f08bf42ea040508f801509ef80010cdb200210f2d88bf01211943012916d1871c14d42d998f4211d27a08290918f8024034ad695c04f00f0504f0f004299f0c43002f08bf45ea011408f802409ef8001011f00f05294618bf0121194301297ff482af03303ff57faf2d998842bff47baf400818f8001034aa525d01f00f032a9c43ea021301f0f001002c08bf41ea020308f8003067e74af20d3138a8c0f24e0188479df8e4009df8e510219dddf84080ddf850a0dde90e69129c03e09df8e4009df8e51040ea012010e64ff0000a01e04ff0ff3a1f9a259f00f07dbcd0f86c114ff0ff3a002900f07384012f259f00f2708451f82600002800f06b84401a820740f067846ff4403240f60f030244cff6fd739a42c0f05d840b1853f8081cb14240f0578453f80c6c0022a6f10111cff6ff729142c0f04d8453f8041cb14240f04884c0f540308642249600f242849846002b00f03d842498259fc128c0f0398498f80000012840f033844fea89660f2409f00f0704eae6767e43892466432c9af60a8df8d0600e269178d3781079527919f0200f08bf012607fb06f604fb06f64fead6268df8d1604ff00d0608bf022607fb06f604fb06f64fead6268df8d2604ff00c0608bf032607fb06f604fb06f64fead6268df8d3604ff00b0608bf042607fb06f604fb06f64fead6268df8d4604ff00a0608bf052607fb06f604fb06f64fead6268df8d5604ff0090608bf062607fb06f604fb06f64fead6268df8d6604ff0080608bf072607fb06f604fb06f64fead6268df8d7604ff0070608bf082607fb06f604fb06f64fead6268df8d8604ff0060608bf092607fb06f604fb06f64fead6268df8d9604ff0050608bf0a2607fb06f604fb06f64fead6268df8da604ff0040608bf0b2607fb06f604fb06f64fead6268df8db604ff0030608bf0c2607fb06f604fb06f64fead6268df8dc604ff0020608bf0d2607fb06f604fb06f64fead6268df8dd604ff0010608bf0e2607fb06f604fb06f64fead6268df8de604ff0000608bf0f26774367434ff0000aff0a002d8df8df7000f06a831b0643ea01410beb2144110641ea004000142d9e2090c9f30010a8f13f0a2f461e903de32c9bd0f86c014ff0ff3a002800f051835978012900f24d831a7842ea012150f82120002a00f04583121a930740f041836ff4403340f60f071344cff6fd77bb42259fc0f03683104450f8083c8b4240f0308350f80c1c0027a1f10113cff6ff77bb42c0f02583259f002800f0228350f8043c8b4240f01d83c2f54032914200f21883002240ab00f082fd002800f00e832c9f0f24f978b8787b793a79090641ea0040190641ea0241ba790beb2040970602f00f0304eae7775f4389246743ff0a8df8d0700e2712f0200f08bf012703fb07f704fb07f74fead7278df8d1704ff00d0708bf022703fb07f704fb07f74fead7278df8d2704ff00c0708bf032703fb07f704fb07f74fead7278df8d3704ff00b0708bf042703fb07f704fb07f74fead7278df8d4704ff00a0708bf052703fb07f704fb07f74fead7278df8d5704ff0090708bf062703fb07f704fb07f74fead7278df8d6704ff0080708bf072703fb07f704fb07f74fead7278df8d7704ff0070708bf082703fb07f704fb07f74fead7278df8d8704ff0060708bf092703fb07f704fb07f74fead7278df8d9704ff0050708bf0a2703fb07f704fb07f74fead7278df8da704ff0040708bf0b2703fb07f704fb07f74fead7278df8db704ff0030708bf0c2703fb07f704fb07f74fead7278df8dc704ff0020708bf0d2703fb07f704fb07f74fead7278df8dd704ff0010708bf0e2703fb07f704fb07f74fead7278df8de704ff0000708bf0f277b436343db0ac2f3001209148df8df30049234aa2d9b0392cde90001304629464a4640af4ff0000a029701f077fc30e2dde924c7199cca462c98013450442c901d984ff0000a8442bef6d9aa2b9ae14311448c42b8bf21461b986fea41010144012966db1798189a01fb1101a2eb0b024ff0ff33012a03eb420235db02fb02f38b4231dd1398a0eb4b0303f10807032f03f106021fdb02fb02f68e421bdd052f03f104021adb02fb02f68e4216dd072f03f1020215db02fb02f68e4211dd0bf1040b092fa3f1080203db5b438b421346dbdc083207e00bf1010b04e00bf1020b01e00bf1030b259fbbf1010f23db0832931f5b438b421edcbbf1020f19db131f5b438b4200f3bd80bbf1020f11d0931e5b438b4200f3b880bbf1040f09db02fb02f38b4200f3b380bbf1040b02f10802ded14ff0000b1c98ca460beb0002299821182a9801fb09c310282098a0eb0b080bd101202d991e9e019018464346009602f030f9ddf890c066e7b4fa84f1159849090143269349d11498844246d01299601e091b19948842b8bf01466fea4100119908440128c0f2a4800c991a9b00fb10100f99cb1a4ff0ff31012b01eb430176db01fb01f7874272dd59001a9ca1f1090303f10807032f03f1060162db01fb01f686425edd052f03f104015cdb01fb01f6864258dd072f03f1020156db01fb01f6864252dd0434092fa3f1080103db5b4383420b46dcdc083148e02d9822eae27c8045a8bf8046c445a3da5feacc7019940ad026994fea5c000a5c2a9b02f0f0021a430cf1010c0a544fea5c02b2eb580f1ad226982c9f0d9900eb5803bc18264606f8011b9e4280f08180a01c9842317080f08180e61c9e4201707bd20437b81898423170ead376e0269800eb5c0072e0abf1010b56e7abf1020b53e7abf1030b50e7013402e0023400e003341a941a9c012c1edb08318b1f5b43834219dc022c16db0b1f5b43834200f3c780022c0fd08b1e5b43834200f3c280042c08db01fb01f3834200f3bd80043c01f10801e2d100241098ddf8b4a000eb040c22eae27ed445b8bfe246d6451a944fda5feace700ad026994fea5e000a5c2a9b02f0f0021a430ef1010e0a544fea5e07b7eb5a0f18d226982c9c0d9900eb5a06e019034603f8011bb34213d2821cb242197021d2c31cb34211701cd20434e219b2421970ecd317e0269800eb5e0213e0601c02e0421c0fe030465feac8723ff460aec445bff45dae01780e9a01f00f011143017055e61a461a9c5feaca7007d0d64505d210780e9900f00f00084310702099e0430e1801202d9c019026981e9f214662463346009702f005f826eae67ca045a8bfa046c445bff633ae5feacc710ad026984fea5c01425c2a9b02f0f0021a430cf1010c42544fea5c02b2eb580f18d226982c9f00eb58030d98bc18264606f8010b9e4211d2a11c9942307010d2e61c9e4208700bd20437b91899423070ecd306e0269800eb5c0102e0611c00e031465feac8723ff4fcadc445bff4f9ad08780e9a00f00f0010430870f1e5013c49e7023c47e7033c45e7013f00f0d08116f8010b411e1f29f7d341b220290fdb0aeb40011af81000497840ea0122c12a06d32499404638ab00f073fa0028e4d14ff0ff3a259f1f9a219dbaf1000f40f00d82239801378742dde9270100eb01047df48eae08e2259f4ff0000afef7a0b8dde91d67b74204dd012710e0259ffef797b8dde91d675045a7eb0607b7fa87f74fea57174ff00006c8bf01263740ddf854e0bef1000f199700f07e814fea5a044ff0040c14942cea8a0413940aeb030403eb0009013c00f0010509f00106670812972cea8407ac1911971f1b249b209a05eb000cd4188aea0002400810904fea5900ddf898800f9004eb0b007908404538bf804601200aeb0503cdf898801a91012988bf7808ddf82cb000f0030120f0030a580820900beb53000a904fea5c001c902c984ff0000800eb5c0001300990d0071b904398ddf864c02d904ff0ff3000eb57000d90c7f341000c906ff0010000eb570008906ff0020000eb57000e91179707901895249607e0269808f1010880454ff000013ef40eaa6fea08014046bcf1000f18bf0eeb01001d9900eb01022d9902fb01b10db9002305e0149b139ccb5ce34003f00f032a930eb9002304e0129b119ccb5ce3401b0129931e9b0344dde92b0403fb00491b9c1c98002c209c484421441ed11a9af3f7ebf9dde9185cddf854e045b110992a9a19f8010000f0f000104409f80100249e1f9a002eb4d00f99299b19f8010000f00f00184309f80100aae72d9cbcf1000f04fb02f25bd0179b022bded30c9b002b1a9b23d01a9b0d9ccb5c0f5dc3f3031343ea071303550c9b012b234616d00d9b089ccb5c0f5dc3f3031343ea071303550c9b022b234609d0089b079ccb5c095dc3f3031343ea0111234601550d990329b3d30a991144ca18cf5c12f8016cc7f3031747ea0617c61806f8017c12f8017c12f8024cc7f3031747ea041706f8027c12f8027c12f8034cc7f3031747ea041706f8037c12f8037c12f8042cc7f30317043b47ea021206f8042cd4d185e7179c022cfff482af179f082f01d200222be02b9f5f430a9b03eb020c099a174400220ceb020674781cf80250c4f3031444ea0515bc1804f8015cb5787378c5f3031545ea0313bb54f378b578c3f3031343ea051363703379f6780432c3f3031343ea06139245a370dbd10e9bddf82cb0dde9185c002b3ff449af531ccf5c8e5c0e9cc7f3031747ea0617012c87543ff43daf971cce5dcc5cc6f3031646ea04160e9cddf82cb0022cc6543ff42faf0a44d278c95dc2f3031242ea0111c15525e71f9a0021fef7fbb82098ddf800b120eae0702c9004e020440b38013d04464ed02d9911f8010b1f282d91f4d90aeb40011af81000497840ea0122c12afff42cae2499404638ab00f098f800283ff424ae1e98299e049034a8039038a80290dde92a70ddf8809026993a463346cdf80490009401f058f8dde93a013b46204401eb0902b84224eae471b8bf0346b242a8bf32468b42c1dd2c9fba42beddbbf10f0fbbd82c9e169c921b5b1a04ebcb0724f83b100bf1010b7e80bb80fa80ade74ff0000aebe54ff0ff300df2fc4dbde8f08fa01a18bf4ff0ff300df2fc4dbde8f08f00bfb0b586b008b3d0f86c01f0b10023b3eb512f1ad150f82130bbb11b1a9d0714d16ff4403540f60f041d44cff6fd74a5420bd3184450f8085c8d4206d150f80c1ca1f1011515f5803f03d24ff0ff3006b0b0bd50f8045c8d42f7d1c3f540339942f3d80028f1d0037813f00b0fedd1144601ab0022002500f00ff80028e5d00198dde903122060481c40086160a260e0602846256106b0b0bd91429cbf002070472de9f041855c4ff0050c6e0758bf4ff0030c15f0f30f02d1891a614502d20020bde8f0818418627894f802e0002e05d5e078267942ea0e2240ea062e002a4ff0000018bfbef1000fead0b2f5207fe7d8bef5f07f88bfbde8f081082d38bf0120072d18610ad8501c400800fb0ef0a1eb0c018842d3d804eb0c0828e004eb0c080efb02f5002000bf00eb0c04a142c6d008eb00073e78360903d00127b54213d2bde70c1b012cbad07e781eb10227b5420ad2b4e7042cb2d3bc78fe7854ea0626add00427b542aad3ad1b3844dcd158600120c3f800809a60c3f80ce0bde8f0812de9f04f89b0dde91257069005fb07f000294fea4007029100f0d48100200390089002984ff0010e00264ff0000b4ff000084ff0000c4ff000090497079205930be000bf4ff0000c4ff00308cdf80ca009f10109814500f0ba81bcf1000f40f0b4810699b8f1020f11f809a0ead0b8f1010f15d0ddf81cc0b8f1000f40f086805fea1a100af00f01089100f092800499814280f09a804ff0010c4ff0000882e1079abaf1000f00f07f800499514576d3a1eb0a010491089901eb01100901019114e000bfa15d089f01f0f0013943a1550136aaf1010a4ff0010eae4224bf00260bf1010bbaf1000f00f0598113998b45d1d20bfb0324bef1000fe3d0a91bb1eb5a0f88bf4fea5a0141b38d0804eb060817d0002208f10107a2eb850c6ff003057b1904350ceb05020432d870187158719871f5d1059b079a2f1d11f0030504d10ce0002711f0030508d0012d08f8070004d04744022d787018bfb870aaeb410abaf1000f0e440dd0129dae42b5d2a15d019f01f00f013943aaf1010a4ff0000ea155afe7129d4ff0000aa6e7039a52ea0a2213d004990392914280f08f804ff0010c4ff0000853e74ff0000c4ff002084ee74ff0000c4ff00108f4e000214ff0010c4ff00008039142e7089a091a049102eb02111201cdf80090019214e000bf1af80670089a07f0f00717440af80670013601384ff0010eae4224bf00260bf1010b002800f0d680139a934580f0cd800bfb03cabef1000fe2d0ad1bb5eb500ff14688bf45087db3ac080aeb06081ed0002308f10107a3eb840c6ff0030400bf07eb040e04340ceb040304338ef803108ef804108ef805108ef80610f0d1059bddf81cc0271d15f0030404d10ce0002715f0030408d0012c08f8071004d04744022c797018bfb970a0eb45002e4488b1129dae4210d21af80620019f02f00f023a430af8062001384ff0000e0028abd180e000bf129d0020ce469de7891a0491089901eb011009010191114613e000bf1af80620089c02f0f00222430af80620013601394ff0010eae4224bf00260bf1010b002953d0139a9345bff4ccae0bfb03cabef1000fe3d0ad1bb5eb510f88bf4d085db3ac080aeb060819d0002208f10107a2eb840c6ff0030400bf3a1904340ceb04030433d070107150719071f5d1059bddf81cc0271d15f0030404d10de0002715f0030409d0012c08f8070005d04744022c787018bfb87000bfa1eb45012e4489b1129dae42b6d21af80620019f02f00f023a4301394ff0000e0af806200029b0d103e000bf129d0021a4e74ff000084ff0000c029852e64ff0010c4ff0000803e04ff000084ff0000c0298ddf8009045e60120012107e04ff0010cb8fa88f0049f8cf001014009b7fa87f252091140084009b0bde8f08f00bf2de9f041826882b10121816007220020511e20fa01f1c90708bfbde8f0814ff0ff3191400843bde8f0818c46056841688d42e9d06b1c03602c78ff2c11d18b4200f0dc80ac1c046095f90120b2f1ff3fd6b200f3d080bf2e22d806f03f0602238fe062b2b2f1ff3f02dd07222046cfe7bf2c09d804f03f070e224ff0010e4ff000084ff0000c2ee0df2c0fd804f01f0715224ff0000c4ff001084ff0010e22e0df2e0fd806f01f0603236ae0ef2c0fd804f00f071c224ff0010c4ff000084ff0010e10e0ef2e58d806f00f06042358e0f02c40f093804ff0000820224ff0000c4ff0000e00278b424fea072603d0af1c07606d7803e00125856000251f46c02c45ea06046cd35b1a7d1ab3fa83f3b5fa85f55b096d092b434fea042403d001258560002503e07e1c06603d783746b8f1000f44ea050453d17d1ab5fa85f56d092b434fea042403d001258560002503e07e1c06603d783746bcf1000f44ea05043ed1791ab1fa81f1490919434fea042131d001238360002030e0f02e3ed1002605234ff0010e00250127ea074fea062617d18c4215d0621c0260247801379f4246ea040623d000bf8a424fea062609d0541c0460127801371643bb422246f3d116e02246013701254ff0000e9f4214468560dad112e07b1c0360387840ea0104bef1000f20467ff413afbde8f08114465feace7202d0091b8e4204d9012181600020bde8f081a11907462046002263463960bde8f041ffe78446081ab0f5806f01d90020704761454ff00000fad0002a18bf70472de9f04fabb09fed251a9fed250a9fed253a9e4600264ff0ff380df12c090df10c0ab7ee002ab0ee084ab6ee005ab1ee006ab9ee007a00206746019009e000bf49f826500af8064001368f42bc4600f0cb8217f8012b501e01280fd8012a23d18f4274d09cf901000cf10207b0f1ff3fc5b240f38f8000220724f5e1052a37d0042a30d0032a49d1002e00f0d982701e59f820501af8004093e200bf0000004f000000cf000000008f4254d00cf102079cf801008f4247d09cf802200cf103078f4240ea02203fd09cf803200cf104078f4240ea024037d09cf804200cf10507130600223ae0002e00f0aa82013eace7022ec0f0a582b21e59f82230701e59f8205049f820301af802301af800400af8003049f822500af8024096e702f0f80010281fd1002e00f08b82701e1af80030002b40f0858259f82050152a6fd16d420022002406463de20023012206e001220724002588e101220020002343ea000501242fe2a2f12000062827d8002e00f06782731e1af80300002800f0618259f82300242a40f0858080f00045012400221e4617e2bf2d5ad801220292002205f03f000e244ff0000b00928f4200f019819cf802300cf10308002215e1322a51d002f0fe0030284dd0402a40f0a380032ec0f03682701e1af80020002a00f03082b21e1af80230002b00f02a82033e1af80630002b00f0248209eb8202d2ed000a09eb860209eb8000d2ed001ad0ed002a70eee10a62eea00a71eea00acee1002800f00e82023e1af80600002840f0088259f82600122a58dc102a00f03081112a00f03381122a40f02c81454354e1df2d44d8002205f01f00152400924ff0010b0122bae0002e00f0eb81a2f13100013eb0fa80f01af806304009834240f0e08159f82600302a7bd100ee900af8eee00a94e1002b00f0d481023e1af80630002b00f0ce8159f82630212a40f3ed80222a00f02581232a00f02981252a40f03a8100ee903a01ee900af4ee610af1ee10fa48bff0ee601a11ee905a71e1ef2d72d805f00f001c24012274e0a2f11303022b80f0c480002d00f0a381b0f1004f08bf15f1010300f0eb80132a40f0f18090fbf5f5f1e0a2f1480010f1070f4ff00000c0f05f81002e00f05c81013e1af80600002800f0858109eb8600d0ed000af4ee420af1ee10faf5ee400ac8bff0ee420af1ee10fa48bff0ee430a432a72ee601a00f3b480412a00f0f380422a00f00f81432a40f0008161eea10a72ee600a1de1312a40f0948000ee900a20f00040b0f1ff4f4ff00002f4ee410aa8bf0122f1ee10fa4ff0000080f21c81f4ee400af1ee10fa00f11681002a40f01381fdeee00a10ee905a98e0f02d4ff0000040f00981002220244ff0000b009202928f427ff4e7ae00230122b8460002c02d43ea000535d3781aa8eb0103b0fa80f0b3fa83f340095b0918434fea05270ebf18f8013b00230122bbf1000f43ea07051fd1a8eb0103b3fa83f35b0918430ebf18f8013b002301222f02009d002d43ea07050ed1a8eb0103b3fa83f35b0918434fea05230ebf18f8010b0020012240ea0305474602984ff0ff3838b1601e25fa00f0c0071cbf08fa04f005430024a3e0162a10d1a842b8bf054637e0202a4cd0212a51d100ee903a01ee900a70eee10a8ee005442ae0a842c8bf054626e0451b24e0002800f1c680def800200546824238bf15469042019888bf0120019015e0442a48d0452a5fd0462a4ed161eea10a61e00025132a4ff0000208bf4ff00045002469e090fbf5f202fb15050022002462e000ee903a01ee900a61eea00a57e000ee900af5ee400af1ee10fa00f0928001ee903ac1eea00a4ae000ee903a01ee900a71eea00a43e000ee903a01ee900af4ee610af1ee10fac8bff0ee601a11ee905a37e060eea01a70eea00a74ee600a61eea00a2ce0f4ee450af1ee10fa18d570eea01a60eea10a22e0f4ee450af1ee10fa15d560ee861a60eea11a60eea10a16e060eea00a13e060eea01a60eea10a0ee071eea10a61eea00a72ee600a07e061ee870a61eea00a61eea00a70ee820a10ee905a012400224ff000005ab9202e09d0002c3ff436ad25f00040b0f1ff4ffff630ad00202bb0bde8f08f56b3701e1af8001059f82000d9b102ee100a20f00040b0f1ff4f4ff00001b4ee412aa8bf0121f1ee10fa4ff00000e4dab4ee402af1ee10fadfd40029ddd1bdeec20a10ee100a019adef804101143cef804102bb0bde8f08f00202bb0bde8f08f80b5002970d090f900c0bcf1200f5ffa8cfc07db012010604ff0010ec3f800c0704680bdacf1c20ebef11d0f0fd801295ad090f9011011f1410f55dc02211160407800f03f006cf38a108446e4e70cf0f00ebef1e00f23d1032945d390f901e01ef1410f40dc90f9021011f1410f3bdcbcf1e00f5ffa8ef101d1a02934d3bcf1ed0f01d19f292fd80321116042784fea0c3189b2807802f03f0241ea821136e004294ff0000ebbd3acf1f50111f1050fb6d390f901e01ef1410f15dc90f9021011f1410f10dc90f9031011f1410f0bdcbcf1f00f5ffa8ef101d1902904d3bcf1f40f03d18f2901d9002080bd042111604ff4e011427801ea8c4c817802f03f02c0784cea023201f03f0142ea811100f03f0001eb000c7fe72de9f04188b04af2bb261c46c0f24e0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f0812de9f04f91b00df1700b9be8400a10913169ddf868a0002900f05c813768bbf1000f40f05181874200f04e8199f80f100f2940f0498199f80e100e2940f0448199f80d100d2940f03f8199f80c100c2940f03a8199f80b100b2940f0358199f80a100a2940f0308199f80910092940f02b8199f80810082940f0268199f80710072940f0218199f80610062940f01c8199f80510052940f0178199f80410042940f0128199f80310032940f00d8199f80210022940f0088199f80110012940f0038199f80010002940f0fe80ddf86cc0caf10001d6f808e001eaea75ccf10001f76801eaec740895aeeb05062aeaea75391b2ceaec77521b9642a3eb07030d97b8bf32469942b8bf0b46012a0e930992c0f2ea820e990129c0f2e6820ef10101a4464c08089900eb55000c9048100b90042020ea81000790099801310138cde905014810049004fb0cf000f102094ff0000a0a94cde901c507e000bf0e980af1010a8245a14400f0bf821c98109bd0f800e00aeb0c0000fb04e20d980c9e504400fb0360eb0703d10b9edde9083810e00b9b2f46d35c079d0678eb4006f0f00603f00f03334400f8013b069bdde904683d469119da074fea58070f9208d13a468b466446f1f74affa4460a9c68e000bf8b46b8f1020f63d37a1e032a04d2002117f0030237d15be06ff0030202ea58034e44c3f1000c0eeb0601461c039700bf8d18ef78ac78c7f3031747ea0417b418e7702f79eb78c7f3031747ea031323716b792f79c3f3031343ea07136371ab796f79c3f30313043243ea07130ceb02070437a371dcd1dde901c50a9c039f111d17f0030224d04b1cbe461bf803701bf80160c7f3031747ea0617012a4754774616d08e1c1bf806701bf80350c7f3031747ea0517029d022ac754774608d05944c9781bf80620c1f3031141ea021181555feac8713ff45aaf0f9a1bf80710002a4ff0000208bf0422c35dd14003f00f0242ea0111c15549e7874200f09f815c460021aae17568002d00f0068200214ff0000e09e0dde90e71d8f804503944dde91d9ba94280f0f881ddf870800f91d8f80060775c3c0901d0012117e06d1a012d00f0ea8106eb010c9cf8016016b1344602210be0042dc0f0df819cf802509cf8036055ea062400f0d781042117f00f07bbfa8bf60e914fea561619f807b018bf012746ea07091b9f4fea0b1c14f00301754600f09980d8f808504ff00008befbf5f60d9106fb15e55544b5f1ff3fc8bf4ff0010809ea080101291ad1f61918d4954216d29e4214d210990b9406fb01046e08a15ded070c916ff00f05594604bf0f256146ddf8308005ea08052943a1550b9c0d99ddf8708001290ef1010563d0d8f80810b5fbf1f606fb115101eb0a050021b5f1ff3fc8bf012101ea090101291cd1f6191ad4954218d29e4216d2109906fb01016e080b91895ded070c916ff00f05594604bf0f256146ddf8308005ea080529430b9dddf87080a9550d990ef10205022930d0d8f80810b5fbf1f606fb115101eb0a050021b5f1ff3fc8bf012101ea090101291cd1f6191ad4954218d29e4216d2109906fb01016e080c91895ded070d916ff00f05594604bf0f256146ddf8348005ea080529430c9dddf87080a9550ef1030500bf611e0329a64405d220e700bf0435ae453ff41cafd8f808100024b5fbf1f606fb11515144b1f1ff3fc8bf012404ea0904012c19d1f61917d4914215d29e4213d2109c5f4606fb04044e08c90714f806806ff00f0104bf0f21674601ea0801ddf8708039431b9fa155d8f808106c1cb4fbf1f606fb114100245144b1f1ff3fc8bf012404ea0904012c19d1f61917d4914215d29e4213d2109c5f4606fb04044e08c90714f806806ff00f0104bf0f21674601ea0801ddf8708039431b9fa155d8f80810ac1cb4fbf1f606fb114100245144b1f1ff3fc8bf012404ea0904012c19d1f61917d4914215d29e4213d2109c5f4606fb04044e08c90714f806806ff00f0104bf0f21674601ea0801ddf8708039431b9fa155d8f80810ec1cb4fbf1f606fb114100245144b1f1ff3fc8bf012404ea0904012c7ff46aaff6193ff567af9142bff464af9e42bff461af109c5f4606fb04044e08c90714f806806ff00f0104bf0f21674601ea0801ddf8708039431b9fa1554be71b995c46002901dd012108e0b1fa81f149090027baf1000fc8bf01273940d6f808e0f76807fb0ef8b8f1000f56d0b4fa84f4bb4600274ff0ff3c6409c9070e940f9106e001370bfb0ef84745acf1010c44d20f990ef101050029394618bf08eb0c01b1fbfef46d0804fb1e161c99096804fb05117508495d042525ea8605e94011f00f08454618bf01250e990d43012dd8d116eb0a065cbf1b9d14eb0504d1d49642cfd29c42cdd2109d19f8081004fb050e74081ef80450f60705f00f0805f0f00545ea010508bf48ea01151c990ef80450d1f808e0d1f80cb0b3e711b0bde8f08f00bf2de9f04fd2b08a46dde95b125e9f11447a1821eae17b5245b8bf9246d3450c9080f221815d99dde95f2001f0010711440b914fea5b01033808910bf101010090580007914fea5a050a90002101200df1480e01970493cde902ba099507e000bf0698012100284ff0000000f0fc80c00706906098059108bf0830ddf800a06ff007086ff0040c4ff0000b4ff000090e9018bf0af1080acdf834a000bf0e994fea5900085c102400f0f0030f90cde910c81af8040f0eeb08021af8011c9af8015043ea111701f00f011943517243ea101100f00f00917218430eeb0c01087243ea15109af802601772487205f00f001843887243ea1610907306f00f00184308f10808083c0cf1080cd073d1d10f98ddf834a00201102350465c4600bf10f8041f0eeb040610f8017c457842ea171807f00f071743777442ea111701f00f011143f17442ea151190f802c0317505f00f011143717542ea1c11b1750cf00f011143083b04f1080486f81080b774f175d5d1ddf84480ddf840c009f1020908f120080cf1200cb9f1100f0bf1200b86d101980599049b48405f99dde902ba099d8842bff45eaf5d9900eb01090c9803fb090808e000bf0b9809f1020981450a988044bff44eaf0c98dc4609fb03015feacb700bd0089fddf81cc0c85d1ef8002000f0f00002f00f021044c855b5eb5c0f23d94fea5c004f194646315c32181ef80140114601f8014bb9421cd20c781ef80450941cbc420d7016d221781ef801102170d41cbc420fd2217804361ef8011021703418bc42e0d306e001eb5c045feaca7005d1b7e7541c099d5feaca70b2d0d445b0da20781ef8001000f00f0001f0f00108442070a6e752b0bde8f08f2de9f04f87b0149d6c68072c79d824b12f89b94275d0012c70d1002871d0d0f86c61002e6dd00027b7eb512f69d156f82170002f65d0a7eb060c5fea8c7760d16ff440370ceb070e40f60f07cff6fd77be4556d306eb0c0e5ef8086c8e4250d15ef80c6ca6f1011717f5803f49d35ef8047cb74245d1ccf54037be4241d8bef1000f3ed0032e3cd39ef80070022f38d19446aa69ddf84890dde91087ddf84cb09a469ab1c3464fead10801f0070401234f4612f80890a34043ea090302f80830b946d846ddf84cb0119f6c6805eb4402631c6b6011810ef10101721e63460595cde9039bcde90187cdf800a0fbf718ff69680139696007b0bde8f08f6f89b94204d14ff0ff3007b0bde8f08f022c3ff484afaf89b942f4d0032c3ff47eafef89b942eed0042c3ff478af2f8ab942e8d0052c3ff472af6f8ab942e2d0062c3ff46cafaf8ab9427ff468afdae722eae27c8b42b8bf19468c45a8bf704770b5dde904231ab913b1bde870407047b2f10f0e18bf9e465feacc730fd04fea5c03c45cbef1000f03d004f00f05aa4203d904f0f0041443c4540cf1010c4fea5c0400eb5c034d08bef1000f00eb510049d0ac425fd200bf1c78164604f00f05b2eb141f38bf2609954298bf154645ea06141d4605f8014b854267d25c78164604f00f05b2eb141f38bf2609954298bf15469c1c45ea061584425d7040d29c78164604f00f05b2eb141f38bf2609954298bf1546dc1c45ea061584429d702fd2dc78164604f00f05b2eb141f38bf2609954298bf154645ea06151c1ddd7084422346b9d31ce0ac4219d242ea021500bf1c4604f8015b844228d29c1c84425d700ed2dc1c84429d700ad21c1ddd7084422346edd304e01c46c80704d175e71c46c8073ff472af8c45bff46faf2078bef1000f03d0b2eb101f7ff667af00f00f0040ea02102070bde8704070475c1cc807e9d15ae72de9f04f81b04ef66c6b4ff29c554ff62c4848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0bd804ff47570f8f7e9fd002800f02e81414600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff43bafbff35f8f00bf45e8000121b155e8001fa142f8d02fe7d0f87812002974d104464ff41630f6f7cbfa014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8781200e00021084601b0bde8f08f00bf70b5044690f8800384f8810350b3d4f8700347f64c6642f66535c2f20706c0f2440520b131682d31c4f8781310e040f22f00c0f200007844002122460023a847c4f87003316801f12d01c4f8781330b105f1e8022d219047002808bf70bd002084f8810370bd00bf2de9f047a4b0d0f87442bff35f8fa4b142f691760546c0f2440620460021b04770b1d5f8700340b142f64d42c0f244022d2124b0bde8f047104724b0bde8f08747f64c69c2f20709d9f8007095f8810300283fd0f7f7dafce0b3d5f87813781a17d495f8640110b995f80403e0b395f8800385f8810380b3d5f8700342f64d48c0f2440878b1d9f800102d31c5f878131de0d5f8700300b342f64d42c91bc0f24402904719e04ff64b70cff6ff70a8f1e807784400212a460023b847c5f87003d9f8001001f12d01c5f8781310b12d21c04710b1002085f8810306f16601204624b0bde8f04708474af21f37c0f2470700200290adf80c00b84795f8640110b995f8040318b12846fff746ffe5e70df104084046f5f7f0fe02a9284600224ff0000afbf72dfa81464046f6f72df8b9f1000f02d085f881a303e00146284600f009f89df80c000028c7d107f14c008047c3e700bf2de9f04f90f8b13300f5617c4cf82310591c41f69a134b4390f8b0b31b0c03eb8303a1eb4301bbf1090f80f8b1139cbf0bf1010b80f8b0b3bbf1040f0bf0030e05d200244ff000094ff0000a2ee00bf0fc0800244ff000094ff0000a00eb8406d6f88473d6f88853b7fbfbf3d6f88c1303eb090203fb1b73b5fbfbf7d6f8906307fb1b5553443a44b1fbfbf72b44b6fbfbf53a4407fb1b1105eb020905fb1b6219440434a04502eb010ad7d1bef1000f1dd05cf82450bef1010fb5fbfbf606fb1b55b144aa4412d00ceb84056e68bef1020fb6fbfbf404fb1b66a144b24406d0ae68b6fbfbf505fb1b66a944b244bafbfbf202eb0901c0f8ac13bde8f08f00bff0b500294ff6ff7e00f0af80012903d10022c90775d1a8e021f00102c2f1000c6ff0010241f221038418a578e7788eea05252cb26e0036b203ead43484ea450503ead6366c0086ea450624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead53585ea460686ea07273eb27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead636023286ea470703ead5360ceb0205023586ea470e95d10232c90733d0805c41f221038eea002001b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead23181ea400e1ffa8ef0f0bd2de9f04fcbb00646c07c8b468df80600b08954998df80700000a8df808008df80a10080af1898df80b00080a17468df80d000020012b8df805308df809b08df80c108df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f008df820008df821008df822004cd1307e96f81780a0f1090144f62510c2f29240a1fb0002891a02eb510100970df10500b8eb910f88bf4fea9108c8ebc80706f11a0400f1010a00f10509093021463a46f0f73bfe06f1210506f1280029460722f0f733fe284621460722f0f72efe20465146032207f10905f0f727fe06f11d0049460422f0f721fef07d02289cbf0130f075009fbb450cd0b8f1240f13d94ff0ff304bb0bde8f08fbb454ff00905f07509d14ef60b67abb20df10502c0f247070120f02115e002200df12304aab28df82300a01c0df105018df82470f0f7f5fda81c4af6610782b2c0f24607f02021460023b8474bb0bde8f08f2de9f04184b0044690f84d0000284ff0000000f09a8094f84f1042f28b0701f078018df8071094f85010c0f25b0701f007018df80610cde9020002a906200022b847054670b9002002adcde902000df1070128460122f0f7bbfd012029460122b847054694f84e00a8b1002602a906200022cde90266b84760b9cde9026602ae04f1540130460122f0f7a2fd572031460122b8470543002602a906200022cde90266b84768b90df108080df1060140460122cde90266f0f78bfd312041460122b847054302a906200022cde90266b84768b9002002ae04f15301cde9020030460122f0f775fd462031460122b8470543002602a906200022cde90266b84768b90df1080804f1510140460222cde90266f0f75efd362041460222b847054302a9a3200022cde90266b847054302a997200022cde90266b84742f62b310543c0f2440102208847284615b104b0bde8f081002184f84e1084f84d10a16504b0bde8f08100bf2de9f04f8bb00190233002254ff000080df10c0a4ff001094ff0000b002402900be000bf09fa0bf0002f0bf1010b08bf0443bbf1070f354426d041f66320c0f20000784410f80b0041f65d21c0f20001794411f80b6042f2fb0351463246c0f25b03cde90988cde90788cde90588cde90388984707460028d4d10298514628443246f0f7f3fccde742f2fb00c0f25b004ff60073002600f1340703a9c0f21f0381200c22cde90966cde90766cde90566cde90366b84730b9029803a928440c22f0f7d4fc8026ddf804c040f6b80146ea0400220ac2f200018cf823008cf824200a68002a00f0ad8045f2fd774ff6ff766ff0020241f22103c0f202070d681544ed7886ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4342d7984ea460686ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4346d7984ea460686ea052635b2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead4350332ba4285ea46067ff460afb1b2090a01e0ff26ff218cf83e109cf84c109cf84d208cf83f1026218cf83d608cf84020ff288cf84b101cbf04208cf81c000bb0bde8f08f70b588b042f2fb0514460e46c0f25b05002181280791cde90511cde90311cde90111009107d11b023435694681202246a84720b905e069462246a84708b108b070bd6946054630462246f0f7d5fb284608b070bd2de9f0478cb08579972d17d00446fe2d0fd0fd2d2ad142f69570c0f25b008047002084f84e0084f84d00a0650cb0bde8f08720460cb0bde8f047cbe50020cde9040042f2fb00c0f25b00a0f1700304a9972000229847044642f62b31c0f244010220884720460cb0bde8f08742f2fb08002094f80c90677bc0f25b08cde90a00cde90800cde90600cde9040004a99f200422c047064620b901a804a90422f0f781fb002e6ff00100ddd19df80410bd29d9d19df805104029d5d19df806101029d1d194f84d00002852d047f64c60c2f20700006843f698210844a065002604a937200222cde90a66cde90866cde90666cde90466c047002840f0898002a804a90222f0f74ffb04a947200122cde90a66cde90866cde90666cde90466c047002875d10df1030004a90122f0f73bfb0020cde90a00cde90800cde90600cde9040004a905200122c047002860d10df1020004a9012249ea0726f0f724fb352d5bdc002d00f08180012d70d19df8020082e0002604a9052001228df80e60adf80c600296cde90a66cde90866cde90666cde90466c047d8bb0df1080a04a950460122f0f700fb04a935200122cde90a66cde90866cde90666cde90466c04738bb0af1010004a90122f0f7edfa002604a937200222cde90a66cde90866cde90666cde90466c04798b90af1020004a90222f0f7d9fa04a947200122cde90a66cde90866cde90666cde904660126c04778b14ff0ff300cb0bde8f087362d6ed0572d15d194f84e00002800f08580572039e00af1040004a90122f0f7b5fa04f14f0051460722f0f7affa84f84d603ee700208df801006946284601228df8009000f04ef9312d044627d036e09df80200002e00f0380618bf4036704010f0380f10d09df808009df8091041f25a530e22c0f2000302ea96027b449a5a41ea0020904239d8012000218df80110694601228df8006000f024f904460df103014620012200f01df9054602a93620022200f017f945ea040141ea00040025a8f1700604a9a3200022cde90455b047044304a997200022cde90455b0470443aee69df802000e2101ea900041f2e241c0f200017944085a864204d96ff001000cb0bde8f0876946362002228df800708df8019000f0e6f80446d0e70020cde90a00cde90800cde90600cde9040004a9592001220125c04700287ff45caf04f1540004a90122f0f721fa84f84e5060e740f6b800c2f200000168002904bf4ff0ff30704780b584b000684ef2173102904ff41630039002a8c0f2470188474ff4f07042f29f7e01904ff4207cc0f25b0e0020002100220023cdf800c0f04704b080bd00bf2de9f04340f6b801c2f2000109684ff0000e01f1020c4cf6cd4340f2762808e00ef1010ebef5f07f0cf5a07c08bfbde8f083aef1a0096ff00906644607e000bf07eb0711a170053445452e46e8d290f84c1006f10a054f1e112905fb03f208bf570d122908d1a6f1e6019f294ff000079cbfb9f19f0f077b07eb071104f8021c90f84c1006f10c024f1e112903fb02f208bf570d122908d1a6f1e4019f294ff000079cbfb9f19f0f077b07eb071104f8011c90f84c1006f10e024f1e112903fb02f208bf570d122908d1a6f1e2019f294ff000079cbfb9f19f0f077b07eb0711217090f84c1006f110024f1e112903fb02f208bf570d122908d1a6f1e0019f294ff000079cbfb9f19f0f077b07eb0711617090f84c1006f112024f1e112903fb02f208bf570d12298ad1a6f1de019f294ff000079cbfb9f19f0f077b80e700bf2de9f04383b042f28b0914460e460546c0f25b090027694606200022cde90077c84710b103b0bde8f083e846404631462246cde90077f0f737f9284641462246c84703b0bde8f08370b50446d0f83c010d4638b92046f6f7fffd0028c4f83c0108bf70bd42f64d46c0f2440606f140018847d4f83c0129463246bde8704010472de9f04f81b0934612788ab1022300bf0beb030717f8012c62b11bf8032062b17a784ab1ba780433002af1d1023b04e0002205e0013b00e0013303eb4302520042eae272931c02f1010c01eb810203f0030e23f0030a00eb8213a3f5a0784ff0ff3906e009f10109b9f10d0f08f5a0786bd009eb010202eb8202bcf1030f00eb821605d24ff0ff34bef1000f2ed1e9e70024474604e000bfa24507f1020721d0621cd209042a03d83a7802f0f0023a70a21cd209042a03d87a7802f00f027a70e21cd209042a03d87a7802f0f0027a700434e209042adfd86208b55c05f00f05b554d9e7013cbef1000fbbd0a21cd509042d08d85208b55ce7076ff00f0708bf0f273d40b554bef1010fabd0e21cd509042d09d85208b55ce7074ff00f0708bf6ff00f073d40b554bef1020f9ad0221dd509042d96d85208e407b55c6ff00f0408bf0f242c40b4548ce79bf80010cdf800b0002900f0ce80022041f2410a41f2420b41f2430808e0009a063012f8011f03330029009200f0bd80a1f17f0717f15f0ff1d340f65d42c0f2000201eb41017a4402eb81014ff20014a1f5c079cff6ff7402e014f52074ded0494619f9027bc509042d09d8b7f1ff3f06dc1a1992f8016f46f0f00682f8016f5ffa87fe5fea4e6208d5042d06d81a1992f8017f47f00f0782f8017f5fea8e620ad540f27d22904206d81a1992f8027f47f0f00782f8027f5feace6209d5b0f51f7f06d81a1992f8027f47f00f0782f8027f5fea0e724fea900c09d5bcf19e0f06d81a1992f8036f46f0f00682f8036f5fea4e720ad540f27a22904206d81a1992f8037f47f00f0782f8037f91f90110042d09d8b1f1ff3f06dc1a1912f80a7047f0f00702f80a70c9b24a0608d5042d06d81a1912f80a7047f00f0702f80a708a060ad540f27d22904206d81a1912f80b7047f0f00702f80b70ca0609d5b0f51f7f06d81a1912f80b7047f00f0702f80b700a0709d5bcf19e0f06d81a1912f8087047f0f00702f8087049077ff565af40f27a2188423ff660af191911f8082042f00f0201f8082057e701b0bde8f08f00bf2de9f04f81b0134681464a1c00f08f808e0a4ff0000b19f80b20002a00f0888109eb0b077a78002a00f00a81ba78002a00f00981fa78002a00f008813a79002a00f007817a79002a00f00681ba79002a00f00581fa79002a00f004813a7a002a00f003817a7a002a00f00281ba7a002a00f00181fa7a002a00f000813a7b002a00f0ff807a7b002a00f0fe80ba7b002a00f0fd80fa7b002a00f0fc803a7c002a00f0fb807a7c002a00f0fa80ba7c002a00f0fb80fa7c002a00f0fc803a7d002a00f0fd807a7d002a00f0fe80ba7d002a00f0ff80fa7d002a00f000813a7e002a00f001817a7e002a00f00281ba7e002a00f00381fa7e002a00f004813a7f002a00f005817a7f002a00f00681ba7f002a00f00781fa7f002a00f008810bf1200bbbf1600f7ff477af4ff0600b00e1002119f80120002a00f0af8109eb01056a78002a00f0b080aa78002a00f0b180ea78002a00f0b2802a79002a00f0b3806a79002a00f0b480aa79002a00f0b580ea79002a00f0b6802a7a002a00f0b7806a7a002a00f0b880aa7a002a00f0b980ea7a002a00f0ba802a7b002a00f0bb806a7b002a00f0bc80aa7b002a00f0bd80ea7b002a00f050812a7c002a00f04e816a7c002a00f04c81aa7c002a00f04a81ea7c002a00f048812a7d002a00f046816a7d002a00f04481aa7d002a00f04281ea7d002a00f040812a7e002a00f03e816a7e002a00f03c81aa7e002a00f03a81ea7e002a00f038812a7f002a00f036816a7f002a00f03481aa7f002a00f03281ea7f002a00f03081203160297ff479af32e10bf1010b74e00bf1020b71e00bf1030b6ee00bf1040b6be00bf1050b68e00bf1060b65e00bf1070b62e00bf1080b5fe00bf1090b5ce00bf10a0b59e00bf10b0b56e00bf10c0b53e00bf10d0b50e00bf10e0b4de00bf10f0b4ae00bf1100b47e00bf1110b44e00131f6e00bf1120b3fe00231f1e00bf1130b3ae00331ece00bf1140b35e00431e7e00bf1150b30e00531e2e00bf1160b2be00631dde00bf1170b26e00731d8e00bf1180b21e00831d3e00bf1190b1ce00931cee00bf11a0b17e00a31c9e00bf11b0b12e00b31c4e00bf11c0b0de00c31bfe00bf11d0b08e00d31bae00bf11e0b03e00e31b5e00bf11f0bb1f5205f4ff0010100931fd364234ff47a7442f2107700bf01eb81025200b6fbf2f50a2d11d301fb03f2b6fbf2f50a2d0bd301fb04f2b6fbf2f50a2d05d37943b6fbf1f2092ae7d800e011465846bbf15f0f98bf5f20c0f1000c1ceb0b0a53d0b6fbf1f74cf6cd4207f13003ccf6cc4209f80b300a29a1fb023547d31af1010342d007fb11674fead508b7fbf8fe48f21f520ef1300309eb0b06c5f2eb1273706429a1fb023434d31af102032cd00efb18776409b7fbf4f544f6d35205f13003c1f26202b370b1f57a7fa1fb022323d31af1030218d005fb14729b0941f25975b2fbf3f7cdf2b715a1fb055407f13005f57007fb132642f210720bf1040b91424fea5431a9d209e0834607e00bf1010b04e00bf1020b01e00bf1030b0020009b09f80b002de00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f020a7003d1002189f85f1001e000204870002119f80100002800f0a18009eb0105687800285ed0a87800285dd0e87800285cd0287900285bd0687900285ad0a879002859d0e879002858d0287a002857d0687a002856d0a87a002855d0e87a002854d0287b002853d0687b002852d0a87b002851d0e87b002850d0287c00284fd0687c00284ed0a87c00284dd0e87c00284cd0287d00284bd0687d00284ad0a87d002849d0e87d002848d0287e002847d0687e002846d0a87e002845d0e87e002844d0287f002843d0687f002842d0a87f002841d0e87f002840d0203160297ff498af44e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2905d849444ff02f00087040f09580002089f85f00581c00f094809d0a4ff0000e19f80e00002800f08d8109eb0e067078002800f00f81b078002800f00e81f078002800f00d813079002800f00c817079002800f00b81b079002800f00a81f079002800f00981307a002800f00881707a002800f00781b07a002800f00681f07a002800f00581307b002800f00481707b002800f00381b07b002800f00281f07b002800f00181307c002800f00081707c002800f0ff80b07c002800f00081f07c002800f00181307d002800f00281707d002800f00381b07d002800f00481f07d002800f00581307e002800f00681707e002800f00781b07e002800f00881f07e002800f00981307f002800f00a81707f002800f00b81b07f002800f00c81f07f002800f00d810ef1200ebef1600f7ff477af4ff0600e05e100204870581c7ff46caf002119f80100002800f0aa8109eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04b81107c002800f04981507c002800f04781907c002800f04581d07c002800f04381107d002800f04181507d002800f03f81907d002800f03d81d07d002800f03b81107e002800f03981507e002800f03781907e002800f03581d07e002800f03381107f002800f03181507f002800f02f81907f002800f02d81d07f002800f02b81203160297ff479af2de10ef1010e74e00ef1020e71e00ef1030e6ee00ef1040e6be00ef1050e68e00ef1060e65e00ef1070e62e00ef1080e5fe00ef1090e5ce00ef10a0e59e00ef10b0e56e00ef10c0e53e00ef10d0e50e00ef10e0e4de00ef10f0e4ae00ef1100e47e00ef1110e44e00131f1e00ef1120e3fe00231ece00ef1130e3ae00331e7e00ef1140e35e00431e2e00ef1150e30e00531dde00ef1160e2be00631d8e00ef1170e26e00731d3e00ef1180e21e00831cee00ef1190e1ce00931c9e00ef11a0e17e00a31c4e00ef11b0e12e00b31bfe00ef11c0e0de00c31bae00ef11d0e08e00d31b5e00ef11e0e03e00e31b0e00ef11f0e0122b3f5205f1ed364234ff47a7442f2107602eb82004100b5fbf1f00a2811d302fb03f1b5fbf1f00a280bd302fb04f1b5fbf1f00a2805d37243b5fbf2f00928e7d800e00a46f046bef15f0f98bf4ff05f08c8f1000c42f2107a1ceb0e074dd0b5fbf2f30a2a03f1300009f80e004cf6cd40ccf6cc40a2fb000641d3781c3dd003fb1253f608b3fbf6f409eb0e0504f13000687048f21f50c5f2eb10642aa2fb000130d3b81c29d004fb16334e09b3fbf6f4b2f57a7f04f13000a87044f6d350c1f26200a2fb000120d3f81c16d004fb1630890941f25974b0fbf1f3cdf2b714a2fb044603f13004ec7003fb11050ef1040e52454fea5632afd209e0c64607e00ef1010e04e00ef1020e01e00ef1030e002009f80e0001b0bde8f08f0f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f00087005d1002089f85f0001b0bde8f08f0020487001b0bde8f08f434d01ffff0000000000000052420100ff00000008031000000000000000000046616365636c61772f3334000000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a89000000000000000009f0535593747c181040101010201020c005353a829c51bd314a810260d5b0bfe090000", "desc": "append injected blobs to main-app payload" }, { "offset": 779123, "old": "805a3900", - "new": "f0173a00", + "new": "e06a3a00", "desc": "main-app subheader payload size (ps)" }, { "offset": 152, "old": "005b3900", - "new": "70183a00", + "new": "606b3a00", "desc": "main-app TOC entry size (ps + 128)" }, { "offset": 779243, "old": "805a3904", - "new": "f0173a04", + "new": "e06a3a04", "desc": "main-app preamble length (low 24 bits)" }, { "offset": 779247, "old": "effdbcea", - "new": "ed6be905", + "new": "38f29188", "desc": "[5] ota/s200_firmware_ota.bin preamble crc32" }, { "offset": 156, "old": "c0c6322a", - "new": "18540264", + "new": "f8d63d83", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)" }, { "offset": 779127, "old": "c0c6322a", - "new": "18540264", + "new": "f8d63d83", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)" } ] diff --git a/app/g2/flash-prompt-protocol.ts b/app/g2/flash-prompt-protocol.ts deleted file mode 100644 index d4e166e4..00000000 --- a/app/g2/flash-prompt-protocol.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { bytes, concat, integer, readBytes, readInteger, readString, type ProtocolMessage } from './ble-protocol' -const text = (field: number, value: string) => bytes(field, new Uint8Array(Array.from(value, ch => ch.charCodeAt(0)))) -export function createFlashPrompt(magic: number, warning: string): Uint8Array { - const geometry = (y: number, height: number, id: number, name: string) => concat( - integer(1, 0), integer(2, y), integer(3, 280), integer(4, height), integer(9, id), text(10, name)) - const list = concat(geometry(150, 120, 2, 'flashmenu'), bytes(11, concat(integer(1, 2), integer(3, 1), text(4, 'No, cancel'), text(4, 'Yes, flash'))), integer(12, 1)) - const label = concat(geometry(0, 130, 1, 'flashwarn'), text(12, warning)) - return concat(integer(1, 0), integer(2, magic), bytes(3, concat(integer(1, 2), bytes(2, list), bytes(3, label), integer(5, 10000)))) -} -export function flashPromptSelection(message: ProtocolMessage): boolean | null { - if (message.sid !== 0xe0 || ![1, 6].includes(message.flag)) return null - const event = readBytes(message.payload, 13), list = event && readBytes(event, 1) - if (!list || readString(list, 2) !== 'flashmenu' || readInteger(list, 5, 0) !== 0) return null - const index = readInteger(list, 4, -1), name = readString(list, 3).toLowerCase() - if (index === 1 || name.startsWith('yes')) return true - if (index === 0 || name.startsWith('no')) return false - return null -} diff --git a/app/g2/glance-host.ts b/app/g2/glance-host.ts index 002fc147..845e4cf8 100644 --- a/app/g2/glance-host.ts +++ b/app/g2/glance-host.ts @@ -23,7 +23,7 @@ export const GLANCE_SURFACE_Z_ORDER = 900; /** Surface operations needed by the board, shared by Android and iOS. */ export type GlanceDisplay = Pick; + "configureSurface" | "setSurfaceVisible" | "setSurfaceDepth" | "setScreenBlanked" | "submitSurfaceFrame">; export type GlanceHostOptions = { getDisplay: () => GlanceDisplay | null; @@ -52,6 +52,8 @@ export class GlanceHost { private shown = false; private hideTimer: ReturnType | null = null; private configuredFor: GlanceDisplay | null = null; + /** Last depth given to the surface; null forces the next render to set it. */ + private configuredDepth: number | null = null; /** Serializes show/hide against each other and against in-flight renders. */ private queue: Promise = Promise.resolve(); private rendering = false; @@ -158,6 +160,7 @@ export class GlanceHost { transparency: "opaque", }); await display.setSurfaceVisible(GLANCE_SURFACE_ID, false); + this.configuredDepth = null; this.configuredFor = display; } @@ -256,6 +259,12 @@ export class GlanceHost { const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes)); const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)); + // The glasses copy the screen shifted per lens while the board covers it. + const depth = this.options.getProvider()?.depth() ?? 0; + if (depth !== this.configuredDepth) { + await display.setSurfaceDepth(GLANCE_SURFACE_ID, depth); + this.configuredDepth = depth; + } await frameTimings.spanAsync(frameId, "submit", () => display.submitSurfaceFrame( GLANCE_SURFACE_ID, diff --git a/app/g2/glasses-session.ts b/app/g2/glasses-session.ts deleted file mode 100644 index c3dd390f..00000000 --- a/app/g2/glasses-session.ts +++ /dev/null @@ -1,638 +0,0 @@ -import { setBrightness } from './brightness-protocol' -import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client' -import { type CompassEvent } from '../native/compass-types' -import * as protocol from './ble-protocol' -import { iosBleTraffic } from './ble-traffic-counters' -import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer' -import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport' -import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat' -import { hexToBytes } from '../util/hex-util' -import { deviceAddressError } from './ios-peripheral-identity' -import { decodeWearState, enableWearDetection, queryWearState } from './wear-protocol' -import type { SessionTexturePlanner, TextureFrame } from './texture-planner' - -declare function setTimeout(callback: () => void, ms: number): number -declare function clearTimeout(id: number): void -export type SessionAddresses = { left: string; right: string; ring: string } -export type TransportEvent = { kind: string; identifier?: string; characteristic?: string; data?: string; message?: string; state?: number; authorized?: boolean } -export interface SessionTransport { - onEvent(listener: (event: TransportEvent) => void): () => void - resolveDevices(addresses: SessionAddresses): Promise> - connect(identifier: string, requiresANCS?: boolean): Promise<{ characteristics: string[]; maxWrite: number }> - subscribe(identifier: string, characteristic: string): Promise - write(identifier: string, characteristic: string, bytes: Uint8Array): Promise - disconnect(identifier: string): void - stopScan(): void -} -export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'; - status: string; battery: number | null; charging: boolean | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string } -type PendingAck = { resolve: (message: protocol.ProtocolMessage) => void; reject: (error: Error) => void; - timer: number | null; command: number; label: string } -class AckTimeout extends Error {} -// Match Android ConnectionOptions.WINDOW_SIZE. This bounds complete image -// messages queued for writing or awaiting ACK, not individual BLE packets. -const DISPLAY_WINDOW_SIZE = 3 -type CfwPending = { payload: Uint8Array; checksum: number; role: string; magic: number; ackLenses: number; retries: number; - retryPending: boolean; deadline: number; resolve: () => void; reject: (error: Error) => void } -type DisplayFrame = { packed: Uint8Array; commands: Uint8Array[]; offset: number; pending: number } - -/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */ -export class GlassesSession { - state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false, - frames: 0, capabilities: '', leftVersion: '', rightVersion: '' } - readonly notifications: AncsClient - private generation = 0 - private ids: Record = {} - private limits: Record = {} - private cfwPending: CfwPending[] = [] - private cfwTransports = new Map() - private cfwTimer: number | null = null - private pending = new Map() - private receiver = new protocol.MessageReceiver() - private writes = new Map>() - private magic = 100 - private sequence = 0x40 - private timer: number | null = null - private retryTimer: number | null = null - private ringRetryTimer: number | null = null - private ringRetryCount = 0 - private ringConnecting = false - private retryCount = 0 - private wanted = false - private addresses: SessionAddresses | null = null - private latest: Uint8Array | null = null - private latestTextures: TextureFrame | null = null - private displayed: Uint8Array | null = null - private lastEnqueued: Uint8Array | null = null - // Match Android: advance only for emitted deltas, skipping 0 / 0xffff. - private nextImageFrameId = 1 - private displayFrames: DisplayFrame[] = [] - private displaySending: DisplayFrame | null = null - private displayInFlight = 0 - private pumping = false - private lastHeartbeat = 0 - private lastLease = 0 - private lastSettings = 0 - private charging = false - private layoutCreated = false - private off: () => void - private stopping: Promise | null = null - private audioListener: ((packet: Uint8Array) => void) | null = null - private microphoneToken = 0 - private microphoneWork: Promise = Promise.resolve() - private microphoneEnabled = false - private compassWanted = false - private compassSent: boolean | null = null - private compassStopping = false - private compassWork: Promise = Promise.resolve() - constructor(private readonly transport: SessionTransport, - private readonly onState: (state: SessionState) => void, - private readonly onInput: (input: protocol.GlassesInput) => void, - private readonly log: (message: string) => void = () => {}, - private readonly onActivity: () => void = () => {}, - private readonly onCompass: (event: CompassEvent) => void = () => {}, - private readonly onWearState: (wearing: boolean) => void = () => {}, - onNotificationsChanged: (key?: string, popup?: boolean) => void = () => {}, - private readonly textures?: SessionTexturePlanner) { - this.notifications = new AncsClient(packet => this.writePackets('right', () => [packet]), onNotificationsChanged, message => this.log('ANCS: ' + message)) - this.off = transport.onEvent(event => this.receive(event)) - } - private update(phase: SessionState['phase'], status: string): void { - this.state = { ...this.state, phase, status }; this.log(status); this.onState({ ...this.state }) - } - async start(addresses: SessionAddresses, retry = false): Promise { - if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return - const invalid = deviceAddressError(addresses) - if (invalid) { this.update('error', invalid); return } - this.addresses = { ...addresses }; this.wanted = true; this.compassStopping = false - if (!retry) this.retryCount = 0 - const generation = ++this.generation - this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false } - this.charging = false - this.update('connecting', 'Finding configured devices…') - try { - const ids = await this.transport.resolveDevices(addresses); this.check(generation) - this.ids = ids - for (const role of ['right', 'left']) { - this.update('connecting', `Connecting ${role} arm…`) - const details = await this.transport.connect(ids[role], role === 'right'); this.check(generation) - this.limits[role] = details.maxWrite - if (!details.characteristics.includes(protocol.G2_WRITE) || !details.characteristics.includes(protocol.G2_NOTIFY)) throw new Error(`${role} arm is missing G2 communication characteristics.`) - await this.transport.subscribe(ids[role], protocol.G2_NOTIFY); this.check(generation) - } - this.update('connecting', 'Authenticating… Accept pairing if iOS asks.') - await Promise.all(['right', 'left'].map(async role => { - try { - const answer = await this.request(role, protocol.SID.auth, protocol.authentication, 'Security authentication', 30_000, 0) - if (!protocol.authenticationSucceeded(answer, answer.magic)) throw new Error(`${role} arm rejected authentication.`) - } catch (error) { - if (!(error instanceof AckTimeout)) throw error - // Older CFW may not implement the reply. A settings response below - // still has to prove we have a working, compatible session. - this.log(`${role} authentication unconfirmed; checking firmware response`) - } - })) - this.check(generation) - this.update('connecting', 'Checking glasses firmware…') - const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query') - this.applySettings(settings) - const firmware = { leftVersion: this.state.leftVersion, rightVersion: this.state.rightVersion, extension: this.state.capabilities } - if (!hasCompatibleFirmware(firmware)) - throw new Error((firmwareIncompatibilityMessage(firmware) || `The glasses must report Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION} or newer.`) + ' Use Devices → Check firmware to install it.') - this.update('connecting', 'Starting glasses display…') - await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156) - await this.lease(true) - await this.request('right', protocol.SID.hub, protocol.createLayout, 'Create display layout') - this.check(generation); this.layoutCreated = true - await this.enableWearDetectionAndRequestState(); this.check(generation) - this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now() - this.displayed = null; this.retryCount = 0 - this.update('connected', 'Glasses connected') - if (/^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION) - this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right) - else this.notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`) - await this.syncCompass(); this.check(generation) - this.schedule() - if (addresses.ring && ids.ring) await this.connectRing(generation) - else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found') - } catch (error) { - if (generation !== this.generation) return - this.fail(error, retry || error instanceof AckTimeout) - } - } - private async connectRing(generation: number): Promise { - if (this.ringConnecting || generation !== this.generation || this.state.phase !== 'connected' || !this.ids.ring) return - this.ringConnecting = true - const identifier = this.ids.ring - try { - const ring = await this.transport.connect(identifier); this.check(generation) - this.log(`R1 services discovered: ${ring.characteristics.join(', ')}`) - const notifications = protocol.RING_NOTIFY.filter(uuid => ring.characteristics.includes(uuid)) - if (!notifications.length) throw new Error('No R1 gesture notifications found') - let subscribed = 0 - for (const uuid of notifications) { - this.log(`Subscribing R1 ${uuid}`) - try { await this.transport.subscribe(identifier, uuid); this.check(generation); subscribed++; this.log(`R1 subscribed ${uuid}`) } - catch (error) { this.check(generation); this.log(`R1 channel unavailable ${uuid}: ${this.message(error)}`) } - } - if (!subscribed) throw new Error('Neither R1 notification channel could be subscribed') - this.check(generation); this.state.ring = true; this.ringRetryCount = 0 - this.update('connected', 'Glasses and ring connected') - } catch (error) { - if (generation !== this.generation) return - this.state.ring = false - this.transport.disconnect(identifier) - this.update('connected', `Glasses connected; ring unavailable: ${this.message(error)}`) - } finally { - if (generation === this.generation) { - this.ringConnecting = false - if (!this.state.ring) this.scheduleRingRetry() - } - } - } - private scheduleRingRetry(): void { - if (!this.wanted || this.state.phase !== 'connected' || !this.ids.ring || this.ringConnecting || this.ringRetryTimer !== null || this.ringRetryCount >= 5) return - const generation = this.generation, delay = Math.min(30_000, 2000 * 2 ** this.ringRetryCount++) - this.log(`Retrying ring in ${delay / 1000}s`) - this.ringRetryTimer = setTimeout(() => { - this.ringRetryTimer = null - if (generation === this.generation) void this.connectRing(generation) - }, delay) - } - private check(generation: number): void { if (generation !== this.generation) throw new Error('Connection cancelled') } - private nextMagic(): number { - for (let i = 0; i < 156; i++) { - const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1 - if (magic !== 156 && !this.cfwPending.some(item => item.magic === magic) && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic - } - throw new Error('All message identifiers are in use') - } - private request(role: string, sid: number, build: (magic: number) => Uint8Array, label: string, - timeout = 3500, flag = 0x20, fixedMagic?: number): Promise { - const magic = fixedMagic ?? this.nextMagic(), payload = build(magic), key = `${sid}:${magic}` - this.log(`TX ${label} ${role} sid=${sid.toString(16)} magic=${magic}`) - const generation = this.generation - return new Promise((resolve, reject) => { - const pending: PendingAck = { resolve, reject, timer: null, command: protocol.readInteger(payload, 1, -1), label } - this.pending.set(key, pending) - this.send(role, sid, flag, payload).then(() => { - if (this.pending.get(key) !== pending || generation !== this.generation) return - pending.timer = setTimeout(() => { - this.pending.delete(key); reject(new AckTimeout(`${label} was not acknowledged by the glasses.`)) - }, timeout) - }).catch(error => { - if (this.pending.get(key) !== pending) return - this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer); reject(error) - }) - }) - } - private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise { - return this.writePackets(role, () => protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role])) - } - private writePackets(role: string, build: () => Uint8Array[], current = () => true): Promise { - const identifier = this.ids[role], generation = this.generation - if (!identifier) return Promise.reject(new Error(`No ${role} device connected`)) - const previous = this.writes.get(identifier) ?? Promise.resolve() - // A failed write poisons this link's queue until reset: do not transmit - // later fragments after a hole in the image stream. - const work = previous.then(async () => { - this.check(generation) - if (!current()) return - for (const frame of build()) { - this.check(generation) - if (!current()) return - await this.transport.write(identifier, protocol.G2_WRITE, frame) - iosBleTraffic.recordWrite(frame.length) - } - iosBleTraffic.recordMessage() - }) - // A whole message owns the write queue; heartbeat packets cannot split an image message. - this.writes.set(identifier, work) - return work - } - private lease(acquire: boolean): Promise { - return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire)))) - } - private requestCfw(role: string, payload: Uint8Array): Promise { - if (payload.length > CFW_MAX_MESSAGE) return Promise.reject(new Error('CFW message too large')) - return new Promise((resolve, reject) => { - const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0, - retryPending: false, deadline: 0, resolve, reject } - this.cfwPending.push(item) - this.sendCfw(item) - }) - } - private sendCfw(item: CfwPending): void { - const generation = this.generation, magic = item.magic - const current = () => generation === this.generation && item.magic === magic && this.cfwPending.includes(item) - void this.writePackets(item.role, () => { - let transport = this.cfwTransports.get(item.role) - if (!transport) { transport = new CfwTransport(); this.cfwTransports.set(item.role, transport) } - if (item.retries) transport.reset() - return transport.encode(item.payload, magic, 3, this.limits[item.role]) - }, current).then(() => { - if (!current()) return - item.deadline = Date.now() + 500 - this.scheduleCfwRecovery() - }).catch(error => { if (current()) this.fail(error, true) }) - } - private receiveCfw(identifier: string, packet: Uint8Array): void { - const acks = parseCfwAcks(packet) - if (!acks) return - for (const ack of acks) { - // The ingress temple relays the other lens's result on the SAME link. - const item = this.cfwPending.find(p => this.ids[p.role] === identifier && p.magic === ack.streamId) - if (!item || ack.messageId !== 0) continue - if (ack.nack) item.retryPending = true - else if (ack.size === item.payload.length && ack.checksum === item.checksum) item.ackLenses |= ack.lens - this.log(`CFW ${ack.nack ? 'NACK' : 'ACK'} stream=${ack.streamId} lens=${ack.lens} size=${ack.size} crc=${ack.checksum} confirmed=${item.ackLenses}`) - } - while (this.cfwPending.length) { - const head = this.cfwPending[0] - if (head.retryPending || head.ackLenses !== 3) break - this.cfwPending.shift(); head.resolve() - } - this.scheduleCfwRecovery() - } - private scheduleCfwRecovery(): void { - if (this.cfwTimer !== null) clearTimeout(this.cfwTimer) - this.cfwTimer = null - let deadline = Infinity - for (const item of this.cfwPending) { - if (item.retryPending) deadline = 0 - else if (item.ackLenses !== 3 && item.deadline) deadline = Math.min(deadline, item.deadline) - } - if (deadline === Infinity) return - this.cfwTimer = setTimeout(() => { - this.cfwTimer = null - this.recoverCfw() - }, Math.max(0, deadline - Date.now())) - } - private recoverCfw(): void { - if (!this.cfwPending.some(p => p.retryPending || (p.ackLenses !== 3 && p.deadline > 0 && p.deadline <= Date.now()))) { - this.scheduleCfwRecovery(); return - } - if (this.cfwPending.some(p => p.retries >= 3)) { this.fail(new Error('CFW recovery retry limit'), true); return } - this.log(`CFW recovery: replay ${this.cfwPending.length} unresolved messages`) - // Go back to the oldest unresolved command, including any ACKed tail. - // Fresh stream IDs reject stale ACKs; each replay resets compression history. - for (const item of this.cfwPending) { - item.magic = this.nextMagic(); item.retries++; item.ackLenses = 0 - item.deadline = 0; item.retryPending = false - } - for (const item of this.cfwPending) this.sendCfw(item) - } - private receive(event: TransportEvent): void { - if (!Object.values(this.ids).includes(event.identifier ?? '')) return - if (event.kind === 'ancs-authorization' && event.identifier === this.ids.right) { - if (event.authorized === false) { - const active = this.notifications.state !== 'disconnected' - const command = this.notifications.stopCommand() - this.notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.') - if (active && this.state.phase === 'connected') void this.writePackets('right', () => [command]).catch(() => {}) - } else if (this.state.phase === 'connected' && /^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION) { - this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right) - } - return - } - if (event.kind === 'disconnected') { - if (event.identifier === this.ids.ring) { - this.state.ring = false - if (this.state.phase === 'connected') { - this.update('connected', `Glasses connected; ring disconnected${event.message ? ': ' + event.message : ''}`) - this.scheduleRingRetry() - } - } else if (['connected', 'connecting'].includes(this.state.phase)) this.fail(new Error(event.message || 'Glasses disconnected'), true) - return - } - if (event.kind !== 'notification' || !event.data) return - if (event.characteristic === protocol.G2_RENDER_NOTIFY && event.identifier !== this.ids.ring) { - if (this.audioListener && this.state.phase === 'connected') { - this.audioListener(hexToBytes(event.data)) - this.onActivity(); this.wake() - } - return - } - const ring = event.identifier === this.ids.ring - if (ring ? !protocol.RING_NOTIFY.includes(event.characteristic ?? '') : event.characteristic !== protocol.G2_NOTIFY) return - try { - const data = hexToBytes(event.data) - if (ring) { - const input = protocol.decodeRingInput(data) - if (input) { this.log(`Direct R1 gesture ${input.eventType}`); this.onInput(input) } - return - } - if (event.identifier === this.ids.right && this.notifications.receive(data)) return - for (const message of this.receiver.receive(event.identifier!, data)) { - this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`) - if (message.sid === protocol.SID.cfw) { - this.receiveCfw(event.identifier!, message.packet!) - continue - } - if (![1, 6].includes(message.flag)) { - // A master arm can relay the other arm's ACK; magic is allocated - // across BOTH links, matching Android's communicator. - const key = `${message.sid}:${message.magic}`, pending = this.pending.get(key) - if (pending) { - this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer) - pending.resolve(message) - } - } - if (message.sid === protocol.SID.settings) this.applySettings(message) - const wearing = decodeWearState(message) - if (wearing !== null) this.onWearState(wearing) - if (event.identifier === this.ids.right) { - if (this.state.phase === 'connected') { - const compass = protocol.decodeCompassInput(message) - if (compass) this.onCompass(compass) - } - const input = protocol.decodeGlassesInput(message) - if (input) { - if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') { - // Respect an explicit glasses-side exit; the user can reconnect. - void this.stop(); continue - } - this.onInput(input) - } - } - } - } catch (error) { this.log(`Ignored malformed BLE notification: ${this.message(error)}`) } - finally { - // CoreBluetooth can wake a suspended iOS process for a notification. - // Service wall-clock deadlines now, without waiting for a suspended timer. - if (this.state.phase === 'connected') { this.onActivity(); this.wake() } - } - } - private applySettings(message: protocol.ProtocolMessage): void { - const values = protocol.readBytes(message.payload, 4) - const capabilities = protocol.readString(message.payload, 100) - if (capabilities) this.state.capabilities = capabilities - if (values) { - const left = protocol.readString(values, 5), right = protocol.readString(values, 6) - if (left) this.state.leftVersion = left - if (right) this.state.rightVersion = right - const battery = protocol.readInteger(values, 12, -1), charging = protocol.readInteger(values, 13, -1) - if (battery >= 0 && battery <= 100) this.state.battery = battery - if (charging >= 0) this.state.charging = this.charging = charging > 0 - } - this.onState({ ...this.state }) - } - async enableWearDetectionAndRequestState(): Promise { - const generation = this.generation - // Like Android, a missing enable ACK must not suppress the current-state query. - try { await this.request('right', protocol.SID.settings, enableWearDetection, 'Enable wear detection') } - catch (error) { if (!(error instanceof AckTimeout)) throw error; this.log(this.message(error)) } - this.check(generation) - await this.send('right', protocol.SID.settings, 0x20, queryWearState()) - this.check(generation) - await this.send('left', protocol.SID.settings, 0x20, queryWearState()) - } - async setBrightness(level: number | null): Promise { - if (this.state.phase !== 'connected') return - await this.request('right', protocol.SID.settings, magic => setBrightness(magic, level), 'Brightness') - } - setFrame(gray: Uint8Array, textures?: TextureFrame): void { - if (this.state.phase !== 'connected') return - this.latest = protocol.packGray4(gray, 640, 480) - this.latestTextures = textures ?? null - this.schedule(0) - } - /** Same mode-5 kind-4 sequencer payload and acknowledged image path as Android. */ - async playBuzzerSequence(payload: Uint8Array): Promise { - if (this.state.phase !== 'connected' || !this.layoutCreated || payload.length < 3) return - const generation = this.generation - try { await this.requestCfw('left', new Uint8Array(payload)) } - catch (error) { if (generation === this.generation) this.fail(error, true) } - } - /** Serialize mic enable/disable, including a release while enable awaits ACK. */ - setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise { - const token = ++this.microphoneToken, generation = this.generation - this.audioListener = enabled ? listener ?? null : null - const current = () => generation === this.generation && token === this.microphoneToken - const work = this.microphoneWork.catch(() => {}).then(async () => { - if (!current()) return - if (this.state.phase !== 'connected') { - if (enabled) throw new Error('Connect the glasses before starting voice input.') - return - } - if (enabled) { - await Promise.all(['left', 'right'].map(role => this.transport.subscribe(this.ids[role], protocol.G2_RENDER_NOTIFY))) - if (!current()) return - } - await this.request('right', protocol.SID.hub, magic => protocol.audioControl(magic, enabled), enabled ? 'Microphone enable' : 'Microphone disable') - if (current()) { this.microphoneEnabled = enabled; this.log(`Glasses microphone ${enabled ? 'enabled' : 'disabled'}`) } - }).catch(error => { - if (current()) { this.audioListener = null; this.microphoneEnabled = false } - throw error - }) - this.microphoneWork = work - return work - } - setCompassEnabled(enabled: boolean): void { - this.compassWanted = enabled - void this.syncCompass() - } - /** Serialize changes so a release cannot overtake an unacknowledged enable. */ - private syncCompass(): Promise { - const generation = this.generation - const work = this.compassWork.then(async () => { - if (generation !== this.generation || this.state.phase !== 'connected') return - const enabled = !this.compassStopping && this.compassWanted - if (this.compassSent === enabled) return - // Same CFW mode 10, 100 ms report interval and zero minimum change as Android. - await this.requestCfw('left', new Uint8Array(enabled ? [10, 2, 100, 0, 0, 0] : [10, 0])) - if (generation === this.generation) this.compassSent = enabled - }).catch(error => { if (generation === this.generation) this.fail(error, true) }) - this.compassWork = work - return work - } - wake(): void { - if (this.timer !== null) { clearTimeout(this.timer); this.timer = null } - void this.pump() - } - private schedule(delay = 1000): void { - if (delay === 0 && this.timer !== null) { clearTimeout(this.timer); this.timer = null } - if (this.timer !== null || this.pumping || this.state.phase !== 'connected') return - this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay) - } - private canSendDisplay(): boolean { - // Also bound completed frames retained behind a missing/out-of-order ACK. - return this.displayInFlight < DISPLAY_WINDOW_SIZE && !!(this.displaySending || - (this.latest && !this.charging && this.displayFrames.length < DISPLAY_WINDOW_SIZE)) - } - private fillDisplayWindow(generation: number): void { - while (this.canSendDisplay()) { - if (!this.displaySending) { - const packed = this.latest!; this.latest = null - const textureFrame = this.latestTextures; this.latestTextures = null - // Compare with the last enqueued image, not the last ACKed one. In an - // A -> B -> A sequence, B may still be in flight when A is requested. - if (this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue - const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null - let commands: Uint8Array[] - if (texturePlan) { - // Uploads participate in the same ordered, acknowledged command window as - // the image. Coalescing only happens before planning mutates residency. - commands = [...texturePlan.uploads, texturePlan.payload] - this.nextImageFrameId = texturePlan.nextFid - this.log(`Display textures (${texturePlan.uploads.length} uploads, cache=${texturePlan.usedBytes}B)`) - } else { - const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId) - const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed)) - if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1 - commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId) - if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands) - this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1 - this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`) - } - this.displaySending = { packed, commands, offset: 0, pending: 0 } - this.displayFrames.push(this.displaySending); this.lastEnqueued = packed - } - const frame = this.displaySending, payload = frame.commands[frame.offset++] - frame.pending++; this.displayInFlight++ - if (frame.offset >= frame.commands.length) this.displaySending = null - // Writes serialize on L while up to three complete commands await their - // two-lens ACKs. Retire commands in submission order. - void this.requestCfw('left', payload).then(() => { - if (generation !== this.generation) return - frame.pending--; this.displayInFlight--; this.lastHeartbeat = Date.now() - // Retire in submission order even if relayed ACKs arrive out of order. - // A frame counts only after every command has been acknowledged. - while (this.displayFrames.length) { - const completed = this.displayFrames[0] - if (completed.offset < completed.commands.length || completed.pending) break - this.displayFrames.shift(); this.displayed = completed.packed - this.state.frames++; iosBleTraffic.recordDisplayFrame(); this.onState({ ...this.state }) - this.log(`Display frame ${this.state.frames} acknowledged (${completed.commands.length} commands)`) - } - this.wake() - }).catch(error => { if (generation === this.generation) this.fail(error, true) }) - } - } - private async pump(): Promise { - if (this.pumping || this.state.phase !== 'connected') return - this.pumping = true; const generation = this.generation - try { - const now = Date.now() - // Firmware frees textures when its 90-second framebuffer lease expires. - // After a long iOS suspension, reconnect rather than replaying pending - // draws against unknown memory. Leave a margin for the two arm writes. - if (now - this.lastLease >= 80_000) throw new Error('Display lease lapsed; restarting session') - if (now - this.lastHeartbeat >= 4000) { - const gap = now - this.lastHeartbeat - await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now() - this.log(`Heartbeat acknowledged (gap ${gap}ms)`) - } - if (now - this.lastLease >= 45_000) { await this.lease(true); this.lastLease = Date.now() } - if (now - this.lastSettings >= (this.charging ? 30_000 : 300_000)) { - const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Battery query') - this.applySettings(settings); this.lastSettings = Date.now() - } - this.check(generation) - this.fillDisplayWindow(generation) - } catch (error) { if (generation === this.generation) this.fail(error, true) } - finally { if (generation === this.generation) { this.pumping = false; this.schedule(this.canSendDisplay() ? 0 : 1000) } } - } - private reset(): void { - this.notifications.stop() - ++this.generation - this.compassSent = null; this.compassWork = Promise.resolve() - ++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false - if (this.timer !== null) clearTimeout(this.timer) - if (this.retryTimer !== null) clearTimeout(this.retryTimer) - if (this.ringRetryTimer !== null) clearTimeout(this.ringRetryTimer) - this.ringRetryTimer = null; this.ringConnecting = false; this.ringRetryCount = 0 - this.timer = this.retryTimer = null; this.pumping = false - const pending = [...this.pending.values()]; this.pending.clear() - for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) } - if (this.cfwTimer !== null) clearTimeout(this.cfwTimer) - this.cfwTimer = null - const custom = this.cfwPending; this.cfwPending = [] - for (const item of custom) item.reject(new Error('Connection ended')) - this.cfwTransports.clear() - this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null - this.latestTextures = null; this.textures?.reset() - this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0 - } - private closeLinks(): void { - const ids = this.ids; this.ids = {}; this.limits = {} - for (const identifier of Object.values(ids)) this.transport.disconnect(identifier) - this.transport.stopScan(); this.layoutCreated = false; this.state.ring = false - } - private fail(error: unknown, retry: boolean): void { - const message = this.message(error); this.reset(); this.closeLinks() - if (retry && this.wanted && this.addresses && this.retryCount < 5) { - const delay = Math.min(30_000, 2000 * 2 ** this.retryCount++) - this.update('retrying', `${message} Retrying in ${delay / 1000}s…`) - this.retryTimer = setTimeout(() => { this.retryTimer = null; if (this.wanted && this.addresses) void this.start(this.addresses, true) }, delay) - } else { this.wanted = false; this.update('error', message) } - } - stop(): Promise { - if (this.stopping) return this.stopping - this.stopping = this.stopInternal().finally(() => { this.stopping = null }) - return this.stopping - } - private async stopInternal(): Promise { - this.wanted = false - this.compassStopping = true - await this.syncCompass() - // Stop mic capture before ending the display session. Late enable ACKs - // cannot leave a microphone streaming after an explicit Disconnect. - if (this.audioListener || this.microphoneEnabled) { - try { await this.setMicrophone(false) } catch (error) { this.log(`Microphone cleanup: ${this.message(error)}`) } - } - if (this.notifications.state !== 'disconnected') { - try { await this.writePackets('right', () => [this.notifications.stopCommand()]) } catch {} - } - const cleanup = this.layoutCreated && this.state.phase === 'connected' - this.update('disconnecting', 'Disconnecting…'); this.reset() - try { - if (cleanup) { - await this.requestCfw('left', new Uint8Array([11])) - } - } catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) } - finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') } - } - dispose(): void { void this.stop().finally(() => this.off()) } - private message(error: unknown): string { return error instanceof Error ? error.message : String(error) } -} diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index 3593cdb5..7e8ec2dd 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -17,10 +17,11 @@ import { IosNavigationSensors } from '../native/ios-navigation-sensors' import { Utils } from '@nativescript/core' import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios' import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core' -import { iosBluetooth } from '../native/ios-bluetooth' import { iosVoiceInput } from '../native/ios-voice-input' import { nightscoutBridge } from '../native/nightscout-bridge' -import { GlassesSession, type SessionState } from './glasses-session' +import { FaceclawCommunicatorBridge, resolveIosPeripherals, type CommunicatorState, type RawInputEvent } from '../native/faceclaw-communicator.ios' +import { PreviewDisplayTarget, type DisplayTarget } from '../native/preview-display.ios' +import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client' import { GlanceHost, type GlanceDisplay } from './glance-host' import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen' import { OsEventTypeList } from './events' @@ -34,64 +35,90 @@ import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings' import { readPhoneBatteryState } from '../native/phone-battery' import { iosAppUnavailableReason } from '../apps/ios-availability' -import { SurfaceCompositor } from '../graphics/surface-compositor' -import { flattenPlanesWithDraws, type Plane } from '../graphics/plane' +import { flattenPlanesWithDraws, planesFingerprint, type Plane } from '../graphics/plane' import { prepareFrameDraws } from '../graphics/glyph-wire' -import { IosTexturePlanner } from '../native/texture-planner.ios' import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image' -import { previewPixels } from '../native/ios-graphics' import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures' import { noopLayerActions, type LayerActions } from '../ui/layers' import { TextViewerLayer } from '../apps/files/text-viewer' import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell' import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell/geometry' import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged, - previewColorSetting, lockScreenEnabledSetting, brightnessSetting, brightnessSettingToLevel } from '../ui/dashboard-settings' + previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences } from '../ui/dashboard-settings' import type { PhoneGesture } from '../phone-ui/phone-gestures' import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state' import { findSoundEffect, playSoundEffect } from '../ui/sound-effects' -/** iOS host for the shared app registry, shell, compositor and BLE session. */ +const SHELL_SURFACE_ID = 'shell' +const SHELL_SURFACE_Z_ORDER = 1 +const LOCK_SURFACE_Z_ORDER = 1000 +/** The shell render loop waits this long for the glasses to take a frame before moving on. */ +const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 2000 + +export type SessionPhase = 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error' +export type SessionState = { phase: SessionPhase; status: string; battery: number | null; charging: boolean | null; + ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string } + +function ancsCapable(capabilities: string): boolean { + return /^Faceclaw\/(\d+)/.test(capabilities) && Number(capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION +} + +/** + * iOS host for the shared app registry, shell and BLE session. The session, + * compositor and transport live in shared Kotlin (FaceclawCommunicatorBridge + * over FaceclawKitIosGlassesSession); with no glasses connected the same + * compositor runs headless (PreviewDisplayTarget) so the phone mirror works. + */ export class IosPreviewController { - private readonly compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT) + private previewTarget: PreviewDisplayTarget | null = null + private communicator: FaceclawCommunicatorBridge | null = null + private readonly startedCommunicators = new WeakSet() + state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false, + frames: 0, capabilities: '', leftVersion: '', rightVersion: '' } + private notifications: AncsClient | null = null + private readonly sessionOffs: (() => void)[] = [] + private stopping: Promise | null = null + /** Resolves once the current display target has every surface configured. */ + private displayReady: Promise = Promise.resolve() + private connecting = false + private sessionCreated = false + private wearDetectionRequested = false private readonly glanceDisplay: GlanceDisplay = { - configureSurface: async (id, options) => { - this.compositor.configureSurface(id, options) - }, - setSurfaceVisible: async (id, visible) => { - this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() - }, - setScreenBlanked: async blanked => { - this.compositor.setScreenBlanked(blanked); this.scheduleFrame() - }, - submitSurfaceFrame: async (id, pixels, rect, _fingerprint, _paintMs, _frameId, draws) => { - this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.scheduleFrame() + configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) }, + setSurfaceVisible: async (id, visible) => { await this.display?.setSurfaceVisible(id, visible); this.schedulePreviewUpdate() }, + setSurfaceDepth: async (id, depth) => { await this.display?.setSurfaceDepth(id, depth) }, + setScreenBlanked: async blanked => { await this.display?.setScreenBlanked(blanked); this.schedulePreviewUpdate() }, + submitSurfaceFrame: async (id, pixels, rect, fingerprint, paintMs, frameId, draws) => { + await this.display?.submitSurfaceFrame(id, pixels, rect, fingerprint, paintMs, frameId, draws); this.schedulePreviewUpdate() }, } private readonly glance = new GlanceHost({ - getDisplay: () => this.glanceDisplay, + getDisplay: () => this.display ? this.glanceDisplay : null, getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null, canShow: () => this.runtimeNeeded && !this.glassesLocked, // iOS retains the EvenHub session while the shell sleeps. The board's // opaque first frame is ready before we unblank the compositor. ensureSessionActive: async () => { - this.compositor.setScreenBlanked(false); this.scheduleFrame(); return true + await this.display?.setScreenBlanked(false); this.schedulePreviewUpdate(); return true }, onHiddenWhileAsleep: () => { - if (!shell.isScreenOn()) this.compositor.setScreenBlanked(true) - this.scheduleFrame() + if (!shell.isScreenOn()) void this.display?.setScreenBlanked(true).then(() => this.schedulePreviewUpdate()) + else this.schedulePreviewUpdate() }, - onVisibilityChanged: () => this.scheduleFrame(), + onVisibilityChanged: () => this.schedulePreviewUpdate(), appendLog: message => this.logBluetooth(message), }) - private renderTimer: ReturnType | null = null + private previewTimer: ReturnType | null = null private clockTimer: ReturnType | null = null private offSettings: (() => void) | null = null private lastBrightness: string | null = null private active = false private runtimeRunning = false private clockMinute = -1 - private shellDirty = true + private shellRenderInProgress = false + private shellRenderQueued = false + private nextFrameId = 1 + private nextWorkerFrame = 0 private inputQueue: Promise = Promise.resolve() private lastLayout = '' private readonly appHosts = new Map() @@ -102,13 +129,12 @@ export class IosPreviewController { private glassesWorn: boolean | null = null private glassesLocked = false private lockEnabled = lockScreenEnabledSetting.get() - private session: GlassesSession | null = null private acknowledgedFrames = 0 private logLines: string[] = [] private logTimer: ReturnType | null = null private readonly actions: LayerActions = { ...noopLayerActions, - playBuzzerSequence: payload => this.session?.playBuzzerSequence(payload), + playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload), requestRender: () => this.requestShellRender(), disconnect: () => this.disconnect(), startVoiceCapture: endpointing => this.startVoiceCapture(endpointing), @@ -130,11 +156,7 @@ export class IosPreviewController { private readonly onError: (message: string) => void, private readonly onConnectionState: (state: SessionState) => void = () => {}, private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {}) { - this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' }) - this.compositor.configureSurface(LOCK_SCREEN_SURFACE_ID, { - x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: 1000, transparency: 'opaque', - }) - this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, false) + this.ensurePreviewDisplay() shell.configure({ actions: this.actions, voiceInputEnabled: true, @@ -151,7 +173,8 @@ export class IosPreviewController { onWindowsChanged: () => this.requestShellRender(), onScreenStateChanged: on => { if (on) this.glance.dismiss() - this.compositor.setScreenBlanked(!on); this.requestShellRender() + void this.display?.setScreenBlanked(!on).catch(error => this.fail(error)) + this.requestShellRender() }, }) const launcher = createLauncherWindow({ @@ -160,9 +183,9 @@ export class IosPreviewController { launchApp: id => this.launchApp(id), uninstallApp: id => this.uninstallApp(id), submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes), - setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(LAUNCHER_SURFACE_ID, visible); this.scheduleFrame() }, + setSurfaceVisible: visible => this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible), }) - this.configureWindow(launcher) + void this.configureWindow(launcher) shell.registerWindow(launcher) shell.wake('window') shell.focusWindow(launcher.windowId) @@ -198,14 +221,54 @@ export class IosPreviewController { for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app)) } + /** The compositor frames go to: the live session, else the headless preview. */ + private get display(): DisplayTarget | null { return this.communicator ?? this.previewTarget } + + private ensurePreviewDisplay(): void { + if (this.communicator || this.previewTarget) return + const target = new PreviewDisplayTarget() + this.previewTarget = target + target.activate(() => this.schedulePreviewUpdate()) + this.displayReady = this.registerSurfaces(target).catch(error => this.fail(error)) + } + + private teardownPreviewDisplay(): void { + const target = this.previewTarget + if (!target) return + this.previewTarget = null + this.glance.reset() + target.release() + } + + /** (Re)create every compositor surface on a fresh display target. */ + private async registerSurfaces(target: DisplayTarget): Promise { + await target.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT) + await target.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, + zOrder: SHELL_SURFACE_Z_ORDER, transparency: 'color-key' }) + await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1) + await target.configureSurface(LOCK_SCREEN_SURFACE_ID, { + x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: LOCK_SURFACE_Z_ORDER, transparency: 'opaque', + }) + if (this.glassesLocked) await this.submitLockImage(target) + await target.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, this.glassesLocked) + await target.setScreenBlanked(!shell.isScreenOn()) + this.lastLayout = '' + for (const window of shell.getWindows()) await this.configureWindow(window, target) + } + + private async submitLockImage(target: DisplayTarget): Promise { + const image = createLockScreenImage() + await target.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(), + { x: 0, y: 0, width: image.width, height: image.height }, 'lock-screen', 0, 0) + } + resume(): void { if (this.active) return this.active = true this.syncRuntime() this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable) this.logBluetooth('Phone foreground') - if (this.session) this.onConnectionState({ ...this.session.state }) - this.session?.wake() + if (this.communicator) this.onConnectionState({ ...this.state }) this.relayout() shell.foregroundWindow()?.requestRender() this.requestShellRender() @@ -216,12 +279,11 @@ export class IosPreviewController { iosVoiceInput.stopPhoneCapture() // The phone preview is hidden; the glasses still own their screen and input. this.syncRuntime() - this.logBluetooth(`Phone background; glasses ${this.session?.state.phase ?? 'disconnected'}`) + this.logBluetooth(`Phone background; glasses ${this.state.phase}`) this.flushBluetoothLog() - this.session?.wake() } private get runtimeNeeded(): boolean { - return this.active || !!this.session && ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.session.state.phase) + return this.active || ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.state.phase) } private syncRuntime(): void { const running = this.runtimeNeeded @@ -231,14 +293,14 @@ export class IosPreviewController { if (!running) { void nightscoutBridge.stop().catch(error => this.fail(error)) this.glance.dismiss() - this.compositor.setScreenBlanked(!shell.isScreenOn()) + void this.display?.setScreenBlanked(!shell.isScreenOn()).catch(error => this.fail(error)) this.offSettings?.(); this.offSettings = null for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer) for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer) UIDevice.currentDevice.batteryMonitoringEnabled = false if (this.clockTimer !== null) clearInterval(this.clockTimer) - if (this.renderTimer !== null) clearTimeout(this.renderTimer) - this.clockTimer = this.renderTimer = null + if (this.previewTimer !== null) clearTimeout(this.previewTimer) + this.clockTimer = this.previewTimer = null return } UIDevice.currentDevice.batteryMonitoringEnabled = true @@ -268,12 +330,13 @@ export class IosPreviewController { this.clockTimer = setInterval(() => this.refreshClock(), 60_000) } private pushBrightness(): void { - const session = this.session - if (session?.state.phase !== 'connected') { this.lastBrightness = null; return } - const value = brightnessSetting.get() + const communicator = this.communicator + if (!communicator || this.state.phase !== 'connected') { this.lastBrightness = null; return } + const preferences = getBrightnessPreferences() + const value = JSON.stringify(preferences) if (value === this.lastBrightness) return this.lastBrightness = value - void session.setBrightness(brightnessSettingToLevel(value)).catch(error => { + void communicator.configureBrightness(preferences).catch(error => { this.lastBrightness = null this.logBluetooth(`Brightness: ${String(error)}`) }) @@ -285,12 +348,13 @@ export class IosPreviewController { if (!enabled) this.setGlassesLocked(false) else { if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true) - if (this.session?.state.phase === 'connected') - void this.session.enableWearDetectionAndRequestState().catch(error => this.fail(error)) + if (this.communicator && this.state.phase === 'connected') + void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error)) } } private handlePhoneLockState(locked: boolean): void { this.phoneLocked = locked + this.communicator?.onPhoneLockSignal() if (!locked) this.setGlassesLocked(false) else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true) } @@ -304,17 +368,21 @@ export class IosPreviewController { } private setGlassesLocked(locked: boolean): void { if (locked === this.glassesLocked) return - if (locked) { - const image = createLockScreenImage() - this.compositor.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(), - { x: 0, y: 0, width: image.width, height: image.height }) - } this.glassesLocked = locked if (locked) { this.glance.dismiss() iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.') } - this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked) + void (async () => { + await this.displayReady + const display = this.display + if (!display) return + { + if (locked) await this.submitLockImage(display) + await display.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked) + this.schedulePreviewUpdate() + } + })().catch(error => this.fail(error)) this.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`) this.requestShellRender() } @@ -322,47 +390,90 @@ export class IosPreviewController { const minute = Math.floor(Date.now() / 60_000) if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() } } - private configureWindow(window: ShellWindow): void { - this.compositor.configureSurface(window.surfaceId, { + private async configureWindow(window: ShellWindow, target: DisplayTarget | null = this.display): Promise { + if (!target) return + await target.configureSurface(window.surfaceId, { ...appViewportRect(window.heightMode, window.appId), zOrder: 0, transparency: 'opaque', }) - this.compositor.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId) + await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId) } private relayout(): void { const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";") if (layout === this.lastLayout) return this.lastLayout = layout for (const window of shell.getWindows()) { - this.configureWindow(window) + void this.configureWindow(window).catch(error => this.fail(error)) window.relayout?.() } } + /** Submit a painted frame for an in-process window (e.g. the launcher). */ private async submit(id: string, planes: Plane[]): Promise { + await this.displayReady + const display = this.display + if (!display) return + const fingerprint = planesFingerprint(planes) const { image, draws } = flattenPlanesWithDraws(planes) - this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, prepareFrameDraws(draws)) - this.scheduleFrame() + await display.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, + fingerprint, -1, 0, prepareFrameDraws(draws)) + this.schedulePreviewUpdate() } - private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() } - private scheduleFrame(): void { - if (!this.runtimeNeeded || this.renderTimer !== null) return - // Coalesce window/chrome updates into one glasses frame, capped at 30 fps. - this.renderTimer = setTimeout(() => { - this.renderTimer = null - if (!this.runtimeNeeded) return + /** Worker windows ship baked pixels; every submit is new content to the compositor. */ + private async submitWorkerPixels(id: string, pixels: Uint8Array, width: number, height: number, draws: ArrayBuffer | null): Promise { + await this.displayReady + const display = this.display + if (!display) return + await display.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, `${id}#${++this.nextWorkerFrame}`, -1, 0, draws) + this.schedulePreviewUpdate() + } + private setWindowSurfaceVisible(id: string, visible: boolean): void { + void this.display?.setSurfaceVisible(id, visible).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error)) + } + private removeWindowSurface(id: string): void { + void this.display?.removeSurface(id).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error)) + } + /** Re-render and resubmit the shell surface; one render in flight, at most one queued. */ + private requestShellRender(): void { + if (this.shellRenderInProgress) { this.shellRenderQueued = true; return } + this.shellRenderInProgress = true + void (async () => { try { - if (this.shellDirty) { - this.shellDirty = false - const { image, draws } = flattenPlanesWithDraws(shell.paintSurface(), { width: 640, height: 480 }) - this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 }, prepareFrameDraws(draws)) - this.compositor.setUnderlayDim(1, shell.underlayDim()) - } - const { pixels, textures } = this.compositor.compositeFrame() - this.session?.setFrame(pixels, textures) - if (this.active) { - const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green') - this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard' - : `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`) - } + do { + this.shellRenderQueued = false + await this.renderShell() + } while (this.shellRenderQueued) + } catch (error) { this.fail(error) } + finally { this.shellRenderInProgress = false } + })() + } + private async renderShell(): Promise { + await this.displayReady + const display = this.display + if (!display) return + const startedAt = Date.now() + const bytes = shell.paintScene() + const frameId = this.allocateFrameId() + await display.submitShellScene(bytes, Date.now() - startedAt, frameId) + // Backpressure: the next shell render waits for this one to reach the + // glasses (the preview target resolves immediately; nothing transmits). + await display.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS) + this.schedulePreviewUpdate() + } + private allocateFrameId(): number { + const id = this.nextFrameId + this.nextFrameId = this.nextFrameId >= 0x7fffffff ? 1 : this.nextFrameId + 1 + return id + } + /** Refresh the phone mirror from the composited screen, coalesced at 30 fps. */ + private schedulePreviewUpdate(): void { + if (!this.active || this.previewTimer !== null) return + this.previewTimer = setTimeout(() => { + this.previewTimer = null + if (!this.active) return + try { + const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green') + if (!image) return + this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard' + : `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`) } catch (error) { this.fail(error) } }, 33) } @@ -471,7 +582,7 @@ export class IosPreviewController { launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create), ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create), submitWindowFrame: (id, planes) => this.submit(id, planes), - setWindowSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() }, + setWindowSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible), requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`), setTextEditorHost: () => {}, } @@ -486,13 +597,17 @@ export class IosPreviewController { let surfaceReady = false const app = create({ actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(), - setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() }, - removeSurface: () => { surfaceReady = false; this.compositor.removeSurface(surfaceId); this.scheduleFrame() }, - reconfigureSurface: () => { const window = shell.getWindows().find(w => w.windowId === windowId); if (window) this.configureWindow(window); this.requestShellRender() }, + setSurfaceVisible: visible => this.setWindowSurfaceVisible(surfaceId, visible), + removeSurface: () => { surfaceReady = false; this.removeWindowSurface(surfaceId) }, + reconfigureSurface: () => { + const window = shell.getWindows().find(w => w.windowId === windowId) + if (window) void this.configureWindow(window).catch(error => this.fail(error)) + this.requestShellRender() + }, onClosed: () => { this.inProcessApps.delete(windowId) }, }) this.inProcessApps.set(windowId, app) - this.configureWindow(app.window) + await this.configureWindow(app.window) surfaceReady = true shell.registerWindow(app.window); shell.focusWindow(windowId) app.requestRender(); this.requestShellRender() @@ -505,16 +620,19 @@ export class IosPreviewController { ? new IosNavigationSensors(event => worker.postMessage({ type: 'navigation-sensors', event })) : undefined const host = new WorkerAppHost({ appId, worker, navigationSensors, + onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId) }, playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload), openUrl: url => { void Utils.openUrl(url) }, configureSurface: async (id, visible, heightMode) => { - this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) - this.compositor.setSurfaceVisible(id, visible) + const display = this.display + if (!display) return + await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) + await display.setSurfaceVisible(id, visible) }, - setSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() }, - removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() }, + setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible), + removeSurface: id => this.removeWindowSurface(id), submitPixels: (id, pixels, width, height, draws) => { - this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, draws); this.scheduleFrame() + void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error)) }, requestShellRender: () => this.requestShellRender(), openSettings: section => { void this.launchApp('settings', { section }) }, @@ -530,45 +648,194 @@ export class IosPreviewController { displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length]) } get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) } - get connectionState(): SessionState | null { return this.session?.state ?? null } + get connectionState(): SessionState | null { return this.sessionCreated ? this.state : null } get connectionDetails(): string { const state = this.connectionState return state ? `${state.status}\nL: ${state.leftVersion || 'unknown'}\nR: ${state.rightVersion || 'unknown'}\nBattery: ${state.battery ?? '?'}%\nRing: ${state.ring ? 'connected' : 'disconnected'}\nFrames acknowledged: ${state.frames}\n${state.capabilities}\n\n${this.logLines.slice(-12).join('\n')}` : 'Preview only. Configure devices, then connect.' } + private update(phase: SessionPhase, status: string): void { + this.state = { ...this.state, phase, status } + this.logBluetooth(status) + this.emitState() + } + private emitState(): void { + if (this.state.phase !== 'connected') resetRingInputFilter() + this.pushBrightness() + this.maybePlayWelcomeSound(this.state) + if (this.state.phase !== 'connected' && this.state.phase !== 'connecting') this.glassesWorn = null + if (this.state.phase !== 'connected') iosVoiceInput.handleSessionEnded() + shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging }) + this.syncRuntime() + if (this.active) this.onConnectionState({ ...this.state }) + this.requestShellRender() + } async connect(): Promise { - if (!this.active) return + if (!this.active || this.connecting || this.stopping) return + if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses) if (error) { this.onError(error); return } - if (!this.session) { - this.session = new GlassesSession(iosBluetooth(), state => { - if (state.phase !== "connected") resetRingInputFilter() - this.pushBrightness() - this.maybePlayWelcomeSound(state) - if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null - if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded() - shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging }) - this.syncRuntime() - if (this.active) this.onConnectionState(state) - this.requestShellRender() - }, input => { - this.inputQueue = this.inputQueue.then(async () => { - if (this.session?.state.phase !== 'connected') return - await this.receiveInput(rawInputEventToInputEvent(input), - input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT) - this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`) - }).catch(error => this.fail(error)) - }, message => this.logBluetooth(message), () => { - if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true) - this.refreshClock() - }, receiveCompassEvent, wearing => this.handleWearState(wearing), (key, popup) => { - iosNotificationsChanged(key, popup); this.requestShellRender() - }, new IosTexturePlanner()) - bindIosNotifications(this.session.notifications) - bindCompassSession(this.session) + this.connecting = true + this.sessionCreated = true + try { + this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false } + this.update('connecting', 'Finding configured devices…') + let identifiers + try { identifiers = await resolveIosPeripherals(addresses) } + catch (failure) { + this.update('error', failure instanceof Error ? failure.message : String(failure)) + return + } + if (this.communicator) await this.closeCommunicator() + const communicator = new FaceclawCommunicatorBridge(identifiers) + this.teardownPreviewDisplay() + this.communicator = communicator + this.wearDetectionRequested = false + this.bindCommunicator(communicator) + this.displayReady = this.registerSurfaces(communicator) + await this.displayReady + this.update('connecting', 'Connecting to the glasses…') + await communicator.configureBrightness(getBrightnessPreferences()) + this.startedCommunicators.add(communicator) + await communicator.start() + for (const window of shell.getWindows()) window.requestRender() + this.requestShellRender() + } catch (failure) { + this.fail(failure) + await this.closeCommunicator().catch(() => {}) + this.ensurePreviewDisplay() + this.update('error', failure instanceof Error ? failure.message : String(failure)) + } finally { + this.connecting = false } - await this.session.start(addresses) } - async disconnect(): Promise { await this.session?.stop() } + private bindCommunicator(communicator: FaceclawCommunicatorBridge): void { + const notifications = new AncsClient(packet => communicator.writeRawToRight(packet), (key, popup) => { + iosNotificationsChanged(key, popup); this.requestShellRender() + }, message => this.logBluetooth('ANCS: ' + message)) + this.notifications = notifications + bindIosNotifications(notifications) + bindCompassSession(communicator) + communicator.setRequiresAncs(true) + this.sessionOffs.push( + communicator.onStateChange(state => this.handleSessionState(communicator, state)), + communicator.onRingEvent(input => this.handleRingEvent(input)), + communicator.onBatteryState(state => { + if (this.communicator !== communicator) return + this.state = { ...this.state, battery: state.battery >= 0 && state.battery <= 100 ? state.battery : this.state.battery, + charging: state.chargingStatus >= 0 ? state.chargingStatus > 0 : this.state.charging, + ring: (state.ringBattery ?? -1) >= 0 || this.state.ring } + shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging }) + if (this.active) this.onConnectionState({ ...this.state }) + this.requestShellRender() + }), + communicator.onFirmwareInfo(info => { + if (this.communicator !== communicator) return + this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion, + rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities } + if (this.active) this.onConnectionState({ ...this.state }) + this.syncNotifications(communicator) + }), + communicator.onFrameMetrics(() => { + if (this.communicator !== communicator) return + this.state = { ...this.state, frames: this.state.frames + 1 } + this.onActivity() + this.maybePlayWelcomeSound(this.state) + if (this.active) this.onConnectionState({ ...this.state }) + this.schedulePreviewUpdate() + }), + communicator.onWearState(wearing => { if (this.communicator === communicator) this.handleWearState(wearing) }), + communicator.addCompassListener(receiveCompassEvent), + communicator.onAncsRelayFrame(frame => { if (this.communicator === communicator) notifications.receive(frame) }), + communicator.onAncsAuthorization(authorized => { + if (this.communicator !== communicator) return + if (authorized) { this.syncNotifications(communicator); return } + const active = notifications.state !== 'disconnected' + const command = notifications.stopCommand() + notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.') + if (active && this.state.phase === 'connected') void communicator.writeRawToRight(command).catch(() => {}) + }), + ) + } + private handleSessionState(communicator: FaceclawCommunicatorBridge, state: CommunicatorState): void { + if (this.communicator !== communicator) return + // Registering the listener makes the Kotlin session report its current state, which is + // "disconnected" before start(); that must not be mistaken for the session ending. + if (!this.startedCommunicators.has(communicator)) return + const phase: SessionPhase = state.phase === 'charging' ? 'connected' : state.phase === 'unpaired' ? 'error' : state.phase + if (phase === 'connected' && this.state.phase !== 'connected') { + this.state = { ...this.state, frames: this.state.frames } + if (!this.wearDetectionRequested) { + this.wearDetectionRequested = true + void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`)) + } + } + this.update(phase, state.status) + if (phase === 'connected') this.syncNotifications(communicator) + else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) { + // The session ended on its own (retries exhausted, an arm unpaired): release it and go headless. + void this.disconnect(state.status) + } + } + private handleRingEvent(input: RawInputEvent): void { + this.inputQueue = this.inputQueue.then(async () => { + if (this.state.phase !== 'connected') return + this.onActivity() + if (input.ringInput) this.state = { ...this.state, ring: true } + await this.receiveInput(rawInputEventToInputEvent(input), + input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT) + this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`) + }).catch(error => this.fail(error)) + } + /** Any glasses traffic: CoreBluetooth can wake a suspended process, so refresh what timers would have. */ + private onActivity(): void { + if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true) + this.refreshClock() + } + /** Start iPhone notification relay once the firmware is known to support it and ANCS is authorized. */ + private syncNotifications(communicator: FaceclawCommunicatorBridge): void { + const notifications = this.notifications + if (!notifications || this.state.phase !== 'connected') return + if (!ancsCapable(this.state.capabilities)) { + if (this.state.capabilities) notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`) + return + } + if (notifications.state !== 'disconnected') return + notifications.start((Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0, communicator.rightWriteLimit()) + } + async disconnect(reason?: string): Promise { + if (this.stopping) return this.stopping + const communicator = this.communicator + if (!communicator) return + // Marked before any bridge call: the session may report "disconnected" from inside disconnect(). + let finished!: () => void + this.stopping = new Promise(resolve => { finished = resolve }) + try { + const notifications = this.notifications + if (notifications && notifications.state !== 'disconnected') { + const command = notifications.stopCommand() + notifications.stop() + if (this.state.phase === 'connected') { try { await communicator.writeRawToRight(command) } catch {} } + } + if (this.state.phase === 'connected' || this.state.phase === 'connecting' || this.state.phase === 'retrying') + this.update('disconnecting', 'Disconnecting…') + await communicator.disconnect().catch(error => this.logBluetooth(`Disconnect: ${String(error)}`)) + } finally { + await this.closeCommunicator() + this.ensurePreviewDisplay() + this.update(reason ? 'error' : 'disconnected', reason ?? 'Preview only') + this.stopping = null + finished() + } + } + private async closeCommunicator(): Promise { + const communicator = this.communicator + if (!communicator) return + for (const off of this.sessionOffs.splice(0)) off() + this.notifications = null + this.communicator = null + this.glance.reset() + await communicator.close().catch(error => this.logBluetooth(`Close: ${String(error)}`)) + } private maybePlayWelcomeSound(state: SessionState): void { const firstNewFrame = state.frames > this.acknowledgedFrames this.acknowledgedFrames = state.frames @@ -583,17 +850,18 @@ export class IosPreviewController { startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() } private async prepareVoiceCapture(): Promise { if (this.glassesLocked) return false - const usePhoneMic = this.session?.state.phase !== 'connected' + const usePhoneMic = this.state.phase !== 'connected' if (usePhoneMic && !this.active) return false const ready = await iosVoiceInput.prepare(this.active, usePhoneMic) if (!ready && this.active) this.onError(iosVoiceInput.statusText) - return ready && !this.glassesLocked && (this.session?.state.phase === 'connected' || this.active) + return ready && !this.glassesLocked && (this.state.phase === 'connected' || this.active) } private async startVoiceCapture(endpointing = false): Promise { if (this.glassesLocked) return const log = (message: string) => this.logBluetooth(message) - if (this.session?.state.phase === 'connected') { - await iosVoiceInput.startGlassesCapture(this.session, log, endpointing) + const communicator = this.communicator + if (communicator && this.state.phase === 'connected') { + await iosVoiceInput.startGlassesCapture(communicator, log, endpointing) } else if (this.active) { // Recheck microphone permission if the glasses disconnected after prepare. if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return @@ -620,13 +888,21 @@ export class IosPreviewController { if (this.glassesLocked || !this.active) return shell.startKeyboardInput() } - private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void }): Promise { + private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void; validationError?(value?: string): string | null }): Promise { if (!this.active) { this.logBluetooth('Editing text settings requires opening Faceclaw on the phone'); return false } - const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: setting.get(), inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' }) - if (result.result) { - setting.set(result.text) + let draft = setting.get() + while (true) { + const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: draft, inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' }) + if (!result.result) return false + draft = result.text + const error = setting.validationError?.(draft) + if (error) { + await Dialogs.alert({ title: setting.editorTitle, message: error, okButtonText: 'OK' }) + continue + } + setting.set(draft) if (setting === nightscoutSiteUrlSetting || setting === nightscoutApiTokenSetting) await nightscoutBridge.refreshNow() + return true } - return result.result } } diff --git a/app/g2/stock-connection.ts b/app/g2/stock-connection.ts deleted file mode 100644 index edce1b4f..00000000 --- a/app/g2/stock-connection.ts +++ /dev/null @@ -1,152 +0,0 @@ -import * as p from './ble-protocol' -import type { SessionTransport, SessionAddresses, TransportEvent } from './glasses-session' -import { hexToBytes } from '../util/hex-util' - -declare function setTimeout(callback: () => void, ms: number): number -declare function clearTimeout(id: number): void - -export const OTA_WRITE = '00002760-08c2-11e1-9073-0e8ac72e0001' -export const OTA_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e0002' -export class StockTimeout extends Error {} -type Waiter = { role: string; characteristic: string; match: (message: p.ProtocolMessage) => boolean; - resolve: (message: p.ProtocolMessage) => void; reject: (error: Error) => void; timer: ReturnType | null } - -/** Exclusive, short-lived stock-protocol connection for onboarding/OTA. No display pump. */ -export class StockConnection { - ids: Record = {} - private limits: Record = {} - private receiver = new p.MessageReceiver() - private waiters = new Set() - private listeners = new Set<(role: string, message: p.ProtocolMessage) => void>() - private lost = new Set() - private writes = new Map>() - private closed = false - private magic = 0x60 - private sequence = 0 - private off: () => void - constructor(private transport: SessionTransport, private log: (line: string) => void = () => {}) { - this.off = transport.onEvent(event => this.receive(event)) - } - check(): void { if (this.closed) throw new Error('Cancelled.') } - isConnected(role: string): boolean { return !this.closed && !!this.limits[role] && !this.lost.has(role) } - async resolve(addresses: SessionAddresses): Promise { - this.ids = await this.transport.resolveDevices(addresses); this.check() - } - async connect(role: string, ota = false): Promise { - this.check(); this.lost.delete(role) - const id = this.ids[role] - if (!id) throw new Error(`No ${role} lens configured.`) - const details = await this.transport.connect(id) - if (this.closed) { this.transport.disconnect(id); this.check() } - this.limits[role] = details.maxWrite - for (const characteristic of [p.G2_WRITE, p.G2_NOTIFY, ...(ota ? [OTA_WRITE, OTA_NOTIFY] : [])]) - if (!details.characteristics.includes(characteristic)) throw new Error(`${role} lens is missing ${ota ? 'firmware update' : 'G2'} characteristics.`) - await this.transport.subscribe(id, p.G2_NOTIFY); this.check() - if (ota) { await this.transport.subscribe(id, OTA_NOTIFY); this.check() } - } - nextMagic(): number { this.magic = this.magic >= 0x7f ? 0x60 : this.magic + 1; return this.magic } - async authenticate(role: string): Promise { - for (let attempt = 0; attempt < 2; attempt++) { - const magic = this.nextMagic() - try { - await this.exchange(role, p.SID.auth, p.authentication(magic), - message => p.authenticationSucceeded(message, magic), 30_000, p.G2_WRITE, p.G2_NOTIFY, 0) - this.log(`Security authentication complete: ${role} lens`); return - } catch (error) { this.check(); if (!(error instanceof StockTimeout) || attempt === 1) throw error } - } - } - request(role: string, sid: number, build: (magic: number) => Uint8Array, timeout = 3500, fixedMagic?: number): Promise { - const magic = fixedMagic ?? this.nextMagic() - return this.exchange(role, sid, build(magic), message => message.sid === sid && message.magic === magic && ![1, 6].includes(message.flag), timeout) - } - write(role: string, sid: number, payload: Uint8Array, characteristic = p.G2_WRITE, flag = 0x20, sequence = ++this.sequence): Promise { - // Keep a fragmented control message together when the prompt heartbeat - // overlaps a settings request or shutdown. - const pending = (this.writes.get(role) ?? Promise.resolve()).catch(() => {}).then(async () => { - this.check() - for (const frame of p.frameMessage(payload, sid, flag, sequence, this.limits[role])) { - this.check() - if (this.lost.has(role)) throw new Error(`Lost connection to the ${role} lens.`) - await this.transport.write(this.ids[role], characteristic, frame) - } - this.check() - if (this.lost.has(role)) throw new Error(`Lost connection to the ${role} lens.`) - }) - this.writes.set(role, pending) - return pending - } - exchange(role: string, sid: number, payload: Uint8Array, match: Waiter['match'], timeout: number, - write = p.G2_WRITE, notify = p.G2_NOTIFY, flag = 0x20, sequence?: number, data?: Uint8Array): Promise { - this.check() - return new Promise((resolve, reject) => { - let written = false, reply: p.ProtocolMessage | null = null - const waiter: Waiter = { role, characteristic: notify, match, - resolve: message => { reply = message; if (written) resolve(message) }, reject, - timer: null } - this.waiters.add(waiter) - void (async () => { - await this.write(role, sid, payload, write, flag, sequence) - if (data) await this.write(role, 0xc1, data, write, flag, sequence) - this.check() - if (!reply && !this.waiters.has(waiter)) return - written = true - if (reply) resolve(reply) - else waiter.timer = setTimeout(() => { - this.waiters.delete(waiter) - reject(new StockTimeout(`No response from the ${role} lens (0x${sid.toString(16)}).`)) - }, timeout) - })().catch(error => { this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); reject(error) }) - }) - } - - onMessage(listener: (role: string, message: p.ProtocolMessage) => void): () => void { - this.listeners.add(listener); return () => this.listeners.delete(listener) - } - private receive(event: TransportEvent): void { - if (this.closed) return - const role = Object.keys(this.ids).find(role => this.ids[role] === event.identifier) - if (!role) return - if (event.kind === 'disconnected') { - this.lost.add(role) - for (const waiter of [...this.waiters]) if (waiter.role === role) { - this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.reject(new Error(`Lost connection to the ${role} lens.`)) - } - return - } - if (event.kind !== 'notification' || !event.data || ![p.G2_NOTIFY, OTA_NOTIFY].includes(event.characteristic!)) return - try { - const data = hexToBytes(event.data) - const messages = event.characteristic === OTA_NOTIFY ? decodeOtaAcks(data) : this.receiver.receive(event.identifier!, data) - for (const message of messages) { - for (const waiter of [...this.waiters]) if (waiter.role === role && waiter.characteristic === event.characteristic && waiter.match(message)) { - this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.resolve(message) - } - if (event.characteristic === p.G2_NOTIFY) for (const listener of this.listeners) listener(role, message) - } - } catch (error) { this.log(`Ignored malformed onboarding notification: ${error}`) } - } - disconnect(role: string): void { this.lost.add(role); if (this.ids[role]) this.transport.disconnect(this.ids[role]); this.receiver.clear() } - close(): void { - if (this.closed) return - this.closed = true; this.off(); this.transport.stopScan() - for (const waiter of this.waiters) { if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.reject(new Error('Cancelled.')) } - this.waiters.clear(); this.listeners.clear() - for (const role of Object.keys(this.ids)) this.disconnect(role) - } -} - -/** OTA ACKs are raw opcode/status bytes, not protobuf; validate the aa21 CRC envelope. */ -export function decodeOtaAcks(data: Uint8Array): p.ProtocolMessage[] { - const messages: p.ProtocolMessage[] = [] - for (let offset = 0; offset + 8 <= data.length;) { - const n = data[offset + 3], end = offset + 8 + n - if (end > data.length) break - if (data[offset] === 0xaa && [0x12, 0x21].includes(data[offset + 1]) && data[offset + 4] === 1 && data[offset + 5] === 1 && n >= 4) { - const payload = data.slice(offset + 8, end - 2) - if (p.crc16(payload) === (data[end - 2] | data[end - 1] << 8)) - messages.push({ sid: data[offset + 6], flag: data[offset + 7], payload, command: payload[0], magic: -1 }) - } - offset = end - } - return messages -} diff --git a/app/g2/texture-planner.ts b/app/g2/texture-planner.ts deleted file mode 100644 index b41af272..00000000 --- a/app/g2/texture-planner.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** Platform-owned snapshot of the same composite as the submitted pixels. */ -export type TextureFrame = { readonly native: object } -export type TexturePlan = { uploads: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number } -/** Sessions own residency; frame snapshots may be coalesced without changing it. */ -export interface SessionTexturePlanner { - plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null - reset(): void -} diff --git a/app/g2/wear-protocol.ts b/app/g2/wear-protocol.ts deleted file mode 100644 index 4e06c1ef..00000000 --- a/app/g2/wear-protocol.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { bytes, concat, integer, readBytes, readInteger, type ProtocolMessage } from './ble-protocol' - -// Matches BleProtocol.buildSetWearDetection/buildFaceclawWakeControl on Android. -export const enableWearDetection = (magic: number): Uint8Array => concat( - integer(1, 1), integer(2, magic), bytes(3, bytes(5, integer(1, 1)))) -export const queryWearState = (): Uint8Array => concat( - integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, 7, 0, 0]))) - -export function decodeWearState(message: ProtocolMessage): boolean | null { - if (message.sid !== 0x10 || message.command !== 3) return null - const event = readBytes(message.payload, 5) - if (!event || readInteger(event, 1, -1) !== 1) return null - const state = readInteger(event, 2, -1) - return state === 0 ? false : state === 1 ? true : null -} diff --git a/app/graphics/display-list.md b/app/graphics/display-list.md new file mode 100644 index 00000000..8f6562fb --- /dev/null +++ b/app/graphics/display-list.md @@ -0,0 +1,92 @@ +# TypeScript display lists + +`GrayImage.drawDisplayList(list, x, y, width, height, depth)` retains a list alongside +the image's ordinary pixels. Existing frame submission (`prepareFrameDraws`) and +shell scene submission carry it to Kotlin. Bounds describe its footprint for plane +occlusion; draw coordinates are relative to its placement. Keep the list, its image +resources, and their pixel buffers immutable after submission. + +```ts +import { DrawExpression as E } from './draw-expression'; +import { DrawOp, type DisplayList } from './display-list'; + +const list: DisplayList = { + resources: [], + timeline: { token: animationId, startedAt: Date.now() }, + calls: [{ + op: DrawOp.ROUNDED_RECT, + x: E.progress(300).ease().lerp(E.f32(0), E.f32(100)).toInt(), + y: 0, + width: 40, height: 20, radius: 6, + background: 4, border: 15, + }], +}; +frame.drawDisplayList(list, 20, 30, 140, 20, 0); +``` + +Supported calls in this first pass: + +- `ROUNDED_RECT`: literal or expression `x`/`y`, literal dimensions/radius, and + 4-bit colors (0–15). Border 16 means no border. Fill takes the maximum of the + background color and existing pixels. +- `IMAGE`: a local resource index, integer `x`/`y`, and `transparent`. Transparent + images skip pixels quantized to zero. Resources have `width`, `height`, and + gray8 `pixels`; a baked `GrayImage` can be used directly. +- `RECT_COPY`: a local resource index, source rectangle and destination + `dx`/`dy`. Source x/y and `dx`/`dy` accept expressions (revision 29); the + source rectangle must stay inside the source at every time, or firmware + rejects the list. `SCREEN` reads the uncomposed frame; its source coordinates + are also relative to this list's placement. Each call may add its own `depth` + to the list's depth. A screen restore normally uses the negative of the list depth. + +Kotlin allocates/cache-pins all image resources, resolves the local indices, applies +surface placement, and includes the calls in the root firmware display list. The +phone's existing display-list renderer consumes the same compiled calls. This uses +the current firmware protocol; no firmware update is needed for this bridge change. + +## Expressions and time + +`E.i32` and `E.f32` construct typed constants. Builders support arithmetic, min/max, +integer remainder, negation, explicit conversions, lerp, and the easing functions +in `ExprOp`. `ease()` defaults to smoothstep. Integers wrap at 32 bits; float +operations round to f32. The firmware's existing stack/error semantics apply. +Rounded-rectangle x/y and rect-copy source/destination coordinates accept +expressions. `E.progress(duration, delay)` stays 0 for the first `delay` ms of +the timeline, so one list can run motions that started at different times. + +`maximum.time()` returns `min(milliseconds since PRESENT, maximum)`. The existing +45 ms repaint timer remains active while this is below maximum. `E.elapsed()` is +a bridge binding: Kotlin replaces it with the timeline's elapsed milliseconds when +building a new root list. It is a constant within that PRESENT, not a second clock. +`E.progress(durationMs)` combines the two and clamps to 0..1 so intervening frames +and PRESENTs continue an animation instead of restarting it. + +Keep `timeline.token` and `timeline.startedAt` stable for an animation; use a new +token when restarting it. JS sends an elapsed duration, so JS and native clocks +need not share an epoch. Fingerprints include the token and expression code but +exclude the changing elapsed duration. Without a timeline, raw `time()` expressions +start at each PRESENT. Native compilation strips the bridge-only elapsed opcode. + +`menu-selection-list.ts` authors both the rounded highlight and the transparent row +image using this API. `menu-scroll-list.ts` authors a menu scroll or bounce: a +viewport-sized copy from a strip resource whose source y animates, then the +highlight. The bounce curve (`app/ui/menu-scroll-motion.ts`) needs no branches: +the easing ops clamp their input to 0..1, so `ease(t / p)` holds at 1 after the +peak and `ease((t - p) / (1 - p))` holds at 0 before it. Navigation eligibility, duration, and easing remain in +TypeScript. There is no animated-menu tag or menu animation policy in Kotlin. + +## Bridge framing + +Tag 7 is followed by a little-endian u32 byte length and a bounded body: +placement (i16 x/y, u16 width/height, i8 depth), timeline (u32 token/elapsed), +u16 resource count, gray8 images (u16 width/height followed by pixels), u16 call +count, then calls. Calls start with the firmware opcode and an i16 relative depth; +operands otherwise use their fixed-width fields, except rounded x/y and rect-copy +x/y/dx/dy which use extended varints. Expressions use the firmware bytecode plus bridge-only +`ELAPSED = 128`. Resource indices are local to this record, never firmware IDs. +Rect-copy source x/y are signed here to permit relative SCREEN coordinates. + +Limits: 256 images per record, 64 KiB per packed image, 4 MiB per bridge record, +4096 calls, and 900 input bytes per expression. Native validates the bound program +against the firmware's 1024-byte limit. The combined scene must also fit the +existing resource cache, root-list size, and VM stack limits. diff --git a/app/graphics/display-list.ts b/app/graphics/display-list.ts new file mode 100644 index 00000000..9f5491a1 --- /dev/null +++ b/app/graphics/display-list.ts @@ -0,0 +1,135 @@ +import { DrawBytes, encodeValue, evaluate, integer, readValue, type DrawValue } from './draw-expression'; + +/** Local resources are assigned firmware cache IDs by Kotlin. SCREEN reads the uncomposed frame. */ +export const SCREEN = 65535; +export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8 } as const; +export const DISPLAY_LIST_RECORD = 7; +export type ListImage = { readonly width: number; readonly height: number; readonly pixels: Uint8Array }; +export type ListCall = ( + | { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number } + | { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean } + | { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue } +) & { depth?: number }; +export type DisplayList = { + readonly resources: readonly ListImage[]; + readonly calls: readonly ListCall[]; + /** Stable identity plus JS clock origin. Native binds ELAPSED again for each PRESENT. */ + /** Set by the decoder: the local PRESENT origin for software replay. */ + readonly presentedAt?: number; + readonly timeline?: { readonly token: number; readonly startedAt: number }; +}; +export type PlacedDisplayList = { displayList: DisplayList; x: number; y: number; width: number; height: number; depth: number }; + +class Writer { + bytes: number[] = []; + u8(n: number): void { this.bytes.push(integer(n, 0, 255)); } + i8(n: number): void { this.u8(integer(n, -128, 127) & 255); } + u16(n: number): void { integer(n, 0, 65535); this.bytes.push(n & 255, n >>> 8); } + i16(n: number): void { this.u16(integer(n, -32768, 32767) & 65535); } + u32(n: number): void { integer(n, 0, 4294967295); this.u16(n & 65535); this.u16(n >>> 16); } + raw(bytes: ArrayLike): void { for (let i = 0; i < bytes.length; i++) this.bytes.push(bytes[i]); } +} + +/** Length-delimited bridge envelope; call operands use fixed widths except extended x/y. */ +export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()): Uint8Array { + const { displayList: list } = placed, out = new Writer(); + integer(placed.width, 1, 640); integer(placed.height, 1, 480); + out.i16(placed.x); out.i16(placed.y); out.u16(placed.width); out.u16(placed.height); out.i8(placed.depth); + out.u32(list.timeline?.token ?? 0); + out.u32(list.timeline ? Math.min(2147483647, Math.max(0, Math.trunc(now - list.timeline.startedAt))) : 0); + integer(list.resources.length, 0, 256); out.u16(list.resources.length); + for (const image of list.resources) { + integer(image.width, 1, 640); integer(image.height, 1, 480); + if (image.pixels.length !== image.width * image.height || 5 + Math.ceil(image.width / 2) * image.height > 65536) throw new Error('Invalid display-list image'); + out.u16(image.width); out.u16(image.height); out.raw(image.pixels); + } + integer(list.calls.length, 0, 4096); out.u16(list.calls.length); + for (const call of list.calls) { + out.u8(call.op); out.i16(call.depth ?? 0); + integer(placed.depth + (call.depth ?? 0), -128, 127); + if (call.op === DrawOp.ROUNDED_RECT) { + out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); + out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.u16(call.radius); + out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16)); + } else { + if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1); + if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate'); + if (call.op === DrawOp.IMAGE && call.resource === SCREEN) throw new Error('Use rectCopy to read SCREEN'); + out.u16(call.resource); + if (call.op === DrawOp.IMAGE) { out.i16(call.x); out.i16(call.y); out.u8(call.transparent ? 31 : 15); } + else { + out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); + out.raw(encodeValue(call.dx)); out.raw(encodeValue(call.dy)); + } + } + } + if (out.bytes.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit'); + const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.bytes.length); header.raw(out.bytes); + return Uint8Array.from(header.bytes); +} + +export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.now()): { placed: PlacedDisplayList; end: number } { + const envelope = new DrawBytes(bytes.subarray(offset)); + if (envelope.u8() !== DISPLAY_LIST_RECORD) throw new Error('Invalid display-list record'); + const length = envelope.u32(); integer(length, 0, 4 * 1024 * 1024); + const r = new DrawBytes(envelope.take(length)); + const x = r.i16(), y = r.i16(), width = r.u16(), height = r.u16(), depth = r.u8() << 24 >> 24; + const token = r.u32(), elapsed = r.u32(), count = r.u16(); integer(count, 0, 256); + const resources: ListImage[] = []; + for (let i = 0; i < count; i++) { const width = r.u16(), height = r.u16(); resources.push({ width, height, pixels: r.take(width * height) }); } + const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096); + for (let i = 0; i < callCount; i++) { + const op = r.u8(), depth = r.i16(); + if (op === DrawOp.ROUNDED_RECT) calls.push({ op, depth, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() }); + else if (op === DrawOp.IMAGE) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 }); + else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) }); + else throw new Error('Unsupported display-list call'); + } + if (r.offset !== length) throw new Error('Trailing display-list bytes'); + const placed = { x, y, width, height, depth, displayList: { resources, calls, presentedAt: now, timeline: { token, startedAt: now - elapsed } } }; + // The writer is also the shared structural validator for software preview records. + encodeDisplayList(placed, now); + return { placed, end: offset + envelope.offset }; +} + +export function dimDisplayList(list: DisplayList, factor: number): DisplayList { + const dim = (v: number) => v === 0 ? 0 : Math.max(1, Math.round(v * factor)); + return { ...list, resources: list.resources.map(r => ({ ...r, pixels: r.pixels.map(dim) })), + calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) } : c) }; +} + +/** Fallback compositor; native preview and glasses replay the compiled list with their existing timers. */ +export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width: number, height: number, placed: PlacedDisplayList, right = false, now = Date.now()): void { + const q = (n: number) => Math.min(15, (n + 8) >> 4) * 16; + const list = placed.displayList; + const elapsed = list.timeline ? Math.max(0, (list.presentedAt ?? now) - list.timeline.startedAt) : 0; + const presentTime = list.presentedAt === undefined ? 0 : Math.max(0, now - list.presentedAt); + const inside = (x: number, y: number, w: number, h: number, radius: number) => { + if (x < 0 || y < 0 || x >= w || y >= h) return false; + const r = Math.min(radius, Math.floor(w / 2), Math.floor(h / 2)); + const dx = Math.max(0, 2 * r - (2 * Math.min(x, w - 1 - x) + 1)), dy = Math.max(0, 2 * r - (2 * Math.min(y, h - 1 - y) + 1)); + return dx * dx + dy * dy <= 4 * r * r; + }; + for (const c of placed.displayList.calls) { + const depth = placed.depth + (c.depth ?? 0), shift = right ? -Math.floor((depth + 1) / 2) : Math.floor(depth / 2); + if (c.op === DrawOp.ROUNDED_RECT) { + const x = placed.x + evaluate(c.x, elapsed, presentTime).value + shift, y = placed.y + evaluate(c.y, elapsed, presentTime).value; + for (let yy = 0; yy < c.height; yy++) for (let xx = 0; xx < c.width; xx++) { + if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || !inside(xx, yy, c.width, c.height, c.radius)) continue; + const p = (y + yy) * width + x + xx; + output[p] = c.border !== 16 && !inside(xx - 1, yy - 1, c.width - 2, c.height - 2, Math.max(0, c.radius - 1)) ? c.border * 16 : Math.max(output[p], c.background * 16); + } + } else { + const source = c.resource === SCREEN ? { width, height, pixels: screen } : placed.displayList.resources[c.resource]; + const w = c.op === DrawOp.IMAGE ? source.width : c.width, h = c.op === DrawOp.IMAGE ? source.height : c.height; + const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value; + const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0); + const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy)); + for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) { + if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue; + const v = q(source.pixels[(sy + yy) * source.width + sx + xx]); + if (c.op !== DrawOp.IMAGE || !c.transparent || v !== 0) output[(y + yy) * width + x + xx] = v; + } + } + } +} diff --git a/app/graphics/draw-expression.ts b/app/graphics/draw-expression.ts new file mode 100644 index 00000000..dab4604e --- /dev/null +++ b/app/graphics/draw-expression.ts @@ -0,0 +1,160 @@ +/** Revision 27 expression bytecode. ELAPSED is a bridge binding, never sent to firmware. */ +export const ExprOp = { + I32: 1, F32: 2, DUP: 3, DROP: 4, SWAP: 5, + IADD: 16, ISUB: 17, IMUL: 18, IDIV: 19, IMOD: 20, INEG: 21, IMIN: 22, IMAX: 23, + FADD: 32, FSUB: 33, FMUL: 34, FDIV: 35, FNEG: 36, FMIN: 37, FMAX: 38, + I2F: 48, F2I: 49, TIME: 50, LERP: 64, SMOOTHSTEP: 65, + EASE_IN_QUAD: 66, EASE_OUT_QUAD: 67, EASE_IN_OUT_QUAD: 68, + EASE_IN_CUBIC: 69, EASE_OUT_CUBIC: 70, EASE_IN_OUT_CUBIC: 71, + ELAPSED: 128, +} as const; + +export function integer(value: number, min: number, max: number): number { + if (!Number.isInteger(value) || value < min || value > max) throw new Error('Draw integer out of range'); + return value; +} +export function varint(value: number, signed = true): number[] { + integer(value, signed ? -2147483648 : 0, signed ? 2147483647 : 4294967295); + const widths = [7, 14, 21, 28, 32], prefixes = [0, 128, 192, 224, 240]; + const size = widths.findIndex(bits => bits === 32 || (signed + ? value >= -(2 ** (bits - 1)) && value < 2 ** (bits - 1) : value < 2 ** bits)) + 1; + const bytes = [size === 5 ? 240 : prefixes[size - 1] | ((value >>> (8 * (size - 1))) & ((1 << (8 - size)) - 1))]; + for (let i = 1; i < size; i++) bytes.push((value >>> (8 * (size - 1 - i))) & 255); + return bytes; +} + +export class DrawBytes { + offset = 0; + constructor(readonly bytes: Uint8Array) {} + take(size: number): Uint8Array { + integer(size, 0, this.bytes.length - this.offset); + const result = this.bytes.slice(this.offset, this.offset + size); this.offset += size; return result; + } + u8(): number { return this.take(1)[0]; } + u16(): number { return this.u8() | this.u8() << 8; } + i16(): number { return this.u16() << 16 >> 16; } + u32(): number { return (this.u16() | this.u16() << 16) >>> 0; } + variable(signed = true): number { + const first = this.u8(); + const size = first < 128 ? 1 : first < 192 ? 2 : first < 224 ? 3 : first < 240 ? 4 : first === 240 ? 5 : 0; + if (!size) throw new Error('Reserved extended-varint prefix'); + let value = size === 5 ? 0 : first & ((1 << (8 - size)) - 1); + for (let i = 1; i < size; i++) value = (value << 8) | this.u8(); + const bits = [7, 14, 21, 28, 32][size - 1]; + return signed ? bits < 32 ? value << (32 - bits) >> (32 - bits) : value : value >>> 0; + } +} + +/** Immutable, typed expression builder; operations use firmware i32/f32 semantics. */ +export class DrawExpression { + private constructor(readonly type: T, readonly code: readonly number[]) { + // Leave room for native elapsed bindings and surface translation. + if (code.length > 900) throw new Error('Draw expression too large'); + } + static i32(value: number): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.I32, ...varint(value)]); } + static f32(value: number): DrawExpression<'f32'> { + if (!Number.isFinite(Math.fround(value))) throw new Error('Invalid draw float'); + const bytes = new Uint8Array(4); new DataView(bytes.buffer).setFloat32(0, value, true); + return new DrawExpression('f32', [ExprOp.F32, ...bytes]); + } + /** Time already elapsed on this list's timeline when the next PRESENT happens. */ + static elapsed(): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.ELAPSED]); } + /** + * A 0..1 timeline fraction that survives subsequent PRESENT clock resets. + * With a delay, it stays 0 for the first delayMs of the timeline; a + * negative delay continues a motion that started before the timeline. + */ + static progress(durationMs: number, delayMs = 0): DrawExpression<'f32'> { + integer(delayMs, -2147483647, 2147483647); + if (delayMs + durationMs <= 0) return DrawExpression.f32(1); + const duration = DrawExpression.i32(integer(durationMs, 1, 2147483647)); + const end = DrawExpression.i32(integer(delayMs + durationMs, 1, 2147483647)); + const elapsed = DrawExpression.elapsed().min(end); + let total = end.sub(elapsed).max(DrawExpression.i32(0)).time().add(elapsed); + if (delayMs) total = total.sub(DrawExpression.i32(delayMs)).max(DrawExpression.i32(0)); + return total.toFloat().div(duration.toFloat()); + } + static decode(code: Uint8Array): DrawExpression<'i32'> { return new DrawExpression('i32', Array.from(code)); } + private binary(other: DrawExpression, i: number, f: number): DrawExpression { + if (other.type !== this.type) throw new Error('Draw expression type mismatch'); + return new DrawExpression(this.type, [...this.code, ...other.code, this.type === 'i32' ? i : f]); + } + add(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IADD, ExprOp.FADD); } + sub(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.ISUB, ExprOp.FSUB); } + mul(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IMUL, ExprOp.FMUL); } + div(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IDIV, ExprOp.FDIV); } + min(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IMIN, ExprOp.FMIN); } + max(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IMAX, ExprOp.FMAX); } + mod(this: DrawExpression<'i32'>, b: DrawExpression<'i32'>): DrawExpression<'i32'> { return this.binary(b, ExprOp.IMOD, ExprOp.IMOD); } + neg(): DrawExpression { return new DrawExpression(this.type, [...this.code, this.type === 'i32' ? ExprOp.INEG : ExprOp.FNEG]); } + toFloat(this: DrawExpression<'i32'>): DrawExpression<'f32'> { return new DrawExpression('f32', [...this.code, ExprOp.I2F]); } + toInt(this: DrawExpression<'f32'>): DrawExpression<'i32'> { return new DrawExpression('i32', [...this.code, ExprOp.F2I]); } + time(this: DrawExpression<'i32'>): DrawExpression<'i32'> { return new DrawExpression('i32', [...this.code, ExprOp.TIME]); } + ease(this: DrawExpression<'f32'>, op: number = ExprOp.SMOOTHSTEP): DrawExpression<'f32'> { + integer(op, ExprOp.SMOOTHSTEP, ExprOp.EASE_IN_OUT_CUBIC); + return new DrawExpression('f32', [...this.code, op]); + } + lerp(this: DrawExpression<'f32'>, from: DrawExpression<'f32'>, to: DrawExpression<'f32'>): DrawExpression<'f32'> { + return new DrawExpression('f32', [...from.code, ...to.code, ...this.code, ExprOp.LERP]); + } +} +export type DrawValue = number | DrawExpression<'i32'>; +export function encodeValue(value: DrawValue): number[] { + return typeof value === 'number' ? varint(value) : [255, ...varint(value.code.length, false), ...value.code]; +} +export function readValue(reader: DrawBytes): DrawValue { + if (reader.bytes[reader.offset] !== 255) return reader.variable(); + reader.u8(); return DrawExpression.decode(reader.take(reader.variable(false))); +} + +/** Software preview of the same VM; invalid programs return zero and discard pending animation. */ +export function evaluate(value: DrawValue, elapsed: number, presentTime = 0): { value: number; pending: boolean } { + if (typeof value === 'number') return { value, pending: false }; + const reader = new DrawBytes(Uint8Array.from(value.code)), stack: { value: number; float: boolean }[] = []; + let pending = false; + const push = (value: number, float = false) => { + value = float ? Math.fround(value) : value | 0; + if (!Number.isFinite(value) || stack.length === 32) throw new Error('Invalid expression stack'); + stack.push({ value, float }); + }; + const pop = (float: boolean) => { const v = stack.pop(); if (!v || v.float !== float) throw new Error('Expression type mismatch'); return v.value; }; + const toInt = (v: number) => { if (v < -2147483648 || v >= 2147483648) throw new Error('Invalid integer conversion'); return Math.trunc(v); }; + try { + while (reader.offset < reader.bytes.length) { + const op = reader.u8(); + if (op === ExprOp.I32) push(reader.variable()); + else if (op === ExprOp.F32) { const b = reader.take(4); push(new DataView(b.buffer).getFloat32(0, true), true); } + else if (op === ExprOp.ELAPSED) push(Math.min(2147483647, Math.max(0, elapsed))); + else if (op === ExprOp.I2F) push(pop(false), true); + else if (op === ExprOp.F2I) push(toInt(pop(true))); + else if (op === ExprOp.TIME) { const max = pop(false); if (max < 0) throw new Error('Invalid time'); const t = Math.min(Math.max(0, presentTime), max); pending ||= t !== max; push(t); } + else if (op === ExprOp.INEG || op === ExprOp.FNEG) push(-pop(op === ExprOp.FNEG), op === ExprOp.FNEG); + else if (op >= ExprOp.IADD && op <= ExprOp.IMAX || op >= ExprOp.FADD && op <= ExprOp.FMAX) { + const float = op >= ExprOp.FADD, b = pop(float), a = pop(float); + const key = float ? op - ExprOp.FADD : op - ExprOp.IADD; + if ((key === 3 || !float && key === 4) && b === 0) throw new Error('Division by zero'); + const result = key === 0 ? a + b : key === 1 ? a - b : key === 2 ? float ? a * b : Math.imul(a, b) : key === 3 ? a / b : !float && key === 4 ? a % b : key === (float ? 5 : 6) ? Math.min(a, b) : Math.max(a, b); + push(result, float); + } else if (op === ExprOp.LERP) { const t = pop(true), b = pop(true), a = pop(true); push(a + Math.fround(Math.fround(b - a) * t), true); } + else if (op >= ExprOp.SMOOTHSTEP && op <= ExprOp.EASE_IN_OUT_CUBIC) { + const t = Math.max(0, Math.min(1, pop(true))), u = Math.fround(1 - t), f = Math.fround; + let result: number; + switch (op) { + case ExprOp.SMOOTHSTEP: result = f(t * t) * f(3 - f(2 * t)); break; + case ExprOp.EASE_IN_QUAD: result = t * t; break; + case ExprOp.EASE_OUT_QUAD: result = 1 - f(u * u); break; + case ExprOp.EASE_IN_OUT_QUAD: result = t < .5 ? f(2 * t) * t : 1 - f(f(2 * u) * u); break; + case ExprOp.EASE_IN_CUBIC: result = f(t * t) * t; break; + case ExprOp.EASE_OUT_CUBIC: result = 1 - f(f(u * u) * u); break; + default: result = t < .5 ? f(f(4 * t) * t) * t : 1 - f(f(f(4 * u) * u) * u); + } + push(result, true); + } else if (op === ExprOp.DUP) { const a = stack[stack.length - 1]; if (!a) throw new Error('Underflow'); push(a.value, a.float); } + else if (op === ExprOp.DROP) { if (!stack.pop()) throw new Error('Underflow'); } + else if (op === ExprOp.SWAP) { if (stack.length < 2) throw new Error('Underflow'); const a = stack.pop()!, b = stack.pop()!; stack.push(a, b); } + else throw new Error('Unknown expression opcode'); + } + const top = stack.pop(); if (!top) throw new Error('Empty expression'); + return { value: top.float ? toInt(top.value) : top.value, pending }; + } catch { return { value: 0, pending: false }; } +} diff --git a/app/graphics/glyph-wire.ts b/app/graphics/glyph-wire.ts index 970fe821..cd15f0e2 100644 --- a/app/graphics/glyph-wire.ts +++ b/app/graphics/glyph-wire.ts @@ -1,3 +1,4 @@ +import { DrawRecordKind, encodePresentation } from "./presentation-wire"; /** * Marshals deferred-draw identity (text glyphs and icon images) to the Kotlin * side for the texture-cache pipeline (CFW modes 18/19/20; see @@ -84,6 +85,7 @@ const fwRegistered = new Set(); * unseen ink members' rasters with the Kotlin FwGlyphAtlas as a side effect. */ function prepareFwTextRun(placed: PlacedFwText): boolean { + if (!textureAtlasAvailable()) return false; if (!inRange16(placed.x) || !inRange16(placed.y)) return false; if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false; let registration: Array<{ cp: number; data: NonNullable> }> | null = null; @@ -152,11 +154,12 @@ function inRange16(v: number): boolean { * in draw order. Returns null when nothing is expressible (or without a native atlas). */ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null { - if (!textureAtlasAvailable() || draws.length === 0) return null; + if (draws.length === 0) return null; // Pass 1: resolve ids, register unseen rasters, size the buffer. let registration: Map | null = null; const fwRunOk = new Map(); + const presentations = new Map(); let bytes = 0; for (const placed of draws) { if (placed.kind === "glyph") { @@ -175,7 +178,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | (placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph); } else if (placed.kind === "image") { if (!inRange16(placed.x) || !inRange16(placed.y)) continue; - if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES; + if (placed.presentation) { const record = encodePresentation(placed); presentations.set(placed, record); bytes += record.length; } + else if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES; } else { const ok = prepareFwTextRun(placed); fwRunOk.set(placed, ok); @@ -198,7 +202,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | if (!inRange16(placed.x) || !inRange16(placed.y)) continue; const state = fontWireState(placed.font); if (!state || !representableGlyph(placed.font, placed.glyph)) continue; - out.setUint8(offset, 0); + out.setUint8(offset, DrawRecordKind.GLYPH); out.setUint16(offset + 1, state.fontId, true); out.setUint32(offset + 3, placed.glyph.encoding, true); out.setInt16(offset + 7, placed.x, true); @@ -207,16 +211,17 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | offset += GLYPH_RECORD_BYTES; } else if (placed.kind === "image") { if (!inRange16(placed.x) || !inRange16(placed.y)) continue; + if (placed.presentation) { const bytes = presentations.get(placed)!; new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; } const id = imageId(placed); if (id === null) continue; - out.setUint8(offset, 1); + out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE); out.setUint32(offset + 1, id, true); out.setInt16(offset + 5, placed.x, true); out.setInt16(offset + 7, placed.y, true); offset += IMAGE_RECORD_BYTES; } else { if (!fwRunOk.get(placed)) continue; - out.setUint8(offset, 2); + out.setUint8(offset, DrawRecordKind.FIRMWARE_TEXT); out.setInt16(offset + 1, placed.x, true); out.setInt16(offset + 3, placed.y, true); out.setUint8(offset + 5, placed.value); diff --git a/app/graphics/image.ts b/app/graphics/image.ts index 70028f61..ac339a3d 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -1,3 +1,6 @@ +import { menuSelectionList } from "./menu-selection-list"; +import { encodeDisplayList, dimDisplayList, paintDisplayList, type DisplayList } from "./display-list"; +import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion"; import { Glyph } from "./bdffont"; import { wrapText } from "./textwrap"; @@ -73,6 +76,7 @@ export type PlacedGlyph = { * from the moment it is placed. */ export type PlacedImage = { + presentation?: { displayList?: DisplayList; mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] }; kind: "image"; source: GrayImage; x: number; @@ -148,7 +152,9 @@ export class GrayImage { this.width = width; this.height = height; this.pixels = new Uint8Array(width * height); - this.clear(fill); + if (fill !== 0) { + this.clear(fill); + } } get draws(): readonly DeferredDraw[] { @@ -391,6 +397,9 @@ export class GrayImage { hash = mixInt(hash, placed.source.width); hash = mixInt(hash, placed.source.height); hash = mixInt(hash, placed.source.sourceContentHash32()); + if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value); + if (p.displayList) for (const byte of encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)) hash = mixInt(hash, byte); + } } else { hash = mixInt(hash, 0xf17e); hash = mixInt(hash, placed.font.atlasTag); @@ -501,6 +510,31 @@ export class GrayImage { this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs }); } + /** Retain the selected row separately from the menu surface for glasses-side composition. */ + drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2, animation?: MenuHighlightAnimation): void { + const baked = source.withDrawsBaked(); + this.drawList.push({ kind: "image", source: baked, x, y, + presentation: { radius, background, border, depth, displayList: menuSelectionList(baked, background, border, radius, animation) } }); + } + + /** Submit a retained list alongside this frame; coordinates are relative to x/y. */ + drawDisplayList(displayList: DisplayList, x: number, y: number, width: number, height: number, depth = 0): void { + this.drawList.push({ kind: "image", source: new GrayImage(width, height), x, y, + presentation: { radius: 0, background: 0, border: 0, depth, displayList } }); + } + + /** Replay an image at stereo depth. Masked images preserve intentional gray8 black (1). */ + drawDepthImage(source: GrayImage, x: number, y: number, depth: number, masked = false): void { + this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y, + presentation: { mode: masked ? "masked-image" : "image", radius: 0, background: 0, border: 0, depth } }); + } + + copyPresentationsInto(target: GrayImage, dx: number, dy: number): void { + for (const placed of this.drawList) if (placed.kind === "image" && placed.presentation) { + target.drawList.push({ ...placed, x: placed.x + dx, y: placed.y + dy }); + } + } + /** * Bake this image's deferred draws into its own pixels and clear the list. * Used where later raster must be able to cover earlier draws within the @@ -513,7 +547,8 @@ export class GrayImage { if (placed.kind === "glyph") { rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value); } else if (placed.kind === "image") { - this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); + if (placed.presentation) { const one = new GrayImage(this.width, this.height); one.drawList.push(placed); one.bakeDrawsInto(this, 0, 0); } + else this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); } else { placed.font.bakeFwTextRun(this, placed, 0, 0); } @@ -534,7 +569,7 @@ export class GrayImage { const copy = new GrayImage(this.width, this.height, 0); copy.pixels.set(this.pixels); for (const placed of this.drawList) { - if (placed.kind === "image") { + if (placed.kind === "image" && !placed.presentation) { copy.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); } } @@ -544,7 +579,10 @@ export class GrayImage { if (value !== 0) pixels[i] = dimValue(value, scale); } for (const placed of this.drawList) { - if (placed.kind !== "image") { + if (placed.kind === "image" && placed.presentation) { + const p = placed.presentation; + copy.drawList.push({ ...placed, source: placed.source.dimmed(factor), presentation: { ...p, background: dimValue(p.background, scale), border: dimValue(p.border, scale), displayList: p.displayList && dimDisplayList(p.displayList, factor) } }); + } else if (placed.kind !== "image") { copy.drawList.push({ ...placed, value: dimValue(placed.value, scale) }); } } @@ -556,11 +594,11 @@ export class GrayImage { * Returns this image unchanged when there are none, otherwise a baked copy * with an empty draw list. */ - withDrawsBaked(): GrayImage { + withDrawsBaked(presentations = true): GrayImage { if (!this.drawList.length) return this; const baked = new GrayImage(this.width, this.height, 0); baked.pixels.set(this.pixels); - this.bakeDrawsInto(baked, 0, 0); + this.bakeDrawsInto(baked, 0, 0, presentations); return baked; } @@ -579,11 +617,33 @@ export class GrayImage { } /** Rasterize this image's deferred draws into target, translated by (dx, dy). */ - bakeDrawsInto(target: GrayImage, dx: number, dy: number): void { + bakeDrawsInto(target: GrayImage, dx: number, dy: number, presentations = true): void { for (const placed of this.drawList) { if (placed.kind === "glyph") { rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value); } else if (placed.kind === "image") { + if (placed.presentation) { + if (!presentations) continue; + if (placed.presentation.displayList) { + paintDisplayList(target.pixels, target.pixels.slice(), target.width, target.height, + { displayList: placed.presentation.displayList, x: placed.x + dx, y: placed.y + dy, width: placed.source.width, height: placed.source.height, depth: placed.presentation.depth }); + continue; + } + if (placed.presentation.mode) { + target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true }); + continue; + } + const { radius, background, border } = placed.presentation; + const fill = new GrayImage(placed.source.width, placed.source.height); + fill.fillRoundedRect(0, 0, fill.width, fill.height, background, radius); + for (let y = 0; y < fill.height; y++) for (let x = 0; x < fill.width; x++) { + const tx = placed.x + dx + x, ty = placed.y + dy + y; + if (tx >= 0 && ty >= 0 && tx < target.width && ty < target.height) { + const i = ty * target.width + tx; target.pixels[i] = Math.max(target.pixels[i], fill.pixels[y * fill.width + x]); + } + } + target.drawRoundedRect(placed.x + dx, placed.y + dy, fill.width, fill.height, border, radius); + } target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true }); } else { placed.font.bakeFwTextRun(target, placed, dx, dy); diff --git a/app/graphics/menu-scroll-list.ts b/app/graphics/menu-scroll-list.ts new file mode 100644 index 00000000..0f6cc839 --- /dev/null +++ b/app/graphics/menu-scroll-list.ts @@ -0,0 +1,45 @@ +import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'; +import { DrawExpression as E, type DrawValue } from './draw-expression'; +import { DrawOp, type DisplayList, type ListCall, type ListImage } from './display-list'; + +export type MenuScrollHighlight = { + /** Row top within the viewport; clamped inside it. */ + y: DrawValue; + width: number; + height: number; + radius: number; + /** Gray8, like drawMenuSelection. */ + background: number; + border: number; +}; + +/** + * A highlight sliding by (0, dy) to rest at `y`, on a list timeline that + * started at `timelineStart` (the slide may have started before or after it). + */ +export function slidingHighlightY(y: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> { + if (!motion?.dy) return E.i32(y); + return E.progress(motion.durationMs, motion.startedAt - timelineStart).ease() + .lerp(E.i32(y + motion.dy).toFloat(), E.i32(y).toFloat()).toInt(); +} + +/** + * A menu viewport moving over a pre-rendered strip. `strip` holds every row + * visible at any point of the motion, and sits `stripX` pixels into the + * viewport; `sourceY` is the strip row at the viewport's top, animated on + * `timeline`. The highlight is max-blended over the copied rows. + */ +export function menuScrollList(strip: ListImage, stripX: number, viewportHeight: number, + timeline: { token: number; startedAt: number }, sourceY: DrawValue, highlight?: MenuScrollHighlight): DisplayList { + const calls: ListCall[] = [ + { op: DrawOp.RECT_COPY, resource: 0, x: 0, y: sourceY, width: strip.width, height: viewportHeight, dx: stripX, dy: 0 }, + ]; + if (highlight) { + const q = (n: number) => Math.min(15, (n + 8) >> 4); + const y = typeof highlight.y === 'number' ? highlight.y + : highlight.y.max(E.i32(0)).min(E.i32(viewportHeight - highlight.height)); + calls.push({ op: DrawOp.ROUNDED_RECT, x: 0, y, width: highlight.width, height: highlight.height, radius: highlight.radius, + background: q(highlight.background), border: q(highlight.border) }); + } + return { resources: [strip], timeline: { token: timeline.token, startedAt: timeline.startedAt }, calls }; +} diff --git a/app/graphics/menu-selection-list.ts b/app/graphics/menu-selection-list.ts new file mode 100644 index 00000000..6a334039 --- /dev/null +++ b/app/graphics/menu-selection-list.ts @@ -0,0 +1,18 @@ +import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'; +import { DrawExpression as E } from './draw-expression'; +import { DrawOp, type DisplayList, type ListImage } from './display-list'; + +/** Menu policy and easing live in TypeScript; the bridge carries only generic calls/expressions. */ +export function menuSelectionList(image: ListImage, background: number, border: number, radius: number, animation?: MenuHighlightAnimation): DisplayList { + const coordinate = (delta: number) => { + if (!animation || !delta) return 0; + const progress = E.progress(animation.durationMs).ease(); + return progress.lerp(E.i32(delta).toFloat(), E.f32(0)).toInt(); + }; + const q = (n: number) => Math.min(15, (n + 8) >> 4); + return { resources: [image], timeline: animation, + calls: [ + { op: DrawOp.ROUNDED_RECT, x: coordinate(animation?.dx ?? 0), y: coordinate(animation?.dy ?? 0), width: image.width, height: image.height, radius, background: q(background), border: q(border) }, + { op: DrawOp.IMAGE, resource: 0, x: 0, y: 0, transparent: true }, + ] }; +} diff --git a/app/graphics/plane.ts b/app/graphics/plane.ts index 15cf5c98..f644fee8 100644 --- a/app/graphics/plane.ts +++ b/app/graphics/plane.ts @@ -1,4 +1,4 @@ -import { GrayImage, type DeferredDraw } from "./image"; +import { GrayImage, type DeferredDraw, type PlacedImage } from "./image"; /** * The unit apps (and the shell) submit to the compositor: instead of one @@ -8,18 +8,17 @@ import { GrayImage, type DeferredDraw } from "./image"; * transparent, 1 is black (the two quantize to the same 4bpp shade, so * reserving 0 costs nothing visible). * - * This representation exists to carry structure the wire pipeline will later - * exploit: a plane's glyph list can become on-glasses display-list references - * (texture cache), and the offset can diverge per lens for stereo depth. For - * now the compositor boundary just flattens (flattenPlanes) and feeds the - * existing incremental-update and screen-preview pipeline. + * Static planes form the incremental screen buffer. Planes with stereo depth + * and retained presentation draws replay from resources in the root display list. */ export type Plane = { + shellKey?: number; + depth?: number; + dimUnderneath?: number; image: GrayImage; /** * Offset of the plane's top-left within the submitted frame. Applied - * identically to both lenses for now; a per-lens divergence (stereo depth - * for floating overlays) is the planned extension. + * before any per-lens horizontal offset from depth. */ x: number; y: number; @@ -70,8 +69,9 @@ export function flattenPlanes( /** * Flatten planes and also return the frame's deferred draws (glyphs and * images), translated into frame coordinates and in bake order (plane order, - * then each image's draw order). The flattened image has every draw baked in, - * exactly as flattenPlanes produces; the list preserves the draws' identity + * then each image's draw order). Regular draws are baked into the image; + * selected-menu presentation records remain unbaked for root-list replay. + * The list preserves the draws' identity * so the texture-cache pipeline can replay them as on-glasses cached draws * (see graphics/glyph-wire.ts). */ @@ -79,15 +79,54 @@ export function flattenPlanesWithDraws( planes: readonly Plane[], size?: { width: number; height: number }, ): { image: GrayImage; draws: DeferredDraw[] } { - const image = flattenPlanes(planes, size); + const rasters = planes.map(p => p.image.withDrawsBaked(false)); + const image = flattenPlanes(planes.map((plane, i) => ({ ...plane, image: plane.depth ? new GrayImage(plane.image.width, plane.image.height) : rasters[i]! })), size); const draws: DeferredDraw[] = []; - for (const plane of planes) { - for (const placed of plane.image.draws) { - draws.push( - plane.x === 0 && plane.y === 0 - ? placed - : { ...placed, x: placed.x + plane.x, y: placed.y + plane.y }, - ); + for (const [index, plane] of planes.entries()) { + const retained: DeferredDraw[] = []; + if (plane.depth) { + // Keep the whole menu off the screen buffer, including its black background. + const raster = rasters[index]!; + let left = raster.width, top = raster.height, right = -1, bottom = -1; + for (let y = 0; y < raster.height; y++) for (let x = 0; x < raster.width; x++) if (raster.pixels[y*raster.width+x]) { + left = Math.min(left,x); top = Math.min(top,y); right = Math.max(right,x); bottom = Math.max(bottom,y); + } + if (right >= left) { + const crop = new GrayImage(right-left+1,bottom-top+1); + for(let y=0;y d.kind === "image" && d.presentation).map(d => { + const draw = d as PlacedImage; + return { ...draw, presentation: { ...draw.presentation!, depth: draw.presentation!.depth + plane.depth! } }; + })] : plane.image.draws; + for (const placed of planeDraws) { + const translated = { ...placed, x: placed.x + plane.x, y: placed.y + plane.y }; + if (translated.kind === "image" && translated.presentation) { + // Restore opaque pixels from later planes after replaying this row. The screen + // already contains those pixels; rect copies preserve rounded/irregular occlusion. + const occlusions: { x: number; y: number; width: number; height: number }[] = []; + const depth = translated.presentation.depth; + const leftShift = Math.floor(depth / 2), rightShift = -Math.floor((depth + 1) / 2); + const left = Math.max(0, translated.x + Math.min(leftShift, rightShift)); + const right = Math.min(image.width, translated.x + translated.source.width + Math.max(leftShift, rightShift)); + for (let y = Math.max(0, translated.y); y < Math.min(image.height, translated.y + translated.source.height); y++) { + let start = -1; + for (let x = left; x <= right; x++) { + const covered = x < right && planes.some((p, i) => i > index && !p.depth && x >= p.x && y >= p.y && x < p.x + p.image.width && y < p.y + p.image.height && rasters[i]!.pixels[(y-p.y)*p.image.width+x-p.x] !== 0); + if (covered && start < 0) start = x; + if (!covered && start >= 0) { + const previous = occlusions.find(r => r.x === start && r.width === x - start && r.y + r.height === y); + if (previous) previous.height++; else occlusions.push({ x: start, y, width: x-start, height: 1 }); + start = -1; + } + } + } + translated.presentation = { ...translated.presentation, occlusions }; + } + draws.push(translated); } } return { image, draws }; @@ -100,6 +139,6 @@ export function flattenPlanesWithDraws( */ export function planesFingerprint(planes: readonly Plane[]): string { return planes - .map((plane) => `${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`) + .map((plane) => `${plane.depth ?? 0}:${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`) .join("|"); } diff --git a/app/graphics/presentation-wire.ts b/app/graphics/presentation-wire.ts new file mode 100644 index 00000000..0e8c361d --- /dev/null +++ b/app/graphics/presentation-wire.ts @@ -0,0 +1,102 @@ +import type { PlacedImage } from './image' +import { DISPLAY_LIST_RECORD, DrawOp, SCREEN, encodeDisplayList, readDisplayList, paintDisplayList, type DisplayList } from './display-list' + +/** TS/native bridge tags, not firmware draw opcodes. Keep in sync with DrawRecordKind.kt. */ +export const DrawRecordKind = { + GLYPH: 0, + TEXTURE_IMAGE: 1, + FIRMWARE_TEXT: 2, + MENU_SELECTION: 3, + TRANSPARENT_IMAGE: 4, + MASKED_IMAGE: 5, + DISPLAY_LIST: DISPLAY_LIST_RECORD, +} as const + +export function isPresentationKind(kind: number | undefined): boolean { + return kind === DrawRecordKind.MENU_SELECTION || kind === DrawRecordKind.TRANSPARENT_IMAGE || + kind === DrawRecordKind.MASKED_IMAGE || kind === DrawRecordKind.DISPLAY_LIST +} + +/** Presentation record: tag, signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */ +export function encodePresentation(draw: PlacedImage): Uint8Array { + const p = draw.presentation! + const { width, height, pixels } = draw.source + if (width < 1 || height < 1 || width > 640 || height > 480 || !p.displayList && 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit') + if (!Number.isInteger(p.depth) || p.depth < -128 || p.depth > 127) throw new Error('Invalid menu depth') + if ((p.occlusions?.length ?? 0) > 2048) throw new Error('Too many menu occlusion rectangles') + if (p.displayList) { + const calls = [...p.displayList.calls, ...(p.occlusions ?? []).map(r => ({ + op: DrawOp.RECT_COPY, resource: SCREEN, x: r.x - draw.x, y: r.y - draw.y, + width: r.width, height: r.height, dx: r.x - draw.x, dy: r.y - draw.y, depth: -p.depth, + }))]; + return encodeDisplayList({ displayList: { ...p.displayList, calls }, x: draw.x, y: draw.y, width, height, depth: p.depth }); + } + const header = 16; + const bytes = new Uint8Array(header + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer) + bytes[0] = p.mode === "image" ? DrawRecordKind.TRANSPARENT_IMAGE + : p.mode === "masked-image" ? DrawRecordKind.MASKED_IMAGE : DrawRecordKind.MENU_SELECTION + view.setInt16(1, draw.x, true); view.setInt16(3, draw.y, true) + view.setUint16(5, width, true); view.setUint16(7, height, true); view.setUint16(9, p.radius, true) + bytes[11] = p.background; bytes[12] = p.border; view.setInt8(13, p.depth) + view.setUint16(14, p.occlusions?.length ?? 0, true) + bytes.set(pixels, header) + let offset = header + pixels.length + for (const rect of p.occlusions ?? []) { + view.setUint16(offset, rect.x, true); view.setUint16(offset + 2, rect.y, true) + view.setUint16(offset + 4, rect.width, true); view.setUint16(offset + 6, rect.height, true); offset += 8 + } + return bytes +} + +export type Selection = { displayList?: DisplayList; mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] } +export function readPresentation(bytes: Uint8Array, offset: number): { selection: Selection; end: number } { + if (bytes[offset] === DrawRecordKind.DISPLAY_LIST) { + const { placed, end } = readDisplayList(bytes, offset); + return { selection: { ...placed, radius: 0, background: 0, border: 0, pixels: new Uint8Array(0), occlusions: [] }, end }; + } + const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true) + const kind = bytes[offset] + const header = 16 + const selection: Selection = { mode: kind === DrawRecordKind.TRANSPARENT_IMAGE ? "image" : kind === DrawRecordKind.MASKED_IMAGE ? "masked-image" : undefined, x: v.getInt16(offset+1,true), y: v.getInt16(offset+3,true), width:w, height:h, + radius:v.getUint16(offset+9,true), background:bytes[offset+11]!, border:bytes[offset+12]!, depth:v.getInt8(offset+13), + pixels:bytes.slice(offset+header,offset+header+w*h), occlusions:[] } + let end=offset+header+w*h + for(let i=0;iMath.min(15,(n+8)>>4), shift=right?-Math.floor((row.depth+1)/2):Math.floor(row.depth/2) + const inside=(x:number,y:number,w:number,h:number,r:number)=>{ + if(x<0||y<0||x>=w||y>=h) return false + r=Math.min(r,Math.floor(w/2),Math.floor(h/2)) + const dx=Math.max(0,2*r-(2*Math.min(x,w-1-x)+1)),dy=Math.max(0,2*r-(2*Math.min(y,h-1-y)+1)) + return dx*dx+dy*dy<=4*r*r + } + const highlightX = row.x, highlightY = row.y + if (!row.mode) for(let yy=0;yy=width||y>=height || !inside(xx,yy,row.width,row.height,row.radius)) continue + const i=y*width+x + output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16) + } + for(let yy=0;yy=width||y>=height) continue + const value=q(row.pixels[yy*row.width+xx]!) + if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[y*width+x]=value*16 + } + for(const r of row.occlusions) for(let yy=r.y;yy=0&&yy>=0&&xx 65536) throw new Error(`Shell surface ${plane.shellKey} exceeds 64 KiB (${w}×${h})`) + const cropped = shellCrop(image, left, top, w, h, plane.shellKey!) + const selections = plane.image.draws + .filter((d): d is PlacedImage => d.kind === "image" && !!d.presentation) + .map(d => encodePresentation({ ...d, x: d.x + plane.x, y: d.y + plane.y, + presentation: { ...d.presentation!, depth: d.presentation!.depth + (plane.depth ?? 0) } })) + layers.push({ depth: plane.depth ?? 0, selections, image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) }) + } + const result = new Uint8Array(2 + layers.reduce((n, l) => n + 16 + l.image.pixels.length + l.selections.reduce((n, b) => n + b.length, 0), 0)) + const view = new DataView(result.buffer); let p = 0 + const word = (n: number) => { view.setUint16(p, n, true); p += 2 } + word(layers.length) + for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(l.selections.length); word(l.depth); result.set(l.image.pixels, p); p += l.image.pixels.length; for (const selection of l.selections) { result.set(selection, p); p += selection.length } } + return result +} diff --git a/app/graphics/surface-compositor.ios.ts b/app/graphics/surface-compositor.ios.ts index 10f869d2..be080bfc 100644 --- a/app/graphics/surface-compositor.ios.ts +++ b/app/graphics/surface-compositor.ios.ts @@ -1,5 +1,4 @@ import { fromData, toData } from '../native/kotlin-data' -import type { TextureFrame } from '../g2/texture-planner' import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' declare const FaceclawKitIosSurfaceCompositor: any @@ -19,6 +18,7 @@ export class SurfaceCompositor { } removeSurface(id: string): void { this.native.removeId(id); this.surfaces.delete(id) } setSurfaceVisible(id: string, visible: boolean): void { this.native.visibleIdVisible(id, visible) } + setSurfaceDepth(id: string, depth: number): void { this.native.depthIdDepth(id, depth) } setUnderlayDim(belowZOrder: number, factor: number): void { if (!Number.isFinite(factor)) throw new Error('Invalid dim factor') this.native.dimBelowFactor(belowZOrder, factor) @@ -33,9 +33,6 @@ export class SurfaceCompositor { this.native.submitDrawsIdDataXYWidthHeightDraws(id, toData(pixels), rect.x, rect.y, rect.width, rect.height, draws ? toData(new Uint8Array(draws)) : null) } + setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) } composite(): Uint8Array { return fromData(this.native.composite()) } - compositeFrame(): { pixels: Uint8Array; textures: TextureFrame } { - const native = this.native.compositeFrame() - return { pixels: fromData(native.pixels), textures: { native } } - } } diff --git a/app/graphics/surface-compositor.ts b/app/graphics/surface-compositor.ts index d7ea2ca6..30a86dfa 100644 --- a/app/graphics/surface-compositor.ts +++ b/app/graphics/surface-compositor.ts @@ -1,14 +1,17 @@ +import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire" /** Platform-independent 8bpp surface composition for the local display. */ -import type { TextureFrame } from '../g2/texture-planner' export type SurfaceRect = { x: number; y: number; width: number; height: number } export type SurfaceConfiguration = SurfaceRect & { zOrder: number transparency: 'opaque' | 'color-key' } -type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean } +type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean; selections: Selection[] } export class SurfaceCompositor { private readonly surfaces = new Map() + private shellScene: Uint8Array | null = null + private shellReceivedAt = 0 + setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes); this.shellReceivedAt = Date.now() } private dimBelow = 0 private dimFactor = 1 private blanked = false @@ -23,13 +26,15 @@ export class SurfaceCompositor { const previous = this.surfaces.get(id) const pixels = previous?.width === options.width && previous.height === options.height ? previous.pixels : new Uint8Array(options.width * options.height) - this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true }) + this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true, selections: previous?.selections ?? [] }) } removeSurface(id: string): void { this.surfaces.delete(id) } setSurfaceVisible(id: string, visible: boolean): void { const surface = this.surfaces.get(id) if (surface) surface.visible = visible } + /** Stereo depth only shifts the lenses; this local composite shows the unshifted screen. */ + setSurfaceDepth(_id: string, _depth: number): void {} setUnderlayDim(belowZOrder: number, factor: number): void { if (!Number.isFinite(factor)) throw new Error('Invalid dim factor') this.dimBelow = belowZOrder @@ -43,6 +48,7 @@ export class SurfaceCompositor { || rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) { throw new Error('Invalid frame buffer or rectangle') } + surface.selections = presentationRecords(_draws) const left = Math.max(0, rect.x), right = Math.min(surface.width, rect.x + rect.width) const top = Math.max(0, rect.y), bottom = Math.min(surface.height, rect.y + rect.height) for (let y = top; y < bottom; y++) { @@ -51,15 +57,14 @@ export class SurfaceCompositor { surface.pixels.set(pixels.subarray(source, source + right - left), y * surface.width + left) } } - compositeFrame(): { pixels: Uint8Array; textures?: TextureFrame } { return { pixels: this.composite() } } composite(): Uint8Array { const output = new Uint8Array(this.width * this.height) if (this.blanked) return output - const surfaces = [...this.surfaces.values()].filter(s => s.visible).sort((a, b) => a.zOrder - b.zOrder) + const surfaces = [...this.surfaces.values()].filter(s => s.visible && !(this.shellScene && s === this.surfaces.get('shell'))).sort((a, b) => a.zOrder - b.zOrder) for (const surface of surfaces) { const left = Math.max(0, surface.x), right = Math.min(this.width, surface.x + surface.width) const top = Math.max(0, surface.y), bottom = Math.min(this.height, surface.y + surface.height) - const dim = surface.zOrder < this.dimBelow ? this.dimFactor : 1 + const dim = !this.shellScene && surface.zOrder < this.dimBelow ? this.dimFactor : 1 for (let y = top; y < bottom; y++) { let source = (y - surface.y) * surface.width + left - surface.x let target = y * this.width + left @@ -72,6 +77,27 @@ export class SurfaceCompositor { } } } + const screen = output.slice() + if (!surfaces.some(s => s.zOrder > 1) && (this.shellScene || surfaces.some(s => s.selections.length))) { + for(let i=0;i>4)*16 + for(const surface of surfaces) for(const row of surface.selections) paintPresentation(output,screen,this.width,this.height,{...row,x:row.x+surface.x,y:row.y+surface.y,occlusions:row.occlusions.map(r=>({...r,x:r.x+surface.x,y:r.y+surface.y}))}) + } + if (this.shellScene && !surfaces.some(s => s.zOrder > 1)) { + const quantize = (v: number) => Math.min(15, (v + 8) >> 4) + for (let i=0;i=0 && y+yy>=0 && x+xx+shift 0) totals.messages++ + return { ...totals } +} diff --git a/app/native/device-info-probe.ios.ts b/app/native/device-info-probe.ios.ts index ac82157c..913c5e12 100644 --- a/app/native/device-info-probe.ios.ts +++ b/app/native/device-info-probe.ios.ts @@ -1,68 +1,89 @@ -import { StockConnection, StockTimeout } from '../g2/stock-connection' -import * as p from '../g2/ble-protocol' import { iosBluetooth } from './ios-bluetooth' +import { kotlinListener } from './kotlin-listener.ios' import type { DeviceInfo, DeviceInfoState } from './device-info-probe' export type { DeviceInfo, DeviceInfoState } from './device-info-probe' +declare const FaceclawKitIosDeviceInfoProbe: any, FaceclawKitFaceclawDeviceInfoProbeListener: any + +/** + * iOS twin of the Android DeviceInfoProbe wrapper: the shared Kotlin DeviceInfoProbeFlow + * runs over a CoreBluetooth central (FaceclawKitIosDeviceInfoProbe); TypeScript only maps + * the stored MAC addresses to peripheral identifiers first. Callbacks arrive on the main + * queue and are fanned out asynchronously like the Android wrapper does. + */ export class DeviceInfoProbe { - private logs = new Set<(line: string) => void>() - private states = new Set<(state: DeviceInfoState, detail: string) => void>() - private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) }) - constructor(private right: string, private left = '') {} - onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) } - onStateChange(fn: (state: DeviceInfoState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) } - async run(): Promise { - const snapshots = new Map() - const parse = (message: p.ProtocolMessage): DeviceInfo => { - const values = p.readBytes(message.payload, 4) - return { leftVersion: values ? p.readString(values, 5) : '', rightVersion: values ? p.readString(values, 6) : '', extension: p.readString(message.payload, 100) } - } - const hasVersion = (info: DeviceInfo) => !!(info.leftVersion || info.rightVersion || info.extension) - // Some stock builds push settings using their own magic instead of the - // query's magic. Keep a valid snapshot as the Android probe does. - this.link.onMessage((role, message) => { - if (message.sid === p.SID.settings) { const info = parse(message); if (hasVersion(info)) snapshots.set(role, info) } + private probe: any = null + private listenerProxy: object | null = null + private readonly logListeners = new Set<(line: string) => void>() + private readonly stateListeners = new Set<(state: DeviceInfoState, detail: string) => void>() + private settled = false + private cancelled = false + private resolveFn: ((info: DeviceInfo) => void) | null = null + private rejectFn: ((error: Error) => void) | null = null + + constructor(private readonly rightAddress: string, private readonly leftAddress = '') {} + + onLog(listener: (line: string) => void): () => void { + this.logListeners.add(listener) + return () => this.logListeners.delete(listener) + } + + onStateChange(listener: (state: DeviceInfoState, detail: string) => void): () => void { + this.stateListeners.add(listener) + return () => this.stateListeners.delete(listener) + } + + run(): Promise { + return new Promise((resolve, reject) => { + this.resolveFn = resolve + this.rejectFn = reject + void this.start() }) + } + + private async start(): Promise { + let identifiers: Record try { - await this.link.resolve({ right: this.right, left: this.left, ring: '' }) - for (const role of ['right', ...(this.left ? ['left'] : [])]) { - await this.bringUp(role) - } - let lastError: unknown - for (const role of ['right', ...(this.left ? ['left'] : [])]) { - for (const fn of this.states) fn('querying', role) - try { - if (!this.link.isConnected(role)) await this.bringUp(role) - await this.link.request(role, p.SID.launch, p.prelude, 3500, 156) - for (let attempt = 0; attempt < 2; attempt++) { - try { - const message = await this.link.request(role, p.SID.settings, p.settingsQuery) - const info = parse(message) - if (hasVersion(info)) return info - } catch (error) { this.link.check(); lastError = error } - if (snapshots.has(role)) return snapshots.get(role)! - } - } catch (error) { this.link.check(); lastError = error } - } - throw lastError ?? new Error("Connected, but couldn't read a firmware version.") - } finally { this.close() } - } - private async bringUp(role: string): Promise { - for (let attempt = 0; ; attempt++) { - try { - for (const fn of this.states) fn('connecting', role) - await this.link.connect(role) - for (const fn of this.states) fn('authenticating', role) - // Version reads remain available on older stock builds without an auth ACK. - try { await this.link.authenticate(role) } catch (error) { if (!(error instanceof StockTimeout)) throw error } - return - } catch (error) { - this.link.check(); this.link.disconnect(role) - if (attempt === 2) throw error - await new Promise(resolve => setTimeout(resolve, 1500)); this.link.check() - } + this.emit(this.stateListeners, 'connecting', 'right') + identifiers = await iosBluetooth().resolveDevices({ right: this.rightAddress, left: this.leftAddress, ring: '' }) + } catch (error) { + this.settle(error instanceof Error ? error : new Error(String(error)), null) + return } + if (this.cancelled) { this.settle(new Error('Cancelled.'), null); return } + this.probe = FaceclawKitIosDeviceInfoProbe.alloc().initWithRightAddressLeftAddress(identifiers.right ?? '', identifiers.left ?? '') + this.listenerProxy = kotlinListener(FaceclawKitFaceclawDeviceInfoProbeListener, { + onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')), + onStateStateDetail: (state: string, detail: string) => + this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? '')), + onResultLeftVersionRightVersionExtension: (leftVersion: string, rightVersion: string, extension: string) => + this.settle(null, { leftVersion: String(leftVersion ?? ''), rightVersion: String(rightVersion ?? ''), extension: String(extension ?? '') }), + onErrorMessage: (message: string) => this.settle(new Error(String(message ?? '')), null), + }) + this.probe.setListenerListener(this.listenerProxy) + this.probe.start() + } + + cancel(): void { + this.cancelled = true + try { this.probe?.cancel() } catch { /* ignore */ } + } + + close(): void { + this.cancelled = true + try { this.probe?.close() } catch { /* ignore */ } + } + + private settle(error: Error | null, info: DeviceInfo | null): void { + if (this.settled) return + this.settled = true + this.close() + if (error) this.rejectFn?.(error) + else if (info) this.resolveFn?.(info) + } + + private emit(listeners: Set<(...args: A) => void>, ...args: A): void { + const snapshot = Array.from(listeners) + setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0) } - cancel(): void { this.link.close() } - close(): void { this.link.close() } } diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts new file mode 100644 index 00000000..47fe1231 --- /dev/null +++ b/app/native/faceclaw-communicator.ios.ts @@ -0,0 +1,685 @@ +import type { RingInput } from "../g2/ring-input"; +import { File, knownFolders, path, type ImageSource } from "@nativescript/core"; +import { type FirmwareInfo } from "../g2/firmware-compat"; +import { type CompassEvent } from "./compass-types"; +import { fromData, toData } from "./kotlin-data"; +import { previewPixels } from "./ios-graphics"; +import { iosBluetooth } from "./ios-bluetooth"; + +export type { FirmwareInfo }; + +// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble). +declare const FaceclawKitIosGlassesSession: any; +declare const FaceclawKitIosProtocolPlatform: any; +declare const FaceclawKitFaceclawBleCommunicatorListener: any; +declare const FaceclawKitIosAncsListener: any; +declare const FaceclawKitIosAudioPacketListener: any; +declare const FaceclawKitFaceclawCompassListener: any; + + +export type CommunicatorPhase = + | "disconnected" + | "connecting" + | "connected" + | "charging" + | "retrying" + | "unpaired" + | "disconnecting"; + +export type CommunicatorState = { + phase: CommunicatorPhase; + status: string; +}; + +export type HeadsetBatteryState = { + battery: number; + chargingStatus: number; + ringBattery?: number; + ringChargingStatus?: number; +}; + +export type FrameMetrics = { + paintMs: number; + transmitMs: number; + tileCount: number; +}; + +/** See the Android twin: compositor surface configuration in screen pixels. */ +export type SurfaceOptions = { + x: number; + y: number; + width: number; + height: number; + zOrder: number; + transparency: "opaque" | "color-key"; +}; + +export type RawInputEvent = { ringInput?: RingInput } & ( + | { kind: "list-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "text-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "sys-event"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "even-ai"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "watch-gesture"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "display-wake"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } +); + +function nonNegativeNumber(value: number): number { + const numeric = Number(value); + return Number.isFinite(numeric) ? Math.max(0, numeric) : 0; +} + +/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */ +export function dimFactor256(factor: number): number { + return Math.round(Math.max(0, Math.min(1, factor)) * 256); +} + +function timestamp(): string { + const now = new Date(); + const pad = (value: number, width = 2) => String(value).padStart(width, "0"); + return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`; +} + +export type PeripheralIdentifiers = { right: string; left: string; ring: string }; + +/** + * Map the configured MAC addresses to CoreBluetooth identifiers through the scan + * layer (app/native/ios-bluetooth.ts): the mapping a previous scan persisted, else a + * fresh scan that classifies Even advertisements. The scan layer also owns Bluetooth + * readiness (creating the central triggers the permission prompt on first use). + */ +export async function resolveIosPeripherals(addresses: { right: string; left: string; ring?: string }): Promise { + const resolved = await iosBluetooth().resolveDevices({ right: addresses.right, left: addresses.left, ring: addresses.ring ?? "" }); + return { + right: String(resolved.right ?? "").toUpperCase(), + left: String(resolved.left ?? "").toUpperCase(), + ring: String(resolved.ring ?? "").toUpperCase(), + }; +} + +/** Bluetooth powered on and authorized (prompts for permission the first time); rejects with the user-facing reason. */ +export function ensureIosBluetoothReady(): Promise { + return iosBluetooth().ensureReady(); +} + +@NativeClass() +class SessionListener extends NSObject { + static ObjCProtocols = [FaceclawKitFaceclawBleCommunicatorListener]; + bridge!: FaceclawCommunicatorBridge; + onStateChangePhaseStatus(phase: string, status: string): void { this.bridge.handleStateChange(String(phase ?? ""), String(status ?? "")); } + onRingEventKindContainerNameEventTypeEventSourceSystemExitReasonCodeFrameIdRingTickRingTypeRingAuxRingSpeed( + kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number, + frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number, + ): void { + this.bridge.handleRingEvent(String(kind ?? ""), String(containerName ?? ""), Number(eventType), Number(eventSource), + Number(systemExitReasonCode), Number(frameId), Number(ringTick), Number(ringType), Number(ringAux), Number(ringSpeed)); + } + onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void { + this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging)); + } + onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); } + onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); } + onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); } + onEvenAppConflictMessage(message: string): void { this.bridge.handleEvenAppConflict(String(message ?? "")); } + onFrameMetricsPaintMsTransmitMsTileCount(paintMs: number, transmitMs: number, tileCount: number): void { + this.bridge.handleFrameMetrics(Number(paintMs), Number(transmitMs), Number(tileCount)); + } + onFrameFinishedFrameIdOutcome(frameId: number, outcome: string): void { this.bridge.handleFrameFinished(Number(frameId), String(outcome ?? "")); } + onFirmwareInfoLeftVersionRightVersionExtension(leftVersion: string, rightVersion: string, extension: string): void { + this.bridge.handleFirmwareInfo(String(leftVersion ?? ""), String(rightVersion ?? ""), String(extension ?? "")); + } +} + +@NativeClass() +class AncsListener extends NSObject { + static ObjCProtocols = [FaceclawKitIosAncsListener]; + bridge!: FaceclawCommunicatorBridge; + onAncsAuthorizationAuthorized(authorized: boolean): void { this.bridge.handleAncsAuthorization(Boolean(authorized)); } + onRelayFrameData(data: NSData): void { this.bridge.handleAncsRelayFrame(fromData(data)); } +} + +@NativeClass() +class AudioPacketListener extends NSObject { + static ObjCProtocols = [FaceclawKitIosAudioPacketListener]; + handler!: (packet: Uint8Array, arm: string, arrivalMs: number) => void; + onAudioPacketDataArmArrivalMs(data: NSData, arm: string, arrivalMs: number): void { + this.handler(fromData(data), String(arm ?? ""), Number(arrivalMs)); + } +} + +@NativeClass() +class CompassListener extends NSObject { + static ObjCProtocols = [FaceclawKitFaceclawCompassListener]; + handler!: (event: CompassEvent) => void; + onCompassEventCommandHeadingDegreesMagneticAccuracyMagneticAnomaliesOrientationSourceDiagnosticFlagsSampleTimeMs( + command: number, headingDegrees: number, magneticAccuracy: number, magneticAnomalies: number, + orientationSource: number, diagnosticFlags: number, sampleTimeMs: number, + ): void { + this.handler({ + command: Number(command), headingDegrees: Number(headingDegrees), + diagnostics: diagnosticFlags >= 0 ? { + magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies), + orientationSource: Number(orientationSource), flags: Number(diagnosticFlags), sampleTimeMs: Number(sampleTimeMs), + } : undefined, + }); + } +} + +/** + * The iOS twin of the Android communicator bridge: the same public API over + * the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth + * underneath). Addresses are CoreBluetooth identifiers (resolveIosPeripherals + * maps the configured MACs). Every Kotlin callback already arrives on the + * main queue; listener fan-out is still deferred with setTimeout like on + * Android so callers never re-enter the bridge from a Kotlin frame. + */ +export class FaceclawCommunicatorBridge { + private readonly communicator: any; + private readonly listenerProxy: SessionListener; + private ancsProxy: AncsListener | null = null; + private nativeCallQueue: Promise = Promise.resolve(); + private queuedNativeCalls = 0; + private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); + private readonly finishedFrameOutcomes = new Map(); + private readonly frameFinishedWaiters = new Map void>>(); + private readonly stateListeners = new Set<(state: CommunicatorState) => void>(); + private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); + private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); + private readonly silentModeListeners = new Set<(silent: boolean) => void>(); + private readonly wearStateListeners = new Set<(wearing: boolean) => void>(); + private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>(); + private latestWearState: boolean | null = null; + private latestPhoneLockState: boolean | null = null; + private readonly evenAppConflictListeners = new Set<(message: string) => void>(); + private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>(); + private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>(); + private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>(); + private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>(); + private audioListener: AudioPacketListener | null = null; + private microphoneWork: Promise = Promise.resolve(); + private microphoneToken = 0; + + constructor(addresses: { right: string; left: string; ring?: string }) { + this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress( + String(addresses.right ?? "").toUpperCase(), + String(addresses.left ?? "").toUpperCase(), + String(addresses.ring ?? "").toUpperCase(), + ); + this.listenerProxy = SessionListener.new() as SessionListener; + this.listenerProxy.bridge = this; + this.communicator.setListenerListener(this.listenerProxy); + } + + // ----- Kotlin listener entry points (main queue) ----------------------------------- + + /** @internal */ + handleStateChange(phase: string, status: string): void { + this.emitAsync(this.stateListeners, { phase: phase as CommunicatorPhase, status }); + } + + /** @internal */ + handleRingEvent(kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number, + frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number): void { + const event = { + kind: kind as RawInputEvent["kind"], containerName, eventType, eventSource, systemExitReasonCode, frameId, + ringInput: ringTick >= 0 ? { tick: ringTick, type: ringType, aux: ringAux, speed: ringSpeed } : undefined, + } as RawInputEvent; + this.emitAsync(this.ringListeners, event); + } + + /** @internal */ + handleBatteryState(battery: number, chargingStatus: number, ringBattery: number, ringChargingStatus: number): void { + this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus }); + } + + /** @internal */ + handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); } + + /** @internal */ + handleWearState(wearing: boolean): void { + this.latestWearState = wearing; + this.emitAsync(this.wearStateListeners, wearing); + } + + /** @internal */ + handlePhoneLockState(locked: boolean): void { + this.latestPhoneLockState = locked; + this.emitAsync(this.phoneLockStateListeners, locked); + } + + /** @internal */ + handleEvenAppConflict(message: string): void { this.emitAsync(this.evenAppConflictListeners, message); } + + /** @internal */ + handleFrameMetrics(paintMs: number, transmitMs: number, tileCount: number): void { + const metrics = { paintMs: nonNegativeNumber(paintMs), transmitMs: nonNegativeNumber(transmitMs), tileCount: nonNegativeNumber(tileCount) }; + const waiters = Array.from(this.frameMetricWaiters); + this.frameMetricWaiters.clear(); + for (const waiter of waiters) setTimeout(() => waiter(metrics), 0); + this.emitAsync(this.frameMetricsListeners, metrics); + } + + /** @internal */ + handleFrameFinished(frameId: number, outcome: string): void { this.recordFrameFinished(frameId, outcome); } + + /** @internal */ + handleFirmwareInfo(leftVersion: string, rightVersion: string, extension: string): void { + this.emitAsync(this.firmwareInfoListeners, { leftVersion, rightVersion, extension }); + } + + /** @internal */ + handleAncsAuthorization(authorized: boolean): void { this.emitAsync(this.ancsAuthorizationListeners, authorized); } + + /** @internal */ + handleAncsRelayFrame(frame: Uint8Array): void { this.emitAsync(this.ancsRelayListeners, frame); } + + private emitAsync(listeners: Set<(value: T) => void>, value: T): void { + const snapshot = Array.from(listeners); + setTimeout(() => { + for (const listener of snapshot) listener(value); + }, 0); + } + + /** Same FIFO as the Android bridge: native calls stay ordered; the fast path skips a task hop when idle. */ + private enqueueNativeCall(operation: () => T, inlineWhenIdle = false): Promise { + if (inlineWhenIdle && this.queuedNativeCalls === 0) { + try { + return Promise.resolve(operation()); + } catch (error) { + return Promise.reject(error); + } + } + this.queuedNativeCalls++; + const run = () => + new Promise((resolve, reject) => { + setTimeout(() => { + this.queuedNativeCalls--; + try { + resolve(operation()); + } catch (error) { + reject(error); + } + }, 0); + }); + const result = this.nativeCallQueue.then(run, run); + this.nativeCallQueue = result.then(() => undefined, () => undefined); + return result; + } + + // ----- listeners ------------------------------------------------------------------- + + onStateChange(listener: (state: CommunicatorState) => void): () => void { + this.stateListeners.add(listener); + return () => this.stateListeners.delete(listener); + } + + onRingEvent(listener: (event: RawInputEvent) => void): () => void { + this.ringListeners.add(listener); + return () => this.ringListeners.delete(listener); + } + + onBatteryState(listener: (state: HeadsetBatteryState) => void): () => void { + this.batteryListeners.add(listener); + return () => this.batteryListeners.delete(listener); + } + + onSilentMode(listener: (silent: boolean) => void): () => void { + this.silentModeListeners.add(listener); + return () => this.silentModeListeners.delete(listener); + } + + onWearState(listener: (wearing: boolean) => void): () => void { + this.wearStateListeners.add(listener); + if (this.latestWearState !== null) { + const wearing = this.latestWearState; + setTimeout(() => { if (this.wearStateListeners.has(listener)) listener(wearing); }, 0); + } + return () => this.wearStateListeners.delete(listener); + } + + onPhoneLockState(listener: (locked: boolean) => void): () => void { + this.phoneLockStateListeners.add(listener); + if (this.latestPhoneLockState !== null) { + const locked = this.latestPhoneLockState; + setTimeout(() => { if (this.phoneLockStateListeners.has(listener)) listener(locked); }, 0); + } + return () => this.phoneLockStateListeners.delete(listener); + } + + onEvenAppConflict(listener: (message: string) => void): () => void { + this.evenAppConflictListeners.add(listener); + return () => this.evenAppConflictListeners.delete(listener); + } + + onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void { + this.frameMetricsListeners.add(listener); + return () => this.frameMetricsListeners.delete(listener); + } + + onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void { + this.firmwareInfoListeners.add(listener); + return () => this.firmwareInfoListeners.delete(listener); + } + + getNativeCommunicator(): any { + return this.communicator; + } + + private recordFrameFinished(frameId: number, outcome: string): void { + if (!Number.isFinite(frameId) || frameId <= 0) return; + this.finishedFrameOutcomes.set(frameId, outcome); + while (this.finishedFrameOutcomes.size > 128) { + const oldest = this.finishedFrameOutcomes.keys().next().value; + if (oldest === undefined) break; + this.finishedFrameOutcomes.delete(oldest); + } + const waiters = this.frameFinishedWaiters.get(frameId); + if (waiters) { + this.frameFinishedWaiters.delete(frameId); + for (const waiter of waiters) setTimeout(() => waiter(outcome), 0); + } + } + + waitForFrameFinished(frameId: number, timeoutMs: number): Promise { + const known = this.finishedFrameOutcomes.get(frameId); + if (known !== undefined) return Promise.resolve(known); + if (frameId <= 0) return Promise.resolve(null); + const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs))); + return new Promise((resolve) => { + let settled = false; + const onFinished = (outcome: string) => { + if (settled) return; + settled = true; + clearTimeout(timeoutHandle); + resolve(outcome); + }; + let waiters = this.frameFinishedWaiters.get(frameId); + if (!waiters) { + waiters = new Set(); + this.frameFinishedWaiters.set(frameId, waiters); + } + waiters.add(onFinished); + const timeoutHandle = setTimeout(() => { + if (settled) return; + settled = true; + const pending = this.frameFinishedWaiters.get(frameId); + if (pending) { + pending.delete(onFinished); + if (pending.size === 0) this.frameFinishedWaiters.delete(frameId); + } + resolve(null); + }, delayMs); + }); + } + + waitForNextFrameMetrics(timeoutMs: number): Promise { + const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs))); + return new Promise((resolve) => { + let settled = false; + let timeoutHandle: ReturnType | null = null; + const complete = (metrics: FrameMetrics | null) => { + if (settled) return; + settled = true; + if (timeoutHandle !== null) clearTimeout(timeoutHandle); + this.frameMetricWaiters.delete(onMetrics); + resolve(metrics); + }; + const onMetrics = (metrics: FrameMetrics) => complete(metrics); + this.frameMetricWaiters.add(onMetrics); + timeoutHandle = setTimeout(() => complete(null), delayMs); + }); + } + + // ----- session --------------------------------------------------------------------- + + async start(): Promise { + await this.enqueueNativeCall(() => { this.communicator.start(); }); + } + + async setG2ScreenOn(screenOn: boolean): Promise { + await this.enqueueNativeCall(() => this.communicator.setG2ScreenOnScreenOn(Boolean(screenOn))); + } + + async setFirmwareDebugFlags(enabled: boolean): Promise { + await this.enqueueNativeCall(() => this.communicator.setFirmwareDebugFlagsEnabled(Boolean(enabled))); + } + + /** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */ + async setBrightness(autoAdjust: boolean, level: number): Promise { + await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level))); + } + + async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise { + await this.enqueueNativeCall(() => this.communicator.configureBrightnessAutoLevelMinimumMaximumCurveFadeMs(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs)); + } + + async enableWearDetectionAndRequestState(): Promise { + await this.enqueueNativeCall(() => this.communicator.enableWearDetectionAndRequestState()); + } + + isSessionReady(): boolean { + return Boolean(this.communicator.isSessionReady()); + } + + /** The platform's lock/screen signal (the iOS host also observes protected data itself). */ + onPhoneLockSignal(): void { + this.communicator.onPhoneLockSignal(); + } + + // ----- compositor ------------------------------------------------------------------ + + async configureCompositorScreen(width: number, height: number): Promise { + await this.enqueueNativeCall(() => { + this.communicator.configureCompositorScreenWidthHeight(Math.round(width), Math.round(height)); + }); + } + + /** Phone-UI preview of the current composited screen, or null if none yet. */ + getCompositePreview(green = false): ImageSource | null { + const frame = this.communicator.previewComposite(); + if (!frame) return null; + const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight()); + if (width <= 0 || height <= 0) return null; + return previewPixels(fromData(frame.pixels), width, height, green); + } + + /** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */ + saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string { + if (crop) return ""; + const png = this.communicator.screenshotPng(); + if (!png) return ""; + const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`); + File.fromPath(target).writeSync(png); + return target; + } + + /** Screen recording is not available on iOS yet. */ + startScreenRecording(): void {} + + recordScreenFrame(): void {} + + stopScreenRecording(): string { return ""; } + + async configureSurface(id: string, options: SurfaceOptions): Promise { + const transparency = options.transparency === "color-key" ? 1 : 0; + await this.enqueueNativeCall(() => { + this.communicator.configureSurfaceIdXYWidthHeightZOrderTransparency( + id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height), + Math.round(options.zOrder), transparency, + ); + }); + } + + async removeSurface(id: string): Promise { + await this.enqueueNativeCall(() => { this.communicator.removeSurfaceId(id); }); + } + + async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { + const snapshot = new Uint8Array(bytes); + await this.enqueueNativeCall(() => this.communicator.submitShellSceneBytesPaintMsFrameId(toData(snapshot), Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId))), true); + } + + async setUnderlayDim(belowZOrder: number, factor: number): Promise { + await this.enqueueNativeCall(() => { + this.communicator.setUnderlayDimBelowZOrderFactor256(Math.round(belowZOrder), dimFactor256(factor)); + }); + } + + async setSurfaceVisible(id: string, visible: boolean): Promise { + await this.enqueueNativeCall(() => { this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible)); }); + } + + async setSurfaceDepth(id: string, depth: number): Promise { + await this.enqueueNativeCall(() => { this.communicator.setSurfaceDepthIdDepth(id, Math.round(depth)); }); + } + + async setScreenBlanked(blanked: boolean): Promise { + await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); }); + } + + async submitSurfaceFrame( + surfaceId: string, + pixels8bpp: Uint8Array, + rect: { x: number; y: number; width: number; height: number }, + fingerprint: string, + paintMs = -1, + frameId = 0, + glyphs: ArrayBuffer | null = null, + ): Promise { + const snapshot = new Uint8Array(pixels8bpp); + const draws = glyphs ? toData(new Uint8Array(glyphs)) : null; + await this.enqueueNativeCall(() => { + this.communicator.submitSurfaceFramePixels8bppSurfaceIdRectXRectYRectWidthRectHeightContentFingerprintPaintMsFrameIdGlyphs( + toData(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), Math.round(rect.width), Math.round(rect.height), + fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), draws, + ); + }, true); + } + + // ----- controls -------------------------------------------------------------------- + + async disconnect(): Promise { + await this.enqueueNativeCall(() => this.communicator.disconnect()); + } + + async sendShutdown(exitMode = 0): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.sendShutdownExitMode(exitMode))); + } + + async sendCfwCleanup(): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.sendCfwCleanup())); + } + + async suspendEvenHubSession(): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.suspendEvenHubSession())); + } + + async resumeEvenHubSession(): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.resumeEvenHubSession())); + } + + async setFaceclawWakeLeaseEnabled(enabled: boolean): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled)))); + } + + async awaitEvenHubSessionReady(timeoutMs: number): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs))))); + } + + /** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */ + async playBuzzerSequence(payload: Uint8Array): Promise { + const snapshot = new Uint8Array(payload); + await this.enqueueNativeCall(() => { this.communicator.playBuzzerSequencePayload(toData(snapshot)); }); + } + + async close(): Promise { + this.setAncsListeners(false); + await this.enqueueNativeCall(() => this.communicator.close()); + } + + // ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ---------------------- + + /** CFW mode-10 compass on behalf of `owner`; the magnetometer runs while any owner wants it. */ + setCompassEnabled(enabled: boolean, owner = "compass"): void { + this.communicator.setCompassEnabledForOwnerOwnerEnable(owner, Boolean(enabled)); + } + + /** Compass heading/calibration events, delivered on the main queue. */ + addCompassListener(listener: (event: CompassEvent) => void): () => void { + const proxy = CompassListener.new() as CompassListener; + proxy.handler = listener; + this.communicator.addCompassListenerListener(proxy); + return () => { this.communicator.removeCompassListenerListener(proxy); }; + } + + /** + * Serialized glasses microphone enable/disable (the voice bridge's + * IosMicrophoneSession contract). A release while an enable awaits its ACK + * still wins: the token check drops the stale packets. + */ + setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise { + const token = ++this.microphoneToken; + const work = this.microphoneWork.catch(() => {}).then(async () => { + if (token !== this.microphoneToken) return; + if (!enabled) { + this.audioListener = null; + await this.enqueueNativeCall(() => this.communicator.stopG2AudioCapture()); + return; + } + if (!this.isSessionReady()) throw new Error("Connect the glasses before starting voice input."); + const proxy = AudioPacketListener.new() as AudioPacketListener; + proxy.handler = (packet) => { if (token === this.microphoneToken && listener) listener(packet); }; + this.audioListener = proxy; + const started = await this.enqueueNativeCall(() => Boolean(this.communicator.startG2AudioCaptureNsDataListener(proxy))); + if (!started) { + this.audioListener = null; + throw new Error("The glasses did not enable their microphone. Reconnect and try again."); + } + }); + this.microphoneWork = work; + return work; + } + + /** Ask CoreBluetooth for ANCS on the right arm's next connection (call before start()). */ + setRequiresAncs(required: boolean): void { + this.communicator.setRequiresAncsRequired(Boolean(required)); + this.setAncsListeners(required); + } + + isAncsAuthorized(): boolean { + return Boolean(this.communicator.isAncsAuthorized()); + } + + onAncsAuthorization(listener: (authorized: boolean) => void): () => void { + this.ancsAuthorizationListeners.add(listener); + return () => this.ancsAuthorizationListeners.delete(listener); + } + + /** Relay frames ('A','N',...) received on the right arm, for the ANCS client. */ + onAncsRelayFrame(listener: (frame: Uint8Array) => void): () => void { + this.ancsRelayListeners.add(listener); + return () => this.ancsRelayListeners.delete(listener); + } + + /** Queue one relay packet for the right arm; the Kotlin side writes it off the JS thread. */ + async writeRawToRight(packet: Uint8Array): Promise { + if (!this.isSessionReady()) throw new Error("Glasses are not connected."); + const snapshot = new Uint8Array(packet); + this.communicator.writeRawToRightPacket(toData(snapshot)); + } + + /** The right arm's usable write payload for relay packets (20 until connected). */ + rightWriteLimit(): number { + return Number(this.communicator.rightWriteLimit()) || 20; + } + + private setAncsListeners(install: boolean): void { + if (install && !this.ancsProxy) { + this.ancsProxy = AncsListener.new() as AncsListener; + this.ancsProxy.bridge = this; + this.communicator.setAncsListenerListener(this.ancsProxy); + } else if (!install && this.ancsProxy) { + this.communicator.setAncsListenerListener(null); + this.ancsProxy = null; + } + } +} diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 83395bd4..2663195e 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -137,7 +137,6 @@ export class FaceclawCommunicatorBridge { // does not race against finishes that land before the wait starts. private readonly finishedFrameOutcomes = new Map(); private readonly frameFinishedWaiters = new Map void>>(); - private readonly logListeners = new Set<(line: string) => void>(); private readonly stateListeners = new Set<(state: CommunicatorState) => void>(); private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); @@ -161,9 +160,6 @@ export class FaceclawCommunicatorBridge { addresses.ring ?? "", ); this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({ - onLog: (line: string) => { - this.emitAsync(this.logListeners, String(line)); - }, onStateChange: (phase: string, status: string) => { const state = { phase: String(phase) as CommunicatorPhase, @@ -293,11 +289,6 @@ export class FaceclawCommunicatorBridge { return result; } - onLog(listener: (line: string) => void): () => void { - this.logListeners.add(listener); - return () => this.logListeners.delete(listener); - } - onStateChange(listener: (state: CommunicatorState) => void): () => void { this.stateListeners.add(listener); return () => this.stateListeners.delete(listener); @@ -451,11 +442,15 @@ export class FaceclawCommunicatorBridge { await this.enqueueJavaCall(() => this.communicator.setFirmwareDebugFlags(Boolean(enabled))); } - /** Set lens brightness: auto (ambient sensor) or an explicit 0-100 level. */ + /** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */ async setBrightness(autoAdjust: boolean, level: number): Promise { await this.enqueueJavaCall(() => this.communicator.setBrightness(Boolean(autoAdjust), Math.round(level))); } + async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise { + await this.enqueueJavaCall(() => this.communicator.configureBrightness(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs)); + } + async enableWearDetectionAndRequestState(): Promise { await this.enqueueJavaCall(() => this.communicator.enableWearDetectionAndRequestState()); } @@ -541,6 +536,11 @@ export class FaceclawCommunicatorBridge { * next composite. How a shell overlay's Layer.dimUnderneath reaches the * window surfaces beneath the shell surface. */ + async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { + const snapshot = new Uint8Array(bytes); + await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true); + } + async setUnderlayDim(belowZOrder: number, factor: number): Promise { await this.enqueueJavaCall(() => { this.communicator.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor)); @@ -554,6 +554,16 @@ export class FaceclawCommunicatorBridge { }); } + /** + * Stereo depth for a surface, applied while it is the topmost visible + * surface and opaquely covers the screen; takes effect at its next frame. + */ + async setSurfaceDepth(id: string, depth: number): Promise { + await this.enqueueJavaCall(() => { + this.communicator.setSurfaceDepth(id, Math.round(depth)); + }); + } + /** * Blank (screen off) or unblank the composited output; retained surface * state survives, so unblanking restores the screen without repaints. diff --git a/app/native/firmware-flasher.ios.ts b/app/native/firmware-flasher.ios.ts index bcbf8613..c52e3b80 100644 --- a/app/native/firmware-flasher.ios.ts +++ b/app/native/firmware-flasher.ios.ts @@ -1,83 +1,124 @@ import { Application } from '@nativescript/core' -import { StockConnection } from '../g2/stock-connection' -import { flashLensImage, validateFirmware } from '../g2/firmware-ota' import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches' import { firmwareSha256, readFirmwareFile } from './firmware-files.ios' import { iosBluetooth } from './ios-bluetooth' -import type { FlashState, FlashProgress } from './firmware-flasher' -export type { FlashState, FlashProgress } from './firmware-flasher' +import { kotlinListener } from './kotlin-listener.ios' +import type { FlashState, FlashProgress } from '../g2/firmware-types' +export type { FlashState, FlashProgress } from '../g2/firmware-types' +declare const FaceclawKitIosFirmwareFlasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any + +/** + * iOS twin of the Android FirmwareFlasher wrapper. The OTA transfer itself is the shared + * Kotlin OtaFlashFlow over a CoreBluetooth central (FaceclawKitIosFirmwareFlasher); this + * wrapper keeps the iOS-only policy around it: the image must still hash to the prepared + * custom (or pinned stock) firmware, the phone must stay in the foreground with the screen + * awake, and MAC addresses are mapped to peripheral identifiers first. + */ export class FirmwareFlasher { - private logs = new Set<(line: string) => void>() - private states = new Set<(state: FlashState, detail: string) => void>() - private progresses = new Set<(value: FlashProgress) => void>() - private completes = new Set<(success: boolean, detail: string) => void>() - private link = new StockConnection(iosBluetooth(), line => this.log(line)) + private flasher: any = null + private listenerProxy: object | null = null + private readonly logListeners = new Set<(line: string) => void>() + private readonly progressListeners = new Set<(progress: FlashProgress) => void>() + private readonly stateListeners = new Set<(state: FlashState, detail: string) => void>() + private readonly completeListeners = new Set<(success: boolean, detail: string) => void>() private started = false + private finished = false private cancelled = false - private sleeps = new Set<() => void>() private foregroundError = '' - constructor(private addresses: { right: string; left: string }, private path: string) {} - onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) } - onStateChange(fn: (state: FlashState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) } - onProgress(fn: (value: FlashProgress) => void): () => void { this.progresses.add(fn); return () => this.progresses.delete(fn) } - onComplete(fn: (success: boolean, detail: string) => void): () => void { this.completes.add(fn); return () => this.completes.delete(fn) } - private log(line: string): void { for (const fn of this.logs) fn(line) } - private state(state: FlashState, detail = ''): void { for (const fn of this.states) fn(state, detail) } - start(): void { if (!this.started) { this.started = true; void this.run() } } + private previousIdle = false + + constructor(private readonly addresses: { right: string; left: string }, private readonly path: string) {} + + onLog(listener: (line: string) => void): () => void { + this.logListeners.add(listener) + return () => this.logListeners.delete(listener) + } + + onProgress(listener: (progress: FlashProgress) => void): () => void { + this.progressListeners.add(listener) + return () => this.progressListeners.delete(listener) + } + + onStateChange(listener: (state: FlashState, detail: string) => void): () => void { + this.stateListeners.add(listener) + return () => this.stateListeners.delete(listener) + } + + onComplete(listener: (success: boolean, detail: string) => void): () => void { + this.completeListeners.add(listener) + return () => this.completeListeners.delete(listener) + } + + start(): void { + if (this.started) return + this.started = true + void this.run() + } + private readonly backgrounded = () => { this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.' this.cancel() } + private async run(): Promise { - const previousIdle = UIApplication.sharedApplication.idleTimerDisabled + this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled UIApplication.sharedApplication.idleTimerDisabled = true Application.on(Application.suspendEvent, this.backgrounded) - let success = false, detail = '' try { - this.link.check() if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.') - this.state('validating') + this.emit(this.stateListeners, 'validating', '') const image = readFirmwareFile(this.path) const hash = firmwareSha256(image.buffer) if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.') - const segments = validateFirmware(image) - await this.link.resolve({ ...this.addresses, ring: '' }) - for (const role of ['left', 'right']) { - if (role === 'right') { - this.state('rebooting', 'Left lens done. Both lenses reboot briefly; reconnecting for the right lens.') - await this.delay(5000) - } - this.state('connecting', role) - const deadline = Date.now() + (role === 'left' ? 30_000 : 120_000) - for (;;) { - try { await this.link.connect(role, true); await this.link.authenticate(role); break } - catch (error) { this.link.check(); this.link.disconnect(role); if (Date.now() >= deadline) throw error; await this.delay(2500) } - } - await this.delay(2500) - this.state('flashing', role) - await flashLensImage(this.link, role, image, segments, value => { for (const fn of this.progresses) fn(value) }, line => this.log(line), ms => this.delay(ms)) - this.link.disconnect(role) - } - success = true; detail = 'Both lenses flashed. Your glasses are rebooting.' + const identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' }) + if (this.cancelled) throw new Error('Cancelled.') + this.flasher = FaceclawKitIosFirmwareFlasher.alloc().initWithRightAddressLeftAddressFirmwarePath(identifiers.right ?? '', identifiers.left ?? '', this.path) + this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, { + onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')), + onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: ( + lens: string, componentIndex: number, componentCount: number, blockIndex: number, blockCount: number, bytesSent: number, bytesTotal: number, + ) => this.emit(this.progressListeners, { + lens: String(lens ?? ''), componentIndex: Number(componentIndex), componentCount: Number(componentCount), + blockIndex: Number(blockIndex), blockCount: Number(blockCount), bytesSent: Number(bytesSent), bytesTotal: Number(bytesTotal), + }), + onStateStateDetail: (state: string, detail: string) => { + // The terminal state is reported by finish() so a foreground/cancel reason can replace the detail. + const name = String(state) as FlashState + if (name !== 'done' && name !== 'error') this.emit(this.stateListeners, name, String(detail ?? '')) + }, + onCompleteSuccessDetail: (success: boolean, detail: string) => this.finish(Boolean(success), String(detail ?? '')), + }) + this.flasher.setListenerListener(this.listenerProxy) + this.flasher.start() } catch (error) { - detail = this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : String((error as Error).message ?? error)) - } finally { - Application.off(Application.suspendEvent, this.backgrounded) - UIApplication.sharedApplication.idleTimerDisabled = previousIdle - this.link.close() + this.finish(false, String((error as Error).message ?? error)) } - this.state(success ? 'done' : 'error', detail) - for (const fn of this.completes) fn(success, detail) } - private delay(ms: number): Promise { - this.link.check() - return new Promise((resolve, reject) => { - const cancel = () => { clearTimeout(timer); this.sleeps.delete(cancel); reject(new Error('Cancelled.')) } - const timer = setTimeout(() => { this.sleeps.delete(cancel); resolve() }, ms) - this.sleeps.add(cancel) - }) + + private finish(success: boolean, detail: string): void { + if (this.finished) return + this.finished = true + Application.off(Application.suspendEvent, this.backgrounded) + UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle + try { this.flasher?.close() } catch { /* ignore */ } + const message = success ? detail + : this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : detail) + this.emit(this.stateListeners, success ? 'done' : 'error', message) + this.emit(this.completeListeners, success, message) } - cancel(): void { this.cancelled = true; this.link.close(); for (const cancel of [...this.sleeps]) cancel() } + + cancel(): void { + this.cancelled = true + try { this.flasher?.cancel() } catch { /* ignore */ } + // Before the Kotlin flow exists (validation, address resolution) nothing will report completion. + if (!this.flasher) this.finish(false, 'Cancelled.') + } + close(): void { this.cancel() } + + private emit(listeners: Set<(...args: A) => void>, ...args: A): void { + const snapshot = Array.from(listeners) + setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0) + } } diff --git a/app/native/flash-prompt-communicator.ios.ts b/app/native/flash-prompt-communicator.ios.ts index bea0933f..62e918b3 100644 --- a/app/native/flash-prompt-communicator.ios.ts +++ b/app/native/flash-prompt-communicator.ios.ts @@ -1,88 +1,98 @@ -import { StockConnection } from '../g2/stock-connection' -import { createFlashPrompt, flashPromptSelection } from '../g2/flash-prompt-protocol' -import * as p from '../g2/ble-protocol' import { iosBluetooth } from './ios-bluetooth' +import { kotlinListener } from './kotlin-listener.ios' import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator' export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator' +declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any + +function normalizePercent(value: number): number | null { + const n = Number(value) + return Number.isFinite(n) && n >= 0 ? n : null +} + +/** + * iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin + * FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows + * the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery. + */ export class FlashPromptCommunicator { - private logs = new Set<(line: string) => void>() - private states = new Set<(state: FlashPromptState, detail: string) => void>() - private results = new Set<(approved: boolean) => void>() - private batteries = new Set<(battery: FlashPromptBattery) => void>() - private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) }) + private communicator: any = null + private listenerProxy: object | null = null + private readonly logListeners = new Set<(line: string) => void>() + private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>() + private readonly resultListeners = new Set<(approved: boolean) => void>() + private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>() + private started = false private closed = false - private started = false - private heartbeat: ReturnType | null = null - private stopSelection: (() => void) | null = null - constructor(private addresses: { right: string; left: string }, private warning: string, private options?: { skipPrompt?: boolean }) {} - onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) } - onStateChange(fn: (state: FlashPromptState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) } - onResult(fn: (approved: boolean) => void): () => void { this.results.add(fn); return () => this.results.delete(fn) } - onBattery(fn: (battery: FlashPromptBattery) => void): () => void { this.batteries.add(fn); return () => this.batteries.delete(fn) } - private state(state: FlashPromptState, detail = ''): void { for (const fn of this.states) fn(state, detail) } - start(): void { if (!this.started) { this.started = true; void this.run() } } + + constructor( + private readonly addresses: { right: string; left: string }, + private readonly warningText: string, + private readonly options?: { skipPrompt?: boolean }, + ) {} + + onLog(listener: (line: string) => void): () => void { + this.logListeners.add(listener) + return () => this.logListeners.delete(listener) + } + + onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void { + this.stateListeners.add(listener) + return () => this.stateListeners.delete(listener) + } + + onBattery(listener: (battery: FlashPromptBattery) => void): () => void { + this.batteryListeners.add(listener) + return () => this.batteryListeners.delete(listener) + } + + onResult(listener: (approved: boolean) => void): () => void { + this.resultListeners.add(listener) + return () => this.resultListeners.delete(listener) + } + + start(): void { + if (this.started) return + this.started = true + void this.run() + } + private async run(): Promise { + let identifiers: Record try { - this.state('connecting') - await this.link.resolve({ ...this.addresses, ring: '' }) - // Complete pairing for one peripheral before starting the other. - for (const role of ['right', 'left']) { - await this.link.connect(role) - if (role === 'right') this.state('connected') - await this.link.authenticate(role) - } - await this.link.request('right', p.SID.launch, p.prelude, 3500, 156) - if (!this.options?.skipPrompt) { - // Listen before creating the page: an immediate tap may precede its ACK. - const selection = this.selection() - // Attach a rejection handler immediately, even while create-page is pending. - void selection.catch(() => {}) - for (let attempt = 0; ; attempt++) { - try { await this.link.request('right', p.SID.hub, magic => createFlashPrompt(magic, this.warning)); break } - catch (error) { this.link.check(); if (attempt) throw error } - } - this.state('prompting') - this.heartbeat = setInterval(() => { - void this.link.write('right', p.SID.hub, p.heartbeat(this.link.nextMagic())).catch(error => { - if (!this.closed) this.state('error', String(error.message ?? error)) - this.close() - }) - }, 4000) - const approved = await selection - this.stopHeartbeat() - // Stock shutdown returns the glasses to their normal UI before OTA. - await this.link.write('right', p.SID.hub, p.concat(p.integer(1, 9), p.integer(2, this.link.nextMagic()), p.bytes(11, p.integer(1, 0)))) - if (!approved) { for (const fn of this.results) fn(false); return } - } - this.state('battery') - const battery: FlashPromptBattery = { right: null, left: null } - for (const role of ['right', 'left'] as const) { - for (let attempt = 0; attempt < 2; attempt++) { - try { - const message = await this.link.request(role, p.SID.settings, p.settingsQuery) - const values = p.readBytes(message.payload, 4), value = values ? p.readInteger(values, 12, -1) : -1 - if (value >= 0 && value <= 100) { battery[role] = value; break } - } catch (error) { this.link.check() } - } - } - for (const fn of this.batteries) fn(battery) - for (const fn of this.results) fn(true) - } catch (error) { if (!this.closed) this.state('error', String((error as Error).message ?? error)) } - finally { this.close() } + this.emit(this.stateListeners, 'connecting', '') + identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' }) + } catch (error) { + if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error)) + return + } + if (this.closed) return + this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt( + identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt)) + this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, { + onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')), + onStateStateDetail: (state: string, detail: string) => + this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')), + onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) => + this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }), + onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)), + }) + this.communicator.setListenerListener(this.listenerProxy) + this.communicator.start() + } + + cancel(): void { + this.closed = true + try { this.communicator?.cancel() } catch { /* ignore */ } } - private selection(): Promise { - return new Promise((resolve, reject) => { - const cleanup = () => { clearTimeout(timer); off(); this.stopSelection = null } - const off = this.link.onMessage((role, message) => { - const result = role === 'right' ? flashPromptSelection(message) : null - if (result !== null) { cleanup(); resolve(result) } - }) - const timer = setTimeout(() => { cleanup(); reject(new Error('No response from the glasses.')) }, 120_000) - this.stopSelection = () => { cleanup(); reject(new Error('Cancelled.')) } - }) + + close(): void { + this.closed = true + try { this.communicator?.close() } catch { /* ignore */ } + } + + private emit(listeners: Set<(...args: A) => void>, ...args: A): void { + const snapshot = Array.from(listeners) + setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0) } - private stopHeartbeat(): void { if (this.heartbeat) clearInterval(this.heartbeat); this.heartbeat = null } - cancel(): void { this.close() } - close(): void { this.closed = true; this.stopHeartbeat(); this.stopSelection?.(); this.link.close() } } diff --git a/app/native/frame-timings.ts b/app/native/frame-timings.ts index 56e2a5e1..294349e8 100644 --- a/app/native/frame-timings.ts +++ b/app/native/frame-timings.ts @@ -1,6 +1,6 @@ /** * TS-side wrapper for the Java FrameTimings singleton (per-frame latency - * instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is + * instrumentation; see FrameTimings.kt). Frame ID 0 means "no frame" and is * ignored by every method, so callers never need to null-check. * * Frames form a tree: pass the causing frame's ID as `parentFrameId` when one diff --git a/app/native/ios-bluetooth.ts b/app/native/ios-bluetooth.ts index 20bc5025..b1016b29 100644 --- a/app/native/ios-bluetooth.ts +++ b/app/native/ios-bluetooth.ts @@ -1,75 +1,80 @@ import { ApplicationSettings } from '@nativescript/core' import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity' import { hexToBytes } from '../util/hex-util' +import { kotlinListener } from './kotlin-listener.ios' -declare const FaceclawBluetooth: any -type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number; - identifier?: string; characteristic?: string; data?: string; message?: string } & Partial -export type ConnectionDetails = { characteristics: string[]; maxWrite: number } -export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice } -type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType } +declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any, FaceclawKitIosBleScanListener: any -/** One central manager per app, with asynchronous main-queue GATT operations. */ +/** CBManagerState raw values reported by the Kotlin central. */ +export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5 + +export type BluetoothEvent = + | { kind: 'state'; state: number; authorization: number } + | { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean } + | { kind: 'device'; device: IosDevice } + | { kind: 'scan-started' } + | { kind: 'scan-stopped' } + +/** + * Scanning and MAC-address-to-identifier resolution over the shared Kotlin CoreBluetooth + * central (FaceclawKitIosBleCentral). GATT sessions no longer pass through here: the + * session and stock-firmware flows own their own Kotlin centrals. What stays in TypeScript + * is the Even classification of advertisements and the persisted identifier map, which the + * Android address model (MACs) needs on iOS. + */ export class IosBluetooth { - private readonly native = FaceclawBluetooth.new() + private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared) private readonly listeners = new Set<(event: BluetoothEvent) => void>() - private readonly pending = new Map() - private readonly advertisements = new Map() readonly devices = new Map() - private requestId = 0 private scanTimer: ReturnType | null = null private scanGeneration = 0 + private readonly scanListener: object state = 0 + authorization = 0 scanning = false constructor() { - this.native.eventHandler = (json: string) => { - try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) } - } + this.scanListener = kotlinListener(FaceclawKitIosBleScanListener, { + onBluetoothStateStateAuthorization: (state: number, authorization: number) => { + this.state = Number(state); this.authorization = Number(authorization) + this.emit({ kind: 'state', state: this.state, authorization: this.authorization }) + }, + onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => { + // The Kotlin central already merges split advertisements and bounds its cache. + const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false } + this.emit({ kind: 'advertisement', ...raw }) + const device = identifyIosPeripheral(raw) + if (device) { + this.devices.set(device.address, device) + ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role })) + this.emit({ kind: 'device', device }) + } + }, + }) + this.native.setScanListenerListener(this.scanListener) } onEvent(listener: (event: BluetoothEvent) => void): () => void { this.listeners.add(listener); return () => { this.listeners.delete(listener) } } private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) } - private receive(event: NativeEvent): void { - if (event.kind === 'completion') { - const pending = this.pending.get(event.id!) - if (pending) { - this.pending.delete(event.id!); clearTimeout(pending.timer) - if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!) - } - return - } - if (event.kind === 'state') this.state = event.state! - if (event.kind === 'advertisement') { - const previous = this.advertisements.get(event.identifier!) - const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '', - manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false } - // Bound the cache even in a crowded radio environment. - if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!) - this.advertisements.set(raw.identifier, raw) - const device = identifyIosPeripheral(raw) - if (device) { - this.devices.set(device.address, device) - ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role })) - this.emit({ kind: 'device', device }) - } - return - } - this.emit(event) + /** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */ + private ensureCentral(): void { + // A zero timeout returns immediately after creating the central; the state arrives + // through the scan listener on the main queue. + this.native.waitForPoweredOnTimeoutMs(0) } ensureReady(): Promise { + if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve() return new Promise((resolve, reject) => { const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() } const off = this.onEvent(event => { if (event.kind !== 'state') return - const state = (event as NativeEvent).state - if (state === 5) finish() - else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.')) - else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.')) - else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.')) + if (event.state === BLE_STATE_POWERED_ON) finish() + else if (event.state === BLE_STATE_UNSUPPORTED) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.')) + else if (event.state === BLE_STATE_UNAUTHORIZED) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.')) + else if (event.state === BLE_STATE_POWERED_OFF) finish(new Error('Turn on Bluetooth, then try again.')) }) const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000) - this.native.initializeBluetooth() + this.ensureCentral() }) } async startScan(durationMs = 12_000): Promise { @@ -77,17 +82,18 @@ export class IosBluetooth { const generation = this.scanGeneration await this.ensureReady() if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled') - this.scanning = true; this.native.scan(true) + this.scanning = true; this.native.startScan() this.scanTimer = setTimeout(() => this.stopScan(), durationMs) this.emit({ kind: 'scan-started' }) } stopScan(): void { ++this.scanGeneration if (this.scanTimer) clearTimeout(this.scanTimer) - this.scanTimer = null; this.native.scan(false) + this.scanTimer = null const wasScanning = this.scanning; this.scanning = false - if (wasScanning) this.emit({ kind: 'scan-stopped' }) + if (wasScanning) { this.native.stopScan(); this.emit({ kind: 'scan-stopped' }) } } + /** Peripheral identifiers for the stored MAC addresses, scanning for any that are not remembered. */ async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise> { const generation = this.scanGeneration await this.ensureReady() @@ -109,7 +115,7 @@ export class IosBluetooth { await new Promise((resolve, reject) => { const off = this.onEvent(event => { if (event.kind === 'device') { - const device = (event as { device: IosDevice }).device + const device = event.device for (const role of ['left', 'right', 'ring'] as const) { if (addresses[role] === device.address && device.role === role) result[role] = device.identifier } @@ -123,29 +129,6 @@ export class IosBluetooth { }) return result } - private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000, disconnectOnTimeout = true): Promise { - const id = ++this.requestId - return new Promise((resolve, reject) => { - const timer = setTimeout(() => { - this.pending.delete(id); if (disconnectOnTimeout) this.native.disconnect(identifier) - reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.')) - }, timeoutMs) - this.pending.set(id, { resolve, reject, timer }) - try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) } - }) - } - connect(identifier: string, requiresANCS = false): Promise { return this.operation(identifier, id => this.native.connectRequiresANCSRequestId(identifier, requiresANCS, id), 20_000) } - async subscribe(identifier: string, characteristic: string): Promise { - // R1 exposes two notification channels; one may never complete its CCCD - // request. Keep the link alive so the other channel can still be tried. - await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id), 30_000, false) - } - async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise { - const copy = new Uint8Array(bytes) - const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength) - await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id)) - } - disconnect(identifier: string): void { this.native.disconnect(identifier) } forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) } } let instance: IosBluetooth | null = null diff --git a/app/native/kotlin-listener.ios.ts b/app/native/kotlin-listener.ios.ts new file mode 100644 index 00000000..8bbab1bb --- /dev/null +++ b/app/native/kotlin-listener.ios.ts @@ -0,0 +1,10 @@ +/** + * Implements a FaceclawKit (Kotlin) listener protocol with plain JavaScript methods. + * NativeScript maps the method names to the protocol's selectors by camel-casing the + * selector parts (`onLog:` -> `onLogLine`, `onState:detail:` -> `onStateStateDetail`, ...). + * Keep a reference to the returned object for as long as the Kotlin side may call it. + */ +export function kotlinListener(protocol: unknown, methods: T): T { + const NativeListener = (NSObject as any).extend(methods, { protocols: [protocol] }) + return NativeListener.new() as T +} diff --git a/app/native/kotlin-protocol-smoke.ios.ts b/app/native/kotlin-protocol-smoke.ios.ts index d3c41337..ecd2629e 100644 --- a/app/native/kotlin-protocol-smoke.ios.ts +++ b/app/native/kotlin-protocol-smoke.ios.ts @@ -1,11 +1,9 @@ +import { GrayImage } from "../graphics/image" +import { encodeShellScene } from "../graphics/shell-scene" import { decodeImageBytes } from './image-bytes.ios' import * as protocol from '../g2/ble-protocol.ios' -import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios' import { SurfaceCompositor } from '../graphics/surface-compositor.ios' -import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios' import { lvglMetrics } from './lvgl-font.ios' -import { textureImageId } from './texture-atlas.ios' -import { IosTexturePlanner } from './texture-planner.ios' /** Development-only checks of real NativeScript selectors and bulk data crossings. */ export function runKotlinProtocolSmokeTest(): void { @@ -21,31 +19,20 @@ export function runKotlinProtocolSmokeTest(): void { const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2)))) const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 }) assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event') - const transport = new CfwTransport() - const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185) - assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression') - transport.reset() - assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK') const c = new SurfaceCompositor(3, 2) c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' }) c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 }) assert(hex(c.composite()) === '0010ff203040', 'composition') assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing') + c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255])) + assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition') c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking') - const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2) - textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' }) - const white = new Uint8Array(4).fill(255) - const icon = textureImageId('protocol-smoke-white', 2, 2, white) - const draw = new DataView(new ArrayBuffer(9)); draw.setUint8(0, 1); draw.setUint32(1, icon, true) - textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer) - const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2) - const cold = textures.plan(null, packed, snapshot.textures, 1) - assert(!!cold && cold.uploads[0]?.[0] === 18 && cold.payload[0] === 8, 'texture upload and draw') - assert(textures.plan(null, packed, snapshot.textures, 1)?.uploads.length === 0, 'texture reuse') - textures.reset() - assert((textures.plan(null, packed, snapshot.textures, 1)?.uploads.length ?? 0) > 0, 'texture reset') - const changed = new Uint8Array(16); changed[0] = 255 - assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update') + const selectedSurface = new GrayImage(8, 4, 1) + selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2) + const selectedPreview = new SurfaceCompositor(8, 4) + selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }])) + const selectedPixels = selectedPreview.composite() + assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge') assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge') const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0, payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) }) diff --git a/app/native/preview-display.ios.ts b/app/native/preview-display.ios.ts new file mode 100644 index 00000000..2a70a769 --- /dev/null +++ b/app/native/preview-display.ios.ts @@ -0,0 +1,146 @@ +import { File, knownFolders, path, type ImageSource } from "@nativescript/core"; +import { SurfaceCompositor } from "../graphics/surface-compositor"; +import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image"; +import { previewPixels } from "./ios-graphics"; +import { toData } from "./kotlin-data"; +import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator.ios"; + +declare const FaceclawKitIosProtocol: any; + +/** The display subset of the communicator bridge; see the Android twin. */ +export type DisplayTarget = Pick< + FaceclawCommunicatorBridge, + | "configureCompositorScreen" + | "configureSurface" + | "removeSurface" + | "setSurfaceVisible" + | "setSurfaceDepth" + | "setUnderlayDim" + | "setScreenBlanked" + | "submitSurfaceFrame" + | "submitShellScene" + | "waitForFrameFinished" + | "getCompositePreview" + | "saveScreenshot" + | "startScreenRecording" + | "recordScreenFrame" + | "stopScreenRecording" +>; + +function timestamp(): string { + const now = new Date(); + const pad = (value: number, width = 2) => String(value).padStart(width, "0"); + return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`; +} + +/** + * Headless display target for iOS while no glasses session exists: the same + * shared Kotlin SurfaceCompositor a live session composes with, so the phone + * mirror, apps and shell render identically before connecting. Frames end + * at the compositor; there is no transmit backpressure to wait on. + */ +export class PreviewDisplayTarget implements DisplayTarget { + private compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT); + private onFrameComposited: (() => void) | null = null; + private released = false; + + /** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */ + activate(onFrameComposited: () => void): void { + this.onFrameComposited = onFrameComposited; + } + + release(): void { + this.released = true; + this.onFrameComposited = null; + } + + private composited(): void { + if (!this.released) this.onFrameComposited?.(); + } + + async configureCompositorScreen(width: number, height: number): Promise { + if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) { + this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height)); + } + } + + async configureSurface(id: string, options: SurfaceOptions): Promise { + this.compositor.configureSurface(id, { + x: Math.round(options.x), y: Math.round(options.y), width: Math.round(options.width), height: Math.round(options.height), + zOrder: Math.round(options.zOrder), transparency: options.transparency, + }); + } + + async removeSurface(id: string): Promise { + this.compositor.removeSurface(id); + this.composited(); + } + + async setSurfaceVisible(id: string, visible: boolean): Promise { + this.compositor.setSurfaceVisible(id, Boolean(visible)); + this.composited(); + } + + async setSurfaceDepth(id: string, depth: number): Promise { + this.compositor.setSurfaceDepth(id, Math.round(depth)); + } + + async setUnderlayDim(belowZOrder: number, factor: number): Promise { + this.compositor.setUnderlayDim(Math.round(belowZOrder), Math.max(0, Math.min(1, factor))); + this.composited(); + } + + async setScreenBlanked(blanked: boolean): Promise { + this.compositor.setScreenBlanked(Boolean(blanked)); + this.composited(); + } + + async submitSurfaceFrame( + surfaceId: string, + pixels8bpp: Uint8Array, + rect: { x: number; y: number; width: number; height: number }, + _fingerprint: string, + _paintMs = -1, + _frameId = 0, + glyphs: ArrayBuffer | null = null, + ): Promise { + this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp, + { x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs); + this.composited(); + } + + async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise { + this.compositor.setShellScene(bytes); + this.composited(); + } + + /** Nothing transmits: every frame is finished as soon as it is composited. */ + waitForFrameFinished(frameId: number, _timeoutMs: number): Promise { + return Promise.resolve(frameId > 0 ? "composited" : null); + } + + /** The current composite as gray pixels (row-major, compositor size). */ + composite(): Uint8Array { + return this.compositor.composite(); + } + + getCompositePreview(green = false): ImageSource | null { + return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green); + } + + /** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */ + saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string { + if (crop) return ""; + const png = FaceclawKitIosProtocol.new().pngDataWidthHeight(toData(this.compositor.composite()), this.compositor.width, this.compositor.height); + if (!png) return ""; + const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`); + File.fromPath(target).writeSync(png); + return target; + } + + startScreenRecording(): void {} + + recordScreenFrame(): void {} + + stopScreenRecording(): string { return ""; } +} diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index baad5305..33a226de 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -19,9 +19,11 @@ export type DisplayTarget = Pick< | "configureSurface" | "removeSurface" | "setSurfaceVisible" + | "setSurfaceDepth" | "setUnderlayDim" | "setScreenBlanked" | "submitSurfaceFrame" + | "submitShellScene" | "waitForFrameFinished" | "getCompositePreview" | "saveScreenshot" @@ -97,6 +99,14 @@ export class PreviewDisplayTarget implements DisplayTarget { this.compositor.setSurfaceVisible(id, Boolean(visible)); } + async setSurfaceDepth(id: string, depth: number): Promise { + this.compositor.setSurfaceDepth(id, Math.round(depth)); + } + + async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { + this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId); + } + async setUnderlayDim(belowZOrder: number, factor: number): Promise { this.compositor.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor)); } diff --git a/app/native/remote-input.ios.ts b/app/native/remote-input.ios.ts index d20e9e5c..3e6c9b07 100644 --- a/app/native/remote-input.ios.ts +++ b/app/native/remote-input.ios.ts @@ -1,13 +1,16 @@ import { createListenerPool, type InterfaceAddress } from '../remote/listeners'; -declare const FaceclawRemoteInput: any; -export function remoteNative() { +declare const FaceclawKitIosRemoteInput: any; +/** The shared Kotlin RemoteInputSession over POSIX sockets; request notifications arrive on the main queue. */ +export function remoteNative(onRequestReady: () => void) { return createListenerPool(() => { - const native = FaceclawRemoteInput.alloc().init(); - return { start: (port, address) => String(native.startAddress(port, address)), stop: () => native.stop(), - nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeResponse(id, response) }; + const native = FaceclawKitIosRemoteInput.new(); + native.setRequestListenerListener(onRequestReady); + return { start: (port, address) => String(native.startAddressPortAddress(port, address)), stop: () => { native.setRequestListenerListener(null); native.stop(); }, + nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeIdValue(id, response) }; }); } -export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(FaceclawRemoteInput.interfaces())); } -export function randomTokenSecret(): string { return String(FaceclawRemoteInput.shared().randomSecret()); } -export function tokenHash(value: string): string { return String(FaceclawRemoteInput.shared().tokenDigest(value)); } +function shared(): any { return FaceclawKitIosRemoteInput.companion.shared; } +export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(shared().interfaces())); } +export function randomTokenSecret(): string { return String(shared().randomSecret()); } +export function tokenHash(value: string): string { return String(shared().tokenDigestValue(value)); } export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; } diff --git a/app/native/remote-input.ts b/app/native/remote-input.ts index 8650ef07..0be486be 100644 --- a/app/native/remote-input.ts +++ b/app/native/remote-input.ts @@ -1,10 +1,12 @@ import { createListenerPool, type InterfaceAddress } from '../remote/listeners'; import { Utils } from '@nativescript/core'; declare const com: any; -export function remoteNative() { +export function remoteNative(onRequestReady: () => void) { return createListenerPool(() => { const native = new com.faceclaw.app.FaceclawRemoteInput(); - return { start: (port, address) => String(native.startAddress(port, address)), stop: () => native.stop(), + const ready = new java.lang.Runnable({ run: onRequestReady }); + native.setRequestListener(ready); + return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); }, nextRequest: () => native.nextRequest(), complete: (id, response) => native.complete(id, response) }; }); } diff --git a/app/native/settings-store.ios.ts b/app/native/settings-store.ios.ts index 7ffd322f..84d49add 100644 --- a/app/native/settings-store.ios.ts +++ b/app/native/settings-store.ios.ts @@ -18,6 +18,12 @@ function markChanged(key: string): void { } let pollTimer: ReturnType | null = null const listeners = new Set<(key: string) => void>() +export function disposeSettingsStore(): void { + listeners.clear() + if (pollTimer !== null) clearInterval(pollTimer) + pollTimer = null + observed.clear() +} function changed(key: string): void { setTimeout(() => { for (const listener of [...listeners]) listener(key) }, 0) } diff --git a/app/native/settings-store.ts b/app/native/settings-store.ts index e738f410..3f3f43da 100644 --- a/app/native/settings-store.ts +++ b/app/native/settings-store.ts @@ -14,6 +14,13 @@ let javaInstance: any = null; let retainedListenerProxy: any = null; const changeListeners = new Set<(key: string) => void>(); +/** Release this isolate's native observer before its worker is terminated. */ +export function disposeSettingsStore(): void { + changeListeners.clear(); + if (retainedListenerProxy !== null) getJava().unregisterListener(retainedListenerProxy); + retainedListenerProxy = null; +} + function getJava(): any { if (javaInstance === null) { const context = Utils.android?.getApplicationContext?.(); diff --git a/app/native/sse.ios.ts b/app/native/sse.ios.ts index bd3e7284..2c5c1609 100644 --- a/app/native/sse.ios.ts +++ b/app/native/sse.ios.ts @@ -1,28 +1,19 @@ import { type SseListener } from './sse-types'; import { withUserAgent } from '../util/http'; +import { kotlinListener } from './kotlin-listener.ios'; export { type SseListener } from './sse-types'; -declare const FaceclawSseRequest: any; +declare const FaceclawKitIosSseRequest: any, FaceclawKitFaceclawSseListener: any; -/** Drain native events on the requesting JS isolate, never on a URLSession thread. */ +/** The shared Kotlin SseStream over NSURLSession; the Kotlin side delivers every callback on the main queue, in order. */ export function openSseRequest(url: string, body: string, headers: Record, listener: SseListener): { cancel(): void } { - const native = FaceclawSseRequest.alloc().initWithURLBodyHeaders(url, body, JSON.stringify(withUserAgent(headers))); let ended = false; - const cancel = () => { if (ended) return; ended = true; clearInterval(timer); native.cancel(); }; - const timer = setInterval(() => { - try { - for (const event of JSON.parse(native.takeEvents())) { - if (ended) break; - if (event.kind === 'line') listener.onLine(event.text); - else { - cancel(); - if (event.kind === 'complete') listener.onComplete(); - else if (event.kind === 'http-error') listener.onHttpError(event.code, event.text); - else listener.onFailure(event.text); - } - } - } catch (error) { - cancel(); listener.onFailure(String((error as Error)?.message ?? error)); - } - }, 25); - return { cancel }; + const once = (deliver: () => void) => { if (ended) return; ended = true; deliver(); }; + const nativeListener = kotlinListener(FaceclawKitFaceclawSseListener, { + onLineLine: (line: string) => { if (!ended) listener.onLine(String(line ?? '')); }, + onHttpErrorCodeBody: (code: number, errorBody: string) => once(() => listener.onHttpError(Number(code), String(errorBody ?? ''))), + onComplete: () => once(() => listener.onComplete()), + onFailureMessage_: (message: string) => once(() => listener.onFailure(String(message ?? ''))), + }); + const native = FaceclawKitIosSseRequest.alloc().initWithUrlBodyHeadersJsonListener(url, body, JSON.stringify(withUserAgent(headers)), nativeListener); + return { cancel: () => { if (ended) return; ended = true; native.cancel(); } }; } diff --git a/app/native/texture-planner.ios.ts b/app/native/texture-planner.ios.ts deleted file mode 100644 index aabbc056..00000000 --- a/app/native/texture-planner.ios.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { fromData, toData, nativeArray } from './kotlin-data' -import type { SessionTexturePlanner, TextureFrame, TexturePlan } from '../g2/texture-planner' -declare const FaceclawKitIosTexturePlanner: any - -export class IosTexturePlanner implements SessionTexturePlanner { - private readonly native = FaceclawKitIosTexturePlanner.new() - reset(): void { this.native.reset() } - plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null { - const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId) - return result ? { payload: fromData(result.payload), uploads: nativeArray(result.uploads, fromData), - nextFid: result.nextFid, usedBytes: result.usedBytes } : null - } -} diff --git a/app/phone-ui/main-page.ts b/app/phone-ui/main-page.ts index 244b6d05..b22a42d5 100644 --- a/app/phone-ui/main-page.ts +++ b/app/phone-ui/main-page.ts @@ -15,6 +15,7 @@ type MainPageState = { model: MainViewModel propertyChangeHandler: (args: EventData & { propertyName?: string }) => void orientationHandler: () => void + detachPreview: () => void } function getPageState(page: Page): MainPageState | undefined { @@ -32,6 +33,7 @@ function cleanupPage(page: Page): void { return } state.model.off(Observable.propertyChangeEvent, state.propertyChangeHandler) + state.detachPreview() Application.off(Application.orientationChangedEvent, state.orientationHandler) // navigatingTo builds a fresh model each visit; drop the old one's // controller/settings subscriptions or every navigation leaks a listener. @@ -130,6 +132,8 @@ export function loaded(args: EventData) { const state: MainPageState = { model, + detachPreview: dashboardController.attachPhonePreview(() => + page.isLoaded && !model.displayPreviewMessage && (!isAndroid || !!page.android?.isShown())), orientationHandler: () => { setTimeout(() => { model.refreshLayoutMetrics() diff --git a/app/phone-ui/main-page.xml b/app/phone-ui/main-page.xml index c1bfbff0..e80b711b 100644 --- a/app/phone-ui/main-page.xml +++ b/app/phone-ui/main-page.xml @@ -106,50 +106,76 @@ - - + room. The tabbed controls are collapsed while it is up. + + NativeScript has no maxHeight, so the transparent outer grid's + fixed height is the cap: it shrinks to the page when the keyboard + leaves less room than that, and the bottom-aligned modal inside + sizes to its content within it. Past the cap the fields scroll + and the buttons stay put. + + Any GridLayout in here needs an explicit rows="auto": the implicit + single star row stretches to fill all the height offered (Stack- + and DockLayout offer it AT_MOST the remaining height), which is + what made the form balloon with the keyboard minimized. --> + + +