forked from Blendman974/faceclaw
Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes
This commit is contained in:
commit
a2166c6a6c
822 files changed
+79434
-29944
No files matched your search
@@ -0,0 +1,22 @@
|
|||||||
|
name: Secret scan
|
||||||
|
|
||||||
|
# Scans for committed secrets on pushes to main and on pull requests.
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
gitleaks:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- uses: gitleaks/gitleaks-action@v2
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
name: Tests
|
||||||
|
|
||||||
|
# Runs the test suite on pushes to main and on pull requests.
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- run: npm ci
|
||||||
|
- run: npm test
|
||||||
|
|
||||||
|
# The shared Kotlin tests (tests/kotlin, on the JVM host). The runner image
|
||||||
|
# already carries the JDK 17 and Android SDK the build needs.
|
||||||
|
test-kotlin:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- run: npm ci
|
||||||
|
- run: npm run test:kotlin
|
||||||
+24
-1
@@ -1,11 +1,17 @@
|
|||||||
# NativeScript
|
# NativeScript
|
||||||
hooks/
|
hooks/*
|
||||||
|
!hooks/before-prepare/
|
||||||
|
hooks/before-prepare/*
|
||||||
|
!hooks/before-prepare/faceclaw-kotlin.js
|
||||||
node_modules/
|
node_modules/
|
||||||
platforms/
|
platforms/
|
||||||
|
|
||||||
# Release APKs from scripts/release.sh
|
# Release APKs from scripts/release.sh
|
||||||
dist/
|
dist/
|
||||||
|
|
||||||
|
# CPU captures and source maps pulled from the installed app
|
||||||
|
profiles/
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
@@ -39,10 +45,27 @@ old
|
|||||||
|
|
||||||
# pull_config.sh output: the phone's live settings, including API keys
|
# pull_config.sh output: the phone's live settings, including API keys
|
||||||
faceclaw_settings.xml
|
faceclaw_settings.xml
|
||||||
|
faceclaw_settings.ios.json
|
||||||
|
|
||||||
# Developer configs
|
# Developer configs
|
||||||
build_paths.sh
|
build_paths.sh
|
||||||
|
**/xcuserdata/
|
||||||
|
|
||||||
|
# Kotlin build outputs and staged NativeScript artifacts
|
||||||
|
native/kotlin/.gradle/
|
||||||
|
native/kotlin/.kotlin/
|
||||||
|
native/kotlin/build/
|
||||||
|
native/kotlin/shared/build/
|
||||||
|
native/kotlin/local.properties
|
||||||
|
native/kotlin/plugin/platforms/android/*.aar
|
||||||
|
native/kotlin/plugin/platforms/ios/
|
||||||
|
!native/kotlin/plugin/platforms/
|
||||||
|
!native/kotlin/plugin/platforms/android/
|
||||||
|
!native/kotlin/plugin/platforms/android/include.gradle
|
||||||
|
|
||||||
|
tests/kotlin/.gradle/
|
||||||
|
tests/kotlin/.kotlin/
|
||||||
|
tests/kotlin/build/
|
||||||
|
|
||||||
# tools/health-preview.cjs output (rendered PNGs, regenerate with the tool)
|
# tools/health-preview.cjs output (rendered PNGs, regenerate with the tool)
|
||||||
preview/
|
preview/
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Secret-scanning config for the workflow in .github/workflows/secret-scan.yml.
|
||||||
|
[extend]
|
||||||
|
useDefault = true
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
id = "generic-api-key"
|
||||||
|
[rules.allowlist]
|
||||||
|
description = "SIGN_KEY in even-api.ts is a client-side constant the app needs to sign EvenHub requests, not a credential."
|
||||||
|
paths = ['''app/apps/evenhub/even-api\.ts''']
|
||||||
@@ -262,6 +262,7 @@ android {
|
|||||||
|
|
||||||
sourceSets.main {
|
sourceSets.main {
|
||||||
jniLibs.srcDirs += ["$projectDir/src/main/jniLibs"]
|
jniLibs.srcDirs += ["$projectDir/src/main/jniLibs"]
|
||||||
|
// Shared Kotlin is packaged by the local @faceclaw/kotlin plugin.
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
@@ -288,7 +289,25 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NativeScript can retain deleted App_Resources sources in its generated
|
||||||
|
// project. Remove the former copy before Kotlin compilation after this move.
|
||||||
|
def removeLegacyKotlinBridgeSource = tasks.register("removeLegacyKotlinBridgeSource", Delete) {
|
||||||
|
delete file("$projectDir/src/main/java/com/faceclaw/shared/KotlinBridge.kt")
|
||||||
|
// 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/../../${migration.kotlin}").isFile()) {
|
||||||
|
throw new GradleException("Missing Kotlin replacement: ${migration.kotlin}")
|
||||||
|
}
|
||||||
|
delete file("$projectDir/src/main/java/${migration.java}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tasks.matching { it.name.startsWith("compile") && it.name.endsWith("Kotlin") }.configureEach {
|
||||||
|
dependsOn removeLegacyKotlinBridgeSource
|
||||||
|
}
|
||||||
|
|
||||||
tasks.matching { it.name == "preBuild" }.configureEach {
|
tasks.matching { it.name == "preBuild" }.configureEach {
|
||||||
|
dependsOn removeLegacyKotlinBridgeSource
|
||||||
dependsOn tasks.named("prepareFaceclawNativeLibs")
|
dependsOn tasks.named("prepareFaceclawNativeLibs")
|
||||||
dependsOn tasks.named("prepareFaceclawLlama")
|
dependsOn tasks.named("prepareFaceclawLlama")
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ project.ext {
|
|||||||
// androidXTransition = "1.4.1"
|
// androidXTransition = "1.4.1"
|
||||||
// androidXViewPager = "1.0.0"
|
// androidXViewPager = "1.0.0"
|
||||||
|
|
||||||
// useKotlin = true
|
// NativeScript 9 already applies kotlin-android. Match the shared KMP compiler
|
||||||
// kotlinVersion = "1.6.0"
|
// for our .kt sources; no separate Kotlin installation is required.
|
||||||
|
kotlinVersion = "2.4.20"
|
||||||
}
|
}
|
||||||
@@ -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<AudioEffect> {
|
||||||
|
val effects = ArrayList<AudioEffect>()
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
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 connect(address: String, timeoutMs: Int, autoConnect: Boolean): Boolean =
|
||||||
|
bleManager.connect(address, timeoutMs, autoConnect)
|
||||||
|
|
||||||
|
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<ByteArray>,
|
||||||
|
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()
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<ByteArray>, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<FaceclawAlarmService.Ringing> 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()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */
|
|
||||||
public interface FaceclawAlarmListener {
|
|
||||||
void onPhoneAction(long id, String action, int minutes);
|
|
||||||
}
|
|
||||||
@@ -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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
-34
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+28
@@ -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 -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<Long, Ringing> 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<Long, Long> deliveredEarly = Collections.synchronizedMap(new LinkedHashMap<>());
|
|
||||||
private static final Map<Long, Long> 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<Ringing> 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<Long, Long> 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<Ringing> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<Ringing> = 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<Any, Runnable>()
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-10
@@ -1,10 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawAmbientLightListener {
|
|
||||||
/**
|
|
||||||
* A CFW ambient-light report (settings-channel field 105) from the master
|
|
||||||
* temple. body is the raw 24-byte ['A','L',ver,reason,...] record; it is
|
|
||||||
* decoded on the TS side (app/native/ambient-light.ts).
|
|
||||||
*/
|
|
||||||
void onAmbientLight(byte[] body);
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawAudioPacketListener {
|
|
||||||
void onAudioPacket(byte[] data, String arm, long arrivalMs);
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,427 @@
|
|||||||
|
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
|
||||||
|
@Volatile private var ringWorkerThread: 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 startRingWorker(body: () -> Unit) {
|
||||||
|
val thread = Thread(body, "FaceclawRing")
|
||||||
|
ringWorkerThread = thread
|
||||||
|
thread.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun interruptRingWorker() {
|
||||||
|
ringWorkerThread?.interrupt()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun supportsRingHealth(): Boolean = true
|
||||||
|
|
||||||
|
override fun appendRingFrameJournal(line: String) {
|
||||||
|
val folder = java.io.File(appContext.filesDir, "health")
|
||||||
|
if (!folder.isDirectory && !folder.mkdirs()) {
|
||||||
|
throw java.io.IOException("cannot create health folder")
|
||||||
|
}
|
||||||
|
java.io.FileOutputStream(java.io.File(folder, "ring-frames.jsonl"), true).use { out ->
|
||||||
|
out.write(line.toByteArray(Charsets.UTF_8))
|
||||||
|
out.fd.sync()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.requestRingHealthNow]. */
|
||||||
|
fun requestRingHealthNow() = core.requestRingHealthNow()
|
||||||
|
|
||||||
|
/** See [GlassesSessionCore.takeRingHealthBatch]. */
|
||||||
|
fun takeRingHealthBatch(): RingHealthBatch = core.takeRingHealthBatch()
|
||||||
|
|
||||||
|
/** See [GlassesSessionCore.clearRingHealthRecordsBelow]. */
|
||||||
|
fun clearRingHealthRecordsBelow(watermark: Long): Int = core.clearRingHealthRecordsBelow(watermark)
|
||||||
|
|
||||||
|
/** See [GlassesSessionCore.getRingHealthRecords]. */
|
||||||
|
fun getRingHealthRecords(): List<RingProtocol.HealthRecord> = core.getRingHealthRecords()
|
||||||
|
|
||||||
|
/** 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawBleCommunicatorListener {
|
|
||||||
void onLog(String line);
|
|
||||||
void onStateChange(String phase, String status);
|
|
||||||
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
|
|
||||||
void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging);
|
|
||||||
void onSilentMode(boolean silent);
|
|
||||||
void onWearState(boolean wearing);
|
|
||||||
void onPhoneLockState(boolean locked);
|
|
||||||
void onEvenAppConflict(String message);
|
|
||||||
void onFrameMetrics(int paintMs, int transmitMs, int tileCount);
|
|
||||||
void onFrameFinished(int frameId, String outcome);
|
|
||||||
void onFirmwareInfo(String leftVersion, String rightVersion, String extension);
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawBleListener {
|
|
||||||
void onNotification(String address, String characteristicUuid, byte[] data);
|
|
||||||
void onConnectionStateChange(String address, boolean connected);
|
|
||||||
}
|
|
||||||
@@ -1,578 +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<String, BluetoothGatt> gattClients = new ConcurrentHashMap<>();
|
|
||||||
private final Object bluetoothApiLock = new Object();
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, CountDownLatch> connectLatches = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, Boolean> connectResults = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, CountDownLatch> servicesLatches = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, Integer> servicesStatuses = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, CountDownLatch> mtuLatches = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, Integer> negotiatedMtus = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, Integer> mtuStatuses = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, CountDownLatch> descriptorLatches = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, Integer> descriptorStatuses = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, CountDownLatch> writeLatches = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, Integer> 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) {
|
|
||||||
return connect(address, timeoutMs, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* EXPERIMENTAL (2026-09-11): the ring specifically needs autoConnect=true.
|
|
||||||
* Even's own vendor app connects the ring with Android's autoConnect flag
|
|
||||||
* set (confirmed via its own BLE debug log, "connect() - device: ...,
|
|
||||||
* auto: true"), while every other faceclaw connection (both glasses arms)
|
|
||||||
* uses autoConnect=false. Direct connect (false) is faster to establish
|
|
||||||
* but Android supervises it less patiently; a live test tonight showed
|
|
||||||
* the ring-only direct connection dying almost exactly 5 seconds in,
|
|
||||||
* repeatedly, with nothing else competing for it - consistent with a
|
|
||||||
* companion device Android's stack expects to be babied via autoConnect,
|
|
||||||
* not direct-connected like the glasses. autoConnect=true can take much
|
|
||||||
* longer to actually complete (Android manages it as a background
|
|
||||||
* reconnect, not an immediate attempt), so this needs its own longer
|
|
||||||
* timeout - do not reuse CONNECT_TIMEOUT_MS/awaitLatch's short window for
|
|
||||||
* it, that would just trade one spurious failure for another.
|
|
||||||
*/
|
|
||||||
public boolean connect(String address, int timeoutMs, boolean autoConnect) {
|
|
||||||
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,
|
|
||||||
autoConnect,
|
|
||||||
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(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<byte[]> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,557 @@
|
|||||||
|
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<String, BluetoothGatt>()
|
||||||
|
private val bluetoothApiLock = Any()
|
||||||
|
|
||||||
|
private val connectLatches = ConcurrentHashMap<String, CountDownLatch>()
|
||||||
|
private val connectResults = ConcurrentHashMap<String, Boolean>()
|
||||||
|
|
||||||
|
private val servicesLatches = ConcurrentHashMap<String, CountDownLatch>()
|
||||||
|
private val servicesStatuses = ConcurrentHashMap<String, Int>()
|
||||||
|
|
||||||
|
private val mtuLatches = ConcurrentHashMap<String, CountDownLatch>()
|
||||||
|
private val negotiatedMtus = ConcurrentHashMap<String, Int>()
|
||||||
|
private val mtuStatuses = ConcurrentHashMap<String, Int>()
|
||||||
|
|
||||||
|
private val descriptorLatches = ConcurrentHashMap<String, CountDownLatch>()
|
||||||
|
private val descriptorStatuses = ConcurrentHashMap<String, Int>()
|
||||||
|
|
||||||
|
private val writeLatches = ConcurrentHashMap<String, CountDownLatch>()
|
||||||
|
private val writeStatuses = ConcurrentHashMap<String, Int>()
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var 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 = connect(address, timeoutMs, false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EXPERIMENTAL (2026-09-11): the ring specifically needs autoConnect=true.
|
||||||
|
* Even's own vendor app connects the ring with Android's autoConnect flag
|
||||||
|
* set (confirmed via its own BLE debug log, "connect() - device: ...,
|
||||||
|
* auto: true"), while every other faceclaw connection (both glasses arms)
|
||||||
|
* uses autoConnect=false. Direct connect (false) is faster to establish
|
||||||
|
* but Android supervises it less patiently; a live test tonight showed
|
||||||
|
* the ring-only direct connection dying almost exactly 5 seconds in,
|
||||||
|
* repeatedly, with nothing else competing for it - consistent with a
|
||||||
|
* companion device Android's stack expects to be babied via autoConnect,
|
||||||
|
* not direct-connected like the glasses. autoConnect=true can take much
|
||||||
|
* longer to actually complete (Android manages it as a background
|
||||||
|
* reconnect, not an immediate attempt), so this needs its own longer
|
||||||
|
* timeout - do not reuse CONNECT_TIMEOUT_MS/awaitLatch's short window for
|
||||||
|
* it, that would just trade one spurious failure for another.
|
||||||
|
*/
|
||||||
|
fun connect(address: String?, timeoutMs: Int, autoConnect: Boolean): 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,
|
||||||
|
autoConnect,
|
||||||
|
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<ByteArray?>?,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
import java.util.Calendar
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 const val DAY_MS = 24L * 60 * 60 * 1000
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
private class Event(
|
||||||
|
val id: Long,
|
||||||
|
val title: String,
|
||||||
|
val startMs: Long,
|
||||||
|
val endMs: Long,
|
||||||
|
val allDay: Boolean,
|
||||||
|
val location: String,
|
||||||
|
val calendarName: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON array of events that haven't ended yet and start before
|
||||||
|
* now+windowMs, ordered by start time, capped at maxEvents. Each
|
||||||
|
* element carries id, title, startMs, endMs, allDay, location, and
|
||||||
|
* calendarName. All-day events are reported as local-midnight
|
||||||
|
* boundaries (matching EventKit on iOS), not the UTC-midnight
|
||||||
|
* boundaries the provider stores them as.
|
||||||
|
*/
|
||||||
|
@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)
|
||||||
|
|
||||||
|
// The provider matches all-day instances by their UTC-midnight
|
||||||
|
// bounds, which can be up to a day away from the local day they
|
||||||
|
// represent; widen the query and filter after converting.
|
||||||
|
val builder = CalendarContract.Instances.CONTENT_URI.buildUpon()
|
||||||
|
ContentUris.appendId(builder, now - DAY_MS)
|
||||||
|
ContentUris.appendId(builder, end + DAY_MS)
|
||||||
|
val uri = builder.build()
|
||||||
|
|
||||||
|
val events = ArrayList<Event>()
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
try {
|
||||||
|
cursor = context.contentResolver.query(
|
||||||
|
uri,
|
||||||
|
PROJECTION,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
CalendarContract.Instances.BEGIN + " ASC")
|
||||||
|
if (cursor != null) {
|
||||||
|
val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
|
||||||
|
val local = Calendar.getInstance()
|
||||||
|
// Rows arrive in raw-BEGIN order, but converting all-day
|
||||||
|
// events moves them by less than a day. Once `limit`
|
||||||
|
// events are kept, nothing starting after the latest of
|
||||||
|
// them can make the cut, and no row whose raw BEGIN is a
|
||||||
|
// day past that can start before it.
|
||||||
|
var cutoff = Long.MAX_VALUE
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
if (cutoff != Long.MAX_VALUE && cursor.getLong(2) - DAY_MS > cutoff) break
|
||||||
|
val event = readEvent(cursor, utc, local)
|
||||||
|
if (event.endMs <= now || event.startMs > end || event.startMs > cutoff) continue
|
||||||
|
events.add(event)
|
||||||
|
if (events.size == limit) cutoff = events.maxOf { it.startMs }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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()
|
||||||
|
}
|
||||||
|
|
||||||
|
events.sortBy { it.startMs }
|
||||||
|
val out = JSONArray()
|
||||||
|
for (event in events.take(limit)) {
|
||||||
|
try {
|
||||||
|
out.put(buildEventJson(event))
|
||||||
|
} catch (e: JSONException) {
|
||||||
|
Log.w(TAG, "failed to serialize calendar event", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun readEvent(cursor: Cursor, utc: Calendar, local: Calendar): Event {
|
||||||
|
val allDay = cursor.getInt(4) != 0
|
||||||
|
var startMs = cursor.getLong(2)
|
||||||
|
var endMs = cursor.getLong(3)
|
||||||
|
if (allDay) {
|
||||||
|
startMs = utcMidnightToLocal(startMs, utc, local)
|
||||||
|
endMs = utcMidnightToLocal(endMs, utc, local)
|
||||||
|
}
|
||||||
|
return Event(
|
||||||
|
id = cursor.getLong(0),
|
||||||
|
title = if (cursor.isNull(1)) "" else cursor.getString(1),
|
||||||
|
startMs = startMs,
|
||||||
|
endMs = endMs,
|
||||||
|
allDay = allDay,
|
||||||
|
location = if (cursor.isNull(5)) "" else cursor.getString(5),
|
||||||
|
calendarName = if (cursor.isNull(6)) "" else cursor.getString(6),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All-day events are stored as UTC midnights (CalendarContract
|
||||||
|
* requires EVENT_TIMEZONE=UTC for them); re-anchor that calendar
|
||||||
|
* date to midnight in the device's timezone.
|
||||||
|
*/
|
||||||
|
private fun utcMidnightToLocal(utcMs: Long, utc: Calendar, local: Calendar): Long {
|
||||||
|
utc.timeInMillis = utcMs
|
||||||
|
local.timeZone = TimeZone.getDefault()
|
||||||
|
local.clear()
|
||||||
|
local.set(utc.get(Calendar.YEAR), utc.get(Calendar.MONTH), utc.get(Calendar.DAY_OF_MONTH))
|
||||||
|
return local.timeInMillis
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(JSONException::class)
|
||||||
|
private fun buildEventJson(event: Event): JSONObject {
|
||||||
|
val json = JSONObject()
|
||||||
|
json.put("id", event.id)
|
||||||
|
json.put("title", event.title)
|
||||||
|
json.put("startMs", event.startMs)
|
||||||
|
json.put("endMs", event.endMs)
|
||||||
|
json.put("allDay", event.allDay)
|
||||||
|
json.put("location", event.location)
|
||||||
|
json.put("calendarName", event.calendarName)
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<byte[]> 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
-17
@@ -1,17 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawCaptionEngineListener {
|
|
||||||
/**
|
|
||||||
* One finished utterance. text may be empty when no ASR model is loaded
|
|
||||||
* or recognition produced nothing; embedding is the L2-normalized
|
|
||||||
* voice-print or null when the speaker model is unavailable or the
|
|
||||||
* utterance was too short. startMs/endMs are on the engine's sample
|
|
||||||
* clock (milliseconds of audio since start()).
|
|
||||||
*/
|
|
||||||
void onUtterance(String text, float[] embedding, long startMs, long endMs, double peakRms);
|
|
||||||
|
|
||||||
/** A speech onset was detected; the utterance is now being collected. */
|
|
||||||
void onSpeechStart(long startMs);
|
|
||||||
|
|
||||||
void onStatus(String status);
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Receives stock compass heading and calibration notifications on the thread that subscribed. */
|
|
||||||
public interface FaceclawCompassListener {
|
|
||||||
void onCompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies,
|
|
||||||
int orientationSource, int diagnosticFlags, long sampleTimeMs);
|
|
||||||
}
|
|
||||||
@@ -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<String> args = new ArrayList<>();
|
|
||||||
List<String> 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<String> args = new ArrayList<>();
|
|
||||||
List<String> 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<String> 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<String> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<String> = ArrayList()
|
||||||
|
val where: MutableList<String> = 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<Any?>(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<String> = ArrayList()
|
||||||
|
val where: MutableList<String> = 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<Any?>(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<Any?>(toSpeakerId, sessionId, fromSpeakerId))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteSpeaker(speakerId: Long) {
|
||||||
|
val db = writableDatabase
|
||||||
|
db.execSQL("UPDATE segments SET speaker_id=NULL WHERE speaker_id=?", arrayOf<Any?>(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<String> = 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<Any?>(recordingCutoffMs))
|
||||||
|
}
|
||||||
|
if (captionCutoffMs > 0) {
|
||||||
|
val paths: MutableList<String?> = 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<Any?>(captionCutoffMs))
|
||||||
|
val count = db.rawQuery("SELECT changes()", null)
|
||||||
|
count.close()
|
||||||
|
db.execSQL("DELETE FROM sessions WHERE started_at < ?", arrayOf<Any?>(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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<ScanResult> 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<String> bondedCandidateJsonList() {
|
|
||||||
List<String> out = new ArrayList<>();
|
|
||||||
Set<BluetoothDevice> 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<byte[]> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<ByteArray>? = 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<ScanResult?>?) {
|
||||||
|
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<String> {
|
||||||
|
val out = ArrayList<String>()
|
||||||
|
val bonded: Set<BluetoothDevice>? = 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Callbacks from FaceclawDeviceDiscovery's live scan to the TypeScript layer. */
|
|
||||||
public interface FaceclawDeviceDiscoveryListener {
|
|
||||||
/**
|
|
||||||
* One advertisement (or one bonded device) as a JSON object:
|
|
||||||
* {address, name, manufacturerData (hex, company id included), rssi|null,
|
|
||||||
* txPower|null, connectable|null, bonded, source, seenAtMs}.
|
|
||||||
* Protocol decoding happens in TypeScript (see app/g2/even-advertisement.ts).
|
|
||||||
*/
|
|
||||||
void onAdvertisement(String json);
|
|
||||||
|
|
||||||
/** The platform refused to start or continue the scan. */
|
|
||||||
void onScanFailed(int errorCode, String message);
|
|
||||||
|
|
||||||
/** Diagnostic line for the pairing log. */
|
|
||||||
void onLog(String line);
|
|
||||||
}
|
|
||||||
@@ -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<Integer> 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<byte[]> 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, ConnectionOptions.WRITE_TYPE, 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<byte[]> frames = BleProtocol.framePb(payload, sid, flag, seq);
|
|
||||||
boolean written = bleManager.writeFrames(
|
|
||||||
address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, 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<byte[]> 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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<OfflineSpeakerDiarizationSegment>? = 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))) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-50
@@ -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<String> rules = Collections.singleton(originRule);
|
|
||||||
WebViewCompat.addDocumentStartJavaScript(webView, script, rules);
|
|
||||||
return true;
|
|
||||||
} catch (Throwable error) {
|
|
||||||
Log.w(TAG, "addDocumentStartJavaScript failed for " + originRule, error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<String> = Collections.singleton(originRule)
|
||||||
|
WebViewCompat.addDocumentStartJavaScript(webView, script, rules)
|
||||||
|
return true
|
||||||
|
} catch (error: Throwable) {
|
||||||
|
Log.w(TAG, "addDocumentStartJavaScript failed for $originRule", error)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Callbacks from a hosted EvenHub app's WebView to the TypeScript host.
|
|
||||||
* All methods are delivered on the Android main thread (NativeScript's JS
|
|
||||||
* thread), regardless of which WebView-internal thread produced them.
|
|
||||||
*/
|
|
||||||
public interface FaceclawEvenHubListener {
|
|
||||||
/**
|
|
||||||
* The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args).
|
|
||||||
* argsJson is the JSON-encoded argument array; callId identifies the
|
|
||||||
* promise to resolve via window.__fcResolve(callId, ok, value).
|
|
||||||
*/
|
|
||||||
void onEvenAppMessage(String handlerName, String argsJson, int callId);
|
|
||||||
|
|
||||||
/** The WebView finished loading the app's page. */
|
|
||||||
void onPageFinished(String url);
|
|
||||||
}
|
|
||||||
+7
-12
@@ -1,8 +1,8 @@
|
|||||||
package com.faceclaw.app;
|
package com.faceclaw.app
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context
|
||||||
import android.view.View;
|
import android.view.View
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A WebView for hosting an EvenHub app that keeps rendering and running JS even
|
* 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
|
* 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.
|
* 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) {
|
override fun onWindowVisibilityChanged(visibility: Int) {
|
||||||
super(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onWindowVisibilityChanged(int visibility) {
|
|
||||||
// Never propagate a hidden window: keep Chromium's page-visibility
|
// Never propagate a hidden window: keep Chromium's page-visibility
|
||||||
// "visible" so the renderer isn't frozen when Faceclaw backgrounds.
|
// "visible" so the renderer isn't frozen when Faceclaw backgrounds.
|
||||||
super.onWindowVisibilityChanged(View.VISIBLE);
|
super.onWindowVisibilityChanged(View.VISIBLE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
-172
@@ -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 = "<script>" + injectScript + "</script>";
|
|
||||||
// After <head...> if present, else before everything.
|
|
||||||
int headIndex = indexOfIgnoreCase(html, "<head");
|
|
||||||
if (headIndex >= 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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+75
-72
@@ -1,16 +1,15 @@
|
|||||||
package com.faceclaw.app;
|
package com.faceclaw.app
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity
|
||||||
import android.os.Build;
|
import android.os.Build
|
||||||
import android.os.Handler;
|
import android.os.Handler
|
||||||
import android.os.Looper;
|
import android.os.Looper
|
||||||
import android.view.View;
|
import android.view.View
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps EvenHub app WebViews alive and rendering while the phone shows the
|
* 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
|
* activity is foreground. When Faceclaw itself is backgrounded, the window goes
|
||||||
* invisible and Chromium freezes the renderer — the timer/rAF ticks and pushed
|
* 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
|
* 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
|
* Chromium about window visibility so the page never goes hidden; and each is
|
||||||
* pinned to IMPORTANT renderer priority (not waived when not visible) with
|
* pinned to IMPORTANT renderer priority (not waived when not visible) with
|
||||||
* timers/onResume asserted on attach. The main Looper keeps ticking under the
|
* timers/onResume asserted on attach. The main Looper keeps ticking under the
|
||||||
* foreground service, so apps keep driving their glasses windows in the
|
* foreground service, so apps keep driving their glasses windows in the
|
||||||
* background.
|
* background.
|
||||||
*/
|
*/
|
||||||
public class FaceclawEvenHubWebViewHost {
|
class FaceclawEvenHubWebViewHost {
|
||||||
private static FaceclawEvenHubWebViewHost instance;
|
private var overlay: FrameLayout? = null
|
||||||
|
private var shown = false
|
||||||
|
|
||||||
|
private val mainHandler = Handler(Looper.getMainLooper())
|
||||||
|
private val webViews: MutableList<WebView> = 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private var instance: FaceclawEvenHubWebViewHost? = null
|
||||||
|
|
||||||
/** How often the host fires the JS timer queue. 60Hz keeps games smooth. */
|
/** How often the host fires the JS timer queue. 60Hz keeps games smooth. */
|
||||||
private static final long TICK_MS = 16;
|
private const val TICK_MS = 16L
|
||||||
|
|
||||||
private FrameLayout overlay;
|
@JvmStatic
|
||||||
private boolean shown = false;
|
@Synchronized
|
||||||
|
fun getInstance(): FaceclawEvenHubWebViewHost {
|
||||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
if (instance == null) instance = FaceclawEvenHubWebViewHost()
|
||||||
private final List<WebView> webViews = new ArrayList<>();
|
return instance!!
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
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) {
|
private fun ensureOverlay(activity: Activity): FrameLayout {
|
||||||
if (overlay != null) return overlay;
|
overlay?.let { return it }
|
||||||
ViewGroup content = activity.findViewById(android.R.id.content);
|
val content = activity.findViewById<ViewGroup>(android.R.id.content)
|
||||||
overlay = new FrameLayout(activity);
|
val created = FrameLayout(activity)
|
||||||
|
overlay = created
|
||||||
// index 0 = behind the NativeScript content view.
|
// index 0 = behind the NativeScript content view.
|
||||||
content.addView(overlay, 0, new FrameLayout.LayoutParams(
|
content.addView(created, 0, FrameLayout.LayoutParams(
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||||
return overlay;
|
return created
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a WebView to the host, full-size and rendering, hidden behind the UI. */
|
/** Add a WebView to the host, full-size and rendering, hidden behind the UI. */
|
||||||
public void attach(Activity activity, WebView web) {
|
fun attach(activity: Activity, web: WebView) {
|
||||||
FrameLayout o = ensureOverlay(activity);
|
val o = ensureOverlay(activity)
|
||||||
web.setVisibility(View.VISIBLE);
|
web.visibility = View.VISIBLE
|
||||||
if (web.getParent() == null) {
|
if (web.parent == null) {
|
||||||
o.addView(web, new FrameLayout.LayoutParams(
|
o.addView(web, FrameLayout.LayoutParams(
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||||
}
|
}
|
||||||
// resumeTimers() is process-global; make sure nothing left timers paused.
|
// resumeTimers() is process-global; make sure nothing left timers paused.
|
||||||
// onResume() undoes any per-WebView pause. Together with
|
// onResume() undoes any per-WebView pause. Together with
|
||||||
// FaceclawEvenHubWebView faking window visibility, this keeps the page
|
// FaceclawEvenHubWebView faking window visibility, this keeps the page
|
||||||
// running JS while Faceclaw is backgrounded.
|
// running JS while Faceclaw is backgrounded.
|
||||||
web.resumeTimers();
|
web.resumeTimers()
|
||||||
web.onResume();
|
web.onResume()
|
||||||
// Keep the renderer process at IMPORTANT priority even when the WebView
|
// Keep the renderer process at IMPORTANT priority even when the WebView
|
||||||
// isn't visible (waivedWhenNotVisible=false), so Android doesn't
|
// isn't visible (waivedWhenNotVisible=false), so Android doesn't
|
||||||
// deprioritize/kill it in the background.
|
// deprioritize/kill it in the background.
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
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) {
|
if (!ticking) {
|
||||||
ticking = true;
|
ticking = true
|
||||||
mainHandler.postDelayed(ticker, TICK_MS);
|
mainHandler.postDelayed(ticker, TICK_MS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Bring an app's WebView to the front so it is visible on the phone. */
|
/** Bring an app's WebView to the front so it is visible on the phone. */
|
||||||
public void showOnPhone(WebView web) {
|
fun showOnPhone(web: WebView) {
|
||||||
if (overlay == null) return;
|
val o = overlay ?: return
|
||||||
web.bringToFront();
|
web.bringToFront()
|
||||||
overlay.bringToFront();
|
o.bringToFront()
|
||||||
shown = true;
|
shown = true
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Send the overlay back behind the NativeScript UI. */
|
/** Send the overlay back behind the NativeScript UI. */
|
||||||
public void hideOnPhone() {
|
fun hideOnPhone() {
|
||||||
shown = false;
|
shown = false
|
||||||
if (overlay == null) return;
|
val o = overlay ?: return
|
||||||
ViewGroup content = (ViewGroup) overlay.getParent();
|
val content = o.parent as ViewGroup?
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
content.removeView(overlay);
|
content.removeView(o)
|
||||||
content.addView(overlay, 0);
|
content.addView(o, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isShown() {
|
fun isShown(): Boolean {
|
||||||
return shown;
|
return shown
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Remove and destroy a WebView (its app is closing). */
|
/** Remove and destroy a WebView (its app is closing). */
|
||||||
public void detach(WebView web) {
|
fun detach(web: WebView) {
|
||||||
webViews.remove(web);
|
webViews.remove(web)
|
||||||
if (webViews.isEmpty() && ticking) {
|
if (webViews.isEmpty() && ticking) {
|
||||||
ticking = false;
|
ticking = false
|
||||||
mainHandler.removeCallbacks(ticker);
|
mainHandler.removeCallbacks(ticker)
|
||||||
}
|
}
|
||||||
if (overlay != null) overlay.removeView(web);
|
overlay?.removeView(web)
|
||||||
web.destroy();
|
web.destroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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<byte[]> 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<Segment> 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<Segment> 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<byte[]> frames = BleProtocol.framePb(payload, sid, FLAG_OTA, seq);
|
|
||||||
return bleManager.writeFrames(
|
|
||||||
address, writeChar, frames, ConnectionOptions.WRITE_TYPE, 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<Segment> 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<Segment> 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<Segment> validate(byte[] img) {
|
|
||||||
List<Segment> 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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-24
@@ -1,24 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Callbacks from FaceclawFirmwareFlasher to the TypeScript layer. */
|
|
||||||
public interface FaceclawFirmwareFlasherListener {
|
|
||||||
void onLog(String line);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fine-grained progress for the UI bar. `lens` is "left"/"right";
|
|
||||||
* component/block indices are 1-based against their counts. `bytesSent` /
|
|
||||||
* `bytesTotal` cover the whole lens image, so a bar driven by them moves
|
|
||||||
* evenly even though the components differ wildly in size.
|
|
||||||
*/
|
|
||||||
void onProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount,
|
|
||||||
long bytesSent, long bytesTotal);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Coarse lifecycle. `state` is one of: validating, connecting, flashing,
|
|
||||||
* rebooting, done, error. `detail` carries the lens name or a message.
|
|
||||||
*/
|
|
||||||
void onState(String state, String detail);
|
|
||||||
|
|
||||||
/** Terminal callback: success or failure, with an explanatory message. */
|
|
||||||
void onComplete(boolean success, String detail);
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.channels.FileChannel;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Native helpers for the on-device custom-firmware build: SHA-256 hashing and
|
|
||||||
* binary file writing. Both take a java.nio.ByteBuffer so NativeScript can
|
|
||||||
* marshal a JS ArrayBuffer straight across without a per-element copy of the
|
|
||||||
* ~4 MB firmware image (and with no overload ambiguity). Also home of the one
|
|
||||||
* shared byte→hex encoder for com.faceclaw.app.
|
|
||||||
*/
|
|
||||||
public final class FaceclawFirmwareUtil {
|
|
||||||
private FaceclawFirmwareUtil() {}
|
|
||||||
|
|
||||||
/** Lowercase hex of the bytes. The TS-side decoder is case-insensitive. */
|
|
||||||
public static String bytesToHex(byte[] bytes) {
|
|
||||||
if (bytes == null || bytes.length == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
|
||||||
for (byte b : bytes) {
|
|
||||||
int v = b & 0xff;
|
|
||||||
sb.append(Character.forDigit(v >>> 4, 16));
|
|
||||||
sb.append(Character.forDigit(v & 0x0f, 16));
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Lowercase hex SHA-256 of the buffer's remaining bytes. */
|
|
||||||
public static String sha256Hex(ByteBuffer data) {
|
|
||||||
try {
|
|
||||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
||||||
md.update(data.duplicate());
|
|
||||||
return bytesToHex(md.digest());
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new RuntimeException("SHA-256 unavailable", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Write the buffer's remaining bytes to `path`, overwriting any existing file. */
|
|
||||||
public static void writeFile(String path, ByteBuffer data) {
|
|
||||||
try (FileOutputStream fos = new FileOutputStream(path);
|
|
||||||
FileChannel channel = fos.getChannel()) {
|
|
||||||
ByteBuffer view = data.duplicate();
|
|
||||||
while (view.hasRemaining()) {
|
|
||||||
channel.write(view);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("firmware write failed: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-546
@@ -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<String, CountDownLatch> pendingAcks = new ConcurrentHashMap<>();
|
|
||||||
private final ConcurrentHashMap<String, byte[]> 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<byte[]> frames = BleProtocol.framePb(payload, sid, flag, seq);
|
|
||||||
return bleManager.writeFrames(
|
|
||||||
address,
|
|
||||||
BleProtocol.WRITE_CHAR_UUID,
|
|
||||||
frames,
|
|
||||||
ConnectionOptions.WRITE_TYPE,
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+88
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Callbacks from FaceclawFlashPromptCommunicator to the TypeScript layer. */
|
|
||||||
public interface FaceclawFlashPromptListener {
|
|
||||||
void onLog(String line);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lifecycle updates. `state` is one of: connecting, connected, prompting,
|
|
||||||
* battery, result, cancelled, timeout, disconnected, error. `detail` carries the
|
|
||||||
* "approved"/"declined" text for result, or an error/explanation message.
|
|
||||||
*/
|
|
||||||
void onState(String state, String detail);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Battery percent of each arm, read after the user approved (or right
|
|
||||||
* after auth when the prompt is skipped); -1 when an arm didn't answer.
|
|
||||||
* Always fires before onResult(true).
|
|
||||||
*/
|
|
||||||
void onBattery(int rightPercent, int leftPercent);
|
|
||||||
|
|
||||||
/** Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true = flash, false = cancel. */
|
|
||||||
void onResult(boolean approved);
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Receives IMU (accelerometer) readings decoded from the glasses' sys-event
|
|
||||||
* stream. Registered on FaceclawBleCommunicator via addImuListener; callbacks
|
|
||||||
* are delivered on the Android main thread.
|
|
||||||
*/
|
|
||||||
public interface FaceclawImuListener {
|
|
||||||
void onImuData(double x, double y, double z, int eventSource);
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Callbacks for one on-phone LLM generation. Like the other bridge listeners,
|
|
||||||
* implementations are typically created on the NativeScript JS thread and
|
|
||||||
* FaceclawLlamaRunner posts these callbacks back to that thread's Looper.
|
|
||||||
*/
|
|
||||||
public interface FaceclawLlamaListener {
|
|
||||||
/** A piece of generated text (may be a partial word). */
|
|
||||||
void onToken(String piece);
|
|
||||||
|
|
||||||
/** Generation finished: "stop" (EOG), "length", or "cancelled". */
|
|
||||||
void onDone(String stopReason);
|
|
||||||
|
|
||||||
void onError(String message);
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** One-shot Android location callbacks for the TypeScript Weather bridge. */
|
|
||||||
public interface FaceclawLocationListener {
|
|
||||||
void onLocation(double latitude, double longitude, float accuracyMeters, long timestampMs);
|
|
||||||
|
|
||||||
void onError(String message);
|
|
||||||
}
|
|
||||||
@@ -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<String> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Continuous location callbacks for the TypeScript navigation bridge. */
|
|
||||||
public interface FaceclawLocationTrackerListener {
|
|
||||||
/**
|
|
||||||
* A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them
|
|
||||||
* (common when stationary); accuracyMeters is -1 when unknown.
|
|
||||||
*/
|
|
||||||
void onLocation(double latitude, double longitude, float accuracyMeters,
|
|
||||||
float bearingDeg, float speedMps, long timestampMs);
|
|
||||||
|
|
||||||
void onError(String message);
|
|
||||||
}
|
|
||||||
@@ -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<ResolveInfo> 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<JSONObject> 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<MediaBrowser.MediaItem> 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<MediaBrowser.MediaItem> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<ResolveInfo>
|
||||||
|
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<JSONObject> = 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<MediaBrowser>(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<MediaBrowser.MediaItem>) {
|
||||||
|
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<MediaBrowser.MediaItem>?): 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-10
@@ -1,10 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawMediaBrowserListener {
|
|
||||||
void onConnectResult(int requestId, boolean connected, String rootId, String error);
|
|
||||||
|
|
||||||
void onBrowseResult(int requestId, String childrenJson, String error);
|
|
||||||
|
|
||||||
/** The service connection dropped after connecting (player crashed or was killed). */
|
|
||||||
void onDisconnected();
|
|
||||||
}
|
|
||||||
@@ -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<MediaSession.QueueItem> 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<String> 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<String> 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<MediaSession.QueueItem> 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<MediaController> 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<MediaController> 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<MediaController> controllers) {
|
|
||||||
FaceclawMediaControllerListener currentListener = listener;
|
|
||||||
if (currentListener == null || controllers == null) return;
|
|
||||||
JSONArray apps = new JSONArray();
|
|
||||||
Set<String> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<MediaSession.QueueItem>?) {
|
||||||
|
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<String> = 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<String> = 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<MediaSession.QueueItem>? = 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<MediaController>?) {
|
||||||
|
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>?): 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<MediaController>?) {
|
||||||
|
val currentListener = listener
|
||||||
|
if (currentListener == null || controllers == null) return
|
||||||
|
val apps = JSONArray()
|
||||||
|
val seen: MutableSet<String> = 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 ?: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawMediaControllerListener {
|
|
||||||
/** All observed session owners, before filtering or choosing the active player. */
|
|
||||||
void onSessionAppsChanged(String appsJson);
|
|
||||||
|
|
||||||
void onStateChange(
|
|
||||||
String playbackState,
|
|
||||||
String packageName,
|
|
||||||
String appName,
|
|
||||||
String title,
|
|
||||||
String artist,
|
|
||||||
String album,
|
|
||||||
long positionMs,
|
|
||||||
long durationMs,
|
|
||||||
float playbackSpeed,
|
|
||||||
boolean canPlayPause,
|
|
||||||
boolean canSkipNext,
|
|
||||||
boolean canSkipPrevious,
|
|
||||||
boolean accessEnabled,
|
|
||||||
String status
|
|
||||||
);
|
|
||||||
}
|
|
||||||
-657
@@ -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<FaceclawNotificationListener> notificationListeners = new CopyOnWriteArraySet<>();
|
|
||||||
private static final Set<String> 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<String> 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<StatusBarNotification>() {
|
|
||||||
@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
|
|
||||||
* <externalFilesDir>/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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+662
@@ -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<FaceclawNotificationListener> = CopyOnWriteArraySet()
|
||||||
|
private val activeNotificationWakeKeys: MutableSet<String> = 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<StatusBarNotification?>?
|
||||||
|
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<StatusBarNotification?>?
|
||||||
|
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<String> = 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<StatusBarNotification?>?
|
||||||
|
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<StatusBarNotification?> {
|
||||||
|
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<Notification.Action>? = 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<StatusBarNotification?>?
|
||||||
|
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
|
||||||
|
* <externalFilesDir>/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<StatusBarNotification?>?
|
||||||
|
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<CharSequence>? = 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.Action?>? = 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawMicStatusListener {
|
|
||||||
/**
|
|
||||||
* A CFW mic_control field-104 status record from one temple. body is the
|
|
||||||
* raw 21-byte ['M','C',ver,...] record (decoded on the TS side); arm is
|
|
||||||
* "L" or "R" for the temple's own BLE link, "?" if unmatched.
|
|
||||||
*/
|
|
||||||
void onMicStatus(byte[] body, String arm);
|
|
||||||
}
|
|
||||||
@@ -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 "<dest>.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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Progress callbacks for FaceclawModelDownloader, posted to the creating thread's Looper. */
|
|
||||||
public interface FaceclawModelDownloaderListener {
|
|
||||||
void onProgress(long bytesDownloaded, long totalBytes);
|
|
||||||
|
|
||||||
/** The file is fully downloaded, verified, and moved to its final path. */
|
|
||||||
void onDone(String path);
|
|
||||||
|
|
||||||
void onError(String message);
|
|
||||||
}
|
|
||||||
@@ -1,266 +0,0 @@
|
|||||||
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;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Phone-side noise suppression for the microphone stream.
|
|
||||||
*
|
|
||||||
* Android's built-in noise cancellation (the {@link NoiseSuppressor} capture
|
|
||||||
* effect) only attaches to AudioRecord sessions on the phone's own microphones. Faceclaw's audio
|
|
||||||
* arrives as raw PCM over BLE from the glasses, which that effect chain never
|
|
||||||
* sees, so this class picks the best engine that can actually reach the
|
|
||||||
* samples:
|
|
||||||
*
|
|
||||||
* - Audio captured by the phone itself: {@link #attachPlatformEffects(int)}
|
|
||||||
* enables the device's built-in NS/AEC/AGC chain (hardware or OEM software,
|
|
||||||
* whatever the Android build ships) on the AudioRecord session.
|
|
||||||
* - Audio from the glasses (the normal case): {@link #process(byte[])} runs a
|
|
||||||
* WebRTC-NS-family spectral suppressor here on the phone — 16 ms sqrt-Hann
|
|
||||||
* frames at 50% overlap, per-bin asymmetric noise-floor tracking, and an
|
|
||||||
* over-subtracted Wiener gain with a residual floor so speech never pumps
|
|
||||||
* in and out of silence.
|
|
||||||
*/
|
|
||||||
public final class FaceclawNoiseSuppressor {
|
|
||||||
|
|
||||||
private static final int FFT_SIZE = 256;
|
|
||||||
private static final int HOP = FFT_SIZE / 2;
|
|
||||||
private static final float OVERSUBTRACTION = 1.6f;
|
|
||||||
/** About -16 dB of residual: keeps the noise bed natural instead of gated. */
|
|
||||||
private static final float GAIN_FLOOR = 0.15f;
|
|
||||||
private static final float GAIN_SMOOTHING = 0.6f;
|
|
||||||
/** Frames of pure noise-floor learning after start/reset (~100 ms). */
|
|
||||||
private static final int NOISE_WARMUP_FRAMES = 12;
|
|
||||||
|
|
||||||
private final int sampleRate;
|
|
||||||
private final float[] window = new float[FFT_SIZE];
|
|
||||||
private final float[] history = new float[FFT_SIZE];
|
|
||||||
private final float[] overlap = new float[HOP];
|
|
||||||
private final float[] noise = new float[FFT_SIZE / 2 + 1];
|
|
||||||
private final float[] gain = new float[FFT_SIZE / 2 + 1];
|
|
||||||
private final float[] re = new float[FFT_SIZE];
|
|
||||||
private final float[] im = new float[FFT_SIZE];
|
|
||||||
private final short[] pending = new short[HOP];
|
|
||||||
private int pendingCount = 0;
|
|
||||||
private int warmupFrames = 0;
|
|
||||||
|
|
||||||
public FaceclawNoiseSuppressor(int sampleRate) {
|
|
||||||
this.sampleRate = sampleRate;
|
|
||||||
// sqrt-Hann (sine) analysis+synthesis windows: exact COLA at 50% overlap.
|
|
||||||
for (int i = 0; i < FFT_SIZE; i++) {
|
|
||||||
window[i] = (float) Math.sin(Math.PI * (i + 0.5) / FFT_SIZE);
|
|
||||||
}
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSampleRate() {
|
|
||||||
return sampleRate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Forget the learned noise floor (e.g. after the suppressor was toggled off). */
|
|
||||||
public void reset() {
|
|
||||||
Arrays.fill(history, 0f);
|
|
||||||
Arrays.fill(overlap, 0f);
|
|
||||||
Arrays.fill(noise, 0f);
|
|
||||||
Arrays.fill(gain, 1f);
|
|
||||||
pendingCount = 0;
|
|
||||||
warmupFrames = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Suppress noise in 16-bit little-endian mono PCM. Streaming: input not
|
|
||||||
* filling a whole 8 ms hop is carried into the next call, so the output
|
|
||||||
* may be up to one hop shorter than the input (and longer on a later
|
|
||||||
* call); total latency is one hop.
|
|
||||||
*/
|
|
||||||
public byte[] process(byte[] pcm16le) {
|
|
||||||
if (pcm16le == null || pcm16le.length < 2) {
|
|
||||||
return pcm16le;
|
|
||||||
}
|
|
||||||
int sampleCount = pcm16le.length / 2;
|
|
||||||
int hops = (pendingCount + sampleCount) / HOP;
|
|
||||||
byte[] out = new byte[hops * HOP * 2];
|
|
||||||
int outPos = 0;
|
|
||||||
int inPos = 0;
|
|
||||||
while (inPos < sampleCount) {
|
|
||||||
int take = Math.min(HOP - pendingCount, sampleCount - inPos);
|
|
||||||
for (int i = 0; i < take; i++) {
|
|
||||||
int lo = pcm16le[(inPos + i) * 2] & 0xff;
|
|
||||||
int hi = pcm16le[(inPos + i) * 2 + 1];
|
|
||||||
pending[pendingCount + i] = (short) ((hi << 8) | lo);
|
|
||||||
}
|
|
||||||
pendingCount += take;
|
|
||||||
inPos += take;
|
|
||||||
if (pendingCount == HOP) {
|
|
||||||
outPos = processHop(out, outPos);
|
|
||||||
pendingCount = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int processHop(byte[] out, int outPos) {
|
|
||||||
System.arraycopy(history, HOP, history, 0, FFT_SIZE - HOP);
|
|
||||||
for (int i = 0; i < HOP; i++) {
|
|
||||||
history[FFT_SIZE - HOP + i] = pending[i] / 32768f;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < FFT_SIZE; i++) {
|
|
||||||
re[i] = history[i] * window[i];
|
|
||||||
im[i] = 0f;
|
|
||||||
}
|
|
||||||
fft(re, im, false);
|
|
||||||
|
|
||||||
boolean warming = warmupFrames < NOISE_WARMUP_FRAMES;
|
|
||||||
if (warming) {
|
|
||||||
warmupFrames++;
|
|
||||||
}
|
|
||||||
for (int b = 0; b <= FFT_SIZE / 2; b++) {
|
|
||||||
float power = re[b] * re[b] + im[b] * im[b];
|
|
||||||
if (warming) {
|
|
||||||
noise[b] = warmupFrames == 1 ? power : noise[b] + (power - noise[b]) / warmupFrames;
|
|
||||||
} else if (power < noise[b]) {
|
|
||||||
// Track the floor down quickly; drift up slowly so speech
|
|
||||||
// energy never gets learned as noise.
|
|
||||||
noise[b] += (power - noise[b]) * 0.2f;
|
|
||||||
} else {
|
|
||||||
noise[b] = Math.min(power, noise[b] * 1.006f + 1e-10f);
|
|
||||||
}
|
|
||||||
float clean = power - OVERSUBTRACTION * noise[b];
|
|
||||||
float g = clean > 0f ? clean / power : 0f;
|
|
||||||
if (g < GAIN_FLOOR) {
|
|
||||||
g = GAIN_FLOOR;
|
|
||||||
}
|
|
||||||
g = GAIN_SMOOTHING * gain[b] + (1f - GAIN_SMOOTHING) * g;
|
|
||||||
gain[b] = g;
|
|
||||||
re[b] *= g;
|
|
||||||
im[b] *= g;
|
|
||||||
if (b > 0 && b < FFT_SIZE / 2) {
|
|
||||||
re[FFT_SIZE - b] *= g;
|
|
||||||
im[FFT_SIZE - b] *= g;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fft(re, im, true);
|
|
||||||
for (int i = 0; i < HOP; i++) {
|
|
||||||
float value = (overlap[i] + re[i] * window[i]) * 32767f;
|
|
||||||
int sample = Math.round(value);
|
|
||||||
if (sample > 32767) {
|
|
||||||
sample = 32767;
|
|
||||||
}
|
|
||||||
if (sample < -32768) {
|
|
||||||
sample = -32768;
|
|
||||||
}
|
|
||||||
out[outPos + i * 2] = (byte) (sample & 0xff);
|
|
||||||
out[outPos + i * 2 + 1] = (byte) ((sample >> 8) & 0xff);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < HOP; i++) {
|
|
||||||
overlap[i] = re[HOP + i] * window[HOP + i];
|
|
||||||
}
|
|
||||||
return outPos + HOP * 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** In-place iterative radix-2 FFT (inverse includes the 1/N scale). */
|
|
||||||
private static void fft(float[] re, float[] im, boolean inverse) {
|
|
||||||
int n = re.length;
|
|
||||||
for (int i = 1, j = 0; i < n; i++) {
|
|
||||||
int bit = n >> 1;
|
|
||||||
for (; (j & bit) != 0; bit >>= 1) {
|
|
||||||
j ^= bit;
|
|
||||||
}
|
|
||||||
j ^= bit;
|
|
||||||
if (i < j) {
|
|
||||||
float t = re[i];
|
|
||||||
re[i] = re[j];
|
|
||||||
re[j] = t;
|
|
||||||
t = im[i];
|
|
||||||
im[i] = im[j];
|
|
||||||
im[j] = t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int len = 2; len <= n; len <<= 1) {
|
|
||||||
double ang = 2 * Math.PI / len * (inverse ? 1 : -1);
|
|
||||||
float wRe = (float) Math.cos(ang);
|
|
||||||
float wIm = (float) Math.sin(ang);
|
|
||||||
for (int i = 0; i < n; i += len) {
|
|
||||||
float curRe = 1f;
|
|
||||||
float curIm = 0f;
|
|
||||||
for (int k = 0; k < len / 2; k++) {
|
|
||||||
int a = i + k;
|
|
||||||
int b = a + len / 2;
|
|
||||||
float xr = re[b] * curRe - im[b] * curIm;
|
|
||||||
float xi = re[b] * curIm + im[b] * curRe;
|
|
||||||
re[b] = re[a] - xr;
|
|
||||||
im[b] = im[a] - xi;
|
|
||||||
re[a] += xr;
|
|
||||||
im[a] += xi;
|
|
||||||
float nextRe = curRe * wRe - curIm * wIm;
|
|
||||||
curIm = curRe * wIm + curIm * wRe;
|
|
||||||
curRe = nextRe;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (inverse) {
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
re[i] /= n;
|
|
||||||
im[i] /= n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 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<AudioEffect> 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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
public interface FaceclawNotificationListener {
|
|
||||||
void onNotificationPosted(String key);
|
|
||||||
}
|
|
||||||
@@ -1,190 +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, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<Barcode>() {
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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> { 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())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.faceclaw.app;
|
|
||||||
|
|
||||||
/** Callbacks for one FaceclawQrScanner.scan; exactly one method fires. */
|
|
||||||
public interface FaceclawQrScannerListener {
|
|
||||||
void onResult(String text);
|
|
||||||
|
|
||||||
void onCancelled();
|
|
||||||
|
|
||||||
void onError(String message);
|
|
||||||
}
|
|
||||||
@@ -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<InterfaceAddress>()
|
||||||
|
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()
|
||||||
|
}
|
||||||
Loaded 100 of 822 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user