NotificationUtils.java revision c56b233013cf107c702ef9f61305282670ad804a
1d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/*
2d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Copyright (C) 2013 The Android Open Source Project
3d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
4d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
5d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * you may not use this file except in compliance with the License.
6d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * You may obtain a copy of the License at
7d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
8d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
9d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
10d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Unless required by applicable law or agreed to in writing, software
11d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
12d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * See the License for the specific language governing permissions and
14d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * limitations under the License.
15d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
16d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypackage com.android.mail.utils;
17d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
18d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.Notification;
19d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.NotificationManager;
20d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.PendingIntent;
21d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentResolver;
22d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentUris;
23d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentValues;
24d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Context;
25d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Intent;
26d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.res.Resources;
27d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.database.Cursor;
28d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.Bitmap;
29d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.BitmapFactory;
30d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.net.Uri;
31d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract;
32d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract.CommonDataKinds.Email;
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract.Contacts.Photo;
34d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.support.v4.app.NotificationCompat;
35d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableString;
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableStringBuilder;
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils;
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.CharacterStyle;
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.TextAppearanceSpan;
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.util.Pair;
4161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedyimport android.util.SparseArray;
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
43d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.EmailAddress;
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
45d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.R;
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.MessageCursor;
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.SendersView;
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.AccountPreferences;
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.FolderPreferences;
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.MailPrefs;
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Account;
52390cab939304705cc8adeb8e4810abf904fec22fScott Kennedyimport com.android.mail.providers.Address;
53d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Conversation;
54d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Folder;
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Message;
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.UIProvider;
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationAction;
5809400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HTML;
5909400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HTML4;
6009400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HtmlDocument;
6109400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HtmlTree;
62ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sappersteinimport com.google.common.base.Objects;
637f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedyimport com.google.common.collect.ImmutableList;
6409400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Lists;
6509400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Sets;
66d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
67d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.io.ByteArrayInputStream;
68d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.ArrayList;
69d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Arrays;
70d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Collection;
71d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.List;
72d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.concurrent.ConcurrentHashMap;
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
75d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class NotificationUtils {
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static final String LOG_TAG = LogTag.getLogTag();
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** Contains a list of <(account, label), unread conversations> */
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static NotificationMap sActiveNotificationMap = null;
80d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
8161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static final SparseArray<Bitmap> sNotificationIcons = new SparseArray<Bitmap>();
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static TextAppearanceSpan sNotificationUnreadStyleSpan;
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharacterStyle sNotificationReadStyleSpan;
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** A factory that produces a plain text converter that removes elided text. */
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final HtmlTree.PlainTextConverterFactory MESSAGE_CONVERTER_FACTORY =
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            new HtmlTree.PlainTextConverterFactory() {
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                @Override
90d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                public HtmlTree.PlainTextConverter createInstance() {
91d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return new MailMessagePlainTextConverter();
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            };
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears all notifications in response to the user tapping "Clear" in the status bar.
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearAllNotfications(Context context) {
9913a732747e1cf835b04211fad09b722accf877d5Scott Kennedy        LogUtils.v(LOG_TAG, "NotificationUtils: Clearing all notifications.");
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.clear();
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns the notification map, creating it if necessary.
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static synchronized NotificationMap getNotificationMap(Context context) {
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sActiveNotificationMap == null) {
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap = new NotificationMap();
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // populate the map from the cached data
113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap.loadNotificationMap(context);
114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return sActiveNotificationMap;
116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Class representing the existing notifications, and the number of unread and
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * unseen conversations that triggered each.
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationMap
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            extends ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>> {
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String NOTIFICATION_PART_SEPARATOR = " ";
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final int NUM_NOTIFICATION_PARTS= 4;
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Retuns the unread count for the given NotificationKey.
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnread(NotificationKey key) {
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value = get(key);
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.first : null;
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Retuns the unread unseen count for the given NotificationKey.
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnseen(NotificationKey key) {
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value = get(key);
141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.second : null;
142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Store the unread and unseen value for the given NotificationKey
146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void put(NotificationKey key, int unread, int unseen) {
148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value =
149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Pair<Integer, Integer>(Integer.valueOf(unread), Integer.valueOf(unseen));
150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            put(key, value);
151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Populates the notification map with previously cached data.
155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void loadNotificationMap(final Context context) {
157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = mailPrefs.getActiveNotificationSet();
159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (notificationSet != null) {
160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                for (String notificationEntry : notificationSet) {
161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Get the parts of the string that make the notification entry
162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] notificationParts =
163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            TextUtils.split(notificationEntry, NOTIFICATION_PART_SEPARATOR);
164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationParts.length == NUM_NOTIFICATION_PARTS) {
165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri accountUri = Uri.parse(notificationParts[0]);
166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor accountCursor = context.getContentResolver().query(
167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                accountUri, UIProvider.ACCOUNTS_PROJECTION, null, null, null);
168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Account account;
169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (accountCursor.moveToFirst()) {
171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                account = new Account(accountCursor);
172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            accountCursor.close();
177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri folderUri = Uri.parse(notificationParts[1]);
180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor folderCursor = context.getContentResolver().query(
181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folderUri, UIProvider.FOLDERS_PROJECTION, null, null, null);
182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Folder folder;
183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (folderCursor.moveToFirst()) {
185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folder = new Folder(folderCursor);
186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            folderCursor.close();
191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final NotificationKey key = new NotificationKey(account, folder);
194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unreadValue = Integer.valueOf(notificationParts[2]);
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unseenValue = Integer.valueOf(notificationParts[3]);
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Pair<Integer, Integer> unreadUnseenValue =
197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                new Pair<Integer, Integer>(unreadValue, unseenValue);
198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        put(key, unreadUnseenValue);
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Cache the notification map.
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void saveNotificationMap(Context context) {
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = Sets.newHashSet();
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<NotificationKey> keys = keySet();
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey key : keys) {
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Pair<Integer, Integer> value = get(key);
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unreadCount = value.first;
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unseenCount = value.second;
214ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                if (unreadCount != null && unseenCount != null) {
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] partValues = new String[] {
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            key.account.uri.toString(), key.folder.uri.toString(),
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            unreadCount.toString(), unseenCount.toString()};
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues));
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            mailPrefs.cacheActiveNotificationSet(notificationSet);
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return the title of this notification with each account and the number of unread and unseen
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversations for it. Also remove any account in the map that has 0 unread.
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String createNotificationString(NotificationMap notifications) {
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder result = new StringBuilder();
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int i = 0;
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keysToRemove = Sets.newHashSet();
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : notifications.keySet()) {
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unread = notifications.getUnread(key);
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unseen = notifications.getUnseen(key);
237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unread == null || unread.intValue() == 0) {
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                keysToRemove.add(key);
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (i > 0) result.append(", ");
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                result.append(key.toString() + " (" + unread + ", " + unseen + ")");
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : keysToRemove) {
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notifications.remove(key);
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return result.toString();
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts and cancel them.
255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAllNotifications(Context context) {
25793486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy        LogUtils.d(LOG_TAG, "NotificationUtils: cancelAllNotifications - cancelling all");
258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        NotificationManager nm = (NotificationManager) context.getSystemService(
259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                Context.NOTIFICATION_SERVICE);
260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        nm.cancelAll();
261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        clearAllNotfications(context);
262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, cancel them, and repost.
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAndResendNotifications(Context context) {
2694162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy        LogUtils.d(LOG_TAG, "NotificationUtils: cancelAndResendNotifications");
270ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein        resendNotifications(context, true, null, null);
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, optionally cancel them, and repost.
275ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * This happens when locale changes. If you only want to resend messages from one
276ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * account-folder pair, pass in the account and folder that should be resent.
277ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All other account-folder pairs will not have their notifications resent.
278ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All notifications will be resent if account or folder is null.
279ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *
280ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param context Current context.
281ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param cancelExisting True, if all notifications should be canceled before resending.
282ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                       False, otherwise.
283ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param accountUri The {@link Uri} of the {@link Account} of the notification
284ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                   upon which an action occurred.
285ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param folderUri The {@link Uri} of the {@link Folder} of the notification
286ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                  upon which an action occurred.
287ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     */
288ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein    public static void resendNotifications(Context context, final boolean cancelExisting,
289ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            final Uri accountUri, final Uri folderUri) {
2902638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy        LogUtils.d(LOG_TAG, "NotificationUtils: resendNotifications ");
2912638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cancelExisting) {
29393486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy            LogUtils.d(LOG_TAG, "NotificationUtils: resendNotifications - cancelling all");
294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm =
295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            nm.cancelAll();
297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Re-validate the notifications.
299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<NotificationKey> keys = notificationMap.keySet();
301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey notification : keys) {
302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder = notification.folder;
303ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            final int notificationId = getNotificationId(notification.account.name,
304ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                    folder);
305ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein
306ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // Only resend notifications if the notifications are from the same folder
307ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // and same account as the undo notification that was previously displayed.
30826b29ef558cce109c51584081c52781760a02dd9Scott Kennedy            if (accountUri != null && !Objects.equal(accountUri, notification.account.uri) &&
30926b29ef558cce109c51584081c52781760a02dd9Scott Kennedy                    folderUri != null && !Objects.equal(folderUri, folder.uri)) {
3102638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                LogUtils.d(LOG_TAG, "NotificationUtils: resendNotifications - not resending %s / %s"
3112638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                        + " because it doesn't match %s / %s",
3122638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                        notification.account.uri, folder.uri, accountUri, folderUri);
313ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                continue;
314ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            }
315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
3162638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy            LogUtils.d(LOG_TAG, "NotificationUtils: resendNotifications - resending %s / %s",
3172638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                    notification.account.uri, folder.uri);
3182638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationAction undoableAction =
320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sUndoNotifications.get(notificationId);
321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (undoableAction == null) {
322ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                validateNotifications(context, folder, notification.account, true,
323ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                        false, notification);
324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Create an undo notification
326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.createUndoNotification(context, undoableAction);
327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
331d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
332d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications for the specified account.
333d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void validateAccountNotifications(Context context, String account) {
3354162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy        LogUtils.d(LOG_TAG, "NotificationUtils: validateAccountNotifications - %s", account);
3364162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        List<NotificationKey> notificationsToCancel = Lists.newArrayList();
338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Iterate through the notification map to see if there are any entries that correspond to
339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // labels that are not in the sync set.
340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keys = notificationMap.keySet();
342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final AccountPreferences accountPreferences = new AccountPreferences(context, account);
343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean enabled = accountPreferences.areNotificationsEnabled();
344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (!enabled) {
345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Cancel all notifications for this account
346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (notification.account.name.equals(account)) {
348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationsToCancel.add(notification);
349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Iterate through the notification map to see if there are any entries that
353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // correspond to labels that are not in the notification set.
354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (notification.account.name.equals(account)) {
356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // If notification is not enabled for this label, remember this NotificationKey
357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // to later cancel the notification, and remove the entry from the map
358d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Folder folder = notification.folder;
359d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final boolean isInbox =
360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            notification.account.settings.defaultInbox.equals(folder.uri);
361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final FolderPreferences folderPreferences = new FolderPreferences(
362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            context, notification.account.name, folder, isInbox);
363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!folderPreferences.areNotificationsEnabled()) {
365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationsToCancel.add(notification);
366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
368d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
370d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Cancel & remove the invalid notifications.
372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationsToCancel.size() > 0) {
373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm = (NotificationManager) context.getSystemService(
374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Context.NOTIFICATION_SERVICE);
375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : notificationsToCancel) {
376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Folder folder = notification.folder;
377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int notificationId = getNotificationId(notification.account.name, folder);
37893486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                LogUtils.d(LOG_TAG,
37993486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                        "NotificationUtils: validateAccountNotifications - cancelling %s / %s",
38093486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                        notification.account.name, folder.persistentId);
381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notificationMap.remove(notification);
383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sUndoNotifications.remove(notificationId);
384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.saveNotificationMap(context);
387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Display only one notification.
392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void setNewEmailIndicator(Context context, final int unreadCount,
394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int unseenCount, final Account account, final Folder folder,
395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean getAttention) {
3964162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy        LogUtils.d(LOG_TAG, "NotificationUtils: setNewEmailIndicator unreadCount = %d, "
3974162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy            + "unseenCount = %d, account = %s, folder = %s, getAttention = %b", unreadCount,
3984162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy            unseenCount, account.name, folder.uri, getAttention);
3994162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean ignoreUnobtrusiveSetting = false;
401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
402dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy        final int notificationId = getNotificationId(account.name, folder);
403dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy
404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Update the notification map
405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount == 0) {
40893486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy            LogUtils.d(LOG_TAG,
40993486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                    "NotificationUtils: setNewEmailIndicator - cancelling %s / %s",
41093486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                    account.name, folder.persistentId);
411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.remove(key);
412dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy            ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
413dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy                    .cancel(notificationId);
414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!notificationMap.containsKey(key)) {
416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This account previously didn't have any unread mail; ignore the "unobtrusive
417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notifications" setting and play sound and/or vibrate the device even if a
418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notification already exists (bug 2412348).
419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                ignoreUnobtrusiveSetting = true;
420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.put(key, unreadCount, unseenCount);
422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
42613a732747e1cf835b04211fad09b722accf877d5Scott Kennedy            LogUtils.v(LOG_TAG, "NotificationUtils: New email: %s mapSize: %d getAttention: %b",
427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    createNotificationString(notificationMap), notificationMap.size(),
428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    getAttention);
429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (NotificationActionUtils.sUndoNotifications.get(notificationId) == null) {
432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            validateNotifications(context, folder, account, getAttention, ignoreUnobtrusiveSetting,
433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    key);
434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications notification.
439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void validateNotifications(Context context, final Folder folder,
441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, boolean getAttention, boolean ignoreUnobtrusiveSetting,
442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key) {
443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        NotificationManager nm = (NotificationManager)
445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context.getSystemService(Context.NOTIFICATION_SERVICE);
446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
44913a732747e1cf835b04211fad09b722accf877d5Scott Kennedy            LogUtils.v(LOG_TAG, "NotificationUtils: Validating Notification: %s mapSize: %d "
45013a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    + "folder: %s getAttention: %b", createNotificationString(notificationMap),
451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationMap.size(), folder.name, getAttention);
452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // The number of unread messages for this account and label.
454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unread = notificationMap.getUnread(key);
455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int unreadCount = unread != null ? unread.intValue() : 0;
456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unseen = notificationMap.getUnseen(key);
457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int unseenCount = unseen != null ? unseen.intValue() : 0;
458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor cursor = null;
460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
462d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Uri.Builder uriBuilder = folder.conversationListUri.buildUpon();
463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            uriBuilder.appendQueryParameter(
464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.SEEN_QUERY_PARAMETER, Boolean.FALSE.toString());
4650be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // Do not allow this quick check to disrupt any active network-enabled conversation
4660be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // cursor.
4670be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            uriBuilder.appendQueryParameter(
4680be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    UIProvider.ConversationListQueryParameters.USE_NETWORK,
4690be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    Boolean.FALSE.toString());
470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor = context.getContentResolver().query(uriBuilder.build(),
471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.CONVERSATION_PROJECTION, null, null, null);
472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int cursorUnseenCount = cursor.getCount();
473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Make sure the unseen count matches the number of items in the cursor.  But, we don't
475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // want to overwrite a 0 unseen count that was specified in the intent
476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount != 0 && unseenCount != cursorUnseenCount) {
47713a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                LogUtils.d(LOG_TAG, "NotificationUtils: "
47813a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                        + "Unseen count doesn't match cursor count.  unseen: %d cursor count: %d",
479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        unseenCount, cursorUnseenCount);
480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = cursorUnseenCount;
481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For the purpose of the notifications, the unseen count should be capped at the num of
484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // unread conversations.
485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount > unreadCount) {
486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = unreadCount;
487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int notificationId = getNotificationId(account.name, folder);
490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount == 0) {
49293486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                LogUtils.d(LOG_TAG,
49393486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                        "NotificationUtils: validateNotifications - cancelling %s / %s",
49493486c0916e222acfffb1ec8f2d68760c5209614Scott Kennedy                        account.name, folder.persistentId);
495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // We now have all we need to create the notification and the pending intent
500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            PendingIntent clickIntent;
501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setSmallIcon(R.drawable.stat_notify_email);
504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setTicker(account.name);
505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long when;
507d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long oldWhen =
509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sNotificationTimestamps.get(notificationId);
510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (oldWhen != 0) {
511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = oldWhen;
512d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = System.currentTimeMillis();
514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
515d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setWhen(when);
517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The timestamp is now stored in the notification, so we can remove it from here
519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Dispatch a CLEAR_NEW_MAIL_NOTIFICATIONS intent if the user taps the "X" next to a
522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // notification.  Also this intent gets fired when the user taps on a notification as
523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the AutoCancel flag has been set
524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Intent cancelNotificationIntent =
525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.setPackage(context.getPackageName());
52709400efa442422299acf21abe20e3470f9d965abScott Kennedy            cancelNotificationIntent.setData(Utils.appendVersionQueryParameter(context,
52809400efa442422299acf21abe20e3470f9d965abScott Kennedy                    folder.uri));
52948cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
53048cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder);
531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setDeleteIntent(PendingIntent.getService(
533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, notificationId, cancelNotificationIntent, 0));
534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Ensure that the notification is cleared when the user selects it
536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setAutoCancel(true);
537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
538d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean eventInfoConfigured = false;
539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isInbox = account.settings.defaultInbox.equals(folder.uri);
541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences =
542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new FolderPreferences(context, account.name, folder, isInbox);
543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (isInbox) {
545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new AccountPreferences(context, account.name);
547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                moveNotificationSetting(accountPreferences, folderPreferences);
548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.areNotificationsEnabled()) {
551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Don't notify
552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unreadCount > 0) {
556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // How can I order this properly?
557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor.moveToNext()) {
55809400efa442422299acf21abe20e3470f9d965abScott Kennedy                    Intent notificationIntent = createViewConversationIntent(context, account,
55909400efa442422299acf21abe20e3470f9d965abScott Kennedy                            folder, null);
560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Launch directly to the conversation, if the
562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // number of unseen conversations == 1
563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (unseenCount == 1) {
56409400efa442422299acf21abe20e3470f9d965abScott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
56509400efa442422299acf21abe20e3470f9d965abScott Kennedy                                cursor);
566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationIntent == null) {
56913a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                        LogUtils.e(LOG_TAG, "NotificationUtils: "
57013a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                                + "Null intent when building notification");
571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return;
572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    clickIntent = PendingIntent.getActivity(context, -1, notificationIntent, 0);
575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    configureLatestEventInfoFromConversation(context, account, folderPreferences,
576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            notification, cursor, clickIntent, notificationIntent,
577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            account.name, unreadCount, unseenCount, folder, when);
578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    eventInfoConfigured = true;
579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean notifyOnce = !folderPreferences.isEveryMessageNotificationEnabled();
585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
586ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (!ignoreUnobtrusiveSetting && notifyOnce) {
587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // If the user has "unobtrusive notifications" enabled, only alert the first time
588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // new mail is received in this account.  This is the default behavior.  See
589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // bugs 2412348 and 2413490.
590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setOnlyAlertOnce(true);
591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
59313a732747e1cf835b04211fad09b722accf877d5Scott Kennedy            LogUtils.d(LOG_TAG, "NotificationUtils: Account: %s vibrate: %s", account.name,
594ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                    Boolean.toString(folderPreferences.isNotificationVibrateEnabled()));
595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int defaults = 0;
597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            /*
599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * We do not want to notify if this is coming back from an Undo notification, hence the
600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * oldWhen check.
601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             */
602ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (getAttention && oldWhen == 0) {
603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
604d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new AccountPreferences(context, account.name);
605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (accountPreferences.areNotificationsEnabled()) {
606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (vibrate) {
607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        defaults |= Notification.DEFAULT_VIBRATE;
608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            : Uri.parse(ringtoneUri));
61213a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    LogUtils.d(LOG_TAG, "NotificationUtils: "
61313a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                            + "New email in %s vibrateWhen: %s, playing notification: %s",
614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            account.name, vibrate, ringtoneUri);
615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (eventInfoConfigured) {
619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                defaults |= Notification.DEFAULT_LIGHTS;
620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setDefaults(defaults);
621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (oldWhen != 0) {
623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // We do not want to display the ticker again if we are re-displaying this
624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notification (like from an Undo notification)
625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setTicker(null);
626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
628d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.notify(notificationId, notification.build());
629d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
630d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (cursor != null) {
632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                cursor.close();
633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
635d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
636d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
637d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
638d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return an {@link Intent} which, if launched, will display the corresponding conversation
639d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
64009400efa442422299acf21abe20e3470f9d965abScott Kennedy    private static Intent createViewConversationIntent(final Context context, final Account account,
64109400efa442422299acf21abe20e3470f9d965abScott Kennedy            final Folder folder, final Cursor cursor) {
642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (folder == null || account == null) {
64313a732747e1cf835b04211fad09b722accf877d5Scott Kennedy            LogUtils.e(LOG_TAG, "NotificationUtils#createViewConversationIntent(): "
64413a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    + "Null account or folder.  account: %s folder: %s", account, folder);
645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Intent intent;
649d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cursor == null) {
651b39aaf53a555c1046ef31b3fecf15d086acca013Scott Kennedy            intent = Utils.createViewFolderIntent(context, folder.uri, account);
652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // A conversation cursor has been specified, so this intent is intended to be go
654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // directly to the one new conversation
655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the Conversation object
657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
658b39aaf53a555c1046ef31b3fecf15d086acca013Scott Kennedy            intent = Utils.createViewConversationIntent(context, conversation, folder.uri,
659b39aaf53a555c1046ef31b3fecf15d086acca013Scott Kennedy                    account);
660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return intent;
663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
664d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
66561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getDefaultNotificationIcon(
66661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            final Context context, final Folder folder, final boolean multipleNew) {
667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Bitmap icon;
66861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (folder.notificationIconResId != 0) {
66961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getIcon(context, folder.notificationIconResId);
67061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        } else if (multipleNew) {
67161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getIcon(context, R.drawable.ic_notification_multiple_mail_holo_dark);
672d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
67361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getIcon(context, R.drawable.ic_contact_picture);
674d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
675d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
67861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getIcon(final Context context, final int resId) {
67961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap cachedIcon = sNotificationIcons.get(resId);
68061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (cachedIcon != null) {
68161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            return cachedIcon;
68261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        }
68361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
68461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resId);
68561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        sNotificationIcons.put(resId, icon);
68661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
68761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        return icon;
68861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    }
68961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void configureLatestEventInfoFromConversation(final Context context,
691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, final FolderPreferences folderPreferences,
692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationCompat.Builder notification, final Cursor conversationCursor,
693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final PendingIntent clickIntent, final Intent notificationIntent,
694d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationAccount, final int unreadCount, final int unseenCount,
695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder, final long when) {
696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Resources res = context.getResources();
697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
69813a732747e1cf835b04211fad09b722accf877d5Scott Kennedy        LogUtils.w(LOG_TAG, "NotificationUtils: Showing notification with unreadCount of %d and "
699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                + "unseenCount of %d", unreadCount, unseenCount);
700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String notificationTicker = null;
702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
703d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Boolean indicating that this notification is for a non-inbox label.
704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean isInbox = account.settings.defaultInbox.equals(folder.uri);
705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Notification label name for user label notifications.
707d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String notificationLabelName = isInbox ? null : folder.name;
708d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
709d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unseenCount > 1) {
710d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Build the string that describes the number of new messages
711d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String newMessagesString = res.getString(R.string.new_messages, unseenCount);
712d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Use the default notification icon
714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setLargeIcon(
71561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                    getDefaultNotificationIcon(context, folder, true /* multiple new messages */));
716d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
717d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The ticker initially start as the new messages string.
718d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = newMessagesString;
719d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
720d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The title of the notification is the new messages string
721d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setContentTitle(newMessagesString);
722d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
723c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy            // TODO(skennedy) Can we remove this check?
724d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (com.android.mail.utils.Utils.isRunningJellybeanOrLater()) {
725d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // For a new-style notification
726d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int maxNumDigestItems = context.getResources().getInteger(
727d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        R.integer.max_num_notification_digest_items);
728d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
729d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setSubText(isInbox ? notificationAccount : notificationLabelName);
731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final NotificationCompat.InboxStyle digest =
733d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new NotificationCompat.InboxStyle(notification);
734d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
735c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy                // TODO(skennedy) I do not believe this line is necessary
736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                digest.setBigContentTitle(newMessagesString);
737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int numDigestItems = 0;
739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                do {
740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Conversation conversation = new Conversation(conversationCursor);
741d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!conversation.read) {
743d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        boolean multipleUnreadThread = false;
744d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // TODO(cwren) extract this pattern into a helper
745d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
746d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Cursor cursor = null;
747d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        MessageCursor messageCursor = null;
748d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
749d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final Uri.Builder uriBuilder = conversation.messageListUri.buildUpon();
750d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            uriBuilder.appendQueryParameter(
751d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.LABEL_QUERY_PARAMETER, notificationLabelName);
752d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor = context.getContentResolver().query(uriBuilder.build(),
753d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.MESSAGE_PROJECTION, null, null, null);
754d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            messageCursor = new MessageCursor(cursor);
755d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
756b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                            String from = "";
757d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            String fromAddress = "";
758d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
759d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
760d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                fromAddress = message.getFrom();
761b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                if (fromAddress == null) {
762b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                    fromAddress = "";
763b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                }
764d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                from = getDisplayableSender(fromAddress);
765d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
766d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
767d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
768d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (!message.read &&
769d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        !fromAddress.contentEquals(message.getFrom())) {
770d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    multipleUnreadThread = true;
771d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
772d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
773d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
774d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final SpannableStringBuilder sendersBuilder;
775d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (multipleUnreadThread) {
776d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final int sendersLength =
777d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        res.getInteger(R.integer.swipe_senders_length);
778d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
779d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = getStyledSenders(context, conversationCursor,
780d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        sendersLength, notificationAccount);
781d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
7823507af9240a2401a4f5b855fd01c52ec6d1e2588Paul Westbrook                                if (from == null) {
78313a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                                    LogUtils.e(LOG_TAG, "NotificationUtils: null from string in " +
7843507af9240a2401a4f5b855fd01c52ec6d1e2588Paul Westbrook                                            "configureLatestEventInfoFromConversation");
7853507af9240a2401a4f5b855fd01c52ec6d1e2588Paul Westbrook                                    from = "";
7863507af9240a2401a4f5b855fd01c52ec6d1e2588Paul Westbrook                                }
787d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = new SpannableStringBuilder(from);
788d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
789d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final CharSequence digestLine = getSingleMessageInboxLine(context,
790d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    sendersBuilder.toString(),
791d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    conversation.subject,
792d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    conversation.snippet);
793d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            digest.addLine(digestLine);
794d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            numDigestItems++;
795d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
796d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor != null) {
797d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                messageCursor.close();
798d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
799d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (cursor != null) {
800d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                cursor.close();
801d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
802d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
803d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
804d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } while (numDigestItems <= maxNumDigestItems && conversationCursor.moveToNext());
805d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
806d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
807d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setContentText(
808d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        isInbox ? notificationAccount : notificationLabelName);
809d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
810d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
811d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For notifications for a single new conversation, we want to get the information from
812d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the conversation
813d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
814d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Move the cursor to the most recent unread conversation
815d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            seekToLatestUnreadConversation(conversationCursor);
816d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
817d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(conversationCursor);
818d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
819d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Cursor cursor = null;
820d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            MessageCursor messageCursor = null;
821d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean multipleUnseenThread = false;
822d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String from = null;
823d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            try {
824ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                final Uri uri = conversation.messageListUri.buildUpon().appendQueryParameter(
825ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                        UIProvider.LABEL_QUERY_PARAMETER, folder.persistentId).build();
826ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                cursor = context.getContentResolver().query(uri, UIProvider.MESSAGE_PROJECTION,
827ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                        null, null, null);
828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                messageCursor = new MessageCursor(cursor);
829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Use the information from the last sender in the conversation that triggered
830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // this notification.
831d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String fromAddress = "";
833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Message message = messageCursor.getMessage();
835d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    fromAddress = message.getFrom();
836d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    from = getDisplayableSender(fromAddress);
837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setLargeIcon(
83861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                            getContactIcon(context, getSenderAddress(fromAddress), folder));
839d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
840d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
841d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Assume that the last message in this conversation is unread
842d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int firstUnseenMessagePos = messageCursor.getPosition();
843d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
844d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Message message = messageCursor.getMessage();
845d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final boolean unseen = !message.seen;
846d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (unseen) {
847d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        firstUnseenMessagePos = messageCursor.getPosition();
848d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (!multipleUnseenThread
849d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                && !fromAddress.contentEquals(message.getFrom())) {
850d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            multipleUnseenThread = true;
851d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
852d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
853d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
854d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
855c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy                // TODO(skennedy) Can we remove this check?
856d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (Utils.isRunningJellybeanOrLater()) {
857d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For a new-style notification
858d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
859d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (multipleUnseenThread) {
860d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // The title of a single conversation is the list of senders.
861d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        int sendersLength = res.getInteger(R.integer.swipe_senders_length);
862d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
863d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final SpannableStringBuilder sendersBuilder = getStyledSenders(
864d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                context, conversationCursor, sendersLength, notificationAccount);
865d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
866d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification.setContentTitle(sendersBuilder);
867d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // For a single new conversation, the ticker is based on the sender's name.
868d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationTicker = sendersBuilder.toString();
869d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } else {
870d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // The title of a single message the sender.
871d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification.setContentTitle(from);
872d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // For a single new conversation, the ticker is based on the sender's name.
873d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationTicker = from;
874d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
875d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
876d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification content will be the subject of the conversation.
877d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentText(
878d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            getSingleMessageLittleText(context, conversation.subject));
879d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
880d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification subtext will be the subject of the conversation for inbox
881d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications, or will based on the the label name for user label
882d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications.
883d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSubText(isInbox ? notificationAccount : notificationLabelName);
884d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
885d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (multipleUnseenThread) {
88661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                        notification.setLargeIcon(
88761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                                getDefaultNotificationIcon(context, folder, true));
888d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
889d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final NotificationCompat.BigTextStyle bigText =
890d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            new NotificationCompat.BigTextStyle(notification);
891d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
892d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Seek the message cursor to the first unread message
89392b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    final Message message;
89492b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    if (messageCursor.moveToPosition(firstUnseenMessagePos)) {
89592b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        message = messageCursor.getMessage();
89692b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        bigText.bigText(getSingleMessageBigText(context,
89792b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                conversation.subject, message));
89892b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    } else {
89913a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                        LogUtils.e(LOG_TAG, "NotificationUtils: Failed to load message");
90092b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        message = null;
90192b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
902d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
90392b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    if (message != null) {
90492b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        final Set<String> notificationActions =
905cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                                folderPreferences.getNotificationActions(account);
906d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
90792b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        final int notificationId = getNotificationId(notificationAccount, folder);
908d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
90992b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        NotificationActionUtils.addNotificationActions(context, notificationIntent,
91092b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                notification, account, conversation, message, folder,
91192b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                notificationId, when, notificationActions);
91292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
913d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
914d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For an old-style notification
915d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
916d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The title of a single conversation notification is built from both the sender
917d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // and subject of the new message.
918d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentTitle(getSingleMessageNotificationTitle(context,
919d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            from, conversation.subject));
920d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
921d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification content will be the subject of the conversation for inbox
922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications, or will based on the the label name for user label
923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications.
924d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentText(
925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            isInbox ? notificationAccount : notificationLabelName);
926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
927d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For a single new conversation, the ticker is based on the sender's name.
928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTicker = from;
929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } finally {
931d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (messageCursor != null) {
932d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    messageCursor.close();
933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
934d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
935d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    cursor.close();
936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
937d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
938d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
939d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
940d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Build the notification ticker
941d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationLabelName != null && notificationTicker != null) {
942d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // This is a per label notification, format the ticker with that information
943d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = res.getString(R.string.label_notification_ticker,
944d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationLabelName, notificationTicker);
945d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
946d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
947d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationTicker != null) {
948d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we didn't generate a notification ticker, it will default to account name
949d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setTicker(notificationTicker);
950d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
951d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
952d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Set the number in the notification
953d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount > 1) {
954d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setNumber(unreadCount);
955d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
956d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
957d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notification.setContentIntent(clickIntent);
958d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
959d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
960d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder getStyledSenders(final Context context,
961d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Cursor conversationCursor, final int maxLength, final String account) {
962d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Conversation conversation = new Conversation(conversationCursor);
963d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final com.android.mail.providers.ConversationInfo conversationInfo =
964d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                conversation.conversationInfo;
965d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ArrayList<SpannableString> senders = new ArrayList<SpannableString>();
966d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sNotificationUnreadStyleSpan == null) {
967d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationUnreadStyleSpan = new TextAppearanceSpan(
968d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, R.style.NotificationSendersUnreadTextAppearance);
969d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationReadStyleSpan =
970d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSendersReadTextAppearance);
971d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
972d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SendersView.format(context, conversationInfo, "", maxLength, senders, null, null, account,
973d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sNotificationUnreadStyleSpan, sNotificationReadStyleSpan, false);
974d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
975d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return ellipsizeStyledSenders(context, senders);
976d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
977d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
978d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sSendersSplitToken = null;
979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sElidedPaddingToken = null;
980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
981d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder ellipsizeStyledSenders(final Context context,
982d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            ArrayList<SpannableString> styledSenders) {
983d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sSendersSplitToken == null) {
984d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sSendersSplitToken = context.getString(R.string.senders_split_token);
985d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sElidedPaddingToken = context.getString(R.string.elided_padding_token);
986d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
987d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
988d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder builder = new SpannableStringBuilder();
989d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString prevSender = null;
990d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (SpannableString sender : styledSenders) {
991d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (sender == null) {
99213a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                LogUtils.e(LOG_TAG, "NotificationUtils: null sender iterating over styledSenders");
993d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                continue;
994d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
995d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
996d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (SendersView.sElidedString.equals(sender.toString())) {
997d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
998d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
999d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (builder.length() > 0
1000d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    && (prevSender == null || !SendersView.sElidedString.equals(prevSender
1001d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            .toString()))) {
1002d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1003d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sSendersSplitToken + sender);
1004d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1005d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1006d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1007d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(sender);
1008d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1009d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return builder;
1010d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1011d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1012d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableString copyStyles(CharacterStyle[] spans, CharSequence newText) {
1013d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString s = new SpannableString(newText);
1014d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (spans != null && spans.length > 0) {
1015d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            s.setSpan(spans[0], 0, s.length(), 0);
1016d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1017d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return s;
1018d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1019d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1020d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1021d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Seeks the cursor to the position of the most recent unread conversation. If no unread
1022d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversation is found, the position of the cursor will be restored, and false will be
1023d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * returned.
1024d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1025d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static boolean seekToLatestUnreadConversation(final Cursor cursor) {
1026d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int initialPosition = cursor.getPosition();
1027d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        do {
1028d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
1029d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!conversation.read) {
1030d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return true;
1031d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1032d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } while (cursor.moveToNext());
1033d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1034d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Didn't find an unread conversation, reset the position.
1035d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        cursor.moveToPosition(initialPosition);
1036d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return false;
1037d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1038d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1039d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1040d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1041d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1042d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1043d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senders Sender of the new message that triggered the notification.
1044d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1045d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param snippet Snippet of the new message that triggered the notification
1046d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1047d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1048d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1049d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageInboxLine(Context context,
1050d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String senders, String subject, String snippet) {
1051d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // TODO(cwren) finish this step toward commmon code with getSingleMessageBigText
1052d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1053d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String subjectSnippet = !TextUtils.isEmpty(subject) ? subject : snippet;
1054d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1055d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationPrimarySpan =
1056d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
1057d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1058d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(senders)) {
1059d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the senders are empty, just use the subject/snippet.
1060d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return subjectSnippet;
1061d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(subjectSnippet)) {
1062d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject/snippet is empty, just use the senders.
1063d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(senders);
1064d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan, 0, senders.length(), 0);
1065d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1066d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1067d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1068d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String formatString = context.getResources().getString(
1069d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.multiple_new_message_notification_item);
1070d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSecondarySpan =
1071d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1072d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1073d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String instantiatedString = String.format(formatString, senders, subjectSnippet);
1074d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1075d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(instantiatedString);
1076d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1077d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isOrderReversed = formatString.indexOf("%2$s") <
1078d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    formatString.indexOf("%1$s");
1079d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int primaryOffset =
1080d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(senders) :
1081d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(senders));
1082d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int secondaryOffset =
1083d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(subjectSnippet) :
1084d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(subjectSnippet));
1085d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan,
1086d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    primaryOffset, primaryOffset + senders.length(), 0);
1087d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSecondarySpan,
1088d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    secondaryOffset, secondaryOffset + subjectSnippet.length(), 0);
1089d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1090d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1091d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1092d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1093d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1094d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1095d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1096d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1097d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in {@link Notification.ContentText}
1098d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1099d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageLittleText(Context context, String subject) {
1100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final SpannableString spannableString = new SpannableString(subject);
1104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return spannableString;
1107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param message the {@link Message} to be displayed.
1115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageBigText(Context context, String subject,
1119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Message message) {
1120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String snippet = getMessageBodyWithoutElidedText(message);
1125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Change multiple newlines (with potential white space between), into a single new line
1127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String collapsedSnippet =
1128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";
1129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just use the snippet.
1132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return snippet;
1133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(collapsedSnippet)) {
1134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the snippet is empty, just use the subject.
1135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(subject);
1136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationBigTextFormat = context.getResources().getString(
1141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_big_text);
1142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") >
1146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationBigTextFormat.indexOf("%1$s");
1147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String bigText =
1148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    String.format(notificationBigTextFormat, subject, collapsedSnippet);
1149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(bigText);
1150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isSubjectFirst ? bigText.indexOf(subject) : bigText.lastIndexOf(subject));
1153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan,
1154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the title for a notification for a single new conversation
1162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sender Sender of the new message that triggered the notification.
1164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use as a {@link Notification} title.
1166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageNotificationTitle(Context context,
1168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String sender, String subject) {
1169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just set the title to the sender's information.
1172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return sender;
1173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationTitleFormat = context.getResources().getString(
1175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_title);
1176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectLast = notificationTitleFormat.indexOf("%2$s") >
1180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTitleFormat.indexOf("%1$s");
1181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String titleString = String.format(notificationTitleFormat, sender, subject);
1182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Format the string so the subject is using the secondaryText style
1184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString titleSpannable = new SpannableString(titleString);
1185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Find the offset of the subject.
1187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    isSubjectLast ? titleString.lastIndexOf(subject) : titleString.indexOf(subject);
1189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSubjectSpan =
1190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            titleSpannable.setSpan(notificationSubjectSpan,
1192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return titleSpannable;
1194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
11987f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears the notifications for the specified account/folder.
1199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
12007f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    public static void clearFolderNotification(Context context, Account account, Folder folder,
12017f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final boolean markSeen) {
120213a732747e1cf835b04211fad09b722accf877d5Scott Kennedy        LogUtils.v(LOG_TAG, "NotificationUtils: Clearing all notifications for %s/%s", account.name,
120313a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                folder.name);
1204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
1205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
1206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.remove(key);
1207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
1208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
12097f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationManager notificationManager =
12107f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
12117f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        notificationManager.cancel(getNotificationId(account.name, folder));
12127f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12137f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        if (markSeen) {
12147f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            markSeen(context, folder);
12157f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
12167f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    }
12177f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12187f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    /**
12197f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears all notifications for the specified account.
12207f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     */
12217f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    public static void clearAccountNotifications(final Context context, final String account) {
12227f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        LogUtils.v(LOG_TAG, "NotificationUtils: Clearing all notifications for %s", account);
12237f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
12247f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12257f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        // Find all NotificationKeys for this account
12267f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final ImmutableList.Builder<NotificationKey> keyBuilder = ImmutableList.builder();
12277f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12287f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey key : notificationMap.keySet()) {
12297f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            if (account.equals(key.account.name)) {
12307f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                keyBuilder.add(key);
12317f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            }
12327f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
12337f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12347f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final List<NotificationKey> notificationKeys = keyBuilder.build();
12357f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12367f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationManager notificationManager =
12377f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
12387f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12397f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey notificationKey : notificationKeys) {
12407f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final Folder folder = notificationKey.folder;
12417f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationManager.cancel(getNotificationId(account, folder));
12427f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationMap.remove(notificationKey);
12437f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
12447f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
12457f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        notificationMap.saveNotificationMap(context);
1246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static ArrayList<Long> findContacts(Context context, Collection<String> addresses) {
1249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<String> whereArgs = new ArrayList<String>();
1250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder whereBuilder = new StringBuilder();
1251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] questionMarks = new String[addresses.size()];
1252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereArgs.addAll(addresses);
1254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Arrays.fill(questionMarks, "?");
1255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereBuilder.append(Email.DATA1 + " IN (").
1256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(TextUtils.join(",", questionMarks)).
1257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(")");
1258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ContentResolver resolver = context.getContentResolver();
1260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor c = resolver.query(Email.CONTENT_URI,
1261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new String[]{Email.CONTACT_ID}, whereBuilder.toString(),
1262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                whereArgs.toArray(new String[0]), null);
1263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = new ArrayList<Long>();
1265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (c == null) {
1266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return contactIds;
1267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
1269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (c.moveToNext()) {
1270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                contactIds.add(c.getLong(0));
1271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
1273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            c.close();
1274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return contactIds;
1276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
127861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getContactIcon(
127961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            Context context, String senderAddress, final Folder folder) {
1280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (senderAddress == null) {
1281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
1282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Bitmap icon = null;
1284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = findContacts(
1285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, Arrays.asList(new String[] { senderAddress }));
1286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (contactIds != null) {
1288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the ideal size for this icon.
1289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Resources res = context.getResources();
1290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int idealIconHeight =
1291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
1292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int idealIconWidth =
1293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
1294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (long id : contactIds) {
1295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri contactUri =
1296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
1297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri photoUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
1298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Cursor cursor = context.getContentResolver().query(
1299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        photoUri, new String[] { Photo.PHOTO }, null, null, null);
1300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
1302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    try {
1303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (cursor.moveToFirst()) {
1304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            byte[] data = cursor.getBlob(0);
1305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (data != null) {
1306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                icon = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
1307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null && icon.getHeight() < idealIconHeight) {
1308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    // We should scale this image to fit the intended size
1309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    icon = Bitmap.createScaledBitmap(
1310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                            icon, idealIconWidth, idealIconHeight, true);
1311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null) {
1313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
1314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } finally {
1318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        cursor.close();
1319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (icon == null) {
1324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // icon should be the default gmail icon.
132561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getDefaultNotificationIcon(context, folder, false /* single new message */);
1326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
1328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getMessageBodyWithoutElidedText(final Message message) {
13315e7e88b9f5ac1f239753e13ffb3ee4b249722c9aScott Kennedy        return getMessageBodyWithoutElidedText(message.getBodyAsHtml());
1332d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1333d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static String getMessageBodyWithoutElidedText(String html) {
1335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(html)) {
1336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return "";
1337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Get the html "tree" for this message body
1339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final HtmlTree htmlTree = com.android.mail.utils.Utils.getHtmlTree(html);
1340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        htmlTree.setPlainTextConverterFactory(MESSAGE_CONVERTER_FACTORY);
1341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return htmlTree.getPlainText();
1343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void markSeen(final Context context, final Folder folder) {
1346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Uri uri = folder.uri;
1347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ContentValues values = new ContentValues(1);
1349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        values.put(UIProvider.ConversationColumns.SEEN, 1);
1350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        context.getContentResolver().update(uri, values, null, null);
1352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns a displayable string representing
1356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the message sender. It has a preference toward showing the name,
1357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * but will fall back to the address if that is all that is available.
1358d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1359d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getDisplayableSender(String sender) {
1360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String displayableSender = address.getName();
1363390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1364390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        if (!TextUtils.isEmpty(displayableSender)) {
1365390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy            return Address.decodeAddressName(displayableSender);
1366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1367390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1368390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        // If that fails, default to the sender address.
1369390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        displayableSender = address.getAddress();
1370390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = sender;
1375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return displayableSender;
1377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns only the address portion of a message sender.
1381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getSenderAddress(String sender) {
1383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String tokenizedAddress = address.getAddress();
1386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(tokenizedAddress)) {
1390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            tokenizedAddress = sender;
1391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return tokenizedAddress;
1393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static int getNotificationId(final String account, final Folder folder) {
1396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return 1 ^ account.hashCode() ^ folder.hashCode();
1397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationKey {
1400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Account account;
1401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Folder folder;
1402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public NotificationKey(Account account, Folder folder) {
1404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.account = account;
1405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.folder = folder;
1406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public boolean equals(Object other) {
1410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!(other instanceof NotificationKey)) {
1411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return false;
1412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key = (NotificationKey) other;
1414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return account.equals(key.account) && folder.equals(key.folder);
1415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public String toString() {
1419e8a7c4c94cc2e391c6d2b5ade74ca842bc6b3a84Scott Kennedy            return account.name + " " + folder.name;
1420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public int hashCode() {
1424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int accountHashCode = account.hashCode();
1425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int folderHashCode = folder.hashCode();
1426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return accountHashCode ^ folderHashCode;
1427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Contains the logic for converting the contents of one HtmlTree into
1432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * plaintext.
1433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static class MailMessagePlainTextConverter extends HtmlTree.DefaultPlainTextConverter {
1435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings for parsing html message bodies
1436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_NAME = "div";
1437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME = "class";
1438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE = "elided-text";
1439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HTML.Attribute ELIDED_TEXT_ATTRIBUTE =
1441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new HTML.Attribute(ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME, HTML.Attribute.NO_TYPE);
1442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HtmlDocument.Node ELIDED_TEXT_REPLACEMENT_NODE =
1444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                HtmlDocument.createSelfTerminatingTag(HTML4.BR_ELEMENT, null, null, null);
1445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private int mEndNodeElidedTextBlock = -1;
1447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void addNode(HtmlDocument.Node n, int nodeNum, int endNum) {
1450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we are in the middle of an elided text block, don't add this node
1451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (nodeNum < mEndNodeElidedTextBlock) {
1452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (nodeNum == mEndNodeElidedTextBlock) {
1454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                super.addNode(ELIDED_TEXT_REPLACEMENT_NODE, nodeNum, endNum);
1455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this tag starts another elided text block, we want to remember the end
1459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (n instanceof HtmlDocument.Tag) {
1460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                boolean foundElidedTextTag = false;
1461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HtmlDocument.Tag htmlTag = (HtmlDocument.Tag)n;
1462d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HTML.Element htmlElement = htmlTag.getElement();
1463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (ELIDED_TEXT_ELEMENT_NAME.equals(htmlElement.getName())) {
1464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Make sure that the class is what is expected
1465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final List<HtmlDocument.TagAttribute> attributes =
1466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            htmlTag.getAttributes(ELIDED_TEXT_ATTRIBUTE);
1467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    for (HtmlDocument.TagAttribute attribute : attributes) {
1468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE.equals(
1469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                attribute.getValue())) {
1470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Found an "elided-text" div.  Remember information about this tag
1471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            mEndNodeElidedTextBlock = endNum;
1472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            foundElidedTextTag = true;
1473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            break;
1474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (foundElidedTextTag) {
1479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return;
1480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1483c56b233013cf107c702ef9f61305282670ad804aScott Kennedy            super.addNode(n, nodeNum, endNum);
1484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * During account setup in Email, we may not have an inbox yet, so the notification setting had
1489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * to be stored in {@link AccountPreferences}. If it is still there, we need to move it to the
1490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link FolderPreferences} now.
1491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void moveNotificationSetting(final AccountPreferences accountPreferences,
1493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences) {
1494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (accountPreferences.isDefaultInboxNotificationsEnabledSet()) {
1495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this setting has been changed some other way, don't overwrite it
1496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.isNotificationsEnabledSet()) {
1497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean notificationsEnabled =
1498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        accountPreferences.getDefaultInboxNotificationsEnabled();
1499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                folderPreferences.setNotificationsEnabled(notificationsEnabled);
1501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            accountPreferences.clearDefaultInboxNotificationsEnabled();
1504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
1507