blob: 7a3befe35cd74d4e89c9a06e25308126506feae7 [file] [log] [blame]
Sailesh Nepal1bef3392016-01-24 18:21:53 -08001/*
2 * Copyright (C) 2016 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
Tyler Gunn7e45b722018-12-04 12:56:45 -080019import android.annotation.NonNull;
Sailesh Nepal1bef3392016-01-24 18:21:53 -080020import android.annotation.SdkConstant;
Hall Liu6dfa2492019-10-01 17:20:39 -070021import android.annotation.SystemApi;
22import android.annotation.TestApi;
Sailesh Nepal1bef3392016-01-24 18:21:53 -080023import android.app.Service;
tonyzhu9e1d4f82018-10-22 15:11:31 +080024import android.content.ComponentName;
Sailesh Nepal1bef3392016-01-24 18:21:53 -080025import android.content.Intent;
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -080026import android.net.Uri;
Sailesh Nepal1bef3392016-01-24 18:21:53 -080027import android.os.Handler;
28import android.os.IBinder;
29import android.os.Looper;
30import android.os.Message;
31import android.os.RemoteException;
32
33import com.android.internal.os.SomeArgs;
Sailesh Nepal1bef3392016-01-24 18:21:53 -080034import com.android.internal.telecom.ICallScreeningAdapter;
Tyler Gunne0caec72018-11-30 14:21:18 -080035import com.android.internal.telecom.ICallScreeningService;
Sailesh Nepal1bef3392016-01-24 18:21:53 -080036
37/**
38 * This service can be implemented by the default dialer (see
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -080039 * {@link TelecomManager#getDefaultDialerPackage()}) or a third party app to allow or disallow
Tyler Gunna842e7622019-03-29 11:32:08 -070040 * incoming calls before they are shown to a user. A {@link CallScreeningService} can also see
41 * outgoing calls for the purpose of providing caller ID services for those calls.
Sailesh Nepal1bef3392016-01-24 18:21:53 -080042 * <p>
43 * Below is an example manifest registration for a {@code CallScreeningService}.
44 * <pre>
45 * {@code
46 * <service android:name="your.package.YourCallScreeningServiceImplementation"
47 * android:permission="android.permission.BIND_SCREENING_SERVICE">
48 * <intent-filter>
49 * <action android:name="android.telecom.CallScreeningService"/>
50 * </intent-filter>
51 * </service>
52 * }
53 * </pre>
Tyler Gunn7e45b722018-12-04 12:56:45 -080054 * <p>
55 * A CallScreeningService performs two functions:
56 * <ol>
57 * <li>Call blocking/screening - the service can choose which calls will ring on the user's
58 * device, and which will be silently sent to voicemail.</li>
Tyler Gunna842e7622019-03-29 11:32:08 -070059 * <li>Call identification - services which provide call identification functionality can
60 * display a user-interface of their choosing which contains identifying information for a call.
61 * </li>
Tyler Gunn7e45b722018-12-04 12:56:45 -080062 * </ol>
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -080063 * <p>
64 * <h2>Becoming the {@link CallScreeningService}</h2>
65 * Telecom will bind to a single app chosen by the user which implements the
66 * {@link CallScreeningService} API when there are new incoming and outgoing calls.
67 * <p>
68 * The code snippet below illustrates how your app can request that it fills the call screening
69 * role.
70 * <pre>
71 * {@code
72 * private static final int REQUEST_ID = 1;
73 *
74 * public void requestRole() {
75 * RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
Grace Jia2d21e952019-09-20 14:57:00 -070076 * Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -080077 * startActivityForResult(intent, REQUEST_ID);
78 * }
79 *
80 * &#64;Override
81 * public void onActivityResult(int requestCode, int resultCode, Intent data) {
82 * if (requestCode == REQUEST_ID) {
83 * if (resultCode == android.app.Activity.RESULT_OK) {
84 * // Your app is now the call screening app
85 * } else {
86 * // Your app is not the call screening app
87 * }
88 * }
89 * }
90 * </pre>
Sailesh Nepal1bef3392016-01-24 18:21:53 -080091 */
92public abstract class CallScreeningService extends Service {
93 /**
94 * The {@link Intent} that must be declared as handled by the service.
95 */
96 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
97 public static final String SERVICE_INTERFACE = "android.telecom.CallScreeningService";
98
99 private static final int MSG_SCREEN_CALL = 1;
100
101 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
102 @Override
103 public void handleMessage(Message msg) {
104 switch (msg.what) {
105 case MSG_SCREEN_CALL:
106 SomeArgs args = (SomeArgs) msg.obj;
107 try {
108 mCallScreeningAdapter = (ICallScreeningAdapter) args.arg1;
109 onScreenCall(
110 Call.Details.createFromParcelableCall((ParcelableCall) args.arg2));
111 } finally {
112 args.recycle();
113 }
114 break;
115 }
116 }
117 };
118
119 private final class CallScreeningBinder extends ICallScreeningService.Stub {
120 @Override
121 public void screenCall(ICallScreeningAdapter adapter, ParcelableCall call) {
122 Log.v(this, "screenCall");
123 SomeArgs args = SomeArgs.obtain();
124 args.arg1 = adapter;
125 args.arg2 = call;
126 mHandler.obtainMessage(MSG_SCREEN_CALL, args).sendToTarget();
127 }
128 }
129
130 private ICallScreeningAdapter mCallScreeningAdapter;
131
132 /*
133 * Information about how to respond to an incoming call.
134 */
Sailesh Nepalf4460712016-01-27 16:45:51 -0800135 public static class CallResponse {
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800136 private final boolean mShouldDisallowCall;
137 private final boolean mShouldRejectCall;
Usman Abdullah47b392d2019-03-06 15:54:56 -0800138 private final boolean mShouldSilenceCall;
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800139 private final boolean mShouldSkipCallLog;
140 private final boolean mShouldSkipNotification;
Hall Liu69554cf2019-11-11 17:44:09 -0800141 private final boolean mShouldScreenCallViaAudioProcessing;
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800142
143 private CallResponse(
144 boolean shouldDisallowCall,
145 boolean shouldRejectCall,
Usman Abdullah47b392d2019-03-06 15:54:56 -0800146 boolean shouldSilenceCall,
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800147 boolean shouldSkipCallLog,
Hall Liu6dfa2492019-10-01 17:20:39 -0700148 boolean shouldSkipNotification,
Hall Liu69554cf2019-11-11 17:44:09 -0800149 boolean shouldScreenCallViaAudioProcessing) {
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800150 if (!shouldDisallowCall
151 && (shouldRejectCall || shouldSkipCallLog || shouldSkipNotification)) {
152 throw new IllegalStateException("Invalid response state for allowed call.");
153 }
154
Hall Liu69554cf2019-11-11 17:44:09 -0800155 if (shouldDisallowCall && shouldScreenCallViaAudioProcessing) {
Hall Liu6dfa2492019-10-01 17:20:39 -0700156 throw new IllegalStateException("Invalid response state for allowed call.");
157 }
158
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800159 mShouldDisallowCall = shouldDisallowCall;
160 mShouldRejectCall = shouldRejectCall;
161 mShouldSkipCallLog = shouldSkipCallLog;
162 mShouldSkipNotification = shouldSkipNotification;
Usman Abdullah47b392d2019-03-06 15:54:56 -0800163 mShouldSilenceCall = shouldSilenceCall;
Hall Liu69554cf2019-11-11 17:44:09 -0800164 mShouldScreenCallViaAudioProcessing = shouldScreenCallViaAudioProcessing;
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800165 }
166
167 /*
168 * @return Whether the incoming call should be blocked.
169 */
170 public boolean getDisallowCall() {
171 return mShouldDisallowCall;
172 }
173
174 /*
175 * @return Whether the incoming call should be disconnected as if the user had manually
176 * rejected it.
177 */
178 public boolean getRejectCall() {
179 return mShouldRejectCall;
180 }
181
182 /*
Usman Abdullah47b392d2019-03-06 15:54:56 -0800183 * @return Whether the ringtone should be silenced for the incoming call.
184 */
185 public boolean getSilenceCall() {
186 return mShouldSilenceCall;
187 }
188
189 /*
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800190 * @return Whether the incoming call should not be displayed in the call log.
191 */
192 public boolean getSkipCallLog() {
193 return mShouldSkipCallLog;
194 }
195
196 /*
197 * @return Whether a missed call notification should not be shown for the incoming call.
198 */
199 public boolean getSkipNotification() {
200 return mShouldSkipNotification;
201 }
202
Hall Liu6dfa2492019-10-01 17:20:39 -0700203 /**
204 * @return Whether we should enter the {@link Call#STATE_AUDIO_PROCESSING} state to allow
205 * for further screening of the call.
206 * @hide
207 */
Hall Liu69554cf2019-11-11 17:44:09 -0800208 public boolean getShouldScreenCallViaAudioProcessing() {
209 return mShouldScreenCallViaAudioProcessing;
Hall Liu6dfa2492019-10-01 17:20:39 -0700210 }
211
Sailesh Nepalf4460712016-01-27 16:45:51 -0800212 public static class Builder {
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800213 private boolean mShouldDisallowCall;
214 private boolean mShouldRejectCall;
Usman Abdullah47b392d2019-03-06 15:54:56 -0800215 private boolean mShouldSilenceCall;
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800216 private boolean mShouldSkipCallLog;
217 private boolean mShouldSkipNotification;
Hall Liu69554cf2019-11-11 17:44:09 -0800218 private boolean mShouldScreenCallViaAudioProcessing;
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800219
Tyler Gunne0caec72018-11-30 14:21:18 -0800220 /**
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800221 * Sets whether the incoming call should be blocked.
222 */
223 public Builder setDisallowCall(boolean shouldDisallowCall) {
224 mShouldDisallowCall = shouldDisallowCall;
225 return this;
226 }
227
Tyler Gunne0caec72018-11-30 14:21:18 -0800228 /**
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800229 * Sets whether the incoming call should be disconnected as if the user had manually
230 * rejected it. This property should only be set to true if the call is disallowed.
231 */
232 public Builder setRejectCall(boolean shouldRejectCall) {
233 mShouldRejectCall = shouldRejectCall;
234 return this;
235 }
236
Tyler Gunne0caec72018-11-30 14:21:18 -0800237 /**
Usman Abdullah47b392d2019-03-06 15:54:56 -0800238 * Sets whether ringing should be silenced for the incoming call. When set
239 * to {@code true}, the Telecom framework will not play a ringtone for the call.
240 * The call will, however, still be sent to the default dialer app if it is not blocked.
241 * A {@link CallScreeningService} can use this to ensure a potential nuisance call is
242 * still surfaced to the user, but in a less intrusive manner.
243 *
244 * Setting this to true only makes sense when the call has not been disallowed
245 * using {@link #setDisallowCall(boolean)}.
246 */
247 public @NonNull Builder setSilenceCall(boolean shouldSilenceCall) {
248 mShouldSilenceCall = shouldSilenceCall;
249 return this;
250 }
251
252 /**
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800253 * Sets whether the incoming call should not be displayed in the call log. This property
254 * should only be set to true if the call is disallowed.
Tyler Gunne0caec72018-11-30 14:21:18 -0800255 * <p>
256 * Note: Calls will still be logged with type
257 * {@link android.provider.CallLog.Calls#BLOCKED_TYPE}, regardless of how this property
258 * is set.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800259 */
260 public Builder setSkipCallLog(boolean shouldSkipCallLog) {
261 mShouldSkipCallLog = shouldSkipCallLog;
262 return this;
263 }
264
Tyler Gunne0caec72018-11-30 14:21:18 -0800265 /**
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800266 * Sets whether a missed call notification should not be shown for the incoming call.
267 * This property should only be set to true if the call is disallowed.
268 */
269 public Builder setSkipNotification(boolean shouldSkipNotification) {
270 mShouldSkipNotification = shouldSkipNotification;
271 return this;
272 }
273
Hall Liu6dfa2492019-10-01 17:20:39 -0700274 /**
275 * Sets whether to request background audio processing so that the in-call service can
276 * screen the call further. If set to {@code true}, {@link #setDisallowCall} should be
277 * called with {@code false}, and all other parameters in this builder will be ignored.
278 *
279 * This request will only be honored if the {@link CallScreeningService} shares the same
280 * uid as the default dialer app. Otherwise, the call will go through as usual.
281 *
Hall Liu69554cf2019-11-11 17:44:09 -0800282 * @param shouldScreenCallViaAudioProcessing Whether to request further call screening.
Hall Liu6dfa2492019-10-01 17:20:39 -0700283 * @hide
284 */
285 @SystemApi
286 @TestApi
Hall Liu69554cf2019-11-11 17:44:09 -0800287 public @NonNull Builder setShouldScreenCallViaAudioProcessing(
288 boolean shouldScreenCallViaAudioProcessing) {
289 mShouldScreenCallViaAudioProcessing = shouldScreenCallViaAudioProcessing;
Hall Liu6dfa2492019-10-01 17:20:39 -0700290 return this;
291 }
292
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800293 public CallResponse build() {
294 return new CallResponse(
295 mShouldDisallowCall,
296 mShouldRejectCall,
Usman Abdullah47b392d2019-03-06 15:54:56 -0800297 mShouldSilenceCall,
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800298 mShouldSkipCallLog,
Hall Liu6dfa2492019-10-01 17:20:39 -0700299 mShouldSkipNotification,
Hall Liu69554cf2019-11-11 17:44:09 -0800300 mShouldScreenCallViaAudioProcessing);
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800301 }
302 }
303 }
304
305 public CallScreeningService() {
306 }
307
308 @Override
309 public IBinder onBind(Intent intent) {
310 Log.v(this, "onBind");
311 return new CallScreeningBinder();
312 }
313
314 @Override
315 public boolean onUnbind(Intent intent) {
316 Log.v(this, "onUnbind");
317 return false;
318 }
319
320 /**
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800321 * Called when a new incoming or outgoing call is added which is not in the user's contact list.
322 * <p>
323 * A {@link CallScreeningService} must indicate whether an incoming call is allowed or not by
324 * calling
325 * {@link CallScreeningService#respondToCall(Call.Details, CallScreeningService.CallResponse)}.
326 * Your app can tell if a call is an incoming call by checking to see if
327 * {@link Call.Details#getCallDirection()} is {@link Call.Details#DIRECTION_INCOMING}.
328 * <p>
Tyler Gunne0caec72018-11-30 14:21:18 -0800329 * Note: The {@link Call.Details} instance provided to a call screening service will only have
330 * the following properties set. The rest of the {@link Call.Details} properties will be set to
331 * their default value or {@code null}.
332 * <ul>
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800333 * <li>{@link Call.Details#getCallDirection()}</li>
Tyler Gunne0caec72018-11-30 14:21:18 -0800334 * <li>{@link Call.Details#getConnectTimeMillis()}</li>
335 * <li>{@link Call.Details#getCreationTimeMillis()}</li>
336 * <li>{@link Call.Details#getHandle()}</li>
337 * <li>{@link Call.Details#getHandlePresentation()}</li>
338 * </ul>
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800339 * <p>
340 * Only calls where the {@link Call.Details#getHandle() handle} {@link Uri#getScheme() scheme}
341 * is {@link PhoneAccount#SCHEME_TEL} are passed for call
342 * screening. Further, only calls which are not in the user's contacts are passed for
343 * screening. For outgoing calls, no post-dial digits are passed.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800344 *
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800345 * @param callDetails Information about a new call, see {@link Call.Details}.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800346 */
Tyler Gunn7e45b722018-12-04 12:56:45 -0800347 public abstract void onScreenCall(@NonNull Call.Details callDetails);
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800348
349 /**
Usman Abdullah47b392d2019-03-06 15:54:56 -0800350 * Responds to the given incoming call, either allowing it, silencing it or disallowing it.
Tyler Gunn7e45b722018-12-04 12:56:45 -0800351 * <p>
352 * The {@link CallScreeningService} calls this method to inform the system whether the call
Usman Abdullah47b392d2019-03-06 15:54:56 -0800353 * should be silently blocked or not. In the event that it should not be blocked, it may
354 * also be requested to ring silently.
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800355 * <p>
356 * Calls to this method are ignored unless the {@link Call.Details#getCallDirection()} is
357 * {@link Call.Details#DIRECTION_INCOMING}.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800358 *
359 * @param callDetails The call to allow.
Tyler Gunn7e45b722018-12-04 12:56:45 -0800360 * <p>
361 * Must be the same {@link Call.Details call} which was provided to the
362 * {@link CallScreeningService} via {@link #onScreenCall(Call.Details)}.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800363 * @param response The {@link CallScreeningService.CallResponse} which contains information
364 * about how to respond to a call.
365 */
Tyler Gunn7e45b722018-12-04 12:56:45 -0800366 public final void respondToCall(@NonNull Call.Details callDetails,
367 @NonNull CallResponse response) {
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800368 try {
369 if (response.getDisallowCall()) {
370 mCallScreeningAdapter.disallowCall(
371 callDetails.getTelecomCallId(),
372 response.getRejectCall(),
373 !response.getSkipCallLog(),
tonyzhu9e1d4f82018-10-22 15:11:31 +0800374 !response.getSkipNotification(),
375 new ComponentName(getPackageName(), getClass().getName()));
Usman Abdullah47b392d2019-03-06 15:54:56 -0800376 } else if (response.getSilenceCall()) {
377 mCallScreeningAdapter.silenceCall(callDetails.getTelecomCallId());
Hall Liu69554cf2019-11-11 17:44:09 -0800378 } else if (response.getShouldScreenCallViaAudioProcessing()) {
Hall Liu31de23d2019-10-11 15:38:29 -0700379 mCallScreeningAdapter.screenCallFurther(callDetails.getTelecomCallId());
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800380 } else {
381 mCallScreeningAdapter.allowCall(callDetails.getTelecomCallId());
382 }
383 } catch (RemoteException e) {
384 }
385 }
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800386}