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