Home | History | Annotate | Download | only in mail
      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.phone.common.mail;
     17 
     18 import com.android.internal.annotations.VisibleForTesting;
     19 
     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     public enum RecipientType {
     30         TO, CC, BCC,
     31     }
     32 
     33     protected String mUid;
     34 
     35     private HashSet<String> mFlags = null;
     36 
     37     protected Date mInternalDate;
     38 
     39     public String getUid() {
     40         return mUid;
     41     }
     42 
     43     public void setUid(String uid) {
     44         this.mUid = uid;
     45     }
     46 
     47     public abstract String getSubject() throws MessagingException;
     48 
     49     public abstract void setSubject(String subject) throws MessagingException;
     50 
     51     public Date getInternalDate() {
     52         return mInternalDate;
     53     }
     54 
     55     public void setInternalDate(Date internalDate) {
     56         this.mInternalDate = internalDate;
     57     }
     58 
     59     public abstract Date getReceivedDate() throws MessagingException;
     60 
     61     public abstract Date getSentDate() throws MessagingException;
     62 
     63     public abstract void setSentDate(Date sentDate) throws MessagingException;
     64 
     65     public abstract Address[] getRecipients(String type) throws MessagingException;
     66 
     67     public abstract void setRecipients(String type, Address[] addresses)
     68             throws MessagingException;
     69 
     70     public void setRecipient(String type, Address address) throws MessagingException {
     71         setRecipients(type, new Address[] {
     72             address
     73         });
     74     }
     75 
     76     public abstract Address[] getFrom() throws MessagingException;
     77 
     78     public abstract void setFrom(Address from) throws MessagingException;
     79 
     80     public abstract Address[] getReplyTo() throws MessagingException;
     81 
     82     public abstract void setReplyTo(Address[] from) throws MessagingException;
     83 
     84     // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID");
     85     public abstract void setMessageId(String messageId) throws MessagingException;
     86     public abstract String getMessageId() throws MessagingException;
     87 
     88     @Override
     89     public boolean isMimeType(String mimeType) throws MessagingException {
     90         return getContentType().startsWith(mimeType);
     91     }
     92 
     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      */
    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.
    109      * Only used for testing.
    110      */
    111     @VisibleForTesting
    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 
    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      * @param flags
    127      * @param set
    128      */
    129     public void setFlags(String[] flags, boolean set) throws MessagingException {
    130         for (String flag : flags) {
    131             setFlag(flag, set);
    132         }
    133     }
    134 
    135     public boolean isSet(String flag) {
    136         return getFlagSet().contains(flag);
    137     }
    138 
    139     public abstract void saveChanges() throws MessagingException;
    140 
    141     @Override
    142     public String toString() {
    143         return getClass().getSimpleName() + ':' + mUid;
    144     }
    145 }
    146