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