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