From 0bdf9c8d6faa3b34f0324e970d4c669edec94c00 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 19 Mar 2020 13:59:22 -0500 Subject: [PATCH] One geofence per place --- .../java/org/tasks/data/LocationDaoTest.kt | 67 ++++++++++++- .../java/org/tasks/makers/GeofenceMaker.kt | 31 ++++++ .../java/org/tasks/makers/PlaceMaker.kt | 3 + .../java/org/tasks/location/GeofenceApi.java | 94 +++++++------------ .../GeofenceTransitionsIntentService.java | 22 +++-- .../tasks/activities/PlaceSettingsActivity.kt | 2 +- .../main/java/org/tasks/caldav/iCalendar.kt | 9 +- .../main/java/org/tasks/data/LocationDao.java | 51 ++++++++-- .../java/org/tasks/data/MergedGeofence.java | 52 ++++++++++ .../java/org/tasks/jobs/AfterSaveWork.java | 6 +- .../main/java/org/tasks/jobs/CleanupWork.java | 8 +- .../notifications/NotificationManager.java | 8 +- .../java/org/tasks/ui/LocationControlSet.java | 4 +- 13 files changed, 269 insertions(+), 88 deletions(-) create mode 100644 app/src/androidTest/java/org/tasks/makers/GeofenceMaker.kt create mode 100644 app/src/main/java/org/tasks/data/MergedGeofence.java diff --git a/app/src/androidTest/java/org/tasks/data/LocationDaoTest.kt b/app/src/androidTest/java/org/tasks/data/LocationDaoTest.kt index d887a513e..b5f273952 100644 --- a/app/src/androidTest/java/org/tasks/data/LocationDaoTest.kt +++ b/app/src/androidTest/java/org/tasks/data/LocationDaoTest.kt @@ -2,21 +2,30 @@ package org.tasks.data import androidx.test.ext.junit.runners.AndroidJUnit4 import com.natpryce.makeiteasy.MakeItEasy.with -import org.junit.Assert.assertEquals +import com.todoroo.astrid.dao.TaskDao +import org.junit.Assert.* import org.junit.Test import org.junit.runner.RunWith import org.tasks.caldav.GeoUtils.toLikeString +import org.tasks.date.DateTimeUtils.newDateTime import org.tasks.injection.InjectingTestCase import org.tasks.injection.TestComponent +import org.tasks.makers.GeofenceMaker.ARRIVAL +import org.tasks.makers.GeofenceMaker.DEPARTURE +import org.tasks.makers.GeofenceMaker.PLACE +import org.tasks.makers.GeofenceMaker.TASK +import org.tasks.makers.GeofenceMaker.newGeofence import org.tasks.makers.PlaceMaker.LATITUDE import org.tasks.makers.PlaceMaker.LONGITUDE import org.tasks.makers.PlaceMaker.newPlace +import org.tasks.makers.TaskMaker.* import javax.inject.Inject @RunWith(AndroidJUnit4::class) class LocationDaoTest : InjectingTestCase() { @Inject lateinit var locationDao: LocationDao + @Inject lateinit var taskDao: TaskDao @Test fun getExistingPlace() { @@ -43,6 +52,62 @@ class LocationDaoTest : InjectingTestCase() { assertEquals(-116.816944, place?.longitude) } + @Test + fun noActiveGeofences() { + val place = newPlace() + locationDao.insert(place) + taskDao.createNew(newTask(with(ID, 1))) + locationDao.insert(newGeofence(with(TASK, 1), with(PLACE, place.uid))) + + assertNull(locationDao.getGeofencesByPlace(place.uid)) + } + + @Test + fun activeArrivalGeofence() { + val place = newPlace() + locationDao.insert(place) + taskDao.createNew(newTask(with(ID, 1))) + locationDao.insert(newGeofence(with(TASK, 1), with(PLACE, place.uid), with(ARRIVAL, true))) + + val geofence = locationDao.getGeofencesByPlace(place.uid) + + assertTrue(geofence.arrival) + assertFalse(geofence.departure) + } + + @Test + fun activeDepartureGeofence() { + val place = newPlace() + locationDao.insert(place) + taskDao.createNew(newTask(with(ID, 1))) + locationDao.insert(newGeofence(with(TASK, 1), with(PLACE, place.uid), with(DEPARTURE, true))) + + val geofence = locationDao.getGeofencesByPlace(place.uid) + + assertFalse(geofence.arrival) + assertTrue(geofence.departure) + } + + @Test + fun geofenceInactiveForCompletedTask() { + val place = newPlace() + locationDao.insert(place) + taskDao.createNew(newTask(with(ID, 1), with(COMPLETION_TIME, newDateTime()))) + locationDao.insert(newGeofence(with(TASK, 1), with(PLACE, place.uid), with(ARRIVAL, true))) + + assertNull(locationDao.getGeofencesByPlace(place.uid)) + } + + @Test + fun geofenceInactiveForDeletedTask() { + val place = newPlace() + locationDao.insert(place) + taskDao.createNew(newTask(with(ID, 1), with(DELETION_TIME, newDateTime()))) + locationDao.insert(newGeofence(with(TASK, 1), with(PLACE, place.uid), with(ARRIVAL, true))) + + assertNull(locationDao.getGeofencesByPlace(place.uid)) + } + override fun inject(component: TestComponent) = component.inject(this) } diff --git a/app/src/androidTest/java/org/tasks/makers/GeofenceMaker.kt b/app/src/androidTest/java/org/tasks/makers/GeofenceMaker.kt new file mode 100644 index 000000000..dbbe971f1 --- /dev/null +++ b/app/src/androidTest/java/org/tasks/makers/GeofenceMaker.kt @@ -0,0 +1,31 @@ +package org.tasks.makers + +import com.natpryce.makeiteasy.Instantiator +import com.natpryce.makeiteasy.Property +import com.natpryce.makeiteasy.PropertyLookup +import com.natpryce.makeiteasy.PropertyValue +import com.todoroo.astrid.helper.UUIDHelper +import org.tasks.data.Geofence +import org.tasks.data.Place + +object GeofenceMaker { + val PLACE: Property = Property.newProperty() + val TASK: Property = Property.newProperty() + val ARRIVAL: Property = Property.newProperty() + val DEPARTURE: Property = Property.newProperty() + val RADIUS: Property = Property.newProperty() + + private val instantiator = Instantiator { lookup: PropertyLookup -> + val geofence = Geofence() + geofence.place = lookup.valueOf(PLACE, "") + geofence.task = lookup.valueOf(TASK, 1) + geofence.isArrival = lookup.valueOf(ARRIVAL, false) + geofence.isDeparture = lookup.valueOf(DEPARTURE, false) + geofence.radius = lookup.valueOf(RADIUS, 250) + geofence + } + + fun newGeofence(vararg properties: PropertyValue): Geofence { + return Maker.make(instantiator, *properties) + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/org/tasks/makers/PlaceMaker.kt b/app/src/androidTest/java/org/tasks/makers/PlaceMaker.kt index 9d4b2be7c..bdcb00537 100644 --- a/app/src/androidTest/java/org/tasks/makers/PlaceMaker.kt +++ b/app/src/androidTest/java/org/tasks/makers/PlaceMaker.kt @@ -4,14 +4,17 @@ import com.natpryce.makeiteasy.Instantiator import com.natpryce.makeiteasy.Property import com.natpryce.makeiteasy.PropertyLookup import com.natpryce.makeiteasy.PropertyValue +import com.todoroo.astrid.helper.UUIDHelper import org.tasks.data.Place object PlaceMaker { val LATITUDE: Property = Property.newProperty() val LONGITUDE: Property = Property.newProperty() + val UUID: Property = Property.newProperty() private val instantiator = Instantiator { lookup: PropertyLookup -> val place = Place() + place.uid = lookup.valueOf(UUID, UUIDHelper.newUUID()) place.latitude = lookup.valueOf(LATITUDE, 0.0) place.longitude = lookup.valueOf(LONGITUDE, 0.0) place diff --git a/app/src/googleplay/java/org/tasks/location/GeofenceApi.java b/app/src/googleplay/java/org/tasks/location/GeofenceApi.java index 4f95d4337..b50904827 100644 --- a/app/src/googleplay/java/org/tasks/location/GeofenceApi.java +++ b/app/src/googleplay/java/org/tasks/location/GeofenceApi.java @@ -1,25 +1,24 @@ package org.tasks.location; import static com.google.android.gms.location.Geofence.NEVER_EXPIRE; -import static com.google.common.base.Predicates.notNull; -import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Lists.transform; -import static java.util.Collections.singletonList; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; -import com.google.android.gms.location.Geofence; +import androidx.annotation.Nullable; +import com.google.android.gms.location.GeofencingClient; import com.google.android.gms.location.GeofencingRequest; import com.google.android.gms.location.GeofencingRequest.Builder; import com.google.android.gms.location.LocationServices; +import com.google.common.collect.ImmutableList; import java.util.List; import javax.inject.Inject; -import org.tasks.data.Location; import org.tasks.data.LocationDao; +import org.tasks.data.MergedGeofence; +import org.tasks.data.Place; import org.tasks.injection.ForApplication; import org.tasks.preferences.PermissionChecker; +import timber.log.Timber; public class GeofenceApi { @@ -38,77 +37,56 @@ public class GeofenceApi { } public void registerAll() { - register(locationDao.getActiveGeofences()); + update(locationDao.getPlacesWithGeofences()); } - public void register(long taskId) { - register(locationDao.getActiveGeofences(taskId)); + public void update(long taskId) { + update(locationDao.getPlaceForTask(taskId)); } - public void register(Location location) { - register(singletonList(location)); + public void update(String place) { + update(locationDao.getPlace(place)); } - private void register(final List locations) { - if (!permissionChecker.canAccessLocation()) { - return; - } - - List requests = getRequests(locations); - if (!requests.isEmpty()) { - LocationServices.getGeofencingClient(context) - .addGeofences( - new Builder().addGeofences(requests).build(), - PendingIntent.getBroadcast( - context, - 0, - new Intent(context, GeofenceTransitionsIntentService.Broadcast.class), - PendingIntent.FLAG_UPDATE_CURRENT)); + private void update(List places) { + for (Place place : places) { + update(place); } } - public void cancel(long taskId) { - cancel(locationDao.getGeofences(taskId)); - } - - public void cancel(final Location location) { - if (location != null) { - cancel(singletonList(location)); - } - } - - public void cancel(final List locations) { - if (!permissionChecker.canAccessLocation()) { + public void update(@Nullable Place place) { + if (place == null || !permissionChecker.canAccessLocation()) { return; } - List requestIds = getRequestIds(locations); - if (!requestIds.isEmpty()) { - LocationServices.getGeofencingClient(context).removeGeofences(requestIds); - } - } - @SuppressWarnings("ConstantConditions") - private List getRequestIds(List locations) { - return transform(newArrayList(filter(locations, notNull())), l -> Long.toString(l.getId())); - } - - private List getRequests(List locations) { - return transform( - newArrayList(filter(locations, l -> l != null && (l.isArrival() || l.isDeparture()))), - this::toGoogleGeofence); + GeofencingClient client = LocationServices.getGeofencingClient(context); + MergedGeofence geofence = locationDao.getGeofencesByPlace(place.getUid()); + if (geofence != null) { + Timber.d("Adding geofence for %s", geofence); + client.addGeofences( + new Builder().addGeofence(toGoogleGeofence(geofence)).build(), + PendingIntent.getBroadcast( + context, + 0, + new Intent(context, GeofenceTransitionsIntentService.Broadcast.class), + PendingIntent.FLAG_UPDATE_CURRENT)); + } else { + Timber.d("Removing geofence for %s", place); + client.removeGeofences(ImmutableList.of(Long.toString(place.getId()))); + } } - private com.google.android.gms.location.Geofence toGoogleGeofence(Location location) { + private com.google.android.gms.location.Geofence toGoogleGeofence(MergedGeofence geofence) { int transitionTypes = 0; - if (location.isArrival()) { + if (geofence.isArrival()) { transitionTypes |= GeofencingRequest.INITIAL_TRIGGER_ENTER; } - if (location.isDeparture()) { + if (geofence.isDeparture()) { transitionTypes |= GeofencingRequest.INITIAL_TRIGGER_EXIT; } return new com.google.android.gms.location.Geofence.Builder() - .setCircularRegion(location.getLatitude(), location.getLongitude(), location.getRadius()) - .setRequestId(Long.toString(location.getId())) + .setCircularRegion(geofence.getLatitude(), geofence.getLongitude(), geofence.getRadius()) + .setRequestId(geofence.getUid()) .setTransitionTypes(transitionTypes) .setExpirationDuration(NEVER_EXPIRE) .build(); diff --git a/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java b/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java index c6726e7f9..6c8eb7e0e 100644 --- a/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java +++ b/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java @@ -4,7 +4,6 @@ import static com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER import static com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT; import static com.todoroo.astrid.reminders.ReminderService.TYPE_GEOFENCE_ENTER; import static com.todoroo.astrid.reminders.ReminderService.TYPE_GEOFENCE_EXIT; -import static java.util.Collections.singletonList; import static org.tasks.time.DateTimeUtils.currentTimeMillis; import android.content.BroadcastReceiver; @@ -12,11 +11,13 @@ import android.content.Context; import android.content.Intent; import androidx.core.app.JobIntentService; import com.google.android.gms.location.GeofencingEvent; +import com.google.common.collect.Lists; import java.util.List; import javax.inject.Inject; import org.tasks.Notifier; -import org.tasks.data.Location; +import org.tasks.data.Geofence; import org.tasks.data.LocationDao; +import org.tasks.data.Place; import org.tasks.injection.InjectingJobIntentService; import org.tasks.injection.ServiceComponent; import org.tasks.notifications.Notification; @@ -58,20 +59,27 @@ public class GeofenceTransitionsIntentService extends InjectingJobIntentService com.google.android.gms.location.Geofence triggeringGeofence, boolean arrival) { String requestId = triggeringGeofence.getRequestId(); try { + Place place = locationDao.getPlace(requestId); + if (place == null) { + Timber.e("Can't find place for requestId %s", requestId); + return; + } + List geofences = arrival + ? locationDao.getArrivalGeofences(place.getUid()) + : locationDao.getDepartureGeofences(place.getUid()); notifier.triggerNotifications( - singletonList( - toNotification(locationDao.getGeofence(Long.parseLong(requestId)), arrival))); + Lists.transform(geofences, g -> toNotification(place, g, arrival))); } catch (Exception e) { Timber.e(e, "Error triggering geofence %s: %s", requestId, e.getMessage()); } } - private Notification toNotification(Location location, boolean arrival) { + private Notification toNotification(Place place, Geofence geofence, boolean arrival) { Notification notification = new Notification(); - notification.taskId = location.getTask(); + notification.taskId = geofence.getTask(); notification.type = arrival ? TYPE_GEOFENCE_ENTER : TYPE_GEOFENCE_EXIT; notification.timestamp = currentTimeMillis(); - notification.location = location.getId(); + notification.location = place.getId(); return notification; } diff --git a/app/src/main/java/org/tasks/activities/PlaceSettingsActivity.kt b/app/src/main/java/org/tasks/activities/PlaceSettingsActivity.kt index e32c14c66..442df092c 100644 --- a/app/src/main/java/org/tasks/activities/PlaceSettingsActivity.kt +++ b/app/src/main/java/org/tasks/activities/PlaceSettingsActivity.kt @@ -95,7 +95,7 @@ class PlaceSettingsActivity : BaseListSettingsActivity(), MapFragment.MapFragmen } override fun delete() { - locationDao.getGeofencesByPlace(place.uid).forEach(locationDao::delete) + locationDao.deleteGeofencesByPlace(place.uid) locationDao.delete(place) setResult(Activity.RESULT_OK, Intent(TaskListFragment.ACTION_DELETED)) finish() diff --git a/app/src/main/java/org/tasks/caldav/iCalendar.kt b/app/src/main/java/org/tasks/caldav/iCalendar.kt index f8e8beca6..fab11c4d0 100644 --- a/app/src/main/java/org/tasks/caldav/iCalendar.kt +++ b/app/src/main/java/org/tasks/caldav/iCalendar.kt @@ -10,7 +10,6 @@ import com.google.common.collect.Sets.difference import com.google.common.collect.Sets.newHashSet import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.dao.TaskDao -import com.todoroo.astrid.data.SyncFlags import com.todoroo.astrid.helper.UUIDHelper import com.todoroo.astrid.service.TaskCreator import net.fortuna.ical4j.model.Parameter @@ -29,6 +28,7 @@ import java.io.ByteArrayOutputStream import java.io.StringReader import javax.inject.Inject +@Suppress("ClassName") class iCalendar @Inject constructor( private val tagDataDao: TagDataDao, private val preferences: Preferences, @@ -85,14 +85,13 @@ class iCalendar @Inject constructor( val geofence = Geofence(place!!.uid, preferences) geofence.task = taskId geofence.id = locationDao.insert(geofence) - geofenceApi.register(Location(geofence, place)) } else if (place != existing.place) { - geofenceApi.cancel(existing) val geofence = existing.geofence geofence.place = place!!.uid locationDao.update(geofence) - geofenceApi.register(existing) + geofenceApi.update(existing.place) } + geofenceApi.update(place) } fun getTags(categories: List): List { @@ -154,8 +153,8 @@ class iCalendar @Inject constructor( val geo = remote.geoPosition if (geo == null) { locationDao.getActiveGeofences(task.getId()).forEach { - geofenceApi.cancel(it) locationDao.delete(it.geofence) + geofenceApi.update(it.place) } } else { setPlace(task.getId(), geo) diff --git a/app/src/main/java/org/tasks/data/LocationDao.java b/app/src/main/java/org/tasks/data/LocationDao.java index 2f49eaee6..b030373db 100644 --- a/app/src/main/java/org/tasks/data/LocationDao.java +++ b/app/src/main/java/org/tasks/data/LocationDao.java @@ -15,17 +15,59 @@ import org.tasks.filters.LocationFilters; public interface LocationDao { @Query( - "SELECT * FROM geofences INNER JOIN places ON geofences.place = places.uid WHERE geofence_id = :id LIMIT 1") - Location getGeofence(Long id); + "SELECT places.*" + + " FROM places" + + " INNER JOIN geofences ON geofences.place = places.uid" + + " INNER JOIN tasks ON geofences.task = tasks._id" + + " WHERE tasks.completed = 0 AND tasks.deleted = 0" + + " AND (geofences.arrival > 0 OR geofences.departure > 0)" + + " GROUP BY places.uid") + List getPlacesWithGeofences(); @Query( - "SELECT * FROM geofences INNER JOIN places ON geofences.place = places.uid WHERE task = :taskId ORDER BY name ASC LIMIT 1") + "SELECT places.*," + + " max(geofences.arrival) as arrival," + + " max(geofences.departure) as departure," + + " min(geofences.radius) as radius" + + " FROM places" + + " INNER JOIN geofences ON geofences.place = places.uid" + + " INNER JOIN tasks ON tasks._id = geofences.task" + + " WHERE place = :uid AND tasks.completed = 0 AND tasks.deleted = 0" + + " AND (geofences.arrival > 0 OR geofences.departure > 0)" + + " GROUP BY places.uid") + MergedGeofence getGeofencesByPlace(String uid); + + @Query("DELETE FROM geofences WHERE place = :place") + void deleteGeofencesByPlace(String place); + + @Query( + "SELECT geofences.* FROM geofences" + + " INNER JOIN tasks ON tasks._id = geofences.task" + + " WHERE place = :place AND arrival = 1 AND tasks.completed = 0 AND tasks.deleted = 0") + List getArrivalGeofences(String place); + + @Query( + "SELECT geofences.* FROM geofences" + + " INNER JOIN tasks ON tasks._id = geofences.task" + + " WHERE place = :place AND departure = 1 AND tasks.completed = 0 AND tasks.deleted = 0") + List getDepartureGeofences(String place); + + @Query( + "SELECT * FROM geofences" + + " INNER JOIN places ON geofences.place = places.uid" + + " WHERE task = :taskId ORDER BY name ASC LIMIT 1") Location getGeofences(long taskId); @Query( "SELECT geofences.*, places.* FROM geofences INNER JOIN places ON geofences.place = places.uid INNER JOIN tasks ON tasks._id = geofences.task WHERE tasks._id = :taskId AND tasks.deleted = 0 AND tasks.completed = 0") List getActiveGeofences(long taskId); + @Query("SELECT places.*" + + " FROM places" + + " INNER JOIN geofences ON geofences.place = places.uid" + + " WHERE geofences.task = :taskId") + Place getPlaceForTask(long taskId); + @Query( "SELECT geofences.*, places.* FROM geofences INNER JOIN places ON geofences.place = places.uid INNER JOIN tasks ON tasks._id = geofences.task WHERE tasks.deleted = 0 AND tasks.completed = 0") List getActiveGeofences(); @@ -57,9 +99,6 @@ public interface LocationDao { @Query("SELECT * FROM geofences WHERE task = :taskId") List getGeofencesForTask(long taskId); - @Query("SELECT * FROM geofences WHERE place = :uid") - List getGeofencesByPlace(String uid); - @Query("SELECT * FROM places") List getPlaces(); diff --git a/app/src/main/java/org/tasks/data/MergedGeofence.java b/app/src/main/java/org/tasks/data/MergedGeofence.java new file mode 100644 index 000000000..2c67fb14b --- /dev/null +++ b/app/src/main/java/org/tasks/data/MergedGeofence.java @@ -0,0 +1,52 @@ +package org.tasks.data; + +import androidx.room.Embedded; + +public class MergedGeofence { + @Embedded Place place; + boolean arrival; + boolean departure; + int radius; + + public Place getPlace() { + return place; + } + + public String getUid() { + return place.getUid(); + } + + public double getLatitude() { + return place.getLatitude(); + } + + public double getLongitude() { + return place.getLongitude(); + } + + public boolean isArrival() { + return arrival; + } + + public boolean isDeparture() { + return departure; + } + + public int getRadius() { + return radius; + } + + @Override + public String toString() { + return "MergedGeofence{" + + "place=" + + place.getDisplayName() + + ", arrival=" + + arrival + + ", departure=" + + departure + + ", radius=" + + radius + + '}'; + } +} diff --git a/app/src/main/java/org/tasks/jobs/AfterSaveWork.java b/app/src/main/java/org/tasks/jobs/AfterSaveWork.java index 7bf7a5530..be61066f0 100644 --- a/app/src/main/java/org/tasks/jobs/AfterSaveWork.java +++ b/app/src/main/java/org/tasks/jobs/AfterSaveWork.java @@ -98,9 +98,9 @@ public class AfterSaveWork extends InjectingWorker { if (justCompleted || justDeleted) { notificationManager.cancel(taskId); - geofenceApi.cancel(taskId); - } else if (completionDateModified || deletionDateModified) { - geofenceApi.register(taskId); + } + if (completionDateModified || deletionDateModified) { + geofenceApi.update(taskId); } if (justCompleted) { diff --git a/app/src/main/java/org/tasks/jobs/CleanupWork.java b/app/src/main/java/org/tasks/jobs/CleanupWork.java index cee18a9cf..f67991f1f 100644 --- a/app/src/main/java/org/tasks/jobs/CleanupWork.java +++ b/app/src/main/java/org/tasks/jobs/CleanupWork.java @@ -7,6 +7,8 @@ import com.todoroo.astrid.alarms.AlarmService; import com.todoroo.astrid.reminders.ReminderService; import com.todoroo.astrid.timers.TimerPlugin; import javax.inject.Inject; +import org.tasks.data.Geofence; +import org.tasks.data.LocationDao; import org.tasks.data.TaskAttachment; import org.tasks.data.TaskAttachmentDao; import org.tasks.data.UserActivity; @@ -29,6 +31,7 @@ public class CleanupWork extends InjectingWorker { @Inject AlarmService alarmService; @Inject TaskAttachmentDao taskAttachmentDao; @Inject UserActivityDao userActivityDao; + @Inject LocationDao locationDao; public CleanupWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); @@ -47,7 +50,10 @@ public class CleanupWork extends InjectingWorker { alarmService.cancelAlarms(task); reminderService.cancelReminder(task); notificationManager.cancel(task); - geofenceApi.cancel(task); + for (Geofence geofence : locationDao.getGeofencesForTask(task)) { + locationDao.delete(geofence); + geofenceApi.update(geofence.getPlace()); + } for (TaskAttachment attachment : taskAttachmentDao.getAttachments(task)) { FileHelper.delete(context, attachment.parseUri()); taskAttachmentDao.delete(attachment); diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.java b/app/src/main/java/org/tasks/notifications/NotificationManager.java index 47a5fa41c..9cfba95b2 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.java +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.java @@ -41,8 +41,8 @@ import java.util.List; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; import org.tasks.R; -import org.tasks.data.Location; import org.tasks.data.LocationDao; +import org.tasks.data.Place; import org.tasks.injection.ApplicationScope; import org.tasks.injection.ForApplication; import org.tasks.intents.TaskIntents; @@ -383,14 +383,14 @@ public class NotificationManager { PendingIntent.getActivity(context, (int) id, intent, PendingIntent.FLAG_UPDATE_CURRENT)); if (type == TYPE_GEOFENCE_ENTER || type == TYPE_GEOFENCE_EXIT) { - Location location = locationDao.getGeofence(notification.location); - if (location != null) { + Place place = locationDao.getPlace(notification.location); + if (place != null) { builder.setContentText( context.getString( type == TYPE_GEOFENCE_ENTER ? R.string.location_arrived : R.string.location_departed, - location.getDisplayName())); + place.getDisplayName())); } } else if (!Strings.isNullOrEmpty(taskDescription)) { builder diff --git a/app/src/main/java/org/tasks/ui/LocationControlSet.java b/app/src/main/java/org/tasks/ui/LocationControlSet.java index e3e5f4078..c9cd0b78a 100644 --- a/app/src/main/java/org/tasks/ui/LocationControlSet.java +++ b/app/src/main/java/org/tasks/ui/LocationControlSet.java @@ -263,8 +263,8 @@ public class LocationControlSet extends TaskEditControlFragment { } if (original != null) { - geofenceApi.cancel(original); locationDao.delete(original.geofence); + geofenceApi.update(original.place); } if (location != null) { Place place = location.place; @@ -272,7 +272,7 @@ public class LocationControlSet extends TaskEditControlFragment { geofence.setTask(task.getId()); geofence.setPlace(place.getUid()); geofence.setId(locationDao.insert(geofence)); - geofenceApi.register(location); + geofenceApi.update(place); } task.setModificationDate(DateUtilities.now()); }