Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3ef646d0 authored by Justin Klaassen's avatar Justin Klaassen
Browse files

Only update group summary when necessary

Bug: 30614779
Change-Id: If77f63ec7ab635b0675f529d77c273a9ecc92053
parent 1b3b2c1e
Loading
Loading
Loading
Loading
+63 −43
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import com.android.deskclock.provider.AlarmInstance;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Objects;

public final class AlarmNotifications {
    public static final String EXTRA_NOTIFICATION_ID = "extra_notification_id";
@@ -118,7 +119,7 @@ public final class AlarmNotifications {

        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
        nm.notify(instance.hashCode(), notification.build());
        updateAlarmGroupNotification(context);
        updateUpcomingAlarmGroupNotification(context);
    }

    public static void showHighPriorityNotification(Context context, AlarmInstance instance) {
@@ -156,9 +157,10 @@ public final class AlarmNotifications {

        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
        nm.notify(instance.hashCode(), notification.build());
        updateAlarmGroupNotification(context);
        updateUpcomingAlarmGroupNotification(context);
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static boolean isGroupSummary(Notification n) {
        return (n.flags & Notification.FLAG_GROUP_SUMMARY) == Notification.FLAG_GROUP_SUMMARY;
    }
@@ -181,22 +183,39 @@ public final class AlarmNotifications {
        return firstActiveNotification;
    }

    public static void updateAlarmGroupNotification(Context context) {
    @TargetApi(Build.VERSION_CODES.N)
    public static Notification getActiveGroupSummaryNotification(Context context, String group) {
        final NotificationManager nm =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        final StatusBarNotification[] notifications = nm.getActiveNotifications();
        for (StatusBarNotification statusBarNotification : notifications) {
            final Notification n = statusBarNotification.getNotification();
            if (isGroupSummary(n) && group.equals(n.getGroup())) {
                return n;
            }
        }
        return null;
    }

    public static void updateUpcomingAlarmGroupNotification(Context context) {
        if (!Utils.isNOrLater()) {
            return;
        }

        final NotificationManagerCompat nm = NotificationManagerCompat.from(context);

        final Notification firstActiveNotification =
                getFirstActiveNotification(context, UPCOMING_GROUP_KEY);
        if (firstActiveNotification == null) {
        final Notification firstUpcoming = getFirstActiveNotification(context, UPCOMING_GROUP_KEY);
        if (firstUpcoming == null) {
            nm.cancel(ALARM_GROUP_NOTIFICATION_ID);
            return;
        }

        NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(context)
        Notification summary = getActiveGroupSummaryNotification(context, UPCOMING_GROUP_KEY);
        if (summary == null
                || !Objects.equals(summary.contentIntent, firstUpcoming.contentIntent)) {
            summary = new NotificationCompat.Builder(context)
                    .setShowWhen(false)
                    .setContentIntent(firstUpcoming.contentIntent)
                    .setColor(ContextCompat.getColor(context, R.color.default_background))
                    .setSmallIcon(R.drawable.stat_notify_alarm)
                    .setGroup(UPCOMING_GROUP_KEY)
@@ -204,29 +223,31 @@ public final class AlarmNotifications {
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLocalOnly(true);

        summaryNotification.setContentIntent(firstActiveNotification.contentIntent);

        nm.notify(ALARM_GROUP_NOTIFICATION_ID, summaryNotification.build());
                    .setLocalOnly(true)
                    .build();
            nm.notify(ALARM_GROUP_NOTIFICATION_ID, summary);
        }
    }

    public static void updateAlarmGroupMissedNotification(Context context) {
    public static void updateMissedAlarmGroupNotification(Context context) {
        if (!Utils.isNOrLater()) {
            return;
        }

        final NotificationManagerCompat nm = NotificationManagerCompat.from(context);

        final Notification firstActiveNotification =
                getFirstActiveNotification(context, MISSED_GROUP_KEY);
        if (firstActiveNotification == null) {
        final Notification firstMissed = getFirstActiveNotification(context, MISSED_GROUP_KEY);
        if (firstMissed == null) {
            nm.cancel(ALARM_GROUP_MISSED_NOTIFICATION_ID);
            return;
        }

        NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(context)
        Notification summary = getActiveGroupSummaryNotification(context, MISSED_GROUP_KEY);
        if (summary == null
                || !Objects.equals(summary.contentIntent, firstMissed.contentIntent)) {
            summary = new NotificationCompat.Builder(context)
                    .setShowWhen(false)
                    .setContentIntent(firstMissed.contentIntent)
                    .setColor(ContextCompat.getColor(context, R.color.default_background))
                    .setSmallIcon(R.drawable.stat_notify_alarm)
                    .setGroup(MISSED_GROUP_KEY)
@@ -234,11 +255,10 @@ public final class AlarmNotifications {
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLocalOnly(true);

        summaryNotification.setContentIntent(firstActiveNotification.contentIntent);

        nm.notify(ALARM_GROUP_MISSED_NOTIFICATION_ID, summaryNotification.build());
                    .setLocalOnly(true)
                    .build();
            nm.notify(ALARM_GROUP_MISSED_NOTIFICATION_ID, summary);
        }
    }

    public static void showSnoozeNotification(Context context, AlarmInstance instance) {
@@ -277,7 +297,7 @@ public final class AlarmNotifications {

        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
        nm.notify(instance.hashCode(), notification.build());
        updateAlarmGroupNotification(context);
        updateUpcomingAlarmGroupNotification(context);
    }

    public static void showMissedNotification(Context context, AlarmInstance instance) {
@@ -320,7 +340,7 @@ public final class AlarmNotifications {

        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
        nm.notify(hashCode, notification.build());
        updateAlarmGroupMissedNotification(context);
        updateMissedAlarmGroupNotification(context);
    }

    public static void showAlarmNotification(Service service, AlarmInstance instance) {
@@ -385,8 +405,8 @@ public final class AlarmNotifications {
        LogUtils.v("Clearing notifications for alarm instance: " + instance.mId);
        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
        nm.cancel(instance.hashCode());
        updateAlarmGroupNotification(context);
        updateAlarmGroupMissedNotification(context);
        updateUpcomingAlarmGroupNotification(context);
        updateMissedAlarmGroupNotification(context);
    }

    /**