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