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