1 /* 2 * Copyright (C) 2014 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 com.android.server.telecom; 18 19 import android.app.AppOpsManager; 20 21 import android.app.Activity; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.res.Resources; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.Trace; 29 import android.os.UserHandle; 30 import android.telecom.GatewayInfo; 31 import android.telecom.Log; 32 import android.telecom.PhoneAccount; 33 import android.telecom.PhoneAccountHandle; 34 import android.telecom.TelecomManager; 35 import android.telecom.VideoProfile; 36 import android.telephony.DisconnectCause; 37 import android.text.TextUtils; 38 39 import com.android.internal.annotations.VisibleForTesting; 40 41 // TODO: Needed for move to system service: import com.android.internal.R; 42 43 /** 44 * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the 45 * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which 46 * contains the phone number being dialed. Applications can use this intent to (1) see which numbers 47 * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call 48 * from being placed. 49 * 50 * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it 51 * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}. 52 * 53 * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail 54 * number) are exempt from being broadcast. 55 * 56 * Calls to emergency numbers are still broadcast for informative purposes. The call is placed 57 * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented. 58 */ 59 @VisibleForTesting 60 public class NewOutgoingCallIntentBroadcaster { 61 /** 62 * Legacy string constants used to retrieve gateway provider extras from intents. These still 63 * need to be copied from the source call intent to the destination intent in order to 64 * support third party gateway providers that are still using old string constants in 65 * Telephony. 66 */ 67 public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE = 68 "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE"; 69 public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI"; 70 71 private final CallsManager mCallsManager; 72 private final Call mCall; 73 private final Intent mIntent; 74 private final Context mContext; 75 private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter; 76 private final TelecomSystem.SyncRoot mLock; 77 78 /* 79 * Whether or not the outgoing call intent originated from the default phone application. If 80 * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent. 81 */ 82 private final boolean mIsDefaultOrSystemPhoneApp; 83 84 @VisibleForTesting 85 public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call, 86 Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter, 87 boolean isDefaultPhoneApp) { 88 mContext = context; 89 mCallsManager = callsManager; 90 mCall = call; 91 mIntent = intent; 92 mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter; 93 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp; 94 mLock = mCallsManager.getLock(); 95 } 96 97 /** 98 * Processes the result of the outgoing call broadcast intent, and performs callbacks to 99 * the OutgoingCallIntentBroadcasterListener as necessary. 100 */ 101 public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver { 102 103 @Override 104 public void onReceive(Context context, Intent intent) { 105 try { 106 Log.startSession("NOCBIR.oR"); 107 Trace.beginSection("onReceiveNewOutgoingCallBroadcast"); 108 synchronized (mLock) { 109 Log.v(this, "onReceive: %s", intent); 110 111 // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is 112 // used as the actual number to call. (If null, no call will be placed.) 113 String resultNumber = getResultData(); 114 Log.i(this, "Received new-outgoing-call-broadcast for %s with data %s", mCall, 115 Log.pii(resultNumber)); 116 117 boolean endEarly = false; 118 long disconnectTimeout = 119 Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()); 120 if (resultNumber == null) { 121 Log.v(this, "Call cancelled (null number), returning..."); 122 disconnectTimeout = getDisconnectTimeoutFromApp( 123 getResultExtras(false), disconnectTimeout); 124 endEarly = true; 125 } else if (mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber( 126 mContext, resultNumber)) { 127 Log.w(this, "Cannot modify outgoing call to emergency number %s.", 128 resultNumber); 129 disconnectTimeout = 0; 130 endEarly = true; 131 } 132 133 if (endEarly) { 134 if (mCall != null) { 135 mCall.disconnect(disconnectTimeout); 136 } 137 return; 138 } 139 140 // If this call is already disconnected then we have nothing more to do. 141 if (mCall.isDisconnected()) { 142 Log.w(this, "Call has already been disconnected," + 143 " ignore the broadcast Call %s", mCall); 144 return; 145 } 146 147 Uri resultHandleUri = Uri.fromParts( 148 mPhoneNumberUtilsAdapter.isUriNumber(resultNumber) ? 149 PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL, 150 resultNumber, null); 151 152 Uri originalUri = mIntent.getData(); 153 154 if (originalUri.getSchemeSpecificPart().equals(resultNumber)) { 155 Log.v(this, "Call number unmodified after" + 156 " new outgoing call intent broadcast."); 157 } else { 158 Log.v(this, "Retrieved modified handle after outgoing call intent" + 159 " broadcast: Original: %s, Modified: %s", 160 Log.pii(originalUri), 161 Log.pii(resultHandleUri)); 162 } 163 164 GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri); 165 mCall.setNewOutgoingCallIntentBroadcastIsDone(); 166 mCallsManager.placeOutgoingCall(mCall, resultHandleUri, gatewayInfo, 167 mIntent.getBooleanExtra( 168 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false), 169 mIntent.getIntExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, 170 VideoProfile.STATE_AUDIO_ONLY)); 171 } 172 } finally { 173 Trace.endSection(); 174 Log.endSession(); 175 } 176 } 177 } 178 179 /** 180 * Processes the supplied intent and starts the outgoing call broadcast process relevant to the 181 * intent. 182 * 183 * This method will handle three kinds of actions: 184 * 185 * - CALL (intent launched by all third party dialers) 186 * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer) 187 * - CALL_EMERGENCY (intent launched by lock screen emergency dialer) 188 * 189 * @return {@link DisconnectCause#NOT_DISCONNECTED} if the call succeeded, and an appropriate 190 * {@link DisconnectCause} if the call did not, describing why it failed. 191 */ 192 @VisibleForTesting 193 public int processIntent() { 194 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster."); 195 196 Intent intent = mIntent; 197 String action = intent.getAction(); 198 final Uri handle = intent.getData(); 199 200 if (handle == null) { 201 Log.w(this, "Empty handle obtained from the call intent."); 202 return DisconnectCause.INVALID_NUMBER; 203 } 204 205 boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme()); 206 if (isVoicemailNumber) { 207 if (Intent.ACTION_CALL.equals(action) 208 || Intent.ACTION_CALL_PRIVILEGED.equals(action)) { 209 // Voicemail calls will be handled directly by the telephony connection manager 210 Log.i(this, "Placing call immediately instead of waiting for " 211 + " OutgoingCallBroadcastReceiver: %s", intent); 212 213 // Since we are not going to go through "Outgoing call broadcast", make sure 214 // we mark it as ready. 215 mCall.setNewOutgoingCallIntentBroadcastIsDone(); 216 217 boolean speakerphoneOn = mIntent.getBooleanExtra( 218 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false); 219 mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn, 220 VideoProfile.STATE_AUDIO_ONLY); 221 222 return DisconnectCause.NOT_DISCONNECTED; 223 } else { 224 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent); 225 return DisconnectCause.OUTGOING_CANCELED; 226 } 227 } 228 229 String number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext); 230 if (TextUtils.isEmpty(number)) { 231 Log.w(this, "Empty number obtained from the call intent."); 232 return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED; 233 } 234 235 boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number); 236 if (!isUriNumber) { 237 number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number); 238 number = mPhoneNumberUtilsAdapter.stripSeparators(number); 239 } 240 241 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number); 242 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber); 243 244 rewriteCallIntentAction(intent, isPotentialEmergencyNumber); 245 action = intent.getAction(); 246 // True for certain types of numbers that are not intended to be intercepted or modified 247 // by third parties (e.g. emergency numbers). 248 boolean callImmediately = false; 249 250 if (Intent.ACTION_CALL.equals(action)) { 251 if (isPotentialEmergencyNumber) { 252 if (!mIsDefaultOrSystemPhoneApp) { 253 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s " 254 + "unless caller is system or default dialer.", number, intent); 255 launchSystemDialer(intent.getData()); 256 return DisconnectCause.OUTGOING_CANCELED; 257 } else { 258 callImmediately = true; 259 } 260 } 261 } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) { 262 if (!isPotentialEmergencyNumber) { 263 Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL " 264 + "Intent %s.", number, intent); 265 return DisconnectCause.OUTGOING_CANCELED; 266 } 267 callImmediately = true; 268 } else { 269 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent); 270 return DisconnectCause.INVALID_NUMBER; 271 } 272 273 // True for all managed calls, false for self-managed calls. 274 boolean sendNewOutgoingCallBroadcast = true; 275 PhoneAccountHandle targetPhoneAccount = mIntent.getParcelableExtra( 276 TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE); 277 if (targetPhoneAccount != null) { 278 PhoneAccount phoneAccount = 279 mCallsManager.getPhoneAccountRegistrar().getPhoneAccountUnchecked( 280 targetPhoneAccount); 281 if (phoneAccount != null && phoneAccount.isSelfManaged()) { 282 callImmediately = true; 283 sendNewOutgoingCallBroadcast = false; 284 Log.i(this, "Skipping NewOutgoingCallBroadcast for self-managed call."); 285 } 286 } 287 288 if (callImmediately) { 289 Log.i(this, "Placing call immediately instead of waiting for " 290 + " OutgoingCallBroadcastReceiver: %s", intent); 291 String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL; 292 boolean speakerphoneOn = mIntent.getBooleanExtra( 293 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false); 294 int videoState = mIntent.getIntExtra( 295 TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, 296 VideoProfile.STATE_AUDIO_ONLY); 297 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null, 298 speakerphoneOn, videoState); 299 300 // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast 301 // so that third parties can still inspect (but not intercept) the outgoing call. When 302 // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to 303 // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra. 304 } 305 306 if (sendNewOutgoingCallBroadcast) { 307 UserHandle targetUser = mCall.getInitiatingUser(); 308 Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser); 309 broadcastIntent(intent, number, !callImmediately, targetUser); 310 } 311 return DisconnectCause.NOT_DISCONNECTED; 312 } 313 314 /** 315 * Sends a new outgoing call ordered broadcast so that third party apps can cancel the 316 * placement of the call or redirect it to a different number. 317 * 318 * @param originalCallIntent The original call intent. 319 * @param number Call number that was stored in the original call intent. 320 * @param receiverRequired Whether or not the result from the ordered broadcast should be 321 * processed using a {@link NewOutgoingCallIntentBroadcaster}. 322 * @param targetUser User that the broadcast sent to. 323 */ 324 private void broadcastIntent( 325 Intent originalCallIntent, 326 String number, 327 boolean receiverRequired, 328 UserHandle targetUser) { 329 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL); 330 if (number != null) { 331 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number); 332 } 333 334 // Force receivers of this broadcast intent to run at foreground priority because we 335 // want to finish processing the broadcast intent as soon as possible. 336 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND 337 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); 338 Log.v(this, "Broadcasting intent: %s.", broadcastIntent); 339 340 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent); 341 342 mContext.sendOrderedBroadcastAsUser( 343 broadcastIntent, 344 targetUser, 345 android.Manifest.permission.PROCESS_OUTGOING_CALLS, 346 AppOpsManager.OP_PROCESS_OUTGOING_CALLS, 347 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null, 348 null, // scheduler 349 Activity.RESULT_OK, // initialCode 350 number, // initialData: initial value for the result data (number to be modified) 351 null); // initialExtras 352 } 353 354 /** 355 * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the 356 * source intent to the destination one. 357 * 358 * @param src Intent which may contain the provider's extras. 359 * @param dst Intent where a copy of the extras will be added if applicable. 360 */ 361 public void checkAndCopyProviderExtras(Intent src, Intent dst) { 362 if (src == null) { 363 return; 364 } 365 if (hasGatewayProviderExtras(src)) { 366 dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE, 367 src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE)); 368 dst.putExtra(EXTRA_GATEWAY_URI, 369 src.getStringExtra(EXTRA_GATEWAY_URI)); 370 Log.d(this, "Found and copied gateway provider extras to broadcast intent."); 371 return; 372 } 373 374 Log.d(this, "No provider extras found in call intent."); 375 } 376 377 /** 378 * Check if valid gateway provider information is stored as extras in the intent 379 * 380 * @param intent to check for 381 * @return true if the intent has all the gateway information extras needed. 382 */ 383 private boolean hasGatewayProviderExtras(Intent intent) { 384 final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); 385 final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI); 386 387 return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString); 388 } 389 390 private static Uri getGatewayUriFromString(String gatewayUriString) { 391 return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString); 392 } 393 394 /** 395 * Extracts gateway provider information from a provided intent.. 396 * 397 * @param intent to extract gateway provider information from. 398 * @param trueHandle The actual call handle that the user is trying to dial 399 * @return GatewayInfo object containing extracted gateway provider information as well as 400 * the actual handle the user is trying to dial. 401 */ 402 public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) { 403 if (intent == null) { 404 return null; 405 } 406 407 // Check if gateway extras are present. 408 String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); 409 Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI)); 410 if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) { 411 return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle); 412 } 413 414 return null; 415 } 416 417 private void launchSystemDialer(Uri handle) { 418 Intent systemDialerIntent = new Intent(); 419 final Resources resources = mContext.getResources(); 420 systemDialerIntent.setClassName( 421 resources.getString(R.string.ui_default_package), 422 resources.getString(R.string.dialer_default_class)); 423 systemDialerIntent.setAction(Intent.ACTION_DIAL); 424 systemDialerIntent.setData(handle); 425 systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 426 Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent); 427 mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT); 428 } 429 430 /** 431 * Check whether or not this is an emergency number, in order to enforce the restriction 432 * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency 433 * calls. 434 * 435 * To prevent malicious 3rd party apps from making emergency calls by passing in an 436 * "invalid" number like "9111234" (that isn't technically an emergency number but might 437 * still result in an emergency call with some networks), we use 438 * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber. 439 * 440 * @param number number to inspect in order to determine whether or not an emergency number 441 * is potentially being dialed 442 * @return True if the handle is potentially an emergency number. 443 */ 444 private boolean isPotentialEmergencyNumber(String number) { 445 Log.v(this, "Checking restrictions for number : %s", Log.pii(number)); 446 return (number != null) 447 && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number); 448 } 449 450 /** 451 * Given a call intent and whether or not the number to dial is an emergency number, rewrite 452 * the call intent action to an appropriate one. 453 * 454 * @param intent Intent to rewrite the action for 455 * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency 456 * number. 457 */ 458 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) { 459 String action = intent.getAction(); 460 461 /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */ 462 if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) { 463 if (isPotentialEmergencyNumber) { 464 Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential" 465 + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead."); 466 action = Intent.ACTION_CALL_EMERGENCY; 467 } else { 468 action = Intent.ACTION_CALL; 469 } 470 Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action); 471 intent.setAction(action); 472 } 473 } 474 475 private long getDisconnectTimeoutFromApp(Bundle resultExtras, long defaultTimeout) { 476 if (resultExtras != null) { 477 long disconnectTimeout = resultExtras.getLong( 478 TelecomManager.EXTRA_NEW_OUTGOING_CALL_CANCEL_TIMEOUT, defaultTimeout); 479 if (disconnectTimeout < 0) { 480 disconnectTimeout = 0; 481 } 482 return Math.min(disconnectTimeout, 483 Timeouts.getMaxNewOutgoingCallCancelMillis(mContext.getContentResolver())); 484 } else { 485 return defaultTimeout; 486 } 487 } 488 } 489