Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(android): fixes from the audit of the phone app

Bee1b6634ce0492
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt b/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt
index cf7c469..11ed4b6 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt
@@ -16,8 +16,7 @@ import kotlinx.coroutines.flow.asStateFlow
/**
* Plays live PCM-16LE audio frames streamed from a paired PC (`pc.audio.relay`)
- * through the phone's full media DSP pipeline (`PERFORMANCE_MODE_NONE`) so OEM
- * speaker tuning / Dolby Atmos / bass enhancement remain active, while a
+ * on a low-latency output (`PERFORMANCE_MODE_LOW_LATENCY`), while a
* dedicated `THREAD_PRIORITY_URGENT_AUDIO` worker maintains a tight ~35–45 ms
* jitter cushion without ever calling `pause()`/`flush()` mid-stream.
*/
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.kt b/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.kt
index 5e540a7..47b0473 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.kt
@@ -66,6 +66,8 @@ data class QuadCorners(
*/
object DocumentScannerProcessor {
private const val MAX_WORK_SIDE = 180
+ /** The detector works at [MAX_WORK_SIDE]; a bigger copy than this adds nothing. */
+ private const val DETECT_MAX_EDGE = 720
private const val MIN_QUAD_AREA_FRACTION = 0.15f
private const val MIN_INTERIOR_ANGLE_DEG = 45f
private const val MAX_INTERIOR_ANGLE_DEG = 135f
@@ -549,11 +551,26 @@ object DocumentScannerProcessor {
/** Detects a document quad directly from an Android [Bitmap]. */
fun detectDocumentQuad(bitmap: Bitmap): QuadCorners {
- val w = bitmap.width
- val h = bitmap.height
- if (w < 16 || h < 16) return DEFAULT_QUAD
+ if (bitmap.width < 16 || bitmap.height < 16) return DEFAULT_QUAD
+ // The corners come back as fractions of the picture, so a smaller copy
+ // finds the same ones without a full-size photo's worth of arrays.
+ val longest = max(bitmap.width, bitmap.height)
+ val source = if (longest > DETECT_MAX_EDGE) {
+ val scale = DETECT_MAX_EDGE.toFloat() / longest
+ Bitmap.createScaledBitmap(
+ bitmap,
+ (bitmap.width * scale).roundToInt().coerceAtLeast(16),
+ (bitmap.height * scale).roundToInt().coerceAtLeast(16),
+ true,
+ )
+ } else {
+ bitmap
+ }
+ val w = source.width
+ val h = source.height
val pixels = IntArray(w * h)
- bitmap.getPixels(pixels, 0, w, 0, 0, w, h)
+ source.getPixels(pixels, 0, w, 0, 0, w, h)
+ if (source !== bitmap) source.recycle()
val luma = ByteArray(w * h) { i ->
val c = pixels[i]
val r = c ushr 16 and 0xFF
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt
index 4e278d2..91e2275 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt
@@ -14,11 +14,12 @@ object AndroidContext {
fun install(context: Context) {
if (installed) return
- installed = true
+ // Marked only once it worked, so a failed load is tried again.
runCatching {
// The core's library, as JNA loads it for everything else: the same copy.
System.loadLibrary("nectarlink_ffi")
nativeInstall(context.applicationContext)
+ installed = true
}.onFailure { Log.w(TAG, "the core can't read the phone's DNS servers", it) }
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
index e62ebee..67e0fca 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
@@ -1829,7 +1829,7 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener
_messages.tryEmit(msg)
}
}
- TimelineKind.FILE, TimelineKind.PHOTO, TimelineKind.RECORDING -> {
+ TimelineKind.FILE, TimelineKind.PHOTO -> {
val target = entry.target
when {
target.startsWith("content://") -> sendFiles(entry.deviceId, listOf(Uri.parse(target)))
@@ -1987,7 +1987,14 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener
else PairingState.Failed(PairingFailure.Declined),
)
}
- scope.launch(Dispatchers.IO) { runCatching { node?.pairingConfirm(matches) } }
+ scope.launch(Dispatchers.IO) {
+ // The result comes as an event; with no answer sent there won't be one.
+ val sent = runCatching { node?.pairingConfirm(matches) != null }.getOrDefault(false)
+ if (matches && !sent) {
+ val failed = PairingState.Failed(PairingFailure.Other("the answer didn't reach the core"))
+ _state.update { s -> if (s.pairing is PairingState.Confirmed) s.copy(pairing = failed) else s }
+ }
+ }
}
/** Keeps local discovery working while the pairing screen is open. */
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mic/MicRequests.kt b/android/app/src/main/kotlin/app/nectarlink/android/mic/MicRequests.kt
index d7fad9b..9b45561 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/mic/MicRequests.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/mic/MicRequests.kt
@@ -80,7 +80,10 @@ class MicStartActivity : ComponentActivity() {
private var pcId: String? = null
private val permission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
- if (granted) pcId?.let { MicService.start(this, it) }
+ pcId?.let { pc ->
+ // Declined: the PC hears the microphone is off instead of waiting it out.
+ if (granted) MicService.start(this, pc) else (application as NectarlinkApplication).core.micStop(pc)
+ }
finish()
}
diff --git a/core/nectarlink-core/src/session.rs b/core/nectarlink-core/src/session.rs
index aca2b6f..8c4a8ce 100644
--- a/core/nectarlink-core/src/session.rs
+++ b/core/nectarlink-core/src/session.rs
@@ -579,7 +579,11 @@ async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: Envelope) ->
}
}
types::AUDIO_PCM => {
- if let Ok(frame) = env.body::<AudioPcmFrame>() {
+ // A rate or layout out of range would play at the wrong speed: dropped.
+ if let Ok(frame) = env.body::<AudioPcmFrame>()
+ && (8_000..=96_000).contains(&frame.sample_rate)
+ && (1..=2).contains(&frame.channels)
+ {
shared.emit(NodeEvent::PcAudioFrame {
device: peer,
sample_rate: frame.sample_rate,