fix: resolve Handler leak, race condition, and duplicate location send in speed calculation
This commit is contained in:
parent
c9e49c6cbd
commit
b5fabd980f
1 changed files with 8 additions and 3 deletions
|
|
@ -57,6 +57,7 @@ class LocationTrackingService : Service() {
|
|||
private var isTracking = false
|
||||
private var isNetworkAvailable = true
|
||||
private var bypassFilterOnce = false
|
||||
private val speedHandler = Handler(Looper.getMainLooper())
|
||||
|
||||
private var currentConfig: TrackingConfig? = null
|
||||
private var pendingSpeedLocation: Location? = null
|
||||
|
|
@ -188,6 +189,7 @@ class LocationTrackingService : Service() {
|
|||
Log.d(TAG, "stopTracking: set tracking_active=false")
|
||||
fusedLocationProvider.stopLocationUpdates()
|
||||
heartbeatScheduler.cancelHeartbeat()
|
||||
speedHandler.removeCallbacksAndMessages(null)
|
||||
releaseWakeLock()
|
||||
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
|
|
@ -236,8 +238,11 @@ class LocationTrackingService : Service() {
|
|||
val nativeSpeed = if (location.hasSpeed()) location.speed else null
|
||||
|
||||
if (nativeSpeed == null || nativeSpeed <= 0) {
|
||||
pendingSpeedLocation = location
|
||||
requestLocationForSpeedCalculation()
|
||||
if (!speedRequestPending) {
|
||||
pendingSpeedLocation = location
|
||||
requestLocationForSpeedCalculation()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val traccarLocation = TraccarLocation(
|
||||
|
|
@ -296,7 +301,7 @@ class LocationTrackingService : Service() {
|
|||
if (speedRequestPending) return
|
||||
|
||||
speedRequestPending = true
|
||||
Handler(Looper.getMainLooper()).postDelayed({
|
||||
speedHandler.postDelayed({
|
||||
fusedLocationProvider.getLastLocation { location ->
|
||||
location?.let {
|
||||
val calculatedSpeed = distanceFilterProcessor.getCalculatedSpeed(it)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue