NotificationUtils.java revision 26a20758413f0f8629ea71a0943304e3ae740165
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;
35f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sappersteinimport android.support.v4.text.BidiFormatter;
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableString;
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableStringBuilder;
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils;
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.CharacterStyle;
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.TextAppearanceSpan;
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.util.Pair;
4261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedyimport android.util.SparseArray;
43d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
44821e578a71c7015646522e729600618f0ec16fc0Tony Mantlerimport com.android.emailcommon.mail.Address;
45d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.EmailAddress;
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.R;
484fe0af81874976a1995191321e35c844b2229811Andy Huangimport com.android.mail.analytics.Analytics;
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.MessageCursor;
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.SendersView;
51fe23512c2be21d7c87677f5263078021002d89cbScott Kennedyimport com.android.mail.photomanager.LetterTileProvider;
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;
60c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedyimport com.android.mail.ui.ImageCanvas.Dimensions;
61d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationAction;
621bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HTML;
631bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HTML4;
641bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HtmlDocument;
651bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HtmlTree;
66ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sappersteinimport com.google.common.base.Objects;
677f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedyimport com.google.common.collect.ImmutableList;
6809400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Lists;
6909400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Sets;
70d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
71d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.io.ByteArrayInputStream;
72d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.ArrayList;
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Arrays;
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Collection;
75d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.List;
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.concurrent.ConcurrentHashMap;
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class NotificationUtils {
80c7d754703a0b5c2ca4b4dc9675d733881f85b746Scott Kennedy    public static final String LOG_TAG = "NotifUtils";
81d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** Contains a list of <(account, label), unread conversations> */
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static NotificationMap sActiveNotificationMap = null;
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
8561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static final SparseArray<Bitmap> sNotificationIcons = new SparseArray<Bitmap>();
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static TextAppearanceSpan sNotificationUnreadStyleSpan;
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharacterStyle sNotificationReadStyleSpan;
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
90d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** A factory that produces a plain text converter that removes elided text. */
91d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final HtmlTree.PlainTextConverterFactory MESSAGE_CONVERTER_FACTORY =
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            new HtmlTree.PlainTextConverterFactory() {
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                @Override
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                public HtmlTree.PlainTextConverter createInstance() {
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return new MailMessagePlainTextConverter();
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            };
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
9912fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    private static final BidiFormatter BIDI_FORMATTER = BidiFormatter.getInstance();
100f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears all notifications in response to the user tapping "Clear" in the status bar.
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearAllNotfications(Context context) {
1052f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications.");
106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.clear();
108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns the notification map, creating it if necessary.
113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static synchronized NotificationMap getNotificationMap(Context context) {
115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sActiveNotificationMap == null) {
116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap = new NotificationMap();
117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // populate the map from the cached data
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap.loadNotificationMap(context);
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return sActiveNotificationMap;
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Class representing the existing notifications, and the number of unread and
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * unseen conversations that triggered each.
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1282354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee    private static final class NotificationMap {
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String NOTIFICATION_PART_SEPARATOR = " ";
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final int NUM_NOTIFICATION_PARTS= 4;
1322354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        private final ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>> mMap =
1332354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            new ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>>();
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
1362354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the number of key values pairs in the inner map.
1372354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1382354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public int size() {
1392354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.size();
1402354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1412354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1422354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1432354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns a set of key values.
1442354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1452354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Set<NotificationKey> keySet() {
1462354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.keySet();
1472354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1482354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1492354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1502354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Remove the key from the inner map and return its value.
1512354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1522354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be removed.
1532354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return The value associated with this key.
1542354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1552354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Pair<Integer, Integer> remove(NotificationKey key) {
1562354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.remove(key);
1572354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1582354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1592354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1602354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Clear all key-value pairs in the map.
1612354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1622354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public void clear() {
1632354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.clear();
1642354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1652354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1662354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1672354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Discover if a key-value pair with this key exists.
1682354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1692354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be checked.
1702354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return If a key-value pair with this key exists in the map.
1712354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1722354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public boolean containsKey(NotificationKey key) {
1732354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.containsKey(key);
1742354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1752354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1762354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1772354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread count for the given NotificationKey.
178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnread(NotificationKey key) {
1802354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.first : null;
182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
1852354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread unseen count for the given NotificationKey.
186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnseen(NotificationKey key) {
1882354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.second : null;
190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Store the unread and unseen value for the given NotificationKey
194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void put(NotificationKey key, int unread, int unseen) {
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value =
197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Pair<Integer, Integer>(Integer.valueOf(unread), Integer.valueOf(unseen));
1982354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.put(key, value);
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Populates the notification map with previously cached data.
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void loadNotificationMap(final Context context) {
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = mailPrefs.getActiveNotificationSet();
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (notificationSet != null) {
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                for (String notificationEntry : notificationSet) {
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Get the parts of the string that make the notification entry
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] notificationParts =
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            TextUtils.split(notificationEntry, NOTIFICATION_PART_SEPARATOR);
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationParts.length == NUM_NOTIFICATION_PARTS) {
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri accountUri = Uri.parse(notificationParts[0]);
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor accountCursor = context.getContentResolver().query(
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                accountUri, UIProvider.ACCOUNTS_PROJECTION, null, null, null);
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Account account;
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (accountCursor.moveToFirst()) {
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                account = new Account(accountCursor);
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            accountCursor.close();
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri folderUri = Uri.parse(notificationParts[1]);
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor folderCursor = context.getContentResolver().query(
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folderUri, UIProvider.FOLDERS_PROJECTION, null, null, null);
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Folder folder;
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (folderCursor.moveToFirst()) {
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folder = new Folder(folderCursor);
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            folderCursor.close();
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final NotificationKey key = new NotificationKey(account, folder);
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unreadValue = Integer.valueOf(notificationParts[2]);
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unseenValue = Integer.valueOf(notificationParts[3]);
2442354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                        put(key, unreadValue, unseenValue);
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Cache the notification map.
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void saveNotificationMap(Context context) {
254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = Sets.newHashSet();
2552354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Set<NotificationKey> keys = mMap.keySet();
256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey key : keys) {
2572354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                final Pair<Integer, Integer> value = mMap.get(key);
258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unreadCount = value.first;
259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unseenCount = value.second;
260ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                if (unreadCount != null && unseenCount != null) {
261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] partValues = new String[] {
262259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            key.account.uri.toString(), key.folder.folderUri.fullUri.toString(),
263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            unreadCount.toString(), unseenCount.toString()};
264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues));
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            mailPrefs.cacheActiveNotificationSet(notificationSet);
269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return the title of this notification with each account and the number of unread and unseen
274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversations for it. Also remove any account in the map that has 0 unread.
275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String createNotificationString(NotificationMap notifications) {
277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder result = new StringBuilder();
278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int i = 0;
279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keysToRemove = Sets.newHashSet();
280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : notifications.keySet()) {
281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unread = notifications.getUnread(key);
282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unseen = notifications.getUnseen(key);
283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unread == null || unread.intValue() == 0) {
284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                keysToRemove.add(key);
285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (i > 0) result.append(", ");
287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                result.append(key.toString() + " (" + unread + ", " + unseen + ")");
288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : keysToRemove) {
293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notifications.remove(key);
294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return result.toString();
297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts and cancel them.
301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAllNotifications(Context context) {
3032f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "cancelAllNotifications - cancelling all");
304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        NotificationManager nm = (NotificationManager) context.getSystemService(
305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                Context.NOTIFICATION_SERVICE);
306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        nm.cancelAll();
307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        clearAllNotfications(context);
308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, cancel them, and repost.
312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAndResendNotifications(Context context) {
3152f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "cancelAndResendNotifications");
316ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein        resendNotifications(context, true, null, null);
317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, optionally cancel them, and repost.
321ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * This happens when locale changes. If you only want to resend messages from one
322ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * account-folder pair, pass in the account and folder that should be resent.
323ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All other account-folder pairs will not have their notifications resent.
324ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All notifications will be resent if account or folder is null.
325ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *
326ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param context Current context.
327ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param cancelExisting True, if all notifications should be canceled before resending.
328ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                       False, otherwise.
329ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param accountUri The {@link Uri} of the {@link Account} of the notification
330ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                   upon which an action occurred.
331ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param folderUri The {@link Uri} of the {@link Folder} of the notification
332ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                  upon which an action occurred.
333ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     */
334ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein    public static void resendNotifications(Context context, final boolean cancelExisting,
335259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            final Uri accountUri, final FolderUri folderUri) {
3362f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "resendNotifications ");
3372638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cancelExisting) {
3392f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - cancelling all");
340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm =
341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            nm.cancelAll();
343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Re-validate the notifications.
345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<NotificationKey> keys = notificationMap.keySet();
347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey notification : keys) {
348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder = notification.folder;
349b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
350b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(notification.account.getAccountManagerAccount(), folder);
351ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein
352ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // Only resend notifications if the notifications are from the same folder
353ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // and same account as the undo notification that was previously displayed.
35426b29ef558cce109c51584081c52781760a02dd9Scott Kennedy            if (accountUri != null && !Objects.equal(accountUri, notification.account.uri) &&
355259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folderUri != null && !Objects.equal(folderUri, folder.folderUri)) {
3562f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "resendNotifications - not resending %s / %s"
3572638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                        + " because it doesn't match %s / %s",
358259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                        notification.account.uri, folder.folderUri, accountUri, folderUri);
359ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                continue;
360ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            }
361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
3622f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - resending %s / %s",
363259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    notification.account.uri, folder.folderUri);
3642638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationAction undoableAction =
366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sUndoNotifications.get(notificationId);
367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (undoableAction == null) {
368ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                validateNotifications(context, folder, notification.account, true,
369ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                        false, notification);
370d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Create an undo notification
372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.createUndoNotification(context, undoableAction);
373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications for the specified account.
379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void validateAccountNotifications(Context context, String account) {
3812f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "validateAccountNotifications - %s", account);
3824162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        List<NotificationKey> notificationsToCancel = Lists.newArrayList();
384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Iterate through the notification map to see if there are any entries that correspond to
385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // labels that are not in the sync set.
386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keys = notificationMap.keySet();
388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final AccountPreferences accountPreferences = new AccountPreferences(context, account);
389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean enabled = accountPreferences.areNotificationsEnabled();
390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (!enabled) {
391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Cancel all notifications for this account
392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
393b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                if (notification.account.getAccountManagerAccount().name.equals(account)) {
394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationsToCancel.add(notification);
395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Iterate through the notification map to see if there are any entries that
399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // correspond to labels that are not in the notification set.
400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
401b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                if (notification.account.getAccountManagerAccount().name.equals(account)) {
402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // If notification is not enabled for this label, remember this NotificationKey
403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // to later cancel the notification, and remove the entry from the map
404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Folder folder = notification.folder;
405259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    final boolean isInbox = folder.folderUri.equals(
406259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            notification.account.settings.defaultInbox);
407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final FolderPreferences folderPreferences = new FolderPreferences(
408afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                            context, notification.account.getEmailAddress(), folder, isInbox);
409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!folderPreferences.areNotificationsEnabled()) {
411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationsToCancel.add(notification);
412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Cancel & remove the invalid notifications.
418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationsToCancel.size() > 0) {
419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm = (NotificationManager) context.getSystemService(
420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Context.NOTIFICATION_SERVICE);
421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : notificationsToCancel) {
422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Folder folder = notification.folder;
423b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                final int notificationId =
424b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                        getNotificationId(notification.account.getAccountManagerAccount(), folder);
4252f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "validateAccountNotifications - cancelling %s / %s",
42626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        notification.account.getEmailAddress(), folder.persistentId);
427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notificationMap.remove(notification);
429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sUndoNotifications.remove(notificationId);
430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.saveNotificationMap(context);
433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Display only one notification.
438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void setNewEmailIndicator(Context context, final int unreadCount,
440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int unseenCount, final Account account, final Folder folder,
441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean getAttention) {
4422f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "setNewEmailIndicator unreadCount = %d, unseenCount = %d, account = %s,"
44326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                + " folder = %s, getAttention = %b", unreadCount, unseenCount,
44426a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                account.getEmailAddress(), folder.folderUri, getAttention);
4454162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean ignoreUnobtrusiveSetting = false;
447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
448b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        final int notificationId = getNotificationId(account.getAccountManagerAccount(), folder);
449dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy
450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Update the notification map
451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount == 0) {
45426a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            LogUtils.d(LOG_TAG, "setNewEmailIndicator - cancelling %s / %s",
45526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    account.getEmailAddress(), folder.persistentId);
456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.remove(key);
457dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy            ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
458dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy                    .cancel(notificationId);
459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
460b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook            LogUtils.d(LOG_TAG, "setNewEmailIndicator - update count for: %s / %s " +
46126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    "to: unread: %d unseen %d", account.getEmailAddress(), folder.persistentId,
462b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    unreadCount, unseenCount);
463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!notificationMap.containsKey(key)) {
464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This account previously didn't have any unread mail; ignore the "unobtrusive
465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notifications" setting and play sound and/or vibrate the device even if a
466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notification already exists (bug 2412348).
467b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "setNewEmailIndicator - ignoringUnobtrusiveSetting");
468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                ignoreUnobtrusiveSetting = true;
469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.put(key, unreadCount, unseenCount);
471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
4752f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.v(LOG_TAG, "New email: %s mapSize: %d getAttention: %b",
476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    createNotificationString(notificationMap), notificationMap.size(),
477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    getAttention);
478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (NotificationActionUtils.sUndoNotifications.get(notificationId) == null) {
481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            validateNotifications(context, folder, account, getAttention, ignoreUnobtrusiveSetting,
482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    key);
483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications notification.
488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void validateNotifications(Context context, final Folder folder,
490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, boolean getAttention, boolean ignoreUnobtrusiveSetting,
491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key) {
492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        NotificationManager nm = (NotificationManager)
494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context.getSystemService(Context.NOTIFICATION_SERVICE);
495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
49832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification: %s mapSize: %d "
499b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "folder: %s getAttention: %b ignoreUnobtrusive: %b",
500b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    createNotificationString(notificationMap),
501b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    notificationMap.size(), folder.name, getAttention, ignoreUnobtrusiveSetting);
50232fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        } else {
50332fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification, mapSize: %d "
504b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "getAttention: %b ignoreUnobtrusive: %b", notificationMap.size(),
505b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    getAttention, ignoreUnobtrusiveSetting);
506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
507d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // The number of unread messages for this account and label.
508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unread = notificationMap.getUnread(key);
509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int unreadCount = unread != null ? unread.intValue() : 0;
510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unseen = notificationMap.getUnseen(key);
511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int unseenCount = unseen != null ? unseen.intValue() : 0;
512d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor cursor = null;
514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
515d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Uri.Builder uriBuilder = folder.conversationListUri.buildUpon();
517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            uriBuilder.appendQueryParameter(
518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.SEEN_QUERY_PARAMETER, Boolean.FALSE.toString());
5190be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // Do not allow this quick check to disrupt any active network-enabled conversation
5200be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // cursor.
5210be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            uriBuilder.appendQueryParameter(
5220be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    UIProvider.ConversationListQueryParameters.USE_NETWORK,
5230be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    Boolean.FALSE.toString());
524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor = context.getContentResolver().query(uriBuilder.build(),
525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.CONVERSATION_PROJECTION, null, null, null);
5263a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            if (cursor == null) {
5273a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                // This folder doesn't exist.
52832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
52932fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        "The cursor is null, so the specified folder probably does not exist");
5303a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                clearFolderNotification(context, account, folder, false);
5313a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                return;
5323a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            }
533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int cursorUnseenCount = cursor.getCount();
534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Make sure the unseen count matches the number of items in the cursor.  But, we don't
536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // want to overwrite a 0 unseen count that was specified in the intent
537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount != 0 && unseenCount != cursorUnseenCount) {
53832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
5392f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        "Unseen count doesn't match cursor count.  unseen: %d cursor count: %d",
540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        unseenCount, cursorUnseenCount);
541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = cursorUnseenCount;
542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For the purpose of the notifications, the unseen count should be capped at the num of
545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // unread conversations.
546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount > unreadCount) {
547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = unreadCount;
548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
550b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
551b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(account.getAccountManagerAccount(), folder);
552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount == 0) {
55432fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "validateNotifications - cancelling account %s / folder %s",
55526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
55632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        LogUtils.sanitizeName(LOG_TAG, folder.persistentId));
557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
558d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // We now have all we need to create the notification and the pending intent
562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            PendingIntent clickIntent;
563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
565d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setSmallIcon(R.drawable.stat_notify_email);
56626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            notification.setTicker(account.getDisplayName());
567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long when;
569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long oldWhen =
571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sNotificationTimestamps.get(notificationId);
572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (oldWhen != 0) {
573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = oldWhen;
574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = System.currentTimeMillis();
576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setWhen(when);
579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The timestamp is now stored in the notification, so we can remove it from here
581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Dispatch a CLEAR_NEW_MAIL_NOTIFICATIONS intent if the user taps the "X" next to a
584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // notification.  Also this intent gets fired when the user taps on a notification as
585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the AutoCancel flag has been set
586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Intent cancelNotificationIntent =
587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.setPackage(context.getPackageName());
58909400efa442422299acf21abe20e3470f9d965abScott Kennedy            cancelNotificationIntent.setData(Utils.appendVersionQueryParameter(context,
590259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri));
59148cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
59248cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder);
593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setDeleteIntent(PendingIntent.getService(
595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, notificationId, cancelNotificationIntent, 0));
596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Ensure that the notification is cleared when the user selects it
598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setAutoCancel(true);
599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean eventInfoConfigured = false;
601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
602259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            final boolean isInbox = folder.folderUri.equals(account.settings.defaultInbox);
603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences =
604afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                    new FolderPreferences(context, account.getEmailAddress(), folder, isInbox);
605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (isInbox) {
607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
608afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                        new AccountPreferences(context, account.getEmailAddress());
609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                moveNotificationSetting(accountPreferences, folderPreferences);
610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.areNotificationsEnabled()) {
61332fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "Notifications are disabled for this folder; not notifying");
614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Don't notify
615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unreadCount > 0) {
619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // How can I order this properly?
620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor.moveToNext()) {
621bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    final Intent notificationIntent;
622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
623bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    // Launch directly to the conversation, if there is only 1 unseen conversation
6244fe0af81874976a1995191321e35c844b2229811Andy Huang                    final boolean launchConversationMode = (unseenCount == 1);
6254fe0af81874976a1995191321e35c844b2229811Andy Huang                    if (launchConversationMode) {
62609400efa442422299acf21abe20e3470f9d965abScott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
62709400efa442422299acf21abe20e3470f9d965abScott Kennedy                                cursor);
628bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    } else {
629bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
630bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                                null);
631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
6334fe0af81874976a1995191321e35c844b2229811Andy Huang                    Analytics.getInstance().sendEvent("notification_create",
6344fe0af81874976a1995191321e35c844b2229811Andy Huang                            launchConversationMode ? "conversation" : "conversation_list",
6354fe0af81874976a1995191321e35c844b2229811Andy Huang                            folder.getTypeDescription(), unseenCount);
6364fe0af81874976a1995191321e35c844b2229811Andy Huang
637d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationIntent == null) {
6382f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        LogUtils.e(LOG_TAG, "Null intent when building notification");
639d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return;
640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
6424fe0af81874976a1995191321e35c844b2229811Andy Huang                    // Amend the click intent with a hint that its source was a notification,
6434fe0af81874976a1995191321e35c844b2229811Andy Huang                    // but remove the hint before it's used to generate notification action
6444fe0af81874976a1995191321e35c844b2229811Andy Huang                    // intents. This prevents the following sequence:
6454fe0af81874976a1995191321e35c844b2229811Andy Huang                    // 1. generate single notification
6464fe0af81874976a1995191321e35c844b2229811Andy Huang                    // 2. user clicks reply, then completes Compose activity
6474fe0af81874976a1995191321e35c844b2229811Andy Huang                    // 3. main activity launches, gets FROM_NOTIFICATION hint in intent
6484fe0af81874976a1995191321e35c844b2229811Andy Huang                    notificationIntent.putExtra(Utils.EXTRA_FROM_NOTIFICATION, true);
6494fe0af81874976a1995191321e35c844b2229811Andy Huang                    clickIntent = PendingIntent.getActivity(context, -1, notificationIntent,
6504fe0af81874976a1995191321e35c844b2229811Andy Huang                            PendingIntent.FLAG_UPDATE_CURRENT);
6514fe0af81874976a1995191321e35c844b2229811Andy Huang                    notificationIntent.removeExtra(Utils.EXTRA_FROM_NOTIFICATION);
6524fe0af81874976a1995191321e35c844b2229811Andy Huang
653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    configureLatestEventInfoFromConversation(context, account, folderPreferences,
654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            notification, cursor, clickIntent, notificationIntent,
655b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                            unreadCount, unseenCount, folder, when);
656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    eventInfoConfigured = true;
657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
659d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean notifyOnce = !folderPreferences.isEveryMessageNotificationEnabled();
663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
664ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (!ignoreUnobtrusiveSetting && notifyOnce) {
665d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // If the user has "unobtrusive notifications" enabled, only alert the first time
666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // new mail is received in this account.  This is the default behavior.  See
667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // bugs 2412348 and 2413490.
668b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "Setting Alert Once");
669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setOnlyAlertOnce(true);
670d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
671d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
67232fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Account: %s vibrate: %s",
67326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
674ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                    Boolean.toString(folderPreferences.isNotificationVibrateEnabled()));
675d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int defaults = 0;
677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            /*
679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * We do not want to notify if this is coming back from an Undo notification, hence the
680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * oldWhen check.
681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             */
682ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (getAttention && oldWhen == 0) {
683d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
684cfb969b27f83777e08cd2f1a994dc023574a4739Tony Mantler                        new AccountPreferences(context, account.getEmailAddress());
685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (accountPreferences.areNotificationsEnabled()) {
686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (vibrate) {
687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        defaults |= Notification.DEFAULT_VIBRATE;
688d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
689d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            : Uri.parse(ringtoneUri));
69232fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                    LogUtils.i(LOG_TAG, "New email in %s vibrateWhen: %s, playing notification: %s",
69326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()), vibrate,
69426a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            ringtoneUri);
695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
69832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            // TODO(skennedy) Why do we do any of the above if we're just going to bail here?
699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (eventInfoConfigured) {
700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                defaults |= Notification.DEFAULT_LIGHTS;
701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setDefaults(defaults);
702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
703d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (oldWhen != 0) {
704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // We do not want to display the ticker again if we are re-displaying this
705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notification (like from an Undo notification)
706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setTicker(null);
707d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
708d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
709d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.notify(notificationId, notification.build());
71032fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            } else {
71132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "event info not configured - not notifying");
712d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (cursor != null) {
715d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                cursor.close();
716d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
717d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
718d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
719d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
720d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
721d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return an {@link Intent} which, if launched, will display the corresponding conversation
722d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
72309400efa442422299acf21abe20e3470f9d965abScott Kennedy    private static Intent createViewConversationIntent(final Context context, final Account account,
72409400efa442422299acf21abe20e3470f9d965abScott Kennedy            final Folder folder, final Cursor cursor) {
725d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (folder == null || account == null) {
7262f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.e(LOG_TAG, "createViewConversationIntent(): "
72713a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    + "Null account or folder.  account: %s folder: %s", account, folder);
728d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
729d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Intent intent;
732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
733d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cursor == null) {
734259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewFolderIntent(context, folder.folderUri.fullUri, account);
735d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // A conversation cursor has been specified, so this intent is intended to be go
737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // directly to the one new conversation
738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the Conversation object
740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
741259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewConversationIntent(context, conversation,
742259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri, account);
743d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
744d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
745d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return intent;
746d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
747d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
74861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getDefaultNotificationIcon(
74961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            final Context context, final Folder folder, final boolean multipleNew) {
750c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int resId;
75161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (folder.notificationIconResId != 0) {
752c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = folder.notificationIconResId;
75361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        } else if (multipleNew) {
754c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = R.drawable.ic_notification_multiple_mail_holo_dark;
755d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
756c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = R.drawable.ic_contact_picture;
757d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
758c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
759c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final Bitmap icon = getIcon(context, resId);
760c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
761c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        if (icon == null) {
762c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            LogUtils.e(LOG_TAG, "Couldn't decode notif icon res id %d", resId);
763c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        }
764c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
765d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
766d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
767d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
76861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getIcon(final Context context, final int resId) {
76961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap cachedIcon = sNotificationIcons.get(resId);
77061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (cachedIcon != null) {
77161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            return cachedIcon;
77261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        }
77361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
77461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resId);
77561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        sNotificationIcons.put(resId, icon);
77661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
77761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        return icon;
77861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    }
77961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
780d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void configureLatestEventInfoFromConversation(final Context context,
781d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, final FolderPreferences folderPreferences,
782d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationCompat.Builder notification, final Cursor conversationCursor,
783d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final PendingIntent clickIntent, final Intent notificationIntent,
784b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int unreadCount, final int unseenCount,
785d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder, final long when) {
786d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Resources res = context.getResources();
78726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountDisplayName = account.getDisplayName();
78826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountEmail = account.getEmailAddress();
789d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
79032fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        LogUtils.i(LOG_TAG, "Showing notification with unreadCount of %d and unseenCount of %d",
7912f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                unreadCount, unseenCount);
792d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
793d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String notificationTicker = null;
794d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
795d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Boolean indicating that this notification is for a non-inbox label.
796259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final boolean isInbox = folder.folderUri.fullUri.equals(account.settings.defaultInbox);
797d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
798d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Notification label name for user label notifications.
799d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String notificationLabelName = isInbox ? null : folder.name;
800d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
801d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unseenCount > 1) {
802d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Build the string that describes the number of new messages
803d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String newMessagesString = res.getString(R.string.new_messages, unseenCount);
804d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
805d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Use the default notification icon
806d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setLargeIcon(
80761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                    getDefaultNotificationIcon(context, folder, true /* multiple new messages */));
808d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
809d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The ticker initially start as the new messages string.
810d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = newMessagesString;
811d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
812d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The title of the notification is the new messages string
813d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setContentTitle(newMessagesString);
814d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
815c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy            // TODO(skennedy) Can we remove this check?
816d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (com.android.mail.utils.Utils.isRunningJellybeanOrLater()) {
817d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // For a new-style notification
818d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int maxNumDigestItems = context.getResources().getInteger(
819d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        R.integer.max_num_notification_digest_items);
820d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
821d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
82226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                notification.setSubText(
82326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
824d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
825d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final NotificationCompat.InboxStyle digest =
826d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new NotificationCompat.InboxStyle(notification);
827d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int numDigestItems = 0;
829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                do {
830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Conversation conversation = new Conversation(conversationCursor);
831d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!conversation.read) {
833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        boolean multipleUnreadThread = false;
834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // TODO(cwren) extract this pattern into a helper
835d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
836d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Cursor cursor = null;
837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        MessageCursor messageCursor = null;
838d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
839d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final Uri.Builder uriBuilder = conversation.messageListUri.buildUpon();
840d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            uriBuilder.appendQueryParameter(
841d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.LABEL_QUERY_PARAMETER, notificationLabelName);
842d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor = context.getContentResolver().query(uriBuilder.build(),
843d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.MESSAGE_PROJECTION, null, null, null);
844d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            messageCursor = new MessageCursor(cursor);
845d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
846b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                            String from = "";
847d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            String fromAddress = "";
848d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
849d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
850d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                fromAddress = message.getFrom();
851b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                if (fromAddress == null) {
852b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                    fromAddress = "";
853b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                }
854d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                from = getDisplayableSender(fromAddress);
855d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
856d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
857d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
858d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (!message.read &&
859d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        !fromAddress.contentEquals(message.getFrom())) {
860d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    multipleUnreadThread = true;
861d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
862d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
863d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
864d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final SpannableStringBuilder sendersBuilder;
865d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (multipleUnreadThread) {
866d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final int sendersLength =
867d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        res.getInteger(R.integer.swipe_senders_length);
868d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
869d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = getStyledSenders(context, conversationCursor,
87026a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                                        sendersLength, notificationAccountEmail);
871d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
87212fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                sendersBuilder =
87312fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                        new SpannableStringBuilder(getWrappedFromString(from));
874d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
875d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final CharSequence digestLine = getSingleMessageInboxLine(context,
876d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    sendersBuilder.toString(),
877d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    conversation.subject,
878edd6c1a2807d2ade930dfd4622707298dc470d64Tony Mantler                                    conversation.getSnippet());
879d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            digest.addLine(digestLine);
880d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            numDigestItems++;
881d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
882d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor != null) {
883d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                messageCursor.close();
884d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
885d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (cursor != null) {
886d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                cursor.close();
887d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
888d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
889d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
890d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } while (numDigestItems <= maxNumDigestItems && conversationCursor.moveToNext());
891d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
892d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
893d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setContentText(
89426a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
895d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
896d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
897d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For notifications for a single new conversation, we want to get the information from
898d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the conversation
899d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
900d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Move the cursor to the most recent unread conversation
901d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            seekToLatestUnreadConversation(conversationCursor);
902d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
903d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(conversationCursor);
904d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
905d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Cursor cursor = null;
906d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            MessageCursor messageCursor = null;
907d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean multipleUnseenThread = false;
908d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String from = null;
909d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            try {
910ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                final Uri uri = conversation.messageListUri.buildUpon().appendQueryParameter(
911ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                        UIProvider.LABEL_QUERY_PARAMETER, folder.persistentId).build();
912ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                cursor = context.getContentResolver().query(uri, UIProvider.MESSAGE_PROJECTION,
913ffbf86fc9f7ab851719829135caa2cb7780c5c3fScott Kennedy                        null, null, null);
914d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                messageCursor = new MessageCursor(cursor);
915d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Use the information from the last sender in the conversation that triggered
916d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // this notification.
917d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
918d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                String fromAddress = "";
919d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
920d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Message message = messageCursor.getMessage();
921d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    fromAddress = message.getFrom();
922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    from = getDisplayableSender(fromAddress);
923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setLargeIcon(
924c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                            getContactIcon(context, from, getSenderAddress(fromAddress), folder));
925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
927d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Assume that the last message in this conversation is unread
928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int firstUnseenMessagePos = messageCursor.getPosition();
929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Message message = messageCursor.getMessage();
931d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final boolean unseen = !message.seen;
932d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (unseen) {
933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        firstUnseenMessagePos = messageCursor.getPosition();
934d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (!multipleUnseenThread
935d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                && !fromAddress.contentEquals(message.getFrom())) {
936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            multipleUnseenThread = true;
937d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
938d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
939d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
940d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
941c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy                // TODO(skennedy) Can we remove this check?
942d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (Utils.isRunningJellybeanOrLater()) {
943d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For a new-style notification
944d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
945d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (multipleUnseenThread) {
946d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // The title of a single conversation is the list of senders.
947d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        int sendersLength = res.getInteger(R.integer.swipe_senders_length);
948d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
949d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final SpannableStringBuilder sendersBuilder = getStyledSenders(
95026a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                                context, conversationCursor, sendersLength,
95126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                                notificationAccountEmail);
952d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
953d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification.setContentTitle(sendersBuilder);
954d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // For a single new conversation, the ticker is based on the sender's name.
955d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationTicker = sendersBuilder.toString();
956d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } else {
95712fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                        from = getWrappedFromString(from);
958d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // The title of a single message the sender.
959d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notification.setContentTitle(from);
960d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // For a single new conversation, the ticker is based on the sender's name.
961d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationTicker = from;
962d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
963d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
964d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification content will be the subject of the conversation.
965d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentText(
966d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            getSingleMessageLittleText(context, conversation.subject));
967d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
968d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification subtext will be the subject of the conversation for inbox
969d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications, or will based on the the label name for user label
970d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications.
97126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    notification.setSubText(isInbox ?
97226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            notificationAccountDisplayName : notificationLabelName);
973d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
974d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (multipleUnseenThread) {
97561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                        notification.setLargeIcon(
97661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                                getDefaultNotificationIcon(context, folder, true));
977d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
978d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final NotificationCompat.BigTextStyle bigText =
979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            new NotificationCompat.BigTextStyle(notification);
980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
981d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Seek the message cursor to the first unread message
98292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    final Message message;
98392b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    if (messageCursor.moveToPosition(firstUnseenMessagePos)) {
98492b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        message = messageCursor.getMessage();
98592b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        bigText.bigText(getSingleMessageBigText(context,
98692b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                conversation.subject, message));
98792b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    } else {
9882f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        LogUtils.e(LOG_TAG, "Failed to load message");
98992b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        message = null;
99092b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
991d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
99292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    if (message != null) {
99392b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        final Set<String> notificationActions =
994cde6eb0eb380f6212b7091d9381b3fec5006f6a8Scott Kennedy                                folderPreferences.getNotificationActions(account);
995d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
996b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                        final int notificationId = getNotificationId(
997b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                                account.getAccountManagerAccount(), folder);
998d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
99992b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                        NotificationActionUtils.addNotificationActions(context, notificationIntent,
100092b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                notification, account, conversation, message, folder,
100192b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                                notificationId, when, notificationActions);
100292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
1003d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
1004d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For an old-style notification
1005d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1006d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The title of a single conversation notification is built from both the sender
1007d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // and subject of the new message.
1008d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentTitle(getSingleMessageNotificationTitle(context,
1009d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            from, conversation.subject));
1010d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1011d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // The notification content will be the subject of the conversation for inbox
1012d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications, or will based on the the label name for user label
1013d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notifications.
1014d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setContentText(
101526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            isInbox ? notificationAccountDisplayName : notificationLabelName);
1016d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1017d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // For a single new conversation, the ticker is based on the sender's name.
1018d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTicker = from;
1019d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1020d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } finally {
1021d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (messageCursor != null) {
1022d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    messageCursor.close();
1023d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1024d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
1025d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    cursor.close();
1026d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1027d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1028d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1029d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1030d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Build the notification ticker
1031d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationLabelName != null && notificationTicker != null) {
1032d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // This is a per label notification, format the ticker with that information
1033d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = res.getString(R.string.label_notification_ticker,
1034d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationLabelName, notificationTicker);
1035d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1036d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1037d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationTicker != null) {
1038d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we didn't generate a notification ticker, it will default to account name
1039d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setTicker(notificationTicker);
1040d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1041d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1042d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Set the number in the notification
1043d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount > 1) {
1044d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setNumber(unreadCount);
1045d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1046d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1047d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notification.setContentIntent(clickIntent);
1048d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1049d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
105012fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    private static String getWrappedFromString(String from) {
105112fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        if (from == null) {
105212fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            LogUtils.e(LOG_TAG, "null from string in getWrappedFromString");
105312fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            from = "";
105412fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        }
105512fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        from = BIDI_FORMATTER.unicodeWrap(from);
105612fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        return from;
105712fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    }
105812fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook
1059d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder getStyledSenders(final Context context,
1060d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Cursor conversationCursor, final int maxLength, final String account) {
1061d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Conversation conversation = new Conversation(conversationCursor);
1062d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final com.android.mail.providers.ConversationInfo conversationInfo =
1063d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                conversation.conversationInfo;
1064d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ArrayList<SpannableString> senders = new ArrayList<SpannableString>();
1065d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sNotificationUnreadStyleSpan == null) {
1066d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationUnreadStyleSpan = new TextAppearanceSpan(
1067d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, R.style.NotificationSendersUnreadTextAppearance);
1068d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationReadStyleSpan =
1069d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSendersReadTextAppearance);
1070d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1071d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SendersView.format(context, conversationInfo, "", maxLength, senders, null, null, account,
1072d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sNotificationUnreadStyleSpan, sNotificationReadStyleSpan, false);
1073d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1074d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return ellipsizeStyledSenders(context, senders);
1075d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1076d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1077d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sSendersSplitToken = null;
1078d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sElidedPaddingToken = null;
1079d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1080d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder ellipsizeStyledSenders(final Context context,
1081d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            ArrayList<SpannableString> styledSenders) {
1082d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sSendersSplitToken == null) {
1083d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sSendersSplitToken = context.getString(R.string.senders_split_token);
1084d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sElidedPaddingToken = context.getString(R.string.elided_padding_token);
1085d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1086d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1087d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder builder = new SpannableStringBuilder();
1088d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString prevSender = null;
1089d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (SpannableString sender : styledSenders) {
1090d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (sender == null) {
10912f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.e(LOG_TAG, "null sender iterating over styledSenders");
1092d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                continue;
1093d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1094d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
1095d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (SendersView.sElidedString.equals(sender.toString())) {
1096d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1097d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
1098d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (builder.length() > 0
1099d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    && (prevSender == null || !SendersView.sElidedString.equals(prevSender
1100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            .toString()))) {
1101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sSendersSplitToken + sender);
1103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(sender);
1107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return builder;
1109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableString copyStyles(CharacterStyle[] spans, CharSequence newText) {
1112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString s = new SpannableString(newText);
1113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (spans != null && spans.length > 0) {
1114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            s.setSpan(spans[0], 0, s.length(), 0);
1115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return s;
1117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Seeks the cursor to the position of the most recent unread conversation. If no unread
1121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversation is found, the position of the cursor will be restored, and false will be
1122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * returned.
1123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static boolean seekToLatestUnreadConversation(final Cursor cursor) {
1125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int initialPosition = cursor.getPosition();
1126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        do {
1127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
1128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!conversation.read) {
1129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return true;
1130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } while (cursor.moveToNext());
1132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Didn't find an unread conversation, reset the position.
1134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        cursor.moveToPosition(initialPosition);
1135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return false;
1136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senders Sender of the new message that triggered the notification.
1143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param snippet Snippet of the new message that triggered the notification
1145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageInboxLine(Context context,
1149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String senders, String subject, String snippet) {
1150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // TODO(cwren) finish this step toward commmon code with getSingleMessageBigText
1151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String subjectSnippet = !TextUtils.isEmpty(subject) ? subject : snippet;
1153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1154d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationPrimarySpan =
1155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
1156d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1157d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(senders)) {
1158d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the senders are empty, just use the subject/snippet.
1159d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return subjectSnippet;
1160d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(subjectSnippet)) {
1161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject/snippet is empty, just use the senders.
1162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(senders);
1163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan, 0, senders.length(), 0);
1164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String formatString = context.getResources().getString(
1168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.multiple_new_message_notification_item);
1169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSecondarySpan =
1170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
11728173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein            // senders is already individually unicode wrapped so it does not need to be done here
1173f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein            final String instantiatedString = String.format(formatString,
11748173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein                    senders,
117512fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                    BIDI_FORMATTER.unicodeWrap(subjectSnippet));
1176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(instantiatedString);
1178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isOrderReversed = formatString.indexOf("%2$s") <
1180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    formatString.indexOf("%1$s");
1181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int primaryOffset =
1182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(senders) :
1183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(senders));
1184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int secondaryOffset =
1185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(subjectSnippet) :
1186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(subjectSnippet));
1187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan,
1188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    primaryOffset, primaryOffset + senders.length(), 0);
1189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSecondarySpan,
1190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    secondaryOffset, secondaryOffset + subjectSnippet.length(), 0);
1191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1199f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * @return a {@link CharSequence} suitable for use in
1200f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * {@link NotificationCompat.Builder#setContentText}
1201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageLittleText(Context context, String subject) {
1203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final SpannableString spannableString = new SpannableString(subject);
1207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return spannableString;
1210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param message the {@link Message} to be displayed.
1218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageBigText(Context context, String subject,
1222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Message message) {
1223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String snippet = getMessageBodyWithoutElidedText(message);
1228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Change multiple newlines (with potential white space between), into a single new line
1230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String collapsedSnippet =
1231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";
1232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just use the snippet.
1235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return snippet;
1236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(collapsedSnippet)) {
1237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the snippet is empty, just use the subject.
1238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(subject);
1239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationBigTextFormat = context.getResources().getString(
1244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_big_text);
1245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") >
1249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationBigTextFormat.indexOf("%1$s");
1250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String bigText =
1251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    String.format(notificationBigTextFormat, subject, collapsedSnippet);
1252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(bigText);
1253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isSubjectFirst ? bigText.indexOf(subject) : bigText.lastIndexOf(subject));
1256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan,
1257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the title for a notification for a single new conversation
1265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sender Sender of the new message that triggered the notification.
1267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use as a {@link Notification} title.
1269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageNotificationTitle(Context context,
1271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String sender, String subject) {
1272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just set the title to the sender's information.
1275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return sender;
1276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationTitleFormat = context.getResources().getString(
1278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_title);
1279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectLast = notificationTitleFormat.indexOf("%2$s") >
1283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTitleFormat.indexOf("%1$s");
1284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String titleString = String.format(notificationTitleFormat, sender, subject);
1285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Format the string so the subject is using the secondaryText style
1287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString titleSpannable = new SpannableString(titleString);
1288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Find the offset of the subject.
1290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    isSubjectLast ? titleString.lastIndexOf(subject) : titleString.indexOf(subject);
1292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSubjectSpan =
1293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            titleSpannable.setSpan(notificationSubjectSpan,
1295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return titleSpannable;
1297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
13017f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears the notifications for the specified account/folder.
1302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
13037f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    public static void clearFolderNotification(Context context, Account account, Folder folder,
13047f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final boolean markSeen) {
130526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        LogUtils.v(LOG_TAG, "Clearing all notifications for %s/%s", account.getEmailAddress(),
130626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                folder.name);
1307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
1308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
1309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.remove(key);
1310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
1311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
13127f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationManager notificationManager =
13137f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
1314b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        notificationManager.cancel(getNotificationId(account.getAccountManagerAccount(), folder));
13157f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13167f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        if (markSeen) {
13177f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            markSeen(context, folder);
13187f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
13197f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    }
13207f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13217f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    /**
13227f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears all notifications for the specified account.
13237f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     */
1324b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static void clearAccountNotifications(final Context context,
1325b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final android.accounts.Account account) {
13262f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications for %s", account);
13277f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
13287f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13297f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        // Find all NotificationKeys for this account
13307f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final ImmutableList.Builder<NotificationKey> keyBuilder = ImmutableList.builder();
13317f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13327f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey key : notificationMap.keySet()) {
1333b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            if (account.equals(key.account.getAccountManagerAccount())) {
13347f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                keyBuilder.add(key);
13357f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            }
13367f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
13377f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13387f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final List<NotificationKey> notificationKeys = keyBuilder.build();
13397f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13407f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationManager notificationManager =
13417f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
13427f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13437f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey notificationKey : notificationKeys) {
13447f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final Folder folder = notificationKey.folder;
13457f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationManager.cancel(getNotificationId(account, folder));
13467f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationMap.remove(notificationKey);
13477f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
13487f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
13497f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        notificationMap.saveNotificationMap(context);
1350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static ArrayList<Long> findContacts(Context context, Collection<String> addresses) {
1353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<String> whereArgs = new ArrayList<String>();
1354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder whereBuilder = new StringBuilder();
1355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] questionMarks = new String[addresses.size()];
1356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereArgs.addAll(addresses);
1358d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Arrays.fill(questionMarks, "?");
1359d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereBuilder.append(Email.DATA1 + " IN (").
1360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(TextUtils.join(",", questionMarks)).
1361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(")");
1362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ContentResolver resolver = context.getContentResolver();
1364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor c = resolver.query(Email.CONTENT_URI,
1365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new String[]{Email.CONTACT_ID}, whereBuilder.toString(),
1366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                whereArgs.toArray(new String[0]), null);
1367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1368d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = new ArrayList<Long>();
1369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (c == null) {
1370d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return contactIds;
1371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
1373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (c.moveToNext()) {
1374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                contactIds.add(c.getLong(0));
1375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
1377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            c.close();
1378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return contactIds;
1380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1382c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy    private static Bitmap getContactIcon(final Context context, final String displayName,
1383c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            final String senderAddress, final Folder folder) {
1384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (senderAddress == null) {
1385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
1386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1387c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Bitmap icon = null;
1389c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1390c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final List<Long> contactIds = findContacts( context, Arrays.asList(
1391c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                new String[] { senderAddress }));
1392c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1393c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        // Get the ideal size for this icon.
1394c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final Resources res = context.getResources();
1395c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int idealIconHeight =
1396c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
1397c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int idealIconWidth =
1398c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
1399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (contactIds != null) {
14014046d0695e102368201bb2f3a268a4f85c8bc4dbScott Kennedy            for (final long id : contactIds) {
1402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri contactUri =
1403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
1404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri photoUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
1405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Cursor cursor = context.getContentResolver().query(
1406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        photoUri, new String[] { Photo.PHOTO }, null, null, null);
1407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
1409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    try {
1410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (cursor.moveToFirst()) {
14114046d0695e102368201bb2f3a268a4f85c8bc4dbScott Kennedy                            final byte[] data = cursor.getBlob(0);
1412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (data != null) {
1413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                icon = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
1414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null && icon.getHeight() < idealIconHeight) {
1415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    // We should scale this image to fit the intended size
1416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    icon = Bitmap.createScaledBitmap(
1417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                            icon, idealIconWidth, idealIconHeight, true);
1418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null) {
1420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
1421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } finally {
1425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        cursor.close();
1426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1430c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1431c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        if (icon == null) {
1432c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            // Make a colorful tile!
1433c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            final Dimensions dimensions = new Dimensions(idealIconWidth, idealIconHeight,
1434c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                    Dimensions.SCALE_ONE);
1435c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1436fe23512c2be21d7c87677f5263078021002d89cbScott Kennedy            icon = new LetterTileProvider(context).getLetterTile(dimensions,
1437c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                    displayName, senderAddress);
1438c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        }
1439c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (icon == null) {
1441c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            // Icon should be the default mail icon.
144261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getDefaultNotificationIcon(context, folder, false /* single new message */);
1443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
1445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getMessageBodyWithoutElidedText(final Message message) {
14485e7e88b9f5ac1f239753e13ffb3ee4b249722c9aScott Kennedy        return getMessageBodyWithoutElidedText(message.getBodyAsHtml());
1449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static String getMessageBodyWithoutElidedText(String html) {
1452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(html)) {
1453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return "";
1454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Get the html "tree" for this message body
1456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final HtmlTree htmlTree = com.android.mail.utils.Utils.getHtmlTree(html);
1457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        htmlTree.setPlainTextConverterFactory(MESSAGE_CONVERTER_FACTORY);
1458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return htmlTree.getPlainText();
1460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1462d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void markSeen(final Context context, final Folder folder) {
1463259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final Uri uri = folder.folderUri.fullUri;
1464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ContentValues values = new ContentValues(1);
1466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        values.put(UIProvider.ConversationColumns.SEEN, 1);
1467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        context.getContentResolver().update(uri, values, null, null);
1469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns a displayable string representing
1473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the message sender. It has a preference toward showing the name,
1474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * but will fall back to the address if that is all that is available.
1475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getDisplayableSender(String sender) {
1477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String displayableSender = address.getName();
1480390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1481390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        if (!TextUtils.isEmpty(displayableSender)) {
1482821e578a71c7015646522e729600618f0ec16fc0Tony Mantler            return Address.decodeAddressPersonal(displayableSender);
1483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1484390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1485390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        // If that fails, default to the sender address.
1486390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        displayableSender = address.getAddress();
1487390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = sender;
1492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return displayableSender;
1494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns only the address portion of a message sender.
1498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getSenderAddress(String sender) {
1500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String tokenizedAddress = address.getAddress();
1503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(tokenizedAddress)) {
1507d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            tokenizedAddress = sender;
1508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return tokenizedAddress;
1510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1512b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static int getNotificationId(final android.accounts.Account account,
1513b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final Folder folder) {
1514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return 1 ^ account.hashCode() ^ folder.hashCode();
1515d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationKey {
1518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Account account;
1519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Folder folder;
1520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public NotificationKey(Account account, Folder folder) {
1522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.account = account;
1523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.folder = folder;
1524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public boolean equals(Object other) {
1528d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!(other instanceof NotificationKey)) {
1529d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return false;
1530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key = (NotificationKey) other;
1532b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            return account.getAccountManagerAccount().equals(key.account.getAccountManagerAccount())
1533b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    && folder.equals(key.folder);
1534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public String toString() {
153826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            return account.getDisplayName() + " " + folder.name;
1539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public int hashCode() {
1543b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int accountHashCode = account.getAccountManagerAccount().hashCode();
1544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int folderHashCode = folder.hashCode();
1545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return accountHashCode ^ folderHashCode;
1546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Contains the logic for converting the contents of one HtmlTree into
1551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * plaintext.
1552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static class MailMessagePlainTextConverter extends HtmlTree.DefaultPlainTextConverter {
1554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings for parsing html message bodies
1555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_NAME = "div";
1556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME = "class";
1557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE = "elided-text";
1558d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HTML.Attribute ELIDED_TEXT_ATTRIBUTE =
1560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new HTML.Attribute(ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME, HTML.Attribute.NO_TYPE);
1561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HtmlDocument.Node ELIDED_TEXT_REPLACEMENT_NODE =
1563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                HtmlDocument.createSelfTerminatingTag(HTML4.BR_ELEMENT, null, null, null);
1564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1565d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private int mEndNodeElidedTextBlock = -1;
1566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void addNode(HtmlDocument.Node n, int nodeNum, int endNum) {
1569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we are in the middle of an elided text block, don't add this node
1570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (nodeNum < mEndNodeElidedTextBlock) {
1571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (nodeNum == mEndNodeElidedTextBlock) {
1573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                super.addNode(ELIDED_TEXT_REPLACEMENT_NODE, nodeNum, endNum);
1574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this tag starts another elided text block, we want to remember the end
1578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (n instanceof HtmlDocument.Tag) {
1579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                boolean foundElidedTextTag = false;
1580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HtmlDocument.Tag htmlTag = (HtmlDocument.Tag)n;
1581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HTML.Element htmlElement = htmlTag.getElement();
1582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (ELIDED_TEXT_ELEMENT_NAME.equals(htmlElement.getName())) {
1583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Make sure that the class is what is expected
1584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final List<HtmlDocument.TagAttribute> attributes =
1585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            htmlTag.getAttributes(ELIDED_TEXT_ATTRIBUTE);
1586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    for (HtmlDocument.TagAttribute attribute : attributes) {
1587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE.equals(
1588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                attribute.getValue())) {
1589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Found an "elided-text" div.  Remember information about this tag
1590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            mEndNodeElidedTextBlock = endNum;
1591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            foundElidedTextTag = true;
1592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            break;
1593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (foundElidedTextTag) {
1598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return;
1599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1602c56b233013cf107c702ef9f61305282670ad804aScott Kennedy            super.addNode(n, nodeNum, endNum);
1603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1604d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * During account setup in Email, we may not have an inbox yet, so the notification setting had
1608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * to be stored in {@link AccountPreferences}. If it is still there, we need to move it to the
1609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link FolderPreferences} now.
1610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void moveNotificationSetting(final AccountPreferences accountPreferences,
1612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences) {
1613d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (accountPreferences.isDefaultInboxNotificationsEnabledSet()) {
1614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this setting has been changed some other way, don't overwrite it
1615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.isNotificationsEnabledSet()) {
1616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean notificationsEnabled =
1617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        accountPreferences.getDefaultInboxNotificationsEnabled();
1618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                folderPreferences.setNotificationsEnabled(notificationsEnabled);
1620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            accountPreferences.clearDefaultInboxNotificationsEnabled();
1623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
1626