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