blob: 97af06c1d64c00e7c9ae93d1491873b13bf756d7 [file] [log] [blame]
Tyler Gunnd081f042018-12-04 12:56:45 -08001/*
2 * Copyright (C) 2018 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.annotation.IntDef;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.content.pm.ApplicationInfo;
23import android.graphics.drawable.Icon;
24import android.os.Parcel;
25import android.os.Parcelable;
26
27import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29import java.util.Objects;
30
31/**
32 * Encapsulates information about an incoming or outgoing {@link Call} provided by a
33 * {@link CallScreeningService}.
34 * <p>
35 * Call identified information is consumed by the {@link InCallService dialer} app to provide the
36 * user with more information about a call. This can include information such as the name of the
37 * caller, address, etc. Call identification information is persisted to the
38 * {@link android.provider.CallLog}.
39 */
40public final class CallIdentification implements Parcelable {
41 /**
42 * Builder for {@link CallIdentification} instances.
43 * <p>
44 * A {@link CallScreeningService} uses this class to create new instances of
45 * {@link CallIdentification} for a screened call.
46 */
47 public static class Builder {
48 private String mName;
49 private String mDescription;
50 private String mDetails;
51 private Icon mPhoto;
52 private int mNuisanceConfidence = CallIdentification.CONFIDENCE_UNKNOWN;
53 private String mPackageName;
54 private String mAppName;
55
56 /**
57 * Default builder constructor.
58 */
59 public Builder() {
60 // Default constructor
61 }
62
63 /**
64 * Create instance of call identification with specified package/app name.
65 *
66 * @param callIdPackageName The package name.
67 * @param callIdAppName The app name.
68 * @hide
69 */
70 public Builder(String callIdPackageName, String callIdAppName) {
71 mPackageName = callIdPackageName;
72 mAppName = callIdAppName;
73 }
74
75 /**
76 * Sets the name associated with the {@link CallIdentification} being built.
77 * <p>
78 * Could be a business name, for example.
79 *
80 * @param name The name associated with the call, or {@code null} if none is provided.
81 * @return Builder instance.
82 */
83 public Builder setName(@Nullable String name) {
84 mName = name;
85 return this;
86 }
87
88 /**
89 * Sets the description associated with the {@link CallIdentification} being built.
90 * <p>
91 * A description of the call as identified by a {@link CallScreeningService}. The
92 * description is typically presented by Dialer apps after the
93 * {@link CallIdentification#getName() name} to provide a short piece of relevant
94 * information about the call. This could include a location, address, or a message
95 * regarding the potential nature of the call (e.g. potential telemarketer).
96 *
97 * @param description The call description, or {@code null} if none is provided.
98 * @return Builder instance.
99 */
100 public Builder setDescription(@Nullable String description) {
101 mDescription = description;
102 return this;
103 }
104
105 /**
106 * Sets the details associated with the {@link CallIdentification} being built.
107 * <p>
108 * The details is typically presented by Dialer apps after the
109 * {@link CallIdentification#getName() name} and
110 * {@link CallIdentification#getDescription() description} to provide further clarifying
111 * information about the call. This could include, for example, the opening hours of a
112 * business, or a stats about the number of times a call has been reported as spam.
113 *
114 * @param details The call details, or {@code null} if none is provided.
115 * @return Builder instance.
116 */
117 public Builder setDetails(@Nullable String details) {
118 mDetails = details;
119 return this;
120 }
121
122 /**
123 * Sets the photo associated with the {@link CallIdentification} being built.
124 * <p>
125 * This could be, for example, a business logo, or a photo of the caller.
126 *
127 * @param photo The photo associated with the call, or {@code null} if none was provided.
128 * @return Builder instance.
129 */
130 public Builder setPhoto(@Nullable Icon photo) {
131 mPhoto = photo;
132 return this;
133 }
134
135 /**
136 * Sets the nuisance confidence with the {@link CallIdentification} being built.
137 * <p>
138 * This can be used to specify how confident the {@link CallScreeningService} is that a call
139 * is or is not a nuisance call.
140 *
141 * @param nuisanceConfidence The nuisance confidence.
142 * @return The builder.
143 */
144 public Builder setNuisanceConfidence(@NuisanceConfidence int nuisanceConfidence) {
145 mNuisanceConfidence = nuisanceConfidence;
146 return this;
147 }
148
149 /**
150 * Creates a new instance of {@link CallIdentification} based on the parameters set in this
151 * builder.
152 *
153 * @return {@link CallIdentification} instance.
154 */
155 public CallIdentification build() {
156 return new CallIdentification(mName, mDescription, mDetails, mPhoto,
157 mNuisanceConfidence, mPackageName, mAppName);
158 }
159 }
160
161 /** @hide */
162 @Retention(RetentionPolicy.SOURCE)
163 @IntDef(
164 prefix = { "CONFIDENCE_" },
165 value = {CONFIDENCE_NUISANCE, CONFIDENCE_LIKELY_NUISANCE, CONFIDENCE_UNKNOWN,
166 CONFIDENCE_LIKELY_NOT_NUISANCE, CONFIDENCE_NOT_NUISANCE})
167 public @interface NuisanceConfidence {}
168
169 /**
170 * Call has been identified as a nuisance call.
171 * <p>
172 * Returned from {@link #getNuisanceConfidence()} to indicate that a
173 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
174 * nuisance call.
175 */
176 public static final int CONFIDENCE_NUISANCE = 2;
177
178 /**
179 * Call has been identified as a likely nuisance call.
180 * <p>
181 * Returned from {@link #getNuisanceConfidence()} to indicate that a
182 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
183 * nuisance call.
184 */
185 public static final int CONFIDENCE_LIKELY_NUISANCE = 1;
186
187 /**
188 * Call could not be classified as nuisance or non-nuisance.
189 * <p>
190 * Returned from {@link #getNuisanceConfidence()} to indicate that a
191 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
192 * nuisance call.
193 */
194 public static final int CONFIDENCE_UNKNOWN = 0;
195
196 /**
197 * Call has been identified as not likely to be a nuisance call.
198 * <p>
199 * Returned from {@link #getNuisanceConfidence()} to indicate that a
200 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
201 * nuisance call.
202 */
203 public static final int CONFIDENCE_LIKELY_NOT_NUISANCE = -1;
204
205 /**
206 * Call has been identified as not a nuisance call.
207 * <p>
208 * Returned from {@link #getNuisanceConfidence()} to indicate that a
209 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
210 * nuisance call.
211 */
212 public static final int CONFIDENCE_NOT_NUISANCE = -2;
213
214 /**
215 * Default constructor for {@link CallIdentification}.
216 *
217 * @param name The name.
218 * @param description The description.
219 * @param details The details.
220 * @param photo The photo.
221 * @param nuisanceConfidence Confidence that this is a nuisance call.
222 * @hide
223 */
224 private CallIdentification(@Nullable String name, @Nullable String description,
225 @Nullable String details, @Nullable Icon photo,
226 @NuisanceConfidence int nuisanceConfidence) {
227 this(name, description, details, photo, nuisanceConfidence, null, null);
228 }
229
230 /**
231 * Default constructor for {@link CallIdentification}.
232 *
233 * @param name The name.
234 * @param description The description.
235 * @param details The details.
236 * @param photo The photo.
237 * @param nuisanceConfidence Confidence that this is a nuisance call.
238 * @param callScreeningPackageName Package name of the {@link CallScreeningService} which
239 * provided the call identification.
240 * @param callScreeningAppName App name of the {@link CallScreeningService} which provided the
241 * call identification.
242 * @hide
243 */
244 private CallIdentification(@Nullable String name, @Nullable String description,
245 @Nullable String details, @Nullable Icon photo,
246 @NuisanceConfidence int nuisanceConfidence, @NonNull String callScreeningPackageName,
247 @NonNull String callScreeningAppName) {
248 mName = name;
249 mDescription = description;
250 mDetails = details;
251 mPhoto = photo;
252 mNuisanceConfidence = nuisanceConfidence;
253 mCallScreeningAppName = callScreeningPackageName;
254 mCallScreeningPackageName = callScreeningAppName;
255 }
256
257 private String mName;
258 private String mDescription;
259 private String mDetails;
260 private Icon mPhoto;
261 private int mNuisanceConfidence;
262 private String mCallScreeningPackageName;
263 private String mCallScreeningAppName;
264
265 @Override
266 public int describeContents() {
267 return 0;
268 }
269
270 @Override
271 public void writeToParcel(Parcel parcel, int i) {
272 parcel.writeString(mName);
273 parcel.writeString(mDescription);
274 parcel.writeString(mDetails);
275 parcel.writeParcelable(mPhoto, 0);
276 parcel.writeInt(mNuisanceConfidence);
277 parcel.writeString(mCallScreeningPackageName);
278 parcel.writeString(mCallScreeningAppName);
279 }
280
281 /**
282 * Responsible for creating CallIdentification objects for deserialized Parcels.
283 */
284 public static final Parcelable.Creator<CallIdentification> CREATOR =
285 new Parcelable.Creator<CallIdentification> () {
286
287 @Override
288 public CallIdentification createFromParcel(Parcel source) {
289 String name = source.readString();
290 String description = source.readString();
291 String details = source.readString();
292 Icon photo = source.readParcelable(ClassLoader.getSystemClassLoader());
293 int nuisanceConfidence = source.readInt();
294 String callScreeningPackageName = source.readString();
295 String callScreeningAppName = source.readString();
296 return new CallIdentification(name, description, details, photo,
297 nuisanceConfidence, callScreeningPackageName, callScreeningAppName);
298 }
299
300 @Override
301 public CallIdentification[] newArray(int size) {
302 return new CallIdentification[size];
303 }
304 };
305
306 /**
307 * The name associated with the number.
308 * <p>
309 * The name of the call as identified by a {@link CallScreeningService}. Could be a business
310 * name, for example.
311 *
312 * @return The name associated with the number, or {@code null} if none was provided.
313 */
314 public final @Nullable String getName() {
315 return mName;
316 }
317
318 /**
319 * Description of the call.
320 * <p>
321 * A description of the call as identified by a {@link CallScreeningService}. The description
322 * is typically presented by Dialer apps after the {@link #getName() name} to provide a short
323 * piece of relevant information about the call. This could include a location, address, or a
324 * message regarding the potential nature of the call (e.g. potential telemarketer).
325 *
326 * @return The call description, or {@code null} if none was provided.
327 */
328 public final @Nullable String getDescription() {
329 return mDescription;
330 }
331
332 /**
333 * Details of the call.
334 * <p>
335 * Details of the call as identified by a {@link CallScreeningService}. The details
336 * are typically presented by Dialer apps after the {@link #getName() name} and
337 * {@link #getDescription() description} to provide further clarifying information about the
338 * call. This could include, for example, the opening hours of a business, or stats about
339 * the number of times a call has been reported as spam.
340 *
341 * @return The call details, or {@code null} if none was provided.
342 */
343 public final @Nullable String getDetails() {
344 return mDetails;
345 }
346
347 /**
348 * Photo associated with the call.
349 * <p>
350 * A photo associated with the call as identified by a {@link CallScreeningService}. This
351 * could be, for example, a business logo, or a photo of the caller.
352 *
353 * @return The photo associated with the call, or {@code null} if none was provided.
354 */
355 public final @Nullable Icon getPhoto() {
356 return mPhoto;
357 }
358
359 /**
360 * Indicates the likelihood that this call is a nuisance call.
361 * <p>
362 * How likely the call is a nuisance call, as identified by a {@link CallScreeningService}.
363 *
364 * @return The nuisance confidence.
365 */
366 public final @NuisanceConfidence
367 int getNuisanceConfidence() {
368 return mNuisanceConfidence;
369 }
370
371 /**
372 * The package name of the {@link CallScreeningService} which provided the
373 * {@link CallIdentification}.
374 * <p>
375 * A {@link CallScreeningService} may not set this property; it is set by the system.
376 * @return the package name
377 */
378 public final @NonNull String getCallScreeningPackageName() {
379 return mCallScreeningPackageName;
380 }
381
382 /**
383 * The {@link android.content.pm.PackageManager#getApplicationLabel(ApplicationInfo) name} of
384 * the {@link CallScreeningService} which provided the {@link CallIdentification}.
385 * <p>
386 * A {@link CallScreeningService} may not set this property; it is set by the system.
387 *
388 * @return The name of the app.
389 */
390 public final @NonNull String getCallScreeningAppName() {
391 return mCallScreeningAppName;
392 }
393
394 /**
395 * Set the package name of the {@link CallScreeningService} which provided this information.
396 *
397 * @param callScreeningPackageName The package name.
398 * @hide
399 */
400 public void setCallScreeningPackageName(@NonNull String callScreeningPackageName) {
401 mCallScreeningPackageName = callScreeningPackageName;
402 }
403
404 /**
405 * Set the app name of the {@link CallScreeningService} which provided this information.
406 *
407 * @param callScreeningAppName The app name.
408 * @hide
409 */
410 public void setCallScreeningAppName(@NonNull String callScreeningAppName) {
411 mCallScreeningAppName = callScreeningAppName;
412 }
413
414 @Override
415 public boolean equals(Object o) {
416 if (this == o) return true;
417 if (o == null || getClass() != o.getClass()) return false;
418 CallIdentification that = (CallIdentification) o;
419 // Note: mPhoto purposely omit as no good comparison exists.
420 return mNuisanceConfidence == that.mNuisanceConfidence
421 && Objects.equals(mName, that.mName)
422 && Objects.equals(mDescription, that.mDescription)
423 && Objects.equals(mDetails, that.mDetails)
424 && Objects.equals(mCallScreeningAppName, that.mCallScreeningAppName)
425 && Objects.equals(mCallScreeningPackageName, that.mCallScreeningPackageName);
426 }
427
428 @Override
429 public int hashCode() {
430 return Objects.hash(mName, mDescription, mDetails, mPhoto, mNuisanceConfidence,
431 mCallScreeningAppName, mCallScreeningPackageName);
432 }
433}