Pass notifications to notifier

pull/795/head
Alex Baker 6 years ago
parent ae635290fc
commit 90867839d4

@ -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(

@ -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<? extends NotificationQueueEntry> entries) {
triggerNotifications(transform(entries, NotificationQueueEntry::toNotification), true);
}
private void triggerNotifications(
List<org.tasks.notifications.Notification> entries, boolean alert) {
public void triggerNotifications(List<Notification> entries) {
List<org.tasks.notifications.Notification> 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()) {

@ -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<? extends NotificationQueueEntry> 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");

Loading…
Cancel
Save