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