blob: 74ee668da32beddc9b43f4259bf416f735bbb80f [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;
33import android.telecom.TelecomManager;
34import android.telecom.VideoProfile;
Yorke Leed7255872014-08-25 15:03:51 -070035import android.telephony.DisconnectCause;
Yorke Lee33501632014-03-17 19:24:12 -070036import android.text.TextUtils;
Yorke Lee33501632014-03-17 19:24:12 -070037
Hall Liu220b4192015-12-11 11:33:08 -080038import com.android.internal.annotations.VisibleForTesting;
39
Tyler Gunn91d43cf2014-09-17 12:19:39 -070040// TODO: Needed for move to system service: import com.android.internal.R;
41
Yorke Lee33501632014-03-17 19:24:12 -070042/**
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 Liu220b4192015-12-11 11:33:08 -080058@VisibleForTesting
59public class NewOutgoingCallIntentBroadcaster {
Yorke Lee33501632014-03-17 19:24:12 -070060 /**
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 Chen0d3076c2014-07-30 14:45:44 -070071 private final Call mCall;
Yorke Lee33501632014-03-17 19:24:12 -070072 private final Intent mIntent;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070073 private final Context mContext;
Hall Liu220b4192015-12-11 11:33:08 -080074 private final PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapter;
Hall Liuc9cf5442016-06-29 10:08:10 -070075 private final TelecomSystem.SyncRoot mLock;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070076
Yorke Leecce5deb2014-06-18 11:27:42 -070077 /*
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 Lee33501632014-03-17 19:24:12 -070082
Hall Liu220b4192015-12-11 11:33:08 -080083 @VisibleForTesting
84 public NewOutgoingCallIntentBroadcaster(Context context, CallsManager callsManager, Call call,
85 Intent intent, PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
86 boolean isDefaultPhoneApp) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -070087 mContext = context;
Yorke Lee33501632014-03-17 19:24:12 -070088 mCallsManager = callsManager;
Nancy Chen0d3076c2014-07-30 14:45:44 -070089 mCall = call;
Yorke Lee33501632014-03-17 19:24:12 -070090 mIntent = intent;
Hall Liu220b4192015-12-11 11:33:08 -080091 mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
Yorke Leecce5deb2014-06-18 11:27:42 -070092 mIsDefaultOrSystemPhoneApp = isDefaultPhoneApp;
Hall Liuc9cf5442016-06-29 10:08:10 -070093 mLock = mCallsManager.getLock();
Yorke Lee33501632014-03-17 19:24:12 -070094 }
95
96 /**
97 * Processes the result of the outgoing call broadcast intent, and performs callbacks to
98 * the OutgoingCallIntentBroadcasterListener as necessary.
99 */
Hall Liu220b4192015-12-11 11:33:08 -0800100 public class NewOutgoingCallBroadcastIntentReceiver extends BroadcastReceiver {
Yorke Lee33501632014-03-17 19:24:12 -0700101
102 @Override
103 public void onReceive(Context context, Intent intent) {
Brad Ebinger11623a32015-11-25 13:52:02 -0800104 try {
105 Log.startSession("NOCBIR.oR");
106 Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
Hall Liuc9cf5442016-06-29 10:08:10 -0700107 synchronized (mLock) {
108 Log.v(this, "onReceive: %s", intent);
Yorke Lee33501632014-03-17 19:24:12 -0700109
Hall Liuc9cf5442016-06-29 10:08:10 -0700110 // 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 Lee33501632014-03-17 19:24:12 -0700115
Hall Liuc9cf5442016-06-29 10:08:10 -0700116 boolean endEarly = false;
Hall Liu63e690c2017-02-14 18:05:03 -0800117 long disconnectTimeout =
118 Timeouts.getNewOutgoingCallCancelMillis(mContext.getContentResolver());
Hall Liuc9cf5442016-06-29 10:08:10 -0700119 if (resultNumber == null) {
120 Log.v(this, "Call cancelled (null number), returning...");
Hall Liu63e690c2017-02-14 18:05:03 -0800121 disconnectTimeout = getDisconnectTimeoutFromApp(
122 getResultExtras(false), disconnectTimeout);
Hall Liuc9cf5442016-06-29 10:08:10 -0700123 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 Liu63e690c2017-02-14 18:05:03 -0800128 disconnectTimeout = 0;
Hall Liuc9cf5442016-06-29 10:08:10 -0700129 endEarly = true;
Brad Ebinger11623a32015-11-25 13:52:02 -0800130 }
Hall Liuc9cf5442016-06-29 10:08:10 -0700131
132 if (endEarly) {
133 if (mCall != null) {
Hall Liu63e690c2017-02-14 18:05:03 -0800134 mCall.disconnect(disconnectTimeout);
Hall Liuc9cf5442016-06-29 10:08:10 -0700135 }
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 Ebinger11623a32015-11-25 13:52:02 -0800170 }
Brad Ebinger11623a32015-11-25 13:52:02 -0800171 } finally {
Yorke Leee4a9c412014-11-14 16:59:42 -0800172 Trace.endSection();
Brad Ebinger11623a32015-11-25 13:52:02 -0800173 Log.endSession();
Yorke Lee33501632014-03-17 19:24:12 -0700174 }
Yorke Lee33501632014-03-17 19:24:12 -0700175 }
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 Leecce5deb2014-06-18 11:27:42 -0700187 *
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700188 * @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 Lee33501632014-03-17 19:24:12 -0700190 */
Hall Liu220b4192015-12-11 11:33:08 -0800191 @VisibleForTesting
192 public int processIntent() {
Yorke Lee33501632014-03-17 19:24:12 -0700193 Log.v(this, "Processing call intent in OutgoingCallIntentBroadcaster.");
194
Yorke Lee33501632014-03-17 19:24:12 -0700195 Intent intent = mIntent;
Nancy Chen308ab8b2014-09-02 16:18:30 -0700196 String action = intent.getAction();
197 final Uri handle = intent.getData();
Yorke Lee33501632014-03-17 19:24:12 -0700198
Nancy Chen308ab8b2014-09-02 16:18:30 -0700199 if (handle == null) {
200 Log.w(this, "Empty handle obtained from the call intent.");
201 return DisconnectCause.INVALID_NUMBER;
202 }
Yorke Lee33501632014-03-17 19:24:12 -0700203
Jay Shrauner56a76b72014-09-05 14:41:48 -0700204 boolean isVoicemailNumber = PhoneAccount.SCHEME_VOICEMAIL.equals(handle.getScheme());
Nancy Chen308ab8b2014-09-02 16:18:30 -0700205 if (isVoicemailNumber) {
Yorke Leefe1ce0a2014-11-20 08:59:46 -0800206 if (Intent.ACTION_CALL.equals(action)
207 || Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700208 // 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 Cordon5f048fe2016-04-04 17:47:20 -0700212 // Since we are not going to go through "Outgoing call broadcast", make sure
213 // we mark it as ready.
214 mCall.setNewOutgoingCallIntentBroadcastIsDone();
215
Nancy Chen308ab8b2014-09-02 16:18:30 -0700216 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700217 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700218 mCallsManager.placeOutgoingCall(mCall, handle, null, speakerphoneOn,
Tyler Gunn5b882492015-06-03 10:03:13 -0700219 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700220
221 return DisconnectCause.NOT_DISCONNECTED;
Yorke Leed7255872014-08-25 15:03:51 -0700222 } else {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700223 Log.i(this, "Unhandled intent %s. Ignoring and not placing call.", intent);
224 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leed7255872014-08-25 15:03:51 -0700225 }
Yorke Lee33501632014-03-17 19:24:12 -0700226 }
227
Hall Liu220b4192015-12-11 11:33:08 -0800228 String number = mPhoneNumberUtilsAdapter.getNumberFromIntent(intent, mContext);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700229 if (TextUtils.isEmpty(number)) {
230 Log.w(this, "Empty number obtained from the call intent.");
231 return DisconnectCause.NO_PHONE_NUMBER_SUPPLIED;
Yorke Lee33501632014-03-17 19:24:12 -0700232 }
233
Hall Liu220b4192015-12-11 11:33:08 -0800234 boolean isUriNumber = mPhoneNumberUtilsAdapter.isUriNumber(number);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700235 if (!isUriNumber) {
Hall Liu220b4192015-12-11 11:33:08 -0800236 number = mPhoneNumberUtilsAdapter.convertKeypadLettersToDigits(number);
237 number = mPhoneNumberUtilsAdapter.stripSeparators(number);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700238 }
239
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700240 final boolean isPotentialEmergencyNumber = isPotentialEmergencyNumber(number);
Yorke Lee33501632014-03-17 19:24:12 -0700241 Log.v(this, "isPotentialEmergencyNumber = %s", isPotentialEmergencyNumber);
242
243 rewriteCallIntentAction(intent, isPotentialEmergencyNumber);
Yorke Leee05257c2014-09-08 18:08:44 -0700244 action = intent.getAction();
Yorke Lee33501632014-03-17 19:24:12 -0700245 // 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 Lee33501632014-03-17 19:24:12 -0700249 if (Intent.ACTION_CALL.equals(action)) {
250 if (isPotentialEmergencyNumber) {
Yorke Leecce5deb2014-06-18 11:27:42 -0700251 if (!mIsDefaultOrSystemPhoneApp) {
252 Log.w(this, "Cannot call potential emergency number %s with CALL Intent %s "
Nancy Chen308ab8b2014-09-02 16:18:30 -0700253 + "unless caller is system or default dialer.", number, intent);
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700254 launchSystemDialer(intent.getData());
Yorke Leed7255872014-08-25 15:03:51 -0700255 return DisconnectCause.OUTGOING_CANCELED;
Yorke Leecce5deb2014-06-18 11:27:42 -0700256 } else {
257 callImmediately = true;
258 }
Yorke Lee33501632014-03-17 19:24:12 -0700259 }
Yorke Lee33501632014-03-17 19:24:12 -0700260 } 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 Chen308ab8b2014-09-02 16:18:30 -0700263 + "Intent %s.", number, intent);
Yorke Leed7255872014-08-25 15:03:51 -0700264 return DisconnectCause.OUTGOING_CANCELED;
Yorke Lee33501632014-03-17 19:24:12 -0700265 }
266 callImmediately = true;
267 } else {
268 Log.w(this, "Unhandled Intent %s. Ignoring and not placing call.", intent);
Yorke Leed7255872014-08-25 15:03:51 -0700269 return DisconnectCause.INVALID_NUMBER;
Yorke Lee33501632014-03-17 19:24:12 -0700270 }
271
272 if (callImmediately) {
273 Log.i(this, "Placing call immediately instead of waiting for "
274 + " OutgoingCallBroadcastReceiver: %s", intent);
Jay Shrauner56a76b72014-09-05 14:41:48 -0700275 String scheme = isUriNumber ? PhoneAccount.SCHEME_SIP : PhoneAccount.SCHEME_TEL;
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700276 boolean speakerphoneOn = mIntent.getBooleanExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700277 TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false);
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700278 int videoState = mIntent.getIntExtra(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700279 TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Tyler Gunn5b882492015-06-03 10:03:13 -0700280 VideoProfile.STATE_AUDIO_ONLY);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700281 mCallsManager.placeOutgoingCall(mCall, Uri.fromParts(scheme, number, null), null,
Nancy Chen0d3076c2014-07-30 14:45:44 -0700282 speakerphoneOn, videoState);
Yorke Lee33501632014-03-17 19:24:12 -0700283
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 Makd3bacb72016-02-03 15:35:27 +0000290 UserHandle targetUser = mCall.getInitiatingUser();
291 Log.i(this, "Sending NewOutgoingCallBroadcast for %s to %s", mCall, targetUser);
292 broadcastIntent(intent, number, !callImmediately, targetUser);
Yorke Leed7255872014-08-25 15:03:51 -0700293 return DisconnectCause.NOT_DISCONNECTED;
Yorke Lee33501632014-03-17 19:24:12 -0700294 }
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 Chen308ab8b2014-09-02 16:18:30 -0700301 * @param number Call number that was stored in the original call intent.
Yorke Lee33501632014-03-17 19:24:12 -0700302 * @param receiverRequired Whether or not the result from the ordered broadcast should be
Tony Makd3bacb72016-02-03 15:35:27 +0000303 * processed using a {@link NewOutgoingCallIntentBroadcaster}.
304 * @param targetUser User that the broadcast sent to.
Yorke Lee33501632014-03-17 19:24:12 -0700305 */
306 private void broadcastIntent(
307 Intent originalCallIntent,
Nancy Chen308ab8b2014-09-02 16:18:30 -0700308 String number,
Tony Makd3bacb72016-02-03 15:35:27 +0000309 boolean receiverRequired,
310 UserHandle targetUser) {
Yorke Lee33501632014-03-17 19:24:12 -0700311 Intent broadcastIntent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
Nancy Chen308ab8b2014-09-02 16:18:30 -0700312 if (number != null) {
313 broadcastIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
Yorke Lee33501632014-03-17 19:24:12 -0700314 }
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 Tate47c73e92017-03-28 18:04:29 -0700318 broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND
319 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Yorke Lee33501632014-03-17 19:24:12 -0700320 Log.v(this, "Broadcasting intent: %s.", broadcastIntent);
321
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700322 checkAndCopyProviderExtras(originalCallIntent, broadcastIntent);
Yorke Lee33501632014-03-17 19:24:12 -0700323
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700324 mContext.sendOrderedBroadcastAsUser(
Yorke Lee33501632014-03-17 19:24:12 -0700325 broadcastIntent,
Tony Makd3bacb72016-02-03 15:35:27 +0000326 targetUser,
Svetoslav973c4e12015-04-29 14:13:51 -0700327 android.Manifest.permission.PROCESS_OUTGOING_CALLS,
328 AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
Yorke Lee33501632014-03-17 19:24:12 -0700329 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
330 null, // scheduler
331 Activity.RESULT_OK, // initialCode
Nancy Chen308ab8b2014-09-02 16:18:30 -0700332 number, // initialData: initial value for the result data (number to be modified)
Yorke Lee33501632014-03-17 19:24:12 -0700333 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 Chen77d2d0e2014-06-24 12:06:03 -0700343 public void checkAndCopyProviderExtras(Intent src, Intent dst) {
344 if (src == null) {
345 return;
346 }
Yorke Lee33501632014-03-17 19:24:12 -0700347 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 Chen77d2d0e2014-06-24 12:06:03 -0700356 Log.d(this, "No provider extras found in call intent.");
Yorke Lee33501632014-03-17 19:24:12 -0700357 }
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 Lee33501632014-03-17 19:24:12 -0700366 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 Gunn91d43cf2014-09-17 12:19:39 -0700399 private void launchSystemDialer(Uri handle) {
Yorke Lee33501632014-03-17 19:24:12 -0700400 Intent systemDialerIntent = new Intent();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700401 final Resources resources = mContext.getResources();
Yorke Lee33501632014-03-17 19:24:12 -0700402 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 Lee39d94c12014-10-08 17:21:28 -0700409 mContext.startActivityAsUser(systemDialerIntent, UserHandle.CURRENT);
Yorke Lee33501632014-03-17 19:24:12 -0700410 }
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 Chen308ab8b2014-09-02 16:18:30 -0700422 * @param number number to inspect in order to determine whether or not an emergency number
Yorke Lee33501632014-03-17 19:24:12 -0700423 * is potentially being dialed
424 * @return True if the handle is potentially an emergency number.
425 */
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700426 private boolean isPotentialEmergencyNumber(String number) {
Nancy Chen308ab8b2014-09-02 16:18:30 -0700427 Log.v(this, "Checking restrictions for number : %s", Log.pii(number));
Hall Liu220b4192015-12-11 11:33:08 -0800428 return (number != null)
429 && mPhoneNumberUtilsAdapter.isPotentialLocalEmergencyNumber(mContext, number);
Yorke Lee33501632014-03-17 19:24:12 -0700430 }
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 Chen308ab8b2014-09-02 16:18:30 -0700437 * @param isPotentialEmergencyNumber Whether or not the number is potentially an emergency
Yorke Lee33501632014-03-17 19:24:12 -0700438 * number.
439 */
440 private void rewriteCallIntentAction(Intent intent, boolean isPotentialEmergencyNumber) {
Yorke Lee33501632014-03-17 19:24:12 -0700441 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 Liu63e690c2017-02-14 18:05:03 -0800456
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 Lee33501632014-03-17 19:24:12 -0700470}