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