Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent GeofencingRequest exception on no zones #3857

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,14 @@ class LocationSensorManager : LocationSensorManagerBase() {
geofencingClient = LocationServices.getGeofencingClient(latestContext)
val intent = getLocationUpdateIntent(true)
val geofencingRequest = createGeofencingRequest()
geofencingClient?.addGeofences(
geofencingRequest,
intent
)
if (geofencingRequest != null) {
geofencingClient?.addGeofences(
geofencingRequest,
intent
)
} else {
Log.w(TAG, "No zones, skipping zone based location updates")
}
} catch (e: Exception) {
Log.e(TAG, "Issue requesting zone updates.", e)
}
Expand Down Expand Up @@ -934,26 +938,29 @@ class LocationSensorManager : LocationSensorManagerBase() {
return zones[serverId] ?: emptyArray()
}

private suspend fun createGeofencingRequest(): GeofencingRequest {
private suspend fun createGeofencingRequest(): GeofencingRequest? {
val geofencingRequestBuilder = GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)

val highAccuracyTriggerRange = getHighAccuracyModeTriggerRange()
val highAccuracyZones = getHighAccuracyModeZones(false)

var geofenceCount = 0
getEnabledServers(latestContext, zoneLocation).map { serverId ->
ioScope.async {
val configuredZones = getZones(serverId, forceRefresh = true)
configuredZones.forEach {
addGeofenceToBuilder(geofencingRequestBuilder, serverId, it)
geofenceCount++
if (highAccuracyTriggerRange > 0 && highAccuracyZones.contains("${serverId}_${it.entityId}")) {
addGeofenceToBuilder(geofencingRequestBuilder, serverId, it, highAccuracyTriggerRange)
geofenceCount++
}
}
geofenceRegistered.add(serverId)
}
}.awaitAll()
return geofencingRequestBuilder.build()
return if (geofenceCount > 0) geofencingRequestBuilder.build() else null
}

private fun addGeofenceToBuilder(
Expand Down