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 @@ -78,6 +78,7 @@ class DeviceMonitor @Inject constructor(
profileKeyState = profile.toBleKeyState(),
profileLearnedAllowOffEnabled = (profile as? AppleDeviceProfile)?.learnedAllowOffEnabled,
profileLastRequestedListeningModeCycleMask = (profile as? AppleDeviceProfile)?.lastRequestedListeningModeCycleMask,
profileLearnedAdaptiveAudioNoiseLevel = (profile as? AppleDeviceProfile)?.learnedAdaptiveAudioNoiseLevel,
reactions = profile.toReactionConfig(),
batteryEstimateEnabled = (profile as? AppleDeviceProfile)?.batteryEstimateEnabled ?: true,
isSystemConnected = profile?.address in connectedAddresses,
Expand Down Expand Up @@ -115,6 +116,7 @@ class DeviceMonitor @Inject constructor(
profileKeyState = profile.toBleKeyState(),
profileLearnedAllowOffEnabled = (profile as? AppleDeviceProfile)?.learnedAllowOffEnabled,
profileLastRequestedListeningModeCycleMask = (profile as? AppleDeviceProfile)?.lastRequestedListeningModeCycleMask,
profileLearnedAdaptiveAudioNoiseLevel = (profile as? AppleDeviceProfile)?.learnedAdaptiveAudioNoiseLevel,
reactions = profile.toReactionConfig(),
batteryEstimateEnabled = (profile as? AppleDeviceProfile)?.batteryEstimateEnabled ?: true,
isSystemConnected = profile.address in state.connectedAddresses,
Expand Down Expand Up @@ -182,6 +184,7 @@ class DeviceMonitor @Inject constructor(
profileKeyState = profile.toBleKeyState(),
profileLearnedAllowOffEnabled = (profile as? AppleDeviceProfile)?.learnedAllowOffEnabled,
profileLastRequestedListeningModeCycleMask = (profile as? AppleDeviceProfile)?.lastRequestedListeningModeCycleMask,
profileLearnedAdaptiveAudioNoiseLevel = (profile as? AppleDeviceProfile)?.learnedAdaptiveAudioNoiseLevel,
reactions = profile.toReactionConfig(),
batteryEstimateEnabled = (profile as? AppleDeviceProfile)?.batteryEstimateEnabled ?: true,
isSystemConnected = profile.address in connectedAddresses,
Expand Down Expand Up @@ -290,6 +293,7 @@ class DeviceMonitor @Inject constructor(
profileKeyState = profile.toBleKeyState(),
profileLearnedAllowOffEnabled = (profile as? AppleDeviceProfile)?.learnedAllowOffEnabled,
profileLastRequestedListeningModeCycleMask = (profile as? AppleDeviceProfile)?.lastRequestedListeningModeCycleMask,
profileLearnedAdaptiveAudioNoiseLevel = (profile as? AppleDeviceProfile)?.learnedAdaptiveAudioNoiseLevel,
reactions = profile.toReactionConfig(),
batteryEstimateEnabled = (profile as? AppleDeviceProfile)?.batteryEstimateEnabled ?: true,
)
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/eu/darken/capod/monitor/core/PodDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ data class PodDevice(
* the live AAP state has no ListeningModeCycle setting (device never echoes 0x1A back).
*/
internal val profileLastRequestedListeningModeCycleMask: Int? = null,
/**
* Last-known AdaptiveAudioNoise level persisted on the profile. Used as a fallback when
* the live AAP state has no AdaptiveAudioNoise setting (device never echoes 0x2E back).
*/
internal val profileLearnedAdaptiveAudioNoiseLevel: Int? = null,
/** Reaction toggle snapshot from the profile. Defaults to all-off when no profile is matched. */
val reactions: ReactionConfig = ReactionConfig(),
/** Whether the dashboard battery time-remaining estimate is enabled for this device (per-profile). */
Expand Down Expand Up @@ -363,6 +368,7 @@ data class PodDevice(

val adaptiveAudioNoise: AapSetting.AdaptiveAudioNoise?
get() = aap?.setting()
?: profileLearnedAdaptiveAudioNoiseLevel?.let { AapSetting.AdaptiveAudioNoise(level = it) }

val pressSpeed: AapSetting.PressSpeed?
get() = aap?.setting()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import javax.inject.Inject
import javax.inject.Singleton

/**
* Persists the learned AllowOffOption and ListeningModeCycle values to [AppleDeviceProfile]
* whenever they change in AAP state. AAP state is dropped on disconnect and neither setting
* is proactively echoed by the device, so without persistence every reconnect would force the
* UI back to defaults (OFF hidden, cycle mask 0x0E).
* Persists the learned AllowOffOption, ListeningModeCycle and AdaptiveAudioNoise values to
* [AppleDeviceProfile] whenever they change in AAP state. AAP state is dropped on disconnect and
* none of these settings are proactively echoed by the device, so without persistence every
* reconnect would force the UI back to defaults (OFF hidden, cycle mask 0x0E, default noise level).
*/
@Singleton
class AapLearnedSettingsPersister @Inject constructor(
Expand All @@ -30,30 +30,35 @@ class AapLearnedSettingsPersister @Inject constructor(
private data class LearnedSnapshot(
val allowOffEnabled: Boolean?,
val cycleMask: Int?,
val adaptiveNoiseLevel: Int?,
)

fun monitor(): Flow<Unit> = aapManager.allStates
.map { states -> states.mapValues { (_, state) -> state.snapshot() } }
.distinctUntilChanged()
.onEach { addressToSnapshot ->
addressToSnapshot.forEach { (address, snapshot) ->
if (snapshot.allowOffEnabled == null && snapshot.cycleMask == null) return@forEach
if (snapshot.allowOffEnabled == null && snapshot.cycleMask == null && snapshot.adaptiveNoiseLevel == null) return@forEach
val profile = profilesRepo.profiles.first()
.filterIsInstance<AppleDeviceProfile>()
.firstOrNull { it.address == address } ?: return@forEach
val allowOffChanged = snapshot.allowOffEnabled != null &&
profile.learnedAllowOffEnabled != snapshot.allowOffEnabled
val cycleChanged = snapshot.cycleMask != null &&
profile.lastRequestedListeningModeCycleMask != snapshot.cycleMask
if (!allowOffChanged && !cycleChanged) return@forEach
val levelChanged = snapshot.adaptiveNoiseLevel != null &&
profile.learnedAdaptiveAudioNoiseLevel != snapshot.adaptiveNoiseLevel
if (!allowOffChanged && !cycleChanged && !levelChanged) return@forEach
profilesRepo.updateAppleProfile(profile.id) {
it.copy(
learnedAllowOffEnabled = if (allowOffChanged) snapshot.allowOffEnabled else it.learnedAllowOffEnabled,
lastRequestedListeningModeCycleMask = if (cycleChanged) snapshot.cycleMask else it.lastRequestedListeningModeCycleMask,
learnedAdaptiveAudioNoiseLevel = if (levelChanged) snapshot.adaptiveNoiseLevel else it.learnedAdaptiveAudioNoiseLevel,
)
}
if (allowOffChanged) log(TAG) { "Persisted learnedAllowOffEnabled=${snapshot.allowOffEnabled} for $address" }
if (cycleChanged) log(TAG) { "Persisted lastRequestedListeningModeCycleMask=0x%02X for $address".format(snapshot.cycleMask) }
if (levelChanged) log(TAG) { "Persisted learnedAdaptiveAudioNoiseLevel=${snapshot.adaptiveNoiseLevel} for $address" }
}
}
.map { }
Expand All @@ -62,6 +67,7 @@ class AapLearnedSettingsPersister @Inject constructor(
private fun AapPodState.snapshot(): LearnedSnapshot = LearnedSnapshot(
allowOffEnabled = setting<AapSetting.AllowOffOption>()?.enabled,
cycleMask = setting<AapSetting.ListeningModeCycle>()?.modeMask,
adaptiveNoiseLevel = setting<AapSetting.AdaptiveAudioNoise>()?.level,
)

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ data class AppleDeviceProfile(
* UI to the default 0x0E (no OFF bit) even if the real cycle on-device includes OFF.
*/
@SerialName("learnedListeningModeCycleMask") val lastRequestedListeningModeCycleMask: Int? = null,
/**
* Last-known device-side AdaptiveAudioNoise level (AAP setting 0x2E). Push-only like the
* other learned settings above, so without persistence every reconnect resets the UI to
* whatever the device happens to push instead of the user's last chosen level.
*/
@SerialName("learnedAdaptiveAudioNoiseLevel") val learnedAdaptiveAudioNoiseLevel: Int? = null,
@SerialName("stemActions") val stemActions: StemActionsConfig = StemActionsConfig(),
) : DeviceProfile, HasReactionConfig {

Expand Down Expand Up @@ -97,6 +103,7 @@ data class AppleDeviceProfile(
"batteryEstimateEnabled=$batteryEstimateEnabled, " +
"learnedAllowOffEnabled=$learnedAllowOffEnabled, " +
"lastRequestedListeningModeCycleMask=$lastRequestedListeningModeCycleMask, " +
"learnedAdaptiveAudioNoiseLevel=$learnedAdaptiveAudioNoiseLevel, " +
"stemActions=$stemActions" +
")"
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,30 @@ class PodDeviceAncModeTest : BaseTest() {
)
device.listeningModeCycle?.modeMask shouldBe 0x0A
}

@Test
fun `profile-learned AdaptiveAudioNoise level is used when AAP state has none`() {
val device = PodDevice(
profileId = null,
ble = null,
aap = AapPodState(settings = emptyMap()),
profileModel = PodModel.AIRPODS_PRO,
profileLearnedAdaptiveAudioNoiseLevel = 42,
)
device.adaptiveAudioNoise?.level shouldBe 42
}

@Test
fun `live AAP AdaptiveAudioNoise overrides profile fallback`() {
val device = PodDevice(
profileId = null,
ble = null,
aap = AapPodState(
settings = mapOf(AapSetting.AdaptiveAudioNoise::class to AapSetting.AdaptiveAudioNoise(level = 75)),
),
profileModel = PodModel.AIRPODS_PRO,
profileLearnedAdaptiveAudioNoiseLevel = 42,
)
device.adaptiveAudioNoise?.level shouldBe 75
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ class AapLearnedSettingsPersisterTest : BaseTest() {
private fun stateWithSettings(
allowOffEnabled: Boolean? = null,
cycleMask: Int? = null,
adaptiveNoiseLevel: Int? = null,
): AapPodState {
val settings = buildMap<kotlin.reflect.KClass<out AapSetting>, AapSetting> {
allowOffEnabled?.let { put(AapSetting.AllowOffOption::class, AapSetting.AllowOffOption(it)) }
cycleMask?.let { put(AapSetting.ListeningModeCycle::class, AapSetting.ListeningModeCycle(it)) }
adaptiveNoiseLevel?.let { put(AapSetting.AdaptiveAudioNoise::class, AapSetting.AdaptiveAudioNoise(it)) }
}
return AapPodState(settings = settings)
}
Expand Down Expand Up @@ -96,6 +98,7 @@ class AapLearnedSettingsPersisterTest : BaseTest() {
val profileWithStored = testProfile.copy(
learnedAllowOffEnabled = true,
lastRequestedListeningModeCycleMask = 0x0F,
learnedAdaptiveAudioNoiseLevel = 50,
)
val profilesRepo = mockk<DeviceProfilesRepo>(relaxUnitFun = true) {
every { profiles } returns flowOf(listOf<eu.darken.capod.profiles.core.DeviceProfile>(profileWithStored))
Expand All @@ -104,11 +107,37 @@ class AapLearnedSettingsPersisterTest : BaseTest() {

val job = launch { persister.monitor().collect {} }

allStates.value = mapOf(testAddress to stateWithSettings(allowOffEnabled = true, cycleMask = 0x0F))
allStates.value = mapOf(
testAddress to stateWithSettings(allowOffEnabled = true, cycleMask = 0x0F, adaptiveNoiseLevel = 50),
)
advanceUntilIdle()

coVerify(exactly = 0) { profilesRepo.updateAppleProfile(any(), any()) }

job.cancel()
}

@Test
fun `persists AdaptiveAudioNoise level to matching profile`() = runTest(UnconfinedTestDispatcher()) {
val allStates = MutableStateFlow<Map<String, AapPodState>>(emptyMap())
val aapManager = mockk<AapConnectionManager>(relaxed = true) {
every { this@mockk.allStates } returns allStates
}
val profilesRepo = mockk<DeviceProfilesRepo>(relaxUnitFun = true) {
every { profiles } returns flowOf(listOf<eu.darken.capod.profiles.core.DeviceProfile>(testProfile))
}
val persister = AapLearnedSettingsPersister(aapManager, profilesRepo)

val job = launch { persister.monitor().collect {} }

allStates.value = mapOf(testAddress to stateWithSettings(adaptiveNoiseLevel = 75))
advanceUntilIdle()

val transform = slot<(AppleDeviceProfile) -> AppleDeviceProfile>()
coVerify { profilesRepo.updateAppleProfile(eq(testProfile.id), capture(transform)) }
val updated = transform.captured(testProfile)
assert(updated.learnedAdaptiveAudioNoiseLevel == 75) { "Expected learnedAdaptiveAudioNoiseLevel=75" }

job.cancel()
}
}