Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresPermission
import com.google.android.gms.location.LocationServices
import timber.log.Timber
import org.owntracks.android.location.geofencing.GeofencingClient
import org.owntracks.android.location.geofencing.GeofencingRequest
import org.owntracks.android.services.BackgroundService
Expand All @@ -15,11 +16,17 @@ class GMSGeofencingClient(
) : GeofencingClient {
override fun removeGeofences(context: Context) {
this.geofencingClient.removeGeofences(getPendingIntent(context))
.addOnSuccessListener { Timber.d("Geofences removed successfully") }
.addOnFailureListener { Timber.e(it, "Failed to remove geofences") }
}

@RequiresPermission(anyOf = ["android.permission.ACCESS_FINE_LOCATION"])
override fun addGeofences(request: GeofencingRequest, context: Context) {
this.geofencingClient.addGeofences(request.toGMSGeofencingRequest(), getPendingIntent(context))
val gmsRequest = request.toGMSGeofencingRequest()
Timber.d("Adding ${gmsRequest.geofences?.size ?: 0} geofences via GMS")
this.geofencingClient.addGeofences(gmsRequest, getPendingIntent(context))
.addOnSuccessListener { Timber.i("Geofences added successfully") }
.addOnFailureListener { Timber.e(it, "Failed to add geofences") }
}

private fun getPendingIntent(context: Context): PendingIntent {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.owntracks.android.gms.location.geofencing

import android.os.SystemClock
import org.owntracks.android.location.geofencing.Geofence
import org.owntracks.android.location.geofencing.GeofencingRequest

Expand All @@ -24,7 +25,10 @@ fun Geofence.toGMSGeofence(): com.google.android.gms.location.Geofence {
}
}
}
this.expirationDuration?.run(builder::setExpirationDuration)
this.expirationDuration?.run {
builder.setExpirationDuration(
minOf(this, Long.MAX_VALUE - SystemClock.elapsedRealtime()))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to set the explicit GMS NEVER_EXPIRE value if this is Long.MAX_VALUE?

  if (this == org.owntracks.android.location.geofencing.Geofence.NEVER_EXPIRE)
             com.google.android.gms.location.Geofence.NEVER_EXPIRE  // -1L, GMS sentinel
         else
             this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes more sense.
I got confused and didn't realize there are 2 different NEVER_EXPIRE constants

}
this.transitionTypes?.run(builder::setTransitionTypes)
this.notificationResponsiveness?.run(builder::setNotificationResponsiveness)
this.loiteringDelay?.run(builder::setLoiteringDelay)
Expand Down