Skip to content

Commit

Permalink
Clean up location manager after making updates suspend (#4818)
Browse files Browse the repository at this point in the history
* Clean up location manager after making updates suspend

* Restore launch blocks for location updates
  • Loading branch information
dshokouhi authored Jan 21, 2025
1 parent 3bc14e0 commit f4098f2
Showing 1 changed file with 54 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,14 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
forceHighAccuracyModeOn = turnOn
forceHighAccuracyModeOff = false
setHighAccuracyModeSetting(latestContext, turnOn)
ioScope.launch {
setupBackgroundLocation()
}
setupBackgroundLocation()
}

MessagingManager.FORCE_OFF -> {
Log.d(TAG, "High accuracy mode forced off")
forceHighAccuracyModeOn = false
forceHighAccuracyModeOff = true
ioScope.launch {
setupBackgroundLocation()
}
setupBackgroundLocation()
}

MessagingManager.HIGH_ACCURACY_SET_UPDATE_INTERVAL -> {
Expand All @@ -268,53 +264,52 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
val zoneEnabled = isEnabled(latestContext, zoneLocation)
val zoneServers = getEnabledServers(latestContext, zoneLocation)

ioScope.launch {
try {
if (!backgroundEnabled && !zoneEnabled) {
removeAllLocationUpdateRequests()
isBackgroundLocationSetup = false
isZoneLocationSetup = false
}
if (!zoneEnabled && isZoneLocationSetup) {
removeGeofenceUpdateRequests()
isZoneLocationSetup = false
}
if (!backgroundEnabled && isBackgroundLocationSetup) {
removeBackgroundUpdateRequests()
stopHighAccuracyService()
isBackgroundLocationSetup = false
}
if (zoneEnabled && !isZoneLocationSetup) {
isZoneLocationSetup = true
requestZoneUpdates()
}
if (zoneEnabled && isZoneLocationSetup && geofenceRegistered != zoneServers) {
Log.d(TAG, "Zone enabled servers changed. Reconfigure zones.")
removeGeofenceUpdateRequests()
requestZoneUpdates()
}

val now = System.currentTimeMillis()
if (
(!highAccuracyModeEnabled && isBackgroundLocationSetup) &&
(lastLocationReceived.all { (it.value + (DEFAULT_LOCATION_MAX_WAIT_TIME * 2L)) < now })
) {
Log.d(TAG, "Background location updates appear to have stopped, restarting location updates")
isBackgroundLocationSetup = false
removeBackgroundUpdateRequests()
} else if (
highAccuracyModeEnabled &&
(lastLocationReceived.all { (it.value + (getHighAccuracyModeUpdateInterval().toLong() * 2000L)) < now })
) {
Log.d(TAG, "High accuracy mode appears to have stopped, restarting high accuracy mode")
isBackgroundLocationSetup = false
stopHighAccuracyService()
}
try {
if (!backgroundEnabled && !zoneEnabled) {
removeAllLocationUpdateRequests()
isBackgroundLocationSetup = false
isZoneLocationSetup = false
}
if (!zoneEnabled && isZoneLocationSetup) {
removeGeofenceUpdateRequests()
isZoneLocationSetup = false
}
if (!backgroundEnabled && isBackgroundLocationSetup) {
removeBackgroundUpdateRequests()
stopHighAccuracyService()
isBackgroundLocationSetup = false
}
if (zoneEnabled && !isZoneLocationSetup) {
isZoneLocationSetup = true
requestZoneUpdates()
}
if (zoneEnabled && isZoneLocationSetup && geofenceRegistered != zoneServers) {
Log.d(TAG, "Zone enabled servers changed. Reconfigure zones.")
removeGeofenceUpdateRequests()
requestZoneUpdates()
}

setupBackgroundLocation(backgroundEnabled, zoneEnabled)
} catch (e: Exception) {
Log.e(TAG, "Issue setting up location tracking", e)
val now = System.currentTimeMillis()
if (
(!highAccuracyModeEnabled && isBackgroundLocationSetup) &&
(lastLocationReceived.all { (it.value + (DEFAULT_LOCATION_MAX_WAIT_TIME * 2L)) < now })
) {
Log.d(TAG, "Background location updates appear to have stopped, restarting location updates")
isBackgroundLocationSetup = false
fusedLocationProviderClient?.flushLocations()
removeBackgroundUpdateRequests()
} else if (
highAccuracyModeEnabled &&
(lastLocationReceived.all { (it.value + (getHighAccuracyModeUpdateInterval().toLong() * 2000L)) < now })
) {
Log.d(TAG, "High accuracy mode appears to have stopped, restarting high accuracy mode")
isBackgroundLocationSetup = false
stopHighAccuracyService()
}

setupBackgroundLocation(backgroundEnabled, zoneEnabled)
} catch (e: Exception) {
Log.e(TAG, "Issue setting up location tracking", e)
}
}

Expand Down Expand Up @@ -823,15 +818,11 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
requestSingleAccurateLocation()
} else {
getEnabledServers(latestContext, zoneLocation).forEach {
ioScope.launch {
sendLocationUpdate(geofencingEvent.triggeringLocation!!, it, trigger)
}
ioScope.launch { sendLocationUpdate(geofencingEvent.triggeringLocation!!, it, trigger) }
}
}

ioScope.launch {
setupBackgroundLocation()
}
setupBackgroundLocation()
}

private suspend fun sendLocationUpdate(location: Location, serverId: Int, trigger: LocationUpdateTrigger?) {
Expand Down Expand Up @@ -1001,9 +992,15 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
configuredZones.forEach {
addGeofenceToBuilder(geofencingRequestBuilder, serverId, it)
geofenceCount++
if (geofenceCount >= 100) {
return@async
}
if (highAccuracyTriggerRange > 0 && highAccuracyZones.contains("${serverId}_${it.entityId}")) {
addGeofenceToBuilder(geofencingRequestBuilder, serverId, it, highAccuracyTriggerRange)
geofenceCount++
if (geofenceCount >= 100) {
return@async
}
}
}
geofenceRegistered.add(serverId)
Expand Down

0 comments on commit f4098f2

Please sign in to comment.