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