Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 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.telecomm; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.ActivityManagerNative; |
| 21 | import android.app.AlertDialog; |
| 22 | import android.app.AppOpsManager; |
| 23 | import android.app.Dialog; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.Intent; |
| 28 | import android.content.res.Configuration; |
| 29 | import android.content.res.Resources; |
| 30 | import android.database.Cursor; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Binder; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
| 36 | import android.os.RemoteException; |
| 37 | import android.os.SystemProperties; |
| 38 | import android.os.UserHandle; |
| 39 | import android.provider.ContactsContract; |
| 40 | import android.telecomm.GatewayInfo; |
| 41 | import android.telecomm.TelecommConstants; |
| 42 | import android.telephony.PhoneNumberUtils; |
| 43 | import android.telephony.TelephonyManager; |
| 44 | import android.text.TextUtils; |
| 45 | import android.view.View; |
| 46 | import android.widget.ProgressBar; |
| 47 | |
| 48 | /** |
| 49 | * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the |
| 50 | * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which |
| 51 | * contains the phone number being dialed. Applications can use this intent to (1) see which numbers |
| 52 | * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call |
| 53 | * from being placed. |
| 54 | * |
| 55 | * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it |
| 56 | * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}. |
| 57 | * |
| 58 | * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail |
| 59 | * number) are exempt from being broadcast. |
| 60 | * |
| 61 | * Calls to emergency numbers are still broadcast for informative purposes. The call is placed |
| 62 | * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented. |
| 63 | */ |
| 64 | class NewOutgoingCallIntentBroadcaster { |
| 65 | /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */ |
| 66 | private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS; |
| 67 | |
| 68 | private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL = |
| 69 | "android.telecomm.extra.ACTUAL_NUMBER_TO_DIAL"; |
| 70 | |
| 71 | /** |
| 72 | * Legacy string constants used to retrieve gateway provider extras from intents. These still |
| 73 | * need to be copied from the source call intent to the destination intent in order to |
| 74 | * support third party gateway providers that are still using old string constants in |
| 75 | * Telephony. |
| 76 | */ |
| 77 | public static final String EXTRA_GATEWAY_PROVIDER_PACKAGE = |
| 78 | "com.android.phone.extra.GATEWAY_PROVIDER_PACKAGE"; |
| 79 | public static final String EXTRA_GATEWAY_URI = "com.android.phone.extra.GATEWAY_URI"; |
Santos Cordon | 571f073 | 2014-06-25 18:13:15 -0700 | [diff] [blame^] | 80 | public static final String EXTRA_GATEWAY_ORIGINAL_URI = |
| 81 | "com.android.phone.extra.GATEWAY_ORIGINAL_URI"; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 82 | |
Yorke Lee | b9d4b36 | 2014-03-24 17:46:03 -0700 | [diff] [blame] | 83 | private static final String SCHEME_TEL = "tel"; |
| 84 | private static final String SCHEME_SIP = "sip"; |
| 85 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 86 | private final CallsManager mCallsManager; |
| 87 | private final ContactInfo mContactInfo; |
| 88 | private final Intent mIntent; |
| 89 | |
| 90 | NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo, |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 91 | Intent intent) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 92 | mCallsManager = callsManager; |
| 93 | mContactInfo = contactInfo; |
| 94 | mIntent = intent; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Processes the result of the outgoing call broadcast intent, and performs callbacks to |
| 99 | * the OutgoingCallIntentBroadcasterListener as necessary. |
| 100 | */ |
| 101 | private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver { |
| 102 | |
| 103 | @Override |
| 104 | public void onReceive(Context context, Intent intent) { |
Sailesh Nepal | b330875 | 2014-04-07 14:12:47 -0700 | [diff] [blame] | 105 | Log.v(this, "onReceive: %s", intent); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 106 | |
| 107 | // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the |
| 108 | // actual number to call. (If null, no call will be placed.) |
| 109 | String resultHandle = getResultData(); |
| 110 | Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle)); |
| 111 | |
| 112 | if (resultHandle == null) { |
| 113 | Log.v(this, "Call cancelled (null number), returning..."); |
| 114 | return; |
Yorke Lee | 6625545 | 2014-06-05 08:09:24 -0700 | [diff] [blame] | 115 | } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(context, resultHandle)) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 116 | Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle); |
| 117 | return; |
| 118 | } |
| 119 | |
Yorke Lee | b9d4b36 | 2014-03-24 17:46:03 -0700 | [diff] [blame] | 120 | Uri resultHandleUri = Uri.fromParts( |
| 121 | PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL, |
| 122 | resultHandle, |
| 123 | null); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 124 | |
| 125 | Uri originalUri = mIntent.getData(); |
| 126 | |
| 127 | if (originalUri.getSchemeSpecificPart().equals(resultHandle)) { |
| 128 | Log.v(this, "Call handle unmodified after new outgoing call intent broadcast."); |
| 129 | } else { |
| 130 | Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: " |
| 131 | + "Original: %s, Modified: %s", |
| 132 | Log.pii(originalUri), |
| 133 | Log.pii(resultHandleUri)); |
| 134 | } |
| 135 | |
| 136 | GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 137 | mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo, |
| 138 | mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE, |
| 139 | false)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Processes the supplied intent and starts the outgoing call broadcast process relevant to the |
| 145 | * intent. |
| 146 | * |
| 147 | * This method will handle three kinds of actions: |
| 148 | * |
| 149 | * - CALL (intent launched by all third party dialers) |
| 150 | * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer) |
| 151 | * - CALL_EMERGENCY (intent launched by lock screen emergency dialer) |
| 152 | */ |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 153 | void processIntent() { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 154 | Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster."); |
| 155 | |
| 156 | final Context context = TelecommApp.getInstance(); |
| 157 | Intent intent = mIntent; |
| 158 | |
| 159 | String handle = PhoneNumberUtils.getNumberFromIntent(intent, context); |
| 160 | |
| 161 | if (TextUtils.isEmpty(handle)) { |
| 162 | Log.w(this, "Empty handle obtained from the call intent."); |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 163 | return; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 166 | boolean isUriNumber = PhoneNumberUtils.isUriNumber(handle); |
| 167 | |
| 168 | if (!isUriNumber) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 169 | handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle); |
| 170 | handle = PhoneNumberUtils.stripSeparators(handle); |
| 171 | } |
| 172 | |
| 173 | final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle); |
| 174 | Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber); |
| 175 | |
| 176 | rewriteCallIntentAction(intent, isPotentialEmergencyNumber); |
| 177 | // True for certain types of numbers that are not intended to be intercepted or modified |
| 178 | // by third parties (e.g. emergency numbers). |
| 179 | boolean callImmediately = false; |
| 180 | |
| 181 | String action = intent.getAction(); |
| 182 | if (Intent.ACTION_CALL.equals(action)) { |
| 183 | if (isPotentialEmergencyNumber) { |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 184 | Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.", |
| 185 | handle, intent); |
| 186 | launchSystemDialer(context, intent.getData()); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 187 | } |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 188 | callImmediately = false; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 189 | } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) { |
| 190 | if (!isPotentialEmergencyNumber) { |
| 191 | Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL " |
| 192 | + "Intent %s.", handle, intent); |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 193 | return; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 194 | } |
| 195 | callImmediately = true; |
| 196 | } else { |
| 197 | Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent); |
Yorke Lee | d362fe3 | 2014-06-19 22:24:28 +0000 | [diff] [blame] | 198 | return; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (callImmediately) { |
| 202 | Log.i(this, "Placing call immediately instead of waiting for " |
| 203 | + " OutgoingCallBroadcastReceiver: %s", intent); |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 204 | String scheme = isUriNumber ? SCHEME_SIP : SCHEME_TEL; |
| 205 | mCallsManager.placeOutgoingCall( |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 206 | Uri.fromParts(scheme, handle, null), mContactInfo, null, |
| 207 | mIntent.getBooleanExtra(TelecommConstants.EXTRA_START_CALL_WITH_SPEAKERPHONE, |
| 208 | false)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 209 | |
| 210 | // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast |
| 211 | // so that third parties can still inspect (but not intercept) the outgoing call. When |
| 212 | // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to |
| 213 | // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra. |
| 214 | } |
| 215 | |
| 216 | broadcastIntent(intent, handle, context, !callImmediately); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Sends a new outgoing call ordered broadcast so that third party apps can cancel the |
| 221 | * placement of the call or redirect it to a different number. |
| 222 | * |
| 223 | * @param originalCallIntent The original call intent. |
| 224 | * @param handle Call handle that was stored in the original call intent. |
| 225 | * @param context Valid context to send the ordered broadcast using. |
| 226 | * @param receiverRequired Whether or not the result from the ordered broadcast should be |
| 227 | * processed using a {@link NewOutgoingCallIntentBroadcaster}. |
| 228 | */ |
| 229 | private void broadcastIntent( |
| 230 | Intent originalCallIntent, |
| 231 | String handle, |
| 232 | Context context, |
| 233 | boolean receiverRequired) { |
| 234 | Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL); |
| 235 | if (handle != null) { |
| 236 | broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle); |
| 237 | } |
| 238 | |
| 239 | // Force receivers of this broadcast intent to run at foreground priority because we |
| 240 | // want to finish processing the broadcast intent as soon as possible. |
| 241 | broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 242 | Log.v(this, "Broadcasting intent: %s.", broadcastIntent); |
| 243 | |
| 244 | checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent); |
| 245 | |
| 246 | context.sendOrderedBroadcastAsUser( |
| 247 | broadcastIntent, |
| 248 | UserHandle.OWNER, |
| 249 | PERMISSION, |
| 250 | receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null, |
| 251 | null, // scheduler |
| 252 | Activity.RESULT_OK, // initialCode |
| 253 | handle, // initialData: initial value for the result data (number to be modified) |
| 254 | null); // initialExtras |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the |
| 259 | * source intent to the destination one. |
| 260 | * |
| 261 | * @param src Intent which may contain the provider's extras. |
| 262 | * @param dst Intent where a copy of the extras will be added if applicable. |
| 263 | */ |
| 264 | public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) { |
| 265 | if (hasGatewayProviderExtras(src)) { |
| 266 | dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE, |
| 267 | src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE)); |
| 268 | dst.putExtra(EXTRA_GATEWAY_URI, |
| 269 | src.getStringExtra(EXTRA_GATEWAY_URI)); |
| 270 | Log.d(this, "Found and copied gateway provider extras to broadcast intent."); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | Log.d(this, "No gateway provider extras found in call intent."); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Check if valid gateway provider information is stored as extras in the intent |
| 279 | * |
| 280 | * @param intent to check for |
| 281 | * @return true if the intent has all the gateway information extras needed. |
| 282 | */ |
| 283 | private boolean hasGatewayProviderExtras(Intent intent) { |
| 284 | if (intent == null) { |
| 285 | return false; |
| 286 | } |
| 287 | final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); |
| 288 | final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI); |
| 289 | |
| 290 | return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString); |
| 291 | } |
| 292 | |
| 293 | private static Uri getGatewayUriFromString(String gatewayUriString) { |
| 294 | return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Extracts gateway provider information from a provided intent.. |
| 299 | * |
| 300 | * @param intent to extract gateway provider information from. |
| 301 | * @param trueHandle The actual call handle that the user is trying to dial |
| 302 | * @return GatewayInfo object containing extracted gateway provider information as well as |
| 303 | * the actual handle the user is trying to dial. |
| 304 | */ |
| 305 | public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) { |
| 306 | if (intent == null) { |
| 307 | return null; |
| 308 | } |
| 309 | |
| 310 | // Check if gateway extras are present. |
| 311 | String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); |
| 312 | Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI)); |
| 313 | if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) { |
| 314 | return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle); |
| 315 | } |
| 316 | |
| 317 | return null; |
| 318 | } |
| 319 | |
| 320 | private void launchSystemDialer(Context context, Uri handle) { |
| 321 | Intent systemDialerIntent = new Intent(); |
| 322 | final Resources resources = context.getResources(); |
| 323 | systemDialerIntent.setClassName( |
| 324 | resources.getString(R.string.ui_default_package), |
| 325 | resources.getString(R.string.dialer_default_class)); |
| 326 | systemDialerIntent.setAction(Intent.ACTION_DIAL); |
| 327 | systemDialerIntent.setData(handle); |
| 328 | systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 329 | Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent); |
| 330 | context.startActivity(systemDialerIntent); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Check whether or not this is an emergency number, in order to enforce the restriction |
| 335 | * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency |
| 336 | * calls. |
| 337 | * |
| 338 | * To prevent malicious 3rd party apps from making emergency calls by passing in an |
| 339 | * "invalid" number like "9111234" (that isn't technically an emergency number but might |
| 340 | * still result in an emergency call with some networks), we use |
| 341 | * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber. |
| 342 | * |
| 343 | * @param context Valid context |
| 344 | * @param handle Handle to inspect in order to determine whether or not an emergency number |
| 345 | * is potentially being dialed |
| 346 | * @return True if the handle is potentially an emergency number. |
| 347 | */ |
| 348 | private boolean isPotentialEmergencyNumber(Context context, String handle) { |
| 349 | Log.v(this, "Checking restrictions for number : %s", Log.pii(handle)); |
Yorke Lee | 6625545 | 2014-06-05 08:09:24 -0700 | [diff] [blame] | 350 | return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(context,handle); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Given a call intent and whether or not the number to dial is an emergency number, rewrite |
| 355 | * the call intent action to an appropriate one. |
| 356 | * |
| 357 | * @param intent Intent to rewrite the action for |
| 358 | * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency |
| 359 | * number. |
| 360 | */ |
| 361 | private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) { |
| 362 | if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) { |
| 363 | // If we were launched directly from the CallActivity, not one of its more privileged |
| 364 | // aliases, then make sure that only the non-privileged actions are allowed. |
| 365 | if (!Intent.ACTION_CALL.equals(intent.getAction())) { |
| 366 | Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL"); |
| 367 | intent.setAction(Intent.ACTION_CALL); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | String action = intent.getAction(); |
| 372 | |
| 373 | /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */ |
| 374 | if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) { |
| 375 | if (isPotentialEmergencyNumber) { |
| 376 | Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential" |
| 377 | + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead."); |
| 378 | action = Intent.ACTION_CALL_EMERGENCY; |
| 379 | } else { |
| 380 | action = Intent.ACTION_CALL; |
| 381 | } |
| 382 | Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action); |
| 383 | intent.setAction(action); |
| 384 | } |
| 385 | } |
| 386 | } |