Pass notifications to notifier

pull/795/head
Alex Baker 7 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_ENTER;
import static com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT; 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.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -15,6 +18,7 @@ import org.tasks.data.Location;
import org.tasks.data.LocationDao; import org.tasks.data.LocationDao;
import org.tasks.injection.InjectingJobIntentService; import org.tasks.injection.InjectingJobIntentService;
import org.tasks.injection.ServiceComponent; import org.tasks.injection.ServiceComponent;
import org.tasks.notifications.Notification;
import timber.log.Timber; import timber.log.Timber;
public class GeofenceTransitionsIntentService extends InjectingJobIntentService { public class GeofenceTransitionsIntentService extends InjectingJobIntentService {
@ -53,15 +57,23 @@ public class GeofenceTransitionsIntentService extends InjectingJobIntentService
com.google.android.gms.location.Geofence triggeringGeofence, boolean arrival) { com.google.android.gms.location.Geofence triggeringGeofence, boolean arrival) {
String requestId = triggeringGeofence.getRequestId(); String requestId = triggeringGeofence.getRequestId();
try { try {
Location location = locationDao.getGeofence(Long.parseLong(requestId)); notifier.triggerNotification(
notifier.triggerTaskNotification(location, arrival); toNotification(locationDao.getGeofence(Long.parseLong(requestId)), arrival));
} catch (Exception e) { } catch (Exception e) {
Timber.e(e, "Error triggering geofence %s: %s", requestId, e.getMessage()); 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 @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
JobIntentService.enqueueWork( 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 android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static com.google.common.collect.Iterables.skip; import static com.google.common.collect.Iterables.skip;
import static com.google.common.collect.Lists.newArrayList; 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.notifications.NotificationManager.MAX_NOTIFICATIONS;
import static org.tasks.time.DateTimeUtils.currentTimeMillis; import static org.tasks.time.DateTimeUtils.currentTimeMillis;
@ -23,10 +22,9 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import org.tasks.data.Location;
import org.tasks.injection.ForApplication; import org.tasks.injection.ForApplication;
import org.tasks.jobs.NotificationQueueEntry;
import org.tasks.notifications.AudioManager; import org.tasks.notifications.AudioManager;
import org.tasks.notifications.Notification;
import org.tasks.notifications.NotificationManager; import org.tasks.notifications.NotificationManager;
import org.tasks.notifications.TelephonyManager; import org.tasks.notifications.TelephonyManager;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
@ -107,22 +105,11 @@ public class Notifier {
notificationManager.notify(filter.listingTitle.hashCode(), builder, true, false, false); notificationManager.notify(filter.listingTitle.hashCode(), builder, true, false, false);
} }
public void triggerTaskNotification(Location location, boolean arrival) { public void triggerNotification(Notification notification) {
org.tasks.notifications.Notification notification = new org.tasks.notifications.Notification(); triggerNotifications(Collections.singletonList(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 triggerTaskNotifications(List<? extends NotificationQueueEntry> entries) { public void triggerNotifications(List<Notification> entries) {
triggerNotifications(transform(entries, NotificationQueueEntry::toNotification), true);
}
private void triggerNotifications(
List<org.tasks.notifications.Notification> entries, boolean alert) {
List<org.tasks.notifications.Notification> notifications = new ArrayList<>(); List<org.tasks.notifications.Notification> notifications = new ArrayList<>();
boolean ringFiveTimes = false; boolean ringFiveTimes = false;
boolean ringNonstop = false; boolean ringNonstop = false;
@ -152,10 +139,9 @@ public class Notifier {
notifications = newArrayList(skip(notifications, notifications.size() - MAX_NOTIFICATIONS)); notifications = newArrayList(skip(notifications, notifications.size() - MAX_NOTIFICATIONS));
} }
notificationManager.notifyTasks(notifications, alert, ringNonstop, ringFiveTimes); notificationManager.notifyTasks(notifications, true, ringNonstop, ringFiveTimes);
if (alert if (preferences.getBoolean(R.string.p_voiceRemindersEnabled, false)
&& preferences.getBoolean(R.string.p_voiceRemindersEnabled, false)
&& !ringNonstop && !ringNonstop
&& !audioManager.notificationsMuted() && !audioManager.notificationsMuted()
&& telephonyManager.callStateIdle()) { && telephonyManager.callStateIdle()) {

@ -1,5 +1,7 @@
package org.tasks.jobs; package org.tasks.jobs;
import static com.google.common.collect.Lists.transform;
import android.content.Intent; import android.content.Intent;
import android.os.IBinder; import android.os.IBinder;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -41,7 +43,8 @@ public class NotificationService extends InjectingService {
try { try {
if (!preferences.isCurrentlyQuietHours()) { if (!preferences.isCurrentlyQuietHours()) {
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs(); List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();
notifier.triggerTaskNotifications(overdueJobs); notifier.triggerNotifications(
transform(overdueJobs, NotificationQueueEntry::toNotification));
boolean success = notificationQueue.remove(overdueJobs); boolean success = notificationQueue.remove(overdueJobs);
if (BuildConfig.DEBUG && !success) { if (BuildConfig.DEBUG && !success) {
throw new RuntimeException("Failed to remove jobs from queue"); throw new RuntimeException("Failed to remove jobs from queue");

Loading…
Cancel
Save