Summary notification for all platforms

pull/574/head
Alex Baker 7 years ago
parent 50eedbfe03
commit e6f2f4ca50

@ -36,7 +36,6 @@ import org.tasks.ui.TimePreference;
import javax.inject.Inject;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastNougat;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo;
import static com.todoroo.andlib.utility.AndroidUtilities.preOreo;
import static org.tasks.PermissionUtil.verifyPermissions;
@ -88,7 +87,6 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
requires(atLeastOreo(), R.string.notification_channel_settings);
requires(atLeastMarshmallow(), R.string.battery_optimization_settings);
requires(preOreo(), R.string.p_rmd_ringtone, R.string.p_rmd_vibrate, R.string.p_led_notification);
requires(atLeastNougat(), R.string.p_bundle_notifications);
}
@TargetApi(Build.VERSION_CODES.O)

@ -9,8 +9,10 @@ import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.sql.QueryTemplate;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import org.tasks.R;
@ -20,6 +22,7 @@ import org.tasks.intents.TaskIntents;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
@ -44,17 +47,19 @@ public class NotificationManager {
private final android.app.NotificationManager notificationManager;
private final NotificationDao notificationDao;
private final TaskDao taskDao;
private final Context context;
private final Preferences preferences;
@Inject
public NotificationManager(@ForApplication Context context, Preferences preferences,
NotificationDao notificationDao) {
NotificationDao notificationDao, TaskDao taskDao) {
this.context = context;
this.preferences = preferences;
notificationManager = (android.app.NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
this.notificationDao = notificationDao;
this.taskDao = taskDao;
if (atLeastOreo()) {
notificationManager.createNotificationChannel(createNotificationChannel(NOTIFICATION_CHANNEL_DEFAULT, R.string.notifications));
notificationManager.createNotificationChannel(createNotificationChannel(NOTIFICATION_CHANNEL_CALLS, R.string.missed_calls));
@ -132,19 +137,32 @@ public class NotificationManager {
private void updateSummary(boolean notify, boolean nonStop, boolean fiveTimes) {
if (preferences.bundleNotifications()) {
if (notificationDao.count() == 0) {
int taskCount = notificationDao.count();
if (taskCount == 0) {
notificationManager.cancel(SUMMARY_NOTIFICATION_ID);
} else {
Iterable<Long> notificationIds = transform(notificationDao.getAll(), n -> n.taskId);
QueryTemplate query = new QueryTemplate().where(Task.ID.in(notificationIds));
Filter notifications = new Filter(context.getString(R.string.notifications),
new QueryTemplate().where(Task.ID.in(notificationIds)));
query);
List<Task> tasks = taskDao.toList(Query.select(Task.PROPERTIES).withQueryTemplate(query.toString()));
long when = notificationDao.latestTimestamp();
String summaryTitle = context.getString(R.string.task_count, taskCount);
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
.setBigContentTitle(summaryTitle);
for (Task task : tasks) {
style.addLine(task.getTitle());
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationManager.NOTIFICATION_CHANNEL_DEFAULT)
.setContentTitle(summaryTitle)
.setContentText(context.getString(R.string.app_name))
.setGroupSummary(true)
.setGroup(GROUP_KEY)
.setShowWhen(true)
.setWhen(when)
.setSmallIcon(R.drawable.ic_done_all_white_24dp)
.setStyle(style)
.setNumber(taskCount)
.setContentIntent(PendingIntent.getActivity(context, 0, TaskIntents.getTaskListIntent(context, notifications), PendingIntent.FLAG_UPDATE_CURRENT));
if (notify) {
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)

@ -414,6 +414,6 @@ public class Preferences {
}
public boolean bundleNotifications() {
return atLeastNougat() && getBoolean(R.string.p_bundle_notifications, true);
return getBoolean(R.string.p_bundle_notifications, true);
}
}

Loading…
Cancel
Save