• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.VisibleForTesting;
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 static final String RECIPIENT_TYPE_TO = "to";
26   public static final String RECIPIENT_TYPE_CC = "cc";
27   public static final String RECIPIENT_TYPE_BCC = "bcc";
28 
29   public enum RecipientType {
30     TO,
31     CC,
32     BCC,
33   }
34 
35   protected String mUid;
36 
37   private HashSet<String> mFlags = null;
38 
39   protected Date mInternalDate;
40 
getUid()41   public String getUid() {
42     return mUid;
43   }
44 
setUid(String uid)45   public void setUid(String uid) {
46     this.mUid = uid;
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(String type)67   public abstract Address[] getRecipients(String type) throws MessagingException;
68 
setRecipients(String type, Address[] addresses)69   public abstract void setRecipients(String type, Address[] addresses) throws MessagingException;
70 
setRecipient(String type, Address address)71   public void setRecipient(String type, Address address) throws MessagingException {
72     setRecipients(type, new Address[] {address});
73   }
74 
getFrom()75   public abstract Address[] getFrom() throws MessagingException;
76 
setFrom(Address from)77   public abstract void setFrom(Address from) throws MessagingException;
78 
getReplyTo()79   public abstract Address[] getReplyTo() throws MessagingException;
80 
setReplyTo(Address[] from)81   public abstract void setReplyTo(Address[] from) throws MessagingException;
82 
83   // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID");
setMessageId(String messageId)84   public abstract void setMessageId(String messageId) throws MessagingException;
85 
getMessageId()86   public abstract String getMessageId() throws MessagingException;
87 
88   @Override
isMimeType(String mimeType)89   public boolean isMimeType(String mimeType) throws MessagingException {
90     return getContentType().startsWith(mimeType);
91   }
92 
getFlagSet()93   private HashSet<String> getFlagSet() {
94     if (mFlags == null) {
95       mFlags = new HashSet<String>();
96     }
97     return mFlags;
98   }
99 
100   /*
101    * TODO Refactor Flags at some point to be able to store user defined flags.
102    */
getFlags()103   public String[] getFlags() {
104     return getFlagSet().toArray(new String[] {});
105   }
106 
107   /**
108    * Set/clear a flag directly, without involving overrides of {@link #setFlag} in subclasses. Only
109    * used for testing.
110    */
111   @VisibleForTesting
setFlagDirectlyForTest(String flag, boolean set)112   private final void setFlagDirectlyForTest(String flag, boolean set) throws MessagingException {
113     if (set) {
114       getFlagSet().add(flag);
115     } else {
116       getFlagSet().remove(flag);
117     }
118   }
119 
setFlag(String flag, boolean set)120   public void setFlag(String flag, boolean set) throws MessagingException {
121     setFlagDirectlyForTest(flag, set);
122   }
123 
124   /**
125    * This method calls setFlag(String, boolean)
126    *
127    * @param flags
128    * @param set
129    */
setFlags(String[] flags, boolean set)130   public void setFlags(String[] flags, boolean set) throws MessagingException {
131     for (String flag : flags) {
132       setFlag(flag, set);
133     }
134   }
135 
isSet(String flag)136   public boolean isSet(String 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