fix: stop orphaned GPS synchronization

This commit is contained in:
admin
2026-07-11 00:27:52 +02:00
parent 89235fbb46
commit 7bba4dc878
9 changed files with 225 additions and 40 deletions
@@ -1,12 +1,23 @@
package pl.firmatpp.kierowca
import android.app.Application
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import pl.firmatpp.kierowca.data.upload.OfflineOutboxManager
import pl.firmatpp.kierowca.diagnostics.AppDiagnostics
class DriverApplication : Application() {
private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
override fun onCreate() {
super.onCreate()
AppDiagnostics.installFirebaseCrashlytics()
AppDiagnostics.log("app_started")
applicationScope.launch {
runCatching { OfflineOutboxManager(this@DriverApplication).repairGpsQueue() }
.onFailure { AppDiagnostics.reportNonFatal(it, "repair_gps_queue") }
}
}
}
@@ -3,8 +3,9 @@ package pl.firmatpp.kierowca.data.upload
import android.content.Context
import androidx.work.WorkManager
import java.io.File
import java.util.concurrent.TimeUnit
class OfflineOutboxManager(context: Context) {
class OfflineOutboxManager(private val context: Context) {
private val database = DriverDatabase.get(context)
private val workManager = WorkManager.getInstance(context)
@@ -14,6 +15,27 @@ class OfflineOutboxManager(context: Context) {
database.routeActionDao().allUnsent().size +
database.routePointDao().allUnsent().size
suspend fun repairGpsQueue() {
val retryableStatuses = setOf(
RoutePointStatus.Pending,
RoutePointStatus.Syncing,
RoutePointStatus.FailedRetryable,
)
val routeIds = database.routePointDao().allUnsent()
.filter { it.status in retryableStatuses }
.map { it.routeId }
.distinct()
routeIds.forEach { routeId ->
runCatching {
workManager.cancelUniqueWork(RoutePointWorker.uniqueWorkName(routeId))
.result
.get(5, TimeUnit.SECONDS)
}
RoutePointOutbox(context).enqueueWorker(routeId, delaySeconds = 0L)
}
}
suspend fun clearAll() {
val photos = database.photoUploadDao().allRecords()
val dispatchSheets = database.dispatchSheetUploadDao().allRecords()
@@ -37,6 +37,9 @@ interface RoutePointDao {
@Query("SELECT COUNT(*) FROM route_points WHERE status IN ('PENDING', 'SYNCING', 'FAILED_RETRYABLE')")
fun observeUnsentCount(): Flow<Int>
@Query("SELECT MIN(createdAtEpochMillis) FROM route_points WHERE status IN ('PENDING', 'SYNCING', 'FAILED_RETRYABLE')")
fun observeOldestUnsentAt(): Flow<Long?>
@Query(
"""
UPDATE route_points
@@ -54,7 +54,7 @@ class RoutePointOutbox(
workManager.enqueueUniqueWork(
RoutePointWorker.uniqueWorkName(routeId),
ExistingWorkPolicy.APPEND_OR_REPLACE,
ExistingWorkPolicy.KEEP,
request,
)
}
@@ -4,6 +4,7 @@ import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import java.util.UUID
import kotlinx.coroutines.delay
import pl.firmatpp.kierowca.data.ApiErrorKind
import pl.firmatpp.kierowca.data.ApiErrorMapper
import pl.firmatpp.kierowca.data.DriverRepository
@@ -23,31 +24,34 @@ class RoutePointWorker(
override suspend fun doWork(): Result {
if (!NetworkMonitor(applicationContext).isCurrentlyValidated()) return Result.retry()
val routeId = inputData.getString(KEY_ROUTE_ID) ?: return Result.failure()
val points = dao.pendingForRoute(routeId, BATCH_LIMIT)
if (points.isEmpty()) return Result.success()
while (!isStopped) {
val points = dao.pendingForRoute(routeId, BATCH_LIMIT)
if (points.isEmpty()) return Result.success()
val pointIds = points.map { it.clientPointId }
dao.updateStatus(pointIds, RoutePointStatus.Syncing, null, attemptIncrement = 1)
val pointIds = points.map { it.clientPointId }
dao.updateStatus(pointIds, RoutePointStatus.Syncing, null, attemptIncrement = 1)
return runCatching {
val response = repository.storeRoutePoints(
routeId = routeId,
body = RoutePointBatchBody(
clientBatchId = UUID.randomUUID().toString(),
points = points.map { it.toDto() },
),
)
dao.delete(pointIds)
if (response.arrived) {
ActiveRouteTrackingService.markArrived(applicationContext, routeId)
val outcome = runCatching {
repository.storeRoutePoints(
routeId = routeId,
body = RoutePointBatchBody(
clientBatchId = UUID.randomUUID().toString(),
points = points.map { it.toDto() },
),
)
}
if (dao.pendingCount(routeId) > 0) {
RoutePointOutbox(applicationContext).enqueueWorker(routeId, delaySeconds = 1L)
val response = outcome.getOrNull()
if (response != null) {
dao.delete(pointIds)
if (response.arrived) ActiveRouteTrackingService.markArrived(applicationContext, routeId)
if (dao.pendingCount(routeId) == 0) {
delay(750L)
if (dao.pendingCount(routeId) == 0) return Result.success()
}
continue
}
Result.success()
}.getOrElse { throwable ->
val throwable = outcome.exceptionOrNull() ?: return Result.retry()
val error = ApiErrorMapper.map(throwable)
AppDiagnostics.reportNonFatal(
throwable = throwable,
@@ -61,11 +65,18 @@ class RoutePointWorker(
"retryable" to error.retryable,
),
)
if (isInactiveRoutePointError(error.code)) {
dao.delete(pointIds)
ActiveRouteTrackingService.stop(applicationContext, routeId)
continue
}
val status = if (error.retryable) RoutePointStatus.FailedRetryable else RoutePointStatus.FailedPermanent
dao.updateStatus(pointIds, status, error.message, attemptIncrement = 0)
if (error.retryable && error.kind != ApiErrorKind.Auth) Result.retry() else Result.failure()
return if (error.retryable && error.kind != ApiErrorKind.Auth) Result.retry() else Result.failure()
}
return Result.retry()
}
private fun RoutePointEntity.toDto(): RoutePointDto =
@@ -90,3 +101,5 @@ class RoutePointWorker(
fun uniqueWorkName(routeId: String): String = "route-point-sync-$routeId"
}
}
internal fun isInactiveRoutePointError(code: String?): Boolean = code == "ROUTE_POINTS_INVALID_STATUS"
@@ -56,6 +56,10 @@ class ActiveRouteTrackingService : Service(), LocationListener {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
ACTION_STOP -> {
val requestedRouteId = intent.getStringExtra(EXTRA_ROUTE_ID)
if (!requestedRouteId.isNullOrBlank() && routeId != requestedRouteId) {
return START_NOT_STICKY
}
stopSelf()
return START_NOT_STICKY
}
@@ -64,13 +68,25 @@ class ActiveRouteTrackingService : Service(), LocationListener {
if (!id.isNullOrBlank() && (routeId == null || id == routeId)) {
routeId = id
arrived = true
runCatching { locationManager.removeUpdates(this) }
startForegroundCompat(buildNotification())
}
return START_STICKY
return START_NOT_STICKY
}
else -> {
routeId = intent?.getStringExtra(EXTRA_ROUTE_ID) ?: routeId
driverId = intent?.getStringExtra(EXTRA_DRIVER_ID) ?: driverId
val nextRouteId = intent?.getStringExtra(EXTRA_ROUTE_ID) ?: routeId
val nextDriverId = intent?.getStringExtra(EXTRA_DRIVER_ID) ?: driverId
if (nextRouteId.isNullOrBlank() || nextDriverId.isNullOrBlank()) {
stopSelf()
return START_NOT_STICKY
}
val routeChanged = nextRouteId != routeId
routeId = nextRouteId
driverId = nextDriverId
getSharedPreferences(TRACKING_PREFERENCES, Context.MODE_PRIVATE)
.edit()
.putString(ACTIVE_ROUTE_ID, nextRouteId)
.apply()
routeLabel = intent?.getStringExtra(EXTRA_ROUTE_LABEL) ?: routeLabel
destinationLat = intent?.takeIf { it.hasExtra(EXTRA_DESTINATION_LAT) }?.getDoubleExtra(EXTRA_DESTINATION_LAT, 0.0)
?: destinationLat
@@ -80,10 +96,10 @@ class ActiveRouteTrackingService : Service(), LocationListener {
?.takeIf { it > 0.0 }
?: totalDistanceMeters
progressEnabled = intent?.getBooleanExtra(EXTRA_PROGRESS_ENABLED, true) ?: progressEnabled
arrived = false
if (routeChanged) arrived = false
startForegroundCompat(buildNotification())
requestLocationUpdates()
return START_STICKY
if (!arrived) requestLocationUpdates()
return START_NOT_STICKY
}
}
}
@@ -96,6 +112,7 @@ class ActiveRouteTrackingService : Service(), LocationListener {
if (!arrived && remaining != null && remaining <= arrivalRadiusMeters(location)) {
arrived = true
runCatching { locationManager.removeUpdates(this) }
startForegroundCompat(buildNotification())
} else {
startForegroundCompat(buildNotification())
@@ -119,14 +136,19 @@ class ActiveRouteTrackingService : Service(), LocationListener {
override fun onDestroy() {
runCatching { locationManager.removeUpdates(this) }
getSharedPreferences(TRACKING_PREFERENCES, Context.MODE_PRIVATE)
.edit()
.remove(ACTIVE_ROUTE_ID)
.apply()
scope.cancel()
super.onDestroy()
}
private fun requestLocationUpdates() {
if (!hasLocationPermission()) {
if (!hasLocationPermission() || routeId.isNullOrBlank() || driverId.isNullOrBlank()) {
return
}
runCatching { locationManager.removeUpdates(this) }
runCatching {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_INTERVAL_MS, 0f, this)
}
@@ -266,6 +288,8 @@ class ActiveRouteTrackingService : Service(), LocationListener {
private const val EXTRA_DESTINATION_LON = "destinationLon"
private const val EXTRA_TOTAL_DISTANCE_METERS = "totalDistanceMeters"
private const val EXTRA_PROGRESS_ENABLED = "progressEnabled"
private const val TRACKING_PREFERENCES = "active_route_tracking"
private const val ACTIVE_ROUTE_ID = "active_route_id"
fun start(context: Context, route: DriverRouteDto, driver: DriverDto, progressEnabled: Boolean = true) {
val intent = Intent(context, ActiveRouteTrackingService::class.java).apply {
@@ -281,12 +305,16 @@ class ActiveRouteTrackingService : Service(), LocationListener {
ContextCompat.startForegroundService(context, intent)
}
fun stop(context: Context) {
context.startService(
Intent(context, ActiveRouteTrackingService::class.java).apply {
action = ACTION_STOP
},
)
fun stop(context: Context, routeId: String? = null) {
val activeRouteId = context.getSharedPreferences(TRACKING_PREFERENCES, Context.MODE_PRIVATE)
.getString(ACTIVE_ROUTE_ID, null)
if (routeId == null || routeId == activeRouteId) {
context.stopService(Intent(context, ActiveRouteTrackingService::class.java))
context.getSharedPreferences(TRACKING_PREFERENCES, Context.MODE_PRIVATE)
.edit()
.remove(ACTIVE_ROUTE_ID)
.apply()
}
}
fun markArrived(context: Context, routeId: String) {
@@ -118,6 +118,7 @@ data class DriverUiState(
val pendingLogoutItems: Int = 0,
val pendingDispatchUploads: Int = 0,
val pendingGpsPoints: Int = 0,
val oldestPendingGpsAtEpochMillis: Long? = null,
val diagnostics: DiagnosticSnapshot? = null,
val diagnosticsLoading: Boolean = false,
val diagnosticsError: String? = null,
@@ -138,13 +139,20 @@ data class DriverUiState(
val displaySelectedRoute: DriverRouteDto?
get() = selectedRouteProjection?.route
val pendingOperationalItems: Int
private val pendingUserOperationalItems: Int
get() = queuedPhotoUploads.count { it.status != PhotoUploadStatus.Confirmed.storageValue && it.status != PhotoUploadStatus.Cancelled.storageValue } +
visibleRouteActions.count { it.status != RouteActionStatus.Confirmed } + pendingDispatchUploads + pendingGpsPoints
visibleRouteActions.count { it.status != RouteActionStatus.Confirmed } + pendingDispatchUploads
val gpsQueueNeedsAttention: Boolean
get() = isOnline && oldestPendingGpsAtEpochMillis?.let { System.currentTimeMillis() - it >= 2 * 60 * 1000L } == true
val pendingOperationalItems: Int
get() = pendingUserOperationalItems + if (!isOnline || serverConnectionState == ServerConnectionState.Degraded || gpsQueueNeedsAttention) pendingGpsPoints else 0
val operationalQueueNeedsAttention: Boolean
get() = queuedPhotoUploads.any { it.status == PhotoUploadStatus.FailedPermanent.storageValue } ||
visibleRouteActions.any { it.status == RouteActionStatus.FailedPermanent || it.status == RouteActionStatus.FailedConflict }
visibleRouteActions.any { it.status == RouteActionStatus.FailedPermanent || it.status == RouteActionStatus.FailedConflict } ||
gpsQueueNeedsAttention
}
internal fun DriverUiState.withApiError(throwable: Throwable): DriverUiState {
@@ -240,6 +248,11 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
_state.update { it.copy(pendingGpsPoints = count) }
}
}
viewModelScope.launch {
DriverDatabase.get(application).routePointDao().observeOldestUnsentAt().collect { oldest ->
_state.update { it.copy(oldestPendingGpsAtEpochMillis = oldest) }
}
}
viewModelScope.launch {
liveSyncClient.connectionState.collect { connectionState ->
DriverRuntimeSyncState.realtimeConnected = connectionState == LiveSyncConnectionState.Connected
@@ -405,6 +418,9 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
if (_state.value.isOnline && !cached.stale) registerPushTokenIfAvailable(driverId)
}
observeDispatchSheetUploads(response.dispatchSheetReminder?.workDate)
if (_state.value.isOnline && !cached.stale && settings?.selectedDate == settings?.today) {
reconcileTrackingService(response.routes.today, response.session.driver)
}
}.onFailure { throwable ->
if (generation != routesRequestGeneration) return@onFailure
reportHandledException("load_routes", throwable, mapOf("date" to date))
@@ -457,6 +473,9 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
}
observePhotoUploads(routeId)
observeRouteActions(routeId)
if (_state.value.isOnline && !cached.stale) {
reconcileTrackingServiceForRoute(response.route, _state.value.driver)
}
}
fun refreshSelectedRoute() {
@@ -484,6 +503,9 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
error = null,
)
}
if (_state.value.isOnline && !cached.stale) {
reconcileTrackingServiceForRoute(response.route, _state.value.driver)
}
}
.onFailure { throwable ->
if (generation == routeDetailRequestGeneration) _state.update { it.withApiError(throwable) }
@@ -1389,6 +1411,35 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
}
}
private fun reconcileTrackingService(routes: List<DriverRouteDto>, driver: DriverDto) {
val activeRoute = routes.firstOrNull { it.status == "W TRAKCIE" || it.trackingStatus == "active" }
if (activeRoute == null) {
ActiveRouteTrackingService.stop(getApplication())
} else {
ActiveRouteTrackingService.start(
context = getApplication(),
route = activeRoute,
driver = driver,
progressEnabled = _state.value.routeProgressNotificationEnabled,
)
}
}
private fun reconcileTrackingServiceForRoute(route: DriverRouteDto, driver: DriverDto?) {
if (route.status == "W TRAKCIE" || route.trackingStatus == "active") {
if (driver != null) {
ActiveRouteTrackingService.start(
context = getApplication(),
route = route,
driver = driver,
progressEnabled = _state.value.routeProgressNotificationEnabled,
)
}
} else {
ActiveRouteTrackingService.stop(getApplication(), route.id)
}
}
private fun observeRouteActions(routeId: String) {
routeActionsJob?.cancel()
routeActionsJob = viewModelScope.launch {