From f764e339448fa4c4ad694b7f13a0f6390d01afec Mon Sep 17 00:00:00 2001 From: fiatcode Date: Thu, 30 Apr 2026 13:12:13 +0700 Subject: [PATCH] fix: read config from SharedPreferences and persist tracking state --- .../service/LocationTrackingService.kt | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/kotlin/com/traccar/traccar_client/service/LocationTrackingService.kt b/android/app/src/main/kotlin/com/traccar/traccar_client/service/LocationTrackingService.kt index 9855d3f..482170d 100644 --- a/android/app/src/main/kotlin/com/traccar/traccar_client/service/LocationTrackingService.kt +++ b/android/app/src/main/kotlin/com/traccar/traccar_client/service/LocationTrackingService.kt @@ -7,6 +7,7 @@ import android.app.PendingIntent import android.app.Service import android.content.Context import android.content.Intent +import android.content.SharedPreferences import android.location.Location import android.os.Binder import android.os.Build @@ -43,6 +44,7 @@ class LocationTrackingService : Service() { private lateinit var database: AppDatabase private lateinit var httpClient: TraccarHttpClient private lateinit var connectivityReceiver: ConnectivityReceiver + private lateinit var prefs: SharedPreferences private var wakeLock: PowerManager.WakeLock? = null private var isTracking = false @@ -56,6 +58,7 @@ class LocationTrackingService : Service() { override fun onCreate() { super.onCreate() + prefs = getSharedPreferences("traccar_prefs", Context.MODE_PRIVATE) fusedLocationProvider = FusedLocationProvider(this) distanceFilterProcessor = DistanceFilterProcessor() heartbeatScheduler = HeartbeatScheduler(this) @@ -89,7 +92,17 @@ class LocationTrackingService : Service() { fun startTracking() { if (isTracking) return - val config = currentConfig ?: TrackingConfig() + val config = TrackingConfig( + serverUrl = prefs.getString("server_url", "https://demo.traccar.org") ?: "https://demo.traccar.org", + deviceId = prefs.getString("device_id", "") ?: "", + accuracy = prefs.getInt("accuracy", 0), + interval = prefs.getInt("interval", 300), + fastestInterval = prefs.getInt("fastest_interval", 30), + distanceFilter = prefs.getInt("distance_filter", 75), + heartbeat = prefs.getInt("heartbeat", 60), + offlineBuffer = prefs.getBoolean("offline_buffer", true), + stopDetection = prefs.getBoolean("stop_detection", true) + ) currentConfig = config acquireWakeLock() @@ -97,6 +110,7 @@ class LocationTrackingService : Service() { isTracking = true distanceFilterProcessor.reset() + prefs.edit().putBoolean("tracking_active", true).apply() fusedLocationProvider.startLocationUpdates( interval = config.interval * 1000L, @@ -119,6 +133,7 @@ class LocationTrackingService : Service() { if (!isTracking) return isTracking = false + prefs.edit().putBoolean("tracking_active", false).apply() fusedLocationProvider.stopLocationUpdates() heartbeatScheduler.cancelHeartbeat() releaseWakeLock() @@ -176,6 +191,17 @@ class LocationTrackingService : Service() { "timestamp" to location.time )) + prefs.edit().apply { + putFloat("last_latitude", location.latitude.toFloat()) + putFloat("last_longitude", location.longitude.toFloat()) + if (location.hasAccuracy()) putFloat("last_accuracy", location.accuracy) + if (location.hasSpeed() && location.speed > 0) putFloat("last_speed", location.speed) + if (location.hasBearing() && location.bearing >= 0) putFloat("last_heading", location.bearing) + if (location.hasAltitude() && location.altitude != 0.0) putFloat("last_altitude", location.altitude.toFloat()) + putLong("last_timestamp", location.time) + apply() + } + if (isNetworkAvailable) { sendLocationToServer(traccarLocation) } else {