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