From 47f580838db0185292c989fb448f2bcb6ab33dac Mon Sep 17 00:00:00 2001 From: Guillaume Jacquart Date: Wed, 17 Aug 2022 19:02:40 +0200 Subject: 5842: fix real location never fix in A-P UI --- .../domain/usecases/FakeLocationStateUseCase.kt | 71 ++++++++++++---------- 1 file changed, 39 insertions(+), 32 deletions(-) (limited to 'app/src/main/java/foundation/e/privacycentralapp/domain') diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt index f7b5439..9f39312 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt @@ -35,6 +35,7 @@ import foundation.e.privacymodules.permissions.data.ApplicationDescription import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlin.random.Random @@ -45,8 +46,12 @@ class FakeLocationStateUseCase( private val citiesRepository: CityDataSource, private val appDesc: ApplicationDescription, private val appContext: Context, - private val coroutineScope: CoroutineScope + coroutineScope: CoroutineScope ) { + companion object { + private const val TAG = "FakeLocationStateUseCase" + } + private val _configuredLocationMode = MutableStateFlow>(Triple(LocationMode.REAL_LOCATION, null, null)) val configuredLocationMode: StateFlow> = _configuredLocationMode @@ -131,67 +136,69 @@ class FakeLocationStateUseCase( val currentLocation = MutableStateFlow(null) private var localListener = object : LocationListener { - val providerName = LocationManager.NETWORK_PROVIDER override fun onLocationChanged(location: Location) { - currentLocation.value = location + currentLocation.update { previous -> + if ((previous?.time?: 0) + 1800 < location.time || + (previous?.accuracy?: Float.MAX_VALUE) > location.accuracy) { + location + } else { + previous + } + } } // Deprecated since API 29, never called. override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {} - // TODO migration to minSdk31 , check still working. override fun onProviderEnabled(provider: String) { - reset(provider) + reset() } override fun onProviderDisabled(provider: String) { - reset(provider) + reset() } - private fun reset(provider: String?) { - if (provider == providerName) { - stopListeningLocation() - currentLocation.value = null - startListeningLocation() - } + private fun reset() { + stopListeningLocation() + currentLocation.value = null + startListeningLocation() } } fun startListeningLocation(): Boolean { return if (hasAcquireLocationPermission()) { - requestLocationUpdates(localListener) + requestLocationUpdates() true } else false } fun stopListeningLocation() { - removeUpdates(localListener) + locationManager.removeUpdates(localListener) } - fun requestLocationUpdates(listener: LocationListener) { + private fun requestLocationUpdates() { try { locationManager.requestLocationUpdates( - LocationManager.NETWORK_PROVIDER, // TODO: tight this with fakelocation module. - 0L, + LocationManager.NETWORK_PROVIDER, + 1000L, + 0f, + localListener + ) + locationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + 1000L, 0f, - listener + localListener + ) - // locationManager.requestLocationUpdates( - // LocationManager.NETWORK_PROVIDER, // TODO: tight this with fakelocation module. - // 0L, - // 0f, - // listener - // ) - - val location: Location? = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) - location?.let { listener.onLocationChanged(it) } + + locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)?: + locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.let { + localListener.onLocationChanged(it) + } } catch (se: SecurityException) { - Log.e("DebugLoc", "Missing permission", se) + Log.e(TAG, "Missing permission", se) } } - - fun removeUpdates(listener: LocationListener) { - locationManager.removeUpdates(listener) - } } -- cgit v1.2.3