diff --git a/app/src/googleplay/java/org/tasks/gtasks/GoogleTaskSyncAdapter.java b/app/src/googleplay/java/org/tasks/gtasks/GoogleTaskSyncAdapter.java index 9065fd934..d44b4b4dc 100644 --- a/app/src/googleplay/java/org/tasks/gtasks/GoogleTaskSyncAdapter.java +++ b/app/src/googleplay/java/org/tasks/gtasks/GoogleTaskSyncAdapter.java @@ -160,7 +160,7 @@ public class GoogleTaskSyncAdapter extends InjectingAbstractThreadedSyncAdapter .setAutoCancel(true) .setSmallIcon(android.R.drawable.ic_dialog_alert) .setTicker(context.getString(R.string.common_google_play_services_notification_ticker)); - notificationManager.notify(Constants.NOTIFICATION_SYNC_ERROR, builder.build(), true, false, false); + notificationManager.notify(Constants.NOTIFICATION_SYNC_ERROR, builder, true, false, false); } private void synchronize() throws IOException { diff --git a/app/src/main/java/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java b/app/src/main/java/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java index ba40423ff..9b63f6f93 100644 --- a/app/src/main/java/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java +++ b/app/src/main/java/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java @@ -230,7 +230,7 @@ public class PhoneStateChangedReceiver extends InjectingBroadcastReceiver { .addAction(R.drawable.ic_phone_white_24dp, context.getString(R.string.MCA_return_call), PendingIntent.getActivity(context, callNow.hashCode(), callNow, PendingIntent.FLAG_UPDATE_CURRENT)) .addAction(R.drawable.ic_add_white_24dp, context.getString(R.string.MCA_add_task), PendingIntent.getActivity(context, callLater.hashCode(), callLater, PendingIntent.FLAG_UPDATE_CURRENT)); - notificationManager.notify(number.hashCode(), builder.build(), true, false, false); + notificationManager.notify(number.hashCode(), builder, true, false, false); } private Bitmap getContactImage(long contactId) { diff --git a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java index 7de9af982..109b7c65a 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java @@ -5,7 +5,6 @@ */ package com.todoroo.astrid.timers; -import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -106,16 +105,15 @@ public class TimerPlugin { String appName = r.getString(R.string.app_name); String text = r.getString(R.string.TPl_notification, r.getQuantityString(R.plurals.Ntasks, count, count)); - Notification notification = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_TIMERS) + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_TIMERS) .setContentIntent(pendingIntent) .setContentTitle(appName) .setContentText(text) .setWhen(currentTimeMillis()) .setSmallIcon(R.drawable.ic_timer_white_24dp) .setAutoCancel(false) - .setOngoing(true) - .build(); - notificationManager.notify(Constants.NOTIFICATION_TIMER, notification, false, false, false); + .setOngoing(true); + notificationManager.notify(Constants.NOTIFICATION_TIMER, builder, false, false, false); } } } diff --git a/app/src/main/java/org/tasks/Notifier.java b/app/src/main/java/org/tasks/Notifier.java index 8635c4467..2c39da701 100644 --- a/app/src/main/java/org/tasks/Notifier.java +++ b/app/src/main/java/org/tasks/Notifier.java @@ -88,7 +88,7 @@ public class Notifier { intent.putExtra(TaskListActivity.OPEN_FILTER, filter); PendingIntent pendingIntent = PendingIntent.getActivity(context, (title + query).hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT); - NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_TASKER) + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_TASKER) .setSmallIcon(R.drawable.ic_check_white_24dp) .setCategory(NotificationCompat.CATEGORY_REMINDER) .setTicker(title) @@ -102,7 +102,7 @@ public class Notifier { notificationManager.notify( (title + query).hashCode(), - notification.build(), + builder, true, false, false); @@ -225,7 +225,7 @@ public class Notifier { } public void triggerNotifications(List entries, boolean alert) { - Map notifications = new LinkedHashMap<>(); + Map notifications = new LinkedHashMap<>(); boolean ringFiveTimes = false; boolean ringNonstop = false; for (int i = 0 ; i < entries.size() ; i++) { @@ -243,7 +243,7 @@ public class Notifier { notification.setGroupAlertBehavior(alert && (preferences.bundleNotifications() ? entries.size() == 1 : i == entries.size() - 1) ? NotificationCompat.GROUP_ALERT_CHILDREN : NotificationCompat.GROUP_ALERT_SUMMARY); - notifications.put(entry, notification.build()); + notifications.put(entry, notification); } } @@ -260,9 +260,9 @@ public class Notifier { !ringNonstop && !audioManager.notificationsMuted() && telephonyManager.callStateIdle()) { - for (Notification notification : notifications.values()) { + for (NotificationCompat.Builder notification : notifications.values()) { AndroidUtilities.sleepDeep(2000); - voiceOutputAssistant.speak(notification.tickerText.toString()); + voiceOutputAssistant.speak(notification.build().tickerText.toString()); } } } diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.java b/app/src/main/java/org/tasks/notifications/NotificationManager.java index f3e6e0fee..c65880cca 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.java +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.java @@ -9,7 +9,6 @@ import android.content.Intent; import android.os.Build; import android.support.v4.app.NotificationCompat; -import com.google.common.base.Optional; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.QueryTemplate; import com.todoroo.astrid.api.Filter; @@ -21,6 +20,7 @@ import org.tasks.injection.ApplicationScope; import org.tasks.injection.ForApplication; import org.tasks.intents.TaskIntents; import org.tasks.preferences.Preferences; +import org.tasks.ui.CheckBoxes; import java.util.ArrayList; import java.util.List; @@ -53,16 +53,18 @@ public class NotificationManager { private final TaskDao taskDao; private final Context context; private final Preferences preferences; + private final CheckBoxes checkBoxes; @Inject public NotificationManager(@ForApplication Context context, Preferences preferences, - NotificationDao notificationDao, TaskDao taskDao) { + NotificationDao notificationDao, TaskDao taskDao, CheckBoxes checkBoxes) { this.context = context; this.preferences = preferences; - notificationManager = (android.app.NotificationManager) - context.getSystemService(Context.NOTIFICATION_SERVICE); this.notificationDao = notificationDao; this.taskDao = taskDao; + this.checkBoxes = checkBoxes; + notificationManager = (android.app.NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); if (atLeastOreo()) { notificationManager.createNotificationChannel(createNotificationChannel(NOTIFICATION_CHANNEL_DEFAULT, R.string.notifications)); notificationManager.createNotificationChannel(createNotificationChannel(NOTIFICATION_CHANNEL_CALLS, R.string.missed_calls)); @@ -94,21 +96,28 @@ public class NotificationManager { .subscribe(); } - public void notifyTasks(Map notifications, boolean alert, boolean nonstop, boolean fiveTimes) { + public void notifyTasks(Map notifications, boolean alert, boolean nonstop, boolean fiveTimes) { notificationDao.insertAll(newArrayList(notifications.keySet())); updateSummary(alert && notifications.size() > 1, nonstop, fiveTimes); - ArrayList> entries = newArrayList(notifications.entrySet()); + ArrayList> entries = newArrayList(notifications.entrySet()); int last = entries.size() - 1; - for (int i = 0; i < last; i++) { - Map.Entry entry = entries.get(i); - notify(entry.getKey().taskId, entry.getValue(), false, false, false); + for (int i = 0; i <= last; i++) { + Map.Entry entry = entries.get(i); + long taskId = entry.getKey().taskId; + Task task = taskDao.fetch(taskId); + NotificationCompat.Builder builder = entry.getValue(); + builder.setColor(getPriorityColor(task.getImportance())); + if (i < last) { + notify(taskId, builder, false, false, false); + } else { + notify(taskId, builder, alert, nonstop, fiveTimes); + } } - Map.Entry entry = entries.get(last); - notify(entry.getKey().taskId, entry.getValue(), alert, nonstop, fiveTimes); } - public void notify(long notificationId, Notification notification, boolean alert, boolean nonstop, boolean fiveTimes) { + public void notify(long notificationId, NotificationCompat.Builder builder, boolean alert, boolean nonstop, boolean fiveTimes) { + Notification notification = builder.build(); if (preferences.getBoolean(R.string.p_rmd_enabled, true)) { int ringTimes = 1; if (preferences.getBoolean(R.string.p_rmd_persistent, true)) { @@ -152,14 +161,17 @@ public class NotificationManager { List tasks = taskDao.toList(Query.select(Task.PROPERTIES) .withQueryTemplate(query.toString())); long when = notificationDao.latestTimestamp(); + int maxPriority = 3; String summaryTitle = context.getString(R.string.task_count, taskCount); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle() .setBigContentTitle(summaryTitle); for (org.tasks.notifications.Notification notification : notifications) { - Optional task = tryFind(tasks, t -> t.getId() == notification.taskId); - if (task.isPresent()) { - style.addLine(task.get().getTitle()); + Task task = tryFind(tasks, t -> t.getId() == notification.taskId).orNull(); + if (task == null) { + continue; } + style.addLine(task.getTitle()); + maxPriority = Math.min(maxPriority, task.getImportance()); } NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_DEFAULT) .setContentTitle(summaryTitle) @@ -170,6 +182,7 @@ public class NotificationManager { .setWhen(when) .setSmallIcon(R.drawable.ic_done_all_white_24dp) .setStyle(style) + .setColor(getPriorityColor(maxPriority)) .setNumber(taskCount) .setContentIntent(PendingIntent.getActivity(context, 0, TaskIntents.getTaskListIntent(context, filter), PendingIntent.FLAG_UPDATE_CURRENT)); if (notify) { @@ -181,10 +194,15 @@ public class NotificationManager { .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); } - notify(NotificationManager.SUMMARY_NOTIFICATION_ID, builder.build(), notify, nonStop, fiveTimes); + notify(NotificationManager.SUMMARY_NOTIFICATION_ID, builder, notify, nonStop, fiveTimes); } } else { notificationManager.cancel(NotificationManager.SUMMARY_NOTIFICATION_ID); } } + + private int getPriorityColor(int priority) { + priority = Math.max(0, Math.min(3, priority)); + return checkBoxes.getPriorityColors().get(priority); + } }