blob: 3797c68d202fe819d06bf5cabd3e7a0ebe38d3c9 [file] [log] [blame]
Yorke Lee33501632014-03-17 19:24:12 -07001/*
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 Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Yorke Lee33501632014-03-17 19:24:12 -070018
Svetoslav973c4e12015-04-29 14:13:51 -070019import android.app.AppOpsManager;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070020
Yorke Lee33501632014-03-17 19:24:12 -070021import android.app.Activity;
Yorke Lee33501632014-03-17 19:24:12 -070022import android.content.BroadcastReceiver;
23import android.content.Context;
Yorke Lee33501632014-03-17 19:24:12 -070024import android.content.Intent;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.content.res.Resources;
Yorke Lee33501632014-03-17 19:24:12 -070026import android.net.Uri;
Hall Liu63e690c2017-02-14 18:05:03 -080027import android.os.Bundle;
Yorke Leee4a9c412014-11-14 16:59:42 -080028import android.os.Trace;
Yorke Lee33501632014-03-17 19:24:12 -070029import android.os.UserHandle;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070030import android.telecom.GatewayInfo;
Brad Ebinger953e1af2016-10-05 15:45:22 -070031import android.telecom.Log;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070032import android.telecom.PhoneAccount;
Tyler Gunnb4f90ff2017-09-22 11:07:10 -070033import android.telecom.PhoneAccountHandle;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070034import android.telecom.TelecomManager;
35import android.telecom.VideoProfile;
Yorke Leed7255872014-08-25 15:03:51 -070036import android.telephony.DisconnectCause;
Yorke Lee33501632014-03-17 19:24:12 -070037import android.text.TextUtils;
Yorke Lee33501632014-03-17 19:24:12 -070038
Hall Liu220b4192015-12-11 11:33:08 -080039import com.android.internal.annotations.VisibleForTesting;
40
Tyler Gunn91d43cf2014-09-17 12:19:39 -070041// TODO: Needed for move to system service: import com.android.internal.R;
42
Yorke Lee33501632014-03-17 19:24:12 -070043/**
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 Liu220b4192015-12-11 11:33:08 -080059@VisibleForTesting
60public class NewOutgoingCallIntentBroadcaster {
Yorke Lee33501632014-03-17 19:24:12 -070061 /**
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 Chen0d3076c2014-07-30 14:45:44 -070072 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070073 private final Intent mIntent;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070074 private final Context mContext;
Hall Liu220b4192015-12-11 11:33:08 -080075 private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
Hall Liuc9cf5442016-06-29 10:08:10 -070076 private final TelecomSystem.SyncRoot mLock;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070077
Yorke Leecce5deb2014-06-18 11:27:42 -070078 /*
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 Lee33501632014-03-17 19:24:12 -070083
Hall Liu220b4192015-12-11 11:33:08 -080084 @VisibleForTesting
85 public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
86 Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
87 boolean isDefaultPhoneApp) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -070088 mContext = context;
Yorke Lee33501632014-03-17 19:24:12 -070089 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070090 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070091 mIntent = intent;
Hall Liu220b4192015-12-11 11:33:08 -080092 mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
Yorke Leecce5deb2014-06-18 11:27:42 -070093 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Hall Liuc9cf5442016-06-29 10:08:10 -070094 mLock = mCallsManager.getLock();
Yorke Lee33501632014-03-17 19:24:12 -070095 }
96
97 /**
98 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
99 * the OutgoingCallIntentBroadcasterListener as necessary.
100 */
Hall Liu220b4192015-12-11 11:33:08 -0800101 public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
Yorke Lee33501632014-03-17 19:24:12 -0700102
103 @Override
104 public void onReceive(Context context, Intent intent) {
Brad Ebinger11623a32015-11-25 13:52:02 -0800105 try {
106 Log.startSession("NOCBIR.oR");
107 Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
Hall Liuc9cf5442016-06-29 10:08:10 -0700108 synchronized (mLock) {
109 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700110
Hall Liuc9cf5442016-06-29 10:08:10 -0700111 // 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 Lee33501632014-03-17 19:24:12 -0700116
Hall Liuc9cf5442016-06-29 10:08:10 -0700117 boolean endEarly = false;
Hall Liu63e690c2017-02-14 18:05:03 -0800118 long disconnectTimeout =
119 Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver());
Hall Liuc9cf5442016-06-29 10:08:10 -0700120 if (resultNumber == null) {
121 Log.v(this, "Call cancelled (null number), returning...");
Hall Liu63e690c2017-02-14 18:05:03 -0800122 disconnectTimeout = getDisconnectTimeoutFromApp(
123 getResultExtras(false), disconnectTimeout);
Hall Liuc9cf5442016-06-29 10:08:10 -0700124 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 Liu63e690c2017-02-14 18:05:03 -0800129 disconnectTimeout = 0;
Hall Liuc9cf5442016-06-29 10:08:10 -0700130 endEarly = true;
Brad Ebinger11623a32015-11-25 13:52:02 -0800131 }
Hall Liuc9cf5442016-06-29 10:08:10 -0700132
133 if (endEarly) {
134 if (mCall != null) {
Hall Liu63e690c2017-02-14 18:05:03 -0800135 mCall.disconnect(disconnectTimeout);
Hall Liuc9cf5442016-06-29 10:08:10 -0700136 }
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 Gunn708a7ba2018-04-04 10:27:43 -0700147 // 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 Liuc9cf5442016-06-29 10:08:10 -0700150 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 Liud32c9db2017-09-27 14:23:15 -0700168 placeOutgoingCallImmediately(mCall, resultHandleUri, gatewayInfo,
Hall Liuc9cf5442016-06-29 10:08:10 -0700169 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 Ebinger11623a32015-11-25 13:52:02 -0800173 }
Brad Ebinger11623a32015-11-25 13:52:02 -0800174 } finally {
Yorke Leee4a9c412014-11-14 16:59:42 -0800175 Trace.endSection();
Brad Ebinger11623a32015-11-25 13:52:02 -0800176 Log.endSession();
Yorke Lee33501632014-03-17 19:24:12 -0700177 }
Yorke Lee33501632014-03-17 19:24:12 -0700178 }
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 Leecce5deb2014-06-18 11:27:42 -0700190 *
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700191 * @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 Lee33501632014-03-17 19:24:12 -0700193 */
Hall Liu220b4192015-12-11 11:33:08 -0800194 @VisibleForTesting
195 public int processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700196 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
197
Yorke Lee33501632014-03-17 19:24:12 -0700198 Intent intent = mIntent;
Nancy Chen308ab8b2014-09-02 16:18:30 -0700199 String action = intent.getAction();
200 final Uri handle = intent.getData();
Yorke Lee33501632014-03-17 19:24:12 -0700201
Nancy Chen308ab8b2014-09-02 16:18:30 -0700202 if (handle == null) {
203 Log.w(this, "Empty handle obtained from the call intent.");
204 return DisconnectCause.INVALID_NUMBER;
205 }
Yorke Lee33501632014-03-17 19:24:12 -0700206
Jay Shrauner56a76b72014-09-05 14:41:48 -0700207 boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
Nancy Chen308ab8b2014-09-02 16:18:30 -0700208 if (isVoicemailNumber) {
Yorke Leefe1ce0a2014-11-20 08:59:46 -0800209 if (Intent.ACTION_CALL.equals(action)
210 || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700211 // Voicemail calls will be handled directly by the telephony connection manager
Santos Cordon5f048fe2016-04-04 17:47:20 -0700212
Nancy Chen308ab8b2014-09-02 16:18:30 -0700213 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700214 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Jordan Liud32c9db2017-09-27 14:23:15 -0700215 placeOutgoingCallImmediately(mCall, handle, null, speakerphoneOn,
Tyler Gunn5b882492015-06-03 10:03:13 -0700216 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700217
218 return DisconnectCause.NOT_DISCONNECTED;
Yorke Leed7255872014-08-25 15:03:51 -0700219 } else {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700220 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
221 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leed7255872014-08-25 15:03:51 -0700222 }
Yorke Lee33501632014-03-17 19:24:12 -0700223 }
224
Tyler Gunnb4f90ff2017-09-22 11:07:10 -0700225 PhoneAccountHandle targetPhoneAccount = mIntent.getParcelableExtra(
226 TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
Tyler Gunn708a7ba2018-04-04 10:27:43 -0700227 boolean isSelfManaged = false;
Tyler Gunnb4f90ff2017-09-22 11:07:10 -0700228 if (targetPhoneAccount != null) {
229 PhoneAccount phoneAccount =
230 mCallsManager.getPhoneAccountRegistrar().getPhoneAccountUnchecked(
231 targetPhoneAccount);
Tyler Gunn708a7ba2018-04-04 10:27:43 -0700232 if (phoneAccount != null) {
233 isSelfManaged = phoneAccount.isSelfManaged();
Tyler Gunnb4f90ff2017-09-22 11:07:10 -0700234 }
235 }
236
Tyler Gunn708a7ba2018-04-04 10:27:43 -0700237 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 Shrauner56a76b72014-09-05 14:41:48 -0700291 String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
Tyler Gunn708a7ba2018-04-04 10:27:43 -0700292 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 Nepal905dfba2014-07-14 08:20:41 -0700301 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700302 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700303 int videoState = mIntent.getIntExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700304 TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Tyler Gunn5b882492015-06-03 10:03:13 -0700305 VideoProfile.STATE_AUDIO_ONLY);
Tyler Gunn708a7ba2018-04-04 10:27:43 -0700306 placeOutgoingCallImmediately(mCall, callingAddress, null,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700307 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700308
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 Gunnb4f90ff2017-09-22 11:07:10 -0700315 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 Leed7255872014-08-25 15:03:51 -0700320 return DisconnectCause.NOT_DISCONNECTED;
Yorke Lee33501632014-03-17 19:24:12 -0700321 }
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 Chen308ab8b2014-09-02 16:18:30 -0700328 * @param number Call number that was stored in the original call intent.
Yorke Lee33501632014-03-17 19:24:12 -0700329 * @param receiverRequired Whether or not the result from the ordered broadcast should be
Tony Makd3bacb72016-02-03 15:35:27 +0000330 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
331 * @param targetUser User that the broadcast sent to.
Yorke Lee33501632014-03-17 19:24:12 -0700332 */
333 private void broadcastIntent(
334 Intent originalCallIntent,
Nancy Chen308ab8b2014-09-02 16:18:30 -0700335 String number,
Tony Makd3bacb72016-02-03 15:35:27 +0000336 boolean receiverRequired,
337 UserHandle targetUser) {
Yorke Lee33501632014-03-17 19:24:12 -0700338 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700339 if (number != null) {
340 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
Yorke Lee33501632014-03-17 19:24:12 -0700341 }
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 Tate47c73e92017-03-28 18:04:29 -0700345 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND
346 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Yorke Lee33501632014-03-17 19:24:12 -0700347 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
348
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700349 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700350
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700351 mContext.sendOrderedBroadcastAsUser(
Yorke Lee33501632014-03-17 19:24:12 -0700352 broadcastIntent,
Tony Makd3bacb72016-02-03 15:35:27 +0000353 targetUser,
Svetoslav973c4e12015-04-29 14:13:51 -0700354 android.Manifest.permission.PROCESS_OUTGOING_CALLS,
355 AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
Yorke Lee33501632014-03-17 19:24:12 -0700356 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
357 null, // scheduler
358 Activity.RESULT_OK, // initialCode
Nancy Chen308ab8b2014-09-02 16:18:30 -0700359 number, // initialData: initial value for the result data (number to be modified)
Yorke Lee33501632014-03-17 19:24:12 -0700360 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 Chen77d2d0e2014-06-24 12:06:03 -0700370 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
371 if (src == null) {
372 return;
373 }
Yorke Lee33501632014-03-17 19:24:12 -0700374 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 Chen77d2d0e2014-06-24 12:06:03 -0700383 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700384 }
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 Lee33501632014-03-17 19:24:12 -0700393 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 Liud32c9db2017-09-27 14:23:15 -0700426 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 Gunn91d43cf2014-09-17 12:19:39 -0700436 private void launchSystemDialer(Uri handle) {
Yorke Lee33501632014-03-17 19:24:12 -0700437 Intent systemDialerIntent = new Intent();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700438 final Resources resources = mContext.getResources();
Yorke Lee33501632014-03-17 19:24:12 -0700439 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 Lee39d94c12014-10-08 17:21:28 -0700446 mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
Yorke Lee33501632014-03-17 19:24:12 -0700447 }
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 Chen308ab8b2014-09-02 16:18:30 -0700459 * @param number number to inspect in order to determine whether or not an emergency number
Yorke Lee33501632014-03-17 19:24:12 -0700460 * is potentially being dialed
461 * @return True if the handle is potentially an emergency number.
462 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700463 private boolean isPotentialEmergencyNumber(String number) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700464 Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
Hall Liu220b4192015-12-11 11:33:08 -0800465 return (number != null)
466 && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number);
Yorke Lee33501632014-03-17 19:24:12 -0700467 }
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 Chen308ab8b2014-09-02 16:18:30 -0700474 * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
Yorke Lee33501632014-03-17 19:24:12 -0700475 * number.
476 */
477 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700478 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 Liu63e690c2017-02-14 18:05:03 -0800493
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 Lee33501632014-03-17 19:24:12 -0700507}