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"; |
| 80 | |
Yorke Lee | b9d4b36 | 2014-03-24 17:46:03 -0700 | [diff] [blame^] | 81 | private static final String SCHEME_TEL = "tel"; |
| 82 | private static final String SCHEME_SIP = "sip"; |
| 83 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 84 | private final CallsManager mCallsManager; |
| 85 | private final ContactInfo mContactInfo; |
| 86 | private final Intent mIntent; |
| 87 | |
| 88 | NewOutgoingCallIntentBroadcaster(CallsManager callsManager, ContactInfo contactInfo, |
| 89 | Intent intent) { |
| 90 | mCallsManager = callsManager; |
| 91 | mContactInfo = contactInfo; |
| 92 | mIntent = intent; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Processes the result of the outgoing call broadcast intent, and performs callbacks to |
| 97 | * the OutgoingCallIntentBroadcasterListener as necessary. |
| 98 | */ |
| 99 | private class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver { |
| 100 | |
| 101 | @Override |
| 102 | public void onReceive(Context context, Intent intent) { |
| 103 | Log.v(this, "doReceive: %s"); |
| 104 | |
| 105 | // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is used as the |
| 106 | // actual number to call. (If null, no call will be placed.) |
| 107 | String resultHandle = getResultData(); |
| 108 | Log.v(this, "- got number from resultData: %s", Log.pii(resultHandle)); |
| 109 | |
| 110 | if (resultHandle == null) { |
| 111 | Log.v(this, "Call cancelled (null number), returning..."); |
| 112 | return; |
| 113 | } else if (PhoneNumberUtils.isPotentialLocalEmergencyNumber(resultHandle, context)) { |
| 114 | Log.w(this, "Cannot modify outgoing call to emergency number %s.", resultHandle); |
| 115 | return; |
| 116 | } |
| 117 | |
Yorke Lee | b9d4b36 | 2014-03-24 17:46:03 -0700 | [diff] [blame^] | 118 | Uri resultHandleUri = Uri.fromParts( |
| 119 | PhoneNumberUtils.isUriNumber(resultHandle) ? SCHEME_SIP : SCHEME_TEL, |
| 120 | resultHandle, |
| 121 | null); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 122 | |
| 123 | Uri originalUri = mIntent.getData(); |
| 124 | |
| 125 | if (originalUri.getSchemeSpecificPart().equals(resultHandle)) { |
| 126 | Log.v(this, "Call handle unmodified after new outgoing call intent broadcast."); |
| 127 | } else { |
| 128 | Log.v(this, "Retrieved modified handle after outgoing call intent broadcast: " |
| 129 | + "Original: %s, Modified: %s", |
| 130 | Log.pii(originalUri), |
| 131 | Log.pii(resultHandleUri)); |
| 132 | } |
| 133 | |
| 134 | GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri); |
| 135 | mCallsManager.placeOutgoingCall(resultHandleUri, mContactInfo, gatewayInfo); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Processes the supplied intent and starts the outgoing call broadcast process relevant to the |
| 141 | * intent. |
| 142 | * |
| 143 | * This method will handle three kinds of actions: |
| 144 | * |
| 145 | * - CALL (intent launched by all third party dialers) |
| 146 | * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer) |
| 147 | * - CALL_EMERGENCY (intent launched by lock screen emergency dialer) |
| 148 | */ |
| 149 | void processIntent() { |
| 150 | Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster."); |
| 151 | |
| 152 | final Context context = TelecommApp.getInstance(); |
| 153 | Intent intent = mIntent; |
| 154 | |
| 155 | String handle = PhoneNumberUtils.getNumberFromIntent(intent, context); |
| 156 | |
| 157 | if (TextUtils.isEmpty(handle)) { |
| 158 | Log.w(this, "Empty handle obtained from the call intent."); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (!PhoneNumberUtils.isUriNumber(handle)) { |
| 163 | handle = PhoneNumberUtils.convertKeypadLettersToDigits(handle); |
| 164 | handle = PhoneNumberUtils.stripSeparators(handle); |
| 165 | } |
| 166 | |
| 167 | final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(context, handle); |
| 168 | Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber); |
| 169 | |
| 170 | rewriteCallIntentAction(intent, isPotentialEmergencyNumber); |
| 171 | // True for certain types of numbers that are not intended to be intercepted or modified |
| 172 | // by third parties (e.g. emergency numbers). |
| 173 | boolean callImmediately = false; |
| 174 | |
| 175 | String action = intent.getAction(); |
| 176 | if (Intent.ACTION_CALL.equals(action)) { |
| 177 | if (isPotentialEmergencyNumber) { |
| 178 | Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s.", |
| 179 | handle, intent); |
| 180 | launchSystemDialer(context, intent.getData()); |
| 181 | } |
| 182 | callImmediately = false; |
| 183 | } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) { |
| 184 | if (!isPotentialEmergencyNumber) { |
| 185 | Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL " |
| 186 | + "Intent %s.", handle, intent); |
| 187 | return; |
| 188 | } |
| 189 | callImmediately = true; |
| 190 | } else { |
| 191 | Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | if (callImmediately) { |
| 196 | Log.i(this, "Placing call immediately instead of waiting for " |
| 197 | + " OutgoingCallBroadcastReceiver: %s", intent); |
| 198 | mCallsManager.placeOutgoingCall(Uri.parse(handle), mContactInfo, null); |
| 199 | |
| 200 | // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast |
| 201 | // so that third parties can still inspect (but not intercept) the outgoing call. When |
| 202 | // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to |
| 203 | // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra. |
| 204 | } |
| 205 | |
| 206 | broadcastIntent(intent, handle, context, !callImmediately); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sends a new outgoing call ordered broadcast so that third party apps can cancel the |
| 211 | * placement of the call or redirect it to a different number. |
| 212 | * |
| 213 | * @param originalCallIntent The original call intent. |
| 214 | * @param handle Call handle that was stored in the original call intent. |
| 215 | * @param context Valid context to send the ordered broadcast using. |
| 216 | * @param receiverRequired Whether or not the result from the ordered broadcast should be |
| 217 | * processed using a {@link NewOutgoingCallIntentBroadcaster}. |
| 218 | */ |
| 219 | private void broadcastIntent( |
| 220 | Intent originalCallIntent, |
| 221 | String handle, |
| 222 | Context context, |
| 223 | boolean receiverRequired) { |
| 224 | Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL); |
| 225 | if (handle != null) { |
| 226 | broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, handle); |
| 227 | } |
| 228 | |
| 229 | // Force receivers of this broadcast intent to run at foreground priority because we |
| 230 | // want to finish processing the broadcast intent as soon as possible. |
| 231 | broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 232 | Log.v(this, "Broadcasting intent: %s.", broadcastIntent); |
| 233 | |
| 234 | checkAndCopyGatewayProviderExtras(originalCallIntent, broadcastIntent); |
| 235 | |
| 236 | context.sendOrderedBroadcastAsUser( |
| 237 | broadcastIntent, |
| 238 | UserHandle.OWNER, |
| 239 | PERMISSION, |
| 240 | receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null, |
| 241 | null, // scheduler |
| 242 | Activity.RESULT_OK, // initialCode |
| 243 | handle, // initialData: initial value for the result data (number to be modified) |
| 244 | null); // initialExtras |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the |
| 249 | * source intent to the destination one. |
| 250 | * |
| 251 | * @param src Intent which may contain the provider's extras. |
| 252 | * @param dst Intent where a copy of the extras will be added if applicable. |
| 253 | */ |
| 254 | public void checkAndCopyGatewayProviderExtras(Intent src, Intent dst) { |
| 255 | if (hasGatewayProviderExtras(src)) { |
| 256 | dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE, |
| 257 | src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE)); |
| 258 | dst.putExtra(EXTRA_GATEWAY_URI, |
| 259 | src.getStringExtra(EXTRA_GATEWAY_URI)); |
| 260 | Log.d(this, "Found and copied gateway provider extras to broadcast intent."); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | Log.d(this, "No gateway provider extras found in call intent."); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Check if valid gateway provider information is stored as extras in the intent |
| 269 | * |
| 270 | * @param intent to check for |
| 271 | * @return true if the intent has all the gateway information extras needed. |
| 272 | */ |
| 273 | private boolean hasGatewayProviderExtras(Intent intent) { |
| 274 | if (intent == null) { |
| 275 | return false; |
| 276 | } |
| 277 | final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); |
| 278 | final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI); |
| 279 | |
| 280 | return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString); |
| 281 | } |
| 282 | |
| 283 | private static Uri getGatewayUriFromString(String gatewayUriString) { |
| 284 | return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Extracts gateway provider information from a provided intent.. |
| 289 | * |
| 290 | * @param intent to extract gateway provider information from. |
| 291 | * @param trueHandle The actual call handle that the user is trying to dial |
| 292 | * @return GatewayInfo object containing extracted gateway provider information as well as |
| 293 | * the actual handle the user is trying to dial. |
| 294 | */ |
| 295 | public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) { |
| 296 | if (intent == null) { |
| 297 | return null; |
| 298 | } |
| 299 | |
| 300 | // Check if gateway extras are present. |
| 301 | String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); |
| 302 | Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI)); |
| 303 | if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) { |
| 304 | return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle); |
| 305 | } |
| 306 | |
| 307 | return null; |
| 308 | } |
| 309 | |
| 310 | private void launchSystemDialer(Context context, Uri handle) { |
| 311 | Intent systemDialerIntent = new Intent(); |
| 312 | final Resources resources = context.getResources(); |
| 313 | systemDialerIntent.setClassName( |
| 314 | resources.getString(R.string.ui_default_package), |
| 315 | resources.getString(R.string.dialer_default_class)); |
| 316 | systemDialerIntent.setAction(Intent.ACTION_DIAL); |
| 317 | systemDialerIntent.setData(handle); |
| 318 | systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 319 | Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent); |
| 320 | context.startActivity(systemDialerIntent); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Check whether or not this is an emergency number, in order to enforce the restriction |
| 325 | * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency |
| 326 | * calls. |
| 327 | * |
| 328 | * To prevent malicious 3rd party apps from making emergency calls by passing in an |
| 329 | * "invalid" number like "9111234" (that isn't technically an emergency number but might |
| 330 | * still result in an emergency call with some networks), we use |
| 331 | * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber. |
| 332 | * |
| 333 | * @param context Valid context |
| 334 | * @param handle Handle to inspect in order to determine whether or not an emergency number |
| 335 | * is potentially being dialed |
| 336 | * @return True if the handle is potentially an emergency number. |
| 337 | */ |
| 338 | private boolean isPotentialEmergencyNumber(Context context, String handle) { |
| 339 | Log.v(this, "Checking restrictions for number : %s", Log.pii(handle)); |
| 340 | return (handle != null) && PhoneNumberUtils.isPotentialLocalEmergencyNumber(handle,context); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Given a call intent and whether or not the number to dial is an emergency number, rewrite |
| 345 | * the call intent action to an appropriate one. |
| 346 | * |
| 347 | * @param intent Intent to rewrite the action for |
| 348 | * @param isPotentialEmergencyNumber Whether or not the handle is potentially an emergency |
| 349 | * number. |
| 350 | */ |
| 351 | private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) { |
| 352 | if (CallActivity.class.getName().equals(intent.getComponent().getClassName())) { |
| 353 | // If we were launched directly from the CallActivity, not one of its more privileged |
| 354 | // aliases, then make sure that only the non-privileged actions are allowed. |
| 355 | if (!Intent.ACTION_CALL.equals(intent.getAction())) { |
| 356 | Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL"); |
| 357 | intent.setAction(Intent.ACTION_CALL); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | String action = intent.getAction(); |
| 362 | |
| 363 | /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */ |
| 364 | if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) { |
| 365 | if (isPotentialEmergencyNumber) { |
| 366 | Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential" |
| 367 | + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead."); |
| 368 | action = Intent.ACTION_CALL_EMERGENCY; |
| 369 | } else { |
| 370 | action = Intent.ACTION_CALL; |
| 371 | } |
| 372 | Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action); |
| 373 | intent.setAction(action); |
| 374 | } |
| 375 | } |
| 376 | } |