blob: 745affd6b17d7fa9090610df989d569ccdafc181 [file] [log] [blame]
Tyler Gunn7e45b722018-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 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +000047 public final static class Builder {
Tyler Gunnb4830552019-02-15 16:04:13 -080048 private CharSequence mName;
49 private CharSequence mDescription;
50 private CharSequence mDetails;
Tyler Gunn7e45b722018-12-04 12:56:45 -080051 private Icon mPhoto;
52 private int mNuisanceConfidence = CallIdentification.CONFIDENCE_UNKNOWN;
53 private String mPackageName;
Tyler Gunnb4830552019-02-15 16:04:13 -080054 private CharSequence mAppName;
Tyler Gunn7e45b722018-12-04 12:56:45 -080055
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 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +000070 public Builder(@NonNull String callIdPackageName, @NonNull CharSequence callIdAppName) {
Tyler Gunn7e45b722018-12-04 12:56:45 -080071 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 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +000083 public @NonNull Builder setName(@Nullable CharSequence name) {
Tyler Gunn7e45b722018-12-04 12:56:45 -080084 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 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +0000100 public @NonNull Builder setDescription(@Nullable CharSequence description) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800101 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 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +0000117
118 public @NonNull Builder setDetails(@Nullable CharSequence details) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800119 mDetails = details;
120 return this;
121 }
122
123 /**
124 * Sets the photo associated with the {@link CallIdentification} being built.
125 * <p>
126 * This could be, for example, a business logo, or a photo of the caller.
127 *
128 * @param photo The photo associated with the call, or {@code null} if none was provided.
129 * @return Builder instance.
130 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +0000131 public @NonNull Builder setPhoto(@Nullable Icon photo) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800132 mPhoto = photo;
133 return this;
134 }
135
136 /**
137 * Sets the nuisance confidence with the {@link CallIdentification} being built.
138 * <p>
139 * This can be used to specify how confident the {@link CallScreeningService} is that a call
140 * is or is not a nuisance call.
141 *
142 * @param nuisanceConfidence The nuisance confidence.
143 * @return The builder.
144 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +0000145 public @NonNull Builder setNuisanceConfidence(@NuisanceConfidence int nuisanceConfidence) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800146 mNuisanceConfidence = nuisanceConfidence;
147 return this;
148 }
149
150 /**
151 * Creates a new instance of {@link CallIdentification} based on the parameters set in this
152 * builder.
153 *
154 * @return {@link CallIdentification} instance.
155 */
Slava Shklyaeve65e4012019-03-04 16:02:34 +0000156 public @NonNull CallIdentification build() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800157 return new CallIdentification(mName, mDescription, mDetails, mPhoto,
158 mNuisanceConfidence, mPackageName, mAppName);
159 }
160 }
161
162 /** @hide */
163 @Retention(RetentionPolicy.SOURCE)
164 @IntDef(
165 prefix = { "CONFIDENCE_" },
166 value = {CONFIDENCE_NUISANCE, CONFIDENCE_LIKELY_NUISANCE, CONFIDENCE_UNKNOWN,
167 CONFIDENCE_LIKELY_NOT_NUISANCE, CONFIDENCE_NOT_NUISANCE})
168 public @interface NuisanceConfidence {}
169
170 /**
171 * Call has been identified as a nuisance call.
172 * <p>
173 * Returned from {@link #getNuisanceConfidence()} to indicate that a
174 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
175 * nuisance call.
176 */
177 public static final int CONFIDENCE_NUISANCE = 2;
178
179 /**
180 * Call has been identified as a likely nuisance call.
181 * <p>
182 * Returned from {@link #getNuisanceConfidence()} to indicate that a
183 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
184 * nuisance call.
185 */
186 public static final int CONFIDENCE_LIKELY_NUISANCE = 1;
187
188 /**
189 * Call could not be classified as nuisance or non-nuisance.
190 * <p>
191 * Returned from {@link #getNuisanceConfidence()} to indicate that a
192 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
193 * nuisance call.
194 */
195 public static final int CONFIDENCE_UNKNOWN = 0;
196
197 /**
198 * Call has been identified as not likely to be a nuisance call.
199 * <p>
200 * Returned from {@link #getNuisanceConfidence()} to indicate that a
201 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
202 * nuisance call.
203 */
204 public static final int CONFIDENCE_LIKELY_NOT_NUISANCE = -1;
205
206 /**
207 * Call has been identified as not a nuisance call.
208 * <p>
209 * Returned from {@link #getNuisanceConfidence()} to indicate that a
210 * {@link CallScreeningService} to indicate how confident it is that a call is or is not a
211 * nuisance call.
212 */
213 public static final int CONFIDENCE_NOT_NUISANCE = -2;
214
215 /**
216 * Default constructor for {@link CallIdentification}.
217 *
218 * @param name The name.
219 * @param description The description.
220 * @param details The details.
221 * @param photo The photo.
222 * @param nuisanceConfidence Confidence that this is a nuisance call.
223 * @hide
224 */
225 private CallIdentification(@Nullable String name, @Nullable String description,
226 @Nullable String details, @Nullable Icon photo,
227 @NuisanceConfidence int nuisanceConfidence) {
228 this(name, description, details, photo, nuisanceConfidence, null, null);
229 }
230
231 /**
232 * Default constructor for {@link CallIdentification}.
233 *
234 * @param name The name.
235 * @param description The description.
236 * @param details The details.
237 * @param photo The photo.
238 * @param nuisanceConfidence Confidence that this is a nuisance call.
239 * @param callScreeningPackageName Package name of the {@link CallScreeningService} which
240 * provided the call identification.
241 * @param callScreeningAppName App name of the {@link CallScreeningService} which provided the
242 * call identification.
243 * @hide
244 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800245 private CallIdentification(@Nullable CharSequence name, @Nullable CharSequence description,
246 @Nullable CharSequence details, @Nullable Icon photo,
Tyler Gunn7e45b722018-12-04 12:56:45 -0800247 @NuisanceConfidence int nuisanceConfidence, @NonNull String callScreeningPackageName,
Tyler Gunnb4830552019-02-15 16:04:13 -0800248 @NonNull CharSequence callScreeningAppName) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800249 mName = name;
250 mDescription = description;
251 mDetails = details;
252 mPhoto = photo;
253 mNuisanceConfidence = nuisanceConfidence;
Tyler Gunn9e76fd12018-12-17 09:56:11 -0800254 mCallScreeningAppName = callScreeningAppName;
255 mCallScreeningPackageName = callScreeningPackageName;
Tyler Gunn7e45b722018-12-04 12:56:45 -0800256 }
257
Tyler Gunnb4830552019-02-15 16:04:13 -0800258 private CharSequence mName;
259 private CharSequence mDescription;
260 private CharSequence mDetails;
Tyler Gunn7e45b722018-12-04 12:56:45 -0800261 private Icon mPhoto;
262 private int mNuisanceConfidence;
263 private String mCallScreeningPackageName;
Tyler Gunnb4830552019-02-15 16:04:13 -0800264 private CharSequence mCallScreeningAppName;
Tyler Gunn7e45b722018-12-04 12:56:45 -0800265
266 @Override
267 public int describeContents() {
268 return 0;
269 }
270
271 @Override
272 public void writeToParcel(Parcel parcel, int i) {
Tyler Gunnb4830552019-02-15 16:04:13 -0800273 parcel.writeCharSequence(mName);
274 parcel.writeCharSequence(mDescription);
275 parcel.writeCharSequence(mDetails);
Tyler Gunn7e45b722018-12-04 12:56:45 -0800276 parcel.writeParcelable(mPhoto, 0);
277 parcel.writeInt(mNuisanceConfidence);
278 parcel.writeString(mCallScreeningPackageName);
Tyler Gunnb4830552019-02-15 16:04:13 -0800279 parcel.writeCharSequence(mCallScreeningAppName);
Tyler Gunn7e45b722018-12-04 12:56:45 -0800280 }
281
282 /**
283 * Responsible for creating CallIdentification objects for deserialized Parcels.
284 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700285 public static final @android.annotation.NonNull Parcelable.Creator<CallIdentification> CREATOR =
Tyler Gunn7e45b722018-12-04 12:56:45 -0800286 new Parcelable.Creator<CallIdentification> () {
287
288 @Override
289 public CallIdentification createFromParcel(Parcel source) {
Tyler Gunnb4830552019-02-15 16:04:13 -0800290 CharSequence name = source.readCharSequence();
291 CharSequence description = source.readCharSequence();
292 CharSequence details = source.readCharSequence();
Tyler Gunn7e45b722018-12-04 12:56:45 -0800293 Icon photo = source.readParcelable(ClassLoader.getSystemClassLoader());
294 int nuisanceConfidence = source.readInt();
295 String callScreeningPackageName = source.readString();
Tyler Gunnb4830552019-02-15 16:04:13 -0800296 CharSequence callScreeningAppName = source.readCharSequence();
Tyler Gunn7e45b722018-12-04 12:56:45 -0800297 return new CallIdentification(name, description, details, photo,
298 nuisanceConfidence, callScreeningPackageName, callScreeningAppName);
299 }
300
301 @Override
302 public CallIdentification[] newArray(int size) {
303 return new CallIdentification[size];
304 }
305 };
306
307 /**
308 * The name associated with the number.
309 * <p>
310 * The name of the call as identified by a {@link CallScreeningService}. Could be a business
311 * name, for example.
312 *
313 * @return The name associated with the number, or {@code null} if none was provided.
314 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800315 public final @Nullable CharSequence getName() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800316 return mName;
317 }
318
319 /**
320 * Description of the call.
321 * <p>
322 * A description of the call as identified by a {@link CallScreeningService}. The description
323 * is typically presented by Dialer apps after the {@link #getName() name} to provide a short
324 * piece of relevant information about the call. This could include a location, address, or a
325 * message regarding the potential nature of the call (e.g. potential telemarketer).
326 *
327 * @return The call description, or {@code null} if none was provided.
328 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800329 public final @Nullable CharSequence getDescription() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800330 return mDescription;
331 }
332
333 /**
334 * Details of the call.
335 * <p>
336 * Details of the call as identified by a {@link CallScreeningService}. The details
337 * are typically presented by Dialer apps after the {@link #getName() name} and
338 * {@link #getDescription() description} to provide further clarifying information about the
339 * call. This could include, for example, the opening hours of a business, or stats about
340 * the number of times a call has been reported as spam.
341 *
342 * @return The call details, or {@code null} if none was provided.
343 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800344 public final @Nullable CharSequence getDetails() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800345 return mDetails;
346 }
347
348 /**
349 * Photo associated with the call.
350 * <p>
351 * A photo associated with the call as identified by a {@link CallScreeningService}. This
352 * could be, for example, a business logo, or a photo of the caller.
353 *
354 * @return The photo associated with the call, or {@code null} if none was provided.
355 */
356 public final @Nullable Icon getPhoto() {
357 return mPhoto;
358 }
359
360 /**
361 * Indicates the likelihood that this call is a nuisance call.
362 * <p>
363 * How likely the call is a nuisance call, as identified by a {@link CallScreeningService}.
364 *
365 * @return The nuisance confidence.
366 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800367 public final @NuisanceConfidence int getNuisanceConfidence() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800368 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800390 public final @NonNull CharSequence getCallScreeningAppName() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800391 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800410 public void setCallScreeningAppName(@NonNull CharSequence callScreeningAppName) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800411 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 }
Tyler Gunn9e76fd12018-12-17 09:56:11 -0800433
434 @Override
435 public String toString() {
436 StringBuilder sb = new StringBuilder();
437 sb.append("[CallId mName=");
438 sb.append(Log.pii(mName));
439 sb.append(", mDesc=");
440 sb.append(mDescription);
441 sb.append(", mDet=");
442 sb.append(mDetails);
443 sb.append(", conf=");
444 sb.append(mNuisanceConfidence);
445 sb.append(", appName=");
446 sb.append(mCallScreeningAppName);
447 sb.append(", pkgName=");
448 sb.append(mCallScreeningPackageName);
449 return sb.toString();
450 }
Tyler Gunn7e45b722018-12-04 12:56:45 -0800451}