From 986cc2c16dcb6e3b8bfb08c00e85f18ce832fbc2 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:40:44 +0200 Subject: [PATCH] Revert "DevicePoseProvider: be picky about locations (#1344)" This reverts commit 4bb092d98bf3b5bc0e93076918a8d1e7a2b1fc4c. --- .../devicepose/DevicePoseProvider.kt | 32 ++++++++-------- .../de/mm20/launcher2/ktx/AndroidLocation.kt | 37 ------------------- .../java/de/mm20/launcher2/ktx/Boolean.kt | 13 +------ 3 files changed, 17 insertions(+), 65 deletions(-) delete mode 100644 core/ktx/src/main/java/de/mm20/launcher2/ktx/AndroidLocation.kt diff --git a/core/devicepose/src/main/java/de/mm20/launcher2/devicepose/DevicePoseProvider.kt b/core/devicepose/src/main/java/de/mm20/launcher2/devicepose/DevicePoseProvider.kt index 7b13c408..aa03f74d 100644 --- a/core/devicepose/src/main/java/de/mm20/launcher2/devicepose/DevicePoseProvider.kt +++ b/core/devicepose/src/main/java/de/mm20/launcher2/devicepose/DevicePoseProvider.kt @@ -18,8 +18,6 @@ import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.channelFlow -import de.mm20.launcher2.ktx.foldOrNull -import de.mm20.launcher2.ktx.isBetterThan import kotlinx.coroutines.flow.combine class DevicePoseProvider internal constructor( @@ -39,16 +37,10 @@ class DevicePoseProvider internal constructor( } fun getLocation(minTimeMs: Long = 1000, minDistanceM: Float = 1f) = channelFlow { - fun updateLocation(update: Location?) { - if (update == null) return - if (!update.isBetterThan(lastLocation)) return - lastLocation = update - updateDeclination(update) - trySend(update) - } - val locationCallback = LocationListenerCompat { - updateLocation(it) + lastLocation = it + updateDeclination(it) + trySend(it) } context.getSystemService() @@ -58,14 +50,20 @@ class DevicePoseProvider internal constructor( val hasCoarseAccess = context.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION) - val previousLocation = - hasFineAccess.foldOrNull { getLastKnownLocation(LocationManager.GPS_PROVIDER) } ?: - hasCoarseAccess.foldOrNull { getLastKnownLocation(LocationManager.NETWORK_PROVIDER) } + val location = + (if (hasFineAccess) this@runCatching.getLastKnownLocation(LocationManager.GPS_PROVIDER) else null) + ?: if (hasCoarseAccess) this@runCatching.getLastKnownLocation( + LocationManager.NETWORK_PROVIDER + ) else null - updateLocation(previousLocation) + if (location != null) { + lastLocation = location + updateDeclination(location) + trySend(location) + } if (hasFineAccess) { - requestLocationUpdates( + this@runCatching.requestLocationUpdates( LocationManager.GPS_PROVIDER, minTimeMs, minDistanceM, @@ -73,7 +71,7 @@ class DevicePoseProvider internal constructor( ) } if (hasCoarseAccess) { - requestLocationUpdates( + this@runCatching.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, minTimeMs, minDistanceM, diff --git a/core/ktx/src/main/java/de/mm20/launcher2/ktx/AndroidLocation.kt b/core/ktx/src/main/java/de/mm20/launcher2/ktx/AndroidLocation.kt deleted file mode 100644 index c1dbec4e..00000000 --- a/core/ktx/src/main/java/de/mm20/launcher2/ktx/AndroidLocation.kt +++ /dev/null @@ -1,37 +0,0 @@ -package de.mm20.launcher2.ktx - -import kotlin.time.Duration.Companion.minutes -import kotlin.time.Duration.Companion.nanoseconds - -import android.location.Location as AndroidLocation - -/* https://github.com/streetcomplete/StreetComplete/blob/master/app/src/main/java/de/westnordost/streetcomplete/util/location/LocationUtils.kt - * GPL-3.0-or-later - */ -fun AndroidLocation.isBetterThan(previous: AndroidLocation?): Boolean { - if (longitude.isNaN() || latitude.isNaN()) return false - if (previous == null) return true - - val locationTimeDiff = elapsedRealtimeNanos.nanoseconds - previous.elapsedRealtimeNanos.nanoseconds - val isMuchNewer = locationTimeDiff > 2.minutes - val isMuchOlder = locationTimeDiff < (-2).minutes - val isNewer = locationTimeDiff.isPositive() - - val accuracyDelta = accuracy - previous.accuracy - val isLessAccurate = accuracyDelta > 0f - val isMoreAccurate = accuracyDelta < 0f - val isMuchLessAccurate = accuracyDelta > 200f - - val isFromSameProvider = provider == previous.provider - - return when { - // the user has likely moved - isMuchNewer -> true - // If the new location is more than two minutes older, it must be worse - isMuchOlder -> false - isMoreAccurate -> true - isNewer && !isLessAccurate -> true - isNewer && !isMuchLessAccurate && isFromSameProvider -> true - else -> false - } -} diff --git a/core/ktx/src/main/java/de/mm20/launcher2/ktx/Boolean.kt b/core/ktx/src/main/java/de/mm20/launcher2/ktx/Boolean.kt index 6473e0a3..5c8196f6 100644 --- a/core/ktx/src/main/java/de/mm20/launcher2/ktx/Boolean.kt +++ b/core/ktx/src/main/java/de/mm20/launcher2/ktx/Boolean.kt @@ -1,14 +1,5 @@ package de.mm20.launcher2.ktx -fun Boolean.toInt(): Int { +inline fun Boolean.toInt(): Int { return if (this) 1 else 0 -} - -fun Boolean.fold( - whenTrue: () -> T, - otherwise: () -> T -): T = if (this) whenTrue() else otherwise() - -fun Boolean.foldOrNull( - whenTrue: () -> T -): T? = fold(whenTrue) { null } +} \ No newline at end of file