fix: read config from SharedPreferences and persist tracking state
This commit is contained in:
parent
4365084625
commit
f764e33944
1 changed files with 27 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ import android.app.PendingIntent
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.location.Location
|
import android.location.Location
|
||||||
import android.os.Binder
|
import android.os.Binder
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
|
@ -43,6 +44,7 @@ class LocationTrackingService : Service() {
|
||||||
private lateinit var database: AppDatabase
|
private lateinit var database: AppDatabase
|
||||||
private lateinit var httpClient: TraccarHttpClient
|
private lateinit var httpClient: TraccarHttpClient
|
||||||
private lateinit var connectivityReceiver: ConnectivityReceiver
|
private lateinit var connectivityReceiver: ConnectivityReceiver
|
||||||
|
private lateinit var prefs: SharedPreferences
|
||||||
|
|
||||||
private var wakeLock: PowerManager.WakeLock? = null
|
private var wakeLock: PowerManager.WakeLock? = null
|
||||||
private var isTracking = false
|
private var isTracking = false
|
||||||
|
|
@ -56,6 +58,7 @@ class LocationTrackingService : Service() {
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
prefs = getSharedPreferences("traccar_prefs", Context.MODE_PRIVATE)
|
||||||
fusedLocationProvider = FusedLocationProvider(this)
|
fusedLocationProvider = FusedLocationProvider(this)
|
||||||
distanceFilterProcessor = DistanceFilterProcessor()
|
distanceFilterProcessor = DistanceFilterProcessor()
|
||||||
heartbeatScheduler = HeartbeatScheduler(this)
|
heartbeatScheduler = HeartbeatScheduler(this)
|
||||||
|
|
@ -89,7 +92,17 @@ class LocationTrackingService : Service() {
|
||||||
fun startTracking() {
|
fun startTracking() {
|
||||||
if (isTracking) return
|
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
|
currentConfig = config
|
||||||
|
|
||||||
acquireWakeLock()
|
acquireWakeLock()
|
||||||
|
|
@ -97,6 +110,7 @@ class LocationTrackingService : Service() {
|
||||||
|
|
||||||
isTracking = true
|
isTracking = true
|
||||||
distanceFilterProcessor.reset()
|
distanceFilterProcessor.reset()
|
||||||
|
prefs.edit().putBoolean("tracking_active", true).apply()
|
||||||
|
|
||||||
fusedLocationProvider.startLocationUpdates(
|
fusedLocationProvider.startLocationUpdates(
|
||||||
interval = config.interval * 1000L,
|
interval = config.interval * 1000L,
|
||||||
|
|
@ -119,6 +133,7 @@ class LocationTrackingService : Service() {
|
||||||
if (!isTracking) return
|
if (!isTracking) return
|
||||||
|
|
||||||
isTracking = false
|
isTracking = false
|
||||||
|
prefs.edit().putBoolean("tracking_active", false).apply()
|
||||||
fusedLocationProvider.stopLocationUpdates()
|
fusedLocationProvider.stopLocationUpdates()
|
||||||
heartbeatScheduler.cancelHeartbeat()
|
heartbeatScheduler.cancelHeartbeat()
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
|
|
@ -176,6 +191,17 @@ class LocationTrackingService : Service() {
|
||||||
"timestamp" to location.time
|
"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) {
|
if (isNetworkAvailable) {
|
||||||
sendLocationToServer(traccarLocation)
|
sendLocationToServer(traccarLocation)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue