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