From 90867839d4f3bd8324fe1ffe6b3c241dbfaa5575 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 24 Jan 2019 15:19:40 -0600 Subject: [PATCH] Pass notifications to notifier --- .../GeofenceTransitionsIntentService.java | 18 ++++++++++--- app/src/main/java/org/tasks/Notifier.java | 26 +++++-------------- .../org/tasks/jobs/NotificationService.java | 5 +++- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java b/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java index eb175c035..1538fef1d 100644 --- a/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java +++ b/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.java @@ -2,6 +2,9 @@ package org.tasks.location; 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 org.tasks.time.DateTimeUtils.currentTimeMillis; import android.content.BroadcastReceiver; import android.content.Context; @@ -15,6 +18,7 @@ import org.tasks.data.Location; import org.tasks.data.LocationDao; import org.tasks.injection.InjectingJobIntentService; import org.tasks.injection.ServiceComponent; +import org.tasks.notifications.Notification; import timber.log.Timber; public class GeofenceTransitionsIntentService extends InjectingJobIntentService { @@ -53,15 +57,23 @@ public class GeofenceTransitionsIntentService extends InjectingJobIntentService com.google.android.gms.location.Geofence triggeringGeofence, boolean arrival) { String requestId = triggeringGeofence.getRequestId(); try { - Location location = locationDao.getGeofence(Long.parseLong(requestId)); - notifier.triggerTaskNotification(location, arrival); + notifier.triggerNotification( + toNotification(locationDao.getGeofence(Long.parseLong(requestId)), arrival)); } catch (Exception e) { Timber.e(e, "Error triggering geofence %s: %s", requestId, e.getMessage()); } } - public static class Broadcast extends BroadcastReceiver { + private Notification toNotification(Location location, boolean arrival) { + Notification notification = new Notification(); + notification.taskId = location.getTask(); + notification.type = arrival ? TYPE_GEOFENCE_ENTER : TYPE_GEOFENCE_EXIT; + notification.timestamp = currentTimeMillis(); + notification.location = location.getId(); + return notification; + } + public static class Broadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { JobIntentService.enqueueWork( diff --git a/app/src/main/java/org/tasks/Notifier.java b/app/src/main/java/org/tasks/Notifier.java index a7dc6eb80..ec21790ec 100644 --- a/app/src/main/java/org/tasks/Notifier.java +++ b/app/src/main/java/org/tasks/Notifier.java @@ -4,7 +4,6 @@ import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static com.google.common.collect.Iterables.skip; import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Lists.transform; import static org.tasks.notifications.NotificationManager.MAX_NOTIFICATIONS; import static org.tasks.time.DateTimeUtils.currentTimeMillis; @@ -23,10 +22,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.inject.Inject; -import org.tasks.data.Location; import org.tasks.injection.ForApplication; -import org.tasks.jobs.NotificationQueueEntry; import org.tasks.notifications.AudioManager; +import org.tasks.notifications.Notification; import org.tasks.notifications.NotificationManager; import org.tasks.notifications.TelephonyManager; import org.tasks.preferences.Preferences; @@ -107,22 +105,11 @@ public class Notifier { notificationManager.notify(filter.listingTitle.hashCode(), builder, true, false, false); } - public void triggerTaskNotification(Location location, boolean arrival) { - org.tasks.notifications.Notification notification = new org.tasks.notifications.Notification(); - notification.taskId = location.getTask(); - notification.type = - arrival ? ReminderService.TYPE_GEOFENCE_ENTER : ReminderService.TYPE_GEOFENCE_EXIT; - notification.timestamp = currentTimeMillis(); - notification.location = location.getId(); - triggerNotifications(Collections.singletonList(notification), true); + public void triggerNotification(Notification notification) { + triggerNotifications(Collections.singletonList(notification)); } - public void triggerTaskNotifications(List entries) { - triggerNotifications(transform(entries, NotificationQueueEntry::toNotification), true); - } - - private void triggerNotifications( - List entries, boolean alert) { + public void triggerNotifications(List entries) { List notifications = new ArrayList<>(); boolean ringFiveTimes = false; boolean ringNonstop = false; @@ -152,10 +139,9 @@ public class Notifier { notifications = newArrayList(skip(notifications, notifications.size() - MAX_NOTIFICATIONS)); } - notificationManager.notifyTasks(notifications, alert, ringNonstop, ringFiveTimes); + notificationManager.notifyTasks(notifications, true, ringNonstop, ringFiveTimes); - if (alert - && preferences.getBoolean(R.string.p_voiceRemindersEnabled, false) + if (preferences.getBoolean(R.string.p_voiceRemindersEnabled, false) && !ringNonstop && !audioManager.notificationsMuted() && telephonyManager.callStateIdle()) { diff --git a/app/src/main/java/org/tasks/jobs/NotificationService.java b/app/src/main/java/org/tasks/jobs/NotificationService.java index 13399500a..3b2602b03 100644 --- a/app/src/main/java/org/tasks/jobs/NotificationService.java +++ b/app/src/main/java/org/tasks/jobs/NotificationService.java @@ -1,5 +1,7 @@ package org.tasks.jobs; +import static com.google.common.collect.Lists.transform; + import android.content.Intent; import android.os.IBinder; import androidx.annotation.Nullable; @@ -41,7 +43,8 @@ public class NotificationService extends InjectingService { try { if (!preferences.isCurrentlyQuietHours()) { List overdueJobs = notificationQueue.getOverdueJobs(); - notifier.triggerTaskNotifications(overdueJobs); + notifier.triggerNotifications( + transform(overdueJobs, NotificationQueueEntry::toNotification)); boolean success = notificationQueue.remove(overdueJobs); if (BuildConfig.DEBUG && !success) { throw new RuntimeException("Failed to remove jobs from queue");