|
|
@ -11,6 +11,7 @@ import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.support.v4.app.NotificationManagerCompat;
|
|
|
|
import android.support.v4.app.NotificationManagerCompat;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
import com.google.common.base.Strings;
|
|
|
|
import com.google.common.base.Strings;
|
|
|
|
import com.todoroo.andlib.sql.Query;
|
|
|
|
import com.todoroo.andlib.sql.Query;
|
|
|
|
import com.todoroo.andlib.sql.QueryTemplate;
|
|
|
|
import com.todoroo.andlib.sql.QueryTemplate;
|
|
|
@ -45,12 +46,12 @@ import timber.log.Timber;
|
|
|
|
|
|
|
|
|
|
|
|
import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
|
|
|
|
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.concat;
|
|
|
|
import static com.google.common.collect.Iterables.tryFind;
|
|
|
|
import static com.google.common.collect.Iterables.tryFind;
|
|
|
|
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 com.google.common.collect.Lists.transform;
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybean;
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastNougat;
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo;
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo;
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.preLollipop;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ApplicationScope
|
|
|
|
@ApplicationScope
|
|
|
|
public class NotificationManager {
|
|
|
|
public class NotificationManager {
|
|
|
@ -143,12 +144,14 @@ public class NotificationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void notifyTasks(List<org.tasks.notifications.Notification> newNotifications, boolean alert, boolean nonstop, boolean fiveTimes) {
|
|
|
|
public void notifyTasks(List<org.tasks.notifications.Notification> newNotifications, boolean alert, boolean nonstop, boolean fiveTimes) {
|
|
|
|
|
|
|
|
List<org.tasks.notifications.Notification> existingNotifications = notificationDao.getAllOrdered();
|
|
|
|
notificationDao.insertAll(newNotifications);
|
|
|
|
notificationDao.insertAll(newNotifications);
|
|
|
|
List<org.tasks.notifications.Notification> notifications = notificationDao.getAllOrdered();
|
|
|
|
int totalCount = existingNotifications.size() + newNotifications.size();
|
|
|
|
int totalCount = notifications.size();
|
|
|
|
|
|
|
|
if (totalCount == 0) {
|
|
|
|
if (totalCount == 0) {
|
|
|
|
notificationManagerCompat.cancel(SUMMARY_NOTIFICATION_ID);
|
|
|
|
notificationManagerCompat.cancel(SUMMARY_NOTIFICATION_ID);
|
|
|
|
} else if (totalCount == 1) {
|
|
|
|
} else if (totalCount == 1) {
|
|
|
|
|
|
|
|
List<org.tasks.notifications.Notification> notifications =
|
|
|
|
|
|
|
|
newArrayList(concat(existingNotifications, newNotifications));
|
|
|
|
org.tasks.notifications.Notification notification = notifications.get(0);
|
|
|
|
org.tasks.notifications.Notification notification = notifications.get(0);
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification);
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification);
|
|
|
|
if (builder != null) {
|
|
|
|
if (builder != null) {
|
|
|
@ -156,29 +159,35 @@ public class NotificationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notificationManagerCompat.cancel(SUMMARY_NOTIFICATION_ID);
|
|
|
|
notificationManagerCompat.cancel(SUMMARY_NOTIFICATION_ID);
|
|
|
|
} else if (preferences.bundleNotifications()) {
|
|
|
|
} else if (preferences.bundleNotifications()) {
|
|
|
|
updateSummary(false, false, false);
|
|
|
|
updateSummary(false, false, false, Collections.emptyList());
|
|
|
|
|
|
|
|
|
|
|
|
for (org.tasks.notifications.Notification notification : notifications) {
|
|
|
|
if (existingNotifications.size() == 1) {
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification);
|
|
|
|
org.tasks.notifications.Notification notification = existingNotifications.get(0);
|
|
|
|
if (builder != null) {
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification)
|
|
|
|
builder.setGroup(GROUP_KEY)
|
|
|
|
.setGroup(GROUP_KEY)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MIN);
|
|
|
|
notify(notification.taskId, builder, false, false, false);
|
|
|
|
notify(notification.taskId, builder, false, false, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (newNotifications.size() == 1) {
|
|
|
|
if (atLeastNougat() && newNotifications.size() == 1) {
|
|
|
|
org.tasks.notifications.Notification notification = newNotifications.get(0);
|
|
|
|
org.tasks.notifications.Notification notification = newNotifications.get(0);
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification);
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification);
|
|
|
|
if (builder != null) {
|
|
|
|
if (builder != null) {
|
|
|
|
builder.setGroup(GROUP_KEY)
|
|
|
|
builder.setGroup(GROUP_KEY)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH);
|
|
|
|
|
|
|
|
notify(notification.taskId, builder, alert, nonstop, fiveTimes);
|
|
|
|
notify(notification.taskId, builder, alert, nonstop, fiveTimes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
updateSummary(alert, nonstop, fiveTimes);
|
|
|
|
for (org.tasks.notifications.Notification notification : newNotifications) {
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder = getTaskNotification(notification);
|
|
|
|
|
|
|
|
if (builder != null) {
|
|
|
|
|
|
|
|
builder.setGroup(GROUP_KEY)
|
|
|
|
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
|
|
|
|
|
|
|
|
notify(notification.taskId, builder, false, false, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateSummary(alert, nonstop, fiveTimes, newNotifications);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
for (org.tasks.notifications.Notification notification : newNotifications) {
|
|
|
|
for (org.tasks.notifications.Notification notification : newNotifications) {
|
|
|
@ -201,7 +210,6 @@ public class NotificationManager {
|
|
|
|
builder.setOngoing(preferences.usePersistentReminders());
|
|
|
|
builder.setOngoing(preferences.usePersistentReminders());
|
|
|
|
if (alert) {
|
|
|
|
if (alert) {
|
|
|
|
builder.setSound(preferences.getRingtone())
|
|
|
|
builder.setSound(preferences.getRingtone())
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
|
|
|
|
|
|
.setDefaults(preferences.getNotificationDefaults());
|
|
|
|
.setDefaults(preferences.getNotificationDefaults());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
builder.setDefaults(0)
|
|
|
|
builder.setDefaults(0)
|
|
|
@ -220,11 +228,7 @@ public class NotificationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void updateSummary(boolean notify, boolean nonStop, boolean fiveTimes) {
|
|
|
|
private void updateSummary(boolean notify, boolean nonStop, boolean fiveTimes, List<org.tasks.notifications.Notification> newNotifications) {
|
|
|
|
if (!preferences.bundleNotifications()) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<org.tasks.notifications.Notification> notifications = notificationDao.getAllOrdered();
|
|
|
|
List<org.tasks.notifications.Notification> notifications = notificationDao.getAllOrdered();
|
|
|
|
int taskCount = notifications.size();
|
|
|
|
int taskCount = notifications.size();
|
|
|
|
ArrayList<Long> taskIds = newArrayList(transform(notifications, n -> n.taskId));
|
|
|
|
ArrayList<Long> taskIds = newArrayList(transform(notifications, n -> n.taskId));
|
|
|
@ -237,33 +241,41 @@ public class NotificationManager {
|
|
|
|
String summaryTitle = context.getString(R.string.task_count, taskCount);
|
|
|
|
String summaryTitle = context.getString(R.string.task_count, taskCount);
|
|
|
|
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
|
|
|
|
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
|
|
|
|
.setBigContentTitle(summaryTitle);
|
|
|
|
.setBigContentTitle(summaryTitle);
|
|
|
|
|
|
|
|
List<String> titles = newArrayList();
|
|
|
|
|
|
|
|
List<String> ticker = newArrayList();
|
|
|
|
for (org.tasks.notifications.Notification notification : notifications) {
|
|
|
|
for (org.tasks.notifications.Notification notification : notifications) {
|
|
|
|
Task task = tryFind(tasks, t -> t.getId() == notification.taskId).orNull();
|
|
|
|
Task task = tryFind(tasks, t -> t.getId() == notification.taskId).orNull();
|
|
|
|
if (task == null) {
|
|
|
|
if (task == null) {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
style.addLine(task.getTitle());
|
|
|
|
String title = task.getTitle();
|
|
|
|
|
|
|
|
style.addLine(title);
|
|
|
|
|
|
|
|
titles.add(title);
|
|
|
|
maxPriority = Math.min(maxPriority, task.getImportance());
|
|
|
|
maxPriority = Math.min(maxPriority, task.getImportance());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (org.tasks.notifications.Notification notification : newNotifications) {
|
|
|
|
|
|
|
|
Task task = tryFind(tasks, t -> t.getId() == notification.taskId).orNull();
|
|
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker.add(task.getTitle());
|
|
|
|
|
|
|
|
}
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_DEFAULT)
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_DEFAULT)
|
|
|
|
.setContentTitle(summaryTitle)
|
|
|
|
.setContentTitle(summaryTitle)
|
|
|
|
.setContentText(context.getString(R.string.app_name))
|
|
|
|
.setContentText(Joiner.on(", ").join(titles))
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setWhen(when)
|
|
|
|
.setWhen(when)
|
|
|
|
.setSmallIcon(R.drawable.ic_done_all_white_24dp)
|
|
|
|
.setSmallIcon(R.drawable.ic_done_all_white_24dp)
|
|
|
|
.setStyle(style)
|
|
|
|
.setStyle(style)
|
|
|
|
.setColor(checkBoxes.getPriorityColor(maxPriority))
|
|
|
|
.setColor(checkBoxes.getPriorityColor(maxPriority))
|
|
|
|
.setNumber(taskCount)
|
|
|
|
|
|
|
|
.setOnlyAlertOnce(false)
|
|
|
|
.setOnlyAlertOnce(false)
|
|
|
|
.setContentIntent(PendingIntent.getActivity(context, 0, TaskIntents.getTaskListIntent(context, filter), PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
|
.setContentIntent(PendingIntent.getActivity(context, 0, TaskIntents.getTaskListIntent(context, filter), PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
|
.setGroupSummary(true)
|
|
|
|
.setGroupSummary(true)
|
|
|
|
.setGroup(GROUP_KEY)
|
|
|
|
.setGroup(GROUP_KEY)
|
|
|
|
|
|
|
|
.setTicker(Joiner.on(", ").join(ticker))
|
|
|
|
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
|
|
.setGroupAlertBehavior(notify ? NotificationCompat.GROUP_ALERT_SUMMARY : NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
.setGroupAlertBehavior(notify ? NotificationCompat.GROUP_ALERT_SUMMARY : NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
|
|
|
|
|
|
|
|
if (preLollipop()) {
|
|
|
|
|
|
|
|
builder.setTicker(summaryTitle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Intent snoozeIntent = new Intent(context, SnoozeActivity.class);
|
|
|
|
Intent snoozeIntent = new Intent(context, SnoozeActivity.class);
|
|
|
|
snoozeIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
snoozeIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
snoozeIntent.putExtra(SnoozeActivity.EXTRA_TASK_IDS, taskIds);
|
|
|
|
snoozeIntent.putExtra(SnoozeActivity.EXTRA_TASK_IDS, taskIds);
|
|
|
@ -324,11 +336,8 @@ public class NotificationManager {
|
|
|
|
.setSmallIcon(R.drawable.ic_check_white_24dp)
|
|
|
|
.setSmallIcon(R.drawable.ic_check_white_24dp)
|
|
|
|
.setWhen(when)
|
|
|
|
.setWhen(when)
|
|
|
|
.setOnlyAlertOnce(false)
|
|
|
|
.setOnlyAlertOnce(false)
|
|
|
|
.setShowWhen(true);
|
|
|
|
.setShowWhen(true)
|
|
|
|
|
|
|
|
.setTicker(taskTitle);
|
|
|
|
if (preLollipop()) {
|
|
|
|
|
|
|
|
builder.setTicker(taskTitle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Intent intent = new Intent(context, NotificationActivity.class);
|
|
|
|
final Intent intent = new Intent(context, NotificationActivity.class);
|
|
|
|
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK);
|
|
|
|
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK);
|
|
|
|