From 66340c6ba8b6c6a0c6fd8820cf7e1bead477409f Mon Sep 17 00:00:00 2001 From: Guillaume Jacquart Date: Fri, 14 Oct 2022 15:05:04 +0200 Subject: [PATCH] 5842: use GMS FusedLoc to fix real loc endless loading --- app/build.gradle | 6 +- .../usecases/FakeLocationStateUseCase.kt | 80 +++++-------------- dependencies.gradle | 8 +- 3 files changed, 32 insertions(+), 62 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 42bf90d8..dc99186c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -140,19 +140,21 @@ dependencies { Libs.AndroidX.work, Libs.material, + Libs.elib, + Libs.Retrofit.retrofit, Libs.Retrofit.scalars, + Libs.GMS.location, Libs.MapBox.sdk, Libs.mpAndroidCharts ) - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1' + debugImplementation Libs.leakCanary testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' - implementation 'foundation.e:elib:0.0.1-alpha11' } static def log(Object val) { 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 14268915..bd1475ab 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 @@ -21,10 +21,11 @@ import android.app.AppOpsManager import android.content.Context import android.content.pm.PackageManager import android.location.Location -import android.location.LocationListener -import android.location.LocationManager -import android.os.Bundle -import android.util.Log +import android.os.Looper +import com.google.android.gms.location.LocationListener +import com.google.android.gms.location.LocationRequest +import com.google.android.gms.location.LocationServices +import com.google.android.gms.location.Priority import foundation.e.privacycentralapp.data.repositories.LocalStateRepository import foundation.e.privacycentralapp.domain.entities.LocationMode import foundation.e.privacycentralapp.dummy.CityDataSource @@ -63,8 +64,8 @@ class FakeLocationStateUseCase( } } - private val locationManager: LocationManager - get() = appContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager + private val fusedLocationClient + get() = LocationServices.getFusedLocationProviderClient(appContext) private fun hasAcquireLocationPermission(): Boolean { return (appContext.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) @@ -80,6 +81,7 @@ class FakeLocationStateUseCase( localStateRepository.locationMode.value = configuredLocationMode.value.first } else { fakeLocationModule.stopFakeLocation() + currentLocation.value = null localStateRepository.locationMode.value = LocationMode.REAL_LOCATION } } @@ -135,36 +137,7 @@ class FakeLocationStateUseCase( val currentLocation = MutableStateFlow(null) - private var localListener = object : LocationListener { - - override fun onLocationChanged(location: 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?) {} - - override fun onProviderEnabled(provider: String) { - reset() - } - - override fun onProviderDisabled(provider: String) { - reset() - } - - private fun reset() { - stopListeningLocation() - currentLocation.value = null - startListeningLocation() - } - } + private var localListener = LocationListener { location -> currentLocation.update { location } } fun startListeningLocation(): Boolean { return if (hasAcquireLocationPermission()) { @@ -174,31 +147,20 @@ class FakeLocationStateUseCase( } fun stopListeningLocation() { - locationManager.removeUpdates(localListener) + fusedLocationClient.removeLocationUpdates(localListener) } private fun requestLocationUpdates() { - try { - locationManager.requestLocationUpdates( - LocationManager.NETWORK_PROVIDER, - 1000L, - 0f, - localListener - ) - locationManager.requestLocationUpdates( - LocationManager.GPS_PROVIDER, - 1000L, - 0f, - localListener - - ) - - locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)?: - locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.let { - localListener.onLocationChanged(it) - } - } catch (se: SecurityException) { - Log.e(TAG, "Missing permission", se) - } + val locationRequest = LocationRequest.Builder( + Priority.PRIORITY_HIGH_ACCURACY, + // 1,5s is a good value for the UX while setting specific location. + 1500L, + ).build() + + fusedLocationClient.requestLocationUpdates( + locationRequest, + localListener, + Looper.getMainLooper() + ) } } diff --git a/dependencies.gradle b/dependencies.gradle index 6c4acb31..79cdb7fa 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -32,7 +32,7 @@ libs.junit = "junit:junit:4.13.1" libs.robolectric = "org.robolectric:robolectric:4.5" libs.mockK = "io.mockk:mockk:1.10.5" -libs.leakCanary = "com.squareup.leakcanary:leakcanary-android:2.6" +libs.leakCanary = "com.squareup.leakcanary:leakcanary-android:2.9.1" libs.truth = "com.google.truth:truth:1.1" @@ -106,11 +106,17 @@ libs.Hilt = [ libs.material = 'com.google.android.material:material:1.6.1' +libs.elib = 'foundation.e:elib:0.0.1-alpha11' + libs.Retrofit = [ retrofit: 'com.squareup.retrofit2:retrofit:2.9.0', scalars: 'com.squareup.retrofit2:converter-scalars:2.9.0' ] +versions.gms = "21.0.0" +libs.GMS = [ + location: "com.google.android.gms:play-services-location:$versions.gms" +] versions.mapbox="9.6.1" libs.MapBox = [ -- GitLab