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