blob: cde7f608fa6ad1f3b24250dff195850e8c2f6263 [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 */
47 public 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -080070 public Builder(String callIdPackageName, 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -080083 public 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800100 public 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800117 public Builder setDetails(@Nullable CharSequence details) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800118 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800244 private CallIdentification(@Nullable CharSequence name, @Nullable CharSequence description,
245 @Nullable CharSequence details, @Nullable Icon photo,
Tyler Gunn7e45b722018-12-04 12:56:45 -0800246 @NuisanceConfidence int nuisanceConfidence, @NonNull String callScreeningPackageName,
Tyler Gunnb4830552019-02-15 16:04:13 -0800247 @NonNull CharSequence callScreeningAppName) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800248 mName = name;
249 mDescription = description;
250 mDetails = details;
251 mPhoto = photo;
252 mNuisanceConfidence = nuisanceConfidence;
Tyler Gunn9e76fd12018-12-17 09:56:11 -0800253 mCallScreeningAppName = callScreeningAppName;
254 mCallScreeningPackageName = callScreeningPackageName;
Tyler Gunn7e45b722018-12-04 12:56:45 -0800255 }
256
Tyler Gunnb4830552019-02-15 16:04:13 -0800257 private CharSequence mName;
258 private CharSequence mDescription;
259 private CharSequence mDetails;
Tyler Gunn7e45b722018-12-04 12:56:45 -0800260 private Icon mPhoto;
261 private int mNuisanceConfidence;
262 private String mCallScreeningPackageName;
Tyler Gunnb4830552019-02-15 16:04:13 -0800263 private CharSequence mCallScreeningAppName;
Tyler Gunn7e45b722018-12-04 12:56:45 -0800264
265 @Override
266 public int describeContents() {
267 return 0;
268 }
269
270 @Override
271 public void writeToParcel(Parcel parcel, int i) {
Tyler Gunnb4830552019-02-15 16:04:13 -0800272 parcel.writeCharSequence(mName);
273 parcel.writeCharSequence(mDescription);
274 parcel.writeCharSequence(mDetails);
Tyler Gunn7e45b722018-12-04 12:56:45 -0800275 parcel.writeParcelable(mPhoto, 0);
276 parcel.writeInt(mNuisanceConfidence);
277 parcel.writeString(mCallScreeningPackageName);
Tyler Gunnb4830552019-02-15 16:04:13 -0800278 parcel.writeCharSequence(mCallScreeningAppName);
Tyler Gunn7e45b722018-12-04 12:56:45 -0800279 }
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) {
Tyler Gunnb4830552019-02-15 16:04:13 -0800289 CharSequence name = source.readCharSequence();
290 CharSequence description = source.readCharSequence();
291 CharSequence details = source.readCharSequence();
Tyler Gunn7e45b722018-12-04 12:56:45 -0800292 Icon photo = source.readParcelable(ClassLoader.getSystemClassLoader());
293 int nuisanceConfidence = source.readInt();
294 String callScreeningPackageName = source.readString();
Tyler Gunnb4830552019-02-15 16:04:13 -0800295 CharSequence callScreeningAppName = source.readCharSequence();
Tyler Gunn7e45b722018-12-04 12:56:45 -0800296 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800314 public final @Nullable CharSequence getName() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800315 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800328 public final @Nullable CharSequence getDescription() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800329 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800343 public final @Nullable CharSequence getDetails() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800344 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 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800366 public final @NuisanceConfidence int getNuisanceConfidence() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800367 return mNuisanceConfidence;
368 }
369
370 /**
371 * The package name of the {@link CallScreeningService} which provided the
372 * {@link CallIdentification}.
373 * <p>
374 * A {@link CallScreeningService} may not set this property; it is set by the system.
375 * @return the package name
376 */
377 public final @NonNull String getCallScreeningPackageName() {
378 return mCallScreeningPackageName;
379 }
380
381 /**
382 * The {@link android.content.pm.PackageManager#getApplicationLabel(ApplicationInfo) name} of
383 * the {@link CallScreeningService} which provided the {@link CallIdentification}.
384 * <p>
385 * A {@link CallScreeningService} may not set this property; it is set by the system.
386 *
387 * @return The name of the app.
388 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800389 public final @NonNull CharSequence getCallScreeningAppName() {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800390 return mCallScreeningAppName;
391 }
392
393 /**
394 * Set the package name of the {@link CallScreeningService} which provided this information.
395 *
396 * @param callScreeningPackageName The package name.
397 * @hide
398 */
399 public void setCallScreeningPackageName(@NonNull String callScreeningPackageName) {
400 mCallScreeningPackageName = callScreeningPackageName;
401 }
402
403 /**
404 * Set the app name of the {@link CallScreeningService} which provided this information.
405 *
406 * @param callScreeningAppName The app name.
407 * @hide
408 */
Tyler Gunnb4830552019-02-15 16:04:13 -0800409 public void setCallScreeningAppName(@NonNull CharSequence callScreeningAppName) {
Tyler Gunn7e45b722018-12-04 12:56:45 -0800410 mCallScreeningAppName = callScreeningAppName;
411 }
412
413 @Override
414 public boolean equals(Object o) {
415 if (this == o) return true;
416 if (o == null || getClass() != o.getClass()) return false;
417 CallIdentification that = (CallIdentification) o;
418 // Note: mPhoto purposely omit as no good comparison exists.
419 return mNuisanceConfidence == that.mNuisanceConfidence
420 && Objects.equals(mName, that.mName)
421 && Objects.equals(mDescription, that.mDescription)
422 && Objects.equals(mDetails, that.mDetails)
423 && Objects.equals(mCallScreeningAppName, that.mCallScreeningAppName)
424 && Objects.equals(mCallScreeningPackageName, that.mCallScreeningPackageName);
425 }
426
427 @Override
428 public int hashCode() {
429 return Objects.hash(mName, mDescription, mDetails, mPhoto, mNuisanceConfidence,
430 mCallScreeningAppName, mCallScreeningPackageName);
431 }
Tyler Gunn9e76fd12018-12-17 09:56:11 -0800432
433 @Override
434 public String toString() {
435 StringBuilder sb = new StringBuilder();
436 sb.append("[CallId mName=");
437 sb.append(Log.pii(mName));
438 sb.append(", mDesc=");
439 sb.append(mDescription);
440 sb.append(", mDet=");
441 sb.append(mDetails);
442 sb.append(", conf=");
443 sb.append(mNuisanceConfidence);
444 sb.append(", appName=");
445 sb.append(mCallScreeningAppName);
446 sb.append(", pkgName=");
447 sb.append(mCallScreeningPackageName);
448 return sb.toString();
449 }
Tyler Gunn7e45b722018-12-04 12:56:45 -0800450}