Improve lid state detection

This commit is contained in:
darken
2022-01-18 15:13:28 +01:00
parent 477774078a
commit 5731f43a57
3 changed files with 27 additions and 10 deletions
@@ -75,7 +75,8 @@ class PodMonitor @Inject constructor(
pods[it.identifier] = it
}
}
val now = Instant.now()
// pods.values.sortedWith(compareBy<PodDevice> { Duration.between(it.lastSeenAt,now) }.thenByDescending { it.rssi })
pods.values.sortedByDescending { it.rssi }
}
.onStart { emit(emptyList()) }
@@ -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<UByte>) {
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
@@ -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" }
}
}
}