Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.telecom; |
| 18 | |
Hall Liu | ff4a125 | 2020-01-01 16:27:14 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Hall Liu | ff4a125 | 2020-01-01 16:27:14 -0800 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
| 24 | import android.annotation.TestApi; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 25 | import android.app.Service; |
tonyzhu | 9e1d4f8 | 2018-10-22 15:11:31 +0800 | [diff] [blame] | 26 | import android.content.ComponentName; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Tyler Gunn | 460b7d4 | 2020-05-15 10:19:32 -0700 | [diff] [blame] | 28 | import android.content.pm.ServiceInfo; |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.IBinder; |
| 32 | import android.os.Looper; |
| 33 | import android.os.Message; |
| 34 | import android.os.RemoteException; |
| 35 | |
| 36 | import com.android.internal.os.SomeArgs; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 37 | import com.android.internal.telecom.ICallScreeningAdapter; |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 38 | import com.android.internal.telecom.ICallScreeningService; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * This service can be implemented by the default dialer (see |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 42 | * {@link TelecomManager#getDefaultDialerPackage()}) or a third party app to allow or disallow |
Tyler Gunn | a842e762 | 2019-03-29 11:32:08 -0700 | [diff] [blame] | 43 | * incoming calls before they are shown to a user. A {@link CallScreeningService} can also see |
| 44 | * outgoing calls for the purpose of providing caller ID services for those calls. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 45 | * <p> |
| 46 | * Below is an example manifest registration for a {@code CallScreeningService}. |
| 47 | * <pre> |
| 48 | * {@code |
| 49 | * <service android:name="your.package.YourCallScreeningServiceImplementation" |
| 50 | * android:permission="android.permission.BIND_SCREENING_SERVICE"> |
| 51 | * <intent-filter> |
| 52 | * <action android:name="android.telecom.CallScreeningService"/> |
| 53 | * </intent-filter> |
| 54 | * </service> |
| 55 | * } |
| 56 | * </pre> |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 57 | * <p> |
| 58 | * A CallScreeningService performs two functions: |
| 59 | * <ol> |
| 60 | * <li>Call blocking/screening - the service can choose which calls will ring on the user's |
| 61 | * device, and which will be silently sent to voicemail.</li> |
Tyler Gunn | a842e762 | 2019-03-29 11:32:08 -0700 | [diff] [blame] | 62 | * <li>Call identification - services which provide call identification functionality can |
| 63 | * display a user-interface of their choosing which contains identifying information for a call. |
| 64 | * </li> |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 65 | * </ol> |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 66 | * <p> |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 67 | * <h2>Becoming the CallScreeningService</h2> |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 68 | * Telecom will bind to a single app chosen by the user which implements the |
| 69 | * {@link CallScreeningService} API when there are new incoming and outgoing calls. |
| 70 | * <p> |
| 71 | * The code snippet below illustrates how your app can request that it fills the call screening |
| 72 | * role. |
| 73 | * <pre> |
| 74 | * {@code |
| 75 | * private static final int REQUEST_ID = 1; |
| 76 | * |
| 77 | * public void requestRole() { |
| 78 | * RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE); |
Grace Jia | 2d21e95 | 2019-09-20 14:57:00 -0700 | [diff] [blame] | 79 | * Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING); |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 80 | * startActivityForResult(intent, REQUEST_ID); |
| 81 | * } |
| 82 | * |
| 83 | * @Override |
| 84 | * public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 85 | * if (requestCode == REQUEST_ID) { |
| 86 | * if (resultCode == android.app.Activity.RESULT_OK) { |
| 87 | * // Your app is now the call screening app |
| 88 | * } else { |
| 89 | * // Your app is not the call screening app |
| 90 | * } |
| 91 | * } |
| 92 | * } |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 93 | * } |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 94 | * </pre> |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 95 | * |
| 96 | * <h2>CallScreeningService Lifecycle</h2> |
| 97 | * |
| 98 | * The framework binds to the {@link CallScreeningService} implemented by the user-chosen app |
| 99 | * filling the {@link android.app.role.RoleManager#ROLE_CALL_SCREENING} role when incoming calls are |
| 100 | * received (prior to ringing) and when outgoing calls are placed. The platform calls the |
| 101 | * {@link #onScreenCall(Call.Details)} method to provide your service with details about the call. |
| 102 | * <p> |
| 103 | * For incoming calls, the {@link CallScreeningService} must call |
| 104 | * {@link #respondToCall(Call.Details, CallResponse)} within 5 seconds of being bound to indicate to |
| 105 | * the platform whether the call should be blocked or not. Your app must do this even if it is |
| 106 | * primarily performing caller ID operations and not screening calls. It is important to perform |
| 107 | * screening operations in a timely matter as the user's device will not begin ringing until the |
| 108 | * response is received (or the timeout is hit). A {@link CallScreeningService} may choose to |
| 109 | * perform local database lookups to help determine if a call should be screened or not; care should |
| 110 | * be taken to ensure the timeout is not repeatedly hit, causing delays in the incoming call flow. |
| 111 | * <p> |
| 112 | * If your app provides a caller ID experience, it should launch an activity to show the caller ID |
| 113 | * information from {@link #onScreenCall(Call.Details)}. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 114 | */ |
| 115 | public abstract class CallScreeningService extends Service { |
| 116 | /** |
| 117 | * The {@link Intent} that must be declared as handled by the service. |
| 118 | */ |
| 119 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 120 | public static final String SERVICE_INTERFACE = "android.telecom.CallScreeningService"; |
| 121 | |
| 122 | private static final int MSG_SCREEN_CALL = 1; |
| 123 | |
| 124 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 125 | @Override |
| 126 | public void handleMessage(Message msg) { |
| 127 | switch (msg.what) { |
| 128 | case MSG_SCREEN_CALL: |
| 129 | SomeArgs args = (SomeArgs) msg.obj; |
| 130 | try { |
| 131 | mCallScreeningAdapter = (ICallScreeningAdapter) args.arg1; |
Grace Jia | 90b3804 | 2019-11-06 14:12:33 -0800 | [diff] [blame] | 132 | Call.Details callDetails = Call.Details |
| 133 | .createFromParcelableCall((ParcelableCall) args.arg2); |
| 134 | onScreenCall(callDetails); |
| 135 | if (callDetails.getCallDirection() == Call.Details.DIRECTION_OUTGOING) { |
| 136 | mCallScreeningAdapter.allowCall(callDetails.getTelecomCallId()); |
| 137 | } |
| 138 | } catch (RemoteException e) { |
| 139 | Log.w(this, "Exception when screening call: " + e); |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 140 | } finally { |
| 141 | args.recycle(); |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | private final class CallScreeningBinder extends ICallScreeningService.Stub { |
| 149 | @Override |
| 150 | public void screenCall(ICallScreeningAdapter adapter, ParcelableCall call) { |
| 151 | Log.v(this, "screenCall"); |
| 152 | SomeArgs args = SomeArgs.obtain(); |
| 153 | args.arg1 = adapter; |
| 154 | args.arg2 = call; |
| 155 | mHandler.obtainMessage(MSG_SCREEN_CALL, args).sendToTarget(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private ICallScreeningAdapter mCallScreeningAdapter; |
| 160 | |
| 161 | /* |
| 162 | * Information about how to respond to an incoming call. |
| 163 | */ |
Sailesh Nepal | f446071 | 2016-01-27 16:45:51 -0800 | [diff] [blame] | 164 | public static class CallResponse { |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 165 | private final boolean mShouldDisallowCall; |
| 166 | private final boolean mShouldRejectCall; |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 167 | private final boolean mShouldSilenceCall; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 168 | private final boolean mShouldSkipCallLog; |
| 169 | private final boolean mShouldSkipNotification; |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 170 | private final boolean mShouldScreenCallViaAudioProcessing; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 171 | |
| 172 | private CallResponse( |
| 173 | boolean shouldDisallowCall, |
| 174 | boolean shouldRejectCall, |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 175 | boolean shouldSilenceCall, |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 176 | boolean shouldSkipCallLog, |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 177 | boolean shouldSkipNotification, |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 178 | boolean shouldScreenCallViaAudioProcessing) { |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 179 | if (!shouldDisallowCall |
| 180 | && (shouldRejectCall || shouldSkipCallLog || shouldSkipNotification)) { |
| 181 | throw new IllegalStateException("Invalid response state for allowed call."); |
| 182 | } |
| 183 | |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 184 | if (shouldDisallowCall && shouldScreenCallViaAudioProcessing) { |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 185 | throw new IllegalStateException("Invalid response state for allowed call."); |
| 186 | } |
| 187 | |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 188 | mShouldDisallowCall = shouldDisallowCall; |
| 189 | mShouldRejectCall = shouldRejectCall; |
| 190 | mShouldSkipCallLog = shouldSkipCallLog; |
| 191 | mShouldSkipNotification = shouldSkipNotification; |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 192 | mShouldSilenceCall = shouldSilenceCall; |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 193 | mShouldScreenCallViaAudioProcessing = shouldScreenCallViaAudioProcessing; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * @return Whether the incoming call should be blocked. |
| 198 | */ |
| 199 | public boolean getDisallowCall() { |
| 200 | return mShouldDisallowCall; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * @return Whether the incoming call should be disconnected as if the user had manually |
| 205 | * rejected it. |
| 206 | */ |
| 207 | public boolean getRejectCall() { |
| 208 | return mShouldRejectCall; |
| 209 | } |
| 210 | |
| 211 | /* |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 212 | * @return Whether the ringtone should be silenced for the incoming call. |
| 213 | */ |
| 214 | public boolean getSilenceCall() { |
| 215 | return mShouldSilenceCall; |
| 216 | } |
| 217 | |
| 218 | /* |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 219 | * @return Whether the incoming call should not be displayed in the call log. |
| 220 | */ |
| 221 | public boolean getSkipCallLog() { |
| 222 | return mShouldSkipCallLog; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * @return Whether a missed call notification should not be shown for the incoming call. |
| 227 | */ |
| 228 | public boolean getSkipNotification() { |
| 229 | return mShouldSkipNotification; |
| 230 | } |
| 231 | |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 232 | /** |
| 233 | * @return Whether we should enter the {@link Call#STATE_AUDIO_PROCESSING} state to allow |
| 234 | * for further screening of the call. |
| 235 | * @hide |
| 236 | */ |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 237 | public boolean getShouldScreenCallViaAudioProcessing() { |
| 238 | return mShouldScreenCallViaAudioProcessing; |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Sailesh Nepal | f446071 | 2016-01-27 16:45:51 -0800 | [diff] [blame] | 241 | public static class Builder { |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 242 | private boolean mShouldDisallowCall; |
| 243 | private boolean mShouldRejectCall; |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 244 | private boolean mShouldSilenceCall; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 245 | private boolean mShouldSkipCallLog; |
| 246 | private boolean mShouldSkipNotification; |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 247 | private boolean mShouldScreenCallViaAudioProcessing; |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 248 | |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 249 | /** |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 250 | * Sets whether the incoming call should be blocked. |
| 251 | */ |
| 252 | public Builder setDisallowCall(boolean shouldDisallowCall) { |
| 253 | mShouldDisallowCall = shouldDisallowCall; |
| 254 | return this; |
| 255 | } |
| 256 | |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 257 | /** |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 258 | * Sets whether the incoming call should be disconnected as if the user had manually |
| 259 | * rejected it. This property should only be set to true if the call is disallowed. |
| 260 | */ |
| 261 | public Builder setRejectCall(boolean shouldRejectCall) { |
| 262 | mShouldRejectCall = shouldRejectCall; |
| 263 | return this; |
| 264 | } |
| 265 | |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 266 | /** |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 267 | * Sets whether ringing should be silenced for the incoming call. When set |
| 268 | * to {@code true}, the Telecom framework will not play a ringtone for the call. |
| 269 | * The call will, however, still be sent to the default dialer app if it is not blocked. |
| 270 | * A {@link CallScreeningService} can use this to ensure a potential nuisance call is |
| 271 | * still surfaced to the user, but in a less intrusive manner. |
| 272 | * |
| 273 | * Setting this to true only makes sense when the call has not been disallowed |
| 274 | * using {@link #setDisallowCall(boolean)}. |
| 275 | */ |
| 276 | public @NonNull Builder setSilenceCall(boolean shouldSilenceCall) { |
| 277 | mShouldSilenceCall = shouldSilenceCall; |
| 278 | return this; |
| 279 | } |
| 280 | |
| 281 | /** |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 282 | * Sets whether the incoming call should not be displayed in the call log. This property |
| 283 | * should only be set to true if the call is disallowed. |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 284 | * <p> |
| 285 | * Note: Calls will still be logged with type |
| 286 | * {@link android.provider.CallLog.Calls#BLOCKED_TYPE}, regardless of how this property |
| 287 | * is set. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 288 | */ |
| 289 | public Builder setSkipCallLog(boolean shouldSkipCallLog) { |
| 290 | mShouldSkipCallLog = shouldSkipCallLog; |
| 291 | return this; |
| 292 | } |
| 293 | |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 294 | /** |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 295 | * Sets whether a missed call notification should not be shown for the incoming call. |
| 296 | * This property should only be set to true if the call is disallowed. |
| 297 | */ |
| 298 | public Builder setSkipNotification(boolean shouldSkipNotification) { |
| 299 | mShouldSkipNotification = shouldSkipNotification; |
| 300 | return this; |
| 301 | } |
| 302 | |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Sets whether to request background audio processing so that the in-call service can |
| 305 | * screen the call further. If set to {@code true}, {@link #setDisallowCall} should be |
| 306 | * called with {@code false}, and all other parameters in this builder will be ignored. |
Tyler Gunn | 460b7d4 | 2020-05-15 10:19:32 -0700 | [diff] [blame] | 307 | * <p> |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 308 | * This request will only be honored if the {@link CallScreeningService} shares the same |
| 309 | * uid as the default dialer app. Otherwise, the call will go through as usual. |
Tyler Gunn | 460b7d4 | 2020-05-15 10:19:32 -0700 | [diff] [blame] | 310 | * <p> |
| 311 | * Apps built with SDK version {@link android.os.Build.VERSION_CODES#R} or later which |
| 312 | * are using the microphone as part of audio processing should specify the |
| 313 | * foreground service type using the attribute |
| 314 | * {@link android.R.attr#foregroundServiceType} in the {@link CallScreeningService} |
| 315 | * service element of the app's manifest file. |
| 316 | * The {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_MICROPHONE} attribute should be |
| 317 | * specified. |
| 318 | * @see |
| 319 | * <a href="https://developer.android.com/preview/privacy/foreground-service-types"> |
| 320 | * the Android Developer Site</a> for more information. |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 321 | * |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 322 | * @param shouldScreenCallViaAudioProcessing Whether to request further call screening. |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 323 | * @hide |
| 324 | */ |
| 325 | @SystemApi |
| 326 | @TestApi |
Hall Liu | ff4a125 | 2020-01-01 16:27:14 -0800 | [diff] [blame] | 327 | @RequiresPermission(Manifest.permission.CAPTURE_AUDIO_OUTPUT) |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 328 | public @NonNull Builder setShouldScreenCallViaAudioProcessing( |
| 329 | boolean shouldScreenCallViaAudioProcessing) { |
| 330 | mShouldScreenCallViaAudioProcessing = shouldScreenCallViaAudioProcessing; |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 331 | return this; |
| 332 | } |
| 333 | |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 334 | public CallResponse build() { |
| 335 | return new CallResponse( |
| 336 | mShouldDisallowCall, |
| 337 | mShouldRejectCall, |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 338 | mShouldSilenceCall, |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 339 | mShouldSkipCallLog, |
Hall Liu | 6dfa249 | 2019-10-01 17:20:39 -0700 | [diff] [blame] | 340 | mShouldSkipNotification, |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 341 | mShouldScreenCallViaAudioProcessing); |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | public CallScreeningService() { |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public IBinder onBind(Intent intent) { |
| 351 | Log.v(this, "onBind"); |
| 352 | return new CallScreeningBinder(); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public boolean onUnbind(Intent intent) { |
| 357 | Log.v(this, "onUnbind"); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | /** |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 362 | * Called when a new incoming or outgoing call is added. |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 363 | * <p> |
| 364 | * A {@link CallScreeningService} must indicate whether an incoming call is allowed or not by |
| 365 | * calling |
| 366 | * {@link CallScreeningService#respondToCall(Call.Details, CallScreeningService.CallResponse)}. |
| 367 | * Your app can tell if a call is an incoming call by checking to see if |
| 368 | * {@link Call.Details#getCallDirection()} is {@link Call.Details#DIRECTION_INCOMING}. |
| 369 | * <p> |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 370 | * <em>Note:</em> A {@link CallScreeningService} must respond to a call within 5 seconds. After |
| 371 | * this time, the framework will unbind from the {@link CallScreeningService} and ignore its |
| 372 | * response. |
| 373 | * <p> |
| 374 | * <em>Note:</em> The {@link Call.Details} instance provided to a call screening service will |
| 375 | * only have the following properties set. The rest of the {@link Call.Details} properties will |
| 376 | * be set to their default value or {@code null}. |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 377 | * <ul> |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 378 | * <li>{@link Call.Details#getCallDirection()}</li> |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 379 | * <li>{@link Call.Details#getCallerNumberVerificationStatus()}</li> |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 380 | * <li>{@link Call.Details#getConnectTimeMillis()}</li> |
| 381 | * <li>{@link Call.Details#getCreationTimeMillis()}</li> |
| 382 | * <li>{@link Call.Details#getHandle()}</li> |
Tyler Gunn | e0caec7 | 2018-11-30 14:21:18 -0800 | [diff] [blame] | 383 | * </ul> |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 384 | * <p> |
| 385 | * Only calls where the {@link Call.Details#getHandle() handle} {@link Uri#getScheme() scheme} |
| 386 | * is {@link PhoneAccount#SCHEME_TEL} are passed for call |
| 387 | * screening. Further, only calls which are not in the user's contacts are passed for |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 388 | * screening, unless the {@link CallScreeningService} has been granted |
| 389 | * {@link Manifest.permission#READ_CONTACTS} permission by the user. For outgoing calls, no |
| 390 | * post-dial digits are passed. |
| 391 | * <p> |
| 392 | * Calls with a {@link Call.Details#getHandlePresentation()} of |
| 393 | * {@link TelecomManager#PRESENTATION_RESTRICTED}, {@link TelecomManager#PRESENTATION_UNKNOWN} |
| 394 | * or {@link TelecomManager#PRESENTATION_PAYPHONE} presentation are not provided to the |
| 395 | * {@link CallScreeningService}. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 396 | * |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 397 | * @param callDetails Information about a new call, see {@link Call.Details}. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 398 | */ |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 399 | public abstract void onScreenCall(@NonNull Call.Details callDetails); |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 400 | |
| 401 | /** |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 402 | * Responds to the given incoming call, either allowing it, silencing it or disallowing it. |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 403 | * <p> |
| 404 | * The {@link CallScreeningService} calls this method to inform the system whether the call |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 405 | * should be silently blocked or not. In the event that it should not be blocked, it may |
| 406 | * also be requested to ring silently. |
Tyler Gunn | 9e76fd19b | 2018-12-17 09:56:11 -0800 | [diff] [blame] | 407 | * <p> |
| 408 | * Calls to this method are ignored unless the {@link Call.Details#getCallDirection()} is |
| 409 | * {@link Call.Details#DIRECTION_INCOMING}. |
Tyler Gunn | 467acc4 | 2020-10-07 15:42:06 -0700 | [diff] [blame^] | 410 | * <p> |
| 411 | * For incoming calls, a {@link CallScreeningService} MUST call this method within 5 seconds of |
| 412 | * {@link #onScreenCall(Call.Details)} being invoked by the platform. |
| 413 | * <p> |
| 414 | * Calls which are blocked/rejected will be logged to the system call log with a call type of |
| 415 | * {@link android.provider.CallLog.Calls#BLOCKED_TYPE} and |
| 416 | * {@link android.provider.CallLog.Calls#BLOCK_REASON_CALL_SCREENING_SERVICE} block reason. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 417 | * |
| 418 | * @param callDetails The call to allow. |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 419 | * <p> |
| 420 | * Must be the same {@link Call.Details call} which was provided to the |
| 421 | * {@link CallScreeningService} via {@link #onScreenCall(Call.Details)}. |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 422 | * @param response The {@link CallScreeningService.CallResponse} which contains information |
| 423 | * about how to respond to a call. |
| 424 | */ |
Tyler Gunn | 7e45b72 | 2018-12-04 12:56:45 -0800 | [diff] [blame] | 425 | public final void respondToCall(@NonNull Call.Details callDetails, |
| 426 | @NonNull CallResponse response) { |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 427 | try { |
| 428 | if (response.getDisallowCall()) { |
| 429 | mCallScreeningAdapter.disallowCall( |
| 430 | callDetails.getTelecomCallId(), |
| 431 | response.getRejectCall(), |
| 432 | !response.getSkipCallLog(), |
tonyzhu | 9e1d4f8 | 2018-10-22 15:11:31 +0800 | [diff] [blame] | 433 | !response.getSkipNotification(), |
| 434 | new ComponentName(getPackageName(), getClass().getName())); |
Usman Abdullah | 47b392d | 2019-03-06 15:54:56 -0800 | [diff] [blame] | 435 | } else if (response.getSilenceCall()) { |
| 436 | mCallScreeningAdapter.silenceCall(callDetails.getTelecomCallId()); |
Hall Liu | 69554cf | 2019-11-11 17:44:09 -0800 | [diff] [blame] | 437 | } else if (response.getShouldScreenCallViaAudioProcessing()) { |
Hall Liu | 31de23d | 2019-10-11 15:38:29 -0700 | [diff] [blame] | 438 | mCallScreeningAdapter.screenCallFurther(callDetails.getTelecomCallId()); |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 439 | } else { |
| 440 | mCallScreeningAdapter.allowCall(callDetails.getTelecomCallId()); |
| 441 | } |
| 442 | } catch (RemoteException e) { |
| 443 | } |
| 444 | } |
Sailesh Nepal | 1bef339 | 2016-01-24 18:21:53 -0800 | [diff] [blame] | 445 | } |