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