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