Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down