Message.java revision bb87b7fa57f25fc9cee1f572dd253f33f58f81da
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.utils.Utils;
20
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.text.TextUtils;
26
27
28public class Message implements Parcelable {
29    public long id;
30    public long serverId;
31    public Uri uri;
32    public long conversationId;
33    public String subject;
34    public String snippet;
35    public String from;
36    public String to;
37    public String cc;
38    public String bcc;
39    public String replyTo;
40    public long dateReceivedMs;
41    public String bodyHtml;
42    public String bodyText;
43    public boolean embedsExternalResources;
44    public String refMessageId;
45    public int draftType;
46    public boolean appendRefMessageContent;
47    public boolean hasAttachments;
48    public Uri attachmentListUri;
49    public long messageFlags;
50    public String joinedAttachmentInfos;
51    public String saveUri;
52    public String sendUri;
53    public boolean alwaysShowImages;
54    public boolean includeQuotedText;
55    public int quotedTextOffset;
56
57    private String[] mToAddresses = null;
58    private String[] mCcAddresses = null;
59    private String[] mBccAddresses = null;
60    private String[] mReplyToAddresses = null;
61
62    @Override
63    public int describeContents() {
64        return 0;
65    }
66
67    @Override
68    public void writeToParcel(Parcel dest, int flags) {
69        dest.writeLong(id);
70        dest.writeLong(serverId);
71        dest.writeParcelable(uri, 0);
72        dest.writeLong(conversationId);
73        dest.writeString(subject);
74        dest.writeString(snippet);
75        dest.writeString(from);
76        dest.writeString(to);
77        dest.writeString(cc);
78        dest.writeString(bcc);
79        dest.writeString(replyTo);
80        dest.writeLong(dateReceivedMs);
81        dest.writeString(bodyHtml);
82        dest.writeString(bodyText);
83        dest.writeInt(embedsExternalResources ? 1 : 0);
84        dest.writeString(refMessageId);
85        dest.writeInt(draftType);
86        dest.writeInt(appendRefMessageContent ? 1 : 0);
87        dest.writeInt(hasAttachments ? 1 : 0);
88        dest.writeParcelable(attachmentListUri, 0);
89        dest.writeLong(messageFlags);
90        dest.writeString(joinedAttachmentInfos);
91        dest.writeString(saveUri);
92        dest.writeString(sendUri);
93        dest.writeInt(alwaysShowImages ? 1 : 0);
94        dest.writeInt(includeQuotedText ? 1 : 0);
95        dest.writeInt(quotedTextOffset);
96    }
97
98    private Message(Parcel in) {
99        id = in.readLong();
100        serverId = in.readLong();
101        uri = in.readParcelable(null);
102        conversationId = in.readLong();
103        subject = in.readString();
104        snippet = in.readString();
105        from = in.readString();
106        to = in.readString();
107        cc = in.readString();
108        bcc = in.readString();
109        replyTo = in.readString();
110        dateReceivedMs = in.readLong();
111        bodyHtml = in.readString();
112        bodyText = in.readString();
113        embedsExternalResources = in.readInt() != 0;
114        refMessageId = in.readString();
115        draftType = in.readInt();
116        appendRefMessageContent = in.readInt() != 0;
117        hasAttachments = in.readInt() != 0;
118        attachmentListUri = in.readParcelable(null);
119        messageFlags = in.readLong();
120        joinedAttachmentInfos = in.readString();
121        saveUri = in.readString();
122        sendUri = in.readString();
123        alwaysShowImages = in.readInt() != 0;
124        includeQuotedText = in.readInt() != 0;
125        quotedTextOffset = in.readInt();
126    }
127
128    public Message() {
129
130    }
131
132    @Override
133    public String toString() {
134        return "[message id=" + id + "]";
135    }
136
137    public static final Creator<Message> CREATOR = new Creator<Message>() {
138
139        @Override
140        public Message createFromParcel(Parcel source) {
141            return new Message(source);
142        }
143
144        @Override
145        public Message[] newArray(int size) {
146            return new Message[size];
147        }
148
149    };
150
151    public Message(Cursor cursor) {
152        if (cursor != null) {
153            id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN);
154            serverId = cursor.getLong(UIProvider.MESSAGE_SERVER_ID_COLUMN);
155            String message = cursor.getString(UIProvider.MESSAGE_URI_COLUMN);
156            uri = !TextUtils.isEmpty(message) ? Uri.parse(message) : null;
157            conversationId = cursor.getLong(UIProvider.MESSAGE_CONVERSATION_ID_COLUMN);
158            subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN);
159            snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN);
160            from = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN);
161            to = cursor.getString(UIProvider.MESSAGE_TO_COLUMN);
162            cc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN);
163            bcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN);
164            replyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN);
165            dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN);
166            bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN);
167            bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN);
168            embedsExternalResources = cursor
169                    .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0;
170            refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN);
171            draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN);
172            appendRefMessageContent = cursor
173                    .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0;
174            hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0;
175            final String attachmentsUri = cursor
176                    .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN);
177            attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri
178                    .parse(attachmentsUri) : null;
179            messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN);
180            joinedAttachmentInfos = cursor
181                    .getString(UIProvider.MESSAGE_JOINED_ATTACHMENT_INFOS_COLUMN);
182            saveUri = cursor
183                    .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN);
184            sendUri = cursor
185                    .getString(UIProvider.MESSAGE_SEND_URI_COLUMN);
186            alwaysShowImages = cursor.getInt(UIProvider.ALWAYS_SHOW_IMAGES_COLUMN) != 0;
187            includeQuotedText = cursor.getInt(UIProvider.INCLUDE_QUOTED_TEXT_COLUMN) != 0;
188            quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN);
189        }
190    }
191
192    public boolean isStarred() {
193        return (messageFlags & UIProvider.MessageFlags.STARRED) == UIProvider.MessageFlags.STARRED;
194    }
195
196    public synchronized String[] getToAddresses() {
197        if (mToAddresses == null) {
198            mToAddresses = Utils.splitCommaSeparatedString(to);
199        }
200        return mToAddresses;
201    }
202
203    public synchronized String[] getCcAddresses() {
204        if (mCcAddresses == null) {
205            mCcAddresses = Utils.splitCommaSeparatedString(cc);
206        }
207        return mCcAddresses;
208    }
209
210    public synchronized String[] getBccAddresses() {
211        if (mBccAddresses == null) {
212            mBccAddresses = Utils.splitCommaSeparatedString(bcc);
213        }
214        return mBccAddresses;
215    }
216
217    public synchronized String[] getReplyToAddresses() {
218        if (mReplyToAddresses == null) {
219            mReplyToAddresses = Utils.splitCommaSeparatedString(replyTo);
220        }
221        return mReplyToAddresses;
222    }
223}
224