• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.emailcommon.mail;
18 
19 import java.util.Date;
20 import java.util.HashSet;
21 
22 public 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 
getUid()37     public String getUid() {
38         return mUid;
39     }
40 
setUid(String uid)41     public void setUid(String uid) {
42         this.mUid = uid;
43     }
44 
getFolder()45     public Folder getFolder() {
46         return mFolder;
47     }
48 
getSubject()49     public abstract String getSubject() throws MessagingException;
50 
setSubject(String subject)51     public abstract void setSubject(String subject) throws MessagingException;
52 
getInternalDate()53     public Date getInternalDate() {
54         return mInternalDate;
55     }
56 
setInternalDate(Date internalDate)57     public void setInternalDate(Date internalDate) {
58         this.mInternalDate = internalDate;
59     }
60 
getReceivedDate()61     public abstract Date getReceivedDate() throws MessagingException;
62 
getSentDate()63     public abstract Date getSentDate() throws MessagingException;
64 
setSentDate(Date sentDate)65     public abstract void setSentDate(Date sentDate) throws MessagingException;
66 
getRecipients(RecipientType type)67     public abstract Address[] getRecipients(RecipientType type) throws MessagingException;
68 
setRecipients(RecipientType type, Address[] addresses)69     public abstract void setRecipients(RecipientType type, Address[] addresses)
70             throws MessagingException;
71 
setRecipient(RecipientType type, Address address)72     public void setRecipient(RecipientType type, Address address) throws MessagingException {
73         setRecipients(type, new Address[] {
74             address
75         });
76     }
77 
getFrom()78     public abstract Address[] getFrom() throws MessagingException;
79 
setFrom(Address from)80     public abstract void setFrom(Address from) throws MessagingException;
81 
getReplyTo()82     public abstract Address[] getReplyTo() throws MessagingException;
83 
setReplyTo(Address[] from)84     public abstract void setReplyTo(Address[] from) throws MessagingException;
85 
86     // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID");
setMessageId(String messageId)87     public abstract void setMessageId(String messageId) throws MessagingException;
getMessageId()88     public abstract String getMessageId() throws MessagingException;
89 
90     @Override
isMimeType(String mimeType)91     public boolean isMimeType(String mimeType) throws MessagingException {
92         return getContentType().startsWith(mimeType);
93     }
94 
getFlagSet()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      */
getFlags()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      */
setFlagDirectlyForTest(Flag flag, boolean set)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 
setFlag(Flag flag, boolean set)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      */
setFlags(Flag[] flags, boolean set)130     public void setFlags(Flag[] flags, boolean set) throws MessagingException {
131         for (Flag flag : flags) {
132             setFlag(flag, set);
133         }
134     }
135 
isSet(Flag flag)136     public boolean isSet(Flag flag) {
137         return getFlagSet().contains(flag);
138     }
139 
saveChanges()140     public abstract void saveChanges() throws MessagingException;
141 
142     @Override
toString()143     public String toString() {
144         return getClass().getSimpleName() + ':' + mUid;
145     }
146 }
147