Message.java revision aa27bc0e1c3bb6be4609b00007637a9d3e960f5e
1/*
2 * Copyright (C) 2008 The Android Open Source Project
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.emailcommon.mail;
18
19import java.util.Date;
20import java.util.HashSet;
21
22public abstract class Message implements Part, Body {
23    public static final Message[] EMPTY_ARRAY = new Message[0];
24
25    public enum RecipientType {
26        TO, CC, BCC,
27    }
28
29    protected String mUid;
30
31    private HashSet<Flag> mFlags = null;
32
33    protected Date mInternalDate;
34
35    protected Folder mFolder;
36
37    public String getUid() {
38        return mUid;
39    }
40
41    public void setUid(String uid) {
42        this.mUid = uid;
43    }
44
45    public Folder getFolder() {
46        return mFolder;
47    }
48
49    public abstract String getSubject() throws MessagingException;
50
51    public abstract void setSubject(String subject) throws MessagingException;
52
53    public Date getInternalDate() {
54        return mInternalDate;
55    }
56
57    public void setInternalDate(Date internalDate) {
58        this.mInternalDate = internalDate;
59    }
60
61    public abstract Date getReceivedDate() throws MessagingException;
62
63    public abstract Date getSentDate() throws MessagingException;
64
65    public abstract void setSentDate(Date sentDate) throws MessagingException;
66
67    public abstract Address[] getRecipients(RecipientType type) throws MessagingException;
68
69    public abstract void setRecipients(RecipientType type, Address[] addresses)
70            throws MessagingException;
71
72    public void setRecipient(RecipientType type, Address address) throws MessagingException {
73        setRecipients(type, new Address[] {
74            address
75        });
76    }
77
78    public abstract Address[] getFrom() throws MessagingException;
79
80    public abstract void setFrom(Address from) throws MessagingException;
81
82    public abstract Address[] getReplyTo() throws MessagingException;
83
84    public abstract void setReplyTo(Address[] from) throws MessagingException;
85
86    // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID");
87    public abstract void setMessageId(String messageId) throws MessagingException;
88    public abstract String getMessageId() throws MessagingException;
89
90    @Override
91    public boolean isMimeType(String mimeType) throws MessagingException {
92        return getContentType().startsWith(mimeType);
93    }
94
95    private HashSet<Flag> getFlagSet() {
96        if (mFlags == null) {
97            mFlags = new HashSet<Flag>();
98        }
99        return mFlags;
100    }
101
102    /*
103     * TODO Refactor Flags at some point to be able to store user defined flags.
104     */
105    public Flag[] getFlags() {
106        return getFlagSet().toArray(new Flag[] {});
107    }
108
109    /**
110     * Set/clear a flag directly, without involving overrides of {@link #setFlag} in subclasses.
111     * Only used for testing.
112     */
113    public final void setFlagDirectlyForTest(Flag flag, boolean set) throws MessagingException {
114        if (set) {
115            getFlagSet().add(flag);
116        } else {
117            getFlagSet().remove(flag);
118        }
119    }
120
121    public void setFlag(Flag flag, boolean set) throws MessagingException {
122        setFlagDirectlyForTest(flag, set);
123    }
124
125    /**
126     * This method calls setFlag(Flag, boolean)
127     * @param flags
128     * @param set
129     */
130    public void setFlags(Flag[] flags, boolean set) throws MessagingException {
131        for (Flag flag : flags) {
132            setFlag(flag, set);
133        }
134    }
135
136    public boolean isSet(Flag flag) {
137        return getFlagSet().contains(flag);
138    }
139
140    public abstract void saveChanges() throws MessagingException;
141
142    @Override
143    public String toString() {
144        return getClass().getSimpleName() + ':' + mUid;
145    }
146}
147