NotificationUtils.java revision 15490239c82cdb4765e87e926c8c1945adedd1ed
1d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/*
2d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Copyright (C) 2013 The Android Open Source Project
3d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
4d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
5d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * you may not use this file except in compliance with the License.
6d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * You may obtain a copy of the License at
7d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
8d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
9d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
10d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Unless required by applicable law or agreed to in writing, software
11d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
12d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * See the License for the specific language governing permissions and
14d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * limitations under the License.
15d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
16d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypackage com.android.mail.utils;
17d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
18d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.Notification;
19d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.NotificationManager;
20d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.PendingIntent;
21d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentResolver;
22d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentUris;
23d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentValues;
24d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Context;
25d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Intent;
26d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.res.Resources;
27d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.database.Cursor;
28d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.Bitmap;
29d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.BitmapFactory;
30d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.net.Uri;
3115490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport android.preview.support.v4.app.NotificationManagerCompat;
3215490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport android.preview.support.wearable.notifications.WearableNotifications;
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract;
34d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract.CommonDataKinds.Email;
35d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract.Contacts.Photo;
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.support.v4.app.NotificationCompat;
37f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sappersteinimport android.support.v4.text.BidiFormatter;
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableString;
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableStringBuilder;
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils;
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.CharacterStyle;
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.TextAppearanceSpan;
4315490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport android.util.ArrayMap;
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.util.Pair;
4561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedyimport android.util.SparseArray;
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
47821e578a71c7015646522e729600618f0ec16fc0Tony Mantlerimport com.android.emailcommon.mail.Address;
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.EmailAddress;
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.R;
514fe0af81874976a1995191321e35c844b2229811Andy Huangimport com.android.mail.analytics.Analytics;
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.MessageCursor;
53d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.SendersView;
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;
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.io.ByteArrayInputStream;
75d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.ArrayList;
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Arrays;
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Collection;
7815490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.HashMap;
7915490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.HashSet;
80d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.List;
8115490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.Map;
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.concurrent.ConcurrentHashMap;
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class NotificationUtils {
86c7d754703a0b5c2ca4b4dc9675d733881f85b746Scott Kennedy    public static final String LOG_TAG = "NotifUtils";
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** Contains a list of <(account, label), unread conversations> */
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static NotificationMap sActiveNotificationMap = null;
90d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
9161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static final SparseArray<Bitmap> sNotificationIcons = new SparseArray<Bitmap>();
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static TextAppearanceSpan sNotificationUnreadStyleSpan;
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharacterStyle sNotificationReadStyleSpan;
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** A factory that produces a plain text converter that removes elided text. */
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final HtmlTree.PlainTextConverterFactory MESSAGE_CONVERTER_FACTORY =
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            new HtmlTree.PlainTextConverterFactory() {
99d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                @Override
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                public HtmlTree.PlainTextConverter createInstance() {
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return new MailMessagePlainTextConverter();
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            };
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
105963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein    private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
106f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein
10715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    private static Map<NotificationKey, Set<Integer>> sChildNotificationsMap =
10815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            new HashMap<NotificationKey, Set<Integer>>();
10915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears all notifications in response to the user tapping "Clear" in the status bar.
112d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
113d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearAllNotfications(Context context) {
1142f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications.");
115d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
116d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.clear();
117d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns the notification map, creating it if necessary.
122d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static synchronized NotificationMap getNotificationMap(Context context) {
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sActiveNotificationMap == null) {
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap = new NotificationMap();
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // populate the map from the cached data
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap.loadNotificationMap(context);
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return sActiveNotificationMap;
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Class representing the existing notifications, and the number of unread and
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * unseen conversations that triggered each.
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1372354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee    private static final class NotificationMap {
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String NOTIFICATION_PART_SEPARATOR = " ";
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final int NUM_NOTIFICATION_PARTS= 4;
1412354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        private final ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>> mMap =
1422354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            new ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>>();
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
1452354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the number of key values pairs in the inner map.
1462354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1472354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public int size() {
1482354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.size();
1492354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1502354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1512354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1522354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns a set of key values.
1532354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1542354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Set<NotificationKey> keySet() {
1552354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.keySet();
1562354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1572354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1582354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1592354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Remove the key from the inner map and return its value.
1602354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1612354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be removed.
1622354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return The value associated with this key.
1632354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1642354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Pair<Integer, Integer> remove(NotificationKey key) {
1652354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.remove(key);
1662354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1672354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1682354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1692354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Clear all key-value pairs in the map.
1702354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1712354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public void clear() {
1722354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.clear();
1732354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1742354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1752354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1762354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Discover if a key-value pair with this key exists.
1772354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1782354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be checked.
1792354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return If a key-value pair with this key exists in the map.
1802354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1812354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public boolean containsKey(NotificationKey key) {
1822354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.containsKey(key);
1832354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1842354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1852354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1862354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread count for the given NotificationKey.
187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnread(NotificationKey key) {
1892354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.first : null;
191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
193d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
1942354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread unseen count for the given NotificationKey.
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnseen(NotificationKey key) {
1972354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.second : null;
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Store the unread and unseen value for the given NotificationKey
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void put(NotificationKey key, int unread, int unseen) {
205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value =
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Pair<Integer, Integer>(Integer.valueOf(unread), Integer.valueOf(unseen));
2072354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.put(key, value);
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Populates the notification map with previously cached data.
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void loadNotificationMap(final Context context) {
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = mailPrefs.getActiveNotificationSet();
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (notificationSet != null) {
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                for (String notificationEntry : notificationSet) {
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Get the parts of the string that make the notification entry
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] notificationParts =
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            TextUtils.split(notificationEntry, NOTIFICATION_PART_SEPARATOR);
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationParts.length == NUM_NOTIFICATION_PARTS) {
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri accountUri = Uri.parse(notificationParts[0]);
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor accountCursor = context.getContentResolver().query(
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                accountUri, UIProvider.ACCOUNTS_PROJECTION, null, null, null);
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Account account;
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (accountCursor.moveToFirst()) {
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                account = new Account(accountCursor);
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            accountCursor.close();
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri folderUri = Uri.parse(notificationParts[1]);
237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor folderCursor = context.getContentResolver().query(
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folderUri, UIProvider.FOLDERS_PROJECTION, null, null, null);
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Folder folder;
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (folderCursor.moveToFirst()) {
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folder = new Folder(folderCursor);
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            folderCursor.close();
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final NotificationKey key = new NotificationKey(account, folder);
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unreadValue = Integer.valueOf(notificationParts[2]);
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unseenValue = Integer.valueOf(notificationParts[3]);
2532354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                        put(key, unreadValue, unseenValue);
254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Cache the notification map.
261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void saveNotificationMap(Context context) {
263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = Sets.newHashSet();
2642354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Set<NotificationKey> keys = mMap.keySet();
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey key : keys) {
2662354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                final Pair<Integer, Integer> value = mMap.get(key);
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unreadCount = value.first;
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unseenCount = value.second;
269ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                if (unreadCount != null && unseenCount != null) {
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] partValues = new String[] {
271259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            key.account.uri.toString(), key.folder.folderUri.fullUri.toString(),
272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            unreadCount.toString(), unseenCount.toString()};
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues));
274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            mailPrefs.cacheActiveNotificationSet(notificationSet);
278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return the title of this notification with each account and the number of unread and unseen
283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversations for it. Also remove any account in the map that has 0 unread.
284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String createNotificationString(NotificationMap notifications) {
286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder result = new StringBuilder();
287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int i = 0;
288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keysToRemove = Sets.newHashSet();
289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : notifications.keySet()) {
290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unread = notifications.getUnread(key);
291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unseen = notifications.getUnseen(key);
292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unread == null || unread.intValue() == 0) {
293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                keysToRemove.add(key);
294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (i > 0) result.append(", ");
296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                result.append(key.toString() + " (" + unread + ", " + unseen + ")");
297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : keysToRemove) {
302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notifications.remove(key);
303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return result.toString();
306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts and cancel them.
310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAllNotifications(Context context) {
3122f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "cancelAllNotifications - cancelling all");
31315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        nm.cancelAll();
315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        clearAllNotfications(context);
316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, cancel them, and repost.
320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
322963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein    public static void cancelAndResendNotificationsOnLocaleChange(Context context) {
323963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        LogUtils.d(LOG_TAG, "cancelAndResendNotificationsOnLocaleChange");
324963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        sBidiFormatter = BidiFormatter.getInstance();
325ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein        resendNotifications(context, true, null, null);
326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, optionally cancel them, and repost.
330ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * This happens when locale changes. If you only want to resend messages from one
331ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * account-folder pair, pass in the account and folder that should be resent.
332ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All other account-folder pairs will not have their notifications resent.
333ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All notifications will be resent if account or folder is null.
334ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *
335ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param context Current context.
336ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param cancelExisting True, if all notifications should be canceled before resending.
337ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                       False, otherwise.
338ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param accountUri The {@link Uri} of the {@link Account} of the notification
339ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                   upon which an action occurred.
340ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param folderUri The {@link Uri} of the {@link Folder} of the notification
341ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                  upon which an action occurred.
342ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     */
343ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein    public static void resendNotifications(Context context, final boolean cancelExisting,
344259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            final Uri accountUri, final FolderUri folderUri) {
3452f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "resendNotifications ");
3462638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cancelExisting) {
3482f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - cancelling all");
34915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            nm.cancelAll();
351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Re-validate the notifications.
353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<NotificationKey> keys = notificationMap.keySet();
355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey notification : keys) {
356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder = notification.folder;
357b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
358b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(notification.account.getAccountManagerAccount(), folder);
359ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein
360ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // Only resend notifications if the notifications are from the same folder
361ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // and same account as the undo notification that was previously displayed.
36226b29ef558cce109c51584081c52781760a02dd9Scott Kennedy            if (accountUri != null && !Objects.equal(accountUri, notification.account.uri) &&
363259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folderUri != null && !Objects.equal(folderUri, folder.folderUri)) {
3642f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "resendNotifications - not resending %s / %s"
3652638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                        + " because it doesn't match %s / %s",
366259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                        notification.account.uri, folder.folderUri, accountUri, folderUri);
367ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                continue;
368ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            }
369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
3702f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - resending %s / %s",
371259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    notification.account.uri, folder.folderUri);
3722638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationAction undoableAction =
374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sUndoNotifications.get(notificationId);
375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (undoableAction == null) {
376ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                validateNotifications(context, folder, notification.account, true,
377ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                        false, notification);
378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Create an undo notification
380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.createUndoNotification(context, undoableAction);
381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications for the specified account.
387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void validateAccountNotifications(Context context, String account) {
3892f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "validateAccountNotifications - %s", account);
3904162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        List<NotificationKey> notificationsToCancel = Lists.newArrayList();
392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Iterate through the notification map to see if there are any entries that correspond to
393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // labels that are not in the sync set.
394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keys = notificationMap.keySet();
396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final AccountPreferences accountPreferences = new AccountPreferences(context, account);
397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean enabled = accountPreferences.areNotificationsEnabled();
398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (!enabled) {
399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Cancel all notifications for this account
400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
401b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                if (notification.account.getAccountManagerAccount().name.equals(account)) {
402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationsToCancel.add(notification);
403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Iterate through the notification map to see if there are any entries that
407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // correspond to labels that are not in the notification set.
408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
409b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                if (notification.account.getAccountManagerAccount().name.equals(account)) {
410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // If notification is not enabled for this label, remember this NotificationKey
411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // to later cancel the notification, and remove the entry from the map
412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Folder folder = notification.folder;
413259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    final boolean isInbox = folder.folderUri.equals(
414259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            notification.account.settings.defaultInbox);
415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final FolderPreferences folderPreferences = new FolderPreferences(
416afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                            context, notification.account.getEmailAddress(), folder, isInbox);
417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!folderPreferences.areNotificationsEnabled()) {
419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationsToCancel.add(notification);
420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Cancel & remove the invalid notifications.
426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationsToCancel.size() > 0) {
427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationManager nm = (NotificationManager) context.getSystemService(
428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    Context.NOTIFICATION_SERVICE);
429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : notificationsToCancel) {
430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Folder folder = notification.folder;
431b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                final int notificationId =
432b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                        getNotificationId(notification.account.getAccountManagerAccount(), folder);
4332f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "validateAccountNotifications - cancelling %s / %s",
43426a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        notification.account.getEmailAddress(), folder.persistentId);
435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notificationMap.remove(notification);
437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sUndoNotifications.remove(notificationId);
438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.saveNotificationMap(context);
441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Display only one notification.
446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void setNewEmailIndicator(Context context, final int unreadCount,
448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int unseenCount, final Account account, final Folder folder,
449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean getAttention) {
4502f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "setNewEmailIndicator unreadCount = %d, unseenCount = %d, account = %s,"
45126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                + " folder = %s, getAttention = %b", unreadCount, unseenCount,
45226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                account.getEmailAddress(), folder.folderUri, getAttention);
4534162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean ignoreUnobtrusiveSetting = false;
455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
456b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        final int notificationId = getNotificationId(account.getAccountManagerAccount(), folder);
457dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy
458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Update the notification map
459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount == 0) {
46226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            LogUtils.d(LOG_TAG, "setNewEmailIndicator - cancelling %s / %s",
46326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    account.getEmailAddress(), folder.persistentId);
464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.remove(key);
465dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy            ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
466dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy                    .cancel(notificationId);
467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
468b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook            LogUtils.d(LOG_TAG, "setNewEmailIndicator - update count for: %s / %s " +
46926a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    "to: unread: %d unseen %d", account.getEmailAddress(), folder.persistentId,
470b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    unreadCount, unseenCount);
471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!notificationMap.containsKey(key)) {
472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This account previously didn't have any unread mail; ignore the "unobtrusive
473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notifications" setting and play sound and/or vibrate the device even if a
474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notification already exists (bug 2412348).
475b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "setNewEmailIndicator - ignoringUnobtrusiveSetting");
476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                ignoreUnobtrusiveSetting = true;
477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.put(key, unreadCount, unseenCount);
479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
4832f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.v(LOG_TAG, "New email: %s mapSize: %d getAttention: %b",
484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    createNotificationString(notificationMap), notificationMap.size(),
485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    getAttention);
486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (NotificationActionUtils.sUndoNotifications.get(notificationId) == null) {
489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            validateNotifications(context, folder, account, getAttention, ignoreUnobtrusiveSetting,
490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    key);
491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications notification.
496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void validateNotifications(Context context, final Folder folder,
498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, boolean getAttention, boolean ignoreUnobtrusiveSetting,
499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key) {
500d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
50115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
50532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification: %s mapSize: %d "
506b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "folder: %s getAttention: %b ignoreUnobtrusive: %b",
507b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    createNotificationString(notificationMap),
508b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    notificationMap.size(), folder.name, getAttention, ignoreUnobtrusiveSetting);
50932fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        } else {
51032fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification, mapSize: %d "
511b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "getAttention: %b ignoreUnobtrusive: %b", notificationMap.size(),
512b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    getAttention, ignoreUnobtrusiveSetting);
513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // The number of unread messages for this account and label.
515d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unread = notificationMap.getUnread(key);
516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int unreadCount = unread != null ? unread.intValue() : 0;
517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unseen = notificationMap.getUnseen(key);
518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int unseenCount = unseen != null ? unseen.intValue() : 0;
519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor cursor = null;
521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Uri.Builder uriBuilder = folder.conversationListUri.buildUpon();
524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            uriBuilder.appendQueryParameter(
525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.SEEN_QUERY_PARAMETER, Boolean.FALSE.toString());
5260be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // Do not allow this quick check to disrupt any active network-enabled conversation
5270be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // cursor.
5280be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            uriBuilder.appendQueryParameter(
5290be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    UIProvider.ConversationListQueryParameters.USE_NETWORK,
5300be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    Boolean.FALSE.toString());
531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor = context.getContentResolver().query(uriBuilder.build(),
532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.CONVERSATION_PROJECTION, null, null, null);
5333a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            if (cursor == null) {
5343a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                // This folder doesn't exist.
53532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
53632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        "The cursor is null, so the specified folder probably does not exist");
5373a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                clearFolderNotification(context, account, folder, false);
5383a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                return;
5393a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            }
540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int cursorUnseenCount = cursor.getCount();
541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Make sure the unseen count matches the number of items in the cursor.  But, we don't
543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // want to overwrite a 0 unseen count that was specified in the intent
544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount != 0 && unseenCount != cursorUnseenCount) {
54532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
5462f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        "Unseen count doesn't match cursor count.  unseen: %d cursor count: %d",
547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        unseenCount, cursorUnseenCount);
548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = cursorUnseenCount;
549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For the purpose of the notifications, the unseen count should be capped at the num of
552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // unread conversations.
553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount > unreadCount) {
554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = unreadCount;
555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
557b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
558b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(account.getAccountManagerAccount(), folder);
559d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
56015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationKey notificationKey = new NotificationKey(account, folder);
56115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount == 0) {
56332fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "validateNotifications - cancelling account %s / folder %s",
56426a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
56532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        LogUtils.sanitizeName(LOG_TAG, folder.persistentId));
566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
56715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                cancelChildNotifications(notificationKey, nm);
56815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // We now have all we need to create the notification and the pending intent
573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            PendingIntent clickIntent;
574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
57615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            WearableNotifications.Builder wearableNotification =
57715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    new WearableNotifications.Builder(notification);
57815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            Map<Integer, WearableNotifications.Builder> msgNotifications =
57915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    new ArrayMap<Integer, WearableNotifications.Builder>();
580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setSmallIcon(R.drawable.stat_notify_email);
58126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            notification.setTicker(account.getDisplayName());
582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long when;
584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long oldWhen =
586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sNotificationTimestamps.get(notificationId);
587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (oldWhen != 0) {
588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = oldWhen;
589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = System.currentTimeMillis();
591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setWhen(when);
594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The timestamp is now stored in the notification, so we can remove it from here
596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
597d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Dispatch a CLEAR_NEW_MAIL_NOTIFICATIONS intent if the user taps the "X" next to a
599d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // notification.  Also this intent gets fired when the user taps on a notification as
600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the AutoCancel flag has been set
601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Intent cancelNotificationIntent =
602d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.setPackage(context.getPackageName());
60409400efa442422299acf21abe20e3470f9d965abScott Kennedy            cancelNotificationIntent.setData(Utils.appendVersionQueryParameter(context,
605259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri));
60648cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
60748cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder);
608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setDeleteIntent(PendingIntent.getService(
610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, notificationId, cancelNotificationIntent, 0));
611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Ensure that the notification is cleared when the user selects it
613d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setAutoCancel(true);
614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean eventInfoConfigured = false;
616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
617259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            final boolean isInbox = folder.folderUri.equals(account.settings.defaultInbox);
618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences =
619afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                    new FolderPreferences(context, account.getEmailAddress(), folder, isInbox);
620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (isInbox) {
622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
623afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                        new AccountPreferences(context, account.getEmailAddress());
624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                moveNotificationSetting(accountPreferences, folderPreferences);
625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.areNotificationsEnabled()) {
62832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "Notifications are disabled for this folder; not notifying");
629d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Don't notify
630d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unreadCount > 0) {
634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // How can I order this properly?
635d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor.moveToNext()) {
636bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    final Intent notificationIntent;
637d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
638bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    // Launch directly to the conversation, if there is only 1 unseen conversation
6394fe0af81874976a1995191321e35c844b2229811Andy Huang                    final boolean launchConversationMode = (unseenCount == 1);
6404fe0af81874976a1995191321e35c844b2229811Andy Huang                    if (launchConversationMode) {
64109400efa442422299acf21abe20e3470f9d965abScott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
64209400efa442422299acf21abe20e3470f9d965abScott Kennedy                                cursor);
643bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    } else {
644bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
645bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                                null);
646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
6484fe0af81874976a1995191321e35c844b2229811Andy Huang                    Analytics.getInstance().sendEvent("notification_create",
6494fe0af81874976a1995191321e35c844b2229811Andy Huang                            launchConversationMode ? "conversation" : "conversation_list",
6504fe0af81874976a1995191321e35c844b2229811Andy Huang                            folder.getTypeDescription(), unseenCount);
6514fe0af81874976a1995191321e35c844b2229811Andy Huang
652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationIntent == null) {
6532f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        LogUtils.e(LOG_TAG, "Null intent when building notification");
654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return;
655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
6574fe0af81874976a1995191321e35c844b2229811Andy Huang                    // Amend the click intent with a hint that its source was a notification,
6584fe0af81874976a1995191321e35c844b2229811Andy Huang                    // but remove the hint before it's used to generate notification action
6594fe0af81874976a1995191321e35c844b2229811Andy Huang                    // intents. This prevents the following sequence:
6604fe0af81874976a1995191321e35c844b2229811Andy Huang                    // 1. generate single notification
6614fe0af81874976a1995191321e35c844b2229811Andy Huang                    // 2. user clicks reply, then completes Compose activity
6624fe0af81874976a1995191321e35c844b2229811Andy Huang                    // 3. main activity launches, gets FROM_NOTIFICATION hint in intent
6634fe0af81874976a1995191321e35c844b2229811Andy Huang                    notificationIntent.putExtra(Utils.EXTRA_FROM_NOTIFICATION, true);
6644fe0af81874976a1995191321e35c844b2229811Andy Huang                    clickIntent = PendingIntent.getActivity(context, -1, notificationIntent,
6654fe0af81874976a1995191321e35c844b2229811Andy Huang                            PendingIntent.FLAG_UPDATE_CURRENT);
6664fe0af81874976a1995191321e35c844b2229811Andy Huang                    notificationIntent.removeExtra(Utils.EXTRA_FROM_NOTIFICATION);
6674fe0af81874976a1995191321e35c844b2229811Andy Huang
668d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    configureLatestEventInfoFromConversation(context, account, folderPreferences,
66915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            notification, wearableNotification, msgNotifications, notificationId,
67015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            cursor, clickIntent, notificationIntent, unreadCount, unseenCount,
67115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folder, when);
672d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    eventInfoConfigured = true;
673d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
674d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
675d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean notifyOnce = !folderPreferences.isEveryMessageNotificationEnabled();
679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
680ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (!ignoreUnobtrusiveSetting && notifyOnce) {
681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // If the user has "unobtrusive notifications" enabled, only alert the first time
682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // new mail is received in this account.  This is the default behavior.  See
683d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // bugs 2412348 and 2413490.
684b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "Setting Alert Once");
685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setOnlyAlertOnce(true);
686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
68832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Account: %s vibrate: %s",
68926a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
690ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                    Boolean.toString(folderPreferences.isNotificationVibrateEnabled()));
691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int defaults = 0;
693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
69415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Check if any current child notifications exist previously.  Only notify if one of
69515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // them is new.
69615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            boolean hasNewChildNotification;
69715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            Set<Integer> prevChildNotifications = sChildNotificationsMap.get(notificationKey);
69815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (prevChildNotifications != null) {
69915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                hasNewChildNotification = false;
70015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                for (Integer currentNotificationId : msgNotifications.keySet()) {
70115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    if (!prevChildNotifications.contains(currentNotificationId)) {
70215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        hasNewChildNotification = true;
70315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        break;
70415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    }
70515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
70615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            } else {
70715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                hasNewChildNotification = true;
70815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
70915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
71015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            LogUtils.d(LOG_TAG, "getAttention=%s,oldWhen=%s,hasNewChildNotification=%s",
71115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    getAttention, oldWhen, hasNewChildNotification);
71215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            /*
714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * We do not want to notify if this is coming back from an Undo notification, hence the
715d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * oldWhen check.
716d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             */
71715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (getAttention && oldWhen == 0 && hasNewChildNotification) {
718d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
719cfb969b27f83777e08cd2f1a994dc023574a4739Tony Mantler                        new AccountPreferences(context, account.getEmailAddress());
720d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (accountPreferences.areNotificationsEnabled()) {
721d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (vibrate) {
722d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        defaults |= Notification.DEFAULT_VIBRATE;
723d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
724d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
725d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
726d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            : Uri.parse(ringtoneUri));
72732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                    LogUtils.i(LOG_TAG, "New email in %s vibrateWhen: %s, playing notification: %s",
72826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()), vibrate,
72926a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            ringtoneUri);
730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
73332fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            // TODO(skennedy) Why do we do any of the above if we're just going to bail here?
734d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (eventInfoConfigured) {
735d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                defaults |= Notification.DEFAULT_LIGHTS;
736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setDefaults(defaults);
737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (oldWhen != 0) {
739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // We do not want to display the ticker again if we are re-displaying this
740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notification (like from an Undo notification)
741d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setTicker(null);
742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
743d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
74415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                nm.notify(notificationId, wearableNotification.build());
74515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
74615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (prevChildNotifications != null) {
74715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    Set<Integer> currentNotificationIds = msgNotifications.keySet();
74815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    for (Integer prevChildNotificationId : prevChildNotifications) {
74915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        if (!currentNotificationIds.contains(prevChildNotificationId)) {
75015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            nm.cancel(prevChildNotificationId);
75115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            LogUtils.d(LOG_TAG, "canceling child notification %s",
75215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    prevChildNotificationId);
75315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        }
75415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    }
75515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
75615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
75715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                for (Map.Entry<Integer, WearableNotifications.Builder> entry
75815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        : msgNotifications.entrySet()) {
75915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    nm.notify(entry.getKey(), entry.getValue().build());
76015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    LogUtils.d(LOG_TAG, "notifying child notification %s", entry.getKey());
76115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
76215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
76315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                Set<Integer> childNotificationIds = new HashSet<Integer>();
76415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                childNotificationIds.addAll(msgNotifications.keySet());
76515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                sChildNotificationsMap.put(notificationKey, childNotificationIds);
76632fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            } else {
76732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "event info not configured - not notifying");
768d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
769d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
770d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (cursor != null) {
771d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                cursor.close();
772d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
773d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
774d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
775d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
776d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
777d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return an {@link Intent} which, if launched, will display the corresponding conversation
778d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
77909400efa442422299acf21abe20e3470f9d965abScott Kennedy    private static Intent createViewConversationIntent(final Context context, final Account account,
78009400efa442422299acf21abe20e3470f9d965abScott Kennedy            final Folder folder, final Cursor cursor) {
781d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (folder == null || account == null) {
7822f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.e(LOG_TAG, "createViewConversationIntent(): "
78313a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    + "Null account or folder.  account: %s folder: %s", account, folder);
784d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
785d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
786d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
787d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Intent intent;
788d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
789d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cursor == null) {
790259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewFolderIntent(context, folder.folderUri.fullUri, account);
791d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
792d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // A conversation cursor has been specified, so this intent is intended to be go
793d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // directly to the one new conversation
794d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
795d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the Conversation object
796d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
797259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewConversationIntent(context, conversation,
798259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri, account);
799d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
800d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
801d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return intent;
802d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
803d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
80461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getDefaultNotificationIcon(
80561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            final Context context, final Folder folder, final boolean multipleNew) {
806c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int resId;
80761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (folder.notificationIconResId != 0) {
808c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = folder.notificationIconResId;
80961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        } else if (multipleNew) {
810c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = R.drawable.ic_notification_multiple_mail_holo_dark;
811d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
812c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = R.drawable.ic_contact_picture;
813d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
814c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
815c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final Bitmap icon = getIcon(context, resId);
816c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
817c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        if (icon == null) {
818c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            LogUtils.e(LOG_TAG, "Couldn't decode notif icon res id %d", resId);
819c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        }
820c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
821d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
822d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
823d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
82461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getIcon(final Context context, final int resId) {
82561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap cachedIcon = sNotificationIcons.get(resId);
82661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (cachedIcon != null) {
82761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            return cachedIcon;
82861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        }
82961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
83061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resId);
83161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        sNotificationIcons.put(resId, icon);
83261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
83361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        return icon;
83461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    }
83561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
836d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void configureLatestEventInfoFromConversation(final Context context,
837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, final FolderPreferences folderPreferences,
83815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final NotificationCompat.Builder notification,
83915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final WearableNotifications.Builder summaryWearNotif,
84015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final Map<Integer, WearableNotifications.Builder> msgNotifications,
84115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final int summaryNotificationId, final Cursor conversationCursor,
842d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final PendingIntent clickIntent, final Intent notificationIntent,
843b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int unreadCount, final int unseenCount,
844d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder, final long when) {
845d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Resources res = context.getResources();
84626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountDisplayName = account.getDisplayName();
84726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountEmail = account.getEmailAddress();
848d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
84932fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        LogUtils.i(LOG_TAG, "Showing notification with unreadCount of %d and unseenCount of %d",
8502f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                unreadCount, unseenCount);
851d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
852d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String notificationTicker = null;
853d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
854d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Boolean indicating that this notification is for a non-inbox label.
855259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final boolean isInbox = folder.folderUri.fullUri.equals(account.settings.defaultInbox);
856d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
857d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Notification label name for user label notifications.
858d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String notificationLabelName = isInbox ? null : folder.name;
859d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
860d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unseenCount > 1) {
861d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Build the string that describes the number of new messages
862d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String newMessagesString = res.getString(R.string.new_messages, unseenCount);
863d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
864d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Use the default notification icon
865d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setLargeIcon(
86661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                    getDefaultNotificationIcon(context, folder, true /* multiple new messages */));
867d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
868d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The ticker initially start as the new messages string.
869d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = newMessagesString;
870d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
871d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The title of the notification is the new messages string
872d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setContentTitle(newMessagesString);
873d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
874c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy            // TODO(skennedy) Can we remove this check?
875d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (com.android.mail.utils.Utils.isRunningJellybeanOrLater()) {
876d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // For a new-style notification
877d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int maxNumDigestItems = context.getResources().getInteger(
878d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        R.integer.max_num_notification_digest_items);
879d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
880d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
88126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                notification.setSubText(
88226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
883d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
884d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final NotificationCompat.InboxStyle digest =
885d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new NotificationCompat.InboxStyle(notification);
886d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
88715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // Group by account.
88815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                String notificationGroupKey =
88915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        account.uri.toString() + "/" + folder.folderUri.fullUri;
89015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                summaryWearNotif.setGroup(notificationGroupKey,
89115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        WearableNotifications.GROUP_ORDER_SUMMARY);
89215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
893d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int numDigestItems = 0;
894d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                do {
895d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Conversation conversation = new Conversation(conversationCursor);
896d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
897d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!conversation.read) {
898d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        boolean multipleUnreadThread = false;
899d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // TODO(cwren) extract this pattern into a helper
900d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
901d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Cursor cursor = null;
902d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        MessageCursor messageCursor = null;
903d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
904d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final Uri.Builder uriBuilder = conversation.messageListUri.buildUpon();
905d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            uriBuilder.appendQueryParameter(
906d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.LABEL_QUERY_PARAMETER, notificationLabelName);
907d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor = context.getContentResolver().query(uriBuilder.build(),
908d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.MESSAGE_PROJECTION, null, null, null);
909d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            messageCursor = new MessageCursor(cursor);
910d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
911b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                            String from = "";
912d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            String fromAddress = "";
913d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
914d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
915d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                fromAddress = message.getFrom();
916b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                if (fromAddress == null) {
917b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                    fromAddress = "";
918b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                }
919d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                from = getDisplayableSender(fromAddress);
920d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
921d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (!message.read &&
924d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        !fromAddress.contentEquals(message.getFrom())) {
925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    multipleUnreadThread = true;
926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
927d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final SpannableStringBuilder sendersBuilder;
930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (multipleUnreadThread) {
931d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final int sendersLength =
932d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        res.getInteger(R.integer.swipe_senders_length);
933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
934d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = getStyledSenders(context, conversationCursor,
93526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                                        sendersLength, notificationAccountEmail);
936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
93712fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                sendersBuilder =
93812fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                        new SpannableStringBuilder(getWrappedFromString(from));
939d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
940d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final CharSequence digestLine = getSingleMessageInboxLine(context,
941d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    sendersBuilder.toString(),
942d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    conversation.subject,
943edd6c1a2807d2ade930dfd4622707298dc470d64Tony Mantler                                    conversation.getSnippet());
944d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            digest.addLine(digestLine);
945d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            numDigestItems++;
94615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
94715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            // Adding child notification for Wear.
94815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            NotificationCompat.Builder childNotif =
94915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    new NotificationCompat.Builder(context);
95015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            childNotif.setSmallIcon(R.drawable.stat_notify_email);
95115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            childNotif.setContentText(digestLine);
95215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
95315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            WearableNotifications.Builder childWearNotif =
95415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    new WearableNotifications.Builder(childNotif).setGroup(
95515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                            notificationGroupKey, numDigestItems);
95615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            int childNotificationId = getNotificationId(summaryNotificationId,
95715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    conversation.hashCode());
95815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
95915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            configureNotifForOneConversation(context, account, folderPreferences,
96015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    childNotif, childWearNotif, conversationCursor,
96115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    notificationIntent, folder, when, res,
96215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    notificationAccountDisplayName, notificationAccountEmail,
96315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    isInbox, notificationLabelName, childNotificationId);
96415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            msgNotifications.put(childNotificationId, childWearNotif);
965d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
966d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor != null) {
967d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                messageCursor.close();
968d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
969d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (cursor != null) {
970d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                cursor.close();
971d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
972d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
973d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
974d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } while (numDigestItems <= maxNumDigestItems && conversationCursor.moveToNext());
975d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
976d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
977d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setContentText(
97826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
98115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // For notifications for a single new conversation, we want to get the information
98215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // from the conversation
983d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
984d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Move the cursor to the most recent unread conversation
985d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            seekToLatestUnreadConversation(conversationCursor);
986d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
98715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notificationTicker = configureNotifForOneConversation(context, account,
98815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    folderPreferences, notification, summaryWearNotif, conversationCursor,
98915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationIntent, folder, when, res, notificationAccountDisplayName,
99015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationAccountEmail, isInbox, notificationLabelName,
99115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    summaryNotificationId);
99215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
993d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
99415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        // Build the notification ticker
99515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (notificationLabelName != null && notificationTicker != null) {
99615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // This is a per label notification, format the ticker with that information
99715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notificationTicker = res.getString(R.string.label_notification_ticker,
99815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationLabelName, notificationTicker);
99915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1000d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
100115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (notificationTicker != null) {
100215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // If we didn't generate a notification ticker, it will default to account name
100315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notification.setTicker(notificationTicker);
100415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
100515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
100615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        // Set the number in the notification
100715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (unreadCount > 1) {
100815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notification.setNumber(unreadCount);
100915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1010d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
101115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        notification.setContentIntent(clickIntent);
101215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
1013d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
101415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    /**
101515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     * Configure the notification for one conversation.  When there are multiple conversations,
101615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     * this method is used to configure bundled notification for Android Wear.
101715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     */
101815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    private static String configureNotifForOneConversation(Context context, Account account,
101915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            FolderPreferences folderPreferences, NotificationCompat.Builder notification,
102015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            WearableNotifications.Builder summaryWearNotif, Cursor conversationCursor,
102115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            Intent notificationIntent, Folder folder, long when, Resources res,
102215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            String notificationAccountDisplayName, String notificationAccountEmail, boolean isInbox,
102315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            String notificationLabelName, int notificationId) {
1024d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
102515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        String notificationTicker;
1026d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
102715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final Conversation conversation = new Conversation(conversationCursor);
1028d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
102915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        Cursor cursor = null;
103015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        MessageCursor messageCursor = null;
103115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        boolean multipleUnseenThread = false;
103215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        String from = null;
103315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        try {
103415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final Uri uri = conversation.messageListUri.buildUpon().appendQueryParameter(
103515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    UIProvider.LABEL_QUERY_PARAMETER, folder.persistentId).build();
103615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            cursor = context.getContentResolver().query(uri, UIProvider.MESSAGE_PROJECTION,
103715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    null, null, null);
103815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            messageCursor = new MessageCursor(cursor);
103915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Use the information from the last sender in the conversation that triggered
104015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // this notification.
104115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
104215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            String fromAddress = "";
104315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
104415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message = messageCursor.getMessage();
104515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                fromAddress = message.getFrom();
104615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                from = getDisplayableSender(fromAddress);
104715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setLargeIcon(
104815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        getContactIcon(context, from, getSenderAddress(fromAddress), folder));
104915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1050d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
105115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Assume that the last message in this conversation is unread
105215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            int firstUnseenMessagePos = messageCursor.getPosition();
105315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
105415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message = messageCursor.getMessage();
105515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final boolean unseen = !message.seen;
105615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (unseen) {
105715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    firstUnseenMessagePos = messageCursor.getPosition();
105815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    if (!multipleUnseenThread
105915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            && !fromAddress.contentEquals(message.getFrom())) {
106015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        multipleUnseenThread = true;
106192b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
106215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
106315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1064d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
106515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // TODO(skennedy) Can we remove this check?
106615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (Utils.isRunningJellybeanOrLater()) {
106715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For a new-style notification
1068d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
106915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (multipleUnseenThread) {
107015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // The title of a single conversation is the list of senders.
107115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    int sendersLength = res.getInteger(R.integer.swipe_senders_length);
1072d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
107315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    final SpannableStringBuilder sendersBuilder = getStyledSenders(
107415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            context, conversationCursor, sendersLength,
107515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            notificationAccountEmail);
107615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
107715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notification.setContentTitle(sendersBuilder);
107815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // For a single new conversation, the ticker is based on the sender's name.
107915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationTicker = sendersBuilder.toString();
1080d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
108115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    from = getWrappedFromString(from);
108215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // The title of a single message the sender.
108315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notification.setContentTitle(from);
108415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // For a single new conversation, the ticker is based on the sender's name.
108515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationTicker = from;
108615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
1087d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
108815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification content will be the subject of the conversation.
108915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setContentText(
109015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        getSingleMessageLittleText(context, conversation.subject));
1091d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
109215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification subtext will be the subject of the conversation for inbox
109315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications, or will based on the the label name for user label
109415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications.
109515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setSubText(isInbox ?
109615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        notificationAccountDisplayName : notificationLabelName);
1097d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
109815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (multipleUnseenThread) {
109915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notification.setLargeIcon(
110015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            getDefaultNotificationIcon(context, folder, true));
1101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
110215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final NotificationCompat.BigTextStyle bigText =
110315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        new NotificationCompat.BigTextStyle(notification);
110415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
110515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // Seek the message cursor to the first unread message
110615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message;
110715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (messageCursor.moveToPosition(firstUnseenMessagePos)) {
110815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    message = messageCursor.getMessage();
110915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    bigText.bigText(getSingleMessageBigText(context,
111015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            conversation.subject, message));
111115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                } else {
111215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    LogUtils.e(LOG_TAG, "Failed to load message");
111315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    message = null;
1114d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
111515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
111615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (message != null) {
111715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    final Set<String> notificationActions =
111815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folderPreferences.getNotificationActions(account);
111915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
112015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    NotificationActionUtils.addNotificationActions(context, notificationIntent,
112115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            notification, summaryWearNotif, account, conversation, message,
112215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folder, notificationId, when, notificationActions);
1123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
112415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            } else {
112515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For an old-style notification
1126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
112715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The title of a single conversation notification is built from both the sender
112815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // and subject of the new message.
112915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setContentTitle(getSingleMessageNotificationTitle(context,
113015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        from, conversation.subject));
1131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
113215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification content will be the subject of the conversation for inbox
113315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications, or will based on the the label name for user label
113415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications.
113515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setContentText(
113615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        isInbox ? notificationAccountDisplayName : notificationLabelName);
1137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
113815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For a single new conversation, the ticker is based on the sender's name.
113915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notificationTicker = from;
114015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
114115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        } finally {
114215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (messageCursor != null) {
114315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                messageCursor.close();
114415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
114515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (cursor != null) {
114615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                cursor.close();
114715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
114915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        return notificationTicker;
1150d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
115212fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    private static String getWrappedFromString(String from) {
115312fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        if (from == null) {
115412fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            LogUtils.e(LOG_TAG, "null from string in getWrappedFromString");
115512fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            from = "";
115612fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        }
1157963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        from = sBidiFormatter.unicodeWrap(from);
115812fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        return from;
115912fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    }
116012fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook
1161d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder getStyledSenders(final Context context,
1162d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Cursor conversationCursor, final int maxLength, final String account) {
1163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Conversation conversation = new Conversation(conversationCursor);
1164d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final com.android.mail.providers.ConversationInfo conversationInfo =
1165d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                conversation.conversationInfo;
1166d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ArrayList<SpannableString> senders = new ArrayList<SpannableString>();
1167d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sNotificationUnreadStyleSpan == null) {
1168d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationUnreadStyleSpan = new TextAppearanceSpan(
1169d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, R.style.NotificationSendersUnreadTextAppearance);
1170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationReadStyleSpan =
1171d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSendersReadTextAppearance);
1172d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SendersView.format(context, conversationInfo, "", maxLength, senders, null, null, account,
1174d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sNotificationUnreadStyleSpan, sNotificationReadStyleSpan, false);
1175d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1176d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return ellipsizeStyledSenders(context, senders);
1177d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1178d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sSendersSplitToken = null;
1180d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sElidedPaddingToken = null;
1181d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1182d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder ellipsizeStyledSenders(final Context context,
1183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            ArrayList<SpannableString> styledSenders) {
1184d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sSendersSplitToken == null) {
1185d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sSendersSplitToken = context.getString(R.string.senders_split_token);
1186d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sElidedPaddingToken = context.getString(R.string.elided_padding_token);
1187d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1188d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1189d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder builder = new SpannableStringBuilder();
1190d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString prevSender = null;
1191d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (SpannableString sender : styledSenders) {
1192d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (sender == null) {
11932f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.e(LOG_TAG, "null sender iterating over styledSenders");
1194d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                continue;
1195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
1197d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (SendersView.sElidedString.equals(sender.toString())) {
1198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
1200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (builder.length() > 0
1201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    && (prevSender == null || !SendersView.sElidedString.equals(prevSender
1202d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            .toString()))) {
1203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sSendersSplitToken + sender);
1205d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(sender);
1209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return builder;
1211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableString copyStyles(CharacterStyle[] spans, CharSequence newText) {
1214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString s = new SpannableString(newText);
1215d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (spans != null && spans.length > 0) {
1216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            s.setSpan(spans[0], 0, s.length(), 0);
1217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return s;
1219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Seeks the cursor to the position of the most recent unread conversation. If no unread
1223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversation is found, the position of the cursor will be restored, and false will be
1224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * returned.
1225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static boolean seekToLatestUnreadConversation(final Cursor cursor) {
1227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int initialPosition = cursor.getPosition();
1228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        do {
1229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
1230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!conversation.read) {
1231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return true;
1232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } while (cursor.moveToNext());
1234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Didn't find an unread conversation, reset the position.
1236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        cursor.moveToPosition(initialPosition);
1237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return false;
1238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senders Sender of the new message that triggered the notification.
1245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param snippet Snippet of the new message that triggered the notification
1247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageInboxLine(Context context,
1251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String senders, String subject, String snippet) {
1252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // TODO(cwren) finish this step toward commmon code with getSingleMessageBigText
1253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String subjectSnippet = !TextUtils.isEmpty(subject) ? subject : snippet;
1255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationPrimarySpan =
1257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
1258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(senders)) {
1260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the senders are empty, just use the subject/snippet.
1261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return subjectSnippet;
1262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(subjectSnippet)) {
1263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject/snippet is empty, just use the senders.
1264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(senders);
1265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan, 0, senders.length(), 0);
1266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String formatString = context.getResources().getString(
1270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.multiple_new_message_notification_item);
1271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSecondarySpan =
1272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
12748173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein            // senders is already individually unicode wrapped so it does not need to be done here
1275f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein            final String instantiatedString = String.format(formatString,
12768173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein                    senders,
1277963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein                    sBidiFormatter.unicodeWrap(subjectSnippet));
1278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(instantiatedString);
1280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isOrderReversed = formatString.indexOf("%2$s") <
1282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    formatString.indexOf("%1$s");
1283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int primaryOffset =
1284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(senders) :
1285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(senders));
1286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int secondaryOffset =
1287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(subjectSnippet) :
1288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(subjectSnippet));
1289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan,
1290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    primaryOffset, primaryOffset + senders.length(), 0);
1291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSecondarySpan,
1292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    secondaryOffset, secondaryOffset + subjectSnippet.length(), 0);
1293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1301f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * @return a {@link CharSequence} suitable for use in
1302f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * {@link NotificationCompat.Builder#setContentText}
1303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageLittleText(Context context, String subject) {
1305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final SpannableString spannableString = new SpannableString(subject);
1309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return spannableString;
1312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param message the {@link Message} to be displayed.
1320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageBigText(Context context, String subject,
1324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Message message) {
1325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String snippet = getMessageBodyWithoutElidedText(message);
1330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1331d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Change multiple newlines (with potential white space between), into a single new line
1332d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String collapsedSnippet =
1333d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";
1334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just use the snippet.
1337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return snippet;
1338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(collapsedSnippet)) {
1339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the snippet is empty, just use the subject.
1340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(subject);
1341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationBigTextFormat = context.getResources().getString(
1346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_big_text);
1347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") >
1351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationBigTextFormat.indexOf("%1$s");
1352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String bigText =
1353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    String.format(notificationBigTextFormat, subject, collapsedSnippet);
1354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(bigText);
1355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1356d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isSubjectFirst ? bigText.indexOf(subject) : bigText.lastIndexOf(subject));
1358d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan,
1359d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the title for a notification for a single new conversation
1367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1368d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sender Sender of the new message that triggered the notification.
1369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1370d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use as a {@link Notification} title.
1371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageNotificationTitle(Context context,
1373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String sender, String subject) {
1374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just set the title to the sender's information.
1377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return sender;
1378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationTitleFormat = context.getResources().getString(
1380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_title);
1381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectLast = notificationTitleFormat.indexOf("%2$s") >
1385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTitleFormat.indexOf("%1$s");
1386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String titleString = String.format(notificationTitleFormat, sender, subject);
1387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Format the string so the subject is using the secondaryText style
1389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString titleSpannable = new SpannableString(titleString);
1390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Find the offset of the subject.
1392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    isSubjectLast ? titleString.lastIndexOf(subject) : titleString.indexOf(subject);
1394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSubjectSpan =
1395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            titleSpannable.setSpan(notificationSubjectSpan,
1397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return titleSpannable;
1399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
14037f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears the notifications for the specified account/folder.
1404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
14057f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    public static void clearFolderNotification(Context context, Account account, Folder folder,
14067f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final boolean markSeen) {
140726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        LogUtils.v(LOG_TAG, "Clearing all notifications for %s/%s", account.getEmailAddress(),
140826a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                folder.name);
1409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
1410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
1411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.remove(key);
1412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
1413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
141415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final NotificationManagerCompat notificationManager =
141515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                NotificationManagerCompat.from(context);
1416b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        notificationManager.cancel(getNotificationId(account.getAccountManagerAccount(), folder));
14177f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
141815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        cancelChildNotifications(key, notificationManager);
141915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
14207f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        if (markSeen) {
14217f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            markSeen(context, folder);
14227f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
14237f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    }
14247f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
14257f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    /**
14267f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears all notifications for the specified account.
14277f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     */
1428b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static void clearAccountNotifications(final Context context,
1429b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final android.accounts.Account account) {
14302f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications for %s", account);
14317f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
14327f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
14337f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        // Find all NotificationKeys for this account
14347f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final ImmutableList.Builder<NotificationKey> keyBuilder = ImmutableList.builder();
14357f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
14367f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey key : notificationMap.keySet()) {
1437b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            if (account.equals(key.account.getAccountManagerAccount())) {
14387f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                keyBuilder.add(key);
14397f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            }
14407f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
14417f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
14427f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final List<NotificationKey> notificationKeys = keyBuilder.build();
14437f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
144415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final NotificationManagerCompat notificationManager =
144515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                NotificationManagerCompat.from(context);
14467f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
14477f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey notificationKey : notificationKeys) {
14487f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final Folder folder = notificationKey.folder;
14497f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationManager.cancel(getNotificationId(account, folder));
14507f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationMap.remove(notificationKey);
145115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
145215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            cancelChildNotifications(notificationKey, notificationManager);
14537f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
14547f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
14557f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        notificationMap.saveNotificationMap(context);
1456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
145815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    private static void cancelChildNotifications(NotificationKey key,
145915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationManagerCompat nm) {
146015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        Set<Integer> childNotifications = sChildNotificationsMap.get(key);
146115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (childNotifications != null) {
146215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            for (Integer childNotification : childNotifications) {
146315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                nm.cancel(childNotification);
146415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
146515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            sChildNotificationsMap.remove(key);
146615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
146715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
146815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static ArrayList<Long> findContacts(Context context, Collection<String> addresses) {
1470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<String> whereArgs = new ArrayList<String>();
1471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder whereBuilder = new StringBuilder();
1472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] questionMarks = new String[addresses.size()];
1473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereArgs.addAll(addresses);
1475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Arrays.fill(questionMarks, "?");
1476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereBuilder.append(Email.DATA1 + " IN (").
1477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(TextUtils.join(",", questionMarks)).
1478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(")");
1479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ContentResolver resolver = context.getContentResolver();
1481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor c = resolver.query(Email.CONTENT_URI,
1482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new String[]{Email.CONTACT_ID}, whereBuilder.toString(),
1483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                whereArgs.toArray(new String[0]), null);
1484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = new ArrayList<Long>();
1486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (c == null) {
1487d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return contactIds;
1488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
1490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (c.moveToNext()) {
1491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                contactIds.add(c.getLong(0));
1492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
1494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            c.close();
1495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1496d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return contactIds;
1497d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1498d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1499c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy    private static Bitmap getContactIcon(final Context context, final String displayName,
1500c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            final String senderAddress, final Folder folder) {
1501d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (senderAddress == null) {
1502d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
1503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1504c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Bitmap icon = null;
1506c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1507c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final List<Long> contactIds = findContacts( context, Arrays.asList(
1508c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                new String[] { senderAddress }));
1509c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1510c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        // Get the ideal size for this icon.
1511c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final Resources res = context.getResources();
1512c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int idealIconHeight =
1513c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
1514c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int idealIconWidth =
1515c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
1516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (contactIds != null) {
15184046d0695e102368201bb2f3a268a4f85c8bc4dbScott Kennedy            for (final long id : contactIds) {
1519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri contactUri =
1520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
1521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Uri photoUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
1522d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Cursor cursor = context.getContentResolver().query(
1523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        photoUri, new String[] { Photo.PHOTO }, null, null, null);
1524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor != null) {
1526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    try {
1527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (cursor.moveToFirst()) {
15284046d0695e102368201bb2f3a268a4f85c8bc4dbScott Kennedy                            final byte[] data = cursor.getBlob(0);
1529d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (data != null) {
1530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                icon = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
1531d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null && icon.getHeight() < idealIconHeight) {
1532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    // We should scale this image to fit the intended size
1533d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    icon = Bitmap.createScaledBitmap(
1534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                            icon, idealIconWidth, idealIconHeight, true);
1535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (icon != null) {
1537d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
1538d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
1539d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1540d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1541d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } finally {
1542d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        cursor.close();
1543d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1544d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1547c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1548c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        if (icon == null) {
1549c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            // Make a colorful tile!
1550c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            final Dimensions dimensions = new Dimensions(idealIconWidth, idealIconHeight,
1551c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                    Dimensions.SCALE_ONE);
1552c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1553fe23512c2be21d7c87677f5263078021002d89cbScott Kennedy            icon = new LetterTileProvider(context).getLetterTile(dimensions,
1554c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy                    displayName, senderAddress);
1555c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        }
1556c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (icon == null) {
1558c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            // Icon should be the default mail icon.
155961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            icon = getDefaultNotificationIcon(context, folder, false /* single new message */);
1560d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1561d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
1562d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getMessageBodyWithoutElidedText(final Message message) {
15655e7e88b9f5ac1f239753e13ffb3ee4b249722c9aScott Kennedy        return getMessageBodyWithoutElidedText(message.getBodyAsHtml());
1566d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1567d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1568d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static String getMessageBodyWithoutElidedText(String html) {
1569d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(html)) {
1570d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return "";
1571d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Get the html "tree" for this message body
1573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final HtmlTree htmlTree = com.android.mail.utils.Utils.getHtmlTree(html);
1574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        htmlTree.setPlainTextConverterFactory(MESSAGE_CONVERTER_FACTORY);
1575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return htmlTree.getPlainText();
1577d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1578d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void markSeen(final Context context, final Folder folder) {
1580259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final Uri uri = folder.folderUri.fullUri;
1581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ContentValues values = new ContentValues(1);
1583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        values.put(UIProvider.ConversationColumns.SEEN, 1);
1584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        context.getContentResolver().update(uri, values, null, null);
1586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1589d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns a displayable string representing
1590d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the message sender. It has a preference toward showing the name,
1591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * but will fall back to the address if that is all that is available.
1592d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1593d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getDisplayableSender(String sender) {
1594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1595d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1596d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String displayableSender = address.getName();
1597390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1598390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        if (!TextUtils.isEmpty(displayableSender)) {
1599821e578a71c7015646522e729600618f0ec16fc0Tony Mantler            return Address.decodeAddressPersonal(displayableSender);
1600d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1601390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1602390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        // If that fails, default to the sender address.
1603390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        displayableSender = address.getAddress();
1604390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1608d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = sender;
1609d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1610d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return displayableSender;
1611d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1613d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns only the address portion of a message sender.
1615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getSenderAddress(String sender) {
1617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String tokenizedAddress = address.getAddress();
1620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(tokenizedAddress)) {
1624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            tokenizedAddress = sender;
1625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return tokenizedAddress;
1627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1628d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1629b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static int getNotificationId(final android.accounts.Account account,
1630b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final Folder folder) {
1631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return 1 ^ account.hashCode() ^ folder.hashCode();
1632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
163415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    private static int getNotificationId(int summaryNotificationId, int childHashCode) {
163515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        return summaryNotificationId ^ childHashCode;
163615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
163715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1638d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationKey {
1639d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Account account;
1640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Folder folder;
1641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public NotificationKey(Account account, Folder folder) {
1643d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.account = account;
1644d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.folder = folder;
1645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public boolean equals(Object other) {
1649d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!(other instanceof NotificationKey)) {
1650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return false;
1651d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key = (NotificationKey) other;
1653b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            return account.getAccountManagerAccount().equals(key.account.getAccountManagerAccount())
1654b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    && folder.equals(key.folder);
1655d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public String toString() {
165926a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            return account.getDisplayName() + " " + folder.name;
1660d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public int hashCode() {
1664b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int accountHashCode = account.getAccountManagerAccount().hashCode();
1665d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int folderHashCode = folder.hashCode();
1666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return accountHashCode ^ folderHashCode;
1667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1668d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1670d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1671d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Contains the logic for converting the contents of one HtmlTree into
1672d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * plaintext.
1673d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1674d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static class MailMessagePlainTextConverter extends HtmlTree.DefaultPlainTextConverter {
1675d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings for parsing html message bodies
1676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_NAME = "div";
1677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME = "class";
1678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE = "elided-text";
1679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HTML.Attribute ELIDED_TEXT_ATTRIBUTE =
1681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new HTML.Attribute(ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME, HTML.Attribute.NO_TYPE);
1682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1683d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HtmlDocument.Node ELIDED_TEXT_REPLACEMENT_NODE =
1684d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                HtmlDocument.createSelfTerminatingTag(HTML4.BR_ELEMENT, null, null, null);
1685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private int mEndNodeElidedTextBlock = -1;
1687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1688d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1689d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void addNode(HtmlDocument.Node n, int nodeNum, int endNum) {
1690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we are in the middle of an elided text block, don't add this node
1691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (nodeNum < mEndNodeElidedTextBlock) {
1692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (nodeNum == mEndNodeElidedTextBlock) {
1694d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                super.addNode(ELIDED_TEXT_REPLACEMENT_NODE, nodeNum, endNum);
1695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1698d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this tag starts another elided text block, we want to remember the end
1699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (n instanceof HtmlDocument.Tag) {
1700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                boolean foundElidedTextTag = false;
1701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HtmlDocument.Tag htmlTag = (HtmlDocument.Tag)n;
1702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HTML.Element htmlElement = htmlTag.getElement();
1703d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (ELIDED_TEXT_ELEMENT_NAME.equals(htmlElement.getName())) {
1704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Make sure that the class is what is expected
1705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final List<HtmlDocument.TagAttribute> attributes =
1706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            htmlTag.getAttributes(ELIDED_TEXT_ATTRIBUTE);
1707d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    for (HtmlDocument.TagAttribute attribute : attributes) {
1708d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE.equals(
1709d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                attribute.getValue())) {
1710d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Found an "elided-text" div.  Remember information about this tag
1711d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            mEndNodeElidedTextBlock = endNum;
1712d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            foundElidedTextTag = true;
1713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            break;
1714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1715d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1716d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1717d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1718d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (foundElidedTextTag) {
1719d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return;
1720d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1721d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1722d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1723c56b233013cf107c702ef9f61305282670ad804aScott Kennedy            super.addNode(n, nodeNum, endNum);
1724d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1725d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1726d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1727d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1728d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * During account setup in Email, we may not have an inbox yet, so the notification setting had
1729d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * to be stored in {@link AccountPreferences}. If it is still there, we need to move it to the
1730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link FolderPreferences} now.
1731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void moveNotificationSetting(final AccountPreferences accountPreferences,
1733d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences) {
1734d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (accountPreferences.isDefaultInboxNotificationsEnabledSet()) {
1735d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this setting has been changed some other way, don't overwrite it
1736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.isNotificationsEnabledSet()) {
1737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean notificationsEnabled =
1738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        accountPreferences.getDefaultInboxNotificationsEnabled();
1739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                folderPreferences.setNotificationsEnabled(notificationsEnabled);
1741d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1743d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            accountPreferences.clearDefaultInboxNotificationsEnabled();
1744d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1745d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1746d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
1747