Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ fun DeviceSettingsScreenHost(
onNavigateUp = { vm.navUp() },
onAncModeChange = { vm.setAncMode(it) },
onConversationalAwarenessChange = { vm.setConversationalAwareness(it) },
onNcWithOneAirPodChange = { vm.setNcWithOneAirPod(it) },
onPersonalizedVolumeChange = { vm.setPersonalizedVolume(it) },
onToneVolumeChange = { vm.setToneVolume(it) },
onAdaptiveAudioNoiseChange = { vm.setAdaptiveAudioNoise(it) },
Expand Down Expand Up @@ -187,7 +186,6 @@ fun DeviceSettingsScreen(
onNavigateUp: () -> Unit,
onAncModeChange: (AapSetting.AncMode.Value) -> Unit = {},
onConversationalAwarenessChange: (Boolean) -> Unit = {},
onNcWithOneAirPodChange: (Boolean) -> Unit = {},
onPersonalizedVolumeChange: (Boolean) -> Unit = {},
onToneVolumeChange: (Int) -> Unit = {},
onAdaptiveAudioNoiseChange: (Int) -> Unit = {},
Expand Down Expand Up @@ -325,7 +323,8 @@ fun DeviceSettingsScreen(
if (features.hasDualPods) {
val onePodModeActive = reactions.autoPlay ||
reactions.autoPause ||
(reactions.autoConnect && reactions.autoConnectCondition == AutoConnectCondition.IN_EAR)
(reactions.autoConnect && reactions.autoConnectCondition == AutoConnectCondition.IN_EAR) ||
features.hasNcOneAirpod
SettingsBaseItem(
title = stringResource(R.string.settings_onepod_mode_label),
subtitle = stringResource(R.string.settings_onepod_mode_description),
Expand Down Expand Up @@ -450,7 +449,6 @@ fun DeviceSettingsScreen(

// ── Noise Control ────────────────────────────
val ancMode = device.ancMode
val ncOneAirpod = device.ncWithOneAirPod
val adaptiveNoise = device.adaptiveAudioNoise
if (features.hasAncControl && ancMode != null) {
val cycleMask = if (features.hasListeningModeCycle) {
Expand All @@ -471,7 +469,6 @@ fun DeviceSettingsScreen(
enabled = enabled,
)
val hasNoiseExtras = (features.hasAdaptiveAudioNoise && adaptiveNoise != null) ||
(features.hasNcOneAirpod && ncOneAirpod != null) ||
(!isPro && features.hasListeningModeCycle)
if (hasNoiseExtras) {
HorizontalDivider(
Expand All @@ -487,16 +484,6 @@ fun DeviceSettingsScreen(
isAdaptiveMode = ancMode.current == AapSetting.AncMode.Value.ADAPTIVE,
)
}
if (features.hasNcOneAirpod && ncOneAirpod != null) {
SettingsSwitchItem(
icon = Icons.TwoTone.Headphones,
title = stringResource(R.string.device_settings_nc_one_airpod_label),
subtitle = stringResource(R.string.device_settings_nc_one_airpod_description),
checked = ncOneAirpod.enabled,
onCheckedChange = onNcWithOneAirPodChange,
enabled = enabled,
)
}
if (!isPro && features.hasListeningModeCycle) {
SettingsBaseItem(
icon = Icons.TwoTone.Loop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ class DeviceSettingsViewModel @Inject constructor(

fun setConversationalAwareness(enabled: Boolean) = send(AapCommand.SetConversationalAwareness(enabled))

fun setNcWithOneAirPod(enabled: Boolean) = send(AapCommand.SetNcWithOneAirPod(enabled))

fun setPersonalizedVolume(enabled: Boolean) = send(AapCommand.SetPersonalizedVolume(enabled))

fun setToneVolume(level: Int) = send(AapCommand.SetToneVolume(level))
Expand Down Expand Up @@ -299,7 +297,14 @@ class DeviceSettingsViewModel @Inject constructor(

fun setOnePodMode(enabled: Boolean) {
log(TAG, INFO) { "setOnePodMode($enabled)" }
launch { updateProfileNow { it.copy(onePodMode = enabled) } }
launch {
updateProfileNow { it.copy(onePodMode = enabled) }
// Opportunistic immediate sync — NcOnePodSender handles deferred/reconnect
val device = deviceMonitor.getDeviceForProfile(targetProfileId.value ?: return@launch)
if (device != null && device.isAapConnected && device.model.features.hasNcOneAirpod) {
sendInternal(AapCommand.SetNcWithOneAirPod(enabled))
}
}
}

fun setAutoPlay(enabled: Boolean) = launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AapLifecycleManager @Inject constructor(
private val aapKeyPersister: AapKeyPersister,
private val stemConfigSender: StemConfigSender,
private val stemPressReaction: StemPressReaction,
private val ncOnePodSender: NcOnePodSender,
) {
fun start() {
log(TAG) { "start()" }
Expand All @@ -34,6 +35,7 @@ class AapLifecycleManager @Inject constructor(
aapKeyPersister.monitor(),
stemConfigSender.monitor(),
stemPressReaction.monitor(),
ncOnePodSender.monitor(),
)
.catch { e -> log(TAG, WARN) { "AAP lifecycle error: ${e.asLog()}" } }
.setupCommonEventHandlers(TAG) { "aapActive" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package eu.darken.capod.monitor.core.aap

import eu.darken.capod.common.debug.logging.Logging.Priority.WARN
import eu.darken.capod.common.debug.logging.log
import eu.darken.capod.common.debug.logging.logTag
import eu.darken.capod.common.flow.setupCommonEventHandlers
import eu.darken.capod.pods.core.apple.aap.AapConnectionManager
import eu.darken.capod.pods.core.apple.aap.AapPodState
import eu.darken.capod.pods.core.apple.aap.protocol.AapCommand
import eu.darken.capod.profiles.core.AppleDeviceProfile
import eu.darken.capod.profiles.core.DeviceProfilesRepo
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class NcOnePodSender @Inject constructor(
private val aapManager: AapConnectionManager,
private val profilesRepo: DeviceProfilesRepo,
) {
fun monitor(): Flow<Unit> = combine(
aapManager.allStates,
profilesRepo.profiles,
) { states, profiles ->
val appleProfiles = profiles.filterIsInstance<AppleDeviceProfile>()
states.entries
.filter { (_, s) -> s.connectionState == AapPodState.ConnectionState.READY }
.mapNotNull { (address, _) ->
val profile = appleProfiles.firstOrNull { it.address == address }
if (profile != null && profile.model.features.hasNcOneAirpod) {
address to profile.onePodMode
} else {
null
}
}
}
.distinctUntilChanged()
.onEach { commands ->
for ((address, enabled) in commands) {
try {
aapManager.sendCommand(address, AapCommand.SetNcWithOneAirPod(enabled))
log(TAG) { "Sent SetNcWithOneAirPod($enabled) to $address" }
} catch (e: Exception) {
log(TAG, WARN) { "SetNcWithOneAirPod send failed for $address: $e" }
}
}
}
.map { }
.setupCommonEventHandlers(TAG) { "ncOnePod" }

companion object {
private val TAG = logTag("Monitor", "NcOnePodSender")
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<string name="settings_compat_offloaded_batching_disabled_summary">Don\'t let the system group collected BLE data before forwarding it to us.</string>

<string name="settings_onepod_mode_label">One pod mode</string>
<string name="settings_onepod_mode_description">Treat a single in-ear pod as worn. Auto play/pause and &quot;In ear&quot; auto-connect react to either pod instead of requiring both.</string>
<string name="settings_onepod_mode_description">Auto play/pause, auto-connect, and on supported models noise cancellation, react to a single pod instead of requiring both.</string>
<string name="settings_popup_caseopen_label">Show case popup</string>
<string name="settings_popup_caseopen_description">Show a popup when the device case is opened (experimental).</string>
<string name="settings_popup_connected_label">Show connection popup</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -45,7 +44,6 @@ import testhelpers.coroutine.TestDispatcherProvider
import testhelpers.datastore.FakeDataStoreValue
import testhelpers.livedata.InstantExecutorExtension

@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(InstantExecutorExtension::class)
class DeviceSettingsViewModelTest : BaseTest() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
Expand All @@ -39,7 +38,6 @@ import testhelpers.coroutine.TestDispatcherProvider
import testhelpers.datastore.FakeDataStoreValue
import testhelpers.livedata.InstantExecutorExtension

@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(InstantExecutorExtension::class)
class OverviewViewModelTest : BaseTest() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -37,7 +36,6 @@ import testhelpers.BaseTest
import testhelpers.TestTimeSource
import java.time.Instant

@OptIn(ExperimentalCoroutinesApi::class)
class DeviceMonitorTest : BaseTest() {

private val testDispatcher = UnconfinedTestDispatcher()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
Expand All @@ -30,7 +29,6 @@ import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import testhelpers.BaseTest

@OptIn(ExperimentalCoroutinesApi::class)
class AapAutoConnectTest : BaseTest() {

private val testDispatcher = UnconfinedTestDispatcher()
Expand Down
Loading