summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/browse/SendersView.java
blob: 4bbdb4f4ab1ce718783c97f7ae8d771107d819dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (C) 2012 Google Inc.
 * Licensed to The Android Open Source Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mail.browse;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.support.v4.text.BidiFormatter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.CharacterStyle;
import android.text.style.TextAppearanceSpan;

import com.android.mail.R;
import com.android.mail.providers.Conversation;
import com.android.mail.providers.ConversationInfo;
import com.android.mail.providers.ParticipantInfo;
import com.android.mail.providers.UIProvider;
import com.android.mail.ui.DividedImageCanvas;
import com.android.mail.utils.ObjectCache;
import com.google.common.base.Objects;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.Map;

public class SendersView {
    private static final Integer DOES_NOT_EXIST = -5;
    // FIXME(ath): make all of these statics instance variables, and have callers hold onto this
    // instance as long as appropriate (e.g. activity lifetime).
    // no need to listen for configuration changes.
    private static String sSendersSplitToken;
    private static CharSequence sDraftSingularString;
    private static CharSequence sDraftPluralString;
    private static CharSequence sSendingString;
    private static CharSequence sRetryingString;
    private static CharSequence sFailedString;
    private static String sDraftCountFormatString;
    private static CharacterStyle sDraftsStyleSpan;
    private static CharacterStyle sSendingStyleSpan;
    private static CharacterStyle sRetryingStyleSpan;
    private static CharacterStyle sFailedStyleSpan;
    private static TextAppearanceSpan sUnreadStyleSpan;
    private static CharacterStyle sReadStyleSpan;
    private static String sMeSubjectString;
    private static String sMeObjectString;
    private static String sToHeaderString;
    private static String sMessageCountSpacerString;
    public static CharSequence sElidedString;
    private static BroadcastReceiver sConfigurationChangedReceiver;
    private static TextAppearanceSpan sMessageInfoReadStyleSpan;
    private static TextAppearanceSpan sMessageInfoUnreadStyleSpan;
    private static BidiFormatter sBidiFormatter;

    // We only want to have at most 2 Priority to length maps.  This will handle the case where
    // there is a widget installed on the launcher while the user is scrolling in the app
    private static final int MAX_PRIORITY_LENGTH_MAP_LIST = 2;

    // Cache of priority to length maps.  We can't just use a single instance as it may be
    // modified from different threads
    private static final ObjectCache<Map<Integer, Integer>> PRIORITY_LENGTH_MAP_CACHE =
            new ObjectCache<Map<Integer, Integer>>(
                    new ObjectCache.Callback<Map<Integer, Integer>>() {
                        @Override
                        public Map<Integer, Integer> newInstance() {
                            return Maps.newHashMap();
                        }
                        @Override
                        public void onObjectReleased(Map<Integer, Integer> object) {
                            object.clear();
                        }
                    }, MAX_PRIORITY_LENGTH_MAP_LIST);

    public static Typeface getTypeface(boolean isUnread) {
        return isUnread ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT;
    }

    private static synchronized void getSenderResources(
            Context context, final boolean resourceCachingRequired) {
        if (sConfigurationChangedReceiver == null && resourceCachingRequired) {
            sConfigurationChangedReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    sDraftSingularString = null;
                    getSenderResources(context, true);
                }
            };
            context.registerReceiver(sConfigurationChangedReceiver, new IntentFilter(
                    Intent.ACTION_CONFIGURATION_CHANGED));
        }
        if (sDraftSingularString == null) {
            Resources res = context.getResources();
            sSendersSplitToken = res.getString(R.string.senders_split_token);
            sElidedString = res.getString(R.string.senders_elided);
            sDraftSingularString = res.getQuantityText(R.plurals.draft, 1);
            sDraftPluralString = res.getQuantityText(R.plurals.draft, 2);
            sDraftCountFormatString = res.getString(R.string.draft_count_format);
            sMeSubjectString = res.getString(R.string.me_subject_pronoun);
            sMeObjectString = res.getString(R.string.me_object_pronoun);
            sToHeaderString = res.getString(R.string.to_heading);
            sMessageInfoUnreadStyleSpan = new TextAppearanceSpan(context,
                    R.style.MessageInfoUnreadTextAppearance);
            sMessageInfoReadStyleSpan = new TextAppearanceSpan(context,
                    R.style.MessageInfoReadTextAppearance);
            sDraftsStyleSpan = new TextAppearanceSpan(context, R.style.DraftTextAppearance);
            sUnreadStyleSpan = new TextAppearanceSpan(context, R.style.SendersUnreadTextAppearance);
            sSendingStyleSpan = new TextAppearanceSpan(context, R.style.SendingTextAppearance);
            sRetryingStyleSpan = new TextAppearanceSpan(context, R.style.RetryingTextAppearance);
            sFailedStyleSpan = new TextAppearanceSpan(context, R.style.FailedTextAppearance);
            sReadStyleSpan = new TextAppearanceSpan(context, R.style.SendersReadTextAppearance);
            sMessageCountSpacerString = res.getString(R.string.message_count_spacer);
            sSendingString = res.getString(R.string.sending);
            sRetryingString = res.getString(R.string.message_retrying);
            sFailedString = res.getString(R.string.message_failed);
            sBidiFormatter = BidiFormatter.getInstance();
        }
    }

    public static SpannableStringBuilder createMessageInfo(Context context, Conversation conv,
            final boolean resourceCachingRequired) {
        SpannableStringBuilder messageInfo = new SpannableStringBuilder();

        try {
            ConversationInfo conversationInfo = conv.conversationInfo;
            int sendingStatus = conv.sendingState;
            boolean hasSenders = false;
            // This covers the case where the sender is "me" and this is a draft
            // message, which means this will only run once most of the time.
            for (ParticipantInfo p : conversationInfo.participantInfos) {
                if (!TextUtils.isEmpty(p.name)) {
                    hasSenders = true;
                    break;
                }
            }
            getSenderResources(context, resourceCachingRequired);
            int count = conversationInfo.messageCount;
            int draftCount = conversationInfo.draftCount;
            if (count > 1) {
                messageInfo.append(count + "");
            }
            messageInfo.setSpan(CharacterStyle.wrap(
                    conv.read ? sMessageInfoReadStyleSpan : sMessageInfoUnreadStyleSpan),
                    0, messageInfo.length(), 0);
            if (draftCount > 0) {
                // If we are showing a message count or any draft text and there
                // is at least 1 sender, prepend the sending state text with a
                // comma.
                if (hasSenders || count > 1) {
                    messageInfo.append(sSendersSplitToken);
                }
                SpannableStringBuilder draftString = new SpannableStringBuilder();
                if (draftCount == 1) {
                    draftString.append(sDraftSingularString);
                } else {
                    draftString.append(sDraftPluralString).append(
                            String.format(sDraftCountFormatString, draftCount));
                }
                draftString.setSpan(CharacterStyle.wrap(sDraftsStyleSpan), 0,
                        draftString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                messageInfo.append(draftString);
            }

            boolean showState = sendingStatus == UIProvider.ConversationSendingState.SENDING ||
                    sendingStatus == UIProvider.ConversationSendingState.RETRYING ||
                    sendingStatus == UIProvider.ConversationSendingState.SEND_ERROR;
            if (showState) {
                // If we are showing a message count or any draft text, prepend
                // the sending state text with a comma.
                if (count > 1 || draftCount > 0) {
                    messageInfo.append(sSendersSplitToken);
                }

                SpannableStringBuilder stateSpan = new SpannableStringBuilder();

                if (sendingStatus == UIProvider.ConversationSendingState.SENDING) {
                    stateSpan.append(sSendingString);
                    stateSpan.setSpan(sSendingStyleSpan, 0, stateSpan.length(), 0);
                } else if (sendingStatus == UIProvider.ConversationSendingState.RETRYING) {
                    stateSpan.append(sRetryingString);
                    stateSpan.setSpan(sRetryingStyleSpan, 0, stateSpan.length(), 0);
                } else if (sendingStatus == UIProvider.ConversationSendingState.SEND_ERROR) {
                    stateSpan.append(sFailedString);
                    stateSpan.setSpan(sFailedStyleSpan, 0, stateSpan.length(), 0);
                }
                messageInfo.append(stateSpan);
            }

            // Prepend a space if we are showing other message info text.
            if (count > 1 || (draftCount > 0 && hasSenders) || showState) {
                messageInfo.insert(0, sMessageCountSpacerString);
            }
        } finally {
            if (!resourceCachingRequired) {
                clearResourceCache();
            }
        }

        return messageInfo;
    }

    public static void format(Context context, ConversationInfo conversationInfo,
            String messageInfo, int maxChars, ArrayList<SpannableString> styledSenders,
            ArrayList<String> displayableSenderNames, ArrayList<String> displayableSenderEmails,
            String account, final boolean showToHeader, final boolean resourceCachingRequired) {
        try {
            getSenderResources(context, resourceCachingRequired);
            format(context, conversationInfo, messageInfo, maxChars, styledSenders,
                    displayableSenderNames, displayableSenderEmails, account,
                    sUnreadStyleSpan, sReadStyleSpan, showToHeader, resourceCachingRequired);
        } finally {
            if (!resourceCachingRequired) {
                clearResourceCache();
            }
        }
    }

    public static void format(Context context, ConversationInfo conversationInfo,
            String messageInfo, int maxChars, ArrayList<SpannableString> styledSenders,
            ArrayList<String> displayableSenderNames, ArrayList<String> displayableSenderEmails,
            String account, final TextAppearanceSpan notificationUnreadStyleSpan,
            final CharacterStyle notificationReadStyleSpan, final boolean showToHeader,
            final boolean resourceCachingRequired) {
        try {
            getSenderResources(context, resourceCachingRequired);
            handlePriority(maxChars, messageInfo, conversationInfo, styledSenders,
                    displayableSenderNames, displayableSenderEmails, account,
                    notificationUnreadStyleSpan, notificationReadStyleSpan, showToHeader);
        } finally {
            if (!resourceCachingRequired) {
                clearResourceCache();
            }
        }
    }

    private static void handlePriority(int maxChars, String messageInfoString,
            ConversationInfo conversationInfo, ArrayList<SpannableString> styledSenders,
            ArrayList<String> displayableSenderNames, ArrayList<String> displayableSenderEmails,
            String account, final TextAppearanceSpan unreadStyleSpan,
            final CharacterStyle readStyleSpan, final boolean showToHeader) {
        boolean shouldAddPhotos = displayableSenderEmails != null;
        int maxPriorityToInclude = -1; // inclusive
        int numCharsUsed = messageInfoString.length(); // draft, number drafts,
                                                       // count
        int numSendersUsed = 0;
        int numCharsToRemovePerWord = 0;
        int maxFoundPriority = 0;
        if (numCharsUsed > maxChars) {
            numCharsToRemovePerWord = numCharsUsed - maxChars;
        }

        final Map<Integer, Integer> priorityToLength = PRIORITY_LENGTH_MAP_CACHE.get();
        try {
            priorityToLength.clear();
            int senderLength;
            for (ParticipantInfo info : conversationInfo.participantInfos) {
                final String senderName = info.name;
                senderLength = !TextUtils.isEmpty(senderName) ? senderName.length() : 0;
                priorityToLength.put(info.priority, senderLength);
                maxFoundPriority = Math.max(maxFoundPriority, info.priority);
            }
            while (maxPriorityToInclude < maxFoundPriority) {
                if (priorityToLength.containsKey(maxPriorityToInclude + 1)) {
                    int length = numCharsUsed + priorityToLength.get(maxPriorityToInclude + 1);
                    if (numCharsUsed > 0)
                        length += 2;
                    // We must show at least two senders if they exist. If we don't
                    // have space for both
                    // then we will truncate names.
                    if (length > maxChars && numSendersUsed >= 2) {
                        break;
                    }
                    numCharsUsed = length;
                    numSendersUsed++;
                }
                maxPriorityToInclude++;
            }
        } finally {
            PRIORITY_LENGTH_MAP_CACHE.release(priorityToLength);
        }
        // We want to include this entry if
        // 1) The onlyShowUnread flags is not set
        // 2) The above flag is set, and the message is unread
        ParticipantInfo currentParticipant;
        SpannableString spannableDisplay;
        CharacterStyle style;
        boolean appendedElided = false;
        Map<String, Integer> displayHash = Maps.newHashMap();
        String firstDisplayableSenderEmail = null;
        String firstDisplayableSender = null;
        for (int i = 0; i < conversationInfo.participantInfos.size(); i++) {
            currentParticipant = conversationInfo.participantInfos.get(i);
            final String currentEmail = currentParticipant.email;

            final String currentName = currentParticipant.name;
            String nameString = !TextUtils.isEmpty(currentName) ? currentName : "";
            if (nameString.length() == 0) {
                // if we're showing the To: header, show the object version of me.
                nameString = getMe(showToHeader /* useObjectMe */);
            }
            if (numCharsToRemovePerWord != 0) {
                nameString = nameString.substring(0,
                        Math.max(nameString.length() - numCharsToRemovePerWord, 0));
            }

            final int priority = currentParticipant.priority;
            style = CharacterStyle.wrap(currentParticipant.readConversation ? readStyleSpan :
                    unreadStyleSpan);
            if (priority <= maxPriorityToInclude) {
                spannableDisplay = new SpannableString(sBidiFormatter.unicodeWrap(nameString));
                // Don't duplicate senders; leave the first instance, unless the
                // current instance is also unread.
                int oldPos = displayHash.containsKey(currentName) ? displayHash
                        .get(currentName) : DOES_NOT_EXIST;
                // If this sender doesn't exist OR the current message is
                // unread, add the sender.
                if (oldPos == DOES_NOT_EXIST || !currentParticipant.readConversation) {
                    // If the sender entry already existed, and is right next to the
                    // current sender, remove the old entry.
                    if (oldPos != DOES_NOT_EXIST && i > 0 && oldPos == i - 1
                            && oldPos < styledSenders.size()) {
                        // Remove the old one!
                        styledSenders.set(oldPos, null);
                        if (shouldAddPhotos && !TextUtils.isEmpty(currentEmail)) {
                            displayableSenderEmails.remove(currentEmail);
                            displayableSenderNames.remove(currentName);
                        }
                    }
                    displayHash.put(currentName, i);
                    spannableDisplay.setSpan(style, 0, spannableDisplay.length(), 0);
                    styledSenders.add(spannableDisplay);
                }
            } else {
                if (!appendedElided) {
                    spannableDisplay = new SpannableString(sElidedString);
                    spannableDisplay.setSpan(style, 0, spannableDisplay.length(), 0);
                    appendedElided = true;
                    styledSenders.add(spannableDisplay);
                }
            }
            if (shouldAddPhotos) {
                String senderEmail = TextUtils.isEmpty(currentName) ?
                        account :
                            TextUtils.isEmpty(currentEmail) ? currentName : currentEmail;
                if (i == 0) {
                    // Always add the first sender!
                    firstDisplayableSenderEmail = senderEmail;
                    firstDisplayableSender = currentName;
                } else {
                    if (!Objects.equal(firstDisplayableSenderEmail, senderEmail)) {
                        int indexOf = displayableSenderEmails.indexOf(senderEmail);
                        if (indexOf > -1) {
                            displayableSenderEmails.remove(indexOf);
                            displayableSenderNames.remove(indexOf);
                        }
                        displayableSenderEmails.add(senderEmail);
                        displayableSenderNames.add(currentName);
                        if (displayableSenderEmails.size() > DividedImageCanvas.MAX_DIVISIONS) {
                            displayableSenderEmails.remove(0);
                            displayableSenderNames.remove(0);
                        }
                    }
                }
            }
        }
        if (shouldAddPhotos && !TextUtils.isEmpty(firstDisplayableSenderEmail)) {
            if (displayableSenderEmails.size() < DividedImageCanvas.MAX_DIVISIONS) {
                displayableSenderEmails.add(0, firstDisplayableSenderEmail);
                displayableSenderNames.add(0, firstDisplayableSender);
            } else {
                displayableSenderEmails.set(0, firstDisplayableSenderEmail);
                displayableSenderNames.set(0, firstDisplayableSender);
            }
        }
    }

    static String getMe(boolean useObjectMe) {
        return useObjectMe ? sMeObjectString : sMeSubjectString;
    }

    public static SpannableString getFormattedToHeader() {
        final SpannableString formattedToHeader = new SpannableString(sToHeaderString);
        final CharacterStyle readStyle = CharacterStyle.wrap(sReadStyleSpan);
        formattedToHeader.setSpan(readStyle, 0, formattedToHeader.length(), 0);
        return formattedToHeader;
    }

    public static SpannableString getSingularDraftString() {
        final SpannableString formattedDraftString = new SpannableString(sDraftSingularString);
        final CharacterStyle readStyle = CharacterStyle.wrap(sDraftsStyleSpan);
        formattedDraftString.setSpan(readStyle, 0, formattedDraftString.length(), 0);
        return formattedDraftString;
    }

    private static void clearResourceCache() {
        sDraftSingularString = null;
    }
}