1 /*
2  * Copyright (C) 2011 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 
17 package com.example.android.voicemail.common.core;
18 
19 import android.net.Uri;
20 
21 /**
22  * A simple immutable data object to represent a voicemail.
23  */
24 public final class VoicemailImpl implements Voicemail {
25     private final Long mTimestamp;
26     private final String mNumber;
27     private final Long mId;
28     private final Long mDuration;
29     private final String mSource;
30     private final String mProviderData;
31     private final Uri mUri;
32     private final Boolean mIsRead;
33     private final boolean mHasContent;
34 
35     // TODO: 5. We should probably consider changing "number" everywhere to "contact", given that
36     // it's not clear that these will be restricted to telephone numbers.
37 
VoicemailImpl( Long timestamp, String number, Long id, Long duration, String source, String providerData, Uri uri, Boolean isRead, boolean hasContent)38     private VoicemailImpl(
39             Long timestamp,
40             String number,
41             Long id,
42             Long duration,
43             String source,
44             String providerData,
45             Uri uri,
46             Boolean isRead,
47             boolean hasContent) {
48         mId = id;
49         mNumber = number;
50         mDuration = duration;
51         mTimestamp = timestamp;
52         mSource = source;
53         mProviderData = providerData;
54         mUri = uri;
55         mIsRead = isRead;
56         mHasContent = hasContent;
57     }
58 
59     /**
60      * Create a {@link Builder} for a new {@link Voicemail} to be inserted.
61      * <p>
62      * The number and the timestamp are mandatory for insertion.
63      */
createForInsertion(long timestamp, String number)64     public static Builder createForInsertion(long timestamp, String number) {
65         return new Builder().setNumber(number).setTimestamp(timestamp);
66     }
67 
68     /**
69      * Create a {@link Builder} for updating a {@link Voicemail}.
70      * <p>
71      * Only the id of the voicemail to be updated is mandatory.
72      */
createForUpdate(long id)73     public static Builder createForUpdate(long id) {
74         return new Builder().setId(id);
75     }
76 
77     /**
78      * Create a {@link Builder} for a new {@link Voicemail}, such as one suitable for returning from
79      * a list of results or creating from scratch.
80      */
createEmptyBuilder()81     public static Builder createEmptyBuilder() {
82         return new Builder();
83     }
84 
85     /**
86      * Builder pattern for creating a {@link VoicemailImpl}.
87      * <p>
88      * All fields are optional, and can be set with the various {@code setXXX} methods.
89      * <p>
90      * This class is <b>not thread safe</b>
91      */
92     public static class Builder {
93         private Long mBuilderTimestamp;
94         private String mBuilderNumber;
95         private Long mBuilderId;
96         private Long mBuilderDuration;
97         private String mBuilderSourcePackage;
98         private String mBuilderSourceData;
99         private Uri mBuilderUri;
100         private Boolean mBuilderIsRead;
101         private boolean mBuilderHasContent;
102 
103         /** You should use the correct factory method to construct a builder. */
Builder()104         private Builder() {
105         }
106 
setNumber(String number)107         public Builder setNumber(String number) {
108             mBuilderNumber = number;
109             return this;
110         }
111 
setTimestamp(long timestamp)112         public Builder setTimestamp(long timestamp) {
113             mBuilderTimestamp = timestamp;
114             return this;
115         }
116 
setId(long id)117         public Builder setId(long id) {
118             mBuilderId = id;
119             return this;
120         }
121 
setDuration(long duration)122         public Builder setDuration(long duration) {
123             mBuilderDuration = duration;
124             return this;
125         }
126 
setSourcePackage(String sourcePackage)127         public Builder setSourcePackage(String sourcePackage) {
128             mBuilderSourcePackage = sourcePackage;
129             return this;
130         }
131 
setSourceData(String sourceData)132         public Builder setSourceData(String sourceData) {
133             mBuilderSourceData = sourceData;
134             return this;
135         }
136 
setUri(Uri uri)137         public Builder setUri(Uri uri) {
138             mBuilderUri = uri;
139             return this;
140         }
141 
setIsRead(boolean isRead)142         public Builder setIsRead(boolean isRead) {
143             mBuilderIsRead = isRead;
144             return this;
145         }
146 
setHasContent(boolean hasContent)147         public Builder setHasContent(boolean hasContent) {
148             mBuilderHasContent = hasContent;
149             return this;
150         }
151 
build()152         public VoicemailImpl build() {
153             return new VoicemailImpl(mBuilderTimestamp, mBuilderNumber, mBuilderId,
154                     mBuilderDuration,
155                     mBuilderSourcePackage, mBuilderSourceData, mBuilderUri,
156                     mBuilderIsRead,
157                     mBuilderHasContent);
158         }
159     }
160 
161     @Override
getId()162     public long getId() {
163         return hasId() ? mId : -1;
164     }
165 
166     @Override
hasId()167     public boolean hasId() {
168         return mId != null;
169     }
170 
171     @Override
getNumber()172     public String getNumber() {
173         return mNumber;
174     }
175 
176     @Override
hasNumber()177     public boolean hasNumber() {
178         return mNumber != null;
179     }
180 
181     @Override
getTimestampMillis()182     public long getTimestampMillis() {
183         return hasTimestampMillis() ? mTimestamp : 0;
184     }
185 
186     @Override
hasTimestampMillis()187     public boolean hasTimestampMillis() {
188         return mTimestamp != null;
189     }
190 
191     @Override
getDuration()192     public long getDuration() {
193         return hasDuration() ? mDuration : 0;
194     }
195 
196     @Override
hasDuration()197     public boolean hasDuration() {
198         return mDuration != null;
199     }
200 
201     @Override
getSourcePackage()202     public String getSourcePackage() {
203         return mSource;
204     }
205 
206     @Override
hasSourcePackage()207     public boolean hasSourcePackage() {
208         return mSource != null;
209     }
210 
211     @Override
getSourceData()212     public String getSourceData() {
213         return mProviderData;
214     }
215 
216     @Override
hasSourceData()217     public boolean hasSourceData() {
218         return mProviderData != null;
219     }
220 
221     @Override
getUri()222     public Uri getUri() {
223         return mUri;
224     }
225 
226     @Override
hasUri()227     public boolean hasUri() {
228         return mUri != null;
229     }
230 
231     @Override
isRead()232     public boolean isRead() {
233         return hasRead() ? mIsRead : false;
234     }
235 
236     @Override
hasRead()237     public boolean hasRead() {
238         return mIsRead != null;
239     }
240 
241     @Override
hasContent()242     public boolean hasContent() {
243         return mHasContent;
244     }
245 
246     @Override
toString()247     public String toString() {
248         return "VoicemailImpl [mTimestamp=" + mTimestamp + ", mNumber=" + mNumber + ", mId=" + mId
249                 + ", mDuration=" + mDuration + ", mSource=" + mSource + ", mProviderData="
250                 + mProviderData + ", mUri=" + mUri + ", mIsRead=" + mIsRead + ", mHasContent="
251                 + mHasContent + "]";
252     }
253 }
254