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