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

Commit 94f3a246 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

fix: implement fallback location

parent dc7b9cea
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ before_script:
  - export JAVA_OPTS="-XX:+HeapDumpOnOutOfMemoryError -Xmx4096m"

.build_script: &build_script
  - ./gradlew assembleMapboxDefaultDebug assembleDefaultDebug
  - ./gradlew assembleMapboxDefaultRelease assembleDefaultRelease
  - ./gradlew --no-daemon --max-workers=2 -Dkotlin.compiler.execution.strategy=in-process assembleMapboxDefaultDebug assembleDefaultDebug
  - ./gradlew --no-daemon --max-workers=2 -Dkotlin.compiler.execution.strategy=in-process assembleMapboxDefaultRelease assembleDefaultRelease

.sign_release_apks: &sign_release_apks
  - |
+16 −2
Original line number Diff line number Diff line
@@ -367,7 +367,11 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
            try {
                val ichnaeaCandidate = ichnaea.retrieveMultiWifiLocation(wifis) { wifi, location ->
                    if (settings.wifiCaching) database.putWifiLocation(wifi, location)
                }!!
                }
                if (ichnaeaCandidate == null) {
                    Log.w(TAG, "Ichnaea returned no wifi location for ${wifis.size} wifi networks")
                    return null
                }
                ichnaeaCandidate.time = System.currentTimeMillis()
                return ichnaeaCandidate
            } catch (e: Exception) {
@@ -412,7 +416,9 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
        val previousLastRealtimeMillis = lastWifiDetailsRealtimeMillis
        if (scanResultRealtimeMillis != 0L) lastWifiDetailsRealtimeMillis = scanResultRealtimeMillis
        lifecycleScope.launch {
            val location = queryWifiLocation(requestableWifis)
            val location = queryWifiLocation(requestableWifis) ?: getRecentFallbackLocation().also {
                if (it != null) Log.w(TAG, "Falling back to recent cached location after wifi lookup failure")
            }
            if (location == null) {
                lastWifiDetailsRealtimeMillis = previousLastRealtimeMillis
                return@launch
@@ -426,6 +432,14 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
        }
    }

    private fun getRecentFallbackLocation(): Location? {
        return synchronized(locationLock) {
            listOfNotNull(lastWifiLocation, lastCellLocation, lastLocation)
                .maxByOrNull { it.elapsedMillis }
                ?.let { Location(it).apply { provider = "${it.provider}-fallback" } }
        }
    }

    override fun onWifiSourceFailed() {
        // Wifi source failed, create a new one
        wifiDetailsSource?.disable()