From 5731f43a57d4f0f12cbc3b467395e76ace4a5654 Mon Sep 17 00:00:00 2001 From: darken Date: Tue, 18 Jan 2022 15:13:28 +0100 Subject: [PATCH] Improve lid state detection --- .../darken/capod/monitor/core/PodMonitor.kt | 3 ++- .../capod/pods/core/apple/DualApplePods.kt | 25 +++++++++++++------ .../capod/reaction/popup/PopUpReaction.kt | 9 +++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt b/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt index 99f413e4..dc8ed14d 100644 --- a/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt +++ b/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt @@ -75,7 +75,8 @@ class PodMonitor @Inject constructor( pods[it.identifier] = it } } - + val now = Instant.now() +// pods.values.sortedWith(compareBy { Duration.between(it.lastSeenAt,now) }.thenByDescending { it.rssi }) pods.values.sortedByDescending { it.rssi } } .onStart { emit(emptyList()) } diff --git a/app/src/main/java/eu/darken/capod/pods/core/apple/DualApplePods.kt b/app/src/main/java/eu/darken/capod/pods/core/apple/DualApplePods.kt index 77b3d674..7d67d8a6 100644 --- a/app/src/main/java/eu/darken/capod/pods/core/apple/DualApplePods.kt +++ b/app/src/main/java/eu/darken/capod/pods/core/apple/DualApplePods.kt @@ -1,6 +1,7 @@ package eu.darken.capod.pods.core.apple import android.content.Context +import android.util.Range import androidx.annotation.StringRes import eu.darken.capod.R import eu.darken.capod.common.debug.logging.log @@ -96,15 +97,25 @@ interface DualApplePods : ApplePods, HasDualPods, HasEarDetection, HasCase { get() = rawCaseBattery.upperNibble.isBitSet(2) val caseLidState: LidState - get() = LidState.values().firstOrNull { it.raw == rawCaseLidState } ?: LidState.UNKNOWN + get() { + return LidState.values().firstOrNull { it.rawRange.contains(rawCaseLidState) } ?: LidState.UNKNOWN + } - enum class LidState(val raw: UByte?) { - OPEN(0x31), - CLOSED(0x38), - NOT_IN_CASE(0x01), - UNKNOWN(null); + /** + * TODO this is glitchy + * The counters generally increase if quickly and repeatedly: + * - open/close + * - add/remove the last airpod to the case + * They reset after some time to their start values. + * The upper limits are not the maximums but are only reached if playing with the case. + */ + enum class LidState(val rawRange: Range) { + OPEN(0x30, 0x37), + CLOSED(0x38, 0x3F), + NOT_IN_CASE(0x00, 0x03), + UNKNOWN(0xFF, 0xFF); - constructor(raw: Int) : this(raw.toUByte()) + constructor(vararg raw: Int) : this(Range(raw[0].toUByte(), (raw[1].toUByte()))) } val deviceColor: DeviceColor diff --git a/app/src/main/java/eu/darken/capod/reaction/popup/PopUpReaction.kt b/app/src/main/java/eu/darken/capod/reaction/popup/PopUpReaction.kt index a89fc6c7..ece5b67d 100644 --- a/app/src/main/java/eu/darken/capod/reaction/popup/PopUpReaction.kt +++ b/app/src/main/java/eu/darken/capod/reaction/popup/PopUpReaction.kt @@ -26,9 +26,12 @@ class PopUpReaction @Inject constructor( .flatMapLatest { if (it) podMonitor.mainDevice else emptyFlow() } .withPrevious() .map { (previous, current) -> - if (previous is DualApplePods? && current is DualApplePods) { - log(TAG) { "previous=${previous?.rawCaseLidState}, current=${current.rawCaseLidState}" } + log(TAG) { + val prev = previous?.rawCaseLidState?.let { String.format("%02X", it.toByte()) } + val cur = current.rawCaseLidState.let { String.format("%02X", it.toByte()) } + "previous=$prev (${previous?.caseLidState}), current=$cur (${current.caseLidState})" + } log(TAG, VERBOSE) { "previous-id=${previous?.identifier}, current-id=${current.identifier}" } val isSameDeviceWithCaseNowOpen = @@ -41,6 +44,8 @@ class PopUpReaction @Inject constructor( if (current.caseLidState == DualApplePods.LidState.OPEN) { log(TAG, INFO) { "Show popup" } + } else if (current.caseLidState == DualApplePods.LidState.CLOSED) { + log(TAG, INFO) { "Hide popup" } } } }