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