Message.java revision 3233bff8ae08a56543c9f5abf1bc6ab38f0574ce
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 com.android.mail.providers.UIProvider.MessageColumns;
20import com.android.mail.utils.Utils;
21
22import android.content.AsyncQueryHandler;
23import android.content.ContentValues;
24import android.database.Cursor;
25import android.net.Uri;
26import android.os.Parcel;
27import android.os.Parcelable;
28import android.text.TextUtils;
29
30
31public class Message implements Parcelable {
32    public long id;
33    public long serverId;
34    public Uri uri;
35    public long conversationId;
36    public String subject;
37    public String snippet;
38    public String from;
39    public String to;
40    public String cc;
41    public String bcc;
42    public String replyTo;
43    public long dateReceivedMs;
44    public String bodyHtml;
45    public String bodyText;
46    public boolean embedsExternalResources;
47    public String refMessageId;
48    public int draftType;
49    public boolean appendRefMessageContent;
50    public boolean hasAttachments;
51    public Uri attachmentListUri;
52    public long messageFlags;
53    public String joinedAttachmentInfos;
54    public String saveUri;
55    public String sendUri;
56    public boolean alwaysShowImages;
57    public boolean includeQuotedText;
58    public int quotedTextOffset;
59
60    private String[] mToAddresses = null;
61    private String[] mCcAddresses = null;
62    private String[] mBccAddresses = null;
63    private String[] mReplyToAddresses = null;
64
65    @Override
66    public int describeContents() {
67        return 0;
68    }
69
70    @Override
71    public boolean equals(Object o) {
72        if (o == null || !(o instanceof Message)) {
73            return false;
74        }
75        final Uri otherUri = ((Message) o).uri;
76        if (uri == null) {
77            return (otherUri == null);
78        }
79        return uri.equals(otherUri);
80    }
81
82    @Override
83    public int hashCode() {
84        return uri == null ? 0 : uri.hashCode();
85    }
86
87    @Override
88    public void writeToParcel(Parcel dest, int flags) {
89        dest.writeLong(id);
90        dest.writeLong(serverId);
91        dest.writeParcelable(uri, 0);
92        dest.writeLong(conversationId);
93        dest.writeString(subject);
94        dest.writeString(snippet);
95        dest.writeString(from);
96        dest.writeString(to);
97        dest.writeString(cc);
98        dest.writeString(bcc);
99        dest.writeString(replyTo);
100        dest.writeLong(dateReceivedMs);
101        dest.writeString(bodyHtml);
102        dest.writeString(bodyText);
103        dest.writeInt(embedsExternalResources ? 1 : 0);
104        dest.writeString(refMessageId);
105        dest.writeInt(draftType);
106        dest.writeInt(appendRefMessageContent ? 1 : 0);
107        dest.writeInt(hasAttachments ? 1 : 0);
108        dest.writeParcelable(attachmentListUri, 0);
109        dest.writeLong(messageFlags);
110        dest.writeString(joinedAttachmentInfos);
111        dest.writeString(saveUri);
112        dest.writeString(sendUri);
113        dest.writeInt(alwaysShowImages ? 1 : 0);
114        dest.writeInt(includeQuotedText ? 1 : 0);
115        dest.writeInt(quotedTextOffset);
116    }
117
118    private Message(Parcel in) {
119        id = in.readLong();
120        serverId = in.readLong();
121        uri = in.readParcelable(null);
122        conversationId = in.readLong();
123        subject = in.readString();
124        snippet = in.readString();
125        from = in.readString();
126        to = in.readString();
127        cc = in.readString();
128        bcc = in.readString();
129        replyTo = in.readString();
130        dateReceivedMs = in.readLong();
131        bodyHtml = in.readString();
132        bodyText = in.readString();
133        embedsExternalResources = in.readInt() != 0;
134        refMessageId = in.readString();
135        draftType = in.readInt();
136        appendRefMessageContent = in.readInt() != 0;
137        hasAttachments = in.readInt() != 0;
138        attachmentListUri = in.readParcelable(null);
139        messageFlags = in.readLong();
140        joinedAttachmentInfos = in.readString();
141        saveUri = in.readString();
142        sendUri = in.readString();
143        alwaysShowImages = in.readInt() != 0;
144        includeQuotedText = in.readInt() != 0;
145        quotedTextOffset = in.readInt();
146    }
147
148    public Message() {
149
150    }
151
152    @Override
153    public String toString() {
154        return "[message id=" + id + "]";
155    }
156
157    public static final Creator<Message> CREATOR = new Creator<Message>() {
158
159        @Override
160        public Message createFromParcel(Parcel source) {
161            return new Message(source);
162        }
163
164        @Override
165        public Message[] newArray(int size) {
166            return new Message[size];
167        }
168
169    };
170
171    public Message(Cursor cursor) {
172        if (cursor != null) {
173            id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN);
174            serverId = cursor.getLong(UIProvider.MESSAGE_SERVER_ID_COLUMN);
175            String message = cursor.getString(UIProvider.MESSAGE_URI_COLUMN);
176            uri = !TextUtils.isEmpty(message) ? Uri.parse(message) : null;
177            conversationId = cursor.getLong(UIProvider.MESSAGE_CONVERSATION_ID_COLUMN);
178            subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN);
179            snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN);
180            from = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN);
181            to = cursor.getString(UIProvider.MESSAGE_TO_COLUMN);
182            cc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN);
183            bcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN);
184            replyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN);
185            dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN);
186            bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN);
187            bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN);
188            embedsExternalResources = cursor
189                    .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0;
190            refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN);
191            draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN);
192            appendRefMessageContent = cursor
193                    .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0;
194            hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0;
195            final String attachmentsUri = cursor
196                    .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN);
197            attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri
198                    .parse(attachmentsUri) : null;
199            messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN);
200            joinedAttachmentInfos = cursor
201                    .getString(UIProvider.MESSAGE_JOINED_ATTACHMENT_INFOS_COLUMN);
202            saveUri = cursor
203                    .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN);
204            sendUri = cursor
205                    .getString(UIProvider.MESSAGE_SEND_URI_COLUMN);
206            alwaysShowImages = cursor.getInt(UIProvider.ALWAYS_SHOW_IMAGES_COLUMN) != 0;
207            includeQuotedText = cursor.getInt(UIProvider.INCLUDE_QUOTED_TEXT_COLUMN) != 0;
208            quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN);
209        }
210    }
211
212    public boolean isStarred() {
213        return (messageFlags & UIProvider.MessageFlags.STARRED) == UIProvider.MessageFlags.STARRED;
214    }
215
216    public synchronized String[] getToAddresses() {
217        if (mToAddresses == null) {
218            mToAddresses = Utils.splitCommaSeparatedString(to);
219        }
220        return mToAddresses;
221    }
222
223    public synchronized String[] getCcAddresses() {
224        if (mCcAddresses == null) {
225            mCcAddresses = Utils.splitCommaSeparatedString(cc);
226        }
227        return mCcAddresses;
228    }
229
230    public synchronized String[] getBccAddresses() {
231        if (mBccAddresses == null) {
232            mBccAddresses = Utils.splitCommaSeparatedString(bcc);
233        }
234        return mBccAddresses;
235    }
236
237    public synchronized String[] getReplyToAddresses() {
238        if (mReplyToAddresses == null) {
239            mReplyToAddresses = Utils.splitCommaSeparatedString(replyTo);
240        }
241        return mReplyToAddresses;
242    }
243
244    /**
245     * Returns whether a "Show Pictures" button should initially appear for this message. If the
246     * button is shown, the message must also block all non-local images in the body. Inversely, if
247     * the button is not shown, the message must show all images within (or else the user would be
248     * stuck with no images and no way to reveal them).
249     *
250     * @return true if a "Show Pictures" button should appear.
251     */
252    public boolean shouldShowImagePrompt() {
253        return embedsExternalResources && !alwaysShowImages;
254    }
255
256    /**
257     * Helper method to command a provider to mark all messages from this sender with the
258     * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set.
259     *
260     * @param handler a caller-provided handler to run the query on
261     * @param token (optional) token to identify the command to the handler
262     * @param cookie (optional) cookie to pass to the handler
263     */
264    public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) {
265        final ContentValues values = new ContentValues(1);
266        values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1);
267
268        handler.startUpdate(token, cookie, uri, values, null, null);
269    }
270
271}
272