feat: require precise GPS for imported routes and release 0.13.4

This commit is contained in:
Darío Sevilla committed 2026-10-13 09:48:19 +00:00
1 parent 27bc1f6958
commit 4ffd4d874e
5 files changed
+53 -6

No files matched your search

@@ -387,14 +387,17 @@ open class MainActivity : Activity() {
val current = service ?: return
if (!current.preparedRouteIsImported()) { hint.text = "Vuelve a cargar la ruta GPX"; return }
positionWait?.cancel()
if (current.hasFreshGpsFix()) { perform(Action.ROUTE); return }
if (current.hasGuidanceGpsFix()) { perform(Action.ROUTE); return }
current.setPhoneVisible(true)
hint.text = "Esperando GPS preciso para iniciar el GPX…"
routeLoading?.visibility = View.VISIBLE
startButton?.isEnabled = false
positionWait = scope.launch {
try {
withTimeout(60_000) { current.gpsReady.first { it && current.hasFreshGpsFix() } }
withTimeout(60_000) {
// Map readiness stays true while accuracy improves from 100 m to 25 m.
while (!current.hasGuidanceGpsFix()) delay(200)
}
ensureActive()
if (current.preparedRouteIsImported()) perform(Action.ROUTE)
} catch (_: TimeoutCancellationException) { hint.text = "Sin GPS preciso · activa ubicación y pulsa Iniciar navegación de nuevo" }
@@ -723,7 +723,7 @@ class TftService : Service() {
fun isGpsSessionActive(): Boolean = gpsRequested
fun hasFreshGpsFix(): Boolean = usableGpsFix(AndroidLocationProvider.MAP_ACCURACY_METERS)
fun usesCompassHeading(): Boolean = (locationProvider ?: visibleGpsProvider)?.headingFromCompass == true
private fun hasGuidanceGpsFix(): Boolean = usableGpsFix(AndroidLocationProvider.GUIDANCE_ACCURACY_METERS)
fun hasGuidanceGpsFix(): Boolean = usableGpsFix(AndroidLocationProvider.GUIDANCE_ACCURACY_METERS)
private fun usableGpsFix(maxAccuracy: Float): Boolean = lastGpsFix?.let { it.accuracyMeters.isFinite() && it.accuracyMeters in 0f..maxAccuracy && SystemClock.elapsedRealtime() - lastGpsElapsed in 0..15_000 && (locationProvider?.providerEnabled?.value == true || (phoneVisible && visibleGpsProvider?.providerEnabled?.value == true)) } == true
private fun stopVisibleGps() {
visibleGpsJob?.cancel(); visibleGpsJob = null
@@ -157,4 +157,44 @@ class ImportedRouteTest {
} finally { activityController.destroy(); serviceController.destroy() }
}
@Test fun importedStartWaitsForGuidanceAccuracyEvenWhenMapReadinessStaysTrue() {
val activityController = Robolectric.buildActivity(PhoneWithoutGpu::class.java).create()
val serviceController = Robolectric.buildService(TftService::class.java).create()
val activity = activityController.get()
val service = serviceController.get()
service.mapRenderOverride = { _, _ -> TftFrame(encodedBytes = byteArrayOf(1), timestampMs = 0) }
try {
val application = RuntimeEnvironment.getApplication()
shadowOf(application).grantPermissions(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
field("service").set(activity, service)
val manager = application.getSystemService(LocationManager::class.java)
shadowOf(manager).setProviderEnabled(LocationManager.GPS_PROVIDER, true)
service.prepareImportedRoute(imported()); tick()
service.setPhoneVisible(true); tick()
fun fix(accuracy: Float) {
shadowOf(manager).simulateLocation(Location(LocationManager.GPS_PROVIDER).apply {
latitude = 43.36; longitude = -5.85; this.accuracy = accuracy
time = System.currentTimeMillis(); elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
})
tick()
}
fix(80f)
assertTrue(service.gpsReady.value)
assertTrue(service.hasFreshGpsFix())
assertFalse(service.hasGuidanceGpsFix())
MainActivity::class.java.getDeclaredMethod("startImportedRoute").apply { isAccessible = true }.invoke(activity)
val pending = field("positionWait").get(activity) as kotlinx.coroutines.Job
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(600))
assertTrue(pending.isActive)
assertNull(shadowOf(activity).nextStartedService)
assertFalse(service.hasActiveGuidance())
fix(20f)
assertTrue(service.gpsReady.value)
assertTrue(service.hasGuidanceGpsFix())
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(400))
assertTrue(pending.isCompleted)
assertEquals(TftService.ACTION_ROUTE_GPS, shadowOf(activity).nextStartedService.action)
} finally { activityController.destroy(); serviceController.destroy() }
}
}