Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
fix(android): keep call controls on the active call (#59)
alexfoxtmc222bc4e330e
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/calls/CallCompanion.kt b/android/app/src/main/kotlin/app/nectarlink/android/calls/CallCompanion.ktindex 8e3610e..12f8c78 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/calls/CallCompanion.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/calls/CallCompanion.kt@@ -19,8 +19,13 @@ import app.nectarlink.core.CallControls* MANAGE_ONGOING_CALLS (Android 12+), an app-op Elevated's shell grants.*/class CallCompanion : InCallService() {- /** The call being controlled: the active one, or the latest. */- private var call: Call? = null+ /**+ * The call being controlled: the active one, else the held one, else the+ * latest. A second call waiting to be answered doesn't take over the one+ * in progress, so hanging up, holding and keys reach the call a PC shows.+ */+ private val call: Call?+ get() = calls.let { all -> all.getOrNull(controlledIndex(all.map { it.stateNow() })) }private val callback = object : Call.Callback() {override fun onStateChanged(call: Call, state: Int) = changed()@@ -39,13 +44,11 @@ class CallCompanion : InCallService() {override fun onCallAdded(call: Call) {call.registerCallback(callback)- this.call = callchanged()}override fun onCallRemoved(call: Call) {call.unregisterCallback(callback)- if (this.call === call) this.call = calls.lastOrNull { it !== call }changed()}@@ -157,6 +160,16 @@ class CallCompanion : InCallService() {}}+/**+ * Which of Telecom's calls, by their states in Telecom's order, a PC+ * controls: the last active one, else the last held one, else the last not+ * yet disconnected; -1 when there's none.+ */+internal fun controlledIndex(states: List<Int>): Int =+ states.indexOfLast { it == Call.STATE_ACTIVE }.takeIf { it >= 0 }+ ?: states.indexOfLast { it == Call.STATE_HOLDING }.takeIf { it >= 0 }+ ?: states.indexOfLast { it != Call.STATE_DISCONNECTED }+/** The call's state: `Details.getState` from Android 12, `getState` before. */private fun Call.stateNow(): Int =if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) details.state else @Suppress("DEPRECATION") statediff --git a/android/app/src/main/kotlin/app/nectarlink/android/calls/PhoneCalls.kt b/android/app/src/main/kotlin/app/nectarlink/android/calls/PhoneCalls.ktindex 0bf01b7..0d43ef8 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/calls/PhoneCalls.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/calls/PhoneCalls.kt@@ -115,6 +115,8 @@ internal class PhoneCalls(logObserver = nullmain.removeCallbacks(notifyLog)current = null+ // The IDLE broadcast that would unmute it no longer arrives.+ restoreRinger()}private fun stateChanged(state: String, number: String?) {diff --git a/android/app/src/test/kotlin/app/nectarlink/android/calls/CallCompanionTargetTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/calls/CallCompanionTargetTest.ktnew file mode 100644index 0000000..ee99659--- /dev/null+++ b/android/app/src/test/kotlin/app/nectarlink/android/calls/CallCompanionTargetTest.kt@@ -0,0 +1,21 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.calls++import android.telecom.Call+import org.junit.Assert.assertEquals+import org.junit.Test++class CallCompanionTargetTest {+ @Test+ fun waitingCallDoesNotTakeOverTheActiveOne() {+ assertEquals(0, controlledIndex(listOf(Call.STATE_ACTIVE, Call.STATE_RINGING)))+ assertEquals(0, controlledIndex(listOf(Call.STATE_HOLDING, Call.STATE_RINGING)))+ assertEquals(0, controlledIndex(listOf(Call.STATE_RINGING)))+ }++ @Test+ fun noCallToControlWhenAllAreGone() {+ assertEquals(-1, controlledIndex(emptyList()))+ assertEquals(-1, controlledIndex(listOf(Call.STATE_DISCONNECTED)))+ }+}