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 getBody()86 public abstract Body getBody() throws MessagingException; 87 getContentType()88 public abstract String getContentType() throws MessagingException; 89 addHeader(String name, String value)90 public abstract void addHeader(String name, String value) throws MessagingException; 91 setHeader(String name, String value)92 public abstract void setHeader(String name, String value) throws MessagingException; 93 getHeader(String name)94 public abstract String[] getHeader(String name) throws MessagingException; 95 removeHeader(String name)96 public abstract void removeHeader(String name) throws MessagingException; 97 98 // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID"); setMessageId(String messageId)99 public abstract void setMessageId(String messageId) throws MessagingException; getMessageId()100 public abstract String getMessageId() throws MessagingException; 101 setBody(Body body)102 public abstract void setBody(Body body) throws MessagingException; 103 isMimeType(String mimeType)104 public boolean isMimeType(String mimeType) throws MessagingException { 105 return getContentType().startsWith(mimeType); 106 } 107 getFlagSet()108 private HashSet<Flag> getFlagSet() { 109 if (mFlags == null) { 110 mFlags = new HashSet<Flag>(); 111 } 112 return mFlags; 113 } 114 115 /* 116 * TODO Refactor Flags at some point to be able to store user defined flags. 117 */ getFlags()118 public Flag[] getFlags() { 119 return getFlagSet().toArray(new Flag[] {}); 120 } 121 122 /** 123 * Set/clear a flag directly, without involving overrides of {@link #setFlag} in subclasses. 124 * Only used for testing. 125 */ setFlagDirectlyForTest(Flag flag, boolean set)126 public final void setFlagDirectlyForTest(Flag flag, boolean set) throws MessagingException { 127 if (set) { 128 getFlagSet().add(flag); 129 } else { 130 getFlagSet().remove(flag); 131 } 132 } 133 setFlag(Flag flag, boolean set)134 public void setFlag(Flag flag, boolean set) throws MessagingException { 135 setFlagDirectlyForTest(flag, set); 136 } 137 138 /** 139 * This method calls setFlag(Flag, boolean) 140 * @param flags 141 * @param set 142 */ setFlags(Flag[] flags, boolean set)143 public void setFlags(Flag[] flags, boolean set) throws MessagingException { 144 for (Flag flag : flags) { 145 setFlag(flag, set); 146 } 147 } 148 isSet(Flag flag)149 public boolean isSet(Flag flag) { 150 return getFlagSet().contains(flag); 151 } 152 saveChanges()153 public abstract void saveChanges() throws MessagingException; 154 155 @Override toString()156 public String toString() { 157 return getClass().getSimpleName() + ':' + mUid; 158 } 159 } 160