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