Message.java revision 5c5fd571b40a15b95ea5a5d6033f377706ffd0fb
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.text.TextUtils;
26
27import com.android.mail.browse.ConversationCursor;
28import com.android.mail.browse.MessageCursor;
29import com.android.mail.providers.UIProvider.ConversationColumns;
30import com.android.mail.providers.UIProvider.MessageColumns;
31import com.android.mail.utils.Utils;
32
33import java.util.Collections;
34import java.util.List;
35
36
37public class Message implements Parcelable {
38    public long id;
39    public long serverId;
40    public Uri uri;
41    public String conversationUri;
42    public String subject;
43    public String snippet;
44    public String from;
45    public String to;
46    public String cc;
47    public String bcc;
48    public String replyTo;
49    public long dateReceivedMs;
50    public String bodyHtml;
51    public String bodyText;
52    public boolean embedsExternalResources;
53    public String refMessageId;
54    public int draftType;
55    public boolean appendRefMessageContent;
56    public boolean hasAttachments;
57    public Uri attachmentListUri;
58    public long messageFlags;
59    @Deprecated
60    public String joinedAttachmentInfos;
61    public String saveUri;
62    public String sendUri;
63    public boolean alwaysShowImages;
64    public boolean read;
65    public boolean starred;
66    public int quotedTextOffset;
67    public String attachmentsJson;
68    public Uri accountUri;
69
70    private transient String[] mToAddresses = null;
71    private transient String[] mCcAddresses = null;
72    private transient String[] mBccAddresses = null;
73    private transient String[] mReplyToAddresses = null;
74
75    private transient List<Attachment> mAttachments = null;
76
77    // While viewing a list of messages, points to the MessageCursor that contains it
78    private transient MessageCursor mMessageCursor = null;
79
80    @Override
81    public int describeContents() {
82        return 0;
83    }
84
85    @Override
86    public boolean equals(Object o) {
87        if (o == null || !(o instanceof Message)) {
88            return false;
89        }
90        final Uri otherUri = ((Message) o).uri;
91        if (uri == null) {
92            return (otherUri == null);
93        }
94        return uri.equals(otherUri);
95    }
96
97    @Override
98    public int hashCode() {
99        return uri == null ? 0 : uri.hashCode();
100    }
101
102    @Override
103    public void writeToParcel(Parcel dest, int flags) {
104        dest.writeLong(id);
105        dest.writeLong(serverId);
106        dest.writeParcelable(uri, 0);
107        dest.writeString(conversationUri);
108        dest.writeString(subject);
109        dest.writeString(snippet);
110        dest.writeString(from);
111        dest.writeString(to);
112        dest.writeString(cc);
113        dest.writeString(bcc);
114        dest.writeString(replyTo);
115        dest.writeLong(dateReceivedMs);
116        dest.writeString(bodyHtml);
117        dest.writeString(bodyText);
118        dest.writeInt(embedsExternalResources ? 1 : 0);
119        dest.writeString(refMessageId);
120        dest.writeInt(draftType);
121        dest.writeInt(appendRefMessageContent ? 1 : 0);
122        dest.writeInt(hasAttachments ? 1 : 0);
123        dest.writeParcelable(attachmentListUri, 0);
124        dest.writeLong(messageFlags);
125        dest.writeString(joinedAttachmentInfos);
126        dest.writeString(saveUri);
127        dest.writeString(sendUri);
128        dest.writeInt(alwaysShowImages ? 1 : 0);
129        dest.writeInt(quotedTextOffset);
130        dest.writeString(attachmentsJson);
131        dest.writeParcelable(accountUri, 0);
132    }
133
134    private Message(Parcel in) {
135        id = in.readLong();
136        serverId = in.readLong();
137        uri = in.readParcelable(null);
138        conversationUri = in.readString();
139        subject = in.readString();
140        snippet = in.readString();
141        from = in.readString();
142        to = in.readString();
143        cc = in.readString();
144        bcc = in.readString();
145        replyTo = in.readString();
146        dateReceivedMs = in.readLong();
147        bodyHtml = in.readString();
148        bodyText = in.readString();
149        embedsExternalResources = in.readInt() != 0;
150        refMessageId = in.readString();
151        draftType = in.readInt();
152        appendRefMessageContent = in.readInt() != 0;
153        hasAttachments = in.readInt() != 0;
154        attachmentListUri = in.readParcelable(null);
155        messageFlags = in.readLong();
156        joinedAttachmentInfos = in.readString();
157        saveUri = in.readString();
158        sendUri = in.readString();
159        alwaysShowImages = in.readInt() != 0;
160        quotedTextOffset = in.readInt();
161        attachmentsJson = in.readString();
162        accountUri = in.readParcelable(null);
163    }
164
165    public Message() {
166
167    }
168
169    @Override
170    public String toString() {
171        return "[message id=" + id + "]";
172    }
173
174    public static final Creator<Message> CREATOR = new Creator<Message>() {
175
176        @Override
177        public Message createFromParcel(Parcel source) {
178            return new Message(source);
179        }
180
181        @Override
182        public Message[] newArray(int size) {
183            return new Message[size];
184        }
185
186    };
187
188    public Message(MessageCursor cursor) {
189        this((Cursor)cursor);
190        // Only set message cursor if appropriate
191        mMessageCursor = cursor;
192    }
193
194    public Message(Cursor cursor) {
195        if (cursor != null) {
196            id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN);
197            serverId = cursor.getLong(UIProvider.MESSAGE_SERVER_ID_COLUMN);
198            String message = cursor.getString(UIProvider.MESSAGE_URI_COLUMN);
199            uri = !TextUtils.isEmpty(message) ? Uri.parse(message) : null;
200            conversationUri = cursor.getString(UIProvider.MESSAGE_CONVERSATION_URI_COLUMN);
201            subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN);
202            snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN);
203            from = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN);
204            to = cursor.getString(UIProvider.MESSAGE_TO_COLUMN);
205            cc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN);
206            bcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN);
207            replyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN);
208            dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN);
209            bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN);
210            bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN);
211            embedsExternalResources = cursor
212                    .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0;
213            refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN);
214            draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN);
215            appendRefMessageContent = cursor
216                    .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0;
217            hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0;
218            final String attachmentsUri = cursor
219                    .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN);
220            attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri
221                    .parse(attachmentsUri) : null;
222            messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN);
223            joinedAttachmentInfos = cursor
224                    .getString(UIProvider.MESSAGE_JOINED_ATTACHMENT_INFOS_COLUMN);
225            saveUri = cursor
226                    .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN);
227            sendUri = cursor
228                    .getString(UIProvider.MESSAGE_SEND_URI_COLUMN);
229            alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0;
230            read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0;
231            starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0;
232            quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN);
233            attachmentsJson = cursor.getString(UIProvider.MESSAGE_ATTACHMENTS_COLUMN);
234            String accountUriString = cursor.getString(UIProvider.MESSAGE_ACCOUNT_URI_COLUMN);
235            accountUri = !TextUtils.isEmpty(accountUriString) ? Uri.parse(accountUriString) : null;
236        }
237    }
238
239    public synchronized String[] getToAddresses() {
240        if (mToAddresses == null) {
241            mToAddresses = Utils.splitCommaSeparatedString(to);
242        }
243        return mToAddresses;
244    }
245
246    public synchronized String[] getCcAddresses() {
247        if (mCcAddresses == null) {
248            mCcAddresses = Utils.splitCommaSeparatedString(cc);
249        }
250        return mCcAddresses;
251    }
252
253    public synchronized String[] getBccAddresses() {
254        if (mBccAddresses == null) {
255            mBccAddresses = Utils.splitCommaSeparatedString(bcc);
256        }
257        return mBccAddresses;
258    }
259
260    public synchronized String[] getReplyToAddresses() {
261        if (mReplyToAddresses == null) {
262            mReplyToAddresses = Utils.splitCommaSeparatedString(replyTo);
263        }
264        return mReplyToAddresses;
265    }
266
267    public synchronized List<Attachment> getAttachments() {
268        if (mAttachments == null) {
269            if (attachmentsJson != null) {
270                mAttachments = Attachment.fromJSONArray(attachmentsJson);
271            } else {
272                mAttachments = Collections.emptyList();
273            }
274        }
275        return mAttachments;
276    }
277
278    /**
279     * Returns whether a "Show Pictures" button should initially appear for this message. If the
280     * button is shown, the message must also block all non-local images in the body. Inversely, if
281     * the button is not shown, the message must show all images within (or else the user would be
282     * stuck with no images and no way to reveal them).
283     *
284     * @return true if a "Show Pictures" button should appear.
285     */
286    public boolean shouldShowImagePrompt() {
287        return embedsExternalResources && !alwaysShowImages;
288    }
289
290    /**
291     * Helper method to command a provider to mark all messages from this sender with the
292     * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set.
293     *
294     * @param handler a caller-provided handler to run the query on
295     * @param token (optional) token to identify the command to the handler
296     * @param cookie (optional) cookie to pass to the handler
297     */
298    public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) {
299        final ContentValues values = new ContentValues(1);
300        values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1);
301
302        handler.startUpdate(token, cookie, uri, values, null, null);
303    }
304
305    /**
306     * Helper method to command a provider to star/unstar this message.
307     *
308     * @param starred whether to star or unstar the message
309     * @param handler a caller-provided handler to run the query on
310     * @param token (optional) token to identify the command to the handler
311     * @param cookie (optional) cookie to pass to the handler
312     */
313    public void star(boolean starred, AsyncQueryHandler handler, int token, Object cookie) {
314        this.starred = starred;
315        boolean conversationStarred = starred;
316        // If we're unstarring, we need to find out if the conversation is still starred
317        if (mMessageCursor != null && !starred) {
318            conversationStarred = mMessageCursor.isConversationStarred();
319        }
320        // Update the conversation cursor so that changes are reflected simultaneously
321        ConversationCursor.setConversationColumn(conversationUri, ConversationColumns.STARRED,
322                conversationStarred);
323        final ContentValues values = new ContentValues(1);
324        values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0);
325
326        handler.startUpdate(token, cookie, uri, values, null, null);
327    }
328
329}
330