summaryrefslogtreecommitdiffstats
path: root/java/com/android/voicemail/impl/mail/Message.java
blob: aea5d3eadaeb7fad8188a12669d88e57413fc5de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.voicemail.impl.mail;

import android.support.annotation.VisibleForTesting;
import java.util.Date;
import java.util.HashSet;

public abstract class Message implements Part, Body {
  public static final Message[] EMPTY_ARRAY = new Message[0];

  public static final String RECIPIENT_TYPE_TO = "to";
  public static final String RECIPIENT_TYPE_CC = "cc";
  public static final String RECIPIENT_TYPE_BCC = "bcc";

  public enum RecipientType {
    TO,
    CC,
    BCC,
  }

  protected String mUid;

  private HashSet<String> mFlags = null;

  protected Date mInternalDate;

  public String getUid() {
    return mUid;
  }

  public void setUid(String uid) {
    this.mUid = uid;
  }

  public abstract String getSubject() throws MessagingException;

  public abstract void setSubject(String subject) throws MessagingException;

  public Date getInternalDate() {
    return mInternalDate;
  }

  public void setInternalDate(Date internalDate) {
    this.mInternalDate = internalDate;
  }

  public abstract Date getReceivedDate() throws MessagingException;

  public abstract Date getSentDate() throws MessagingException;

  public abstract void setSentDate(Date sentDate) throws MessagingException;

  public abstract Address[] getRecipients(String type) throws MessagingException;

  public abstract void setRecipients(String type, Address[] addresses) throws MessagingException;

  public void setRecipient(String type, Address address) throws MessagingException {
    setRecipients(type, new Address[] {address});
  }

  public abstract Address[] getFrom() throws MessagingException;

  public abstract void setFrom(Address from) throws MessagingException;

  public abstract Address[] getReplyTo() throws MessagingException;

  public abstract void setReplyTo(Address[] from) throws MessagingException;

  // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID");
  public abstract void setMessageId(String messageId) throws MessagingException;

  public abstract String getMessageId() throws MessagingException;

  @Override
  public boolean isMimeType(String mimeType) throws MessagingException {
    return getContentType().startsWith(mimeType);
  }

  private HashSet<String> getFlagSet() {
    if (mFlags == null) {
      mFlags = new HashSet<String>();
    }
    return mFlags;
  }

  /*
   * TODO Refactor Flags at some point to be able to store user defined flags.
   */
  public String[] getFlags() {
    return getFlagSet().toArray(new String[] {});
  }

  /**
   * Set/clear a flag directly, without involving overrides of {@link #setFlag} in subclasses. Only
   * used for testing.
   */
  @VisibleForTesting
  private final void setFlagDirectlyForTest(String flag, boolean set) throws MessagingException {
    if (set) {
      getFlagSet().add(flag);
    } else {
      getFlagSet().remove(flag);
    }
  }

  public void setFlag(String flag, boolean set) throws MessagingException {
    setFlagDirectlyForTest(flag, set);
  }

  /**
   * This method calls setFlag(String, boolean)
   *
   * @param flags
   * @param set
   */
  public void setFlags(String[] flags, boolean set) throws MessagingException {
    for (String flag : flags) {
      setFlag(flag, set);
    }
  }

  public boolean isSet(String flag) {
    return getFlagSet().contains(flag);
  }

  public abstract void saveChanges() throws MessagingException;

  @Override
  public String toString() {
    return getClass().getSimpleName() + ':' + mUid;
  }
}