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