diff --git a/app-common/src/main/java/eu/darken/capod/pods/core/apple/ApplePodsFactory.kt b/app-common/src/main/java/eu/darken/capod/pods/core/apple/ApplePodsFactory.kt index 3fcbfbf4..a3ed4a86 100644 --- a/app-common/src/main/java/eu/darken/capod/pods/core/apple/ApplePodsFactory.kt +++ b/app-common/src/main/java/eu/darken/capod/pods/core/apple/ApplePodsFactory.kt @@ -2,6 +2,7 @@ package eu.darken.capod.pods.core.apple import eu.darken.capod.common.bluetooth.BleScanResult import eu.darken.capod.common.debug.logging.Logging.Priority.VERBOSE +import eu.darken.capod.common.debug.logging.Logging.Priority.WARN import eu.darken.capod.common.debug.logging.log import eu.darken.capod.common.lowerNibble import eu.darken.capod.common.upperNibble @@ -66,7 +67,7 @@ abstract class ApplePodsFactory(private val tag: String) { override fun toString(): String = "KnownDevice(history=${history.size}, last=${history.last()})" companion object { - const val MAX_HISTORY = 10 + const val MAX_HISTORY = 20 } } @@ -88,6 +89,22 @@ abstract class ApplePodsFactory(private val tag: String) { ?.caseLidState } + open fun historyTrimmer( + pods: List + ): List { + var trimmed = pods.takeLast(KnownDevice.MAX_HISTORY) + + // We want to keep the case information + // If both pods are removed, and produce more history, we otherwise lose the case battery info. + val caseInfoFat = pods.lastOrNull { it is HasCase && it.batteryCasePercent != null } + val caseInfoTrimmed = trimmed.lastOrNull { it is HasCase && it.batteryCasePercent != null } + if (caseInfoFat != null && caseInfoTrimmed == null) { + trimmed = listOf(caseInfoFat) + trimmed.takeLast(KnownDevice.MAX_HISTORY - 1) + } + + return trimmed + } + internal open fun searchHistory(current: PodType): KnownDevice? { val scanResult = current.scanResult val message = current.proximityMessage @@ -103,7 +120,7 @@ abstract class ApplePodsFactory(private val tag: String) { .filter { it.history.size > KnownDevice.MAX_HISTORY } .toList() .forEach { - knownDevices[it.id] = it.copy(history = it.history.takeLast(KnownDevice.MAX_HISTORY)) + knownDevices[it.id] = it.copy(history = historyTrimmer(it.history)) } var recognizedDevice: KnownDevice? = knownDevices.values @@ -118,7 +135,7 @@ abstract class ApplePodsFactory(private val tag: String) { } if (recognizedDevice == null) { - log(tag) { "searchHistory1: Didn't recognize: $message" } + log(tag, WARN) { "searchHistory1: Didn't recognize: $message" } } return recognizedDevice diff --git a/app-common/src/test/java/eu/darken/capod/pods/core/apple/airpods/AirPodsProTest.kt b/app-common/src/test/java/eu/darken/capod/pods/core/apple/airpods/AirPodsProTest.kt index 4f5bf513..40645274 100644 --- a/app-common/src/test/java/eu/darken/capod/pods/core/apple/airpods/AirPodsProTest.kt +++ b/app-common/src/test/java/eu/darken/capod/pods/core/apple/airpods/AirPodsProTest.kt @@ -170,4 +170,19 @@ class AirPodsProTest : BaseAirPodsTest() { batteryCasePercent shouldBe 0.6f } } + + @Test + fun `left pod has no data`() = runBlockingTest { + create("07 19 01 0E 20 0B F9 8F 03 00 05 5B 59 67 4C F7 F3 EF 01 BA F4 92 1B 26 E4 90 40") { + rawPodsBattery shouldBe 0xF9.toUByte() + + batteryLeftPodPercent shouldBe null + batteryRightPodPercent shouldBe 0.9f + + isCaseCharging shouldBe false + isRightPodCharging shouldBe false + isLeftPodCharging shouldBe false + batteryCasePercent shouldBe null + } + } } \ No newline at end of file