Message.java revision a990a715012379451344246c5156d29938f12886
1/**
2 * Copyright (c) 2012, Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mail.providers;
18
19import android.content.AsyncQueryHandler;
20import android.content.ContentValues;
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.provider.BaseColumns;
26import android.text.Html;
27import android.text.SpannedString;
28import android.text.TextUtils;
29import android.text.util.Rfc822Token;
30import android.text.util.Rfc822Tokenizer;
31
32import com.android.mail.providers.UIProvider.MessageColumns;
33import com.android.mail.utils.Utils;
34import com.google.common.base.Objects;
35
36import java.util.Collections;
37import java.util.List;
38import java.util.regex.Pattern;
39
40
41public class Message implements Parcelable {
42    /**
43     * Regex pattern used to look for any inline images in message bodies, including Gmail-hosted
44     * relative-URL images, Gmail emoticons, and any external inline images (although we usually
45     * count on the server to detect external images).
46     */
47    private static Pattern INLINE_IMAGE_PATTERN = Pattern.compile("<img\\s+[^>]*src=",
48            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
49
50    /**
51     * @see BaseColumns#_ID
52     */
53    public long id;
54    /**
55     * @see UIProvider.MessageColumns#SERVER_ID
56     */
57    public String serverId;
58    /**
59     * @see UIProvider.MessageColumns#URI
60     */
61    public Uri uri;
62    /**
63     * @see UIProvider.MessageColumns#CONVERSATION_ID
64     */
65    public Uri conversationUri;
66    /**
67     * @see UIProvider.MessageColumns#SUBJECT
68     */
69    public String subject;
70    /**
71     * @see UIProvider.MessageColumns#SNIPPET
72     */
73    public String snippet;
74    /**
75     * @see UIProvider.MessageColumns#FROM
76     */
77    private String mFrom;
78    /**
79     * @see UIProvider.MessageColumns#TO
80     */
81    private String mTo;
82    /**
83     * @see UIProvider.MessageColumns#CC
84     */
85    private String mCc;
86    /**
87     * @see UIProvider.MessageColumns#BCC
88     */
89    private String mBcc;
90    /**
91     * @see UIProvider.MessageColumns#REPLY_TO
92     */
93    private String mReplyTo;
94    /**
95     * @see UIProvider.MessageColumns#DATE_RECEIVED_MS
96     */
97    public long dateReceivedMs;
98    /**
99     * @see UIProvider.MessageColumns#BODY_HTML
100     */
101    public String bodyHtml;
102    /**
103     * @see UIProvider.MessageColumns#BODY_TEXT
104     */
105    public String bodyText;
106    /**
107     * @see UIProvider.MessageColumns#EMBEDS_EXTERNAL_RESOURCES
108     */
109    public boolean embedsExternalResources;
110    /**
111     * @see UIProvider.MessageColumns#REF_MESSAGE_ID
112     */
113    public Uri refMessageUri;
114    /**
115     * @see UIProvider.MessageColumns#DRAFT_TYPE
116     */
117    public int draftType;
118    /**
119     * @see UIProvider.MessageColumns#APPEND_REF_MESSAGE_CONTENT
120     */
121    public boolean appendRefMessageContent;
122    /**
123     * @see UIProvider.MessageColumns#HAS_ATTACHMENTS
124     */
125    public boolean hasAttachments;
126    /**
127     * @see UIProvider.MessageColumns#ATTACHMENT_LIST_URI
128     */
129    public Uri attachmentListUri;
130    /**
131     * @see UIProvider.MessageColumns#MESSAGE_FLAGS
132     */
133    public long messageFlags;
134    /**
135     * @see UIProvider.MessageColumns#ALWAYS_SHOW_IMAGES
136     */
137    public boolean alwaysShowImages;
138    /**
139     * @see UIProvider.MessageColumns#READ
140     */
141    public boolean read;
142    /**
143     * @see UIProvider.MessageColumns#SEEN
144     */
145    public boolean seen;
146    /**
147     * @see UIProvider.MessageColumns#STARRED
148     */
149    public boolean starred;
150    /**
151     * @see UIProvider.MessageColumns#QUOTE_START_POS
152     */
153    public int quotedTextOffset;
154    /**
155     * @see UIProvider.MessageColumns#ATTACHMENTS
156     *<p>
157     * N.B. this value is NOT immutable and may change during conversation view render.
158     */
159    public String attachmentsJson;
160    /**
161     * @see UIProvider.MessageColumns#MESSAGE_ACCOUNT_URI
162     */
163    public Uri accountUri;
164    /**
165     * @see UIProvider.MessageColumns#EVENT_INTENT_URI
166     */
167    public Uri eventIntentUri;
168    /**
169     * @see UIProvider.MessageColumns#SPAM_WARNING_STRING
170     */
171    public String spamWarningString;
172    /**
173     * @see UIProvider.MessageColumns#SPAM_WARNING_LEVEL
174     */
175    public int spamWarningLevel;
176    /**
177     * @see UIProvider.MessageColumns#SPAM_WARNING_LINK_TYPE
178     */
179    public int spamLinkType;
180    /**
181     * @see UIProvider.MessageColumns#VIA_DOMAIN
182     */
183    public String viaDomain;
184    /**
185     * @see UIProvider.MessageColumns#IS_SENDING
186     */
187    public boolean isSending;
188
189    private transient String[] mFromAddresses = null;
190    private transient String[] mToAddresses = null;
191    private transient String[] mCcAddresses = null;
192    private transient String[] mBccAddresses = null;
193    private transient String[] mReplyToAddresses = null;
194
195    private transient List<Attachment> mAttachments = null;
196
197    @Override
198    public int describeContents() {
199        return 0;
200    }
201
202    @Override
203    public boolean equals(Object o) {
204        return this == o || (o != null && o instanceof Message
205                && Objects.equal(uri, ((Message) o).uri));
206    }
207
208    @Override
209    public int hashCode() {
210        return uri == null ? 0 : uri.hashCode();
211    }
212
213    @Override
214    public void writeToParcel(Parcel dest, int flags) {
215        dest.writeLong(id);
216        dest.writeString(serverId);
217        dest.writeParcelable(uri, 0);
218        dest.writeParcelable(conversationUri, 0);
219        dest.writeString(subject);
220        dest.writeString(snippet);
221        dest.writeString(mFrom);
222        dest.writeString(mTo);
223        dest.writeString(mCc);
224        dest.writeString(mBcc);
225        dest.writeString(mReplyTo);
226        dest.writeLong(dateReceivedMs);
227        dest.writeString(bodyHtml);
228        dest.writeString(bodyText);
229        dest.writeInt(embedsExternalResources ? 1 : 0);
230        dest.writeParcelable(refMessageUri, 0);
231        dest.writeInt(draftType);
232        dest.writeInt(appendRefMessageContent ? 1 : 0);
233        dest.writeInt(hasAttachments ? 1 : 0);
234        dest.writeParcelable(attachmentListUri, 0);
235        dest.writeLong(messageFlags);
236        dest.writeInt(alwaysShowImages ? 1 : 0);
237        dest.writeInt(quotedTextOffset);
238        dest.writeString(attachmentsJson);
239        dest.writeParcelable(accountUri, 0);
240        dest.writeParcelable(eventIntentUri, 0);
241        dest.writeString(spamWarningString);
242        dest.writeInt(spamWarningLevel);
243        dest.writeInt(spamLinkType);
244        dest.writeString(viaDomain);
245        dest.writeInt(isSending ? 1 : 0);
246    }
247
248    private Message(Parcel in) {
249        id = in.readLong();
250        serverId = in.readString();
251        uri = in.readParcelable(null);
252        conversationUri = in.readParcelable(null);
253        subject = in.readString();
254        snippet = in.readString();
255        mFrom = in.readString();
256        mTo = in.readString();
257        mCc = in.readString();
258        mBcc = in.readString();
259        mReplyTo = in.readString();
260        dateReceivedMs = in.readLong();
261        bodyHtml = in.readString();
262        bodyText = in.readString();
263        embedsExternalResources = in.readInt() != 0;
264        refMessageUri = in.readParcelable(null);
265        draftType = in.readInt();
266        appendRefMessageContent = in.readInt() != 0;
267        hasAttachments = in.readInt() != 0;
268        attachmentListUri = in.readParcelable(null);
269        messageFlags = in.readLong();
270        alwaysShowImages = in.readInt() != 0;
271        quotedTextOffset = in.readInt();
272        attachmentsJson = in.readString();
273        accountUri = in.readParcelable(null);
274        eventIntentUri = in.readParcelable(null);
275        spamWarningString = in.readString();
276        spamWarningLevel = in.readInt();
277        spamLinkType = in.readInt();
278        viaDomain = in.readString();
279        isSending = in.readInt() != 0;
280    }
281
282    public Message() {
283
284    }
285
286    @Override
287    public String toString() {
288        return "[message id=" + id + "]";
289    }
290
291    public static final Creator<Message> CREATOR = new Creator<Message>() {
292
293        @Override
294        public Message createFromParcel(Parcel source) {
295            return new Message(source);
296        }
297
298        @Override
299        public Message[] newArray(int size) {
300            return new Message[size];
301        }
302
303    };
304
305    public Message(Cursor cursor) {
306        if (cursor != null) {
307            id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN);
308            serverId = cursor.getString(UIProvider.MESSAGE_SERVER_ID_COLUMN);
309            final String messageUriStr = cursor.getString(UIProvider.MESSAGE_URI_COLUMN);
310            uri = !TextUtils.isEmpty(messageUriStr) ? Uri.parse(messageUriStr) : null;
311            final String convUriStr = cursor.getString(UIProvider.MESSAGE_CONVERSATION_URI_COLUMN);
312            conversationUri = !TextUtils.isEmpty(convUriStr) ? Uri.parse(convUriStr) : null;
313            subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN);
314            snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN);
315            mFrom = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN);
316            mTo = cursor.getString(UIProvider.MESSAGE_TO_COLUMN);
317            mCc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN);
318            mBcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN);
319            mReplyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN);
320            dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN);
321            bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN);
322            bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN);
323            embedsExternalResources = cursor
324                    .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0;
325            final String refMessageUriStr =
326                    cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_URI_COLUMN);
327            refMessageUri = !TextUtils.isEmpty(refMessageUriStr) ?
328                    Uri.parse(refMessageUriStr) : null;
329            draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN);
330            appendRefMessageContent = cursor
331                    .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0;
332            hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0;
333            final String attachmentsUri = cursor
334                    .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN);
335            attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri
336                    .parse(attachmentsUri) : null;
337            messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN);
338            alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0;
339            read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0;
340            seen = cursor.getInt(UIProvider.MESSAGE_SEEN_COLUMN) != 0;
341            starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0;
342            quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN);
343            attachmentsJson = cursor.getString(UIProvider.MESSAGE_ATTACHMENTS_COLUMN);
344            String accountUriString = cursor.getString(UIProvider.MESSAGE_ACCOUNT_URI_COLUMN);
345            accountUri = !TextUtils.isEmpty(accountUriString) ? Uri.parse(accountUriString) : null;
346            eventIntentUri =
347                    Utils.getValidUri(cursor.getString(UIProvider.MESSAGE_EVENT_INTENT_COLUMN));
348            spamWarningString =
349                    cursor.getString(UIProvider.MESSAGE_SPAM_WARNING_STRING_ID_COLUMN);
350            spamWarningLevel = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LEVEL_COLUMN);
351            spamLinkType = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LINK_TYPE_COLUMN);
352            viaDomain = cursor.getString(UIProvider.MESSAGE_VIA_DOMAIN_COLUMN);
353            isSending = cursor.getInt(UIProvider.MESSAGE_IS_SENDING_COLUMN) != 0;
354        }
355    }
356
357    public boolean isFlaggedReplied() {
358        return (messageFlags & UIProvider.MessageFlags.REPLIED) ==
359                UIProvider.MessageFlags.REPLIED;
360    }
361
362    public boolean isFlaggedForwarded() {
363        return (messageFlags & UIProvider.MessageFlags.FORWARDED) ==
364                UIProvider.MessageFlags.FORWARDED;
365    }
366
367    public boolean isFlaggedCalendarInvite() {
368        return (messageFlags & UIProvider.MessageFlags.CALENDAR_INVITE) ==
369                UIProvider.MessageFlags.CALENDAR_INVITE;
370    }
371
372    public String getFrom() {
373        return mFrom;
374    }
375
376    public synchronized void setFrom(final String from) {
377        mFrom = from;
378        mFromAddresses = null;
379    }
380
381    public String getTo() {
382        return mTo;
383    }
384
385    public synchronized void setTo(final String to) {
386        mTo = to;
387        mToAddresses = null;
388    }
389
390    public String getCc() {
391        return mCc;
392    }
393
394    public synchronized void setCc(final String cc) {
395        mCc = cc;
396        mCcAddresses = null;
397    }
398
399    public String getBcc() {
400        return mBcc;
401    }
402
403    public synchronized void setBcc(final String bcc) {
404        mBcc = bcc;
405        mBccAddresses = null;
406    }
407
408    public String getReplyTo() {
409        return mReplyTo;
410    }
411
412    public synchronized void setReplyTo(final String replyTo) {
413        mReplyTo = replyTo;
414        mReplyToAddresses = null;
415    }
416
417    public synchronized String[] getFromAddresses() {
418        if (mFromAddresses == null) {
419            mFromAddresses = tokenizeAddresses(mFrom);
420        }
421        return mFromAddresses;
422    }
423
424    public synchronized String[] getToAddresses() {
425        if (mToAddresses == null) {
426            mToAddresses = tokenizeAddresses(mTo);
427        }
428        return mToAddresses;
429    }
430
431    public synchronized String[] getCcAddresses() {
432        if (mCcAddresses == null) {
433            mCcAddresses = tokenizeAddresses(mCc);
434        }
435        return mCcAddresses;
436    }
437
438    public synchronized String[] getBccAddresses() {
439        if (mBccAddresses == null) {
440            mBccAddresses = tokenizeAddresses(mBcc);
441        }
442        return mBccAddresses;
443    }
444
445    public synchronized String[] getReplyToAddresses() {
446        if (mReplyToAddresses == null) {
447            mReplyToAddresses = tokenizeAddresses(mReplyTo);
448        }
449        return mReplyToAddresses;
450    }
451
452    public static String[] tokenizeAddresses(String addresses) {
453        if (TextUtils.isEmpty(addresses)) {
454            return new String[0];
455        }
456        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addresses);
457        String[] strings = new String[tokens.length];
458        for (int i = 0; i < tokens.length;i++) {
459            strings[i] = tokens[i].toString();
460        }
461        return strings;
462    }
463
464    public List<Attachment> getAttachments() {
465        if (mAttachments == null) {
466            if (attachmentsJson != null) {
467                mAttachments = Attachment.fromJSONArray(attachmentsJson);
468            } else {
469                mAttachments = Collections.emptyList();
470            }
471        }
472        return mAttachments;
473    }
474
475    /**
476     * Returns whether a "Show Pictures" button should initially appear for this message. If the
477     * button is shown, the message must also block all non-local images in the body. Inversely, if
478     * the button is not shown, the message must show all images within (or else the user would be
479     * stuck with no images and no way to reveal them).
480     *
481     * @return true if a "Show Pictures" button should appear.
482     */
483    public boolean shouldShowImagePrompt() {
484        return !alwaysShowImages && embedsExternalResources();
485    }
486
487    private boolean embedsExternalResources() {
488        return embedsExternalResources ||
489                (!TextUtils.isEmpty(bodyHtml) && INLINE_IMAGE_PATTERN.matcher(bodyHtml).find());
490    }
491
492    /**
493     * Helper method to command a provider to mark all messages from this sender with the
494     * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set.
495     *
496     * @param handler a caller-provided handler to run the query on
497     * @param token (optional) token to identify the command to the handler
498     * @param cookie (optional) cookie to pass to the handler
499     */
500    public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) {
501        alwaysShowImages = true;
502
503        final ContentValues values = new ContentValues(1);
504        values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1);
505
506        handler.startUpdate(token, cookie, uri, values, null, null);
507    }
508
509    public String getBodyAsHtml() {
510        String body = "";
511        if (!TextUtils.isEmpty(bodyHtml)) {
512            body = bodyHtml;
513        } else if (!TextUtils.isEmpty(bodyText)) {
514            body = Html.toHtml(new SpannedString(bodyText));
515        }
516        return body;
517    }
518
519}
520