NotificationUtils.java revision cde6eb0eb380f6212b7091d9381b3fec5006f6a8
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.Html;
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.Spannable;
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableString;
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableStringBuilder;
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.Spanned;
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils;
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils.SimpleStringSplitter;
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.CharacterStyle;
43d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.TextAppearanceSpan;
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.util.Pair;
4561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedyimport android.util.SparseArray;
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.EmailAddress;
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.R;
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.MessageCursor;
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.SendersView;
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.AccountPreferences;
53d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.FolderPreferences;
54d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.MailPrefs;
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Account;
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Conversation;
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Folder;
58d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Message;
59d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.UIProvider;
60d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationAction;
6109400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HTML;
6209400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HTML4;
6309400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HtmlDocument;
6409400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.android.common.html.parser.HtmlTree;
6509400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Lists;
6609400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Maps;
6709400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Sets;
68d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
69d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.io.ByteArrayInputStream;
70d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.ArrayList;
71d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Arrays;
72d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Collection;
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.List;
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Map;
75d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.concurrent.ConcurrentHashMap;
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class NotificationUtils {
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static final String LOG_TAG = LogTag.getLogTag();
80d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
81d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** Contains a list of <(account, label), unread conversations> */
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static NotificationMap sActiveNotificationMap = null;
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
8461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static final SparseArray<Bitmap> sNotificationIcons = new SparseArray<Bitmap>();
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static TextAppearanceSpan sNotificationUnreadStyleSpan;
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharacterStyle sNotificationReadStyleSpan;
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final Map<Integer, Integer> sPriorityToLength = Maps.newHashMap();
90d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final SimpleStringSplitter SENDER_LIST_SPLITTER =
91d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            new SimpleStringSplitter(Utils.SENDER_LIST_SEPARATOR);
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String[] sSenderFragments = new String[8];
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** A factory that produces a plain text converter that removes elided text. */
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final HtmlTree.PlainTextConverterFactory MESSAGE_CONVERTER_FACTORY =
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            new HtmlTree.PlainTextConverterFactory() {
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                @Override
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                public HtmlTree.PlainTextConverter createInstance() {
99d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return new MailMessagePlainTextConverter();
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            };
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears all notifications in response to the user tapping "Clear" in the status bar.
105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearAllNotfications(Context context) {
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications.");
108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.clear();
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns the notification map, creating it if necessary.
115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static synchronized NotificationMap getNotificationMap(Context context) {
117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sActiveNotificationMap == null) {
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap = new NotificationMap();
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // populate the map from the cached data
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap.loadNotificationMap(context);
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return sActiveNotificationMap;
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Class representing the existing notifications, and the number of unread and
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * unseen conversations that triggered each.
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationMap
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            extends ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>> {
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String NOTIFICATION_PART_SEPARATOR = " ";
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final int NUM_NOTIFICATION_PARTS= 4;
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Retuns the unread count for the given NotificationKey.
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnread(NotificationKey key) {
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value = get(key);
141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.first : null;
142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Retuns the unread unseen count for the given NotificationKey.
146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnseen(NotificationKey key) {
148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value = get(key);
149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.second : null;
150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Store the unread and unseen value for the given NotificationKey
154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void put(NotificationKey key, int unread, int unseen) {
156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value =
157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Pair<Integer, Integer>(Integer.valueOf(unread), Integer.valueOf(unseen));
158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            put(key, value);
159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Populates the notification map with previously cached data.
163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void loadNotificationMap(final Context context) {
165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = mailPrefs.getActiveNotificationSet();
167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (notificationSet != null) {
168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                for (String notificationEntry : notificationSet) {
169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Get the parts of the string that make the notification entry
170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] notificationParts =
171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            TextUtils.split(notificationEntry, NOTIFICATION_PART_SEPARATOR);
172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationParts.length == NUM_NOTIFICATION_PARTS) {
173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri accountUri = Uri.parse(notificationParts[0]);
174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor accountCursor = context.getContentResolver().query(
175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                accountUri, UIProvider.ACCOUNTS_PROJECTION, null, null, null);
176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Account account;
177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (accountCursor.moveToFirst()) {
179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                account = new Account(accountCursor);
180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            accountCursor.close();
185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri folderUri = Uri.parse(notificationParts[1]);
188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor folderCursor = context.getContentResolver().query(
189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folderUri, UIProvider.FOLDERS_PROJECTION, null, null, null);
190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Folder folder;
191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (folderCursor.moveToFirst()) {
193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folder = new Folder(folderCursor);
194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            folderCursor.close();
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final NotificationKey key = new NotificationKey(account, folder);
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unreadValue = Integer.valueOf(notificationParts[2]);
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unseenValue = Integer.valueOf(notificationParts[3]);
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Pair<Integer, Integer> unreadUnseenValue =
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                new Pair<Integer, Integer>(unreadValue, unseenValue);
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        put(key, unreadUnseenValue);
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Cache the notification map.
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void saveNotificationMap(Context context) {
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = Sets.newHashSet();
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<NotificationKey> keys = keySet();
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey key : keys) {
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Pair<Integer, Integer> value = get(key);
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unreadCount = value.first;
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unseenCount = value.second;
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (value != null && unreadCount != null && unseenCount != null) {
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] partValues = new String[] {
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            key.account.uri.toString(), key.folder.uri.toString(),
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            unreadCount.toString(), unseenCount.toString()};
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues));
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            mailPrefs.cacheActiveNotificationSet(notificationSet);
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return the title of this notification with each account and the number of unread and unseen
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversations for it. Also remove any account in the map that has 0 unread.
237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String createNotificationString(NotificationMap notifications) {
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder result = new StringBuilder();
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int i = 0;
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keysToRemove = Sets.newHashSet();
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : notifications.keySet()) {
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unread = notifications.getUnread(key);
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unseen = notifications.getUnseen(key);
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unread == null || unread.intValue() == 0) {
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                keysToRemove.add(key);
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (i > 0) result.append(", ");
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                result.append(key.toString() + " (" + unread + ", " + unseen + ")");
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : keysToRemove) {
255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notifications.remove(key);
256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return result.toString();
259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts and cancel them.
263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAllNotifications(Context context) {
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        NotificationManager nm = (NotificationManager) context.getSystemService(
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                Context.NOTIFICATION_SERVICE);
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        nm.cancelAll();
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        clearAllNotfications(context);
269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, cancel them, and repost.
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAndResendNotifications(Context context) {
276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        resendNotifications(context, true);
277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, optionally cancel them, and repost.
281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void resendNotifications(Context context, final boolean cancelExisting) {
284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cancelExisting) {
285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm =
286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            nm.cancelAll();
288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Re-validate the notifications.
290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<NotificationKey> keys = notificationMap.keySet();
292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey notification : keys) {
293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder = notification.folder;
294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int notificationId = getNotificationId(notification.account.name, folder);
295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationAction undoableAction =
297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sUndoNotifications.get(notificationId);
298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (undoableAction == null) {
299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                validateNotifications(context, folder, notification.account, true, false,
300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification);
301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Create an undo notification
303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.createUndoNotification(context, undoableAction);
304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications for the specified account.
310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void validateAccountNotifications(Context context, String account) {
312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        List<NotificationKey> notificationsToCancel = Lists.newArrayList();
313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Iterate through the notification map to see if there are any entries that correspond to
314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // labels that are not in the sync set.
315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keys = notificationMap.keySet();
317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final AccountPreferences accountPreferences = new AccountPreferences(context, account);
318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean enabled = accountPreferences.areNotificationsEnabled();
319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (!enabled) {
320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Cancel all notifications for this account
321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (notification.account.name.equals(account)) {
323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationsToCancel.add(notification);
324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Iterate through the notification map to see if there are any entries that
328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // correspond to labels that are not in the notification set.
329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (notification.account.name.equals(account)) {
331d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // If notification is not enabled for this label, remember this NotificationKey
332d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // to later cancel the notification, and remove the entry from the map
333d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Folder folder = notification.folder;
334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final boolean isInbox =
335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            notification.account.settings.defaultInbox.equals(folder.uri);
336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final FolderPreferences folderPreferences = new FolderPreferences(
337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            context, notification.account.name, folder, isInbox);
338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!folderPreferences.areNotificationsEnabled()) {
340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationsToCancel.add(notification);
341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Cancel & remove the invalid notifications.
347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationsToCancel.size() > 0) {
348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm = (NotificationManager) context.getSystemService(
349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Context.NOTIFICATION_SERVICE);
350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : notificationsToCancel) {
351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Folder folder = notification.folder;
352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int notificationId = getNotificationId(notification.account.name, folder);
353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notificationMap.remove(notification);
355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sUndoNotifications.remove(notificationId);
356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
358d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.saveNotificationMap(context);
359d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Display only one notification.
364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void setNewEmailIndicator(Context context, final int unreadCount,
366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int unseenCount, final Account account, final Folder folder,
367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean getAttention) {
368d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean ignoreUnobtrusiveSetting = false;
369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
370dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy        final int notificationId = getNotificationId(account.name, folder);
371dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy
372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Update the notification map
373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount == 0) {
376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.remove(key);
377dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy            ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
378dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy                    .cancel(notificationId);
379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!notificationMap.containsKey(key)) {
381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This account previously didn't have any unread mail; ignore the "unobtrusive
382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notifications" setting and play sound and/or vibrate the device even if a
383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notification already exists (bug 2412348).
384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                ignoreUnobtrusiveSetting = true;
385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.put(key, unreadCount, unseenCount);
387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            LogUtils.v(LOG_TAG, "New email: %s mapSize: %d getAttention: %b",
392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    createNotificationString(notificationMap), notificationMap.size(),
393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    getAttention);
394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (NotificationActionUtils.sUndoNotifications.get(notificationId) == null) {
397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            validateNotifications(context, folder, account, getAttention, ignoreUnobtrusiveSetting,
398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    key);
399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications notification.
404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void validateNotifications(Context context, final Folder folder,
406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, boolean getAttention, boolean ignoreUnobtrusiveSetting,
407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key) {
408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        NotificationManager nm = (NotificationManager)
410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context.getSystemService(Context.NOTIFICATION_SERVICE);
411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            LogUtils.v(LOG_TAG, "Validating Notification: %s mapSize: %d folder: %s "
415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    + "getAttention: %b", createNotificationString(notificationMap),
416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationMap.size(), folder.name, getAttention);
417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // The number of unread messages for this account and label.
419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unread = notificationMap.getUnread(key);
420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int unreadCount = unread != null ? unread.intValue() : 0;
421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unseen = notificationMap.getUnseen(key);
422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int unseenCount = unseen != null ? unseen.intValue() : 0;
423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor cursor = null;
425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Uri.Builder uriBuilder = folder.conversationListUri.buildUpon();
428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            uriBuilder.appendQueryParameter(
429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.SEEN_QUERY_PARAMETER, Boolean.FALSE.toString());
430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor = context.getContentResolver().query(uriBuilder.build(),
431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.CONVERSATION_PROJECTION, null, null, null);
432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int cursorUnseenCount = cursor.getCount();
433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Make sure the unseen count matches the number of items in the cursor.  But, we don't
435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // want to overwrite a 0 unseen count that was specified in the intent
436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount != 0 && unseenCount != cursorUnseenCount) {
437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                LogUtils.d(LOG_TAG,
438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        "Unseen count doesn't match cursor count.  unseen: %d cursor count: %d",
439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        unseenCount, cursorUnseenCount);
440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = cursorUnseenCount;
441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For the purpose of the notifications, the unseen count should be capped at the num of
444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // unread conversations.
445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount > unreadCount) {
446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = unreadCount;
447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int notificationId = getNotificationId(account.name, folder);
450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount == 0) {
452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // We now have all we need to create the notification and the pending intent
457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            PendingIntent clickIntent;
458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setSmallIcon(R.drawable.stat_notify_email);
461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setTicker(account.name);
462d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long when;
464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long oldWhen =
466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sNotificationTimestamps.get(notificationId);
467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (oldWhen != 0) {
468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = oldWhen;
469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = System.currentTimeMillis();
471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setWhen(when);
474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The timestamp is now stored in the notification, so we can remove it from here
476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Dispatch a CLEAR_NEW_MAIL_NOTIFICATIONS intent if the user taps the "X" next to a
479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // notification.  Also this intent gets fired when the user taps on a notification as
480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the AutoCancel flag has been set
481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Intent cancelNotificationIntent =
482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.setPackage(context.getPackageName());
48409400efa442422299acf21abe20e3470f9d965abScott Kennedy            cancelNotificationIntent.setData(Utils.appendVersionQueryParameter(context,
48509400efa442422299acf21abe20e3470f9d965abScott Kennedy                    folder.uri));
486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.putExtra(MailIntentService.ACCOUNT_EXTRA, account);
487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.putExtra(MailIntentService.FOLDER_EXTRA, folder);
488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setDeleteIntent(PendingIntent.getService(
490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, notificationId, cancelNotificationIntent, 0));
491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Ensure that the notification is cleared when the user selects it
493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setAutoCancel(true);
494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean eventInfoConfigured = false;
496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isInbox = account.settings.defaultInbox.equals(folder.uri);
498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences =
499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new FolderPreferences(context, account.name, folder, isInbox);
500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (isInbox) {
502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new AccountPreferences(context, account.name);
504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                moveNotificationSetting(accountPreferences, folderPreferences);
505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
507d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.areNotificationsEnabled()) {
508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Don't notify
509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
512d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unreadCount > 0) {
513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // How can I order this properly?
514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor.moveToNext()) {
51509400efa442422299acf21abe20e3470f9d965abScott Kennedy                    Intent notificationIntent = createViewConversationIntent(context, account,
51609400efa442422299acf21abe20e3470f9d965abScott Kennedy                            folder, null);
517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Launch directly to the conversation, if the
519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // number of unseen conversations == 1
520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (unseenCount == 1) {
52109400efa442422299acf21abe20e3470f9d965abScott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
52209400efa442422299acf21abe20e3470f9d965abScott Kennedy                                cursor);
523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationIntent == null) {
526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        LogUtils.e(LOG_TAG, "Null intent when building notification");
527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return;
528d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
529d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    clickIntent = PendingIntent.getActivity(context, -1, notificationIntent, 0);
531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    configureLatestEventInfoFromConversation(context, account, folderPreferences,
532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            notification, cursor, clickIntent, notificationIntent,
533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            account.name, unreadCount, unseenCount, folder, when);
534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    eventInfoConfigured = true;
535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
538d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean notifyOnce = !folderPreferences.isEveryMessageNotificationEnabled();
541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!ignoreUnobtrusiveSetting && account != null && notifyOnce) {
543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // If the user has "unobtrusive notifications" enabled, only alert the first time
544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // new mail is received in this account.  This is the default behavior.  See
545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // bugs 2412348 and 2413490.
546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setOnlyAlertOnce(true);
547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (account != null) {
550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                LogUtils.d(LOG_TAG, "Account: %s vibrate: %s", account.name,
551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Boolean.toString(folderPreferences.isNotificationVibrateEnabled()));
552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int defaults = 0;
555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            /*
557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * We do not want to notify if this is coming back from an Undo notification, hence the
558d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * oldWhen check.
559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             */
560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (getAttention && account != null && oldWhen == 0) {
561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new AccountPreferences(context, account.name);
563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (accountPreferences.areNotificationsEnabled()) {
564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (vibrate) {
565d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        defaults |= Notification.DEFAULT_VIBRATE;
566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            : Uri.parse(ringtoneUri));
570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    LogUtils.d(LOG_TAG, "New email in %s vibrateWhen: %s, playing notification: %s",
571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            account.name, vibrate, ringtoneUri);
572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (eventInfoConfigured) {
576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                defaults |= Notification.DEFAULT_LIGHTS;
577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setDefaults(defaults);
578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (oldWhen != 0) {
580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // We do not want to display the ticker again if we are re-displaying this
581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notification (like from an Undo notification)
582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setTicker(null);
583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.notify(notificationId, notification.build());
586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (cursor != null) {
589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                cursor.close();
590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return an {@link Intent} which, if launched, will display the corresponding conversation
596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
59709400efa442422299acf21abe20e3470f9d965abScott Kennedy    private static Intent createViewConversationIntent(final Context context, final Account account,
59809400efa442422299acf21abe20e3470f9d965abScott Kennedy            final Folder folder, final Cursor cursor) {
599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (folder == null || account == null) {
600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            LogUtils.e(LOG_TAG, "Null account or folder.  account: %s folder: %s",
601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    account, folder);
602d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
604d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Intent intent;
606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cursor == null) {
608b39aaf53a555c1046ef31b3fecf15d086acca013Scott Kennedy            intent = Utils.createViewFolderIntent(context, folder.uri, account);
609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // A conversation cursor has been specified, so this intent is intended to be go
611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // directly to the one new conversation
612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
613d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the Conversation object
614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
615b39aaf53a555c1046ef31b3fecf15d086acca013Scott Kennedy            intent = Utils.createViewConversationIntent(context, conversation, folder.uri,
616b39aaf53a555c1046ef31b3fecf15d086acca013Scott Kennedy                    account);
617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return intent;
620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
62261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getDefaultNotificationIcon(
62361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            final Context context, final Folder folder, final boolean multipleNew) {
624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Bitmap icon;
62561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (folder.notificationIconResId != 0) {
62661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getIcon(context, folder.notificationIconResId);
62761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        } else if (multipleNew) {
62861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getIcon(context, R.drawable.ic_notification_multiple_mail_holo_dark);
629d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
63061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getIcon(context, R.drawable.ic_contact_picture);
631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
63561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getIcon(final Context context, final int resId) {
63661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap cachedIcon = sNotificationIcons.get(resId);
63761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (cachedIcon != null) {
63861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            return cachedIcon;
63961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        }
64061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
64161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resId);
64261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        sNotificationIcons.put(resId, icon);
64361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
64461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        return icon;
64561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    }
64661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void configureLatestEventInfoFromConversation(final Context context,
648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, final FolderPreferences folderPreferences,
649d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationCompat.Builder notification, final Cursor conversationCursor,
650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final PendingIntent clickIntent, final Intent notificationIntent,
651d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationAccount, final int unreadCount, final int unseenCount,
652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder, final long when) {
653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Resources res = context.getResources();
654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        LogUtils.w(LOG_TAG, "Showing notification with unreadCount of %d and "
656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                + "unseenCount of %d", unreadCount, unseenCount);
657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String notificationTicker = null;
659d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Boolean indicating that this notification is for a non-inbox label.
661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean isInbox = account.settings.defaultInbox.equals(folder.uri);
662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Notification label name for user label notifications.
664d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String notificationLabelName = isInbox ? null : folder.name;
665d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unseenCount > 1) {
667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Build the string that describes the number of new messages
668d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String newMessagesString = res.getString(R.string.new_messages, unseenCount);
669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
670d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Use the default notification icon
671d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setLargeIcon(
67261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                    getDefaultNotificationIcon(context, folder, true /* multiple new messages */));
673d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
674d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The ticker initially start as the new messages string.
675d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = newMessagesString;
676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The title of the notification is the new messages string
678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setContentTitle(newMessagesString);
679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (com.android.mail.utils.Utils.isRunningJellybeanOrLater()) {
681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // For a new-style notification
682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int maxNumDigestItems = context.getResources().getInteger(
683d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        R.integer.max_num_notification_digest_items);
684d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setSubText(isInbox ? notificationAccount : notificationLabelName);
687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
688d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final NotificationCompat.InboxStyle digest =
689d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new NotificationCompat.InboxStyle(notification);
690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                digest.setBigContentTitle(newMessagesString);
692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int numDigestItems = 0;
694d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                do {
695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Conversation conversation = new Conversation(conversationCursor);
696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!conversation.read) {
698d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        boolean multipleUnreadThread = false;
699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // TODO(cwren) extract this pattern into a helper
700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Cursor cursor = null;
702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        MessageCursor messageCursor = null;
703d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final Uri.Builder uriBuilder = conversation.messageListUri.buildUpon();
705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            uriBuilder.appendQueryParameter(
706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.LABEL_QUERY_PARAMETER, notificationLabelName);
707d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor = context.getContentResolver().query(uriBuilder.build(),
708d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.MESSAGE_PROJECTION, null, null, null);
709d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            messageCursor = new MessageCursor(cursor);
710d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
711b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                            String from = "";
712d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            String fromAddress = "";
713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
715d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                fromAddress = message.getFrom();
716b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                if (fromAddress == null) {
717b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                    fromAddress = "";
718b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                }
719d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                from = getDisplayableSender(fromAddress);
720d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
721d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
722d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
723d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (!message.read &&
724d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        !fromAddress.contentEquals(message.getFrom())) {
725d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    multipleUnreadThread = true;
726d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
727d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
728d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
729d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final SpannableStringBuilder sendersBuilder;
730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (multipleUnreadThread) {
731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final int sendersLength =
732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        res.getInteger(R.integer.swipe_senders_length);
733d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
734d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = getStyledSenders(context, conversationCursor,
735d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        sendersLength, notificationAccount);
736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = new SpannableStringBuilder(from);
738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final CharSequence digestLine = getSingleMessageInboxLine(context,
740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    sendersBuilder.toString(),
741d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    conversation.subject,
742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    conversation.snippet);
743d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            digest.addLine(digestLine);
744d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            numDigestItems++;
745d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
746d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor != null) {
747d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                messageCursor.close();
748d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
749d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (cursor != null) {
750d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                cursor.close();
751d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
752d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
753d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
754d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } while (numDigestItems <= maxNumDigestItems && conversationCursor.moveToNext());
755d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
756d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
757d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setContentText(
758d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        isInbox ? notificationAccount : notificationLabelName);
759d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
760d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
761d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For notifications for a single new conversation, we want to get the information from
762d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the conversation
763d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
764d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Move the cursor to the most recent unread conversation
765d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            seekToLatestUnreadConversation(conversationCursor);
766d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
767d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(conversationCursor);
768d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
769d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Cursor cursor = null;
770d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            MessageCursor messageCursor = null;
771d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean multipleUnseenThread = false;
772d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String from = null;
773d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            try {
774ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                final Uri uri = conversation.messageListUri.buildUpon().appendQueryParameter(
775ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                        UIProvider.LABEL_QUERY_PARAMETER, folder.persistentId).build();
776ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                cursor = context.getContentResolver().query(uri, UIProvider.MESSAGE_PROJECTION,
777ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                        null, null, null);
778d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                messageCursor = new MessageCursor(cursor);
779d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Use the information from the last sender in the conversation that triggered
780d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // this notification.
781d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
782d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String fromAddress = "";
783d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
784d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Message message = messageCursor.getMessage();
785d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    fromAddress = message.getFrom();
786d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    from = getDisplayableSender(fromAddress);
787d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setLargeIcon(
78861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                            getContactIcon(context, getSenderAddress(fromAddress), folder));
789d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
790d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
791d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Assume that the last message in this conversation is unread
792d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int firstUnseenMessagePos = messageCursor.getPosition();
793d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
794d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Message message = messageCursor.getMessage();
795d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final boolean unseen = !message.seen;
796d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (unseen) {
797d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        firstUnseenMessagePos = messageCursor.getPosition();
798d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (!multipleUnseenThread
799d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                && !fromAddress.contentEquals(message.getFrom())) {
800d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            multipleUnseenThread = true;
801d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
802d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
803d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
804d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
805d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (Utils.isRunningJellybeanOrLater()) {
806d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For a new-style notification
807d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
808d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (multipleUnseenThread) {
809d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // The title of a single conversation is the list of senders.
810d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        int sendersLength = res.getInteger(R.integer.swipe_senders_length);
811d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
812d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final SpannableStringBuilder sendersBuilder = getStyledSenders(
813d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                context, conversationCursor, sendersLength, notificationAccount);
814d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
815d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification.setContentTitle(sendersBuilder);
816d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // For a single new conversation, the ticker is based on the sender's name.
817d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationTicker = sendersBuilder.toString();
818d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } else {
819d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // The title of a single message the sender.
820d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification.setContentTitle(from);
821d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // For a single new conversation, the ticker is based on the sender's name.
822d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationTicker = from;
823d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
824d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
825d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification content will be the subject of the conversation.
826d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentText(
827d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            getSingleMessageLittleText(context, conversation.subject));
828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification subtext will be the subject of the conversation for inbox
830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications, or will based on the the label name for user label
831d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications.
832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSubText(isInbox ? notificationAccount : notificationLabelName);
833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (multipleUnseenThread) {
83561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                        notification.setLargeIcon(
83661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                                getDefaultNotificationIcon(context, folder, true));
837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
838d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final NotificationCompat.BigTextStyle bigText =
839d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            new NotificationCompat.BigTextStyle(notification);
840d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
841d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Seek the message cursor to the first unread message
84292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    final Message message;
84392b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    if (messageCursor.moveToPosition(firstUnseenMessagePos)) {
84492b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        message = messageCursor.getMessage();
84592b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        bigText.bigText(getSingleMessageBigText(context,
84692b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                conversation.subject, message));
84792b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    } else {
84892b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        LogUtils.e(LOG_TAG, "Failed to load message");
84992b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        message = null;
85092b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
851d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
85292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    if (message != null) {
85392b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        final Set<String> notificationActions =
854cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                                folderPreferences.getNotificationActions(account);
855d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
85692b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        final int notificationId = getNotificationId(notificationAccount, folder);
857d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
85892b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        NotificationActionUtils.addNotificationActions(context, notificationIntent,
85992b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                notification, account, conversation, message, folder,
86092b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                notificationId, when, notificationActions);
86192b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
862d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
863d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For an old-style notification
864d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
865d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The title of a single conversation notification is built from both the sender
866d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // and subject of the new message.
867d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentTitle(getSingleMessageNotificationTitle(context,
868d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            from, conversation.subject));
869d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
870d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification content will be the subject of the conversation for inbox
871d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications, or will based on the the label name for user label
872d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications.
873d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentText(
874d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            isInbox ? notificationAccount : notificationLabelName);
875d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
876d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For a single new conversation, the ticker is based on the sender's name.
877d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTicker = from;
878d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
879d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } finally {
880d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (messageCursor != null) {
881d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    messageCursor.close();
882d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
883d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
884d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    cursor.close();
885d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
886d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
887d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
888d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
889d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Build the notification ticker
890d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationLabelName != null && notificationTicker != null) {
891d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // This is a per label notification, format the ticker with that information
892d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = res.getString(R.string.label_notification_ticker,
893d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationLabelName, notificationTicker);
894d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
895d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
896d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationTicker != null) {
897d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we didn't generate a notification ticker, it will default to account name
898d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setTicker(notificationTicker);
899d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
900d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
901d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Set the number in the notification
902d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount > 1) {
903d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setNumber(unreadCount);
904d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
905d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
906d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notification.setContentIntent(clickIntent);
907d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
908d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
909d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder getStyledSenders(final Context context,
910d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Cursor conversationCursor, final int maxLength, final String account) {
911d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Conversation conversation = new Conversation(conversationCursor);
912d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final com.android.mail.providers.ConversationInfo conversationInfo =
913d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                conversation.conversationInfo;
914d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ArrayList<SpannableString> senders = new ArrayList<SpannableString>();
915d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sNotificationUnreadStyleSpan == null) {
916d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationUnreadStyleSpan = new TextAppearanceSpan(
917d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, R.style.NotificationSendersUnreadTextAppearance);
918d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationReadStyleSpan =
919d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSendersReadTextAppearance);
920d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
921d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SendersView.format(context, conversationInfo, "", maxLength, senders, null, null, account,
922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sNotificationUnreadStyleSpan, sNotificationReadStyleSpan, false);
923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
924d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return ellipsizeStyledSenders(context, senders);
925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
927d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sSendersSplitToken = null;
928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sElidedPaddingToken = null;
929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder ellipsizeStyledSenders(final Context context,
931d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            ArrayList<SpannableString> styledSenders) {
932d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sSendersSplitToken == null) {
933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sSendersSplitToken = context.getString(R.string.senders_split_token);
934d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sElidedPaddingToken = context.getString(R.string.elided_padding_token);
935d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
937d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder builder = new SpannableStringBuilder();
938d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString prevSender = null;
939d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (SpannableString sender : styledSenders) {
940d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (sender == null) {
941d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                LogUtils.e(LOG_TAG, "null sender while iterating over styledSenders");
942d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                continue;
943d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
944d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
945d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (SendersView.sElidedString.equals(sender.toString())) {
946d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
947d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
948d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (builder.length() > 0
949d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    && (prevSender == null || !SendersView.sElidedString.equals(prevSender
950d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            .toString()))) {
951d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
952d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sSendersSplitToken + sender);
953d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
954d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
955d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
956d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(sender);
957d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
958d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return builder;
959d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
960d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
961d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableString copyStyles(CharacterStyle[] spans, CharSequence newText) {
962d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString s = new SpannableString(newText);
963d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (spans != null && spans.length > 0) {
964d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            s.setSpan(spans[0], 0, s.length(), 0);
965d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
966d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return s;
967d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
968d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
969d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
970d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Seeks the cursor to the position of the most recent unread conversation. If no unread
971d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversation is found, the position of the cursor will be restored, and false will be
972d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * returned.
973d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
974d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static boolean seekToLatestUnreadConversation(final Cursor cursor) {
975d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int initialPosition = cursor.getPosition();
976d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        do {
977d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
978d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!conversation.read) {
979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return true;
980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
981d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } while (cursor.moveToNext());
982d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
983d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Didn't find an unread conversation, reset the position.
984d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        cursor.moveToPosition(initialPosition);
985d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return false;
986d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
987d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
988d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
989d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
990d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
991d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
992d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senders Sender of the new message that triggered the notification.
993d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
994d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param snippet Snippet of the new message that triggered the notification
995d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
996d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
997d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
998d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageInboxLine(Context context,
999d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String senders, String subject, String snippet) {
1000d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // TODO(cwren) finish this step toward commmon code with getSingleMessageBigText
1001d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1002d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String subjectSnippet = !TextUtils.isEmpty(subject) ? subject : snippet;
1003d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1004d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationPrimarySpan =
1005d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
1006d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1007d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(senders)) {
1008d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the senders are empty, just use the subject/snippet.
1009d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return subjectSnippet;
1010d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(subjectSnippet)) {
1011d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject/snippet is empty, just use the senders.
1012d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(senders);
1013d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan, 0, senders.length(), 0);
1014d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1015d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1016d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1017d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String formatString = context.getResources().getString(
1018d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.multiple_new_message_notification_item);
1019d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSecondarySpan =
1020d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1021d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1022d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String instantiatedString = String.format(formatString, senders, subjectSnippet);
1023d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1024d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(instantiatedString);
1025d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1026d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isOrderReversed = formatString.indexOf("%2$s") <
1027d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    formatString.indexOf("%1$s");
1028d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int primaryOffset =
1029d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(senders) :
1030d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(senders));
1031d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int secondaryOffset =
1032d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(subjectSnippet) :
1033d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(subjectSnippet));
1034d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan,
1035d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    primaryOffset, primaryOffset + senders.length(), 0);
1036d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSecondarySpan,
1037d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    secondaryOffset, secondaryOffset + subjectSnippet.length(), 0);
1038d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1039d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1040d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1041d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1042d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1043d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1044d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1045d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1046d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in {@link Notification.ContentText}
1047d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1048d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageLittleText(Context context, String subject) {
1049d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1050d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1051d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1052d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final SpannableString spannableString = new SpannableString(subject);
1053d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1054d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1055d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return spannableString;
1056d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1057d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1058d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1059d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1060d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1061d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1062d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1063d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param message the {@link Message} to be displayed.
1064d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1065d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1066d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1067d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageBigText(Context context, String subject,
1068d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Message message) {
1069d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1070d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1071d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1072d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1073d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String snippet = getMessageBodyWithoutElidedText(message);
1074d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1075d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Change multiple newlines (with potential white space between), into a single new line
1076d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String collapsedSnippet =
1077d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";
1078d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1079d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1080d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just use the snippet.
1081d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return snippet;
1082d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(collapsedSnippet)) {
1083d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the snippet is empty, just use the subject.
1084d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(subject);
1085d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1086d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1087d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1088d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1089d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationBigTextFormat = context.getResources().getString(
1090d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_big_text);
1091d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1092d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1093d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1094d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") >
1095d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationBigTextFormat.indexOf("%1$s");
1096d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String bigText =
1097d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    String.format(notificationBigTextFormat, subject, collapsedSnippet);
1098d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(bigText);
1099d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isSubjectFirst ? bigText.indexOf(subject) : bigText.lastIndexOf(subject));
1102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan,
1103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the title for a notification for a single new conversation
1111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sender Sender of the new message that triggered the notification.
1113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use as a {@link Notification} title.
1115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageNotificationTitle(Context context,
1117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String sender, String subject) {
1118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just set the title to the sender's information.
1121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return sender;
1122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationTitleFormat = context.getResources().getString(
1124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_title);
1125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectLast = notificationTitleFormat.indexOf("%2$s") >
1129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTitleFormat.indexOf("%1$s");
1130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String titleString = String.format(notificationTitleFormat, sender, subject);
1131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Format the string so the subject is using the secondaryText style
1133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString titleSpannable = new SpannableString(titleString);
1134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Find the offset of the subject.
1136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    isSubjectLast ? titleString.lastIndexOf(subject) : titleString.indexOf(subject);
1138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSubjectSpan =
1139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            titleSpannable.setSpan(notificationSubjectSpan,
1141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return titleSpannable;
1143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Adds a fragment with given style to a string builder.
1148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param builder the current string builder
1150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param fragment the fragment to be added
1151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param style the style of the fragment
1152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param withSpaces whether to add the whole fragment or to divide it into
1153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *            smaller ones
1154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void addStyledFragment(SpannableStringBuilder builder, String fragment,
1156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle style, boolean withSpaces) {
1157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (withSpaces) {
1158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int pos = builder.length();
1159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(fragment);
1160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.setSpan(CharacterStyle.wrap(style), pos, builder.length(),
1161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
1162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int start = 0;
1164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (true) {
1165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int pos = fragment.substring(start).indexOf(' ');
1166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (pos == -1) {
1167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    addStyledFragment(builder, fragment.substring(start), style, true);
1168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    break;
1169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
1170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    pos += start;
1171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (start < pos) {
1172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        addStyledFragment(builder, fragment.substring(start, pos), style, true);
1173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        builder.append(' ');
1174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    start = pos + 1;
1176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (start >= fragment.length()) {
1177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        break;
1178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Uses sender instructions to build a formatted string.
1186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <p>Sender list instructions contain compact information about the sender list. Most work that
1188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * can be done without knowing how much room will be availble for the sender list is done when
1189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * creating the instructions.
1190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <p>The instructions string consists of tokens separated by SENDER_LIST_SEPARATOR. Here are
1192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the tokens, one per line:<ul>
1193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><tt>n</tt></li>
1194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><em>int</em>, the number of non-draft messages in the conversation</li>
1195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><tt>d</tt</li>
1196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><em>int</em>, the number of drafts in the conversation</li>
1197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><tt>l</tt></li>
1198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><em>literal html to be included in the output</em></li>
1199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><tt>s</tt> indicates that the message is sending (in the outbox without errors)</li>
1200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><tt>f</tt> indicates that the message failed to send (in the outbox with errors)</li>
1201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><em>for each message</em><ul>
1202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *   <li><em>int</em>, 0 for read, 1 for unread</li>
1203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *   <li><em>int</em>, the priority of the message. Zero is the most important</li>
1204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *   <li><em>text</em>, the sender text or blank for messages from 'me'</li>
1205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * </ul></li>
1206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <li><tt>e</tt> to indicate that one or more messages have been elided</li>
1207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * <p>The instructions indicate how many messages and drafts are in the conversation and then
1209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * describe the most important messages in order, indicating the priority of each message and
1210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * whether the message is unread.
1211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param instructions instructions as described above
1213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senderBuilder the SpannableStringBuilder to append to for sender information
1214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param statusBuilder the SpannableStringBuilder to append to for status
1215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param maxChars the number of characters available to display the text
1216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param unreadStyle the CharacterStyle for unread messages, or null
1217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param draftsStyle the CharacterStyle for draft messages, or null
1218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sendingString the string to use when there are messages scheduled to be sent
1219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sendFailedString the string to use when there are messages that mailed to send
1220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param meString the string to use for messages sent by this user
1221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param draftString the string to use for "Draft"
1222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param draftPluralString the string to use for "Drafts"
1223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param showNumMessages false means do not show the message count
1224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param onlyShowUnread true means the only information from unread messages should be included
1225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static synchronized void getSenderSnippet(
1227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String instructions, SpannableStringBuilder senderBuilder,
1228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            SpannableStringBuilder statusBuilder, int maxChars,
1229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle unreadStyle,
1230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle readStyle,
1231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle draftsStyle,
1232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharSequence meString, CharSequence draftString, CharSequence draftPluralString,
1233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharSequence sendingString, CharSequence sendFailedString,
1234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean forceAllUnread, boolean forceAllRead, boolean allowDraft,
1235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean showNumMessages, boolean onlyShowUnread) {
1236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assert !(forceAllUnread && forceAllRead);
1237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean unreadStatusIsForced = forceAllUnread || forceAllRead;
1238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean forcedUnreadStatus = forceAllUnread;
1239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Measure each fragment. It's ok to iterate over the entire set of fragments because it is
1241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // never a long list, even if there are many senders.
1242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Map<Integer, Integer> priorityToLength = sPriorityToLength;
1243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        priorityToLength.clear();
1244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int maxFoundPriority = Integer.MIN_VALUE;
1246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int numMessages = 0;
1247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int numDrafts = 0;
1248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        CharSequence draftsFragment = "";
1249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        CharSequence sendingFragment = "";
1250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        CharSequence sendFailedFragment = "";
1251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SENDER_LIST_SPLITTER.setString(instructions);
1253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int numFragments = 0;
1254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] fragments = sSenderFragments;
1255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int currentSize = fragments.length;
1256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        while (SENDER_LIST_SPLITTER.hasNext()) {
1257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            fragments[numFragments++] = SENDER_LIST_SPLITTER.next();
1258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (numFragments == currentSize) {
1259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sSenderFragments = new String[2 * currentSize];
1260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                System.arraycopy(fragments, 0, sSenderFragments, 0, currentSize);
1261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                currentSize *= 2;
1262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                fragments = sSenderFragments;
1263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (int i = 0; i < numFragments;) {
1267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String fragment0 = fragments[i++];
1268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if ("".equals(fragment0)) {
1269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This should be the final fragment.
1270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_ELIDED.equals(fragment0)) {
1271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // ignore
1272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_NUM_MESSAGES.equals(fragment0)) {
1273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                numMessages = Integer.valueOf(fragments[i++]);
1274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_NUM_DRAFTS.equals(fragment0)) {
1275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String numDraftsString = fragments[i++];
1276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                numDrafts = Integer.parseInt(numDraftsString);
1277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                draftsFragment = numDrafts == 1 ? draftString :
1278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        draftPluralString + " (" + numDraftsString + ")";
1279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_LITERAL.equals(fragment0)) {
1280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                senderBuilder.append(Html.fromHtml(fragments[i++]));
1281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_SENDING.equals(fragment0)) {
1283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sendingFragment = sendingString;
1284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_SEND_FAILED.equals(fragment0)) {
1285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sendFailedFragment = sendFailedString;
1286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final String unreadString = fragment0;
1288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean unread = unreadStatusIsForced
1289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        ? forcedUnreadStatus : Integer.parseInt(unreadString) != 0;
1290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String priorityString = fragments[i++];
1291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                CharSequence nameString = fragments[i++];
1292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (nameString.length() == 0) nameString = meString;
1293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int priority = Integer.parseInt(priorityString);
1294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // We want to include this entry if
1296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                //   1) The onlyShowUnread flags is not set
1297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                //   2) The above flag is set, and the message is unread
1298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (!onlyShowUnread || unread) {
1299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    priorityToLength.put(priority, nameString.length());
1300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    maxFoundPriority = Math.max(maxFoundPriority, priority);
1301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String numMessagesFragment =
1305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                (numMessages != 0 && showNumMessages) ?
1306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                " \u00A0" + Integer.toString(numMessages + numDrafts) : "";
1307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Don't allocate fixedFragment unless we need it
1309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder fixedFragment = null;
1310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int fixedFragmentLength = 0;
1311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (draftsFragment.length() != 0 && allowDraft) {
1312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (fixedFragment == null) {
1313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                fixedFragment = new SpannableStringBuilder();
1314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            fixedFragment.append(draftsFragment);
1316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (draftsStyle != null) {
1317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                fixedFragment.setSpan(
1318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        CharacterStyle.wrap(draftsStyle),
1319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        0, fixedFragment.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
1320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sendingFragment.length() != 0) {
1323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (fixedFragment == null) {
1324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                fixedFragment = new SpannableStringBuilder();
1325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (fixedFragment.length() != 0) fixedFragment.append(", ");
1327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            fixedFragment.append(sendingFragment);
1328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sendFailedFragment.length() != 0) {
1330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (fixedFragment == null) {
1331d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                fixedFragment = new SpannableStringBuilder();
1332d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1333d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (fixedFragment.length() != 0) fixedFragment.append(", ");
1334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            fixedFragment.append(sendFailedFragment);
1335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (fixedFragment != null) {
1338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            fixedFragmentLength = fixedFragment.length();
1339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        maxChars -= fixedFragmentLength;
1341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int maxPriorityToInclude = -1; // inclusive
1343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int numCharsUsed = numMessagesFragment.length();
1344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int numSendersUsed = 0;
1345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        while (maxPriorityToInclude < maxFoundPriority) {
1346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (priorityToLength.containsKey(maxPriorityToInclude + 1)) {
1347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int length = numCharsUsed + priorityToLength.get(maxPriorityToInclude + 1);
1348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (numCharsUsed > 0) length += 2;
1349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // We must show at least two senders if they exist. If we don't have space for both
1350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // then we will truncate names.
1351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (length > maxChars && numSendersUsed >= 2) {
1352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    break;
1353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                numCharsUsed = length;
1355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                numSendersUsed++;
1356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            maxPriorityToInclude++;
1358d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1359d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int numCharsToRemovePerWord = 0;
1361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (numCharsUsed > maxChars) {
1362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            numCharsToRemovePerWord = (numCharsUsed - maxChars) / numSendersUsed;
1363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String lastFragment = null;
1366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        CharacterStyle lastStyle = null;
1367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (int i = 0; i < numFragments;) {
1368d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String fragment0 = fragments[i++];
1369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if ("".equals(fragment0)) {
1370d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This should be the final fragment.
1371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_ELIDED.equals(fragment0)) {
1372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (lastFragment != null) {
1373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    addStyledFragment(senderBuilder, lastFragment, lastStyle, false);
1374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    senderBuilder.append(" ");
1375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    addStyledFragment(senderBuilder, "..", lastStyle, true);
1376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    senderBuilder.append(" ");
1377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                lastFragment = null;
1379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_NUM_MESSAGES.equals(fragment0)) {
1380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
1381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_NUM_DRAFTS.equals(fragment0)) {
1382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
1383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_SENDING.equals(fragment0)) {
1384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (Utils.SENDER_LIST_TOKEN_SEND_FAILED.equals(fragment0)) {
1385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final String unreadString = fragment0;
1387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final String priorityString = fragments[i++];
1388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String nameString = fragments[i++];
1389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean unread = unreadStatusIsForced
1390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        ? forcedUnreadStatus : Integer.parseInt(unreadString) != 0;
1391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // We want to include this entry if
1393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                //   1) The onlyShowUnread flags is not set
1394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                //   2) The above flag is set, and the message is unread
1395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (!onlyShowUnread || unread) {
1396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (nameString.length() == 0) {
1397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        nameString = meString.toString();
1398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } else {
1399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        nameString = Html.fromHtml(nameString).toString();
1400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (numCharsToRemovePerWord != 0) {
1402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        nameString = nameString.substring(
1403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                0, Math.max(nameString.length() - numCharsToRemovePerWord, 0));
1404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final int priority = Integer.parseInt(priorityString);
1406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (priority <= maxPriorityToInclude) {
1407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (lastFragment != null && !lastFragment.equals(nameString)) {
1408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            addStyledFragment(
1409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    senderBuilder, lastFragment.concat(","), lastStyle, false);
1410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            senderBuilder.append(" ");
1411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        lastFragment = nameString;
1413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        lastStyle = unread ? unreadStyle : readStyle;
1414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } else {
1415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (lastFragment != null) {
1416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            addStyledFragment(senderBuilder, lastFragment, lastStyle, false);
1417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Adjacent spans can cause the TextView in Gmail widget
1418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // confused and leads to weird behavior on scrolling.
1419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Our workaround here is to separate the spans by
1420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // spaces.
1421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            senderBuilder.append(" ");
1422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            addStyledFragment(senderBuilder, "..", lastStyle, true);
1423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            senderBuilder.append(" ");
1424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        lastFragment = null;
1426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (lastFragment != null) {
1431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            addStyledFragment(senderBuilder, lastFragment, lastStyle, false);
1432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        senderBuilder.append(numMessagesFragment);
1434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (fixedFragmentLength != 0) {
1435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            statusBuilder.append(fixedFragment);
1436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears the notifications for the specified account/folder/conversation.
1441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearFolderNotification(Context context, Account account, Folder folder) {
1443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications for %s/%s", account.name, folder.name);
1444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
1445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
1446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.remove(key);
1447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
1448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        markSeen(context, folder);
1450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static ArrayList<Long> findContacts(Context context, Collection<String> addresses) {
1453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<String> whereArgs = new ArrayList<String>();
1454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder whereBuilder = new StringBuilder();
1455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] questionMarks = new String[addresses.size()];
1456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereArgs.addAll(addresses);
1458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Arrays.fill(questionMarks, "?");
1459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereBuilder.append(Email.DATA1 + " IN (").
1460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(TextUtils.join(",", questionMarks)).
1461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(")");
1462d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ContentResolver resolver = context.getContentResolver();
1464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor c = resolver.query(Email.CONTENT_URI,
1465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new String[]{Email.CONTACT_ID}, whereBuilder.toString(),
1466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                whereArgs.toArray(new String[0]), null);
1467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = new ArrayList<Long>();
1469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (c == null) {
1470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return contactIds;
1471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
1473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (c.moveToNext()) {
1474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                contactIds.add(c.getLong(0));
1475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
1477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            c.close();
1478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return contactIds;
1480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
148261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getContactIcon(
148361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            Context context, String senderAddress, final Folder folder) {
1484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (senderAddress == null) {
1485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
1486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Bitmap icon = null;
1488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = findContacts(
1489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, Arrays.asList(new String[] { senderAddress }));
1490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (contactIds != null) {
1492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the ideal size for this icon.
1493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Resources res = context.getResources();
1494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int idealIconHeight =
1495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
1496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int idealIconWidth =
1497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
1498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (long id : contactIds) {
1499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri contactUri =
1500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
1501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri photoUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
1502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Cursor cursor = context.getContentResolver().query(
1503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        photoUri, new String[] { Photo.PHOTO }, null, null, null);
1504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
1506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    try {
1507d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (cursor.moveToFirst()) {
1508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            byte[] data = cursor.getBlob(0);
1509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (data != null) {
1510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                icon = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
1511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null && icon.getHeight() < idealIconHeight) {
1512d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    // We should scale this image to fit the intended size
1513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    icon = Bitmap.createScaledBitmap(
1514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                            icon, idealIconWidth, idealIconHeight, true);
1515d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null) {
1517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
1518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } finally {
1522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        cursor.close();
1523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (icon == null) {
1528d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // icon should be the default gmail icon.
152961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getDefaultNotificationIcon(context, folder, false /* single new message */);
1530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
1532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getMessageBodyWithoutElidedText(final Message message) {
15355e7e88b9f5ac1f239753e13ffb3ee4b249722c9aScott Kennedy        return getMessageBodyWithoutElidedText(message.getBodyAsHtml());
1536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1538d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static String getMessageBodyWithoutElidedText(String html) {
1539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(html)) {
1540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return "";
1541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Get the html "tree" for this message body
1543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final HtmlTree htmlTree = com.android.mail.utils.Utils.getHtmlTree(html);
1544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        htmlTree.setPlainTextConverterFactory(MESSAGE_CONVERTER_FACTORY);
1545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return htmlTree.getPlainText();
1547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void markSeen(final Context context, final Folder folder) {
1550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Uri uri = folder.uri;
1551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ContentValues values = new ContentValues(1);
1553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        values.put(UIProvider.ConversationColumns.SEEN, 1);
1554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        context.getContentResolver().update(uri, values, null, null);
1556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1558d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns a displayable string representing
1560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the message sender. It has a preference toward showing the name,
1561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * but will fall back to the address if that is all that is available.
1562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getDisplayableSender(String sender) {
1564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1565d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String displayableSender = address.getName();
1567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If that fails, default to the sender address.
1568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = address.getAddress();
1570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = sender;
1575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return displayableSender;
1577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns only the address portion of a message sender.
1581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getSenderAddress(String sender) {
1583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String tokenizedAddress = address.getAddress();
1586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(tokenizedAddress)) {
1590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            tokenizedAddress = sender;
1591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return tokenizedAddress;
1593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static int getNotificationId(final String account, final Folder folder) {
1596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return 1 ^ account.hashCode() ^ folder.hashCode();
1597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationKey {
1600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Account account;
1601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Folder folder;
1602d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public NotificationKey(Account account, Folder folder) {
1604d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.account = account;
1605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.folder = folder;
1606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public boolean equals(Object other) {
1610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!(other instanceof NotificationKey)) {
1611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return false;
1612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1613d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key = (NotificationKey) other;
1614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return account.equals(key.account) && folder.equals(key.folder);
1615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public String toString() {
1619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return account.toString() + " " + folder.name;
1620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public int hashCode() {
1624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int accountHashCode = account.hashCode();
1625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int folderHashCode = folder.hashCode();
1626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return accountHashCode ^ folderHashCode;
1627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1628d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1629d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1630d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Contains the logic for converting the contents of one HtmlTree into
1632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * plaintext.
1633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static class MailMessagePlainTextConverter extends HtmlTree.DefaultPlainTextConverter {
1635d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings for parsing html message bodies
1636d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_NAME = "div";
1637d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME = "class";
1638d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE = "elided-text";
1639d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HTML.Attribute ELIDED_TEXT_ATTRIBUTE =
1641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new HTML.Attribute(ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME, HTML.Attribute.NO_TYPE);
1642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1643d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HtmlDocument.Node ELIDED_TEXT_REPLACEMENT_NODE =
1644d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                HtmlDocument.createSelfTerminatingTag(HTML4.BR_ELEMENT, null, null, null);
1645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private int mEndNodeElidedTextBlock = -1;
1647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1649d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void addNode(HtmlDocument.Node n, int nodeNum, int endNum) {
1650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we are in the middle of an elided text block, don't add this node
1651d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (nodeNum < mEndNodeElidedTextBlock) {
1652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (nodeNum == mEndNodeElidedTextBlock) {
1654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                super.addNode(ELIDED_TEXT_REPLACEMENT_NODE, nodeNum, endNum);
1655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this tag starts another elided text block, we want to remember the end
1659d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (n instanceof HtmlDocument.Tag) {
1660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                boolean foundElidedTextTag = false;
1661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HtmlDocument.Tag htmlTag = (HtmlDocument.Tag)n;
1662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HTML.Element htmlElement = htmlTag.getElement();
1663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (ELIDED_TEXT_ELEMENT_NAME.equals(htmlElement.getName())) {
1664d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Make sure that the class is what is expected
1665d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final List<HtmlDocument.TagAttribute> attributes =
1666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            htmlTag.getAttributes(ELIDED_TEXT_ATTRIBUTE);
1667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    for (HtmlDocument.TagAttribute attribute : attributes) {
1668d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE.equals(
1669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                attribute.getValue())) {
1670d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Found an "elided-text" div.  Remember information about this tag
1671d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            mEndNodeElidedTextBlock = endNum;
1672d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            foundElidedTextTag = true;
1673d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            break;
1674d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1675d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (foundElidedTextTag) {
1679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return;
1680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1683d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            super.addNode(n, nodeNum, endNum);
1684d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1688d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * During account setup in Email, we may not have an inbox yet, so the notification setting had
1689d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * to be stored in {@link AccountPreferences}. If it is still there, we need to move it to the
1690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link FolderPreferences} now.
1691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void moveNotificationSetting(final AccountPreferences accountPreferences,
1693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences) {
1694d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (accountPreferences.isDefaultInboxNotificationsEnabledSet()) {
1695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this setting has been changed some other way, don't overwrite it
1696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.isNotificationsEnabledSet()) {
1697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean notificationsEnabled =
1698d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        accountPreferences.getDefaultInboxNotificationsEnabled();
1699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                folderPreferences.setNotificationsEnabled(notificationsEnabled);
1701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1703d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            accountPreferences.clearDefaultInboxNotificationsEnabled();
1704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
1707