NotificationUtils.java revision e806c9447c7137d2a7a828e7ccdc1f8961aa1c2a
1d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/*
2d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Copyright (C) 2013 The Android Open Source Project
3d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
4d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
5d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * you may not use this file except in compliance with the License.
6d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * You may obtain a copy of the License at
7d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
8d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
9d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
10d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Unless required by applicable law or agreed to in writing, software
11d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
12d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * See the License for the specific language governing permissions and
14d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * limitations under the License.
15d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
16d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypackage com.android.mail.utils;
17d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
18d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.Notification;
19d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.app.PendingIntent;
20d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentResolver;
21d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentUris;
22d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.ContentValues;
23d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Context;
24d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.Intent;
25d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.content.res.Resources;
26d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.database.Cursor;
27d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.Bitmap;
28d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.graphics.BitmapFactory;
29d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.net.Uri;
308913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sappersteinimport android.os.Looper;
31d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract;
32d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.provider.ContactsContract.CommonDataKinds.Email;
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.support.v4.app.NotificationCompat;
34439aa5d7d9226de4682c3697c68c0703b8847518Alan Lauimport android.support.v4.app.NotificationManagerCompat;
35f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sappersteinimport android.support.v4.text.BidiFormatter;
368f10ced86b3c8e7229900d6089f56bf12b026116Andy Huangimport android.support.v4.util.ArrayMap;
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableString;
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.SpannableStringBuilder;
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.TextUtils;
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.CharacterStyle;
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.text.style.TextAppearanceSpan;
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.util.Pair;
4361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedyimport android.util.SparseArray;
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
45821e578a71c7015646522e729600618f0ec16fc0Tony Mantlerimport com.android.emailcommon.mail.Address;
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.EmailAddress;
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.MailIntentService;
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.R;
494fe0af81874976a1995191321e35c844b2229811Andy Huangimport com.android.mail.analytics.Analytics;
5089bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sappersteinimport com.android.mail.browse.ConversationItemView;
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.MessageCursor;
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.browse.SendersView;
538913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sappersteinimport com.android.mail.photo.ContactPhotoFetcher;
54fe23512c2be21d7c87677f5263078021002d89cbScott Kennedyimport com.android.mail.photomanager.LetterTileProvider;
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.AccountPreferences;
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.FolderPreferences;
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.preferences.MailPrefs;
58d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Account;
59d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Conversation;
60d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Folder;
61d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.Message;
62d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.providers.UIProvider;
63c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedyimport com.android.mail.ui.ImageCanvas.Dimensions;
64d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport com.android.mail.utils.NotificationActionUtils.NotificationAction;
651bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HTML;
661bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HTML4;
671bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HtmlDocument;
681bdbfefe4b144c7b031a1d9242a0fa061a0ae6b5Scott Kennedyimport com.google.android.mail.common.html.parser.HtmlTree;
69ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sappersteinimport com.google.common.base.Objects;
707f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedyimport com.google.common.collect.ImmutableList;
7109400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Lists;
7209400efa442422299acf21abe20e3470f9d965abScott Kennedyimport com.google.common.collect.Sets;
73644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lauimport com.google.common.io.Closeables;
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
75644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lauimport java.io.InputStream;
76644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lauimport java.lang.ref.WeakReference;
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.ArrayList;
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Arrays;
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Collection;
8015490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.HashMap;
8115490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.HashSet;
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.List;
8315490239c82cdb4765e87e926c8c1945adedd1edAlan Lauimport java.util.Map;
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.Set;
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport java.util.concurrent.ConcurrentHashMap;
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class NotificationUtils {
88c7d754703a0b5c2ca4b4dc9675d733881f85b746Scott Kennedy    public static final String LOG_TAG = "NotifUtils";
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
908913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static final String EXTRA_UNREAD_COUNT = "unread-count";
918913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static final String EXTRA_UNSEEN_COUNT = "unseen-count";
928913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static final String EXTRA_GET_ATTENTION = "get-attention";
938913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** Contains a list of <(account, label), unread conversations> */
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static NotificationMap sActiveNotificationMap = null;
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
9761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static final SparseArray<Bitmap> sNotificationIcons = new SparseArray<Bitmap>();
98644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static WeakReference<Bitmap> sDefaultWearableBg = new WeakReference<Bitmap>(null);
99d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static TextAppearanceSpan sNotificationUnreadStyleSpan;
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharacterStyle sNotificationReadStyleSpan;
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /** A factory that produces a plain text converter that removes elided text. */
104d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static final HtmlTree.PlainTextConverterFactory MESSAGE_CONVERTER_FACTORY =
105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            new HtmlTree.PlainTextConverterFactory() {
106d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                @Override
107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                public HtmlTree.PlainTextConverter createInstance() {
108d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return new MailMessagePlainTextConverter();
109d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
110d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            };
111d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
112963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein    private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
113f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein
114e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    // Maps summary notification to conversation notification ids.
115e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    private static Map<NotificationKey, Set<Integer>> sConversationNotificationMap =
11615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            new HashMap<NotificationKey, Set<Integer>>();
11715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
118d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
119d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Clears all notifications in response to the user tapping "Clear" in the status bar.
120d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
121d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void clearAllNotfications(Context context) {
1222f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications.");
123d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
124d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.clear();
125d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
126d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
127d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
128d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
129d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns the notification map, creating it if necessary.
130d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static synchronized NotificationMap getNotificationMap(Context context) {
132d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sActiveNotificationMap == null) {
133d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap = new NotificationMap();
134d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
135d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // populate the map from the cached data
136d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sActiveNotificationMap.loadNotificationMap(context);
137d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
138d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return sActiveNotificationMap;
139d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
140d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
141d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
142d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Class representing the existing notifications, and the number of unread and
143d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * unseen conversations that triggered each.
144d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1452354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee    private static final class NotificationMap {
146d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
147d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String NOTIFICATION_PART_SEPARATOR = " ";
148d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final int NUM_NOTIFICATION_PARTS= 4;
1492354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        private final ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>> mMap =
1502354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            new ConcurrentHashMap<NotificationKey, Pair<Integer, Integer>>();
151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
152d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
1532354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the number of key values pairs in the inner map.
1542354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1552354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public int size() {
1562354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.size();
1572354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1582354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1592354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1602354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns a set of key values.
1612354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1622354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Set<NotificationKey> keySet() {
1632354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.keySet();
1642354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1652354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1662354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1672354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Remove the key from the inner map and return its value.
1682354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1692354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be removed.
1702354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return The value associated with this key.
1712354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1722354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public Pair<Integer, Integer> remove(NotificationKey key) {
1732354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.remove(key);
1742354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1752354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1762354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1772354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Clear all key-value pairs in the map.
1782354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1792354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public void clear() {
1802354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.clear();
1812354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1822354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1832354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1842354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Discover if a key-value pair with this key exists.
1852354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         *
1862354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @param key The key {@link NotificationKey} to be checked.
1872354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * @return If a key-value pair with this key exists in the map.
1882354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         */
1892354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        public boolean containsKey(NotificationKey key) {
1902354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            return mMap.containsKey(key);
1912354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        }
1922354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee
1932354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee        /**
1942354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread count for the given NotificationKey.
195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
196d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnread(NotificationKey key) {
1972354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
198d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.first : null;
199d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
200d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
201d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
2022354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee         * Returns the unread unseen count for the given NotificationKey.
203d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public Integer getUnseen(NotificationKey key) {
2052354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Pair<Integer, Integer> value = mMap.get(key);
206d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return value != null ? value.second : null;
207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
208d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
209d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
210d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Store the unread and unseen value for the given NotificationKey
211d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void put(NotificationKey key, int unread, int unseen) {
213d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Pair<Integer, Integer> value =
214d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Pair<Integer, Integer>(Integer.valueOf(unread), Integer.valueOf(unseen));
2152354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            mMap.put(key, value);
216d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
217d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
219d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Populates the notification map with previously cached data.
220d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
221d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void loadNotificationMap(final Context context) {
222d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
223d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = mailPrefs.getActiveNotificationSet();
224d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (notificationSet != null) {
225d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                for (String notificationEntry : notificationSet) {
226d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Get the parts of the string that make the notification entry
227d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] notificationParts =
228d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            TextUtils.split(notificationEntry, NOTIFICATION_PART_SEPARATOR);
229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationParts.length == NUM_NOTIFICATION_PARTS) {
230d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri accountUri = Uri.parse(notificationParts[0]);
231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor accountCursor = context.getContentResolver().query(
232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                accountUri, UIProvider.ACCOUNTS_PROJECTION, null, null, null);
233d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Account account;
234d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
235d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (accountCursor.moveToFirst()) {
236d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                account = new Account(accountCursor);
237d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
238d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
239d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
240d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
241d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            accountCursor.close();
242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Uri folderUri = Uri.parse(notificationParts[1]);
245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Cursor folderCursor = context.getContentResolver().query(
246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folderUri, UIProvider.FOLDERS_PROJECTION, null, null, null);
247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Folder folder;
248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (folderCursor.moveToFirst()) {
250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                folder = new Folder(folderCursor);
251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                continue;
253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
255d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            folderCursor.close();
256d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final NotificationKey key = new NotificationKey(account, folder);
259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unreadValue = Integer.valueOf(notificationParts[2]);
260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        final Integer unseenValue = Integer.valueOf(notificationParts[3]);
2612354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                        put(key, unreadValue, unseenValue);
262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        /**
268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         * Cache the notification map.
269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy         */
270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public synchronized void saveNotificationMap(Context context) {
271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Set<String> notificationSet = Sets.newHashSet();
2722354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee            final Set<NotificationKey> keys = mMap.keySet();
273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey key : keys) {
2742354fd98dec04736bd35c1ec8c130cb1aa18ff9cAnthony Lee                final Pair<Integer, Integer> value = mMap.get(key);
275d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unreadCount = value.first;
276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Integer unseenCount = value.second;
277ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                if (unreadCount != null && unseenCount != null) {
278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final String[] partValues = new String[] {
279259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            key.account.uri.toString(), key.folder.folderUri.fullUri.toString(),
280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            unreadCount.toString(), unseenCount.toString()};
281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationSet.add(TextUtils.join(NOTIFICATION_PART_SEPARATOR, partValues));
282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final MailPrefs mailPrefs = MailPrefs.get(context);
285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            mailPrefs.cacheActiveNotificationSet(notificationSet);
286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return the title of this notification with each account and the number of unread and unseen
291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversations for it. Also remove any account in the map that has 0 unread.
292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String createNotificationString(NotificationMap notifications) {
294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder result = new StringBuilder();
295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int i = 0;
296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keysToRemove = Sets.newHashSet();
297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : notifications.keySet()) {
298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unread = notifications.getUnread(key);
299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            Integer unseen = notifications.getUnseen(key);
300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unread == null || unread.intValue() == 0) {
301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                keysToRemove.add(key);
302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (i > 0) result.append(", ");
304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                result.append(key.toString() + " (" + unread + ", " + unseen + ")");
305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                i++;
306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey key : keysToRemove) {
310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notifications.remove(key);
311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return result.toString();
314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts and cancel them.
318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void cancelAllNotifications(Context context) {
3202f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "cancelAllNotifications - cancelling all");
32115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        nm.cancelAll();
323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        clearAllNotfications(context);
324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, cancel them, and repost.
328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * This happens when locale changes.
329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     **/
3308913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static void cancelAndResendNotificationsOnLocaleChange(
3318913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            Context context, final ContactPhotoFetcher photoFetcher) {
332963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        LogUtils.d(LOG_TAG, "cancelAndResendNotificationsOnLocaleChange");
333963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        sBidiFormatter = BidiFormatter.getInstance();
3348913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        resendNotifications(context, true, null, null, photoFetcher);
335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Get all notifications for all accounts, optionally cancel them, and repost.
339ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * This happens when locale changes. If you only want to resend messages from one
340ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * account-folder pair, pass in the account and folder that should be resent.
341ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All other account-folder pairs will not have their notifications resent.
342ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * All notifications will be resent if account or folder is null.
343ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *
344ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param context Current context.
345ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param cancelExisting True, if all notifications should be canceled before resending.
346ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                       False, otherwise.
347ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param accountUri The {@link Uri} of the {@link Account} of the notification
348ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                   upon which an action occurred.
349ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     * @param folderUri The {@link Uri} of the {@link Folder} of the notification
350ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     *                  upon which an action occurred.
351ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein     */
352ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein    public static void resendNotifications(Context context, final boolean cancelExisting,
3538913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final Uri accountUri, final FolderUri folderUri,
3548913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final ContactPhotoFetcher photoFetcher) {
3552f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "resendNotifications ");
3562638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
357d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cancelExisting) {
3582f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - cancelling all");
35915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            nm.cancelAll();
361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Re-validate the notifications.
363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Set<NotificationKey> keys = notificationMap.keySet();
365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (NotificationKey notification : keys) {
366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Folder folder = notification.folder;
367b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
368b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(notification.account.getAccountManagerAccount(), folder);
369ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein
370ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // Only resend notifications if the notifications are from the same folder
371ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            // and same account as the undo notification that was previously displayed.
37226b29ef558cce109c51584081c52781760a02dd9Scott Kennedy            if (accountUri != null && !Objects.equal(accountUri, notification.account.uri) &&
373259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folderUri != null && !Objects.equal(folderUri, folder.folderUri)) {
3742f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "resendNotifications - not resending %s / %s"
3752638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy                        + " because it doesn't match %s / %s",
376259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                        notification.account.uri, folder.folderUri, accountUri, folderUri);
377ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                continue;
378ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein            }
379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
3802f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.d(LOG_TAG, "resendNotifications - resending %s / %s",
381259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    notification.account.uri, folder.folderUri);
3822638b48e063e469d48daf6a7c0d3a9b2d56d7beaScott Kennedy
383d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final NotificationAction undoableAction =
384d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sUndoNotifications.get(notificationId);
385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (undoableAction == null) {
386ddd17bc2963b67ba07dfd1e2d9b1f17abf8c8e4aAndrew Sapperstein                validateNotifications(context, folder, notification.account, true,
3878913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        false, notification, photoFetcher);
388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Create an undo notification
390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.createUndoNotification(context, undoableAction);
391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications for the specified account.
397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void validateAccountNotifications(Context context, String account) {
3992f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "validateAccountNotifications - %s", account);
4004162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        List<NotificationKey> notificationsToCancel = Lists.newArrayList();
402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Iterate through the notification map to see if there are any entries that correspond to
403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // labels that are not in the sync set.
404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Set<NotificationKey> keys = notificationMap.keySet();
406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final AccountPreferences accountPreferences = new AccountPreferences(context, account);
407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final boolean enabled = accountPreferences.areNotificationsEnabled();
408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (!enabled) {
409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Cancel all notifications for this account
410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
411b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                if (notification.account.getAccountManagerAccount().name.equals(account)) {
412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationsToCancel.add(notification);
413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Iterate through the notification map to see if there are any entries that
417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // correspond to labels that are not in the notification set.
418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : keys) {
419b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                if (notification.account.getAccountManagerAccount().name.equals(account)) {
420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // If notification is not enabled for this label, remember this NotificationKey
421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // to later cancel the notification, and remove the entry from the map
422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Folder folder = notification.folder;
423259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    final boolean isInbox = folder.folderUri.equals(
424259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                            notification.account.settings.defaultInbox);
425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final FolderPreferences folderPreferences = new FolderPreferences(
426afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                            context, notification.account.getEmailAddress(), folder, isInbox);
427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!folderPreferences.areNotificationsEnabled()) {
429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        notificationsToCancel.add(notification);
430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Cancel & remove the invalid notifications.
436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (notificationsToCancel.size() > 0) {
437b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            for (NotificationKey notification : notificationsToCancel) {
439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final Folder folder = notification.folder;
440b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                final int notificationId =
441b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                        getNotificationId(notification.account.getAccountManagerAccount(), folder);
4422f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.d(LOG_TAG, "validateAccountNotifications - cancelling %s / %s",
44326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        notification.account.getEmailAddress(), folder.persistentId);
444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notificationMap.remove(notification);
446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sUndoNotifications.remove(notificationId);
447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
448b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau
449e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                cancelConversationNotifications(notification, nm);
450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.saveNotificationMap(context);
452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
4558913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static void sendSetNewEmailIndicatorIntent(Context context, final int unreadCount,
4568913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final int unseenCount, final Account account, final Folder folder,
4578913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final boolean getAttention) {
4588913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        LogUtils.i(LOG_TAG, "sendSetNewEmailIndicator account: %s, folder: %s",
4598913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
4608913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                LogUtils.sanitizeName(LOG_TAG, folder.name));
4618913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
4628913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final Intent intent = new Intent(MailIntentService.ACTION_SEND_SET_NEW_EMAIL_INDICATOR);
4638913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.setPackage(context.getPackageName()); // Make sure we only deliver this to ourselves
4648913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(EXTRA_UNREAD_COUNT, unreadCount);
4658913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(EXTRA_UNSEEN_COUNT, unseenCount);
4668913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(Utils.EXTRA_ACCOUNT, account);
4678913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(Utils.EXTRA_FOLDER, folder);
4688913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        intent.putExtra(EXTRA_GET_ATTENTION, getAttention);
4698913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        context.startService(intent);
4708913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    }
4718913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
4738913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein     * Display only one notification. Should only be called from
4748913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein     * {@link com.android.mail.MailIntentService}. Use {@link #sendSetNewEmailIndicatorIntent}
4758913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein     * if you need to perform this action anywhere else.
476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void setNewEmailIndicator(Context context, final int unreadCount,
478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int unseenCount, final Account account, final Folder folder,
4798913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final boolean getAttention, final ContactPhotoFetcher photoFetcher) {
4802f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.d(LOG_TAG, "setNewEmailIndicator unreadCount = %d, unseenCount = %d, account = %s,"
48126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                + " folder = %s, getAttention = %b", unreadCount, unseenCount,
48226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                account.getEmailAddress(), folder.folderUri, getAttention);
4834162f83633a001cf03ff7bbb5e1abae92392049bScott Kennedy
484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        boolean ignoreUnobtrusiveSetting = false;
485d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
486b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        final int notificationId = getNotificationId(account.getAccountManagerAccount(), folder);
487dab8a94a939988e99597845e89cfe3fd1da0ab88Scott Kennedy
488d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Update the notification map
489d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
490d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unreadCount == 0) {
49226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            LogUtils.d(LOG_TAG, "setNewEmailIndicator - cancelling %s / %s",
49326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    account.getEmailAddress(), folder.persistentId);
494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.remove(key);
495b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau
496b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau            NotificationManagerCompat nm = NotificationManagerCompat.from(context);
497b9027cc33cd7b4c5c6f9f3efc8e044e49b491dc6Alan Lau            nm.cancel(notificationId);
498e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            cancelConversationNotifications(key, nm);
499d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
500b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook            LogUtils.d(LOG_TAG, "setNewEmailIndicator - update count for: %s / %s " +
50126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    "to: unread: %d unseen %d", account.getEmailAddress(), folder.persistentId,
502b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    unreadCount, unseenCount);
503d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!notificationMap.containsKey(key)) {
504d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // This account previously didn't have any unread mail; ignore the "unobtrusive
505d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notifications" setting and play sound and/or vibrate the device even if a
506d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // notification already exists (bug 2412348).
507b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "setNewEmailIndicator - ignoringUnobtrusiveSetting");
508d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                ignoreUnobtrusiveSetting = true;
509d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
510d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationMap.put(key, unreadCount, unseenCount);
511d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
512d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
513d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
514d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
5152f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.v(LOG_TAG, "New email: %s mapSize: %d getAttention: %b",
516d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    createNotificationString(notificationMap), notificationMap.size(),
517d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    getAttention);
518d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
519d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
520d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (NotificationActionUtils.sUndoNotifications.get(notificationId) == null) {
521d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            validateNotifications(context, folder, account, getAttention, ignoreUnobtrusiveSetting,
5228913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    key, photoFetcher);
523d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
524d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
525d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
526d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
527d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Validate the notifications notification.
528d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
529d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void validateNotifications(Context context, final Folder folder,
530d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, boolean getAttention, boolean ignoreUnobtrusiveSetting,
5318913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            NotificationKey key, final ContactPhotoFetcher photoFetcher) {
532d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
53315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
534d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
535d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
536d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
53732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification: %s mapSize: %d "
538b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "folder: %s getAttention: %b ignoreUnobtrusive: %b",
539b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    createNotificationString(notificationMap),
540b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    notificationMap.size(), folder.name, getAttention, ignoreUnobtrusiveSetting);
54132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        } else {
54232fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Validating Notification, mapSize: %d "
543b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    + "getAttention: %b ignoreUnobtrusive: %b", notificationMap.size(),
544b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                    getAttention, ignoreUnobtrusiveSetting);
545d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
546d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // The number of unread messages for this account and label.
547d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unread = notificationMap.getUnread(key);
548d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int unreadCount = unread != null ? unread.intValue() : 0;
549d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Integer unseen = notificationMap.getUnseen(key);
550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        int unseenCount = unseen != null ? unseen.intValue() : 0;
551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
552d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor cursor = null;
553d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
554d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
555d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Uri.Builder uriBuilder = folder.conversationListUri.buildUpon();
556d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            uriBuilder.appendQueryParameter(
557d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.SEEN_QUERY_PARAMETER, Boolean.FALSE.toString());
5580be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // Do not allow this quick check to disrupt any active network-enabled conversation
5590be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            // cursor.
5600be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang            uriBuilder.appendQueryParameter(
5610be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    UIProvider.ConversationListQueryParameters.USE_NETWORK,
5620be9243458fc713bc34b3ab76bfb0529f7e67871Andy Huang                    Boolean.FALSE.toString());
563d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cursor = context.getContentResolver().query(uriBuilder.build(),
564d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    UIProvider.CONVERSATION_PROJECTION, null, null, null);
5653a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            if (cursor == null) {
5663a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                // This folder doesn't exist.
56732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
56832fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        "The cursor is null, so the specified folder probably does not exist");
5693a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                clearFolderNotification(context, account, folder, false);
5703a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu                return;
5713a7b45bd1a03ce6158d766ac8837f1c8cb3758e1Yu Ping Hu            }
572d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int cursorUnseenCount = cursor.getCount();
573d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
574d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Make sure the unseen count matches the number of items in the cursor.  But, we don't
575d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // want to overwrite a 0 unseen count that was specified in the intent
576d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount != 0 && unseenCount != cursorUnseenCount) {
57732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG,
5782f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        "Unseen count doesn't match cursor count.  unseen: %d cursor count: %d",
579d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        unseenCount, cursorUnseenCount);
580d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = cursorUnseenCount;
581d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
582d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
583d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // For the purpose of the notifications, the unseen count should be capped at the num of
584d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // unread conversations.
585d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount > unreadCount) {
586d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                unseenCount = unreadCount;
587d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
588d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
589b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int notificationId =
590b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    getNotificationId(account.getAccountManagerAccount(), folder);
591d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
59215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationKey notificationKey = new NotificationKey(account, folder);
59315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
594d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unseenCount == 0) {
59532fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "validateNotifications - cancelling account %s / folder %s",
59626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
59732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                        LogUtils.sanitizeName(LOG_TAG, folder.persistentId));
598d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                nm.cancel(notificationId);
599e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                cancelConversationNotifications(notificationKey, nm);
60015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
601d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
602d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
603d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
604d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // We now have all we need to create the notification and the pending intent
605d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            PendingIntent clickIntent;
606d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
607d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
608439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            NotificationCompat.WearableExtender wearableExtender =
609439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    new NotificationCompat.WearableExtender();
610c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            Map<Integer, NotificationBuilders> msgNotifications =
611c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                    new ArrayMap<Integer, NotificationBuilders>();
612d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setSmallIcon(R.drawable.stat_notify_email);
61326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            notification.setTicker(account.getDisplayName());
614d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
615d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long when;
616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final long oldWhen =
618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    NotificationActionUtils.sNotificationTimestamps.get(notificationId);
619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (oldWhen != 0) {
620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = oldWhen;
621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                when = System.currentTimeMillis();
623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setWhen(when);
626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The timestamp is now stored in the notification, so we can remove it from here
628d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationActionUtils.sNotificationTimestamps.delete(notificationId);
629d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
630d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Dispatch a CLEAR_NEW_MAIL_NOTIFICATIONS intent if the user taps the "X" next to a
631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // notification.  Also this intent gets fired when the user taps on a notification as
632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // the AutoCancel flag has been set
633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Intent cancelNotificationIntent =
634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
635d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            cancelNotificationIntent.setPackage(context.getPackageName());
63609400efa442422299acf21abe20e3470f9d965abScott Kennedy            cancelNotificationIntent.setData(Utils.appendVersionQueryParameter(context,
637259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri));
63848cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
63948cfe4613549cafdf36e2a524afba730522bf291Scott Kennedy            cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder);
640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setDeleteIntent(PendingIntent.getService(
642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, notificationId, cancelNotificationIntent, 0));
643d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
644d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Ensure that the notification is cleared when the user selects it
645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setAutoCancel(true);
646d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
647d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            boolean eventInfoConfigured = false;
648d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
649259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            final boolean isInbox = folder.folderUri.equals(account.settings.defaultInbox);
650d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences =
651afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                    new FolderPreferences(context, account.getEmailAddress(), folder, isInbox);
652d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (isInbox) {
654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
655afb10d038c70c0a8646f0315836c49c704765ff9Tony Mantler                        new AccountPreferences(context, account.getEmailAddress());
656d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                moveNotificationSetting(accountPreferences, folderPreferences);
657d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
658d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
659d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.areNotificationsEnabled()) {
66032fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "Notifications are disabled for this folder; not notifying");
661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // Don't notify
662d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
664d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
665d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (unreadCount > 0) {
666d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // How can I order this properly?
667d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (cursor.moveToNext()) {
668bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    final Intent notificationIntent;
669d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
670bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    // Launch directly to the conversation, if there is only 1 unseen conversation
6714fe0af81874976a1995191321e35c844b2229811Andy Huang                    final boolean launchConversationMode = (unseenCount == 1);
6724fe0af81874976a1995191321e35c844b2229811Andy Huang                    if (launchConversationMode) {
67309400efa442422299acf21abe20e3470f9d965abScott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
67409400efa442422299acf21abe20e3470f9d965abScott Kennedy                                cursor);
675bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                    } else {
676bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                        notificationIntent = createViewConversationIntent(context, account, folder,
677bd8acad098abbf074184aa1273d269f7f7a54b37Scott Kennedy                                null);
678d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
6804fe0af81874976a1995191321e35c844b2229811Andy Huang                    Analytics.getInstance().sendEvent("notification_create",
6814fe0af81874976a1995191321e35c844b2229811Andy Huang                            launchConversationMode ? "conversation" : "conversation_list",
6824fe0af81874976a1995191321e35c844b2229811Andy Huang                            folder.getTypeDescription(), unseenCount);
6834fe0af81874976a1995191321e35c844b2229811Andy Huang
684d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (notificationIntent == null) {
6852f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                        LogUtils.e(LOG_TAG, "Null intent when building notification");
686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        return;
687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
688d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
68905a18446a96fa8ea2ae077b482499abf67366e32Alan Lau                    clickIntent = createClickPendingIntent(context, notificationIntent);
6904fe0af81874976a1995191321e35c844b2229811Andy Huang
691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    configureLatestEventInfoFromConversation(context, account, folderPreferences,
692439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                            notification, wearableExtender, msgNotifications, notificationId,
69315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            cursor, clickIntent, notificationIntent, unreadCount, unseenCount,
6948913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                            folder, when, photoFetcher);
695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    eventInfoConfigured = true;
696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
698d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean notifyOnce = !folderPreferences.isEveryMessageNotificationEnabled();
702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
703ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy            if (!ignoreUnobtrusiveSetting && notifyOnce) {
704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // If the user has "unobtrusive notifications" enabled, only alert the first time
705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // new mail is received in this account.  This is the default behavior.  See
706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // bugs 2412348 and 2413490.
707b1b855b71e502459e1571444fb1a9f74883c8330Paul Westbrook                LogUtils.d(LOG_TAG, "Setting Alert Once");
708d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setOnlyAlertOnce(true);
709d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
710d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
71132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            LogUtils.i(LOG_TAG, "Account: %s vibrate: %s",
71226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                    LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
713ff8553f20964f4c31b0c503a9e1daff6ae08a9c7Scott Kennedy                    Boolean.toString(folderPreferences.isNotificationVibrateEnabled()));
714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
715d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            int defaults = 0;
716d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
717e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            // Check if any current conversation notifications exist previously.  Only notify if
718e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            // one of them is new.
719e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            boolean hasNewConversationNotification;
720e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            Set<Integer> prevConversationNotifications =
721e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    sConversationNotificationMap.get(notificationKey);
722e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            if (prevConversationNotifications != null) {
723e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                hasNewConversationNotification = false;
72415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                for (Integer currentNotificationId : msgNotifications.keySet()) {
725e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    if (!prevConversationNotifications.contains(currentNotificationId)) {
726e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                        hasNewConversationNotification = true;
72715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        break;
72815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    }
72915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
73015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            } else {
731e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                hasNewConversationNotification = true;
73215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
73315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
734e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            LogUtils.d(LOG_TAG, "getAttention=%s,oldWhen=%s,hasNewConversationNotification=%s",
735e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    getAttention, oldWhen, hasNewConversationNotification);
73615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            /*
738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * We do not want to notify if this is coming back from an Undo notification, hence the
739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             * oldWhen check.
740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy             */
741e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            if (getAttention && oldWhen == 0 && hasNewConversationNotification) {
742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final AccountPreferences accountPreferences =
743cfb969b27f83777e08cd2f1a994dc023574a4739Tony Mantler                        new AccountPreferences(context, account.getEmailAddress());
744d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (accountPreferences.areNotificationsEnabled()) {
745d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (vibrate) {
746d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        defaults |= Notification.DEFAULT_VIBRATE;
747d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
748d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
749d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
750d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            : Uri.parse(ringtoneUri));
75132fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                    LogUtils.i(LOG_TAG, "New email in %s vibrateWhen: %s, playing notification: %s",
75226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()), vibrate,
75326a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                            ringtoneUri);
754d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
755d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
756d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
75732fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            // TODO(skennedy) Why do we do any of the above if we're just going to bail here?
758d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (eventInfoConfigured) {
759d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                defaults |= Notification.DEFAULT_LIGHTS;
760d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setDefaults(defaults);
761d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
762d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (oldWhen != 0) {
763d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // We do not want to display the ticker again if we are re-displaying this
764d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // notification (like from an Undo notification)
765d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notification.setTicker(null);
766d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
767d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
768439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                notification.extend(wearableExtender);
769c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                nm.notify(notificationId, notification.build());
77015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
771e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                if (prevConversationNotifications != null) {
77215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    Set<Integer> currentNotificationIds = msgNotifications.keySet();
773e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    for (Integer prevConversationNotificationId : prevConversationNotifications) {
774e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                        if (!currentNotificationIds.contains(prevConversationNotificationId)) {
775e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            nm.cancel(prevConversationNotificationId);
776e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            LogUtils.d(LOG_TAG, "canceling conversation notification %s",
777e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    prevConversationNotificationId);
77815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        }
77915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    }
78015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
78115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
782c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                for (Map.Entry<Integer, NotificationBuilders> entry : msgNotifications.entrySet()) {
783c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                    NotificationBuilders builders = entry.getValue();
784439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    builders.notifBuilder.extend(builders.wearableNotifBuilder);
785c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                    nm.notify(entry.getKey(), builders.notifBuilder.build());
786e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                    LogUtils.d(LOG_TAG, "notifying conversation notification %s", entry.getKey());
78715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
78815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
789e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                Set<Integer> conversationNotificationIds = new HashSet<Integer>();
790e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                conversationNotificationIds.addAll(msgNotifications.keySet());
791e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                sConversationNotificationMap.put(notificationKey, conversationNotificationIds);
79232fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy            } else {
79332fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy                LogUtils.i(LOG_TAG, "event info not configured - not notifying");
794d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
795d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
796d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (cursor != null) {
797d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                cursor.close();
798d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
799d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
800d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
801d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
80205a18446a96fa8ea2ae077b482499abf67366e32Alan Lau    private static PendingIntent createClickPendingIntent(Context context,
80305a18446a96fa8ea2ae077b482499abf67366e32Alan Lau            Intent notificationIntent) {
80405a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // Amend the click intent with a hint that its source was a notification,
80505a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // but remove the hint before it's used to generate notification action
80605a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // intents. This prevents the following sequence:
80705a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // 1. generate single notification
80805a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // 2. user clicks reply, then completes Compose activity
80905a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        // 3. main activity launches, gets FROM_NOTIFICATION hint in intent
81005a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        notificationIntent.putExtra(Utils.EXTRA_FROM_NOTIFICATION, true);
81105a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        PendingIntent clickIntent = PendingIntent.getActivity(context, -1, notificationIntent,
81205a18446a96fa8ea2ae077b482499abf67366e32Alan Lau                PendingIntent.FLAG_UPDATE_CURRENT);
81305a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        notificationIntent.removeExtra(Utils.EXTRA_FROM_NOTIFICATION);
81405a18446a96fa8ea2ae077b482499abf67366e32Alan Lau        return clickIntent;
81505a18446a96fa8ea2ae077b482499abf67366e32Alan Lau    }
81605a18446a96fa8ea2ae077b482499abf67366e32Alan Lau
817d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
818d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return an {@link Intent} which, if launched, will display the corresponding conversation
819d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
82009400efa442422299acf21abe20e3470f9d965abScott Kennedy    private static Intent createViewConversationIntent(final Context context, final Account account,
82109400efa442422299acf21abe20e3470f9d965abScott Kennedy            final Folder folder, final Cursor cursor) {
822d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (folder == null || account == null) {
8232f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy            LogUtils.e(LOG_TAG, "createViewConversationIntent(): "
82413a732747e1cf835b04211fad09b722accf877d5Scott Kennedy                    + "Null account or folder.  account: %s folder: %s", account, folder);
825d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return null;
826d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
827d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Intent intent;
829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (cursor == null) {
831259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewFolderIntent(context, folder.folderUri.fullUri, account);
832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // A conversation cursor has been specified, so this intent is intended to be go
834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // directly to the one new conversation
835d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
836d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Get the Conversation object
837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
838259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy            intent = Utils.createViewConversationIntent(context, conversation,
839259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy                    folder.folderUri.fullUri, account);
840d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
841d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
842d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return intent;
843d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
844d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
84561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getDefaultNotificationIcon(
84661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            final Context context, final Folder folder, final boolean multipleNew) {
847c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final int resId;
84861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (folder.notificationIconResId != 0) {
849c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = folder.notificationIconResId;
85061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        } else if (multipleNew) {
851c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = R.drawable.ic_notification_multiple_mail_holo_dark;
852d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
853c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            resId = R.drawable.ic_contact_picture;
854d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
855c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
856c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        final Bitmap icon = getIcon(context, resId);
857c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
858c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        if (icon == null) {
859c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy            LogUtils.e(LOG_TAG, "Couldn't decode notif icon res id %d", resId);
860c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy        }
861c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
862d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return icon;
863d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
864d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
86561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    private static Bitmap getIcon(final Context context, final int resId) {
86661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap cachedIcon = sNotificationIcons.get(resId);
86761bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        if (cachedIcon != null) {
86861bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy            return cachedIcon;
86961bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        }
87061bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
87161bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        final Bitmap icon = BitmapFactory.decodeResource(context.getResources(), resId);
87261bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        sNotificationIcons.put(resId, icon);
87361bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
87461bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy        return icon;
87561bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy    }
87661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy
877644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static Bitmap getDefaultWearableBg(Context context) {
878644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        Bitmap bg = sDefaultWearableBg.get();
879644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        if (bg == null) {
880644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            bg = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_email);
881644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            sDefaultWearableBg = new WeakReference<Bitmap>(bg);
882644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        }
883644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        return bg;
884644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    }
885644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
886d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static void configureLatestEventInfoFromConversation(final Context context,
887d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Account account, final FolderPreferences folderPreferences,
88815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final NotificationCompat.Builder notification,
889439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            final NotificationCompat.WearableExtender wearableExtender,
890c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            final Map<Integer, NotificationBuilders> msgNotifications,
89115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final int summaryNotificationId, final Cursor conversationCursor,
892d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final PendingIntent clickIntent, final Intent notificationIntent,
893b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int unreadCount, final int unseenCount,
8948913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final Folder folder, final long when, final ContactPhotoFetcher photoFetcher) {
895d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Resources res = context.getResources();
89626a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountDisplayName = account.getDisplayName();
89726a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        final String notificationAccountEmail = account.getEmailAddress();
898d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
89932fc8e41dde99760eb769a0d0c17f6c485a0b5cfScott Kennedy        LogUtils.i(LOG_TAG, "Showing notification with unreadCount of %d and unseenCount of %d",
9002f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                unreadCount, unseenCount);
901d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
902d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String notificationTicker = null;
903d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
904d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Boolean indicating that this notification is for a non-inbox label.
905259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final boolean isInbox = folder.folderUri.fullUri.equals(account.settings.defaultInbox);
906d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
907d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Notification label name for user label notifications.
908d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String notificationLabelName = isInbox ? null : folder.name;
909d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
910d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (unseenCount > 1) {
911d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Build the string that describes the number of new messages
912d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String newMessagesString = res.getString(R.string.new_messages, unseenCount);
913d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
914d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Use the default notification icon
915d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setLargeIcon(
91661bd0e84a3b0545a722bbfe931233b47afdf52e2Scott Kennedy                    getDefaultNotificationIcon(context, folder, true /* multiple new messages */));
917d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
918d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The ticker initially start as the new messages string.
919d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notificationTicker = newMessagesString;
920d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
921d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // The title of the notification is the new messages string
922d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            notification.setContentTitle(newMessagesString);
923d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
924c8bc5d10567d917c73e8fd286ea19a4c7f076d89Scott Kennedy            // TODO(skennedy) Can we remove this check?
925d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (com.android.mail.utils.Utils.isRunningJellybeanOrLater()) {
926d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // For a new-style notification
927d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final int maxNumDigestItems = context.getResources().getInteger(
928d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        R.integer.max_num_notification_digest_items);
929d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
930d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
93126a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                notification.setSubText(
93226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
933d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
934d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final NotificationCompat.InboxStyle digest =
935d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        new NotificationCompat.InboxStyle(notification);
936d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
93715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // Group by account.
93815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                String notificationGroupKey =
93915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        account.uri.toString() + "/" + folder.folderUri.fullUri;
940439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                notification.setGroup(notificationGroupKey).setGroupSummary(true);
94115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
942644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                ConfigResult firstResult = null;
943d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                int numDigestItems = 0;
944d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                do {
945d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final Conversation conversation = new Conversation(conversationCursor);
946d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
947d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    if (!conversation.read) {
948d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        boolean multipleUnreadThread = false;
949d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        // TODO(cwren) extract this pattern into a helper
950d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
951d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        Cursor cursor = null;
952d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        MessageCursor messageCursor = null;
953d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        try {
954d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final Uri.Builder uriBuilder = conversation.messageListUri.buildUpon();
955d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            uriBuilder.appendQueryParameter(
956d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.LABEL_QUERY_PARAMETER, notificationLabelName);
957d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            cursor = context.getContentResolver().query(uriBuilder.build(),
958d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    UIProvider.MESSAGE_PROJECTION, null, null, null);
959d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            messageCursor = new MessageCursor(cursor);
960d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
961b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                            String from = "";
962d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            String fromAddress = "";
963d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
964d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
965d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                fromAddress = message.getFrom();
966b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                if (fromAddress == null) {
967b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                    fromAddress = "";
968b18d75ad0651b8806773d2b79da35dc725390edbYu Ping Hu                                }
969d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                from = getDisplayableSender(fromAddress);
970d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
971d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
972d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final Message message = messageCursor.getMessage();
973d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                if (!message.read &&
974d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        !fromAddress.contentEquals(message.getFrom())) {
975d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    multipleUnreadThread = true;
976d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    break;
977d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                }
978d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
979d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final SpannableStringBuilder sendersBuilder;
980d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (multipleUnreadThread) {
981d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                final int sendersLength =
982d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                        res.getInteger(R.integer.swipe_senders_length);
983d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
984d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                sendersBuilder = getStyledSenders(context, conversationCursor,
98526a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                                        sendersLength, notificationAccountEmail);
986d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            } else {
98712fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                sendersBuilder =
98812fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook                                        new SpannableStringBuilder(getWrappedFromString(from));
989d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
990d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            final CharSequence digestLine = getSingleMessageInboxLine(context,
991d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                    sendersBuilder.toString(),
99289bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                                    ConversationItemView.filterTag(context, conversation.subject),
993edd6c1a2807d2ade930dfd4622707298dc470d64Tony Mantler                                    conversation.getSnippet());
994d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            digest.addLine(digestLine);
995d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            numDigestItems++;
99615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
997e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            // Adding conversation notification for Wear.
998e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            NotificationCompat.Builder conversationNotif =
99915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    new NotificationCompat.Builder(context);
1000e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setSmallIcon(R.drawable.stat_notify_email);
1001e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setContentText(digestLine);
1002e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            Intent conversationNotificationIntent = createViewConversationIntent(
1003e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    context, account, folder, conversationCursor);
1004e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            PendingIntent conversationClickIntent = createClickPendingIntent(
1005e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    context, conversationNotificationIntent);
1006e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setContentIntent(conversationClickIntent);
1007e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setAutoCancel(true);
1008e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau
1009c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau                            String groupSortKey = String.format("%010d", numDigestItems);
1010e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setGroup(notificationGroupKey);
1011e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            conversationNotif.setSortKey(groupSortKey);
1012439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau
1013e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            int conversationNotificationId = getNotificationId(
1014e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    summaryNotificationId, conversation.hashCode());
101515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1016e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            final NotificationCompat.WearableExtender conversationWearExtender =
1017439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                                    new NotificationCompat.WearableExtender();
10188913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                            final ConfigResult result =
10198913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                                    configureNotifForOneConversation(context, account,
1020e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    folderPreferences, conversationNotif, conversationWearExtender,
1021644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                    conversationCursor, notificationIntent, folder, when, res,
102215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                                    notificationAccountDisplayName, notificationAccountEmail,
1023e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    isInbox, notificationLabelName, conversationNotificationId,
10248913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                                    photoFetcher);
1025e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                            msgNotifications.put(conversationNotificationId,
1026e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                    NotificationBuilders.of(conversationNotif,
1027e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                                            conversationWearExtender));
1028644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1029644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            if (firstResult == null) {
1030644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                firstResult = result;
1031644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            }
1032d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        } finally {
1033d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (messageCursor != null) {
1034d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                messageCursor.close();
1035d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1036d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            if (cursor != null) {
1037d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                cursor.close();
1038d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            }
1039d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1040d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1041d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } while (numDigestItems <= maxNumDigestItems && conversationCursor.moveToNext());
1042644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1043644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                if (firstResult != null && firstResult.contactIconInfo != null) {
1044439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    wearableExtender.setBackground(firstResult.contactIconInfo.wearableBg);
1045644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                } else {
1046644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                    LogUtils.w(LOG_TAG, "First contact icon is null!");
1047439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    wearableExtender.setBackground(getDefaultWearableBg(context));
1048644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                }
1049d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1050d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                // The body of the notification is the account name, or the label name.
1051d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                notification.setContentText(
105226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                        isInbox ? notificationAccountDisplayName : notificationLabelName);
1053d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1054d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
105515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // For notifications for a single new conversation, we want to get the information
105615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // from the conversation
1057d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1058d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Move the cursor to the most recent unread conversation
1059d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            seekToLatestUnreadConversation(conversationCursor);
1060d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
10618913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final ConfigResult result = configureNotifForOneConversation(context, account,
1062439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                    folderPreferences, notification, wearableExtender, conversationCursor,
106315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationIntent, folder, when, res, notificationAccountDisplayName,
106415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationAccountEmail, isInbox, notificationLabelName,
10658913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    summaryNotificationId, photoFetcher);
1066644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            notificationTicker = result.notificationTicker;
1067644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1068439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            wearableExtender.setBackground(result.contactIconInfo.wearableBg);
106915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1070d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
107115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        // Build the notification ticker
107215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (notificationLabelName != null && notificationTicker != null) {
107315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // This is a per label notification, format the ticker with that information
107415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notificationTicker = res.getString(R.string.label_notification_ticker,
107515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notificationLabelName, notificationTicker);
107615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1077d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
107815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (notificationTicker != null) {
107915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // If we didn't generate a notification ticker, it will default to account name
108015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notification.setTicker(notificationTicker);
108115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
108215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
108315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        // Set the number in the notification
108415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        if (unreadCount > 1) {
108515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            notification.setNumber(unreadCount);
108615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
1087d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
108815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        notification.setContentIntent(clickIntent);
108915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
1090d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
109115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    /**
109215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     * Configure the notification for one conversation.  When there are multiple conversations,
109315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     * this method is used to configure bundled notification for Android Wear.
109415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau     */
1095644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static ConfigResult configureNotifForOneConversation(Context context,
1096644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            Account account, FolderPreferences folderPreferences,
1097644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau            NotificationCompat.Builder notification,
1098439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau            NotificationCompat.WearableExtender wearExtender, Cursor conversationCursor,
109915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            Intent notificationIntent, Folder folder, long when, Resources res,
11008913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            String notificationAccountDisplayName, String notificationAccountEmail,
11018913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            boolean isInbox, String notificationLabelName, int notificationId,
11028913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final ContactPhotoFetcher photoFetcher) {
1103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
11048913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final ConfigResult result = new ConfigResult();
1105d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
110615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final Conversation conversation = new Conversation(conversationCursor);
1107d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
110815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        Cursor cursor = null;
110915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        MessageCursor messageCursor = null;
111015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        boolean multipleUnseenThread = false;
111115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        String from = null;
111215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        try {
111315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            final Uri uri = conversation.messageListUri.buildUpon().appendQueryParameter(
111415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    UIProvider.LABEL_QUERY_PARAMETER, folder.persistentId).build();
111515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            cursor = context.getContentResolver().query(uri, UIProvider.MESSAGE_PROJECTION,
111615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    null, null, null);
111715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            messageCursor = new MessageCursor(cursor);
111815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Use the information from the last sender in the conversation that triggered
111915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // this notification.
112015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
112115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            String fromAddress = "";
112215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (messageCursor.moveToPosition(messageCursor.getCount() - 1)) {
112315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message = messageCursor.getMessage();
112415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                fromAddress = message.getFrom();
112515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                from = getDisplayableSender(fromAddress);
11268913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                result.contactIconInfo = getContactIcon(
11278913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        context, account.getAccountManagerAccount().name, from,
11288913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        getSenderAddress(fromAddress), folder, photoFetcher);
1129644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                notification.setLargeIcon(result.contactIconInfo.icon);
113015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1131d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
113215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // Assume that the last message in this conversation is unread
113315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            int firstUnseenMessagePos = messageCursor.getPosition();
113415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            while (messageCursor.moveToPosition(messageCursor.getPosition() - 1)) {
113515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message = messageCursor.getMessage();
113615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final boolean unseen = !message.seen;
113715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (unseen) {
113815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    firstUnseenMessagePos = messageCursor.getPosition();
113915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    if (!multipleUnseenThread
114015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            && !fromAddress.contentEquals(message.getFrom())) {
114115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        multipleUnseenThread = true;
114292b21746be07f31fdbe2aee510f9fcd8726a9cb3Paul Westbrook                    }
114315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
114415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1145d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
114689bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein            final String subject = ConversationItemView.filterTag(context, conversation.subject);
114789bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein
114815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            // TODO(skennedy) Can we remove this check?
114915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (Utils.isRunningJellybeanOrLater()) {
115015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For a new-style notification
1151d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
115215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (multipleUnseenThread) {
115315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // The title of a single conversation is the list of senders.
115415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    int sendersLength = res.getInteger(R.integer.swipe_senders_length);
1155d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
115615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    final SpannableStringBuilder sendersBuilder = getStyledSenders(
115715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            context, conversationCursor, sendersLength,
115815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            notificationAccountEmail);
115915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
116015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notification.setContentTitle(sendersBuilder);
116115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // For a single new conversation, the ticker is based on the sender's name.
1162644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                    result.notificationTicker = sendersBuilder.toString();
1163d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                } else {
116415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    from = getWrappedFromString(from);
116515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // The title of a single message the sender.
116615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notification.setContentTitle(from);
116715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    // For a single new conversation, the ticker is based on the sender's name.
1168644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                    result.notificationTicker = from;
116915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                }
1170d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
117115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification content will be the subject of the conversation.
117289bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                notification.setContentText(getSingleMessageLittleText(context, subject));
1173d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
117415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification subtext will be the subject of the conversation for inbox
117515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications, or will based on the the label name for user label
117615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications.
117715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setSubText(isInbox ?
117815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        notificationAccountDisplayName : notificationLabelName);
1179d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
118015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (multipleUnseenThread) {
118115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    notification.setLargeIcon(
118215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            getDefaultNotificationIcon(context, folder, true));
1183d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
118415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final NotificationCompat.BigTextStyle bigText =
118515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        new NotificationCompat.BigTextStyle(notification);
118615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
118715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // Seek the message cursor to the first unread message
118815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                final Message message;
118915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (messageCursor.moveToPosition(firstUnseenMessagePos)) {
119015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    message = messageCursor.getMessage();
119189bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                    bigText.bigText(getSingleMessageBigText(context, subject, message));
119215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                } else {
119315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    LogUtils.e(LOG_TAG, "Failed to load message");
119415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    message = null;
1195d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
119615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
119715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                if (message != null) {
119815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    final Set<String> notificationActions =
119915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folderPreferences.getNotificationActions(account);
120015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
120115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                    NotificationActionUtils.addNotificationActions(context, notificationIntent,
1202439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                            notification, wearExtender, account, conversation, message,
120315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                            folder, notificationId, when, notificationActions);
1204d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
120515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            } else {
120615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For an old-style notification
1207d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
120815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The title of a single conversation notification is built from both the sender
120915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // and subject of the new message.
121089bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                notification.setContentTitle(
121189bf7ce646a0ee67e0a8ee10d8834bcf54257d97Andrew Sapperstein                        getSingleMessageNotificationTitle(context, from, subject));
1212d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
121315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // The notification content will be the subject of the conversation for inbox
121415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications, or will based on the the label name for user label
121515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // notifications.
121615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                notification.setContentText(
121715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                        isInbox ? notificationAccountDisplayName : notificationLabelName);
1218d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
121915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                // For a single new conversation, the ticker is based on the sender's name.
1220644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                result.notificationTicker = from;
122115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
122215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        } finally {
122315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (messageCursor != null) {
122415490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                messageCursor.close();
122515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
122615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            if (cursor != null) {
122715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                cursor.close();
122815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1229d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1230644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        return result;
1231d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1232d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
123312fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    private static String getWrappedFromString(String from) {
123412fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        if (from == null) {
123512fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            LogUtils.e(LOG_TAG, "null from string in getWrappedFromString");
123612fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook            from = "";
123712fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        }
1238963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein        from = sBidiFormatter.unicodeWrap(from);
123912fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook        return from;
124012fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook    }
124112fb02b96c7394172ad9798a9de25eeb7611aafdPaul Westbrook
1242d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder getStyledSenders(final Context context,
1243d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Cursor conversationCursor, final int maxLength, final String account) {
1244d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final Conversation conversation = new Conversation(conversationCursor);
1245d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final com.android.mail.providers.ConversationInfo conversationInfo =
1246d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                conversation.conversationInfo;
1247d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ArrayList<SpannableString> senders = new ArrayList<SpannableString>();
1248d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sNotificationUnreadStyleSpan == null) {
1249d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationUnreadStyleSpan = new TextAppearanceSpan(
1250d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    context, R.style.NotificationSendersUnreadTextAppearance);
1251d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sNotificationReadStyleSpan =
1252d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSendersReadTextAppearance);
1253d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1254d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SendersView.format(context, conversationInfo, "", maxLength, senders, null, null, account,
125510ea28ab588d8e922c0f78f0f47fe479739ec2cfJames Lemieux                sNotificationUnreadStyleSpan, sNotificationReadStyleSpan,
125610ea28ab588d8e922c0f78f0f47fe479739ec2cfJames Lemieux                false /* showToHeader */, false /* resourceCachingRequired */);
1257d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1258d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return ellipsizeStyledSenders(context, senders);
1259d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1260d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1261d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sSendersSplitToken = null;
1262d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String sElidedPaddingToken = null;
1263d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1264d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableStringBuilder ellipsizeStyledSenders(final Context context,
1265d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            ArrayList<SpannableString> styledSenders) {
1266d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (sSendersSplitToken == null) {
1267d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sSendersSplitToken = context.getString(R.string.senders_split_token);
1268d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            sElidedPaddingToken = context.getString(R.string.elided_padding_token);
1269d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1270d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1271d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableStringBuilder builder = new SpannableStringBuilder();
1272d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString prevSender = null;
1273d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        for (SpannableString sender : styledSenders) {
1274d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (sender == null) {
12752f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy                LogUtils.e(LOG_TAG, "null sender iterating over styledSenders");
1276d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                continue;
1277d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1278d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
1279d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (SendersView.sElidedString.equals(sender.toString())) {
1280d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1281d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
1282d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (builder.length() > 0
1283d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    && (prevSender == null || !SendersView.sElidedString.equals(prevSender
1284d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            .toString()))) {
1285d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1286d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                sender = copyStyles(spans, sSendersSplitToken + sender);
1287d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else {
1288d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                prevSender = sender;
1289d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1290d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            builder.append(sender);
1291d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1292d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return builder;
1293d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1294d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1295d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static SpannableString copyStyles(CharacterStyle[] spans, CharSequence newText) {
1296d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        SpannableString s = new SpannableString(newText);
1297d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (spans != null && spans.length > 0) {
1298d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            s.setSpan(spans[0], 0, s.length(), 0);
1299d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1300d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return s;
1301d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1302d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1303d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1304d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Seeks the cursor to the position of the most recent unread conversation. If no unread
1305d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * conversation is found, the position of the cursor will be restored, and false will be
1306d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * returned.
1307d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1308d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static boolean seekToLatestUnreadConversation(final Cursor cursor) {
1309d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final int initialPosition = cursor.getPosition();
1310d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        do {
1311d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Conversation conversation = new Conversation(cursor);
1312d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!conversation.read) {
1313d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return true;
1314d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1315d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } while (cursor.moveToNext());
1316d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1317d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Didn't find an unread conversation, reset the position.
1318d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        cursor.moveToPosition(initialPosition);
1319d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return false;
1320d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1321d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1322d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1323d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1324d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1325d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1326d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param senders Sender of the new message that triggered the notification.
1327d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1328d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param snippet Snippet of the new message that triggered the notification
1329d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1330d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1331d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1332d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageInboxLine(Context context,
1333d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String senders, String subject, String snippet) {
1334d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // TODO(cwren) finish this step toward commmon code with getSingleMessageBigText
1335d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1336d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String subjectSnippet = !TextUtils.isEmpty(subject) ? subject : snippet;
1337d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1338d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationPrimarySpan =
1339d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new TextAppearanceSpan(context, R.style.NotificationPrimaryText);
1340d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1341d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(senders)) {
1342d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the senders are empty, just use the subject/snippet.
1343d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return subjectSnippet;
1344d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(subjectSnippet)) {
1345d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject/snippet is empty, just use the senders.
1346d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(senders);
1347d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan, 0, senders.length(), 0);
1348d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1349d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1350d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1351d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String formatString = context.getResources().getString(
1352d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.multiple_new_message_notification_item);
1353d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSecondarySpan =
1354d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1355d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
13568173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein            // senders is already individually unicode wrapped so it does not need to be done here
1357f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein            final String instantiatedString = String.format(formatString,
13588173cf41d2acf48c7b45ae3e7868fc391aae0359Andrew Sapperstein                    senders,
1359963c7e4f178037acebddfd13e16fc627ef368c82Andrew Sapperstein                    sBidiFormatter.unicodeWrap(subjectSnippet));
1360d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1361d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(instantiatedString);
1362d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1363d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isOrderReversed = formatString.indexOf("%2$s") <
1364d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    formatString.indexOf("%1$s");
1365d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int primaryOffset =
1366d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(senders) :
1367d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(senders));
1368d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int secondaryOffset =
1369d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isOrderReversed ? instantiatedString.lastIndexOf(subjectSnippet) :
1370d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                     instantiatedString.indexOf(subjectSnippet));
1371d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationPrimarySpan,
1372d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    primaryOffset, primaryOffset + senders.length(), 0);
1373d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSecondarySpan,
1374d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    secondaryOffset, secondaryOffset + subjectSnippet.length(), 0);
1375d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1376d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1377d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1378d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1379d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1380d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1381d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1382d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1383f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * @return a {@link CharSequence} suitable for use in
1384f58109699c207a2eaeca38d557d9b9a7e7664decAndrew Sapperstein     * {@link NotificationCompat.Builder#setContentText}
1385d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1386d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageLittleText(Context context, String subject) {
1387d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1388d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1389d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1390d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final SpannableString spannableString = new SpannableString(subject);
1391d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1392d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1393d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return spannableString;
1394d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1395d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1396d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1397d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Sets the bigtext for a notification for a single new conversation
1398d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *
1399d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1400d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1401d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param message the {@link Message} to be displayed.
1402d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use in
1403d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     *         {@link android.support.v4.app.NotificationCompat.BigTextStyle}
1404d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1405d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageBigText(Context context, String subject,
1406d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final Message message) {
1407d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1408d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(
1409d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                context, R.style.NotificationPrimaryText);
1410d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1411d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String snippet = getMessageBodyWithoutElidedText(message);
1412d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1413d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Change multiple newlines (with potential white space between), into a single new line
1414d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final String collapsedSnippet =
1415d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";
1416d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1417d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1418d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just use the snippet.
1419d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return snippet;
1420d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else if (TextUtils.isEmpty(collapsedSnippet)) {
1421d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the snippet is empty, just use the subject.
1422d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(subject);
1423d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan, 0, subject.length(), 0);
1424d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1425d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1426d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1427d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationBigTextFormat = context.getResources().getString(
1428d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_big_text);
1429d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1430d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1431d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1432d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") >
1433d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationBigTextFormat.indexOf("%1$s");
1434d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String bigText =
1435d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    String.format(notificationBigTextFormat, subject, collapsedSnippet);
1436d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString spannableString = new SpannableString(bigText);
1437d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1438d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1439d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    (isSubjectFirst ? bigText.indexOf(subject) : bigText.lastIndexOf(subject));
1440d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            spannableString.setSpan(notificationSubjectSpan,
1441d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1442d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1443d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return spannableString;
1444d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1445d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1446d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1447d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1448d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Gets the title for a notification for a single new conversation
1449d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param context
1450d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param sender Sender of the new message that triggered the notification.
1451d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @param subject Subject of the new message that triggered the notification
1452d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * @return a {@link CharSequence} suitable for use as a {@link Notification} title.
1453d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1454d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static CharSequence getSingleMessageNotificationTitle(Context context,
1455d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            String sender, String subject) {
1456d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1457d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(subject)) {
1458d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If the subject is empty, just set the title to the sender's information.
1459d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return sender;
1460d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } else {
1461d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String notificationTitleFormat = context.getResources().getString(
1462d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    R.string.single_new_message_notification_title);
1463d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1464d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Localizers may change the order of the parameters, look at how the format
1465d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // string is structured.
1466d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final boolean isSubjectLast = notificationTitleFormat.indexOf("%2$s") >
1467d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    notificationTitleFormat.indexOf("%1$s");
1468d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final String titleString = String.format(notificationTitleFormat, sender, subject);
1469d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1470d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Format the string so the subject is using the secondaryText style
1471d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final SpannableString titleSpannable = new SpannableString(titleString);
1472d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1473d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // Find the offset of the subject.
1474d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int subjectOffset =
1475d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    isSubjectLast ? titleString.lastIndexOf(subject) : titleString.indexOf(subject);
1476d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final TextAppearanceSpan notificationSubjectSpan =
1477d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    new TextAppearanceSpan(context, R.style.NotificationSecondaryText);
1478d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            titleSpannable.setSpan(notificationSubjectSpan,
1479d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    subjectOffset, subjectOffset + subject.length(), 0);
1480d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return titleSpannable;
1481d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1482d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1483d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1484d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
14857f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears the notifications for the specified account/folder.
1486d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
14877f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    public static void clearFolderNotification(Context context, Account account, Folder folder,
14887f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final boolean markSeen) {
148926a20758413f0f8629ea71a0943304e3ae740165Tony Mantler        LogUtils.v(LOG_TAG, "Clearing all notifications for %s/%s", account.getEmailAddress(),
149026a20758413f0f8629ea71a0943304e3ae740165Tony Mantler                folder.name);
1491d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
1492d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final NotificationKey key = new NotificationKey(account, folder);
1493d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.remove(key);
1494d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        notificationMap.saveNotificationMap(context);
1495d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
149615490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final NotificationManagerCompat notificationManager =
149715490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                NotificationManagerCompat.from(context);
1498b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler        notificationManager.cancel(getNotificationId(account.getAccountManagerAccount(), folder));
14997f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
1500e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        cancelConversationNotifications(key, notificationManager);
150115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
15027f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        if (markSeen) {
15037f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            markSeen(context, folder);
15047f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
15057f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    }
15067f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
15077f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy    /**
1508e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau     * Use content resolver to update a conversation.  Should not be called from a main thread.
1509e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau     */
1510e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    public static void markConversationAsReadAndSeen(Context context, Uri conversationUri) {
1511e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        LogUtils.v(LOG_TAG, "markConversationAsReadAndSeen=%s", conversationUri);
1512e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau
1513e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        final ContentValues values = new ContentValues(2);
1514e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        values.put(UIProvider.ConversationColumns.SEEN, Boolean.TRUE);
1515e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        values.put(UIProvider.ConversationColumns.READ, Boolean.TRUE);
1516e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        context.getContentResolver().update(conversationUri, values, null, null);
1517e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    }
1518e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau
1519e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    /**
15207f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     * Clears all notifications for the specified account.
15217f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy     */
1522b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static void clearAccountNotifications(final Context context,
1523b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final android.accounts.Account account) {
15242f97af9484bb3a226856c441943c5b0f32910155Scott Kennedy        LogUtils.v(LOG_TAG, "Clearing all notifications for %s", account);
15257f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final NotificationMap notificationMap = getNotificationMap(context);
15267f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
15277f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        // Find all NotificationKeys for this account
15287f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final ImmutableList.Builder<NotificationKey> keyBuilder = ImmutableList.builder();
15297f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
15307f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey key : notificationMap.keySet()) {
1531b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            if (account.equals(key.account.getAccountManagerAccount())) {
15327f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy                keyBuilder.add(key);
15337f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            }
15347f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
15357f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
15367f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        final List<NotificationKey> notificationKeys = keyBuilder.build();
15377f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
153815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        final NotificationManagerCompat notificationManager =
153915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau                NotificationManagerCompat.from(context);
15407f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
15417f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        for (final NotificationKey notificationKey : notificationKeys) {
15427f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            final Folder folder = notificationKey.folder;
15437f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationManager.cancel(getNotificationId(account, folder));
15447f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy            notificationMap.remove(notificationKey);
154515490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1546e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            cancelConversationNotifications(notificationKey, notificationManager);
15477f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        }
15487f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy
15497f8aed6bea276d6069abbfbdb25263a9ec92a684Scott Kennedy        notificationMap.saveNotificationMap(context);
1550d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1551d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1552e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    private static void cancelConversationNotifications(NotificationKey key,
155315490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            NotificationManagerCompat nm) {
1554e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        final Set<Integer> conversationNotifications = sConversationNotificationMap.get(key);
1555e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        if (conversationNotifications != null) {
1556e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            for (Integer conversationNotification : conversationNotifications) {
1557e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                nm.cancel(conversationNotification);
155815490239c82cdb4765e87e926c8c1945adedd1edAlan Lau            }
1559e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau            sConversationNotificationMap.remove(key);
156015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau        }
156115490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
156215490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
15638913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    private static ContactIconInfo getContactIcon(final Context context, String accountName,
15648913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final String displayName, final String senderAddress, final Folder folder,
15658913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final ContactPhotoFetcher photoFetcher) {
15668913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
15678913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (senderAddress == null) {
15688913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            return null;
15698913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
15708913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
15718913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (Looper.myLooper() == Looper.getMainLooper()) {
15728913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            throw new IllegalStateException(
15738913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    "getContactIcon should not be called on the main thread.");
15748913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
15758913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
15768913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        // Get the ideal size for this icon.
15778913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final Resources res = context.getResources();
15788913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final int idealIconHeight =
15798913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
15808913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final int idealIconWidth =
15818913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
15828913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final int idealWearableBgWidth =
15838913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                res.getDimensionPixelSize(R.dimen.wearable_background_width);
15848913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final int idealWearableBgHeight =
15858913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                res.getDimensionPixelSize(R.dimen.wearable_background_height);
15868913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
15878913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final ContactIconInfo contactIconInfo =
15888913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                photoFetcher != null ? photoFetcher.getContactPhoto(
15898913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                context, accountName, senderAddress,idealIconWidth, idealIconHeight,
15908913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                idealWearableBgWidth, idealWearableBgHeight) :
15918913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                getContactInfo(context, senderAddress, idealIconWidth, idealIconHeight,
15928913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                idealWearableBgWidth, idealWearableBgHeight);
15938913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
15948913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (contactIconInfo.icon == null) {
15958913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            // Make a colorful tile!
15968913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final Dimensions dimensions = new Dimensions(idealIconWidth, idealIconHeight,
15978913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    Dimensions.SCALE_ONE);
15988913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
15998913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            contactIconInfo.icon = new LetterTileProvider(context).getLetterTile(dimensions,
16008913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    displayName, senderAddress);
16018913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
16028913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
16038913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (contactIconInfo.icon == null) {
16048913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            // Icon should be the default mail icon.
16058913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            contactIconInfo.icon = getDefaultNotificationIcon(context, folder,
16068913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                    false /* single new message */);
16078913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
16088913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
16098913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        if (contactIconInfo.wearableBg == null) {
16108913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            contactIconInfo.wearableBg = getDefaultWearableBg(context);
16118913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        }
16128913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
16138913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        return contactIconInfo;
16148913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    }
16158913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein
1616d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static ArrayList<Long> findContacts(Context context, Collection<String> addresses) {
1617d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<String> whereArgs = new ArrayList<String>();
1618d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        StringBuilder whereBuilder = new StringBuilder();
1619d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String[] questionMarks = new String[addresses.size()];
1620d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1621d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereArgs.addAll(addresses);
1622d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Arrays.fill(questionMarks, "?");
1623d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        whereBuilder.append(Email.DATA1 + " IN (").
1624d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(TextUtils.join(",", questionMarks)).
1625d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                append(")");
1626d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1627d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ContentResolver resolver = context.getContentResolver();
1628d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        Cursor c = resolver.query(Email.CONTENT_URI,
1629e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau                new String[] {Email.CONTACT_ID}, whereBuilder.toString(),
1630d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                whereArgs.toArray(new String[0]), null);
1631d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1632d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        ArrayList<Long> contactIds = new ArrayList<Long>();
1633d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (c == null) {
1634d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return contactIds;
1635d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1636d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        try {
1637d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            while (c.moveToNext()) {
1638d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                contactIds.add(c.getLong(0));
1639d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1640d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        } finally {
1641d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            c.close();
1642d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1643d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return contactIds;
1644d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1645d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
16468913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static ContactIconInfo getContactInfo(
16478913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final Context context, final String senderAddress,
16488913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final int idealIconWidth, final int idealIconHeight,
16498913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein            final int idealWearableBgWidth, final int idealWearableBgHeight) {
16508913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final ContactIconInfo contactIconInfo = new ContactIconInfo();
16518913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein        final List<Long> contactIds = findContacts( context, Arrays.asList(
16528913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                new String[] { senderAddress }));
1653d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1654d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (contactIds != null) {
16554046d0695e102368201bb2f3a268a4f85c8bc4dbScott Kennedy            for (final long id : contactIds) {
1656644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                final Uri contactUri = ContentUris.withAppendedId(
1657644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        ContactsContract.Contacts.CONTENT_URI, id);
16588913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                final InputStream inputStream =
16598913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        ContactsContract.Contacts.openContactPhotoInputStream(
16608913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                                context.getContentResolver(), contactUri, true /*preferHighres*/);
1661d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1662644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                if (inputStream != null) {
1663d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    try {
16648913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein                        final Bitmap source = BitmapFactory.decodeStream(inputStream);
1665644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        if (source != null) {
1666644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            // We should scale this image to fit the intended size
1667644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            contactIconInfo.icon = Bitmap.createScaledBitmap(source, idealIconWidth,
1668644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                    idealIconHeight, true);
1669644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1670644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            contactIconInfo.wearableBg = Bitmap.createScaledBitmap(source,
1671644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                                    idealWearableBgWidth, idealWearableBgHeight, true);
1672644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        }
1673644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1674644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        if (contactIconInfo.icon != null) {
1675644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                            break;
1676d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1677d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    } finally {
1678644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau                        Closeables.closeQuietly(inputStream);
1679d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1680d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1681d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1682d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1683c94c80c8b8c92c019f19f2e315dea1ff03ad573fScott Kennedy
1684644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        return contactIconInfo;
1685d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1686d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1687d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getMessageBodyWithoutElidedText(final Message message) {
16885e7e88b9f5ac1f239753e13ffb3ee4b249722c9aScott Kennedy        return getMessageBodyWithoutElidedText(message.getBodyAsHtml());
1689d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1690d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1691d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static String getMessageBodyWithoutElidedText(String html) {
1692d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(html)) {
1693d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return "";
1694d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1695d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Get the html "tree" for this message body
1696d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final HtmlTree htmlTree = com.android.mail.utils.Utils.getHtmlTree(html);
1697d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        htmlTree.setPlainTextConverterFactory(MESSAGE_CONVERTER_FACTORY);
1698d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1699d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return htmlTree.getPlainText();
1700d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1701d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1702d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void markSeen(final Context context, final Folder folder) {
1703259df5b9e11908c8ef7c91483924891dd96b3c27Scott Kennedy        final Uri uri = folder.folderUri.fullUri;
1704d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1705d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final ContentValues values = new ContentValues(1);
1706d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        values.put(UIProvider.ConversationColumns.SEEN, 1);
1707d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1708d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        context.getContentResolver().update(uri, values, null, null);
1709d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1710d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1711d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1712d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns a displayable string representing
1713d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * the message sender. It has a preference toward showing the name,
1714d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * but will fall back to the address if that is all that is available.
1715d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1716d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getDisplayableSender(String sender) {
1717d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1718d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1719d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String displayableSender = address.getName();
1720390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1721390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        if (!TextUtils.isEmpty(displayableSender)) {
1722821e578a71c7015646522e729600618f0ec16fc0Tony Mantler            return Address.decodeAddressPersonal(displayableSender);
1723d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1724390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1725390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        // If that fails, default to the sender address.
1726390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy        displayableSender = address.getAddress();
1727390cab939304705cc8adeb8e4810abf904fec22fScott Kennedy
1728d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1729d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1730d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(displayableSender)) {
1731d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            displayableSender = sender;
1732d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1733d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return displayableSender;
1734d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1735d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1736d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1737d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Returns only the address portion of a message sender.
1738d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1739d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static String getSenderAddress(String sender) {
1740d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        final EmailAddress address = EmailAddress.getEmailAddress(sender);
1741d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1742d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        String tokenizedAddress = address.getAddress();
1743d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1744d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // If we were unable to tokenize a name or address,
1745d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // just use whatever was in the sender.
1746d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (TextUtils.isEmpty(tokenizedAddress)) {
1747d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            tokenizedAddress = sender;
1748d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1749d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return tokenizedAddress;
1750d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1751d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1752b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler    public static int getNotificationId(final android.accounts.Account account,
1753b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final Folder folder) {
1754d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        return 1 ^ account.hashCode() ^ folder.hashCode();
1755d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1756d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1757e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau    private static int getNotificationId(int summaryNotificationId, int conversationHashCode) {
1758e806c9447c7137d2a7a828e7ccdc1f8961aa1c2aAlan Lau        return summaryNotificationId ^ conversationHashCode;
175915490239c82cdb4765e87e926c8c1945adedd1edAlan Lau    }
176015490239c82cdb4765e87e926c8c1945adedd1edAlan Lau
1761d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    private static class NotificationKey {
1762d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Account account;
1763d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public final Folder folder;
1764d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1765d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public NotificationKey(Account account, Folder folder) {
1766d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.account = account;
1767d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            this.folder = folder;
1768d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1769d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1770d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1771d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public boolean equals(Object other) {
1772d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!(other instanceof NotificationKey)) {
1773d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return false;
1774d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1775d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            NotificationKey key = (NotificationKey) other;
1776b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            return account.getAccountManagerAccount().equals(key.account.getAccountManagerAccount())
1777b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler                    && folder.equals(key.folder);
1778d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1779d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1780d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1781d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public String toString() {
178226a20758413f0f8629ea71a0943304e3ae740165Tony Mantler            return account.getDisplayName() + " " + folder.name;
1783d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1784d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1785d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1786d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public int hashCode() {
1787b90a813b107d8f43236e2d3fe12f299d94c82ce8Tony Mantler            final int accountHashCode = account.getAccountManagerAccount().hashCode();
1788d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final int folderHashCode = folder.hashCode();
1789d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            return accountHashCode ^ folderHashCode;
1790d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1791d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1792d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1793d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1794d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Contains the logic for converting the contents of one HtmlTree into
1795d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * plaintext.
1796d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1797d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static class MailMessagePlainTextConverter extends HtmlTree.DefaultPlainTextConverter {
1798d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings for parsing html message bodies
1799d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_NAME = "div";
1800d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME = "class";
1801d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final String ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE = "elided-text";
1802d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1803d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HTML.Attribute ELIDED_TEXT_ATTRIBUTE =
1804d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                new HTML.Attribute(ELIDED_TEXT_ELEMENT_ATTRIBUTE_NAME, HTML.Attribute.NO_TYPE);
1805d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1806d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private static final HtmlDocument.Node ELIDED_TEXT_REPLACEMENT_NODE =
1807d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                HtmlDocument.createSelfTerminatingTag(HTML4.BR_ELEMENT, null, null, null);
1808d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1809d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        private int mEndNodeElidedTextBlock = -1;
1810d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1811d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        @Override
1812d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        public void addNode(HtmlDocument.Node n, int nodeNum, int endNum) {
1813d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If we are in the middle of an elided text block, don't add this node
1814d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (nodeNum < mEndNodeElidedTextBlock) {
1815d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1816d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            } else if (nodeNum == mEndNodeElidedTextBlock) {
1817d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                super.addNode(ELIDED_TEXT_REPLACEMENT_NODE, nodeNum, endNum);
1818d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                return;
1819d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1820d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1821d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this tag starts another elided text block, we want to remember the end
1822d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (n instanceof HtmlDocument.Tag) {
1823d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                boolean foundElidedTextTag = false;
1824d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HtmlDocument.Tag htmlTag = (HtmlDocument.Tag)n;
1825d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final HTML.Element htmlElement = htmlTag.getElement();
1826d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (ELIDED_TEXT_ELEMENT_NAME.equals(htmlElement.getName())) {
1827d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    // Make sure that the class is what is expected
1828d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    final List<HtmlDocument.TagAttribute> attributes =
1829d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            htmlTag.getAttributes(ELIDED_TEXT_ATTRIBUTE);
1830d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    for (HtmlDocument.TagAttribute attribute : attributes) {
1831d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        if (ELIDED_TEXT_ELEMENT_ATTRIBUTE_CLASS_VALUE.equals(
1832d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                                attribute.getValue())) {
1833d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            // Found an "elided-text" div.  Remember information about this tag
1834d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            mEndNodeElidedTextBlock = endNum;
1835d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            foundElidedTextTag = true;
1836d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                            break;
1837d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        }
1838d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    }
1839d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1840d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1841d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                if (foundElidedTextTag) {
1842d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    return;
1843d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                }
1844d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1845d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1846c56b233013cf107c702ef9f61305282670ad804aScott Kennedy            super.addNode(n, nodeNum, endNum);
1847d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1848d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1849d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1850d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
1851d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * During account setup in Email, we may not have an inbox yet, so the notification setting had
1852d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * to be stored in {@link AccountPreferences}. If it is still there, we need to move it to the
1853d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * {@link FolderPreferences} now.
1854d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
1855d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public static void moveNotificationSetting(final AccountPreferences accountPreferences,
1856d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            final FolderPreferences folderPreferences) {
1857d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        if (accountPreferences.isDefaultInboxNotificationsEnabledSet()) {
1858d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            // If this setting has been changed some other way, don't overwrite it
1859d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            if (!folderPreferences.isNotificationsEnabledSet()) {
1860d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                final boolean notificationsEnabled =
1861d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                        accountPreferences.getDefaultInboxNotificationsEnabled();
1862d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1863d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                folderPreferences.setNotificationsEnabled(notificationsEnabled);
1864d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            }
1865d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
1866d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            accountPreferences.clearDefaultInboxNotificationsEnabled();
1867d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
1868d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
1869c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau
1870c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau    private static class NotificationBuilders {
1871c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        public final NotificationCompat.Builder notifBuilder;
1872439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau        public final NotificationCompat.WearableExtender wearableNotifBuilder;
1873c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau
1874c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        private NotificationBuilders(NotificationCompat.Builder notifBuilder,
1875439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                NotificationCompat.WearableExtender wearableNotifBuilder) {
1876c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            this.notifBuilder = notifBuilder;
1877c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            this.wearableNotifBuilder = wearableNotifBuilder;
1878c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        }
1879c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau
1880c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        public static NotificationBuilders of(NotificationCompat.Builder notifBuilder,
1881439aa5d7d9226de4682c3697c68c0703b8847518Alan Lau                NotificationCompat.WearableExtender wearableNotifBuilder) {
1882c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau            return new NotificationBuilders(notifBuilder, wearableNotifBuilder);
1883c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau        }
1884c4288f2504ddf203bf558c7ae0143f36f842535bAlan Lau    }
1885644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
1886644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    private static class ConfigResult {
1887644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public String notificationTicker;
1888644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public ContactIconInfo contactIconInfo;
1889644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    }
1890644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau
18918913ca6aebe6017efe8060b61b8ad38eda95aa8bAndrew Sapperstein    public static class ContactIconInfo {
1892644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public Bitmap icon;
1893644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau        public Bitmap wearableBg;
1894644ec1e2e65bedbbfff4dbb396d3e4f95d244c62Alan Lau    }
1895d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
1896