summaryrefslogtreecommitdiff
path: root/src/com/android/mail/utils/NotificationActionUtils.java
blob: b3935f62a8f2aa8ae262e43d7634f9842fc1727c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.mail.utils;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import androidx.core.app.NotificationCompat;
import androidx.core.app.RemoteInput;
import androidx.core.app.TaskStackBuilder;
import android.widget.RemoteViews;

import com.android.mail.MailIntentService;
import com.android.mail.NotificationActionIntentService;
import com.android.mail.R;
import com.android.mail.compose.ComposeActivity;
import com.android.mail.providers.Account;
import com.android.mail.providers.Conversation;
import com.android.mail.providers.Folder;
import com.android.mail.providers.Message;
import com.android.mail.providers.UIProvider;
import com.android.mail.providers.UIProvider.ConversationOperations;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class NotificationActionUtils {
    private static final String LOG_TAG = "NotifActionUtils";

    public static final String WEAR_REPLY_INPUT = "wear_reply";

    private static long sUndoTimeoutMillis = -1;

    /**
     * If an {@link NotificationAction} exists here for a given notification key, then we should
     * display this undo notification rather than an email notification.
     */
    public static final ObservableSparseArrayCompat<NotificationAction> sUndoNotifications =
            new ObservableSparseArrayCompat<NotificationAction>();

    /**
     * If a {@link Conversation} exists in this set, then the undo notification for this
     * {@link Conversation} was tapped by the user in the notification drawer.
     * We need to properly handle notification actions for this case.
     */
    public static final Set<Conversation> sUndoneConversations = Sets.newHashSet();

    /**
     * If an undo notification is displayed, its timestamp
     * ({@link android.app.Notification.Builder#setWhen(long)}) is stored here so we can use it for
     * the original notification if the action is undone.
     */
    public static final SparseLongArray sNotificationTimestamps = new SparseLongArray();

    public enum NotificationActionType {
        ARCHIVE_REMOVE_LABEL("archive", true, R.drawable.ic_archive_wht_24dp,
                R.drawable.ic_remove_label_wht_24dp, R.string.notification_action_archive,
                R.string.notification_action_remove_label, new ActionToggler() {
            @Override
            public boolean shouldDisplayPrimary(final Folder folder,
                    final Conversation conversation, final Message message) {
                return folder == null || folder.isInbox();
            }
        }),
        DELETE("delete", true, R.drawable.ic_delete_wht_24dp,
                R.string.notification_action_delete),
        REPLY("reply", false, R.drawable.ic_reply_wht_24dp, R.string.notification_action_reply),
        REPLY_ALL("reply_all", false, R.drawable.ic_reply_all_wht_24dp,
                R.string.notification_action_reply_all);

        private final String mPersistedValue;
        private final boolean mIsDestructive;

        private final int mActionIcon;
        private final int mActionIcon2;

        private final int mDisplayString;
        private final int mDisplayString2;

        private final ActionToggler mActionToggler;

        private static final Map<String, NotificationActionType> sPersistedMapping;

        private interface ActionToggler {
            /**
             * Determines if we should display the primary or secondary text/icon.
             *
             * @return <code>true</code> to display primary, <code>false</code> to display secondary
             */
            boolean shouldDisplayPrimary(Folder folder, Conversation conversation, Message message);
        }

        static {
            final NotificationActionType[] values = values();
            final ImmutableMap.Builder<String, NotificationActionType> mapBuilder =
                    new ImmutableMap.Builder<String, NotificationActionType>();

            for (int i = 0; i < values.length; i++) {
                mapBuilder.put(values[i].getPersistedValue(), values[i]);
            }

            sPersistedMapping = mapBuilder.build();
        }

        private NotificationActionType(final String persistedValue, final boolean isDestructive,
                final int actionIcon, final int displayString) {
            mPersistedValue = persistedValue;
            mIsDestructive = isDestructive;
            mActionIcon = actionIcon;
            mActionIcon2 = -1;
            mDisplayString = displayString;
            mDisplayString2 = -1;
            mActionToggler = null;
        }

        private NotificationActionType(final String persistedValue, final boolean isDestructive,
                final int actionIcon, final int actionIcon2, final int displayString,
                final int displayString2, final ActionToggler actionToggler) {
            mPersistedValue = persistedValue;
            mIsDestructive = isDestructive;
            mActionIcon = actionIcon;
            mActionIcon2 = actionIcon2;
            mDisplayString = displayString;
            mDisplayString2 = displayString2;
            mActionToggler = actionToggler;
        }

        public static NotificationActionType getActionType(final String persistedValue) {
            return sPersistedMapping.get(persistedValue);
        }

        public String getPersistedValue() {
            return mPersistedValue;
        }

        public boolean getIsDestructive() {
            return mIsDestructive;
        }

        public int getActionIconResId(final Folder folder, final Conversation conversation,
                final Message message) {
            if (mActionToggler == null || mActionToggler.shouldDisplayPrimary(folder, conversation,
                    message)) {
                return mActionIcon;
            }

            return mActionIcon2;
        }

        public int getDisplayStringResId(final Folder folder, final Conversation conversation,
                final Message message) {
            if (mActionToggler == null || mActionToggler.shouldDisplayPrimary(folder, conversation,
                    message)) {
                return mDisplayString;
            }

            return mDisplayString2;
        }
    }

    /**
     * Adds the appropriate notification actions to the specified
     * {@link androidx.core.app.NotificationCompat.Builder}
     *
     * @param notificationIntent The {@link Intent} used when the notification is clicked
     * @param when The value passed into {@link android.app.Notification.Builder#setWhen(long)}.
     *        This is used for maintaining notification ordering with the undo bar
     * @param notificationActions A {@link Set} set of the actions to display
     */
    public static void addNotificationActions(final Context context,
            final Intent notificationIntent, final NotificationCompat.Builder notification,
            NotificationCompat.WearableExtender wearExtender, final Account account,
            final Conversation conversation, final Message message,
            final Folder folder, final int notificationId, final long when,
            final Set<String> notificationActions) {
        final List<NotificationActionType> sortedActions =
                getSortedNotificationActions(folder, notificationActions);

        for (final NotificationActionType notificationAction : sortedActions) {
            final PendingIntent pendingIntent = getNotificationActionPendingIntent(
                    context, account, conversation, message,
                    folder, notificationIntent, notificationAction, notificationId, when);
            final int actionIconResId = notificationAction.getActionIconResId(folder, conversation,
                    message);
            final String title = context.getString(notificationAction.getDisplayStringResId(
                    folder, conversation, message));

            // Always add all actions to both standard and wearable notifications.
            notification.addAction(actionIconResId, title, pendingIntent);

            // Use a different intent for wear because it triggers different set of behavior:
            // no undo for archive/delete, and mark conversation as read after reply.
            final PendingIntent wearPendingIntent = getWearNotificationActionPendingIntent(
                    context, account, conversation, message,
                    folder, notificationIntent, notificationAction, notificationId, when);

            final NotificationCompat.Action.Builder wearableActionBuilder =
                    new NotificationCompat.Action.Builder(
                            mapWearActionResId(notificationAction, actionIconResId), title,
                            wearPendingIntent);

            if (notificationAction == NotificationActionType.REPLY
                    || notificationAction == NotificationActionType.REPLY_ALL) {
                final String[] choices = context.getResources().getStringArray(
                        R.array.reply_choices);
                wearableActionBuilder.addRemoteInput(
                        new RemoteInput.Builder(WEAR_REPLY_INPUT)
                                .setLabel(title)
                                .setChoices(choices)
                                .build());
            }

            wearExtender.addAction(wearableActionBuilder.build());
            LogUtils.d(LOG_TAG, "Adding wearable action!!");
        }
    }

    private static int mapWearActionResId(NotificationActionType notificationAction,
            int defActionIconResId) {
        switch (notificationAction) {
            case REPLY:
                return R.drawable.ic_wear_full_reply;
            case REPLY_ALL:
                return R.drawable.ic_wear_full_reply_all;
            case ARCHIVE_REMOVE_LABEL:
                return R.drawable.ic_wear_full_archive;
            case DELETE:
                return R.drawable.ic_wear_full_delete;
            default:
                return defActionIconResId;
        }
    }

    /**
     * Sorts the notification actions into the appropriate order, based on current label
     *
     * @param folder The {@link Folder} being notified
     * @param notificationActionStrings The action strings to sort
     */
    private static List<NotificationActionType> getSortedNotificationActions(
            final Folder folder, final Collection<String> notificationActionStrings) {
        final List<NotificationActionType> unsortedActions =
                new ArrayList<NotificationActionType>(notificationActionStrings.size());
        for (final String action : notificationActionStrings) {
            unsortedActions.add(NotificationActionType.getActionType(action));
        }

        final List<NotificationActionType> sortedActions =
                new ArrayList<NotificationActionType>(unsortedActions.size());

        if (folder.isInbox()) {
            // Inbox
            /*
             * Action 1: Archive, Delete, Mute, Mark read, Add star, Mark important, Reply, Reply
             * all, Forward
             */
            /*
             * Action 2: Reply, Reply all, Forward, Mark important, Add star, Mark read, Mute,
             * Delete, Archive
             */
            if (unsortedActions.contains(NotificationActionType.ARCHIVE_REMOVE_LABEL)) {
                sortedActions.add(NotificationActionType.ARCHIVE_REMOVE_LABEL);
            }
            if (unsortedActions.contains(NotificationActionType.DELETE)) {
                sortedActions.add(NotificationActionType.DELETE);
            }
            if (unsortedActions.contains(NotificationActionType.REPLY)) {
                sortedActions.add(NotificationActionType.REPLY);
            }
            if (unsortedActions.contains(NotificationActionType.REPLY_ALL)) {
                sortedActions.add(NotificationActionType.REPLY_ALL);
            }
        } else if (folder.isProviderFolder()) {
            // Gmail system labels
            /*
             * Action 1: Delete, Mute, Mark read, Add star, Mark important, Reply, Reply all,
             * Forward
             */
            /*
             * Action 2: Reply, Reply all, Forward, Mark important, Add star, Mark read, Mute,
             * Delete
             */
            if (unsortedActions.contains(NotificationActionType.DELETE)) {
                sortedActions.add(NotificationActionType.DELETE);
            }
            if (unsortedActions.contains(NotificationActionType.REPLY)) {
                sortedActions.add(NotificationActionType.REPLY);
            }
            if (unsortedActions.contains(NotificationActionType.REPLY_ALL)) {
                sortedActions.add(NotificationActionType.REPLY_ALL);
            }
        } else {
            // Gmail user created labels
            /*
             * Action 1: Remove label, Delete, Mark read, Add star, Mark important, Reply, Reply
             * all, Forward
             */
            /*
             * Action 2: Reply, Reply all, Forward, Mark important, Add star, Mark read, Delete
             */
            if (unsortedActions.contains(NotificationActionType.ARCHIVE_REMOVE_LABEL)) {
                sortedActions.add(NotificationActionType.ARCHIVE_REMOVE_LABEL);
            }
            if (unsortedActions.contains(NotificationActionType.DELETE)) {
                sortedActions.add(NotificationActionType.DELETE);
            }
            if (unsortedActions.contains(NotificationActionType.REPLY)) {
                sortedActions.add(NotificationActionType.REPLY);
            }
            if (unsortedActions.contains(NotificationActionType.REPLY_ALL)) {
                sortedActions.add(NotificationActionType.REPLY_ALL);
            }
        }

        return sortedActions;
    }

    /**
     * Creates a {@link PendingIntent} for the specified notification action.
     */
    private static PendingIntent getNotificationActionPendingIntent(final Context context,
            final Account account, final Conversation conversation, final Message message,
            final Folder folder, final Intent notificationIntent,
            final NotificationActionType action, final int notificationId, final long when) {
        final Uri messageUri = message.uri;

        final NotificationAction notificationAction = new NotificationAction(action, account,
                conversation, message, folder, conversation.id, message.serverId, message.id, when,
                NotificationAction.SOURCE_LOCAL, notificationId);

        switch (action) {
            case REPLY: {
                // Build a task stack that forces the conversation view on the stack before the
                // reply activity.
                final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);

                final Intent intent = createReplyIntent(context, account, messageUri, false);
                intent.setPackage(context.getPackageName());
                intent.setData(conversation.uri);
                intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_FOLDER, folder);

                taskStackBuilder.addNextIntent(notificationIntent).addNextIntent(intent);

                return taskStackBuilder.getPendingIntent(
                        notificationId, PendingIntent.FLAG_UPDATE_CURRENT);
            } case REPLY_ALL: {
                // Build a task stack that forces the conversation view on the stack before the
                // reply activity.
                final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);

                final Intent intent = createReplyIntent(context, account, messageUri, true);
                intent.setPackage(context.getPackageName());
                intent.setData(conversation.uri);
                intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_FOLDER, folder);

                taskStackBuilder.addNextIntent(notificationIntent).addNextIntent(intent);

                return taskStackBuilder.getPendingIntent(
                        notificationId, PendingIntent.FLAG_UPDATE_CURRENT);
            } case ARCHIVE_REMOVE_LABEL: {
                final String intentAction =
                        NotificationActionIntentService.ACTION_ARCHIVE_REMOVE_LABEL;

                final Intent intent = new Intent(intentAction);
                intent.setPackage(context.getPackageName());
                intent.setData(conversation.uri);
                putNotificationActionExtra(intent, notificationAction);

                return PendingIntent.getService(
                        context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            } case DELETE: {
                final String intentAction = NotificationActionIntentService.ACTION_DELETE;

                final Intent intent = new Intent(intentAction);
                intent.setPackage(context.getPackageName());
                intent.setData(conversation.uri);
                putNotificationActionExtra(intent, notificationAction);

                return PendingIntent.getService(
                        context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
        }

        throw new IllegalArgumentException("Invalid NotificationActionType");
    }

    /**
     * Creates a {@link PendingIntent} for the specified Wear notification action.
     */
    private static PendingIntent getWearNotificationActionPendingIntent(final Context context,
            final Account account, final Conversation conversation, final Message message,
            final Folder folder, final Intent notificationIntent,
            final NotificationActionType action, final int notificationId, final long when) {
        final Uri messageUri = message.uri;

        final NotificationAction notificationAction = new NotificationAction(action, account,
                conversation, message, folder, conversation.id, message.serverId, message.id, when,
                NotificationAction.SOURCE_REMOTE, notificationId);

        switch (action) {
            case REPLY:
            case REPLY_ALL: {
                // Build a task stack that forces the conversation view on the stack before the
                // reply activity.
                final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);

                final Intent intent = createReplyIntent(context, account, messageUri,
                        (action == NotificationActionType.REPLY_ALL));
                intent.setPackage(context.getPackageName());
                intent.setData(buildWearUri(conversation.uri));
                intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_FOLDER, folder);
                intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_CONVERSATION, conversation.uri);

                taskStackBuilder.addNextIntent(notificationIntent).addNextIntent(intent);

                return taskStackBuilder.getPendingIntent(notificationId,
                        PendingIntent.FLAG_UPDATE_CURRENT);
            }
            case ARCHIVE_REMOVE_LABEL:
            case DELETE: {
                final String intentAction = (action == NotificationActionType.ARCHIVE_REMOVE_LABEL)
                        ? NotificationActionIntentService.ACTION_ARCHIVE_REMOVE_LABEL
                        : NotificationActionIntentService.ACTION_DELETE;

                final Intent intent = new Intent(intentAction);
                intent.setPackage(context.getPackageName());
                intent.setData(buildWearUri(conversation.uri));
                putNotificationActionExtra(intent, notificationAction);

                return PendingIntent.getService(context, notificationId, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
            }
        }

        throw new IllegalArgumentException("Invalid NotificationActionType");
    }

    private static Uri buildWearUri(Uri uri) {
        return uri.buildUpon().appendQueryParameter("type", "wear").build();
    }

    /**
     * @return an intent which, if launched, will reply to the conversation
     */
    public static Intent createReplyIntent(final Context context, final Account account,
            final Uri messageUri, final boolean isReplyAll) {
        final Intent intent = ComposeActivity.createReplyIntent(context, account, messageUri,
                isReplyAll);
        intent.putExtra(Utils.EXTRA_FROM_NOTIFICATION, true);
        return intent;
    }

    public static class NotificationAction implements Parcelable {
        public static final int SOURCE_LOCAL = 0;
        public static final int SOURCE_REMOTE = 1;

        private final NotificationActionType mNotificationActionType;
        private final Account mAccount;
        private final Conversation mConversation;
        private final Message mMessage;
        private final Folder mFolder;
        private final long mConversationId;
        private final String mMessageId;
        private final long mLocalMessageId;
        private final long mWhen;
        private final int mSource;
        private final int mNotificationId;

        public NotificationAction(final NotificationActionType notificationActionType,
                final Account account, final Conversation conversation, final Message message,
                final Folder folder, final long conversationId, final String messageId,
                final long localMessageId, final long when, final int source,
                final int notificationId) {
            mNotificationActionType = notificationActionType;
            mAccount = account;
            mConversation = conversation;
            mMessage = message;
            mFolder = folder;
            mConversationId = conversationId;
            mMessageId = messageId;
            mLocalMessageId = localMessageId;
            mWhen = when;
            mSource = source;
            mNotificationId = notificationId;
        }

        public NotificationActionType getNotificationActionType() {
            return mNotificationActionType;
        }

        public Account getAccount() {
            return mAccount;
        }

        public Conversation getConversation() {
            return mConversation;
        }

        public Message getMessage() {
            return mMessage;
        }

        public Folder getFolder() {
            return mFolder;
        }

        public long getConversationId() {
            return mConversationId;
        }

        public String getMessageId() {
            return mMessageId;
        }

        public long getLocalMessageId() {
            return mLocalMessageId;
        }

        public long getWhen() {
            return mWhen;
        }

        public int getSource() {
            return mSource;
        }

        public int getNotificationId() {
            return mNotificationId;
        }

        public int getActionTextResId() {
            switch (mNotificationActionType) {
                case ARCHIVE_REMOVE_LABEL:
                    if (mFolder.isInbox()) {
                        return R.string.notification_action_undo_archive;
                    } else {
                        return R.string.notification_action_undo_remove_label;
                    }
                case DELETE:
                    return R.string.notification_action_undo_delete;
                default:
                    throw new IllegalStateException(
                            "There is no action text for this NotificationActionType.");
            }
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(final Parcel out, final int flags) {
            out.writeInt(mNotificationActionType.ordinal());
            out.writeParcelable(mAccount, 0);
            out.writeParcelable(mConversation, 0);
            out.writeParcelable(mMessage, 0);
            out.writeParcelable(mFolder, 0);
            out.writeLong(mConversationId);
            out.writeString(mMessageId);
            out.writeLong(mLocalMessageId);
            out.writeLong(mWhen);
            out.writeInt(mSource);
            out.writeInt(mNotificationId);
        }

        public static final Parcelable.ClassLoaderCreator<NotificationAction> CREATOR =
                new Parcelable.ClassLoaderCreator<NotificationAction>() {
                    @Override
                    public NotificationAction createFromParcel(final Parcel in) {
                        return new NotificationAction(in, null);
                    }

                    @Override
                    public NotificationAction[] newArray(final int size) {
                        return new NotificationAction[size];
                    }

                    @Override
                    public NotificationAction createFromParcel(
                            final Parcel in, final ClassLoader loader) {
                        return new NotificationAction(in, loader);
                    }
                };

        private NotificationAction(final Parcel in, final ClassLoader loader) {
            mNotificationActionType = NotificationActionType.values()[in.readInt()];
            mAccount = in.readParcelable(loader);
            mConversation = in.readParcelable(loader);
            mMessage = in.readParcelable(loader);
            mFolder = in.readParcelable(loader);
            mConversationId = in.readLong();
            mMessageId = in.readString();
            mLocalMessageId = in.readLong();
            mWhen = in.readLong();
            mSource = in.readInt();
            mNotificationId = in.readInt();
        }
    }

    public static Notification createUndoNotification(final Context context,
            final NotificationAction notificationAction, final int notificationId) {
        LogUtils.i(LOG_TAG, "createUndoNotification for %s",
                notificationAction.getNotificationActionType());

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setSmallIcon(R.drawable.ic_notification_mail_24dp);
        builder.setWhen(notificationAction.getWhen());
        builder.setCategory(NotificationCompat.CATEGORY_EMAIL);

        final RemoteViews undoView =
                new RemoteViews(context.getPackageName(), R.layout.undo_notification);
        undoView.setTextViewText(
                R.id.description_text, context.getString(notificationAction.getActionTextResId()));

        final String packageName = context.getPackageName();

        final Intent clickIntent = new Intent(NotificationActionIntentService.ACTION_UNDO);
        clickIntent.setPackage(packageName);
        clickIntent.setData(notificationAction.mConversation.uri);
        putNotificationActionExtra(clickIntent, notificationAction);
        final PendingIntent clickPendingIntent = PendingIntent.getService(context, notificationId,
                clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent);

        builder.setContent(undoView);

        // When the notification is cleared, we perform the destructive action
        final Intent deleteIntent = new Intent(NotificationActionIntentService.ACTION_DESTRUCT);
        deleteIntent.setPackage(packageName);
        deleteIntent.setData(notificationAction.mConversation.uri);
        putNotificationActionExtra(deleteIntent, notificationAction);
        final PendingIntent deletePendingIntent = PendingIntent.getService(context,
                notificationId, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setDeleteIntent(deletePendingIntent);

        final Notification notification = builder.build();

        return notification;
    }

    /**
     * Registers a timeout for the undo notification such that when it expires, the undo bar will
     * disappear, and the action will be performed.
     */
    public static void registerUndoTimeout(
            final Context context, final NotificationAction notificationAction) {
        LogUtils.i(LOG_TAG, "registerUndoTimeout for %s",
                notificationAction.getNotificationActionType());

        if (sUndoTimeoutMillis == -1) {
            sUndoTimeoutMillis =
                    context.getResources().getInteger(R.integer.undo_notification_timeout);
        }

        final AlarmManager alarmManager =
                (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        final long triggerAtMills = SystemClock.elapsedRealtime() + sUndoTimeoutMillis;

        final PendingIntent pendingIntent =
                createUndoTimeoutPendingIntent(context, notificationAction);

        alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtMills, pendingIntent);
    }

    /**
     * Cancels the undo timeout for a notification action. This should be called if the undo
     * notification is clicked (to prevent the action from being performed anyway) or cleared (since
     * we have already performed the action).
     */
    public static void cancelUndoTimeout(
            final Context context, final NotificationAction notificationAction) {
        LogUtils.i(LOG_TAG, "cancelUndoTimeout for %s",
                notificationAction.getNotificationActionType());

        final AlarmManager alarmManager =
                (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        final PendingIntent pendingIntent =
                createUndoTimeoutPendingIntent(context, notificationAction);

        alarmManager.cancel(pendingIntent);
    }

    /**
     * Creates a {@link PendingIntent} to be used for creating and canceling the undo timeout
     * alarm.
     */
    private static PendingIntent createUndoTimeoutPendingIntent(
            final Context context, final NotificationAction notificationAction) {
        final Intent intent = new Intent(NotificationActionIntentService.ACTION_UNDO_TIMEOUT);
        intent.setPackage(context.getPackageName());
        intent.setData(notificationAction.mConversation.uri);
        putNotificationActionExtra(intent, notificationAction);

        final int requestCode = notificationAction.getAccount().hashCode()
                ^ notificationAction.getFolder().hashCode();
        final PendingIntent pendingIntent =
                PendingIntent.getService(context, requestCode, intent, 0);

        return pendingIntent;
    }

    /**
     * Processes the specified destructive action (archive, delete, mute) on the message.
     */
    public static void processDestructiveAction(
            final Context context, final NotificationAction notificationAction) {
        LogUtils.i(LOG_TAG, "processDestructiveAction: %s",
                notificationAction.getNotificationActionType());

        final NotificationActionType destructAction =
                notificationAction.getNotificationActionType();
        final Conversation conversation = notificationAction.getConversation();
        final Folder folder = notificationAction.getFolder();

        final ContentResolver contentResolver = context.getContentResolver();
        final Uri uri = conversation.uri.buildUpon().appendQueryParameter(
                UIProvider.FORCE_UI_NOTIFICATIONS_QUERY_PARAMETER, Boolean.TRUE.toString()).build();

        switch (destructAction) {
            case ARCHIVE_REMOVE_LABEL: {
                if (folder.isInbox()) {
                    // Inbox, so archive
                    final ContentValues values = new ContentValues(1);
                    values.put(UIProvider.ConversationOperations.OPERATION_KEY,
                            UIProvider.ConversationOperations.ARCHIVE);

                    contentResolver.update(uri, values, null, null);
                } else {
                    // Not inbox, so remove label
                    final ContentValues values = new ContentValues(1);

                    final String removeFolderUri = folder.folderUri.fullUri.buildUpon()
                            .appendPath(Boolean.FALSE.toString()).toString();
                    values.put(ConversationOperations.FOLDERS_UPDATED, removeFolderUri);

                    contentResolver.update(uri, values, null, null);
                }
                break;
            }
            case DELETE: {
                contentResolver.delete(uri, null, null);
                break;
            }
            default:
                throw new IllegalArgumentException(
                        "The specified NotificationActionType is not a destructive action.");
        }
    }

    /**
     * Creates and displays an Undo notification for the specified {@link NotificationAction}.
     */
    public static void createUndoNotification(final Context context,
            final NotificationAction notificationAction) {
        LogUtils.i(LOG_TAG, "createUndoNotification for %s",
                notificationAction.getNotificationActionType());

        final int notificationId = NotificationUtils.getNotificationId(
                notificationAction.getAccount().getAccountManagerAccount(),
                notificationAction.getFolder());

        final Notification notification =
                createUndoNotification(context, notificationAction, notificationId);

        final NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, notification);

        sUndoNotifications.put(notificationId, notificationAction);
        sNotificationTimestamps.put(notificationId, notificationAction.getWhen());
    }

    /**
     * Called when an Undo notification has been tapped.
     */
    public static void cancelUndoNotification(final Context context,
            final NotificationAction notificationAction) {
        LogUtils.i(LOG_TAG, "cancelUndoNotification for %s",
                notificationAction.getNotificationActionType());

        final Account account = notificationAction.getAccount();
        final Folder folder = notificationAction.getFolder();
        final Conversation conversation = notificationAction.getConversation();
        final int notificationId =
                NotificationUtils.getNotificationId(account.getAccountManagerAccount(), folder);

        // Note: we must add the conversation before removing the undo notification
        // Otherwise, the observer for sUndoNotifications gets called, which calls
        // handleNotificationActions before the undone conversation has been added to the set.
        sUndoneConversations.add(conversation);
        removeUndoNotification(context, notificationId, false);
        resendNotifications(context, account, folder);
    }

    /**
     * If an undo notification is left alone for a long enough time, it will disappear, this method
     * will be called, and the action will be finalized.
     */
    public static void processUndoNotification(final Context context,
            final NotificationAction notificationAction) {
        LogUtils.i(LOG_TAG, "processUndoNotification, %s",
                notificationAction.getNotificationActionType());

        final Account account = notificationAction.getAccount();
        final Folder folder = notificationAction.getFolder();
        final int notificationId = NotificationUtils.getNotificationId(
                account.getAccountManagerAccount(), folder);
        removeUndoNotification(context, notificationId, true);
        sNotificationTimestamps.delete(notificationId);
        processDestructiveAction(context, notificationAction);
    }

    /**
     * Removes the undo notification.
     *
     * @param removeNow <code>true</code> to remove it from the drawer right away,
     *        <code>false</code> to just remove the reference to it
     */
    private static void removeUndoNotification(
            final Context context, final int notificationId, final boolean removeNow) {
        sUndoNotifications.delete(notificationId);

        if (removeNow) {
            final NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(notificationId);
        }
    }

    /**
     * Broadcasts an {@link Intent} to inform the app to resend its notifications.
     */
    public static void resendNotifications(final Context context, final Account account,
            final Folder folder) {
        LogUtils.i(LOG_TAG, "resendNotifications account: %s, folder: %s",
                account == null ? null : LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
                folder == null ? null : LogUtils.sanitizeName(LOG_TAG, folder.name));

        final Intent intent = new Intent(MailIntentService.ACTION_RESEND_NOTIFICATIONS);
        intent.setPackage(context.getPackageName()); // Make sure we only deliver this to ourselves
        if (account != null) {
            intent.putExtra(Utils.EXTRA_ACCOUNT_URI, account.uri);
        }
        if (folder != null) {
            intent.putExtra(Utils.EXTRA_FOLDER_URI, folder.folderUri.fullUri);
        }
        context.startService(intent);
    }

    public static void registerUndoNotificationObserver(final DataSetObserver observer) {
        sUndoNotifications.getDataSetObservable().registerObserver(observer);
    }

    public static void unregisterUndoNotificationObserver(final DataSetObserver observer) {
        sUndoNotifications.getDataSetObservable().unregisterObserver(observer);
    }

    /**
     * <p>
     * This is a slight hack to avoid an exception in the remote AlarmManagerService process. The
     * AlarmManager adds extra data to this Intent which causes it to inflate. Since the remote
     * process does not know about the NotificationAction class, it throws a ClassNotFoundException.
     * </p>
     * <p>
     * To avoid this, we marshall the data ourselves and then parcel a plain byte[] array. The
     * NotificationActionIntentService class knows to build the NotificationAction object from the
     * byte[] array.
     * </p>
     */
    private static void putNotificationActionExtra(final Intent intent,
            final NotificationAction notificationAction) {
        final Parcel out = Parcel.obtain();
        notificationAction.writeToParcel(out, 0);
        out.setDataPosition(0);
        intent.putExtra(NotificationActionIntentService.EXTRA_NOTIFICATION_ACTION, out.marshall());
    }
}