Skip to content

Commit

Permalink
Location: Fix location updates not being reported properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed May 8, 2024
1 parent 720823e commit 9ca5b62
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.core.location.LocationCompat

const val ACTION_NETWORK_LOCATION_SERVICE = "org.microg.gms.location.network.ACTION_NETWORK_LOCATION_SERVICE"
const val EXTRA_LOCATION = "location"
const val EXTRA_ELAPSED_REALTIME = "elapsed_realtime"
const val EXTRA_PENDING_INTENT = "pending_intent"
const val EXTRA_ENABLE = "enable"
const val EXTRA_INTERVAL_MILLIS = "interval"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.location.Location
import android.os.SystemClock
import android.os.WorkSource
import org.microg.gms.location.EXTRA_LOCATION
import org.microg.gms.location.EXTRA_ELAPSED_REALTIME

class NetworkLocationRequest(
var pendingIntent: PendingIntent,
Expand All @@ -20,10 +21,14 @@ class NetworkLocationRequest(
var bypass: Boolean,
var workSource: WorkSource
) {
private var lastRealtime = 0L
var lastRealtime = 0L
private set

fun send(context: Context, location: Location) {
lastRealtime = SystemClock.elapsedRealtime()
pendingIntent.send(context, 0, Intent().apply { putExtra(EXTRA_LOCATION, location) })
pendingIntent.send(context, 0, Intent().apply {
putExtra(EXTRA_LOCATION, location)
putExtra(EXTRA_ELAPSED_REALTIME, lastRealtime)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
private val cache by lazy { LocationCacheDatabase(this) }
private val movingWifiHelper by lazy { MovingWifiHelper(this) }
private val settings by lazy { LocationSettings(this) }
private val wifiScanCache = LruCache<String, Location>(100)
private val wifiScanCache = LruCache<String, Location>(WIFI_SCAN_CACHE_SIZE)

private var lastHighPowerScanRealtime = 0L
private var lastLowPowerScanRealtime = 0L
Expand Down Expand Up @@ -234,7 +234,7 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
}
}

else -> cacheLocation
else -> Location(cacheLocation)
}?.takeIf { candidate == null || it.accuracy < candidate?.accuracy!! } ?: candidate
} catch (e: Exception) {
Log.w(TAG, "Failed retrieving location for ${requestableWifis.size} wifi networks", e)
Expand Down Expand Up @@ -468,7 +468,7 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
if (activeRequests.isNotEmpty()) {
writer.println("Active requests:")
for (request in activeRequests) {
writer.println("- ${request.workSource} ${request.intervalMillis.formatDuration()} (low power: ${request.lowPower}, bypass: ${request.bypass})")
writer.println("- ${request.workSource} ${request.intervalMillis.formatDuration()} (low power: ${request.lowPower}, bypass: ${request.bypass}) reported ${request.lastRealtime.formatRealtime()}")
}
}
}
Expand All @@ -484,6 +484,7 @@ class NetworkLocationService : LifecycleService(), WifiDetailsCallback, CellDeta
const val MAX_WIFI_SCAN_CACHE_AGE = 1000L * 60 * 60 * 24 // 1 day
const val MAX_LOCAL_WIFI_AGE_NS = 60_000_000_000L // 1 minute
const val MAX_LOCAL_WIFI_SCAN_AGE_NS = 600_000_000_000L // 10 minutes
const val WIFI_SCAN_CACHE_SIZE = 200
}
}

Expand All @@ -500,7 +501,7 @@ fun List<WifiDetails>.hash(): ByteArray? {
fun WifiDetails.hashBytes(): ByteArray {
return macBytes + byteArrayOf(
((maxTimestamp - (timestamp ?: 0L)) / (60 * 1000)).toByte(), // timestamp
((signalStrength ?: 0) / 10).toByte() // signal strength
((signalStrength ?: 0) / 20).toByte() // signal strength
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.os.WorkSource
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService
Expand Down Expand Up @@ -45,6 +46,7 @@ class NetworkLocationProviderPreTiramisu : AbstractLocationProviderPreTiramisu {
private var currentRequest: ProviderRequestUnbundled? = null
private var pendingIntent: PendingIntent? = null
private var lastReportedLocation: Location? = null
private var lastReportTime: Long = 0
private val handler = Handler(Looper.getMainLooper())
private val reportAgainRunnable = Runnable { reportAgain() }

Expand Down Expand Up @@ -80,6 +82,7 @@ class NetworkLocationProviderPreTiramisu : AbstractLocationProviderPreTiramisu {
writer.println("Enabled: $enabled")
writer.println("Current request: $currentRequest")
writer.println("Last reported: $lastReportedLocation")
writer.println("Last report time: ${lastReportTime.formatRealtime()}")
}

override fun onSetRequest(request: ProviderRequestUnbundled, source: WorkSource) {
Expand Down Expand Up @@ -129,8 +132,7 @@ class NetworkLocationProviderPreTiramisu : AbstractLocationProviderPreTiramisu {
private fun reportAgain() {
// Report location again if it's recent enough
lastReportedLocation?.let {
if (it.elapsedMillis + MIN_INTERVAL_MILLIS < SystemClock.elapsedRealtime() ||
it.elapsedMillis + (currentRequest?.interval ?: 0) < SystemClock.elapsedRealtime()) {
if (it.elapsedMillis + max(currentRequest?.interval ?: 0, MIN_INTERVAL_MILLIS) > SystemClock.elapsedRealtime()) {
reportLocationToSystem(it)
}
}
Expand All @@ -141,6 +143,7 @@ class NetworkLocationProviderPreTiramisu : AbstractLocationProviderPreTiramisu {
location.provider = LocationManager.NETWORK_PROVIDER
location.extras?.remove(LOCATION_EXTRA_PRECISION)
lastReportedLocation = location
lastReportTime = SystemClock.elapsedRealtime()
super.reportLocation(location)
val repeatInterval = max(MIN_REPORT_MILLIS, currentRequest?.interval ?: Long.MAX_VALUE)
if (repeatInterval < MIN_INTERVAL_MILLIS) {
Expand Down

0 comments on commit 9ca5b62

Please sign in to comment.