blob: 864c6b1765e5aaffcf8302997f130226c5490319 [file] [log] [blame]
Nancy Chen7c07dfa2015-02-12 09:44:41 -08001/*
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
17package android.telecom;
18
19import android.net.Uri;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * Represents a single voicemail stored in the voicemail content provider.
25 */
26public class Voicemail implements Parcelable {
27 private final Long mTimestamp;
28 private final String mNumber;
29 private final Long mId;
30 private final Long mDuration;
31 private final String mSource;
32 private final String mProviderData;
33 private final Uri mUri;
34 private final Boolean mIsRead;
35 private final Boolean mHasContent;
36
37 private Voicemail(Long timestamp, String number, Long id, Long duration, String source,
38 String providerData, Uri uri, Boolean isRead, Boolean hasContent) {
39 mTimestamp = timestamp;
40 mNumber = number;
41 mId = id;
42 mDuration = duration;
43 mSource = source;
44 mProviderData = providerData;
45 mUri = uri;
46 mIsRead = isRead;
47 mHasContent = hasContent;
48 }
49
50 /**
51 * Create a {@link Builder} for a new {@link Voicemail} to be inserted.
52 * <p>
53 * The number and the timestamp are mandatory for insertion.
54 */
55 public static Builder createForInsertion(long timestamp, String number) {
56 return new Builder().setNumber(number).setTimestamp(timestamp);
57 }
58
59 /**
60 * Builder pattern for creating a {@link Voicemail}. The builder must be created with the
61 * {@link #createForInsertion(long, String)} method.
62 * <p>
63 * This class is <b>not thread safe</b>
64 */
65 public static class Builder {
66 private Long mBuilderTimestamp;
67 private String mBuilderNumber;
68 private Long mBuilderId;
69 private Long mBuilderDuration;
70 private String mBuilderSourcePackage;
71 private String mBuilderSourceData;
72 private Uri mBuilderUri;
73 private Boolean mBuilderIsRead;
74 private boolean mBuilderHasContent;
75
76 /** You should use the correct factory method to construct a builder. */
77 private Builder() {
78 }
79
80 public Builder setNumber(String number) {
81 mBuilderNumber = number;
82 return this;
83 }
84
85 public Builder setTimestamp(long timestamp) {
86 mBuilderTimestamp = timestamp;
87 return this;
88 }
89
90 public Builder setId(long id) {
91 mBuilderId = id;
92 return this;
93 }
94
95 public Builder setDuration(long duration) {
96 mBuilderDuration = duration;
97 return this;
98 }
99
100 public Builder setSourcePackage(String sourcePackage) {
101 mBuilderSourcePackage = sourcePackage;
102 return this;
103 }
104
105 public Builder setSourceData(String sourceData) {
106 mBuilderSourceData = sourceData;
107 return this;
108 }
109
110 public Builder setUri(Uri uri) {
111 mBuilderUri = uri;
112 return this;
113 }
114
115 public Builder setIsRead(boolean isRead) {
116 mBuilderIsRead = isRead;
117 return this;
118 }
119
120 public Builder setHasContent(boolean hasContent) {
121 mBuilderHasContent = hasContent;
122 return this;
123 }
124
125 public Voicemail build() {
126 mBuilderId = mBuilderId == null ? -1 : mBuilderId;
127 mBuilderTimestamp = mBuilderTimestamp == null ? 0 : mBuilderTimestamp;
128 mBuilderDuration = mBuilderDuration == null ? 0: mBuilderDuration;
129 mBuilderIsRead = mBuilderIsRead == null ? false : mBuilderIsRead;
130 return new Voicemail(mBuilderTimestamp, mBuilderNumber, mBuilderId, mBuilderDuration,
131 mBuilderSourcePackage, mBuilderSourceData, mBuilderUri, mBuilderIsRead,
132 mBuilderHasContent);
133 }
134 }
135
136 /**
137 * The identifier of the voicemail in the content provider.
138 * <p>
139 * This may be missing in the case of a new {@link Voicemail} that we plan to insert into the
140 * content provider, since until it has been inserted we don't know what id it should have. If
141 * none is specified, we return -1.
142 */
143 public long getId() {
144 return mId;
145 }
146
147 /** The number of the person leaving the voicemail, empty string if unknown, null if not set. */
148 public String getNumber() {
149 return mNumber;
150 }
151
152 /** The timestamp the voicemail was received, in millis since the epoch, zero if not set. */
153 public long getTimestampMillis() {
154 return mTimestamp;
155 }
156
157 /** Gets the duration of the voicemail in millis, or zero if the field is not set. */
158 public long getDuration() {
159 return mDuration;
160 }
161
162 /**
163 * Returns the package name of the source that added this voicemail, or null if this field is
164 * not set.
165 */
166 public String getSourcePackage() {
167 return mSource;
168 }
169
170 /**
171 * Returns the application-specific data type stored with the voicemail, or null if this field
172 * is not set.
173 * <p>
174 * Source data is typically used as an identifier to uniquely identify the voicemail against
175 * the voicemail server. This is likely to be something like the IMAP UID, or some other
176 * server-generated identifying string.
177 */
178 public String getSourceData() {
179 return mProviderData;
180 }
181
182 /**
183 * Gets the Uri that can be used to refer to this voicemail, and to make it play.
184 * <p>
185 * Returns null if we don't know the Uri.
186 */
187 public Uri getUri() {
188 return mUri;
189 }
190
191 /**
192 * Tells us if the voicemail message has been marked as read.
193 * <p>
194 * Always returns false if this field has not been set, i.e. if hasRead() returns false.
195 */
196 public boolean isRead() {
197 return mIsRead;
198 }
199
200 /**
201 * Tells us if there is content stored at the Uri.
202 */
203 public boolean hasContent() {
204 return mHasContent;
205 }
206
207 @Override
208 public int describeContents() {
209 return 0;
210 }
211
212 @Override
213 public void writeToParcel(Parcel dest, int flags) {
214 dest.writeLong(mTimestamp);
215 dest.writeCharSequence(mNumber);
216 dest.writeLong(mId);
217 dest.writeLong(mDuration);
218 dest.writeCharSequence(mSource);
219 dest.writeCharSequence(mProviderData);
220 if (mUri == null) {
221 dest.writeInt(0);
222 } else {
223 dest.writeInt(1);
224 mUri.writeToParcel(dest, flags);
225 }
226 if (mIsRead) {
227 dest.writeInt(1);
228 } else {
229 dest.writeInt(0);
230 }
231 if (mHasContent) {
232 dest.writeInt(1);
233 } else {
234 dest.writeInt(0);
235 }
236 }
237
238 public static final Creator<Voicemail> CREATOR
239 = new Creator<Voicemail>() {
240 @Override
241 public Voicemail createFromParcel(Parcel in) {
242 return new Voicemail(in);
243 }
244
245 @Override
246 public Voicemail[] newArray(int size) {
247 return new Voicemail[size];
248 }
249 };
250
251 private Voicemail(Parcel in) {
252 mTimestamp = in.readLong();
253 mNumber = (String) in.readCharSequence();
254 mId = in.readLong();
255 mDuration = in.readLong();
256 mSource = (String) in.readCharSequence();
257 mProviderData = (String) in.readCharSequence();
258 if (in.readInt() > 0) {
259 mUri = Uri.CREATOR.createFromParcel(in);
260 } else {
261 mUri = null;
262 }
263 mIsRead = in.readInt() > 0 ? true : false;
264 mHasContent = in.readInt() > 0 ? true : false;
265 }
266}