Revert "DevicePoseProvider: be picky about locations (#1344)"

This reverts commit 4bb092d98b.
This commit is contained in:
MM20
2025-06-09 21:40:44 +02:00
parent db8dbf014c
commit 986cc2c16d
3 changed files with 17 additions and 65 deletions
@@ -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<LocationManager>()
@@ -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,