aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/foundation/e/privacycentralapp/domain
diff options
context:
space:
mode:
authorGuillaume Jacquart <guillaume.jacquart-ext@mousquetaires.com>2022-08-17 19:02:40 +0200
committerGuillaume Jacquart <guillaume.jacquart-ext@mousquetaires.com>2022-08-18 09:59:46 +0200
commit47f580838db0185292c989fb448f2bcb6ab33dac (patch)
tree3b4dc41652e671a74903f96f7f6582d87b6049ac /app/src/main/java/foundation/e/privacycentralapp/domain
parent8884e0423a4e50406b221ee07321a8560bb7420c (diff)
downloadadvanced-privacy-47f580838db0185292c989fb448f2bcb6ab33dac.tar.gz
5842: fix real location never fix in A-P UI
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/domain')
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt71
1 files changed, 39 insertions, 32 deletions
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, Float?, Float?>>(Triple(LocationMode.REAL_LOCATION, null, null))
val configuredLocationMode: StateFlow<Triple<LocationMode, Float?, Float?>> = _configuredLocationMode
@@ -131,67 +136,69 @@ class FakeLocationStateUseCase(
val currentLocation = MutableStateFlow<Location?>(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)
- }
}