blob: 0d97567ed21356f703087127f1c80f31022f08d2 [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 Gunna842e762019-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 Gunna842e762019-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 Liu6dfa2492019-10-01 17:20:39 -0700141 private final boolean mShouldScreenCallFurther;
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,
149 boolean shouldScreenCallFurther) {
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 Liu6dfa2492019-10-01 17:20:39 -0700155 if (shouldDisallowCall && shouldScreenCallFurther) {
156 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 Liu6dfa2492019-10-01 17:20:39 -0700164 mShouldScreenCallFurther = shouldScreenCallFurther;
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 */
208 public boolean getShouldScreenCallFurther() {
209 return mShouldScreenCallFurther;
210 }
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 Liu6dfa2492019-10-01 17:20:39 -0700218 private boolean mShouldScreenCallFurther;
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 *
282 * @param shouldScreenCallFurther Whether to request further call screening.
283 * @hide
284 */
285 @SystemApi
286 @TestApi
287 public Builder setShouldScreenCallFurther(boolean shouldScreenCallFurther) {
288 mShouldScreenCallFurther = shouldScreenCallFurther;
289 return this;
290 }
291
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800292 public CallResponse build() {
293 return new CallResponse(
294 mShouldDisallowCall,
295 mShouldRejectCall,
Usman Abdullah47b392d2019-03-06 15:54:56 -0800296 mShouldSilenceCall,
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800297 mShouldSkipCallLog,
Hall Liu6dfa2492019-10-01 17:20:39 -0700298 mShouldSkipNotification,
299 mShouldScreenCallFurther);
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800300 }
301 }
302 }
303
304 public CallScreeningService() {
305 }
306
307 @Override
308 public IBinder onBind(Intent intent) {
309 Log.v(this, "onBind");
310 return new CallScreeningBinder();
311 }
312
313 @Override
314 public boolean onUnbind(Intent intent) {
315 Log.v(this, "onUnbind");
316 return false;
317 }
318
319 /**
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800320 * Called when a new incoming or outgoing call is added which is not in the user's contact list.
321 * <p>
322 * A {@link CallScreeningService} must indicate whether an incoming call is allowed or not by
323 * calling
324 * {@link CallScreeningService#respondToCall(Call.Details, CallScreeningService.CallResponse)}.
325 * Your app can tell if a call is an incoming call by checking to see if
326 * {@link Call.Details#getCallDirection()} is {@link Call.Details#DIRECTION_INCOMING}.
327 * <p>
Tyler Gunne0caec72018-11-30 14:21:18 -0800328 * Note: The {@link Call.Details} instance provided to a call screening service will only have
329 * the following properties set. The rest of the {@link Call.Details} properties will be set to
330 * their default value or {@code null}.
331 * <ul>
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800332 * <li>{@link Call.Details#getCallDirection()}</li>
Tyler Gunne0caec72018-11-30 14:21:18 -0800333 * <li>{@link Call.Details#getConnectTimeMillis()}</li>
334 * <li>{@link Call.Details#getCreationTimeMillis()}</li>
335 * <li>{@link Call.Details#getHandle()}</li>
336 * <li>{@link Call.Details#getHandlePresentation()}</li>
337 * </ul>
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800338 * <p>
339 * Only calls where the {@link Call.Details#getHandle() handle} {@link Uri#getScheme() scheme}
340 * is {@link PhoneAccount#SCHEME_TEL} are passed for call
341 * screening. Further, only calls which are not in the user's contacts are passed for
342 * screening. For outgoing calls, no post-dial digits are passed.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800343 *
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800344 * @param callDetails Information about a new call, see {@link Call.Details}.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800345 */
Tyler Gunn7e45b722018-12-04 12:56:45 -0800346 public abstract void onScreenCall(@NonNull Call.Details callDetails);
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800347
348 /**
Usman Abdullah47b392d2019-03-06 15:54:56 -0800349 * Responds to the given incoming call, either allowing it, silencing it or disallowing it.
Tyler Gunn7e45b722018-12-04 12:56:45 -0800350 * <p>
351 * The {@link CallScreeningService} calls this method to inform the system whether the call
Usman Abdullah47b392d2019-03-06 15:54:56 -0800352 * should be silently blocked or not. In the event that it should not be blocked, it may
353 * also be requested to ring silently.
Tyler Gunn9e76fd19b2018-12-17 09:56:11 -0800354 * <p>
355 * Calls to this method are ignored unless the {@link Call.Details#getCallDirection()} is
356 * {@link Call.Details#DIRECTION_INCOMING}.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800357 *
358 * @param callDetails The call to allow.
Tyler Gunn7e45b722018-12-04 12:56:45 -0800359 * <p>
360 * Must be the same {@link Call.Details call} which was provided to the
361 * {@link CallScreeningService} via {@link #onScreenCall(Call.Details)}.
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800362 * @param response The {@link CallScreeningService.CallResponse} which contains information
363 * about how to respond to a call.
364 */
Tyler Gunn7e45b722018-12-04 12:56:45 -0800365 public final void respondToCall(@NonNull Call.Details callDetails,
366 @NonNull CallResponse response) {
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800367 try {
368 if (response.getDisallowCall()) {
369 mCallScreeningAdapter.disallowCall(
370 callDetails.getTelecomCallId(),
371 response.getRejectCall(),
372 !response.getSkipCallLog(),
tonyzhu9e1d4f82018-10-22 15:11:31 +0800373 !response.getSkipNotification(),
374 new ComponentName(getPackageName(), getClass().getName()));
Usman Abdullah47b392d2019-03-06 15:54:56 -0800375 } else if (response.getSilenceCall()) {
376 mCallScreeningAdapter.silenceCall(callDetails.getTelecomCallId());
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800377 } else {
378 mCallScreeningAdapter.allowCall(callDetails.getTelecomCallId());
379 }
380 } catch (RemoteException e) {
381 }
382 }
Sailesh Nepal1bef3392016-01-24 18:21:53 -0800383}