NotificationUtils.java revision de2f3f40ab2403b89bebd219dcab4d561fd07387
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.PendingIntent;
20d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentResolver;
21d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentUris;
22d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentValues;
23d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Context;
24d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Intent;
25d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.res.Resources;
26d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.database.Cursor;
27d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.Bitmap;
28d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.BitmapFactory;
29d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.net.Uri;
308913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sappersteinimport android.os.Looper;
31d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract;
32d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract.CommonDataKinds.Email;
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.support.v4.app.NotificationCompat;
34439aa5d7d9226de4682c3697c68c0703b8847518Alan Lauimport android.support.v4.app.NotificationManagerCompat;
35f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sappersteinimport android.support.v4.text.BidiFormatter;
368f10ced86b3c8e7229900d6089f56bf12b026116Andy Huangimport android.support.v4.util.ArrayMap;
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableString;
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableStringBuilder;
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils;
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.CharacterStyle;
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.TextAppearanceSpan;
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.util.Pair;
4361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedyimport android.util.SparseArray;
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
45821e578a71c7015646522e729600618f0ec16fc0Tony Mantlerimport com.android.emailcommon.mail.Address;
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.EmailAddress;
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.R;
494fe0af81874976a1995191321e35c844b2229811Andy Huangimport com.android.mail.analytics.Analytics;
5089bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sappersteinimport com.android.mail.browse.ConversationItemView;
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.MessageCursor;
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.SendersView;
53de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shahimport com.android.mail.photo.ContactFetcher;
54fe23512c2be21d7c87677f5263078021002d89cbScott Kennedyimport com.android.mail.photomanager.LetterTileProvider;
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.AccountPreferences;
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.FolderPreferences;
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.MailPrefs;
58d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Account;
59d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Conversation;
60d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Folder;
61d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Message;
62d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.UIProvider;
63c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedyimport com.android.mail.ui.ImageCanvas.Dimensions;
64d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationAction;
651bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HTML;
661bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HTML4;
671bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HtmlDocument;
681bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HtmlTree;
69ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sappersteinimport com.google.common.base.Objects;
707f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedyimport com.google.common.collect.ImmutableList;
7109400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Lists;
7209400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Sets;
73644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lauimport com.google.common.io.Closeables;
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
75644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lauimport java.io.InputStream;
76644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lauimport java.lang.ref.WeakReference;
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.ArrayList;
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Arrays;
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Collection;
8015490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.HashMap;
8115490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.HashSet;
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.List;
8315490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.Map;
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.concurrent.ConcurrentHashMap;
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class NotificationUtils {
88c7d754703a0b5c2ca4b4dc9675d733881f85b746Scott Kennedy    public static final String LOG_TAG = "NotifUtils";
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
908913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static final String EXTRA_UNREAD_COUNT = "unread-count";
918913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static final String EXTRA_UNSEEN_COUNT = "unseen-count";
928913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static final String EXTRA_GET_ATTENTION = "get-attention";
9310b39450191f95d37e29526ada7a239dcbc858adRohan Shah    private static final int PUBLIC_NOTIFICATIONS_VISIBLE_CHARS = 4;
948913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** Contains a list of <(account, label), unread conversations> */
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static NotificationMap sActiveNotificationMap = null;
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
9861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static final SparseArray<Bitmap> sNotificationIcons = new SparseArray<Bitmap>();
99644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static WeakReference<Bitmap> sDefaultWearableBg = new WeakReference<Bitmap>(null);
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static TextAppearanceSpan sNotificationUnreadStyleSpan;
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharacterStyle sNotificationReadStyleSpan;
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** A factory that produces a plain text converter that removes elided text. */
10577b4c2c31d7601665c337ce5cbc9d84fb9332be8Jin Cao    private static final HtmlTree.ConverterFactory MESSAGE_CONVERTER_FACTORY =
10677b4c2c31d7601665c337ce5cbc9d84fb9332be8Jin Cao            new HtmlTree.ConverterFactory() {
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                @Override
10877b4c2c31d7601665c337ce5cbc9d84fb9332be8Jin Cao                public HtmlTree.Converter<String> createInstance() {
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return new MailMessagePlainTextConverter();
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            };
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
113963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein    private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
114f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein
115e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    // Maps summary notification to conversation notification ids.
116e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    private static Map<NotificationKey, Set<Integer>> sConversationNotificationMap =
11715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            new HashMap<NotificationKey, Set<Integer>>();
11815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears all notifications in response to the user tapping "Clear" in the status bar.
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearAllNotfications(Context context) {
1232f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications.");
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.clear();
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns the notification map, creating it if necessary.
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static synchronized NotificationMap getNotificationMap(Context context) {
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sActiveNotificationMap == null) {
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap = new NotificationMap();
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // populate the map from the cached data
137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap.loadNotificationMap(context);
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return sActiveNotificationMap;
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Class representing the existing notifications, and the number of unread and
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * unseen conversations that triggered each.
145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1462354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee    private static final class NotificationMap {
147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String NOTIFICATION_PART_SEPARATOR = " ";
149d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final int NUM_NOTIFICATION_PARTS= 4;
1502354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        private final ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>> mMap =
1512354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            new ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>>();
152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
1542354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the number of key values pairs in the inner map.
1552354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1562354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public int size() {
1572354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.size();
1582354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1592354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1602354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1612354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns a set of key values.
1622354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1632354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Set<NotificationKey> keySet() {
1642354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.keySet();
1652354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1662354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1672354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1682354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Remove the key from the inner map and return its value.
1692354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1702354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be removed.
1712354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return The value associated with this key.
1722354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1732354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Pair<Integer, Integer> remove(NotificationKey key) {
1742354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.remove(key);
1752354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1762354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1772354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1782354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Clear all key-value pairs in the map.
1792354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1802354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public void clear() {
1812354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.clear();
1822354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1832354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1842354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1852354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Discover if a key-value pair with this key exists.
1862354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1872354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be checked.
1882354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return If a key-value pair with this key exists in the map.
1892354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1902354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public boolean containsKey(NotificationKey key) {
1912354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.containsKey(key);
1922354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1932354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1942354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1952354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread count for the given NotificationKey.
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnread(NotificationKey key) {
1982354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.first : null;
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
2032354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread unseen count for the given NotificationKey.
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnseen(NotificationKey key) {
2062354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.second : null;
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Store the unread and unseen value for the given NotificationKey
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void put(NotificationKey key, int unread, int unseen) {
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value =
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Pair<Integer, Integer>(Integer.valueOf(unread), Integer.valueOf(unseen));
2162354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.put(key, value);
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Populates the notification map with previously cached data.
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void loadNotificationMap(final Context context) {
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = mailPrefs.getActiveNotificationSet();
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (notificationSet != null) {
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                for (String notificationEntry : notificationSet) {
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Get the parts of the string that make the notification entry
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] notificationParts =
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            TextUtils.split(notificationEntry, NOTIFICATION_PART_SEPARATOR);
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationParts.length == NUM_NOTIFICATION_PARTS) {
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri accountUri = Uri.parse(notificationParts[0]);
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor accountCursor = context.getContentResolver().query(
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                accountUri, UIProvider.ACCOUNTS_PROJECTION, null, null, null);
234bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux
235bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                        if (accountCursor == null) {
236bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                            throw new IllegalStateException("Unable to locate account for uri: " +
237bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                                    LogUtils.contentUriToString(accountUri));
238bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                        }
239bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Account account;
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (accountCursor.moveToFirst()) {
2434b0c0127d24e387a4f49d442b63b5c60cedb6922Ray Chen                                account = Account.builder().buildFrom(accountCursor);
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            accountCursor.close();
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri folderUri = Uri.parse(notificationParts[1]);
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor folderCursor = context.getContentResolver().query(
253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folderUri, UIProvider.FOLDERS_PROJECTION, null, null, null);
254bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux
255bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                        if (folderCursor == null) {
256bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                            throw new IllegalStateException("Unable to locate folder for uri: " +
257bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                                    LogUtils.contentUriToString(folderUri));
258bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux                        }
259bf0da35d8b36e49935ca1727bc1c86ff99953c74James Lemieux
260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Folder folder;
261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (folderCursor.moveToFirst()) {
263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folder = new Folder(folderCursor);
264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            folderCursor.close();
269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final NotificationKey key = new NotificationKey(account, folder);
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unreadValue = Integer.valueOf(notificationParts[2]);
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unseenValue = Integer.valueOf(notificationParts[3]);
2742354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                        put(key, unreadValue, unseenValue);
275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Cache the notification map.
282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void saveNotificationMap(Context context) {
284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = Sets.newHashSet();
285bbfc1432baca1a3e4517ea4edc1fb209cbe6997fJay Shrauner            final Set<NotificationKey> keys = keySet();
286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey key : keys) {
287bbfc1432baca1a3e4517ea4edc1fb209cbe6997fJay Shrauner                final Integer unreadCount = getUnread(key);
288bbfc1432baca1a3e4517ea4edc1fb209cbe6997fJay Shrauner                final Integer unseenCount = getUnseen(key);
289ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                if (unreadCount != null && unseenCount != null) {
290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] partValues = new String[] {
291259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            key.account.uri.toString(), key.folder.folderUri.fullUri.toString(),
292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            unreadCount.toString(), unseenCount.toString()};
293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues));
294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            mailPrefs.cacheActiveNotificationSet(notificationSet);
298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return the title of this notification with each account and the number of unread and unseen
303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversations for it. Also remove any account in the map that has 0 unread.
304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String createNotificationString(NotificationMap notifications) {
306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder result = new StringBuilder();
307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int i = 0;
308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keysToRemove = Sets.newHashSet();
309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : notifications.keySet()) {
310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unread = notifications.getUnread(key);
311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unseen = notifications.getUnseen(key);
312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unread == null || unread.intValue() == 0) {
313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                keysToRemove.add(key);
314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (i > 0) result.append(", ");
316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                result.append(key.toString() + " (" + unread + ", " + unseen + ")");
317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : keysToRemove) {
322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notifications.remove(key);
323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return result.toString();
326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts and cancel them.
330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
331d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAllNotifications(Context context) {
3322f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "cancelAllNotifications - cancelling all");
33315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        nm.cancelAll();
335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        clearAllNotfications(context);
336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, cancel them, and repost.
340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
3428913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static void cancelAndResendNotificationsOnLocaleChange(
343de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            Context context, final ContactFetcher contactFetcher) {
344963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        LogUtils.d(LOG_TAG, "cancelAndResendNotificationsOnLocaleChange");
345963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        sBidiFormatter = BidiFormatter.getInstance();
346de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        resendNotifications(context, true, null, null, contactFetcher);
347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, optionally cancel them, and repost.
351ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * This happens when locale changes. If you only want to resend messages from one
352ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * account-folder pair, pass in the account and folder that should be resent.
353ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All other account-folder pairs will not have their notifications resent.
354ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All notifications will be resent if account or folder is null.
355ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *
356ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param context Current context.
357ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param cancelExisting True, if all notifications should be canceled before resending.
358ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                       False, otherwise.
359ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param accountUri The {@link Uri} of the {@link Account} of the notification
360ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                   upon which an action occurred.
361ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param folderUri The {@link Uri} of the {@link Folder} of the notification
362ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                  upon which an action occurred.
363ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     */
364ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein    public static void resendNotifications(Context context, final boolean cancelExisting,
3658913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final Uri accountUri, final FolderUri folderUri,
366de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            final ContactFetcher contactFetcher) {
3672f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "resendNotifications ");
3682638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cancelExisting) {
3702f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - cancelling all");
37115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            nm.cancelAll();
373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Re-validate the notifications.
375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<NotificationKey> keys = notificationMap.keySet();
377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey notification : keys) {
378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder = notification.folder;
379b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
380b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(notification.account.getAccountManagerAccount(), folder);
381ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein
382ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // Only resend notifications if the notifications are from the same folder
383ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // and same account as the undo notification that was previously displayed.
38426b29ef558cce109c51584081c52781760a02dd9Scott Kennedy            if (accountUri != null && !Objects.equal(accountUri, notification.account.uri) &&
385259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folderUri != null && !Objects.equal(folderUri, folder.folderUri)) {
3862f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "resendNotifications - not resending %s / %s"
3872638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                        + " because it doesn't match %s / %s",
388259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                        notification.account.uri, folder.folderUri, accountUri, folderUri);
389ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                continue;
390ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            }
391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
3922f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - resending %s / %s",
393259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    notification.account.uri, folder.folderUri);
3942638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationAction undoableAction =
396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sUndoNotifications.get(notificationId);
397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (undoableAction == null) {
398ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                validateNotifications(context, folder, notification.account, true,
399de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        false, notification, contactFetcher);
400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Create an undo notification
402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.createUndoNotification(context, undoableAction);
403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications for the specified account.
409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
410fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook    public static void validateAccountNotifications(Context context, Account account) {
411fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook        final String email = account.getEmailAddress();
412fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook        LogUtils.d(LOG_TAG, "validateAccountNotifications - %s", email);
4134162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        List<NotificationKey> notificationsToCancel = Lists.newArrayList();
415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Iterate through the notification map to see if there are any entries that correspond to
416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // labels that are not in the sync set.
417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keys = notificationMap.keySet();
419fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook        final AccountPreferences accountPreferences = new AccountPreferences(context,
420fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                account.getAccountId());
421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean enabled = accountPreferences.areNotificationsEnabled();
422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (!enabled) {
423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Cancel all notifications for this account
424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
425fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                if (notification.account.getAccountManagerAccount().name.equals(email)) {
426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationsToCancel.add(notification);
427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Iterate through the notification map to see if there are any entries that
431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // correspond to labels that are not in the notification set.
432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
433fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                if (notification.account.getAccountManagerAccount().name.equals(email)) {
434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // If notification is not enabled for this label, remember this NotificationKey
435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // to later cancel the notification, and remove the entry from the map
436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Folder folder = notification.folder;
437259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    final boolean isInbox = folder.folderUri.equals(
438259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            notification.account.settings.defaultInbox);
439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final FolderPreferences folderPreferences = new FolderPreferences(
440fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                            context, notification.account.getAccountId(), folder, isInbox);
441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!folderPreferences.areNotificationsEnabled()) {
443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationsToCancel.add(notification);
444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Cancel & remove the invalid notifications.
450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationsToCancel.size() > 0) {
451b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : notificationsToCancel) {
453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Folder folder = notification.folder;
454b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                final int notificationId =
455b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                        getNotificationId(notification.account.getAccountManagerAccount(), folder);
4562f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "validateAccountNotifications - cancelling %s / %s",
45726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        notification.account.getEmailAddress(), folder.persistentId);
458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notificationMap.remove(notification);
460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sUndoNotifications.remove(notificationId);
461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
462b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau
463e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                cancelConversationNotifications(notification, nm);
464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.saveNotificationMap(context);
466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
4698913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static void sendSetNewEmailIndicatorIntent(Context context, final int unreadCount,
4708913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final int unseenCount, final Account account, final Folder folder,
4718913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final boolean getAttention) {
4728913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        LogUtils.i(LOG_TAG, "sendSetNewEmailIndicator account: %s, folder: %s",
4738913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
4748913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                LogUtils.sanitizeName(LOG_TAG, folder.name));
4758913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
4768913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final Intent intent = new Intent(MailIntentService.ACTION_SEND_SET_NEW_EMAIL_INDICATOR);
4778913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.setPackage(context.getPackageName()); // Make sure we only deliver this to ourselves
4788913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(EXTRA_UNREAD_COUNT, unreadCount);
4798913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(EXTRA_UNSEEN_COUNT, unseenCount);
4808913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(Utils.EXTRA_ACCOUNT, account);
4818913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(Utils.EXTRA_FOLDER, folder);
4828913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(EXTRA_GET_ATTENTION, getAttention);
4838913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        context.startService(intent);
4848913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    }
4858913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
4878913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein     * Display only one notification. Should only be called from
4888913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein     * {@link com.android.mail.MailIntentService}. Use {@link #sendSetNewEmailIndicatorIntent}
4898913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein     * if you need to perform this action anywhere else.
490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void setNewEmailIndicator(Context context, final int unreadCount,
492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int unseenCount, final Account account, final Folder folder,
493de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            final boolean getAttention, final ContactFetcher contactFetcher) {
4942f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "setNewEmailIndicator unreadCount = %d, unseenCount = %d, account = %s,"
49526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                + " folder = %s, getAttention = %b", unreadCount, unseenCount,
49626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                account.getEmailAddress(), folder.folderUri, getAttention);
4974162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean ignoreUnobtrusiveSetting = false;
499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
500b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        final int notificationId = getNotificationId(account.getAccountManagerAccount(), folder);
501dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy
502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Update the notification map
503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount == 0) {
50626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            LogUtils.d(LOG_TAG, "setNewEmailIndicator - cancelling %s / %s",
50726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    account.getEmailAddress(), folder.persistentId);
508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.remove(key);
509b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau
510b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
511b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau            nm.cancel(notificationId);
512e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            cancelConversationNotifications(key, nm);
513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
514b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook            LogUtils.d(LOG_TAG, "setNewEmailIndicator - update count for: %s / %s " +
51526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    "to: unread: %d unseen %d", account.getEmailAddress(), folder.persistentId,
516b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    unreadCount, unseenCount);
517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!notificationMap.containsKey(key)) {
518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This account previously didn't have any unread mail; ignore the "unobtrusive
519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notifications" setting and play sound and/or vibrate the device even if a
520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notification already exists (bug 2412348).
521b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "setNewEmailIndicator - ignoringUnobtrusiveSetting");
522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                ignoreUnobtrusiveSetting = true;
523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.put(key, unreadCount, unseenCount);
525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
528d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
5292f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.v(LOG_TAG, "New email: %s mapSize: %d getAttention: %b",
530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    createNotificationString(notificationMap), notificationMap.size(),
531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    getAttention);
532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (NotificationActionUtils.sUndoNotifications.get(notificationId) == null) {
535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            validateNotifications(context, folder, account, getAttention, ignoreUnobtrusiveSetting,
536de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    key, contactFetcher);
537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
538d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications notification.
542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void validateNotifications(Context context, final Folder folder,
544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, boolean getAttention, boolean ignoreUnobtrusiveSetting,
545de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            NotificationKey key, final ContactFetcher contactFetcher) {
546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
54715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
55132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification: %s mapSize: %d "
552b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "folder: %s getAttention: %b ignoreUnobtrusive: %b",
553b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    createNotificationString(notificationMap),
554b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    notificationMap.size(), folder.name, getAttention, ignoreUnobtrusiveSetting);
55532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        } else {
55632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification, mapSize: %d "
557b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "getAttention: %b ignoreUnobtrusive: %b", notificationMap.size(),
558b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    getAttention, ignoreUnobtrusiveSetting);
559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // The number of unread messages for this account and label.
561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unread = notificationMap.getUnread(key);
562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int unreadCount = unread != null ? unread.intValue() : 0;
563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unseen = notificationMap.getUnseen(key);
564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int unseenCount = unseen != null ? unseen.intValue() : 0;
565d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor cursor = null;
567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Uri.Builder uriBuilder = folder.conversationListUri.buildUpon();
570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            uriBuilder.appendQueryParameter(
571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.SEEN_QUERY_PARAMETER, Boolean.FALSE.toString());
5720be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // Do not allow this quick check to disrupt any active network-enabled conversation
5730be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // cursor.
5740be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            uriBuilder.appendQueryParameter(
5750be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    UIProvider.ConversationListQueryParameters.USE_NETWORK,
5760be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    Boolean.FALSE.toString());
577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor = context.getContentResolver().query(uriBuilder.build(),
578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.CONVERSATION_PROJECTION, null, null, null);
5793a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            if (cursor == null) {
5803a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                // This folder doesn't exist.
58132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
58232fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        "The cursor is null, so the specified folder probably does not exist");
5833a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                clearFolderNotification(context, account, folder, false);
5843a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                return;
5853a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            }
586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int cursorUnseenCount = cursor.getCount();
587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Make sure the unseen count matches the number of items in the cursor.  But, we don't
589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // want to overwrite a 0 unseen count that was specified in the intent
590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount != 0 && unseenCount != cursorUnseenCount) {
59132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
5922f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        "Unseen count doesn't match cursor count.  unseen: %d cursor count: %d",
593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        unseenCount, cursorUnseenCount);
594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = cursorUnseenCount;
595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For the purpose of the notifications, the unseen count should be capped at the num of
598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // unread conversations.
599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount > unreadCount) {
600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = unreadCount;
601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
602d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
603b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
604b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(account.getAccountManagerAccount(), folder);
605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
60615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationKey notificationKey = new NotificationKey(account, folder);
60715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount == 0) {
60932fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "validateNotifications - cancelling account %s / folder %s",
61026a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
61132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        LogUtils.sanitizeName(LOG_TAG, folder.persistentId));
612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
613e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                cancelConversationNotifications(notificationKey, nm);
61415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // We now have all we need to create the notification and the pending intent
6194e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux            PendingIntent clickIntent = null;
620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
622439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            NotificationCompat.WearableExtender wearableExtender =
623439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    new NotificationCompat.WearableExtender();
624c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            Map<Integer, NotificationBuilders> msgNotifications =
625c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                    new ArrayMap<Integer, NotificationBuilders>();
626ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah
627ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah            if (com.android.mail.utils.Utils.isRunningLOrLater()) {
628ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah                notification.setColor(
629160f8a7166daa4e300157527bc3e65ffcd7a0c1cAndrew Sapperstein                        context.getResources().getColor(R.color.notification_icon_color));
630ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah            }
63110b39450191f95d37e29526ada7a239dcbc858adRohan Shah
63210b39450191f95d37e29526ada7a239dcbc858adRohan Shah            if(unseenCount > 1) {
63310b39450191f95d37e29526ada7a239dcbc858adRohan Shah                notification.setSmallIcon(R.drawable.ic_notification_multiple_mail_24dp);
63410b39450191f95d37e29526ada7a239dcbc858adRohan Shah            } else {
63510b39450191f95d37e29526ada7a239dcbc858adRohan Shah                notification.setSmallIcon(R.drawable.ic_notification_mail_24dp);
63610b39450191f95d37e29526ada7a239dcbc858adRohan Shah            }
63726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            notification.setTicker(account.getDisplayName());
6384e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux            notification.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
63976fb85a30a41254537c0ad26cabec06030fcdbbaPaul Westbrook            notification.setCategory(NotificationCompat.CATEGORY_EMAIL);
640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long when;
642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
643d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long oldWhen =
644d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sNotificationTimestamps.get(notificationId);
645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (oldWhen != 0) {
646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = oldWhen;
647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = System.currentTimeMillis();
649d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
651d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setWhen(when);
652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The timestamp is now stored in the notification, so we can remove it from here
654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Dispatch a CLEAR_NEW_MAIL_NOTIFICATIONS intent if the user taps the "X" next to a
657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // notification.  Also this intent gets fired when the user taps on a notification as
658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the AutoCancel flag has been set
659d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Intent cancelNotificationIntent =
660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.setPackage(context.getPackageName());
66209400efa442422299acf21abe20e3470f9d965abScott Kennedy            cancelNotificationIntent.setData(Utils.appendVersionQueryParameter(context,
663259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri));
66448cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
66548cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder);
666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setDeleteIntent(PendingIntent.getService(
668d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, notificationId, cancelNotificationIntent, 0));
669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
670d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Ensure that the notification is cleared when the user selects it
671d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setAutoCancel(true);
672d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
673d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean eventInfoConfigured = false;
674d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
675259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            final boolean isInbox = folder.folderUri.equals(account.settings.defaultInbox);
676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences =
677fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                    new FolderPreferences(context, account.getAccountId(), folder, isInbox);
678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (isInbox) {
680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
681fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                        new AccountPreferences(context, account.getAccountId());
682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                moveNotificationSetting(accountPreferences, folderPreferences);
683d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
684d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.areNotificationsEnabled()) {
68632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "Notifications are disabled for this folder; not notifying");
687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Don't notify
688d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
689d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unreadCount > 0) {
692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // How can I order this properly?
693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor.moveToNext()) {
694bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    final Intent notificationIntent;
695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
696bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    // Launch directly to the conversation, if there is only 1 unseen conversation
6974fe0af81874976a1995191321e35c844b2229811Andy Huang                    final boolean launchConversationMode = (unseenCount == 1);
6984fe0af81874976a1995191321e35c844b2229811Andy Huang                    if (launchConversationMode) {
69909400efa442422299acf21abe20e3470f9d965abScott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
70009400efa442422299acf21abe20e3470f9d965abScott Kennedy                                cursor);
701bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    } else {
702bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
703bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                                null);
704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
7064fe0af81874976a1995191321e35c844b2229811Andy Huang                    Analytics.getInstance().sendEvent("notification_create",
7074fe0af81874976a1995191321e35c844b2229811Andy Huang                            launchConversationMode ? "conversation" : "conversation_list",
7084fe0af81874976a1995191321e35c844b2229811Andy Huang                            folder.getTypeDescription(), unseenCount);
7094fe0af81874976a1995191321e35c844b2229811Andy Huang
710d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationIntent == null) {
7112f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        LogUtils.e(LOG_TAG, "Null intent when building notification");
712d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return;
713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
71505a18446a96fa8ea2ae077b482499abf67366e32Alan Lau                    clickIntent = createClickPendingIntent(context, notificationIntent);
7164fe0af81874976a1995191321e35c844b2229811Andy Huang
717d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    configureLatestEventInfoFromConversation(context, account, folderPreferences,
718439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                            notification, wearableExtender, msgNotifications, notificationId,
71915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            cursor, clickIntent, notificationIntent, unreadCount, unseenCount,
720de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                            folder, when, contactFetcher);
721d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    eventInfoConfigured = true;
722d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
723d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
724d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
725d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
726d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
727d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean notifyOnce = !folderPreferences.isEveryMessageNotificationEnabled();
728d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
729ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (!ignoreUnobtrusiveSetting && notifyOnce) {
730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // If the user has "unobtrusive notifications" enabled, only alert the first time
731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // new mail is received in this account.  This is the default behavior.  See
732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // bugs 2412348 and 2413490.
733b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "Setting Alert Once");
734d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setOnlyAlertOnce(true);
735d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
73732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Account: %s vibrate: %s",
73826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
739ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                    Boolean.toString(folderPreferences.isNotificationVibrateEnabled()));
740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
741d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int defaults = 0;
742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
743e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            // Check if any current conversation notifications exist previously.  Only notify if
744e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            // one of them is new.
745e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            boolean hasNewConversationNotification;
746e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            Set<Integer> prevConversationNotifications =
747e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    sConversationNotificationMap.get(notificationKey);
748e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            if (prevConversationNotifications != null) {
749e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                hasNewConversationNotification = false;
75015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                for (Integer currentNotificationId : msgNotifications.keySet()) {
751e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    if (!prevConversationNotifications.contains(currentNotificationId)) {
752e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                        hasNewConversationNotification = true;
75315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        break;
75415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    }
75515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
75615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            } else {
757e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                hasNewConversationNotification = true;
75815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
75915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
760e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            LogUtils.d(LOG_TAG, "getAttention=%s,oldWhen=%s,hasNewConversationNotification=%s",
761e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    getAttention, oldWhen, hasNewConversationNotification);
76215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
763d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            /*
764d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * We do not want to notify if this is coming back from an Undo notification, hence the
765d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * oldWhen check.
766d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             */
767e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            if (getAttention && oldWhen == 0 && hasNewConversationNotification) {
768d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
769fd79237327f6803675cad2a64a74411ebda854e3Paul Westbrook                        new AccountPreferences(context, account.getAccountId());
770d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (accountPreferences.areNotificationsEnabled()) {
771d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (vibrate) {
772d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        defaults |= Notification.DEFAULT_VIBRATE;
773d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
774d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
775d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
776d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            : Uri.parse(ringtoneUri));
77732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                    LogUtils.i(LOG_TAG, "New email in %s vibrateWhen: %s, playing notification: %s",
77826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()), vibrate,
77926a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            ringtoneUri);
780d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
781d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
782d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
78332fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            // TODO(skennedy) Why do we do any of the above if we're just going to bail here?
784d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (eventInfoConfigured) {
785d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                defaults |= Notification.DEFAULT_LIGHTS;
786d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setDefaults(defaults);
787d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
788d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (oldWhen != 0) {
789d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // We do not want to display the ticker again if we are re-displaying this
790d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notification (like from an Undo notification)
791d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setTicker(null);
792d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
793d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
794439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                notification.extend(wearableExtender);
7954e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux
7964e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                // create the *public* form of the *private* notification we have been assembling
7974e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                final Notification publicNotification = createPublicNotification(context, account,
7984e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                        folder, when, unseenCount, unreadCount, clickIntent);
799c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah
8004e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                notification.setPublicVersion(publicNotification);
8014e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux
802c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                nm.notify(notificationId, notification.build());
80315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
804e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                if (prevConversationNotifications != null) {
80515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    Set<Integer> currentNotificationIds = msgNotifications.keySet();
806e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    for (Integer prevConversationNotificationId : prevConversationNotifications) {
807e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                        if (!currentNotificationIds.contains(prevConversationNotificationId)) {
808e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            nm.cancel(prevConversationNotificationId);
809e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            LogUtils.d(LOG_TAG, "canceling conversation notification %s",
810e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    prevConversationNotificationId);
81115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        }
81215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    }
81315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
81415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
815c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                for (Map.Entry<Integer, NotificationBuilders> entry : msgNotifications.entrySet()) {
816c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                    NotificationBuilders builders = entry.getValue();
817439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    builders.notifBuilder.extend(builders.wearableNotifBuilder);
818c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                    nm.notify(entry.getKey(), builders.notifBuilder.build());
819e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    LogUtils.d(LOG_TAG, "notifying conversation notification %s", entry.getKey());
82015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
82115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
822e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                Set<Integer> conversationNotificationIds = new HashSet<Integer>();
823e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                conversationNotificationIds.addAll(msgNotifications.keySet());
824e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                sConversationNotificationMap.put(notificationKey, conversationNotificationIds);
82532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            } else {
82632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "event info not configured - not notifying");
827d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (cursor != null) {
830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                cursor.close();
831d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
8354e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    /**
8364e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * Build and return a redacted form of a notification using the given information. This redacted
8374e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * form is shown above the lock screen and is devoid of sensitive information.
8384e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     *
8394e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param context a context used to construct the notification
8404e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param account the account for which the notification is being generated
8414e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param folder the folder for which the notification is being generated
8424e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param when the timestamp of the notification
8434e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param unseenCount the number of unseen messages
8444e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param unreadCount the number of unread messages
8454e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param clickIntent the behavior to invoke if the notification is tapped (note that the user
8464e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     *                    will be prompted to unlock the device before the behavior is executed)
8474e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @return the redacted form of the notification to display above the lock screen
8484e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     */
8494e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    private static Notification createPublicNotification(Context context, Account account,
8504e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux            Folder folder, long when, int unseenCount, int unreadCount, PendingIntent clickIntent) {
8514e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        final boolean multipleUnseen = unseenCount > 1;
85210b39450191f95d37e29526ada7a239dcbc858adRohan Shah
8534e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
8544e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                .setContentTitle(createTitle(context, unseenCount))
8554d27504d0f328312f6614cb6c535c683c6a36e08Rohan Shah                .setContentText(account.getDisplayName())
8564e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                .setContentIntent(clickIntent)
8574e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                .setNumber(unreadCount)
8584e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
85976fb85a30a41254537c0ad26cabec06030fcdbbaPaul Westbrook                .setCategory(NotificationCompat.CATEGORY_EMAIL)
8604e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                .setWhen(when);
8614e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux
862ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah        if (com.android.mail.utils.Utils.isRunningLOrLater()) {
863160f8a7166daa4e300157527bc3e65ffcd7a0c1cAndrew Sapperstein            builder.setColor(context.getResources().getColor(R.color.notification_icon_color));
864ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah        }
865ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah
8664e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        // if this public notification summarizes multiple single notifications, mark it as the
8674e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        // summary notification and generate the same group key as the single notifications
8684e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        if (multipleUnseen) {
8694e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux            builder.setGroup(createGroupKey(account, folder));
8704e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux            builder.setGroupSummary(true);
87110b39450191f95d37e29526ada7a239dcbc858adRohan Shah            builder.setSmallIcon(R.drawable.ic_notification_multiple_mail_24dp);
87210b39450191f95d37e29526ada7a239dcbc858adRohan Shah        } else {
87310b39450191f95d37e29526ada7a239dcbc858adRohan Shah            builder.setSmallIcon(R.drawable.ic_notification_mail_24dp);
8744e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        }
8754e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux
87610b39450191f95d37e29526ada7a239dcbc858adRohan Shah        return builder.build();
87710b39450191f95d37e29526ada7a239dcbc858adRohan Shah    }
878c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah
87910b39450191f95d37e29526ada7a239dcbc858adRohan Shah    /**
8804e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param account the account in which the unread email resides
8814e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param folder the folder in which the unread email resides
8824e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @return a key that groups notifications with common accounts and folders
8834e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     */
8844e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    private static String createGroupKey(Account account, Folder folder) {
8854e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        return account.uri.toString() + "/" + folder.folderUri.fullUri;
8864e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    }
8874e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux
8884e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    /**
8894e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param context a context used to construct the title
8904e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @param unseenCount the number of unseen messages
8914e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     * @return e.g. "1 new message" or "2 new messages"
8924e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux     */
8934e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    private static String createTitle(Context context, int unseenCount) {
8944e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        final Resources resources = context.getResources();
8954e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux        return resources.getQuantityString(R.plurals.new_messages, unseenCount, unseenCount);
8964e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux    }
8974e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux
89805a18446a96fa8ea2ae077b482499abf67366e32Alan Lau    private static PendingIntent createClickPendingIntent(Context context,
89905a18446a96fa8ea2ae077b482499abf67366e32Alan Lau            Intent notificationIntent) {
90005a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // Amend the click intent with a hint that its source was a notification,
90105a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // but remove the hint before it's used to generate notification action
90205a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // intents. This prevents the following sequence:
90305a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // 1. generate single notification
90405a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // 2. user clicks reply, then completes Compose activity
90505a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // 3. main activity launches, gets FROM_NOTIFICATION hint in intent
90605a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        notificationIntent.putExtra(Utils.EXTRA_FROM_NOTIFICATION, true);
90705a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        PendingIntent clickIntent = PendingIntent.getActivity(context, -1, notificationIntent,
90805a18446a96fa8ea2ae077b482499abf67366e32Alan Lau                PendingIntent.FLAG_UPDATE_CURRENT);
90905a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        notificationIntent.removeExtra(Utils.EXTRA_FROM_NOTIFICATION);
91005a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        return clickIntent;
91105a18446a96fa8ea2ae077b482499abf67366e32Alan Lau    }
91205a18446a96fa8ea2ae077b482499abf67366e32Alan Lau
913d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
914d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return an {@link Intent} which, if launched, will display the corresponding conversation
915d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
91609400efa442422299acf21abe20e3470f9d965abScott Kennedy    private static Intent createViewConversationIntent(final Context context, final Account account,
91709400efa442422299acf21abe20e3470f9d965abScott Kennedy            final Folder folder, final Cursor cursor) {
918d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (folder == null || account == null) {
9192f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.e(LOG_TAG, "createViewConversationIntent(): "
92013a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    + "Null account or folder.  account: %s folder: %s", account, folder);
921d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
924d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Intent intent;
925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cursor == null) {
927259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewFolderIntent(context, folder.folderUri.fullUri, account);
928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // A conversation cursor has been specified, so this intent is intended to be go
930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // directly to the one new conversation
931d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
932d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the Conversation object
933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
934259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewConversationIntent(context, conversation,
935259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri, account);
936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
937d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
938d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return intent;
939d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
940d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
94161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getIcon(final Context context, final int resId) {
94261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap cachedIcon = sNotificationIcons.get(resId);
94361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (cachedIcon != null) {
94461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            return cachedIcon;
94561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        }
94661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
94761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resId);
94861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        sNotificationIcons.put(resId, icon);
94961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
95061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        return icon;
95161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    }
95261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
953644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static Bitmap getDefaultWearableBg(Context context) {
954644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        Bitmap bg = sDefaultWearableBg.get();
955644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        if (bg == null) {
956644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            bg = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_email);
957644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            sDefaultWearableBg = new WeakReference<Bitmap>(bg);
958644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        }
959644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        return bg;
960644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    }
961644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
962d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void configureLatestEventInfoFromConversation(final Context context,
963d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, final FolderPreferences folderPreferences,
964de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            final NotificationCompat.Builder notificationBuilder,
965439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            final NotificationCompat.WearableExtender wearableExtender,
966c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            final Map<Integer, NotificationBuilders> msgNotifications,
96715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final int summaryNotificationId, final Cursor conversationCursor,
968d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final PendingIntent clickIntent, final Intent notificationIntent,
969b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int unreadCount, final int unseenCount,
970de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            final Folder folder, final long when, final ContactFetcher contactFetcher) {
971d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Resources res = context.getResources();
97226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountDisplayName = account.getDisplayName();
97326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountEmail = account.getEmailAddress();
974c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah        final boolean multipleUnseen = unseenCount > 1;
975d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
97632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        LogUtils.i(LOG_TAG, "Showing notification with unreadCount of %d and unseenCount of %d",
9772f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                unreadCount, unseenCount);
978d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String notificationTicker = null;
980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
981d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Boolean indicating that this notification is for a non-inbox label.
982259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final boolean isInbox = folder.folderUri.fullUri.equals(account.settings.defaultInbox);
983d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
984d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Notification label name for user label notifications.
985d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String notificationLabelName = isInbox ? null : folder.name;
986d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
987c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah        if (multipleUnseen) {
988d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Build the string that describes the number of new messages
9894e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux            final String newMessagesString = createTitle(context, unseenCount);
990d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
991d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The ticker initially start as the new messages string.
992d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = newMessagesString;
993d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
994d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The title of the notification is the new messages string
995de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            notificationBuilder.setContentTitle(newMessagesString);
996d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
997c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy            // TODO(skennedy) Can we remove this check?
998d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (com.android.mail.utils.Utils.isRunningJellybeanOrLater()) {
999d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // For a new-style notification
1000d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int maxNumDigestItems = context.getResources().getInteger(
1001d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        R.integer.max_num_notification_digest_items);
1002d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1003d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
1004de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setSubText(
100526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
1006d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1007d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final NotificationCompat.InboxStyle digest =
1008de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        new NotificationCompat.InboxStyle(notificationBuilder);
1009d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
10104e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                // Group by account and folder
10114e75cc92043d9d37aa3bda16ce6b0034b64ed329James Lemieux                final String notificationGroupKey = createGroupKey(account, folder);
1012de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                // Track all senders to later tag them along with the digest notification
1013de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                final HashSet<String> sendersList = new HashSet<String>();
1014de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setGroup(notificationGroupKey).setGroupSummary(true);
101515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1016644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                ConfigResult firstResult = null;
1017d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int numDigestItems = 0;
1018d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                do {
1019d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Conversation conversation = new Conversation(conversationCursor);
1020d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1021d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!conversation.read) {
1022d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        boolean multipleUnreadThread = false;
1023d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // TODO(cwren) extract this pattern into a helper
1024d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1025d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Cursor cursor = null;
1026d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        MessageCursor messageCursor = null;
1027d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
1028d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final Uri.Builder uriBuilder = conversation.messageListUri.buildUpon();
1029d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            uriBuilder.appendQueryParameter(
1030d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.LABEL_QUERY_PARAMETER, notificationLabelName);
1031d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor = context.getContentResolver().query(uriBuilder.build(),
1032d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.MESSAGE_PROJECTION, null, null, null);
1033d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            messageCursor = new MessageCursor(cursor);
1034d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1035b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                            String from = "";
1036d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            String fromAddress = "";
1037d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
1038d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
1039d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                fromAddress = message.getFrom();
1040b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                if (fromAddress == null) {
1041b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                    fromAddress = "";
1042b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                }
1043d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                from = getDisplayableSender(fromAddress);
1044de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                                sendersList.add(getSenderAddress(fromAddress));
1045d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1046d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
1047d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
1048d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (!message.read &&
1049d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        !fromAddress.contentEquals(message.getFrom())) {
1050d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    multipleUnreadThread = true;
1051de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                                    sendersList.add(getSenderAddress(message.getFrom()));
1052d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1053d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1054d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final SpannableStringBuilder sendersBuilder;
1055d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (multipleUnreadThread) {
1056d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final int sendersLength =
1057d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        res.getInteger(R.integer.swipe_senders_length);
1058d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1059d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = getStyledSenders(context, conversationCursor,
106026a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                                        sendersLength, notificationAccountEmail);
1061d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
106212fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                sendersBuilder =
106312fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                        new SpannableStringBuilder(getWrappedFromString(from));
1064d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1065d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final CharSequence digestLine = getSingleMessageInboxLine(context,
1066d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    sendersBuilder.toString(),
106789bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                                    ConversationItemView.filterTag(context, conversation.subject),
1068edd6c1a2807d2ade930dfd4622707298dc470d64Tony Mantler                                    conversation.getSnippet());
1069d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            digest.addLine(digestLine);
1070d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            numDigestItems++;
107115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1072e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            // Adding conversation notification for Wear.
1073e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            NotificationCompat.Builder conversationNotif =
107415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    new NotificationCompat.Builder(context);
107576fb85a30a41254537c0ad26cabec06030fcdbbaPaul Westbrook                            conversationNotif.setCategory(NotificationCompat.CATEGORY_EMAIL);
1076c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah
107710b39450191f95d37e29526ada7a239dcbc858adRohan Shah                            conversationNotif.setSmallIcon(
107810b39450191f95d37e29526ada7a239dcbc858adRohan Shah                                    R.drawable.ic_notification_multiple_mail_24dp);
1079c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah
1080ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah                            if (com.android.mail.utils.Utils.isRunningLOrLater()) {
1081ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah                                conversationNotif.setColor(
1082ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah                                        context.getResources()
1083160f8a7166daa4e300157527bc3e65ffcd7a0c1cAndrew Sapperstein                                                .getColor(R.color.notification_icon_color));
1084ccec014e1f11770dd406c2421fdb48a6eccad86aRohan Shah                            }
1085e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setContentText(digestLine);
1086e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            Intent conversationNotificationIntent = createViewConversationIntent(
1087e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    context, account, folder, conversationCursor);
1088e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            PendingIntent conversationClickIntent = createClickPendingIntent(
1089e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    context, conversationNotificationIntent);
1090e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setContentIntent(conversationClickIntent);
1091e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setAutoCancel(true);
1092e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau
1093c92dc65f64e8b09ee78ba2fa799d7d3627373913Alan Lau                            // Conversations are sorted in descending order, but notification sort
1094c92dc65f64e8b09ee78ba2fa799d7d3627373913Alan Lau                            // key is in ascending order.  Invert the order key to get the right
1095c92dc65f64e8b09ee78ba2fa799d7d3627373913Alan Lau                            // order.  Left pad 19 zeros because it's a long.
1096c92dc65f64e8b09ee78ba2fa799d7d3627373913Alan Lau                            String groupSortKey = String.format("%019d",
1097c92dc65f64e8b09ee78ba2fa799d7d3627373913Alan Lau                                    (Long.MAX_VALUE - conversation.orderKey));
1098e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setGroup(notificationGroupKey);
1099e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setSortKey(groupSortKey);
110046ec24391831ee15185dccc8fb1220529f767694Alan Lau                            conversationNotif.setWhen(conversation.dateMs);
1101439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau
1102e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            int conversationNotificationId = getNotificationId(
1103e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    summaryNotificationId, conversation.hashCode());
110415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1105e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            final NotificationCompat.WearableExtender conversationWearExtender =
1106439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                                    new NotificationCompat.WearableExtender();
11078913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                            final ConfigResult result =
11088913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                                    configureNotifForOneConversation(context, account,
1109e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    folderPreferences, conversationNotif, conversationWearExtender,
1110644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                    conversationCursor, notificationIntent, folder, when, res,
111115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    notificationAccountDisplayName, notificationAccountEmail,
1112e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    isInbox, notificationLabelName, conversationNotificationId,
1113de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                                    contactFetcher);
1114e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            msgNotifications.put(conversationNotificationId,
1115e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    NotificationBuilders.of(conversationNotif,
1116e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                            conversationWearExtender));
1117644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1118644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            if (firstResult == null) {
1119644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                firstResult = result;
1120644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            }
1121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
1122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor != null) {
1123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                messageCursor.close();
1124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (cursor != null) {
1126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                cursor.close();
1127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } while (numDigestItems <= maxNumDigestItems && conversationCursor.moveToNext());
1131644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1132de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                // Tag main digest notification with the senders
1133de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                tagNotificationsWithPeople(context, notificationBuilder, sendersList,
1134de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        account.getAccountManagerAccount().name, contactFetcher);
1135de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1136644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                if (firstResult != null && firstResult.contactIconInfo != null) {
1137439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    wearableExtender.setBackground(firstResult.contactIconInfo.wearableBg);
1138644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                } else {
1139644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                    LogUtils.w(LOG_TAG, "First contact icon is null!");
1140439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    wearableExtender.setBackground(getDefaultWearableBg(context));
1141644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                }
1142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
1144de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setContentText(
114526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
1146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
114815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // For notifications for a single new conversation, we want to get the information
114915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // from the conversation
1150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Move the cursor to the most recent unread conversation
1152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            seekToLatestUnreadConversation(conversationCursor);
1153d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
11548913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final ConfigResult result = configureNotifForOneConversation(context, account,
1155de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    folderPreferences, notificationBuilder, wearableExtender, conversationCursor,
115615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationIntent, folder, when, res, notificationAccountDisplayName,
115715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationAccountEmail, isInbox, notificationLabelName,
1158de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    summaryNotificationId, contactFetcher);
1159644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            notificationTicker = result.notificationTicker;
1160644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
116185003dc5fe29370cd3dfe6a2551b04284bfef2b7Jay Shrauner            if (result.contactIconInfo != null) {
116285003dc5fe29370cd3dfe6a2551b04284bfef2b7Jay Shrauner                wearableExtender.setBackground(result.contactIconInfo.wearableBg);
116385003dc5fe29370cd3dfe6a2551b04284bfef2b7Jay Shrauner            } else {
116485003dc5fe29370cd3dfe6a2551b04284bfef2b7Jay Shrauner                wearableExtender.setBackground(getDefaultWearableBg(context));
116585003dc5fe29370cd3dfe6a2551b04284bfef2b7Jay Shrauner            }
116615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
116815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        // Build the notification ticker
116915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (notificationLabelName != null && notificationTicker != null) {
117015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // This is a per label notification, format the ticker with that information
117115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notificationTicker = res.getString(R.string.label_notification_ticker,
117215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationLabelName, notificationTicker);
117315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
117515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (notificationTicker != null) {
117615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // If we didn't generate a notification ticker, it will default to account name
1177de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            notificationBuilder.setTicker(notificationTicker);
117815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
117915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
118015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        // Set the number in the notification
118115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (unreadCount > 1) {
1182de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            notificationBuilder.setNumber(unreadCount);
118315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1185de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        notificationBuilder.setContentIntent(clickIntent);
118615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
1187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
118815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    /**
118915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     * Configure the notification for one conversation.  When there are multiple conversations,
119015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     * this method is used to configure bundled notification for Android Wear.
119115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     */
1192644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static ConfigResult configureNotifForOneConversation(Context context,
1193644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            Account account, FolderPreferences folderPreferences,
1194de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            NotificationCompat.Builder notificationBuilder,
1195439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            NotificationCompat.WearableExtender wearExtender, Cursor conversationCursor,
119615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            Intent notificationIntent, Folder folder, long when, Resources res,
11978913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            String notificationAccountDisplayName, String notificationAccountEmail,
11988913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            boolean isInbox, String notificationLabelName, int notificationId,
1199de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            final ContactFetcher contactFetcher) {
1200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
12018913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final ConfigResult result = new ConfigResult();
1202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
120315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final Conversation conversation = new Conversation(conversationCursor);
1204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1205de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        // Set of all unique senders for unseen messages
1206de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final HashSet<String> sendersList = new HashSet<String>();
120715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        Cursor cursor = null;
120815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        MessageCursor messageCursor = null;
120915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        boolean multipleUnseenThread = false;
121015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        String from = null;
121115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        try {
121215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final Uri uri = conversation.messageListUri.buildUpon().appendQueryParameter(
121315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    UIProvider.LABEL_QUERY_PARAMETER, folder.persistentId).build();
121415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            cursor = context.getContentResolver().query(uri, UIProvider.MESSAGE_PROJECTION,
121515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    null, null, null);
121615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            messageCursor = new MessageCursor(cursor);
121715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Use the information from the last sender in the conversation that triggered
121815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // this notification.
121915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
122015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            String fromAddress = "";
122115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
122215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message = messageCursor.getMessage();
122315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                fromAddress = message.getFrom();
12249461561d3f0ea554138c3b24207a69306e07d4bcAnthony Lee                if (fromAddress == null) {
12259461561d3f0ea554138c3b24207a69306e07d4bcAnthony Lee                    // No sender. Go back to default value.
12265a5b99b97c06fbe54497af0ea9adb8b14a62f91dJay Shrauner                    LogUtils.e(LOG_TAG, "No sender found for message: %d", message.getId());
12279461561d3f0ea554138c3b24207a69306e07d4bcAnthony Lee                    fromAddress = "";
12289461561d3f0ea554138c3b24207a69306e07d4bcAnthony Lee                }
122915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                from = getDisplayableSender(fromAddress);
12308913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                result.contactIconInfo = getContactIcon(
12318913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        context, account.getAccountManagerAccount().name, from,
1232de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        getSenderAddress(fromAddress), folder, contactFetcher);
1233de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                sendersList.add(getSenderAddress(fromAddress));
1234de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setLargeIcon(result.contactIconInfo.icon);
123515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
123715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Assume that the last message in this conversation is unread
123815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            int firstUnseenMessagePos = messageCursor.getPosition();
123915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
124015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message = messageCursor.getMessage();
124115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final boolean unseen = !message.seen;
124215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (unseen) {
124315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    firstUnseenMessagePos = messageCursor.getPosition();
1244de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    sendersList.add(getSenderAddress(message.getFrom()));
124515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    if (!multipleUnseenThread
124615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            && !fromAddress.contentEquals(message.getFrom())) {
124715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        multipleUnseenThread = true;
124892b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
124915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
125015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
125289bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein            final String subject = ConversationItemView.filterTag(context, conversation.subject);
125389bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein
125415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // TODO(skennedy) Can we remove this check?
125515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (Utils.isRunningJellybeanOrLater()) {
125615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For a new-style notification
1257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
125815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (multipleUnseenThread) {
125915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // The title of a single conversation is the list of senders.
126015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    int sendersLength = res.getInteger(R.integer.swipe_senders_length);
1261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
126215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    final SpannableStringBuilder sendersBuilder = getStyledSenders(
126315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            context, conversationCursor, sendersLength,
126415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            notificationAccountEmail);
126515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1266de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    notificationBuilder.setContentTitle(sendersBuilder);
126715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // For a single new conversation, the ticker is based on the sender's name.
1268644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                    result.notificationTicker = sendersBuilder.toString();
1269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
127015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    from = getWrappedFromString(from);
127115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // The title of a single message the sender.
1272de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    notificationBuilder.setContentTitle(from);
127315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // For a single new conversation, the ticker is based on the sender's name.
1274644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                    result.notificationTicker = from;
127515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
1276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
127715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification content will be the subject of the conversation.
1278de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setContentText(getSingleMessageLittleText(context, subject));
1279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
128015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification subtext will be the subject of the conversation for inbox
128115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications, or will based on the the label name for user label
128215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications.
1283de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setSubText(isInbox ?
128415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        notificationAccountDisplayName : notificationLabelName);
1285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
128615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final NotificationCompat.BigTextStyle bigText =
1287de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        new NotificationCompat.BigTextStyle(notificationBuilder);
128815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
128915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // Seek the message cursor to the first unread message
129015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message;
129115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (messageCursor.moveToPosition(firstUnseenMessagePos)) {
129215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    message = messageCursor.getMessage();
129389bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                    bigText.bigText(getSingleMessageBigText(context, subject, message));
129415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                } else {
129515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    LogUtils.e(LOG_TAG, "Failed to load message");
129615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    message = null;
1297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
129815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
129915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (message != null) {
130015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    final Set<String> notificationActions =
130115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folderPreferences.getNotificationActions(account);
130215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
130315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    NotificationActionUtils.addNotificationActions(context, notificationIntent,
1304de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                            notificationBuilder, wearExtender, account, conversation, message,
130515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folder, notificationId, when, notificationActions);
1306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
130715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            } else {
130815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For an old-style notification
1309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
131015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The title of a single conversation notification is built from both the sender
131115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // and subject of the new message.
1312de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setContentTitle(
131389bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                        getSingleMessageNotificationTitle(context, from, subject));
1314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
131515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification content will be the subject of the conversation for inbox
131615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications, or will based on the the label name for user label
131715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications.
1318de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                notificationBuilder.setContentText(
131915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        isInbox ? notificationAccountDisplayName : notificationLabelName);
1320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
132115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For a single new conversation, the ticker is based on the sender's name.
1322644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                result.notificationTicker = from;
132315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1324de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1325de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            tagNotificationsWithPeople(context, notificationBuilder, sendersList,
1326de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    account.getAccountManagerAccount().name, contactFetcher);
132715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        } finally {
132815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (messageCursor != null) {
132915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                messageCursor.close();
133015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
133115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (cursor != null) {
133215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                cursor.close();
133315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1335644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        return result;
1336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1338de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah    /**
1339de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * Iterates through all senders, retrieves contact lookup Uris for each sender, and tags the
1340de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * given notification with these Uris
1341de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param context
1342de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param notificationBuilder
1343de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param sendersList List of unique senders to be tagged with the conversation
1344de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param accountName
1345de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param contactFetcher Implementation of ContactLookupUriFetcher (null by default)
1346de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     */
1347de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah    private static void tagNotificationsWithPeople(Context context,
1348de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            NotificationCompat.Builder notificationBuilder, HashSet<String> sendersList,
1349de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            String accountName, ContactFetcher contactFetcher) {
1350de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        // If there is a ContactLookupUriFetcher, go through all unique senders
1351de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        // in the combined notification and add each one as a person.
1352de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        if (contactFetcher != null) {
1353de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            for (final String sender : sendersList) {
1354de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                final Uri contactLookupUri =
1355de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        contactFetcher.getContactLookupUri(context,
1356de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                                accountName, sender);
1357de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1358de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                if (contactLookupUri != null) {
1359de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    notificationBuilder.addPerson(contactLookupUri.toString());
1360de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                }
1361de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            }
1362de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1363de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        // If implementation for the fetcher is not provided, rely on the ContentResolver
1364de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        // to query for contacts and tag the notification
1365de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        } else {
1366de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            findAndTagContacts(context, sendersList, notificationBuilder);
1367de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        }
1368de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah    }
1369de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1370de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah    /**
1371de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * Queries for contact id and lookup key to tag the notification with Person objects
1372de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * based on the addresses given.
1373de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param context
1374de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param addresses set of addresses to tag the single notification with
1375de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     * @param notificationBuilder
1376de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah     */
1377de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah    private static void findAndTagContacts(Context context,
1378de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            HashSet<String> addresses, NotificationCompat.Builder notificationBuilder) {
1379de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final ArrayList<String> whereArgs = new ArrayList<String>(addresses);
1380de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final StringBuilder whereBuilder = new StringBuilder();
1381de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final String[] questionMarks = new String[addresses.size()];
1382de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1383de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        Arrays.fill(questionMarks, "?");
1384de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        whereBuilder.append(Email.DATA1 + " IN (").
1385de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                append(TextUtils.join(",", questionMarks)).
1386de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                append(")");
1387de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1388de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final ContentResolver resolver = context.getContentResolver();
1389de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final Cursor c = resolver.query(Email.CONTENT_URI,
1390de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                new String[] {Email.CONTACT_ID, ContactsContract.Contacts.LOOKUP_KEY},
1391de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                whereBuilder.toString(),
1392de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                whereArgs.toArray(new String[0]), null);
1393de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1394de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        if (c == null) {
1395de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            // No query results - no contacts to tag
1396de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            return;
1397de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        }
1398de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1399de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final int contactIdCol = c.getColumnIndex(Email.CONTACT_ID);
1400de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        final int lookupKeyCol = c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
1401de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1402de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        try {
1403de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            while (c.moveToNext()) {
1404de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                c.getString(lookupKeyCol);
1405de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1406de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                // Get lookup uri based on id and lookup key
1407de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                final Uri contactLookupUri = ContactsContract.Contacts.getLookupUri(
1408de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                        c.getLong(contactIdCol), c.getString(lookupKeyCol));
1409de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
1410de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                // Add Person if uri is found
1411de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                if (contactLookupUri != null) {
1412de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                    notificationBuilder.addPerson(contactLookupUri.toString());
1413de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                }
1414de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            }
1415de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        } finally {
1416de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            c.close();
1417de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah        }
1418de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah    }
1419de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah
142012fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    private static String getWrappedFromString(String from) {
142112fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        if (from == null) {
142212fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            LogUtils.e(LOG_TAG, "null from string in getWrappedFromString");
142312fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            from = "";
142412fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        }
1425963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        from = sBidiFormatter.unicodeWrap(from);
142612fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        return from;
142712fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    }
142812fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook
1429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder getStyledSenders(final Context context,
1430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Cursor conversationCursor, final int maxLength, final String account) {
1431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Conversation conversation = new Conversation(conversationCursor);
1432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final com.android.mail.providers.ConversationInfo conversationInfo =
1433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                conversation.conversationInfo;
1434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ArrayList<SpannableString> senders = new ArrayList<SpannableString>();
1435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sNotificationUnreadStyleSpan == null) {
1436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationUnreadStyleSpan = new TextAppearanceSpan(
1437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, R.style.NotificationSendersUnreadTextAppearance);
1438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationReadStyleSpan =
1439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSendersReadTextAppearance);
1440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SendersView.format(context, conversationInfo, "", maxLength, senders, null, null, account,
144210ea28ab588d8e922c0f78f0f47fe479739ec2cfJames Lemieux                sNotificationUnreadStyleSpan, sNotificationReadStyleSpan,
144310ea28ab588d8e922c0f78f0f47fe479739ec2cfJames Lemieux                false /* showToHeader */, false /* resourceCachingRequired */);
1444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return ellipsizeStyledSenders(context, senders);
1446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sSendersSplitToken = null;
1449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sElidedPaddingToken = null;
1450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder ellipsizeStyledSenders(final Context context,
1452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            ArrayList<SpannableString> styledSenders) {
1453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sSendersSplitToken == null) {
1454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sSendersSplitToken = context.getString(R.string.senders_split_token);
1455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sElidedPaddingToken = context.getString(R.string.elided_padding_token);
1456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder builder = new SpannableStringBuilder();
1459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString prevSender = null;
1460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (SpannableString sender : styledSenders) {
1461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (sender == null) {
14622f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.e(LOG_TAG, "null sender iterating over styledSenders");
1463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                continue;
1464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
1466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (SendersView.sElidedString.equals(sender.toString())) {
1467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
1469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (builder.length() > 0
1470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    && (prevSender == null || !SendersView.sElidedString.equals(prevSender
1471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            .toString()))) {
1472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sSendersSplitToken + sender);
1474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(sender);
1478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return builder;
1480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableString copyStyles(CharacterStyle[] spans, CharSequence newText) {
1483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString s = new SpannableString(newText);
1484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (spans != null && spans.length > 0) {
1485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            s.setSpan(spans[0], 0, s.length(), 0);
1486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return s;
1488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Seeks the cursor to the position of the most recent unread conversation. If no unread
1492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversation is found, the position of the cursor will be restored, and false will be
1493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * returned.
1494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static boolean seekToLatestUnreadConversation(final Cursor cursor) {
1496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int initialPosition = cursor.getPosition();
1497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        do {
1498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
1499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!conversation.read) {
1500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return true;
1501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } while (cursor.moveToNext());
1503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Didn't find an unread conversation, reset the position.
1505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        cursor.moveToPosition(initialPosition);
1506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return false;
1507d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1512d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senders Sender of the new message that triggered the notification.
1514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1515d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param snippet Snippet of the new message that triggered the notification
1516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageInboxLine(Context context,
1520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String senders, String subject, String snippet) {
1521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // TODO(cwren) finish this step toward commmon code with getSingleMessageBigText
1522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String subjectSnippet = !TextUtils.isEmpty(subject) ? subject : snippet;
1524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationPrimarySpan =
1526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
1527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1528d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(senders)) {
1529d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the senders are empty, just use the subject/snippet.
1530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return subjectSnippet;
1531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(subjectSnippet)) {
1532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject/snippet is empty, just use the senders.
1533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(senders);
1534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan, 0, senders.length(), 0);
1535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1538d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String formatString = context.getResources().getString(
1539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.multiple_new_message_notification_item);
1540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSecondarySpan =
1541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
15438173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein            // senders is already individually unicode wrapped so it does not need to be done here
1544f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein            final String instantiatedString = String.format(formatString,
15458173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein                    senders,
1546963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein                    sBidiFormatter.unicodeWrap(subjectSnippet));
1547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(instantiatedString);
1549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isOrderReversed = formatString.indexOf("%2$s") <
1551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    formatString.indexOf("%1$s");
1552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int primaryOffset =
1553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(senders) :
1554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(senders));
1555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int secondaryOffset =
1556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(subjectSnippet) :
1557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(subjectSnippet));
1558d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan,
1559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    primaryOffset, primaryOffset + senders.length(), 0);
1560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSecondarySpan,
1561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    secondaryOffset, secondaryOffset + subjectSnippet.length(), 0);
1562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1565d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1570f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * @return a {@link CharSequence} suitable for use in
1571f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * {@link NotificationCompat.Builder#setContentText}
1572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageLittleText(Context context, String subject) {
1574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final SpannableString spannableString = new SpannableString(subject);
1578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return spannableString;
1581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param message the {@link Message} to be displayed.
1589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageBigText(Context context, String subject,
1593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Message message) {
1594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
159800d1c3df5d9c33d140c169a2b6ce0494d6028920Rohan Shah        final String snippet = getMessageBodyWithoutElidedText(message);
1599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Change multiple newlines (with potential white space between), into a single new line
1601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String collapsedSnippet =
1602d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";
1603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1604d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just use the snippet.
1606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return snippet;
1607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(collapsedSnippet)) {
1608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the snippet is empty, just use the subject.
1609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(subject);
1610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1613d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationBigTextFormat = context.getResources().getString(
1615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_big_text);
1616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") >
1620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationBigTextFormat.indexOf("%1$s");
1621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String bigText =
1622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    String.format(notificationBigTextFormat, subject, collapsedSnippet);
1623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(bigText);
1624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isSubjectFirst ? bigText.indexOf(subject) : bigText.lastIndexOf(subject));
1627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan,
1628d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1629d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1630d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1635d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the title for a notification for a single new conversation
1636d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1637d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sender Sender of the new message that triggered the notification.
1638d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1639d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use as a {@link Notification} title.
1640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageNotificationTitle(Context context,
1642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String sender, String subject) {
1643d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1644d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just set the title to the sender's information.
1646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return sender;
1647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationTitleFormat = context.getResources().getString(
1649d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_title);
1650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1651d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectLast = notificationTitleFormat.indexOf("%2$s") >
1654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTitleFormat.indexOf("%1$s");
1655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String titleString = String.format(notificationTitleFormat, sender, subject);
1656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Format the string so the subject is using the secondaryText style
1658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString titleSpannable = new SpannableString(titleString);
1659d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Find the offset of the subject.
1661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    isSubjectLast ? titleString.lastIndexOf(subject) : titleString.indexOf(subject);
1663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSubjectSpan =
1664d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1665d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            titleSpannable.setSpan(notificationSubjectSpan,
1666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return titleSpannable;
1668d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1670d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1671d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
16727f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears the notifications for the specified account/folder.
1673d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
16747f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    public static void clearFolderNotification(Context context, Account account, Folder folder,
16757f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final boolean markSeen) {
167626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        LogUtils.v(LOG_TAG, "Clearing all notifications for %s/%s", account.getEmailAddress(),
167726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                folder.name);
1678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
1679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
1680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.remove(key);
1681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
1682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
168315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final NotificationManagerCompat notificationManager =
168415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                NotificationManagerCompat.from(context);
1685b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        notificationManager.cancel(getNotificationId(account.getAccountManagerAccount(), folder));
16867f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
1687e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        cancelConversationNotifications(key, notificationManager);
168815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
16897f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        if (markSeen) {
16907f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            markSeen(context, folder);
16917f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
16927f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    }
16937f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
16947f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    /**
1695e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau     * Use content resolver to update a conversation.  Should not be called from a main thread.
1696e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau     */
1697e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    public static void markConversationAsReadAndSeen(Context context, Uri conversationUri) {
1698e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        LogUtils.v(LOG_TAG, "markConversationAsReadAndSeen=%s", conversationUri);
1699e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau
1700e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        final ContentValues values = new ContentValues(2);
1701e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        values.put(UIProvider.ConversationColumns.SEEN, Boolean.TRUE);
1702e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        values.put(UIProvider.ConversationColumns.READ, Boolean.TRUE);
1703e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        context.getContentResolver().update(conversationUri, values, null, null);
1704e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    }
1705e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau
1706e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    /**
17077f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears all notifications for the specified account.
17087f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     */
1709b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static void clearAccountNotifications(final Context context,
1710b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final android.accounts.Account account) {
17112f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications for %s", account);
17127f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
17137f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
17147f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        // Find all NotificationKeys for this account
17157f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final ImmutableList.Builder<NotificationKey> keyBuilder = ImmutableList.builder();
17167f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
17177f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey key : notificationMap.keySet()) {
1718b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            if (account.equals(key.account.getAccountManagerAccount())) {
17197f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                keyBuilder.add(key);
17207f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            }
17217f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
17227f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
17237f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final List<NotificationKey> notificationKeys = keyBuilder.build();
17247f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
172515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final NotificationManagerCompat notificationManager =
172615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                NotificationManagerCompat.from(context);
17277f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
17287f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey notificationKey : notificationKeys) {
17297f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final Folder folder = notificationKey.folder;
17307f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationManager.cancel(getNotificationId(account, folder));
17317f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationMap.remove(notificationKey);
173215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1733e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            cancelConversationNotifications(notificationKey, notificationManager);
17347f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
17357f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
17367f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        notificationMap.saveNotificationMap(context);
1737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1739e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    private static void cancelConversationNotifications(NotificationKey key,
174015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationManagerCompat nm) {
1741e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        final Set<Integer> conversationNotifications = sConversationNotificationMap.get(key);
1742e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        if (conversationNotifications != null) {
1743e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            for (Integer conversationNotification : conversationNotifications) {
1744e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                nm.cancel(conversationNotification);
174515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1746e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            sConversationNotificationMap.remove(key);
174715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
174815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
174915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
17508913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    private static ContactIconInfo getContactIcon(final Context context, String accountName,
17518913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final String displayName, final String senderAddress, final Folder folder,
1752de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            final ContactFetcher contactFetcher) {
17538913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (Looper.myLooper() == Looper.getMainLooper()) {
17548913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            throw new IllegalStateException(
17558913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    "getContactIcon should not be called on the main thread.");
17568913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
17578913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
1758a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner        final ContactIconInfo contactIconInfo;
1759f47f70414d0c88213f7b8091f67cc335c1212fd6Tony Mantler        if (TextUtils.isEmpty(senderAddress)) {
1760a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            contactIconInfo = new ContactIconInfo();
1761a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner        } else {
1762a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            // Get the ideal size for this icon.
1763a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            final Resources res = context.getResources();
1764a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            final int idealIconHeight =
1765a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
1766a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            final int idealIconWidth =
1767a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                    res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
1768a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            final int idealWearableBgWidth =
1769a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                    res.getDimensionPixelSize(R.dimen.wearable_background_width);
1770a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            final int idealWearableBgHeight =
1771a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                    res.getDimensionPixelSize(R.dimen.wearable_background_height);
1772a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner
1773de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah            if (contactFetcher != null) {
1774de2f3f40ab2403b89bebd219dcab4d561fd07387Rohan Shah                contactIconInfo = contactFetcher.getContactPhoto(context, accountName,
1775a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                        senderAddress, idealIconWidth, idealIconHeight, idealWearableBgWidth,
1776a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                        idealWearableBgHeight);
1777a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            } else {
1778a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                contactIconInfo = getContactInfo(context, senderAddress, idealIconWidth,
1779a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                        idealIconHeight, idealWearableBgWidth, idealWearableBgHeight);
1780a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            }
17818913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
1782a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            if (contactIconInfo.icon == null) {
1783a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                // Make a colorful tile!
1784a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                final Dimensions dimensions = new Dimensions(idealIconWidth, idealIconHeight,
1785a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner                        Dimensions.SCALE_ONE);
17868913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
1787d267498a916882165afb88e7e42cd898d5e748d0Régis Décamps                contactIconInfo.icon = new LetterTileProvider(context.getResources())
1788d267498a916882165afb88e7e42cd898d5e748d0Régis Décamps                        .getLetterTile(dimensions, displayName, senderAddress);
1789a242bf72f4c5d085a00b723b8cd09e8856eaf265Jay Shrauner            }
179028d44b2bec95381693b203f0515218aaf5418f04Rohan Shah
179128d44b2bec95381693b203f0515218aaf5418f04Rohan Shah            // Only turn the square photo/letter tile into a circle for L and later
179228d44b2bec95381693b203f0515218aaf5418f04Rohan Shah            if (Utils.isRunningLOrLater()) {
1793d267498a916882165afb88e7e42cd898d5e748d0Régis Décamps                contactIconInfo.icon = BitmapUtil.frameBitmapInCircle(contactIconInfo.icon);
179428d44b2bec95381693b203f0515218aaf5418f04Rohan Shah            }
17958913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
17968913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
17978913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (contactIconInfo.icon == null) {
179810b39450191f95d37e29526ada7a239dcbc858adRohan Shah            // Use anonymous icon due to lack of sender
179910b39450191f95d37e29526ada7a239dcbc858adRohan Shah            contactIconInfo.icon = getIcon(context,
180010b39450191f95d37e29526ada7a239dcbc858adRohan Shah                    R.drawable.ic_notification_anonymous_avatar_32dp);
18018913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
18028913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
18038913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (contactIconInfo.wearableBg == null) {
18048913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            contactIconInfo.wearableBg = getDefaultWearableBg(context);
18058913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
18068913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
18078913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        return contactIconInfo;
18088913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    }
18098913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
1810d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static ArrayList<Long> findContacts(Context context, Collection<String> addresses) {
1811d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<String> whereArgs = new ArrayList<String>();
1812d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder whereBuilder = new StringBuilder();
1813d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] questionMarks = new String[addresses.size()];
1814d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1815d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereArgs.addAll(addresses);
1816d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Arrays.fill(questionMarks, "?");
1817d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereBuilder.append(Email.DATA1 + " IN (").
1818d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(TextUtils.join(",", questionMarks)).
1819d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(")");
1820d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1821d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ContentResolver resolver = context.getContentResolver();
1822d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor c = resolver.query(Email.CONTENT_URI,
1823e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                new String[] {Email.CONTACT_ID}, whereBuilder.toString(),
1824d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                whereArgs.toArray(new String[0]), null);
1825d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1826d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = new ArrayList<Long>();
1827d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (c == null) {
1828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return contactIds;
1829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
1831d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (c.moveToNext()) {
1832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                contactIds.add(c.getLong(0));
1833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
1835d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            c.close();
1836d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return contactIds;
1838d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1839d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
18408913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static ContactIconInfo getContactInfo(
18418913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final Context context, final String senderAddress,
18428913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final int idealIconWidth, final int idealIconHeight,
18438913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final int idealWearableBgWidth, final int idealWearableBgHeight) {
18448913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final ContactIconInfo contactIconInfo = new ContactIconInfo();
1845c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah        final List<Long> contactIds = findContacts(context, Arrays.asList(
1846c41edc70b7de16b7e43f77680d51846daae1fa2aRohan Shah                new String[]{senderAddress}));
1847d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1848d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (contactIds != null) {
18494046d0695e102368201bb2f3a268a4f85c8bc4dbScott Kennedy            for (final long id : contactIds) {
1850644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                final Uri contactUri = ContentUris.withAppendedId(
1851644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        ContactsContract.Contacts.CONTENT_URI, id);
18528913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                final InputStream inputStream =
18538913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        ContactsContract.Contacts.openContactPhotoInputStream(
18548913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                                context.getContentResolver(), contactUri, true /*preferHighres*/);
1855d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1856644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                if (inputStream != null) {
1857d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    try {
18588913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        final Bitmap source = BitmapFactory.decodeStream(inputStream);
1859644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        if (source != null) {
1860644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            // We should scale this image to fit the intended size
1861644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            contactIconInfo.icon = Bitmap.createScaledBitmap(source, idealIconWidth,
1862644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                    idealIconHeight, true);
1863644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1864644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            contactIconInfo.wearableBg = Bitmap.createScaledBitmap(source,
1865644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                    idealWearableBgWidth, idealWearableBgHeight, true);
1866644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        }
1867644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1868644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        if (contactIconInfo.icon != null) {
1869644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            break;
1870d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1871d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } finally {
1872644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        Closeables.closeQuietly(inputStream);
1873d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1874d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1875d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1876d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1877c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1878644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        return contactIconInfo;
1879d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1880d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1881d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getMessageBodyWithoutElidedText(final Message message) {
18825e7e88b9f5ac1f239753e13ffb3ee4b249722c9aScott Kennedy        return getMessageBodyWithoutElidedText(message.getBodyAsHtml());
1883d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1884d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1885d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static String getMessageBodyWithoutElidedText(String html) {
1886d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(html)) {
1887d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return "";
1888d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1889d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Get the html "tree" for this message body
1890d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final HtmlTree htmlTree = com.android.mail.utils.Utils.getHtmlTree(html);
189177b4c2c31d7601665c337ce5cbc9d84fb9332be8Jin Cao        htmlTree.setConverterFactory(MESSAGE_CONVERTER_FACTORY);
1892d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1893d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return htmlTree.getPlainText();
1894d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1895d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1896d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void markSeen(final Context context, final Folder folder) {
1897259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final Uri uri = folder.folderUri.fullUri;
1898d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1899d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ContentValues values = new ContentValues(1);
1900d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        values.put(UIProvider.ConversationColumns.SEEN, 1);
1901d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1902d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        context.getContentResolver().update(uri, values, null, null);
1903d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1904d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1905d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1906d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns a displayable string representing
1907d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the message sender. It has a preference toward showing the name,
1908d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * but will fall back to the address if that is all that is available.
1909d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1910d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getDisplayableSender(String sender) {
1911d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1912d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1913d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String displayableSender = address.getName();
1914390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1915390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        if (!TextUtils.isEmpty(displayableSender)) {
1916821e578a71c7015646522e729600618f0ec16fc0Tony Mantler            return Address.decodeAddressPersonal(displayableSender);
1917d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1918390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1919390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        // If that fails, default to the sender address.
1920390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        displayableSender = address.getAddress();
1921390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1924d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = sender;
1926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1927d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return displayableSender;
1928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1931d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns only the address portion of a message sender.
1932d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getSenderAddress(String sender) {
1934d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1935d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String tokenizedAddress = address.getAddress();
1937d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1938d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1939d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1940d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(tokenizedAddress)) {
1941d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            tokenizedAddress = sender;
1942d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1943d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return tokenizedAddress;
1944d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1945d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1946b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static int getNotificationId(final android.accounts.Account account,
1947b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final Folder folder) {
1948d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return 1 ^ account.hashCode() ^ folder.hashCode();
1949d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1950d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1951e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    private static int getNotificationId(int summaryNotificationId, int conversationHashCode) {
1952e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        return summaryNotificationId ^ conversationHashCode;
195315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
195415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1955d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationKey {
1956d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Account account;
1957d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Folder folder;
1958d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1959d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public NotificationKey(Account account, Folder folder) {
1960d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.account = account;
1961d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.folder = folder;
1962d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1963d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1964d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1965d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public boolean equals(Object other) {
1966d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!(other instanceof NotificationKey)) {
1967d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return false;
1968d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1969d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key = (NotificationKey) other;
1970b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            return account.getAccountManagerAccount().equals(key.account.getAccountManagerAccount())
1971b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    && folder.equals(key.folder);
1972d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1973d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1974d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1975d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public String toString() {
197626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            return account.getDisplayName() + " " + folder.name;
1977d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1978d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public int hashCode() {
1981b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int accountHashCode = account.getAccountManagerAccount().hashCode();
1982d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int folderHashCode = folder.hashCode();
1983d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return accountHashCode ^ folderHashCode;
1984d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1985d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1986d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1987d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1988d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Contains the logic for converting the contents of one HtmlTree into
1989d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * plaintext.
1990d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1991d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static class MailMessagePlainTextConverter extends HtmlTree.DefaultPlainTextConverter {
1992d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings for parsing html message bodies
1993d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_NAME = "div";
1994d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME = "class";
1995d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE = "elided-text";
1996d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1997d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HTML.Attribute ELIDED_TEXT_ATTRIBUTE =
1998d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new HTML.Attribute(ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME, HTML.Attribute.NO_TYPE);
1999d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2000d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HtmlDocument.Node ELIDED_TEXT_REPLACEMENT_NODE =
2001d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                HtmlDocument.createSelfTerminatingTag(HTML4.BR_ELEMENT, null, null, null);
2002d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2003d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private int mEndNodeElidedTextBlock = -1;
2004d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2005d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
2006d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void addNode(HtmlDocument.Node n, int nodeNum, int endNum) {
2007d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we are in the middle of an elided text block, don't add this node
2008d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (nodeNum < mEndNodeElidedTextBlock) {
2009d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
2010d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (nodeNum == mEndNodeElidedTextBlock) {
2011d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                super.addNode(ELIDED_TEXT_REPLACEMENT_NODE, nodeNum, endNum);
2012d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
2013d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
2014d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2015d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this tag starts another elided text block, we want to remember the end
2016d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (n instanceof HtmlDocument.Tag) {
2017d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                boolean foundElidedTextTag = false;
2018d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HtmlDocument.Tag htmlTag = (HtmlDocument.Tag)n;
2019d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HTML.Element htmlElement = htmlTag.getElement();
2020d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (ELIDED_TEXT_ELEMENT_NAME.equals(htmlElement.getName())) {
2021d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Make sure that the class is what is expected
2022d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final List<HtmlDocument.TagAttribute> attributes =
2023d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            htmlTag.getAttributes(ELIDED_TEXT_ATTRIBUTE);
2024d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    for (HtmlDocument.TagAttribute attribute : attributes) {
2025d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE.equals(
2026d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                attribute.getValue())) {
2027d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Found an "elided-text" div.  Remember information about this tag
2028d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            mEndNodeElidedTextBlock = endNum;
2029d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            foundElidedTextTag = true;
2030d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            break;
2031d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
2032d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
2033d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
2034d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2035d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (foundElidedTextTag) {
2036d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return;
2037d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
2038d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
2039d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2040c56b233013cf107c702ef9f61305282670ad804aScott Kennedy            super.addNode(n, nodeNum, endNum);
2041d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
2042d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
2043d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2044d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
2045d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * During account setup in Email, we may not have an inbox yet, so the notification setting had
2046d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * to be stored in {@link AccountPreferences}. If it is still there, we need to move it to the
2047d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link FolderPreferences} now.
2048d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
2049d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void moveNotificationSetting(final AccountPreferences accountPreferences,
2050d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences) {
2051d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (accountPreferences.isDefaultInboxNotificationsEnabledSet()) {
2052d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this setting has been changed some other way, don't overwrite it
2053d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.isNotificationsEnabledSet()) {
2054d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean notificationsEnabled =
2055d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        accountPreferences.getDefaultInboxNotificationsEnabled();
2056d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2057d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                folderPreferences.setNotificationsEnabled(notificationsEnabled);
2058d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
2059d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
2060d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            accountPreferences.clearDefaultInboxNotificationsEnabled();
2061d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
2062d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
2063c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau
2064c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau    private static class NotificationBuilders {
2065c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        public final NotificationCompat.Builder notifBuilder;
2066439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau        public final NotificationCompat.WearableExtender wearableNotifBuilder;
2067c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau
2068c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        private NotificationBuilders(NotificationCompat.Builder notifBuilder,
2069439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                NotificationCompat.WearableExtender wearableNotifBuilder) {
2070c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            this.notifBuilder = notifBuilder;
2071c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            this.wearableNotifBuilder = wearableNotifBuilder;
2072c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        }
2073c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau
2074c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        public static NotificationBuilders of(NotificationCompat.Builder notifBuilder,
2075439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                NotificationCompat.WearableExtender wearableNotifBuilder) {
2076c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            return new NotificationBuilders(notifBuilder, wearableNotifBuilder);
2077c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        }
2078c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau    }
2079644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
2080644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static class ConfigResult {
2081644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public String notificationTicker;
2082644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public ContactIconInfo contactIconInfo;
2083644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    }
2084644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
20858913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static class ContactIconInfo {
2086644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public Bitmap icon;
2087644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public Bitmap wearableBg;
2088644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    }
2089d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
2090