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