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 c441356..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 @@ -11,16 +11,20 @@ import io.herow.sdk.detection.location.LocationDispatcher class GeofencingReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { - val geofencingEvent = GeofencingEvent.fromIntent(intent!!) - if (geofencingEvent != null) { - if (!geofencingEvent.hasError()) { - val location = geofencingEvent.triggeringLocation - if (location != null) { - LocationDispatcher.dispatchLocation(location) - } + intent?.let { + val geofencingEvent = GeofencingEvent.fromIntent(intent) + treat(context,geofencingEvent) + } + } + + private fun treat(context: Context?, event: GeofencingEvent?) { + event?.let { + if (!it.hasError()) { + val location = it.triggeringLocation + LocationDispatcher.dispatchLocation(location) } else { val errorMessage: String = - GeofenceStatusCodes.getStatusCodeString(geofencingEvent.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 e31fcb9..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 @@ -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 : $it" + ) + if (lastLocation != null) { + GlobalLogger.shared.info(null, "LastLocation is: $lastLocation") + GlobalLogger.shared.info(null, "NewLocation is: $it") - GlobalLogger.shared.info(null, "Value of skip: $skip") + skip = + if (lastLocation!!.latitude != it.latitude && lastLocation!!.longitude != newLocation.longitude) { + GlobalLogger.shared.info(null, "New and Last locations are different") + val distance = it.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 = it.time - lastLocation!!.time + val timeInSeconds = time / 1000 + GlobalLogger.shared.info(null, "Time is: $timeInSeconds") + + distance < 10 && it.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(it) + } + + lastLocation = it + } 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 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) }