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

Unverified Commit f8c47b3c authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Location: Automatically fallback to WifiManager if WifiScanner fails

parent ea44f4f3
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -51,8 +51,8 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
    private val activeRequests = HashSet<NetworkLocationRequest>()
    private val highPowerScanRunnable = Runnable { this.scan(false) }
    private val lowPowerScanRunnable = Runnable { this.scan(true) }
    private val wifiDetailsSource by lazy { WifiDetailsSource.create(this, this) }
    private val cellDetailsSource by lazy { CellDetailsSource.create(this, this) }
    private var wifiDetailsSource: WifiDetailsSource? = null
    private var cellDetailsSource: CellDetailsSource? = null
    private val mozilla by lazy { MozillaLocationServiceClient(this) }
    private val cache by lazy { LocationCacheDatabase(this) }
    private val movingWifiHelper by lazy { MovingWifiHelper(this) }
@@ -85,8 +85,8 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
        handlerThread = HandlerThread(NetworkLocationService::class.java.simpleName)
        handlerThread.start()
        handler = Handler(handlerThread.looper)
        wifiDetailsSource.enable()
        cellDetailsSource.enable()
        wifiDetailsSource = WifiDetailsSource.create(this, this).apply { enable() }
        cellDetailsSource = CellDetailsSource.create(this, this).apply { enable() }
        try {
            getSystemService<LocationManager>()?.let { locationManager ->
                LocationManagerCompat.requestLocationUpdates(
@@ -107,8 +107,8 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
        if (!lowPower) lastHighPowerScanRealtime = SystemClock.elapsedRealtime()
        lastLowPowerScanRealtime = SystemClock.elapsedRealtime()
        val workSource = synchronized(activeRequests) { activeRequests.minByOrNull { it.intervalMillis }?.workSource }
        wifiDetailsSource.startScan(workSource)
        cellDetailsSource.startScan(workSource)
        wifiDetailsSource?.startScan(workSource)
        cellDetailsSource?.startScan(workSource)
        updateRequests()
    }

@@ -182,8 +182,10 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta

    override fun onDestroy() {
        handlerThread.stop()
        wifiDetailsSource.disable()
        cellDetailsSource.disable()
        wifiDetailsSource?.disable()
        wifiDetailsSource = null
        cellDetailsSource?.disable()
        cellDetailsSource = null
        super.onDestroy()
    }

@@ -289,6 +291,12 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
        }
    }

    override fun onWifiSourceFailed() {
        // Wifi source failed, create a new one
        wifiDetailsSource?.disable()
        wifiDetailsSource = WifiDetailsSource.create(this, this).apply { enable() }
    }

    override fun onCellDetailsAvailable(cells: List<CellDetails>) {
        val scanResultTimestamp = min(cells.maxOf { it.timestamp ?: Long.MAX_VALUE }, System.currentTimeMillis())
        val scanResultRealtimeMillis =
+1 −0
Original line number Diff line number Diff line
@@ -7,4 +7,5 @@ package org.microg.gms.location.network.wifi

interface WifiDetailsCallback {
    fun onWifiDetailsAvailable(wifis: List<WifiDetails>)
    fun onWifiSourceFailed()
}
 No newline at end of file
+14 −6
Original line number Diff line number Diff line
@@ -15,22 +15,29 @@ import android.os.Build.VERSION.SDK_INT
import android.os.WorkSource
import android.util.Log
import androidx.core.content.ContextCompat
import org.microg.gms.location.network.TAG

@SuppressLint("WrongConstant")
class WifiScannerSource(private val context: Context, private val callback: WifiDetailsCallback) : WifiDetailsSource {
    override fun startScan(workSource: WorkSource?) {
        val scanner = context.getSystemService("wifiscanner") as WifiScanner
        scanner.startScan(WifiScanner.ScanSettings(), object : WifiScanner.ScanListener {
        scanner.startScan(WifiScanner.ScanSettings().apply {
            band = WifiScanner.WIFI_BAND_BOTH
        }, object : WifiScanner.ScanListener {
            override fun onSuccess() {
                Log.d(org.microg.gms.location.network.TAG, "Not yet implemented: onSuccess")
                Log.d(TAG, "Not yet implemented: onSuccess")
                failed = false
            }

            override fun onFailure(reason: Int, description: String?) {
                Log.d(org.microg.gms.location.network.TAG, "Not yet implemented: onFailure ${reason} ${description}")
                Log.d(TAG, "Not yet implemented: onFailure $reason $description")
                failed = true
                callback.onWifiSourceFailed()
            }

            @Deprecated("Not supported on all devices")
            override fun onPeriodChanged(periodInMs: Int) {
                Log.d(org.microg.gms.location.network.TAG, "Not yet implemented: onPeriodChanged")
                Log.d(TAG, "Not yet implemented: onPeriodChanged $periodInMs")
            }

            override fun onResults(results: Array<out WifiScanner.ScanData>) {
@@ -38,14 +45,15 @@ class WifiScannerSource(private val context: Context, private val callback: Wifi
            }

            override fun onFullResult(fullScanResult: ScanResult) {
                Log.d(org.microg.gms.location.network.TAG, "Not yet implemented: onFullResult")
                Log.d(TAG, "Not yet implemented: onFullResult $fullScanResult")
            }
        }, workSource)
    }

    companion object {
        private var failed = false
        fun isSupported(context: Context): Boolean {
            return SDK_INT >= 26 && (context.getSystemService("wifiscanner") as? WifiScanner) != null && ContextCompat.checkSelfPermission(context, Manifest.permission.LOCATION_HARDWARE) == PERMISSION_GRANTED
            return SDK_INT >= 26 && !failed && (context.getSystemService("wifiscanner") as? WifiScanner) != null && ContextCompat.checkSelfPermission(context, Manifest.permission.LOCATION_HARDWARE) == PERMISSION_GRANTED
        }
    }
}
 No newline at end of file