Skip to content

Commit

Permalink
Merge pull request #74 from Woosmap/fix/replaceGeofence
Browse files Browse the repository at this point in the history
Replace geofence method
  • Loading branch information
Llumbroso committed Mar 10, 2022
2 parents cb62e2b + d622f3e commit 56bddbb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ public void removeGeofences(String id) {
positionsManager.removeGeofence(id);
}

public void replaceGeofenceIsochrone(String oldId, String newId, final LatLng latLng, final float radius, final String idStore) {
positionsManager.replaceGeofence(oldId, newId, radius,latLng.latitude,latLng.longitude,idStore );
public void replaceGeofence(String oldId, String newId, final LatLng latLng, final float radius, final String type) {
if(type.equals( "circle" )) {
mGeofencingClient.removeGeofences( Collections.singletonList( oldId ) );
Geofence geofence = geofenceHelper.getGeofence( newId, latLng, radius, Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT );
GeofencingRequest geofencingRequest = geofenceHelper.getGeofencingRequest( geofence );
positionsManager.replaceGeofenceCircle( oldId, geofenceHelper, geofencingRequest, getGeofencePendingIntent(), mGeofencingClient, newId, radius, latLng.latitude, latLng.longitude);
} else {
positionsManager.replaceGeofence( oldId, newId, radius, latLng.latitude, latLng.longitude, type );
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1366,16 +1366,21 @@ class PositionsManager(val context: Context, private val db: WoosmapDb) {
region.type = "isochrone"

Thread {
this.db.regionsDAO.deleteRegionFromId(oldId)
this.db.regionsDAO.createRegion(region)
val regionDB = this.db.regionsDAO.getRegionFromId(oldId)
if(regionDB == null) {
Log.d(WoosmapSdkTag, "Region to replace not exist id = " + oldId)
} else {
this.db.regionsDAO.deleteRegionFromId(oldId)
this.db.regionsDAO.createRegion(region)

if (Woosmap.getInstance().regionReadyListener != null) {
Woosmap.getInstance().regionReadyListener.RegionReadyCallback(region)
}
if (Woosmap.getInstance().regionReadyListener != null) {
Woosmap.getInstance().regionReadyListener.RegionReadyCallback(region)
}

val lastPosition = this.db.movingPositionsDao.getLastMovingPosition()
if (lastPosition != null) {
calculateDistanceWithRegion(lastPosition,this.db.regionsDAO.regionIsochrone)
val lastPosition = this.db.movingPositionsDao.getLastMovingPosition()
if (lastPosition != null) {
calculateDistanceWithRegion(lastPosition, this.db.regionsDAO.regionIsochrone)
}
}

}.start()
Expand Down Expand Up @@ -1416,4 +1421,45 @@ class PositionsManager(val context: Context, private val db: WoosmapDb) {
}.start()
}

@SuppressLint("MissingPermission")
fun replaceGeofenceCircle(
oldId: String,
geofenceHelper: GeofenceHelper,
geofencingRequest: GeofencingRequest,
GeofencePendingIntent: PendingIntent,
mGeofencingClient: GeofencingClient,
newId: String,
radius: Float,
latitude: Double,
longitude: Double,
) {
var region = Region()
region.lat = latitude
region.lng = longitude
region.identifier = newId
region.idStore = ""
region.radius = radius.toDouble()
region.dateTime = System.currentTimeMillis()
region.type = "circle"

Thread {
val regionDB = this.db.regionsDAO.getRegionFromId(oldId)
if(regionDB == null) {
Log.d(WoosmapSdkTag, "Region to replace not exist id = " + oldId)
} else {
this.db.regionsDAO.deleteRegionFromId(oldId)
this.db.regionsDAO.createRegion(region)
mGeofencingClient.addGeofences(geofencingRequest, GeofencePendingIntent).run {
addOnSuccessListener {
Log.d(WoosmapSdkTag,"onSuccess: Geofence Added...")
}
addOnFailureListener {
val errorMessage = geofenceHelper.getErrorString(exception)
Log.d(WoosmapSdkTag,"onFailure "+errorMessage)
}
}
}
}.start()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,12 @@ public void removeGeofence(String id) {
}
public void removeGeofence() { locationManager.removeGeofences();}

public void replaceGeofenceIsochrone(String oldId, String newId, LatLng latLng, float radius){
locationManager.replaceGeofenceIsochrone(oldId, newId, latLng, radius, "");
public void replaceGeofence(String oldId, String newId, LatLng latLng, float radius){
locationManager.replaceGeofence(oldId, newId, latLng, radius, "circle");
}

public void replaceGeofenceIsochrone(String oldId, String newId, LatLng latLng, float radius, String idStore){
locationManager.replaceGeofenceIsochrone(oldId, newId, latLng, radius, idStore);
public void replaceGeofence(String oldId, String newId, LatLng latLng, float radius, String type){
locationManager.replaceGeofence(oldId, newId, latLng, radius, type);
}

// Monitors the state of the connection to the service.
Expand Down

0 comments on commit 56bddbb

Please sign in to comment.