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