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

Commit 60edea82 authored by Sara Ting's avatar Sara Ting
Browse files

Fix snoozing from notification, and have notifications change state as the event starts/ends.

For example, as an event ends, its notification will move into the expired digest one.

Bug:6282451
Change-Id: I81842d21f3f7b79e8aa4959164be14860cab6fe2
parent 57dd943e
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -100,6 +100,9 @@ public class AlertService extends Service {
    private static final String DISMISS_OLD_SELECTION = CalendarAlerts.END + "<? AND "
            + CalendarAlerts.STATE + "=?";

    // The grace period before changing a notification's priority bucket.
    private static final int DEPRIORITIZE_GRACE_PERIOD_MS = 15 * 60 * 1000;

    void processMessage(Message msg) {
        Bundle bundle = (Bundle) msg.obj;

@@ -310,9 +313,11 @@ public class AlertService extends Service {
        boolean defaultVibrate = shouldUseDefaultVibrate(context, prefs);
        String ringtone = quietUpdate ? null : prefs.getString(
                GeneralPreferences.KEY_ALERTS_RINGTONE, null);
        long nextRefreshTime = Long.MAX_VALUE;

        // Post the individual future events (higher priority).
        for (NotificationInfo info : futureEvents) {
            nextRefreshTime = Math.min(nextRefreshTime, info.startMillis);
            String summaryText = AlertUtils.formatTimeLocation(context, info.startMillis,
                    info.allDay, info.location);
            postNotification(info, summaryText, context, quietUpdate, doPopup, defaultVibrate,
@@ -329,9 +334,13 @@ public class AlertService extends Service {
            // Keep concurrent events high priority (to appear higher in the notification list)
            // until 15 minutes into the event.
            boolean highPriority = false;
            if (currentTime < info.startMillis + (15 * 60 * 1000)) {
            long gracePeriodEnd = info.startMillis + DEPRIORITIZE_GRACE_PERIOD_MS;
            if (currentTime < gracePeriodEnd) {
                highPriority = true;
                nextRefreshTime = Math.min(nextRefreshTime, gracePeriodEnd);
            }
            nextRefreshTime = Math.min(nextRefreshTime, info.endMillis);

            postNotification(info, summaryText, context, quietUpdate, (doPopup && highPriority),
                    defaultVibrate, ringtone, highPriority, nm);
        }
@@ -353,7 +362,9 @@ public class AlertService extends Service {
                notification = AlertReceiver.makeDigestNotification(context,
                    expiredEvents, expiredDigestTitle);
            }
            addNotificationOptions(notification, quietUpdate, null, defaultVibrate, ringtone);

            // Add options for a quiet update.
            addNotificationOptions(notification, true, expiredDigestTitle, defaultVibrate, ringtone);

            // Remove any individual expired notifications before posting.
            for (NotificationInfo expiredInfo : expiredEvents) {
@@ -368,7 +379,14 @@ public class AlertService extends Service {
                        + ", notificationId:" + AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID
                        + (quietUpdate ? ", quiet" : ", loud"));
            }
        } else {
            nm.cancel(AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID);
        }

        // Schedule the next silent refresh time so notifications will change
        // buckets (eg. drop into expired digest, etc).
        AlertUtils.scheduleNextNotificationRefresh(context, null, nextRefreshTime);

        return true;
    }

+27 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.calendar.alerts;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
@@ -64,19 +65,42 @@ public class AlertUtils {
     * @param alarmTime The time to fire the intent in UTC millis since epoch
     */
    public static void scheduleAlarm(Context context, AlarmManager manager, long alarmTime) {
        scheduleAlarmHelper(context, manager, alarmTime, false);
    }

        // The default snooze delay: 5 minutes
    /**
     * Schedules the next alarm to silently refresh the notifications.  Note that if there
     * is a pending silent refresh alarm, it will be replaced with this one.
     */
    static void scheduleNextNotificationRefresh(Context context, AlarmManager manager,
            long alarmTime) {
        scheduleAlarmHelper(context, manager, alarmTime, true);
    }

    private static void scheduleAlarmHelper(Context context, AlarmManager manager, long alarmTime,
            boolean quietUpdate) {
        if (manager == null) {
            manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        }

        int alarmType = AlarmManager.RTC_WAKEUP;
        Intent intent = new Intent(CalendarContract.ACTION_EVENT_REMINDER);
        intent.setData(CalendarAlerts.CONTENT_URI);
        intent.setClass(context, AlertReceiver.class);
        if (quietUpdate) {
            intent.putExtra(AlertUtils.QUIET_UPDATE_KEY, true);
            alarmType = AlarmManager.RTC;
        } else {
            // Set data field so we get a unique PendingIntent instance per alarm or else alarms
            // may be dropped.
            Uri.Builder builder = CalendarAlerts.CONTENT_URI.buildUpon();
            ContentUris.appendId(builder, alarmTime);
            intent.setData(builder.build());
        }

        intent.putExtra(CalendarContract.CalendarAlerts.ALARM_TIME, alarmTime);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        manager.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
        manager.set(alarmType, alarmTime, pi);
    }

    /**