summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-09-07 12:30:06 +0000
committerGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-09-07 12:30:06 +0000
commit2945b690f98712d66e8dfc0f053e8efee7efa174 (patch)
tree5b7d0de5380699e85943f284dfd6d0a131036f14
parent54fcb13a713993687e8385dd2ff6d70563b5a1c2 (diff)
parent75f1a289daa4131aad939b4f4189c604f4afc2d3 (diff)
Merge branch '5842-fix_reallocaiton_not_loading' into 'main'
5842: fix real location never fix in A-P UI See merge request e/os/advanced-privacy!83
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt71
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt15
2 files changed, 51 insertions, 35 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 aad3b63..42979f0 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)
- }
}
diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt b/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt
index d98cb5d..2e014e2 100644
--- a/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt
+++ b/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt
@@ -135,6 +135,15 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location)
bindClickListeners()
render(viewModel.state.value)
+ viewLifecycleOwner.lifecycleScope.launch {
+ viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
+ viewModel.singleEvents.collect { event ->
+ if (event is FakeLocationViewModel.SingleEvent.LocationUpdatedEvent) {
+ updateLocation(event.location, event.mode)
+ }
+ }
+ }
+ }
}
}
@@ -156,9 +165,6 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location)
is FakeLocationViewModel.SingleEvent.ErrorEvent -> {
displayToast(event.error)
}
- is FakeLocationViewModel.SingleEvent.LocationUpdatedEvent -> {
- updateLocation(event.location, event.mode)
- }
is FakeLocationViewModel.SingleEvent.RequestLocationPermission -> {
// TODO for standalone: rationale dialog
locationPermissionRequest.launch(arrayOf(
@@ -166,6 +172,9 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location)
Manifest.permission.ACCESS_COARSE_LOCATION
))
}
+ is FakeLocationViewModel.SingleEvent.LocationUpdatedEvent -> {
+ // Nothing here, another collect linked to mapbox view.
+ }
}
}
}