Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 66340c6b authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

5842: use GMS FusedLoc to fix real loc endless loading

parent 067ff1de
Loading
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -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) {
+21 −59
Original line number Diff line number Diff line
@@ -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<Location?>(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
        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()
        )
            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)
        }
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -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 = [