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; |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
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; |
Brad Ebinger | 953e1af | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 31 | import android.telecom.Log; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 32 | import android.telecom.PhoneAccount; |
Tyler Gunn | b4f90ff | 2017-09-22 11:07:10 -0700 | [diff] [blame] | 33 | import android.telecom.PhoneAccountHandle; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 34 | import android.telecom.TelecomManager; |
| 35 | import android.telecom.VideoProfile; |
Yorke Lee | d725587 | 2014-08-25 15:03:51 -0700 | [diff] [blame] | 36 | import android.telephony.DisconnectCause; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 37 | import android.text.TextUtils; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 38 | |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
| 40 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 41 | // TODO: Needed for move to system service: import com.android.internal.R; |
| 42 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 43 | /** |
| 44 | * OutgoingCallIntentBroadcaster receives CALL and CALL_PRIVILEGED Intents, and broadcasts the |
| 45 | * ACTION_NEW_OUTGOING_CALL intent. ACTION_NEW_OUTGOING_CALL is an ordered broadcast intent which |
| 46 | * contains the phone number being dialed. Applications can use this intent to (1) see which numbers |
| 47 | * are being dialed, (2) redirect a call (change the number being dialed), or (3) prevent a call |
| 48 | * from being placed. |
| 49 | * |
| 50 | * After the other applications have had a chance to see the ACTION_NEW_OUTGOING_CALL intent, it |
| 51 | * finally reaches the {@link NewOutgoingCallBroadcastIntentReceiver}. |
| 52 | * |
| 53 | * Calls where no number is present (like for a CDMA "empty flash" or a nonexistent voicemail |
| 54 | * number) are exempt from being broadcast. |
| 55 | * |
| 56 | * Calls to emergency numbers are still broadcast for informative purposes. The call is placed |
| 57 | * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented. |
| 58 | */ |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 59 | @VisibleForTesting |
| 60 | public class NewOutgoingCallIntentBroadcaster { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 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"; |
| 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; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 75 | private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter; |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 76 | private final TelecomSystem.SyncRoot mLock; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 77 | |
Yorke Lee | cce5deb | 2014-06-18 11:27:42 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Whether or not the outgoing call intent originated from the default phone application. If |
| 80 | * so, it will be allowed to make emergency calls, even with the ACTION_CALL intent. |
| 81 | */ |
| 82 | private final boolean mIsDefaultOrSystemPhoneApp; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 83 | |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 84 | @VisibleForTesting |
| 85 | public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call, |
| 86 | Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter, |
| 87 | boolean isDefaultPhoneApp) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 88 | mContext = context; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 89 | mCallsManager = callsManager; |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 90 | mCall = call; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 91 | mIntent = intent; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 92 | mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter; |
Yorke Lee | cce5deb | 2014-06-18 11:27:42 -0700 | [diff] [blame] | 93 | mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp; |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 94 | mLock = mCallsManager.getLock(); |
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"); |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 108 | synchronized (mLock) { |
| 109 | Log.v(this, "onReceive: %s", intent); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 110 | |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 111 | // Once the NEW_OUTGOING_CALL broadcast is finished, the resultData is |
| 112 | // used as the actual number to call. (If null, no call will be placed.) |
| 113 | String resultNumber = getResultData(); |
| 114 | Log.i(this, "Received new-outgoing-call-broadcast for %s with data %s", mCall, |
| 115 | Log.pii(resultNumber)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 116 | |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 117 | boolean endEarly = false; |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 118 | long disconnectTimeout = |
| 119 | Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver()); |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 120 | if (resultNumber == null) { |
| 121 | Log.v(this, "Call cancelled (null number), returning..."); |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 122 | disconnectTimeout = getDisconnectTimeoutFromApp( |
| 123 | getResultExtras(false), disconnectTimeout); |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 124 | endEarly = true; |
| 125 | } else if (mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber( |
| 126 | mContext, resultNumber)) { |
| 127 | Log.w(this, "Cannot modify outgoing call to emergency number %s.", |
| 128 | resultNumber); |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 129 | disconnectTimeout = 0; |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 130 | endEarly = true; |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 131 | } |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 132 | |
| 133 | if (endEarly) { |
| 134 | if (mCall != null) { |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 135 | mCall.disconnect(disconnectTimeout); |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 136 | } |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // If this call is already disconnected then we have nothing more to do. |
| 141 | if (mCall.isDisconnected()) { |
| 142 | Log.w(this, "Call has already been disconnected," + |
| 143 | " ignore the broadcast Call %s", mCall); |
| 144 | return; |
| 145 | } |
| 146 | |
Tyler Gunn | 708a7ba | 2018-04-04 10:27:43 -0700 | [diff] [blame] | 147 | // TODO: Remove the assumption that phone numbers are either SIP or TEL. |
| 148 | // This does not impact self-managed ConnectionServices as they do not use the |
| 149 | // NewOutgoingCallIntentBroadcaster. |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 150 | Uri resultHandleUri = Uri.fromParts( |
| 151 | mPhoneNumberUtilsAdapter.isUriNumber(resultNumber) ? |
| 152 | PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL, |
| 153 | resultNumber, null); |
| 154 | |
| 155 | Uri originalUri = mIntent.getData(); |
| 156 | |
| 157 | if (originalUri.getSchemeSpecificPart().equals(resultNumber)) { |
| 158 | Log.v(this, "Call number unmodified after" + |
| 159 | " new outgoing call intent broadcast."); |
| 160 | } else { |
| 161 | Log.v(this, "Retrieved modified handle after outgoing call intent" + |
| 162 | " broadcast: Original: %s, Modified: %s", |
| 163 | Log.pii(originalUri), |
| 164 | Log.pii(resultHandleUri)); |
| 165 | } |
| 166 | |
| 167 | GatewayInfo gatewayInfo = getGateWayInfoFromIntent(intent, resultHandleUri); |
Jordan Liu | d32c9db | 2017-09-27 14:23:15 -0700 | [diff] [blame] | 168 | placeOutgoingCallImmediately(mCall, resultHandleUri, gatewayInfo, |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 169 | mIntent.getBooleanExtra( |
| 170 | TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false), |
| 171 | mIntent.getIntExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, |
| 172 | VideoProfile.STATE_AUDIO_ONLY)); |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 173 | } |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 174 | } finally { |
Yorke Lee | e4a9c41 | 2014-11-14 16:59:42 -0800 | [diff] [blame] | 175 | Trace.endSection(); |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 176 | Log.endSession(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 177 | } |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Processes the supplied intent and starts the outgoing call broadcast process relevant to the |
| 183 | * intent. |
| 184 | * |
| 185 | * This method will handle three kinds of actions: |
| 186 | * |
| 187 | * - CALL (intent launched by all third party dialers) |
| 188 | * - CALL_PRIVILEGED (intent launched by system apps e.g. system Dialer, voice Dialer) |
| 189 | * - CALL_EMERGENCY (intent launched by lock screen emergency dialer) |
Yorke Lee | cce5deb | 2014-06-18 11:27:42 -0700 | [diff] [blame] | 190 | * |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 191 | * @return {@link DisconnectCause#NOT_DISCONNECTED} if the call succeeded, and an appropriate |
| 192 | * {@link DisconnectCause} if the call did not, describing why it failed. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 193 | */ |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 194 | @VisibleForTesting |
| 195 | public int processIntent() { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 196 | Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster."); |
| 197 | |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 198 | Intent intent = mIntent; |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 199 | String action = intent.getAction(); |
| 200 | final Uri handle = intent.getData(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 201 | |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 202 | if (handle == null) { |
| 203 | Log.w(this, "Empty handle obtained from the call intent."); |
| 204 | return DisconnectCause.INVALID_NUMBER; |
| 205 | } |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 206 | |
Jay Shrauner | 56a76b7 | 2014-09-05 14:41:48 -0700 | [diff] [blame] | 207 | boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme()); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 208 | if (isVoicemailNumber) { |
Yorke Lee | fe1ce0a | 2014-11-20 08:59:46 -0800 | [diff] [blame] | 209 | if (Intent.ACTION_CALL.equals(action) |
| 210 | || Intent.ACTION_CALL_PRIVILEGED.equals(action)) { |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 211 | // Voicemail calls will be handled directly by the telephony connection manager |
Santos Cordon | 5f048fe | 2016-04-04 17:47:20 -0700 | [diff] [blame] | 212 | |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 213 | boolean speakerphoneOn = mIntent.getBooleanExtra( |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 214 | TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false); |
Jordan Liu | d32c9db | 2017-09-27 14:23:15 -0700 | [diff] [blame] | 215 | placeOutgoingCallImmediately(mCall, handle, null, speakerphoneOn, |
Tyler Gunn | 5b88249 | 2015-06-03 10:03:13 -0700 | [diff] [blame] | 216 | VideoProfile.STATE_AUDIO_ONLY); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 217 | |
| 218 | return DisconnectCause.NOT_DISCONNECTED; |
Yorke Lee | d725587 | 2014-08-25 15:03:51 -0700 | [diff] [blame] | 219 | } else { |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 220 | Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent); |
| 221 | return DisconnectCause.OUTGOING_CANCELED; |
Yorke Lee | d725587 | 2014-08-25 15:03:51 -0700 | [diff] [blame] | 222 | } |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Tyler Gunn | b4f90ff | 2017-09-22 11:07:10 -0700 | [diff] [blame] | 225 | PhoneAccountHandle targetPhoneAccount = mIntent.getParcelableExtra( |
| 226 | TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE); |
Tyler Gunn | 708a7ba | 2018-04-04 10:27:43 -0700 | [diff] [blame] | 227 | boolean isSelfManaged = false; |
Tyler Gunn | b4f90ff | 2017-09-22 11:07:10 -0700 | [diff] [blame] | 228 | if (targetPhoneAccount != null) { |
| 229 | PhoneAccount phoneAccount = |
| 230 | mCallsManager.getPhoneAccountRegistrar().getPhoneAccountUnchecked( |
| 231 | targetPhoneAccount); |
Tyler Gunn | 708a7ba | 2018-04-04 10:27:43 -0700 | [diff] [blame] | 232 | if (phoneAccount != null) { |
| 233 | isSelfManaged = phoneAccount.isSelfManaged(); |
Tyler Gunn | b4f90ff | 2017-09-22 11:07:10 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Tyler Gunn | 708a7ba | 2018-04-04 10:27:43 -0700 | [diff] [blame] | 237 | String number = ""; |
| 238 | // True for certain types of numbers that are not intended to be intercepted or modified |
| 239 | // by third parties (e.g. emergency numbers). |
| 240 | boolean callImmediately = false; |
| 241 | // True for all managed calls, false for self-managed calls. |
| 242 | boolean sendNewOutgoingCallBroadcast = true; |
| 243 | Uri callingAddress = handle; |
| 244 | |
| 245 | if (!isSelfManaged) { |
| 246 | // Placing a managed call |
| 247 | number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext); |
| 248 | if (TextUtils.isEmpty(number)) { |
| 249 | Log.w(this, "Empty number obtained from the call intent."); |
| 250 | return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED; |
| 251 | } |
| 252 | |
| 253 | // TODO: Cleanup this dialing code; it makes the assumption that we're dialing with a |
| 254 | // SIP or TEL URI. |
| 255 | boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number); |
| 256 | if (!isUriNumber) { |
| 257 | number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number); |
| 258 | number = mPhoneNumberUtilsAdapter.stripSeparators(number); |
| 259 | } |
| 260 | |
| 261 | final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number); |
| 262 | Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber); |
| 263 | |
| 264 | rewriteCallIntentAction(intent, isPotentialEmergencyNumber); |
| 265 | action = intent.getAction(); |
| 266 | |
| 267 | if (Intent.ACTION_CALL.equals(action)) { |
| 268 | if (isPotentialEmergencyNumber) { |
| 269 | if (!mIsDefaultOrSystemPhoneApp) { |
| 270 | Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s " |
| 271 | + "unless caller is system or default dialer.", number, intent); |
| 272 | launchSystemDialer(intent.getData()); |
| 273 | return DisconnectCause.OUTGOING_CANCELED; |
| 274 | } else { |
| 275 | callImmediately = true; |
| 276 | } |
| 277 | } |
| 278 | } else if (Intent.ACTION_CALL_EMERGENCY.equals(action)) { |
| 279 | if (!isPotentialEmergencyNumber) { |
| 280 | Log.w(this, "Cannot call non-potential-emergency number %s with EMERGENCY_CALL " |
| 281 | + "Intent %s.", number, intent); |
| 282 | return DisconnectCause.OUTGOING_CANCELED; |
| 283 | } |
| 284 | callImmediately = true; |
| 285 | } else { |
| 286 | Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent); |
| 287 | return DisconnectCause.INVALID_NUMBER; |
| 288 | } |
| 289 | |
| 290 | // TODO: Support dialing using URIs instead of just assuming SIP or TEL. |
Jay Shrauner | 56a76b7 | 2014-09-05 14:41:48 -0700 | [diff] [blame] | 291 | String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL; |
Tyler Gunn | 708a7ba | 2018-04-04 10:27:43 -0700 | [diff] [blame] | 292 | callingAddress = Uri.fromParts(scheme, number, null); |
| 293 | } else { |
| 294 | // Self-managed call. |
| 295 | callImmediately = true; |
| 296 | sendNewOutgoingCallBroadcast = false; |
| 297 | Log.i(this, "Skipping NewOutgoingCallBroadcast for self-managed call."); |
| 298 | } |
| 299 | |
| 300 | if (callImmediately) { |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 301 | boolean speakerphoneOn = mIntent.getBooleanExtra( |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 302 | TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 303 | int videoState = mIntent.getIntExtra( |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 304 | TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, |
Tyler Gunn | 5b88249 | 2015-06-03 10:03:13 -0700 | [diff] [blame] | 305 | VideoProfile.STATE_AUDIO_ONLY); |
Tyler Gunn | 708a7ba | 2018-04-04 10:27:43 -0700 | [diff] [blame] | 306 | placeOutgoingCallImmediately(mCall, callingAddress, null, |
Nancy Chen | 0d3076c | 2014-07-30 14:45:44 -0700 | [diff] [blame] | 307 | speakerphoneOn, videoState); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 308 | |
| 309 | // Don't return but instead continue and send the ACTION_NEW_OUTGOING_CALL broadcast |
| 310 | // so that third parties can still inspect (but not intercept) the outgoing call. When |
| 311 | // the broadcast finally reaches the OutgoingCallBroadcastReceiver, we'll know not to |
| 312 | // initiate the call again because of the presence of the EXTRA_ALREADY_CALLED extra. |
| 313 | } |
| 314 | |
Tyler Gunn | b4f90ff | 2017-09-22 11:07:10 -0700 | [diff] [blame] | 315 | if (sendNewOutgoingCallBroadcast) { |
| 316 | UserHandle targetUser = mCall.getInitiatingUser(); |
| 317 | Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser); |
| 318 | broadcastIntent(intent, number, !callImmediately, targetUser); |
| 319 | } |
Yorke Lee | d725587 | 2014-08-25 15:03:51 -0700 | [diff] [blame] | 320 | return DisconnectCause.NOT_DISCONNECTED; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Sends a new outgoing call ordered broadcast so that third party apps can cancel the |
| 325 | * placement of the call or redirect it to a different number. |
| 326 | * |
| 327 | * @param originalCallIntent The original call intent. |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 328 | * @param number Call number that was stored in the original call intent. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 329 | * @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] | 330 | * processed using a {@link NewOutgoingCallIntentBroadcaster}. |
| 331 | * @param targetUser User that the broadcast sent to. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 332 | */ |
| 333 | private void broadcastIntent( |
| 334 | Intent originalCallIntent, |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 335 | String number, |
Tony Mak | d3bacb7 | 2016-02-03 15:35:27 +0000 | [diff] [blame] | 336 | boolean receiverRequired, |
| 337 | UserHandle targetUser) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 338 | Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL); |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 339 | if (number != null) { |
| 340 | broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // Force receivers of this broadcast intent to run at foreground priority because we |
| 344 | // want to finish processing the broadcast intent as soon as possible. |
Christopher Tate | 47c73e9 | 2017-03-28 18:04:29 -0700 | [diff] [blame] | 345 | broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND |
| 346 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 347 | Log.v(this, "Broadcasting intent: %s.", broadcastIntent); |
| 348 | |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 349 | checkAndCopyProviderExtras(originalCallIntent, broadcastIntent); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 350 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 351 | mContext.sendOrderedBroadcastAsUser( |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 352 | broadcastIntent, |
Tony Mak | d3bacb7 | 2016-02-03 15:35:27 +0000 | [diff] [blame] | 353 | targetUser, |
Svetoslav | 973c4e1 | 2015-04-29 14:13:51 -0700 | [diff] [blame] | 354 | android.Manifest.permission.PROCESS_OUTGOING_CALLS, |
| 355 | AppOpsManager.OP_PROCESS_OUTGOING_CALLS, |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 356 | receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null, |
| 357 | null, // scheduler |
| 358 | Activity.RESULT_OK, // initialCode |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 359 | number, // initialData: initial value for the result data (number to be modified) |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 360 | null); // initialExtras |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Copy all the expected extras set when a 3rd party gateway provider is to be used, from the |
| 365 | * source intent to the destination one. |
| 366 | * |
| 367 | * @param src Intent which may contain the provider's extras. |
| 368 | * @param dst Intent where a copy of the extras will be added if applicable. |
| 369 | */ |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 370 | public void checkAndCopyProviderExtras(Intent src, Intent dst) { |
| 371 | if (src == null) { |
| 372 | return; |
| 373 | } |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 374 | if (hasGatewayProviderExtras(src)) { |
| 375 | dst.putExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE, |
| 376 | src.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE)); |
| 377 | dst.putExtra(EXTRA_GATEWAY_URI, |
| 378 | src.getStringExtra(EXTRA_GATEWAY_URI)); |
| 379 | Log.d(this, "Found and copied gateway provider extras to broadcast intent."); |
| 380 | return; |
| 381 | } |
| 382 | |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 383 | Log.d(this, "No provider extras found in call intent."); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Check if valid gateway provider information is stored as extras in the intent |
| 388 | * |
| 389 | * @param intent to check for |
| 390 | * @return true if the intent has all the gateway information extras needed. |
| 391 | */ |
| 392 | private boolean hasGatewayProviderExtras(Intent intent) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 393 | final String name = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); |
| 394 | final String uriString = intent.getStringExtra(EXTRA_GATEWAY_URI); |
| 395 | |
| 396 | return !TextUtils.isEmpty(name) && !TextUtils.isEmpty(uriString); |
| 397 | } |
| 398 | |
| 399 | private static Uri getGatewayUriFromString(String gatewayUriString) { |
| 400 | return TextUtils.isEmpty(gatewayUriString) ? null : Uri.parse(gatewayUriString); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Extracts gateway provider information from a provided intent.. |
| 405 | * |
| 406 | * @param intent to extract gateway provider information from. |
| 407 | * @param trueHandle The actual call handle that the user is trying to dial |
| 408 | * @return GatewayInfo object containing extracted gateway provider information as well as |
| 409 | * the actual handle the user is trying to dial. |
| 410 | */ |
| 411 | public static GatewayInfo getGateWayInfoFromIntent(Intent intent, Uri trueHandle) { |
| 412 | if (intent == null) { |
| 413 | return null; |
| 414 | } |
| 415 | |
| 416 | // Check if gateway extras are present. |
| 417 | String gatewayPackageName = intent.getStringExtra(EXTRA_GATEWAY_PROVIDER_PACKAGE); |
| 418 | Uri gatewayUri = getGatewayUriFromString(intent.getStringExtra(EXTRA_GATEWAY_URI)); |
| 419 | if (!TextUtils.isEmpty(gatewayPackageName) && gatewayUri != null) { |
| 420 | return new GatewayInfo(gatewayPackageName, gatewayUri, trueHandle); |
| 421 | } |
| 422 | |
| 423 | return null; |
| 424 | } |
| 425 | |
Jordan Liu | d32c9db | 2017-09-27 14:23:15 -0700 | [diff] [blame] | 426 | private void placeOutgoingCallImmediately(Call call, Uri handle, GatewayInfo gatewayInfo, |
| 427 | boolean speakerphoneOn, int videoState) { |
| 428 | Log.i(this, |
| 429 | "Placing call immediately instead of waiting for OutgoingCallBroadcastReceiver"); |
| 430 | // Since we are not going to go through "Outgoing call broadcast", make sure |
| 431 | // we mark it as ready. |
| 432 | mCall.setNewOutgoingCallIntentBroadcastIsDone(); |
| 433 | mCallsManager.placeOutgoingCall(call, handle, gatewayInfo, speakerphoneOn, videoState); |
| 434 | } |
| 435 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 436 | private void launchSystemDialer(Uri handle) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 437 | Intent systemDialerIntent = new Intent(); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 438 | final Resources resources = mContext.getResources(); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 439 | systemDialerIntent.setClassName( |
| 440 | resources.getString(R.string.ui_default_package), |
| 441 | resources.getString(R.string.dialer_default_class)); |
| 442 | systemDialerIntent.setAction(Intent.ACTION_DIAL); |
| 443 | systemDialerIntent.setData(handle); |
| 444 | systemDialerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 445 | Log.v(this, "calling startActivity for default dialer: %s", systemDialerIntent); |
Yorke Lee | 39d94c1 | 2014-10-08 17:21:28 -0700 | [diff] [blame] | 446 | mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Check whether or not this is an emergency number, in order to enforce the restriction |
| 451 | * that only the CALL_PRIVILEGED and CALL_EMERGENCY intents are allowed to make emergency |
| 452 | * calls. |
| 453 | * |
| 454 | * To prevent malicious 3rd party apps from making emergency calls by passing in an |
| 455 | * "invalid" number like "9111234" (that isn't technically an emergency number but might |
| 456 | * still result in an emergency call with some networks), we use |
| 457 | * isPotentialLocalEmergencyNumber instead of isLocalEmergencyNumber. |
| 458 | * |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 459 | * @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] | 460 | * is potentially being dialed |
| 461 | * @return True if the handle is potentially an emergency number. |
| 462 | */ |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 463 | private boolean isPotentialEmergencyNumber(String number) { |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 464 | Log.v(this, "Checking restrictions for number : %s", Log.pii(number)); |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 465 | return (number != null) |
| 466 | && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Given a call intent and whether or not the number to dial is an emergency number, rewrite |
| 471 | * the call intent action to an appropriate one. |
| 472 | * |
| 473 | * @param intent Intent to rewrite the action for |
Nancy Chen | 308ab8b | 2014-09-02 16:18:30 -0700 | [diff] [blame] | 474 | * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 475 | * number. |
| 476 | */ |
| 477 | private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 478 | String action = intent.getAction(); |
| 479 | |
| 480 | /* Change CALL_PRIVILEGED into CALL or CALL_EMERGENCY as needed. */ |
| 481 | if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) { |
| 482 | if (isPotentialEmergencyNumber) { |
| 483 | Log.i(this, "ACTION_CALL_PRIVILEGED is used while the number is a potential" |
| 484 | + " emergency number. Using ACTION_CALL_EMERGENCY as an action instead."); |
| 485 | action = Intent.ACTION_CALL_EMERGENCY; |
| 486 | } else { |
| 487 | action = Intent.ACTION_CALL; |
| 488 | } |
| 489 | Log.v(this, " - updating action from CALL_PRIVILEGED to %s", action); |
| 490 | intent.setAction(action); |
| 491 | } |
| 492 | } |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 493 | |
| 494 | private long getDisconnectTimeoutFromApp(Bundle resultExtras, long defaultTimeout) { |
| 495 | if (resultExtras != null) { |
| 496 | long disconnectTimeout = resultExtras.getLong( |
| 497 | TelecomManager.EXTRA_NEW_OUTGOING_CALL_CANCEL_TIMEOUT, defaultTimeout); |
| 498 | if (disconnectTimeout < 0) { |
| 499 | disconnectTimeout = 0; |
| 500 | } |
| 501 | return Math.min(disconnectTimeout, |
| 502 | Timeouts.getMaxNewOutgoingCallCancelMillis(mContext.getContentResolver())); |
| 503 | } else { |
| 504 | return defaultTimeout; |
| 505 | } |
| 506 | } |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 507 | } |