From bef82c1da5e552c150d9fc134a146ed6a45d6df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Mon, 11 Sep 2023 17:56:25 +0200 Subject: [PATCH] Prevent GeofencingRequest exception on no zones --- .../android/sensors/LocationSensorManager.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt index dce74006525..78c8a585a50 100644 --- a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt +++ b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt @@ -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) } @@ -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(