From 3b8e79598bc4eecb73db77b29d7a6dd911f819e6 Mon Sep 17 00:00:00 2001 From: damien Date: Fri, 4 Feb 2022 10:19:57 +0100 Subject: [PATCH 1/2] Refactor --- .../geofencing/GeofencingReceiver.kt | 26 ++++--- .../detection/location/LocationDispatcher.kt | 72 ++++++++++--------- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt b/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt index c82d095..921f77a 100644 --- a/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt +++ b/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt @@ -11,16 +11,24 @@ import io.herow.sdk.detection.location.LocationDispatcher class GeofencingReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { - val geofencingEvent = GeofencingEvent.fromIntent(intent!!) - if (!geofencingEvent.hasError()) { - val location = geofencingEvent.triggeringLocation - LocationDispatcher.dispatchLocation(location) - } else { - val errorMessage: String = - GeofenceStatusCodes.getStatusCodeString(geofencingEvent.errorCode) - GlobalLogger.shared.error(context, "Error message is: $errorMessage") + intent?.let { + val geofencingEvent = GeofencingEvent.fromIntent(intent) + treat(context,geofencingEvent) + } + } + + private fun treat(context: Context?, event: GeofencingEvent?) { + event?.let { + if (!event.hasError()) { + val location = event.triggeringLocation + LocationDispatcher.dispatchLocation(location) + } else { + val errorMessage: String = + GeofenceStatusCodes.getStatusCodeString(event.errorCode) + GlobalLogger.shared.error(context, "Error message is: $errorMessage") + println(errorMessage) + } - println(errorMessage) } } } \ No newline at end of file diff --git a/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt b/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt index e31fcb9..93b73ea 100644 --- a/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt +++ b/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt @@ -18,45 +18,47 @@ object LocationDispatcher { /** * Update location only if distance is >20 m or if distance is <20 & time is >5 minutes */ - fun dispatchLocation(newLocation: Location) { - var skip = false - GlobalLogger.shared.debug( - null, - " try to dispatchLocation : $newLocation" - ) - if (lastLocation != null) { - GlobalLogger.shared.info(null, "LastLocation is: $lastLocation") - GlobalLogger.shared.info(null, "NewLocation is: $newLocation") - - skip = - if (lastLocation!!.latitude != newLocation.latitude && lastLocation!!.longitude != newLocation.longitude) { - GlobalLogger.shared.info(null, "New and Last locations are different") - val distance = newLocation.distanceTo(lastLocation!!) - - GlobalLogger.shared.info(null, "Distance is: $distance") - val time = newLocation.time - lastLocation!!.time - val timeInSeconds = time / 1000 - GlobalLogger.shared.info(null, "Time is: $timeInSeconds") - - distance < 10 && newLocation.time - lastLocation!!.time < TimeHelper.FIVE_SECONDS_MS - } else { - true - } - } + fun dispatchLocation(newLocation: Location?) { + newLocation?.let { + var skip = false + GlobalLogger.shared.debug( + null, + " try to dispatchLocation : $newLocation" + ) + if (lastLocation != null) { + GlobalLogger.shared.info(null, "LastLocation is: $lastLocation") + GlobalLogger.shared.info(null, "NewLocation is: $newLocation") - GlobalLogger.shared.info(null, "Value of skip: $skip") + skip = + if (lastLocation!!.latitude != newLocation.latitude && lastLocation!!.longitude != newLocation.longitude) { + GlobalLogger.shared.info(null, "New and Last locations are different") + val distance = newLocation.distanceTo(lastLocation!!) - if (!skip || skipCount > 5) { - skipCount = 0 - for (locationListener in locationListeners) { - GlobalLogger.shared.info(null, "Dispatching location to: $locationListener") - locationListener.onLocationUpdate(newLocation) + GlobalLogger.shared.info(null, "Distance is: $distance") + val time = newLocation.time - lastLocation!!.time + val timeInSeconds = time / 1000 + GlobalLogger.shared.info(null, "Time is: $timeInSeconds") + + distance < 10 && newLocation.time - lastLocation!!.time < TimeHelper.FIVE_SECONDS_MS + } else { + true + } } - lastLocation = newLocation - } else { - skipCount += 1 + GlobalLogger.shared.info(null, "Value of skip: $skip") + + if (!skip || skipCount > 5) { + skipCount = 0 + for (locationListener in locationListeners) { + GlobalLogger.shared.info(null, "Dispatching location to: $locationListener") + locationListener.onLocationUpdate(newLocation) + } + + lastLocation = newLocation + } else { + skipCount += 1 + } + GlobalLogger.shared.info(null, "End value of skip: $skip") } - GlobalLogger.shared.info(null, "End value of skip: $skip") } } \ No newline at end of file From 2d60626956195a2a43fcf09a043c6b36ca07fb91 Mon Sep 17 00:00:00 2001 From: damien Date: Fri, 4 Feb 2022 15:07:40 +0100 Subject: [PATCH 2/2] Udapte lib and refactor --- build.gradle | 2 +- .../detection/geofencing/GeofencingReceiver.kt | 6 +++--- .../sdk/detection/location/LocationDispatcher.kt | 16 ++++++++-------- .../sdk/detection/location/LocationManager.kt | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 118fed3..9687515 100644 --- a/build.gradle +++ b/build.gradle @@ -101,7 +101,7 @@ subprojects { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" - implementation "com.google.android.gms:play-services-location:18.0.0" + implementation "com.google.android.gms:play-services-location:19.0.1" // Retrofit for API call implementation "com.squareup.retrofit2:retrofit:$retrofit_version" diff --git a/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt b/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt index c1933a0..9b561c8 100644 --- a/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt +++ b/detection/src/main/java/io/herow/sdk/detection/geofencing/GeofencingReceiver.kt @@ -19,12 +19,12 @@ class GeofencingReceiver : BroadcastReceiver() { private fun treat(context: Context?, event: GeofencingEvent?) { event?.let { - if (!event.hasError()) { - val location = event.triggeringLocation + if (!it.hasError()) { + val location = it.triggeringLocation LocationDispatcher.dispatchLocation(location) } else { val errorMessage: String = - GeofenceStatusCodes.getStatusCodeString(event.errorCode) + GeofenceStatusCodes.getStatusCodeString(it.errorCode) GlobalLogger.shared.error(context, "Error message is: $errorMessage") println(errorMessage) } diff --git a/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt b/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt index 93b73ea..74a2ce5 100644 --- a/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt +++ b/detection/src/main/java/io/herow/sdk/detection/location/LocationDispatcher.kt @@ -23,23 +23,23 @@ object LocationDispatcher { var skip = false GlobalLogger.shared.debug( null, - " try to dispatchLocation : $newLocation" + " try to dispatchLocation : $it" ) if (lastLocation != null) { GlobalLogger.shared.info(null, "LastLocation is: $lastLocation") - GlobalLogger.shared.info(null, "NewLocation is: $newLocation") + GlobalLogger.shared.info(null, "NewLocation is: $it") skip = - if (lastLocation!!.latitude != newLocation.latitude && lastLocation!!.longitude != newLocation.longitude) { + if (lastLocation!!.latitude != it.latitude && lastLocation!!.longitude != newLocation.longitude) { GlobalLogger.shared.info(null, "New and Last locations are different") - val distance = newLocation.distanceTo(lastLocation!!) + val distance = it.distanceTo(lastLocation!!) GlobalLogger.shared.info(null, "Distance is: $distance") - val time = newLocation.time - lastLocation!!.time + val time = it.time - lastLocation!!.time val timeInSeconds = time / 1000 GlobalLogger.shared.info(null, "Time is: $timeInSeconds") - distance < 10 && newLocation.time - lastLocation!!.time < TimeHelper.FIVE_SECONDS_MS + distance < 10 && it.time - lastLocation!!.time < TimeHelper.FIVE_SECONDS_MS } else { true } @@ -51,10 +51,10 @@ object LocationDispatcher { skipCount = 0 for (locationListener in locationListeners) { GlobalLogger.shared.info(null, "Dispatching location to: $locationListener") - locationListener.onLocationUpdate(newLocation) + locationListener.onLocationUpdate(it) } - lastLocation = newLocation + lastLocation = it } else { skipCount += 1 } diff --git a/detection/src/main/java/io/herow/sdk/detection/location/LocationManager.kt b/detection/src/main/java/io/herow/sdk/detection/location/LocationManager.kt index 546940b..fb00706 100644 --- a/detection/src/main/java/io/herow/sdk/detection/location/LocationManager.kt +++ b/detection/src/main/java/io/herow/sdk/detection/location/LocationManager.kt @@ -46,10 +46,10 @@ class LocationManager( init { locationCallback = object : LocationCallback() { - override fun onLocationResult(locationResult: LocationResult?) { - locationResult ?: return - if (locationResult.locations.isNotEmpty()) { - val lastLocation = locationResult.locations.last() + override fun onLocationResult(p0: LocationResult) { + p0 ?: return + if (p0.locations.isNotEmpty()) { + val lastLocation = p0.locations.last() LocationDispatcher.dispatchLocation(lastLocation) updateMonitoring(lastLocation) }