Message.java revision d8e249ee7abb0e1ad24e17e4e299736d4a150936
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 read;
58    public boolean starred;
59    public boolean includeQuotedText;
60    public int quotedTextOffset;
61
62    private String[] mToAddresses = null;
63    private String[] mCcAddresses = null;
64    private String[] mBccAddresses = null;
65    private String[] mReplyToAddresses = null;
66
67    @Override
68    public int describeContents() {
69        return 0;
70    }
71
72    @Override
73    public boolean equals(Object o) {
74        if (o == null || !(o instanceof Message)) {
75            return false;
76        }
77        final Uri otherUri = ((Message) o).uri;
78        if (uri == null) {
79            return (otherUri == null);
80        }
81        return uri.equals(otherUri);
82    }
83
84    @Override
85    public int hashCode() {
86        return uri == null ? 0 : uri.hashCode();
87    }
88
89    @Override
90    public void writeToParcel(Parcel dest, int flags) {
91        dest.writeLong(id);
92        dest.writeLong(serverId);
93        dest.writeParcelable(uri, 0);
94        dest.writeLong(conversationId);
95        dest.writeString(subject);
96        dest.writeString(snippet);
97        dest.writeString(from);
98        dest.writeString(to);
99        dest.writeString(cc);
100        dest.writeString(bcc);
101        dest.writeString(replyTo);
102        dest.writeLong(dateReceivedMs);
103        dest.writeString(bodyHtml);
104        dest.writeString(bodyText);
105        dest.writeInt(embedsExternalResources ? 1 : 0);
106        dest.writeString(refMessageId);
107        dest.writeInt(draftType);
108        dest.writeInt(appendRefMessageContent ? 1 : 0);
109        dest.writeInt(hasAttachments ? 1 : 0);
110        dest.writeParcelable(attachmentListUri, 0);
111        dest.writeLong(messageFlags);
112        dest.writeString(joinedAttachmentInfos);
113        dest.writeString(saveUri);
114        dest.writeString(sendUri);
115        dest.writeInt(alwaysShowImages ? 1 : 0);
116        dest.writeInt(includeQuotedText ? 1 : 0);
117        dest.writeInt(quotedTextOffset);
118    }
119
120    private Message(Parcel in) {
121        id = in.readLong();
122        serverId = in.readLong();
123        uri = in.readParcelable(null);
124        conversationId = in.readLong();
125        subject = in.readString();
126        snippet = in.readString();
127        from = in.readString();
128        to = in.readString();
129        cc = in.readString();
130        bcc = in.readString();
131        replyTo = in.readString();
132        dateReceivedMs = in.readLong();
133        bodyHtml = in.readString();
134        bodyText = in.readString();
135        embedsExternalResources = in.readInt() != 0;
136        refMessageId = in.readString();
137        draftType = in.readInt();
138        appendRefMessageContent = in.readInt() != 0;
139        hasAttachments = in.readInt() != 0;
140        attachmentListUri = in.readParcelable(null);
141        messageFlags = in.readLong();
142        joinedAttachmentInfos = in.readString();
143        saveUri = in.readString();
144        sendUri = in.readString();
145        alwaysShowImages = in.readInt() != 0;
146        includeQuotedText = in.readInt() != 0;
147        quotedTextOffset = in.readInt();
148    }
149
150    public Message() {
151
152    }
153
154    @Override
155    public String toString() {
156        return "[message id=" + id + "]";
157    }
158
159    public static final Creator<Message> CREATOR = new Creator<Message>() {
160
161        @Override
162        public Message createFromParcel(Parcel source) {
163            return new Message(source);
164        }
165
166        @Override
167        public Message[] newArray(int size) {
168            return new Message[size];
169        }
170
171    };
172
173    public Message(Cursor cursor) {
174        if (cursor != null) {
175            id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN);
176            serverId = cursor.getLong(UIProvider.MESSAGE_SERVER_ID_COLUMN);
177            String message = cursor.getString(UIProvider.MESSAGE_URI_COLUMN);
178            uri = !TextUtils.isEmpty(message) ? Uri.parse(message) : null;
179            conversationId = cursor.getLong(UIProvider.MESSAGE_CONVERSATION_ID_COLUMN);
180            subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN);
181            snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN);
182            from = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN);
183            to = cursor.getString(UIProvider.MESSAGE_TO_COLUMN);
184            cc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN);
185            bcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN);
186            replyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN);
187            dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN);
188            bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN);
189            bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN);
190            embedsExternalResources = cursor
191                    .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0;
192            refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN);
193            draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN);
194            appendRefMessageContent = cursor
195                    .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0;
196            hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0;
197            final String attachmentsUri = cursor
198                    .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN);
199            attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri
200                    .parse(attachmentsUri) : null;
201            messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN);
202            joinedAttachmentInfos = cursor
203                    .getString(UIProvider.MESSAGE_JOINED_ATTACHMENT_INFOS_COLUMN);
204            saveUri = cursor
205                    .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN);
206            sendUri = cursor
207                    .getString(UIProvider.MESSAGE_SEND_URI_COLUMN);
208            alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0;
209            read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0;
210            starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0;
211            includeQuotedText = cursor.getInt(UIProvider.INCLUDE_QUOTED_TEXT_COLUMN) != 0;
212            quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN);
213        }
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     * Helper method to command a provider to star/unstar this message.
273     *
274     * @param starred whether to star or unstar the message
275     * @param handler a caller-provided handler to run the query on
276     * @param token (optional) token to identify the command to the handler
277     * @param cookie (optional) cookie to pass to the handler
278     */
279    public void star(boolean starred, AsyncQueryHandler handler, int token, Object cookie) {
280        final ContentValues values = new ContentValues(1);
281        values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0);
282
283        handler.startUpdate(token, cookie, uri, values, null, null);
284    }
285
286}
287